Skip to content

feat(nextedit): add /api/nextedit/completions endpoint - #3481

Closed
kilo-code-bot[bot] wants to merge 3 commits into
mainfrom
mark/nextedit-endpoint
Closed

feat(nextedit): add /api/nextedit/completions endpoint#3481
kilo-code-bot[bot] wants to merge 3 commits into
mainfrom
mark/nextedit-endpoint

Conversation

@kilo-code-bot

@kilo-code-bot kilo-code-bot Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a new proxy endpoint at /api/nextedit/completions to support next-edit (NES) requests, complementing the existing /api/fim/completions route. For now only Inception (Mercury Edit) is supported — the same provider used by Kilo-Org/kilocode#10536.

The route mirrors the structure of /api/fim/completions (auth → balance/org checks → BYOK lookup → upstream proxy → usage logging), but speaks the chat-style messages shape that Inception's /v1/edit/completions endpoint expects. Only models prefixed inception/ are accepted; anything else returns unsupported_nextedit_model.

Changes

  • apps/web/src/app/api/nextedit/completions/route.ts — new POST handler. Validates the request, resolves provider from the inception/ model prefix, enforces org policy (only / ignore), proxies to https://api.inceptionlabs.ai/v1/edit/completions, and reports usage.
  • apps/web/src/lib/ai-gateway/llm-proxy-helpers.ts — adds extractNextEditPromptInfo and countAndStoreNextEditUsage (reuses the Inception FIM per-token cost formula since it's the same model family).
  • apps/web/src/lib/proxy-error-types.ts — adds unsupported_nextedit_model.
  • packages/db/src/schema-types.ts — adds nextedit_completions to GatewayApiKindSchema so usage rows can be tagged distinctly from fim_completions.

Notes

  • /v1/edit/completions only accepts a single role: "user" message (Mercury bakes its system prompt server-side and returns 400 on role: "system"), so the schema enforces that shape implicitly via messages: Array(min(1)) and the route forwards messages verbatim.
  • Streaming is not supported by the upstream endpoint today; the schema rejects stream: true.
  • Pricing reuses computeInceptionFimMicrodollarCost (0.25/0.75 microdollars per input/output token) because Mercury Edit 2 sits in the same Inception family as the existing FIM model.

Verification

  • Manual review of the route against the existing /api/fim/completions route to ensure the auth/balance/org-policy/BYOK flow is identical, with only the request/response parsing and upstream URL swapped.

Visual Changes

N/A — backend-only.

Reviewer Notes

  • Full repo pnpm typecheck was skipped (dependencies not installed in this sandbox; full typecheck takes long enough to disrupt the loop). CI will run it.
  • The new nextedit_completions enum value is additive; existing consumers of GatewayApiKind accept it as a string without further changes.
  • Future work: route through the Kilo gateway instead of calling Inception directly, and add martian/other providers once they expose an equivalent endpoint.

Adds a new proxy route at /api/nextedit/completions that mirrors the
shape of /api/fim/completions but targets Inception's
/v1/edit/completions endpoint. For now only the 'inception/' provider
prefix is supported; the route accepts a chat-style messages array
(single user message, no system role — Inception bakes the system
prompt server-side) and returns the upstream response unchanged.

Adds 'nextedit_completions' to GatewayApiKindSchema and a matching
'unsupported_nextedit_model' error type. Pricing reuses Inception's
FIM per-token rates.
Comment thread apps/web/src/lib/ai-gateway/llm-proxy-helpers.ts Outdated
Comment thread apps/web/src/app/api/nextedit/completions/route.ts
@kilo-code-bot

kilo-code-bot Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

Both previously flagged warnings have been resolved: max_tokens is now validated as a positive integer at the Zod schema boundary, and countAndStoreNextEditUsage is now guarded with a .catch() that reports to Sentry and returns null cleanly.

Resolved Issues (previously flagged)
File Issue Status
apps/web/src/lib/ai-gateway/llm-proxy-helpers.ts parseNextEditUsageFromString unguarded rejection inside after() ✅ Fixed — .catch() with captureException added
apps/web/src/app/api/nextedit/completions/route.ts Negative max_tokens bypassing security guard ✅ Fixed — schema changed to z.number().int().positive().optional()
Files Reviewed (4 files)
  • apps/web/src/app/api/nextedit/completions/route.ts — no issues
  • apps/web/src/lib/ai-gateway/llm-proxy-helpers.ts — no issues
  • apps/web/src/lib/proxy-error-types.ts — no issues
  • packages/db/src/schema-types.ts — no issues

Fix these issues in Kilo Cloud


Reviewed by claude-4.6-sonnet-20260217 · 227,578 tokens

Review guidance: REVIEW.md from base branch main

…tive max_tokens

Address kilo-code-bot review:
- Wrap parseNextEditUsageFromString in .catch so non-JSON upstream
  error bodies surface as Sentry events instead of silent unhandled
  rejections inside after().
- Constrain max_tokens to a positive integer at the schema level so
  values like -1 can no longer slip past the !max_tokens / >limit
  guard.
@markijbema markijbema closed this May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant