-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simplified graph event streaming: an async generator, and an SSE …
…implementation
- Loading branch information
Showing
14 changed files
with
455 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { it, describe } from 'node:test'; | ||
import { strict as assert } from 'node:assert'; | ||
import { loadTestGraphInProcessor, testProcessContext } from '../testUtils'; | ||
|
||
describe('GraphProcessor', () => { | ||
it('Can run passthrough graph', async () => { | ||
const processor = await loadTestGraphInProcessor('Passthrough'); | ||
|
||
const outputs = await processor.processGraph(testProcessContext(), { | ||
input: { | ||
type: 'string', | ||
value: 'input value', | ||
}, | ||
}); | ||
|
||
assert.deepEqual(outputs.output, { | ||
type: 'string', | ||
value: 'input value', | ||
}); | ||
}); | ||
|
||
it('Can stream graph processor events', async () => { | ||
const processor = await loadTestGraphInProcessor('Passthrough'); | ||
|
||
processor.processGraph(testProcessContext(), { | ||
input: { | ||
type: 'string', | ||
value: 'input value', | ||
}, | ||
}); | ||
|
||
const eventNames: string[] = []; | ||
for await (const event of processor.events()) { | ||
if (event.type !== 'trace') { | ||
eventNames.push(event.type); | ||
} | ||
} | ||
|
||
assert.equal(eventNames[eventNames.length - 2], 'done'); | ||
assert.equal(eventNames[eventNames.length - 1], 'finish'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
version: 4 | ||
data: | ||
attachedData: | ||
trivet: | ||
testSuites: [] | ||
version: 1 | ||
graphs: | ||
kqaNrBo0WpJ1EOc2hj0zK: | ||
metadata: | ||
description: "" | ||
id: kqaNrBo0WpJ1EOc2hj0zK | ||
name: Passthrough | ||
nodes: | ||
'[Dp5_0MQuZk7_UTdBQGX-P]:graphOutput "Graph Output"': | ||
data: | ||
dataType: string | ||
id: output | ||
visualData: 1185/525/330/10// | ||
'[hHAeA3eIMmdfGFOYeool0]:passthrough "Passthrough"': | ||
outgoingConnections: | ||
- output1->"Graph Output" Dp5_0MQuZk7_UTdBQGX-P/value | ||
visualData: 928/554/205/9// | ||
'[pbV0xrVlBYe1I3Mew3CIT]:graphInput "Graph Input"': | ||
data: | ||
dataType: string | ||
id: input | ||
useDefaultValueInput: false | ||
outgoingConnections: | ||
- data->"Passthrough" hHAeA3eIMmdfGFOYeool0/input1 | ||
visualData: 549/516/330/11// | ||
metadata: | ||
description: "" | ||
id: ytCHmBvDFSkCnQ9L7DJLB | ||
title: Untitled Project | ||
plugins: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { readFile } from 'node:fs/promises'; | ||
import { deserializeProject, GraphProcessor, type ProcessContext, type Project } from '../src/index.js'; | ||
import { dirname, join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
const testDir = dirname(fileURLToPath(import.meta.url)); | ||
|
||
export async function loadTestGraphs(): Promise<Project> { | ||
return loadProjectFromFile(join(testDir, './test-graphs.rivet-project')); | ||
} | ||
|
||
export async function loadTestGraphInProcessor(graphName: string) { | ||
const project = await loadTestGraphs(); | ||
const graph = Object.values(project.graphs).find((g) => g.metadata!.name === graphName); | ||
|
||
if (!graph) { | ||
throw new Error(`Could not find graph with name ${graphName}`); | ||
} | ||
|
||
return new GraphProcessor(project, graph.metadata!.id!); | ||
} | ||
|
||
export async function loadProjectFromFile(path: string): Promise<Project> { | ||
const content = await readFile(path, { encoding: 'utf8' }); | ||
return loadProjectFromString(content); | ||
} | ||
|
||
export function loadProjectFromString(content: string): Project { | ||
const [project] = deserializeProject(content); | ||
return project; | ||
} | ||
|
||
export function testProcessContext(): ProcessContext { | ||
return { | ||
settings: { | ||
openAiKey: process.env.OPENAI_API_KEY, | ||
openAiOrganization: process.env.OPENAI_ORG_ID, | ||
openAiEndpoint: process.env.OPENAI_API_ENDPOINT, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src", "test"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
module.exports = { | ||
"extends": "../../.eslintrc.cjs", | ||
"root": true, | ||
"overrides": [{ | ||
"files": ["*.ts", "*.tsx"], | ||
"parserOptions": { | ||
"project": true, | ||
"ecmaVersion": "latest", | ||
"sourceType": "module", | ||
} | ||
}] | ||
} | ||
extends: '../../.eslintrc.cjs', | ||
root: true, | ||
overrides: [ | ||
{ | ||
files: ['*.ts', '*.tsx'], | ||
parserOptions: { | ||
project: './packages/node/tsconfig.eslint.json', | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.