@yuriai/web 0.2.6 → 0.2.8-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/README.md +7 -0
  2. package/dist/assets/{AssistantView-Ct7V8lpF.js → AssistantView-Df-G2DX8.js} +3 -3
  3. package/dist/assets/{DiagramView-Ch2D2gUQ.js → DiagramView-BoVqJRQb.js} +1 -1
  4. package/dist/assets/{GuiPluginSurface-sdtt33qQ.js → GuiPluginSurface-DxLoeTDb.js} +1 -1
  5. package/dist/assets/MarkdownRenderer-DYi_qpYy.js +2 -0
  6. package/dist/assets/{MarkdownRendererImpl-CtfnAdo-.js → MarkdownRendererImpl-CMxRMFqp.js} +1 -1
  7. package/dist/assets/{MarkstreamRendererImpl-CWtvbZSX.js → MarkstreamRendererImpl-DQ4urBZW.js} +1 -1
  8. package/dist/assets/{MobileDetailNavigation-CUx-H4fe.js → MobileDetailNavigation-C4fJlKdu.js} +1 -1
  9. package/dist/assets/{MobileShareBridge-DUwPWQNz.js → MobileShareBridge-DLC-TdRy.js} +1 -1
  10. package/dist/assets/{MobileTabPageHeader-BtLdnDa3.js → MobileTabPageHeader-CMbbtHh7.js} +1 -1
  11. package/dist/assets/{MultiRunWindow-Gs-5PM3Z.js → MultiRunWindow-Cwp8-_Wh.js} +1 -1
  12. package/dist/assets/{SettingsView-BJy3bKbz.js → SettingsView-CYDk0wpN.js} +1 -1
  13. package/dist/assets/{SettingsWindow-w3K1Dbo1.js → SettingsWindow-Vuzphf9b.js} +1 -1
  14. package/dist/assets/{TerminalView-DnmN2KHj.js → TerminalView-Dhalx6_Z.js} +1 -1
  15. package/dist/assets/{ToolOutputDialog-Ctr_rqwb.js → ToolOutputDialog-DBxSxWNh.js} +1 -1
  16. package/dist/assets/{appThemeRegistry-3MgV1x6f.js → appThemeRegistry-JEhMXHGF.js} +1 -1
  17. package/dist/assets/{capgoAdapter-CpthNQYY.js → capgoAdapter-BgD0wyhC.js} +1 -1
  18. package/dist/assets/{fileMentionTouchSelection-1havvnZN.js → fileMentionTouchSelection-BQdfRBkk.js} +1 -1
  19. package/dist/assets/{gitApi-Bw4AQDuA.js → gitApi-DhDVqj9b.js} +1 -1
  20. package/dist/assets/{insertionBoundaries-_8AmjaTs.js → insertionBoundaries-BMe1F3uB.js} +1 -1
  21. package/dist/assets/{main-C09TKUe7.js → main-CfOYzhn0.js} +4 -4
  22. package/dist/assets/{main-C5PgzX4P.js → main-DXlMu2LF.js} +2 -2
  23. package/dist/assets/{mcpQueries-B-cYFZaO.js → mcpQueries-vxcQAIGB.js} +1 -1
  24. package/dist/assets/{miniChat-C_az-fSk.js → miniChat-D48iGEEW.js} +2 -2
  25. package/dist/assets/{mobile-CnXTR6QC.js → mobile-Cy5qSiDY.js} +2 -2
  26. package/dist/assets/{mobileBackNavigation-DSDlOhh6.js → mobileBackNavigation-QtBK9OSb.js} +1 -1
  27. package/dist/assets/{multirun-DgLVkagL.js → multirun-BR9a6EBo.js} +1 -1
  28. package/dist/assets/{native-config-w5ntuSb5.js → native-config-CHySeHif.js} +1 -1
  29. package/dist/assets/{projectMeta-OMvGIxob.js → projectMeta-Bn2vBNDe.js} +1 -1
  30. package/dist/assets/{readiness-6XMJtgLY.js → readiness-BAssoNVf.js} +1 -1
  31. package/dist/assets/{renderElectronMiniChatApp-rvO-rcnc.js → renderElectronMiniChatApp-B9Kci_8q.js} +2 -2
  32. package/dist/assets/{renderMobileApp-DheZQr5N.js → renderMobileApp-CBivL8Ps.js} +4 -4
  33. package/dist/assets/{runtimeConfig-cAbdKbv6.js → runtimeConfig-COdEwlK-.js} +1 -1
  34. package/dist/assets/{runtimeEndpointReset-CPsJLvK2.js → runtimeEndpointReset-D3x76Qpf.js} +3 -3
  35. package/dist/assets/{sessionChangeSummary-BEB6JZpN.js → sessionChangeSummary-Dtgq0CWg.js} +1 -1
  36. package/dist/assets/{sessionLookup-BI5wTzCJ.js → sessionLookup--Wlr3e8X.js} +1 -1
  37. package/dist/assets/{speechSettingsQueries-DZ9a20QR.js → speechSettingsQueries-D3fc_4Iy.js} +1 -1
  38. package/dist/assets/types-D8_26ZKT.js +2 -0
  39. package/dist/assets/{useAppFontEffects-CKCYZ3VJ.js → useAppFontEffects-DLT9jVl1.js} +6 -6
  40. package/dist/assets/{useDesktopWindowControlsLayout-CloJ_VZ3.js → useDesktopWindowControlsLayout-BNAZWH9L.js} +1 -1
  41. package/dist/assets/{useEffectiveDirectory-CuajC8G4.js → useEffectiveDirectory-D2_-KCB7.js} +1 -1
  42. package/dist/assets/{useMobileNavigationStore-07uYrwvK.js → useMobileNavigationStore-DJwk64Po.js} +3 -3
  43. package/dist/assets/{useWorkerHighlightedLines-2Vw7Hzud.js → useWorkerHighlightedLines-k48txabk.js} +1 -1
  44. package/dist/index.html +5 -5
  45. package/dist/mini-chat.html +5 -5
  46. package/dist/mobile.html +5 -5
  47. package/package.json +9 -4
  48. package/server/index.js +3 -3
  49. package/server/lib/assistants/DOCUMENTATION.md +12 -1
  50. package/server/lib/assistants/contact-tools.js +2 -2
  51. package/server/lib/assistants/harness.js +21 -17
  52. package/server/lib/assistants/harness.test.js +19 -23
  53. package/server/lib/assistants/memory.http.test.js +2 -2
  54. package/server/lib/assistants/routes.js +1 -3
  55. package/server/lib/llm/DOCUMENTATION.md +116 -161
  56. package/server/lib/llm/bridge-client.js +60 -0
  57. package/server/lib/llm/bridge-client.test.js +35 -0
  58. package/server/lib/llm/bridge-contract.js +24 -0
  59. package/server/lib/llm/bridge-plugin.bundle.js +1136 -0
  60. package/server/lib/llm/bridge-plugin.js +127 -0
  61. package/server/lib/llm/bridge-plugin.test.js +164 -0
  62. package/server/lib/llm/completions.js +42 -18
  63. package/server/lib/llm/completions.test.js +8 -4
  64. package/server/lib/llm/generate.js +43 -728
  65. package/server/lib/llm/generate.test.js +62 -1063
  66. package/server/lib/llm/routes.js +0 -2
  67. package/server/lib/package-manager.js +1 -1
  68. package/server/lib/small-model/DOCUMENTATION.md +11 -17
  69. package/server/lib/small-model/index.js +5 -4
  70. package/dist/assets/MarkdownRenderer-C1giOHSB.js +0 -2
  71. package/dist/assets/types-BihDq7ZW.js +0 -2
  72. package/server/lib/llm/temp-directory.js +0 -58
  73. package/server/lib/llm/temp-directory.test.js +0 -57
@@ -1,161 +1,116 @@
1
- # Yuri LLM Gateway
2
-
3
- OpenAI-shaped Chat Completions for the in-app Assistant contact. This is the
4
- only model path the Assistant harness (`pi-agent-core` `streamFn`) talks to.
5
-
6
- ## Contract
7
-
8
- - `GET /api/yuri/llm/models` — available `{providerID, modelID}` catalog
9
- from official `@opencode/client` `provider.list` + `model.list`
10
- (`{ location, data }`). `ModelInfo.id` is the external modelID used in
11
- generate/session refs (`ModelInfo.modelID` is a separate internal field).
12
- Vision prefers `capabilities.input.image === true` (SDK object shape), then
13
- v2 list `capabilities.input` containing `"image"`, then legacy
14
- modalities/input/attachment. Catalog loads use a bounded AbortSignal
15
- (default 8s) so a pending provider call cannot stall a contact lane.
16
- - `POST /api/yuri/llm/chat/completions` — `{ model, messages, stream?,
17
- providerID?, modelID?, variant? }` → OpenAI `chat.completion` JSON.
18
- This **public** gateway is **non-streaming**. `stream: true` is rejected with
19
- `validation_error` (HTTP 400). Do not emit fake SSE after the fact on this
20
- route. The contact UI may stream later via a **server-internal** path only.
21
-
22
- `model` may be `providerID/modelID` or a bare `modelID` paired with `providerID`.
23
-
24
- `variant` is an optional OpenCode model variant string. Null, omitted, or empty
25
- values preserve the provider default. Other types fail validation before a model
26
- call. A selected variant is forwarded unchanged on the throwaway session's
27
- `model` ref (both paths). OpenCode owns provider-specific variant
28
- semantics. Variant selection changes inference configuration only; completion
29
- output continues to include text parts and keeps reasoning parts private.
30
-
31
- ## Internals (official `@opencode/client`)
32
-
33
- Credentials stay in OpenCode. This module does not read `auth.json`, does not
34
- call Anthropic/OpenAI/plugin SDKs, and does not use the `openai` npm package.
35
-
36
- ### Text path (no vision image bytes)
37
-
38
- `session.generate` inside a throwaway session owned by the hidden deny-all
39
- `yuri-text` agent (same temp root, same deny-all verification and
40
- `session.create` / `session.remove` lifecycle as the attachment path below):
41
-
42
- 1. Ensure `.opencode/agent/yuri-text.md` and verify its last catch-all
43
- permission rule is deny-all, with only deny rules after it, before creating the session.
44
- 2. `session.create({ location, agent: 'yuri-text', model, metadata })`.
45
- 3. `session.generate({ sessionID, prompt })` → `{ text }`. One model call with
46
- the session's provider context, no tool loop, no messages written. System
47
- text is prepended into `prompt`.
48
- 4. Always `session.remove` in `finally` (interrupt first on abort/timeout).
49
-
50
- By default, empty text responses are retried at most twice, after 500ms and
51
- 1000ms, in the same throwaway session with the same prompt. All attempts and
52
- backoff share the original generation deadline (90s by default).
53
-
54
- Internal callers may pass `timeoutMs: 0` to disable that deadline and
55
- `retryEmptyTextUntilCancelled: true` to recover empty text without an attempt
56
- limit. Backoff doubles from 500ms to a maximum of 30s. Contact turns use both
57
- options, including tool follow-ups and terminal-operation summaries. Cancellation
58
- interrupts requests and backoff immediately, then interrupts/removes the
59
- throwaway session. Thrown upstream errors still surface; authentication/config
60
- errors are not silently retried. This does not remove provider-owned deadlines.
61
- Application tools execute outside this text-only call and are not replayed.
62
-
63
- Contacts also pass `textEnvelope: true`: the text-only prompt requests a JSON
64
- object with one `text` string containing the full application response. Decode
65
- that exact envelope (optionally in a `json` fence) before contact protocol
66
- validation. Preserve unwrapped responses for the existing contact parser, since
67
- live models sometimes ignore the transport envelope. Do not unwrap arbitrary
68
- objects or recursively parse tool arguments. This avoids depending solely on
69
- bare tool-like text surviving a text-only generation endpoint; real DeepSeek
70
- multi-tool runs previously returned empty text or only progress at this boundary.
71
- The attachment path retains its existing streaming protocol.
72
-
73
- These three options are in-process arguments, never read from HTTP request
74
- bodies. Other utility/public gateway calls retain their bounded defaults.
75
-
76
- `generate.text` (sessionless) is not used: session-scoped providers such as
77
- OpenCode Go reject calls without a session (`x-opencode-session`).
78
- `onTextDelta` is skipped honestly (no fake typewriter).
79
-
80
- OpenCode loads a fresh location's config lazily, so `agent.get` can report the
81
- agent as missing on the first read. Deny-all verification polls within a
82
- bounded window (5s) before failing.
83
-
84
- ### Attachment / vision path
85
-
86
- When the catalog marks the model vision-capable and the request includes image
87
- data-URL file parts:
88
-
89
- 1. Ensure the throwaway temp directory agent markdown (`yuri-llm`,
90
- permissions array ending in `action:* resource:* effect:deny`).
91
- 2. `agent.get({ agentID: 'yuri-llm', location })` and require the **last catch-all**
92
- permissions rule to be deny-all, with only deny rules after it. This accepts
93
- OpenCode's appended `browser` deny while rejecting trailing allow/ask rules.
94
- Failure → `llm_attachment_generation_unavailable`
95
- **before** any `session.prompt`. Title is never treated as permission isolation.
96
- 3. `session.create({ location, agent, model: { id, providerID, variant? },
97
- metadata: { yuri: { llm: { purpose: 'chat-completions' } } } })`
98
- (bare session id). Host `persistSessionMetadata` then commits the same
99
- isolation patch; `onSystemSessionPersisted` drops the throwaway session
100
- from the sidebar index. Create-time `metadata` is a hint only.
101
- 4. Optional `session.instructions.entry.put` for the system prompt.
102
- 5. Subscribe for `session.text.delta` (`data.sessionID` / `assistantMessageID` /
103
- `ordinal` / `delta`) before prompt; unsubscribe in `finally`.
104
- 6. `session.prompt({ sessionID, text, files: [{ uri, name? }], delivery })`.
105
- 7. `session.wait` then `message.list({ order: 'desc' })`; verify newest assistant
106
- `finish` / `error`; extract text content.
107
- 8. On timeout/abort: `session.interrupt({ continue: false })` and always
108
- `session.remove` in `finally`.
109
-
110
- Non-vision models keep the `[image: …]` description strategy and never forward
111
- image bytes. Data URLs are strictly validated; a single attachment is capped at
112
- 20 MiB (plus existing inline text-file limits). An external/temp workspace that
113
- OpenCode cannot see fails explicitly (`llm_attachment_generation_unavailable`).
114
-
115
- `ensureLlmTempDirectory` keeps one process-wide throwaway root (`yuri-llm-…`).
116
- Concurrent first callers share a single `mkdtemp` inflight; every caller still
117
- writes/refreshes `.opencode/agent/<name>.md` after settle so a mid-flight ensure
118
- cannot skip a newer agent body. Concurrent warm refreshes serialize through one
119
- write chain (last committed body wins; no interleaved partial writes).
120
- `stopLlmTempDirectory` clears the singleton for shutdown/tests.
121
-
122
- There is no `/generate` probe, no `tool.ids` deny map, and no `promptAsync` path.
123
- There is no `config.providers` catalog path.
124
-
125
- ### Internal token callback (in-process only)
126
-
127
- `generateOpenCodeText` / `createChatCompletion` accept optional
128
- `onTextDelta(text: string)` and `globalEventHub`.
129
-
130
- - **Attachment session path:** real `session.text.delta` tokens for this session.
131
- - **`session.generate` text path:** cannot emit live deltas — skip `onTextDelta`.
132
- - **HTTP `POST .../chat/completions`:** does not pass these options and still
133
- rejects `stream: true`.
134
-
135
- No connected provider → `no_provider` (HTTP 400). Upstream failure is never
136
- an empty success. The 502 body includes `{ error, message }`.
137
-
138
- ## Ownership
139
-
140
- The hidden generator agent explicitly describes application-owned tool calls:
141
- it emits `yuri-tool` JSON for the contact harness to execute and receives
142
- the results on subsequent requests. Native OpenCode tool permissions remain
143
- denied. Capability statements and execution errors must follow the supplied
144
- application tool catalog and actual results.
145
-
146
- The Assistant contact harness owns system prompt, Yuri transcript,
147
- bubble splitting, and Yuri API tools (`assign_session`). Those tools
148
- deliver through contact **cards**, not this completions payload. The gateway
149
- stays a text generator: OpenCode coding tools stay denied on both paths via the
150
- verified agent permissions ruleset.
151
-
152
- The gateway is also the single OpenCode-backed path for server utility calls
153
- (`small-model`: commit messages, session titles, goal audits, scheduled-task
154
- distill, TTS summaries, PR descriptions). No server module calls provider APIs
155
- directly with OpenCode credentials.
156
-
157
- Internal completion/generate calls accept an optional AbortSignal from the
158
- contact continuation. It is combined with the generator deadline and passed to
159
- upstream requests. Cancellation still runs throwaway-session cleanup
160
- (`interrupt` + `remove`); it must not turn a late model response into a new
161
- tool operation.
1
+ # Yuri clean LLM gateway
2
+
3
+ The Assistant and small-model utilities share `createChatCompletion`. The Host
4
+ always connects to the selected **external OpenCode service**. It does not embed,
5
+ start or replace OpenCode to provide inference.
6
+
7
+ ## Ownership and security
8
+
9
+ - `bridge-client.js` writes one self-contained built-in plugin to a Yuri-owned
10
+ temporary `.opencode/plugins` directory. Official client `plugin.list` activates
11
+ that isolated Location. No global/project OpenCode configuration is changed.
12
+ - Calls use the official authenticated `client.rpc(LlmBridge)` transport, scoped
13
+ to that directory. `ready` verifies protocol version 2 and runtime service availability; only RPC-not-ready
14
+ startup errors receive a bounded readiness retry. Generation is not rerun for
15
+ arbitrary transport errors, because a response may already have been produced.
16
+ - `bridge-plugin.js` runs **inside external OpenCode**. It reads that Location's
17
+ live `ModelResolver.Service` through the RPC caller's Location context. The
18
+ external service itself resolves the selected model/variant, active integration
19
+ credential, OAuth refresh, native/legacy driver and provider overlays. The bridge
20
+ uses `@opencode/ai` single-call inference, never a parallel model resolver. Credentials never cross
21
+ the inference RPC or enter the Host's inference result. Upstream exceptions are
22
+ replaced with a fixed error and allowlisted failure category/HTTP status rather
23
+ than leaking private content, response bodies, headers or credentials. Defects
24
+ are sanitized too; cancellation remains interruption, not a provider failure.
25
+ - Only caller-provided system/messages/tools enter inference. Session history,
26
+ agents, AGENTS.md, Skills, context/generate hooks, native tools and the OpenCode
27
+ Agent loop are not used. Model body overrides cannot replace context or tools.
28
+ - The existing `model.request`, `http.request` and `http.response` transport hooks
29
+ DO run against the same live host registry. They retain provider endpoint,
30
+ authorization, request/response and legacy AI SDK adaptation without invoking
31
+ context-building hooks. A stable synthetic `ses_` identity supplies cache/header
32
+ affinity; it is never persisted. Provider hooks may attempt a Session lookup,
33
+ receive not-found and use their existing fallback. The Host never creates,
34
+ prompts, generates through, or writes a Session for inference.
35
+ - The plugin only returns proposed native calls. Yuri's existing pi Agent owns
36
+ execution, permissions, sequencing, terminal-operation gates and cancellation.
37
+ A completed side effect is never replayed by inference recovery.
38
+
39
+ ## Supported providers and deployment
40
+
41
+ There is no Yuri provider/package allowlist or blanket OAuth rejection. All
42
+ configured text-inference providers use the external service's own resolver and
43
+ driver loading, including OpenAI/Codex OAuth, OpenCode, API-key, ambient-credential
44
+ and legacy AI SDK providers. Whether a particular model accepts text/tools/media
45
+ and the active account has access remains the upstream provider's contract.
46
+ This is a generic integration path, not evidence that every vendor/account has
47
+ been tested. There is **no fallback** to session.generate or a second model.
48
+
49
+ The bridge currently relies on internal `ModelResolver` and `PluginHooks` service
50
+ contracts, keyed by the compatible OpenCode runtime. Missing services fail
51
+ explicitly; protocol version alone does not prove compatibility with every future
52
+ OpenCode release. The bundled SDK normalizes the host model's wrapper while
53
+ retaining the host-owned route implementation; calls back into the host route
54
+ restore its original model wrapper so nominal SDK schemas remain valid. It never instantiates an embedded
55
+ OpenCode host. Requests use stateless HTTP, including for providers whose durable
56
+ Agent sessions prefer WebSocket; providers requiring WebSocket-only operation are
57
+ not established by this path.
58
+
59
+ The external service must be able to read the isolated plugin directory. A
60
+ service on another machine/container without a shared filesystem cannot load a
61
+ Host-local plugin and fails visibly; it needs a separately provisioned bridge
62
+ on its own filesystem. No automatic remote installation or global config edit
63
+ is performed. Web, Electron, hosted mobile and Capacitor clients use the Host's
64
+ same bridge. VS Code's extension-owned runtime does not gain a local Host.
65
+
66
+ `build:llm-bridge` generates `bridge-plugin.bundle.js` with the inference SDK and service tags, not a driver registry.
67
+ It is ignored in source control and included in the web package's `server`
68
+ files. Web build, prepack and development API scripts build it. At runtime the
69
+ Host copies its bytes into the temporary directory, including when the Host
70
+ source lives in Electron ASAR; external OpenCode needs no access to ASAR or
71
+ Yuri's node_modules. The bundle has no machine-specific source directory.
72
+ Shutdown removes the owned directory; a failed staging operation cleans up.
73
+
74
+ ## Completion contract
75
+
76
+ - `GET /api/yuri/llm/models` uses official provider.list/model.list.
77
+ - `POST /api/yuri/llm/chat/completions` remains non-streaming. `stream:true` is
78
+ rejected. No fake token callbacks or post-hoc SSE are emitted.
79
+ - Messages retain structured roles: system/user/assistant/tool. Native tool
80
+ calls carry their original IDs and JSON arguments; tool results retain name
81
+ and tool_call_id. The Assistant supplies its actual tool JSON schemas.
82
+ - Results retain text, tool calls, finish reason, input/output/cache-read usage.
83
+ Empty text with valid tool calls is success. Truly empty results are errors;
84
+ content-filter/error/length endings cannot masquerade as complete output.
85
+ - Each contact retains a stable assistant-scoped cache key, including tool
86
+ follow-ups and result summaries. Cache affinity is not session history. It
87
+ never appends other conversations. No inference response cache is stored.
88
+ - Text files stay bounded inline descriptions. Supported vision inputs become
89
+ native image parts. Data URLs are validated and capped at 20 MiB per file;
90
+ unsupported image models receive descriptions, never image bytes.
91
+
92
+ There is no default inference deadline. An explicit internal `timeoutMs` may
93
+ bound a caller; manual AbortSignal cancellation reaches RPC and the LLM effect.
94
+ Only truly empty responses receive backoff (500ms exponential, capped at 30s).
95
+ Utilities stop after two retries; contacts opt into recovery until cancellation.
96
+ Authentication/configuration/protocol/transport errors are not blindly retried.
97
+ Provider-owned deadlines and Host process loss remain possible failures.
98
+
99
+ ## Validation
100
+
101
+ Focused tests cover clean wire messages, native empty-text tool calls, usage,
102
+ model override rejection, host-resolved OAuth, transport-hook body replacement, sanitized RPC errors,
103
+ session-free routing, long-running cancellation and retained side effects.
104
+ `experiments/llm-provider-bridge/production-smoke.mjs` calls the real small-model
105
+ session-title path and the real pi Agent through the production gateway, external
106
+ OpenCode RPC and a selected real provider. OpenAI/Codex and DeepSeek both passed
107
+ 25 rounds with 24 native calls and eight verified file edits/readbacks. The script
108
+ forbids Host Session API access and compares global config hashes before/after.
109
+ `provider-smoke.mjs` checks one model per currently connected provider and reports
110
+ each failure separately; it is not an exhaustive all-model certification.
111
+ The current connected-provider run passed 16 of 18 selected models. OpenCode
112
+ returned quota HTTP 402 and direct DeepSeek returned authentication HTTP 401;
113
+ the official stateless generation API independently returned the same statuses.
114
+ Neither failure is hidden by an automatic account/model switch.
115
+ This does not establish real-device UI, OAuth expiry behavior against every vendor, remote provisioning, streaming,
116
+ packaged Electron startup or crash-recovery correctness.
@@ -0,0 +1,60 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import os from 'node:os';
4
+ import { randomUUID } from 'node:crypto';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { OpenCode } from '@opencode/client';
7
+ import { LlmBridge, LLM_BRIDGE_VERSION } from './bridge-contract.js';
8
+
9
+ let directoryPromise;
10
+ export async function ensureLlmBridgeDirectory() {
11
+ if (!directoryPromise) directoryPromise = (async () => {
12
+ const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'yuri-inference-'));
13
+ try {
14
+ const plugins = path.join(directory, '.opencode', 'plugins');
15
+ await fs.mkdir(plugins, { recursive: true });
16
+ const plugin = path.join(path.dirname(fileURLToPath(import.meta.url)), 'bridge-plugin.bundle.js');
17
+ await fs.copyFile(plugin, path.join(plugins, 'yuri-llm.js'));
18
+ return directory;
19
+ } catch (error) { await fs.rm(directory, { recursive: true, force: true }); throw error; }
20
+ })().catch(error => { directoryPromise = undefined; throw error; });
21
+ return directoryPromise;
22
+ }
23
+
24
+ export async function stopLlmBridgeDirectory() {
25
+ const pending = directoryPromise;
26
+ directoryPromise = undefined;
27
+ if (pending) await fs.rm(await pending, { recursive: true, force: true });
28
+ }
29
+
30
+ export async function generateCleanCompletion({ buildOpenCodeUrl, getOpenCodeAuthHeaders, clientFactory,
31
+ providerID, modelID, variant, messages, tools, cacheKey = randomUUID(), maxOutputTokens, signal,
32
+ ensureBridgeDirectory = ensureLlmBridgeDirectory,
33
+ }) {
34
+ signal?.throwIfAborted();
35
+ const client = clientFactory?.() ?? OpenCode.make({ baseUrl: buildOpenCodeUrl('/', '').replace(/\/$/, ''), headers: getOpenCodeAuthHeaders() });
36
+ const directory = await ensureBridgeDirectory();
37
+ const options = { location: { directory }, signal };
38
+ const rpc = client.rpc(LlmBridge);
39
+ // Loading this isolated Location activates only its own plugin file, without global config writes.
40
+ await client.plugin.list({ location: { directory } }, { signal });
41
+ let ready;
42
+ for (let attempt = 0; ; attempt++) {
43
+ try { ready = await rpc.ready({}, options); break; }
44
+ catch (error) {
45
+ if (signal?.aborted || error?.type !== 'rpc.unavailable' || attempt >= 20) throw error;
46
+ await new Promise((resolve, reject) => {
47
+ const abort = () => { clearTimeout(timer); signal.removeEventListener('abort', abort); reject(signal.reason); };
48
+ const timer = setTimeout(() => { signal?.removeEventListener('abort', abort); resolve(); }, 250);
49
+ signal?.addEventListener('abort', abort, { once: true });
50
+ if (signal?.aborted) abort();
51
+ });
52
+ }
53
+ }
54
+ if (ready.version !== LLM_BRIDGE_VERSION) throw new Error('Incompatible Yuri LLM bridge');
55
+ const result = await rpc.generate({ providerID, modelID, ...(variant ? { variant } : {}), messages,
56
+ ...(tools ? { tools } : {}), cacheKey, ...(maxOutputTokens ? { maxOutputTokens } : {}),
57
+ }, options);
58
+ signal?.throwIfAborted();
59
+ return { ...result, source: 'opencode.llm.bridge' };
60
+ }
@@ -0,0 +1,35 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { generateCleanCompletion } from './bridge-client.js';
3
+ import { LLM_BRIDGE_VERSION } from './bridge-contract.js';
4
+
5
+ describe('external OpenCode inference bridge', () => {
6
+ it('uses only isolated plugin discovery and authenticated RPC, never Session APIs or config writes', async () => {
7
+ const ready = vi.fn(async () => ({ version: LLM_BRIDGE_VERSION }));
8
+ const generate = vi.fn(async () => ({ text: 'summary', toolCalls: [], finishReason: 'stop' }));
9
+ const client = { plugin: { list: vi.fn(async () => []) }, rpc: vi.fn(() => ({ ready, generate })), session: new Proxy({}, { get() { throw new Error('Session API is forbidden'); } }) };
10
+ const signal = new AbortController().signal;
11
+ await expect(generateCleanCompletion({ providerID: 'p', modelID: 'm', messages: [{ role: 'user', content: 'summarize' }], cacheKey: 'conversation-a', clientFactory: () => client, ensureBridgeDirectory: async () => '/isolated/bridge', signal })).resolves.toMatchObject({ text: 'summary', source: 'opencode.llm.bridge' });
12
+ expect(generate).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ cacheKey: 'conversation-a' }), { location: { directory: '/isolated/bridge' }, signal });
13
+ expect(client.plugin.list).toHaveBeenCalledWith({ location: { directory: '/isolated/bridge' } }, { signal });
14
+ });
15
+
16
+ it('fails closed on incompatible bridge protocol', async () => {
17
+ const generate = vi.fn();
18
+ await expect(generateCleanCompletion({ clientFactory: () => ({ plugin: { list: async () => [] }, rpc: () => ({ ready: async () => ({ version: -1 }), generate }) }), ensureBridgeDirectory: async () => '/isolated' })).rejects.toThrow('Incompatible');
19
+ expect(generate).not.toHaveBeenCalled();
20
+ });
21
+
22
+ it('retries plugin readiness but never repeats an ambiguous generation failure', async () => {
23
+ vi.useFakeTimers();
24
+ try {
25
+ const ready = vi.fn().mockRejectedValueOnce({ type: 'rpc.unavailable' }).mockResolvedValue({ version: LLM_BRIDGE_VERSION });
26
+ const generate = vi.fn().mockRejectedValue(new Error('connection lost'));
27
+ const result = generateCleanCompletion({ clientFactory: () => ({ plugin: { list: async () => [] }, rpc: () => ({ ready, generate }) }), ensureBridgeDirectory: async () => '/isolated' }).catch(error => error);
28
+ await vi.advanceTimersByTimeAsync(250);
29
+ expect(await result).toMatchObject({ message: 'connection lost' });
30
+ expect(ready).toHaveBeenCalledTimes(2);
31
+ expect(generate).toHaveBeenCalledTimes(1);
32
+ expect(vi.getTimerCount()).toBe(0);
33
+ } finally { vi.useRealTimers(); }
34
+ });
35
+ });
@@ -0,0 +1,24 @@
1
+ import { Rpc } from '@opencode/schema/rpc';
2
+
3
+ const object = { type: 'object', additionalProperties: true };
4
+ export const LLM_BRIDGE_VERSION = 2;
5
+ export const LlmBridge = Rpc.define({
6
+ id: 'yuri.llm',
7
+ methods: {
8
+ ready: { input: { type: 'object', additionalProperties: false }, output: { type: 'object', properties: { version: { type: 'integer' } }, required: ['version'], additionalProperties: false } },
9
+ generate: {
10
+ input: {
11
+ type: 'object',
12
+ properties: {
13
+ providerID: { type: 'string', minLength: 1 }, modelID: { type: 'string', minLength: 1 }, variant: { type: 'string' },
14
+ messages: { type: 'array', minItems: 1, items: object }, tools: { type: 'array', items: object },
15
+ cacheKey: { type: 'string', minLength: 1, maxLength: 256 }, maxOutputTokens: { type: 'integer', minimum: 1 },
16
+ },
17
+ required: ['providerID', 'modelID', 'messages', 'cacheKey'], additionalProperties: false,
18
+ },
19
+ output: object,
20
+ errors: { unavailable: object, generation_failed: object },
21
+ },
22
+ },
23
+ events: {},
24
+ });