-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathworkflow-orchestrator.ts
More file actions
73 lines (66 loc) · 2.48 KB
/
Copy pathworkflow-orchestrator.ts
File metadata and controls
73 lines (66 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as restate from "@restatedev/restate-sdk";
import { RestatePromise } from "@restatedev/restate-sdk";
import { openai } from "@ai-sdk/openai";
import {generateText, Output, wrapLanguageModel} from "ai";
import { durableCalls } from "@restatedev/vercel-ai-middleware";
import { ResearchRequestSchema } from "./utils/types";
import { z } from "zod";
const schema = restate.serde.schema;
// <start_here>
export const researchWorker = restate.service({
name: "ResearchWorker",
handlers: {
research: async (ctx: restate.Context, {question}: { question: string }) => {
const model = wrapLanguageModel({
model: openai("gpt-5.4"),
middleware: durableCalls(ctx, { maxRetryAttempts: 3 }),
});
const { text: answer } = await generateText({
model,
system:
"You are a research assistant. Provide a concise, factual answer.",
prompt: question,
});
return { question, answer };
},
},
});
const orchestrator = restate.service({
name: "ResearchReport",
handlers: {
generate: restate.createServiceHandler(
{ input: schema(ResearchRequestSchema) },
async (ctx: restate.Context, {topic}: { topic: string }) => {
const model = wrapLanguageModel({
model: openai("gpt-5.4"),
middleware: durableCalls(ctx, { maxRetryAttempts: 3 }),
});
// Step 1: Orchestrator creates a research plan
const { output: tasks } = await generateText({
model,
system: `You are a research planner. Break the topic into 2-4 research
sub-tasks. Respond with a JSON array of strings, each a specific
research question. Example: ["question 1", "question 2"]`,
prompt: topic,
output: Output.array({element: z.string()})
});
// Step 2: Dispatch workers in parallel
const workerResults = await RestatePromise.all(
tasks.map((question) =>
ctx.serviceClient(researchWorker).research({ question }),
),
);
// Step 3: Combine results into a report
const { text: report } = await generateText({
model,
system:
"You are a report writer. Combine the research findings into a cohesive report.",
prompt: `Topic: ${topic}\n\nResearch findings:\n${JSON.stringify(workerResults)}`,
});
return { report, taskCount: tasks.length };
},
),
},
});
// <end_here>
restate.serve({ services: [orchestrator, researchWorker] });