@tanstack/ai 0.39.0 → 0.39.1
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/esm/activities/chat/index.js +4 -0
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/stream/processor.d.ts +18 -0
- package/dist/esm/activities/chat/stream/processor.js +86 -4
- package/dist/esm/activities/chat/stream/processor.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.d.ts +22 -1
- package/dist/esm/utilities/provider-executed.d.ts +18 -0
- package/dist/esm/utilities/provider-executed.js +15 -0
- package/dist/esm/utilities/provider-executed.js.map +1 -0
- package/package.json +1 -1
- package/skills/ai-core/adapter-configuration/references/anthropic-adapter.md +43 -12
- package/skills/ai-core/media-generation/SKILL.md +1 -1
- package/src/activities/chat/index.ts +8 -0
- package/src/activities/chat/stream/processor.ts +141 -3
- package/src/index.ts +5 -0
- package/src/types.ts +23 -1
- package/src/utilities/provider-executed.ts +32 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-executed.js","sources":["../../../src/utilities/provider-executed.ts"],"sourcesContent":["import type { ProviderExecutedToolMetadata } from '../types'\n\n/**\n * Narrow a tool call's opaque `metadata` to the provider-executed convention.\n * Returns the typed metadata when the call is provider-executed, else `null`.\n *\n * @see ProviderExecutedToolMetadata\n */\nexport function getProviderExecutedMetadata(\n toolCall: { metadata?: unknown } | null | undefined,\n): ProviderExecutedToolMetadata | null {\n const metadata = toolCall?.metadata\n if (\n typeof metadata === 'object' &&\n metadata !== null &&\n (metadata as ProviderExecutedToolMetadata).providerExecuted === true\n ) {\n return metadata as ProviderExecutedToolMetadata\n }\n return null\n}\n\n/**\n * True when a tool call was executed by the provider (e.g. Anthropic\n * `web_search` / `web_fetch` server tools) rather than the agent loop. Such\n * calls must not be routed to client-side execution and are already \"complete\".\n */\nexport function isProviderExecutedToolCall(\n toolCall: { metadata?: unknown } | null | undefined,\n): boolean {\n return getProviderExecutedMetadata(toolCall) !== null\n}\n"],"names":[],"mappings":"AAQO,SAAS,4BACd,UACqC;AACrC,QAAM,WAAW,UAAU;AAC3B,MACE,OAAO,aAAa,YACpB,aAAa,QACZ,SAA0C,qBAAqB,MAChE;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAOO,SAAS,2BACd,UACS;AACT,SAAO,4BAA4B,QAAQ,MAAM;AACnD;"}
|
package/package.json
CHANGED
|
@@ -21,17 +21,23 @@ import { anthropicText } from '@tanstack/ai-anthropic'
|
|
|
21
21
|
|
|
22
22
|
## Key Chat Models
|
|
23
23
|
|
|
24
|
-
| Model | Context Window | Max Output | Notes
|
|
25
|
-
| ------------------- | -------------- | ---------- |
|
|
26
|
-
| `claude-
|
|
27
|
-
| `claude-sonnet-
|
|
28
|
-
| `claude-
|
|
29
|
-
| `claude-opus-4-
|
|
30
|
-
| `claude-
|
|
31
|
-
| `claude-sonnet-4`
|
|
32
|
-
| `claude-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
| Model | Context Window | Max Output | Notes |
|
|
25
|
+
| ------------------- | -------------- | ---------- | ------------------------------------------- |
|
|
26
|
+
| `claude-fable-5` | 1M | 128K | Most capable; thinking always on (adaptive) |
|
|
27
|
+
| `claude-sonnet-5` | 1M | 128K | Best balance; adaptive thinking by default |
|
|
28
|
+
| `claude-opus-4-8` | 1M | 128K | Opus tier; adaptive thinking, no sampling |
|
|
29
|
+
| `claude-opus-4-7` | 1M | 128K | Older Opus; adaptive thinking, no sampling |
|
|
30
|
+
| `claude-opus-4-6` | 200K | 128K | Older Opus, adaptive + budget thinking |
|
|
31
|
+
| `claude-sonnet-4-6` | 1M | 64K | Previous gen balanced, adaptive + budget |
|
|
32
|
+
| `claude-sonnet-4-5` | 200K | 64K | Previous gen balanced |
|
|
33
|
+
| `claude-opus-4-5` | 200K | 32K | Previous gen most capable |
|
|
34
|
+
| `claude-opus-4-1` | 200K | 64K | Deprecated (retires 2026-08-05) |
|
|
35
|
+
| `claude-haiku-4-5` | 200K | 64K | Fast and affordable |
|
|
36
|
+
|
|
37
|
+
Note: Model IDs use the format `claude-sonnet-5`, `claude-opus-4-8`, etc.
|
|
38
|
+
Retired models (Claude 3.x, Sonnet 3.7, Opus 4 / Sonnet 4) and the `-fast`
|
|
39
|
+
variant ids were removed — every registered id resolves against the
|
|
40
|
+
first-party Anthropic API.
|
|
35
41
|
|
|
36
42
|
## Provider-Specific modelOptions
|
|
37
43
|
|
|
@@ -90,11 +96,36 @@ chat({
|
|
|
90
96
|
ANTHROPIC_API_KEY
|
|
91
97
|
```
|
|
92
98
|
|
|
99
|
+
## Adaptive-era modelOptions (Sonnet 5, Fable 5, Opus 4.7/4.8)
|
|
100
|
+
|
|
101
|
+
The per-model types restrict `modelOptions` on the newest models:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
chat({
|
|
105
|
+
adapter: anthropicText('claude-sonnet-5'), // or 'claude-fable-5', 'claude-opus-4-8'
|
|
106
|
+
messages,
|
|
107
|
+
modelOptions: {
|
|
108
|
+
// Adaptive thinking only — budget_tokens is rejected (400).
|
|
109
|
+
// On claude-fable-5, { type: 'disabled' } is also rejected;
|
|
110
|
+
// elsewhere it opts out of thinking.
|
|
111
|
+
thinking: { type: 'adaptive', display: 'summarized' },
|
|
112
|
+
// Effort lives under output_config; 'xhigh' is available on
|
|
113
|
+
// Opus 4.7+, Sonnet 5, and Fable 5.
|
|
114
|
+
output_config: { effort: 'xhigh' },
|
|
115
|
+
max_tokens: 64_000,
|
|
116
|
+
// NO temperature / top_p / top_k — the API rejects them on these models
|
|
117
|
+
},
|
|
118
|
+
})
|
|
119
|
+
```
|
|
120
|
+
|
|
93
121
|
## Gotchas
|
|
94
122
|
|
|
95
123
|
- `thinking.budget_tokens` must be >= 1024 AND less than `modelOptions.max_tokens`.
|
|
96
124
|
Failing either check throws a validation error.
|
|
97
125
|
- Cannot set both `top_p` and `temperature` at the same time (throws error).
|
|
98
|
-
- `claude-
|
|
126
|
+
- `claude-sonnet-5`, `claude-fable-5`, `claude-opus-4-8`, and
|
|
127
|
+
`claude-opus-4-7` do NOT accept `temperature`, `top_p`, `top_k`, or
|
|
128
|
+
`thinking: { type: 'enabled', budget_tokens }` — adaptive thinking +
|
|
129
|
+
`output_config.effort` replace them (typed per model).
|
|
99
130
|
- System prompts support prompt caching via `cache_control` on `TextBlockParam[]`.
|
|
100
131
|
- All Claude models accept `text`, `image`, and `document` (PDF) input.
|
|
@@ -151,7 +151,7 @@ function ImageGenerator() {
|
|
|
151
151
|
|
|
152
152
|
Supported adapters: `openaiImage` (dall-e-2, dall-e-3, gpt-image-1,
|
|
153
153
|
gpt-image-1-mini, gpt-image-2) and `geminiImage` (gemini-3.1-flash-image-preview,
|
|
154
|
-
imagen-4.0-generate-001, etc.).
|
|
154
|
+
gemini-3.1-flash-lite-image, imagen-4.0-generate-001, etc.).
|
|
155
155
|
|
|
156
156
|
```typescript
|
|
157
157
|
import { generateImage } from '@tanstack/ai'
|
|
@@ -12,6 +12,7 @@ import { streamToText } from '../../stream-to-response.js'
|
|
|
12
12
|
import { resolveDebugOption } from '../../logger/resolve'
|
|
13
13
|
import { EventType } from '../../types'
|
|
14
14
|
import { normalizeToolResult } from '../../utilities/tool-result'
|
|
15
|
+
import { isProviderExecutedToolCall } from '../../utilities/provider-executed'
|
|
15
16
|
import { LazyToolManager } from './tools/lazy-tool-manager'
|
|
16
17
|
import {
|
|
17
18
|
MiddlewareAbortError,
|
|
@@ -1884,6 +1885,13 @@ class TextEngine<
|
|
|
1884
1885
|
for (const message of this.messages) {
|
|
1885
1886
|
if (message.role === 'assistant' && message.toolCalls) {
|
|
1886
1887
|
for (const toolCall of message.toolCalls) {
|
|
1888
|
+
// Provider-executed tool calls (e.g. Anthropic `web_search`) were
|
|
1889
|
+
// already run by the provider; they carry no client result, so they
|
|
1890
|
+
// would otherwise look "pending" forever and the loop would try (and
|
|
1891
|
+
// fail) to execute them client-side. Skip them.
|
|
1892
|
+
if (isProviderExecutedToolCall(toolCall)) {
|
|
1893
|
+
continue
|
|
1894
|
+
}
|
|
1887
1895
|
if (!completedToolIds.has(toolCall.id)) {
|
|
1888
1896
|
pending.push(toolCall)
|
|
1889
1897
|
}
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
uiMessageToModelMessages,
|
|
24
24
|
} from '../messages.js'
|
|
25
25
|
import { normalizeToolResult } from '../../../utilities/tool-result'
|
|
26
|
+
import { isProviderExecutedToolCall } from '../../../utilities/provider-executed'
|
|
26
27
|
import { defaultJSONParser } from './json-parser'
|
|
27
28
|
import {
|
|
28
29
|
appendStructuredOutputDelta,
|
|
@@ -403,12 +404,15 @@ export class StreamProcessor {
|
|
|
403
404
|
// 1. It was approved/denied (approval-responded state)
|
|
404
405
|
// 2. It has an output field set (client tool completed via addToolResult)
|
|
405
406
|
// 3. It has a corresponding tool-result part (server tool completed)
|
|
407
|
+
// 4. It is provider-executed (e.g. Anthropic web_search) — already run by
|
|
408
|
+
// the provider, so there is no client result to wait for.
|
|
406
409
|
return toolParts.every(
|
|
407
410
|
(part) =>
|
|
408
411
|
part.state === 'complete' ||
|
|
409
412
|
part.state === 'approval-responded' ||
|
|
410
413
|
(part.output !== undefined && !part.approval) ||
|
|
411
|
-
toolResultIds.has(part.id)
|
|
414
|
+
toolResultIds.has(part.id) ||
|
|
415
|
+
isProviderExecutedToolCall(part),
|
|
412
416
|
)
|
|
413
417
|
}
|
|
414
418
|
|
|
@@ -881,10 +885,140 @@ export class StreamProcessor {
|
|
|
881
885
|
// them directly to UIMessage[] is unsafe and causes "Cannot read properties
|
|
882
886
|
// of undefined (reading 'find')" when code later reads message.parts (e.g.
|
|
883
887
|
// the onToolCallStateChange devtools handler).
|
|
884
|
-
|
|
888
|
+
//
|
|
889
|
+
// The AG-UI `MESSAGES_SNAPSHOT` wire shape cannot reconstruct client-side
|
|
890
|
+
// tool-call metadata a server may omit: a `role: 'tool'` message only carries
|
|
891
|
+
// `toolCallId` + `content`, and an assistant message in the snapshot may
|
|
892
|
+
// drop `toolCalls` the client already observed via `TOOL_CALL_*` events.
|
|
893
|
+
// Without the matching `tool-call` part, later `addToolResult(toolCallId)`
|
|
894
|
+
// calls cannot locate the call and warn + no-op (see #859). To keep the
|
|
895
|
+
// UI representation consistent with the streaming fan-out and preserve the
|
|
896
|
+
// unreconstructable metadata, reconcile the normalized snapshot against
|
|
897
|
+
// the pre-snapshot state; see `reconcileSnapshotToolCalls`.
|
|
898
|
+
const prevMessages = this.messages
|
|
899
|
+
const normalized = chunk.messages.map(aguiSnapshotMessageToUIMessage)
|
|
900
|
+
this.messages = this.reconcileSnapshotToolCalls(normalized, prevMessages)
|
|
885
901
|
this.emitMessagesChange()
|
|
886
902
|
}
|
|
887
903
|
|
|
904
|
+
/**
|
|
905
|
+
* Reconcile a freshly normalized snapshot with the pre-snapshot message
|
|
906
|
+
* state so unreconstructable tool-call metadata is preserved.
|
|
907
|
+
*
|
|
908
|
+
* Post-pass (a): anchor `tool-result`-only assistant messages (the shape
|
|
909
|
+
* `aguiSnapshotMessageToUIMessage` emits for AG-UI `role: 'tool'` wire
|
|
910
|
+
* messages) into the message containing the matching `tool-call` part, or —
|
|
911
|
+
* when the snapshot supplies no such part — the nearest earlier anchorable
|
|
912
|
+
* assistant message, matching the in-stream fan-out shape
|
|
913
|
+
* `assistant: [text, tool-call, tool-result, ...]`. Detached messages with
|
|
914
|
+
* no earlier anchorable assistant are kept verbatim.
|
|
915
|
+
*
|
|
916
|
+
* Post-pass (b): when a `tool-result` part references a `toolCallId` whose
|
|
917
|
+
* `tool-call` part is absent from the snapshot, carry the `tool-call` part
|
|
918
|
+
* forward from the pre-snapshot state (state and output untouched) so a
|
|
919
|
+
* subsequent `addToolResult(toolCallId)` can still locate the call.
|
|
920
|
+
*/
|
|
921
|
+
private reconcileSnapshotToolCalls(
|
|
922
|
+
snapshot: Array<UIMessage>,
|
|
923
|
+
prevMessages: Array<UIMessage>,
|
|
924
|
+
): Array<UIMessage> {
|
|
925
|
+
// Index tool-call parts observed before the snapshot by id so we can
|
|
926
|
+
// restore metadata the snapshot cannot re-emit. Duplicate ids resolve
|
|
927
|
+
// last-write-wins: the same tool call can appear in multiple messages
|
|
928
|
+
// across reconnects, and the most recent part carries the freshest state.
|
|
929
|
+
const prevToolCalls = new Map<string, ToolCallPart>()
|
|
930
|
+
for (const msg of prevMessages) {
|
|
931
|
+
for (const part of msg.parts) {
|
|
932
|
+
if (part.type === 'tool-call') {
|
|
933
|
+
prevToolCalls.set(part.id, part)
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
// Index tool-call parts already present in the snapshot so (b) only fills
|
|
938
|
+
// genuine gaps rather than duplicating a tool-call the snapshot supplies.
|
|
939
|
+
const snapshotToolCallIds = new Set<string>()
|
|
940
|
+
for (const msg of snapshot) {
|
|
941
|
+
for (const part of msg.parts) {
|
|
942
|
+
if (part.type === 'tool-call') {
|
|
943
|
+
snapshotToolCallIds.add(part.id)
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
const reconciled: Array<UIMessage> = []
|
|
949
|
+
for (const msg of snapshot) {
|
|
950
|
+
const toolResultPart =
|
|
951
|
+
msg.role === 'assistant' && msg.parts.length === 1
|
|
952
|
+
? msg.parts.find((p): p is ToolResultPart => p.type === 'tool-result')
|
|
953
|
+
: undefined
|
|
954
|
+
|
|
955
|
+
if (!toolResultPart) {
|
|
956
|
+
reconciled.push(msg)
|
|
957
|
+
continue
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// Prefer the message that actually contains the matching tool-call
|
|
961
|
+
// part. AG-UI `reasoning`/`activity` messages also normalize to
|
|
962
|
+
// `role: 'assistant'`, so anchoring into the nearest assistant alone
|
|
963
|
+
// could separate a result from its call (and a later
|
|
964
|
+
// `addToolResult(toolCallId)` would then append a duplicate result
|
|
965
|
+
// next to the call).
|
|
966
|
+
const target =
|
|
967
|
+
reconciled.findLast((m) =>
|
|
968
|
+
m.parts.some(
|
|
969
|
+
(p) => p.type === 'tool-call' && p.id === toolResultPart.toolCallId,
|
|
970
|
+
),
|
|
971
|
+
) ??
|
|
972
|
+
reconciled.findLast(
|
|
973
|
+
(m) =>
|
|
974
|
+
m.role === 'assistant' &&
|
|
975
|
+
!(m.parts.length === 1 && m.parts[0]?.type === 'tool-result'),
|
|
976
|
+
)
|
|
977
|
+
|
|
978
|
+
if (!target) {
|
|
979
|
+
// No assistant to anchor into — keep the detached message intact.
|
|
980
|
+
if (!snapshotToolCallIds.has(toolResultPart.toolCallId)) {
|
|
981
|
+
console.warn(
|
|
982
|
+
`[StreamProcessor] MESSAGES_SNAPSHOT contains a tool-result for "${toolResultPart.toolCallId}" but no matching tool-call exists in the snapshot, and there is no assistant message to anchor into; addToolResult("${toolResultPart.toolCallId}") will not be able to locate this call`,
|
|
983
|
+
)
|
|
984
|
+
}
|
|
985
|
+
reconciled.push(msg)
|
|
986
|
+
continue
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
const parts = [...target.parts]
|
|
990
|
+
// (b) Fill in a missing tool-call part from the pre-snapshot state when
|
|
991
|
+
// the snapshot references its id via a tool-result but supplies no
|
|
992
|
+
// tool-call metadata of its own.
|
|
993
|
+
if (
|
|
994
|
+
!snapshotToolCallIds.has(toolResultPart.toolCallId) &&
|
|
995
|
+
!parts.some(
|
|
996
|
+
(p) => p.type === 'tool-call' && p.id === toolResultPart.toolCallId,
|
|
997
|
+
)
|
|
998
|
+
) {
|
|
999
|
+
const prev = prevToolCalls.get(toolResultPart.toolCallId)
|
|
1000
|
+
if (prev) {
|
|
1001
|
+
// Insert the carried-over tool-call before its tool-result (pushed
|
|
1002
|
+
// below) so call→result ordering matches the streaming fan-out.
|
|
1003
|
+
parts.push({ ...prev })
|
|
1004
|
+
snapshotToolCallIds.add(prev.id)
|
|
1005
|
+
} else {
|
|
1006
|
+
console.warn(
|
|
1007
|
+
`[StreamProcessor] MESSAGES_SNAPSHOT contains a tool-result for "${toolResultPart.toolCallId}" but no matching tool-call exists in the snapshot or the pre-snapshot state; addToolResult("${toolResultPart.toolCallId}") will not be able to locate this call`,
|
|
1008
|
+
)
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
parts.push(toolResultPart)
|
|
1012
|
+
// Replace rather than push into `target.parts`: a snapshot message that
|
|
1013
|
+
// arrived already carrying `parts` (TanStack server echoing UIMessages)
|
|
1014
|
+
// shares its array with the incoming chunk, and mutating it in place
|
|
1015
|
+
// would corrupt the caller's event object.
|
|
1016
|
+
target.parts = parts
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
return reconciled
|
|
1020
|
+
}
|
|
1021
|
+
|
|
888
1022
|
/**
|
|
889
1023
|
* Handle TEXT_MESSAGE_CONTENT event.
|
|
890
1024
|
*
|
|
@@ -1781,8 +1915,12 @@ export class StreamProcessor {
|
|
|
1781
1915
|
* downgrading a failed call back to 'input-complete'.
|
|
1782
1916
|
*/
|
|
1783
1917
|
private isToolCallPartErrored(toolCallId: string): boolean {
|
|
1918
|
+
// `initialMessages` may be ModelMessage-shaped (no `parts`) — e.g. the
|
|
1919
|
+
// common pattern of seeding a processor with the same messages passed to
|
|
1920
|
+
// `chat()`. Guard the access so iterating them never throws.
|
|
1784
1921
|
return this.messages.some((msg) =>
|
|
1785
|
-
|
|
1922
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- `parts` is typed as required, but seeded ModelMessage-shaped messages can lack it at runtime.
|
|
1923
|
+
msg.parts?.some(
|
|
1786
1924
|
(part) =>
|
|
1787
1925
|
part.type === 'tool-call' &&
|
|
1788
1926
|
part.id === toolCallId &&
|
package/src/index.ts
CHANGED
|
@@ -256,6 +256,11 @@ export {
|
|
|
256
256
|
normalizeToolResult,
|
|
257
257
|
} from './utilities/tool-result'
|
|
258
258
|
|
|
259
|
+
export {
|
|
260
|
+
getProviderExecutedMetadata,
|
|
261
|
+
isProviderExecutedToolCall,
|
|
262
|
+
} from './utilities/provider-executed'
|
|
263
|
+
|
|
259
264
|
// Adapter extension utilities
|
|
260
265
|
export { createModel, extendAdapter } from './extend-adapter'
|
|
261
266
|
export type { ExtendedModelDef, ModelCapabilities } from './extend-adapter'
|
package/src/types.ts
CHANGED
|
@@ -160,6 +160,26 @@ export interface ToolCall<TMetadata = unknown> {
|
|
|
160
160
|
metadata?: TMetadata
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
+
/**
|
|
164
|
+
* Convention for tool-call `metadata` that marks a call as **provider-executed**
|
|
165
|
+
* — run by the provider's own infrastructure (e.g. Anthropic `web_search` /
|
|
166
|
+
* `web_fetch` server tools) rather than by the agent loop. Adapters set
|
|
167
|
+
* `providerExecuted: true` so that:
|
|
168
|
+
*
|
|
169
|
+
* 1. The agent loop never tries to execute the call client-side (see
|
|
170
|
+
* {@link isProviderExecutedToolCall} usage in the chat engine), and
|
|
171
|
+
* 2. The adapter can stash the raw provider result alongside it so the call —
|
|
172
|
+
* and its evidence — round-trips into the next turn's request.
|
|
173
|
+
*
|
|
174
|
+
* Provider-specific payloads live under a namespaced key (e.g. `anthropic`),
|
|
175
|
+
* keeping this convention opaque to the framework core. The index signature
|
|
176
|
+
* preserves those per-adapter fields.
|
|
177
|
+
*/
|
|
178
|
+
export interface ProviderExecutedToolMetadata {
|
|
179
|
+
providerExecuted?: boolean
|
|
180
|
+
[key: string]: unknown
|
|
181
|
+
}
|
|
182
|
+
|
|
163
183
|
// ============================================================================
|
|
164
184
|
// Multimodal Content Types
|
|
165
185
|
// ============================================================================
|
|
@@ -362,7 +382,9 @@ export interface ToolCallPart<TMetadata = unknown> {
|
|
|
362
382
|
/** Tool execution output (for client tools or after approval) */
|
|
363
383
|
output?: any
|
|
364
384
|
/** Provider-specific metadata that round-trips with the tool call.
|
|
365
|
-
* Typed per-adapter via `TToolCallMetadata`.
|
|
385
|
+
* Typed per-adapter via `TToolCallMetadata`. May follow the
|
|
386
|
+
* {@link ProviderExecutedToolMetadata} convention to mark provider-executed
|
|
387
|
+
* server tools (e.g. Anthropic `web_search`). */
|
|
366
388
|
metadata?: TMetadata
|
|
367
389
|
}
|
|
368
390
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ProviderExecutedToolMetadata } from '../types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Narrow a tool call's opaque `metadata` to the provider-executed convention.
|
|
5
|
+
* Returns the typed metadata when the call is provider-executed, else `null`.
|
|
6
|
+
*
|
|
7
|
+
* @see ProviderExecutedToolMetadata
|
|
8
|
+
*/
|
|
9
|
+
export function getProviderExecutedMetadata(
|
|
10
|
+
toolCall: { metadata?: unknown } | null | undefined,
|
|
11
|
+
): ProviderExecutedToolMetadata | null {
|
|
12
|
+
const metadata = toolCall?.metadata
|
|
13
|
+
if (
|
|
14
|
+
typeof metadata === 'object' &&
|
|
15
|
+
metadata !== null &&
|
|
16
|
+
(metadata as ProviderExecutedToolMetadata).providerExecuted === true
|
|
17
|
+
) {
|
|
18
|
+
return metadata as ProviderExecutedToolMetadata
|
|
19
|
+
}
|
|
20
|
+
return null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* True when a tool call was executed by the provider (e.g. Anthropic
|
|
25
|
+
* `web_search` / `web_fetch` server tools) rather than the agent loop. Such
|
|
26
|
+
* calls must not be routed to client-side execution and are already "complete".
|
|
27
|
+
*/
|
|
28
|
+
export function isProviderExecutedToolCall(
|
|
29
|
+
toolCall: { metadata?: unknown } | null | undefined,
|
|
30
|
+
): boolean {
|
|
31
|
+
return getProviderExecutedMetadata(toolCall) !== null
|
|
32
|
+
}
|