@opencode/ai 0.0.0-dev-414 → 0.0.0-dev-416
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/dist/protocols/gemini.js
CHANGED
|
@@ -442,6 +442,12 @@ const mapContinuedUsage = (requests, metadataKey) => {
|
|
|
442
442
|
requests: requests.length,
|
|
443
443
|
});
|
|
444
444
|
};
|
|
445
|
+
const terminalFinishReason = (finishReason) => finishReason && finishReason !== "FINISH_REASON_UNSPECIFIED" ? finishReason : undefined;
|
|
446
|
+
const RECOVERABLE_FINISH_REASONS = new Set([
|
|
447
|
+
"MALFORMED_RESPONSE",
|
|
448
|
+
"MALFORMED_FUNCTION_CALL",
|
|
449
|
+
"UNEXPECTED_TOOL_CALL",
|
|
450
|
+
]);
|
|
445
451
|
const mapFinishReason = (finishReason, hasToolCalls) => {
|
|
446
452
|
if (finishReason === undefined)
|
|
447
453
|
return hasToolCalls ? "tool-calls" : "unknown";
|
|
@@ -515,24 +521,22 @@ const step = (state, event) => {
|
|
|
515
521
|
}));
|
|
516
522
|
}
|
|
517
523
|
const candidate = event.candidates?.[0];
|
|
524
|
+
const finishReason = terminalFinishReason(candidate?.finishReason);
|
|
518
525
|
const nextState = {
|
|
519
526
|
...state,
|
|
520
527
|
promptFeedback: event.promptFeedback ?? state.promptFeedback,
|
|
521
528
|
usage: event.usageMetadata ?? state.usage,
|
|
522
|
-
finishReason:
|
|
529
|
+
finishReason: finishReason ?? state.finishReason,
|
|
523
530
|
};
|
|
524
|
-
|
|
525
|
-
if (candidate?.finishReason === "MALFORMED_RESPONSE")
|
|
531
|
+
if (finishReason && mapFinishReason(finishReason, state.hasToolCalls) === "error")
|
|
526
532
|
return Effect.fail(new AIError({
|
|
527
533
|
reason: new InvalidProviderOutputError({
|
|
528
534
|
route: state.route,
|
|
529
|
-
message: `Gemini stopped with ${
|
|
535
|
+
message: `Gemini stopped with ${finishReason}`,
|
|
530
536
|
body: ProviderShared.encodeJson(event),
|
|
531
|
-
classification: "incomplete-stream",
|
|
537
|
+
classification: RECOVERABLE_FINISH_REASONS.has(finishReason) ? "incomplete-stream" : undefined,
|
|
532
538
|
}),
|
|
533
539
|
}));
|
|
534
|
-
if (candidate?.finishReason && mapFinishReason(candidate.finishReason, state.hasToolCalls) === "error")
|
|
535
|
-
return Effect.fail(ProviderShared.eventError(state.route, `Gemini stopped with ${candidate.finishReason}`, ProviderShared.encodeJson(event)));
|
|
536
540
|
if (!candidate?.content)
|
|
537
541
|
return Effect.succeed([nextState, []]);
|
|
538
542
|
const events = [];
|
|
@@ -692,8 +696,9 @@ export const transport = {
|
|
|
692
696
|
if (typeof event === "object" && event !== null)
|
|
693
697
|
raw.set(event, frame);
|
|
694
698
|
const candidate = Option.getOrUndefined(decodeCheckpoint(event))?.candidates?.[0];
|
|
695
|
-
|
|
696
|
-
|
|
699
|
+
const candidateFinishReason = terminalFinishReason(candidate?.finishReason);
|
|
700
|
+
token = candidate?.continuationToken ?? (candidateFinishReason ? undefined : token);
|
|
701
|
+
finishReason = candidateFinishReason ?? finishReason;
|
|
697
702
|
return event;
|
|
698
703
|
}))), Stream.catchIf(
|
|
699
704
|
// A connection failure may resume; a timeout keeps its meaning and ends the response.
|
|
@@ -35,7 +35,6 @@ interface ParserState {
|
|
|
35
35
|
readonly lifecycle: Lifecycle.State;
|
|
36
36
|
readonly steps: Partial<Record<number, OutputStep>>;
|
|
37
37
|
readonly tools: ToolStream.State<number>;
|
|
38
|
-
readonly completed: boolean;
|
|
39
38
|
}
|
|
40
39
|
export declare const protocol: Protocol<{
|
|
41
40
|
readonly model: string;
|
|
@@ -170,13 +169,18 @@ export declare const protocol: Protocol<{
|
|
|
170
169
|
readonly message?: string | undefined;
|
|
171
170
|
}[] | undefined;
|
|
172
171
|
};
|
|
173
|
-
} | {
|
|
174
|
-
readonly event_type: "error";
|
|
175
|
-
readonly error: unknown;
|
|
176
172
|
} | {
|
|
177
173
|
readonly event_type: "interaction.status_update";
|
|
178
174
|
readonly interaction_id?: string | undefined;
|
|
179
175
|
readonly status: string;
|
|
176
|
+
} | {
|
|
177
|
+
readonly event_type: "error";
|
|
178
|
+
readonly error?: {
|
|
179
|
+
readonly [x: string]: unknown;
|
|
180
|
+
readonly code?: string | undefined;
|
|
181
|
+
readonly error_type?: string | undefined;
|
|
182
|
+
readonly message?: string | undefined;
|
|
183
|
+
} | undefined;
|
|
180
184
|
}, ParserState>;
|
|
181
185
|
export declare const route: Route<{
|
|
182
186
|
readonly model: string;
|
|
@@ -4,7 +4,7 @@ import { Auth } from "../route/auth.js";
|
|
|
4
4
|
import { Endpoint } from "../route/endpoint.js";
|
|
5
5
|
import { Framing } from "../route/framing.js";
|
|
6
6
|
import { Protocol } from "../route/protocol.js";
|
|
7
|
-
import { AIError, LLMEvent, ProviderID, Usage, } from "../schema/index.js";
|
|
7
|
+
import { AIError, ContentPolicyError, InvalidProviderOutputError, LLMEvent, ProviderID, Usage, } from "../schema/index.js";
|
|
8
8
|
import { Media } from "../media.js";
|
|
9
9
|
import { classifyProviderFailure, providerErrorMessage } from "../provider-error.js";
|
|
10
10
|
import { encodeJson } from "../utils/json.js";
|
|
@@ -147,6 +147,11 @@ const UnknownEvent = Schema.Struct({
|
|
|
147
147
|
decode: SchemaGetter.transform(() => ({ event_type: "unknown" })),
|
|
148
148
|
encode: SchemaGetter.forbidden(() => "Unknown Google Interactions stream events are decode-only"),
|
|
149
149
|
}));
|
|
150
|
+
const ErrorDetail = Schema.StructWithRest(Schema.Struct({
|
|
151
|
+
code: lenient(Schema.String),
|
|
152
|
+
error_type: lenient(Schema.String),
|
|
153
|
+
message: lenient(Schema.String),
|
|
154
|
+
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
150
155
|
const Event = Schema.Union([
|
|
151
156
|
Schema.Struct({ event_type: Schema.Literal("step.start"), index: Schema.Number, step: OutputStep }),
|
|
152
157
|
Schema.Struct({ event_type: Schema.Literal("step.delta"), index: Schema.Number, delta: Delta }),
|
|
@@ -161,7 +166,7 @@ const Event = Schema.Union([
|
|
|
161
166
|
status: Schema.String,
|
|
162
167
|
}),
|
|
163
168
|
Schema.Struct({ event_type: Schema.Literal("interaction.completed"), interaction: Interaction }),
|
|
164
|
-
Schema.Struct({ event_type: Schema.Literal("error"), error:
|
|
169
|
+
Schema.Struct({ event_type: Schema.Literal("error"), error: lenient(ErrorDetail) }),
|
|
165
170
|
UnknownEvent,
|
|
166
171
|
]);
|
|
167
172
|
// =============================================================================
|
|
@@ -427,6 +432,23 @@ const onStop = Effect.fnUntraced(function* (state, index) {
|
|
|
427
432
|
const result = yield* ToolStream.finish(ADAPTER, state.tools, index);
|
|
428
433
|
return [{ ...state, tools: result.tools }, result.events ?? []];
|
|
429
434
|
});
|
|
435
|
+
const RECOVERABLE_CODES = new Set([
|
|
436
|
+
"MALFORMED_FUNCTION_CALL",
|
|
437
|
+
"MALFORMED_TOOL_CALL",
|
|
438
|
+
"UNEXPECTED_TOOL_CALL",
|
|
439
|
+
"MALFORMED_RESPONSE",
|
|
440
|
+
]);
|
|
441
|
+
const INVALID_OUTPUT_CODES = new Set(["MISSING_THOUGHT_SIGNATURE", "TOO_MANY_TOOL_CALLS", "NO_IMAGE"]);
|
|
442
|
+
const failureReason = (route, code, message, data, rawBody) => {
|
|
443
|
+
const normalized = code?.trim().toUpperCase().replaceAll("-", "_");
|
|
444
|
+
if (normalized && RECOVERABLE_CODES.has(normalized))
|
|
445
|
+
return new InvalidProviderOutputError({ route, message, body: rawBody, classification: "incomplete-stream" });
|
|
446
|
+
if (normalized && INVALID_OUTPUT_CODES.has(normalized))
|
|
447
|
+
return new InvalidProviderOutputError({ route, message, body: rawBody });
|
|
448
|
+
if (normalized && (normalized === "CONTENT_BLOCKED" || GeminiGenerateContent.contentFiltered(normalized)))
|
|
449
|
+
return new ContentPolicyError({ message, body: rawBody });
|
|
450
|
+
return classifyProviderFailure({ message, data, rawBody });
|
|
451
|
+
};
|
|
430
452
|
const step = Effect.fnUntraced(function* (state, event) {
|
|
431
453
|
switch (event.event_type) {
|
|
432
454
|
case "step.start":
|
|
@@ -439,34 +461,31 @@ const step = Effect.fnUntraced(function* (state, event) {
|
|
|
439
461
|
case "interaction.status_update":
|
|
440
462
|
case "unknown":
|
|
441
463
|
return [state, []];
|
|
442
|
-
case "error":
|
|
464
|
+
case "error": {
|
|
465
|
+
const rawBody = encodeJson(event);
|
|
443
466
|
return yield* new AIError({
|
|
444
|
-
reason:
|
|
445
|
-
message: providerErrorMessage(encodeJson(event)) ?? "Google Interactions stream error",
|
|
446
|
-
data: event.error,
|
|
447
|
-
rawBody: encodeJson(event),
|
|
448
|
-
}),
|
|
467
|
+
reason: failureReason(state.route, event.error?.code ?? event.error?.error_type, event.error?.message?.trim() || providerErrorMessage(rawBody) || "Google Interactions stream error", event.error, rawBody),
|
|
449
468
|
});
|
|
469
|
+
}
|
|
450
470
|
case "interaction.completed": {
|
|
451
471
|
const interaction = event.interaction;
|
|
452
472
|
if (interaction.status === "failed" || interaction.status === "cancelled") {
|
|
453
473
|
const messages = (interaction.errors ?? []).flatMap((error) => (error.message?.trim() ? [error.message] : []));
|
|
474
|
+
const coded = interaction.errors?.find((error) => error.code);
|
|
454
475
|
return yield* new AIError({
|
|
455
|
-
reason:
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
data: interaction.errors?.find((error) => error.code) ?? interaction,
|
|
459
|
-
rawBody: encodeJson(event),
|
|
460
|
-
}),
|
|
476
|
+
reason: failureReason(state.route, coded?.code, messages.length > 0 ? messages.join("\n") : `Google Interactions ${interaction.status}`,
|
|
477
|
+
// The first coded error decides the classification; every message stays visible.
|
|
478
|
+
coded ?? interaction, encodeJson(event)),
|
|
461
479
|
});
|
|
462
480
|
}
|
|
463
481
|
if (!["completed", "requires_action", "incomplete"].includes(interaction.status))
|
|
464
482
|
return yield* ProviderShared.eventError(ADAPTER, `Unexpected terminal Interactions status: ${interaction.status}`, encodeJson(event));
|
|
465
483
|
const pending = yield* ToolStream.finishAll(ADAPTER, state.tools);
|
|
466
484
|
const events = [...pending.events];
|
|
485
|
+
const hasToolCalls = Object.values(state.steps).some((item) => item?.type === "function_call");
|
|
467
486
|
const lifecycle = Lifecycle.finish(state.lifecycle, events, {
|
|
468
487
|
reason: {
|
|
469
|
-
normalized: interaction.status === "requires_action"
|
|
488
|
+
normalized: interaction.status === "requires_action" || (interaction.status === "completed" && hasToolCalls)
|
|
470
489
|
? "tool-calls"
|
|
471
490
|
: interaction.status === "incomplete"
|
|
472
491
|
? "length"
|
|
@@ -476,7 +495,7 @@ const step = Effect.fnUntraced(function* (state, event) {
|
|
|
476
495
|
usage: mapUsage(interaction.usage, state.metadataKey),
|
|
477
496
|
providerMetadata: { [state.metadataKey]: { interactionId: interaction.id } },
|
|
478
497
|
});
|
|
479
|
-
return [{ ...state, lifecycle, tools: pending.tools
|
|
498
|
+
return [{ ...state, lifecycle, tools: pending.tools }, events];
|
|
480
499
|
}
|
|
481
500
|
}
|
|
482
501
|
});
|
|
@@ -490,18 +509,14 @@ export const protocol = Protocol.make({
|
|
|
490
509
|
stream: {
|
|
491
510
|
event: Protocol.jsonEvent(Event),
|
|
492
511
|
initial: (request) => ({
|
|
493
|
-
route: `${request.model.provider}/${
|
|
512
|
+
route: `${request.model.provider}/${request.model.route.id}`,
|
|
494
513
|
metadataKey: request.model.route.providerMetadataKey ?? request.model.provider,
|
|
495
514
|
lifecycle: Lifecycle.initial(),
|
|
496
515
|
steps: {},
|
|
497
516
|
tools: ToolStream.empty(),
|
|
498
|
-
completed: false,
|
|
499
517
|
}),
|
|
500
518
|
step,
|
|
501
519
|
terminal: (event) => event.event_type === "interaction.completed",
|
|
502
|
-
onHalt: (state) => state.completed
|
|
503
|
-
? Effect.succeed([])
|
|
504
|
-
: Effect.fail(ProviderShared.eventError(ADAPTER, "Google Interactions stream ended before interaction.completed")),
|
|
505
520
|
},
|
|
506
521
|
});
|
|
507
522
|
export const route = Route.make({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-416",
|
|
4
4
|
"name": "@opencode/ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -520,7 +520,7 @@
|
|
|
520
520
|
"devDependencies": {
|
|
521
521
|
"@clack/prompts": "1.0.0-alpha.1",
|
|
522
522
|
"@effect/platform-node": "4.0.6",
|
|
523
|
-
"@opencode/http-recorder": "0.0.0-dev-
|
|
523
|
+
"@opencode/http-recorder": "0.0.0-dev-416",
|
|
524
524
|
"@tsconfig/bun": "1.0.9",
|
|
525
525
|
"@types/bun": "1.4.3",
|
|
526
526
|
"@typescript/native": "npm:typescript@7.1.0-beta",
|
|
@@ -530,7 +530,7 @@
|
|
|
530
530
|
"@aws-sdk/credential-providers": "3.1057.0",
|
|
531
531
|
"@smithy/eventstream-codec": "4.2.14",
|
|
532
532
|
"@smithy/util-utf8": "4.2.2",
|
|
533
|
-
"@opencode/schema": "0.0.0-dev-
|
|
533
|
+
"@opencode/schema": "0.0.0-dev-416",
|
|
534
534
|
"aws4fetch": "1.0.20",
|
|
535
535
|
"effect": "4.0.6",
|
|
536
536
|
"google-auth-library": "10.5.0"
|