From 0b56a801eb4b352baf7a7459eef66e2df5b6668e Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sat, 30 Mar 2024 12:45:53 +0100 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/smartai.classes.smartai.ts | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index d3e3448..a0c8a5b 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartai', - version: '0.0.2', + version: '0.0.3', description: 'a standardaized interface to talk to AI models' } diff --git a/ts/smartai.classes.smartai.ts b/ts/smartai.classes.smartai.ts index e69de29..992ea9b 100644 --- a/ts/smartai.classes.smartai.ts +++ b/ts/smartai.classes.smartai.ts @@ -0,0 +1,53 @@ +type TProcessFunction = (input: string) => Promise; + +interface ISmartAiOptions { + processFunction: TProcessFunction; +} + +class SmartAi { + private processFunction: TProcessFunction; + private inputStreamWriter: WritableStreamDefaultWriter | null = null; + private outputStreamController: ReadableStreamDefaultController | null = null; + + constructor(options: ISmartAiOptions) { + this.processFunction = options.processFunction; + } + + private setupOutputStream(): ReadableStream { + return new ReadableStream({ + start: (controller) => { + this.outputStreamController = controller; + } + }); + } + + private setupInputStream(): WritableStream { + return new WritableStream({ + 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 { + if (!this.inputStreamWriter) { + const inputStream = this.setupInputStream(); + this.inputStreamWriter = inputStream.getWriter(); + } + return this.inputStreamWriter; + } + + public getOutputStream(): ReadableStream { + return this.setupOutputStream(); + } +}