Repository navigation
feat(api): chat completions for any app, from one namespace - #413
Merged
Merged
Conversation
Each namespace can answer OpenAI's chat completions API at /api/v1/namespaces/<namespace>/chat/completions (and /models): the configured model answers, grounded in that namespace and citing it, streamed or not. Owners turn it on and pick the read tools the model may use under Connect apps, PATCH /namespaces/<ns>/integrations or set_namespace_integrations. Calls go through the activity ledger and the namespace's budget. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LB2qrE7CENSd3G5h4FxFra
This was referenced Oct 15, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requested by Adeel · project thread
Description
Before: an app that wanted answers from Lens had to speak MCP or call the search API and run its own model.
After: each namespace can answer OpenAI's chat completions API at
/api/v1/namespaces/<namespace>(/chat/completionsand/models). Any app that speaks it gets an answer from the configured model, grounded in that namespace and citing it, with a Lens API key as its key andlensas the model. Its owner turns it on under Admin → Namespaces → the namespace → Connect apps, which copies the base URL and picks the read tools the model may use there. It can also be turned on withPATCH /api/v1/namespaces/<ns>/integrationsor theset_namespace_integrationsMCP tool.How:
domain/completions.pyreads OpenAI'smessages: the last user message is the question, earlier user and assistant messages are the conversation, and system or developer messages are the app's own instructions, which come after Lens's rules. It finds the namespace's excerpts (chat.retrieve) and has the model answer from them, citing[n]. With no model set up, the excerpts come back on their own.integrations.tool_choices), and thenchat.tool_answerruns with aToolboxlimited to those tools, in that namespace. Extensions, hooks, memory and anything that writes are left out, and tool results are wrapped withuntrusted.wrapas in chat. A model that can't call tools answers from the excerpts and says so inlens.notice.[n]with a Sources list (left out when the request has aresponse_format),citations, andlens.sources. Streaming sends OpenAI's chunks ending indata: [DONE], plus SSE comments while the tools run.deps.within). Model calls go through the activity ledger and count forspace:<id>. The namespace's budget gives a 429insufficient_quota, as does the overall hard limit. Errors use OpenAI's shape.space.integrations.chat). Off, it answers 403; people with no role there get 404.This is PR 2 of 3 for the thread, after #401 (an MCP server for each namespace). Next: API keys that open one namespace only.
Motivation and Context
Adeel asked for per-namespace chat completion endpoints that use the configured LLM, grounded and with optional configurable tools, so any general app can be integrated easily.
Types of changes
!in the title)Checklist:
make hookschecks it on commit).tests/api/test_completions.py,__tests__/connect-apps.test.tsx).docs/accordingly (docs/api.md, "Chat completions for any app";docs/mcp.md).## UnreleasedinCHANGELOG.md.Done questions (AGENTS.md)
namespace_integrations/set_namespace_integrationsMCP tools (chat, tools), the API route, and the endpoint itself.space.integrations(chat,tools). Their absence means off, so no upgrade step is needed.max_tokens, enforcingresponse_format,usagecounts, streaming the tools' answer piece by piece, and the namespace assistant's memory.make openapiregenerated the client. The full backend suite and jest pass locally (with the next PR on top: 1108 and 892).domain/completions.py.🤖 Generated with Claude Code
https://claude.ai/code/session_01LB2qrE7CENSd3G5h4FxFra
Generated by Claude Code