diff --git a/docs/en/rfcs/3003-dsh-gateway-model-route.md b/docs/en/rfcs/3003-dsh-gateway-model-route.md new file mode 100644 index 0000000000..240f30540d --- /dev/null +++ b/docs/en/rfcs/3003-dsh-gateway-model-route.md @@ -0,0 +1,286 @@ +--- +title: "RFC 3003: Reach the PowerContext gateway from DSH through a model route" +description: A design proposal that lets DSH users install one plugin and use the gateway's whole-turn capture and recall through a selectable model route. +--- + +- Proposal Name: `dsh_gateway_model_route` +- Start Date: 2026-12-03 +- Status: Proposed +- RFC PR: [oceanbase/powercontext#3003](https://github.com/oceanbase/powercontext/pull/3003) +- Tracking Issue: to be created +- Related RFCs: [RFC 2199](2199-powercontext-gateway.md), [RFC 2086](2086-core-inference-gates-and-optional-gateway.md), [RFC 2528](2528-dsh-plugin-ui-and-agent-handoff.md) and [RFC 1892](1892-script-installation.md) +- Related Issues: [#2040](https://github.com/oceanbase/powercontext/issues/2040) + +# Summary + +The PowerContext DSH plugin registers a model route named `powercontext-gateway` in DSH's model selector. When the user selects it, model requests go through the local PowerContext gateway: the gateway recalls and injects context, and captures the whole turn (user input, tool calls, assistant reply) as a Source. When the user selects another model, the plugin behaves as it does today. + +The user installs the plugin the standard DSH way: enter `powercontext-dsh` in **Plugins → Add plugin**. The gateway executable ships in npm platform packages; the Core runtime is downloaded on first use. Core and the gateway run as the current user's persistent service under `powercontext service`. The DSH plugin only makes sure they are running and never starts a second copy. The upstream model's endpoint and key are entered in the plugin panel. + +This document is a design proposal; it does not mean the behavior has shipped. + +# Motivation + +DSH users have two integration paths today, and each has a clear gap. + +**The plugin path only remembers what the user said.** The plugin captures the user prompt at the start of each turn; assistant replies and tool results never become Sources. In a real run, a fact the user stated was recalled in a new session, while a decision the assistant made after a tool call was not stored and the new session answered "I do not know". Every hook-based host plugin is designed this way; whole turns are captured only through the gateway. + +**The plugin path has a large fixed cost.** The plugin exposes all 63 of the Server's MCP tools to the model. For the same task, the first request is 4,389 input tokens without the plugin and 34,432 with it ([#2040](https://github.com/oceanbase/powercontext/issues/2040) measured the same kind of cost on other hosts). + +**The gateway path works well but is hard to install.** The current documentation asks the user to install Core, download the gateway, hand-write a provider in `cordis.patch.yml`, configure Core's upstream target, set several groups of environment variables, and start three processes in three terminals. DSH's own plugin installation takes one name. + +**The documented installation does not work today.** `powercontext-dsh` is not published to npm, so entering the package name in DSH as documented returns 404. + +The goal is to deliver the gateway path's results with the plugin path's installation experience. + +# Guide-level explanation + +## Install + +1. In DSH, open **Plugins → Add plugin**, enter `powercontext-dsh`, and restart after installation. +2. Open the PowerContext panel in the sidebar. On first open the panel shows "runtime not prepared"; **Prepare** downloads and verifies the Core runtime, then registers and starts the local service. +3. On the panel's **Models** page, choose a provider preset (DeepSeek, OpenAI, or Custom), enter the model name and key, and save once **Test connection** succeeds. The upstream key is kept in the local service's protected configuration (mode 0600); it does not go into the DSH profile or DSH's credential store. + +These steps describe DSH Desktop, the primary path. On `dsh web` the panel's write operations are available only after the local owner is confirmed; see section 6. + +After that, DSH's model selector shows **PowerContext gateway** with the models entered. + +## Use + +- Select a model under **PowerContext gateway** in a session: the gateway recalls, injects, and captures for that session. The plugin registers no PowerContext MCP tools with the model and sends no recall or capture requests of its own. +- Select any other model: the plugin works as it does today (prompt capture, one recall per turn, MCP tools). +- On the gateway route, a conversation continues when Core is unreachable, but that turn uses no memory; the panel and the status bar show "this turn did not use memory" with the reason. +- Both kinds of session can exist at the same time. `/pc`, `/pc doctor`, the panel, and the Dashboard work in both. + +## Service lifecycle + +Core and the gateway are the current user's persistent service and do not start or stop with DSH: + +- When DSH starts, the plugin probes the service; if it is not running and the user did not stop it, the plugin asks for it to be started. +- If it is already running, the plugin does nothing. Several DSH processes (Web, Desktop, headless) share one service. +- After the user stops it with `powercontext service stop`, the plugin does not start it again; the panel shows "stopped manually" and the command that resumes it. +- The same service is available to other Agents' PowerContext integrations. + +## Example + +In one session the user asks the assistant to run a script and choose a release codename from its output. The assistant's reply and tool calls are captured with the turn. The next day, in a new session, the user asks "what release codename did we choose?". The gateway injects the related memory before the request reaches the model, and the assistant answers directly. The user never calls a memory tool. + +# Reference-level explanation + +## 1. Responsibilities + +| Component | Responsibility | +| --- | --- | +| DSH plugin | Register the model route; switch its own behavior by the session's selected model; panel configuration; bootstrap the runtime; make sure the service is running | +| `powercontext service` | Register, start, stop, and restart Core and the gateway after a crash; keep the user's explicit stop | +| Gateway | Recall and injection, whole-turn capture, the conversation prefix that prefix extraction needs | +| Core | Storage, extraction, retrieval; admission checks on requests the gateway forwards | + +The plugin does not own the Core or gateway processes, does not introduce the standalone Ops bundle, and does not modify DSH source code. + +## 2. Registering the model route + +The plugin uses DSH's settings service to write one route into the `providers` of the built-in adapter `llm-pi-ai`: + +```yaml +- id: llm-pi-ai + config: + providers: + powercontext-gateway: + displayName: PowerContext gateway + api: openai-responses + baseURL: http://127.0.0.1:8000/gateway/v1 + apiKeyEnv: POWERCONTEXT_DSH_GATEWAY_KEY + compat: + supportsStrictMode: true + models: + - id: +``` + +- The write is the settings service's merging update, so the user's other providers are kept. +- The route declares `compat.supportsStrictMode`, as the gateway provider snippet in the integration guide does, so function tools are sent with an explicit `strict: false`. +- The route lives in the user's profile layer. DSH's plugin management offers no removal hook, so the route is removed by the plugin's `/pc route remove` command; removing the plugin only through the DSH interface leaves the route in the model selector, and the documentation tells users to run the removal command first. `powercontext setup dsh --remove` keeps its rule of not rewriting the user's patch: when the patch still declares the route, its result says so. +- A profile without the settings service (headless, for example) does not get the route automatically; the documentation provides the equivalent patch fragment. +- A second `llm-pi-ai` instance is not mounted: it fails to load because it declares the built-in provider directory again. +- `baseURL` points at the local service's single external port; see section 6. +- The plugin does not generate the gateway access key. `powercontext service` allocates it and keeps it in the PowerContext data directory, where every host that shares the service reads it; the plugin only passes the value it read to `llm-pi-ai` through DSH's credential service as `POWERCONTEXT_DSH_GATEWAY_KEY` and never rewrites it. When the service runs without an access key on loopback, this credential has no authentication role. Changing the process environment at runtime has no effect on DSH, which reads a snapshot of the environment taken at launch. + +## 3. Switching per session + +The plugin follows the model selection that DSH fixes for each step: + +| Behavior | Gateway route selected | Another route selected | +| --- | --- | --- | +| Plugin's prompt-time recall and injection | off | current behavior | +| Plugin's prompt capture | off | current behavior | +| PowerContext MCP tools | hidden from that Agent | current behavior | +| Guidance | omitted from that step's system prompt | current behavior | +| Skill | stays registered | current behavior | +| `powercontextCompanionMemory` service | unavailable for that session | current behavior | +| `X-PowerContext-Scope-ID` / `X-PowerContext-Conversation-ID` / `X-PowerContext-Turn` headers | sent | not sent | +| `/pc` commands, panel, Dashboard | available | available | + +Implementation constraints: + +- DSH resolves a step's tool set before `agent/pre-step`. The tools are therefore hidden on `agent/created` from the Agent's current model selection, and synchronized again on every pre-step for the steps that follow. +- The first step after a mid-session model change may still have the previous tool visibility; this is a known boundary, see the unresolved questions. +- When a session into which the plugin injected snapshots moves to the gateway route, Core rejects the prepared-context blocks in its history. The plugin tells the user to start a new session when that switch happens. +- DSH assembles the system prompt per step, with that step's model selection, so the guidance can be omitted per step. A Skill is registered globally in DSH and there is no interface to hide it per session; it stays visible on the gateway route, where the MCP tools it describes are unavailable. +- The content returned by `powercontextCompanionMemory` carries a prepared-context block, and the gateway rejects a block it did not inject itself, so the service is unavailable for sessions on the gateway route. The `powercontext-dsh-pet` adapter does not use this service; it reaches Core directly with the pet's own bound Scope and is not affected by per-session switching. +- Model requests on this route that are not turns of a session are marked `X-PowerContext-Turn: internal`: requests DSH marks with a `purpose` (compaction, session titles, auto review), and requests that did not pass a session pre-step, such as requests made by other plugins in the same host. The gateway only forwards them. +- The existing global switches `promptRecall` and `capturePrompts` stay, for users who deploy the gateway themselves. + +## 4. Scope + +The plugin sends the Scope it resolves for the current working directory to the gateway in `X-PowerContext-Scope-ID`, so the gateway's recall and capture land in the same Scope as the `/pc` commands. Resolution uses the plugin's existing resolver: Core resolves the workspace binding, with fallback to the Server's default Scope allowed. When resolution succeeds, the plugin sends that Scope explicitly, whether it is a bound Scope or the Server's default Scope. + +When the resolution request fails, times out, or returns no usable Scope, the plugin does not let the gateway choose another Scope: it sends `X-PowerContext-Turn: internal` for that request, and the gateway forwards it without recall, injection, or capture. This matches the plugin path's existing behavior of skipping recall and capture when no Scope is resolved. On this route, every request from the plugin therefore either carries a resolved Scope or is marked as not taking part in memory; these requests never use the gateway instance's default Scope. + +## 5. Distribution + +| Artifact | Channel | Notes | +| --- | --- | --- | +| `powercontext-dsh` | npm | The existing release job; the first publication still has to happen | +| `powercontext-gateway--` | npm, as optional dependencies of the plugin | Each package holds one executable, about 10 MB; the package manager selects by platform; a download channel only | +| Core runtime archive | GitHub Release, downloaded on first use | Carries its own interpreter and dependencies; the user needs no Python, uv, or Git | + +The Core runtime reuses the output of the managed resource producer (`prepare_managed_resources.py` and `produce_managed_resources.py`). The plugin downloads from the address in a pinned manifest, verifies the SHA-256, and unpacks into a version-named subdirectory of the PowerContext data directory. The rendering layer cannot supply download addresses, commands, or installation paths. + +The gateway's npm platform packages are only a download channel. Preparation copies the executable into the PowerContext data directory, where it is managed per version alongside the Core runtime; the service definition refers to that copy and not to DSH's `node_modules`. Removing or upgrading `powercontext-dsh` therefore does not move or delete a file that a service shared with other Agents depends on. + +The runtime archive is currently about 205 MB, 745 MB unpacked, of which the interpreter's shared library and executables are stored as full duplicate copies (about 250 MB). Removing these duplicates, and providing a download mirror reachable from mainland China, are prerequisites of delivery item 5. + +## 6. Service management + +**Deployment shape.** The `powercontext service` definition is extended to cover the gateway: the gateway is registered, started, and stopped together with Core, in the combined deployment, with a UDS between the two and a single loopback port to the outside. The port stays 8000, so the existing `/pc`, Dashboard, CLI, and Desktop clients keep their address. This requires lifting the restriction in `service/launcher.py` that currently rejects UDS for personal services. In the combined deployment recall injection is on by default, and capture is turned on explicitly with `PC_GATEWAY_SEDIMENT=on`. When the gateway stops, Core is unreachable as well; the remedy in that case is to recover the gateway, not to bypass it. + +**Ensuring the service runs.** + +- At startup the plugin first probes the service's health endpoint. When it answers, the plugin runs no command; `service status` costs about 4 seconds to start and stays off the fast path. +- When the probe fails, the plugin reads `service status --json`: not installed → `install` after the user confirms; installed, not running, and not stopped manually → `start`; stopped manually → show the state only. +- The service definition binds the absolute paths of the runtime and of the gateway executable in the PowerContext data directory. After an upgrade, run `install` again; when a database migration is required, complete it before starting. + +**Keys.** Two different keys are involved: + +| Key | Purpose | Ownership and storage | +| --- | --- | --- | +| Gateway access key | A host reaching the local gateway | Allocated by `powercontext service`, kept in the PowerContext data directory; hosts only read it | +| Upstream API key | The gateway and Core reaching the upstream model | Kept in the service's protected configuration (mode 0600); the panel never displays it | + +**Ownership of configuration.** Several clients (Desktop, the CLI, the DSH plugin) share one service. Who may change the service's configuration, how changes are merged, and how the service is registered again after the environment file's pinned identity changes are specified by a separate configuration management design. This proposal does not define those rules; delivery item 6 (the panel's model configuration page) aligns with that design once it is settled. When the machine already has a service, for example one registered by Desktop, the plugin reuses it: it does not register it again and does not change the runtime it is bound to. + +**Behavior when Core is unreachable.** The managed service runs with `PC_GATEWAY_DEGRADE=passthrough`: when the gateway is running but cannot use Core, it forwards requests without recall or capture. That is acceptable only if the user can see that memory is not in effect, so the plugin must show it: on this route the plugin resolves the Scope through Core before every step, a failed resolution marks the request as not taking part in memory as described in section 4, and the panel and the status bar show "this turn did not use memory" with the reason. When the whole service is stopped, requests cannot be forwarded, and the plugin's error message offers the two ways to recover: start the service, or switch to a direct model. + +**The panel's write operations.** The panel grows from a read-only status view into something that downloads and unpacks a runtime, registers a service that starts at login, stores the upstream key, and changes DSH's model configuration. These operations keep the status interface's existing local-owner decision, without loosening it: + +| Host | Panel write operations | +| --- | --- | +| DSH Desktop | Available, with a confirmation before each operation | +| `dsh web` with `ownerLocal` configured | Same as Desktop | +| `dsh web` without a confirmed local owner | Not available; the panel shows the current state and the equivalent terminal commands | + +## 7. Providers, models, and protection mode + +- One gateway instance has one upstream endpoint per protocol (one for Chat Completions, one for Responses), or one subscription channel; only one subscription channel can be selected at a time, and it replaces the upstream API key. The first version of the panel configures one provider that uses an API key and offers three presets: DeepSeek, OpenAI, and Custom. A preset fixes the protocol and the endpoint, so the user does not have to decide between Responses and Chat Completions. **Test connection** sends one minimal request with the entered model before saving. +- The route prefers the Responses protocol. DeepSeek's API offers a stateless Responses-compatible interface (`base_url` `https://api.deepseek.com`, streaming and function tools supported, `store` and `previous_response_id` not supported), so DSH's default models also use the Responses route; providers that offer only Chat Completions use the Chat route. +- Several models of one provider can be registered. With Core protection mode `disabled`, the gateway forwards by the model name in the request and is not limited to one model. +- This proposal targets individual users and is designed and accepted only under Core's default `disabled` protection mode. `enforcing` mode requires each target to declare its model, window, and cache policy and belongs to team deployments; the panel does not generate that configuration and does not offer this route in that mode. +- Using several providers at once needs several gateway instances or changes to the gateway itself and is out of scope. +- The model used for memory extraction is configured on the Models page and defaults to "automatic". In automatic mode, extraction continues from the request prefix retained for the turn, so it uses the model of that turn's conversation and reuses its prefix cache; when the prefix is unavailable or is not cheaper than ordinary extraction, it falls back to Core's generation model, which defaults to the first model on the Models page. When the user names an extraction model, that model is used. Extraction is billed to the user's upstream account, and the Models page says so. + +## 8. Relation to the existing paths + +- The source installation through `powercontext setup dsh` stays, for development and debugging. +- Deploying the gateway by hand stays; when the user already declares the route, the plugin does not overwrite it. +- Registering the `recommended` MCP toolset by default ([#2040](https://github.com/oceanbase/powercontext/issues/2040)) is independent of this proposal and can be delivered separately. + +## 9. Implementation and acceptance + +Suggested delivery order; each item is usable on its own: + +1. Complete the first npm publication of `powercontext-dsh`; until then the documentation recommends the source installation. +2. The plugin registers the model route, switches per session, and provides the command that removes the route (with a gateway the user runs). +3. The plugin sends the Scope header, delivered in the same change as item 2, so the gateway route never uses the gateway instance's default Scope. +4. npm platform packages for the gateway; `powercontext service` covers the gateway in the combined deployment and allocates the gateway access key. +5. Publication of the Core runtime archive, and the plugin's bootstrap installation. Prerequisites: an archive without duplicates, and a download mirror reachable from mainland China. +6. The panel's model configuration page, aligned with the separate configuration management design. +7. The gateway forwards streamed output to the client as it arrives in the `disabled` protection mode. Core permits early release per request in the allowed `before_send` result; the gateway buffers the reply while forwarding it, and the release record and capture complete after the stream ends. A client that disconnects before the reply ends cancels the call; a disconnect after the reply is complete does not cancel the remaining work, which finishes within a bounded time while the concurrency permit stays held. The Chat Completions route is delivered first, then the Responses route. Until this is delivered, the documentation does not present the route as the default recommendation. + +Acceptance has to run on a real DSH host and distinguish real execution from fixtures: + +- In a new session on the gateway route, a decision the assistant made itself is recalled in another new session, and the recall comes from the gateway's injection rather than a guess by the model. +- In the same installation, a session on another route keeps the current behavior and the gateway receives no request. +- Starting DSH repeatedly, and running several DSH processes at once, leaves the service's main process unchanged. +- After the user stops the service manually, starting DSH does not resume it. +- After the removal command, no route or credential is left in the user's profile. +- When Scope resolution fails or Core is unreachable, a conversation on the gateway route continues, no Source is added to the gateway's default Scope, and the panel shows that the turn did not use memory. +- After delivery item 7, a reply on the gateway route appears progressively, and the delay before the first output is comparable to calling the same model directly. +- With DeepSeek's official endpoint as the upstream, a multi-step turn with tool calls completes through the Responses route, is captured, and is recalled in a new session. + +## 10. Existing evidence + +The following results come from real execution on one Linux x86_64 host: DSH `0.2.2-rc.5` (headless and web profiles), PowerContext master `9e40f8996`, model `gpt-4.1-mini` (api.openai.com). + +| Check | Result | +| --- | --- | +| Plugin path: a new session recalls the assistant's decision | Not recalled; the Sources hold only user prompts | +| Through the gateway: a new session recalls the assistant's decision | Recalled correctly; the Sources are whole turns | +| Input tokens of the first request | No plugin 4,389; plugin path 34,432; gateway route 6,000–7,400 | +| Registering the route through the settings service | Written to the profile layer and merged with an existing provider | +| Switching by the selected model in one installation | On the gateway route the plugin sends no recall or capture request; on other routes the current behavior stays | +| Producing and smoke-testing the runtime archive on Linux | Passed; Core starts in an environment without a system Python | +| Registering with `powercontext service` on Linux | Repeated `start`/`install` keep the main process; recovery after a crash; a manual stop is kept | +| One gateway instance forwarding several models (protection mode `disabled`) | `gpt-4.1-mini` and `gpt-4o-mini` both forwarded | +| The plugin sends the Scope header on the gateway route | The Source landed in the Scope the plugin resolved; nothing was added to the gateway's default Scope | +| Scope resolution fails on the gateway route | The turn completed; the gateway recorded the requests as internal; no Source was added in any Scope | +| Time until the first delta of a streamed request through the gateway (protection mode `disabled`) | Two runs each on the Chat Completions and Responses routes; the first delta always arrived when the whole reply ended (11–17 seconds) | +| Live forwarding on the Chat Completions route (the build of [#3035](https://github.com/oceanbase/powercontext/pull/3035), protection mode `disabled`) | The first body byte arrived after 0.41–1.62 seconds (whole reply 9.4–11.5 seconds); three runs read to the end and three closed at `[DONE]`, all captured; two runs closed in the middle of the reply were cancelled and added no Source; two runs in `shadow` mode still arrived at once when the reply ended | + +Not verified: Windows (including WSL2) and macOS; DSH Desktop; changing the model in the middle of a session; a real npm optional-dependency installation; a service definition that covers the gateway; DeepSeek models through the gateway (its Responses API is stateless and supports streaming and function tools); providers other than OpenAI; model requests made through the gateway route by other plugins in the same host (for example dsh-pet). + +# Drawbacks + +- **An API key for the upstream model is required.** DSH defaults to the official models through a DeepSeek account login. The account login and an API key use the same inference service and differ only in the credential: the former sends a login token, the latter a key. The gateway does not read DSH's login token, so these users first create an API key on the DeepSeek platform and enter it in the panel. +- **No live streaming on the gateway route.** The gateway returns content to the client only after the release check passes: it receives the whole upstream reply and then replays it as stream events at once. The user sees no output until the entire reply has been generated, which is most noticeable with reasoning models and long code replies, and a reply above the response buffer limit (1 MiB) fails. This holds on both the Chat Completions and the Responses route. In the `disabled` protection mode Core's release check does not inspect the reply, so this wait protects nothing; delivery item 7 removes it. +- **The gateway route is unavailable while the service is stopped.** The plugin path only loses memory when Core is unreachable; on the gateway route the model request itself goes through the local service, so a stopped service means starting it or switching models. +- **On the gateway route the model cannot call memory tools.** Model-initiated search, explicit remembering, handoff, and candidate review are unavailable on that route; the user does them with `/pc` commands. +- **Windows needs WSL2.** The gateway depends on Unix sockets and Unix signals and has no native Windows executable; per the current documentation the gateway and Core run together in WSL2. The first version does not install or start the service automatically on Windows; the documentation gives the manual steps inside WSL2. +- **Skill and removal are cleaned up incompletely.** The Skill stays visible on the gateway route, and removing the plugin only through the DSH interface leaves the model route. Both follow from the interfaces DSH offers today. +- **One more step on `dsh web`.** Without a confirmed local owner the panel performs no write operation, and the user completes preparation and configuration in a terminal. +- **The first preparation downloads a large runtime.** +- **A persistent service.** The plugin registers a user service that starts at login on the user's machine; the panel has to say so clearly and obtain confirmation. + +# Rationale and alternatives + +**Why send model requests through the gateway instead of capturing assistant replies in the plugin.** DSH emits `agent/assistant-stream`, so the plugin can read the assistant's output in process. That would implement turn capture once more in every host's hook plugin and would not benefit from the extraction cost savings on the gateway's conversation prefix. It stays as a fallback for account-login users; see future possibilities. + +**Why a model route instead of a global switch.** A route makes "use the gateway or not" one choice in the model selector, switchable per session and without restarting DSH. + +**Why reuse `llm-pi-ai` instead of registering an adapter.** DSH lets a plugin register an adapter with `ctx.llm.registerAdapter`, but the plugin would then implement protocol translation, stream chunks, and replay state itself. `llm-pi-ai` already supports hand-declared OpenAI-compatible gateways. + +**Why manage everything with `powercontext service` instead of child processes of the plugin.** A child process has no clear owner when several DSH processes run; Core exiting with DSH leaves background extraction no time to finish; other Agents' integrations lose Core when DSH closes. A persistent service plus "only make sure it is running" avoids all three and reuses the existing crash recovery and explicit-stop semantics. + +**Why not the standalone Ops bundle.** Ops is valuable because it can repair a Server whose interpreter is broken, at the price of distributing another runtime of about 205 MB. In this proposal the plugin downloads, verifies, and unpacks, and leaves the rest of the lifecycle to `powercontext service` inside the runtime. + +# Prior art + +- The gateway integrations of Codex and OpenCode: the host's configuration points the model endpoint at the gateway, and the gateway path installs no plugin. +- Desktop's managed local Server: pinned resources, digest verification, and user-service registration. +- DSH's own native modules are distributed per platform as `node-addon-system--`. + +# Unresolved questions + +- Whether the first step after a mid-session model change needs extra handling of tool visibility, and how to prompt the user when a session with plugin snapshots in its history moves to the gateway route. +- Whether to support the DeepSeek account login separately (a Messages ingress or a dedicated channel in the gateway), so these users need no API key. That work is independent of this proposal. +- How the plugin would install and start the service inside WSL2 on Windows, and whether loopback access between WSL2 and DSH on Windows is reliable. +- Live forwarding on the Responses route: the gateway currently sends the upstream request without streaming. Forwarding live requires asking the provider to stream when the upstream request is rendered, before `before_send`, while the permission arrives only in the `before_send` result; how the gateway learns before the check that early release is available (for example a hint in Core's capability discovery) is not decided. +- Whether DSH offers an interface to show "this turn did not use memory" inside the session; without one the notice appears only in the panel and the status bar. +- Whether several providers need to be configured at once, and whether several gateway instances or routing inside the gateway should support that. +- The size of the runtime archive after removing duplicates, and who provides and maintains the download mirror. +- Extraction quality with a small model as Core's generation model: in the verification, one turn stated three facts and only two became memories. + +# Future possibilities + +- Whole-turn capture without the gateway for account-login users: the plugin reads the assistant's output from `agent/assistant-stream` and captures it. +- Restore a small set of read-only tools (memory search, for example) on the gateway route, in the same Scope the gateway uses. +- Bring the same "model route plus one managed service" pattern to other hosts that let a plugin register a model provider. diff --git a/docs/zh/rfcs/3003-dsh-gateway-model-route.md b/docs/zh/rfcs/3003-dsh-gateway-model-route.md new file mode 100644 index 0000000000..2a44b7b56d --- /dev/null +++ b/docs/zh/rfcs/3003-dsh-gateway-model-route.md @@ -0,0 +1,286 @@ +--- +title: "RFC 3003: 在 DSH 中以模型路由接入 PowerContext gateway" +description: 让 DSH 用户只安装一个插件,就能通过可选择的模型路由使用 gateway 的完整回合采集与召回的设计提案。 +--- + +- Proposal Name: `dsh_gateway_model_route` +- Start Date: 2026-12-03 +- Status: 提案 +- RFC PR: [oceanbase/powercontext#3003](https://github.com/oceanbase/powercontext/pull/3003) +- Tracking Issue: 待创建 +- Related RFCs: [RFC 2199](2199-powercontext-gateway.md)、[RFC 2086](2086-core-inference-gates-and-optional-gateway.md)、[RFC 2528](2528-dsh-plugin-ui-and-agent-handoff.md) 和 [RFC 1892](1892-script-installation.md) +- Related Issues: [#2040](https://github.com/oceanbase/powercontext/issues/2040) + +# Summary + +PowerContext DSH 插件在 DSH 的模型选择器中登记一条名为 `powercontext-gateway` 的模型路由。用户选中它,模型请求就经过本机的 PowerContext gateway:gateway 负责召回和注入,并把完整回合(用户输入、工具调用、助手回复)采集为 Source。用户选回其他模型,插件恢复现有行为。 + +用户按 DSH 的标准方式安装插件:在 **Plugins → Add plugin** 中输入 `powercontext-dsh`。gateway 可执行文件随 npm 平台包分发;Core 的运行环境在首次使用时下载。Core 和 gateway 作为当前用户的常驻服务由 `powercontext service` 统一管理,DSH 插件只确保它们在运行,不启动第二份。上游模型的地址和密钥在插件面板中填写。 + +本文是设计提案,不代表相关行为已经发布。 + +# Motivation + +DSH 用户今天有两条接入路径,各有明显缺口。 + +**插件路径只记得住用户说过的话。** 插件在每轮开始时采集用户提示词,助手回复和工具结果不进入 Source。实测中,用户陈述的事实能在新会话召回,助手在工具调用后做出的决定没有入库,新会话回答“不知道”。所有基于 hook 的宿主插件都是这个设计;要采集完整回合只能经过 gateway。 + +**插件路径的固定开销大。** 插件向模型暴露 Server 的全部 63 个 MCP 工具。同一任务的首个请求,不装插件是 4,389 输入 token,装了是 34,432([#2040](https://github.com/oceanbase/powercontext/issues/2040) 在其他宿主上量到同类开销)。 + +**gateway 路径效果好,但装不起来。** 现有文档要求用户分别安装 Core、下载 gateway、手写 `cordis.patch.yml` 声明 provider、配置 Core 的上游目标、设置多组环境变量,并在三个终端中分别启动。DSH 自己的插件安装只需要输入一个名字。 + +**文档推荐的安装方式目前不可用。** `powercontext-dsh` 尚未发布到 npm,按文档在 DSH 中输入包名会得到 404。 + +目标是把 gateway 路径的效果装进插件路径的安装体验里。 + +# Guide-level explanation + +## 安装 + +1. 在 DSH 中打开 **Plugins → Add plugin**,输入 `powercontext-dsh`,安装后重启。 +2. 打开侧栏的 PowerContext 面板。首次打开时面板显示“尚未准备运行环境”,点击 **准备** 后下载并校验 Core 运行环境,注册并启动本机服务。 +3. 在面板的 **模型** 页选择供应商预设(DeepSeek、OpenAI 或自定义),填写模型名和密钥,**测试连接** 通过后保存。上游密钥保存在本机服务的受保护配置中(模式 0600),不进入 DSH 的 profile 或凭据存储。 + +以上步骤以 DSH Desktop 为主路径。在 `dsh web` 上,面板的写操作只在确认了本机所有者之后开放,见第 6 节。 + +完成后,DSH 的模型选择器中出现 **PowerContext gateway** 及所填的模型。 + +## 使用 + +- 在会话中选择 **PowerContext gateway** 下的模型:这一会话的召回、注入和采集由 gateway 完成。插件不再向模型注册 PowerContext 的 MCP 工具,也不发送自己的召回和采集请求。 +- 选择其他模型:插件按现有方式工作(提示词采集、每轮一次召回、MCP 工具)。 +- gateway 路由上,Core 不可达时对话照常进行,但这一回合不使用记忆;面板和状态栏显示“本回合未使用记忆”及原因。 +- 两种会话可以同时存在。`/pc`、`/pc doctor`、面板和 Dashboard 在两种会话中都可用。 + +## 服务的生命周期 + +Core 和 gateway 是当前用户的常驻服务,不随 DSH 启停: + +- DSH 启动时,插件探测服务;未在运行且不是用户手动停止的,插件请求启动。 +- 已在运行时插件什么都不做。多个 DSH 进程(Web、Desktop、headless)共用同一份服务。 +- 用户用 `powercontext service stop` 手动停止后,插件不会重新拉起,面板显示“已手动停止”和恢复命令。 +- 同一份服务也可供其他 Agent 的 PowerContext 集成使用。 + +## 示例 + +用户在一个会话中让助手运行脚本并据此选定发布代号。助手的回复和工具调用随这一回合被采集。第二天在新会话中询问“上次定的发布代号是什么”,gateway 在请求到达模型前注入相关记忆,助手直接回答。整个过程中用户没有调用任何记忆工具。 + +# Reference-level explanation + +## 1. 职责划分 + +| 组件 | 职责 | +| --- | --- | +| DSH 插件 | 登记模型路由;按会话所选模型切换自身行为;面板配置;引导安装运行环境;确保服务在运行 | +| `powercontext service` | Core 与 gateway 的注册、启动、停止、崩溃重启;保留用户的显式停止 | +| gateway | 召回与注入、完整回合采集、前缀抽取所需的会话前缀 | +| Core | 存储、抽取、检索;对 gateway 转发请求的准入检查 | + +插件不拥有 Core 或 gateway 进程,不引入独立的 Ops 工具包,也不修改 DSH 源码。 + +## 2. 登记模型路由 + +插件通过 DSH 的设置服务向内置适配器 `llm-pi-ai` 的 `providers` 写入一条路由: + +```yaml +- id: llm-pi-ai + config: + providers: + powercontext-gateway: + displayName: PowerContext gateway + api: openai-responses + baseURL: http://127.0.0.1:8000/gateway/v1 + apiKeyEnv: POWERCONTEXT_DSH_GATEWAY_KEY + compat: + supportsStrictMode: true + models: + - id: <模型名> +``` + +- 写入使用设置服务的合并更新,保留用户已有的其他 provider。 +- 路由声明 `compat.supportsStrictMode`,与集成文档中的 gateway provider 片段一致,使函数工具带着显式的 `strict: false` 发送。 +- 路由写在用户的 profile 层。DSH 的插件管理没有提供卸载钩子,因此路由由插件的 `/pc route remove` 命令移除;只通过 DSH 界面卸载插件会在模型选择器中留下这条路由,文档说明卸载前先执行移除命令。`powercontext setup dsh --remove` 沿用不改写用户 patch 的规则:patch 中仍声明这条路由时,它在结果中给出提示。 +- 没有设置服务的 profile(例如 headless)不会自动登记,文档提供等价的 patch 片段。 +- 不再挂载第二个 `llm-pi-ai` 实例:它会因重复声明内置 provider 目录而加载失败。 +- `baseURL` 指向本机服务对外的唯一端口,见第 6 节。 +- gateway 的访问密钥不由插件生成。它由 `powercontext service` 分配并保存在 PowerContext 数据目录中,由共用这份服务的所有宿主读取;插件只把读到的值通过 DSH 的凭据服务以 `POWERCONTEXT_DSH_GATEWAY_KEY` 提供给 `llm-pi-ai`,不改写它。服务在回环上未启用访问密钥时,这个凭据不承担鉴权作用。运行时修改进程环境变量对 DSH 无效,因为 DSH 读取的是启动时的环境快照。 + +## 3. 按会话切换 + +插件以每一步固定的模型选择为准: + +| 行为 | 选中 gateway 路由 | 选中其他路由 | +| --- | --- | --- | +| 插件的提示词时召回与注入 | 关闭 | 现有行为 | +| 插件的提示词采集 | 关闭 | 现有行为 | +| PowerContext MCP 工具 | 对该 Agent 隐藏 | 现有行为 | +| 使用指引 | 从该步的系统提示中省略 | 现有行为 | +| Skill | 保持登记 | 现有行为 | +| `powercontextCompanionMemory` 服务 | 对该会话返回不可用 | 现有行为 | +| `X-PowerContext-Scope-ID` / `X-PowerContext-Conversation-ID` / `X-PowerContext-Turn` 请求头 | 发送 | 不发送 | +| `/pc` 命令、面板、Dashboard | 可用 | 可用 | + +实现约束: + +- DSH 在 `agent/pre-step` 之前就解析好这一步的工具集。因此工具的隐藏在 `agent/created` 时按 Agent 当前的模型选择执行,并在每个 pre-step 重新同步,供之后的步骤使用。 +- 会话中途切换模型后的第一步可能仍带着切换前的工具可见性;这是已知边界,见未决问题。 +- 插件曾注入过快照的会话切换到 gateway 路由时,历史中的 prepared-context 块会被 Core 拒绝。插件在切换时提示用户新建会话。 +- DSH 按步组装系统提示,并带有该步的模型选择,因此使用指引可以按步省略。Skill 在 DSH 中是全局登记的,没有按会话隐藏的接口;它在 gateway 路由上保持可见,其内容描述的 MCP 工具在该路由上不可用。 +- `powercontextCompanionMemory` 返回的内容带有 prepared-context 块,gateway 会拒绝不是它自己注入的块,因此该服务对 gateway 路由上的会话返回不可用。`powercontext-dsh-pet` 适配器不使用这个服务,它直接访问 Core 并使用宠物自己绑定的 Scope,不受按会话切换影响。 +- 这条路由上不属于会话回合的模型请求标记为 `X-PowerContext-Turn: internal`:DSH 以 `purpose` 标记的请求(压缩、会话标题、自动审查),以及没有经过会话前置步骤的请求,例如同一宿主中其他插件发起的请求。gateway 对这些请求只转发。 +- 现有的全局开关 `promptRecall` 和 `capturePrompts` 保留,用于用户自行部署 gateway 的场景。 + +## 4. Scope + +插件把为当前工作目录解析出的 Scope 通过 `X-PowerContext-Scope-ID` 发送给 gateway,使 gateway 的召回与采集和 `/pc` 命令落在同一个 Scope。Scope 的解析沿用插件现有的解析器:由 Core 按工作区绑定解析,并允许回退到 Server 的默认 Scope。解析成功时,无论结果是绑定的 Scope 还是 Server 的默认 Scope,插件都显式发送该 Scope。 + +解析请求失败、超时或没有返回可用的 Scope 时,插件不会让 gateway 改用其他 Scope:它为该请求发送 `X-PowerContext-Turn: internal`,gateway 只转发请求,不召回、不注入、不采集。这与插件路径现有的行为一致,即解析不出 Scope 时跳过召回与采集。因此在这条路由上,插件发出的每个请求要么带有解析出的 Scope,要么被标记为不参与记忆;gateway 实例的默认 Scope 不会被这些请求使用。 + +## 5. 分发 + +| 制品 | 渠道 | 说明 | +| --- | --- | --- | +| `powercontext-dsh` | npm | 现有发布作业;需要完成首次发布 | +| `powercontext-gateway--` | npm,作为插件的可选依赖 | 每个包只含一个可执行文件,约 10 MB;由包管理器按平台选择;只作为下载渠道 | +| Core 运行环境归档 | GitHub Release,首次使用时下载 | 自带解释器和依赖,用户不需要 Python、uv 或 Git | + +Core 运行环境复用托管资源生产器(`prepare_managed_resources.py` 与 `produce_managed_resources.py`)的产物。插件按固定清单中的地址和 SHA-256 下载并校验,解包到 PowerContext 数据目录下的按版本命名的子目录。渲染层不能提供下载地址、命令或安装路径。 + +gateway 的 npm 平台包只是下载渠道。准备阶段把其中的可执行文件复制到 PowerContext 数据目录,与 Core 运行环境一起按版本托管;服务定义引用的是这份拷贝,不引用 DSH 的 `node_modules`。卸载或升级 `powercontext-dsh` 因此不会移动或删除其他 Agent 共用的服务所依赖的文件。 + +运行环境归档目前约 205 MB,解包后约 745 MB,其中解释器的共享库和可执行文件各有完整的重复拷贝(约 250 MB)。去除这些重复,并提供在中国大陆可达的下载镜像,是第 5 项交付的前置条件。 + +## 6. 服务管理 + +**部署形态。** `powercontext service` 的服务定义扩展为同时覆盖 gateway:gateway 与 Core 一起注册、启动和停止,采用组合部署,两者之间使用 UDS,对外只监听一个回环端口。端口沿用 8000,现有的 `/pc`、Dashboard、CLI 和 Desktop 客户端不需要改地址。这要求放开 `service/launcher.py` 目前对个人服务使用 UDS 的限制。组合部署下召回注入默认开启,采集通过 `PC_GATEWAY_SEDIMENT=on` 显式开启。gateway 停止时 Core 也不可达;这种情况下应当恢复 gateway,而不是绕过它。 + +**确保运行。** + +- 插件启动时先探测服务的健康端点。正常时不调用任何命令;`service status` 的启动开销约 4 秒,不放在快速路径上。 +- 探测失败时,插件读取 `service status --json`:未安装则在用户确认后 `install`;已安装、未运行且不是手动停止则 `start`;手动停止则只显示状态。 +- 服务定义绑定运行环境和 gateway 可执行文件在 PowerContext 数据目录中的绝对路径。升级后重新执行 `install`;需要数据库迁移时先完成迁移,再启动。 + +**密钥。** 涉及两个不同的密钥: + +| 密钥 | 用途 | 归属与存放 | +| --- | --- | --- | +| gateway 访问密钥 | 宿主访问本机 gateway | 由 `powercontext service` 分配,保存在 PowerContext 数据目录;各宿主只读取 | +| 上游 API 密钥 | gateway 与 Core 访问上游模型 | 保存在服务的受保护配置中(模式 0600);面板不回显 | + +**配置的归属。** 多个客户端(Desktop、CLI、DSH 插件)共用同一份服务。谁可以修改服务的配置、修改如何合并、环境文件身份绑定失效后如何重新登记,由一份独立的配置管理设计规定。本提案不定义这些规则;第 6 项交付(面板的模型配置页)在该设计确定后与它对齐。本机已有服务(例如由 Desktop 注册)时,插件复用它,不重新注册,也不改变它绑定的运行环境。 + +**Core 不可达时的行为。** 受管服务以 `PC_GATEWAY_DEGRADE=passthrough` 运行:gateway 在运行但无法使用 Core 时,请求照常转发,不召回、不采集。这只在用户能看到“记忆未生效”时才可接受,因此插件必须显示它:这条路由上每一步开始前插件都要向 Core 解析 Scope,解析失败时该请求按第 4 节标记为不参与记忆,面板和状态栏同时显示“本回合未使用记忆”及原因。服务整体停止时请求无法转发,插件的错误提示给出两个恢复方式:启动服务,或切换到直连模型。 + +**面板的写操作。** 面板从只读的状态展示扩展为会下载并解包运行环境、注册登录时启动的服务、保存上游密钥、修改 DSH 的模型配置。这些操作沿用状态接口现有的本机所有者判定,不放宽: + +| 宿主 | 面板的写操作 | +| --- | --- | +| DSH Desktop | 开放,每项操作前逐项确认 | +| `dsh web`,已配置 `ownerLocal` | 同 Desktop | +| `dsh web`,未确认本机所有者 | 不开放;面板显示当前状态和等价的终端命令 | + +## 7. 供应商、模型与保护模式 + +- 一个 gateway 实例为每种协议配置一个上游地址(Chat Completions 一个,Responses 一个),或者配置一个订阅通道;订阅通道一次只能选一种,并取代上游的 API 密钥。首期面板只配置一个使用 API 密钥的供应商,并提供 DeepSeek、OpenAI 和自定义三种预设;预设决定协议和地址,用户不需要自己判断使用 Responses 还是 Chat Completions。保存前的 **测试连接** 用所填的模型发送一次最小请求。 +- 路由优先使用 Responses 协议。DeepSeek 的 API 提供与 Responses 兼容的无状态接口(`base_url` 为 `https://api.deepseek.com`,支持流式输出和函数工具,不支持 `store` 与 `previous_response_id`),因此 DSH 的默认模型也走 Responses 路由;只提供 Chat Completions 的供应商使用 Chat 路由。 +- 同一供应商下可以登记多个模型。Core 保护模式为 `disabled` 时,gateway 按请求中的模型名转发,不限于某一个模型。 +- 本提案面向个人用户,只在 Core 默认的 `disabled` 保护模式下设计和验收。`enforcing` 模式要求为每个目标声明模型、窗口和缓存策略,属于团队部署的范畴,面板不生成这类配置,也不在该模式下提供这条路由。 +- 同时使用多个供应商需要多个 gateway 实例或 gateway 自身的改动,不在本提案范围内。 +- 记忆抽取使用的模型在模型页中配置,默认值为“自动”。自动模式下,抽取沿用该回合保留的请求前缀,因此使用该回合对话所用的模型并复用它的前缀缓存;前缀不可用或不比普通抽取便宜时,回落到 Core 的生成模型,默认取模型页中的第一个模型。用户显式指定抽取模型时使用所指定的模型。抽取的费用计入用户的上游账户,模型页说明这一点。 + +## 8. 与现有路径的关系 + +- `powercontext setup dsh` 的源码安装路径保留,面向开发和调试。 +- 用户自行部署 gateway 的方式保留;插件检测到路由已由用户声明时不覆盖。 +- 默认注册的 MCP 工具集改为 `recommended` 档([#2040](https://github.com/oceanbase/powercontext/issues/2040)),与本提案相互独立,可单独交付。 + +## 9. 实施与验收 + +建议按以下顺序交付,每一项独立可用: + +1. 完成 `powercontext-dsh` 的 npm 首次发布;文档在发布前以源码安装为推荐路径。 +2. 插件登记模型路由并按会话切换,提供移除路由的命令(依赖用户自行运行的 gateway)。 +3. 插件发送 Scope 请求头,与第 2 项在同一个改动中交付,使 gateway 路由从一开始就不使用 gateway 实例的默认 Scope。 +4. gateway 的 npm 平台包;`powercontext service` 以组合部署覆盖 gateway,并分配 gateway 访问密钥。 +5. Core 运行环境归档的发布,以及插件的引导安装。前置条件:归档去重,以及中国大陆可达的下载镜像。 +6. 面板的模型配置页,与独立的配置管理设计对齐。 +7. gateway 在 `disabled` 保护模式下向客户端实时转发流式输出。Core 在 `before_send` 的准许结果里按请求许可提前交付;gateway 在转发的同时缓冲回复,放行记录与采集在流结束后完成。客户端在回复结束之前断开时调用取消;回复已经完整之后断开不取消收尾工作,它们在限定时间内完成,期间保持并发许可。先交付 Chat Completions 路由,再交付 Responses 路由。在它交付之前,文档不把这条路由作为默认推荐。 + +验收需要在真实 DSH 宿主上执行,并区分实际执行与夹具: + +- 选中 gateway 路由的新会话中,助手自己做出的决定能在另一个新会话中被召回,且召回来自 gateway 注入而非模型猜测。 +- 同一安装下,选中其他路由的会话保持现有行为,gateway 不收到请求。 +- 重复启动 DSH、同时运行多个 DSH 进程,服务的主进程不变。 +- 用户手动停止服务后,启动 DSH 不会恢复服务。 +- 执行移除命令后,用户的 profile 中没有残留的路由和凭据。 +- Scope 解析失败或 Core 不可达时,gateway 路由上的对话继续,gateway 的默认 Scope 中没有新增 Source,面板显示本回合未使用记忆。 +- 第 7 项交付后,gateway 路由上的回复逐步显示,首个输出的延迟与直连同一模型时相当。 +- 以 DeepSeek 官方端点为上游,经 Responses 路由完成带工具调用的多步回合,回合被采集并能在新会话中召回。 + +## 10. 已有的验证证据 + +以下结果来自一台 Linux x86_64 主机上的实际执行:DSH `0.2.2-rc.5`(headless 与 web profile)、PowerContext master `9e40f8996`、模型 `gpt-4.1-mini`(api.openai.com)。 + +| 验证项 | 结果 | +| --- | --- | +| 插件路径下新会话召回助手的决定 | 未召回;Source 只有用户提示词 | +| 经 gateway 的新会话召回助手的决定 | 召回正确;Source 为完整回合 | +| 首个请求输入 token | 无插件 4,389;插件路径 34,432;gateway 路由 6,000–7,400 | +| 通过设置服务登记路由 | 写入 profile 层,并与已有 provider 合并 | +| 同一安装下按所选模型切换 | gateway 路由上插件不发召回与采集请求;其他路由上保持现有行为 | +| 运行环境归档在 Linux 上的生产与冒烟 | 通过;Core 可在无系统 Python 的环境中启动 | +| `powercontext service` 在 Linux 上的注册 | 重复 `start`/`install` 主进程不变;崩溃后自动恢复;手动停止被保留 | +| 一个 gateway 实例转发多个模型(保护模式 `disabled`) | `gpt-4.1-mini` 与 `gpt-4o-mini` 均转发成功 | +| gateway 路由上插件发送 Scope 请求头 | Source 落在插件解析出的 Scope,gateway 的默认 Scope 中没有新增 | +| gateway 路由上 Scope 解析失败 | 回合正常完成;gateway 把请求记录为 internal;任何 Scope 中都没有新增 Source | +| 经 gateway 的流式请求首个增量到达的时间(保护模式 `disabled`) | Chat Completions 与 Responses 路由各两次,首个增量都在整轮结束时到达(整轮 11–17 秒) | +| Chat Completions 路由实时转发([#3035](https://github.com/oceanbase/powercontext/pull/3035) 的构建,保护模式 `disabled`) | 首个正文字节在 0.41–1.62 秒到达(整轮 9.4–11.5 秒);读到结束和在 `[DONE]` 处断开各 3 次,全部完成采集;回复中途断开 2 次,调用取消,没有新增 Source;`shadow` 模式 2 次,仍在整轮结束时一次到达 | + +未验证:Windows(含 WSL2)与 macOS;DSH Desktop;会话中途切换模型;真实的 npm 可选依赖安装;服务定义覆盖 gateway;DeepSeek 的模型经 gateway(其 Responses API 为无状态接口,支持流式和函数工具);OpenAI 以外的供应商;同一宿主中其他插件(例如 dsh-pet)经 gateway 路由发起的模型请求。 + +# Drawbacks + +- **需要上游模型的 API 密钥。** DSH 默认使用 DeepSeek 账号登录的官方模型。账号登录与 API 密钥使用同一个推理服务,区别只在凭据:前者发送登录 token,后者发送密钥。gateway 不读取 DSH 的登录 token,因此这类用户需要先在 DeepSeek 平台创建 API 密钥并填入面板。 +- **gateway 路由上没有实时的流式输出。** gateway 在释放检查通过后才向客户端返回内容:它先完整接收上游的回复,再一次性重放为流式事件。用户在整轮回复生成完之前看不到任何输出,对推理模型和较长的代码回复尤其明显,回复超过响应缓冲上限(1 MiB)时请求失败。这在 Chat Completions 和 Responses 两条路由上都是如此。`disabled` 保护模式下,Core 的释放检查不检查回复内容,这段等待没有对应的保护作用;第 7 项交付用于消除它。 +- **服务停止时 gateway 路由不可用。** 插件路径在 Core 不可达时只是失去记忆;gateway 路由的模型请求本身要经过本机服务,服务停止后必须启动服务或切换模型。 +- **选中 gateway 路由时模型不能主动调用记忆工具。** 模型发起的搜索、显式记忆、交接和候选审查在该路由上不可用,用户通过 `/pc` 命令完成这些操作。 +- **Windows 上需要 WSL2。** gateway 依赖 Unix socket 和 Unix 信号,没有原生 Windows 可执行文件;按现有文档,gateway 和 Core 需要一起运行在 WSL2 中。首期不在 Windows 上自动安装和启动服务,文档提供 WSL2 中的手动步骤。 +- **Skill 与卸载的清理不完整。** Skill 在 gateway 路由上仍然可见;只通过 DSH 界面卸载插件会留下模型路由。两者都受限于 DSH 目前提供的接口。 +- **`dsh web` 上需要额外一步。** 未确认本机所有者时,面板不执行写操作,用户需要在终端中完成准备和配置。 +- **首次准备需要下载较大的运行环境。** +- **常驻服务。** 插件会在用户机器上注册一个登录时启动的用户服务,需要在面板中明确说明并取得确认。 + +# Rationale and alternatives + +**为什么让模型请求经过 gateway,而不是在插件里补采集助手回复。** DSH 提供 `agent/assistant-stream` 事件,插件可以在本进程内读到助手输出。但这会在每个宿主的 hook 插件里各实现一遍回合采集,并且用不上 gateway 会话前缀上的抽取成本优化。该方案保留为账号登录用户的后备,见未来可能性。 + +**为什么用模型路由而不是全局开关。** 路由让“是否使用 gateway”成为用户在模型选择器里的一次选择,可逐会话切换,也不要求重启 DSH。 + +**为什么复用 `llm-pi-ai` 而不是自己注册适配器。** DSH 允许插件通过 `ctx.llm.registerAdapter` 注册适配器,但那要求插件自己实现协议转换、流式分片和回放状态。`llm-pi-ai` 已经支持手工声明的 OpenAI 兼容网关。 + +**为什么统一由 `powercontext service` 管理,而不是由插件拉起子进程。** 插件拉起的子进程在多个 DSH 进程并存时没有明确的拥有者;Core 随 DSH 退出会让后台抽取没有机会完成;其他 Agent 的集成在 DSH 关闭时会失去 Core。常驻服务加“只确保运行”避免了这三个问题,并复用已有的崩溃恢复和显式停止语义。 + +**为什么不使用独立的 Ops 工具包。** Ops 的价值在于 Server 解释器损坏时仍能修复,代价是再分发一份约 205 MB 的运行环境。本提案中插件承担下载、校验和解包,其余生命周期交给运行环境内的 `powercontext service`。 + +# Prior art + +- Codex 与 OpenCode 的 gateway 接入:通过宿主配置把模型端点指向 gateway,gateway 路径不安装插件。 +- Desktop 的受管本地 Server:固定资源、摘要校验和用户服务注册。 +- DSH 自身的原生模块以 `node-addon-system--` 形式按平台分发。 + +# Unresolved questions + +- 会话中途切换模型后的第一步是否需要额外处理工具可见性,以及历史中带插件快照的会话切换到 gateway 路由时的提示方式。 +- 是否为 DeepSeek 账号登录单独提供支持(gateway 增加 Messages 入口或专用通道),使这类用户不必创建 API 密钥。这项工作独立于本提案。 +- Windows 上由插件在 WSL2 中自动安装和启动服务的做法,以及 WSL2 与 Windows 上的 DSH 之间的回环访问是否可靠。 +- Responses 路由的实时转发:gateway 目前对上游发送非流式请求。要实时转发,就要在渲染上游请求时(即 `before_send` 之前)决定请求流式,而许可在 `before_send` 的结果里才给出;gateway 在检查之前如何得知可以提前交付(例如 Core 能力发现中的一项提示)尚未确定。 +- DSH 是否提供在会话内显示“本回合未使用记忆”的接口;没有时提示只出现在面板和状态栏。 +- 是否需要同时配置多个供应商,以及由多个 gateway 实例还是由 gateway 的路由能力来支持。 +- 运行环境归档去重后的体积,以及下载镜像由谁提供和维护。 +- 小模型作为 Core 生成模型时的抽取质量:验证中同一回合陈述的三条事实只有两条被抽取为记忆。 + +# Future possibilities + +- 为账号登录用户提供不经过 gateway 的完整回合采集:插件通过 `agent/assistant-stream` 读取助手输出并采集。 +- 在 gateway 路由上恢复一小组只读工具(例如搜索记忆),与 gateway 使用同一个 Scope。 +- 把同样的“模型路由 + 统一服务”模式推广到其他支持插件登记模型提供方的宿主。