Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(web): add docs Ask AI API route
Richie McIlroy686c1a7e6892
diff --git a/apps/web/__tests__/unit/docs-ask.test.ts b/apps/web/__tests__/unit/docs-ask.test.tsnew file mode 100644index 000000000..3dc9711e7--- /dev/null+++ b/apps/web/__tests__/unit/docs-ask.test.ts@@ -0,0 +1,58 @@+import { describe, expect, it } from "vitest";+import {+ buildDocsAskContext,+ buildDocsAskSystemPrompt,+ rankDocsForQuery,+ tokenizeQuery,+} from "@/lib/docs-ask";+import { getAllDocs } from "@/utils/docs";++describe("docs ask", () => {+ it("tokenizes questions into useful terms", () => {+ const terms = tokenizeQuery("How do I set up Cap in Claude Code?");+ expect(terms).toEqual(expect.arrayContaining(["set", "claude", "code"]));+ expect(terms).not.toContain("cap");+ expect(terms).not.toContain("how");+ });++ it("ranks agent docs first for agent setup questions", () => {+ const docs = getAllDocs();+ const ranked = rankDocsForQuery(+ "install the cli and connect claude code with mcp",+ docs,+ );+ expect(ranked.length).toBeGreaterThan(0);+ expect(ranked.slice(0, 3).map((doc) => doc.slug)).toContain("agents/setup");+ });++ it("builds a bounded context with matching sources", () => {+ const docs = getAllDocs();+ const { context, sources } = buildDocsAskContext(+ "record my screen from an agent",+ docs,+ );+ expect(sources.length).toBeGreaterThan(0);+ expect(sources.length).toBeLessThanOrEqual(6);+ expect(context.length).toBeLessThanOrEqual(30000);+ for (const source of sources) {+ expect(context).toContain(`/docs/${source.slug}`);+ }+ });++ it("falls back to core pages when nothing matches", () => {+ const docs = getAllDocs();+ const { sources } = buildDocsAskContext("zzzz qqqq", docs);+ expect(sources.map((source) => source.slug)).toContain("introduction");+ });++ it("writes the ask system prompt with page list and guardrails", () => {+ const prompt = buildDocsAskSystemPrompt({+ pages: [{ slug: "agents", title: "Cap for Agents" }],+ context: "# Cap for Agents\nPath: /docs/agents\n\nBody",+ });+ expect(prompt).toContain("- Cap for Agents: /docs/agents");+ expect(prompt).toContain("Never invent features");+ expect(prompt).toContain("[email protected]");+ expect(prompt).toContain("[Cap for Agents](/docs/agents)");+ });+});diff --git a/apps/web/app/api/docs/ask/route.ts b/apps/web/app/api/docs/ask/route.tsnew file mode 100644index 000000000..e4f9d69f3--- /dev/null+++ b/apps/web/app/api/docs/ask/route.ts@@ -0,0 +1,208 @@+import { serverEnv } from "@cap/env";+import type { NextRequest } from "next/server";+import { buildDocsAskContext, buildDocsAskSystemPrompt } from "@/lib/docs-ask";+import { isRateLimited, RATE_LIMIT_IDS } from "@/lib/rate-limit";+import { getAllDocs } from "@/utils/docs";++export const dynamic = "force-dynamic";++const ASK_MODEL = "claude-sonnet-5";+const ASK_MAX_TOKENS = 800;+const MAX_QUESTION_LENGTH = 500;+const MAX_HISTORY_TURNS = 6;+const MAX_HISTORY_CONTENT_LENGTH = 4000;+const RATE_LIMIT_WINDOW_MS = 60_000;+const RATE_LIMIT_MAX_REQUESTS = 10;++const requestLog = new Map<string, number[]>();++function isLocallyRateLimited(key: string): boolean {+ const now = Date.now();+ const recent = (requestLog.get(key) ?? []).filter(+ (timestamp) => now - timestamp < RATE_LIMIT_WINDOW_MS,+ );+ if (recent.length >= RATE_LIMIT_MAX_REQUESTS) {+ requestLog.set(key, recent);+ return true;+ }+ recent.push(now);+ requestLog.set(key, recent);+ if (requestLog.size > 5000) {+ for (const [entryKey, timestamps] of requestLog) {+ if (timestamps.every((ts) => now - ts >= RATE_LIMIT_WINDOW_MS)) {+ requestLog.delete(entryKey);+ }+ }+ }+ return false;+}++interface HistoryTurn {+ role: "user" | "assistant";+ content: string;+}++function parseHistory(value: unknown): HistoryTurn[] {+ if (!Array.isArray(value)) return [];+ return value+ .flatMap((entry): HistoryTurn[] => {+ if (!entry || typeof entry !== "object") return [];+ const role = (entry as { role?: unknown }).role;+ const content = (entry as { content?: unknown }).content;+ if (role !== "user" && role !== "assistant") return [];+ if (typeof content !== "string" || !content.trim()) return [];+ return [{ role, content: content.slice(0, MAX_HISTORY_CONTENT_LENGTH) }];+ })+ .slice(-MAX_HISTORY_TURNS * 2);+}++function extractDeltaText(data: string): string | null {+ try {+ const event = JSON.parse(data) as {+ type?: unknown;+ delta?: { type?: unknown; text?: unknown };+ };+ if (+ event.type === "content_block_delta" &&+ event.delta?.type === "text_delta" &&+ typeof event.delta.text === "string"+ ) {+ return event.delta.text;+ }+ } catch {+ return null;+ }+ return null;+}++export async function POST(request: NextRequest) {+ const key = serverEnv().ANTHROPIC_API_KEY;+ if (!key) {+ return Response.json(+ { error: "Ask AI is not available right now. Try searching instead." },+ { status: 503 },+ );+ }++ let payload: unknown;+ try {+ payload = await request.json();+ } catch {+ return Response.json({ error: "Invalid request." }, { status: 400 });+ }++ const question =+ payload && typeof payload === "object"+ ? (payload as { question?: unknown }).question+ : undefined;+ const trimmedQuestion = typeof question === "string" ? question.trim() : "";+ if (+ trimmedQuestion.length < 3 ||+ trimmedQuestion.length > MAX_QUESTION_LENGTH+ ) {+ return Response.json(+ { error: "Ask a question between 3 and 500 characters." },+ { status: 400 },+ );+ }++ const history = parseHistory(+ payload && typeof payload === "object"+ ? (payload as { history?: unknown }).history+ : undefined,+ );++ const ip =+ request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "unknown";+ if (+ isLocallyRateLimited(ip) ||+ (await isRateLimited(RATE_LIMIT_IDS.DOCS_ASK, {+ headers: request.headers,+ }))+ ) {+ return Response.json(+ { error: "Too many questions right now. Try again in a minute." },+ { status: 429 },+ );+ }++ const docs = getAllDocs();+ const rankingQuery = [+ ...history+ .filter((turn) => turn.role === "user")+ .map((turn) => turn.content),+ trimmedQuestion,+ ].join(" ");+ const { context } = buildDocsAskContext(rankingQuery, docs);+ const system = buildDocsAskSystemPrompt({+ pages: docs.map((doc) => ({ slug: doc.slug, title: doc.metadata.title })),+ context,+ });++ const upstream = await fetch("https://api.anthropic.com/v1/messages", {+ method: "POST",+ headers: {+ "x-api-key": key,+ "anthropic-version": "2023-06-01",+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ model: ASK_MODEL,+ max_tokens: ASK_MAX_TOKENS,+ stream: true,+ system,+ messages: [...history, { role: "user", content: trimmedQuestion }],+ }),+ signal: AbortSignal.timeout(60_000),+ }).catch((error) => {+ console.error("docs-ask upstream fetch failed", error);+ return null;+ });++ if (!upstream || !upstream.ok || !upstream.body) {+ const detail = upstream ? await upstream.text().catch(() => "") : "fetch";+ console.error("docs-ask upstream error", upstream?.status, detail);+ return Response.json(+ { error: "Ask AI is having trouble right now. Try again shortly." },+ { status: 502 },+ );+ }++ const upstreamBody = upstream.body;+ const encoder = new TextEncoder();+ const decoder = new TextDecoder();+ let buffer = "";++ const stream = new ReadableStream<Uint8Array>({+ start(controller) {+ const reader = upstreamBody.getReader();+ const pump = (): Promise<void> =>+ reader.read().then(({ done, value }) => {+ if (done) {+ controller.close();+ return;+ }+ buffer += decoder.decode(value, { stream: true });+ const lines = buffer.split("\n");+ buffer = lines.pop() ?? "";+ for (const line of lines) {+ if (!line.startsWith("data:")) continue;+ const text = extractDeltaText(line.slice(5).trim());+ if (text) controller.enqueue(encoder.encode(text));+ }+ return pump();+ });+ return pump().catch((error) => controller.error(error));+ },+ cancel(reason) {+ upstreamBody.cancel(reason).catch(() => undefined);+ },+ });++ return new Response(stream, {+ headers: {+ "Content-Type": "text/plain; charset=utf-8",+ "Cache-Control": "no-store",+ },+ });+}