@opencode-ai/ai
Advanced tools
@@ -587,11 +587,3 @@ import { Schema } from "effect"; | ||
| } | undefined; | ||
| readonly delta?: { | ||
| readonly type?: string | undefined; | ||
| readonly text?: string | undefined; | ||
| readonly thinking?: string | undefined; | ||
| readonly signature?: string | undefined; | ||
| readonly partial_json?: string | undefined; | ||
| readonly stop_reason?: string | null | undefined; | ||
| readonly stop_sequence?: string | null | undefined; | ||
| } | undefined; | ||
| readonly delta?: unknown; | ||
| readonly index?: number | undefined; | ||
@@ -613,14 +605,3 @@ readonly usage?: { | ||
| } | undefined; | ||
| readonly content_block?: { | ||
| readonly type: string; | ||
| readonly id?: string | undefined; | ||
| readonly data?: string | undefined; | ||
| readonly name?: string | undefined; | ||
| readonly input?: unknown; | ||
| readonly text?: string | undefined; | ||
| readonly content?: unknown; | ||
| readonly thinking?: string | undefined; | ||
| readonly signature?: string | undefined; | ||
| readonly tool_use_id?: string | undefined; | ||
| } | undefined; | ||
| readonly content_block?: unknown; | ||
| }, { | ||
@@ -627,0 +608,0 @@ tools: Partial<Record<number, ToolStream.PendingTool>>; |
| import { Buffer } from "node:buffer"; | ||
| import { Effect, Schema } from "effect"; | ||
| import { Effect, Option, Schema } from "effect"; | ||
| import { Tool } from "@opencode-ai/schema/tool"; | ||
@@ -267,2 +267,3 @@ import { Route } from "../route/client.js"; | ||
| }); | ||
| const decodeAnthropicStreamBlock = Schema.decodeUnknownOption(AnthropicStreamBlock); | ||
| const AnthropicStreamDelta = Schema.Struct({ | ||
@@ -277,2 +278,3 @@ type: Schema.optional(Schema.String), | ||
| }); | ||
| const decodeAnthropicStreamDelta = Schema.decodeUnknownOption(AnthropicStreamDelta); | ||
| const AnthropicEvent = Schema.Struct({ | ||
@@ -282,4 +284,4 @@ type: Schema.String, | ||
| message: Schema.optional(Schema.Struct({ usage: Schema.optional(AnthropicUsage) })), | ||
| content_block: Schema.optional(AnthropicStreamBlock), | ||
| delta: Schema.optional(AnthropicStreamDelta), | ||
| content_block: Schema.optional(Schema.Unknown), | ||
| delta: Schema.optional(Schema.Unknown), | ||
| usage: Schema.optional(AnthropicUsage), | ||
@@ -1050,2 +1052,4 @@ // `type` and `message` are both required per Anthropic's spec, but | ||
| if (delta?.type === "text_delta" && delta.text) { | ||
| if (!state.lifecycle.text.has(`text-${event.index ?? 0}`)) | ||
| return [state, NO_EVENTS]; | ||
| const events = []; | ||
@@ -1058,2 +1062,4 @@ return [ | ||
| if (delta?.type === "thinking_delta" && delta.thinking) { | ||
| if (!state.lifecycle.reasoning.has(`reasoning-${event.index ?? 0}`)) | ||
| return [state, NO_EVENTS]; | ||
| const events = []; | ||
@@ -1070,2 +1076,4 @@ return [ | ||
| const index = event.index ?? 0; | ||
| if (!state.lifecycle.reasoning.has(`reasoning-${index}`)) | ||
| return [state, NO_EVENTS]; | ||
| return [ | ||
@@ -1157,8 +1165,33 @@ { | ||
| })); | ||
| const isKnownStreamBlockType = (type) => type === "text" || | ||
| type === "thinking" || | ||
| type === "redacted_thinking" || | ||
| type === "tool_use" || | ||
| type === "server_tool_use" || | ||
| isServerToolResultType(type); | ||
| const isKnownStreamDeltaType = (type) => type === "text_delta" || type === "thinking_delta" || type === "signature_delta" || type === "input_json_delta"; | ||
| const invalidStreamEvent = (event) => Effect.fail(ProviderShared.eventError(ADAPTER, "Invalid anthropic/anthropic-messages stream event", ProviderShared.encodeJson(event))); | ||
| const step = (state, event) => { | ||
| if (!SSE_EVENTS.has(event.type)) | ||
| return Effect.succeed([state, NO_EVENTS]); | ||
| if (event.type !== "content_block_start" && | ||
| event.content_block !== undefined && | ||
| Option.isNone(decodeAnthropicStreamBlock(event.content_block))) | ||
| return invalidStreamEvent(event); | ||
| if (event.type !== "content_block_delta" && | ||
| event.delta !== undefined && | ||
| Option.isNone(decodeAnthropicStreamDelta(event.delta))) | ||
| return invalidStreamEvent(event); | ||
| if (event.type === "message_start") | ||
| return Effect.succeed(onMessageStart(state, event)); | ||
| if (event.type === "content_block_start") { | ||
| const block = event.content_block; | ||
| if (block && (block.type === "tool_use" || block.type === "server_tool_use")) { | ||
| if (!ProviderShared.isRecord(event.content_block) || typeof event.content_block.type !== "string") | ||
| return invalidStreamEvent(event); | ||
| if (!isKnownStreamBlockType(event.content_block.type)) | ||
| return Effect.succeed([state, NO_EVENTS]); | ||
| const decoded = decodeAnthropicStreamBlock(event.content_block); | ||
| if (Option.isNone(decoded)) | ||
| return invalidStreamEvent(event); | ||
| const block = decoded.value; | ||
| if (block.type === "tool_use" || block.type === "server_tool_use") { | ||
| if (event.index === undefined) | ||
@@ -1169,10 +1202,22 @@ return Effect.fail(ProviderShared.eventError(ADAPTER, `Anthropic ${block.type} missing index`)); | ||
| } | ||
| return Effect.succeed(onContentBlockStart(state, event)); | ||
| return Effect.succeed(onContentBlockStart(state, { ...event, content_block: block })); | ||
| } | ||
| if (event.type === "content_block_delta") | ||
| return onContentBlockDelta(state, event); | ||
| if (event.type === "content_block_delta") { | ||
| if (!ProviderShared.isRecord(event.delta)) | ||
| return invalidStreamEvent(event); | ||
| if (typeof event.delta.type === "string" && !isKnownStreamDeltaType(event.delta.type)) | ||
| return Effect.succeed([state, NO_EVENTS]); | ||
| const decoded = decodeAnthropicStreamDelta(event.delta); | ||
| if (Option.isNone(decoded)) | ||
| return invalidStreamEvent(event); | ||
| return onContentBlockDelta(state, { ...event, delta: decoded.value }); | ||
| } | ||
| if (event.type === "content_block_stop") | ||
| return onContentBlockStop(state, event); | ||
| if (event.type === "message_delta") | ||
| return Effect.succeed(onMessageDelta(state, event)); | ||
| if (event.type === "message_delta") { | ||
| const decoded = decodeAnthropicStreamDelta(event.delta); | ||
| if (Option.isNone(decoded)) | ||
| return invalidStreamEvent(event); | ||
| return Effect.succeed(onMessageDelta(state, { ...event, delta: decoded.value })); | ||
| } | ||
| if (event.type === "message_stop") | ||
@@ -1179,0 +1224,0 @@ return onMessageStop(state); |
+3
-3
| { | ||
| "$schema": "https://json.schemastore.org/package.json", | ||
| "version": "0.0.0-dev-18161", | ||
| "version": "0.0.0-dev-18164", | ||
| "name": "@opencode-ai/ai", | ||
@@ -33,3 +33,3 @@ "type": "module", | ||
| "@effect/platform-node": "4.0.0-rc.111", | ||
| "@opencode-ai/http-recorder": "0.0.0-dev-18161", | ||
| "@opencode-ai/http-recorder": "0.0.0-dev-18164", | ||
| "@tsconfig/bun": "1.0.9", | ||
@@ -43,3 +43,3 @@ "@types/bun": "1.3.13", | ||
| "@smithy/util-utf8": "4.2.2", | ||
| "@opencode-ai/schema": "0.0.0-dev-18161", | ||
| "@opencode-ai/schema": "0.0.0-dev-18164", | ||
| "aws4fetch": "1.0.20", | ||
@@ -46,0 +46,0 @@ "effect": "4.0.0-rc.111", |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
1265990
0.14%29059
0.09%+ Added
- Removed