UNPKG

@assistant-ui/react

Version:

React components for AI chat.

95 lines (94 loc) 3.75 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/runtimes/edge/EdgeChatAdapter.ts var EdgeChatAdapter_exports = {}; __export(EdgeChatAdapter_exports, { EdgeChatAdapter: () => EdgeChatAdapter, asAsyncIterable: () => asAsyncIterable }); module.exports = __toCommonJS(EdgeChatAdapter_exports); var import_toCoreMessages = require("./converters/toCoreMessages.cjs"); var import_toLanguageModelTools = require("./converters/toLanguageModelTools.cjs"); var import_assistantDecoderStream = require("./streams/assistantDecoderStream.cjs"); var import_streamPartDecoderStream = require("./streams/utils/streamPartDecoderStream.cjs"); var import_runResultStream = require("./streams/runResultStream.cjs"); var import_toolResultStream = require("./streams/toolResultStream.cjs"); var import_converters = require("./converters/index.cjs"); function asAsyncIterable(source) { return { [Symbol.asyncIterator]: () => { const reader = source.getReader(); return { async next() { const { done, value } = await reader.read(); return done ? { done: true, value: void 0 } : { done: false, value }; } }; } }; } var EdgeChatAdapter = class { constructor(options) { this.options = options; } async *run({ messages, abortSignal, config, unstable_assistantMessageId }) { const headers = new Headers(this.options.headers); headers.set("Content-Type", "application/json"); const result = await fetch(this.options.api, { method: "POST", headers, credentials: this.options.credentials ?? "same-origin", body: JSON.stringify({ system: config.system, messages: this.options.unstable_AISDKInterop ? (0, import_converters.toLanguageModelMessages)( messages ) : (0, import_toCoreMessages.toCoreMessages)(messages, { unstable_includeId: this.options.unstable_sendMessageIds }), tools: config.tools ? (0, import_toLanguageModelTools.toLanguageModelTools)(config.tools) : [], unstable_assistantMessageId, ...config.callSettings, ...config.config, ...this.options.body }), signal: abortSignal }); if (!result.ok) { throw new Error(`Status ${result.status}: ${await result.text()}`); } const stream = result.body.pipeThrough((0, import_streamPartDecoderStream.streamPartDecoderStream)()).pipeThrough((0, import_assistantDecoderStream.assistantDecoderStream)()).pipeThrough((0, import_toolResultStream.toolResultStream)(config.tools, abortSignal)).pipeThrough((0, import_runResultStream.runResultStream)()); let update; for await (update of asAsyncIterable(stream)) { yield update; } if (update === void 0) throw new Error("No data received from Edge Runtime"); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { EdgeChatAdapter, asAsyncIterable }); //# sourceMappingURL=EdgeChatAdapter.js.map