@opencode/ai 0.0.0-dev-466 → 0.0.0-dev-468
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 +27 -16
- package/dist/cache-policy.d.ts +1 -1
- package/dist/cache-policy.js +2 -2
- package/dist/decision-client.d.ts +3 -3
- package/dist/decision.d.ts +13 -120
- package/dist/decision.js +17 -13
- package/dist/effect.d.ts +8 -6
- package/dist/effect.js +4 -3
- package/dist/effort-updates.d.ts +1 -1
- package/dist/effort-updates.js +2 -2
- package/dist/experimental/cloudflare-run-extraction.d.ts +7 -3
- package/dist/experimental/cloudflare-run-extraction.js +4 -1
- package/dist/experimental/extraction.d.ts +12 -38
- package/dist/experimental/extraction.js +13 -11
- package/dist/experimental.d.ts +8 -8
- package/dist/experimental.js +4 -4
- package/dist/image-client.d.ts +11 -5
- package/dist/image-client.js +1 -1
- package/dist/image.d.ts +21 -62
- package/dist/image.js +16 -13
- package/dist/index.d.ts +20 -14
- package/dist/index.js +6 -3
- package/dist/llm.d.ts +60 -25
- package/dist/llm.js +126 -38
- package/dist/protocols/alibaba-images.d.ts +2 -2
- package/dist/protocols/alibaba-messages.js +3 -3
- package/dist/protocols/anthropic-messages.d.ts +1 -1
- package/dist/protocols/anthropic-messages.js +4 -3
- package/dist/protocols/assemblyai-transcription.d.ts +2 -2
- package/dist/protocols/cartesia-speech.d.ts +2 -2
- package/dist/protocols/cloudflare-workers-ai-images.d.ts +2 -2
- package/dist/protocols/deepgram-speech.d.ts +2 -2
- package/dist/protocols/deepgram-transcription.d.ts +2 -2
- package/dist/protocols/elevenlabs-speech.d.ts +2 -2
- package/dist/protocols/elevenlabs-transcription.d.ts +2 -2
- package/dist/protocols/gemini.js +2 -8
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-speech.d.ts +2 -2
- package/dist/protocols/google-transcription.d.ts +2 -2
- package/dist/protocols/google-video.d.ts +2 -2
- package/dist/protocols/meta-images.d.ts +2 -2
- package/dist/protocols/meta-responses.js +4 -3
- package/dist/protocols/minimax-images.d.ts +2 -2
- package/dist/protocols/open-responses.d.ts +1 -21
- package/dist/protocols/openai-chat.d.ts +1 -21
- package/dist/protocols/openai-chat.js +7 -9
- package/dist/protocols/openai-decisions.js +1 -1
- package/dist/protocols/openai-images.d.ts +2 -2
- package/dist/protocols/openai-responses.js +0 -1
- package/dist/protocols/openai-speech.d.ts +2 -2
- package/dist/protocols/openai-transcription.d.ts +2 -2
- package/dist/protocols/runway-video.d.ts +2 -2
- package/dist/protocols/shared.d.ts +1 -1
- package/dist/protocols/shared.js +2 -2
- package/dist/protocols/system-one.d.ts +4 -4
- package/dist/protocols/utils/responses-checkpoint.js +1 -1
- package/dist/protocols/utils/responses-compaction.d.ts +1 -1
- package/dist/protocols/utils/responses-compaction.js +4 -3
- package/dist/protocols/utils/tool-stream.d.ts +3 -63
- package/dist/protocols/xai-images.d.ts +2 -2
- package/dist/protocols/xai-video.d.ts +2 -2
- package/dist/protocols/zai-images.d.ts +2 -2
- package/dist/protocols/zai-messages.js +3 -3
- package/dist/provider-error.js +4 -4
- package/dist/providers/vercel-ai-gateway.js +4 -3
- package/dist/route/client.d.ts +43 -19
- package/dist/route/client.js +30 -18
- package/dist/route/decision.d.ts +6 -6
- package/dist/schema/errors.d.ts +1 -2
- package/dist/schema/errors.js +1 -2
- package/dist/schema/events.d.ts +61 -769
- package/dist/schema/events.js +3 -30
- package/dist/schema/messages.d.ts +20 -92
- package/dist/schema/messages.js +1 -52
- package/dist/speech-client.d.ts +5 -5
- package/dist/speech.d.ts +17 -19
- package/dist/speech.js +16 -12
- package/dist/testing.d.ts +8 -168
- package/dist/tool-runtime.js +12 -30
- package/dist/transcription-client.d.ts +5 -5
- package/dist/transcription.d.ts +17 -19
- package/dist/transcription.js +16 -12
- package/dist/video-client.d.ts +5 -5
- package/dist/video-client.js +1 -1
- package/dist/video.d.ts +18 -20
- package/dist/video.js +16 -12
- package/package.json +3 -3
package/dist/route/client.js
CHANGED
|
@@ -10,7 +10,7 @@ import { normalizeToolHistory } from "../tool-history.js";
|
|
|
10
10
|
import { sanitizeSurrogates } from "../utils/sanitize.js";
|
|
11
11
|
import * as ProviderShared from "../protocols/shared.js";
|
|
12
12
|
import { ToolSchemaProjection } from "../protocols/utils/tool-schema.js";
|
|
13
|
-
import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason, GenerationOptions, HttpOptions,
|
|
13
|
+
import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason, GenerationOptions, HttpOptions, LLMResponse, LanguageModel, Message, LLMEvent, InvalidProviderOutputError, ProviderConfigurationError, ProviderID, ToolChoice, ToolEntry, mergeGenerationOptions, mergeHttpOptions, mergeProviderOptions, withPartial, } from "../schema/index.js";
|
|
14
14
|
const makeRouteLanguageModel = (route, mapped) => {
|
|
15
15
|
const provider = route.provider ?? ("provider" in mapped ? mapped.provider : undefined);
|
|
16
16
|
if (!provider)
|
|
@@ -43,21 +43,22 @@ const mergeHeaders = (...items) => {
|
|
|
43
43
|
return undefined;
|
|
44
44
|
return Object.fromEntries(entries);
|
|
45
45
|
};
|
|
46
|
-
export function canCompact(
|
|
46
|
+
export function canCompact(target, options) {
|
|
47
|
+
const model = target instanceof LanguageModel ? target : target.model;
|
|
47
48
|
if (options?.mechanism === "trigger")
|
|
48
|
-
return
|
|
49
|
+
return model.route.compact?.trigger !== undefined;
|
|
49
50
|
if (options?.mechanism !== undefined && options.mechanism !== "endpoint")
|
|
50
51
|
return false;
|
|
51
|
-
return
|
|
52
|
+
return model.route.compact?.endpoint !== undefined;
|
|
52
53
|
}
|
|
53
|
-
const unsupportedCompaction = (
|
|
54
|
+
const unsupportedCompaction = (model, mechanism) => {
|
|
54
55
|
if (mechanism !== undefined && mechanism !== "endpoint" && mechanism !== "trigger")
|
|
55
56
|
return ProviderShared.invalidRequest(`Unknown compaction mechanism: ${mechanism}`);
|
|
56
57
|
return ProviderShared.unsupportedOperation({
|
|
57
58
|
operation: mechanism === "trigger" ? "compact.trigger" : "compact",
|
|
58
|
-
provider:
|
|
59
|
-
route:
|
|
60
|
-
message: `${
|
|
59
|
+
provider: model.provider,
|
|
60
|
+
route: model.route.id,
|
|
61
|
+
message: `${model.provider}/${model.route.id} does not support ${mechanism === "trigger" ? "trigger" : "explicit"} compaction`,
|
|
61
62
|
});
|
|
62
63
|
};
|
|
63
64
|
export class LLMClientService extends Context.Service()("@opencode/LLMClient") {
|
|
@@ -65,15 +66,16 @@ export class LLMClientService extends Context.Service()("@opencode/LLMClient") {
|
|
|
65
66
|
export const Service = LLMClientService;
|
|
66
67
|
const resolveRequestOptions = (request) => {
|
|
67
68
|
const messages = normalizeToolHistory(request.messages);
|
|
68
|
-
const normalized = messages === request.messages ? request :
|
|
69
|
+
const normalized = messages === request.messages ? request : { ...request, messages };
|
|
69
70
|
const routeDefaults = normalized.model.route.defaults;
|
|
70
71
|
const modelDefaults = normalized.model.defaults;
|
|
71
72
|
const generation = mergeGenerationOptions(routeDefaults.generation, modelDefaults?.generation, normalized.generation);
|
|
72
|
-
return
|
|
73
|
+
return {
|
|
74
|
+
...normalized,
|
|
73
75
|
generation: generation ?? new GenerationOptions({}),
|
|
74
76
|
providerOptions: mergeProviderOptions(routeDefaults.providerOptions, modelDefaults?.providerOptions, normalized.providerOptions),
|
|
75
77
|
http: mergeHttpOptions(routeDefaults.http, modelDefaults?.http, normalized.http),
|
|
76
|
-
}
|
|
78
|
+
};
|
|
77
79
|
};
|
|
78
80
|
const streamError = (route, message, cause) => {
|
|
79
81
|
const failed = cause.reasons.find(Cause.isFailReason)?.error;
|
|
@@ -94,7 +96,7 @@ const requireTerminalEvent = (route) => (events) => Stream.suspend(() => {
|
|
|
94
96
|
// Raw events carry provider frames that may trail the terminal event.
|
|
95
97
|
if (terminal && !LLMEvent.is.raw(event))
|
|
96
98
|
return Effect.fail(ProviderShared.eventError(route, `Provider emitted ${event.type} after the terminal event`));
|
|
97
|
-
if (LLMEvent.is.finish(event)
|
|
99
|
+
if (LLMEvent.is.finish(event))
|
|
98
100
|
terminal = true;
|
|
99
101
|
return Effect.succeed(event);
|
|
100
102
|
}), Stream.onEnd(Effect.suspend(() => (terminal ? Effect.void : Effect.fail(incompleteStreamError(route))))));
|
|
@@ -215,15 +217,24 @@ export function make(input) {
|
|
|
215
217
|
}
|
|
216
218
|
const prepareRequest = (request) => {
|
|
217
219
|
const original = resolveRequestOptions(request);
|
|
218
|
-
const sanitized =
|
|
220
|
+
const sanitized = restoreClasses({ ...sanitizeSurrogates({ ...original, model: undefined }), model: original.model });
|
|
219
221
|
// Deduplicate per sibling level; a tool and a namespace may share a name.
|
|
220
222
|
const dedupe = (tools) => [...new Map(tools.map((tool) => [`${tool.type}:${tool.name}`, tool])).values()].map((tool) => tool.type === "tool" ? tool : { ...tool, tools: dedupe(tool.tools) });
|
|
221
|
-
const resolved = applyCachePolicy(applyEffortUpdates(
|
|
223
|
+
const resolved = applyCachePolicy(applyEffortUpdates({ ...sanitized, tools: ToolSchemaProjection.tools(dedupe(sanitized.tools), sanitized.model) }));
|
|
222
224
|
const headers = resolved.model.route.headers?.({ request: resolved });
|
|
223
225
|
return headers === undefined
|
|
224
226
|
? resolved
|
|
225
|
-
:
|
|
227
|
+
: { ...resolved, http: mergeHttpOptions(new HttpOptions({ headers }), resolved.http) };
|
|
226
228
|
};
|
|
229
|
+
// Sanitizing copies changed class instances into plain records; rebuild them so protocols see the canonical classes.
|
|
230
|
+
const restoreClasses = (request) => ({
|
|
231
|
+
...request,
|
|
232
|
+
messages: request.messages.map(Message.make),
|
|
233
|
+
tools: request.tools.map(ToolEntry.make),
|
|
234
|
+
toolChoice: request.toolChoice === undefined ? undefined : ToolChoice.make(request.toolChoice),
|
|
235
|
+
generation: request.generation === undefined ? undefined : GenerationOptions.make(request.generation),
|
|
236
|
+
http: HttpOptions.make(request.http),
|
|
237
|
+
});
|
|
227
238
|
const compile = Effect.fn("LLM.compile")(function* (request, options) {
|
|
228
239
|
const references = request.messages.flatMap((message) => message.content.flatMap((part) => {
|
|
229
240
|
if (part.type === "media") {
|
|
@@ -281,14 +292,15 @@ export function generate(request, options) {
|
|
|
281
292
|
return yield* (yield* Service).generate(request, options);
|
|
282
293
|
});
|
|
283
294
|
}
|
|
284
|
-
|
|
295
|
+
/** Compact through the client for `options.mechanism`, failing typed when the request's route does not support it. */
|
|
296
|
+
export function compactRequest(request, options) {
|
|
285
297
|
return Effect.gen(function* () {
|
|
286
298
|
const client = yield* Service;
|
|
287
299
|
if (options?.mechanism === "trigger" && canCompact(request, options))
|
|
288
300
|
return yield* client.compact(request, options);
|
|
289
301
|
if ((options?.mechanism === undefined || options.mechanism === "endpoint") && canCompact(request))
|
|
290
302
|
return yield* client.compact(request, options);
|
|
291
|
-
return yield* unsupportedCompaction(request, options?.mechanism);
|
|
303
|
+
return yield* unsupportedCompaction(request.model, options?.mechanism);
|
|
292
304
|
});
|
|
293
305
|
}
|
|
294
306
|
export const layer = Layer.effect(Service, Effect.gen(function* () {
|
|
@@ -300,7 +312,7 @@ export const layer = Layer.effect(Service, Effect.gen(function* () {
|
|
|
300
312
|
return request.model.route.compact.trigger(prepareRequest(request), executor, options);
|
|
301
313
|
if ((options?.mechanism === undefined || options.mechanism === "endpoint") && canCompact(request))
|
|
302
314
|
return request.model.route.compact.endpoint(prepareRequest(request), executor, options);
|
|
303
|
-
return unsupportedCompaction(request, options?.mechanism);
|
|
315
|
+
return unsupportedCompaction(request.model, options?.mechanism);
|
|
304
316
|
});
|
|
305
317
|
}
|
|
306
318
|
return Service.of({
|
package/dist/route/decision.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type { HttpClientResponse } from "effect/http";
|
|
3
|
-
import { DecisionModel, DecisionModelCompatibility, type DecisionOptions, type
|
|
3
|
+
import { DecisionModel, DecisionModelCompatibility, type DecisionOptions, type DecisionRequest, type DecisionResponse } from "../decision.js";
|
|
4
4
|
import { HttpOptions, ProviderID, type AIError, type ProviderOptions } from "../schema/index.js";
|
|
5
5
|
import { Auth } from "./auth.js";
|
|
6
6
|
import { Endpoint } from "./endpoint.js";
|
|
@@ -12,11 +12,11 @@ export interface Protocol {
|
|
|
12
12
|
/** The host only takes inline data, so URL media is downloaded before `body.from`. */
|
|
13
13
|
readonly inlineMedia?: boolean;
|
|
14
14
|
readonly body: {
|
|
15
|
-
readonly from: (request:
|
|
15
|
+
readonly from: (request: DecisionRequest) => Effect.Effect<Record<string, unknown>, AIError>;
|
|
16
16
|
};
|
|
17
17
|
readonly response: {
|
|
18
18
|
readonly decode: (response: HttpClientResponse.HttpClientResponse, context: {
|
|
19
|
-
readonly request:
|
|
19
|
+
readonly request: DecisionRequest;
|
|
20
20
|
readonly providerMetadataKey: string;
|
|
21
21
|
}) => Effect.Effect<DecisionResponse, AIError>;
|
|
22
22
|
};
|
|
@@ -27,7 +27,7 @@ export interface Patch {
|
|
|
27
27
|
readonly provider?: string | ProviderID;
|
|
28
28
|
/** ProviderMetadata namespace for responses; defaults to the provider. */
|
|
29
29
|
readonly providerMetadataKey?: string;
|
|
30
|
-
readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body,
|
|
30
|
+
readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body, DecisionRequest>;
|
|
31
31
|
readonly auth?: Auth.Definition;
|
|
32
32
|
readonly headers?: Record<string, string>;
|
|
33
33
|
readonly http?: HttpOptions.Input;
|
|
@@ -37,7 +37,7 @@ export interface Route {
|
|
|
37
37
|
readonly provider: ProviderID;
|
|
38
38
|
readonly providerMetadataKey: string;
|
|
39
39
|
readonly protocol: Protocol;
|
|
40
|
-
readonly endpoint: Endpoint.Definition<MediaProtocol.Body,
|
|
40
|
+
readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
|
|
41
41
|
readonly auth: Auth.Definition;
|
|
42
42
|
readonly headers?: Record<string, string>;
|
|
43
43
|
readonly http?: HttpOptions;
|
|
@@ -52,7 +52,7 @@ export interface MakeInput extends Omit<Patch, "endpoint"> {
|
|
|
52
52
|
readonly id: string;
|
|
53
53
|
readonly provider: string | ProviderID;
|
|
54
54
|
readonly protocol: Protocol;
|
|
55
|
-
readonly endpoint: Endpoint.Definition<MediaProtocol.Body,
|
|
55
|
+
readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
|
|
56
56
|
}
|
|
57
57
|
export declare const make: (input: MakeInput) => Route;
|
|
58
58
|
export * as DecisionRoute from "./decision.js";
|
package/dist/schema/errors.d.ts
CHANGED
|
@@ -165,8 +165,7 @@ export declare const withPartial: (error: AIError, partial: LLMResponse | undefi
|
|
|
165
165
|
/**
|
|
166
166
|
* Failure type for tool execute handlers. Handlers must map their internal
|
|
167
167
|
* errors to this shape; the runtime catches `ToolFailure`s and surfaces them
|
|
168
|
-
* as
|
|
169
|
-
* can self-correct.
|
|
168
|
+
* as a `tool-result` of `type: "error"` so the model can self-correct.
|
|
170
169
|
*
|
|
171
170
|
* Anything thrown or yielded by a handler that is not a `ToolFailure` is
|
|
172
171
|
* treated as a defect and fails the stream.
|
package/dist/schema/errors.js
CHANGED
|
@@ -132,8 +132,7 @@ export const withPartial = (error, partial) => partial === undefined ? error : n
|
|
|
132
132
|
/**
|
|
133
133
|
* Failure type for tool execute handlers. Handlers must map their internal
|
|
134
134
|
* errors to this shape; the runtime catches `ToolFailure`s and surfaces them
|
|
135
|
-
* as
|
|
136
|
-
* can self-correct.
|
|
135
|
+
* as a `tool-result` of `type: "error"` so the model can self-correct.
|
|
137
136
|
*
|
|
138
137
|
* Anything thrown or yielded by a handler that is not a `ToolFailure` is
|
|
139
138
|
* treated as a defect and fails the stream.
|