|
| 1 | +import { createOpenRouter } from '@openrouter/ai-sdk-provider'; |
| 2 | +import { convertToModelMessages, stepCountIs, streamText, tool, type UIMessage } from 'ai'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import { source } from '@/lib/source'; |
| 5 | +import { Document, type DocumentData } from 'flexsearch'; |
| 6 | + |
| 7 | +interface CustomDocument extends DocumentData { |
| 8 | + url: string; |
| 9 | + title: string; |
| 10 | + description: string; |
| 11 | + content: string; |
| 12 | +} |
| 13 | + |
| 14 | +const searchServer = createSearchServer(); |
| 15 | + |
| 16 | +async function createSearchServer() { |
| 17 | + const search = new Document<CustomDocument>({ |
| 18 | + document: { |
| 19 | + id: 'url', |
| 20 | + index: ['title', 'description', 'content'], |
| 21 | + store: true, |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + const docs = await chunkedAll( |
| 26 | + source.getPages().map(async (page) => { |
| 27 | + if (!('getText' in page.data)) return null; |
| 28 | + |
| 29 | + return { |
| 30 | + title: page.data.title, |
| 31 | + description: page.data.description, |
| 32 | + url: page.url, |
| 33 | + content: await page.data.getText('raw'), |
| 34 | + } as CustomDocument; |
| 35 | + }), |
| 36 | + ); |
| 37 | + |
| 38 | + for (const doc of docs) { |
| 39 | + if (doc) search.add(doc); |
| 40 | + } |
| 41 | + |
| 42 | + return search; |
| 43 | +} |
| 44 | + |
| 45 | +async function chunkedAll<O>(promises: Promise<O>[]): Promise<O[]> { |
| 46 | + const SIZE = 50; |
| 47 | + const out: O[] = []; |
| 48 | + for (let i = 0; i < promises.length; i += SIZE) { |
| 49 | + out.push(...(await Promise.all(promises.slice(i, i + SIZE)))); |
| 50 | + } |
| 51 | + return out; |
| 52 | +} |
| 53 | + |
| 54 | +const openrouter = createOpenRouter({ |
| 55 | + apiKey: process.env.OPENROUTER_API_KEY, |
| 56 | +}); |
| 57 | + |
| 58 | +const systemPrompt = [ |
| 59 | + 'You are an AI assistant for a documentation site.', |
| 60 | + 'Use the `search` tool to retrieve relevant docs context before answering when needed.', |
| 61 | + 'The `search` tool returns raw JSON results from documentation. Use those results to ground your answer and cite sources as markdown links using the document `url` field when available.', |
| 62 | + 'If you cannot find the answer in search results, say you do not know and suggest a better search query.', |
| 63 | +].join('\n'); |
| 64 | + |
| 65 | +export async function POST(req: Request) { |
| 66 | + const reqJson: { messages?: UIMessage[] } = await req.json(); |
| 67 | + |
| 68 | + const result = streamText({ |
| 69 | + model: openrouter.chat(process.env.OPENROUTER_MODEL ?? 'anthropic/claude-3.5-sonnet'), |
| 70 | + stopWhen: stepCountIs(5), |
| 71 | + tools: { |
| 72 | + search: searchTool, |
| 73 | + }, |
| 74 | + messages: [ |
| 75 | + { role: 'system', content: systemPrompt }, |
| 76 | + ...(await convertToModelMessages(reqJson.messages ?? [])), |
| 77 | + ], |
| 78 | + toolChoice: 'auto', |
| 79 | + }); |
| 80 | + |
| 81 | + return result.toUIMessageStreamResponse(); |
| 82 | +} |
| 83 | + |
| 84 | +const searchTool = tool({ |
| 85 | + description: 'Search the docs content and return raw JSON results.', |
| 86 | + inputSchema: z.object({ |
| 87 | + query: z.string(), |
| 88 | + limit: z.number().int().min(1).max(100).default(10), |
| 89 | + }), |
| 90 | + async execute({ query, limit }) { |
| 91 | + const search = await searchServer; |
| 92 | + return await search.searchAsync(query, { limit, merge: true, enrich: true }); |
| 93 | + }, |
| 94 | +}); |
| 95 | + |
| 96 | +export type SearchTool = typeof searchTool; |
0 commit comments