@assistant-ui/react
Version:
Typescript/React library for AI Chat
127 lines (126 loc) • 5.15 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/runtimes/edge/EdgeModelAdapter.ts
var EdgeModelAdapter_exports = {};
__export(EdgeModelAdapter_exports, {
EdgeModelAdapter: () => EdgeModelAdapter,
asAsyncIterable: () => asAsyncIterable
});
module.exports = __toCommonJS(EdgeModelAdapter_exports);
var import_toCoreMessages = require("./converters/toCoreMessages.js");
var import_toLanguageModelTools = require("./converters/toLanguageModelTools.js");
var import_toolResultStream = require("./streams/toolResultStream.js");
var import_converters = require("./converters/index.js");
var import_zod = require("zod");
var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
var import_assistant_stream = require("assistant-stream");
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 toAISDKTools = (tools) => {
return Object.fromEntries(
Object.entries(tools).map(([name, tool]) => [
name,
{
...tool.description ? { description: tool.description } : void 0,
parameters: tool.parameters instanceof import_zod.z.ZodType ? (0, import_zod_to_json_schema.default)(tool.parameters) : tool.parameters
}
])
);
};
var EdgeModelAdapter = class {
constructor(options) {
this.options = options;
}
async *run({
messages,
runConfig,
abortSignal,
context,
unstable_assistantMessageId,
unstable_getMessage
}) {
const headersValue = typeof this.options.headers === "function" ? await this.options.headers() : this.options.headers;
const headers = new Headers(headersValue);
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: context.system,
messages: this.options.unstable_AISDKInterop ? (0, import_converters.toLanguageModelMessages)(messages, {
unstable_includeId: this.options.unstable_sendMessageIds || this.options.sendExtraMessageFields
}) : (0, import_toCoreMessages.toCoreMessages)(messages, {
unstable_includeId: this.options.unstable_sendMessageIds || this.options.sendExtraMessageFields
}),
tools: context.tools ? this.options.unstable_AISDKInterop === "v2" ? toAISDKTools(context.tools) : (0, import_toLanguageModelTools.toLanguageModelTools)(context.tools) : [],
unstable_assistantMessageId,
runConfig,
...context.callSettings,
...context.config,
...this.options.body
}),
signal: abortSignal
});
await this.options.onResponse?.(result);
try {
if (!result.ok) {
throw new Error(`Status ${result.status}: ${await result.text()}`);
}
if (!result.body) {
throw new Error("Response body is null");
}
const stream = result.body.pipeThrough(new import_assistant_stream.DataStreamDecoder()).pipeThrough((0, import_toolResultStream.toolResultStream)(context.tools, abortSignal)).pipeThrough(new import_assistant_stream.AssistantMessageAccumulator());
for await (const update of asAsyncIterable(stream)) {
yield update;
}
this.options.onFinish?.(unstable_getMessage());
} catch (error) {
this.options.onError?.(error);
throw error;
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
EdgeModelAdapter,
asAsyncIterable
});
//# sourceMappingURL=EdgeModelAdapter.js.map
;