@assistant-ui/react
Version:
React components for AI chat.
69 lines • 2.42 kB
JavaScript
// src/runtimes/edge/EdgeChatAdapter.ts
import { toCoreMessages } from "./converters/toCoreMessages.mjs";
import { toLanguageModelTools } from "./converters/toLanguageModelTools.mjs";
import { assistantDecoderStream } from "./streams/assistantDecoderStream.mjs";
import { streamPartDecoderStream } from "./streams/utils/streamPartDecoderStream.mjs";
import { runResultStream } from "./streams/runResultStream.mjs";
import { toolResultStream } from "./streams/toolResultStream.mjs";
import { toLanguageModelMessages } from "./converters/index.mjs";
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 ? toLanguageModelMessages(
messages
) : toCoreMessages(messages, {
unstable_includeId: this.options.unstable_sendMessageIds
}),
tools: config.tools ? 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(streamPartDecoderStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools, abortSignal)).pipeThrough(runResultStream());
let update;
for await (update of asAsyncIterable(stream)) {
yield update;
}
if (update === void 0)
throw new Error("No data received from Edge Runtime");
}
};
export {
EdgeChatAdapter,
asAsyncIterable
};
//# sourceMappingURL=EdgeChatAdapter.mjs.map