Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

agents.ts

116 lines3.8 KB

1import { chat, defineAgent } from '@tanstack/ai'
2import { createOpenRouterText } from '@tanstack/ai-openrouter'
3import { lookupWikipedia, researcher, seo, writer } from '@/lib/blog-agents'
4​
5const lookupWikipediaTool = lookupWikipedia.server(async (input) => {
6 const title =
7 typeof input === 'object' &&
8 input !== null &&
9 'title' in input &&
10 typeof input.title === 'string'
11 ? input.title
12 : ''
13 if (title.length === 0) {
14 throw new Error('lookupWikipedia needs a page title')
15 }
16 const response = await fetch(
17 `https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(title)}`,
18 { headers: { 'user-agent': 'tanstack-ai-subagents-persisted-example' } },
19 )
20 if (response.status === 404) return { title, found: false }
21 if (!response.ok) {
22 throw new Error(`Wikipedia returned ${response.status} for ${title}`)
23 }
24 const page: unknown = await response.json()
25 const extract =
26 typeof page === 'object' &&
27 page !== null &&
28 'extract' in page &&
29 typeof page.extract === 'string'
30 ? page.extract
31 : ''
32 if (extract.length === 0) return { title, found: false }
33 return {
34 title,
35 found: true,
36 extract,
37 url: `https://en.wikipedia.org/wiki/${encodeURIComponent(title)}`,
38 }
39})
40​
41function linkAbort(signal: AbortSignal | undefined) {
42 const abortController = new AbortController()
43 if (signal === undefined) return abortController
44 if (signal.aborted) {
45 abortController.abort()
46 return abortController
47 }
48 signal.addEventListener(
49 'abort',
50 () => {
51 abortController.abort()
52 },
53 { once: true },
54 )
55 return abortController
57​
58export function createBlogAgents(apiKey: string) {
59 const researcherAgent = defineAgent({
60 ...researcher,
61 run: (ctx) =>
62 chat({
63 adapter: createOpenRouterText('openai/gpt-5.5', apiKey),
64 reasoning: 'medium',
65 tools: [lookupWikipediaTool],
66 messages: ctx.messages,
67 threadId: ctx.threadId,
68 runId: ctx.runId,
69 parentRunId: ctx.parentRunId,
70 subagentRunId: ctx.subagentRunId,
71 resume: ctx.resume,
72 abortController: linkAbort(ctx.abortSignal),
73 systemPrompts: [
74 'You research for a blog desk. Call lookupWikipedia once for each topic before you reply. Then reply in Markdown with short notes, and give the Wikipedia URL as the source. Use a list. Do not write the full post.',
75 ],
76 }),
77 })
78​
79 const writerAgent = defineAgent({
80 ...writer,
81 run: (ctx) =>
82 chat({
83 adapter: createOpenRouterText('openai/gpt-5.5', apiKey),
84 messages: ctx.messages,
85 threadId: ctx.threadId,
86 runId: ctx.runId,
87 parentRunId: ctx.parentRunId,
88 subagentRunId: ctx.subagentRunId,
89 resume: ctx.resume,
90 abortController: linkAbort(ctx.abortSignal),
91 systemPrompts: [
92 'You write blog posts in Markdown. Start with one # title. Use short ## sections and a closing line. When earlier messages contain research notes or SEO text, write only from those messages. Do not add facts that are not in those messages.',
93 ],
94 }),
95 })
96​
97 const seoAgent = defineAgent({
98 ...seo,
99 run: (ctx) =>
100 chat({
101 adapter: createOpenRouterText('openai/gpt-5.5', apiKey),
102 messages: ctx.messages,
103 threadId: ctx.threadId,
104 runId: ctx.runId,
105 parentRunId: ctx.parentRunId,
106 subagentRunId: ctx.subagentRunId,
107 resume: ctx.resume,
108 abortController: linkAbort(ctx.abortSignal),
109 systemPrompts: [
110 'You prepare SEO for a blog post. Reply in Markdown. Give 5 title options, one meta description under 160 characters, and a short tag list. Do not write the full article.',
111 ],
112 }),
113 })
114​
115 return [researcherAgent, writerAgent, seoAgent] as const