mirror of
https://github.com/CopilotKit/CopilotKit.git
synced 2026-09-14 16:26:20 +08:00
5ca110e29c
Two things this PR was missing, both now closed. ## The Claude SDK quickstarts are unblocked #6618 put `showcase/integrations/claude-sdk-{python,typescript}` on `createCopilotRuntimeHandler` with `mode: "single-route"`, at the **plain** `route.ts` path. That dissolves the coupling that forced these two pages to be reverted earlier: `verify-shell-docs.ts` asserts each page claims a starter file at `src/app/api/copilotkit/route.ts` AND that the file exists in the extracted starter. Single-route keeps that path, so the prose claims and `requiredStarterFiles` are unchanged — only the fence bodies move to v2. The two content assertions that pinned those pages to v1 (`ExperimentalEmptyAdapter`, `copilotRuntimeNextJSAppRouterEndpoint`) now require `createCopilotRuntimeHandler`, the `/v2` entrypoint, `mode: "single-route"` and a `POST` export. Mutation-checked: flipping the fixture to `mode: "multi-route"` fails with `app/api/copilotkit/route.ts missing single-route mode`. ## Snippet gating: 1 -> 20 route fences I previously claimed the integration pages could not be doctested because of path aliases and per-integration deps. **That was an assumption I never checked, and it was wrong.** Of the 52 migrated route fences, 47 import nothing project-relative; 36 are complete, self-standing routes. 27 pages were eligible, 20 now hold a gated fence — each extracted and typechecked by `tsc --noEmit` against real npm-installed packages in CI. One fence per (page, title): `extract.ts` concatenates tagged blocks sharing a title, so a second complete route on the same page would collide. Mutation-checked on `snippets/integrations/langsmith/index.mdx`: restoring the v1 import in the gated fence turns the run red (20 passed, 1 failed). My first attempt at this check was a no-op — the pattern missed because the fence is JSX-indented — and it "passed" misleadingly. The real check asserts the mutation reached the extracted snippet before trusting the result. ### Harness changes this needed - `extract.ts` now finds the nearest `doctest.json` by walking up to the docs root, instead of looking only in the page's own directory. Otherwise gating 20 pages means ~20 duplicated dependency lists that then drift. A shared list lives at `content/doctest.json`; `docs/integrations/langgraph/` keeps its own (Python deps) and now also carries the TS deps its page needs. - `run.ts` installs each dependency set **once**, into `.doctest-output/.deps/<hash>`, and links it into every snippet sharing that set. Per-snippet installs took **7:58** for 21 snippets, uncomfortably close to the job's 15-minute timeout; shared installs take **0:45** cold. Different dep sets still get separate stores, so this is a dedupe, not a merge. ### `@ag-ui/*` versions have to be pinned to what the runtime expects Unpinned, the gated fences failed with `HttpAgent is not assignable to AbstractAgent — separate declarations of a private property '_debug'`: npm installs a newer `@ag-ui/client` than `@copilotkit/runtime` depends on, so two `AbstractAgent` declarations collide. The sidecar pins `@ag-ui/client@0.0.57` and `@ag-ui/core@0.0.57` to match `@copilotkit/runtime@1.68.3`. ## Seven fences are deliberately NOT gated Un-tagged with the reason, rather than left failing or quietly dropped: - `docs/auth.mdx`, `docs/premium/connect-your-runtime.mdx` — illustrative fences referencing placeholders (`myAgent`, `verifyJwt`) that cannot compile standalone by design. - the four langgraph-family pages and `snippets/self-hosting-copilot-runtime-langgraph-endpoint.mdx` — these hit `LangGraphAgent is not assignable to AbstractAgent — separate declarations of a private property '_debug'`, which pinning does not fix. **That last one is a real pre-existing defect, not a migration regression.** I reconstructed the v1 form of the langgraph quickstart snippet verbatim from `origin/main` and typechecked it against the identical installed dependencies: it fails with the same error. So these snippets have never typechecked against published packages — worth filing separately. It is also what the ~220 `@ts-ignore` comments across `showcase/integrations` were papering over. ## Verified doc-tests (cold, no cache) -> 21 passed, 0 failed in 0:45 mutation check (real, verified) -> 20 passed, 1 failed vitest extract + verify-shell-docs -> 34 passed showcase/shell-docs typecheck -> exit 0 showcase/shell-docs build -> exit 0 structural audit -> 21/21 pages, fence + JSX identical to HEAD Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
565 lines
15 KiB
TypeScript
565 lines
15 KiB
TypeScript
import * as crypto from "node:crypto";
|
|
import * as fs from "node:fs";
|
|
import * as path from "node:path";
|
|
import { execSync, spawn } from "node:child_process";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Types
|
|
// ---------------------------------------------------------------------------
|
|
|
|
interface ManifestEntry {
|
|
id: string;
|
|
file: string;
|
|
lang: string;
|
|
category: string;
|
|
source: string;
|
|
}
|
|
|
|
interface DoctestConfig {
|
|
python?: { deps: string[] };
|
|
typescript?: { deps: string[] };
|
|
node?: { deps: string[] };
|
|
}
|
|
|
|
interface Result {
|
|
id: string;
|
|
category: string;
|
|
status: "pass" | "fail";
|
|
error?: string;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Config
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const OUTPUT_DIR = path.resolve(__dirname, "../../.doctest-output");
|
|
const MANIFEST_PATH = path.join(OUTPUT_DIR, "manifest.json");
|
|
|
|
const DEFAULT_ENV: Record<string, string> = {
|
|
OPENAI_API_KEY: "test-key",
|
|
OPENAI_BASE_URL: "http://localhost:4010",
|
|
};
|
|
|
|
const SERVER_TIMEOUT_MS = 30_000;
|
|
const SERVER_POLL_MS = 500;
|
|
const SCRIPT_TIMEOUT_MS = 30_000;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Seed a snippet directory with a minimal package.json.
|
|
*
|
|
* Replaces `npm init -y`, which derives the package name from the directory
|
|
* name and rejects anything npm considers invalid. Snippet directories are
|
|
* named after the fence title, and a Next.js route handler's title is a path
|
|
* ending in a catch-all segment — `app/api/copilotkit/[[...slug]]/route.ts` —
|
|
* so the leaf directory is literally `[[...slug]]` and `npm init -y` fails
|
|
* with "Invalid name". The name is irrelevant to what these snippets test, so
|
|
* fix it rather than deriving it.
|
|
*/
|
|
function initSnippetPackage(snippetDir: string): void {
|
|
const pkgPath = path.join(snippetDir, "package.json");
|
|
if (fs.existsSync(pkgPath)) return;
|
|
fs.writeFileSync(
|
|
pkgPath,
|
|
JSON.stringify({ name: "doctest-snippet", version: "1.0.0" }, null, 2),
|
|
"utf-8",
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Install a dependency set once and link it into a snippet directory.
|
|
*
|
|
* The store lives at `.doctest-output/.deps/<hash>` and is keyed by the sorted
|
|
* dependency list, so snippets requesting the same set share one install while
|
|
* a snippet with different deps still gets its own. The snippet's own
|
|
* `node_modules` becomes a symlink to the store, which Node and TypeScript both
|
|
* resolve through normally.
|
|
*/
|
|
function installSharedDeps(snippetDir: string, deps: string[]): void {
|
|
const safe = deps.map(validateDepName);
|
|
const key = crypto
|
|
.createHash("sha256")
|
|
.update([...safe].sort().join("\n"))
|
|
.digest("hex")
|
|
.slice(0, 16);
|
|
const store = path.join(OUTPUT_DIR, ".deps", key);
|
|
const storeModules = path.join(store, "node_modules");
|
|
|
|
if (!fs.existsSync(storeModules)) {
|
|
fs.mkdirSync(store, { recursive: true });
|
|
fs.writeFileSync(
|
|
path.join(store, "package.json"),
|
|
JSON.stringify({ name: "doctest-deps", version: "1.0.0" }, null, 2),
|
|
"utf-8",
|
|
);
|
|
execSync(`npm install --no-audit --no-fund ${safe.join(" ")}`, {
|
|
cwd: store,
|
|
stdio: "pipe",
|
|
timeout: 300_000,
|
|
});
|
|
}
|
|
|
|
const link = path.join(snippetDir, "node_modules");
|
|
if (!fs.existsSync(link)) {
|
|
fs.symlinkSync(storeModules, link, "junction");
|
|
}
|
|
}
|
|
|
|
function validateDepName(dep: string): string {
|
|
if (!/^[@\w][\w./-]*(?:@[\w.^~>=<*-]+)?$/.test(dep)) {
|
|
throw new Error(`Invalid dependency name: ${dep}`);
|
|
}
|
|
return dep;
|
|
}
|
|
|
|
/**
|
|
* Find a snippet's `doctest.json`, searching upward to {@link OUTPUT_DIR}.
|
|
*
|
|
* The sidecar is copied once per page, into the page's directory. A snippet
|
|
* whose fence title is a path — `app/api/copilotkit/[[...slug]]/route.ts` —
|
|
* lives several directories below that, so looking only in the snippet's own
|
|
* directory silently finds no config, installs no dependencies, and fails the
|
|
* snippet with "Cannot find module" rather than reporting a missing sidecar.
|
|
*/
|
|
function loadDoctestConfig(snippetDir: string): DoctestConfig {
|
|
let dir = path.resolve(snippetDir);
|
|
const root = path.resolve(OUTPUT_DIR);
|
|
while (dir.startsWith(root)) {
|
|
const configPath = path.join(dir, "doctest.json");
|
|
if (fs.existsSync(configPath)) {
|
|
return JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
|
}
|
|
const parent = path.dirname(dir);
|
|
if (parent === dir) break;
|
|
dir = parent;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
function mergeEnv(extra?: Record<string, string>): Record<string, string> {
|
|
return { ...process.env, ...DEFAULT_ENV, ...extra } as Record<string, string>;
|
|
}
|
|
|
|
async function waitForPort(
|
|
port: number,
|
|
timeoutMs: number,
|
|
pollMs: number,
|
|
shouldContinue: () => boolean = () => true,
|
|
): Promise<boolean> {
|
|
const start = Date.now();
|
|
while (shouldContinue() && Date.now() - start < timeoutMs) {
|
|
try {
|
|
const resp = await fetch(`http://localhost:${port}/`).catch(() => null);
|
|
if (resp) return true;
|
|
} catch {
|
|
// Server not ready yet
|
|
}
|
|
await new Promise((r) => setTimeout(r, pollMs));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function collectProcessOutput(proc: ReturnType<typeof spawn>): {
|
|
isRunning: () => boolean;
|
|
output: () => string;
|
|
} {
|
|
let exited = false;
|
|
let output = "";
|
|
|
|
proc.stdout?.on("data", (chunk) => {
|
|
output += chunk.toString();
|
|
});
|
|
proc.stderr?.on("data", (chunk) => {
|
|
output += chunk.toString();
|
|
});
|
|
proc.on("exit", (code, signal) => {
|
|
exited = true;
|
|
output += `\n[process exited with ${signal ? `signal ${signal}` : `code ${code}`}]`;
|
|
});
|
|
|
|
return {
|
|
isRunning: () => !exited,
|
|
output: () => output.trim(),
|
|
};
|
|
}
|
|
|
|
function serverStartError(
|
|
port: number,
|
|
proc: ReturnType<typeof collectProcessOutput>,
|
|
): string {
|
|
const output = proc.output();
|
|
if (output) {
|
|
return `Server did not bind to port ${port}. Process output:\n${output}`;
|
|
}
|
|
return `Server did not bind to port ${port} within ${SERVER_TIMEOUT_MS}ms`;
|
|
}
|
|
|
|
function detectPort(code: string): number {
|
|
// Look for port=NNNN or PORT=NNNN or --port NNNN
|
|
const match = code.match(/\bport[=\s:]+(\d{4,5})/i);
|
|
return match ? parseInt(match[1], 10) : 8000;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Runners
|
|
// ---------------------------------------------------------------------------
|
|
|
|
async function runPythonServer(
|
|
snippetDir: string,
|
|
entryFile: string,
|
|
config: DoctestConfig,
|
|
): Promise<Result> {
|
|
const id = path.basename(snippetDir);
|
|
const venvDir = path.join(snippetDir, ".venv");
|
|
|
|
try {
|
|
// Create virtualenv
|
|
execSync(`python3 -m venv ${venvDir}`, { cwd: snippetDir, stdio: "pipe" });
|
|
|
|
const pip = path.join(venvDir, "bin", "pip");
|
|
const python = path.join(venvDir, "bin", "python");
|
|
|
|
// Install deps
|
|
const deps = config.python?.deps || [];
|
|
if (deps.length > 0) {
|
|
const safeDeps = deps.map(validateDepName);
|
|
execSync(`${pip} install ${safeDeps.join(" ")}`, {
|
|
cwd: snippetDir,
|
|
stdio: "pipe",
|
|
timeout: 120_000,
|
|
});
|
|
}
|
|
|
|
const code = fs.readFileSync(path.join(snippetDir, entryFile), "utf-8");
|
|
const port = detectPort(code);
|
|
|
|
// Start server
|
|
const proc = spawn(python, [entryFile], {
|
|
cwd: snippetDir,
|
|
env: mergeEnv(),
|
|
stdio: "pipe",
|
|
});
|
|
const serverProcess = collectProcessOutput(proc);
|
|
|
|
try {
|
|
const ready = await waitForPort(
|
|
port,
|
|
SERVER_TIMEOUT_MS,
|
|
SERVER_POLL_MS,
|
|
serverProcess.isRunning,
|
|
);
|
|
|
|
if (!ready) {
|
|
return {
|
|
id,
|
|
category: "server",
|
|
status: "fail",
|
|
error: serverStartError(port, serverProcess),
|
|
};
|
|
}
|
|
|
|
return { id, category: "server", status: "pass" };
|
|
} finally {
|
|
try {
|
|
proc.kill("SIGTERM");
|
|
} catch {}
|
|
}
|
|
} catch (e: any) {
|
|
return {
|
|
id,
|
|
category: "server",
|
|
status: "fail",
|
|
error: e.message || String(e),
|
|
};
|
|
}
|
|
}
|
|
|
|
async function runTypeScriptServer(
|
|
snippetDir: string,
|
|
entryFile: string,
|
|
config: DoctestConfig,
|
|
): Promise<Result> {
|
|
const id = path.basename(snippetDir);
|
|
|
|
try {
|
|
// Init and install deps
|
|
initSnippetPackage(snippetDir);
|
|
|
|
const deps = config.typescript?.deps || config.node?.deps || [];
|
|
if (deps.length > 0) {
|
|
const safeDeps = deps.map(validateDepName);
|
|
execSync(`npm install ${safeDeps.join(" ")}`, {
|
|
cwd: snippetDir,
|
|
stdio: "pipe",
|
|
timeout: 120_000,
|
|
});
|
|
}
|
|
|
|
const code = fs.readFileSync(path.join(snippetDir, entryFile), "utf-8");
|
|
const port = detectPort(code);
|
|
|
|
// Determine runner
|
|
const runner = entryFile.endsWith(".ts") ? "npx tsx" : "node";
|
|
const proc = spawn(
|
|
runner.split(" ")[0],
|
|
[...runner.split(" ").slice(1), entryFile],
|
|
{
|
|
cwd: snippetDir,
|
|
env: mergeEnv(),
|
|
stdio: "pipe",
|
|
},
|
|
);
|
|
const serverProcess = collectProcessOutput(proc);
|
|
|
|
try {
|
|
const ready = await waitForPort(
|
|
port,
|
|
SERVER_TIMEOUT_MS,
|
|
SERVER_POLL_MS,
|
|
serverProcess.isRunning,
|
|
);
|
|
|
|
if (!ready) {
|
|
return {
|
|
id,
|
|
category: "server",
|
|
status: "fail",
|
|
error: serverStartError(port, serverProcess),
|
|
};
|
|
}
|
|
|
|
return { id, category: "server", status: "pass" };
|
|
} finally {
|
|
try {
|
|
proc.kill("SIGTERM");
|
|
} catch {}
|
|
}
|
|
} catch (e: any) {
|
|
return {
|
|
id,
|
|
category: "server",
|
|
status: "fail",
|
|
error: e.message || String(e),
|
|
};
|
|
}
|
|
}
|
|
|
|
async function runScript(
|
|
snippetDir: string,
|
|
entryFile: string,
|
|
lang: string,
|
|
config: DoctestConfig,
|
|
): Promise<Result> {
|
|
const id = path.basename(snippetDir);
|
|
|
|
try {
|
|
if (lang === "python") {
|
|
const venvDir = path.join(snippetDir, ".venv");
|
|
execSync(`python3 -m venv ${venvDir}`, {
|
|
cwd: snippetDir,
|
|
stdio: "pipe",
|
|
});
|
|
const pip = path.join(venvDir, "bin", "pip");
|
|
const python = path.join(venvDir, "bin", "python");
|
|
|
|
const deps = config.python?.deps || [];
|
|
if (deps.length > 0) {
|
|
const safeDeps = deps.map(validateDepName);
|
|
execSync(`${pip} install ${safeDeps.join(" ")}`, {
|
|
cwd: snippetDir,
|
|
stdio: "pipe",
|
|
timeout: 120_000,
|
|
});
|
|
}
|
|
|
|
execSync(`${python} ${entryFile}`, {
|
|
cwd: snippetDir,
|
|
env: mergeEnv(),
|
|
stdio: "pipe",
|
|
timeout: SCRIPT_TIMEOUT_MS,
|
|
});
|
|
} else {
|
|
initSnippetPackage(snippetDir);
|
|
const deps = config.typescript?.deps || config.node?.deps || [];
|
|
if (deps.length > 0) {
|
|
const safeDeps = deps.map(validateDepName);
|
|
execSync(`npm install ${safeDeps.join(" ")}`, {
|
|
cwd: snippetDir,
|
|
stdio: "pipe",
|
|
timeout: 120_000,
|
|
});
|
|
}
|
|
|
|
const runner = entryFile.endsWith(".ts") ? "npx tsx" : "node";
|
|
execSync(`${runner} ${entryFile}`, {
|
|
cwd: snippetDir,
|
|
env: mergeEnv(),
|
|
stdio: "pipe",
|
|
timeout: SCRIPT_TIMEOUT_MS,
|
|
});
|
|
}
|
|
|
|
return { id, category: "script", status: "pass" };
|
|
} catch (e: any) {
|
|
return {
|
|
id,
|
|
category: "script",
|
|
status: "fail",
|
|
error: e.message || String(e),
|
|
};
|
|
}
|
|
}
|
|
|
|
async function runComponent(
|
|
snippetDir: string,
|
|
entryFile: string,
|
|
config: DoctestConfig,
|
|
): Promise<Result> {
|
|
const id = path.basename(snippetDir);
|
|
|
|
try {
|
|
initSnippetPackage(snippetDir);
|
|
|
|
const deps = config.typescript?.deps || [];
|
|
const baseDeps = ["typescript", "@types/react", "@types/node"];
|
|
const allDeps = [...new Set([...baseDeps, ...deps])];
|
|
|
|
// Every component snippet sharing a dependency set installs it ONCE, into
|
|
// a shared directory keyed by that set, and links to it. Installing
|
|
// per-snippet meant N identical `npm install` runs — with ~20 gated
|
|
// snippets that dominated the job's wall clock and pushed it toward the
|
|
// 15-minute CI timeout. Snippets with different dep sets still get their
|
|
// own store, so this is a dedupe, not a merge.
|
|
installSharedDeps(snippetDir, allDeps);
|
|
|
|
// Write minimal tsconfig if none exists
|
|
const tsconfigPath = path.join(snippetDir, "tsconfig.json");
|
|
if (!fs.existsSync(tsconfigPath)) {
|
|
fs.writeFileSync(
|
|
tsconfigPath,
|
|
JSON.stringify(
|
|
{
|
|
compilerOptions: {
|
|
target: "ES2020",
|
|
module: "ESNext",
|
|
moduleResolution: "bundler",
|
|
jsx: "react-jsx",
|
|
strict: true,
|
|
noEmit: true,
|
|
esModuleInterop: true,
|
|
skipLibCheck: true,
|
|
},
|
|
include: [entryFile],
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
"utf-8",
|
|
);
|
|
}
|
|
|
|
execSync("npx tsc --noEmit", {
|
|
cwd: snippetDir,
|
|
stdio: "pipe",
|
|
timeout: SCRIPT_TIMEOUT_MS,
|
|
});
|
|
|
|
return { id, category: "component", status: "pass" };
|
|
} catch (e: any) {
|
|
return {
|
|
id,
|
|
category: "component",
|
|
status: "fail",
|
|
error: e.message || String(e),
|
|
};
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Main
|
|
// ---------------------------------------------------------------------------
|
|
|
|
async function main() {
|
|
if (!fs.existsSync(MANIFEST_PATH)) {
|
|
console.error(
|
|
`Manifest not found at ${MANIFEST_PATH}. Run extract.ts first.`,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
const manifest: ManifestEntry[] = JSON.parse(
|
|
fs.readFileSync(MANIFEST_PATH, "utf-8"),
|
|
);
|
|
|
|
if (manifest.length === 0) {
|
|
console.log("No doctest snippets found in manifest.");
|
|
process.exit(0);
|
|
}
|
|
|
|
console.log(`Running ${manifest.length} doctest snippet(s)...\n`);
|
|
|
|
const results: Result[] = [];
|
|
|
|
for (const entry of manifest) {
|
|
const snippetDir = path.join(OUTPUT_DIR, path.dirname(entry.file));
|
|
const entryFile = path.basename(entry.file);
|
|
const config = loadDoctestConfig(snippetDir);
|
|
|
|
console.log(` Running: ${entry.id} [${entry.category}/${entry.lang}]`);
|
|
|
|
let result: Result;
|
|
|
|
if (entry.category === "server") {
|
|
if (entry.lang === "python") {
|
|
result = await runPythonServer(snippetDir, entryFile, config);
|
|
} else {
|
|
result = await runTypeScriptServer(snippetDir, entryFile, config);
|
|
}
|
|
} else if (entry.category === "script") {
|
|
result = await runScript(snippetDir, entryFile, entry.lang, config);
|
|
} else if (entry.category === "component") {
|
|
result = await runComponent(snippetDir, entryFile, config);
|
|
} else {
|
|
result = {
|
|
id: entry.id,
|
|
category: entry.category,
|
|
status: "fail",
|
|
error: `Unknown category: ${entry.category}`,
|
|
};
|
|
}
|
|
|
|
results.push(result);
|
|
|
|
const icon = result.status === "pass" ? "PASS" : "FAIL";
|
|
console.log(
|
|
` ${icon}: ${entry.id}${result.error ? ` — ${result.error}` : ""}\n`,
|
|
);
|
|
}
|
|
|
|
// Summary
|
|
const passed = results.filter((r) => r.status === "pass").length;
|
|
const failed = results.filter((r) => r.status === "fail").length;
|
|
|
|
console.log("─".repeat(60));
|
|
console.log(
|
|
`Results: ${passed} passed, ${failed} failed, ${results.length} total`,
|
|
);
|
|
console.log("─".repeat(60));
|
|
|
|
if (failed > 0) {
|
|
console.log("\nFailed snippets:");
|
|
for (const r of results.filter((r) => r.status === "fail")) {
|
|
console.log(` ${r.id}: ${r.error}`);
|
|
}
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error("Unexpected error:", e);
|
|
process.exit(1);
|
|
});
|