UNPKG

@anthropic-ai/sdk

Version:
960 lines • 61.4 kB
"use strict";
var _BetaToolRunner_instances, _BetaToolRunner_consumed, _BetaToolRunner_mutated, _BetaToolRunner_state, _BetaToolRunner_options, _BetaToolRunner_message, _BetaToolRunner_stream, _BetaToolRunner_toolResponse, _BetaToolRunner_completion, _BetaToolRunner_iterationCount, _BetaToolRunner_toolRunnerOtelContext, _BetaToolRunner_compaction, _BetaToolRunner_calls, _BetaToolRunner_lastStopReason, _BetaToolRunner_toolOverrides, _BetaToolRunner_pendingToolChanges, _BetaToolRunner_withToolRunnerSpan, _BetaToolRunner_send, _BetaToolRunner_streamThatStartsTools, _BetaToolRunner_startedCallsSettled, _BetaToolRunner_compact, _BetaToolRunner_runnableTools, _BetaToolRunner_availableToolNames, _BetaToolRunner_recordRemovalsFromHistory, _BetaToolRunner_compactAfterFinalTurn, _BetaToolRunner_generateToolResponse, _BetaToolRunner_toolsetFamilies, _BetaToolRunner_flushPendingToolChanges, _BetaToolRunner_pendingToolChangesMessage;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BetaToolRunner = void 0;
const tslib_1 = require("../../internal/tslib.js");
const api_1 = require("@opentelemetry/api");
const BetaRunnableTool_1 = require("./BetaRunnableTool.js");
const family_1 = require("../internal/toolsets/family.js");
const run_1 = require("../internal/toolsets/run.js");
const sanitize_1 = require("../internal/toolsets/sanitize.js");
const errors_1 = require("../internal/toolsets/errors.js");
const error_1 = require("../../core/error.js");
const beta_1 = require("../../resources/beta.js");
const BetaToolRunnerStream_1 = require("../internal/BetaToolRunnerStream.js");
const tracing_1 = require("../../internal/tracing.js");
const tracing_2 = require("../internal/tracing.js");
const headers_1 = require("../../internal/headers.js");
const promise_1 = require("../../internal/utils/promise.js");
const values_1 = require("../../internal/utils/values.js");
const log_1 = require("../../internal/utils/log.js");
const stainless_helper_header_1 = require("../../internal/stainless-helper-header.js");
const USE_A_NEW_RUNNER = "a tool runner's toolsets come only from its tools param; change them with setMessagesParams(), or build a new runner";
const REMOVE_WITH_A_NEW_RUNNER = "a tool runner's toolsets come only from its tools param; remove it there with setMessagesParams(), or build a new runner without it";
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. With `runToolsEagerly` it starts each tool call while the
     * reply is still streaming, unless `deferToolCall()` holds the call.
     */
    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);
            /**
             * The OpenTelemetry `Context` of the loop that is running: the one that was active when the loop
             * started, with the tool runner span set as the active span.
             */
            _BetaToolRunner_toolRunnerOtelContext.set(this, void 0);
            /** A compaction scheduled with `compactBeforeNextTurn()`, in flight until its response has been handled */
            _BetaToolRunner_compaction.set(this, { status: 'idle' });
            /** The tool calls of the current reply that are held or have started, by `tool_use` id. See `runToolsEagerly`. */
            _BetaToolRunner_calls.set(this, void 0);
            /** 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);
            rejectRunToolsEagerlyWithoutStream(params);
            tslib_1.__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(withToolDefinitions(params.messages))),
                },
            }, "f");
            // Cloning drops symbol-keyed properties, so collect helper marks from the original params here: the
            // create()-side collector won't find them on the cloned messages, nor on a toolset's serialized entry.
            const collected = (0, stainless_helper_header_1.collectStainlessHelpers)(params.tools, params.messages);
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_options, {
                ...options,
                headers: (0, headers_1.buildHeaders)([
                    (0, stainless_helper_header_1.helperHeader)('BetaToolRunner'),
                    collected.length ? { [stainless_helper_header_1.STAINLESS_HELPER_HEADER]: collected.join(', ') } : undefined,
                    options?.headers,
                ]),
            }, "f");
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_completion, (0, promise_1.promiseWithResolvers)(), "f");
        }
        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_toolRunnerOtelContext = new WeakMap(), _BetaToolRunner_compaction = new WeakMap(), _BetaToolRunner_calls = new WeakMap(), _BetaToolRunner_lastStopReason = new WeakMap(), _BetaToolRunner_toolOverrides = new WeakMap(), _BetaToolRunner_pendingToolChanges = new WeakMap(), _BetaToolRunner_instances = new WeakSet(), _BetaToolRunner_withToolRunnerSpan = function _BetaToolRunner_withToolRunnerSpan(fn) {
            if (!tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolRunnerOtelContext, "f")) {
                return fn();
            }
            return api_1.context.with(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolRunnerOtelContext, "f"), fn);
        }, Symbol.asyncIterator)]() {
            var _a;
            if (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_consumed, "f")) {
                throw new error_1.AnthropicError('Cannot iterate over a consumed stream');
            }
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_consumed, true, "f");
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_mutated, true, "f");
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
            const toolRunnerSpan = (0, tracing_1.guardTracing)(this.client, () => (0, tracing_2.startToolRunnerSpan)(this.client, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f").openTelemetry?.conversationId));
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_toolRunnerOtelContext, toolRunnerSpan && api_1.trace.setSpan(api_1.context.active(), toolRunnerSpan), "f");
            try {
                while (true) {
                    try {
                        if (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_iterations &&
                            tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_iterationCount, "f") >= tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.max_iterations) {
                            break;
                        }
                        tslib_1.__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 (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status === 'scheduled' &&
                            determineNextStepFromStopReason(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_lastStopReason, "f")) !== 'resume') {
                            yield* tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_compact).call(this, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").config);
                            continue;
                        }
                        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_mutated, false, "f");
                        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
                        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_iterationCount, (_a = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_iterationCount, "f"), _a++, _a), "f");
                        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_message, undefined, "f");
                        const { max_iterations, runToolsEagerly, ...params } = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params;
                        yield* tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_send).call(this, params);
                        const next = await tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_withToolRunnerSpan).call(this, async () => {
                            if (!tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_mutated, "f")) {
                                const message = await tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_message, "f");
                                const nextStep = determineNextStepFromStopReason(message.stop_reason);
                                tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_lastStopReason, message.stop_reason, "f");
                                tslib_1.__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 } = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params;
                                if (message.container) {
                                    if (container == null) {
                                        tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.container = message.container.id;
                                    }
                                    else if (typeof container === 'object' && container.id == null) {
                                        tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.container = { ...container, id: message.container.id };
                                    }
                                }
                                if (nextStep === 'stop') {
                                    return 'stop';
                                }
                                if (nextStep === 'resume') {
                                    return 'continue';
                                }
                            }
                            else {
                                // The caller has taken over the history, so the last response no longer says how it ends.
                                tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_lastStopReason, null, "f");
                            }
                            const toolMessage = await tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_generateToolResponse).call(this, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.at(-1), tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolRunnerOtelContext, "f"));
                            if (toolMessage) {
                                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.push(toolMessage);
                            }
                            else if (!tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_mutated, "f")) {
                                return 'stop';
                            }
                            return 'continue';
                        });
                        if (next === 'stop') {
                            yield* tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_compactAfterFinalTurn).call(this);
                            break;
                        }
                    }
                    finally {
                        tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_stream, "f")?.abort();
                        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_stream, undefined, "f");
                    }
                }
                await tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_startedCallsSettled).call(this);
                if (!tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_message, "f")) {
                    throw new error_1.AnthropicError('ToolRunner concluded without a message from the server');
                }
                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_completion, "f").resolve(await tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_message, "f"));
                (0, tracing_1.guardTracing)(this.client, () => toolRunnerSpan?.setStatus({ code: api_1.SpanStatusCode.OK }));
            }
            catch (error) {
                if (toolRunnerSpan) {
                    (0, tracing_1.guardTracing)(this.client, () => (0, tracing_1.setErrorStatus)(toolRunnerSpan, error));
                }
                tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_consumed, false, "f");
                // Silence unhandled promise errors
                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_completion, "f").promise.catch(() => { });
                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_completion, "f").reject(error);
                tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_completion, (0, promise_1.promiseWithResolvers)(), "f");
                throw error;
            }
            finally {
                // Ends with no status when the application closed the iterator: a cancellation.
                (0, tracing_1.guardTracing)(this.client, () => toolRunnerSpan?.end());
                tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_toolRunnerOtelContext, undefined, "f");
            }
        }
        setMessagesParams(paramsOrMutator) {
            const params = typeof paramsOrMutator === 'function' ? paramsOrMutator(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params) : paramsOrMutator;
            rejectCompactionParam(params);
            rejectRunToolsEagerlyWithoutStream(params);
            if (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status !== 'idle') {
                rejectCompactionEdit(params);
            }
            if (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status === 'in_flight' && params.messages !== tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages) {
                throw new error_1.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.');
            }
            tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params = params;
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_mutated, true, "f");
            // Invalidate cached tool response since parameters changed
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
        }
        setRequestOptions(optionsOrMutator) {
            if (typeof optionsOrMutator === 'function') {
                tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_options, optionsOrMutator(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f")), "f");
            }
            else {
                tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_options, { ...tslib_1.__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. With `runToolsEagerly`, it reuses the calls of
         * the reply that have started and runs the rest, including the ones `deferToolCall()` is holding, so that no
         * call runs twice.
         *
         * @returns A promise that resolves to a BetaUserMessageParam 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 = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f").signal) {
            // Read before the first `await`: a context manager that does not follow promises has lost the
            // caller's context after it. Only a tool span has a use for it.
            const callerOtelContext = this.client._tracer ? api_1.context.active() : undefined;
            const message = (await tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_message, "f")) ?? this.params.messages.at(-1);
            if (!message) {
                return null;
            }
            return tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_generateToolResponse).call(this, message, signal, callerOtelContext);
        }
        /**
         * Hold a tool call of the current reply until you are done with the reply, which is when it runs without
         * streaming: at the end of the loop body, or when you call `generateToolResponse()`.
         *
         * With `runToolsEagerly` the runner otherwise starts each call while the reply streams, as soon as
         * the model has moved on from it: when the next block starts, or the reply stops with `tool_use`. A call
         * never starts before your stream listeners and any `for await` over the stream have handled that event, so
         * you can call this from either once you have seen the call. The other calls of the reply still start early,
         * except the later calls of the same toolset, which a toolset runs in order.
         * It does nothing for a call that has started, outside the loop body, and without `runToolsEagerly`.
         *
         * @param toolUse - The `tool_use` block of the call, or its id
         *
         * @example
         * for await (const stream of runner) {
         *   stream.on('contentBlock', (block) => {
         *     if (block.type === 'tool_use' && block.name === 'delete_file') {
         *       runner.deferToolCall(block);
         *     }
         *   });
         *   await stream.finalMessage();
         *   // No `delete_file` call has started yet.
         * }
         */
        deferToolCall(toolUse) {
            const id = typeof toolUse === 'string' ? toolUse : toolUse.id;
            if (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_calls, "f") && !tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_calls, "f").has(id)) {
                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_calls, "f").set(id, { status: 'held' });
            }
        }
        /**
         * The tool calls of the current reply that `deferToolCall()` is holding, in the model's order, as their
         * `tool_use` blocks. A call is in the list once its block has finished streaming, so read the list when you
         * are done with the stream. A call that has started is not in it. It is empty without
         * `runToolsEagerly`.
         *
         * Held calls are listed whatever the reply's `stop_reason`, because `generateToolResponse()` runs them
         * whatever it is. After `max_tokens` the input of the last call can be cut off.
         *
         * @example
         * for await (const stream of runner) {
         *   stream.on('contentBlock', (block) => {
         *     if (block.type === 'tool_use' && block.name === 'delete_file') {
         *       runner.deferToolCall(block);
         *     }
         *   });
         *   await stream.finalMessage();
         *
         *   const held = runner.deferredToolCalls;
         *   if (held.length > 0 && !(await confirm(held))) break;
         * }
         */
        get deferredToolCalls() {
            const stream = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_stream, "f");
            const calls = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_calls, "f");
            if (!(stream instanceof BetaToolRunnerStream_1.BetaToolRunnerStream) || !calls) {
                return [];
            }
            return stream.toolCalls.filter((toolUse) => calls.get(toolUse.id)?.status === 'held');
        }
        /**
         * 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 tslib_1.__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 (!tslib_1.__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 tslib_1.__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 (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status === 'in_flight') {
                return;
            }
            rejectCompactionEdit(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params);
            tslib_1.__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 call that started while the reply streamed keeps the old one. A raw definition
         * is only sent: the runner never runs it, and stops running a tool of the same name. Passing a runnable toolset,
         * or a raw definition of a toolset the runner has, throws `ToolsetContractError` and adds nothing: a runner's
         * toolsets come only from `params.tools`. A tool named like one of its toolsets is left to the API, which rejects
         * it with the next request. 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) {
            const families = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_toolsetFamilies).call(this);
            for (const tool of tools) {
                if ((0, family_1.isRunnableToolset)(tool)) {
                    throw new errors_1.ToolsetContractError(`addTools() can't add the '${(0, sanitize_1.quotedName)((0, family_1.toolsetFamily)(tool))}' toolset: ${USE_A_NEW_RUNNER}`);
                }
                // The API treats a definition of one of the runner's families as replacing that toolset, which would leave the
                // model with a toolset the runner doesn't run.
                const family = definitionFamily(tool);
                if (families.has(family)) {
                    throw new errors_1.ToolsetContractError(`addTools() can't replace the '${(0, sanitize_1.quotedName)(family)}' toolset: ${USE_A_NEW_RUNNER}`);
                }
            }
            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) {
                    tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolOverrides, "f").set(tool.name, 'run' in tool ? tool : null);
                }
                tslib_1.__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. A call that started while the
         * reply streamed finishes as usual. The model is told in a `tool_removal` block with the next request. Use
         * {@link addTools} to bring a tool back. Passing the name of one of the runner's toolsets throws
         * `ToolsetContractError` and removes nothing. 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) {
            const families = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_toolsetFamilies).call(this);
            for (const tool of tools) {
                // A toolset's name is always its own: the API rejects any other tool of that name.
                const name = typeof tool === 'string' ? tool : tool.name;
                if (families.has(name)) {
                    throw new errors_1.ToolsetContractError(`removeTools() can't remove the '${(0, sanitize_1.quotedName)(name)}' toolset: ${REMOVE_WITH_A_NEW_RUNNER}`);
                }
            }
            for (const tool of tools) {
                const name = typeof tool === 'string' ? tool : tool.name;
                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolOverrides, "f").set(name, null);
                tslib_1.__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) {
        await tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_startedCallsSettled).call(this);
        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_calls, undefined, "f");
        params = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_withToolRunnerSpan).call(this, () => ({
            ...params,
            ...(params.tools && { tools: params.tools.map(toolDefinition) }),
            messages: withToolDefinitions(params.messages),
        }));
        if (params.stream) {
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_stream, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_withToolRunnerSpan).call(this, () => tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.runToolsEagerly ?
                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_streamThatStartsTools).call(this, params)
                : this.client.beta.messages.stream({ ...params }, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f"))), "f");
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_message, tslib_1.__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
            tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_message, "f").catch(() => { });
            yield tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_stream, "f");
        }
        else {
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_message, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_withToolRunnerSpan).call(this, () => this.client.beta.messages.create({ ...params, stream: false }, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f"))), "f");
            yield tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_message, "f");
        }
    }, _BetaToolRunner_streamThatStartsTools = function _BetaToolRunner_streamThatStartsTools(params) {
        const calls = new Map();
        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_calls, calls, "f");
        // A toolset runs its calls one at a time, in the model's order. So a toolset's call starts here only while the
        // toolset is idle, and once one of its calls waits for the reply or fails, its later calls wait for the reply too.
        const toolsets = new Map();
        return BetaToolRunnerStream_1.BetaToolRunnerStream.start(this.client, params, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f"), (toolUse) => {
            // Held, or started by `generateToolResponse()` before a reader that is behind got to the call.
            const known = calls.has(toolUse.id);
            // A call named after a toolset is a toolset call with malformed actions, which `generateToolResponse()` answers.
            const malformed = !toolUse.toolset_name && tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_toolsetFamilies).call(this).has(toolUse.name);
            const family = malformed ? toolUse.name : toolUse.toolset_name;
            if (family) {
                if (known || malformed || toolsets.has(family)) {
                    toolsets.set(family, 'waiting');
                    return;
                }
                toolsets.set(family, 'running');
                const result = runToolsetCall(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params, family, toolUse, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f"), this.client, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolRunnerOtelContext, "f"));
                calls.set(toolUse.id, { status: 'started', result });
                result.then(({ is_error }) => {
                    if (is_error || toolsets.get(family) === 'waiting')
                        toolsets.set(family, 'waiting');
                    else
                        toolsets.delete(family);
                }, () => toolsets.set(family, 'waiting'));
                return;
            }
            if (known) {
                return;
            }
            // The call looks its tool up now, so a later `addTools()` or `removeTools()` doesn't change it.
            const result = runToolCall(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_runnableTools).call(this), tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_availableToolNames).call(this), toolUse, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f"), this.client, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolRunnerOtelContext, "f"));
            // Marks a rejection as handled until the results are collected.
            result.catch(() => { });
            calls.set(toolUse.id, { status: 'started', result });
        });
    }, _BetaToolRunner_startedCallsSettled = 
    /** Waits for the calls of the last streamed reply that have started, whether or not their results were sent. */
    async function _BetaToolRunner_startedCallsSettled() {
        const started = [...(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_calls, "f")?.values() ?? [])].filter((call) => call.status === 'started');
        await Promise.allSettled(started.map((call) => call.result));
    }, _BetaToolRunner_compact = async function* _BetaToolRunner_compact(compaction) {
        rejectCompactionEdit(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params);
        const { max_iterations, runToolsEagerly, ...requestParams } = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params;
        const params = withoutCompactionIncompatibleParams(requestParams);
        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_compaction, { status: 'in_flight' }, "f");
        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, undefined, "f");
        const lastMessage = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_message, "f");
        try {
            yield* tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_send).call(this, { ...params, compaction });
            const message = await tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_message, "f");
            if (message.content.some((block) => block.type === 'compaction' && block.content)) {
                tslib_1.__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.
                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages = [{ role: message.role, content: message.content }];
            }
            else {
                (0, log_1.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.
                tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_message, lastMessage, "f");
            }
        }
        finally {
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_compaction, { status: 'idle' }, "f");
        }
    }, _BetaToolRunner_runnableTools = function _BetaToolRunner_runnableTools() {
        const runnable = new Map();
        for (const tool of tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.tools) {
            if ('run' in tool && 'name' in tool) {
                runnable.set(tool.name, tool);
            }
        }
        for (const [name, tool] of tslib_1.__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(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_runnableTools).call(this).keys());
        for (const message of [...tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_pendingToolChangesMessage).call(this)]) {
            if (!Array.isArray(message.content)) {
                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 = tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_availableToolNames).call(this);
        for (const name of tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_runnableTools).call(this).keys()) {
            if (!available.has(name)) {
                tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolOverrides, "f").set(name, null);
            }
        }
    }, _BetaToolRunner_compactAfterFinalTurn = async function* _BetaToolRunner_compactAfterFinalTurn() {
        if (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").status !== 'scheduled') {
            return;
        }
        const lastContent = tslib_1.__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.
            (0, log_1.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.');
            tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_compaction, { status: 'idle' }, "f");
            return;
        }
        yield* tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_compact).call(this, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_compaction, "f").config);
    }, _BetaToolRunner_generateToolResponse = async function _BetaToolRunner_generateToolResponse(lastMessage, signal, toolOtelContext) {
        if (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f") !== undefined) {
            return tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f");
        }
        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_toolResponse, generateToolResponse(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_runnableTools).call(this), tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_availableToolNames).call(this), lastMessage, { ...tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_options, "f"), signal }, tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_calls, "f"), this.client, toolOtelContext), "f");
        return tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_toolResponse, "f");
    }, _BetaToolRunner_toolsetFamilies = function _BetaToolRunner_toolsetFamilies() {
        return new Set(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.tools.filter(family_1.isRunnableToolset).map(family_1.toolsetFamily));
    }, _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 (tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_lastStopReason, "f") === 'pause_turn' || tslib_1.__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.
        tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_state, "f").params.messages.push(tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_instances, "m", _BetaToolRunner_pendingToolChangesMessage).call(this));
        tslib_1.__classPrivateFieldSet(this, _BetaToolRunner_pendingToolChanges, [], "f");
    }, _BetaToolRunner_pendingToolChangesMessage = function _BetaToolRunner_pendingToolChangesMessage() {
        const content = [];
        for (const change of tslib_1.__classPrivateFieldGet(this, _BetaToolRunner_pendingToolChanges, "f")) {
            if (change.type === 'removal') {
                content.push({ type: 'tool_removal', tool: { type: 'tool_reference', name: change.name } });
                continue;
            }
            const definition = toolDefinition(change.tool);
            content.push({ type: 'tool_addition', tool: { type: 'tool_definition', definition } });
        }
        return { role: 'system', content };
    };
    return BetaToolRunner;
})();
exports.BetaToolRunner = BetaToolRunner;
function rejectCompactionParam(params) {
    if ('compaction' in params && params.compaction != null) {
        throw new error_1.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 rejectRunToolsEagerlyWithoutStream(params) {
    if (params.runToolsEagerly && !params.stream) {
        throw new TypeError("`runToolsEagerly: true` needs `stream: true` in the tool runner's params, because a reply that " +
            "isn't streamed arrives whole.");
    }
}
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 error_1.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;
}
function toolDefinition(tool) {
    if (!('run' in tool)) {
        return tool;
    }
    const apiKeys = beta_1.BETA_CLIENT_TOOL_UNION_KEYS;
    const fields = 'toJSON' in tool && typeof tool.toJSON === 'function' ? tool.toJSON() : tool;
    return {
        ...Object.fromEntries(Object.entries(fields).filter(([key]) => apiKeys.includes(key))),
        ...((0, stainless_helper_header_1.wasCreatedByStainlessHelper)(tool) && { [stainless_helper_header_1.SDK_HELPER_SYMBOL]: tool[stainless_helper_header_1.SDK_HELPER_SYMBOL] }),
    };
}
function withToolDefinitions(messages) {
    return messages.map((message) => {
        if (message.role !== 'system' || !Array.isArray(message.content)) {
            return message;
        }
        const content = message.content.map((block) => block.type === 'tool_addition' && block.tool.type === 'tool_definition' ?
            { ...block, tool: { ...block.tool, definition: toolDefinition(block.tool.definition) } }
            : block);
        return { ...message, content };
    });
}
async function generateToolResponse(params, runnable, available, lastMessage, requestOptions, calls, client, toolOtelContext) {
    // 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;
    }
    // A toolset's members run one at a time in the order the model wrote them, and once one is answered with is_error
    // the rest of that toolset's members in this turn are not run (notExecutedText). Function tools run alongside them
    // and neither wait on nor stop a toolset's members.
    const sequences = new Map();
    const toolResults = await Promise.all(toolUseBlocks.map(async (toolUse) => {
        // The call may have started while the reply streamed, or in an earlier `generateToolResponse()`.
        const call = calls?.get(toolUse.id);
        const started = call?.status === 'started' ? call.result : undefined;
        // Members dispatch by family, never by name: a custom tool may share the bare `name`.
        if (toolUse.toolset_name) {
            const family = toolUse.toolset_name;
            const sequence = sequences.get(family) ?? { tail: Promise.resolve(), failed: false };
            sequences.set(family, sequence);
            const registered = params.tools.some((t) => (0, family_1.isRunnableToolset)(t) && (0, family_1.toolsetFamily)(t) === family);
            const run = started ??
                sequence.tail.then(() => sequence.failed ?
                    (0, run_1.toolsetResultBlock)(toolUse, (0, run_1.notExecutedText)(family), true)
                    : runToolsetCall(params, family, toolUse, requestOptions, client, toolOtelContext));
            // A rejection (a usage error) propagates through Promise.all and stops the run. It also stops this toolset's
            // later members here, which would otherwise still be dispatched after the run has failed.
            sequence.tail = run.then(({ is_error }) => {
                if (is_error && registered)
                    sequence.failed = true;
            }, () => {
                sequence.failed = true;
            });
            calls?.set(toolUse.id, { status: 'started', result: run });
            return run;
        }
        if (started) {
            return started;
        }
        // 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 && params.tools.some((t) => (0, family_1.isRunnableToolset)(t) && (0, family_1.toolsetFamily)(t) === toolUse.name)) {
            // A call named after a toolset in the run, but with no `toolset_name`, is a toolset call with malformed
            // actions. It fails like one of the toolset's calls, so the toolset's later calls in this turn are not run.
            const sequence = sequences.get(toolUse.name) ?? { tail: Promise.resolve(), failed: false };
            sequences.set(toolUse.name, sequence);
            sequence.tail = sequence.tail.then(() => {
                sequence.failed = true;
            });
            return malformedToolsetCallResult(toolUse);
        }
        const result = runToolCall(runnable, available, toolUse, requestOptions, client, toolOtelContext);
        calls?.set(toolUse.id, { status: 'started', result });
        return result;
    }));
    return {
        role: 'user',
        content: toolResults,
    };
}
/** Runs a toolset's call with the toolset of its family in the run. */
async function runToolsetCall(params, family, toolUse, requestOptions, client, toolOtelContext) {
    const toolset = params.tools.find((t) => (0, family_1.isRunnableToolset)(t) && (0, family_1.toolsetFamily)(t) === family);
    if (!toolset) {
        return (0, run_1.toolsetResultBlock)(toolUse, `Error: Toolset '${(0, sanitize_1.quotedName)(family)}' member '${(0, sanitize_1.quotedName)(toolUse.name)}' not found`, true);
    }
    const spanOptions = {
        signal: requestOptions?.signal,
        conversationId: requestOptions?.openTelemetry?.conversationId,
        parentContext: toolOtelContext,
    };
    const outcome = await (0, tracing_2.withToolUseSpan)(client, toolset, toolUse, spanOptions, async () => {
        const block = await (0, run_1.runToolsetMember)(toolset, toolUse, { signal: requestOptions?.signal });
        return { block, content: block.content ?? [], isError: block.is_error === true };
    });
    return outcome.block;
}
async function runToolCall(runnable, available, toolUse, requestOptions, client, toolOtelContext) {
    // 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);
    }
    const spanOptions = {
        signal: requestOptions?.signal,
        conversationId: requestOptions?.openTelemetry?.conversationId,
        parentContext: toolOtelContext,
    };
    const outcome = await (0, tracing_2.withToolUseSpan)(client, tool, toolUse, spanOptions, () => (0, BetaRunnableTool_1.runRunnableTool)(tool, toolUse.input, {
        toolUse,
        toolUseBlock: toolUse,
        signal: requestOptions?.signal,
    }));
    return {
        type: 'tool_result',
        tool_use_id: toolUse.id,
        content: outcome.content,
        ...(outcome.isError ? { is_error: true } : undefined),
    };
}
/**
 * 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;
}
/**
 * A definition's family, as the API computes it for a toolset: its type without the date, and without `_toolset`.
 * So a later `browser_toolset_*` version is the browser family, and `computer_20250124` the computer family.
 */
function definitionFamily(tool) {
    return (tool.type ?? 'custom').replace(/_\d{8}$/, '').replace(/_toolset$/, '');
}
function toolNotFoundResult(toolUse) {
    return {
        type: 'tool_result',
        tool_use_id: toolUse.id,
        content: `Error: Tool '${toolUse.name}' not found`,
        is_error: true,
    };
}
/** The error for a toolset call with malformed actions. None of its actions run. */
function malformedToolsetCallResult(toolUse) {
    return {
        type: 'tool_result',
        tool_use_id: toolUse.id,
        content: `Error: the '${(0, sanitize_1.quotedName)(toolUse.name)}' toolset could not run this call${malformedActions(toolUse.input)}; nothing in the batch was executed`,
        is_error: true,
    };
}
/** What is wrong with the call's `actions`. It quotes action names but no other part of the input. */
function malformedActions(input) {
    const fields = typeof input === 'object' && input !== null ? input : {};
    if (!('actions' in fields)) {
        const action = 'action' in fields ? fields.action : undefined;
        return typeof action === 'string' ?
            `: the call has no 'actions' list (action: '${(0, sanitize_1.quotedName)(action)}')`
            : ": the call has no 'actions' list";
    }
    const { actions } = fields;
    if (typeof actions === 'string')
        return ": 'actions' is text, not a list of actions";
    if (!Array.isArray(actions))
        return ": 'actions' is not a list of actions";
    if (actions.length === 0)
        return ": the 'actions' list is empty";
    const names = [];
    for (const [i, entry] of actions.entries()) {
        const action = typeof entry === 'object' && entry !== null && 'action' in entry ? entry.action : undefined;
        if (typeof action !== 'string')
            return `: action ${i} must be an object with a string 'action' field`;
        names.push(`'${(0, sanitize_1.quotedName)(action)}'`);
    }
    const more = names.length > 10 ? `, and ${names.length - 10} more` : '';
    return `: its actions could not be run as sent (actions: ${names.slice(0, 10).join(', ')}${more})`;
}
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.
            (0, values_1.checkNever)(stopReason);
            return 'stop';
    }
}
//# sourceMappingURL=BetaToolRunner.js.map