@anthropic-ai/sdk 0.125.0 → 0.126.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/CHANGELOG.md +50 -0
- package/client.d.mts +2 -2
- package/client.d.mts.map +1 -1
- package/client.d.ts +2 -2
- package/client.d.ts.map +1 -1
- package/client.js +846 -840
- package/client.js.map +1 -1
- package/client.mjs +846 -834
- package/client.mjs.map +1 -1
- package/core/api-promise.js +71 -68
- package/core/api-promise.js.map +1 -1
- package/core/api-promise.mjs +71 -64
- package/core/api-promise.mjs.map +1 -1
- package/core/error.js +5 -2
- package/core/error.js.map +1 -1
- package/core/error.mjs +5 -2
- package/core/error.mjs.map +1 -1
- package/core/pagination.js +56 -50
- package/core/pagination.js.map +1 -1
- package/core/pagination.mjs +56 -41
- package/core/pagination.mjs.map +1 -1
- package/core/streaming.js +212 -209
- package/core/streaming.js.map +1 -1
- package/core/streaming.mjs +212 -209
- package/core/streaming.mjs.map +1 -1
- package/helpers/beta/mcp.js +14 -19
- package/helpers/beta/mcp.js.map +1 -1
- package/helpers/beta/mcp.mjs +14 -13
- package/helpers/beta/mcp.mjs.map +1 -1
- package/internal/constants.js +1 -1
- package/internal/constants.js.map +1 -1
- package/internal/decoders/jsonl.js +27 -24
- package/internal/decoders/jsonl.js.map +1 -1
- package/internal/decoders/jsonl.mjs +27 -24
- package/internal/decoders/jsonl.mjs.map +1 -1
- package/internal/decoders/line.js +52 -49
- package/internal/decoders/line.js.map +1 -1
- package/internal/decoders/line.mjs +52 -43
- package/internal/decoders/line.mjs.map +1 -1
- package/internal/file-store.js +256 -258
- package/internal/file-store.js.map +1 -1
- package/internal/file-store.mjs +256 -243
- package/internal/file-store.mjs.map +1 -1
- package/internal/node.browser.d.mts.map +1 -1
- package/internal/node.browser.d.ts.map +1 -1
- package/internal/node.browser.js.map +1 -1
- package/internal/node.browser.mjs +7 -7
- package/internal/node.browser.mjs.map +1 -1
- package/internal/shim-types.js +0 -6
- package/internal/shim-types.js.map +1 -1
- package/internal/shim-types.mjs +0 -6
- package/internal/shim-types.mjs.map +1 -1
- package/internal/shims.js +0 -6
- package/internal/shims.js.map +1 -1
- package/internal/shims.mjs +0 -6
- package/internal/shims.mjs.map +1 -1
- package/internal/stainless-helper-header.js +6 -6
- package/internal/stainless-helper-header.js.map +1 -1
- package/internal/utils/async-queue.js +66 -63
- package/internal/utils/async-queue.js.map +1 -1
- package/internal/utils/async-queue.mjs +66 -56
- package/internal/utils/async-queue.mjs.map +1 -1
- package/internal/utils/bytes.d.mts +1 -1
- package/internal/utils/bytes.d.mts.map +1 -1
- package/internal/utils/bytes.d.ts +1 -1
- package/internal/utils/bytes.d.ts.map +1 -1
- package/lib/BetaMessageStream.js +593 -590
- package/lib/BetaMessageStream.js.map +1 -1
- package/lib/BetaMessageStream.mjs +593 -590
- package/lib/BetaMessageStream.mjs.map +1 -1
- package/lib/MessageStream.js +544 -541
- package/lib/MessageStream.js.map +1 -1
- package/lib/MessageStream.mjs +544 -541
- package/lib/MessageStream.mjs.map +1 -1
- package/lib/environments/poller.js +180 -174
- package/lib/environments/poller.js.map +1 -1
- package/lib/environments/poller.mjs +180 -153
- package/lib/environments/poller.mjs.map +1 -1
- package/lib/environments/worker.js +375 -369
- package/lib/environments/worker.js.map +1 -1
- package/lib/environments/worker.mjs +374 -329
- package/lib/environments/worker.mjs.map +1 -1
- package/lib/tools/BetaToolRunner.js +316 -313
- package/lib/tools/BetaToolRunner.js.map +1 -1
- package/lib/tools/BetaToolRunner.mjs +315 -306
- package/lib/tools/BetaToolRunner.mjs.map +1 -1
- package/lib/tools/SessionToolRunner.js +241 -235
- package/lib/tools/SessionToolRunner.js.map +1 -1
- package/lib/tools/SessionToolRunner.mjs +241 -192
- package/lib/tools/SessionToolRunner.mjs.map +1 -1
- package/lib/tools/ToolError.js +39 -36
- package/lib/tools/ToolError.js.map +1 -1
- package/lib/tools/ToolError.mjs +39 -13
- package/lib/tools/ToolError.mjs.map +1 -1
- package/package.json +3 -3
- package/resources/beta/agents/agents.js +135 -132
- package/resources/beta/agents/agents.js.map +1 -1
- package/resources/beta/agents/agents.mjs +135 -132
- package/resources/beta/agents/agents.mjs.map +1 -1
- package/resources/beta/beta.d.mts +5 -5
- package/resources/beta/beta.d.mts.map +1 -1
- package/resources/beta/beta.d.ts +5 -5
- package/resources/beta/beta.d.ts.map +1 -1
- package/resources/beta/beta.js +39 -36
- package/resources/beta/beta.js.map +1 -1
- package/resources/beta/beta.mjs +39 -36
- package/resources/beta/beta.mjs.map +1 -1
- package/resources/beta/dreams.d.mts +2 -28
- package/resources/beta/dreams.d.mts.map +1 -1
- package/resources/beta/dreams.d.ts +2 -28
- package/resources/beta/dreams.d.ts.map +1 -1
- package/resources/beta/environments/environments.js +157 -154
- package/resources/beta/environments/environments.js.map +1 -1
- package/resources/beta/environments/environments.mjs +157 -154
- package/resources/beta/environments/environments.mjs.map +1 -1
- package/resources/beta/environments/work.js +255 -252
- package/resources/beta/environments/work.js.map +1 -1
- package/resources/beta/environments/work.mjs +255 -252
- package/resources/beta/environments/work.mjs.map +1 -1
- package/resources/beta/index.d.mts +2 -2
- package/resources/beta/index.d.mts.map +1 -1
- package/resources/beta/index.d.ts +2 -2
- package/resources/beta/index.d.ts.map +1 -1
- package/resources/beta/index.js.map +1 -1
- package/resources/beta/index.mjs.map +1 -1
- package/resources/beta/memory-stores/memory-stores.js +150 -147
- package/resources/beta/memory-stores/memory-stores.js.map +1 -1
- package/resources/beta/memory-stores/memory-stores.mjs +150 -147
- package/resources/beta/memory-stores/memory-stores.mjs.map +1 -1
- package/resources/beta/messages/batches.d.mts +12 -1
- package/resources/beta/messages/batches.d.mts.map +1 -1
- package/resources/beta/messages/batches.d.ts +12 -1
- package/resources/beta/messages/batches.d.ts.map +1 -1
- package/resources/beta/messages/index.d.mts +1 -1
- package/resources/beta/messages/index.d.mts.map +1 -1
- package/resources/beta/messages/index.d.ts +1 -1
- package/resources/beta/messages/index.d.ts.map +1 -1
- package/resources/beta/messages/index.js.map +1 -1
- package/resources/beta/messages/index.mjs.map +1 -1
- package/resources/beta/messages/messages.d.mts +238 -100
- package/resources/beta/messages/messages.d.mts.map +1 -1
- package/resources/beta/messages/messages.d.ts +238 -100
- package/resources/beta/messages/messages.d.ts.map +1 -1
- package/resources/beta/messages/messages.js +113 -110
- package/resources/beta/messages/messages.js.map +1 -1
- package/resources/beta/messages/messages.mjs +113 -110
- package/resources/beta/messages/messages.mjs.map +1 -1
- package/resources/beta/models.d.mts +22 -1
- package/resources/beta/models.d.mts.map +1 -1
- package/resources/beta/models.d.ts +22 -1
- package/resources/beta/models.d.ts.map +1 -1
- package/resources/beta/organization/federation/federation.js +11 -8
- package/resources/beta/organization/federation/federation.js.map +1 -1
- package/resources/beta/organization/federation/federation.mjs +11 -8
- package/resources/beta/organization/federation/federation.mjs.map +1 -1
- package/resources/beta/organization/federation/rules/rules.js +187 -184
- package/resources/beta/organization/federation/rules/rules.js.map +1 -1
- package/resources/beta/organization/federation/rules/rules.mjs +187 -184
- package/resources/beta/organization/federation/rules/rules.mjs.map +1 -1
- package/resources/beta/organization/organization.js +38 -35
- package/resources/beta/organization/organization.js.map +1 -1
- package/resources/beta/organization/organization.mjs +38 -35
- package/resources/beta/organization/organization.mjs.map +1 -1
- package/resources/beta/organization/service-accounts/service-accounts.js +163 -160
- package/resources/beta/organization/service-accounts/service-accounts.js.map +1 -1
- package/resources/beta/organization/service-accounts/service-accounts.mjs +163 -160
- package/resources/beta/organization/service-accounts/service-accounts.mjs.map +1 -1
- package/resources/beta/organization/workspaces/workspaces.d.mts +3 -3
- package/resources/beta/organization/workspaces/workspaces.d.mts.map +1 -1
- package/resources/beta/organization/workspaces/workspaces.d.ts +3 -3
- package/resources/beta/organization/workspaces/workspaces.d.ts.map +1 -1
- package/resources/beta/organization/workspaces/workspaces.js +97 -94
- package/resources/beta/organization/workspaces/workspaces.js.map +1 -1
- package/resources/beta/organization/workspaces/workspaces.mjs +97 -94
- package/resources/beta/organization/workspaces/workspaces.mjs.map +1 -1
- package/resources/beta/sessions/events.d.mts +12 -21
- package/resources/beta/sessions/events.d.mts.map +1 -1
- package/resources/beta/sessions/events.d.ts +12 -21
- package/resources/beta/sessions/events.d.ts.map +1 -1
- package/resources/beta/sessions/events.js +118 -115
- package/resources/beta/sessions/events.js.map +1 -1
- package/resources/beta/sessions/events.mjs +118 -115
- package/resources/beta/sessions/events.mjs.map +1 -1
- package/resources/beta/sessions/resources.d.mts +0 -3
- package/resources/beta/sessions/resources.d.mts.map +1 -1
- package/resources/beta/sessions/resources.d.ts +0 -3
- package/resources/beta/sessions/resources.d.ts.map +1 -1
- package/resources/beta/sessions/sessions.d.mts +2 -2
- package/resources/beta/sessions/sessions.d.ts +2 -2
- package/resources/beta/sessions/sessions.js +161 -158
- package/resources/beta/sessions/sessions.js.map +1 -1
- package/resources/beta/sessions/sessions.mjs +161 -158
- package/resources/beta/sessions/sessions.mjs.map +1 -1
- package/resources/beta/sessions/threads/threads.js +86 -83
- package/resources/beta/sessions/threads/threads.js.map +1 -1
- package/resources/beta/sessions/threads/threads.mjs +86 -83
- package/resources/beta/sessions/threads/threads.mjs.map +1 -1
- package/resources/beta/skills/skills.js +104 -101
- package/resources/beta/skills/skills.js.map +1 -1
- package/resources/beta/skills/skills.mjs +104 -101
- package/resources/beta/skills/skills.mjs.map +1 -1
- package/resources/beta/tunnels/tunnels.js +188 -185
- package/resources/beta/tunnels/tunnels.js.map +1 -1
- package/resources/beta/tunnels/tunnels.mjs +188 -185
- package/resources/beta/tunnels/tunnels.mjs.map +1 -1
- package/resources/beta/user-profiles.d.mts +45 -0
- package/resources/beta/user-profiles.d.mts.map +1 -1
- package/resources/beta/user-profiles.d.ts +45 -0
- package/resources/beta/user-profiles.d.ts.map +1 -1
- package/resources/beta/user-profiles.js +25 -10
- package/resources/beta/user-profiles.js.map +1 -1
- package/resources/beta/user-profiles.mjs +25 -10
- package/resources/beta/user-profiles.mjs.map +1 -1
- package/resources/beta/vaults/credentials.d.mts +0 -14
- package/resources/beta/vaults/credentials.d.mts.map +1 -1
- package/resources/beta/vaults/credentials.d.ts +0 -14
- package/resources/beta/vaults/credentials.d.ts.map +1 -1
- package/resources/beta/vaults/vaults.js +156 -153
- package/resources/beta/vaults/vaults.js.map +1 -1
- package/resources/beta/vaults/vaults.mjs +156 -153
- package/resources/beta/vaults/vaults.mjs.map +1 -1
- package/resources/completions.d.mts +1 -1
- package/resources/completions.d.ts +1 -1
- package/resources/index.d.mts +1 -1
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +1 -1
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs.map +1 -1
- package/resources/messages/index.d.mts +1 -1
- package/resources/messages/index.d.mts.map +1 -1
- package/resources/messages/index.d.ts +1 -1
- package/resources/messages/index.d.ts.map +1 -1
- package/resources/messages/index.js.map +1 -1
- package/resources/messages/index.mjs.map +1 -1
- package/resources/messages/messages.d.mts +100 -63
- package/resources/messages/messages.d.mts.map +1 -1
- package/resources/messages/messages.d.ts +100 -63
- package/resources/messages/messages.d.ts.map +1 -1
- package/resources/messages/messages.js +115 -112
- package/resources/messages/messages.js.map +1 -1
- package/resources/messages/messages.mjs +115 -112
- package/resources/messages/messages.mjs.map +1 -1
- package/resources/skills/skills.js +88 -85
- package/resources/skills/skills.js.map +1 -1
- package/resources/skills/skills.mjs +88 -85
- package/resources/skills/skills.mjs.map +1 -1
- package/resources/top-level.js +0 -1
- package/resources/top-level.js.map +1 -1
- package/resources/top-level.mjs +0 -1
- package/resources/top-level.mjs.map +1 -1
- package/src/client.ts +13 -1
- package/src/internal/node.browser.ts +7 -7
- package/src/internal/utils/bytes.ts +1 -1
- package/src/resources/beta/beta.ts +24 -1
- package/src/resources/beta/dreams.ts +2 -28
- package/src/resources/beta/index.ts +11 -0
- package/src/resources/beta/messages/batches.ts +13 -1
- package/src/resources/beta/messages/index.ts +10 -0
- package/src/resources/beta/messages/messages.ts +288 -99
- package/src/resources/beta/models.ts +25 -0
- package/src/resources/beta/organization/workspaces/workspaces.ts +3 -3
- package/src/resources/beta/sessions/events.ts +12 -21
- package/src/resources/beta/sessions/resources.ts +0 -3
- package/src/resources/beta/sessions/sessions.ts +2 -2
- package/src/resources/beta/user-profiles.ts +75 -10
- package/src/resources/beta/vaults/credentials.ts +0 -14
- package/src/resources/completions.ts +1 -1
- package/src/resources/index.ts +6 -0
- package/src/resources/messages/index.ts +6 -0
- package/src/resources/messages/messages.ts +128 -62
- package/src/tsconfig.json +13 -3
- package/src/version.ts +1 -1
- package/tools/agent-toolset/fs-util.js +0 -6
- package/tools/agent-toolset/fs-util.js.map +1 -1
- package/tools/agent-toolset/fs-util.mjs +0 -6
- package/tools/agent-toolset/fs-util.mjs.map +1 -1
- package/tools/agent-toolset/memories.js +4 -4
- package/tools/agent-toolset/memories.js.map +1 -1
- package/tools/agent-toolset/memories.mjs +2 -2
- package/tools/agent-toolset/memories.mjs.map +1 -1
- package/tools/agent-toolset/node.browser.js +12 -12
- package/tools/agent-toolset/node.browser.js.map +1 -1
- package/tools/agent-toolset/node.js +142 -139
- package/tools/agent-toolset/node.js.map +1 -1
- package/tools/agent-toolset/node.mjs +132 -125
- package/tools/agent-toolset/node.mjs.map +1 -1
- package/tools/agent-toolset/skills.js +5 -5
- package/tools/agent-toolset/skills.js.map +1 -1
- package/tools/agent-toolset/sync-interval.js +3 -3
- package/tools/agent-toolset/sync-interval.js.map +1 -1
- package/tools/memory/node.browser.js +2 -2
- package/tools/memory/node.browser.js.map +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -13,343 +13,352 @@ import { collectStainlessHelpers, helperHeader, STAINLESS_HELPER_HEADER, } from
|
|
|
13
13
|
* A ToolRunner is an async iterable that yields either BetaMessage or BetaMessageStream objects
|
|
14
14
|
* depending on the streaming configuration.
|
|
15
15
|
*/
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
let tokensUsed = 0;
|
|
69
|
-
if (__classPrivateFieldGet(this, _BetaToolRunner_message, "f") !== undefined) {
|
|
70
|
-
try {
|
|
71
|
-
const message = await __classPrivateFieldGet(this, _BetaToolRunner_message, "f");
|
|
72
|
-
const totalInputTokens = message.usage.input_tokens +
|
|
73
|
-
(message.usage.cache_creation_input_tokens ?? 0) +
|
|
74
|
-
(message.usage.cache_read_input_tokens ?? 0);
|
|
75
|
-
tokensUsed = totalInputTokens + message.usage.output_tokens;
|
|
16
|
+
export var BetaToolRunner = /* @__PURE__ */ (() => {
|
|
17
|
+
/**
|
|
18
|
+
* A ToolRunner handles the automatic conversation loop between the assistant and tools.
|
|
19
|
+
*
|
|
20
|
+
* A ToolRunner is an async iterable that yields either BetaMessage or BetaMessageStream objects
|
|
21
|
+
* depending on the streaming configuration.
|
|
22
|
+
*/
|
|
23
|
+
class BetaToolRunner {
|
|
24
|
+
constructor(client, params, options) {
|
|
25
|
+
_BetaToolRunner_instances.add(this);
|
|
26
|
+
this.client = client;
|
|
27
|
+
/** Whether the async iterator has been consumed */
|
|
28
|
+
_BetaToolRunner_consumed.set(this, false);
|
|
29
|
+
/** Whether parameters have been mutated since the last API call */
|
|
30
|
+
_BetaToolRunner_mutated.set(this, false);
|
|
31
|
+
/** Current state containing the request parameters */
|
|
32
|
+
_BetaToolRunner_state.set(this, void 0);
|
|
33
|
+
_BetaToolRunner_options.set(this, void 0);
|
|
34
|
+
/** Promise for the last message received from the assistant */
|
|
35
|
+
_BetaToolRunner_message.set(this, void 0);
|
|
36
|
+
/** Cached tool response to avoid redundant executions */
|
|
37
|
+
_BetaToolRunner_toolResponse.set(this, void 0);
|
|
38
|
+
/** Promise resolvers for waiting on completion */
|
|
39
|
+
_BetaToolRunner_completion.set(this, void 0);
|
|
40
|
+
/** Number of iterations (API requests) made so far */
|
|
41
|
+
_BetaToolRunner_iterationCount.set(this, 0);
|
|
42
|
+
__classPrivateFieldSet(this, _BetaToolRunner_state, {
|
|
43
|
+
params: {
|
|
44
|
+
// You can't clone the entire params since there are functions as handlers.
|
|
45
|
+
// You also don't really need to clone params.messages, but it probably will prevent a foot gun
|
|
46
|
+
// somewhere.
|
|
47
|
+
...params,
|
|
48
|
+
messages: structuredClone(params.messages),
|
|
49
|
+
},
|
|
50
|
+
}, "f");
|
|
51
|
+
// structuredClone drops symbol-keyed properties, so collect helper marks
|
|
52
|
+
// from the original params here — the create()-side collector won't see
|
|
53
|
+
// them on the cloned messages.
|
|
54
|
+
const collected = collectStainlessHelpers(params.tools, params.messages);
|
|
55
|
+
__classPrivateFieldSet(this, _BetaToolRunner_options, {
|
|
56
|
+
...options,
|
|
57
|
+
headers: buildHeaders([
|
|
58
|
+
helperHeader('BetaToolRunner'),
|
|
59
|
+
collected.length ? { [STAINLESS_HELPER_HEADER]: collected.join(', ') } : undefined,
|
|
60
|
+
options?.headers,
|
|
61
|
+
]),
|
|
62
|
+
}, "f");
|
|
63
|
+
__classPrivateFieldSet(this, _BetaToolRunner_completion, promiseWithResolvers(), "f");
|
|
64
|
+
if (params.compactionControl?.enabled) {
|
|
65
|
+
console.warn('Anthropic: The `compactionControl` parameter is deprecated and will be removed in a future version. ' +
|
|
66
|
+
'Use server-side compaction instead by passing `edits: [{ type: "compact_20260112" }]` in the params passed to `toolRunner()`. ' +
|
|
67
|
+
'See https://platform.claude.com/docs/en/build-with-claude/compaction');
|
|
76
68
|
}
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
}
|
|
70
|
+
async *[(_BetaToolRunner_consumed = new WeakMap(), _BetaToolRunner_mutated = new WeakMap(), _BetaToolRunner_state = new WeakMap(), _BetaToolRunner_options = new WeakMap(), _BetaToolRunner_message = new WeakMap(), _BetaToolRunner_toolResponse = new WeakMap(), _BetaToolRunner_completion = new WeakMap(), _BetaToolRunner_iterationCount = new WeakMap(), _BetaToolRunner_instances = new WeakSet(), _BetaToolRunner_checkAndCompact = async function _BetaToolRunner_checkAndCompact() {
|
|
71
|
+
const compactionControl = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.compactionControl;
|
|
72
|
+
if (!compactionControl || !compactionControl.enabled) {
|
|
79
73
|
return false;
|
|
80
74
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (messages[messages.length - 1].role === 'assistant') {
|
|
90
|
-
// Remove tool_use blocks from the last message to avoid 400 error
|
|
91
|
-
// (tool_use requires tool_result, which we don't have yet)
|
|
92
|
-
const lastMessage = messages[messages.length - 1];
|
|
93
|
-
if (Array.isArray(lastMessage.content)) {
|
|
94
|
-
const nonToolBlocks = lastMessage.content.filter((block) => block.type !== 'tool_use');
|
|
95
|
-
if (nonToolBlocks.length === 0) {
|
|
96
|
-
// If all blocks were tool_use, just remove the message entirely
|
|
97
|
-
messages.pop();
|
|
75
|
+
let tokensUsed = 0;
|
|
76
|
+
if (__classPrivateFieldGet(this, _BetaToolRunner_message, "f") !== undefined) {
|
|
77
|
+
try {
|
|
78
|
+
const message = await __classPrivateFieldGet(this, _BetaToolRunner_message, "f");
|
|
79
|
+
const totalInputTokens = message.usage.input_tokens +
|
|
80
|
+
(message.usage.cache_creation_input_tokens ?? 0) +
|
|
81
|
+
(message.usage.cache_read_input_tokens ?? 0);
|
|
82
|
+
tokensUsed = totalInputTokens + message.usage.output_tokens;
|
|
98
83
|
}
|
|
99
|
-
|
|
100
|
-
|
|
84
|
+
catch {
|
|
85
|
+
// If we can't get the message, skip compaction
|
|
86
|
+
return false;
|
|
101
87
|
}
|
|
102
88
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}, {
|
|
120
|
-
signal: __classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal,
|
|
121
|
-
headers: buildHeaders([__classPrivateFieldGet(this, _BetaToolRunner_options, "f").headers, helperHeader('compaction')]),
|
|
122
|
-
});
|
|
123
|
-
if (response.content[0]?.type !== 'text') {
|
|
124
|
-
throw new AnthropicError('Expected text response for compaction');
|
|
125
|
-
}
|
|
126
|
-
__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages = [
|
|
127
|
-
{
|
|
128
|
-
role: 'user',
|
|
129
|
-
content: response.content,
|
|
130
|
-
},
|
|
131
|
-
];
|
|
132
|
-
return true;
|
|
133
|
-
}, Symbol.asyncIterator)]() {
|
|
134
|
-
var _a;
|
|
135
|
-
if (__classPrivateFieldGet(this, _BetaToolRunner_consumed, "f")) {
|
|
136
|
-
throw new AnthropicError('Cannot iterate over a consumed stream');
|
|
137
|
-
}
|
|
138
|
-
__classPrivateFieldSet(this, _BetaToolRunner_consumed, true, "f");
|
|
139
|
-
__classPrivateFieldSet(this, _BetaToolRunner_mutated, true, "f");
|
|
140
|
-
__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
|
|
141
|
-
try {
|
|
142
|
-
while (true) {
|
|
143
|
-
let stream;
|
|
144
|
-
try {
|
|
145
|
-
if (__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_iterations &&
|
|
146
|
-
__classPrivateFieldGet(this, _BetaToolRunner_iterationCount, "f") >= __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_iterations) {
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
__classPrivateFieldSet(this, _BetaToolRunner_mutated, false, "f");
|
|
150
|
-
__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
|
|
151
|
-
__classPrivateFieldSet(this, _BetaToolRunner_iterationCount, (_a = __classPrivateFieldGet(this, _BetaToolRunner_iterationCount, "f"), _a++, _a), "f");
|
|
152
|
-
__classPrivateFieldSet(this, _BetaToolRunner_message, undefined, "f");
|
|
153
|
-
const { max_iterations, compactionControl, ...params } = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params;
|
|
154
|
-
if (params.stream) {
|
|
155
|
-
stream = this.client.beta.messages.stream({ ...params }, __classPrivateFieldGet(this, _BetaToolRunner_options, "f"));
|
|
156
|
-
__classPrivateFieldSet(this, _BetaToolRunner_message, stream.finalMessage(), "f");
|
|
157
|
-
// Make sure that this promise doesn't throw before we get the option to do something about it.
|
|
158
|
-
// Error will be caught when we call await this.#message ultimately
|
|
159
|
-
__classPrivateFieldGet(this, _BetaToolRunner_message, "f").catch(() => { });
|
|
160
|
-
yield stream;
|
|
89
|
+
const threshold = compactionControl.contextTokenThreshold ?? DEFAULT_TOKEN_THRESHOLD;
|
|
90
|
+
if (tokensUsed < threshold) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
const model = compactionControl.model ?? __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.model;
|
|
94
|
+
const summaryPrompt = compactionControl.summaryPrompt ?? DEFAULT_SUMMARY_PROMPT;
|
|
95
|
+
const messages = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages;
|
|
96
|
+
if (messages[messages.length - 1].role === 'assistant') {
|
|
97
|
+
// Remove tool_use blocks from the last message to avoid 400 error
|
|
98
|
+
// (tool_use requires tool_result, which we don't have yet)
|
|
99
|
+
const lastMessage = messages[messages.length - 1];
|
|
100
|
+
if (Array.isArray(lastMessage.content)) {
|
|
101
|
+
const nonToolBlocks = lastMessage.content.filter((block) => block.type !== 'tool_use');
|
|
102
|
+
if (nonToolBlocks.length === 0) {
|
|
103
|
+
// If all blocks were tool_use, just remove the message entirely
|
|
104
|
+
messages.pop();
|
|
161
105
|
}
|
|
162
106
|
else {
|
|
163
|
-
|
|
164
|
-
yield __classPrivateFieldGet(this, _BetaToolRunner_message, "f");
|
|
107
|
+
lastMessage.content = nonToolBlocks;
|
|
165
108
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const response = await this.client.beta.messages.create({
|
|
112
|
+
model,
|
|
113
|
+
messages: [
|
|
114
|
+
...messages,
|
|
115
|
+
{
|
|
116
|
+
role: 'user',
|
|
117
|
+
content: [
|
|
118
|
+
{
|
|
119
|
+
type: 'text',
|
|
120
|
+
text: summaryPrompt,
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
max_tokens: __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_tokens,
|
|
126
|
+
}, {
|
|
127
|
+
signal: __classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal,
|
|
128
|
+
headers: buildHeaders([__classPrivateFieldGet(this, _BetaToolRunner_options, "f").headers, helperHeader('compaction')]),
|
|
129
|
+
});
|
|
130
|
+
if (response.content[0]?.type !== 'text') {
|
|
131
|
+
throw new AnthropicError('Expected text response for compaction');
|
|
132
|
+
}
|
|
133
|
+
__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages = [
|
|
134
|
+
{
|
|
135
|
+
role: 'user',
|
|
136
|
+
content: response.content,
|
|
137
|
+
},
|
|
138
|
+
];
|
|
139
|
+
return true;
|
|
140
|
+
}, Symbol.asyncIterator)]() {
|
|
141
|
+
var _a;
|
|
142
|
+
if (__classPrivateFieldGet(this, _BetaToolRunner_consumed, "f")) {
|
|
143
|
+
throw new AnthropicError('Cannot iterate over a consumed stream');
|
|
144
|
+
}
|
|
145
|
+
__classPrivateFieldSet(this, _BetaToolRunner_consumed, true, "f");
|
|
146
|
+
__classPrivateFieldSet(this, _BetaToolRunner_mutated, true, "f");
|
|
147
|
+
__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
|
|
148
|
+
try {
|
|
149
|
+
while (true) {
|
|
150
|
+
let stream;
|
|
151
|
+
try {
|
|
152
|
+
if (__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_iterations &&
|
|
153
|
+
__classPrivateFieldGet(this, _BetaToolRunner_iterationCount, "f") >= __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_iterations) {
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
__classPrivateFieldSet(this, _BetaToolRunner_mutated, false, "f");
|
|
157
|
+
__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
|
|
158
|
+
__classPrivateFieldSet(this, _BetaToolRunner_iterationCount, (_a = __classPrivateFieldGet(this, _BetaToolRunner_iterationCount, "f"), _a++, _a), "f");
|
|
159
|
+
__classPrivateFieldSet(this, _BetaToolRunner_message, undefined, "f");
|
|
160
|
+
const { max_iterations, compactionControl, ...params } = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params;
|
|
161
|
+
if (params.stream) {
|
|
162
|
+
stream = this.client.beta.messages.stream({ ...params }, __classPrivateFieldGet(this, _BetaToolRunner_options, "f"));
|
|
163
|
+
__classPrivateFieldSet(this, _BetaToolRunner_message, stream.finalMessage(), "f");
|
|
164
|
+
// Make sure that this promise doesn't throw before we get the option to do something about it.
|
|
165
|
+
// Error will be caught when we call await this.#message ultimately
|
|
166
|
+
__classPrivateFieldGet(this, _BetaToolRunner_message, "f").catch(() => { });
|
|
167
|
+
yield stream;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
__classPrivateFieldSet(this, _BetaToolRunner_message, this.client.beta.messages.create({ ...params, stream: false }, __classPrivateFieldGet(this, _BetaToolRunner_options, "f")), "f");
|
|
171
|
+
yield __classPrivateFieldGet(this, _BetaToolRunner_message, "f");
|
|
172
|
+
}
|
|
173
|
+
const isCompacted = await __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_checkAndCompact).call(this);
|
|
174
|
+
if (!isCompacted) {
|
|
175
|
+
if (!__classPrivateFieldGet(this, _BetaToolRunner_mutated, "f")) {
|
|
176
|
+
const message = await __classPrivateFieldGet(this, _BetaToolRunner_message, "f");
|
|
177
|
+
const nextStep = determineNextStepFromStopReason(message.stop_reason);
|
|
178
|
+
__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.push({ role: message.role, content: message.content });
|
|
179
|
+
// Container-bound server tools reject a follow-up request that omits the container the
|
|
180
|
+
// previous turn ran in, so carry its id forward unless the caller pinned one themselves.
|
|
181
|
+
const { container } = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params;
|
|
182
|
+
if (message.container) {
|
|
183
|
+
if (container == null) {
|
|
184
|
+
__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.container = message.container.id;
|
|
185
|
+
}
|
|
186
|
+
else if (typeof container === 'object' && container.id == null) {
|
|
187
|
+
__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.container = { ...container, id: message.container.id };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (nextStep === 'stop') {
|
|
191
|
+
break;
|
|
178
192
|
}
|
|
179
|
-
|
|
180
|
-
|
|
193
|
+
if (nextStep === 'resume') {
|
|
194
|
+
continue;
|
|
181
195
|
}
|
|
182
196
|
}
|
|
183
|
-
|
|
184
|
-
|
|
197
|
+
const toolMessage = await __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_generateToolResponse).call(this, __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.at(-1));
|
|
198
|
+
if (toolMessage) {
|
|
199
|
+
__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.push(toolMessage);
|
|
185
200
|
}
|
|
186
|
-
if (
|
|
187
|
-
|
|
201
|
+
else if (!__classPrivateFieldGet(this, _BetaToolRunner_mutated, "f")) {
|
|
202
|
+
break;
|
|
188
203
|
}
|
|
189
204
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
else if (!__classPrivateFieldGet(this, _BetaToolRunner_mutated, "f")) {
|
|
195
|
-
break;
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
if (stream) {
|
|
208
|
+
stream.abort();
|
|
196
209
|
}
|
|
197
210
|
}
|
|
198
211
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
stream.abort();
|
|
202
|
-
}
|
|
212
|
+
if (!__classPrivateFieldGet(this, _BetaToolRunner_message, "f")) {
|
|
213
|
+
throw new AnthropicError('ToolRunner concluded without a message from the server');
|
|
203
214
|
}
|
|
215
|
+
__classPrivateFieldGet(this, _BetaToolRunner_completion, "f").resolve(await __classPrivateFieldGet(this, _BetaToolRunner_message, "f"));
|
|
204
216
|
}
|
|
205
|
-
|
|
206
|
-
|
|
217
|
+
catch (error) {
|
|
218
|
+
__classPrivateFieldSet(this, _BetaToolRunner_consumed, false, "f");
|
|
219
|
+
// Silence unhandled promise errors
|
|
220
|
+
__classPrivateFieldGet(this, _BetaToolRunner_completion, "f").promise.catch(() => { });
|
|
221
|
+
__classPrivateFieldGet(this, _BetaToolRunner_completion, "f").reject(error);
|
|
222
|
+
__classPrivateFieldSet(this, _BetaToolRunner_completion, promiseWithResolvers(), "f");
|
|
223
|
+
throw error;
|
|
207
224
|
}
|
|
208
|
-
__classPrivateFieldGet(this, _BetaToolRunner_completion, "f").resolve(await __classPrivateFieldGet(this, _BetaToolRunner_message, "f"));
|
|
209
225
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
226
|
+
setMessagesParams(paramsOrMutator) {
|
|
227
|
+
if (typeof paramsOrMutator === 'function') {
|
|
228
|
+
__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params = paramsOrMutator(__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params = paramsOrMutator;
|
|
232
|
+
}
|
|
233
|
+
__classPrivateFieldSet(this, _BetaToolRunner_mutated, true, "f");
|
|
234
|
+
// Invalidate cached tool response since parameters changed
|
|
235
|
+
__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
|
|
217
236
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
237
|
+
setRequestOptions(optionsOrMutator) {
|
|
238
|
+
if (typeof optionsOrMutator === 'function') {
|
|
239
|
+
__classPrivateFieldSet(this, _BetaToolRunner_options, optionsOrMutator(__classPrivateFieldGet(this, _BetaToolRunner_options, "f")), "f");
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
__classPrivateFieldSet(this, _BetaToolRunner_options, { ...__classPrivateFieldGet(this, _BetaToolRunner_options, "f"), ...optionsOrMutator }, "f");
|
|
243
|
+
}
|
|
222
244
|
}
|
|
223
|
-
|
|
224
|
-
|
|
245
|
+
/**
|
|
246
|
+
* Get the tool response for the last message from the assistant.
|
|
247
|
+
* Avoids redundant tool executions by caching results.
|
|
248
|
+
*
|
|
249
|
+
* @returns A promise that resolves to a BetaMessageParam containing tool results, or null if no tools need to be executed
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* const toolResponse = await runner.generateToolResponse();
|
|
253
|
+
* if (toolResponse) {
|
|
254
|
+
* console.log('Tool results:', toolResponse.content);
|
|
255
|
+
* }
|
|
256
|
+
*/
|
|
257
|
+
async generateToolResponse(signal = __classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal) {
|
|
258
|
+
const message = (await __classPrivateFieldGet(this, _BetaToolRunner_message, "f")) ?? this.params.messages.at(-1);
|
|
259
|
+
if (!message) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
return __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_generateToolResponse).call(this, message, signal);
|
|
225
263
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
264
|
+
/**
|
|
265
|
+
* Wait for the async iterator to complete. This works even if the async iterator hasn't yet started, and
|
|
266
|
+
* will wait for an instance to start and go to completion.
|
|
267
|
+
*
|
|
268
|
+
* @returns A promise that resolves to the final BetaMessage when the iterator completes
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* // Start consuming the iterator
|
|
272
|
+
* for await (const message of runner) {
|
|
273
|
+
* console.log('Message:', message.content);
|
|
274
|
+
* }
|
|
275
|
+
*
|
|
276
|
+
* // Meanwhile, wait for completion from another part of the code
|
|
277
|
+
* const finalMessage = await runner.done();
|
|
278
|
+
* console.log('Final response:', finalMessage.content);
|
|
279
|
+
*/
|
|
280
|
+
done() {
|
|
281
|
+
return __classPrivateFieldGet(this, _BetaToolRunner_completion, "f").promise;
|
|
233
282
|
}
|
|
234
|
-
|
|
235
|
-
|
|
283
|
+
/**
|
|
284
|
+
* Returns a promise indicating that the stream is done. Unlike .done(), this will eagerly read the stream:
|
|
285
|
+
* * If the iterator has not been consumed, consume the entire iterator and return the final message from the
|
|
286
|
+
* assistant.
|
|
287
|
+
* * If the iterator has been consumed, waits for it to complete and returns the final message.
|
|
288
|
+
*
|
|
289
|
+
* @returns A promise that resolves to the final BetaMessage from the conversation
|
|
290
|
+
* @throws {AnthropicError} If no messages were processed during the conversation
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* const finalMessage = await runner.runUntilDone();
|
|
294
|
+
* console.log('Final response:', finalMessage.content);
|
|
295
|
+
*/
|
|
296
|
+
async runUntilDone() {
|
|
297
|
+
// If not yet consumed, start consuming and wait for completion
|
|
298
|
+
if (!__classPrivateFieldGet(this, _BetaToolRunner_consumed, "f")) {
|
|
299
|
+
for await (const _ of this) {
|
|
300
|
+
// Iterator naturally populates this.#message
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// If consumed but not completed, wait for completion
|
|
304
|
+
return this.done();
|
|
236
305
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
*/
|
|
250
|
-
async generateToolResponse(signal = __classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal) {
|
|
251
|
-
const message = (await __classPrivateFieldGet(this, _BetaToolRunner_message, "f")) ?? this.params.messages.at(-1);
|
|
252
|
-
if (!message) {
|
|
253
|
-
return null;
|
|
306
|
+
/**
|
|
307
|
+
* Get the current parameters being used by the ToolRunner.
|
|
308
|
+
*
|
|
309
|
+
* @returns A readonly view of the current ToolRunnerParams
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* const currentParams = runner.params;
|
|
313
|
+
* console.log('Current model:', currentParams.model);
|
|
314
|
+
* console.log('Message count:', currentParams.messages.length);
|
|
315
|
+
*/
|
|
316
|
+
get params() {
|
|
317
|
+
return __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params;
|
|
254
318
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
*
|
|
285
|
-
* @example
|
|
286
|
-
* const finalMessage = await runner.runUntilDone();
|
|
287
|
-
* console.log('Final response:', finalMessage.content);
|
|
288
|
-
*/
|
|
289
|
-
async runUntilDone() {
|
|
290
|
-
// If not yet consumed, start consuming and wait for completion
|
|
291
|
-
if (!__classPrivateFieldGet(this, _BetaToolRunner_consumed, "f")) {
|
|
292
|
-
for await (const _ of this) {
|
|
293
|
-
// Iterator naturally populates this.#message
|
|
294
|
-
}
|
|
319
|
+
/**
|
|
320
|
+
* Add one or more messages to the conversation history.
|
|
321
|
+
*
|
|
322
|
+
* @param messages - One or more BetaMessageParam objects to add to the conversation
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* runner.pushMessages(
|
|
326
|
+
* { role: 'user', content: 'Also, what about the weather in NYC?' }
|
|
327
|
+
* );
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* // Adding multiple messages
|
|
331
|
+
* runner.pushMessages(
|
|
332
|
+
* { role: 'user', content: 'What about NYC?' },
|
|
333
|
+
* { role: 'user', content: 'And Boston?' }
|
|
334
|
+
* );
|
|
335
|
+
*/
|
|
336
|
+
pushMessages(...messages) {
|
|
337
|
+
this.setMessagesParams((params) => ({
|
|
338
|
+
...params,
|
|
339
|
+
messages: [...params.messages, ...messages],
|
|
340
|
+
}));
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Makes the ToolRunner directly awaitable, equivalent to calling .runUntilDone()
|
|
344
|
+
* This allows using `await runner` instead of `await runner.runUntilDone()`
|
|
345
|
+
*/
|
|
346
|
+
then(onfulfilled, onrejected) {
|
|
347
|
+
return this.runUntilDone().then(onfulfilled, onrejected);
|
|
295
348
|
}
|
|
296
|
-
// If consumed but not completed, wait for completion
|
|
297
|
-
return this.done();
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Get the current parameters being used by the ToolRunner.
|
|
301
|
-
*
|
|
302
|
-
* @returns A readonly view of the current ToolRunnerParams
|
|
303
|
-
*
|
|
304
|
-
* @example
|
|
305
|
-
* const currentParams = runner.params;
|
|
306
|
-
* console.log('Current model:', currentParams.model);
|
|
307
|
-
* console.log('Message count:', currentParams.messages.length);
|
|
308
|
-
*/
|
|
309
|
-
get params() {
|
|
310
|
-
return __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params;
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Add one or more messages to the conversation history.
|
|
314
|
-
*
|
|
315
|
-
* @param messages - One or more BetaMessageParam objects to add to the conversation
|
|
316
|
-
*
|
|
317
|
-
* @example
|
|
318
|
-
* runner.pushMessages(
|
|
319
|
-
* { role: 'user', content: 'Also, what about the weather in NYC?' }
|
|
320
|
-
* );
|
|
321
|
-
*
|
|
322
|
-
* @example
|
|
323
|
-
* // Adding multiple messages
|
|
324
|
-
* runner.pushMessages(
|
|
325
|
-
* { role: 'user', content: 'What about NYC?' },
|
|
326
|
-
* { role: 'user', content: 'And Boston?' }
|
|
327
|
-
* );
|
|
328
|
-
*/
|
|
329
|
-
pushMessages(...messages) {
|
|
330
|
-
this.setMessagesParams((params) => ({
|
|
331
|
-
...params,
|
|
332
|
-
messages: [...params.messages, ...messages],
|
|
333
|
-
}));
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Makes the ToolRunner directly awaitable, equivalent to calling .runUntilDone()
|
|
337
|
-
* This allows using `await runner` instead of `await runner.runUntilDone()`
|
|
338
|
-
*/
|
|
339
|
-
then(onfulfilled, onrejected) {
|
|
340
|
-
return this.runUntilDone().then(onfulfilled, onrejected);
|
|
341
349
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
350
|
+
_BetaToolRunner_generateToolResponse = async function _BetaToolRunner_generateToolResponse(lastMessage, signal = __classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal) {
|
|
351
|
+
if (__classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f") !== undefined) {
|
|
352
|
+
return __classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f");
|
|
353
|
+
}
|
|
354
|
+
__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, generateToolResponse(__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params, lastMessage, {
|
|
355
|
+
...__classPrivateFieldGet(this, _BetaToolRunner_options, "f"),
|
|
356
|
+
signal,
|
|
357
|
+
}), "f");
|
|
345
358
|
return __classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f");
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
signal,
|
|
350
|
-
}), "f");
|
|
351
|
-
return __classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f");
|
|
352
|
-
};
|
|
359
|
+
};
|
|
360
|
+
return BetaToolRunner;
|
|
361
|
+
})();
|
|
353
362
|
async function generateToolResponse(params, lastMessage = params.messages.at(-1), requestOptions) {
|
|
354
363
|
// Only process if the last message is from the assistant and has tool use blocks
|
|
355
364
|
if (!lastMessage ||
|