UNPKG

@anthropic-ai/sdk

Version:
739 lines • 43.4 kB
var _BetaToolRunner_instances, _BetaToolRunner_consumed, _BetaToolRunner_mutated, _BetaToolRunner_state, _BetaToolRunner_options, _BetaToolRunner_message, _BetaToolRunner_stream, _BetaToolRunner_toolResponse, _BetaToolRunner_completion, _BetaToolRunner_iterationCount, _BetaToolRunner_compaction, _BetaToolRunner_lastStopReason, _BetaToolRunner_toolOverrides, _BetaToolRunner_pendingToolChanges, _BetaToolRunner_checkAndCompact, _BetaToolRunner_send, _BetaToolRunner_compact, _BetaToolRunner_runnableTools, _BetaToolRunner_availableToolNames, _BetaToolRunner_recordRemovalsFromHistory, _BetaToolRunner_compactAfterFinalTurn, _BetaToolRunner_generateToolResponse, _BetaToolRunner_flushPendingToolChanges, _BetaToolRunner_pendingToolChangesMessage; import { __classPrivateFieldGet, __classPrivateFieldSet } from "../../internal/tslib.mjs"; import { ToolError } from "./ToolError.mjs"; import { AnthropicError } from "../../core/error.mjs"; import { buildHeaders } from "../../internal/headers.mjs"; import { promiseWithResolvers } from "../../internal/utils/promise.mjs"; import { checkNever } from "../../internal/utils/values.mjs"; import { loggerFor } from "../../internal/utils/log.mjs"; import { DEFAULT_SUMMARY_PROMPT, DEFAULT_TOKEN_THRESHOLD } from "./CompactionControl.mjs"; import { collectStainlessHelpers, helperHeader, STAINLESS_HELPER_HEADER, } from "../../internal/stainless-helper-header.mjs"; /** * A ToolRunner handles the automatic conversation loop between the assistant and tools. * * A ToolRunner is an async iterable that yields either BetaMessage or BetaMessageStream objects * depending on the streaming configuration. */ export var BetaToolRunner = /* @__PURE__ */ (() => { /** * A ToolRunner handles the automatic conversation loop between the assistant and tools. * * A ToolRunner is an async iterable that yields either BetaMessage or BetaMessageStream objects * depending on the streaming configuration. */ class BetaToolRunner { constructor(client, params, options) { _BetaToolRunner_instances.add(this); this.client = client; /** Whether the async iterator has been consumed */ _BetaToolRunner_consumed.set(this, false); /** Whether parameters have been mutated since the last API call */ _BetaToolRunner_mutated.set(this, false); /** Current state containing the request parameters */ _BetaToolRunner_state.set(this, void 0); _BetaToolRunner_options.set(this, void 0); /** Promise for the last message received from the assistant */ _BetaToolRunner_message.set(this, void 0); /** The stream of the request in progress, when streaming */ _BetaToolRunner_stream.set(this, void 0); /** Cached tool response to avoid redundant executions */ _BetaToolRunner_toolResponse.set(this, void 0); /** Promise resolvers for waiting on completion */ _BetaToolRunner_completion.set(this, void 0); /** Number of iterations (API requests) made so far */ _BetaToolRunner_iterationCount.set(this, 0); /** A compaction scheduled with `compactBeforeNextTurn()`, in flight until its response has been handled */ _BetaToolRunner_compaction.set(this, { status: 'idle' }); /** The last turn's stop reason, or `null` once the history has been replaced since */ _BetaToolRunner_lastStopReason.set(this, null); /** * `addTools()` / `removeTools()` never edit `params.tools`, because a changed `tools` misses the prompt * cache, so what they change about which tool runs under a name is kept here instead: the runnable tool * added under that name, or `null` once the name was removed or taken by a raw definition. A tool call is * looked up here first, and in `params.tools` only when its name has no entry. */ _BetaToolRunner_toolOverrides.set(this, new Map()); /** Changes queued by `addTools()` / `removeTools()`, in call order, for the next request */ _BetaToolRunner_pendingToolChanges.set(this, []); rejectCompactionParam(params); __classPrivateFieldSet(this, _BetaToolRunner_state, { params: { // You can't clone the entire params since there are functions as handlers. // You also don't really need to clone params.messages, but it probably will prevent a foot gun // somewhere. ...params, // Not structuredClone(): it throws on a function, and a runnable tool written by value into a // `tool_addition` block has `run`. A JSON copy is the messages as they are sent, which drops it. messages: JSON.parse(JSON.stringify(params.messages)), }, }, "f"); // Cloning drops symbol-keyed properties, so collect helper marks // from the original params here — the create()-side collector won't see // them on the cloned messages. const collected = collectStainlessHelpers(params.tools, params.messages); __classPrivateFieldSet(this, _BetaToolRunner_options, { ...options, headers: buildHeaders([ helperHeader('BetaToolRunner'), collected.length ? { [STAINLESS_HELPER_HEADER]: collected.join(', ') } : undefined, options?.headers, ]), }, "f"); __classPrivateFieldSet(this, _BetaToolRunner_completion, promiseWithResolvers(), "f"); if (params.compactionControl?.enabled) { console.warn('Anthropic: The `compactionControl` parameter is deprecated and will be removed in a future version. ' + 'Use server-side compaction instead by passing `edits: [{ type: "compact_20260112" }]` in the params passed to `toolRunner()`. ' + 'See https://platform.claude.com/docs/en/build-with-claude/compaction'); } } async *[(_BetaToolRunner_consumed = new WeakMap(), _BetaToolRunner_mutated = new WeakMap(), _BetaToolRunner_state = new WeakMap(), _BetaToolRunner_options = new WeakMap(), _BetaToolRunner_message = new WeakMap(), _BetaToolRunner_stream = new WeakMap(), _BetaToolRunner_toolResponse = new WeakMap(), _BetaToolRunner_completion = new WeakMap(), _BetaToolRunner_iterationCount = new WeakMap(), _BetaToolRunner_compaction = new WeakMap(), _BetaToolRunner_lastStopReason = new WeakMap(), _BetaToolRunner_toolOverrides = new WeakMap(), _BetaToolRunner_pendingToolChanges = new WeakMap(), _BetaToolRunner_instances = new WeakSet(), _BetaToolRunner_checkAndCompact = async function _BetaToolRunner_checkAndCompact() { const compactionControl = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.compactionControl; if (!compactionControl || !compactionControl.enabled) { return false; } let tokensUsed = 0; if (__classPrivateFieldGet(this, _BetaToolRunner_message, "f") !== undefined) { try { const message = await __classPrivateFieldGet(this, _BetaToolRunner_message, "f"); const totalInputTokens = message.usage.input_tokens + (message.usage.cache_creation_input_tokens ?? 0) + (message.usage.cache_read_input_tokens ?? 0); tokensUsed = totalInputTokens + message.usage.output_tokens; } catch { // If we can't get the message, skip compaction return false; } } const threshold = compactionControl.contextTokenThreshold ?? DEFAULT_TOKEN_THRESHOLD; if (tokensUsed < threshold) { return false; } const model = compactionControl.model ?? __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.model; const summaryPrompt = compactionControl.summaryPrompt ?? DEFAULT_SUMMARY_PROMPT; const messages = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages; if (messages[messages.length - 1].role === 'assistant') { // Remove tool_use blocks from the last message to avoid 400 error // (tool_use requires tool_result, which we don't have yet) const lastMessage = messages[messages.length - 1]; if (Array.isArray(lastMessage.content)) { const nonToolBlocks = lastMessage.content.filter((block) => block.type !== 'tool_use'); if (nonToolBlocks.length === 0) { // If all blocks were tool_use, just remove the message entirely messages.pop(); } else { lastMessage.content = nonToolBlocks; } } } const response = await this.client.beta.messages.create({ model, messages: [ ...messages, { role: 'user', content: [ { type: 'text', text: summaryPrompt, }, ], }, ], max_tokens: __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_tokens, }, { signal: __classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal, headers: buildHeaders([__classPrivateFieldGet(this, _BetaToolRunner_options, "f").headers, helperHeader('compaction')]), }); if (response.content[0]?.type !== 'text') { throw new AnthropicError('Expected text response for compaction'); } // Must run before the history is replaced: a removal the caller wrote into it is known only from it. __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_recordRemovalsFromHistory).call(this); __classPrivateFieldSet(this, _BetaToolRunner_lastStopReason, null, "f"); __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages = [ { role: 'user', content: asContentParam(response.content), }, ]; return true; }, Symbol.asyncIterator)]() { var _a; if (__classPrivateFieldGet(this, _BetaToolRunner_consumed, "f")) { throw new AnthropicError('Cannot iterate over a consumed stream'); } __classPrivateFieldSet(this, _BetaToolRunner_consumed, true, "f"); __classPrivateFieldSet(this, _BetaToolRunner_mutated, true, "f"); __classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f"); try { while (true) { try { if (__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_iterations && __classPrivateFieldGet(this, _BetaToolRunner_iterationCount, "f") >= __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_iterations) { break; } __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_flushPendingToolChanges).call(this); // The API can't compact a conversation that ends mid-turn, so a paused turn is resumed first. if (__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status === 'scheduled' && determineNextStepFromStopReason(__classPrivateFieldGet(this, _BetaToolRunner_lastStopReason, "f")) !== 'resume') { yield* __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_compact).call(this, __classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").config); continue; } __classPrivateFieldSet(this, _BetaToolRunner_mutated, false, "f"); __classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f"); __classPrivateFieldSet(this, _BetaToolRunner_iterationCount, (_a = __classPrivateFieldGet(this, _BetaToolRunner_iterationCount, "f"), _a++, _a), "f"); __classPrivateFieldSet(this, _BetaToolRunner_message, undefined, "f"); const { max_iterations, compactionControl, ...params } = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params; yield* __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_send).call(this, params); const isCompacted = await __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_checkAndCompact).call(this); if (!isCompacted) { if (!__classPrivateFieldGet(this, _BetaToolRunner_mutated, "f")) { const message = await __classPrivateFieldGet(this, _BetaToolRunner_message, "f"); const nextStep = determineNextStepFromStopReason(message.stop_reason); __classPrivateFieldSet(this, _BetaToolRunner_lastStopReason, message.stop_reason, "f"); __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.push({ role: message.role, content: asContentParam(message.content), }); // Container-bound server tools reject a follow-up request that omits the container the // previous turn ran in, so carry its id forward unless the caller pinned one themselves. const { container } = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params; if (message.container) { if (container == null) { __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.container = message.container.id; } else if (typeof container === 'object' && container.id == null) { __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.container = { ...container, id: message.container.id }; } } if (nextStep === 'stop') { yield* __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_compactAfterFinalTurn).call(this); break; } if (nextStep === 'resume') { continue; } } else { // The caller has taken over the history, so the last response no longer says how it ends. __classPrivateFieldSet(this, _BetaToolRunner_lastStopReason, null, "f"); } const toolMessage = await __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_generateToolResponse).call(this, __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.at(-1)); if (toolMessage) { __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.push(toolMessage); } else if (!__classPrivateFieldGet(this, _BetaToolRunner_mutated, "f")) { yield* __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_compactAfterFinalTurn).call(this); break; } } } finally { __classPrivateFieldGet(this, _BetaToolRunner_stream, "f")?.abort(); __classPrivateFieldSet(this, _BetaToolRunner_stream, undefined, "f"); } } if (!__classPrivateFieldGet(this, _BetaToolRunner_message, "f")) { throw new AnthropicError('ToolRunner concluded without a message from the server'); } __classPrivateFieldGet(this, _BetaToolRunner_completion, "f").resolve(await __classPrivateFieldGet(this, _BetaToolRunner_message, "f")); } catch (error) { __classPrivateFieldSet(this, _BetaToolRunner_consumed, false, "f"); // Silence unhandled promise errors __classPrivateFieldGet(this, _BetaToolRunner_completion, "f").promise.catch(() => { }); __classPrivateFieldGet(this, _BetaToolRunner_completion, "f").reject(error); __classPrivateFieldSet(this, _BetaToolRunner_completion, promiseWithResolvers(), "f"); throw error; } } setMessagesParams(paramsOrMutator) { const params = typeof paramsOrMutator === 'function' ? paramsOrMutator(__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params) : paramsOrMutator; rejectCompactionParam(params); if (__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status !== 'idle') { rejectCompactionEdit(params); } if (__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status === 'in_flight' && params.messages !== __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages) { throw new AnthropicError("Message params can't be changed while the conversation is being compacted, because the compaction " + 'response is about to replace them. Change them after this iteration instead.'); } __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params = params; __classPrivateFieldSet(this, _BetaToolRunner_mutated, true, "f"); // Invalidate cached tool response since parameters changed __classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f"); } setRequestOptions(optionsOrMutator) { if (typeof optionsOrMutator === 'function') { __classPrivateFieldSet(this, _BetaToolRunner_options, optionsOrMutator(__classPrivateFieldGet(this, _BetaToolRunner_options, "f")), "f"); } else { __classPrivateFieldSet(this, _BetaToolRunner_options, { ...__classPrivateFieldGet(this, _BetaToolRunner_options, "f"), ...optionsOrMutator }, "f"); } } /** * Get the tool response for the last message from the assistant. * Avoids redundant tool executions by caching results. * * @returns A promise that resolves to a BetaMessageParam containing tool results, or null if no tools need to be executed * * @example * const toolResponse = await runner.generateToolResponse(); * if (toolResponse) { * console.log('Tool results:', toolResponse.content); * } */ async generateToolResponse(signal = __classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal) { const message = (await __classPrivateFieldGet(this, _BetaToolRunner_message, "f")) ?? this.params.messages.at(-1); if (!message) { return null; } return __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_generateToolResponse).call(this, message, signal); } /** * Wait for the async iterator to complete. This works even if the async iterator hasn't yet started, and * will wait for an instance to start and go to completion. * * @returns A promise that resolves to the final BetaMessage when the iterator completes * * @example * // Start consuming the iterator * for await (const message of runner) { * console.log('Message:', message.content); * } * * // Meanwhile, wait for completion from another part of the code * const finalMessage = await runner.done(); * console.log('Final response:', finalMessage.content); */ done() { return __classPrivateFieldGet(this, _BetaToolRunner_completion, "f").promise; } /** * Returns a promise indicating that the stream is done. Unlike .done(), this will eagerly read the stream: * * If the iterator has not been consumed, consume the entire iterator and return the final message from the * assistant. * * If the iterator has been consumed, waits for it to complete and returns the final message. * * @returns A promise that resolves to the final BetaMessage from the conversation * @throws {AnthropicError} If no messages were processed during the conversation * * @example * const finalMessage = await runner.runUntilDone(); * console.log('Final response:', finalMessage.content); */ async runUntilDone() { // If not yet consumed, start consuming and wait for completion if (!__classPrivateFieldGet(this, _BetaToolRunner_consumed, "f")) { for await (const _ of this) { // Iterator naturally populates this.#message } } // If consumed but not completed, wait for completion return this.done(); } /** * Get the current parameters being used by the ToolRunner. * * @returns A readonly view of the current ToolRunnerParams * * @example * const currentParams = runner.params; * console.log('Current model:', currentParams.model); * console.log('Message count:', currentParams.messages.length); */ get params() { return __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params; } /** * Add one or more messages to the conversation history. * * @param messages - One or more BetaMessageParam objects to add to the conversation * * @example * runner.pushMessages( * { role: 'user', content: 'Also, what about the weather in NYC?' } * ); * * @example * // Adding multiple messages * runner.pushMessages( * { role: 'user', content: 'What about NYC?' }, * { role: 'user', content: 'And Boston?' } * ); */ pushMessages(...messages) { this.setMessagesParams((params) => ({ ...params, messages: [...params.messages, ...messages], })); } /** * Schedule a compaction of the conversation. Once the current turn has finished, including any tool * calls, the runner requests a summary and replaces the message history with the compaction response, * which is yielded like any other message. Requires the `compact-2026-09-04` beta. * * @param compaction - The config to send, as `messages.create()` takes it. Defaults to `{ type: 'summarize' }` * * @example * for await (const message of runner) { * if (message.usage.input_tokens > 100_000) { * runner.compactBeforeNextTurn(); * } * } */ compactBeforeNextTurn(compaction) { if (__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status === 'in_flight') { return; } rejectCompactionEdit(__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params); __classPrivateFieldSet(this, _BetaToolRunner_compaction, { status: 'scheduled', config: compaction ?? { type: 'summarize' } }, "f"); } /** * Give the model more tools without changing `params.tools`, which would miss the prompt cache. * * Each tool's whole definition is sent in a `tool_addition` block with the next request, and a * runnable tool replaces a runnable tool of the same name straight away, even for a call already in * the message being handled. A raw definition is only sent: the runner never runs it, and stops * running a tool of the same name. Requires the `inline-tools-2026-09-15` beta, which the runner does * not add for you. * * @param tools - Runnable tools (for example from `betaZodTool()`) or raw tool definitions * * @example * runner.addTools(queryDatabaseTool); */ addTools(...tools) { for (const tool of tools) { // A definition without a `name` (an `mcp_toolset`) is nothing the runner runs or stops running. if ('name' in tool) { __classPrivateFieldGet(this, _BetaToolRunner_toolOverrides, "f").set(tool.name, 'run' in tool ? tool : null); } __classPrivateFieldGet(this, _BetaToolRunner_pendingToolChanges, "f").push({ type: 'addition', tool }); } } /** * Take tools away from the model without changing `params.tools`, which would miss the prompt cache. * * The tools stop being run straight away: a call to one of them, even one in the message being * handled, gets the same "not found" error result as a call to an unknown tool. The model is told * in a `tool_removal` block with the next request. Use {@link addTools} to bring a tool back. * Requires the `inline-tools-2026-09-15` beta, which the runner does not add for you. * * @param tools - The tools to remove, or their names * * @example * runner.removeTools('query_database'); */ removeTools(...tools) { for (const tool of tools) { const name = typeof tool === 'string' ? tool : tool.name; __classPrivateFieldGet(this, _BetaToolRunner_toolOverrides, "f").set(name, null); __classPrivateFieldGet(this, _BetaToolRunner_pendingToolChanges, "f").push({ type: 'removal', name }); } } /** * Makes the ToolRunner directly awaitable, equivalent to calling .runUntilDone() * This allows using `await runner` instead of `await runner.runUntilDone()` */ then(onfulfilled, onrejected) { return this.runUntilDone().then(onfulfilled, onrejected); } } _BetaToolRunner_send = /** * Sends one request and yields its message, or its stream when streaming. `#message` and `#stream` are set * before the yield, so they are there while the caller handles the item; the loop aborts the stream at the * end of the iteration. */ async function* _BetaToolRunner_send(params) { if (params.stream) { __classPrivateFieldSet(this, _BetaToolRunner_stream, this.client.beta.messages.stream({ ...params }, __classPrivateFieldGet(this, _BetaToolRunner_options, "f")), "f"); __classPrivateFieldSet(this, _BetaToolRunner_message, __classPrivateFieldGet(this, _BetaToolRunner_stream, "f").finalMessage(), "f"); // Make sure that this promise doesn't throw before we get the option to do something about it. // Error will be caught when we call await this.#message ultimately __classPrivateFieldGet(this, _BetaToolRunner_message, "f").catch(() => { }); yield __classPrivateFieldGet(this, _BetaToolRunner_stream, "f"); } else { __classPrivateFieldSet(this, _BetaToolRunner_message, this.client.beta.messages.create({ ...params, stream: false }, __classPrivateFieldGet(this, _BetaToolRunner_options, "f")), "f"); yield __classPrivateFieldGet(this, _BetaToolRunner_message, "f"); } }, _BetaToolRunner_compact = async function* _BetaToolRunner_compact(compaction) { rejectCompactionEdit(__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params); const { max_iterations, compactionControl, ...requestParams } = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params; const params = withoutCompactionIncompatibleParams(requestParams); __classPrivateFieldSet(this, _BetaToolRunner_compaction, { status: 'in_flight' }, "f"); __classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f"); const lastMessage = __classPrivateFieldGet(this, _BetaToolRunner_message, "f"); try { yield* __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_send).call(this, { ...params, compaction }); const message = await __classPrivateFieldGet(this, _BetaToolRunner_message, "f"); if (message.content.some((block) => block.type === 'compaction' && block.content)) { __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_recordRemovalsFromHistory).call(this); // The response has to be sent back as it came, first, replacing the messages it summarizes. __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages = [{ role: message.role, content: message.content }]; } else { loggerFor(this.client).warn('Compaction produced no summary; keeping the conversation as it is.'); // If the run ends here, `done()` resolves to the last real message rather than this response. __classPrivateFieldSet(this, _BetaToolRunner_message, lastMessage, "f"); } } finally { __classPrivateFieldSet(this, _BetaToolRunner_compaction, { status: 'idle' }, "f"); } }, _BetaToolRunner_runnableTools = function _BetaToolRunner_runnableTools() { const runnable = new Map(); for (const tool of __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.tools) { if ('run' in tool) { runnable.set(tool.name, tool); } } for (const [name, tool] of __classPrivateFieldGet(this, _BetaToolRunner_toolOverrides, "f")) { if (tool) { runnable.set(name, tool); } else { runnable.delete(name); } } return runnable; }, _BetaToolRunner_availableToolNames = function _BetaToolRunner_availableToolNames() { const available = new Set(__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_runnableTools).call(this).keys()); for (const message of [...__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages, __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_pendingToolChangesMessage).call(this)]) { if (typeof message.content === 'string') { continue; } for (const block of message.content) { if (message.role === 'system') { applyToolChange(block, available); } else if (message.role === 'assistant' && block.type === 'compaction') { // A compaction block's tool_changes stand in for the system messages of the turns it summarized. for (const change of block.tool_changes ?? []) { applyToolChange(change, available); } } } } return available; }, _BetaToolRunner_recordRemovalsFromHistory = function _BetaToolRunner_recordRemovalsFromHistory() { const available = __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_availableToolNames).call(this); for (const name of __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_runnableTools).call(this).keys()) { if (!available.has(name)) { __classPrivateFieldGet(this, _BetaToolRunner_toolOverrides, "f").set(name, null); } } }, _BetaToolRunner_compactAfterFinalTurn = async function* _BetaToolRunner_compactAfterFinalTurn() { if (__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status !== 'scheduled') { return; } const lastContent = __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.at(-1)?.content; if (Array.isArray(lastContent) && lastContent.some((block) => block.type === 'tool_use')) { // A turn that was cut short can end with tool calls that are never run, and the API can't // compact a conversation whose last turn has an unanswered tool call. loggerFor(this.client).warn('The pending compaction was skipped because the last turn ended with tool calls that were not run. ' + 'Call `compactBeforeNextTurn()` again if you continue the conversation.'); __classPrivateFieldSet(this, _BetaToolRunner_compaction, { status: 'idle' }, "f"); return; } yield* __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_compact).call(this, __classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").config); }, _BetaToolRunner_generateToolResponse = async function _BetaToolRunner_generateToolResponse(lastMessage, signal = __classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal) { if (__classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f") !== undefined) { return __classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f"); } __classPrivateFieldSet(this, _BetaToolRunner_toolResponse, generateToolResponse(__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_runnableTools).call(this), __classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_availableToolNames).call(this), lastMessage, { ...__classPrivateFieldGet(this, _BetaToolRunner_options, "f"), signal }), "f"); return __classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f"); }, _BetaToolRunner_flushPendingToolChanges = function _BetaToolRunner_flushPendingToolChanges() { // A paused turn has to go back as the last message, so the changes wait for the request after it. if (__classPrivateFieldGet(this, _BetaToolRunner_lastStopReason, "f") === 'pause_turn' || __classPrivateFieldGet(this, _BetaToolRunner_pendingToolChanges, "f").length === 0) { return; } // Not pushMessages(): that marks the params as changed by the caller, and the runner would then // leave this turn's assistant message and tool results for the caller to append. __classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.push(__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_pendingToolChangesMessage).call(this)); __classPrivateFieldSet(this, _BetaToolRunner_pendingToolChanges, [], "f"); }, _BetaToolRunner_pendingToolChangesMessage = function _BetaToolRunner_pendingToolChangesMessage() { const content = []; for (const change of __classPrivateFieldGet(this, _BetaToolRunner_pendingToolChanges, "f")) { if (change.type === 'removal') { content.push({ type: 'tool_removal', tool: { type: 'tool_reference', name: change.name } }); continue; } // The functions stay out of the definition that is sent. let definition = change.tool; if ('run' in change.tool) { const { run, parse, close, ...rest } = change.tool; definition = rest; } content.push({ type: 'tool_addition', tool: { type: 'tool_definition', definition } }); } return { role: 'system', content }; }; return BetaToolRunner; })(); function rejectCompactionParam(params) { if ('compaction' in params && params.compaction != null) { throw new AnthropicError('`compaction` cannot be set on a tool runner: every request in the loop would compact again. ' + 'Call `runner.compactBeforeNextTurn()` when the conversation should be compacted instead.'); } } function rejectCompactionEdit(params) { // The compaction request is sent without `context_management`, so the API can't refuse the pair there: // it would run and bill the compaction, then refuse the next request. if (params.context_management?.edits?.some((edit) => edit.type.startsWith('compact_'))) { throw new AnthropicError("`compactBeforeNextTurn()` can't be used while `context_management` has a compaction edit, " + "because the API doesn't accept a compaction block together with one. Remove the edit first."); } } /** * A compaction request returns only the compaction block, never a reply, so the API rejects the params that * only shape a reply. The runner's later requests keep them. */ function withoutCompactionIncompatibleParams(params) { const { context_management, stop_sequences, output_format, ...kept } = params; const withoutFormat = ({ format, ...outputConfig }) => outputConfig; if (kept.tool_choice?.type === 'any' || kept.tool_choice?.type === 'tool') { delete kept.tool_choice; } if (kept.output_config) { kept.output_config = withoutFormat(kept.output_config); } if (Array.isArray(kept.fallbacks)) { kept.fallbacks = kept.fallbacks.map((fallback) => fallback.output_config ? { ...fallback, output_config: withoutFormat(fallback.output_config) } : fallback); } return kept; } async function generateToolResponse(runnable, available, lastMessage, requestOptions) { // Only process if the last message is from the assistant and has tool use blocks if (!lastMessage || lastMessage.role !== 'assistant' || !lastMessage.content || typeof lastMessage.content === 'string') { return null; } const toolUseBlocks = lastMessage.content.filter((content) => content.type === 'tool_use'); if (toolUseBlocks.length === 0) { return null; } const toolResults = await Promise.all(toolUseBlocks.map(async (toolUse) => { // A `tool_removal` is only a hint to the model, which may still emit a tool_use for a // withdrawn tool — treat those exactly like a tool that was never defined. const tool = available.has(toolUse.name) ? runnable.get(toolUse.name) : undefined; if (!tool) { return toolNotFoundResult(toolUse); } try { let input = toolUse.input; if ('parse' in tool && tool.parse) { input = tool.parse(input); } const result = await tool.run(input, { toolUse: toolUse, toolUseBlock: toolUse, signal: requestOptions?.signal, }); return { type: 'tool_result', tool_use_id: toolUse.id, content: result, }; } catch (error) { return { type: 'tool_result', tool_use_id: toolUse.id, content: error instanceof ToolError ? error.content : `Error: ${error instanceof Error ? error.message : String(error)}`, is_error: true, }; } })); return { role: 'user', content: toolResults, }; } /** * Response content is sent back as request content unchanged. The generated request type of the * `tool_listing` block is narrower than its response type, so this needs an assertion until the * two agree. */ function asContentParam(content) { return content; } function toolNotFoundResult(toolUse) { return { type: 'tool_result', tool_use_id: toolUse.id, content: `Error: Tool '${toolUse.name}' not found`, is_error: true, }; } function applyToolChange(block, available) { switch (block.type) { case 'tool_removal': case 'tool_addition': applyToolReference(block, available); break; } } function applyToolReference(block, available) { const name = changedToolName(block.tool); if (name === undefined) return; if (block.type === 'tool_removal') { available.delete(name); } else { available.add(name); } } function changedToolName(tool) { switch (tool.type) { case 'tool_reference': return tool.name; case 'tool_definition': // Not every `tools[]` entry has a `name` (e.g. `mcp_toolset`); those are never locally runnable. return 'name' in tool.definition ? tool.definition.name : undefined; default: // mcp_tool_reference / mcp_toolset_reference run server-side; unknown types are ignored // rather than rejected. return undefined; } } /** * Sorts every stop reason into one of three buckets: `run_tools` turns run their client tool * calls and continue the loop; `resume` turns are sent back unchanged so the server continues * them; `stop` turns end the loop without running any tool calls. */ function determineNextStepFromStopReason(stopReason) { if (stopReason === null) return 'stop'; switch (stopReason) { case 'tool_use': return 'run_tools'; case 'pause_turn': // pause_after_compaction hands the turn back before the model answers; sending it back // unchanged continues it. case 'compaction': return 'resume'; case 'end_turn': case 'stop_sequence': case 'max_tokens': case 'model_context_window_exceeded': case 'refusal': return 'stop'; default: // The union is forward-compatible, so a stop reason this SDK doesn't know yet ends the // loop rather than throwing; the `never` check makes tsc reject an unclassified member. checkNever(stopReason); return 'stop'; } } //# sourceMappingURL=BetaToolRunner.mjs.map