|
1 | 1 | import { demoStream } from "@/app/streams"; |
2 | | -import { logger, task } from "@trigger.dev/sdk"; |
| 2 | +import { logger, metadata, task } from "@trigger.dev/sdk"; |
3 | 3 | import { setTimeout } from "timers/promises"; |
4 | 4 |
|
5 | 5 | export type STREAMS = { |
@@ -46,8 +46,6 @@ export type StreamPayload = { |
46 | 46 | export const streamsTask = task({ |
47 | 47 | id: "streams", |
48 | 48 | run: async (payload: StreamPayload = {}, { ctx }) => { |
49 | | - await setTimeout(1000); |
50 | | - |
51 | 49 | const scenario = payload.scenario ?? "continuous"; |
52 | 50 | logger.info("Starting stream scenario", { scenario }); |
53 | 51 |
|
@@ -118,7 +116,89 @@ export const streamsTask = task({ |
118 | 116 |
|
119 | 117 | await waitUntilComplete(); |
120 | 118 |
|
121 | | - await streamsChildTask.triggerAndWait({}); |
| 119 | + logger.info("Stream completed", { scenario }); |
| 120 | + |
| 121 | + return { |
| 122 | + scenario, |
| 123 | + scenarioDescription, |
| 124 | + }; |
| 125 | + }, |
| 126 | +}); |
| 127 | + |
| 128 | +export const metadataStreamsTask = task({ |
| 129 | + id: "metadata-streams", |
| 130 | + run: async (payload: StreamPayload = {}, { ctx }) => { |
| 131 | + const scenario = payload.scenario ?? "continuous"; |
| 132 | + logger.info("Starting stream scenario", { scenario }); |
| 133 | + |
| 134 | + let generator: AsyncGenerator<string>; |
| 135 | + let scenarioDescription: string; |
| 136 | + |
| 137 | + switch (scenario) { |
| 138 | + case "stall": { |
| 139 | + const stallDurationMs = payload.stallDurationMs ?? 3 * 60 * 1000; // Default 3 minutes |
| 140 | + const includePing = payload.includePing ?? false; |
| 141 | + generator = generateLLMTokenStream(includePing, stallDurationMs); |
| 142 | + scenarioDescription = `Stall scenario: ${stallDurationMs / 1000}s with ${ |
| 143 | + includePing ? "ping tokens" : "no pings" |
| 144 | + }`; |
| 145 | + break; |
| 146 | + } |
| 147 | + case "continuous": { |
| 148 | + const durationSec = payload.durationSec ?? 45; |
| 149 | + const intervalMs = payload.intervalMs ?? 10; |
| 150 | + generator = generateContinuousTokenStream(durationSec, intervalMs); |
| 151 | + scenarioDescription = `Continuous scenario: ${durationSec}s with ${intervalMs}ms intervals`; |
| 152 | + break; |
| 153 | + } |
| 154 | + case "burst": { |
| 155 | + const burstCount = payload.burstCount ?? 10; |
| 156 | + const tokensPerBurst = payload.tokensPerBurst ?? 20; |
| 157 | + const burstIntervalMs = payload.burstIntervalMs ?? 5; |
| 158 | + const pauseBetweenBurstsMs = payload.pauseBetweenBurstsMs ?? 2000; |
| 159 | + generator = generateBurstTokenStream( |
| 160 | + burstCount, |
| 161 | + tokensPerBurst, |
| 162 | + burstIntervalMs, |
| 163 | + pauseBetweenBurstsMs |
| 164 | + ); |
| 165 | + scenarioDescription = `Burst scenario: ${burstCount} bursts of ${tokensPerBurst} tokens`; |
| 166 | + break; |
| 167 | + } |
| 168 | + case "slow-steady": { |
| 169 | + const durationMin = payload.durationMin ?? 5; |
| 170 | + const tokenIntervalSec = payload.tokenIntervalSec ?? 5; |
| 171 | + generator = generateSlowSteadyTokenStream(durationMin, tokenIntervalSec); |
| 172 | + scenarioDescription = `Slow steady scenario: ${durationMin}min with ${tokenIntervalSec}s intervals`; |
| 173 | + break; |
| 174 | + } |
| 175 | + case "markdown": { |
| 176 | + const tokenDelayMs = payload.tokenDelayMs ?? 15; |
| 177 | + generator = generateMarkdownTokenStream(tokenDelayMs); |
| 178 | + scenarioDescription = `Markdown scenario: generating formatted content with ${tokenDelayMs}ms delays`; |
| 179 | + break; |
| 180 | + } |
| 181 | + case "performance": { |
| 182 | + const chunkCount = payload.chunkCount ?? 500; |
| 183 | + const chunkIntervalMs = payload.chunkIntervalMs ?? 10; |
| 184 | + generator = generatePerformanceStream(chunkCount, chunkIntervalMs); |
| 185 | + scenarioDescription = `Performance scenario: ${chunkCount} chunks with ${chunkIntervalMs}ms intervals`; |
| 186 | + break; |
| 187 | + } |
| 188 | + default: { |
| 189 | + throw new Error(`Unknown scenario: ${scenario}`); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + logger.info("Starting stream", { scenarioDescription }); |
| 194 | + |
| 195 | + const mockStream = createStreamFromGenerator(generator); |
| 196 | + |
| 197 | + const stream = await metadata.stream("demo", mockStream); |
| 198 | + |
| 199 | + for await (const chunk of stream) { |
| 200 | + logger.info("Received chunk", { chunk }); |
| 201 | + } |
122 | 202 |
|
123 | 203 | logger.info("Stream completed", { scenario }); |
124 | 204 |
|
|
0 commit comments