CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
Blame · Line-by-line history
copilot.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 3cbe3d6 | 1 | /** |
| 2 | * Block D9 — Copilot-style completion HTTP surface. | |
| 3 | * | |
| 4 | * POST /api/copilot/completions (authed — PAT / OAuth / session) | |
| 5 | * Body: { prefix, suffix?, language?, repo? } | |
| 6 | * Returns: { completion, model, cached } | |
| 7 | * | |
| 8 | * GET /api/copilot/ping (unauthed) | |
| 9 | * Returns: { ok: true, aiAvailable: boolean } | |
| 10 | * | |
| 11 | * Keep per-user limits tight: IDE plugins call this on every keystroke, so a | |
| 12 | * misbehaving client can otherwise exhaust the shared Anthropic quota fast. | |
| 13 | */ | |
| 14 | ||
| 15 | import { Hono } from "hono"; | |
| 16 | import { requireAuth } from "../middleware/auth"; | |
| 17 | import type { AuthEnv } from "../middleware/auth"; | |
| 18 | import { rateLimit } from "../middleware/rate-limit"; | |
| 19 | import { completeCode } from "../lib/ai-completion"; | |
| 20 | import { isAiAvailable } from "../lib/ai-client"; | |
| 21 | ||
| 22 | const copilot = new Hono<AuthEnv>(); | |
| 23 | ||
| 24 | // Tight per-caller limit: 60/min. NOTE: `rateLimit` keys by bearer-token | |
| 25 | // prefix when Authorization is present, otherwise by IP — so session-cookie | |
| 26 | // callers share a single IP bucket. That's acceptable for an IDE endpoint | |
| 27 | // where the expected caller is almost always a PAT/OAuth token. | |
| 0316dbb | 28 | const completionLimit = rateLimit(60, 60_000, "copilot"); |
| 3cbe3d6 | 29 | |
| 30 | copilot.get("/api/copilot/ping", (c) => { | |
| 31 | return c.json({ ok: true, aiAvailable: isAiAvailable() }); | |
| 32 | }); | |
| 33 | ||
| 34 | copilot.post( | |
| 35 | "/api/copilot/completions", | |
| 36 | completionLimit, | |
| 37 | requireAuth, | |
| 38 | async (c) => { | |
| 39 | let body: any; | |
| 40 | try { | |
| 41 | body = await c.req.json(); | |
| 42 | } catch { | |
| 43 | return c.json({ error: "invalid JSON body" }, 400); | |
| 44 | } | |
| 45 | if (!body || typeof body !== "object") { | |
| 46 | return c.json({ error: "body must be a JSON object" }, 400); | |
| 47 | } | |
| 48 | ||
| 49 | const { prefix, suffix, language, repo } = body as { | |
| 50 | prefix?: unknown; | |
| 51 | suffix?: unknown; | |
| 52 | language?: unknown; | |
| 53 | repo?: unknown; | |
| 54 | }; | |
| 55 | ||
| 56 | if (typeof prefix !== "string" || prefix.length === 0) { | |
| 57 | return c.json({ error: "prefix (non-empty string) is required" }, 400); | |
| 58 | } | |
| 59 | ||
| 60 | const result = await completeCode({ | |
| 61 | prefix, | |
| 62 | suffix: typeof suffix === "string" ? suffix : undefined, | |
| 63 | language: typeof language === "string" ? language : undefined, | |
| 64 | repoHint: typeof repo === "string" ? repo : undefined, | |
| 65 | }); | |
| 66 | ||
| 67 | return c.json(result); | |
| 68 | } | |
| 69 | ); | |
| 70 | ||
| 71 | export default copilot; |