-
-
Notifications
You must be signed in to change notification settings - Fork 43
feat(docs): add AI assistant #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f0cce3
feat(docs): add AI assistant
maxktz 243ca86
codex: address PR review feedback (#216)
maxktz 36ecb5f
codex: address PR review feedback (#216)
maxktz 19ea71c
fix(docs): accept AI SDK chat envelope
maxktz fdd6a94
codex: address PR review feedback (#216)
maxktz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { | ||
| convertToModelMessages, | ||
| createUIMessageStreamResponse, | ||
| safeValidateUIMessages, | ||
| stepCountIs, | ||
| streamText, | ||
| tool, | ||
| toUIMessageStream, | ||
| } from "ai"; | ||
| import { z } from "zod"; | ||
|
|
||
| import { docsSearch } from "@/lib/docs-search"; | ||
| import { source } from "@/lib/source"; | ||
|
|
||
| const maxRequestSize = 64_000; | ||
| const maxPageContentSize = 8_000; | ||
|
|
||
| const requestSchema = z.object({ | ||
| currentPage: z | ||
| .string() | ||
| .max(200) | ||
| .regex(/^\/docs(?:\/[\w.-]+)*$/) | ||
| .optional(), | ||
| messages: z.array(z.unknown()).min(1).max(20), | ||
| }); | ||
|
|
||
| const systemPrompt = [ | ||
| "You are the PayKit documentation assistant.", | ||
| "Answer questions about PayKit using the search tool before answering.", | ||
| "Ground answers in the search results and cite relevant pages as Markdown links using their url field.", | ||
| "Be concise and practical. If the documentation does not answer the question, say so.", | ||
| ].join("\n"); | ||
|
|
||
| const search = tool({ | ||
| description: "Search the PayKit documentation for information relevant to the user's question.", | ||
| inputSchema: z.object({ | ||
| query: z.string().min(1).max(200), | ||
| limit: z.number().int().min(1).max(6).default(4), | ||
| }), | ||
| async execute({ query, limit }) { | ||
| const results = await docsSearch.search(query, { limit: limit * 4 }); | ||
| const urls = [ | ||
| ...new Set( | ||
| results.flatMap((result) => { | ||
| const url = result.url.split("#", 1)[0]; | ||
| return url ? [url] : []; | ||
| }), | ||
| ), | ||
| ].slice(0, limit); | ||
|
|
||
| return Promise.all( | ||
| urls.map(async (url) => { | ||
| const page = source.getPageByUrl(url); | ||
| if (!page) return null; | ||
|
|
||
| return { | ||
| content: (await page.data.getText("processed")).slice(0, maxPageContentSize), | ||
| description: page.data.description ?? "", | ||
| title: page.data.title, | ||
| url: page.url, | ||
| }; | ||
| }), | ||
| ).then((pages) => pages.filter((page) => page !== null)); | ||
| }, | ||
| }); | ||
|
|
||
| export async function POST(request: Request) { | ||
| const contentLength = Number(request.headers.get("content-length")); | ||
| if (Number.isFinite(contentLength) && contentLength > maxRequestSize) { | ||
| return Response.json({ error: "Request too large" }, { status: 413 }); | ||
| } | ||
|
|
||
| const requestText = await request.text(); | ||
| if (new TextEncoder().encode(requestText).byteLength > maxRequestSize) { | ||
| return Response.json({ error: "Request too large" }, { status: 413 }); | ||
| } | ||
|
|
||
| let requestJson: unknown; | ||
| try { | ||
| requestJson = JSON.parse(requestText); | ||
| } catch { | ||
| return Response.json({ error: "Invalid request" }, { status: 400 }); | ||
| } | ||
|
|
||
| const body = requestSchema.safeParse(requestJson); | ||
| if (!body.success) { | ||
| return Response.json({ error: "Invalid request" }, { status: 400 }); | ||
| } | ||
|
|
||
| const messages = await safeValidateUIMessages({ messages: body.data.messages }); | ||
| if (!messages.success || messages.data.some((message) => message.role === "system")) { | ||
| return Response.json({ error: "Invalid messages" }, { status: 400 }); | ||
| } | ||
|
|
||
| const result = streamText({ | ||
| instructions: body.data.currentPage | ||
| ? `${systemPrompt}\nThe reader is viewing ${body.data.currentPage}.` | ||
| : systemPrompt, | ||
| model: process.env.AI_GATEWAY_MODEL ?? "openai/gpt-5.6-luna", | ||
| messages: await convertToModelMessages(messages.data), | ||
| maxOutputTokens: 2_000, | ||
| prepareStep: ({ stepNumber }) => ({ | ||
| toolChoice: stepNumber === 0 ? "required" : "auto", | ||
| }), | ||
| stopWhen: stepCountIs(4), | ||
| tools: { search }, | ||
| }); | ||
|
|
||
| return createUIMessageStreamResponse({ | ||
| stream: toUIMessageStream({ stream: result.stream }), | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| import { createFromSource } from "fumadocs-core/search/server"; | ||
| import { docsSearch } from "@/lib/docs-search"; | ||
|
|
||
| import { source } from "@/lib/source"; | ||
|
|
||
| export const { GET } = createFromSource(source); | ||
| export const { GET } = docsSearch; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.