@yuriai/web 0.2.7 → 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.
- package/README.md +7 -0
- package/dist/assets/{AssistantView-Be0OKo5o.js → AssistantView-Df-G2DX8.js} +3 -3
- package/dist/assets/{DiagramView-Rpb6oenk.js → DiagramView-BoVqJRQb.js} +1 -1
- package/dist/assets/{GuiPluginSurface-DTH9jNDf.js → GuiPluginSurface-DxLoeTDb.js} +1 -1
- package/dist/assets/MarkdownRenderer-DYi_qpYy.js +2 -0
- package/dist/assets/{MarkdownRendererImpl-CZ9lU4VJ.js → MarkdownRendererImpl-CMxRMFqp.js} +1 -1
- package/dist/assets/{MarkstreamRendererImpl-DastQdu7.js → MarkstreamRendererImpl-DQ4urBZW.js} +1 -1
- package/dist/assets/{MobileDetailNavigation-CittPDZ0.js → MobileDetailNavigation-C4fJlKdu.js} +1 -1
- package/dist/assets/{MobileShareBridge-BxsoZnUJ.js → MobileShareBridge-DLC-TdRy.js} +1 -1
- package/dist/assets/{MobileTabPageHeader-BEgQ3L4u.js → MobileTabPageHeader-CMbbtHh7.js} +1 -1
- package/dist/assets/{MultiRunWindow-1o528NZE.js → MultiRunWindow-Cwp8-_Wh.js} +1 -1
- package/dist/assets/{SettingsView-BGEaQC4R.js → SettingsView-CYDk0wpN.js} +1 -1
- package/dist/assets/{SettingsWindow-MuJFADe3.js → SettingsWindow-Vuzphf9b.js} +1 -1
- package/dist/assets/{TerminalView-CHDyibi_.js → TerminalView-Dhalx6_Z.js} +1 -1
- package/dist/assets/{ToolOutputDialog-Bcry3eoE.js → ToolOutputDialog-DBxSxWNh.js} +1 -1
- package/dist/assets/{appThemeRegistry-CJoBy_eP.js → appThemeRegistry-JEhMXHGF.js} +1 -1
- package/dist/assets/{capgoAdapter-_oWtDrgf.js → capgoAdapter-BgD0wyhC.js} +1 -1
- package/dist/assets/{fileMentionTouchSelection-7KLhnK1O.js → fileMentionTouchSelection-BQdfRBkk.js} +1 -1
- package/dist/assets/{gitApi-BEXddXMX.js → gitApi-DhDVqj9b.js} +1 -1
- package/dist/assets/{insertionBoundaries-HkddYmJd.js → insertionBoundaries-BMe1F3uB.js} +1 -1
- package/dist/assets/{main-CXE9lij2.js → main-CfOYzhn0.js} +4 -4
- package/dist/assets/{main-BKkCvf3L.js → main-DXlMu2LF.js} +2 -2
- package/dist/assets/{mcpQueries-xfKOWrAa.js → mcpQueries-vxcQAIGB.js} +1 -1
- package/dist/assets/{miniChat-BKr-HXzy.js → miniChat-D48iGEEW.js} +2 -2
- package/dist/assets/{mobile-CGL4wWLz.js → mobile-Cy5qSiDY.js} +2 -2
- package/dist/assets/{mobileBackNavigation-VzxRWLxN.js → mobileBackNavigation-QtBK9OSb.js} +1 -1
- package/dist/assets/{multirun-BmF58wXR.js → multirun-BR9a6EBo.js} +1 -1
- package/dist/assets/{native-config-CYjStyIM.js → native-config-CHySeHif.js} +1 -1
- package/dist/assets/{projectMeta-DhdEWvmC.js → projectMeta-Bn2vBNDe.js} +1 -1
- package/dist/assets/{readiness-uM7foMQ8.js → readiness-BAssoNVf.js} +1 -1
- package/dist/assets/{renderElectronMiniChatApp-CYjZYsCZ.js → renderElectronMiniChatApp-B9Kci_8q.js} +2 -2
- package/dist/assets/{renderMobileApp-Dy2bPXrM.js → renderMobileApp-CBivL8Ps.js} +4 -4
- package/dist/assets/{runtimeConfig-Cjjf4017.js → runtimeConfig-COdEwlK-.js} +1 -1
- package/dist/assets/{runtimeEndpointReset-CNyugOcU.js → runtimeEndpointReset-D3x76Qpf.js} +3 -3
- package/dist/assets/{sessionChangeSummary-5npOjhNG.js → sessionChangeSummary-Dtgq0CWg.js} +1 -1
- package/dist/assets/{sessionLookup-ClhS_rqE.js → sessionLookup--Wlr3e8X.js} +1 -1
- package/dist/assets/{speechSettingsQueries-CpOY2X9w.js → speechSettingsQueries-D3fc_4Iy.js} +1 -1
- package/dist/assets/types-D8_26ZKT.js +2 -0
- package/dist/assets/{useAppFontEffects-BMXDDJtn.js → useAppFontEffects-DLT9jVl1.js} +5 -5
- package/dist/assets/{useDesktopWindowControlsLayout-l2q0zXMx.js → useDesktopWindowControlsLayout-BNAZWH9L.js} +1 -1
- package/dist/assets/{useEffectiveDirectory-rDw8BJo1.js → useEffectiveDirectory-D2_-KCB7.js} +1 -1
- package/dist/assets/{useMobileNavigationStore-EmnPOl61.js → useMobileNavigationStore-DJwk64Po.js} +3 -3
- package/dist/assets/{useWorkerHighlightedLines-up7t7cwh.js → useWorkerHighlightedLines-k48txabk.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 +11 -0
- package/server/lib/assistants/harness.js +19 -15
- package/server/lib/assistants/harness.test.js +19 -23
- package/server/lib/assistants/memory.http.test.js +2 -2
- package/server/lib/assistants/routes.js +1 -3
- package/server/lib/llm/DOCUMENTATION.md +116 -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 +42 -18
- package/server/lib/llm/completions.test.js +8 -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/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
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Cause, Context, Effect, Stream } from 'effect';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { HttpClientRequest, HttpClientResponse } from 'effect/http';
|
|
4
|
+
import { AIClient, LLM, LLMRequest, LanguageModel, HttpOptions, Message, Media } from '@opencode/ai/effect';
|
|
5
|
+
import { ModelResolver } from '@opencode/core/model-resolver';
|
|
6
|
+
import { PluginHooks } from '@opencode/core/plugin/hooks';
|
|
7
|
+
import { LlmBridge, LLM_BRIDGE_VERSION } from './bridge-contract.js';
|
|
8
|
+
|
|
9
|
+
const sensitiveBodyKeys = ['messages', 'input', 'system', 'instructions', 'tools', 'tool_choice'];
|
|
10
|
+
const unavailable = () => Object.assign(new Error('External OpenCode clean inference runtime is unavailable or incompatible'), { code: 'unavailable' });
|
|
11
|
+
const providerFailureData = error => {
|
|
12
|
+
const reason = error?.reason;
|
|
13
|
+
const known = new Set(['Authentication', 'RateLimit', 'QuotaExceeded', 'InvalidRequest', 'ProviderConfiguration', 'UnsupportedOperation', 'NoRoute', 'ContentPolicy', 'ProviderInternal', 'Transport', 'Timeout', 'InvalidProviderOutput', 'UnknownProvider']);
|
|
14
|
+
return {
|
|
15
|
+
reason: known.has(reason?._tag) ? reason._tag : 'UnknownProvider',
|
|
16
|
+
...(Number.isInteger(reason?.http?.status) ? { status: reason.http.status } : {}),
|
|
17
|
+
...(!known.has(reason?._tag) ? { diagnostic: error?.message?.includes('Schema validation failed') ? 'runtime_schema_mismatch'
|
|
18
|
+
: error?.message?.includes('Service not found') ? 'missing_runtime_service'
|
|
19
|
+
: error?.name === 'TypeError' ? 'type_error' : error?.name === 'ReferenceError' ? 'reference_error' : 'unclassified' } : {}),
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const toMessage = (message) => {
|
|
24
|
+
if (message.role === 'tool') return Message.tool({ id: message.tool_call_id, name: message.name, result: { type: 'text', value: message.content } });
|
|
25
|
+
if (message.role === 'assistant') return Message.assistant([
|
|
26
|
+
...(message.content ? [Message.text(message.content)] : []),
|
|
27
|
+
...(message.tool_calls ?? []).map(call => ({ type: 'tool-call', id: call.id, name: call.function.name, input: JSON.parse(call.function.arguments) })),
|
|
28
|
+
]);
|
|
29
|
+
const content = [Message.text(message.content)];
|
|
30
|
+
for (const part of message.parts ?? []) {
|
|
31
|
+
if (part.mime?.startsWith('image/')) {
|
|
32
|
+
const comma = part.url.indexOf(',');
|
|
33
|
+
content.push(Message.media(Media.base64(part.url.slice(comma + 1), part.mime)));
|
|
34
|
+
} else content.push(Message.text(`[file: ${part.filename || part.mime}] ${part.url}`));
|
|
35
|
+
}
|
|
36
|
+
return Message.user(content);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// RPC effects run in the external service's Location context. Never build a parallel runtime.
|
|
40
|
+
const hostRuntime = Effect.context().pipe(Effect.flatMap(context => {
|
|
41
|
+
const resolver = Context.getOption(context, ModelResolver.Service);
|
|
42
|
+
const hooks = Context.getOption(context, PluginHooks.Service);
|
|
43
|
+
return resolver._tag === 'Some' && typeof resolver.value.resolve === 'function'
|
|
44
|
+
&& hooks._tag === 'Some' && typeof hooks.value.trigger === 'function'
|
|
45
|
+
? Effect.succeed({ resolver: resolver.value, hooks: hooks.value })
|
|
46
|
+
: Effect.fail(unavailable());
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
// No Session creation, context builder, instruction hooks or tool executors belong here.
|
|
50
|
+
export const generateBridgeResponse = input => Effect.gen(function* () {
|
|
51
|
+
const { resolver, hooks } = yield* hostRuntime;
|
|
52
|
+
const model = yield* resolver.resolve({ providerID: input.providerID, id: input.modelID, ...(input.variant ? { variant: input.variant } : {}) });
|
|
53
|
+
if (!model) return yield* Effect.fail(unavailable());
|
|
54
|
+
if ([model.model.defaults?.http?.body, model.model.route.defaults?.http?.body].some(body => sensitiveBodyKeys.some(key => Object.hasOwn(body ?? {}, key)))) {
|
|
55
|
+
return yield* Effect.fail(unavailable());
|
|
56
|
+
}
|
|
57
|
+
// This ID is cache affinity only; it does not create a durable OpenCode Session.
|
|
58
|
+
const scope = { sessionID: `ses_${createHash('sha256').update(input.cacheKey).digest('hex')}`, agent: 'yuri-inference', model: model.ref, kind: 'generate' };
|
|
59
|
+
const base = LLM.request({
|
|
60
|
+
// The host and bundled SDK have distinct constructors; preserve the host's route, normalize its value wrapper.
|
|
61
|
+
model: LanguageModel.make(model.model),
|
|
62
|
+
system: input.messages.filter(message => message.role === 'system').map(message => message.content).join('\n\n'),
|
|
63
|
+
messages: input.messages.filter(message => message.role !== 'system').map(toMessage),
|
|
64
|
+
tools: input.tools ?? [],
|
|
65
|
+
promptCacheKey: input.cacheKey,
|
|
66
|
+
...(model.cache?.ttl === undefined ? {} : { cache: { ttl: model.cache.ttl } }),
|
|
67
|
+
http: { headers: { 'x-opencode-session': input.cacheKey, 'x-session-affinity': input.cacheKey, 'X-Session-Id': input.cacheKey, 'X-Magpie-Session': input.cacheKey } },
|
|
68
|
+
...(input.maxOutputTokens ? { generation: { maxTokens: input.maxOutputTokens } } : {}),
|
|
69
|
+
});
|
|
70
|
+
// Only transport hooks run. Context/generate hooks could append Agent instructions.
|
|
71
|
+
const adapted = yield* hooks.trigger('session', 'model.request', { ...scope, baseURL: base.model.route.endpoint.baseURL, headers: { ...base.http?.headers } });
|
|
72
|
+
const route = adapted.baseURL !== undefined && adapted.baseURL !== base.model.route.endpoint.baseURL
|
|
73
|
+
? base.model.route.with({ endpoint: { baseURL: adapted.baseURL } }) : base.model.route;
|
|
74
|
+
// Provider transports may construct requests with their own SDK's nominal model schema.
|
|
75
|
+
// Cross that boundary with the original host wrapper, not the bundled SDK wrapper.
|
|
76
|
+
const hostModel = route === model.model.route ? model.model : model.model.constructor.update(model.model, { route });
|
|
77
|
+
const hostRequest = request => ({ ...request, model: hostModel });
|
|
78
|
+
const bridgedRoute = {
|
|
79
|
+
...route,
|
|
80
|
+
body: { ...route.body, from: request => route.body.from(hostRequest(request)) },
|
|
81
|
+
prepareTransport: (body, request, options) => route.prepareTransport(body, hostRequest(request), options),
|
|
82
|
+
streamPrepared: (prepared, request, runtime, options) => route.streamPrepared(prepared, hostRequest(request), runtime, options),
|
|
83
|
+
};
|
|
84
|
+
const request = LLMRequest.update(base, {
|
|
85
|
+
model: LanguageModel.update(base.model, { route: bridgedRoute }),
|
|
86
|
+
http: new HttpOptions({ ...base.http, headers: adapted.headers }),
|
|
87
|
+
});
|
|
88
|
+
const result = yield* LLM.generate(request, {
|
|
89
|
+
http: (req, handler) => Effect.gen(function* () {
|
|
90
|
+
const original = yield* HttpClientRequest.toWeb(req);
|
|
91
|
+
const before = yield* hooks.trigger('session', 'http.request', { ...scope, request: original });
|
|
92
|
+
const sent = before.request === original
|
|
93
|
+
? HttpClientRequest.setBody(HttpClientRequest.fromWeb(original), req.body)
|
|
94
|
+
: before.request.body
|
|
95
|
+
? HttpClientRequest.bodyUint8Array(HttpClientRequest.fromWeb(before.request),
|
|
96
|
+
new Uint8Array(yield* Effect.promise(() => before.request.clone().arrayBuffer())), before.request.headers.get('content-type') ?? undefined)
|
|
97
|
+
: HttpClientRequest.fromWeb(before.request);
|
|
98
|
+
const response = yield* handler(sent);
|
|
99
|
+
const after = yield* hooks.trigger('session', 'http.response', {
|
|
100
|
+
...scope, request: before.request,
|
|
101
|
+
response: new Response([204, 205, 304].includes(response.status) ? null : yield* Stream.toReadableStreamEffect(response.stream), { status: response.status, headers: response.headers }),
|
|
102
|
+
});
|
|
103
|
+
return HttpClientResponse.fromWeb(sent, after.response);
|
|
104
|
+
}),
|
|
105
|
+
}).pipe(Effect.provide(AIClient.layer));
|
|
106
|
+
return {
|
|
107
|
+
text: result.text,
|
|
108
|
+
toolCalls: result.toolCalls.map(call => ({ id: call.id, name: call.name, arguments: call.input })),
|
|
109
|
+
finishReason: result.finishReason.normalized,
|
|
110
|
+
usage: { inputTokens: result.usage?.inputTokens ?? 0, outputTokens: result.usage?.outputTokens ?? 0, cacheReadInputTokens: result.usage?.cacheReadInputTokens ?? 0 },
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
export default {
|
|
115
|
+
id: 'yuri.llm.bridge',
|
|
116
|
+
effect: ctx => ctx.rpc.register(LlmBridge, {
|
|
117
|
+
ready: () => hostRuntime.pipe(Effect.as({ version: LLM_BRIDGE_VERSION })),
|
|
118
|
+
generate: (input, call) => generateBridgeResponse(input).pipe(Effect.catchCause(cause => {
|
|
119
|
+
if (Cause.hasInterrupts(cause)) return Effect.failCause(cause);
|
|
120
|
+
// Upstream failures and defects can contain credentials or private prompts. Never forward or log them.
|
|
121
|
+
const error = Cause.squash(cause);
|
|
122
|
+
return Effect.fail(call.error(error?.code === 'unavailable' ? 'unavailable' : 'generation_failed',
|
|
123
|
+
error?.code === 'unavailable' ? unavailable().message : 'LLM provider generation failed',
|
|
124
|
+
error?.code === 'unavailable' ? {} : providerFailureData(error)));
|
|
125
|
+
})),
|
|
126
|
+
}),
|
|
127
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { Effect } from 'effect';
|
|
4
|
+
import { ModelResolver } from '@opencode/core/model-resolver';
|
|
5
|
+
import { PluginHooks } from '@opencode/core/plugin/hooks';
|
|
6
|
+
import * as compatible from '@opencode/ai/providers/openai-compatible';
|
|
7
|
+
import bridge, { generateBridgeResponse } from './bridge-plugin.js';
|
|
8
|
+
|
|
9
|
+
const servers = [];
|
|
10
|
+
afterEach(async () => { for (const server of servers.splice(0)) { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); } });
|
|
11
|
+
|
|
12
|
+
async function fixture(reply) {
|
|
13
|
+
const requests = [];
|
|
14
|
+
const headers = [];
|
|
15
|
+
const server = http.createServer(async (req, res) => {
|
|
16
|
+
const chunks = [];
|
|
17
|
+
for await (const chunk of req) chunks.push(chunk);
|
|
18
|
+
requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8')));
|
|
19
|
+
headers.push(req.headers);
|
|
20
|
+
res.setHeader('content-type', 'text/event-stream');
|
|
21
|
+
const delta = { ...reply, ...(reply.tool_calls ? { tool_calls: reply.tool_calls.map((call, index) => ({ ...call, index })) } : {}) };
|
|
22
|
+
res.end(`data: ${JSON.stringify({ id: 'fixture', object: 'chat.completion.chunk', created: 1, model: 'fixture', choices: [{ index: 0, delta, finish_reason: reply.tool_calls ? 'tool_calls' : 'stop' }], usage: { prompt_tokens: 100, completion_tokens: 10, total_tokens: 110, prompt_tokens_details: { cached_tokens: 80 } } })}\n\ndata: [DONE]\n\n`);
|
|
23
|
+
});
|
|
24
|
+
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
25
|
+
servers.push(server);
|
|
26
|
+
const model = { id: 'fixture', providerID: 'fixture', package: '@opencode/ai/providers/openai-compatible', settings: { baseURL: `http://127.0.0.1:${server.address().port}/v1` }, capabilities: { input: ['text'], output: ['text'], tools: true }, headers: {}, body: {}, variants: [] };
|
|
27
|
+
const credential = { type: 'key', key: 'fixture-only' };
|
|
28
|
+
const resolve = vi.fn(ref => ModelResolver.withVariant(model, ref.variant).pipe(
|
|
29
|
+
Effect.flatMap(selected => ModelResolver.fromCatalogModel(selected, credential, { loadPackage: () => Effect.succeed(compatible) })),
|
|
30
|
+
Effect.map(runtime => ({ model: runtime, ref })),
|
|
31
|
+
));
|
|
32
|
+
const trigger = vi.fn((_domain, _name, event) => Effect.succeed(event));
|
|
33
|
+
const runEffect = effect => Effect.runPromise(effect.pipe(
|
|
34
|
+
Effect.provideService(ModelResolver.Service, { resolve }),
|
|
35
|
+
Effect.provideService(PluginHooks.Service, { trigger }),
|
|
36
|
+
));
|
|
37
|
+
const run = input => runEffect(generateBridgeResponse(input));
|
|
38
|
+
const input = { providerID: 'fixture', modelID: 'fixture', cacheKey: 'lane-a', messages: [{ role: 'system', content: 'Only this system' }, { role: 'user', content: 'Only this user' }] };
|
|
39
|
+
return { input, requests, headers, model, credential, resolve, trigger, run, runEffect };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('external runtime provider bridge', () => {
|
|
43
|
+
it('sends only caller context and returns measured usage through the host resolver', async () => {
|
|
44
|
+
const { input, requests, resolve, trigger, run } = await fixture({ role: 'assistant', content: 'summary' });
|
|
45
|
+
expect(await run(input)).toMatchObject({ text: 'summary', toolCalls: [], usage: { inputTokens: 100, outputTokens: 10, cacheReadInputTokens: 80 } });
|
|
46
|
+
expect(requests[0].messages).toEqual(input.messages);
|
|
47
|
+
expect(resolve).toHaveBeenCalledExactlyOnceWith({ providerID: 'fixture', id: 'fixture' });
|
|
48
|
+
expect(trigger.mock.calls.map(([, name]) => name)).toEqual(['model.request', 'http.request', 'http.response']);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('retains native tool calls when the provider has no assistant text', async () => {
|
|
52
|
+
const { input, requests, run } = await fixture({ role: 'assistant', content: null, tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'lookup', arguments: '{"id":1}' } }] });
|
|
53
|
+
input.tools = [{ name: 'lookup', description: 'Lookup', inputSchema: { type: 'object', properties: { id: { type: 'integer' } }, required: ['id'] } }];
|
|
54
|
+
expect(await run(input)).toMatchObject({ text: '', toolCalls: [{ id: 'call_1', name: 'lookup', arguments: { id: 1 } }], finishReason: 'tool-calls' });
|
|
55
|
+
expect(requests[0].tools[0].function.name).toBe('lookup');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('rejects model overrides that could inject context', async () => {
|
|
59
|
+
const { input, model, requests, run } = await fixture({ role: 'assistant', content: 'unused' });
|
|
60
|
+
model.body = { messages: [{ role: 'system', content: 'foreign' }] };
|
|
61
|
+
await expect(run(input)).rejects.toMatchObject({ code: 'unavailable' });
|
|
62
|
+
expect(requests).toHaveLength(0);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('round-trips native assistant calls and tool results without flattening roles', async () => {
|
|
66
|
+
const { input, requests, run } = await fixture({ role: 'assistant', content: 'verified' });
|
|
67
|
+
input.messages.push(
|
|
68
|
+
{ role: 'assistant', content: '', tool_calls: [{ id: 'call_1', function: { name: 'lookup', arguments: '{"id":1}' } }] },
|
|
69
|
+
{ role: 'tool', name: 'lookup', tool_call_id: 'call_1', content: '{"value":"found"}' },
|
|
70
|
+
);
|
|
71
|
+
await run(input);
|
|
72
|
+
expect(requests[0].messages[2]).toMatchObject({ role: 'assistant', tool_calls: [{ id: 'call_1', function: { name: 'lookup', arguments: '{"id":1}' } }] });
|
|
73
|
+
expect(requests[0].messages[3]).toMatchObject({ role: 'tool', tool_call_id: 'call_1', content: '{"value":"found"}' });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('accepts host-resolved OAuth and applies refreshed authorization through transport hooks', async () => {
|
|
77
|
+
const { input, credential, trigger, headers, run } = await fixture({ role: 'assistant', content: 'authorized' });
|
|
78
|
+
Object.assign(credential, { type: 'oauth', access: 'fixture-oauth-token' });
|
|
79
|
+
trigger.mockImplementation((_domain, name, event) => Effect.sync(() => {
|
|
80
|
+
if (name === 'model.request') event.headers['session-id'] = event.sessionID;
|
|
81
|
+
if (name === 'http.request') event.request.headers.set('authorization', 'Bearer fixture-refreshed-token');
|
|
82
|
+
return event;
|
|
83
|
+
}));
|
|
84
|
+
expect(await run(input)).toMatchObject({ text: 'authorized' });
|
|
85
|
+
expect(headers[0]).toMatchObject({ authorization: 'Bearer fixture-refreshed-token', 'session-id': expect.stringMatching(/^ses_[0-9a-f]{64}$/) });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('forwards provider and variant selection without a Yuri package allowlist', async () => {
|
|
89
|
+
const { input, model, resolve, run } = await fixture({ role: 'assistant', content: 'variant' });
|
|
90
|
+
model.variants = [{ id: 'custom', body: { temperature: 0.2 } }];
|
|
91
|
+
await run({ ...input, providerID: 'custom-provider', variant: 'custom' });
|
|
92
|
+
expect(resolve).toHaveBeenCalledExactlyOnceWith({ providerID: 'custom-provider', id: 'fixture', variant: 'custom' });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('preserves replacement HTTP request bodies from provider hooks', async () => {
|
|
96
|
+
const { input, trigger, requests, run } = await fixture({ role: 'assistant', content: 'adapted' });
|
|
97
|
+
trigger.mockImplementation((_domain, name, event) => name === 'http.request'
|
|
98
|
+
? Effect.tryPromise(async () => { const body = await event.request.clone().json(); event.request = new Request(event.request, { body: JSON.stringify({ ...body, provider_marker: true }) }); return event; })
|
|
99
|
+
: Effect.succeed(event));
|
|
100
|
+
await run(input);
|
|
101
|
+
expect(requests[0].provider_marker).toBe(true);
|
|
102
|
+
expect(requests[0].messages).toEqual(input.messages);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('retains host model identity after a provider rewrites its endpoint', async () => {
|
|
106
|
+
const { input, trigger, run } = await fixture({ role: 'assistant', content: 'routed' });
|
|
107
|
+
trigger.mockImplementation((_domain, name, event) => Effect.sync(() => {
|
|
108
|
+
if (name === 'model.request') event.baseURL += '/alternate';
|
|
109
|
+
return event;
|
|
110
|
+
}));
|
|
111
|
+
expect(await run(input)).toMatchObject({ text: 'routed' });
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('reports only allowlisted failure categories and status, not provider error bodies', async () => {
|
|
115
|
+
const { resolve, runEffect, input } = await fixture({ role: 'assistant', content: 'unused' });
|
|
116
|
+
resolve.mockImplementation(() => Effect.fail({ message: 'private credential', reason: { _tag: 'Authentication', body: 'private credential', http: { status: 401, headers: { authorization: 'private credential' } } } }));
|
|
117
|
+
let handlers;
|
|
118
|
+
await Effect.runPromise(bridge.effect({ rpc: { register: (_definition, value) => { handlers = value; return Effect.void; } } }));
|
|
119
|
+
const error = (type, message, data) => ({ type, message, data });
|
|
120
|
+
await expect(runEffect(handlers.generate(input, { error }))).rejects.toEqual({ type: 'generation_failed', message: 'LLM provider generation failed', data: { reason: 'Authentication', status: 401 } });
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('fails explicitly when the external runtime services are unavailable', async () => {
|
|
124
|
+
await expect(Effect.runPromise(generateBridgeResponse({}))).rejects.toMatchObject({ code: 'unavailable' });
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('normalizes a model wrapper from a separately bundled host SDK', async () => {
|
|
128
|
+
const { input, resolve, run } = await fixture({ role: 'assistant', content: 'host model' });
|
|
129
|
+
const original = resolve.getMockImplementation();
|
|
130
|
+
resolve.mockImplementation(ref => original(ref).pipe(Effect.map(value => {
|
|
131
|
+
const host = { ...value.model };
|
|
132
|
+
const prepare = host.route.prepareTransport;
|
|
133
|
+
host.route = { ...host.route, prepareTransport: (body, request, options) => {
|
|
134
|
+
expect(request.model).toBe(host);
|
|
135
|
+
return prepare(body, request, options);
|
|
136
|
+
} };
|
|
137
|
+
return { ...value, model: host };
|
|
138
|
+
})));
|
|
139
|
+
expect(await run(input)).toMatchObject({ text: 'host model' });
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('preserves interruption instead of converting cancellation into a provider error', async () => {
|
|
143
|
+
let handlers;
|
|
144
|
+
await Effect.runPromise(bridge.effect({ rpc: { register: (_definition, value) => { handlers = value; return Effect.void; } } }));
|
|
145
|
+
const controller = new AbortController();
|
|
146
|
+
const error = vi.fn();
|
|
147
|
+
const pending = Effect.runPromise(handlers.generate({}, { error }).pipe(
|
|
148
|
+
Effect.provideService(ModelResolver.Service, { resolve: () => Effect.never }),
|
|
149
|
+
Effect.provideService(PluginHooks.Service, { trigger: () => Effect.never }),
|
|
150
|
+
), { signal: controller.signal }).catch(value => value);
|
|
151
|
+
controller.abort();
|
|
152
|
+
expect(await pending).toBeInstanceOf(Error);
|
|
153
|
+
expect(error).not.toHaveBeenCalled();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it.each(['fail', 'die'])('does not expose private upstream %s through RPC', async (failure) => {
|
|
157
|
+
const { resolve, runEffect, input } = await fixture({ role: 'assistant', content: 'unused' });
|
|
158
|
+
resolve.mockImplementation(() => Effect[failure](new Error('private upstream content')));
|
|
159
|
+
let handlers;
|
|
160
|
+
await Effect.runPromise(bridge.effect({ rpc: { register: (_definition, value) => { handlers = value; return Effect.void; } } }));
|
|
161
|
+
const error = (type, message, data) => ({ type, message, data });
|
|
162
|
+
await expect(runEffect(handlers.generate(input, { error }))).rejects.toEqual({ type: 'generation_failed', message: 'LLM provider generation failed', data: { reason: 'UnknownProvider', diagnostic: 'unclassified' } });
|
|
163
|
+
});
|
|
164
|
+
});
|
|
@@ -47,23 +47,37 @@ const normalizeMessages = (messages) => {
|
|
|
47
47
|
throw new LlmError('validation_error', 400, 'messages is required');
|
|
48
48
|
}
|
|
49
49
|
return messages.map((message) => {
|
|
50
|
-
if (!isRecord(message) || typeof message.content !== 'string') {
|
|
50
|
+
if (!isRecord(message) || (typeof message.content !== 'string' && !(message.role === 'assistant' && message.content == null && Array.isArray(message.tool_calls)))) {
|
|
51
51
|
throw new LlmError('validation_error', 400, 'each message needs role and string content');
|
|
52
52
|
}
|
|
53
53
|
const role = message.role;
|
|
54
|
-
if (role !== 'system' && role !== 'user' && role !== 'assistant') {
|
|
55
|
-
throw new LlmError('validation_error', 400, 'message role must be system, user, or
|
|
54
|
+
if (role !== 'system' && role !== 'user' && role !== 'assistant' && role !== 'tool') {
|
|
55
|
+
throw new LlmError('validation_error', 400, 'message role must be system, user, assistant, or tool');
|
|
56
56
|
}
|
|
57
57
|
const parts = Array.isArray(message.parts)
|
|
58
58
|
? message.parts.map(normalizeCompletionFilePart).filter(Boolean)
|
|
59
59
|
: [];
|
|
60
|
-
|
|
60
|
+
const normalized = { role, content: message.content ?? '' };
|
|
61
|
+
if (role === 'tool') {
|
|
62
|
+
if (typeof message.tool_call_id !== 'string' || !message.tool_call_id || typeof message.name !== 'string' || !message.name) throw new LlmError('validation_error', 400, 'tool result needs name and tool_call_id');
|
|
63
|
+
normalized.tool_call_id = message.tool_call_id;
|
|
64
|
+
normalized.name = message.name;
|
|
65
|
+
}
|
|
66
|
+
if (role === 'assistant' && message.tool_calls != null) {
|
|
67
|
+
if (!Array.isArray(message.tool_calls)) throw new LlmError('validation_error', 400, 'tool_calls must be an array');
|
|
68
|
+
normalized.tool_calls = message.tool_calls.map(call => {
|
|
69
|
+
if (typeof call?.id !== 'string' || !call.id || typeof call.function?.name !== 'string' || !call.function.name || typeof call.function?.arguments !== 'string') throw new LlmError('validation_error', 400, 'Invalid assistant tool call');
|
|
70
|
+
try { JSON.parse(call.function.arguments); } catch { throw new LlmError('validation_error', 400, 'Invalid tool arguments'); }
|
|
71
|
+
return { id: call.id, type: 'function', function: { name: call.function.name, arguments: call.function.arguments } };
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return parts.length > 0 ? { ...normalized, parts } : normalized;
|
|
61
75
|
});
|
|
62
76
|
};
|
|
63
77
|
|
|
64
78
|
const completionId = () => `chatcmpl_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
|
|
65
79
|
|
|
66
|
-
export function toOpenAICompletion({ id, model, text, created = Math.floor(Date.now() / 1000) }) {
|
|
80
|
+
export function toOpenAICompletion({ id, model, text, toolCalls = [], usage, created = Math.floor(Date.now() / 1000) }) {
|
|
67
81
|
return {
|
|
68
82
|
id,
|
|
69
83
|
object: 'chat.completion',
|
|
@@ -71,10 +85,10 @@ export function toOpenAICompletion({ id, model, text, created = Math.floor(Date.
|
|
|
71
85
|
model,
|
|
72
86
|
choices: [{
|
|
73
87
|
index: 0,
|
|
74
|
-
message: { role: 'assistant', content: text },
|
|
75
|
-
finish_reason: 'stop',
|
|
88
|
+
message: { role: 'assistant', content: text, ...(toolCalls.length ? { tool_calls: toolCalls.map(call => ({ id: call.id, type: 'function', function: { name: call.name, arguments: JSON.stringify(call.arguments) } })) } : {}) },
|
|
89
|
+
finish_reason: toolCalls.length ? 'tool_calls' : 'stop',
|
|
76
90
|
}],
|
|
77
|
-
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
|
|
91
|
+
usage: { prompt_tokens: usage?.inputTokens ?? 0, completion_tokens: usage?.outputTokens ?? 0, total_tokens: (usage?.inputTokens ?? 0) + (usage?.outputTokens ?? 0), prompt_tokens_details: { cached_tokens: usage?.cacheReadInputTokens ?? 0 } },
|
|
78
92
|
};
|
|
79
93
|
};
|
|
80
94
|
|
|
@@ -83,8 +97,7 @@ export function toOpenAICompletion({ id, model, text, created = Math.floor(Date.
|
|
|
83
97
|
*
|
|
84
98
|
* Public HTTP stays non-streaming (`stream: true` → validation_error).
|
|
85
99
|
* In-process callers (Assistant contact harness) may pass `onTextDelta` and
|
|
86
|
-
* `globalEventHub
|
|
87
|
-
* `session.text.delta` tokens only — never as fake post-hoc SSE on this route.
|
|
100
|
+
* `globalEventHub`. The one-shot bridge does not fabricate token callbacks.
|
|
88
101
|
*/
|
|
89
102
|
export async function createChatCompletion({
|
|
90
103
|
body,
|
|
@@ -93,7 +106,6 @@ export async function createChatCompletion({
|
|
|
93
106
|
clientFactory,
|
|
94
107
|
loadCatalog = loadConnectedCatalog,
|
|
95
108
|
generateText = generateOpenCodeText,
|
|
96
|
-
ensureTempDirectory,
|
|
97
109
|
onTextDelta = null,
|
|
98
110
|
globalEventHub = null,
|
|
99
111
|
persistSessionMetadata = null,
|
|
@@ -101,11 +113,12 @@ export async function createChatCompletion({
|
|
|
101
113
|
signal = null,
|
|
102
114
|
timeoutMs,
|
|
103
115
|
retryEmptyTextUntilCancelled = false,
|
|
104
|
-
|
|
116
|
+
cacheKey,
|
|
117
|
+
maxOutputTokens,
|
|
105
118
|
}) {
|
|
106
119
|
signal?.throwIfAborted();
|
|
107
120
|
if (!isRecord(body)) throw new LlmError('validation_error', 400, 'JSON body is required');
|
|
108
|
-
//
|
|
121
|
+
// The external inference RPC returns a full turn. This HTTP gateway
|
|
109
122
|
// does not token-stream. Do not emit fake SSE after the fact.
|
|
110
123
|
// In-process onTextDelta (optional) is a separate internal callback path.
|
|
111
124
|
if (body.stream === true) {
|
|
@@ -122,6 +135,11 @@ export async function createChatCompletion({
|
|
|
122
135
|
throw new LlmError('validation_error', 400, 'variant must be a string or null');
|
|
123
136
|
}
|
|
124
137
|
const variant = body.variant?.trim() || undefined;
|
|
138
|
+
if (body.tools != null && !Array.isArray(body.tools)) throw new LlmError('validation_error', 400, 'tools must be an array');
|
|
139
|
+
const tools = (body.tools ?? []).map(tool => {
|
|
140
|
+
if (tool?.type !== 'function' || typeof tool.function?.name !== 'string' || !tool.function.name || !isRecord(tool.function.parameters)) throw new LlmError('validation_error', 400, 'Invalid tool schema');
|
|
141
|
+
return { name: tool.function.name, description: tool.function.description || tool.function.name, inputSchema: tool.function.parameters };
|
|
142
|
+
});
|
|
125
143
|
|
|
126
144
|
const client = clientFactory?.() ?? null;
|
|
127
145
|
const catalog = await loadCatalog(client ?? {
|
|
@@ -147,7 +165,6 @@ export async function createChatCompletion({
|
|
|
147
165
|
...(variant ? { variant } : {}),
|
|
148
166
|
messages,
|
|
149
167
|
clientFactory,
|
|
150
|
-
ensureTempDirectory,
|
|
151
168
|
forwardImageParts: Boolean(catalog.models.find((entry) => (
|
|
152
169
|
entry.providerID === resolved.providerID && entry.modelID === resolved.modelID
|
|
153
170
|
))?.acceptsImages),
|
|
@@ -158,7 +175,9 @@ export async function createChatCompletion({
|
|
|
158
175
|
signal,
|
|
159
176
|
timeoutMs,
|
|
160
177
|
retryEmptyTextUntilCancelled,
|
|
161
|
-
|
|
178
|
+
tools,
|
|
179
|
+
...(cacheKey ? { cacheKey } : {}),
|
|
180
|
+
...(maxOutputTokens ? { maxOutputTokens } : {}),
|
|
162
181
|
});
|
|
163
182
|
} catch (error) {
|
|
164
183
|
if (error instanceof LlmError) throw error;
|
|
@@ -174,20 +193,25 @@ export async function createChatCompletion({
|
|
|
174
193
|
: 'LLM attachment generation is unavailable',
|
|
175
194
|
);
|
|
176
195
|
}
|
|
177
|
-
|
|
196
|
+
const failure = new LlmError(
|
|
178
197
|
'upstream_error',
|
|
179
198
|
502,
|
|
180
199
|
typeof error?.message === 'string' && error.message.trim()
|
|
181
200
|
? error.message
|
|
182
201
|
: 'OpenCode generate failed',
|
|
183
202
|
);
|
|
203
|
+
if (error?.type === 'generation_failed' && isRecord(error.data)) {
|
|
204
|
+
failure.providerFailure = error.data;
|
|
205
|
+
}
|
|
206
|
+
throw failure;
|
|
184
207
|
}
|
|
185
208
|
const text = typeof generated?.text === 'string' ? generated.text.trim() : '';
|
|
186
|
-
if (!text) throw new LlmError('upstream_error', 502, 'OpenCode returned no assistant text');
|
|
209
|
+
if (!text && !generated?.toolCalls?.length) throw new LlmError('upstream_error', 502, 'OpenCode returned no assistant text');
|
|
210
|
+
if (generated.toolCalls?.some(call => !tools.some(tool => tool.name === call.name))) throw new LlmError('upstream_error', 502, 'Model returned an unavailable tool');
|
|
187
211
|
|
|
188
212
|
const model = `${resolved.providerID}/${resolved.modelID}`;
|
|
189
213
|
return {
|
|
190
|
-
completion: toOpenAICompletion({ id: completionId(), model, text }),
|
|
214
|
+
completion: toOpenAICompletion({ id: completionId(), model, text, toolCalls: generated.toolCalls, usage: generated.usage }),
|
|
191
215
|
providerID: resolved.providerID,
|
|
192
216
|
modelID: resolved.modelID,
|
|
193
217
|
source: generated.source,
|
|
@@ -2,19 +2,23 @@ import { describe, expect, it, vi } from 'vitest'
|
|
|
2
2
|
import { LlmError, createChatCompletion } from './completions.js'
|
|
3
3
|
|
|
4
4
|
describe('createChatCompletion', () => {
|
|
5
|
+
it.each([{}, [{ id: 1, function: { name: 'read', arguments: '{}' } }]])('rejects malformed native tool calls before inference: %j', async (tool_calls) => {
|
|
6
|
+
const generateText = vi.fn();
|
|
7
|
+
await expect(createChatCompletion({ generateText, body: { model: 'p/m', messages: [{ role: 'assistant', content: '', tool_calls }] } })).rejects.toMatchObject({ code: 'validation_error', statusCode: 400 });
|
|
8
|
+
expect(generateText).not.toHaveBeenCalled();
|
|
9
|
+
});
|
|
5
10
|
it('forwards internal long-running generation options without accepting them from HTTP bodies', async () => {
|
|
6
11
|
const generateText = vi.fn(async () => ({ text: 'done' }))
|
|
7
12
|
const base = {
|
|
8
13
|
generateText,
|
|
9
14
|
loadCatalog: async () => ({ models: [{ providerID: 'p', modelID: 'm' }] }),
|
|
10
|
-
body: { model: 'p/m', messages: [{ role: 'user', content: 'hi' }], timeoutMs: 0, retryEmptyTextUntilCancelled: true
|
|
15
|
+
body: { model: 'p/m', messages: [{ role: 'user', content: 'hi' }], timeoutMs: 0, retryEmptyTextUntilCancelled: true },
|
|
11
16
|
}
|
|
12
17
|
await createChatCompletion(base)
|
|
13
18
|
expect(generateText.mock.calls[0][0].timeoutMs).toBeUndefined()
|
|
14
19
|
expect(generateText.mock.calls[0][0].retryEmptyTextUntilCancelled).not.toBe(true)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
expect(generateText.mock.calls[1][0]).toMatchObject({ timeoutMs: 0, retryEmptyTextUntilCancelled: true, textEnvelope: true })
|
|
20
|
+
await createChatCompletion({ ...base, timeoutMs: 0, retryEmptyTextUntilCancelled: true })
|
|
21
|
+
expect(generateText.mock.calls[1][0]).toMatchObject({ timeoutMs: 0, retryEmptyTextUntilCancelled: true })
|
|
18
22
|
})
|
|
19
23
|
it.each(['high', 'medium', null, ''])('forwards variant %s with default compatibility', async (variant) => {
|
|
20
24
|
const generateText = vi.fn(async () => ({ text: 'done' }));
|