All files / src tool.ts

98.54% Statements 203/206
92.59% Branches 125/135
100% Functions 58/58
98.47% Lines 194/197

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917                                                                                                          13x 487x 11x   476x 476x                             876x               876x 876x 876x 876x             838x 838x                       3x                           82x 82x         82x 39x   82x                     69x       9x                   19x                                     10x                                         17x 17x                                                         10x       8x 8x   8x         1x 1x                                         15x           15x       15x     15x           15x             1x 1x   1x               15x                                     838x 838x             399x               6x 6x                 314x     314x 314x 18x 18x   9x           305x 11x       294x                                                               837x         837x 837x 837x 837x 837x 837x 837x 837x                                                       314x 314x 314x   314x 159x     113x 114x                                             9x 1x   8x 1x   7x 1x   6x 1x   5x 1x   4x 1x   3x 1x   2x 1x   1x       13x 22x                 13x                             486x 5x   481x 230x 2x         228x   251x 44x 5x   39x   207x 25x     25x 25x 28x 28x 28x 2x         26x   21x 21x   182x 173x 2x   171x 171x   117x     117x 117x   162x 162x   9x                       462x 2x 2x                   294x 346x 1x   345x 345x                                             13x 13x 1x             12x 12x                   299x             16x             76x                   17x 17x 45x             5x 5x 4x   1x             1x             222x               5x                     13x                                             14x 14x 13x 13x   13x 1x 12x 2x   10x 10x   3x 1x   2x 3x           12x   13x               5x               6x                     2x 2x 5x   2x                                                         2x     2x               2x   4x   4x                   2x                     1x             1x 1x   1x   3x 2x   1x                 54x             1x             1x                     297x 297x 615x   297x 600x 15x   297x 14x          
import type { JSONSchema7 as AISDKJSONSchema } from 'ai';
import type { Tool as AnthropicTool } from '@anthropic-ai/sdk/resources';
import type { McpSdkServerConfigWithInstance } from '@anthropic-ai/claude-agent-sdk';
import type {
	ChatCompletionFunctionTool,
	ChatCompletionMessageToolCall,
	ChatCompletionToolMessageParam,
} from 'openai/resources/chat/completions';
import type { FunctionTool as OpenAIResponsesFunctionTool } from 'openai/resources/responses/responses';
import type { OverrideProperties } from 'type-fest';
import { peerDependencies } from '../package.json';
import { SUBMIT_FEEDBACK_TOOL_NAME } from './consts';
import {
	type DeclaredHeaders,
	buildRequestHeaders,
	declaredHeaders,
	sanitiseHeaderArguments,
} from './headers';
import { type EndUserSource, callMcpTool, withEndUser } from './mcp-client';
import { cloneJson, foldRootComposition } from './schema';
import type {
	AISDKToolDefinition,
	AISDKToolResult,
	ClaudeAgentSdkOptions,
	ExecuteConfig,
	ExecuteOptions,
	JsonObject,
	JSONSchema,
	McpExecuteConfig,
	ToolExecution,
	ToolParameters,
} from './types';
import { StackOneAPIError } from './utils/error-stackone-api';
import { StackOneError } from './utils/error-stackone';
import { ToolArgumentsError } from './utils/error-tool-arguments';
import { warn } from './utils/logger';
import { serializeToolResult } from './utils/serialize';
import { tryImport } from './utils/try-import';
 
/**
 * JSON Schema with type narrowed to 'object'
 * Used for tool parameter schemas which are always objects
 */
type ObjectJSONSchema = OverrideProperties<JSONSchema, { type: 'object' }>;
 
/**
 * A JSON object: a non-null, non-array value whose prototype is `Object.prototype` or `null`.
 *
 * `typeof value === 'object' && !Array.isArray(value)` alone also accepts a `Date`, `Map`,
 * `Set`, `RegExp` or class instance — none of which are JSON objects, and some of which
 * `JSON.stringify` would otherwise convert silently (a `Date` to a string, a `Map` to `{}`)
 * rather than refuse.
 */
const isPlainObject = (value: unknown): value is JsonObject => {
	if (typeof value !== 'object' || value === null || Array.isArray(value)) {
		return false;
	}
	const prototype: unknown = Object.getPrototypeOf(value);
	return prototype === Object.prototype || prototype === null;
};
 
/**
 * Base class for all tools: a name, a description, the served parameter schema, and conversions
 * to each framework's tool format.
 *
 * `BaseTool` itself cannot execute anything. The tools a {@link StackOneToolSet} returns execute
 * over MCP `tools/call`; a hand-built tool must override {@link BaseTool.execute}.
 */
export class BaseTool {
	name: string;
	description: string;
	parameters: ToolParameters;
	executeConfig: ExecuteConfig;
	#exposeExecutionMetadata = true;
 
	constructor(
		name: string,
		description: string,
		parameters: ToolParameters,
		executeConfig: ExecuteConfig,
	) {
		this.name = name;
		this.description = description;
		this.parameters = parameters;
		this.executeConfig = executeConfig;
	}
 
	/**
	 * Control whether execution metadata should be exposed in AI SDK conversions.
	 */
	setExposeExecutionMetadata(expose: boolean): this {
		this.#exposeExecutionMetadata = expose;
		return this;
	}
 
	/**
	 * Execute the tool with the provided parameters.
	 *
	 * @throws StackOneError Always, on a `BaseTool`: override this to make a tool executable.
	 */
	async execute(
		_inputParams?: JsonObject | string,
		_options?: ExecuteOptions,
	): Promise<JsonObject> {
		throw new StackOneError(
			`Tool ${JSON.stringify(this.name)} has no executor. Override execute() to run a hand-built tool.`,
		);
	}
 
	/**
	 * The served parameter schema, verbatim, as a fresh deep copy.
	 *
	 * Lossless and framework-agnostic: every root keyword the server sent — `$schema`, `$defs`,
	 * `$ref`, `title`, `additionalProperties`, top-level `oneOf`/`anyOf`/`allOf` — and every
	 * nested constraint reaches the caller unchanged, with the served `required` list. Use this
	 * for any framework that accepts JSON Schema; the provider adapters below start from it.
	 */
	toJsonSchema(): ObjectJSONSchema {
		const { required, ...rest } = cloneJson(this.parameters);
		const schema: Record<string, unknown> = {
			...rest,
			type: rest.type || 'object',
			properties: rest.properties ?? {},
		};
		if (Array.isArray(required) && required.length > 0) {
			schema.required = required;
		}
		return schema as ObjectJSONSchema;
	}
 
	/**
	 * {@link toJsonSchema}, adjusted for providers that require a plain object at the root.
	 *
	 * The OpenAI and Anthropic tool APIs both reject a parameters schema with a top-level
	 * `oneOf`/`anyOf`/`allOf`; see {@link foldRootComposition} for how it is folded rather than
	 * lost. Everything else is passed through.
	 */
	#toProviderSchema(): ObjectJSONSchema {
		return foldRootComposition(this.toJsonSchema()) as ObjectJSONSchema;
	}
 
	#createExecutionMetadata(): ToolExecution {
		return { config: cloneJson(this.executeConfig) };
	}
 
	/**
	 * Convert the tool to OpenAI Chat Completions API format.
	 *
	 * The schema is {@link toJsonSchema} with any top-level `oneOf`/`anyOf`/`allOf`/`enum`/`not`
	 * folded into the root, which Chat Completions rejects.
	 */
	toOpenAI(): ChatCompletionFunctionTool {
		return {
			type: 'function',
			function: {
				name: this.name,
				description: this.description,
				parameters: this.#toProviderSchema(),
			},
		};
	}
 
	/**
	 * Convert the tool to Anthropic format.
	 *
	 * The schema is {@link toJsonSchema} with any top-level `oneOf`/`anyOf`/`allOf` folded into
	 * the root: the Messages API rejects `input_schema` with a combinator at the top level.
	 *
	 * @see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
	 */
	toAnthropic(): AnthropicTool {
		return {
			name: this.name,
			description: this.description,
			input_schema: this.#toProviderSchema(),
		};
	}
 
	/**
	 * Convert the tool to OpenAI Responses API format.
	 *
	 * With `strict` (the default) the root schema is closed with `additionalProperties: false`,
	 * replacing whatever the server served there. The SDK does not rewrite anything nested:
	 * OpenAI's strict mode additionally requires every property to be listed in `required` and
	 * every nested object to be closed, so a served schema with optional fields is rejected by
	 * OpenAI in strict mode. Pass `{ strict: false }` for those — the served schema then goes
	 * through unchanged apart from the top-level composition fold.
	 *
	 * @see https://platform.openai.com/docs/api-reference/responses
	 * @see https://platform.openai.com/docs/guides/structured-outputs#supported-schemas
	 */
	toOpenAIResponses(options: { strict?: boolean } = {}): OpenAIResponsesFunctionTool {
		const { strict = true } = options;
		return {
			type: 'function',
			name: this.name,
			description: this.description,
			strict,
			parameters: {
				...this.#toProviderSchema(),
				...(strict ? { additionalProperties: false } : {}),
			},
		};
	}
 
	/**
	 * Convert the tool to Claude Agent SDK format.
	 * Returns a tool definition compatible with the Claude Agent SDK's tool() function.
	 *
	 * The schema is the one {@link toAnthropic} uses. Results are handed back as JSON text, with
	 * file bytes base64-encoded.
	 *
	 * @see https://docs.anthropic.com/en/docs/agents-and-tools/claude-agent-sdk
	 */
	async toClaudeAgentSdkTool(): Promise<{
		name: string;
		description: string;
		inputSchema: Record<string, unknown>;
		handler: (
			args: Record<string, unknown>,
		) => Promise<{ content: Array<{ type: 'text'; text: string }> }>;
	}> {
		const ai = await tryImport<typeof import('ai')>(
			'ai',
			`npm install ai (requires ${peerDependencies.ai})`,
		);
		const inputSchema = ai.jsonSchema(this.#toProviderSchema() as AISDKJSONSchema);
		const execute = this.execute.bind(this);
 
		return {
			name: this.name,
			description: this.description,
			inputSchema,
			handler: async (args: Record<string, unknown>) => {
				const result = await execute(args as JsonObject);
				return {
					content: [{ type: 'text' as const, text: serializeToolResult(result) }],
				};
			},
		};
	}
 
	/**
	 * Convert the tool to AI SDK format.
	 *
	 * The schema is {@link toJsonSchema} with top-level composition folded (the AI SDK hands it
	 * to whichever provider you use, and OpenAI and Anthropic both reject it), and with the root
	 * closed by `additionalProperties: false`, replacing whatever the server served there. This
	 * matches what the AI SDK's OpenAI provider needs for strict structured outputs; nested
	 * objects are passed through as served.
	 */
	async toAISDK(
		options: { executable?: boolean; execution?: ToolExecution | false } = {
			executable: true,
		},
	): Promise<AISDKToolResult> {
		const schema = {
			...this.#toProviderSchema(),
			additionalProperties: false,
		} as AISDKJSONSchema;
 
		/** AI SDK is optional dependency, import only when needed */
		const ai = await tryImport<typeof import('ai')>(
			'ai',
			`npm install ai (requires ${peerDependencies.ai})`,
		);
		const schemaObject = ai.jsonSchema(schema);
 
		const executionOption =
			options.execution !== undefined
				? options.execution
				: this.#exposeExecutionMetadata
					? this.#createExecutionMetadata()
					: false;
 
		const toolDefinition = {
			inputSchema: schemaObject,
			description: this.description,
			execution: executionOption !== false ? executionOption : undefined,
			execute:
				(options.executable ?? true)
					? async (args: Record<string, unknown>) => {
							try {
								return await this.execute(args as JsonObject);
							} catch (error) {
								return `Error executing tool: ${
									error instanceof Error ? error.message : String(error)
								}`;
							}
						}
					: undefined,
		} satisfies AISDKToolDefinition;
 
		return {
			[this.name]: toolDefinition,
		} satisfies AISDKToolResult;
	}
}
 
/**
 * A tool served by StackOne, bound to the account it was listed for.
 */
export class StackOneTool extends BaseTool {
	#accountId: string | undefined;
 
	constructor(
		name: string,
		description: string,
		parameters: ToolParameters,
		executeConfig: ExecuteConfig,
		accountId?: string,
	) {
		super(name, description, parameters, executeConfig);
		this.#accountId = accountId;
	}
 
	/**
	 * Get the account this tool executes against.
	 */
	getAccountId(): string | undefined {
		return this.#accountId;
	}
 
	/**
	 * Rebind this tool to another account. The tools `fetchTools()` returns are built fresh per
	 * call, so this never affects another caller's tools.
	 */
	setAccountId(accountId: string | undefined): this {
		this.#accountId = accountId;
		return this;
	}
 
	/**
	 * Parse tool arguments: a JSON string, an object, or nothing.
	 *
	 * @throws ToolArgumentsError If the arguments are not a JSON object.
	 */
	protected parseArguments(input: JsonObject | string | undefined): JsonObject {
		Iif (input === undefined || input === null) {
			return {};
		}
		let parsed: unknown = input;
		if (typeof input === 'string') {
			try {
				parsed = JSON.parse(input);
			} catch (error) {
				throw new ToolArgumentsError(
					`Invalid JSON in arguments for ${JSON.stringify(this.name)}: ${error instanceof Error ? error.message : String(error)}`,
					{ cause: error },
				);
			}
		}
		if (!isPlainObject(parsed)) {
			throw new ToolArgumentsError(
				`Tool arguments for ${JSON.stringify(this.name)} must be a JSON object`,
			);
		}
		return { ...parsed };
	}
}
 
/**
 * A tool executed over MCP `tools/call`, on the endpoint and account that listed it. Every tool a
 * {@link StackOneToolSet} returns is one: per-action tools, the `search_execute` meta tools, and
 * `stackone_submit_feedback`.
 */
export class StackOneMcpTool extends StackOneTool {
	readonly #endpoint: string;
	readonly #apiKey: string;
	readonly #extraHeaders: Record<string, string>;
	readonly #endUsers: EndUserSource | undefined;
	readonly #timeout: number;
	readonly #declaredHeaders: DeclaredHeaders;
 
	constructor(options: {
		name: string;
		description: string;
		parameters: ToolParameters;
		endpoint: string;
		apiKey: string;
		accountId?: string;
		timeout: number;
		extraHeaders?: Record<string, string>;
		/**
		 * Where the end user of an account, sent as `x-end-user-id`, comes from. Read per call, so
		 * a tool rebound with `setAccountId` sends its new account's end user.
		 */
		endUsers?: EndUserSource;
	}) {
		const executeConfig = {
			kind: 'mcp',
			url: options.endpoint,
			toolName: options.name,
		} as const satisfies McpExecuteConfig;
		super(options.name, options.description, options.parameters, executeConfig, options.accountId);
		this.setExposeExecutionMetadata(false);
		this.#endpoint = options.endpoint;
		this.#apiKey = options.apiKey;
		this.#extraHeaders = { ...options.extraHeaders };
		this.#endUsers = options.endUsers;
		this.#timeout = options.timeout;
		this.#declaredHeaders = declaredHeaders(options.parameters.properties);
	}
 
	/**
	 * Call the tool.
	 *
	 * Arguments are sent as given; the server maps them onto the action. Header arguments — the
	 * entries of a `headers` object, and `headers_<name>` arguments — are the exception: these
	 * arguments are model-controlled, so each is forwarded only if this tool's own schema declares
	 * it, and `Authorization`, `x-account-id`, `User-Agent` and `x-end-user-id` never are. A
	 * `headers` property served as an object with no `properties`, as on `*_execute_action`,
	 * declares every name.
	 *
	 * @returns The result as the server wrote it: for an action tool, an `ActionResult`,
	 *   `{ isError: false, result, defenderMetadata?, policyMetadata? }`. A file action's `result`
	 *   is the server's single-use `download_url`, not the file. Typed `JsonObject` rather than
	 *   `ActionResult`, since a `*_search_actions` meta tool returns bare JSON and `dryRun` the
	 *   call it would send: assert `ActionResult` on an action tool.
	 * @throws ToolArgumentsError If the arguments are not a JSON object or cannot be encoded as
	 *   JSON.
	 * @throws StackOneAPIError If the result carries `isError`, with the status from its payload,
	 *   or the endpoint answers with an HTTP error.
	 * @throws ToolSetLoadError If the endpoint cannot be reached or does not answer in time.
	 */
	override async execute(
		inputParams?: JsonObject | string,
		options?: ExecuteOptions,
	): Promise<JsonObject> {
		const parsed = this.parseArguments(inputParams);
		const args = sanitiseHeaderArguments(parsed, this.#declaredHeaders);
		assertEncodable(this.name, args);
 
		if (options?.dryRun) {
			return { url: this.#endpoint, method: 'tools/call', name: this.name, arguments: args };
		}
 
		const accountId = this.getAccountId();
		return withEndUser(accountId, this.#endUsers, (endUserId) =>
			callMcpTool(
				{
					endpoint: this.#endpoint,
					headers: buildRequestHeaders({
						apiKey: this.#apiKey,
						accountId,
						endUserId,
						extraHeaders: this.#extraHeaders,
					}),
					timeout: this.#timeout,
				},
				this.name,
				args,
			),
		);
	}
}
 
/**
 * Describe why `value` is not a JSON value, for the message {@link assertJsonValue} throws.
 */
function describeUnencodableType(value: unknown): string {
	if (typeof value === 'bigint') {
		return 'a bigint';
	}
	if (typeof value === 'symbol') {
		return 'a symbol';
	}
	if (typeof value === 'function') {
		return 'a function';
	}
	if (value instanceof Date) {
		return 'a Date';
	}
	if (value instanceof Map) {
		return 'a Map';
	}
	if (value instanceof Set) {
		return 'a Set';
	}
	if (value instanceof RegExp) {
		return 'a RegExp';
	}
	if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
		return 'binary data';
	}
	return `an instance of ${(value as object).constructor?.name ?? typeof value}`;
}
 
/** A ToolArgumentsError for arguments that cannot be encoded as JSON. */
const unencodable = (toolName: string, detail: string): ToolArgumentsError =>
	new ToolArgumentsError(
		`Arguments for ${JSON.stringify(toolName)} could not be encoded as JSON: ${detail}`,
	);
 
/**
 * A UTF-16 surrogate without its other half: what a model emits when a token boundary splits an
 * emoji. `JSON.stringify` escapes it as `\ud800` rather than refusing it, but it is not Unicode
 * text, so it cannot be encoded as UTF-8 — Python refuses it, and so does this.
 */
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
 
/**
 * Walk `value`, throwing if it, or anything nested inside it, is not a JSON value.
 *
 * Only `null`, booleans, finite numbers, strings, arrays and plain objects are JSON values.
 * `JSON.stringify` quietly converts some of what this refuses — a `Date` to a string, a `Map`
 * or `Set` to `{}` — rather than refuse it, which would silently send the model a value it
 * never supplied. `undefined` is dropped, as `JSON.stringify` drops it from an object, but is
 * refused inside an array, where `JSON.stringify` would send it as `null`. A string or key
 * holding a lone surrogate is refused too.
 *
 * @throws ToolArgumentsError If `value` is not a JSON value.
 */
function assertJsonValue(value: unknown, path: string, toolName: string, seen: Set<object>): void {
	if (value === null || typeof value === 'boolean') {
		return;
	}
	if (typeof value === 'string') {
		if (LONE_SURROGATE.test(value)) {
			throw unencodable(
				toolName,
				`a string holding a lone surrogate is not Unicode text (at ${JSON.stringify(path)})`,
			);
		}
		return;
	}
	if (typeof value === 'number') {
		if (!Number.isFinite(value)) {
			throw unencodable(toolName, `${value} is not a JSON number`);
		}
		return;
	}
	if (Array.isArray(value)) {
		Iif (seen.has(value)) {
			throw unencodable(toolName, `circular reference (at ${JSON.stringify(path)})`);
		}
		seen.add(value);
		for (let index = 0; index < value.length; index++) {
			const item = value[index];
			const itemPath = `${path}[${index}]`;
			if (item === undefined) {
				throw unencodable(
					toolName,
					`undefined is not a JSON value (at ${JSON.stringify(itemPath)})`,
				);
			}
			assertJsonValue(item, itemPath, toolName, seen);
		}
		seen.delete(value);
		return;
	}
	if (isPlainObject(value)) {
		if (seen.has(value)) {
			throw unencodable(toolName, `circular reference (at ${JSON.stringify(path)})`);
		}
		seen.add(value);
		for (const [key, entry] of Object.entries(value)) {
			// An object property set to undefined is treated as absent, as JSON.stringify treats it.
			Iif (entry === undefined) {
				continue;
			}
			assertJsonKey(key, path, toolName);
			assertJsonValue(entry, `${path}.${key}`, toolName, seen);
		}
		seen.delete(value);
		return;
	}
	throw unencodable(
		toolName,
		`${describeUnencodableType(value)} is not a JSON value (at ${JSON.stringify(path)})`,
	);
}
 
/**
 * Throw if an object key holds a lone surrogate. `path` is where the object is, if anywhere.
 *
 * @throws ToolArgumentsError If the key is not Unicode text.
 */
function assertJsonKey(key: string, path: string | undefined, toolName: string): void {
	if (LONE_SURROGATE.test(key)) {
		const where = path === undefined ? '' : ` (in ${JSON.stringify(path)})`;
		throw unencodable(toolName, `a key holding a lone surrogate is not Unicode text${where}`);
	}
}
 
/**
 * Throw if `args` cannot be sent as JSON: see {@link assertJsonValue}.
 *
 * @throws ToolArgumentsError If any value, however deeply nested, is not a JSON value.
 */
function assertEncodable(toolName: string, args: JsonObject): void {
	for (const [key, value] of Object.entries(args)) {
		if (value === undefined) {
			continue;
		}
		assertJsonKey(key, undefined, toolName);
		assertJsonValue(value, key, toolName, new Set());
	}
}
 
/**
 * A Chat Completions tool call: the `openai` package's own object, or the same shape as a plain
 * object (for example one read back from storage).
 */
type OpenAIToolCall =
	| ChatCompletionMessageToolCall
	| {
			id: string;
			type?: string;
			function?: { name: string; arguments?: string | JsonObject | null };
	  };
 
/** Pull the id, name and arguments out of a tool call, or say why it cannot be run. */
function readOpenAIToolCall(call: OpenAIToolCall): {
	id: string;
	name: string;
	args: string | JsonObject;
	unsupported?: string;
} {
	const id = String(call.id ?? '');
	if (!('function' in call) || !call.function) {
		return {
			id,
			name: '',
			args: {},
			unsupported: `Unsupported tool call type "${String(call.type)}"`,
		};
	}
	const { name, arguments: args } = call.function;
	return { id, name: String(name), args: args || {} };
}
 
/**
 * Collection of tools with utility methods
 */
export class Tools implements Iterable<BaseTool> {
	private tools: BaseTool[];
 
	constructor(tools: BaseTool[]) {
		this.tools = [...tools];
	}
 
	/**
	 * Get the number of tools in the collection
	 */
	get length(): number {
		return this.tools.length;
	}
 
	/**
	 * Get a tool by name. When two accounts serve the same name, this is the first one listed.
	 */
	getTool(name: string): BaseTool | undefined {
		return this.tools.find((tool) => tool.name === name);
	}
 
	/**
	 * The first tool of each name, in order: the one {@link getTool} returns. Every adapter is
	 * built from these, so a model can never call a tool that a lookup by name would not find —
	 * an AI SDK record would keep the last, OpenAI would get two functions of one name, and the
	 * Claude Agent SDK refuses the second outright.
	 */
	#uniqueTools(): BaseTool[] {
		warnOnDuplicateNames(this.tools);
		const seen = new Set<string>();
		return this.tools.filter((tool) => !seen.has(tool.name) && seen.add(tool.name));
	}
 
	/**
	 * Get a StackOne tool by name
	 */
	getStackOneTool(name: string): StackOneTool {
		const tool = this.getTool(name);
		if (tool instanceof StackOneTool) {
			return tool;
		}
		throw new StackOneError(`Tool ${name} is not a StackOne tool`);
	}
 
	/**
	 * Check if a tool is a StackOne tool
	 */
	isStackOneTool(tool: BaseTool): tool is StackOneTool {
		return tool instanceof StackOneTool;
	}
 
	/**
	 * Get all StackOne tools in the collection
	 */
	getStackOneTools(): StackOneTool[] {
		return this.tools.filter((tool): tool is StackOneTool => tool instanceof StackOneTool);
	}
 
	/**
	 * Convert all tools to pure JSON Schema format
	 * Returns an array of objects with name, description, and schema
	 */
	toJsonSchema(): Array<{ name: string; description: string; parameters: JSONSchema }> {
		return this.#uniqueTools().map((tool) => ({
			name: tool.name,
			description: tool.description,
			parameters: tool.toJsonSchema(),
		}));
	}
 
	/**
	 * Convert all tools to OpenAI Chat Completions API format
	 */
	toOpenAI(): ChatCompletionFunctionTool[] {
		return this.#uniqueTools().map((tool) => tool.toOpenAI());
	}
 
	/**
	 * Run a Chat Completions response's tool calls and return the `tool` messages to send back.
	 *
	 * The counterpart to {@link toOpenAI}: that turns these tools into what OpenAI accepts, this
	 * turns what OpenAI returns back into messages for it. Append the assistant message first,
	 * then these, in order:
	 *
	 * ```typescript
	 * const message = response.choices[0].message;
	 * messages.push(message, ...(await tools.executeOpenAIToolCalls(message.tool_calls)));
	 * ```
	 *
	 * A failed call does not throw. Its error — with the server's response body, when there is
	 * one — becomes the tool message's content, so the model can read why and retry. A call to a
	 * tool that is not in this collection is reported the same way. Calls run one at a time, in
	 * order. File bytes in a result are base64-encoded.
	 */
	async executeOpenAIToolCalls(
		toolCalls: readonly OpenAIToolCall[] | null | undefined,
	): Promise<ChatCompletionToolMessageParam[]> {
		const messages: ChatCompletionToolMessageParam[] = [];
		for (const call of toolCalls ?? []) {
			const { id, name, args, unsupported } = readOpenAIToolCall(call);
			const tool = unsupported ? undefined : this.getTool(name);
			let result: unknown;
			if (unsupported) {
				result = { error: unsupported };
			} else if (!tool) {
				result = { error: `Unknown tool ${JSON.stringify(name)}` };
			} else {
				try {
					result = await tool.execute(args);
				} catch (error) {
					if (!(error instanceof StackOneError)) {
						throw error;
					}
					const body = error instanceof StackOneAPIError ? error.responseBody : undefined;
					result = {
						error: error.message,
						...(body != null && body !== '' ? { response_body: body } : {}),
					};
				}
			}
			messages.push({ role: 'tool', tool_call_id: id, content: serializeToolResult(result) });
		}
		return messages;
	}
 
	/**
	 * Convert all tools to Anthropic format
	 * @see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
	 */
	toAnthropic(): AnthropicTool[] {
		return this.#uniqueTools().map((tool) => tool.toAnthropic());
	}
 
	/**
	 * Convert all tools to OpenAI Responses API format
	 * @see https://platform.openai.com/docs/api-reference/responses
	 */
	toOpenAIResponses(options: { strict?: boolean } = {}): OpenAIResponsesFunctionTool[] {
		return this.#uniqueTools().map((tool) => tool.toOpenAIResponses(options));
	}
 
	/**
	 * Convert all tools to AI SDK format
	 */
	async toAISDK(
		options: { executable?: boolean; execution?: ToolExecution | false } = {
			executable: true,
		},
	): Promise<AISDKToolResult> {
		const result: AISDKToolResult = {};
		for (const tool of this.#uniqueTools()) {
			Object.assign(result, await tool.toAISDK(options));
		}
		return result;
	}
 
	/**
	 * Convert all tools to Claude Agent SDK format.
	 * Returns an MCP server configuration that can be passed to the
	 * Claude Agent SDK query() function's mcpServers option.
	 *
	 * @example
	 * ```typescript
	 * const tools = await toolset.fetchTools();
	 * const mcpServer = await tools.toClaudeAgentSdk();
	 *
	 * const result = query({
	 *   prompt: 'Get employee info',
	 *   options: {
	 *     model: 'claude-sonnet-4-5-20250929',
	 *     mcpServers: {
	 *       'stackone-tools': mcpServer,
	 *     },
	 *   },
	 * });
	 * ```
	 *
	 * @see https://docs.anthropic.com/en/docs/agents-and-tools/claude-agent-sdk
	 */
	async toClaudeAgentSdk(
		options: ClaudeAgentSdkOptions = {},
	): Promise<McpSdkServerConfigWithInstance> {
		const { serverName = 'stackone-tools', serverVersion = '1.0.0' } = options;
 
		// Import the Claude Agent SDK dynamically
		const claudeAgentSdk = await tryImport<typeof import('@anthropic-ai/claude-agent-sdk')>(
			'@anthropic-ai/claude-agent-sdk',
			`npm install @anthropic-ai/claude-agent-sdk (requires ${peerDependencies['@anthropic-ai/claude-agent-sdk']})`,
		);
 
		// Convert all tools to Claude Agent SDK format
		// We use type assertions here because the Zod types from our dynamic import
		// don't perfectly match the Claude Agent SDK's expected types at compile time
		const sdkTools = await Promise.all(
			this.#uniqueTools().map(async (baseTool) => {
				const toolDef = await baseTool.toClaudeAgentSdkTool();
				// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Dynamic Zod schema types
				return claudeAgentSdk.tool(
					toolDef.name,
					toolDef.description,
					toolDef.inputSchema as any,
					toolDef.handler as any,
				);
			}),
		);
 
		// Create and return the MCP server
		return claudeAgentSdk.createSdkMcpServer({
			name: serverName,
			version: serverVersion,
			tools: sdkTools,
		});
	}
 
	/**
	 * Filter tools by a predicate function
	 */
	filter(predicate: (tool: BaseTool) => boolean): Tools {
		return new Tools(this.tools.filter(predicate));
	}
 
	/**
	 * Iterator implementation
	 */
	[Symbol.iterator](): Iterator<BaseTool> {
		let index = 0;
		const tools = this.tools;
 
		return {
			next(): IteratorResult<BaseTool> {
				if (index < tools.length) {
					return { value: tools[index++], done: false };
				}
				return { value: undefined as unknown as BaseTool, done: true };
			},
		};
	}
 
	/**
	 * Convert to array
	 */
	toArray(): BaseTool[] {
		return [...this.tools];
	}
 
	/**
	 * Map tools to a new array
	 */
	map<T>(mapper: (tool: BaseTool) => T): T[] {
		return this.tools.map(mapper);
	}
 
	/**
	 * Execute a function for each tool
	 */
	forEach(callback: (tool: BaseTool) => void): void {
		this.tools.forEach(callback);
	}
}
 
/**
 * Two accounts on one provider serve identically named tools. Every lookup and adapter keeps the
 * first one listed — the lowest account id — and drops the rest, so warn that the others are
 * unreachable by name: once when the tools are fetched, and again from each adapter that drops
 * them.
 */
export function warnOnDuplicateNames(tools: readonly BaseTool[]): void {
	const counts = new Map<string, number>();
	for (const tool of tools) {
		counts.set(tool.name, (counts.get(tool.name) ?? 0) + 1);
	}
	const clashing = [...counts]
		.filter(([name, count]) => count > 1 && name !== SUBMIT_FEEDBACK_TOOL_NAME)
		.map(([name]) => name)
		.sort();
	if (clashing.length > 0) {
		warn(
			`${clashing.length} tool name(s) are served by more than one account (${clashing.slice(0, 5).join(', ')}). The first one listed, from the lowest account id, is used — pass account ids to choose.`,
		);
	}
}