-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
90 lines (78 loc) · 2.01 KB
/
index.ts
File metadata and controls
90 lines (78 loc) · 2.01 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { openai } from "@ai-sdk/openai";
import { agent, flowAgent } from "@funkai/agents";
import { z } from "zod";
import { prompts } from "~prompts";
const researcher = agent({
name: "researcher",
model: openai("gpt-4.1"),
system: prompts.agents.researcher.render({
domain: "software engineering",
}),
});
const writer = agent({
name: "writer",
model: openai("gpt-4.1"),
system: prompts.agents.writer.render({
tone: "friendly",
audience: "senior engineers",
}),
});
const reviewer = agent({
name: "reviewer",
model: openai("gpt-4o-mini"),
system: prompts.agents.reviewer.render({
standard: "technical blog post",
}),
});
const pipeline = flowAgent(
{
name: "content-pipeline",
input: z.object({
topic: z.string(),
}),
output: z.object({
article: z.string(),
verdict: z.string(),
}),
},
async ({ input, $ }) => {
const research = await $.agent({
id: "research",
agent: researcher,
input: `Research the topic: ${input.topic}`,
});
if (!research.ok) {
throw new Error(`Research failed: ${research.error.message}`);
}
const draft = await $.agent({
id: "draft",
agent: writer,
input: `Write an article based on these findings:\n${research.output}`,
});
if (!draft.ok) {
throw new Error(`Draft failed: ${draft.error.message}`);
}
const review = await $.agent({
id: "review",
agent: reviewer,
input: `Review this article:\n${draft.output}`,
});
if (!review.ok) {
throw new Error(`Review failed: ${review.error.message}`);
}
return {
article: String(draft.output),
verdict: String(review.output),
};
},
);
const result = await pipeline.generate({
input: { topic: "functional programming patterns in TypeScript" },
});
if (result.ok) {
console.log("Article:", result.output.article);
console.log("Verdict:", result.output.verdict);
console.log("Trace:", result.trace);
} else {
console.error("Error:", result.error);
}