fix(core): update
This commit is contained in:
88
ts/classes.conversation.ts
Normal file
88
ts/classes.conversation.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import type { SmartAi } from "./classes.smartai.js";
|
||||
import { OpenAiProvider } from "./provider.openai.js";
|
||||
|
||||
type TProcessFunction = (input: string) => Promise<string>;
|
||||
|
||||
export interface IConversationOptions {
|
||||
processFunction: TProcessFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* a conversation
|
||||
*/
|
||||
export class Conversation {
|
||||
// STATIC
|
||||
public static async createWithOpenAi(smartaiRef: SmartAi) {
|
||||
const openaiProvider = new OpenAiProvider(smartaiRef.options.openaiToken);
|
||||
const conversation = new Conversation(smartaiRef, {
|
||||
processFunction: async (input) => {
|
||||
return '' // TODO implement proper streaming
|
||||
}
|
||||
});
|
||||
return conversation;
|
||||
}
|
||||
|
||||
public static async createWithAnthropic(smartaiRef: SmartAi) {
|
||||
const anthropicProvider = new OpenAiProvider(smartaiRef.options.anthropicToken);
|
||||
const conversation = new Conversation(smartaiRef, {
|
||||
processFunction: async (input) => {
|
||||
return '' // TODO implement proper streaming
|
||||
}
|
||||
});
|
||||
return conversation;
|
||||
}
|
||||
|
||||
|
||||
// INSTANCE
|
||||
smartaiRef: SmartAi
|
||||
private systemMessage: string;
|
||||
private processFunction: TProcessFunction;
|
||||
private inputStreamWriter: WritableStreamDefaultWriter<string> | null = null;
|
||||
private outputStreamController: ReadableStreamDefaultController<string> | null = null;
|
||||
|
||||
constructor(smartairefArg: SmartAi, options: IConversationOptions) {
|
||||
this.processFunction = options.processFunction;
|
||||
}
|
||||
|
||||
setSystemMessage(systemMessage: string) {
|
||||
this.systemMessage = systemMessage;
|
||||
}
|
||||
|
||||
private setupOutputStream(): ReadableStream<string> {
|
||||
return new ReadableStream<string>({
|
||||
start: (controller) => {
|
||||
this.outputStreamController = controller;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private setupInputStream(): WritableStream<string> {
|
||||
return new WritableStream<string>({
|
||||
write: async (chunk) => {
|
||||
const processedData = await this.processFunction(chunk);
|
||||
if (this.outputStreamController) {
|
||||
this.outputStreamController.enqueue(processedData);
|
||||
}
|
||||
},
|
||||
close: () => {
|
||||
this.outputStreamController?.close();
|
||||
},
|
||||
abort: (err) => {
|
||||
console.error('Stream aborted', err);
|
||||
this.outputStreamController?.error(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public getInputStreamWriter(): WritableStreamDefaultWriter<string> {
|
||||
if (!this.inputStreamWriter) {
|
||||
const inputStream = this.setupInputStream();
|
||||
this.inputStreamWriter = inputStream.getWriter();
|
||||
}
|
||||
return this.inputStreamWriter;
|
||||
}
|
||||
|
||||
public getOutputStream(): ReadableStream<string> {
|
||||
return this.setupOutputStream();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user