@opencode/ai 0.0.0-dev-21098 → 0.0.0-dev-21100

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.
@@ -9,7 +9,7 @@ import { Protocol } from "../route/protocol.js";
9
9
  import { Headers } from "effect/http";
10
10
  import { HttpTransport } from "../route/transport/index.js";
11
11
  import { AIError, HttpOptions, LLMRequest, LLMEvent, mergeJsonRecords, Usage, } from "../schema/index.js";
12
- import { JsonObject, knownString, optionalArray, optionalNull, ProviderShared } from "./shared.js";
12
+ import { JsonObject, knownString, optionalArray, optionalNull, ProviderShared, withUntypedFrames } from "./shared.js";
13
13
  import { classifyProviderFailure } from "../provider-error.js";
14
14
  import { effortUpdate, resolveEffortUpdates } from "../effort-updates.js";
15
15
  import * as Cache from "./utils/cache.js";
@@ -362,7 +362,7 @@ const AnthropicStreamDelta = Schema.StructWithRest(Schema.Struct({
362
362
  stop_details: optionalNull(Schema.StructWithRest(Schema.Struct({ category: optionalNull(Schema.String), explanation: optionalNull(Schema.String) }), [JsonObject])),
363
363
  }), [JsonObject]);
364
364
  const decodeAnthropicStreamDelta = Schema.decodeUnknownOption(AnthropicStreamDelta);
365
- const AnthropicEvent = Schema.StructWithRest(Schema.Struct({
365
+ const TypedAnthropicEvent = Schema.StructWithRest(Schema.Struct({
366
366
  type: Schema.String,
367
367
  index: Schema.optional(Schema.Number),
368
368
  message: Schema.optional(Schema.StructWithRest(Schema.Struct({ usage: Schema.optional(AnthropicUsage) }), [JsonObject])),
@@ -375,6 +375,7 @@ const AnthropicEvent = Schema.StructWithRest(Schema.Struct({
375
375
  // the parser can fall back to whichever field is populated.
376
376
  error: Schema.optional(Schema.StructWithRest(Schema.Struct({ type: Schema.optional(Schema.String), message: Schema.optional(Schema.String) }), [JsonObject])),
377
377
  }), [JsonObject]);
378
+ const AnthropicEvent = withUntypedFrames(TypedAnthropicEvent);
378
379
  const invalid = ProviderShared.invalidRequest;
379
380
  // =============================================================================
380
381
  // Request Lowering
@@ -61,6 +61,8 @@ export declare const protocol: Protocol<{
61
61
  readonly frequency_penalty?: number | undefined;
62
62
  readonly presence_penalty?: number | undefined;
63
63
  }, string, {
64
+ readonly type: "unknown";
65
+ } | {
64
66
  readonly type: "message-start";
65
67
  } | {
66
68
  readonly type: "content-start" | "content-delta";
@@ -1,4 +1,4 @@
1
- import { Effect, Schema } from "effect";
1
+ import { Effect, Schema, SchemaGetter } from "effect";
2
2
  import { Route } from "../route/client.js";
3
3
  import { Endpoint } from "../route/endpoint.js";
4
4
  import { Framing } from "../route/framing.js";
@@ -65,6 +65,26 @@ const NativeUsage = Schema.Struct({
65
65
  billed_units: Schema.optional(TokenCounts),
66
66
  cached_tokens: Schema.optional(Schema.Number),
67
67
  });
68
+ const KNOWN_EVENT_TYPES = new Set([
69
+ "message-start",
70
+ "content-start",
71
+ "content-delta",
72
+ "content-end",
73
+ "tool-plan-delta",
74
+ "tool-call-start",
75
+ "tool-call-delta",
76
+ "tool-call-end",
77
+ "message-end",
78
+ "citation-start",
79
+ "citation-end",
80
+ ]);
81
+ // Cohere may add stream event types. Unknown types are ignored, while a malformed known type still fails decoding.
82
+ const UnknownEvent = Schema.Struct({
83
+ type: Schema.String.check(Schema.makeFilter((type) => !KNOWN_EVENT_TYPES.has(type))),
84
+ }).pipe(Schema.decodeTo(Schema.Struct({ type: Schema.Literal("unknown") }), {
85
+ decode: SchemaGetter.transform(() => ({ type: "unknown" })),
86
+ encode: SchemaGetter.forbidden(() => "Unknown Cohere stream events are decode-only"),
87
+ }));
68
88
  const Event = Schema.Union([
69
89
  Schema.Struct({ type: Schema.Literal("message-start") }),
70
90
  Schema.Struct({
@@ -100,6 +120,7 @@ const Event = Schema.Union([
100
120
  }),
101
121
  // Citation output is outside this basic chat surface.
102
122
  Schema.Struct({ type: Schema.Literals(["citation-start", "citation-end"]) }),
123
+ UnknownEvent,
103
124
  ]);
104
125
  const TOOL_CHOICE = { auto: undefined, none: "NONE", required: "REQUIRED", tool: "REQUIRED" };
105
126
  const fromRequest = Effect.fn("CohereChat.fromRequest")(function* (request) {
@@ -99,6 +99,8 @@ export declare const protocol: Protocol<{
99
99
  readonly previous_interaction_id?: string | undefined;
100
100
  readonly system_instruction?: string | undefined;
101
101
  }, string, {
102
+ readonly event_type: "unknown";
103
+ } | {
102
104
  readonly event_type: "step.start";
103
105
  readonly index: number;
104
106
  readonly step: {
@@ -1,4 +1,4 @@
1
- import { Effect, Schema } from "effect";
1
+ import { Effect, Schema, SchemaGetter } from "effect";
2
2
  import { Route } from "../route/client.js";
3
3
  import { Auth } from "../route/auth.js";
4
4
  import { Endpoint } from "../route/endpoint.js";
@@ -121,6 +121,22 @@ const Interaction = Schema.StructWithRest(Schema.Struct({
121
121
  status: Schema.String,
122
122
  usage: Schema.optional(RawUsage),
123
123
  }), [Schema.Record(Schema.String, Schema.Unknown)]);
124
+ const KNOWN_EVENT_TYPES = new Set([
125
+ "step.start",
126
+ "step.delta",
127
+ "step.stop",
128
+ "interaction.created",
129
+ "interaction.status_update",
130
+ "interaction.completed",
131
+ "error",
132
+ ]);
133
+ // Google may add stream event types. Unknown types are ignored, while a malformed known type still fails decoding.
134
+ const UnknownEvent = Schema.Struct({
135
+ event_type: Schema.String.check(Schema.makeFilter((type) => !KNOWN_EVENT_TYPES.has(type))),
136
+ }).pipe(Schema.decodeTo(Schema.Struct({ event_type: Schema.Literal("unknown") }), {
137
+ decode: SchemaGetter.transform(() => ({ event_type: "unknown" })),
138
+ encode: SchemaGetter.forbidden(() => "Unknown Google Interactions stream events are decode-only"),
139
+ }));
124
140
  const Event = Schema.Union([
125
141
  Schema.Struct({ event_type: Schema.Literal("step.start"), index: Schema.Number, step: OutputStep }),
126
142
  Schema.Struct({ event_type: Schema.Literal("step.delta"), index: Schema.Number, delta: Delta }),
@@ -136,6 +152,7 @@ const Event = Schema.Union([
136
152
  }),
137
153
  Schema.Struct({ event_type: Schema.Literal("interaction.completed"), interaction: Interaction }),
138
154
  Schema.Struct({ event_type: Schema.Literal("error"), error: Schema.Unknown }),
155
+ UnknownEvent,
139
156
  ]);
140
157
  // =============================================================================
141
158
  // Request Body Construction
@@ -403,6 +420,7 @@ const step = Effect.fnUntraced(function* (state, event) {
403
420
  return yield* onStop(state, event.index);
404
421
  case "interaction.created":
405
422
  case "interaction.status_update":
423
+ case "unknown":
406
424
  return [state, []];
407
425
  case "error":
408
426
  return yield* new AIError({
@@ -586,7 +586,7 @@ export type OutputItem = StreamItem & {
586
586
  readonly id: string;
587
587
  readonly namespace?: string;
588
588
  };
589
- export declare const Event: Schema.StructWithRest<Schema.Struct<{
589
+ export declare const Event: Schema.Union<readonly [Schema.StructWithRest<Schema.Struct<{
590
590
  readonly type: Schema.String;
591
591
  readonly delta: Schema.optional<Schema.String>;
592
592
  readonly arguments: Schema.optional<Schema.String>;
@@ -637,17 +637,64 @@ export declare const Event: Schema.StructWithRest<Schema.Struct<{
637
637
  readonly error: Schema.optional<Schema.Unknown>;
638
638
  readonly status: Schema.optional<Schema.Unknown>;
639
639
  readonly status_code: Schema.optional<Schema.Unknown>;
640
- }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>;
640
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>, Schema.decodeTo<Schema.StructWithRest<Schema.Struct<{
641
+ readonly type: Schema.String;
642
+ readonly delta: Schema.optional<Schema.String>;
643
+ readonly arguments: Schema.optional<Schema.String>;
644
+ readonly text: Schema.optional<Schema.String>;
645
+ readonly item_id: Schema.optional<Schema.String>;
646
+ readonly output_index: Schema.optional<Schema.Number>;
647
+ readonly summary_index: Schema.optional<Schema.Number>;
648
+ readonly item: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
649
+ readonly type: Schema.String;
650
+ readonly id: Schema.optional<Schema.String>;
651
+ readonly call_id: Schema.optional<Schema.String>;
652
+ readonly name: Schema.optional<Schema.String>;
653
+ readonly namespace: Schema.optional<Schema.NullOr<Schema.String>>;
654
+ readonly arguments: Schema.optional<Schema.String>;
655
+ readonly encrypted_content: Schema.optional<Schema.NullOr<Schema.String>>;
656
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
657
+ readonly response: Schema.optional<Schema.StructWithRest<Schema.Struct<{
658
+ readonly id: Schema.optional<Schema.String>;
659
+ readonly service_tier: Schema.optional<Schema.NullOr<Schema.String>>;
660
+ readonly incomplete_details: Schema.optional<Schema.NullOr<Schema.Struct<{
661
+ readonly reason: Schema.optional<Schema.String>;
662
+ }>>>;
663
+ readonly output: Schema.optional<Schema.$Array<Schema.StructWithRest<Schema.Struct<{
664
+ readonly type: Schema.String;
665
+ readonly id: Schema.optional<Schema.String>;
666
+ readonly call_id: Schema.optional<Schema.String>;
667
+ readonly name: Schema.optional<Schema.String>;
668
+ readonly namespace: Schema.optional<Schema.NullOr<Schema.String>>;
669
+ readonly arguments: Schema.optional<Schema.String>;
670
+ readonly encrypted_content: Schema.optional<Schema.NullOr<Schema.String>>;
671
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
672
+ readonly usage: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
673
+ readonly input_tokens: Schema.optional<Schema.Number>;
674
+ readonly input_tokens_details: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
675
+ readonly cached_tokens: Schema.optional<Schema.Number>;
676
+ readonly cache_write_tokens: Schema.optional<Schema.Number>;
677
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
678
+ readonly output_tokens: Schema.optional<Schema.Number>;
679
+ readonly output_tokens_details: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
680
+ readonly reasoning_tokens: Schema.optional<Schema.Number>;
681
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
682
+ readonly total_tokens: Schema.optional<Schema.Number>;
683
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
684
+ readonly error: Schema.optional<Schema.Unknown>;
685
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>;
686
+ readonly code: Schema.optional<Schema.Unknown>;
687
+ readonly message: Schema.optional<Schema.Unknown>;
688
+ readonly error: Schema.optional<Schema.Unknown>;
689
+ readonly status: Schema.optional<Schema.Unknown>;
690
+ readonly status_code: Schema.optional<Schema.Unknown>;
691
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>, Schema.$Record<Schema.String, Schema.Unknown>, never, never>]>;
641
692
  export type Event = Schema.Schema.Type<typeof Event>;
642
693
  export type NormalizedEvent = Event & {
643
694
  readonly item?: OutputItem | null;
644
695
  };
645
- /**
646
- * Decodes one WebSocket frame. Some providers and gateways answer a rejected `response.create` with a bare
647
- * `{ "error": ... }` envelope and no event type; that reads as an error event so it classifies instead of
648
- * failing decoding.
649
- */
650
- export declare const decodeChannelEvent: (frame: string) => Effect.Effect<{
696
+ /** Decodes one WebSocket frame. */
697
+ export declare const decodeChannelEvent: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => Effect.Effect<{
651
698
  readonly [x: string]: unknown;
652
699
  readonly type: string;
653
700
  readonly status?: unknown;
@@ -2,7 +2,7 @@ import { Effect, Option, Schema } from "effect";
2
2
  import { HttpTransport } from "../route/transport/index.js";
3
3
  import { Protocol } from "../route/protocol.js";
4
4
  import { AIError, LLMEvent, ProviderInternalError, Usage, } from "../schema/index.js";
5
- import { JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared.js";
5
+ import { JsonObject, optionalArray, optionalNull, ProviderShared, withUntypedFrames } from "./shared.js";
6
6
  import { classifyProviderFailure } from "../provider-error.js";
7
7
  import { effortUpdate } from "../effort-updates.js";
8
8
  import { OpenResponsesOptions } from "./utils/open-responses-options.js";
@@ -251,7 +251,7 @@ export const StreamItem = Schema.StructWithRest(Schema.Struct({
251
251
  // only fail on invalid JSON and otherwise always classifies with the raw body as the fallback.
252
252
  // https://www.openresponses.org/specification
253
253
  const asText = (value) => typeof value === "string" && value.length > 0 ? value : typeof value === "number" ? String(value) : undefined;
254
- export const Event = Schema.StructWithRest(Schema.Struct({
254
+ const TypedEvent = Schema.StructWithRest(Schema.Struct({
255
255
  type: Schema.String,
256
256
  delta: Schema.optional(Schema.String),
257
257
  arguments: Schema.optional(Schema.String),
@@ -275,16 +275,9 @@ export const Event = Schema.StructWithRest(Schema.Struct({
275
275
  status: Schema.optional(Schema.Unknown),
276
276
  status_code: Schema.optional(Schema.Unknown),
277
277
  }), [Schema.Record(Schema.String, Schema.Unknown)]);
278
- const decodeEventValue = Schema.decodeUnknownEffect(Event);
279
- const decodeFrame = Schema.decodeUnknownEffect(ProviderShared.Json);
280
- /**
281
- * Decodes one WebSocket frame. Some providers and gateways answer a rejected `response.create` with a bare
282
- * `{ "error": ... }` envelope and no event type; that reads as an error event so it classifies instead of
283
- * failing decoding.
284
- */
285
- export const decodeChannelEvent = (frame) => decodeFrame(frame).pipe(Effect.flatMap((value) => decodeEventValue(ProviderShared.isRecord(value) && value.type === undefined && value.error != null
286
- ? { ...value, type: "error" }
287
- : value)));
278
+ export const Event = withUntypedFrames(TypedEvent);
279
+ /** Decodes one WebSocket frame. */
280
+ export const decodeChannelEvent = Schema.decodeUnknownEffect(Protocol.jsonEvent(Event));
288
281
  const BASE_ADAPTER = { id: ADAPTER, name: NAME };
289
282
  // =============================================================================
290
283
  // Request Lowering
@@ -11,6 +11,15 @@ export declare const optionalArray: <const S extends Schema.Top>(schema: S) => S
11
11
  export declare const optionalNull: <const S extends Schema.Top>(schema: S) => Schema.optional<Schema.NullOr<S>>;
12
12
  /** Optional field whose malformed value decodes to `undefined` instead of failing the enclosing struct. */
13
13
  export declare const lenient: <const S extends Schema.Top>(schema: S) => Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<S>, S["DecodingServices"]>>;
14
+ /**
15
+ * Accepts stream frames that carry no `type`, alongside the typed event schema. Gateways inject such frames as
16
+ * keep-alives, so they decode as an ignored event; one with an `error` still decodes as an error event.
17
+ */
18
+ export declare const withUntypedFrames: <const S extends Schema.Codec<{
19
+ readonly type: string;
20
+ }, {
21
+ readonly type: string;
22
+ }>>(event: S) => Schema.Union<readonly [S, Schema.decodeTo<S, Schema.$Record<Schema.String, Schema.Unknown>, never, never>]>;
14
23
  /** Provider-defined string enum: known values for autocomplete, any string accepted at runtime. */
15
24
  export declare const knownString: <Known extends string>() => Schema.declare<OpenString<Known>, OpenString<Known>>;
16
25
  export declare const OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH = 64;
@@ -1,5 +1,5 @@
1
1
  import { Tool } from "@opencode/schema/tool";
2
- import { Effect, Option, Schema } from "effect";
2
+ import { Effect, Option, Schema, SchemaGetter } from "effect";
3
3
  import { Headers, HttpClientRequest } from "effect/http";
4
4
  import { Media } from "../media.js";
5
5
  import { InvalidProviderOutputError, InvalidRequestError, UnsupportedOperationError, AIError, LLMRequest, Message, ToolDefinition, } from "../schema/index.js";
@@ -12,6 +12,17 @@ export const optionalArray = (schema) => Schema.optional(Schema.Array(schema));
12
12
  export const optionalNull = (schema) => Schema.optional(Schema.NullOr(schema));
13
13
  /** Optional field whose malformed value decodes to `undefined` instead of failing the enclosing struct. */
14
14
  export const lenient = (schema) => Schema.optionalKey(Schema.UndefinedOr(schema).pipe(Schema.catchDecoding(() => Effect.succeed(Option.some(undefined)))));
15
+ /**
16
+ * Accepts stream frames that carry no `type`, alongside the typed event schema. Gateways inject such frames as
17
+ * keep-alives, so they decode as an ignored event; one with an `error` still decodes as an error event.
18
+ */
19
+ export const withUntypedFrames = (event) => Schema.Union([
20
+ event,
21
+ JsonObject.check(Schema.makeFilter((frame) => !("type" in frame))).pipe(Schema.decodeTo(event, {
22
+ decode: SchemaGetter.transform((frame) => ({ ...frame, type: frame.error != null ? "error" : "ignored" })),
23
+ encode: SchemaGetter.forbidden(() => "Untyped stream frames are decode-only"),
24
+ })),
25
+ ]);
15
26
  /** Provider-defined string enum: known values for autocomplete, any string accepted at runtime. */
16
27
  export const knownString = () => Schema.declare((value) => typeof value === "string", {
17
28
  expected: "string",
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-21098",
3
+ "version": "0.0.0-dev-21100",
4
4
  "name": "@opencode/ai",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "devDependencies": {
35
35
  "@clack/prompts": "1.0.0-alpha.1",
36
36
  "@effect/platform-node": "4.0.1",
37
- "@opencode/http-recorder": "0.0.0-dev-21098",
37
+ "@opencode/http-recorder": "0.0.0-dev-21100",
38
38
  "@tsconfig/bun": "1.0.9",
39
39
  "@types/bun": "1.4.0",
40
40
  "@typescript/native-preview": "7.0.0-dev.20251207.1",
@@ -44,7 +44,7 @@
44
44
  "@aws-sdk/credential-providers": "3.1057.0",
45
45
  "@smithy/eventstream-codec": "4.2.14",
46
46
  "@smithy/util-utf8": "4.2.2",
47
- "@opencode/schema": "0.0.0-dev-21098",
47
+ "@opencode/schema": "0.0.0-dev-21100",
48
48
  "aws4fetch": "1.0.20",
49
49
  "effect": "4.0.1",
50
50
  "google-auth-library": "10.5.0"