@coursebuilder/core
Version:
Core package for Course Builder
126 lines (124 loc) • 3.38 kB
JavaScript
import {
__name,
__publicField
} from "./chunk-VLQXSCFN.js";
// src/providers/openai.ts
import { createOpenAI } from "@ai-sdk/openai";
import { streamText } from "ai";
var STREAM_COMPLETE = `\\ok`;
var _a;
var PartyKitChunkPublisher = (_a = class {
constructor(requestId, partyUrlBase) {
__publicField(this, "requestId");
__publicField(this, "interval", 250);
__publicField(this, "buffer");
__publicField(this, "partyUrl");
this.requestId = requestId;
this.buffer = {
contents: ""
};
this.partyUrl = `${partyUrlBase}/party/${requestId}`;
}
async publishMessage(message) {
await this.sendToPartyKit(message, this.requestId, this.partyUrl);
}
async appendToBufferAndPublish(text) {
let resolve = /* @__PURE__ */ __name((_val) => {
}, "resolve");
this.buffer.contents += text;
if (this.buffer.signal) {
return;
}
this.buffer.signal = new Promise((r) => {
resolve = r;
});
setTimeout(() => {
if (this.buffer.contents.length === 0) {
resolve();
return;
}
this.sendToPartyKit(this.buffer.contents, this.requestId, this.partyUrl);
resolve();
this.buffer = {
contents: ""
};
}, this.interval);
}
async waitForBuffer() {
await this.buffer.signal;
}
async sendToPartyKit(body, requestId, partyUrl) {
return await fetch(partyUrl, {
method: "POST",
body: JSON.stringify({
body,
requestId,
name: "ai.message"
})
}).catch((e) => {
console.error("Failed to send chunk to PartyKit:", e);
});
}
}, __name(_a, "PartyKitChunkPublisher"), _a);
function OpenAIProvider(options) {
const client = createOpenAI({
apiKey: options.apiKey,
...options.baseUrl && {
baseURL: options.baseUrl
}
});
return {
id: "openai",
name: "OpenAI",
type: "llm",
options,
...options,
createChatCompletion: async (createChatOptions) => {
try {
const modelName = createChatOptions.model || options.defaultModel || "gpt-4o";
const publisher = new PartyKitChunkPublisher(createChatOptions.chatId, options.partyUrlBase);
const result = await streamText({
model: client(modelName),
messages: createChatOptions.messages,
onChunk: async ({ chunk }) => {
if (chunk.type === "text-delta") {
await publisher.appendToBufferAndPublish(chunk.textDelta);
}
}
});
let fullText = "";
for await (const textPart of result.textStream) {
fullText += textPart;
}
await publisher.waitForBuffer();
await publisher.publishMessage(STREAM_COMPLETE);
return {
role: "assistant",
content: fullText
};
} catch (error) {
console.error("OpenAI streaming error:", error);
throw error;
}
}
};
}
__name(OpenAIProvider, "OpenAIProvider");
var MockOpenAIProvider = {
id: "mock-openai",
name: "Mock OpenAI",
type: "llm",
options: {
apiKey: "mock-api-key",
partyUrlBase: "mock-callback-url"
},
apiKey: "mock-api-key",
partyUrlBase: "mock-callback-url",
createChatCompletion: () => Promise.resolve(null)
};
export {
STREAM_COMPLETE,
OpenAIProvider,
MockOpenAIProvider
};
//# sourceMappingURL=chunk-NCTNSVHT.js.map