diff --git a/config/default.json b/config/default.json index 1aabbcb..a15e7fd 100644 --- a/config/default.json +++ b/config/default.json @@ -1,7 +1,11 @@ { "file": "./example.rivet-project", "graphName": "chat-history", - "nodeType": "chat", - "nodeName": "Output (Chat)", - "graphInput": "input" -} \ No newline at end of file + "graphInputName": "input", + "streamingOutput": { + "nodeType": "chat", + "nodeName": "Output (Chat)" + }, + "returnGraphOutput": false, + "graphOutputName": "output" + } \ No newline at end of file diff --git a/dist/graphManager.js b/dist/graphManager.js index f333745..116ed42 100644 --- a/dist/graphManager.js +++ b/dist/graphManager.js @@ -1,7 +1,6 @@ import { startDebuggerServer, loadProjectFromString, createProcessor } from '@ironclad/rivet-node'; import config from 'config'; import fs from 'fs/promises'; -import path from 'path'; class GraphManager { constructor() { this.output = null; @@ -24,16 +23,7 @@ class GraphManager { this.startDebuggerServerIfNeeded(); const projectContent = await fs.readFile(config.get('file'), 'utf8'); const project = loadProjectFromString(projectContent); - const graphInput = config.get('graphInput'); - /* Start of adding the dataset provider */ - // Get the file path from the config - const filePath = config.get('file'); - // Get the directory and filename without extension - const directory = path.dirname(filePath); - const filenameWithoutExtension = path.basename(filePath, path.extname(filePath)); - // Construct the new file path - const newFilePath = path.join(directory, `${filenameWithoutExtension}.rivet-data`); - // Initialize the options object + const graphInput = config.get('graphInputName'); const options = { graph: config.get('graphName'), inputs: { @@ -50,7 +40,6 @@ class GraphManager { }; console.log('Creating processor'); // Do not fail application on error - let errorOccurred = false; try { const { processor, run } = createProcessor(project, options); const runPromise = run(); @@ -58,8 +47,8 @@ class GraphManager { let lastContent = ''; for await (const event of processor.events()) { if (event.type === 'partialOutput' && - event.node.type === config.get('nodeType') && - event.node.title === config.get('nodeName')) { + event.node.type === config.get('streamingOutput.nodeType') && + event.node.title === config.get('streamingOutput.nodeName')) { const content = event.outputs.response.value; this.output = content; // Update the output variable with the content if (content.startsWith(lastContent)) { @@ -70,12 +59,17 @@ class GraphManager { } } console.log('Finished processing events'); // Debugging line - await runPromise; + const finalOutputs = await runPromise; + // Also return the graph output if returnGraphOutput is configured as true + if (config.get('returnGraphOutput')) { + yield finalOutputs[config.get('graphOutputName')].value; + } this.isRunning = false; console.log('runGraph finished'); // Debugging line } catch (error) { console.error(error); + // Set isRunning to false to allow the next runGraph call to run this.isRunning = false; } } diff --git a/dist/graphManager.js.map b/dist/graphManager.js.map index c99187e..2d84ba5 100644 --- a/dist/graphManager.js.map +++ b/dist/graphManager.js.map @@ -1 +1 @@ -{"version":3,"file":"graphManager.js","sourceRoot":"","sources":["../src/graphManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,EAGlB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,YAAY;IAAlB;QACI,WAAM,GAAG,IAAI,CAAC;QACd,cAAS,GAAG,KAAK,CAAC;QAClB,mBAAc,GAAG,IAAI,CAAC;IAiG1B,CAAC;IA/FG,sCAAsC;IACtC,2BAA2B;QACvB,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE;YACtB,IAAI,CAAC,cAAc,GAAG,mBAAmB,CAAC,EAAE,CAAC,CAAC;SACjD;IACL,CAAC;IAED,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAgE;QAC5E,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,iBAAiB;QAEjD,IAAI,IAAI,CAAC,SAAS,EAAE;YAChB,OAAO,CAAC,GAAG,CAAC,mDAAmD,CAAC,CAAC,CAAC,iBAAiB;YACnF,OAAO;SACV;QAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,IAAI,CAAC,2BAA2B,EAAE,CAAC;QACnC,MAAM,cAAc,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,EAA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C,0BAA0B;qBACpD;iBACJ;aACJ;YAED,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,CAAC,iBAAiB;YAE5D,MAAM,YAAY,GAAG,MAAM,UAAU,CAAC;YACtC,0EAA0E;YAC1E,IAAG,MAAM,CAAC,GAAG,CAAC,mBAAmB,CAAC,EAAE;gBAChC,MAAM,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC;aAC3D;YACD,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;YAEvB,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC,CAAC,iBAAiB;SACtD;QAAC,OAAO,KAAK,EAAE;YACZ,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACrB,gEAAgE;YAChE,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;SAC1B;IACL,CAAC;IAED,SAAS;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;CACJ;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,YAAY,EAAE,CAAC"} \ No newline at end of file diff --git a/example.rivet-data b/example.rivet-data index fc794a9..d6b6e05 100644 --- a/example.rivet-data +++ b/example.rivet-data @@ -1 +1 @@ -{"datasets":[{"meta":{"id":"J-3PhzWvBMkJHEu6dQLX1","projectId":"vHpwmvjUYbE-MQJ7H_sV_","name":"New Dataset","description":""},"data":{"id":"J-3PhzWvBMkJHEu6dQLX1","rows":[]}}]} \ No newline at end of file +{"datasets":[]} \ No newline at end of file diff --git a/src/graphManager.ts b/src/graphManager.ts index 9161ca9..7a4ddb0 100644 --- a/src/graphManager.ts +++ b/src/graphManager.ts @@ -32,7 +32,7 @@ class GraphManager { this.startDebuggerServerIfNeeded(); const projectContent = await fs.readFile(config.get('file'), 'utf8'); const project = loadProjectFromString(projectContent); - const graphInput = config.get('graphInput') as string; + const graphInput = config.get('graphInputName') as string; const options = { graph: config.get('graphName'), @@ -65,8 +65,8 @@ class GraphManager { for await (const event of processor.events()) { if ( event.type === 'partialOutput' && - event.node.type === config.get('nodeType') && - event.node.title === config.get('nodeName') + event.node.type === config.get('streamingOutput.nodeType') && + event.node.title === config.get('streamingOutput.nodeName') ) { const content = (event.outputs as any).response.value; this.output = content; // Update the output variable with the content @@ -81,7 +81,11 @@ class GraphManager { console.log('Finished processing events'); // Debugging line - await runPromise; + const finalOutputs = await runPromise; + // Also return the graph output if returnGraphOutput is configured as true + if(config.get('returnGraphOutput')) { + yield finalOutputs[config.get('graphOutputName')].value; + } this.isRunning = false; console.log('runGraph finished'); // Debugging line @@ -91,7 +95,7 @@ class GraphManager { this.isRunning = false; } } - + getOutput() { return this.output; }