Skip to content

feat(api): chat completions for any app, from one namespace - #413

Merged
adeelahmad merged 1 commit into
mainfrom
claude/project-thread-iq6ke4
Oct 15, 2026
Merged

adeelahmad merged 1 commit into
mainfrom
claude/project-thread-iq6ke4

Conversation

@adeelahmad

Copy link
Copy Markdown
Owner

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/completions and /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 and lens as 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 with PATCH /api/v1/namespaces/<ns>/integrations or the set_namespace_integrations MCP tool.

How:

  • domain/completions.py reads OpenAI's messages: 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.
  • Tools are off by default: one search and one model call. Owners can offer the assistant's read-only tools (integrations.tool_choices), and then chat.tool_answer runs with a Toolbox limited to those tools, in that namespace. Extensions, hooks, memory and anything that writes are left out, and tool results are wrapped with untrusted.wrap as in chat. A model that can't call tools answers from the excerpts and says so in lens.notice.
  • Citations come back three ways: [n] with a Sources list (left out when the request has a response_format), citations, and lens.sources. Streaming sends OpenAI's chunks ending in data: [DONE], plus SSE comments while the tools run.
  • The person's roles are narrowed to the namespace (deps.within). Model calls go through the activity ledger and count for space:<id>. The namespace's budget gives a 429 insufficient_quota, as does the overall hard limit. Errors use OpenAI's shape.
  • Off by default (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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change; ! in the title)

Checklist:

  • The title is a Conventional Commit.
  • My code follows the code style of this project (make hooks checks it on commit).
  • I added or updated tests (tests/api/test_completions.py, __tests__/connect-apps.test.tsx).
  • My change requires documentation updates, and I have updated docs/ accordingly (docs/api.md, "Chat completions for any app"; docs/mcp.md).
  • Users need more than the title to understand it, so I wrote it under ## Unreleased in CHANGELOG.md.

Done questions (AGENTS.md)

  • Goal: the chat completions part of the ask. I'll merge it on green.
  • Agents: namespace_integrations / set_namespace_integrations MCP tools (chat, tools), the API route, and the endpoint itself.
  • Zero setup: it uses the model already configured; nothing to install.
  • Fewest clicks: one switch, then Copy; tools are checkboxes that save on click.
  • Configurable: the switch and the tools are on the namespace page, off by default, and not in the setup wizard.
  • Light: no new dependency or process. By default it makes one search and one model call.
  • Private and secure: it only reads, within one namespace, with the person's role there. Tool results are wrapped as untrusted. Threat model updated (§1.7 entry row, P-RETRIEVAL).
  • Cost: the cheapest path is the default. Calls are logged in the activity ledger for the namespace, and budgets give a 429.
  • User decides: off until an owner turns it on; existing defaults unchanged.
  • Upgrades: new optional fields on space.integrations (chat, tools). Their absence means off, so no upgrade step is needed.
  • Honest: docs list what isn't done yet: the app's own tools, max_tokens, enforcing response_format, usage counts, streaming the tools' answer piece by piece, and the namespace assistant's memory.
  • Tests, docs and generated files: make openapi regenerated the client. The full backend suite and jest pass locally (with the next PR on top: 1108 and 892).
  • Open standards: OpenAI's chat completions API shape and server-sent events. No new dependencies.
  • Reported in the thread; the refine-later items are listed above and in domain/completions.py.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LB2qrE7CENSd3G5h4FxFra


Generated by Claude Code

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
@adeelahmad adeelahmad self-assigned this Oct 15, 2026
@adeelahmad
adeelahmad merged commit 5858fed into main Oct 15, 2026
11 checks passed
@adeelahmad
adeelahmad deleted the claude/project-thread-iq6ke4 branch October 15, 2026 13:47
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.

2 participants