Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
25db0618d6 | |||
89f9965112 | |||
a3002f3a83 | |||
ecefda5377 | |||
08728266b7 | |||
0b56a801eb |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartai",
|
"name": "@push.rocks/smartai",
|
||||||
"version": "0.0.2",
|
"version": "0.0.5",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a standardaized interface to talk to AI models",
|
"description": "a standardaized interface to talk to AI models",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartai',
|
name: '@push.rocks/smartai',
|
||||||
version: '0.0.2',
|
version: '0.0.5',
|
||||||
description: 'a standardaized interface to talk to AI models'
|
description: 'a standardaized interface to talk to AI models'
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
type TProcessFunction = (input: string) => Promise<string>;
|
||||||
|
|
||||||
|
interface ISmartAiOptions {
|
||||||
|
processFunction: TProcessFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
class SmartAi {
|
||||||
|
private processFunction: TProcessFunction;
|
||||||
|
private inputStreamWriter: WritableStreamDefaultWriter<string> | null = null;
|
||||||
|
private outputStreamController: ReadableStreamDefaultController<string> | null = null;
|
||||||
|
|
||||||
|
constructor(options: ISmartAiOptions) {
|
||||||
|
this.processFunction = options.processFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
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