|
| 1 | +import { TestWorkflowEnvironment } from '@temporalio/testing'; |
| 2 | +import { after, before, describe, it } from 'mocha'; |
| 3 | +import { Worker } from '@temporalio/worker'; |
| 4 | +import { WorkflowStreamClient } from '@temporalio/workflow-streams/client'; |
| 5 | +import type { |
| 6 | + LanguageModelV4, |
| 7 | + LanguageModelV4CallOptions, |
| 8 | + LanguageModelV4GenerateResult, |
| 9 | + LanguageModelV4StreamPart, |
| 10 | + LanguageModelV4StreamResult, |
| 11 | + ProviderV4, |
| 12 | +} from '@ai-sdk/provider'; |
| 13 | +import assert from 'assert'; |
| 14 | +import { streamingAgent, STREAM_TOPIC, consumerDoneSignal } from '../workflows'; |
| 15 | +import * as activities from '../activities'; |
| 16 | +import { AiSdkPlugin } from '@temporalio/ai-sdk'; |
| 17 | + |
| 18 | +// A deterministic, offline model that streams a fixed set of text deltas so |
| 19 | +// these tests need no OPENAI_API_KEY and can assert on exact output. |
| 20 | +class MockStreamModel implements LanguageModelV4 { |
| 21 | + readonly specificationVersion = 'v4'; |
| 22 | + readonly provider = 'mock'; |
| 23 | + readonly modelId = 'mock-model'; |
| 24 | + private readonly chunks: string[]; |
| 25 | + |
| 26 | + constructor(chunks: string[]) { |
| 27 | + this.chunks = chunks; |
| 28 | + } |
| 29 | + |
| 30 | + get supportedUrls(): Record<string, RegExp[]> { |
| 31 | + return {}; |
| 32 | + } |
| 33 | + |
| 34 | + doGenerate(_options: LanguageModelV4CallOptions): Promise<LanguageModelV4GenerateResult> { |
| 35 | + throw new Error('generate not supported by mock'); |
| 36 | + } |
| 37 | + |
| 38 | + doStream(_options: LanguageModelV4CallOptions): Promise<LanguageModelV4StreamResult> { |
| 39 | + const chunks = this.chunks; |
| 40 | + const parts: LanguageModelV4StreamPart[] = [ |
| 41 | + { type: 'stream-start', warnings: [] }, |
| 42 | + { type: 'text-start', id: 't1' }, |
| 43 | + ...chunks.map((delta): LanguageModelV4StreamPart => ({ type: 'text-delta', id: 't1', delta })), |
| 44 | + { type: 'text-end', id: 't1' }, |
| 45 | + { |
| 46 | + type: 'finish', |
| 47 | + finishReason: { unified: 'stop', raw: undefined }, |
| 48 | + usage: { |
| 49 | + inputTokens: { total: 1, noCache: undefined, cacheRead: undefined, cacheWrite: undefined }, |
| 50 | + outputTokens: { total: chunks.length, text: undefined, reasoning: undefined }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + ]; |
| 54 | + return Promise.resolve({ |
| 55 | + stream: new ReadableStream<LanguageModelV4StreamPart>({ |
| 56 | + start(controller) { |
| 57 | + for (const part of parts) controller.enqueue(part); |
| 58 | + controller.close(); |
| 59 | + }, |
| 60 | + }), |
| 61 | + request: {}, |
| 62 | + response: {}, |
| 63 | + }); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +function mockProvider(chunks: string[]): ProviderV4 { |
| 68 | + return { |
| 69 | + specificationVersion: 'v4', |
| 70 | + languageModel: () => new MockStreamModel(chunks), |
| 71 | + embeddingModel: () => { |
| 72 | + throw new Error('not implemented'); |
| 73 | + }, |
| 74 | + imageModel: () => { |
| 75 | + throw new Error('not implemented'); |
| 76 | + }, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +// Collect the live deltas an external subscriber sees on a topic. |
| 81 | +async function collectDeltas(client: any, workflowId: string, topic: string): Promise<string[]> { |
| 82 | + const deltas: string[] = []; |
| 83 | + const streamClient = WorkflowStreamClient.create(client, workflowId); |
| 84 | + for await (const item of streamClient.subscribe<Uint8Array>(topic, 0, { resultType: true })) { |
| 85 | + const part = JSON.parse(new TextDecoder().decode(item.data)); |
| 86 | + if (part.type === 'text-delta') deltas.push(part.delta); |
| 87 | + if (part.type === 'finish') break; |
| 88 | + } |
| 89 | + // Mirror the real consumer: acknowledge receipt so the workflow can complete. |
| 90 | + await client.workflow.getHandle(workflowId).signal(consumerDoneSignal); |
| 91 | + return deltas; |
| 92 | +} |
| 93 | + |
| 94 | +describe('streaming agents', function () { |
| 95 | + this.timeout(30_000); |
| 96 | + |
| 97 | + let testEnv: TestWorkflowEnvironment; |
| 98 | + |
| 99 | + before(async () => { |
| 100 | + testEnv = await TestWorkflowEnvironment.createLocal(); |
| 101 | + }); |
| 102 | + |
| 103 | + after(async () => { |
| 104 | + await testEnv?.teardown(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('streamingAgent publishes live deltas and returns the full text', async () => { |
| 108 | + const { client, nativeConnection } = testEnv; |
| 109 | + const taskQueue = 'test-stream-text'; |
| 110 | + const chunks = ['Dur', 'able ', 'streams ', 'of ', 'thought']; |
| 111 | + |
| 112 | + const worker = await Worker.create({ |
| 113 | + connection: nativeConnection, |
| 114 | + plugins: [new AiSdkPlugin({ modelProvider: mockProvider(chunks) })], |
| 115 | + taskQueue, |
| 116 | + workflowsPath: require.resolve('../workflows'), |
| 117 | + activities, |
| 118 | + }); |
| 119 | + |
| 120 | + await worker.runUntil(async () => { |
| 121 | + const handle = await client.workflow.start(streamingAgent, { |
| 122 | + args: ['Temporal'], |
| 123 | + workflowId: 'test-stream-text-' + Date.now(), |
| 124 | + taskQueue, |
| 125 | + }); |
| 126 | + |
| 127 | + const deltasPromise = collectDeltas(client, handle.workflowId, STREAM_TOPIC); |
| 128 | + const result = await handle.result(); |
| 129 | + const deltas = await deltasPromise; |
| 130 | + |
| 131 | + // The workflow durably reassembles the full text from the replayed stream. |
| 132 | + assert.strictEqual(result, chunks.join('')); |
| 133 | + // The external subscriber saw the response arrive incrementally. |
| 134 | + assert.ok(deltas.length > 1, `expected multiple live deltas, got ${deltas.length}`); |
| 135 | + assert.strictEqual(deltas.join(''), chunks.join('')); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments