@yuriai/web 0.2.7 → 0.2.8-beta.2
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.
- package/README.md +7 -0
- package/dist/assets/{AssistantView-Be0OKo5o.js → AssistantView-BjpY2u0c.js} +3 -3
- package/dist/assets/{DiagramView-Rpb6oenk.js → DiagramView-7j0sZC_P.js} +1 -1
- package/dist/assets/{GuiPluginSurface-DTH9jNDf.js → GuiPluginSurface-D_TK-2vZ.js} +1 -1
- package/dist/assets/MarkdownRenderer-B8zgOCfr.js +2 -0
- package/dist/assets/{MarkdownRendererImpl-CZ9lU4VJ.js → MarkdownRendererImpl-Dh7XVDLd.js} +1 -1
- package/dist/assets/{MarkstreamRendererImpl-DastQdu7.js → MarkstreamRendererImpl-D-qtRrvg.js} +1 -1
- package/dist/assets/{MobileDetailNavigation-CittPDZ0.js → MobileDetailNavigation-LkTVYLZX.js} +1 -1
- package/dist/assets/{MobileShareBridge-BxsoZnUJ.js → MobileShareBridge-CsTyKMtN.js} +1 -1
- package/dist/assets/{MobileTabPageHeader-BEgQ3L4u.js → MobileTabPageHeader-xz9RMXg4.js} +1 -1
- package/dist/assets/{MultiRunWindow-1o528NZE.js → MultiRunWindow-hMpjV7so.js} +1 -1
- package/dist/assets/{SettingsView-BGEaQC4R.js → SettingsView-D8qpss-X.js} +1 -1
- package/dist/assets/{SettingsWindow-MuJFADe3.js → SettingsWindow-B1VuFkFq.js} +1 -1
- package/dist/assets/{TerminalView-CHDyibi_.js → TerminalView-CLcaJh2M.js} +1 -1
- package/dist/assets/{ToolOutputDialog-Bcry3eoE.js → ToolOutputDialog-BuZo3XvN.js} +1 -1
- package/dist/assets/{appThemeRegistry-CJoBy_eP.js → appThemeRegistry-B7sp1Hw8.js} +1 -1
- package/dist/assets/{capgoAdapter-_oWtDrgf.js → capgoAdapter-vxOdqloD.js} +1 -1
- package/dist/assets/{fileMentionTouchSelection-7KLhnK1O.js → fileMentionTouchSelection-DspnKU8D.js} +1 -1
- package/dist/assets/{gitApi-BEXddXMX.js → gitApi-BLYdWaG8.js} +1 -1
- package/dist/assets/{insertionBoundaries-HkddYmJd.js → insertionBoundaries-DqlfZVY9.js} +1 -1
- package/dist/assets/{main-BKkCvf3L.js → main-DA7z198b.js} +2 -2
- package/dist/assets/{main-CXE9lij2.js → main-Qw23QJ7L.js} +4 -4
- package/dist/assets/{mcpQueries-xfKOWrAa.js → mcpQueries-C6Tu2cF3.js} +1 -1
- package/dist/assets/{miniChat-BKr-HXzy.js → miniChat-HafcB3H-.js} +2 -2
- package/dist/assets/{mobile-CGL4wWLz.js → mobile-DwXoGpxm.js} +2 -2
- package/dist/assets/{mobileBackNavigation-VzxRWLxN.js → mobileBackNavigation-9MR5h5K6.js} +1 -1
- package/dist/assets/{multirun-BmF58wXR.js → multirun-D-je_w02.js} +1 -1
- package/dist/assets/{native-config-CYjStyIM.js → native-config-Bb3pPQPD.js} +1 -1
- package/dist/assets/{projectMeta-DhdEWvmC.js → projectMeta-DaCyZW_D.js} +1 -1
- package/dist/assets/{readiness-uM7foMQ8.js → readiness-BHqz_NPS.js} +1 -1
- package/dist/assets/{renderElectronMiniChatApp-CYjZYsCZ.js → renderElectronMiniChatApp-COaMALRh.js} +2 -2
- package/dist/assets/{renderMobileApp-Dy2bPXrM.js → renderMobileApp-BOGntDSu.js} +4 -4
- package/dist/assets/{runtimeConfig-Cjjf4017.js → runtimeConfig-BiSluBXe.js} +1 -1
- package/dist/assets/{runtimeEndpointReset-CNyugOcU.js → runtimeEndpointReset-CJwlI_R-.js} +3 -3
- package/dist/assets/{sessionChangeSummary-5npOjhNG.js → sessionChangeSummary-BAv8zzro.js} +1 -1
- package/dist/assets/{sessionLookup-ClhS_rqE.js → sessionLookup-DOw7g15p.js} +1 -1
- package/dist/assets/{speechSettingsQueries-CpOY2X9w.js → speechSettingsQueries-B8zEN5QM.js} +1 -1
- package/dist/assets/types-BiP0jssb.js +2 -0
- package/dist/assets/{useAppFontEffects-BMXDDJtn.js → useAppFontEffects-De1qI6c-.js} +5 -5
- package/dist/assets/{useDesktopWindowControlsLayout-l2q0zXMx.js → useDesktopWindowControlsLayout-BzXi6Wf1.js} +1 -1
- package/dist/assets/{useEffectiveDirectory-rDw8BJo1.js → useEffectiveDirectory-BcbBl_6y.js} +1 -1
- package/dist/assets/{useMobileNavigationStore-EmnPOl61.js → useMobileNavigationStore-DqDCVXO1.js} +3 -3
- package/dist/assets/{useWorkerHighlightedLines-up7t7cwh.js → useWorkerHighlightedLines-CDVleuma.js} +1 -1
- package/dist/index.html +5 -5
- package/dist/mini-chat.html +5 -5
- package/dist/mobile.html +5 -5
- package/package.json +9 -4
- package/server/index.js +3 -3
- package/server/lib/assistants/DOCUMENTATION.md +14 -0
- package/server/lib/assistants/contact-tools.js +145 -10
- package/server/lib/assistants/contact-tools.test.js +11 -2
- package/server/lib/assistants/harness.js +23 -16
- package/server/lib/assistants/harness.test.js +76 -23
- package/server/lib/assistants/memory.http.test.js +2 -2
- package/server/lib/assistants/routes.js +2 -3
- package/server/lib/assistants/service.js +66 -1
- package/server/lib/assistants/service.test.js +37 -0
- package/server/lib/llm/DOCUMENTATION.md +119 -161
- package/server/lib/llm/bridge-client.js +60 -0
- package/server/lib/llm/bridge-client.test.js +35 -0
- package/server/lib/llm/bridge-contract.js +24 -0
- package/server/lib/llm/bridge-plugin.bundle.js +1136 -0
- package/server/lib/llm/bridge-plugin.js +127 -0
- package/server/lib/llm/bridge-plugin.test.js +164 -0
- package/server/lib/llm/completions.js +43 -18
- package/server/lib/llm/completions.test.js +17 -4
- package/server/lib/llm/generate.js +43 -728
- package/server/lib/llm/generate.test.js +62 -1063
- package/server/lib/llm/routes.js +0 -2
- package/server/lib/opencode/feature-routes-runtime.js +1 -0
- package/server/lib/small-model/DOCUMENTATION.md +11 -17
- package/server/lib/small-model/index.js +5 -4
- package/dist/assets/MarkdownRenderer-Dk1A4sLM.js +0 -2
- package/dist/assets/types-C2ml30_A.js +0 -2
- package/server/lib/llm/temp-directory.js +0 -58
- package/server/lib/llm/temp-directory.test.js +0 -57
|
@@ -1,161 +1,119 @@
|
|
|
1
|
-
# Yuri LLM
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
- `
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
The
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
`
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
+
The contact harness may internally defer tool-name validation to its bounded
|
|
83
|
+
correction loop (`validateToolCalls: false`); HTTP body fields cannot disable
|
|
84
|
+
validation, and the harness validates/normalizes every call before execution.
|
|
85
|
+
- Results retain text, tool calls, finish reason, input/output/cache-read usage.
|
|
86
|
+
Empty text with valid tool calls is success. Truly empty results are errors;
|
|
87
|
+
content-filter/error/length endings cannot masquerade as complete output.
|
|
88
|
+
- Each contact retains a stable assistant-scoped cache key, including tool
|
|
89
|
+
follow-ups and result summaries. Cache affinity is not session history. It
|
|
90
|
+
never appends other conversations. No inference response cache is stored.
|
|
91
|
+
- Text files stay bounded inline descriptions. Supported vision inputs become
|
|
92
|
+
native image parts. Data URLs are validated and capped at 20 MiB per file;
|
|
93
|
+
unsupported image models receive descriptions, never image bytes.
|
|
94
|
+
|
|
95
|
+
There is no default inference deadline. An explicit internal `timeoutMs` may
|
|
96
|
+
bound a caller; manual AbortSignal cancellation reaches RPC and the LLM effect.
|
|
97
|
+
Only truly empty responses receive backoff (500ms exponential, capped at 30s).
|
|
98
|
+
Utilities stop after two retries; contacts opt into recovery until cancellation.
|
|
99
|
+
Authentication/configuration/protocol/transport errors are not blindly retried.
|
|
100
|
+
Provider-owned deadlines and Host process loss remain possible failures.
|
|
101
|
+
|
|
102
|
+
## Validation
|
|
103
|
+
|
|
104
|
+
Focused tests cover clean wire messages, native empty-text tool calls, usage,
|
|
105
|
+
model override rejection, host-resolved OAuth, transport-hook body replacement, sanitized RPC errors,
|
|
106
|
+
session-free routing, long-running cancellation and retained side effects.
|
|
107
|
+
`experiments/llm-provider-bridge/production-smoke.mjs` calls the real small-model
|
|
108
|
+
session-title path and the real pi Agent through the production gateway, external
|
|
109
|
+
OpenCode RPC and a selected real provider. OpenAI/Codex and DeepSeek both passed
|
|
110
|
+
25 rounds with 24 native calls and eight verified file edits/readbacks. The script
|
|
111
|
+
forbids Host Session API access and compares global config hashes before/after.
|
|
112
|
+
`provider-smoke.mjs` checks one model per currently connected provider and reports
|
|
113
|
+
each failure separately; it is not an exhaustive all-model certification.
|
|
114
|
+
The current connected-provider run passed 16 of 18 selected models. OpenCode
|
|
115
|
+
returned quota HTTP 402 and direct DeepSeek returned authentication HTTP 401;
|
|
116
|
+
the official stateless generation API independently returned the same statuses.
|
|
117
|
+
Neither failure is hidden by an automatic account/model switch.
|
|
118
|
+
This does not establish real-device UI, OAuth expiry behavior against every vendor, remote provisioning, streaming,
|
|
119
|
+
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
|
+
});
|