@jini-ai/agent-runtime 0.2.1 → 0.3.0
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 +2 -2
- package/dist/agent-protocol/acp/session.d.ts +206 -1
- package/dist/agent-protocol/acp/session.d.ts.map +1 -1
- package/dist/agent-protocol/acp/session.js +505 -350
- package/dist/agent-protocol/acp/session.js.map +1 -1
- package/dist/agent-protocol/core/json-line-stream.d.ts +53 -0
- package/dist/agent-protocol/core/json-line-stream.d.ts.map +1 -1
- package/dist/agent-protocol/core/json-line-stream.js +307 -255
- package/dist/agent-protocol/core/json-line-stream.js.map +1 -1
- package/dist/agent-protocol/pi-rpc/events.d.ts +22 -1
- package/dist/agent-protocol/pi-rpc/events.d.ts.map +1 -1
- package/dist/agent-protocol/pi-rpc/events.js +189 -153
- package/dist/agent-protocol/pi-rpc/events.js.map +1 -1
- package/dist/agent-protocol/pi-rpc/session.d.ts +39 -0
- package/dist/agent-protocol/pi-rpc/session.d.ts.map +1 -1
- package/dist/agent-protocol/pi-rpc/session.js +143 -107
- package/dist/agent-protocol/pi-rpc/session.js.map +1 -1
- package/dist/claude-stream.d.ts +64 -1
- package/dist/claude-stream.d.ts.map +1 -1
- package/dist/claude-stream.js +568 -441
- package/dist/claude-stream.js.map +1 -1
- package/dist/defs/aider.d.ts.map +1 -1
- package/dist/defs/aider.js +13 -1
- package/dist/defs/aider.js.map +1 -1
- package/dist/defs/amp.d.ts.map +1 -1
- package/dist/defs/amp.js +16 -1
- package/dist/defs/amp.js.map +1 -1
- package/dist/defs/amr.d.ts +2 -1
- package/dist/defs/amr.d.ts.map +1 -1
- package/dist/defs/amr.js +12 -1
- package/dist/defs/amr.js.map +1 -1
- package/dist/defs/antigravity.d.ts +41 -2
- package/dist/defs/antigravity.d.ts.map +1 -1
- package/dist/defs/antigravity.js +201 -22
- package/dist/defs/antigravity.js.map +1 -1
- package/dist/defs/claude.d.ts +2 -0
- package/dist/defs/claude.d.ts.map +1 -1
- package/dist/defs/claude.js +58 -3
- package/dist/defs/claude.js.map +1 -1
- package/dist/defs/codebuddy.d.ts.map +1 -1
- package/dist/defs/codebuddy.js +9 -1
- package/dist/defs/codebuddy.js.map +1 -1
- package/dist/defs/codex.d.ts +1 -0
- package/dist/defs/codex.d.ts.map +1 -1
- package/dist/defs/codex.js +34 -0
- package/dist/defs/codex.js.map +1 -1
- package/dist/defs/copilot.d.ts.map +1 -1
- package/dist/defs/copilot.js +15 -1
- package/dist/defs/copilot.js.map +1 -1
- package/dist/defs/cursor-agent.d.ts.map +1 -1
- package/dist/defs/cursor-agent.js +24 -2
- package/dist/defs/cursor-agent.js.map +1 -1
- package/dist/defs/deepseek.d.ts.map +1 -1
- package/dist/defs/deepseek.js +12 -0
- package/dist/defs/deepseek.js.map +1 -1
- package/dist/defs/devin.d.ts +2 -1
- package/dist/defs/devin.d.ts.map +1 -1
- package/dist/defs/devin.js +12 -7
- package/dist/defs/devin.js.map +1 -1
- package/dist/defs/grok-build.d.ts.map +1 -1
- package/dist/defs/grok-build.js +10 -0
- package/dist/defs/grok-build.js.map +1 -1
- package/dist/defs/hermes.d.ts +1 -0
- package/dist/defs/hermes.d.ts.map +1 -1
- package/dist/defs/hermes.js +3 -0
- package/dist/defs/hermes.js.map +1 -1
- package/dist/defs/kilo.d.ts +1 -0
- package/dist/defs/kilo.d.ts.map +1 -1
- package/dist/defs/kilo.js +3 -0
- package/dist/defs/kilo.js.map +1 -1
- package/dist/defs/kimi.d.ts +1 -0
- package/dist/defs/kimi.d.ts.map +1 -1
- package/dist/defs/kimi.js +3 -0
- package/dist/defs/kimi.js.map +1 -1
- package/dist/defs/kiro.d.ts +1 -0
- package/dist/defs/kiro.d.ts.map +1 -1
- package/dist/defs/kiro.js +3 -0
- package/dist/defs/kiro.js.map +1 -1
- package/dist/defs/pi.d.ts +1 -1
- package/dist/defs/pi.d.ts.map +1 -1
- package/dist/defs/pi.js +6 -1
- package/dist/defs/pi.js.map +1 -1
- package/dist/defs/qoder.d.ts +1 -0
- package/dist/defs/qoder.d.ts.map +1 -1
- package/dist/defs/qoder.js +15 -0
- package/dist/defs/qoder.js.map +1 -1
- package/dist/defs/qwen.d.ts.map +1 -1
- package/dist/defs/qwen.js +9 -0
- package/dist/defs/qwen.js.map +1 -1
- package/dist/defs/reasonix.d.ts +1 -0
- package/dist/defs/reasonix.d.ts.map +1 -1
- package/dist/defs/reasonix.js +3 -0
- package/dist/defs/reasonix.js.map +1 -1
- package/dist/defs/shared.d.ts +31 -1
- package/dist/defs/shared.d.ts.map +1 -1
- package/dist/defs/shared.js +35 -0
- package/dist/defs/shared.js.map +1 -1
- package/dist/defs/trae-cli.d.ts +1 -0
- package/dist/defs/trae-cli.d.ts.map +1 -1
- package/dist/defs/trae-cli.js +3 -0
- package/dist/defs/trae-cli.js.map +1 -1
- package/dist/defs/vibe.d.ts +1 -0
- package/dist/defs/vibe.d.ts.map +1 -1
- package/dist/defs/vibe.js +3 -0
- package/dist/defs/vibe.js.map +1 -1
- package/dist/detection.d.ts +1 -1
- package/dist/detection.d.ts.map +1 -1
- package/dist/detection.js +7 -2
- package/dist/detection.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/log-file.d.ts +23 -0
- package/dist/log-file.d.ts.map +1 -0
- package/dist/log-file.js +56 -0
- package/dist/log-file.js.map +1 -0
- package/dist/model-registry.d.ts +7 -45
- package/dist/model-registry.d.ts.map +1 -1
- package/dist/model-registry.js.map +1 -1
- package/dist/providers/anthropic-messages.d.ts +198 -6
- package/dist/providers/anthropic-messages.d.ts.map +1 -1
- package/dist/providers/anthropic-messages.js +317 -140
- package/dist/providers/anthropic-messages.js.map +1 -1
- package/dist/providers/azure-chat.d.ts +113 -3
- package/dist/providers/azure-chat.d.ts.map +1 -1
- package/dist/providers/azure-chat.js +114 -21
- package/dist/providers/azure-chat.js.map +1 -1
- package/dist/providers/connection-guard.d.ts +121 -1
- package/dist/providers/connection-guard.d.ts.map +1 -1
- package/dist/providers/connection-guard.js +205 -3
- package/dist/providers/connection-guard.js.map +1 -1
- package/dist/providers/connection-test.d.ts +62 -0
- package/dist/providers/connection-test.d.ts.map +1 -0
- package/dist/providers/connection-test.js +404 -0
- package/dist/providers/connection-test.js.map +1 -0
- package/dist/providers/elevenlabs.d.ts.map +1 -1
- package/dist/providers/elevenlabs.js +3 -2
- package/dist/providers/elevenlabs.js.map +1 -1
- package/dist/providers/google-messages.d.ts +208 -7
- package/dist/providers/google-messages.d.ts.map +1 -1
- package/dist/providers/google-messages.js +331 -105
- package/dist/providers/google-messages.js.map +1 -1
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/model-catalog.d.ts.map +1 -1
- package/dist/providers/model-catalog.js +21 -0
- package/dist/providers/model-catalog.js.map +1 -1
- package/dist/providers/ollama-chat.d.ts +190 -1
- package/dist/providers/ollama-chat.d.ts.map +1 -1
- package/dist/providers/ollama-chat.js +319 -110
- package/dist/providers/ollama-chat.js.map +1 -1
- package/dist/providers/openai-chat.d.ts +222 -14
- package/dist/providers/openai-chat.d.ts.map +1 -1
- package/dist/providers/openai-chat.js +377 -128
- package/dist/providers/openai-chat.js.map +1 -1
- package/dist/providers/sse-decode.d.ts +2 -2
- package/dist/providers/sse-decode.js +2 -2
- package/dist/types.d.ts +169 -87
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -4
|
@@ -1,3 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module providers/anthropic-messages
|
|
3
|
+
*
|
|
4
|
+
* Anthropic Messages API wire adapter + tool-loop turn-runner. Built fresh
|
|
5
|
+
* against Anthropic's real, current public API docs (`platform.claude.com/
|
|
6
|
+
* docs/en/build-with-claude/streaming`, `.../api/messages-streaming`),
|
|
7
|
+
* following this repo's established "verify against real API docs, don't
|
|
8
|
+
* guess from memory" convention (see `packages/deploy/src/netlify.ts`'s
|
|
9
|
+
* doc comment for the precedent) — not a byte-for-byte OD port. Only the
|
|
10
|
+
* *shape* is modeled on OD's `apps/daemon/src/routes/chat.ts`'s
|
|
11
|
+
* `runAnthropicToolTurn` (per `ADS-memory/reports/proposals/
|
|
12
|
+
* PROP-http-route-packs-chat-model-proxy-2026-07-21.md`, which read that
|
|
13
|
+
* 2267-line file directly and extracted its mechanism, line numbers, and
|
|
14
|
+
* two confirmed bugs — this task did not have access to the OD source
|
|
15
|
+
* itself). Extends this package's existing `providers/` pattern
|
|
16
|
+
* (`model-catalog.ts`'s BYOK request/response handling,
|
|
17
|
+
* `connection-guard.ts`'s SSRF guard + secret redaction,
|
|
18
|
+
* `role-marker-guard.ts`'s contamination detector, and the
|
|
19
|
+
* "one file per vendor's stream-parsing shape" precedent
|
|
20
|
+
* `claude-stream.ts`/`copilot-stream.ts`/`qoder-stream.ts` already
|
|
21
|
+
* establish) rather than duplicating any of that inside `@jini-ai/http-kit` — see
|
|
22
|
+
* the proposal's "real question: where does provider-specific wire-protocol
|
|
23
|
+
* logic belong?" section for the placement decision this file implements.
|
|
24
|
+
*
|
|
25
|
+
* **Scope for this pass:** the Anthropic Messages API only (native
|
|
26
|
+
* `x-api-key`/`anthropic-version` auth), text + tool-use + image content
|
|
27
|
+
* blocks, and the tool-execution loop. Extended thinking is deliberately out
|
|
28
|
+
* of scope — this adapter never sends a `thinking` request parameter, so
|
|
29
|
+
* Anthropic never emits `thinking`/`signature_delta` content blocks in the
|
|
30
|
+
* response (per the streaming docs: thinking blocks are opt-in), which
|
|
31
|
+
* means there is nothing to parse. A future pass can add it by following
|
|
32
|
+
* `claude-stream.ts#emitSafeText`'s precedent (thinking is passed through
|
|
33
|
+
* *unguarded* — the role-marker guard only polices the user-visible text
|
|
34
|
+
* channel, since thinking content is never re-serialized as a transcript
|
|
35
|
+
* turn boundary).
|
|
36
|
+
*
|
|
37
|
+
* **Image ("vision") support**, added for the vision self-check loop (render
|
|
38
|
+
* the model's own in-progress HTML, screenshot it, hand the PNG back to the
|
|
39
|
+
* model inside a tool result so the next turn sees what it built): the wire
|
|
40
|
+
* shape for `AnthropicImageBlockParam`/`AnthropicBase64ImageSource`/
|
|
41
|
+
* `AnthropicUrlImageSource` is modeled on Anthropic's real, current
|
|
42
|
+
* `ImageBlockParam`/`Base64ImageSource`/`URLImageSource` types
|
|
43
|
+
* (`github.com/anthropics/anthropic-sdk-typescript/blob/main/src/resources/
|
|
44
|
+
* messages/messages.ts`), and the 10 MB base64/4-format size guard below is
|
|
45
|
+
* modeled on the real, current direct-API limits documented at
|
|
46
|
+
* `platform.claude.com/docs/en/build-with-claude/vision#request-limits`
|
|
47
|
+
* (10 MB base64-encoded per image on the direct API; jpeg/png/gif/webp
|
|
48
|
+
* only — the lower 5 MB Bedrock/Google Cloud limit does not apply, since
|
|
49
|
+
* this adapter only ever speaks the direct API). Deliberately narrower than
|
|
50
|
+
* the real API: the real `ToolResultBlockParam.content` union also allows
|
|
51
|
+
* `DocumentBlockParam`/`SearchResultBlockParam`/`ToolReferenceBlockParam`
|
|
52
|
+
* blocks — those are out of scope, since the driving use case only needs
|
|
53
|
+
* text + image.
|
|
54
|
+
*
|
|
55
|
+
* **Two confirmed OD bugs fixed here, not carried forward** (both per the
|
|
56
|
+
* proposal doc above):
|
|
57
|
+
* 1. **Product-identity leak.** OD's OpenRouter branch hardcoded
|
|
58
|
+
* `'HTTP-Referer': 'https://opendesign.dev'` / `'X-Title': 'Open Design'`.
|
|
59
|
+
* OpenRouter itself is out of scope this pass, but the fix generalizes:
|
|
60
|
+
* `AnthropicTurnOptions.extraHeaders` is the *only* way any caller-
|
|
61
|
+
* identifying header reaches the outbound request, and it is always
|
|
62
|
+
* caller-supplied — nothing in this module ever hardcodes a product or
|
|
63
|
+
* gateway identity string.
|
|
64
|
+
* 2. **Duplicate `end`-event bug.** OD's `runAnthropicToolTurn` had two
|
|
65
|
+
* independent `sse.send('end', {})` call sites (role-marker-guard
|
|
66
|
+
* contamination, and normal turn completion) with no `ended`-flag guard,
|
|
67
|
+
* unlike its sibling non-tool-loop streamers in the same file. This
|
|
68
|
+
* module uses the shared `turn-end-guard.ts#createTurnEndGuard` — see
|
|
69
|
+
* that module's doc for why the guard is extracted into its own
|
|
70
|
+
* directly-tested unit rather than a local closure here — from every
|
|
71
|
+
* exit path (contamination, normal stop, max-tool-turns, upstream/
|
|
72
|
+
* network error), guaranteeing exactly one `end` event per call. See
|
|
73
|
+
* `__tests__/anthropic-messages.test.ts`'s "never emits end twice" case
|
|
74
|
+
* for the integration-level regression proof.
|
|
75
|
+
*/
|
|
76
|
+
import { createRoleMarkerGuard } from '../role-marker-guard.js';
|
|
77
|
+
import { type DnsLookupFn, type PinnedFetch } from './connection-guard.js';
|
|
1
78
|
import { type TurnEndReason } from './turn-end-guard.js';
|
|
2
79
|
export interface AnthropicTextBlockParam {
|
|
3
80
|
readonly type: 'text';
|
|
@@ -9,13 +86,30 @@ export interface AnthropicToolUseBlockParam {
|
|
|
9
86
|
readonly name: string;
|
|
10
87
|
readonly input: unknown;
|
|
11
88
|
}
|
|
89
|
+
/** Base64-encoded image source. `media_type` mirrors Anthropic's real, current supported-format list (jpeg/png/gif/webp — see module doc). */
|
|
90
|
+
export interface AnthropicBase64ImageSource {
|
|
91
|
+
readonly type: 'base64';
|
|
92
|
+
readonly media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
|
|
93
|
+
readonly data: string;
|
|
94
|
+
}
|
|
95
|
+
/** A URL Anthropic's servers fetch server-side — never fetched by this adapter, so the size guard below does not apply to it (see `guardToolResult`'s doc). */
|
|
96
|
+
export interface AnthropicUrlImageSource {
|
|
97
|
+
readonly type: 'url';
|
|
98
|
+
readonly url: string;
|
|
99
|
+
}
|
|
100
|
+
export interface AnthropicImageBlockParam {
|
|
101
|
+
readonly type: 'image';
|
|
102
|
+
readonly source: AnthropicBase64ImageSource | AnthropicUrlImageSource;
|
|
103
|
+
}
|
|
104
|
+
/** Content blocks legal inside a `tool_result`'s `content` array. Narrower than the real API's `ToolResultBlockParam.content` union — see module doc's "Image support" section for what was deliberately left out and why. */
|
|
105
|
+
export type AnthropicToolResultContentBlock = AnthropicTextBlockParam | AnthropicImageBlockParam;
|
|
12
106
|
export interface AnthropicToolResultBlockParam {
|
|
13
107
|
readonly type: 'tool_result';
|
|
14
108
|
readonly tool_use_id: string;
|
|
15
|
-
readonly content: string;
|
|
109
|
+
readonly content: string | readonly AnthropicToolResultContentBlock[];
|
|
16
110
|
readonly is_error?: boolean;
|
|
17
111
|
}
|
|
18
|
-
export type AnthropicContentBlockParam = AnthropicTextBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam;
|
|
112
|
+
export type AnthropicContentBlockParam = AnthropicTextBlockParam | AnthropicImageBlockParam | AnthropicToolUseBlockParam | AnthropicToolResultBlockParam;
|
|
19
113
|
export interface AnthropicMessageParam {
|
|
20
114
|
readonly role: 'user' | 'assistant';
|
|
21
115
|
readonly content: string | readonly AnthropicContentBlockParam[];
|
|
@@ -31,10 +125,10 @@ export interface AnthropicToolCall {
|
|
|
31
125
|
readonly input: unknown;
|
|
32
126
|
}
|
|
33
127
|
export interface AnthropicToolResult {
|
|
34
|
-
readonly content: string;
|
|
128
|
+
readonly content: string | readonly AnthropicToolResultContentBlock[];
|
|
35
129
|
readonly isError?: boolean;
|
|
36
130
|
}
|
|
37
|
-
/** Host-owned tool execution. This module only knows how to run the wire-protocol loop — what a tool actually *does* is always the caller's business (matching `@jini-ai/http`'s `RunHttpDeps.onStarted`/`db-ops.ts`'s injected `DaemonDbOperations` "the collaborator is always supplied" convention). */
|
|
131
|
+
/** Host-owned tool execution. This module only knows how to run the wire-protocol loop — what a tool actually *does* is always the caller's business (matching `@jini-ai/http-kit`'s `RunHttpDeps.onStarted`/`db-ops.ts`'s injected `DaemonDbOperations` "the collaborator is always supplied" convention). */
|
|
38
132
|
export type AnthropicToolExecutor = (call: AnthropicToolCall) => Promise<AnthropicToolResult>;
|
|
39
133
|
/** Why a `runAnthropicToolTurn` call ended its event stream. */
|
|
40
134
|
export type AnthropicTurnEndReason = TurnEndReason;
|
|
@@ -52,7 +146,7 @@ export type AnthropicTurnEvent = {
|
|
|
52
146
|
} | {
|
|
53
147
|
readonly type: 'tool_result';
|
|
54
148
|
readonly toolUseId: string;
|
|
55
|
-
readonly content: string;
|
|
149
|
+
readonly content: string | readonly AnthropicToolResultContentBlock[];
|
|
56
150
|
readonly isError: boolean;
|
|
57
151
|
} | {
|
|
58
152
|
readonly type: 'usage';
|
|
@@ -95,16 +189,104 @@ export interface AnthropicTurnOptions {
|
|
|
95
189
|
* leak documented in this module's header comment.
|
|
96
190
|
*/
|
|
97
191
|
readonly extraHeaders?: Record<string, string>;
|
|
192
|
+
/**
|
|
193
|
+
* Injectable HTTP transport, defaulting to `connection-guard.ts`'s {@link pinnedFetch}
|
|
194
|
+
* (SSRF-guarded, DNS-pinned). The dependency-injection seam a test uses to supply a fake
|
|
195
|
+
* directly instead of module-mocking `connection-guard.ts` (or its compiled `dist/` output, from
|
|
196
|
+
* outside this package) — see that module's `PinnedFetch` doc. Never override this in production
|
|
197
|
+
* wiring: doing so bypasses the SSRF guard entirely for whatever transport is supplied.
|
|
198
|
+
*/
|
|
199
|
+
readonly fetchImpl?: PinnedFetch;
|
|
200
|
+
/**
|
|
201
|
+
* Injectable DNS resolver for {@link validateBaseUrlResolved}'s DNS-pinning check, defaulting to
|
|
202
|
+
* {@link defaultDnsLookup}. Same test-only override rationale as `fetchImpl`.
|
|
203
|
+
*/
|
|
204
|
+
readonly dnsLookup?: DnsLookupFn;
|
|
98
205
|
}
|
|
99
206
|
export interface AnthropicTurnResult {
|
|
100
207
|
readonly stopReason: string | null;
|
|
101
208
|
readonly toolTurns: number;
|
|
102
209
|
}
|
|
210
|
+
export declare function anthropicRequestUrl(baseUrl: string | undefined): string;
|
|
211
|
+
export declare function anthropicHeaders(options: AnthropicTurnOptions): Record<string, string>;
|
|
212
|
+
export declare function anthropicRequestBody(options: AnthropicTurnOptions, messages: readonly AnthropicMessageParam[]): Record<string, unknown>;
|
|
213
|
+
export declare function extractAnthropicErrorDetail(rawText: string): string;
|
|
214
|
+
/**
|
|
215
|
+
* Validates one tool-result content block against the real API's format/size constraints.
|
|
216
|
+
* Returns the violation reason, or `null` when the block is legal to send.
|
|
217
|
+
*
|
|
218
|
+
* URL-sourced images are intentionally not size-checked here: Anthropic's servers fetch that URL,
|
|
219
|
+
* not this adapter, so there is no local payload to bound (a different threat model than
|
|
220
|
+
* `connection-guard.ts`'s SSRF guard, which protects *this adapter's own* outbound `baseUrl`
|
|
221
|
+
* request).
|
|
222
|
+
*
|
|
223
|
+
* @complexity O(1) — reads `data.length`, never decodes or parses the base64 payload.
|
|
224
|
+
* @overallScore 100
|
|
225
|
+
*/
|
|
226
|
+
export declare function invalidToolResultContentBlockReason(block: AnthropicToolResultContentBlock): string | null;
|
|
227
|
+
/**
|
|
228
|
+
* Runtime guard applied to every `AnthropicToolExecutor` result before it is wired onto the
|
|
229
|
+
* outbound request or reported via `onEvent`. `AnthropicToolResult` is host-owned (see
|
|
230
|
+
* `AnthropicToolExecutor`'s "Host-owned tool execution" doc comment) — the TS content-block union
|
|
231
|
+
* only constrains a well-behaved host at compile time, not a buggy one at runtime, e.g. a
|
|
232
|
+
* screenshot helper that hands back an oversized PNG or a HEIC file mislabeled as `image/jpeg`.
|
|
233
|
+
* Rather than forwarding an invalid block and letting Anthropic reject the *entire* turn with an
|
|
234
|
+
* opaque upstream 400, this substitutes a normal `is_error` tool_result the model can see and
|
|
235
|
+
* react to — matching this module's existing security-conscious posture
|
|
236
|
+
* (`connection-guard.ts`'s SSRF guard, `role-marker-guard.ts`'s contamination guard).
|
|
237
|
+
*
|
|
238
|
+
* @param result - The (host-supplied, not yet trusted) return value of one `executeTool` call.
|
|
239
|
+
* @returns `result` unchanged when valid; otherwise a replacement `{ content, isError: true }`
|
|
240
|
+
* describing the violation.
|
|
241
|
+
* @complexity O(n) in the number of content blocks; O(1) per block (see
|
|
242
|
+
* `invalidToolResultContentBlockReason`).
|
|
243
|
+
* @overallScore 100
|
|
244
|
+
*/
|
|
245
|
+
export declare function guardToolResult(result: AnthropicToolResult): AnthropicToolResult;
|
|
246
|
+
export interface AnthropicBlockState {
|
|
247
|
+
readonly type: 'text' | 'tool_use' | 'other';
|
|
248
|
+
text: string;
|
|
249
|
+
toolId?: string;
|
|
250
|
+
toolName?: string;
|
|
251
|
+
inputJson: string;
|
|
252
|
+
}
|
|
253
|
+
export interface SingleRequestOutcome {
|
|
254
|
+
readonly stopReason: string | null;
|
|
255
|
+
readonly toolCalls: readonly AnthropicToolCall[];
|
|
256
|
+
readonly text: string;
|
|
257
|
+
}
|
|
258
|
+
/** Mutable reduction state threaded through one streaming request's SSE frame handlers (below). Grouped into one object so each handler takes a single parameter instead of five. Exported so a unit test can construct one directly (e.g. `{ guard: createRoleMarkerGuard('t'), blocks: new Map(), toolCalls: [], fullText: '', stopReason: null, usage: null }`) without going through a full `runAnthropicToolTurn` call. */
|
|
259
|
+
export interface AnthropicStreamState {
|
|
260
|
+
readonly guard: ReturnType<typeof createRoleMarkerGuard>;
|
|
261
|
+
readonly blocks: Map<number, AnthropicBlockState>;
|
|
262
|
+
readonly toolCalls: AnthropicToolCall[];
|
|
263
|
+
fullText: string;
|
|
264
|
+
stopReason: string | null;
|
|
265
|
+
usage: Record<string, unknown> | null;
|
|
266
|
+
}
|
|
267
|
+
/** Parses one SSE frame's data as a JSON object, or `null` for a malformed/empty keep-alive frame or a non-object payload — both are tolerated by the caller as "nothing to do this frame". */
|
|
268
|
+
export declare function parseAnthropicSseData(raw: string): Record<string, unknown> | null;
|
|
269
|
+
export declare function handleContentBlockStart(state: AnthropicStreamState, data: Record<string, unknown>): void;
|
|
270
|
+
/**
|
|
271
|
+
* Feeds one text delta through the role-marker guard and emits the safe portion. Returns
|
|
272
|
+
* `'break'` once the guard flags contamination (the caller ends the turn immediately), otherwise
|
|
273
|
+
* `'continue'`.
|
|
274
|
+
*/
|
|
275
|
+
export declare function handleAnthropicTextDelta(state: AnthropicStreamState, index: number, text: string, onEvent: (event: AnthropicTurnEvent) => void): 'continue' | 'break';
|
|
276
|
+
export declare function handleAnthropicInputJsonDelta(state: AnthropicStreamState, index: number, partialJson: string): void;
|
|
277
|
+
export declare function handleContentBlockDelta(state: AnthropicStreamState, data: Record<string, unknown>, onEvent: (event: AnthropicTurnEvent) => void): 'continue' | 'break';
|
|
278
|
+
/** Parses one tool_use block's accumulated `input_json_delta` text, falling back to `{}` for empty or malformed JSON — mirrors OpenAI's identical fallback for `tool_calls[].function.arguments` in `openai-chat.ts`. */
|
|
279
|
+
export declare function parseAccumulatedToolInputJson(inputJson: string): unknown;
|
|
280
|
+
export declare function handleContentBlockStop(state: AnthropicStreamState, data: Record<string, unknown>, onEvent: (event: AnthropicTurnEvent) => void): void;
|
|
281
|
+
export declare function handleMessageDelta(state: AnthropicStreamState, data: Record<string, unknown>, onEvent: (event: AnthropicTurnEvent) => void): void;
|
|
282
|
+
export declare function handleAnthropicErrorFrame(data: Record<string, unknown>, onEvent: (event: AnthropicTurnEvent) => void, apiKey: string): void;
|
|
283
|
+
/** The SSE event's dispatch key: `data.type` when present (every real Anthropic frame carries one), else the record's `event:` line, else `''` — a key that intentionally matches no entry in {@link ANTHROPIC_FRAME_HANDLERS} (`message_start`/`message_stop`/`ping`/future event types fall through as no-ops, same as before this frame was a lookup table). */
|
|
284
|
+
export declare function anthropicFrameKind(data: Record<string, unknown>, frameEvent: string | null): string;
|
|
103
285
|
/**
|
|
104
286
|
* Runs a full Anthropic Messages API turn, including the tool-execution
|
|
105
287
|
* loop when `options.executeTool` is supplied and the model requests tool
|
|
106
288
|
* use. Emits a generic event stream via `options.onEvent` — a caller (e.g.
|
|
107
|
-
* `@jini-ai/http`'s `model-proxy.ts`) adapts those events onto an outbound SSE
|
|
289
|
+
* `@jini-ai/http-kit`'s `model-proxy.ts`) adapts those events onto an outbound SSE
|
|
108
290
|
* channel; this module has no knowledge of HTTP/Express.
|
|
109
291
|
*
|
|
110
292
|
* Guarantees exactly one `{type: 'end'}` event per call, regardless of which
|
|
@@ -112,5 +294,15 @@ export interface AnthropicTurnResult {
|
|
|
112
294
|
* request/stream error) triggers it — the fix for OD's confirmed duplicate-
|
|
113
295
|
* `end`-event bug (see module doc).
|
|
114
296
|
*/
|
|
297
|
+
/**
|
|
298
|
+
* Decides why the tool loop should stop after one request, or returns `null` when it should
|
|
299
|
+
* instead proceed to execute the pending tool calls. Pure — no events, no I/O — so the three
|
|
300
|
+
* "stop" conditions (no/empty tool_use, no executor, turn ceiling reached) are exercised as plain
|
|
301
|
+
* value-in/value-out assertions rather than through the full request/event-emission machinery.
|
|
302
|
+
*/
|
|
303
|
+
export declare function anthropicLoopExitReason(outcome: SingleRequestOutcome, toolTurns: number, maxToolTurns: number): AnthropicTurnEndReason | null;
|
|
304
|
+
export declare function buildAnthropicAssistantContent(text: string, toolCalls: readonly AnthropicToolCall[]): AnthropicContentBlockParam[];
|
|
305
|
+
/** Runs every pending tool call in order and reduces the results into the `tool_result` blocks the next request's `user` message carries. Emits one `tool_result` event per call as it goes. */
|
|
306
|
+
export declare function executeAnthropicToolCalls(executeTool: AnthropicToolExecutor, calls: readonly AnthropicToolCall[], onEvent: (event: AnthropicTurnEvent) => void): Promise<AnthropicToolResultBlockParam[]>;
|
|
115
307
|
export declare function runAnthropicToolTurn(options: AnthropicTurnOptions): Promise<AnthropicTurnResult>;
|
|
116
308
|
//# sourceMappingURL=anthropic-messages.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic-messages.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic-messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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