deepagents 1.6.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +155 -6687
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +104 -88
- package/dist/index.d.ts +78 -63
- package/dist/index.js +156 -6682
- package/dist/index.js.map +1 -1
- package/package.json +11 -11
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["limit","path","path","limit","z","result","path","limit","ToolMessage","Command","toolTokenLimitBeforeEvict","Command","ToolMessage","HumanMessage","z","SystemMessage","AIMessage","ToolMessage","RemoveMessage","REMOVE_ALL_MESSAGES","z","path","getBackend","z","validateSkillName","getBackend","crypto","str","str","str","parseResponse","Page","client","process","fetch","str","path","Messages","tool","isAutoParsableTool","hasAutoParseableInput","parseToolCall","inputTool","_EventStream_handleError","isAutoParsableTool","tool","content","name","_AbstractChatCompletionRunner_getFinalContent","_AbstractChatCompletionRunner_getFinalMessage","_AbstractChatCompletionRunner_getFinalFunctionToolCall","_AbstractChatCompletionRunner_getFinalFunctionToolCallResult","_AbstractChatCompletionRunner_calculateTotalUsage","_AbstractChatCompletionRunner_validateParams","_AbstractChatCompletionRunner_stringifyFunctionCallResult","escape","e","_ChatCompletionStream_beginRequest","_ChatCompletionStream_getChoiceEventState","_ChatCompletionStream_addChunk","_ChatCompletionStream_emitToolCallDoneEvent","tool","isAutoParsableTool","_ChatCompletionStream_emitContentDoneEvents","_ChatCompletionStream_endRequest","_ChatCompletionStream_getAutoParseableResponseFormat","_ChatCompletionStream_accumulateChatCompletion","_a","rest","content","refusal","hasAutoParseableInput","index","id","assertNever","Completions","MessagesAPI.Messages","Messages","CompletionsAPI.Completions","Completions","TranscriptionsAPI.Transcriptions","TranslationsAPI.Translations","SpeechAPI.Speech","Sessions","Realtime","SessionsAPI.Sessions","TranscriptionSessionsAPI.TranscriptionSessions","Sessions","Threads","SessionsAPI.Sessions","ThreadsAPI.Threads","Threads","_a","_AssistantStream_addEvent","_AssistantStream_endRequest","_AssistantStream_handleMessage","_AssistantStream_handleRunStep","_AssistantStream_handleEvent","_AssistantStream_accumulateRunStep","_AssistantStream_accumulateMessage","_AssistantStream_accumulateContent","_AssistantStream_handleRun","Runs","StepsAPI.Steps","RunsAPI.Runs","MessagesAPI.Messages","Runs","RealtimeAPI.Realtime","ChatKitAPI.ChatKit","AssistantsAPI.Assistants","ThreadsAPI.Threads","Realtime","Files","ContentAPI.Content","FilesAPI.Files","Files","ItemsAPI.Items","Embeddings","response","OutputItemsAPI.OutputItems","RunsAPI.Runs","Files","Graders","GradersAPI.Graders","Graders","Checkpoints","PermissionsAPI.Permissions","CheckpointsAPI.Checkpoints","MethodsAPI.Methods","JobsAPI.Jobs","CheckpointsAPI.Checkpoints","AlphaAPI.Alpha","Checkpoints","GraderModelsAPI.GraderModels","ClientSecretsAPI.ClientSecrets","CallsAPI.Calls","parseToolCall","content","output","tool","_ResponseStream_beginRequest","_ResponseStream_addEvent","event","_ResponseStream_endRequest","_ResponseStream_accumulateResponse","InputItemsAPI.InputItems","InputTokensAPI.InputTokens","PartsAPI.Parts","FilesAPI.Files","FileBatchesAPI.FileBatches","_Webhooks_validateSecret","_Webhooks_getRequiredHeader","API.Completions","API.Chat","API.Embeddings","API.Files","API.Images","API.Audio","API.Moderations","API.Models","API.FineTuning","API.Graders","API.VectorStores","API.Webhooks","API.Beta","API.Batches","API.Uploads","API.Responses","API.Realtime","API.Conversations","API.Evals","API.Containers","API.Videos","Errors.OpenAIError","Shims.getDefaultFetch","Opts.FallbackEncoder","qs.stringify","path","opts","Errors.APIUserAbortError","Errors.APIConnectionTimeoutError","Errors.APIConnectionError","retryMessage","Shims.CancelReadableStream","Page","Pagination.PagePromise","Shims.ReadableStreamFrom","_OpenAI_baseURLOverridden","Errors.APIError","Errors.NotFoundError","Errors.ConflictError","Errors.RateLimitError","Errors.BadRequestError","Errors.AuthenticationError","Errors.InternalServerError","Errors.PermissionDeniedError","Errors.UnprocessableEntityError","Errors.InvalidWebhookSignatureError","Uploads.toFile","Embeddings","Files","UploadsAPIUploads","z","z","z","z","z","path","limit","fsSync","path","fs","limit","path","limit","limit","path","SystemMessage","path","fs","os","z","fs","MAX_SKILL_FILE_SIZE","MAX_SKILL_NAME_LENGTH","MAX_SKILL_DESCRIPTION_LENGTH","fs","path"],"sources":["../src/backends/protocol.ts","../src/backends/utils.ts","../src/backends/state.ts","../src/middleware/fs.ts","../src/middleware/subagents.ts","../src/middleware/patch_tool_calls.ts","../src/middleware/memory.ts","../src/middleware/skills.ts","../src/middleware/utils.ts","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/tslib.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/utils/uuid.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/errors.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/core/error.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/utils/values.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/utils/sleep.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/version.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/detect-platform.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/shims.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/request-options.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/qs/formats.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/qs/utils.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/qs/stringify.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/utils/bytes.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/decoders/line.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/utils/log.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/core/streaming.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/parse.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/core/api-promise.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/core/pagination.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/uploads.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/to-file.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/core/resource.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/utils/path.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/chat/completions/messages.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/parser.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/chatCompletionUtils.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/EventStream.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/RunnableFunction.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/AbstractChatCompletionRunner.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/ChatCompletionRunner.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/_vendor/partial-json-parser/parser.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/ChatCompletionStream.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/ChatCompletionStreamingRunner.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/chat/completions/completions.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/chat/chat.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/headers.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/audio/speech.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/audio/transcriptions.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/audio/translations.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/audio/audio.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/batches.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/assistants.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/realtime/sessions.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/realtime/transcription-sessions.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/realtime/realtime.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/chatkit/sessions.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/chatkit/threads.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/chatkit/chatkit.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/threads/messages.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/threads/runs/steps.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/utils/base64.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/internal/utils/env.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/AssistantStream.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/threads/runs/runs.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/threads/threads.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/beta/beta.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/completions.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/containers/files/content.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/containers/files/files.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/containers/containers.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/conversations/items.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/conversations/conversations.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/embeddings.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/evals/runs/output-items.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/evals/runs/runs.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/evals/evals.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/files.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/fine-tuning/methods.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/fine-tuning/alpha/graders.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/fine-tuning/alpha/alpha.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/fine-tuning/checkpoints/permissions.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/fine-tuning/checkpoints/checkpoints.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/fine-tuning/jobs/checkpoints.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/fine-tuning/jobs/jobs.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/fine-tuning/fine-tuning.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/graders/grader-models.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/graders/graders.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/images.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/models.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/moderations.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/realtime/calls.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/realtime/client-secrets.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/realtime/realtime.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/ResponsesParser.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/responses/ResponseStream.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/responses/input-items.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/responses/input-tokens.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/responses/responses.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/uploads/parts.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/uploads/uploads.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/lib/Util.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/vector-stores/file-batches.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/vector-stores/files.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/vector-stores/vector-stores.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/videos.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/resources/webhooks.mjs","../../../node_modules/.pnpm/openai@6.16.0_zod@4.3.6/node_modules/openai/client.mjs","../../../node_modules/.pnpm/@langchain+openai@1.2.3_@langchain+core@1.1.16_openai@6.16.0_zod@4.3.6__/node_modules/@langchain/openai/dist/tools/computerUse.js","../../../node_modules/.pnpm/@langchain+openai@1.2.3_@langchain+core@1.1.16_openai@6.16.0_zod@4.3.6__/node_modules/@langchain/openai/dist/tools/localShell.js","../../../node_modules/.pnpm/@langchain+openai@1.2.3_@langchain+core@1.1.16_openai@6.16.0_zod@4.3.6__/node_modules/@langchain/openai/dist/tools/shell.js","../../../node_modules/.pnpm/@langchain+openai@1.2.3_@langchain+core@1.1.16_openai@6.16.0_zod@4.3.6__/node_modules/@langchain/openai/dist/tools/applyPatch.js","../src/middleware/summarization.ts","../src/backends/store.ts","../src/backends/filesystem.ts","../src/backends/composite.ts","../src/backends/sandbox.ts","../src/agent.ts","../src/config.ts","../src/middleware/agent-memory.ts","../src/skills/loader.ts"],"sourcesContent":["/**\n * Protocol definition for pluggable memory backends.\n *\n * This module defines the BackendProtocol that all backend implementations\n * must follow. Backends can store files in different locations (state, filesystem,\n * database, etc.) and provide a uniform interface for file operations.\n */\n\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\nexport type MaybePromise<T> = T | Promise<T>;\n\n/**\n * Structured file listing info.\n *\n * Minimal contract used across backends. Only \"path\" is required.\n * Other fields are best-effort and may be absent depending on backend.\n */\nexport interface FileInfo {\n /** File path */\n path: string;\n /** Whether this is a directory */\n is_dir?: boolean;\n /** File size in bytes (approximate) */\n size?: number;\n /** ISO 8601 timestamp of last modification */\n modified_at?: string;\n}\n\n/**\n * Structured grep match entry.\n */\nexport interface GrepMatch {\n /** File path where match was found */\n path: string;\n /** Line number (1-indexed) */\n line: number;\n /** The matching line text */\n text: string;\n}\n\n/**\n * File data structure used by backends.\n *\n * All file data is represented as objects with this structure:\n */\nexport interface FileData {\n /** Lines of text content */\n content: string[];\n /** ISO format timestamp of creation */\n created_at: string;\n /** ISO format timestamp of last modification */\n modified_at: string;\n}\n\n/**\n * Result from backend write operations.\n *\n * Checkpoint backends populate filesUpdate with {file_path: file_data} for LangGraph state.\n * External backends set filesUpdate to null (already persisted to disk/S3/database/etc).\n */\nexport interface WriteResult {\n /** Error message on failure, undefined on success */\n error?: string;\n /** File path of written file, undefined on failure */\n path?: string;\n /**\n * State update dict for checkpoint backends, null for external storage.\n * Checkpoint backends populate this with {file_path: file_data} for LangGraph state.\n * External backends set null (already persisted to disk/S3/database/etc).\n */\n filesUpdate?: Record<string, FileData> | null;\n /** Metadata for the write operation, attached to the ToolMessage */\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Result from backend edit operations.\n *\n * Checkpoint backends populate filesUpdate with {file_path: file_data} for LangGraph state.\n * External backends set filesUpdate to null (already persisted to disk/S3/database/etc).\n */\nexport interface EditResult {\n /** Error message on failure, undefined on success */\n error?: string;\n /** File path of edited file, undefined on failure */\n path?: string;\n /**\n * State update dict for checkpoint backends, null for external storage.\n * Checkpoint backends populate this with {file_path: file_data} for LangGraph state.\n * External backends set null (already persisted to disk/S3/database/etc).\n */\n filesUpdate?: Record<string, FileData> | null;\n /** Number of replacements made, undefined on failure */\n occurrences?: number;\n /** Metadata for the edit operation, attached to the ToolMessage */\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Result of code execution.\n * Simplified schema optimized for LLM consumption.\n */\nexport interface ExecuteResponse {\n /** Combined stdout and stderr output of the executed command */\n output: string;\n /** The process exit code. 0 indicates success, non-zero indicates failure */\n exitCode: number | null;\n /** Whether the output was truncated due to backend limitations */\n truncated: boolean;\n}\n\n/**\n * Standardized error codes for file upload/download operations.\n */\nexport type FileOperationError =\n | \"file_not_found\"\n | \"permission_denied\"\n | \"is_directory\"\n | \"invalid_path\";\n\n/**\n * Result of a single file download operation.\n */\nexport interface FileDownloadResponse {\n /** The file path that was requested */\n path: string;\n /** File contents as Uint8Array on success, null on failure */\n content: Uint8Array | null;\n /** Standardized error code on failure, null on success */\n error: FileOperationError | null;\n}\n\n/**\n * Result of a single file upload operation.\n */\nexport interface FileUploadResponse {\n /** The file path that was requested */\n path: string;\n /** Standardized error code on failure, null on success */\n error: FileOperationError | null;\n}\n\n/**\n * Protocol for pluggable memory backends (single, unified).\n *\n * Backends can store files in different locations (state, filesystem, database, etc.)\n * and provide a uniform interface for file operations.\n *\n * All file data is represented as objects with the FileData structure.\n *\n * Methods can return either direct values or Promises, allowing both\n * synchronous and asynchronous implementations.\n */\nexport interface BackendProtocol {\n /**\n * Structured listing with file metadata.\n *\n * Lists files and directories in the specified directory (non-recursive).\n * Directories have a trailing / in their path and is_dir=true.\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects for files and directories directly in the directory\n */\n lsInfo(path: string): MaybePromise<FileInfo[]>;\n\n /**\n * Read file content with line numbers or an error string.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed), default 0\n * @param limit - Maximum number of lines to read, default 500\n * @returns Formatted file content with line numbers, or error message\n */\n read(filePath: string, offset?: number, limit?: number): MaybePromise<string>;\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n readRaw(filePath: string): MaybePromise<FileData>;\n\n /**\n * Structured search results or error string for invalid input.\n *\n * Searches file contents for a regex pattern.\n *\n * @param pattern - Regex pattern to search for\n * @param path - Base path to search from (default: null)\n * @param glob - Optional glob pattern to filter files (e.g., \"*.py\")\n * @returns List of GrepMatch objects or error string for invalid regex\n */\n grepRaw(\n pattern: string,\n path?: string | null,\n glob?: string | null,\n ): MaybePromise<GrepMatch[] | string>;\n\n /**\n * Structured glob matching returning FileInfo objects.\n *\n * @param pattern - Glob pattern (e.g., `*.py`, `**\\/*.ts`)\n * @param path - Base path to search from (default: \"/\")\n * @returns List of FileInfo objects matching the pattern\n */\n globInfo(pattern: string, path?: string): MaybePromise<FileInfo[]>;\n\n /**\n * Create a new file.\n *\n * @param filePath - Absolute file path\n * @param content - File content as string\n * @returns WriteResult with error populated on failure\n */\n write(filePath: string, content: string): MaybePromise<WriteResult>;\n\n /**\n * Edit a file by replacing string occurrences.\n *\n * @param filePath - Absolute file path\n * @param oldString - String to find and replace\n * @param newString - Replacement string\n * @param replaceAll - If true, replace all occurrences (default: false)\n * @returns EditResult with error, path, filesUpdate, and occurrences\n */\n edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll?: boolean,\n ): MaybePromise<EditResult>;\n\n /**\n * Upload multiple files.\n * Optional - backends that don't support file upload can omit this.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n uploadFiles?(\n files: Array<[string, Uint8Array]>,\n ): MaybePromise<FileUploadResponse[]>;\n\n /**\n * Download multiple files.\n * Optional - backends that don't support file download can omit this.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n downloadFiles?(paths: string[]): MaybePromise<FileDownloadResponse[]>;\n}\n\n/**\n * Protocol for sandboxed backends with isolated runtime.\n * Sandboxed backends run in isolated environments (e.g., containers)\n * and communicate via defined interfaces.\n */\nexport interface SandboxBackendProtocol extends BackendProtocol {\n /**\n * Execute a command in the sandbox.\n *\n * @param command - Full shell command string to execute\n * @returns ExecuteResponse with combined output, exit code, and truncation flag\n */\n execute(command: string): MaybePromise<ExecuteResponse>;\n\n /** Unique identifier for the sandbox backend instance */\n readonly id: string;\n}\n\n/**\n * Type guard to check if a backend supports execution.\n *\n * @param backend - Backend instance to check\n * @returns True if the backend implements SandboxBackendProtocol\n */\nexport function isSandboxBackend(\n backend: BackendProtocol,\n): backend is SandboxBackendProtocol {\n return (\n typeof (backend as SandboxBackendProtocol).execute === \"function\" &&\n typeof (backend as SandboxBackendProtocol).id === \"string\"\n );\n}\n\n/**\n * State and store container for backend initialization.\n *\n * This provides a clean interface for what backends need to access:\n * - state: Current agent state (with files, messages, etc.)\n * - store: Optional persistent store for cross-conversation data\n *\n * Different contexts build this differently:\n * - Tools: Extract state via getCurrentTaskInput(config)\n * - Middleware: Use request.state directly\n */\nexport interface StateAndStore {\n /** Current agent state with files, messages, etc. */\n state: unknown;\n /** Optional BaseStore for persistent cross-conversation storage */\n store?: BaseStore;\n /** Optional assistant ID for per-assistant isolation in store */\n assistantId?: string;\n}\n\n/**\n * Factory function type for creating backend instances.\n *\n * Backends receive StateAndStore which contains the current state\n * and optional store, extracted from the execution context.\n *\n * @example\n * ```typescript\n * // Using in middleware\n * const middleware = createFilesystemMiddleware({\n * backend: (stateAndStore) => new StateBackend(stateAndStore)\n * });\n * ```\n */\nexport type BackendFactory = (stateAndStore: StateAndStore) => BackendProtocol;\n","/**\n * Shared utility functions for memory backend implementations.\n *\n * This module contains both user-facing string formatters and structured\n * helpers used by backends and the composite router. Structured helpers\n * enable composition without fragile string parsing.\n */\n\nimport micromatch from \"micromatch\";\nimport { basename } from \"path\";\nimport type { FileData, GrepMatch } from \"./protocol.js\";\n\n// Constants\nexport const EMPTY_CONTENT_WARNING =\n \"System reminder: File exists but has empty contents\";\nexport const MAX_LINE_LENGTH = 10000;\nexport const LINE_NUMBER_WIDTH = 6;\nexport const TOOL_RESULT_TOKEN_LIMIT = 20000; // Same threshold as eviction\nexport const TRUNCATION_GUIDANCE =\n \"... [results truncated, try being more specific with your parameters]\";\n\n/**\n * Sanitize tool_call_id to prevent path traversal and separator issues.\n *\n * Replaces dangerous characters (., /, \\) with underscores.\n */\nexport function sanitizeToolCallId(toolCallId: string): string {\n return toolCallId.replace(/\\./g, \"_\").replace(/\\//g, \"_\").replace(/\\\\/g, \"_\");\n}\n\n/**\n * Format file content with line numbers (cat -n style).\n *\n * Chunks lines longer than MAX_LINE_LENGTH with continuation markers (e.g., 5.1, 5.2).\n *\n * @param content - File content as string or list of lines\n * @param startLine - Starting line number (default: 1)\n * @returns Formatted content with line numbers and continuation markers\n */\nexport function formatContentWithLineNumbers(\n content: string | string[],\n startLine: number = 1,\n): string {\n let lines: string[];\n if (typeof content === \"string\") {\n lines = content.split(\"\\n\");\n if (lines.length > 0 && lines[lines.length - 1] === \"\") {\n lines = lines.slice(0, -1);\n }\n } else {\n lines = content;\n }\n\n const resultLines: string[] = [];\n for (let i = 0; i < lines.length; i++) {\n const line = lines[i];\n const lineNum = i + startLine;\n\n if (line.length <= MAX_LINE_LENGTH) {\n resultLines.push(\n `${lineNum.toString().padStart(LINE_NUMBER_WIDTH)}\\t${line}`,\n );\n } else {\n // Split long line into chunks with continuation markers\n const numChunks = Math.ceil(line.length / MAX_LINE_LENGTH);\n for (let chunkIdx = 0; chunkIdx < numChunks; chunkIdx++) {\n const start = chunkIdx * MAX_LINE_LENGTH;\n const end = Math.min(start + MAX_LINE_LENGTH, line.length);\n const chunk = line.substring(start, end);\n if (chunkIdx === 0) {\n // First chunk: use normal line number\n resultLines.push(\n `${lineNum.toString().padStart(LINE_NUMBER_WIDTH)}\\t${chunk}`,\n );\n } else {\n // Continuation chunks: use decimal notation (e.g., 5.1, 5.2)\n const continuationMarker = `${lineNum}.${chunkIdx}`;\n resultLines.push(\n `${continuationMarker.padStart(LINE_NUMBER_WIDTH)}\\t${chunk}`,\n );\n }\n }\n }\n }\n\n return resultLines.join(\"\\n\");\n}\n\n/**\n * Check if content is empty and return warning message.\n *\n * @param content - Content to check\n * @returns Warning message if empty, null otherwise\n */\nexport function checkEmptyContent(content: string): string | null {\n if (!content || content.trim() === \"\") {\n return EMPTY_CONTENT_WARNING;\n }\n return null;\n}\n\n/**\n * Convert FileData to plain string content.\n *\n * @param fileData - FileData object with 'content' key\n * @returns Content as string with lines joined by newlines\n */\nexport function fileDataToString(fileData: FileData): string {\n return fileData.content.join(\"\\n\");\n}\n\n/**\n * Create a FileData object with timestamps.\n *\n * @param content - File content as string\n * @param createdAt - Optional creation timestamp (ISO format)\n * @returns FileData object with content and timestamps\n */\nexport function createFileData(content: string, createdAt?: string): FileData {\n const lines = typeof content === \"string\" ? content.split(\"\\n\") : content;\n const now = new Date().toISOString();\n\n return {\n content: lines,\n created_at: createdAt || now,\n modified_at: now,\n };\n}\n\n/**\n * Update FileData with new content, preserving creation timestamp.\n *\n * @param fileData - Existing FileData object\n * @param content - New content as string\n * @returns Updated FileData object\n */\nexport function updateFileData(fileData: FileData, content: string): FileData {\n const lines = typeof content === \"string\" ? content.split(\"\\n\") : content;\n const now = new Date().toISOString();\n\n return {\n content: lines,\n created_at: fileData.created_at,\n modified_at: now,\n };\n}\n\n/**\n * Format file data for read response with line numbers.\n *\n * @param fileData - FileData object\n * @param offset - Line offset (0-indexed)\n * @param limit - Maximum number of lines\n * @returns Formatted content or error message\n */\nexport function formatReadResponse(\n fileData: FileData,\n offset: number,\n limit: number,\n): string {\n const content = fileDataToString(fileData);\n const emptyMsg = checkEmptyContent(content);\n if (emptyMsg) {\n return emptyMsg;\n }\n\n const lines = content.split(\"\\n\");\n const startIdx = offset;\n const endIdx = Math.min(startIdx + limit, lines.length);\n\n if (startIdx >= lines.length) {\n return `Error: Line offset ${offset} exceeds file length (${lines.length} lines)`;\n }\n\n const selectedLines = lines.slice(startIdx, endIdx);\n return formatContentWithLineNumbers(selectedLines, startIdx + 1);\n}\n\n/**\n * Perform string replacement with occurrence validation.\n *\n * @param content - Original content\n * @param oldString - String to replace\n * @param newString - Replacement string\n * @param replaceAll - Whether to replace all occurrences\n * @returns Tuple of [new_content, occurrences] on success, or error message string\n */\nexport function performStringReplacement(\n content: string,\n oldString: string,\n newString: string,\n replaceAll: boolean,\n): [string, number] | string {\n // Use split to count occurrences (simpler than regex)\n const occurrences = content.split(oldString).length - 1;\n\n if (occurrences === 0) {\n return `Error: String not found in file: '${oldString}'`;\n }\n\n if (occurrences > 1 && !replaceAll) {\n return `Error: String '${oldString}' appears ${occurrences} times in file. Use replace_all=True to replace all instances, or provide a more specific string with surrounding context.`;\n }\n\n // Python's str.replace() replaces ALL occurrences\n // Use split/join for consistent behavior\n const newContent = content.split(oldString).join(newString);\n\n return [newContent, occurrences];\n}\n\n/**\n * Truncate list or string result if it exceeds token limit (rough estimate: 4 chars/token).\n */\nexport function truncateIfTooLong(\n result: string[] | string,\n): string[] | string {\n if (Array.isArray(result)) {\n const totalChars = result.reduce((sum, item) => sum + item.length, 0);\n if (totalChars > TOOL_RESULT_TOKEN_LIMIT * 4) {\n const truncateAt = Math.floor(\n (result.length * TOOL_RESULT_TOKEN_LIMIT * 4) / totalChars,\n );\n return [...result.slice(0, truncateAt), TRUNCATION_GUIDANCE];\n }\n return result;\n }\n // string\n if (result.length > TOOL_RESULT_TOKEN_LIMIT * 4) {\n return (\n result.substring(0, TOOL_RESULT_TOKEN_LIMIT * 4) +\n \"\\n\" +\n TRUNCATION_GUIDANCE\n );\n }\n return result;\n}\n\n/**\n * Validate and normalize a directory path.\n *\n * Ensures paths are safe to use by preventing directory traversal attacks\n * and enforcing consistent formatting. All paths are normalized to use\n * forward slashes and start with a leading slash.\n *\n * This function is designed for virtual filesystem paths and rejects\n * Windows absolute paths (e.g., C:/..., F:/...) to maintain consistency\n * and prevent path format ambiguity.\n *\n * @param path - Path to validate\n * @returns Normalized path starting with / and ending with /\n * @throws Error if path is invalid\n *\n * @example\n * ```typescript\n * validatePath(\"foo/bar\") // Returns: \"/foo/bar/\"\n * validatePath(\"/./foo//bar\") // Returns: \"/foo/bar/\"\n * validatePath(\"../etc/passwd\") // Throws: Path traversal not allowed\n * validatePath(\"C:\\\\Users\\\\file\") // Throws: Windows absolute paths not supported\n * ```\n */\nexport function validatePath(path: string | null | undefined): string {\n const pathStr = path || \"/\";\n if (!pathStr || pathStr.trim() === \"\") {\n throw new Error(\"Path cannot be empty\");\n }\n\n let normalized = pathStr.startsWith(\"/\") ? pathStr : \"/\" + pathStr;\n\n if (!normalized.endsWith(\"/\")) {\n normalized += \"/\";\n }\n\n return normalized;\n}\n\n/**\n * Validate and normalize a file path for security.\n *\n * Ensures paths are safe to use by preventing directory traversal attacks\n * and enforcing consistent formatting. All paths are normalized to use\n * forward slashes and start with a leading slash.\n *\n * This function is designed for virtual filesystem paths and rejects\n * Windows absolute paths (e.g., C:/..., F:/...) to maintain consistency\n * and prevent path format ambiguity.\n *\n * @param path - The path to validate and normalize.\n * @param allowedPrefixes - Optional list of allowed path prefixes. If provided,\n * the normalized path must start with one of these prefixes.\n * @returns Normalized canonical path starting with `/` and using forward slashes.\n * @throws Error if path contains traversal sequences (`..` or `~`), is a Windows\n * absolute path (e.g., C:/...), or does not start with an allowed prefix\n * when `allowedPrefixes` is specified.\n *\n * @example\n * ```typescript\n * validateFilePath(\"foo/bar\") // Returns: \"/foo/bar\"\n * validateFilePath(\"/./foo//bar\") // Returns: \"/foo/bar\"\n * validateFilePath(\"../etc/passwd\") // Throws: Path traversal not allowed\n * validateFilePath(\"C:\\\\Users\\\\file.txt\") // Throws: Windows absolute paths not supported\n * validateFilePath(\"/data/file.txt\", [\"/data/\"]) // Returns: \"/data/file.txt\"\n * validateFilePath(\"/etc/file.txt\", [\"/data/\"]) // Throws: Path must start with...\n * ```\n */\nexport function validateFilePath(\n path: string,\n allowedPrefixes?: string[],\n): string {\n // Check for path traversal\n if (path.includes(\"..\") || path.startsWith(\"~\")) {\n throw new Error(`Path traversal not allowed: ${path}`);\n }\n\n // Reject Windows absolute paths (e.g., C:\\..., D:/...)\n // This maintains consistency in virtual filesystem paths\n if (/^[a-zA-Z]:/.test(path)) {\n throw new Error(\n `Windows absolute paths are not supported: ${path}. Please use virtual paths starting with / (e.g., /workspace/file.txt)`,\n );\n }\n\n // Normalize path separators and remove redundant slashes\n let normalized = path.replace(/\\\\/g, \"/\");\n\n // Remove redundant path components (./foo becomes foo, foo//bar becomes foo/bar)\n const parts: string[] = [];\n for (const part of normalized.split(\"/\")) {\n if (part === \".\" || part === \"\") {\n continue;\n }\n parts.push(part);\n }\n normalized = \"/\" + parts.join(\"/\");\n\n // Check allowed prefixes if provided\n if (\n allowedPrefixes &&\n !allowedPrefixes.some((prefix) => normalized.startsWith(prefix))\n ) {\n throw new Error(\n `Path must start with one of ${JSON.stringify(allowedPrefixes)}: ${path}`,\n );\n }\n\n return normalized;\n}\n\n/**\n * Search files dict for paths matching glob pattern.\n *\n * @param files - Dictionary of file paths to FileData\n * @param pattern - Glob pattern (e.g., `*.py`, `**\\/*.ts`)\n * @param path - Base path to search from\n * @returns Newline-separated file paths, sorted by modification time (most recent first).\n * Returns \"No files found\" if no matches.\n *\n * @example\n * ```typescript\n * const files = {\"/src/main.py\": FileData(...), \"/test.py\": FileData(...)};\n * globSearchFiles(files, \"*.py\", \"/\");\n * // Returns: \"/test.py\\n/src/main.py\" (sorted by modified_at)\n * ```\n */\nexport function globSearchFiles(\n files: Record<string, FileData>,\n pattern: string,\n path: string = \"/\",\n): string {\n let normalizedPath: string;\n try {\n normalizedPath = validatePath(path);\n } catch {\n return \"No files found\";\n }\n\n const filtered = Object.fromEntries(\n Object.entries(files).filter(([fp]) => fp.startsWith(normalizedPath)),\n );\n\n // Respect standard glob semantics:\n // - Patterns without path separators (e.g., \"*.py\") match only in the current\n // directory (non-recursive) relative to `path`.\n // - Use \"**\" explicitly for recursive matching.\n const effectivePattern = pattern;\n\n const matches: Array<[string, string]> = [];\n for (const [filePath, fileData] of Object.entries(filtered)) {\n let relative = filePath.substring(normalizedPath.length);\n if (relative.startsWith(\"/\")) {\n relative = relative.substring(1);\n }\n if (!relative) {\n const parts = filePath.split(\"/\");\n relative = parts[parts.length - 1] || \"\";\n }\n\n if (\n micromatch.isMatch(relative, effectivePattern, {\n dot: true,\n nobrace: false,\n })\n ) {\n matches.push([filePath, fileData.modified_at]);\n }\n }\n\n matches.sort((a, b) => b[1].localeCompare(a[1])); // Sort by modified_at descending\n\n if (matches.length === 0) {\n return \"No files found\";\n }\n\n return matches.map(([fp]) => fp).join(\"\\n\");\n}\n\n/**\n * Format grep search results based on output mode.\n *\n * @param results - Dictionary mapping file paths to list of [line_num, line_content] tuples\n * @param outputMode - Output format - \"files_with_matches\", \"content\", or \"count\"\n * @returns Formatted string output\n */\nexport function formatGrepResults(\n results: Record<string, Array<[number, string]>>,\n outputMode: \"files_with_matches\" | \"content\" | \"count\",\n): string {\n if (outputMode === \"files_with_matches\") {\n return Object.keys(results).sort().join(\"\\n\");\n }\n if (outputMode === \"count\") {\n const lines: string[] = [];\n for (const filePath of Object.keys(results).sort()) {\n const count = results[filePath].length;\n lines.push(`${filePath}: ${count}`);\n }\n return lines.join(\"\\n\");\n }\n // content mode\n const lines: string[] = [];\n for (const filePath of Object.keys(results).sort()) {\n lines.push(`${filePath}:`);\n for (const [lineNum, line] of results[filePath]) {\n lines.push(` ${lineNum}: ${line}`);\n }\n }\n return lines.join(\"\\n\");\n}\n\n/**\n * Search file contents for regex pattern.\n *\n * @param files - Dictionary of file paths to FileData\n * @param pattern - Regex pattern to search for\n * @param path - Base path to search from\n * @param glob - Optional glob pattern to filter files (e.g., \"*.py\")\n * @param outputMode - Output format - \"files_with_matches\", \"content\", or \"count\"\n * @returns Formatted search results. Returns \"No matches found\" if no results.\n *\n * @example\n * ```typescript\n * const files = {\"/file.py\": FileData({content: [\"import os\", \"print('hi')\"], ...})};\n * grepSearchFiles(files, \"import\", \"/\");\n * // Returns: \"/file.py\" (with output_mode=\"files_with_matches\")\n * ```\n */\nexport function grepSearchFiles(\n files: Record<string, FileData>,\n pattern: string,\n path: string | null = null,\n glob: string | null = null,\n outputMode: \"files_with_matches\" | \"content\" | \"count\" = \"files_with_matches\",\n): string {\n let regex: RegExp;\n try {\n regex = new RegExp(pattern);\n } catch (e: any) {\n return `Invalid regex pattern: ${e.message}`;\n }\n\n let normalizedPath: string;\n try {\n normalizedPath = validatePath(path);\n } catch {\n return \"No matches found\";\n }\n\n let filtered = Object.fromEntries(\n Object.entries(files).filter(([fp]) => fp.startsWith(normalizedPath)),\n );\n\n if (glob) {\n filtered = Object.fromEntries(\n Object.entries(filtered).filter(([fp]) =>\n micromatch.isMatch(basename(fp), glob, { dot: true, nobrace: false }),\n ),\n );\n }\n\n const results: Record<string, Array<[number, string]>> = {};\n for (const [filePath, fileData] of Object.entries(filtered)) {\n for (let i = 0; i < fileData.content.length; i++) {\n const line = fileData.content[i];\n const lineNum = i + 1;\n if (regex.test(line)) {\n if (!results[filePath]) {\n results[filePath] = [];\n }\n results[filePath].push([lineNum, line]);\n }\n }\n }\n\n if (Object.keys(results).length === 0) {\n return \"No matches found\";\n }\n return formatGrepResults(results, outputMode);\n}\n\n// -------- Structured helpers for composition --------\n\n/**\n * Return structured grep matches from an in-memory files mapping.\n *\n * Returns a list of GrepMatch on success, or a string for invalid inputs\n * (e.g., invalid regex). We deliberately do not raise here to keep backends\n * non-throwing in tool contexts and preserve user-facing error messages.\n */\nexport function grepMatchesFromFiles(\n files: Record<string, FileData>,\n pattern: string,\n path: string | null = null,\n glob: string | null = null,\n): GrepMatch[] | string {\n let regex: RegExp;\n try {\n regex = new RegExp(pattern);\n } catch (e: any) {\n return `Invalid regex pattern: ${e.message}`;\n }\n\n let normalizedPath: string;\n try {\n normalizedPath = validatePath(path);\n } catch {\n return [];\n }\n\n let filtered = Object.fromEntries(\n Object.entries(files).filter(([fp]) => fp.startsWith(normalizedPath)),\n );\n\n if (glob) {\n filtered = Object.fromEntries(\n Object.entries(filtered).filter(([fp]) =>\n micromatch.isMatch(basename(fp), glob, { dot: true, nobrace: false }),\n ),\n );\n }\n\n const matches: GrepMatch[] = [];\n for (const [filePath, fileData] of Object.entries(filtered)) {\n for (let i = 0; i < fileData.content.length; i++) {\n const line = fileData.content[i];\n const lineNum = i + 1;\n if (regex.test(line)) {\n matches.push({ path: filePath, line: lineNum, text: line });\n }\n }\n }\n\n return matches;\n}\n\n/**\n * Group structured matches into the legacy dict form used by formatters.\n */\nexport function buildGrepResultsDict(\n matches: GrepMatch[],\n): Record<string, Array<[number, string]>> {\n const grouped: Record<string, Array<[number, string]>> = {};\n for (const m of matches) {\n if (!grouped[m.path]) {\n grouped[m.path] = [];\n }\n grouped[m.path].push([m.line, m.text]);\n }\n return grouped;\n}\n\n/**\n * Format structured grep matches using existing formatting logic.\n */\nexport function formatGrepMatches(\n matches: GrepMatch[],\n outputMode: \"files_with_matches\" | \"content\" | \"count\",\n): string {\n if (matches.length === 0) {\n return \"No matches found\";\n }\n return formatGrepResults(buildGrepResultsDict(matches), outputMode);\n}\n","/**\n * StateBackend: Store files in LangGraph agent state (ephemeral).\n */\n\nimport type {\n BackendProtocol,\n EditResult,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n StateAndStore,\n WriteResult,\n} from \"./protocol.js\";\nimport {\n createFileData,\n fileDataToString,\n formatReadResponse,\n globSearchFiles,\n grepMatchesFromFiles,\n performStringReplacement,\n updateFileData,\n} from \"./utils.js\";\n\n/**\n * Backend that stores files in agent state (ephemeral).\n *\n * Uses LangGraph's state management and checkpointing. Files persist within\n * a conversation thread but not across threads. State is automatically\n * checkpointed after each agent step.\n *\n * Special handling: Since LangGraph state must be updated via Command objects\n * (not direct mutation), operations return filesUpdate in WriteResult/EditResult\n * for the middleware to apply via Command.\n */\nexport class StateBackend implements BackendProtocol {\n private stateAndStore: StateAndStore;\n\n constructor(stateAndStore: StateAndStore) {\n this.stateAndStore = stateAndStore;\n }\n\n /**\n * Get files from current state.\n */\n private getFiles(): Record<string, FileData> {\n return (\n ((this.stateAndStore.state as any).files as Record<string, FileData>) ||\n {}\n );\n }\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects for files and directories directly in the directory.\n * Directories have a trailing / in their path and is_dir=true.\n */\n lsInfo(path: string): FileInfo[] {\n const files = this.getFiles();\n const infos: FileInfo[] = [];\n const subdirs = new Set<string>();\n\n // Normalize path to have trailing slash for proper prefix matching\n const normalizedPath = path.endsWith(\"/\") ? path : path + \"/\";\n\n for (const [k, fd] of Object.entries(files)) {\n // Check if file is in the specified directory or a subdirectory\n if (!k.startsWith(normalizedPath)) {\n continue;\n }\n\n // Get the relative path after the directory\n const relative = k.substring(normalizedPath.length);\n\n // If relative path contains '/', it's in a subdirectory\n if (relative.includes(\"/\")) {\n // Extract the immediate subdirectory name\n const subdirName = relative.split(\"/\")[0];\n subdirs.add(normalizedPath + subdirName + \"/\");\n continue;\n }\n\n // This is a file directly in the current directory\n const size = fd.content.join(\"\\n\").length;\n infos.push({\n path: k,\n is_dir: false,\n size: size,\n modified_at: fd.modified_at,\n });\n }\n\n // Add directories to the results\n for (const subdir of Array.from(subdirs).sort()) {\n infos.push({\n path: subdir,\n is_dir: true,\n size: 0,\n modified_at: \"\",\n });\n }\n\n infos.sort((a, b) => a.path.localeCompare(b.path));\n return infos;\n }\n\n /**\n * Read file content with line numbers.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n read(filePath: string, offset: number = 0, limit: number = 500): string {\n const files = this.getFiles();\n const fileData = files[filePath];\n\n if (!fileData) {\n return `Error: File '${filePath}' not found`;\n }\n\n return formatReadResponse(fileData, offset, limit);\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n readRaw(filePath: string): FileData {\n const files = this.getFiles();\n const fileData = files[filePath];\n\n if (!fileData) throw new Error(`File '${filePath}' not found`);\n return fileData;\n }\n\n /**\n * Create a new file with content.\n * Returns WriteResult with filesUpdate to update LangGraph state.\n */\n write(filePath: string, content: string): WriteResult {\n const files = this.getFiles();\n\n if (filePath in files) {\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n }\n\n const newFileData = createFileData(content);\n return {\n path: filePath,\n filesUpdate: { [filePath]: newFileData },\n };\n }\n\n /**\n * Edit a file by replacing string occurrences.\n * Returns EditResult with filesUpdate and occurrences.\n */\n edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): EditResult {\n const files = this.getFiles();\n const fileData = files[filePath];\n\n if (!fileData) {\n return { error: `Error: File '${filePath}' not found` };\n }\n\n const content = fileDataToString(fileData);\n const result = performStringReplacement(\n content,\n oldString,\n newString,\n replaceAll,\n );\n\n if (typeof result === \"string\") {\n return { error: result };\n }\n\n const [newContent, occurrences] = result;\n const newFileData = updateFileData(fileData, newContent);\n return {\n path: filePath,\n filesUpdate: { [filePath]: newFileData },\n occurrences: occurrences,\n };\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n grepRaw(\n pattern: string,\n path: string = \"/\",\n glob: string | null = null,\n ): GrepMatch[] | string {\n const files = this.getFiles();\n return grepMatchesFromFiles(files, pattern, path, glob);\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n globInfo(pattern: string, path: string = \"/\"): FileInfo[] {\n const files = this.getFiles();\n const result = globSearchFiles(files, pattern, path);\n\n if (result === \"No files found\") {\n return [];\n }\n\n const paths = result.split(\"\\n\");\n const infos: FileInfo[] = [];\n for (const p of paths) {\n const fd = files[p];\n const size = fd ? fd.content.join(\"\\n\").length : 0;\n infos.push({\n path: p,\n is_dir: false,\n size: size,\n modified_at: fd?.modified_at || \"\",\n });\n }\n return infos;\n }\n\n /**\n * Upload multiple files.\n *\n * Note: Since LangGraph state must be updated via Command objects,\n * the caller must apply filesUpdate via Command after calling this method.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): FileUploadResponse[] & { filesUpdate?: Record<string, FileData> } {\n const responses: FileUploadResponse[] = [];\n const updates: Record<string, FileData> = {};\n\n for (const [path, content] of files) {\n try {\n const contentStr = new TextDecoder().decode(content);\n const fileData = createFileData(contentStr);\n updates[path] = fileData;\n responses.push({ path, error: null });\n } catch {\n responses.push({ path, error: \"invalid_path\" });\n }\n }\n\n // Attach filesUpdate for the caller to apply via Command\n const result = responses as FileUploadResponse[] & {\n filesUpdate?: Record<string, FileData>;\n };\n result.filesUpdate = updates;\n return result;\n }\n\n /**\n * Download multiple files.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n downloadFiles(paths: string[]): FileDownloadResponse[] {\n const files = this.getFiles();\n const responses: FileDownloadResponse[] = [];\n\n for (const path of paths) {\n const fileData = files[path];\n if (!fileData) {\n responses.push({ path, content: null, error: \"file_not_found\" });\n continue;\n }\n\n const contentStr = fileDataToString(fileData);\n const content = new TextEncoder().encode(contentStr);\n responses.push({ path, content, error: null });\n }\n\n return responses;\n }\n}\n","/**\n * Middleware for providing filesystem tools to an agent.\n *\n * Provides ls, read_file, write_file, edit_file, glob, and grep tools with support for:\n * - Pluggable backends (StateBackend, StoreBackend, FilesystemBackend, CompositeBackend)\n * - Tool result eviction for large outputs\n */\n\nimport {\n createMiddleware,\n tool,\n ToolMessage,\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { Command, isCommand, getCurrentTaskInput } from \"@langchain/langgraph\";\nimport { z } from \"zod/v4\";\nimport type {\n BackendProtocol,\n BackendFactory,\n FileData,\n StateAndStore,\n} from \"../backends/protocol.js\";\nimport { isSandboxBackend } from \"../backends/protocol.js\";\nimport { StateBackend } from \"../backends/state.js\";\nimport {\n sanitizeToolCallId,\n formatContentWithLineNumbers,\n} from \"../backends/utils.js\";\n\n/**\n * Tools that should be excluded from the large result eviction logic.\n *\n * This array contains tools that should NOT have their results evicted to the filesystem\n * when they exceed token limits. Tools are excluded for different reasons:\n *\n * 1. Tools with built-in truncation (ls, glob, grep):\n * These tools truncate their own output when it becomes too large. When these tools\n * produce truncated output due to many matches, it typically indicates the query\n * needs refinement rather than full result preservation. In such cases, the truncated\n * matches are potentially more like noise and the LLM should be prompted to narrow\n * its search criteria instead.\n *\n * 2. Tools with problematic truncation behavior (read_file):\n * read_file is tricky to handle as the failure mode here is single long lines\n * (e.g., imagine a jsonl file with very long payloads on each line). If we try to\n * truncate the result of read_file, the agent may then attempt to re-read the\n * truncated file using read_file again, which won't help.\n *\n * 3. Tools that never exceed limits (edit_file, write_file):\n * These tools return minimal confirmation messages and are never expected to produce\n * output large enough to exceed token limits, so checking them would be unnecessary.\n */\nexport const TOOLS_EXCLUDED_FROM_EVICTION = [\n \"ls\",\n \"glob\",\n \"grep\",\n \"read_file\",\n \"edit_file\",\n \"write_file\",\n] as const;\n\n/**\n * Approximate number of characters per token for truncation calculations.\n * Using 4 chars per token as a conservative approximation (actual ratio varies by content)\n * This errs on the high side to avoid premature eviction of content that might fit.\n */\nexport const NUM_CHARS_PER_TOKEN = 4;\n\n/**\n * Message template for evicted tool results.\n */\nconst TOO_LARGE_TOOL_MSG = `Tool result too large, the result of this tool call {tool_call_id} was saved in the filesystem at this path: {file_path}\nYou can read the result from the filesystem by using the read_file tool, but make sure to only read part of the result at a time.\nYou can do this by specifying an offset and limit in the read_file tool call.\nFor example, to read the first 100 lines, you can use the read_file tool with offset=0 and limit=100.\n\nHere is a preview showing the head and tail of the result (lines of the form\n... [N lines truncated] ...\nindicate omitted lines in the middle of the content):\n\n{content_sample}`;\n\n/**\n * Create a preview of content showing head and tail with truncation marker.\n *\n * @param contentStr - The full content string to preview.\n * @param headLines - Number of lines to show from the start (default: 5).\n * @param tailLines - Number of lines to show from the end (default: 5).\n * @returns Formatted preview string with line numbers.\n */\nexport function createContentPreview(\n contentStr: string,\n headLines: number = 5,\n tailLines: number = 5,\n): string {\n const lines = contentStr.split(\"\\n\");\n\n if (lines.length <= headLines + tailLines) {\n // If file is small enough, show all lines\n const previewLines = lines.map((line) => line.substring(0, 1000));\n return formatContentWithLineNumbers(previewLines, 1);\n }\n\n // Show head and tail with truncation marker\n const head = lines.slice(0, headLines).map((line) => line.substring(0, 1000));\n const tail = lines.slice(-tailLines).map((line) => line.substring(0, 1000));\n\n const headSample = formatContentWithLineNumbers(head, 1);\n const truncationNotice = `\\n... [${lines.length - headLines - tailLines} lines truncated] ...\\n`;\n const tailSample = formatContentWithLineNumbers(\n tail,\n lines.length - tailLines + 1,\n );\n\n return headSample + truncationNotice + tailSample;\n}\n\n/**\n * required for type inference\n */\nimport type * as _zodTypes from \"@langchain/core/utils/types\";\nimport type * as _zodMeta from \"@langchain/langgraph/zod\";\nimport type * as _messages from \"@langchain/core/messages\";\n\n/**\n * Zod v3 schema for FileData (re-export from backends)\n */\nconst FileDataSchema = z.object({\n content: z.array(z.string()),\n created_at: z.string(),\n modified_at: z.string(),\n});\n\nexport type { FileData };\n\n/**\n * Merge file updates with support for deletions.\n */\nfunction fileDataReducer(\n left: Record<string, FileData> | undefined,\n right: Record<string, FileData | null>,\n): Record<string, FileData> {\n if (left === undefined) {\n const result: Record<string, FileData> = {};\n for (const [key, value] of Object.entries(right)) {\n if (value !== null) {\n result[key] = value;\n }\n }\n return result;\n }\n\n const result = { ...left };\n for (const [key, value] of Object.entries(right)) {\n if (value === null) {\n delete result[key];\n } else {\n result[key] = value;\n }\n }\n return result;\n}\n\n/**\n * Shared filesystem state schema.\n * Defined at module level to ensure the same object identity is used across all agents,\n * preventing \"Channel already exists with different type\" errors when multiple agents\n * use createFilesystemMiddleware.\n */\nconst FilesystemStateSchema = z.object({\n files: z\n .record(z.string(), FileDataSchema)\n .default({})\n .meta({\n reducer: {\n fn: fileDataReducer,\n schema: z.record(z.string(), FileDataSchema.nullable()),\n },\n }),\n});\n\n/**\n * Resolve backend from factory or instance.\n *\n * @param backend - Backend instance or factory function\n * @param stateAndStore - State and store container for backend initialization\n */\nfunction getBackend(\n backend: BackendProtocol | BackendFactory,\n stateAndStore: StateAndStore,\n): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend(stateAndStore);\n }\n return backend;\n}\n\n// System prompts\nconst FILESYSTEM_SYSTEM_PROMPT = `## Filesystem Tools \\`ls\\`, \\`read_file\\`, \\`write_file\\`, \\`edit_file\\`, \\`glob\\`, \\`grep\\`\n\nYou have access to a filesystem which you can interact with using these tools.\nAll file paths must start with a /.\n\n- ls: list files in a directory (requires absolute path)\n- read_file: read a file from the filesystem\n- write_file: write to a file in the filesystem\n- edit_file: edit a file in the filesystem\n- glob: find files matching a pattern (e.g., \"**/*.py\")\n- grep: search for text within files`;\n\n// Tool descriptions - ported from Python for comprehensive LLM guidance\nexport const LS_TOOL_DESCRIPTION = `Lists all files in a directory.\n\nThis is useful for exploring the filesystem and finding the right file to read or edit.\nYou should almost ALWAYS use this tool before using the read_file or edit_file tools.`;\n\nexport const READ_FILE_TOOL_DESCRIPTION = `Reads a file from the filesystem.\n\nAssume this tool is able to read all files. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.\n\nUsage:\n- By default, it reads up to 500 lines starting from the beginning of the file\n- **IMPORTANT for large files and codebase exploration**: Use pagination with offset and limit parameters to avoid context overflow\n - First scan: read_file(path, limit=100) to see file structure\n - Read more sections: read_file(path, offset=100, limit=200) for next 200 lines\n - Only omit limit (read full file) when necessary for editing\n- Specify offset and limit: read_file(path, offset=0, limit=100) reads first 100 lines\n- Results are returned using cat -n format, with line numbers starting at 1\n- Lines longer than 10,000 characters will be split into multiple lines with continuation markers (e.g., 5.1, 5.2, etc.). When you specify a limit, these continuation lines count towards the limit.\n- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.\n- If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents.\n- You should ALWAYS make sure a file has been read before editing it.`;\n\nexport const WRITE_FILE_TOOL_DESCRIPTION = `Writes to a new file in the filesystem.\n\nUsage:\n- The write_file tool will create a new file.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.`;\n\nexport const EDIT_FILE_TOOL_DESCRIPTION = `Performs exact string replacements in files.\n\nUsage:\n- You must read the file before editing. This tool will error if you attempt an edit without reading the file first.\n- When editing, preserve the exact indentation (tabs/spaces) from the read output. Never include line number prefixes in old_string or new_string.\n- ALWAYS prefer editing existing files over creating new ones.\n- Only use emojis if the user explicitly requests it.`;\n\nexport const GLOB_TOOL_DESCRIPTION = `Find files matching a glob pattern.\n\nSupports standard glob patterns: \\`*\\` (any characters), \\`**\\` (any directories), \\`?\\` (single character).\nReturns a list of absolute file paths that match the pattern.\n\nExamples:\n- \\`**/*.py\\` - Find all Python files\n- \\`*.txt\\` - Find all text files in root\n- \\`/subdir/**/*.md\\` - Find all markdown files under /subdir`;\n\nexport const GREP_TOOL_DESCRIPTION = `Search for a text pattern across files.\n\nSearches for literal text (not regex) and returns matching files or content based on output_mode.\n\nExamples:\n- Search all files: \\`grep(pattern=\"TODO\")\\`\n- Search Python files only: \\`grep(pattern=\"import\", glob=\"*.py\")\\`\n- Show matching lines: \\`grep(pattern=\"error\", output_mode=\"content\")\\``;\nexport const EXECUTE_TOOL_DESCRIPTION = `Executes a shell command in an isolated sandbox environment.\n\nUsage:\nExecutes a given command in the sandbox environment with proper handling and security measures.\nBefore executing the command, please follow these steps:\n\n1. Directory Verification:\n - If the command will create new directories or files, first use the ls tool to verify the parent directory exists and is the correct location\n - For example, before running \"mkdir foo/bar\", first use ls to check that \"foo\" exists and is the intended parent directory\n\n2. Command Execution:\n - Always quote file paths that contain spaces with double quotes (e.g., cd \"path with spaces/file.txt\")\n - Examples of proper quoting:\n - cd \"/Users/name/My Documents\" (correct)\n - cd /Users/name/My Documents (incorrect - will fail)\n - python \"/path/with spaces/script.py\" (correct)\n - python /path/with spaces/script.py (incorrect - will fail)\n - After ensuring proper quoting, execute the command\n - Capture the output of the command\n\nUsage notes:\n - Commands run in an isolated sandbox environment\n - Returns combined stdout/stderr output with exit code\n - If the output is very large, it may be truncated\n - VERY IMPORTANT: You MUST avoid using search commands like find and grep. Instead use the grep, glob tools to search. You MUST avoid read tools like cat, head, tail, and use read_file to read files.\n - When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings)\n - Use '&&' when commands depend on each other (e.g., \"mkdir dir && cd dir\")\n - Use ';' only when you need to run commands sequentially but don't care if earlier commands fail\n - Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of cd\n\nExamples:\n Good examples:\n - execute(command=\"pytest /foo/bar/tests\")\n - execute(command=\"python /path/to/script.py\")\n - execute(command=\"npm install && npm test\")\n\n Bad examples (avoid these):\n - execute(command=\"cd /foo/bar && pytest tests\") # Use absolute path instead\n - execute(command=\"cat file.txt\") # Use read_file tool instead\n - execute(command=\"find . -name '*.py'\") # Use glob tool instead\n - execute(command=\"grep -r 'pattern' .\") # Use grep tool instead\n\nNote: This tool is only available if the backend supports execution (SandboxBackendProtocol).\nIf execution is not supported, the tool will return an error message.`;\n\n// System prompt for execution capability\nexport const EXECUTION_SYSTEM_PROMPT = `## Execute Tool \\`execute\\`\n\nYou have access to an \\`execute\\` tool for running shell commands in a sandboxed environment.\nUse this tool to run commands, scripts, tests, builds, and other shell operations.\n\n- execute: run a shell command in the sandbox (returns output and exit code)`;\n\n/**\n * Create ls tool using backend.\n */\nfunction createLsTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const path = input.path || \"/\";\n const infos = await resolvedBackend.lsInfo(path);\n\n if (infos.length === 0) {\n return `No files found in ${path}`;\n }\n\n // Format output\n const lines: string[] = [];\n for (const info of infos) {\n if (info.is_dir) {\n lines.push(`${info.path} (directory)`);\n } else {\n const size = info.size ? ` (${info.size} bytes)` : \"\";\n lines.push(`${info.path}${size}`);\n }\n }\n return lines.join(\"\\n\");\n },\n {\n name: \"ls\",\n description: customDescription || LS_TOOL_DESCRIPTION,\n schema: z.object({\n path: z\n .string()\n .optional()\n .default(\"/\")\n .describe(\"Directory path to list (default: /)\"),\n }),\n },\n );\n}\n\n/**\n * Create read_file tool using backend.\n */\nfunction createReadFileTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { file_path, offset = 0, limit = 500 } = input;\n return await resolvedBackend.read(file_path, offset, limit);\n },\n {\n name: \"read_file\",\n description: customDescription || READ_FILE_TOOL_DESCRIPTION,\n schema: z.object({\n file_path: z.string().describe(\"Absolute path to the file to read\"),\n offset: z.coerce\n .number()\n .optional()\n .default(0)\n .describe(\"Line offset to start reading from (0-indexed)\"),\n limit: z.coerce\n .number()\n .optional()\n .default(500)\n .describe(\"Maximum number of lines to read\"),\n }),\n },\n );\n}\n\n/**\n * Create write_file tool using backend.\n */\nfunction createWriteFileTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { file_path, content } = input;\n const result = await resolvedBackend.write(file_path, content);\n\n if (result.error) {\n return result.error;\n }\n\n // If filesUpdate is present, return Command to update state\n const message = new ToolMessage({\n content: `Successfully wrote to '${file_path}'`,\n tool_call_id: config.toolCall?.id as string,\n name: \"write_file\",\n metadata: result.metadata,\n });\n\n if (result.filesUpdate) {\n return new Command({\n update: { files: result.filesUpdate, messages: [message] },\n });\n }\n\n return message;\n },\n {\n name: \"write_file\",\n description: customDescription || WRITE_FILE_TOOL_DESCRIPTION,\n schema: z.object({\n file_path: z.string().describe(\"Absolute path to the file to write\"),\n content: z.string().describe(\"Content to write to the file\"),\n }),\n },\n );\n}\n\n/**\n * Create edit_file tool using backend.\n */\nfunction createEditFileTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { file_path, old_string, new_string, replace_all = false } = input;\n const result = await resolvedBackend.edit(\n file_path,\n old_string,\n new_string,\n replace_all,\n );\n\n if (result.error) {\n return result.error;\n }\n\n const message = new ToolMessage({\n content: `Successfully replaced ${result.occurrences} occurrence(s) in '${file_path}'`,\n tool_call_id: config.toolCall?.id as string,\n name: \"edit_file\",\n metadata: result.metadata,\n });\n\n // If filesUpdate is present, return Command to update state\n if (result.filesUpdate) {\n return new Command({\n update: { files: result.filesUpdate, messages: [message] },\n });\n }\n\n // External storage (filesUpdate is null)\n return message;\n },\n {\n name: \"edit_file\",\n description: customDescription || EDIT_FILE_TOOL_DESCRIPTION,\n schema: z.object({\n file_path: z.string().describe(\"Absolute path to the file to edit\"),\n old_string: z\n .string()\n .describe(\"String to be replaced (must match exactly)\"),\n new_string: z.string().describe(\"String to replace with\"),\n replace_all: z\n .boolean()\n .optional()\n .default(false)\n .describe(\"Whether to replace all occurrences\"),\n }),\n },\n );\n}\n\n/**\n * Create glob tool using backend.\n */\nfunction createGlobTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { pattern, path = \"/\" } = input;\n const infos = await resolvedBackend.globInfo(pattern, path);\n\n if (infos.length === 0) {\n return `No files found matching pattern '${pattern}'`;\n }\n\n return infos.map((info) => info.path).join(\"\\n\");\n },\n {\n name: \"glob\",\n description: customDescription || GLOB_TOOL_DESCRIPTION,\n schema: z.object({\n pattern: z.string().describe(\"Glob pattern (e.g., '*.py', '**/*.ts')\"),\n path: z\n .string()\n .optional()\n .default(\"/\")\n .describe(\"Base path to search from (default: /)\"),\n }),\n },\n );\n}\n\n/**\n * Create grep tool using backend.\n */\nfunction createGrepTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { pattern, path = \"/\", glob = null } = input;\n const result = await resolvedBackend.grepRaw(pattern, path, glob);\n\n // If string, it's an error\n if (typeof result === \"string\") {\n return result;\n }\n\n if (result.length === 0) {\n return `No matches found for pattern '${pattern}'`;\n }\n\n // Format output: group by file\n const lines: string[] = [];\n let currentFile: string | null = null;\n for (const match of result) {\n if (match.path !== currentFile) {\n currentFile = match.path;\n lines.push(`\\n${currentFile}:`);\n }\n lines.push(` ${match.line}: ${match.text}`);\n }\n\n return lines.join(\"\\n\");\n },\n {\n name: \"grep\",\n description: customDescription || GREP_TOOL_DESCRIPTION,\n schema: z.object({\n pattern: z.string().describe(\"Regex pattern to search for\"),\n path: z\n .string()\n .optional()\n .default(\"/\")\n .describe(\"Base path to search from (default: /)\"),\n glob: z\n .string()\n .optional()\n .nullable()\n .describe(\"Optional glob pattern to filter files (e.g., '*.py')\"),\n }),\n },\n );\n}\n\n/**\n * Create execute tool using backend.\n */\nfunction createExecuteTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n\n // Runtime check - fail gracefully if not supported\n if (!isSandboxBackend(resolvedBackend)) {\n return (\n \"Error: Execution not available. This agent's backend \" +\n \"does not support command execution (SandboxBackendProtocol). \" +\n \"To use the execute tool, provide a backend that implements SandboxBackendProtocol.\"\n );\n }\n\n const result = await resolvedBackend.execute(input.command);\n\n // Format output for LLM consumption\n const parts = [result.output];\n\n if (result.exitCode !== null) {\n const status = result.exitCode === 0 ? \"succeeded\" : \"failed\";\n parts.push(`\\n[Command ${status} with exit code ${result.exitCode}]`);\n }\n\n if (result.truncated) {\n parts.push(\"\\n[Output was truncated due to size limits]\");\n }\n\n return parts.join(\"\");\n },\n {\n name: \"execute\",\n description: customDescription || EXECUTE_TOOL_DESCRIPTION,\n schema: z.object({\n command: z.string().describe(\"The shell command to execute\"),\n }),\n },\n );\n}\n\n/**\n * Options for creating filesystem middleware.\n */\nexport interface FilesystemMiddlewareOptions {\n /** Backend instance or factory (default: StateBackend) */\n backend?: BackendProtocol | BackendFactory;\n /** Optional custom system prompt override */\n systemPrompt?: string | null;\n /** Optional custom tool descriptions override */\n customToolDescriptions?: Record<string, string> | null;\n /** Optional token limit before evicting a tool result to the filesystem (default: 20000 tokens, ~80KB) */\n toolTokenLimitBeforeEvict?: number | null;\n}\n\n/**\n * Create filesystem middleware with all tools and features.\n */\nexport function createFilesystemMiddleware(\n options: FilesystemMiddlewareOptions = {},\n) {\n const {\n backend = (stateAndStore: StateAndStore) => new StateBackend(stateAndStore),\n systemPrompt: customSystemPrompt = null,\n customToolDescriptions = null,\n toolTokenLimitBeforeEvict = 20000,\n } = options;\n\n const baseSystemPrompt = customSystemPrompt || FILESYSTEM_SYSTEM_PROMPT;\n\n // All tools including execute (execute will be filtered at runtime if backend doesn't support it)\n const allTools = [\n createLsTool(backend, {\n customDescription: customToolDescriptions?.ls,\n }),\n createReadFileTool(backend, {\n customDescription: customToolDescriptions?.read_file,\n }),\n createWriteFileTool(backend, {\n customDescription: customToolDescriptions?.write_file,\n }),\n createEditFileTool(backend, {\n customDescription: customToolDescriptions?.edit_file,\n }),\n createGlobTool(backend, {\n customDescription: customToolDescriptions?.glob,\n }),\n createGrepTool(backend, {\n customDescription: customToolDescriptions?.grep,\n }),\n createExecuteTool(backend, {\n customDescription: customToolDescriptions?.execute,\n }),\n ];\n\n return createMiddleware({\n name: \"FilesystemMiddleware\",\n stateSchema: FilesystemStateSchema,\n tools: allTools,\n wrapModelCall: async (request, handler) => {\n // Check if backend supports execution\n const stateAndStore: StateAndStore = {\n state: request.state || {},\n // @ts-expect-error - request.config is incorrect typed\n store: request.config?.store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const supportsExecution = isSandboxBackend(resolvedBackend);\n\n // Filter tools based on backend capabilities\n let tools = request.tools;\n if (!supportsExecution) {\n tools = tools.filter((t: { name: string }) => t.name !== \"execute\");\n }\n\n // Build system prompt - add execution instructions if available\n let systemPrompt = baseSystemPrompt;\n if (supportsExecution) {\n systemPrompt = `${systemPrompt}\\n\\n${EXECUTION_SYSTEM_PROMPT}`;\n }\n\n // Combine with existing system prompt\n const currentSystemPrompt = request.systemPrompt || \"\";\n const newSystemPrompt = currentSystemPrompt\n ? `${currentSystemPrompt}\\n\\n${systemPrompt}`\n : systemPrompt;\n\n return handler({ ...request, tools, systemPrompt: newSystemPrompt });\n },\n wrapToolCall: async (request, handler) => {\n // Return early if eviction is disabled\n if (!toolTokenLimitBeforeEvict) {\n return handler(request);\n }\n\n // Check if this tool is excluded from eviction\n const toolName = request.toolCall?.name;\n if (\n toolName &&\n TOOLS_EXCLUDED_FROM_EVICTION.includes(\n toolName as (typeof TOOLS_EXCLUDED_FROM_EVICTION)[number],\n )\n ) {\n return handler(request);\n }\n\n const result = await handler(request);\n\n async function processToolMessage(\n msg: ToolMessage,\n toolTokenLimitBeforeEvict: number,\n ) {\n if (\n typeof msg.content === \"string\" &&\n msg.content.length > toolTokenLimitBeforeEvict * NUM_CHARS_PER_TOKEN\n ) {\n // Build StateAndStore from request\n const stateAndStore: StateAndStore = {\n state: request.state || {},\n // @ts-expect-error - request.config is incorrect typed\n store: request.config?.store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const sanitizedId = sanitizeToolCallId(\n request.toolCall?.id || msg.tool_call_id,\n );\n const evictPath = `/large_tool_results/${sanitizedId}`;\n\n const writeResult = await resolvedBackend.write(\n evictPath,\n msg.content,\n );\n\n if (writeResult.error) {\n return { message: msg, filesUpdate: null };\n }\n\n // Create preview showing head and tail of the result\n const contentSample = createContentPreview(msg.content);\n const replacementText = TOO_LARGE_TOOL_MSG.replace(\n \"{tool_call_id}\",\n msg.tool_call_id,\n )\n .replace(\"{file_path}\", evictPath)\n .replace(\"{content_sample}\", contentSample);\n\n const truncatedMessage = new ToolMessage({\n content: replacementText,\n tool_call_id: msg.tool_call_id,\n name: msg.name,\n });\n\n return {\n message: truncatedMessage,\n filesUpdate: writeResult.filesUpdate,\n };\n }\n return { message: msg, filesUpdate: null };\n }\n\n if (ToolMessage.isInstance(result)) {\n const processed = await processToolMessage(\n result,\n toolTokenLimitBeforeEvict,\n );\n\n if (processed.filesUpdate) {\n return new Command({\n update: {\n files: processed.filesUpdate,\n messages: [processed.message],\n },\n });\n }\n\n return processed.message;\n }\n\n if (isCommand(result)) {\n const update = result.update as any;\n if (!update?.messages) {\n return result;\n }\n\n let hasLargeResults = false;\n const accumulatedFiles: Record<string, FileData> = update.files\n ? { ...update.files }\n : {};\n const processedMessages: ToolMessage[] = [];\n\n for (const msg of update.messages) {\n if (ToolMessage.isInstance(msg)) {\n const processed = await processToolMessage(\n msg,\n toolTokenLimitBeforeEvict,\n );\n processedMessages.push(processed.message);\n\n if (processed.filesUpdate) {\n hasLargeResults = true;\n Object.assign(accumulatedFiles, processed.filesUpdate);\n }\n } else {\n processedMessages.push(msg);\n }\n }\n\n if (hasLargeResults) {\n return new Command({\n update: {\n ...update,\n messages: processedMessages,\n files: accumulatedFiles,\n },\n });\n }\n }\n\n return result;\n },\n });\n}\n","import { z } from \"zod/v4\";\nimport {\n createMiddleware,\n createAgent,\n AgentMiddleware,\n tool,\n ToolMessage,\n humanInTheLoopMiddleware,\n SystemMessage,\n type InterruptOnConfig,\n type ReactAgent,\n StructuredTool,\n} from \"langchain\";\nimport { Command, getCurrentTaskInput } from \"@langchain/langgraph\";\nimport type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport type { Runnable } from \"@langchain/core/runnables\";\nimport { HumanMessage } from \"@langchain/core/messages\";\n\nexport type { AgentMiddleware };\n\n// Constants\nconst DEFAULT_SUBAGENT_PROMPT =\n \"In order to complete the objective that the user asks of you, you have access to a number of standard tools.\";\n\n// State keys that are excluded when passing state to subagents and when returning\n// updates from subagents.\n// When returning updates:\n// 1. The messages key is handled explicitly to ensure only the final message is included\n// 2. The todos and structuredResponse keys are excluded as they do not have a defined reducer\n// and no clear meaning for returning them from a subagent to the main agent.\n// 3. The files key is excluded to prevent concurrent subagents from writing to the files\n// channel simultaneously (which causes LastValue errors in LangGraph).\nconst EXCLUDED_STATE_KEYS = [\n \"messages\",\n \"todos\",\n \"structuredResponse\",\n \"files\",\n] as const;\n\nconst DEFAULT_GENERAL_PURPOSE_DESCRIPTION =\n \"General-purpose agent for researching complex questions, searching for files and content, and executing multi-step tasks. When you are searching for a keyword or file and are not confident that you will find the right match in the first few tries use this agent to perform the search for you. This agent has access to all tools as the main agent.\";\n\n// Comprehensive task tool description from Python\nfunction getTaskToolDescription(subagentDescriptions: string[]): string {\n return `\nLaunch an ephemeral subagent to handle complex, multi-step independent tasks with isolated context windows.\n\nAvailable agent types and the tools they have access to:\n${subagentDescriptions.join(\"\\n\")}\n\nWhen using the Task tool, you must specify a subagent_type parameter to select which agent type to use.\n\n## Usage notes:\n1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses\n2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.\n3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.\n4. The agent's outputs should generally be trusted\n5. Clearly tell the agent whether you expect it to create content, perform analysis, or just do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent\n6. If the agent description mentions that it should be used proactively, then you should try your best to use it without the user having to ask for it first. Use your judgement.\n7. When only the general-purpose agent is provided, you should use it for all tasks. It is great for isolating context and token usage, and completing specific, complex tasks, as it has all the same capabilities as the main agent.\n\n### Example usage of the general-purpose agent:\n\n<example_agent_descriptions>\n\"general-purpose\": use this agent for general purpose tasks, it has access to all tools as the main agent.\n</example_agent_descriptions>\n\n<example>\nUser: \"I want to conduct research on the accomplishments of Lebron James, Michael Jordan, and Kobe Bryant, and then compare them.\"\nAssistant: *Uses the task tool in parallel to conduct isolated research on each of the three players*\nAssistant: *Synthesizes the results of the three isolated research tasks and responds to the User*\n<commentary>\nResearch is a complex, multi-step task in it of itself.\nThe research of each individual player is not dependent on the research of the other players.\nThe assistant uses the task tool to break down the complex objective into three isolated tasks.\nEach research task only needs to worry about context and tokens about one player, then returns synthesized information about each player as the Tool Result.\nThis means each research task can dive deep and spend tokens and context deeply researching each player, but the final result is synthesized information, and saves us tokens in the long run when comparing the players to each other.\n</commentary>\n</example>\n\n<example>\nUser: \"Analyze a single large code repository for security vulnerabilities and generate a report.\"\nAssistant: *Launches a single \\`task\\` subagent for the repository analysis*\nAssistant: *Receives report and integrates results into final summary*\n<commentary>\nSubagent is used to isolate a large, context-heavy task, even though there is only one. This prevents the main thread from being overloaded with details.\nIf the user then asks followup questions, we have a concise report to reference instead of the entire history of analysis and tool calls, which is good and saves us time and money.\n</commentary>\n</example>\n\n<example>\nUser: \"Schedule two meetings for me and prepare agendas for each.\"\nAssistant: *Calls the task tool in parallel to launch two \\`task\\` subagents (one per meeting) to prepare agendas*\nAssistant: *Returns final schedules and agendas*\n<commentary>\nTasks are simple individually, but subagents help silo agenda preparation.\nEach subagent only needs to worry about the agenda for one meeting.\n</commentary>\n</example>\n\n<example>\nUser: \"I want to order a pizza from Dominos, order a burger from McDonald's, and order a salad from Subway.\"\nAssistant: *Calls tools directly in parallel to order a pizza from Dominos, a burger from McDonald's, and a salad from Subway*\n<commentary>\nThe assistant did not use the task tool because the objective is super simple and clear and only requires a few trivial tool calls.\nIt is better to just complete the task directly and NOT use the \\`task\\`tool.\n</commentary>\n</example>\n\n### Example usage with custom agents:\n\n<example_agent_descriptions>\n\"content-reviewer\": use this agent after you are done creating significant content or documents\n\"greeting-responder\": use this agent when to respond to user greetings with a friendly joke\n\"research-analyst\": use this agent to conduct thorough research on complex topics\n</example_agent_description>\n\n<example>\nuser: \"Please write a function that checks if a number is prime\"\nassistant: Sure let me write a function that checks if a number is prime\nassistant: First let me use the Write tool to write a function that checks if a number is prime\nassistant: I'm going to use the Write tool to write the following code:\n<code>\nfunction isPrime(n) {\n if (n <= 1) return false\n for (let i = 2; i * i <= n; i++) {\n if (n % i === 0) return false\n }\n return true\n}\n</code>\n<commentary>\nSince significant content was created and the task was completed, now use the content-reviewer agent to review the work\n</commentary>\nassistant: Now let me use the content-reviewer agent to review the code\nassistant: Uses the Task tool to launch with the content-reviewer agent\n</example>\n\n<example>\nuser: \"Can you help me research the environmental impact of different renewable energy sources and create a comprehensive report?\"\n<commentary>\nThis is a complex research task that would benefit from using the research-analyst agent to conduct thorough analysis\n</commentary>\nassistant: I'll help you research the environmental impact of renewable energy sources. Let me use the research-analyst agent to conduct comprehensive research on this topic.\nassistant: Uses the Task tool to launch with the research-analyst agent, providing detailed instructions about what research to conduct and what format the report should take\n</example>\n\n<example>\nuser: \"Hello\"\n<commentary>\nSince the user is greeting, use the greeting-responder agent to respond with a friendly joke\n</commentary>\nassistant: \"I'm going to use the Task tool to launch with the greeting-responder agent\"\n</example>\n `.trim();\n}\n\nconst TASK_SYSTEM_PROMPT = `## \\`task\\` (subagent spawner)\n\nYou have access to a \\`task\\` tool to launch short-lived subagents that handle isolated tasks. These agents are ephemeral — they live only for the duration of the task and return a single result.\n\nWhen to use the task tool:\n- When a task is complex and multi-step, and can be fully delegated in isolation\n- When a task is independent of other tasks and can run in parallel\n- When a task requires focused reasoning or heavy token/context usage that would bloat the orchestrator thread\n- When sandboxing improves reliability (e.g. code execution, structured searches, data formatting)\n- When you only care about the output of the subagent, and not the intermediate steps (ex. performing a lot of research and then returned a synthesized report, performing a series of computations or lookups to achieve a concise, relevant answer.)\n\nSubagent lifecycle:\n1. **Spawn** → Provide clear role, instructions, and expected output\n2. **Run** → The subagent completes the task autonomously\n3. **Return** → The subagent provides a single structured result\n4. **Reconcile** → Incorporate or synthesize the result into the main thread\n\nWhen NOT to use the task tool:\n- If you need to see the intermediate reasoning or steps after the subagent has completed (the task tool hides them)\n- If the task is trivial (a few tool calls or simple lookup)\n- If delegating does not reduce token usage, complexity, or context switching\n- If splitting would add latency without benefit\n\n## Important Task Tool Usage Notes to Remember\n- Whenever possible, parallelize the work that you do. This is true for both tool_calls, and for tasks. Whenever you have independent steps to complete - make tool_calls, or kick off tasks (subagents) in parallel to accomplish them faster. This saves time for the user, which is incredibly important.\n- Remember to use the \\`task\\` tool to silo independent tasks within a multi-part objective.\n- You should use the \\`task\\` tool whenever you have a complex task that will take multiple steps, and is independent from other tasks that the agent needs to complete. These agents are highly competent and efficient.`;\n\n/**\n * Type definitions for pre-compiled agents.\n *\n * @typeParam TRunnable - The type of the runnable (ReactAgent or Runnable).\n * When using `createAgent`, this preserves the middleware types for type inference.\n */\nexport interface CompiledSubAgent<\n TRunnable extends ReactAgent | Runnable = ReactAgent | Runnable,\n> {\n /** The name of the agent */\n name: string;\n /** The description of the agent */\n description: string;\n /** The agent instance */\n runnable: TRunnable;\n}\n\n/**\n * Type definitions for subagents\n */\nexport interface SubAgent {\n /** The name of the agent */\n name: string;\n /** The description of the agent */\n description: string;\n /** The system prompt to use for the agent */\n systemPrompt: string;\n /** The tools to use for the agent (tool instances, not names). Defaults to defaultTools */\n tools?: StructuredTool[];\n /** The model for the agent. Defaults to default_model */\n model?: LanguageModelLike | string;\n /** Additional middleware to append after default_middleware */\n middleware?: readonly AgentMiddleware[];\n /** The tool configs to use for the agent */\n interruptOn?: Record<string, boolean | InterruptOnConfig>;\n}\n\n/**\n * Filter state to exclude certain keys when passing to subagents\n */\nfunction filterStateForSubagent(\n state: Record<string, unknown>,\n): Record<string, unknown> {\n const filtered: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(state)) {\n if (!EXCLUDED_STATE_KEYS.includes(key as never)) {\n filtered[key] = value;\n }\n }\n return filtered;\n}\n\n/**\n * Create Command with filtered state update from subagent result\n */\nfunction returnCommandWithStateUpdate(\n result: Record<string, unknown>,\n toolCallId: string,\n): Command {\n const stateUpdate = filterStateForSubagent(result);\n const messages = result.messages as Array<{ content: string }>;\n const lastMessage = messages?.[messages.length - 1];\n\n return new Command({\n update: {\n ...stateUpdate,\n messages: [\n new ToolMessage({\n content: lastMessage?.content || \"Task completed\",\n tool_call_id: toolCallId,\n name: \"task\",\n }),\n ],\n },\n });\n}\n\n/**\n * Create subagent instances from specifications\n */\nfunction getSubagents(options: {\n defaultModel: LanguageModelLike | string;\n defaultTools: StructuredTool[];\n defaultMiddleware: AgentMiddleware[] | null;\n defaultInterruptOn: Record<string, boolean | InterruptOnConfig> | null;\n subagents: (SubAgent | CompiledSubAgent)[];\n generalPurposeAgent: boolean;\n}): {\n agents: Record<string, ReactAgent | Runnable>;\n descriptions: string[];\n} {\n const {\n defaultModel,\n defaultTools,\n defaultMiddleware,\n defaultInterruptOn,\n subagents,\n generalPurposeAgent,\n } = options;\n\n const defaultSubagentMiddleware = defaultMiddleware || [];\n const agents: Record<string, ReactAgent | Runnable> = {};\n const subagentDescriptions: string[] = [];\n\n // Create general-purpose agent if enabled\n if (generalPurposeAgent) {\n const generalPurposeMiddleware = [...defaultSubagentMiddleware];\n if (defaultInterruptOn) {\n generalPurposeMiddleware.push(\n humanInTheLoopMiddleware({ interruptOn: defaultInterruptOn }),\n );\n }\n\n const generalPurposeSubagent = createAgent({\n model: defaultModel,\n systemPrompt: DEFAULT_SUBAGENT_PROMPT,\n tools: defaultTools as any,\n middleware: generalPurposeMiddleware,\n });\n\n agents[\"general-purpose\"] = generalPurposeSubagent;\n subagentDescriptions.push(\n `- general-purpose: ${DEFAULT_GENERAL_PURPOSE_DESCRIPTION}`,\n );\n }\n\n // Process custom subagents\n for (const agentParams of subagents) {\n subagentDescriptions.push(\n `- ${agentParams.name}: ${agentParams.description}`,\n );\n\n if (\"runnable\" in agentParams) {\n agents[agentParams.name] = agentParams.runnable;\n } else {\n const middleware = agentParams.middleware\n ? [...defaultSubagentMiddleware, ...agentParams.middleware]\n : [...defaultSubagentMiddleware];\n\n const interruptOn = agentParams.interruptOn || defaultInterruptOn;\n if (interruptOn)\n middleware.push(humanInTheLoopMiddleware({ interruptOn }));\n\n agents[agentParams.name] = createAgent({\n model: agentParams.model ?? defaultModel,\n systemPrompt: agentParams.systemPrompt,\n tools: agentParams.tools ?? defaultTools,\n middleware,\n });\n }\n }\n\n return { agents, descriptions: subagentDescriptions };\n}\n\n/**\n * Create the task tool for invoking subagents\n */\nfunction createTaskTool(options: {\n defaultModel: LanguageModelLike | string;\n defaultTools: StructuredTool[];\n defaultMiddleware: AgentMiddleware[] | null;\n defaultInterruptOn: Record<string, boolean | InterruptOnConfig> | null;\n subagents: (SubAgent | CompiledSubAgent)[];\n generalPurposeAgent: boolean;\n taskDescription: string | null;\n}) {\n const {\n defaultModel,\n defaultTools,\n defaultMiddleware,\n defaultInterruptOn,\n subagents,\n generalPurposeAgent,\n taskDescription,\n } = options;\n\n const { agents: subagentGraphs, descriptions: subagentDescriptions } =\n getSubagents({\n defaultModel,\n defaultTools,\n defaultMiddleware,\n defaultInterruptOn,\n subagents,\n generalPurposeAgent,\n });\n\n const finalTaskDescription = taskDescription\n ? taskDescription\n : getTaskToolDescription(subagentDescriptions);\n\n return tool(\n async (\n input: { description: string; subagent_type: string },\n config,\n ): Promise<Command | string> => {\n const { description, subagent_type } = input;\n\n // Validate subagent type\n if (!(subagent_type in subagentGraphs)) {\n const allowedTypes = Object.keys(subagentGraphs)\n .map((k) => `\\`${k}\\``)\n .join(\", \");\n throw new Error(\n `Error: invoked agent of type ${subagent_type}, the only allowed types are ${allowedTypes}`,\n );\n }\n\n const subagent = subagentGraphs[subagent_type];\n\n // Get current state and filter it for subagent\n const currentState = getCurrentTaskInput<Record<string, unknown>>();\n const subagentState = filterStateForSubagent(currentState);\n subagentState.messages = [new HumanMessage({ content: description })];\n\n // Invoke the subagent\n const result = (await subagent.invoke(subagentState, config)) as Record<\n string,\n unknown\n >;\n\n // Return command with filtered state update\n if (!config.toolCall?.id) {\n throw new Error(\"Tool call ID is required for subagent invocation\");\n }\n\n return returnCommandWithStateUpdate(result, config.toolCall.id);\n },\n {\n name: \"task\",\n description: finalTaskDescription,\n schema: z.object({\n description: z\n .string()\n .describe(\"The task to execute with the selected agent\"),\n subagent_type: z\n .string()\n .describe(\n `Name of the agent to use. Available: ${Object.keys(subagentGraphs).join(\", \")}`,\n ),\n }),\n },\n );\n}\n\n/**\n * Options for creating subagent middleware\n */\nexport interface SubAgentMiddlewareOptions {\n /** The model to use for subagents */\n defaultModel: LanguageModelLike | string;\n /** The tools to use for the default general-purpose subagent */\n defaultTools?: StructuredTool[];\n /** Default middleware to apply to all subagents */\n defaultMiddleware?: AgentMiddleware[] | null;\n /** The tool configs for the default general-purpose subagent */\n defaultInterruptOn?: Record<string, boolean | InterruptOnConfig> | null;\n /** A list of additional subagents to provide to the agent */\n subagents?: (SubAgent | CompiledSubAgent)[];\n /** Full system prompt override */\n systemPrompt?: string | null;\n /** Whether to include the general-purpose agent */\n generalPurposeAgent?: boolean;\n /** Custom description for the task tool */\n taskDescription?: string | null;\n}\n\n/**\n * Create subagent middleware with task tool\n */\nexport function createSubAgentMiddleware(options: SubAgentMiddlewareOptions) {\n const {\n defaultModel,\n defaultTools = [],\n defaultMiddleware = null,\n defaultInterruptOn = null,\n subagents = [],\n systemPrompt = TASK_SYSTEM_PROMPT,\n generalPurposeAgent = true,\n taskDescription = null,\n } = options;\n\n const taskTool = createTaskTool({\n defaultModel,\n defaultTools,\n defaultMiddleware,\n defaultInterruptOn,\n subagents,\n generalPurposeAgent,\n taskDescription,\n });\n\n return createMiddleware({\n name: \"subAgentMiddleware\",\n tools: [taskTool],\n wrapModelCall: async (request, handler) => {\n if (systemPrompt !== null) {\n return handler({\n ...request,\n systemMessage: request.systemMessage.concat(\n new SystemMessage({ content: systemPrompt }),\n ),\n });\n }\n return handler(request);\n },\n });\n}\n","import {\n createMiddleware,\n ToolMessage,\n AIMessage,\n /**\n * required for type inference\n */\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { RemoveMessage } from \"@langchain/core/messages\";\nimport { REMOVE_ALL_MESSAGES } from \"@langchain/langgraph\";\n\n/**\n * Create middleware that patches dangling tool calls in the messages history.\n *\n * When an AI message contains tool_calls but subsequent messages don't include\n * the corresponding ToolMessage responses, this middleware adds synthetic\n * ToolMessages saying the tool call was cancelled.\n *\n * @returns AgentMiddleware that patches dangling tool calls\n *\n * @example\n * ```typescript\n * import { createAgent } from \"langchain\";\n * import { createPatchToolCallsMiddleware } from \"./middleware/patch_tool_calls\";\n *\n * const agent = createAgent({\n * model: \"claude-sonnet-4-5-20250929\",\n * middleware: [createPatchToolCallsMiddleware()],\n * });\n * ```\n */\nexport function createPatchToolCallsMiddleware() {\n return createMiddleware({\n name: \"patchToolCallsMiddleware\",\n beforeAgent: async (state) => {\n const messages = state.messages;\n\n if (!messages || messages.length === 0) {\n return;\n }\n\n const patchedMessages: any[] = [];\n\n // Iterate over the messages and add any dangling tool calls\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n patchedMessages.push(msg);\n\n // Check if this is an AI message with tool calls\n if (AIMessage.isInstance(msg) && msg.tool_calls != null) {\n for (const toolCall of msg.tool_calls) {\n // Look for a corresponding ToolMessage in the messages after this one\n const correspondingToolMsg = messages\n .slice(i)\n .find(\n (m: any) =>\n ToolMessage.isInstance(m) && m.tool_call_id === toolCall.id,\n );\n\n if (!correspondingToolMsg) {\n // We have a dangling tool call which needs a ToolMessage\n const toolMsg = `Tool call ${toolCall.name} with id ${toolCall.id} was cancelled - another message came in before it could be completed.`;\n patchedMessages.push(\n new ToolMessage({\n content: toolMsg,\n name: toolCall.name,\n tool_call_id: toolCall.id!,\n }),\n );\n }\n }\n }\n }\n\n // Return state update with RemoveMessage followed by patched messages\n return {\n messages: [\n new RemoveMessage({ id: REMOVE_ALL_MESSAGES }),\n ...patchedMessages,\n ],\n };\n },\n });\n}\n","/**\n * Middleware for loading agent memory/context from AGENTS.md files.\n *\n * This module implements support for the AGENTS.md specification (https://agents.md/),\n * loading memory/context from configurable sources and injecting into the system prompt.\n *\n * ## Overview\n *\n * AGENTS.md files provide project-specific context and instructions to help AI agents\n * work effectively. Unlike skills (which are on-demand workflows), memory is always\n * loaded and provides persistent context.\n *\n * ## Usage\n *\n * ```typescript\n * import { createMemoryMiddleware } from \"@anthropic/deepagents\";\n * import { FilesystemBackend } from \"@anthropic/deepagents\";\n *\n * // Security: FilesystemBackend allows reading/writing from the entire filesystem.\n * // Either ensure the agent is running within a sandbox OR add human-in-the-loop (HIL)\n * // approval to file operations.\n * const backend = new FilesystemBackend({ rootDir: \"/\" });\n *\n * const middleware = createMemoryMiddleware({\n * backend,\n * sources: [\n * \"~/.deepagents/AGENTS.md\",\n * \"./.deepagents/AGENTS.md\",\n * ],\n * });\n *\n * const agent = createDeepAgent({ middleware: [middleware] });\n * ```\n *\n * ## Memory Sources\n *\n * Sources are simply paths to AGENTS.md files that are loaded in order and combined.\n * Multiple sources are concatenated in order, with all content included.\n * Later sources appear after earlier ones in the combined prompt.\n *\n * ## File Format\n *\n * AGENTS.md files are standard Markdown with no required structure.\n * Common sections include:\n * - Project overview\n * - Build/test commands\n * - Code style guidelines\n * - Architecture notes\n */\n\nimport { z } from \"zod\";\nimport {\n createMiddleware,\n /**\n * required for type inference\n */\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\n\nimport type { BackendProtocol, BackendFactory } from \"../backends/protocol.js\";\nimport type { StateBackend } from \"../backends/state.js\";\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\n/**\n * Options for the memory middleware.\n */\nexport interface MemoryMiddlewareOptions {\n /**\n * Backend instance or factory function for file operations.\n * Use a factory for StateBackend since it requires runtime state.\n */\n backend:\n | BackendProtocol\n | BackendFactory\n | ((config: { state: unknown; store?: BaseStore }) => StateBackend);\n\n /**\n * List of memory file paths to load (e.g., [\"~/.deepagents/AGENTS.md\", \"./.deepagents/AGENTS.md\"]).\n * Display names are automatically derived from the paths.\n * Sources are loaded in order.\n */\n sources: string[];\n}\n\n/**\n * State schema for memory middleware.\n */\nconst MemoryStateSchema = z.object({\n /**\n * Dict mapping source paths to their loaded content.\n * Marked as private so it's not included in the final agent state.\n */\n memoryContents: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Default system prompt template for memory.\n * Ported from Python's comprehensive memory guidelines.\n */\nconst MEMORY_SYSTEM_PROMPT = `<agent_memory>\n{memory_contents}\n</agent_memory>\n\n<memory_guidelines>\n The above <agent_memory> was loaded in from files in your filesystem. As you learn from your interactions with the user, you can save new knowledge by calling the \\`edit_file\\` tool.\n\n **Learning from feedback:**\n - One of your MAIN PRIORITIES is to learn from your interactions with the user. These learnings can be implicit or explicit. This means that in the future, you will remember this important information.\n - When you need to remember something, updating memory must be your FIRST, IMMEDIATE action - before responding to the user, before calling other tools, before doing anything else. Just update memory immediately.\n - When user says something is better/worse, capture WHY and encode it as a pattern.\n - Each correction is a chance to improve permanently - don't just fix the immediate issue, update your instructions.\n - A great opportunity to update your memories is when the user interrupts a tool call and provides feedback. You should update your memories immediately before revising the tool call.\n - Look for the underlying principle behind corrections, not just the specific mistake.\n - The user might not explicitly ask you to remember something, but if they provide information that is useful for future use, you should update your memories immediately.\n\n **Asking for information:**\n - If you lack context to perform an action (e.g. send a Slack DM, requires a user ID/email) you should explicitly ask the user for this information.\n - It is preferred for you to ask for information, don't assume anything that you do not know!\n - When the user provides information that is useful for future use, you should update your memories immediately.\n\n **When to update memories:**\n - When the user explicitly asks you to remember something (e.g., \"remember my email\", \"save this preference\")\n - When the user describes your role or how you should behave (e.g., \"you are a web researcher\", \"always do X\")\n - When the user gives feedback on your work - capture what was wrong and how to improve\n - When the user provides information required for tool use (e.g., slack channel ID, email addresses)\n - When the user provides context useful for future tasks, such as how to use tools, or which actions to take in a particular situation\n - When you discover new patterns or preferences (coding styles, conventions, workflows)\n\n **When to NOT update memories:**\n - When the information is temporary or transient (e.g., \"I'm running late\", \"I'm on my phone right now\")\n - When the information is a one-time task request (e.g., \"Find me a recipe\", \"What's 25 * 4?\")\n - When the information is a simple question that doesn't reveal lasting preferences (e.g., \"What day is it?\", \"Can you explain X?\")\n - When the information is an acknowledgment or small talk (e.g., \"Sounds good!\", \"Hello\", \"Thanks for that\")\n - When the information is stale or irrelevant in future conversations\n - Never store API keys, access tokens, passwords, or any other credentials in any file, memory, or system prompt.\n - If the user asks where to put API keys or provides an API key, do NOT echo or save it.\n\n **Examples:**\n Example 1 (remembering user information):\n User: Can you connect to my google account?\n Agent: Sure, I'll connect to your google account, what's your google account email?\n User: john@example.com\n Agent: Let me save this to my memory.\n Tool Call: edit_file(...) -> remembers that the user's google account email is john@example.com\n\n Example 2 (remembering implicit user preferences):\n User: Can you write me an example for creating a deep agent in LangChain?\n Agent: Sure, I'll write you an example for creating a deep agent in LangChain <example code in Python>\n User: Can you do this in JavaScript\n Agent: Let me save this to my memory.\n Tool Call: edit_file(...) -> remembers that the user prefers to get LangChain code examples in JavaScript\n Agent: Sure, here is the JavaScript example<example code in JavaScript>\n\n Example 3 (do not remember transient information):\n User: I'm going to play basketball tonight so I will be offline for a few hours.\n Agent: Okay I'll add a block to your calendar.\n Tool Call: create_calendar_event(...) -> just calls a tool, does not commit anything to memory, as it is transient information\n</memory_guidelines>`;\n\n/**\n * Format loaded memory contents for injection into prompt.\n * Pairs memory locations with their contents for clarity.\n */\nfunction formatMemoryContents(\n contents: Record<string, string>,\n sources: string[],\n): string {\n if (Object.keys(contents).length === 0) {\n return \"(No memory loaded)\";\n }\n\n const sections: string[] = [];\n for (const path of sources) {\n if (contents[path]) {\n sections.push(`${path}\\n${contents[path]}`);\n }\n }\n\n if (sections.length === 0) {\n return \"(No memory loaded)\";\n }\n\n return sections.join(\"\\n\\n\");\n}\n\n/**\n * Load memory content from a backend path.\n *\n * @param backend - Backend to load from.\n * @param path - Path to the AGENTS.md file.\n * @returns File content if found, null otherwise.\n */\nasync function loadMemoryFromBackend(\n backend: BackendProtocol,\n path: string,\n): Promise<string | null> {\n // Use downloadFiles if available, otherwise fall back to read\n if (!backend.downloadFiles) {\n const content = await backend.read(path);\n if (content.startsWith(\"Error:\")) {\n return null;\n }\n return content;\n }\n\n const results = await backend.downloadFiles([path]);\n\n // Should get exactly one response for one path\n if (results.length !== 1) {\n throw new Error(\n `Expected 1 response for path ${path}, got ${results.length}`,\n );\n }\n const response = results[0];\n\n if (response.error != null) {\n // For now, memory files are treated as optional. file_not_found is expected\n // and we skip silently to allow graceful degradation.\n if (response.error === \"file_not_found\") {\n return null;\n }\n // Other errors should be raised\n throw new Error(`Failed to download ${path}: ${response.error}`);\n }\n\n if (response.content != null) {\n // Content is a Uint8Array, decode to string\n return new TextDecoder().decode(response.content);\n }\n\n return null;\n}\n\n/**\n * Create middleware for loading agent memory from AGENTS.md files.\n *\n * Loads memory content from configured sources and injects into the system prompt.\n * Supports multiple sources that are combined together.\n *\n * @param options - Configuration options\n * @returns AgentMiddleware for memory loading and injection\n *\n * @example\n * ```typescript\n * const middleware = createMemoryMiddleware({\n * backend: new FilesystemBackend({ rootDir: \"/\" }),\n * sources: [\n * \"~/.deepagents/AGENTS.md\",\n * \"./.deepagents/AGENTS.md\",\n * ],\n * });\n * ```\n */\nexport function createMemoryMiddleware(options: MemoryMiddlewareOptions) {\n const { backend, sources } = options;\n\n /**\n * Resolve backend from instance or factory.\n */\n function getBackend(state: unknown): BackendProtocol {\n if (typeof backend === \"function\") {\n // It's a factory - call it with state\n return backend({ state }) as BackendProtocol;\n }\n return backend;\n }\n\n return createMiddleware({\n name: \"MemoryMiddleware\",\n stateSchema: MemoryStateSchema,\n\n async beforeAgent(state) {\n // Skip if already loaded\n if (\"memoryContents\" in state && state.memoryContents != null) {\n return undefined;\n }\n\n const resolvedBackend = getBackend(state);\n const contents: Record<string, string> = {};\n\n for (const path of sources) {\n try {\n const content = await loadMemoryFromBackend(resolvedBackend, path);\n if (content) {\n contents[path] = content;\n }\n } catch (error) {\n // Log but continue - memory is optional\n // eslint-disable-next-line no-console\n console.debug(`Failed to load memory from ${path}:`, error);\n }\n }\n\n return { memoryContents: contents };\n },\n\n wrapModelCall(request, handler) {\n // Get memory contents from state\n const memoryContents: Record<string, string> =\n request.state?.memoryContents || {};\n\n // Format memory section\n const formattedContents = formatMemoryContents(memoryContents, sources);\n\n const memorySection = MEMORY_SYSTEM_PROMPT.replace(\n \"{memory_contents}\",\n formattedContents,\n );\n\n // Prepend memory section to system prompt\n const currentSystemPrompt = request.systemPrompt || \"\";\n const newSystemPrompt = currentSystemPrompt\n ? `${memorySection}\\n\\n${currentSystemPrompt}`\n : memorySection;\n\n return handler({ ...request, systemPrompt: newSystemPrompt });\n },\n });\n}\n","/* eslint-disable no-console */\n/**\n * Backend-agnostic skills middleware for loading agent skills from any backend.\n *\n * This middleware implements Anthropic's agent skills pattern with progressive disclosure,\n * loading skills from backend storage via configurable sources.\n *\n * ## Architecture\n *\n * Skills are loaded from one or more **sources** - paths in a backend where skills are\n * organized. Sources are loaded in order, with later sources overriding earlier ones\n * when skills have the same name (last one wins). This enables layering: base -> user\n * -> project -> team skills.\n *\n * The middleware uses backend APIs exclusively (no direct filesystem access), making it\n * portable across different storage backends (filesystem, state, remote storage, etc.).\n *\n * ## Usage\n *\n * ```typescript\n * import { createSkillsMiddleware, FilesystemBackend } from \"@anthropic/deepagents\";\n *\n * const middleware = createSkillsMiddleware({\n * backend: new FilesystemBackend({ rootDir: \"/\" }),\n * sources: [\n * \"/skills/user/\",\n * \"/skills/project/\",\n * ],\n * });\n *\n * const agent = createDeepAgent({ middleware: [middleware] });\n * ```\n *\n * Or use the `skills` parameter on createDeepAgent:\n *\n * ```typescript\n * const agent = createDeepAgent({\n * skills: [\"/skills/user/\", \"/skills/project/\"],\n * });\n * ```\n */\n\nimport { z } from \"zod\";\nimport yaml from \"yaml\";\nimport {\n createMiddleware,\n /**\n * required for type inference\n */\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\n\nimport type { BackendProtocol, BackendFactory } from \"../backends/protocol.js\";\nimport type { StateBackend } from \"../backends/state.js\";\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\n// Security: Maximum size for SKILL.md files to prevent DoS attacks (10MB)\nexport const MAX_SKILL_FILE_SIZE = 10 * 1024 * 1024;\n\n// Agent Skills specification constraints (https://agentskills.io/specification)\nexport const MAX_SKILL_NAME_LENGTH = 64;\nexport const MAX_SKILL_DESCRIPTION_LENGTH = 1024;\n\n/**\n * Metadata for a skill per Agent Skills specification.\n */\nexport interface SkillMetadata {\n /** Skill identifier (max 64 chars, lowercase alphanumeric and hyphens) */\n name: string;\n\n /** What the skill does (max 1024 chars) */\n description: string;\n\n /** Path to the SKILL.md file in the backend */\n path: string;\n\n /** License name or reference to bundled license file */\n license?: string | null;\n\n /** Environment requirements (max 500 chars) */\n compatibility?: string | null;\n\n /** Arbitrary key-value mapping for additional metadata */\n metadata?: Record<string, string>;\n\n /** List of pre-approved tools (experimental) */\n allowedTools?: string[];\n}\n\n/**\n * Options for the skills middleware.\n */\nexport interface SkillsMiddlewareOptions {\n /**\n * Backend instance or factory function for file operations.\n * Use a factory for StateBackend since it requires runtime state.\n */\n backend:\n | BackendProtocol\n | BackendFactory\n | ((config: { state: unknown; store?: BaseStore }) => StateBackend);\n\n /**\n * List of skill source paths to load (e.g., [\"/skills/user/\", \"/skills/project/\"]).\n * Paths must use POSIX conventions (forward slashes).\n * Later sources override earlier ones for skills with the same name (last one wins).\n */\n sources: string[];\n}\n\n/**\n * State schema for skills middleware.\n */\nconst SkillsStateSchema = z.object({\n skillsMetadata: z\n .array(\n z.object({\n name: z.string(),\n description: z.string(),\n path: z.string(),\n license: z.string().nullable().optional(),\n compatibility: z.string().nullable().optional(),\n metadata: z.record(z.string(), z.string()).optional(),\n allowedTools: z.array(z.string()).optional(),\n }),\n )\n .optional(),\n});\n\n/**\n * Skills System Documentation prompt template.\n */\nconst SKILLS_SYSTEM_PROMPT = `\n## Skills System\n\nYou have access to a skills library that provides specialized capabilities and domain knowledge.\n\n{skills_locations}\n\n**Available Skills:**\n\n{skills_list}\n\n**How to Use Skills (Progressive Disclosure):**\n\nSkills follow a **progressive disclosure** pattern - you know they exist (name + description above), but you only read the full instructions when needed:\n\n1. **Recognize when a skill applies**: Check if the user's task matches any skill's description\n2. **Read the skill's full instructions**: The skill list above shows the exact path to use with read_file\n3. **Follow the skill's instructions**: SKILL.md contains step-by-step workflows, best practices, and examples\n4. **Access supporting files**: Skills may include Python scripts, configs, or reference docs - use absolute paths\n\n**When to Use Skills:**\n- When the user's request matches a skill's domain (e.g., \"research X\" → web-research skill)\n- When you need specialized knowledge or structured workflows\n- When a skill provides proven patterns for complex tasks\n\n**Skills are Self-Documenting:**\n- Each SKILL.md tells you exactly what the skill does and how to use it\n- The skill list above shows the full path for each skill's SKILL.md file\n\n**Executing Skill Scripts:**\nSkills may contain Python scripts or other executable files. Always use absolute paths from the skill list.\n\n**Example Workflow:**\n\nUser: \"Can you research the latest developments in quantum computing?\"\n\n1. Check available skills above → See \"web-research\" skill with its full path\n2. Read the skill using the path shown in the list\n3. Follow the skill's research workflow (search → organize → synthesize)\n4. Use any helper scripts with absolute paths\n\nRemember: Skills are tools to make you more capable and consistent. When in doubt, check if a skill exists for the task!\n`;\n\n/**\n * Validate skill name per Agent Skills specification.\n */\nfunction validateSkillName(\n name: string,\n directoryName: string,\n): { valid: boolean; error: string } {\n if (!name) {\n return { valid: false, error: \"name is required\" };\n }\n if (name.length > MAX_SKILL_NAME_LENGTH) {\n return { valid: false, error: \"name exceeds 64 characters\" };\n }\n // Pattern: lowercase alphanumeric, single hyphens between segments, no start/end hyphen\n if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(name)) {\n return {\n valid: false,\n error: \"name must be lowercase alphanumeric with single hyphens only\",\n };\n }\n if (name !== directoryName) {\n return {\n valid: false,\n error: `name '${name}' must match directory name '${directoryName}'`,\n };\n }\n return { valid: true, error: \"\" };\n}\n\n/**\n * Parse YAML frontmatter from SKILL.md content.\n */\nfunction parseSkillMetadataFromContent(\n content: string,\n skillPath: string,\n directoryName: string,\n): SkillMetadata | null {\n if (content.length > MAX_SKILL_FILE_SIZE) {\n console.warn(\n `Skipping ${skillPath}: content too large (${content.length} bytes)`,\n );\n return null;\n }\n\n // Match YAML frontmatter between --- delimiters\n const frontmatterPattern = /^---\\s*\\n([\\s\\S]*?)\\n---\\s*\\n/;\n const match = content.match(frontmatterPattern);\n\n if (!match) {\n console.warn(`Skipping ${skillPath}: no valid YAML frontmatter found`);\n return null;\n }\n\n const frontmatterStr = match[1];\n\n // Parse YAML\n let frontmatterData: Record<string, unknown>;\n try {\n frontmatterData = yaml.parse(frontmatterStr);\n } catch (e) {\n console.warn(`Invalid YAML in ${skillPath}:`, e);\n return null;\n }\n\n if (!frontmatterData || typeof frontmatterData !== \"object\") {\n console.warn(`Skipping ${skillPath}: frontmatter is not a mapping`);\n return null;\n }\n\n // Validate required fields\n const name = frontmatterData.name as string | undefined;\n const description = frontmatterData.description as string | undefined;\n\n if (!name || !description) {\n console.warn(\n `Skipping ${skillPath}: missing required 'name' or 'description'`,\n );\n return null;\n }\n\n // Validate name format per spec (warn but continue for backwards compatibility)\n const validation = validateSkillName(String(name), directoryName);\n if (!validation.valid) {\n console.warn(\n `Skill '${name}' in ${skillPath} does not follow Agent Skills specification: ${validation.error}. Consider renaming for spec compliance.`,\n );\n }\n\n // Validate description length per spec (max 1024 chars)\n let descriptionStr = String(description).trim();\n if (descriptionStr.length > MAX_SKILL_DESCRIPTION_LENGTH) {\n console.warn(\n `Description exceeds ${MAX_SKILL_DESCRIPTION_LENGTH} characters in ${skillPath}, truncating`,\n );\n descriptionStr = descriptionStr.slice(0, MAX_SKILL_DESCRIPTION_LENGTH);\n }\n\n // Parse allowed-tools\n const allowedToolsStr = frontmatterData[\"allowed-tools\"] as\n | string\n | undefined;\n const allowedTools = allowedToolsStr ? allowedToolsStr.split(\" \") : [];\n\n return {\n name: String(name),\n description: descriptionStr,\n path: skillPath,\n metadata: (frontmatterData.metadata as Record<string, string>) || {},\n license:\n typeof frontmatterData.license === \"string\"\n ? frontmatterData.license.trim() || null\n : null,\n compatibility:\n typeof frontmatterData.compatibility === \"string\"\n ? frontmatterData.compatibility.trim() || null\n : null,\n allowedTools,\n };\n}\n\n/**\n * List all skills from a backend source.\n */\nasync function listSkillsFromBackend(\n backend: BackendProtocol,\n sourcePath: string,\n): Promise<SkillMetadata[]> {\n const skills: SkillMetadata[] = [];\n\n // Normalize path to ensure it ends with /\n const normalizedPath = sourcePath.endsWith(\"/\")\n ? sourcePath\n : `${sourcePath}/`;\n\n // List directories in the source path using lsInfo\n let fileInfos: { path: string; is_dir?: boolean }[];\n try {\n fileInfos = await backend.lsInfo(normalizedPath);\n } catch {\n // Source path doesn't exist or can't be listed\n return [];\n }\n\n // Convert FileInfo[] to entries format\n const entries = fileInfos.map((info) => ({\n name: info.path.replace(/\\/$/, \"\").split(\"/\").pop() || \"\",\n type: (info.is_dir ? \"directory\" : \"file\") as \"file\" | \"directory\",\n }));\n\n // Look for subdirectories containing SKILL.md\n for (const entry of entries) {\n if (entry.type !== \"directory\") {\n continue;\n }\n\n const skillMdPath = `${normalizedPath}${entry.name}/SKILL.md`;\n\n // Try to download the SKILL.md file\n let content: string;\n if (backend.downloadFiles) {\n const results = await backend.downloadFiles([skillMdPath]);\n if (results.length !== 1) {\n continue;\n }\n\n const response = results[0];\n if (response.error != null || response.content == null) {\n continue;\n }\n\n // Decode content\n content = new TextDecoder().decode(response.content);\n } else {\n // Fall back to read if downloadFiles is not available\n const readResult = await backend.read(skillMdPath);\n if (readResult.startsWith(\"Error:\")) {\n continue;\n }\n content = readResult;\n }\n const metadata = parseSkillMetadataFromContent(\n content,\n skillMdPath,\n entry.name,\n );\n\n if (metadata) {\n skills.push(metadata);\n }\n }\n\n return skills;\n}\n\n/**\n * Format skills locations for display in system prompt.\n * Shows priority indicator for the last source (highest priority).\n */\nfunction formatSkillsLocations(sources: string[]): string {\n if (sources.length === 0) {\n return \"**Skills Sources:** None configured\";\n }\n\n const lines: string[] = [];\n for (let i = 0; i < sources.length; i++) {\n const sourcePath = sources[i];\n // Extract a friendly name from the path (last non-empty component)\n const name =\n sourcePath\n .replace(/\\/$/, \"\")\n .split(\"/\")\n .filter(Boolean)\n .pop()\n ?.replace(/^./, (c) => c.toUpperCase()) || \"Skills\";\n const suffix = i === sources.length - 1 ? \" (higher priority)\" : \"\";\n lines.push(`**${name} Skills**: \\`${sourcePath}\\`${suffix}`);\n }\n return lines.join(\"\\n\");\n}\n\n/**\n * Format skills metadata for display in system prompt.\n * Shows allowed tools for each skill if specified.\n */\nfunction formatSkillsList(skills: SkillMetadata[], sources: string[]): string {\n if (skills.length === 0) {\n const paths = sources.map((s) => `\\`${s}\\``).join(\" or \");\n return `(No skills available yet. You can create skills in ${paths})`;\n }\n\n const lines: string[] = [];\n for (const skill of skills) {\n lines.push(`- **${skill.name}**: ${skill.description}`);\n if (skill.allowedTools && skill.allowedTools.length > 0) {\n lines.push(` → Allowed tools: ${skill.allowedTools.join(\", \")}`);\n }\n lines.push(` → Read \\`${skill.path}\\` for full instructions`);\n }\n\n return lines.join(\"\\n\");\n}\n\n/**\n * Create backend-agnostic middleware for loading and exposing agent skills.\n *\n * This middleware loads skills from configurable backend sources and injects\n * skill metadata into the system prompt. It implements the progressive disclosure\n * pattern: skill names and descriptions are shown in the prompt, but the agent\n * reads full SKILL.md content only when needed.\n *\n * @param options - Configuration options\n * @returns AgentMiddleware for skills loading and injection\n *\n * @example\n * ```typescript\n * const middleware = createSkillsMiddleware({\n * backend: new FilesystemBackend({ rootDir: \"/\" }),\n * sources: [\"/skills/user/\", \"/skills/project/\"],\n * });\n * ```\n */\nexport function createSkillsMiddleware(options: SkillsMiddlewareOptions) {\n const { backend, sources } = options;\n\n // Closure variable to store loaded skills - wrapModelCall can access this\n // directly since beforeAgent state updates aren't immediately available\n let loadedSkills: SkillMetadata[] = [];\n\n /**\n * Resolve backend from instance or factory.\n */\n function getBackend(state: unknown): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend({ state }) as BackendProtocol;\n }\n return backend;\n }\n\n return createMiddleware({\n name: \"SkillsMiddleware\",\n stateSchema: SkillsStateSchema,\n\n async beforeAgent(state) {\n // Skip if already loaded (check both closure and state)\n if (loadedSkills.length > 0) {\n return undefined;\n }\n if (\"skillsMetadata\" in state && state.skillsMetadata != null) {\n // Restore from state (e.g., after checkpoint restore)\n loadedSkills = state.skillsMetadata as SkillMetadata[];\n return undefined;\n }\n\n const resolvedBackend = getBackend(state);\n const allSkills: Map<string, SkillMetadata> = new Map();\n\n // Load skills from each source in order (later sources override earlier)\n for (const sourcePath of sources) {\n try {\n const skills = await listSkillsFromBackend(\n resolvedBackend,\n sourcePath,\n );\n for (const skill of skills) {\n allSkills.set(skill.name, skill);\n }\n } catch (error) {\n // Log but continue - individual source failures shouldn't break everything\n console.debug(\n `[BackendSkillsMiddleware] Failed to load skills from ${sourcePath}:`,\n error,\n );\n }\n }\n\n // Store in closure for immediate access by wrapModelCall\n loadedSkills = Array.from(allSkills.values());\n\n return { skillsMetadata: loadedSkills };\n },\n\n wrapModelCall(request, handler) {\n // Use closure variable which is populated by beforeAgent\n // Fall back to state for checkpoint restore scenarios\n const skillsMetadata: SkillMetadata[] =\n loadedSkills.length > 0\n ? loadedSkills\n : (request.state?.skillsMetadata as SkillMetadata[]) || [];\n\n // Format skills section\n const skillsLocations = formatSkillsLocations(sources);\n const skillsList = formatSkillsList(skillsMetadata, sources);\n\n const skillsSection = SKILLS_SYSTEM_PROMPT.replace(\n \"{skills_locations}\",\n skillsLocations,\n ).replace(\"{skills_list}\", skillsList);\n\n // Append to existing system prompt\n const currentSystemPrompt = request.systemPrompt || \"\";\n const newSystemPrompt = currentSystemPrompt\n ? `${currentSystemPrompt}\\n\\n${skillsSection}`\n : skillsSection;\n\n return handler({ ...request, systemPrompt: newSystemPrompt });\n },\n });\n}\n","/**\n * Utility functions for middleware.\n *\n * This module provides shared helpers used across middleware implementations.\n */\n\nimport { SystemMessage } from \"@langchain/core/messages\";\n\n/**\n * Append text to a system message.\n *\n * Creates a new SystemMessage with the text appended to the existing content.\n * If the original message has content, the new text is separated by two newlines.\n *\n * @param systemMessage - Existing system message or null/undefined.\n * @param text - Text to add to the system message.\n * @returns New SystemMessage with the text appended.\n *\n * @example\n * ```typescript\n * const original = new SystemMessage({ content: \"You are a helpful assistant.\" });\n * const updated = appendToSystemMessage(original, \"Always be concise.\");\n * // Result: SystemMessage with content \"You are a helpful assistant.\\n\\nAlways be concise.\"\n * ```\n */\nexport function appendToSystemMessage(\n systemMessage: SystemMessage | null | undefined,\n text: string,\n): SystemMessage {\n if (!systemMessage) {\n return new SystemMessage({ content: text });\n }\n\n // Handle both string and array content formats\n const existingContent = systemMessage.content;\n\n if (typeof existingContent === \"string\") {\n const newContent = existingContent ? `${existingContent}\\n\\n${text}` : text;\n return new SystemMessage({ content: newContent });\n }\n\n // For array content (content blocks), append as a new text block\n if (Array.isArray(existingContent)) {\n const newContent = [...existingContent];\n const textToAdd = newContent.length > 0 ? `\\n\\n${text}` : text;\n newContent.push({ type: \"text\", text: textToAdd });\n return new SystemMessage({ content: newContent });\n }\n\n // Fallback for unknown content type\n return new SystemMessage({ content: text });\n}\n\n/**\n * Prepend text to a system message.\n *\n * Creates a new SystemMessage with the text prepended to the existing content.\n * If the original message has content, the new text is separated by two newlines.\n *\n * @param systemMessage - Existing system message or null/undefined.\n * @param text - Text to prepend to the system message.\n * @returns New SystemMessage with the text prepended.\n *\n * @example\n * ```typescript\n * const original = new SystemMessage({ content: \"Always be concise.\" });\n * const updated = prependToSystemMessage(original, \"You are a helpful assistant.\");\n * // Result: SystemMessage with content \"You are a helpful assistant.\\n\\nAlways be concise.\"\n * ```\n */\nexport function prependToSystemMessage(\n systemMessage: SystemMessage | null | undefined,\n text: string,\n): SystemMessage {\n if (!systemMessage) {\n return new SystemMessage({ content: text });\n }\n\n // Handle both string and array content formats\n const existingContent = systemMessage.content;\n\n if (typeof existingContent === \"string\") {\n const newContent = existingContent ? `${text}\\n\\n${existingContent}` : text;\n return new SystemMessage({ content: newContent });\n }\n\n // For array content (content blocks), prepend as a new text block\n if (Array.isArray(existingContent)) {\n const textToAdd = existingContent.length > 0 ? `${text}\\n\\n` : text;\n const newContent = [{ type: \"text\", text: textToAdd }, ...existingContent];\n return new SystemMessage({ content: newContent });\n }\n\n // Fallback for unknown content type\n return new SystemMessage({ content: text });\n}\n","function __classPrivateFieldSet(receiver, state, value, kind, f) {\n if (kind === \"m\")\n throw new TypeError(\"Private method is not writable\");\n if (kind === \"a\" && !f)\n throw new TypeError(\"Private accessor was defined without a setter\");\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver))\n throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\n return kind === \"a\" ? f.call(receiver, value) : f ? (f.value = value) : state.set(receiver, value), value;\n}\nfunction __classPrivateFieldGet(receiver, state, kind, f) {\n if (kind === \"a\" && !f)\n throw new TypeError(\"Private accessor was defined without a getter\");\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver))\n throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\n}\nexport { __classPrivateFieldSet, __classPrivateFieldGet };\n","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n/**\n * https://stackoverflow.com/a/2117523\n */\nexport let uuid4 = function () {\n const { crypto } = globalThis;\n if (crypto?.randomUUID) {\n uuid4 = crypto.randomUUID.bind(crypto);\n return crypto.randomUUID();\n }\n const u8 = new Uint8Array(1);\n const randomByte = crypto ? () => crypto.getRandomValues(u8)[0] : () => (Math.random() * 0xff) & 0xff;\n return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) => (+c ^ (randomByte() & (15 >> (+c / 4)))).toString(16));\n};\n//# sourceMappingURL=uuid.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nexport function isAbortError(err) {\n return (typeof err === 'object' &&\n err !== null &&\n // Spec-compliant fetch implementations\n (('name' in err && err.name === 'AbortError') ||\n // Expo fetch\n ('message' in err && String(err.message).includes('FetchRequestCanceledException'))));\n}\nexport const castToError = (err) => {\n if (err instanceof Error)\n return err;\n if (typeof err === 'object' && err !== null) {\n try {\n if (Object.prototype.toString.call(err) === '[object Error]') {\n // @ts-ignore - not all envs have native support for cause yet\n const error = new Error(err.message, err.cause ? { cause: err.cause } : {});\n if (err.stack)\n error.stack = err.stack;\n // @ts-ignore - not all envs have native support for cause yet\n if (err.cause && !error.cause)\n error.cause = err.cause;\n if (err.name)\n error.name = err.name;\n return error;\n }\n }\n catch { }\n try {\n return new Error(JSON.stringify(err));\n }\n catch { }\n }\n return new Error(err);\n};\n//# sourceMappingURL=errors.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { castToError } from \"../internal/errors.mjs\";\nexport class OpenAIError extends Error {\n}\nexport class APIError extends OpenAIError {\n constructor(status, error, message, headers) {\n super(`${APIError.makeMessage(status, error, message)}`);\n this.status = status;\n this.headers = headers;\n this.requestID = headers?.get('x-request-id');\n this.error = error;\n const data = error;\n this.code = data?.['code'];\n this.param = data?.['param'];\n this.type = data?.['type'];\n }\n static makeMessage(status, error, message) {\n const msg = error?.message ?\n typeof error.message === 'string' ?\n error.message\n : JSON.stringify(error.message)\n : error ? JSON.stringify(error)\n : message;\n if (status && msg) {\n return `${status} ${msg}`;\n }\n if (status) {\n return `${status} status code (no body)`;\n }\n if (msg) {\n return msg;\n }\n return '(no status code or body)';\n }\n static generate(status, errorResponse, message, headers) {\n if (!status || !headers) {\n return new APIConnectionError({ message, cause: castToError(errorResponse) });\n }\n const error = errorResponse?.['error'];\n if (status === 400) {\n return new BadRequestError(status, error, message, headers);\n }\n if (status === 401) {\n return new AuthenticationError(status, error, message, headers);\n }\n if (status === 403) {\n return new PermissionDeniedError(status, error, message, headers);\n }\n if (status === 404) {\n return new NotFoundError(status, error, message, headers);\n }\n if (status === 409) {\n return new ConflictError(status, error, message, headers);\n }\n if (status === 422) {\n return new UnprocessableEntityError(status, error, message, headers);\n }\n if (status === 429) {\n return new RateLimitError(status, error, message, headers);\n }\n if (status >= 500) {\n return new InternalServerError(status, error, message, headers);\n }\n return new APIError(status, error, message, headers);\n }\n}\nexport class APIUserAbortError extends APIError {\n constructor({ message } = {}) {\n super(undefined, undefined, message || 'Request was aborted.', undefined);\n }\n}\nexport class APIConnectionError extends APIError {\n constructor({ message, cause }) {\n super(undefined, undefined, message || 'Connection error.', undefined);\n // in some environments the 'cause' property is already declared\n // @ts-ignore\n if (cause)\n this.cause = cause;\n }\n}\nexport class APIConnectionTimeoutError extends APIConnectionError {\n constructor({ message } = {}) {\n super({ message: message ?? 'Request timed out.' });\n }\n}\nexport class BadRequestError extends APIError {\n}\nexport class AuthenticationError extends APIError {\n}\nexport class PermissionDeniedError extends APIError {\n}\nexport class NotFoundError extends APIError {\n}\nexport class ConflictError extends APIError {\n}\nexport class UnprocessableEntityError extends APIError {\n}\nexport class RateLimitError extends APIError {\n}\nexport class InternalServerError extends APIError {\n}\nexport class LengthFinishReasonError extends OpenAIError {\n constructor() {\n super(`Could not parse response content as the length limit was reached`);\n }\n}\nexport class ContentFilterFinishReasonError extends OpenAIError {\n constructor() {\n super(`Could not parse response content as the request was rejected by the content filter`);\n }\n}\nexport class InvalidWebhookSignatureError extends Error {\n constructor(message) {\n super(message);\n }\n}\n//# sourceMappingURL=error.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { OpenAIError } from \"../../core/error.mjs\";\n// https://url.spec.whatwg.org/#url-scheme-string\nconst startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;\nexport const isAbsoluteURL = (url) => {\n return startsWithSchemeRegexp.test(url);\n};\nexport let isArray = (val) => ((isArray = Array.isArray), isArray(val));\nexport let isReadonlyArray = isArray;\n/** Returns an object if the given value isn't an object, otherwise returns as-is */\nexport function maybeObj(x) {\n if (typeof x !== 'object') {\n return {};\n }\n return x ?? {};\n}\n// https://stackoverflow.com/a/34491287\nexport function isEmptyObj(obj) {\n if (!obj)\n return true;\n for (const _k in obj)\n return false;\n return true;\n}\n// https://eslint.org/docs/latest/rules/no-prototype-builtins\nexport function hasOwn(obj, key) {\n return Object.prototype.hasOwnProperty.call(obj, key);\n}\nexport function isObj(obj) {\n return obj != null && typeof obj === 'object' && !Array.isArray(obj);\n}\nexport const ensurePresent = (value) => {\n if (value == null) {\n throw new OpenAIError(`Expected a value to be given but received ${value} instead.`);\n }\n return value;\n};\nexport const validatePositiveInteger = (name, n) => {\n if (typeof n !== 'number' || !Number.isInteger(n)) {\n throw new OpenAIError(`${name} must be an integer`);\n }\n if (n < 0) {\n throw new OpenAIError(`${name} must be a positive integer`);\n }\n return n;\n};\nexport const coerceInteger = (value) => {\n if (typeof value === 'number')\n return Math.round(value);\n if (typeof value === 'string')\n return parseInt(value, 10);\n throw new OpenAIError(`Could not coerce ${value} (type: ${typeof value}) into a number`);\n};\nexport const coerceFloat = (value) => {\n if (typeof value === 'number')\n return value;\n if (typeof value === 'string')\n return parseFloat(value);\n throw new OpenAIError(`Could not coerce ${value} (type: ${typeof value}) into a number`);\n};\nexport const coerceBoolean = (value) => {\n if (typeof value === 'boolean')\n return value;\n if (typeof value === 'string')\n return value === 'true';\n return Boolean(value);\n};\nexport const maybeCoerceInteger = (value) => {\n if (value == null) {\n return undefined;\n }\n return coerceInteger(value);\n};\nexport const maybeCoerceFloat = (value) => {\n if (value == null) {\n return undefined;\n }\n return coerceFloat(value);\n};\nexport const maybeCoerceBoolean = (value) => {\n if (value == null) {\n return undefined;\n }\n return coerceBoolean(value);\n};\nexport const safeJSON = (text) => {\n try {\n return JSON.parse(text);\n }\n catch (err) {\n return undefined;\n }\n};\n//# sourceMappingURL=values.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nexport const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));\n//# sourceMappingURL=sleep.mjs.map","export const VERSION = '6.16.0'; // x-release-please-version\n//# sourceMappingURL=version.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { VERSION } from \"../version.mjs\";\nexport const isRunningInBrowser = () => {\n return (\n // @ts-ignore\n typeof window !== 'undefined' &&\n // @ts-ignore\n typeof window.document !== 'undefined' &&\n // @ts-ignore\n typeof navigator !== 'undefined');\n};\n/**\n * Note this does not detect 'browser'; for that, use getBrowserInfo().\n */\nfunction getDetectedPlatform() {\n if (typeof Deno !== 'undefined' && Deno.build != null) {\n return 'deno';\n }\n if (typeof EdgeRuntime !== 'undefined') {\n return 'edge';\n }\n if (Object.prototype.toString.call(typeof globalThis.process !== 'undefined' ? globalThis.process : 0) === '[object process]') {\n return 'node';\n }\n return 'unknown';\n}\nconst getPlatformProperties = () => {\n const detectedPlatform = getDetectedPlatform();\n if (detectedPlatform === 'deno') {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': normalizePlatform(Deno.build.os),\n 'X-Stainless-Arch': normalizeArch(Deno.build.arch),\n 'X-Stainless-Runtime': 'deno',\n 'X-Stainless-Runtime-Version': typeof Deno.version === 'string' ? Deno.version : Deno.version?.deno ?? 'unknown',\n };\n }\n if (typeof EdgeRuntime !== 'undefined') {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': `other:${EdgeRuntime}`,\n 'X-Stainless-Runtime': 'edge',\n 'X-Stainless-Runtime-Version': globalThis.process.version,\n };\n }\n // Check if Node.js\n if (detectedPlatform === 'node') {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': normalizePlatform(globalThis.process.platform ?? 'unknown'),\n 'X-Stainless-Arch': normalizeArch(globalThis.process.arch ?? 'unknown'),\n 'X-Stainless-Runtime': 'node',\n 'X-Stainless-Runtime-Version': globalThis.process.version ?? 'unknown',\n };\n }\n const browserInfo = getBrowserInfo();\n if (browserInfo) {\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': 'unknown',\n 'X-Stainless-Runtime': `browser:${browserInfo.browser}`,\n 'X-Stainless-Runtime-Version': browserInfo.version,\n };\n }\n // TODO add support for Cloudflare workers, etc.\n return {\n 'X-Stainless-Lang': 'js',\n 'X-Stainless-Package-Version': VERSION,\n 'X-Stainless-OS': 'Unknown',\n 'X-Stainless-Arch': 'unknown',\n 'X-Stainless-Runtime': 'unknown',\n 'X-Stainless-Runtime-Version': 'unknown',\n };\n};\n// Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts\nfunction getBrowserInfo() {\n if (typeof navigator === 'undefined' || !navigator) {\n return null;\n }\n // NOTE: The order matters here!\n const browserPatterns = [\n { key: 'edge', pattern: /Edge(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'ie', pattern: /MSIE(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'ie', pattern: /Trident(?:.*rv\\:(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'chrome', pattern: /Chrome(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'firefox', pattern: /Firefox(?:\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?/ },\n { key: 'safari', pattern: /(?:Version\\W+(\\d+)\\.(\\d+)(?:\\.(\\d+))?)?(?:\\W+Mobile\\S*)?\\W+Safari/ },\n ];\n // Find the FIRST matching browser\n for (const { key, pattern } of browserPatterns) {\n const match = pattern.exec(navigator.userAgent);\n if (match) {\n const major = match[1] || 0;\n const minor = match[2] || 0;\n const patch = match[3] || 0;\n return { browser: key, version: `${major}.${minor}.${patch}` };\n }\n }\n return null;\n}\nconst normalizeArch = (arch) => {\n // Node docs:\n // - https://nodejs.org/api/process.html#processarch\n // Deno docs:\n // - https://doc.deno.land/deno/stable/~/Deno.build\n if (arch === 'x32')\n return 'x32';\n if (arch === 'x86_64' || arch === 'x64')\n return 'x64';\n if (arch === 'arm')\n return 'arm';\n if (arch === 'aarch64' || arch === 'arm64')\n return 'arm64';\n if (arch)\n return `other:${arch}`;\n return 'unknown';\n};\nconst normalizePlatform = (platform) => {\n // Node platforms:\n // - https://nodejs.org/api/process.html#processplatform\n // Deno platforms:\n // - https://doc.deno.land/deno/stable/~/Deno.build\n // - https://github.com/denoland/deno/issues/14799\n platform = platform.toLowerCase();\n // NOTE: this iOS check is untested and may not work\n // Node does not work natively on IOS, there is a fork at\n // https://github.com/nodejs-mobile/nodejs-mobile\n // however it is unknown at the time of writing how to detect if it is running\n if (platform.includes('ios'))\n return 'iOS';\n if (platform === 'android')\n return 'Android';\n if (platform === 'darwin')\n return 'MacOS';\n if (platform === 'win32')\n return 'Windows';\n if (platform === 'freebsd')\n return 'FreeBSD';\n if (platform === 'openbsd')\n return 'OpenBSD';\n if (platform === 'linux')\n return 'Linux';\n if (platform)\n return `Other:${platform}`;\n return 'Unknown';\n};\nlet _platformHeaders;\nexport const getPlatformHeaders = () => {\n return (_platformHeaders ?? (_platformHeaders = getPlatformProperties()));\n};\n//# sourceMappingURL=detect-platform.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nexport function getDefaultFetch() {\n if (typeof fetch !== 'undefined') {\n return fetch;\n }\n throw new Error('`fetch` is not defined as a global; Either pass `fetch` to the client, `new OpenAI({ fetch })` or polyfill the global, `globalThis.fetch = fetch`');\n}\nexport function makeReadableStream(...args) {\n const ReadableStream = globalThis.ReadableStream;\n if (typeof ReadableStream === 'undefined') {\n // Note: All of the platforms / runtimes we officially support already define\n // `ReadableStream` as a global, so this should only ever be hit on unsupported runtimes.\n throw new Error('`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`');\n }\n return new ReadableStream(...args);\n}\nexport function ReadableStreamFrom(iterable) {\n let iter = Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();\n return makeReadableStream({\n start() { },\n async pull(controller) {\n const { done, value } = await iter.next();\n if (done) {\n controller.close();\n }\n else {\n controller.enqueue(value);\n }\n },\n async cancel() {\n await iter.return?.();\n },\n });\n}\n/**\n * Most browsers don't yet have async iterable support for ReadableStream,\n * and Node has a very different way of reading bytes from its \"ReadableStream\".\n *\n * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490\n */\nexport function ReadableStreamToAsyncIterable(stream) {\n if (stream[Symbol.asyncIterator])\n return stream;\n const reader = stream.getReader();\n return {\n async next() {\n try {\n const result = await reader.read();\n if (result?.done)\n reader.releaseLock(); // release lock when stream becomes closed\n return result;\n }\n catch (e) {\n reader.releaseLock(); // release lock when stream becomes errored\n throw e;\n }\n },\n async return() {\n const cancelPromise = reader.cancel();\n reader.releaseLock();\n await cancelPromise;\n return { done: true, value: undefined };\n },\n [Symbol.asyncIterator]() {\n return this;\n },\n };\n}\n/**\n * Cancels a ReadableStream we don't need to consume.\n * See https://undici.nodejs.org/#/?id=garbage-collection\n */\nexport async function CancelReadableStream(stream) {\n if (stream === null || typeof stream !== 'object')\n return;\n if (stream[Symbol.asyncIterator]) {\n await stream[Symbol.asyncIterator]().return?.();\n return;\n }\n const reader = stream.getReader();\n const cancelPromise = reader.cancel();\n reader.releaseLock();\n await cancelPromise;\n}\n//# sourceMappingURL=shims.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nexport const FallbackEncoder = ({ headers, body }) => {\n return {\n bodyHeaders: {\n 'content-type': 'application/json',\n },\n body: JSON.stringify(body),\n };\n};\n//# sourceMappingURL=request-options.mjs.map","export const default_format = 'RFC3986';\nexport const default_formatter = (v) => String(v);\nexport const formatters = {\n RFC1738: (v) => String(v).replace(/%20/g, '+'),\n RFC3986: default_formatter,\n};\nexport const RFC1738 = 'RFC1738';\nexport const RFC3986 = 'RFC3986';\n//# sourceMappingURL=formats.mjs.map","import { RFC1738 } from \"./formats.mjs\";\nimport { isArray } from \"../utils/values.mjs\";\nexport let has = (obj, key) => ((has = Object.hasOwn ?? Function.prototype.call.bind(Object.prototype.hasOwnProperty)),\n has(obj, key));\nconst hex_table = /* @__PURE__ */ (() => {\n const array = [];\n for (let i = 0; i < 256; ++i) {\n array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());\n }\n return array;\n})();\nfunction compact_queue(queue) {\n while (queue.length > 1) {\n const item = queue.pop();\n if (!item)\n continue;\n const obj = item.obj[item.prop];\n if (isArray(obj)) {\n const compacted = [];\n for (let j = 0; j < obj.length; ++j) {\n if (typeof obj[j] !== 'undefined') {\n compacted.push(obj[j]);\n }\n }\n // @ts-ignore\n item.obj[item.prop] = compacted;\n }\n }\n}\nfunction array_to_object(source, options) {\n const obj = options && options.plainObjects ? Object.create(null) : {};\n for (let i = 0; i < source.length; ++i) {\n if (typeof source[i] !== 'undefined') {\n obj[i] = source[i];\n }\n }\n return obj;\n}\nexport function merge(target, source, options = {}) {\n if (!source) {\n return target;\n }\n if (typeof source !== 'object') {\n if (isArray(target)) {\n target.push(source);\n }\n else if (target && typeof target === 'object') {\n if ((options && (options.plainObjects || options.allowPrototypes)) || !has(Object.prototype, source)) {\n target[source] = true;\n }\n }\n else {\n return [target, source];\n }\n return target;\n }\n if (!target || typeof target !== 'object') {\n return [target].concat(source);\n }\n let mergeTarget = target;\n if (isArray(target) && !isArray(source)) {\n // @ts-ignore\n mergeTarget = array_to_object(target, options);\n }\n if (isArray(target) && isArray(source)) {\n source.forEach(function (item, i) {\n if (has(target, i)) {\n const targetItem = target[i];\n if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {\n target[i] = merge(targetItem, item, options);\n }\n else {\n target.push(item);\n }\n }\n else {\n target[i] = item;\n }\n });\n return target;\n }\n return Object.keys(source).reduce(function (acc, key) {\n const value = source[key];\n if (has(acc, key)) {\n acc[key] = merge(acc[key], value, options);\n }\n else {\n acc[key] = value;\n }\n return acc;\n }, mergeTarget);\n}\nexport function assign_single_source(target, source) {\n return Object.keys(source).reduce(function (acc, key) {\n acc[key] = source[key];\n return acc;\n }, target);\n}\nexport function decode(str, _, charset) {\n const strWithoutPlus = str.replace(/\\+/g, ' ');\n if (charset === 'iso-8859-1') {\n // unescape never throws, no try...catch needed:\n return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);\n }\n // utf-8\n try {\n return decodeURIComponent(strWithoutPlus);\n }\n catch (e) {\n return strWithoutPlus;\n }\n}\nconst limit = 1024;\nexport const encode = (str, _defaultEncoder, charset, _kind, format) => {\n // This code was originally written by Brian White for the io.js core querystring library.\n // It has been adapted here for stricter adherence to RFC 3986\n if (str.length === 0) {\n return str;\n }\n let string = str;\n if (typeof str === 'symbol') {\n string = Symbol.prototype.toString.call(str);\n }\n else if (typeof str !== 'string') {\n string = String(str);\n }\n if (charset === 'iso-8859-1') {\n return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {\n return '%26%23' + parseInt($0.slice(2), 16) + '%3B';\n });\n }\n let out = '';\n for (let j = 0; j < string.length; j += limit) {\n const segment = string.length >= limit ? string.slice(j, j + limit) : string;\n const arr = [];\n for (let i = 0; i < segment.length; ++i) {\n let c = segment.charCodeAt(i);\n if (c === 0x2d || // -\n c === 0x2e || // .\n c === 0x5f || // _\n c === 0x7e || // ~\n (c >= 0x30 && c <= 0x39) || // 0-9\n (c >= 0x41 && c <= 0x5a) || // a-z\n (c >= 0x61 && c <= 0x7a) || // A-Z\n (format === RFC1738 && (c === 0x28 || c === 0x29)) // ( )\n ) {\n arr[arr.length] = segment.charAt(i);\n continue;\n }\n if (c < 0x80) {\n arr[arr.length] = hex_table[c];\n continue;\n }\n if (c < 0x800) {\n arr[arr.length] = hex_table[0xc0 | (c >> 6)] + hex_table[0x80 | (c & 0x3f)];\n continue;\n }\n if (c < 0xd800 || c >= 0xe000) {\n arr[arr.length] =\n hex_table[0xe0 | (c >> 12)] + hex_table[0x80 | ((c >> 6) & 0x3f)] + hex_table[0x80 | (c & 0x3f)];\n continue;\n }\n i += 1;\n c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff));\n arr[arr.length] =\n hex_table[0xf0 | (c >> 18)] +\n hex_table[0x80 | ((c >> 12) & 0x3f)] +\n hex_table[0x80 | ((c >> 6) & 0x3f)] +\n hex_table[0x80 | (c & 0x3f)];\n }\n out += arr.join('');\n }\n return out;\n};\nexport function compact(value) {\n const queue = [{ obj: { o: value }, prop: 'o' }];\n const refs = [];\n for (let i = 0; i < queue.length; ++i) {\n const item = queue[i];\n // @ts-ignore\n const obj = item.obj[item.prop];\n const keys = Object.keys(obj);\n for (let j = 0; j < keys.length; ++j) {\n const key = keys[j];\n const val = obj[key];\n if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {\n queue.push({ obj: obj, prop: key });\n refs.push(val);\n }\n }\n }\n compact_queue(queue);\n return value;\n}\nexport function is_regexp(obj) {\n return Object.prototype.toString.call(obj) === '[object RegExp]';\n}\nexport function is_buffer(obj) {\n if (!obj || typeof obj !== 'object') {\n return false;\n }\n return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));\n}\nexport function combine(a, b) {\n return [].concat(a, b);\n}\nexport function maybe_map(val, fn) {\n if (isArray(val)) {\n const mapped = [];\n for (let i = 0; i < val.length; i += 1) {\n mapped.push(fn(val[i]));\n }\n return mapped;\n }\n return fn(val);\n}\n//# sourceMappingURL=utils.mjs.map","import { encode, is_buffer, maybe_map, has } from \"./utils.mjs\";\nimport { default_format, default_formatter, formatters } from \"./formats.mjs\";\nimport { isArray } from \"../utils/values.mjs\";\nconst array_prefix_generators = {\n brackets(prefix) {\n return String(prefix) + '[]';\n },\n comma: 'comma',\n indices(prefix, key) {\n return String(prefix) + '[' + key + ']';\n },\n repeat(prefix) {\n return String(prefix);\n },\n};\nconst push_to_array = function (arr, value_or_array) {\n Array.prototype.push.apply(arr, isArray(value_or_array) ? value_or_array : [value_or_array]);\n};\nlet toISOString;\nconst defaults = {\n addQueryPrefix: false,\n allowDots: false,\n allowEmptyArrays: false,\n arrayFormat: 'indices',\n charset: 'utf-8',\n charsetSentinel: false,\n delimiter: '&',\n encode: true,\n encodeDotInKeys: false,\n encoder: encode,\n encodeValuesOnly: false,\n format: default_format,\n formatter: default_formatter,\n /** @deprecated */\n indices: false,\n serializeDate(date) {\n return (toISOString ?? (toISOString = Function.prototype.call.bind(Date.prototype.toISOString)))(date);\n },\n skipNulls: false,\n strictNullHandling: false,\n};\nfunction is_non_nullish_primitive(v) {\n return (typeof v === 'string' ||\n typeof v === 'number' ||\n typeof v === 'boolean' ||\n typeof v === 'symbol' ||\n typeof v === 'bigint');\n}\nconst sentinel = {};\nfunction inner_stringify(object, prefix, generateArrayPrefix, commaRoundTrip, allowEmptyArrays, strictNullHandling, skipNulls, encodeDotInKeys, encoder, filter, sort, allowDots, serializeDate, format, formatter, encodeValuesOnly, charset, sideChannel) {\n let obj = object;\n let tmp_sc = sideChannel;\n let step = 0;\n let find_flag = false;\n while ((tmp_sc = tmp_sc.get(sentinel)) !== void undefined && !find_flag) {\n // Where object last appeared in the ref tree\n const pos = tmp_sc.get(object);\n step += 1;\n if (typeof pos !== 'undefined') {\n if (pos === step) {\n throw new RangeError('Cyclic object value');\n }\n else {\n find_flag = true; // Break while\n }\n }\n if (typeof tmp_sc.get(sentinel) === 'undefined') {\n step = 0;\n }\n }\n if (typeof filter === 'function') {\n obj = filter(prefix, obj);\n }\n else if (obj instanceof Date) {\n obj = serializeDate?.(obj);\n }\n else if (generateArrayPrefix === 'comma' && isArray(obj)) {\n obj = maybe_map(obj, function (value) {\n if (value instanceof Date) {\n return serializeDate?.(value);\n }\n return value;\n });\n }\n if (obj === null) {\n if (strictNullHandling) {\n return encoder && !encodeValuesOnly ?\n // @ts-expect-error\n encoder(prefix, defaults.encoder, charset, 'key', format)\n : prefix;\n }\n obj = '';\n }\n if (is_non_nullish_primitive(obj) || is_buffer(obj)) {\n if (encoder) {\n const key_value = encodeValuesOnly ? prefix\n // @ts-expect-error\n : encoder(prefix, defaults.encoder, charset, 'key', format);\n return [\n formatter?.(key_value) +\n '=' +\n // @ts-expect-error\n formatter?.(encoder(obj, defaults.encoder, charset, 'value', format)),\n ];\n }\n return [formatter?.(prefix) + '=' + formatter?.(String(obj))];\n }\n const values = [];\n if (typeof obj === 'undefined') {\n return values;\n }\n let obj_keys;\n if (generateArrayPrefix === 'comma' && isArray(obj)) {\n // we need to join elements in\n if (encodeValuesOnly && encoder) {\n // @ts-expect-error values only\n obj = maybe_map(obj, encoder);\n }\n obj_keys = [{ value: obj.length > 0 ? obj.join(',') || null : void undefined }];\n }\n else if (isArray(filter)) {\n obj_keys = filter;\n }\n else {\n const keys = Object.keys(obj);\n obj_keys = sort ? keys.sort(sort) : keys;\n }\n const encoded_prefix = encodeDotInKeys ? String(prefix).replace(/\\./g, '%2E') : String(prefix);\n const adjusted_prefix = commaRoundTrip && isArray(obj) && obj.length === 1 ? encoded_prefix + '[]' : encoded_prefix;\n if (allowEmptyArrays && isArray(obj) && obj.length === 0) {\n return adjusted_prefix + '[]';\n }\n for (let j = 0; j < obj_keys.length; ++j) {\n const key = obj_keys[j];\n const value = \n // @ts-ignore\n typeof key === 'object' && typeof key.value !== 'undefined' ? key.value : obj[key];\n if (skipNulls && value === null) {\n continue;\n }\n // @ts-ignore\n const encoded_key = allowDots && encodeDotInKeys ? key.replace(/\\./g, '%2E') : key;\n const key_prefix = isArray(obj) ?\n typeof generateArrayPrefix === 'function' ?\n generateArrayPrefix(adjusted_prefix, encoded_key)\n : adjusted_prefix\n : adjusted_prefix + (allowDots ? '.' + encoded_key : '[' + encoded_key + ']');\n sideChannel.set(object, step);\n const valueSideChannel = new WeakMap();\n valueSideChannel.set(sentinel, sideChannel);\n push_to_array(values, inner_stringify(value, key_prefix, generateArrayPrefix, commaRoundTrip, allowEmptyArrays, strictNullHandling, skipNulls, encodeDotInKeys, \n // @ts-ignore\n generateArrayPrefix === 'comma' && encodeValuesOnly && isArray(obj) ? null : encoder, filter, sort, allowDots, serializeDate, format, formatter, encodeValuesOnly, charset, valueSideChannel));\n }\n return values;\n}\nfunction normalize_stringify_options(opts = defaults) {\n if (typeof opts.allowEmptyArrays !== 'undefined' && typeof opts.allowEmptyArrays !== 'boolean') {\n throw new TypeError('`allowEmptyArrays` option can only be `true` or `false`, when provided');\n }\n if (typeof opts.encodeDotInKeys !== 'undefined' && typeof opts.encodeDotInKeys !== 'boolean') {\n throw new TypeError('`encodeDotInKeys` option can only be `true` or `false`, when provided');\n }\n if (opts.encoder !== null && typeof opts.encoder !== 'undefined' && typeof opts.encoder !== 'function') {\n throw new TypeError('Encoder has to be a function.');\n }\n const charset = opts.charset || defaults.charset;\n if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {\n throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');\n }\n let format = default_format;\n if (typeof opts.format !== 'undefined') {\n if (!has(formatters, opts.format)) {\n throw new TypeError('Unknown format option provided.');\n }\n format = opts.format;\n }\n const formatter = formatters[format];\n let filter = defaults.filter;\n if (typeof opts.filter === 'function' || isArray(opts.filter)) {\n filter = opts.filter;\n }\n let arrayFormat;\n if (opts.arrayFormat && opts.arrayFormat in array_prefix_generators) {\n arrayFormat = opts.arrayFormat;\n }\n else if ('indices' in opts) {\n arrayFormat = opts.indices ? 'indices' : 'repeat';\n }\n else {\n arrayFormat = defaults.arrayFormat;\n }\n if ('commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {\n throw new TypeError('`commaRoundTrip` must be a boolean, or absent');\n }\n const allowDots = typeof opts.allowDots === 'undefined' ?\n !!opts.encodeDotInKeys === true ?\n true\n : defaults.allowDots\n : !!opts.allowDots;\n return {\n addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,\n // @ts-ignore\n allowDots: allowDots,\n allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,\n arrayFormat: arrayFormat,\n charset: charset,\n charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,\n commaRoundTrip: !!opts.commaRoundTrip,\n delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter,\n encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode,\n encodeDotInKeys: typeof opts.encodeDotInKeys === 'boolean' ? opts.encodeDotInKeys : defaults.encodeDotInKeys,\n encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder,\n encodeValuesOnly: typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly,\n filter: filter,\n format: format,\n formatter: formatter,\n serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,\n skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,\n // @ts-ignore\n sort: typeof opts.sort === 'function' ? opts.sort : null,\n strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling,\n };\n}\nexport function stringify(object, opts = {}) {\n let obj = object;\n const options = normalize_stringify_options(opts);\n let obj_keys;\n let filter;\n if (typeof options.filter === 'function') {\n filter = options.filter;\n obj = filter('', obj);\n }\n else if (isArray(options.filter)) {\n filter = options.filter;\n obj_keys = filter;\n }\n const keys = [];\n if (typeof obj !== 'object' || obj === null) {\n return '';\n }\n const generateArrayPrefix = array_prefix_generators[options.arrayFormat];\n const commaRoundTrip = generateArrayPrefix === 'comma' && options.commaRoundTrip;\n if (!obj_keys) {\n obj_keys = Object.keys(obj);\n }\n if (options.sort) {\n obj_keys.sort(options.sort);\n }\n const sideChannel = new WeakMap();\n for (let i = 0; i < obj_keys.length; ++i) {\n const key = obj_keys[i];\n if (options.skipNulls && obj[key] === null) {\n continue;\n }\n push_to_array(keys, inner_stringify(obj[key], key, \n // @ts-expect-error\n generateArrayPrefix, commaRoundTrip, options.allowEmptyArrays, options.strictNullHandling, options.skipNulls, options.encodeDotInKeys, options.encode ? options.encoder : null, options.filter, options.sort, options.allowDots, options.serializeDate, options.format, options.formatter, options.encodeValuesOnly, options.charset, sideChannel));\n }\n const joined = keys.join(options.delimiter);\n let prefix = options.addQueryPrefix === true ? '?' : '';\n if (options.charsetSentinel) {\n if (options.charset === 'iso-8859-1') {\n // encodeURIComponent('✓'), the \"numeric entity\" representation of a checkmark\n prefix += 'utf8=%26%2310003%3B&';\n }\n else {\n // encodeURIComponent('✓')\n prefix += 'utf8=%E2%9C%93&';\n }\n }\n return joined.length > 0 ? prefix + joined : '';\n}\n//# sourceMappingURL=stringify.mjs.map","export function concatBytes(buffers) {\n let length = 0;\n for (const buffer of buffers) {\n length += buffer.length;\n }\n const output = new Uint8Array(length);\n let index = 0;\n for (const buffer of buffers) {\n output.set(buffer, index);\n index += buffer.length;\n }\n return output;\n}\nlet encodeUTF8_;\nexport function encodeUTF8(str) {\n let encoder;\n return (encodeUTF8_ ??\n ((encoder = new globalThis.TextEncoder()), (encodeUTF8_ = encoder.encode.bind(encoder))))(str);\n}\nlet decodeUTF8_;\nexport function decodeUTF8(bytes) {\n let decoder;\n return (decodeUTF8_ ??\n ((decoder = new globalThis.TextDecoder()), (decodeUTF8_ = decoder.decode.bind(decoder))))(bytes);\n}\n//# sourceMappingURL=bytes.mjs.map","var _LineDecoder_buffer, _LineDecoder_carriageReturnIndex;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"../tslib.mjs\";\nimport { concatBytes, decodeUTF8, encodeUTF8 } from \"../utils/bytes.mjs\";\n/**\n * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally\n * reading lines from text.\n *\n * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258\n */\nexport class LineDecoder {\n constructor() {\n _LineDecoder_buffer.set(this, void 0);\n _LineDecoder_carriageReturnIndex.set(this, void 0);\n __classPrivateFieldSet(this, _LineDecoder_buffer, new Uint8Array(), \"f\");\n __classPrivateFieldSet(this, _LineDecoder_carriageReturnIndex, null, \"f\");\n }\n decode(chunk) {\n if (chunk == null) {\n return [];\n }\n const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk)\n : typeof chunk === 'string' ? encodeUTF8(chunk)\n : chunk;\n __classPrivateFieldSet(this, _LineDecoder_buffer, concatBytes([__classPrivateFieldGet(this, _LineDecoder_buffer, \"f\"), binaryChunk]), \"f\");\n const lines = [];\n let patternIndex;\n while ((patternIndex = findNewlineIndex(__classPrivateFieldGet(this, _LineDecoder_buffer, \"f\"), __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, \"f\"))) != null) {\n if (patternIndex.carriage && __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, \"f\") == null) {\n // skip until we either get a corresponding `\\n`, a new `\\r` or nothing\n __classPrivateFieldSet(this, _LineDecoder_carriageReturnIndex, patternIndex.index, \"f\");\n continue;\n }\n // we got double \\r or \\rtext\\n\n if (__classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, \"f\") != null &&\n (patternIndex.index !== __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, \"f\") + 1 || patternIndex.carriage)) {\n lines.push(decodeUTF8(__classPrivateFieldGet(this, _LineDecoder_buffer, \"f\").subarray(0, __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, \"f\") - 1)));\n __classPrivateFieldSet(this, _LineDecoder_buffer, __classPrivateFieldGet(this, _LineDecoder_buffer, \"f\").subarray(__classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, \"f\")), \"f\");\n __classPrivateFieldSet(this, _LineDecoder_carriageReturnIndex, null, \"f\");\n continue;\n }\n const endIndex = __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, \"f\") !== null ? patternIndex.preceding - 1 : patternIndex.preceding;\n const line = decodeUTF8(__classPrivateFieldGet(this, _LineDecoder_buffer, \"f\").subarray(0, endIndex));\n lines.push(line);\n __classPrivateFieldSet(this, _LineDecoder_buffer, __classPrivateFieldGet(this, _LineDecoder_buffer, \"f\").subarray(patternIndex.index), \"f\");\n __classPrivateFieldSet(this, _LineDecoder_carriageReturnIndex, null, \"f\");\n }\n return lines;\n }\n flush() {\n if (!__classPrivateFieldGet(this, _LineDecoder_buffer, \"f\").length) {\n return [];\n }\n return this.decode('\\n');\n }\n}\n_LineDecoder_buffer = new WeakMap(), _LineDecoder_carriageReturnIndex = new WeakMap();\n// prettier-ignore\nLineDecoder.NEWLINE_CHARS = new Set(['\\n', '\\r']);\nLineDecoder.NEWLINE_REGEXP = /\\r\\n|[\\n\\r]/g;\n/**\n * This function searches the buffer for the end patterns, (\\r or \\n)\n * and returns an object with the index preceding the matched newline and the\n * index after the newline char. `null` is returned if no new line is found.\n *\n * ```ts\n * findNewLineIndex('abc\\ndef') -> { preceding: 2, index: 3 }\n * ```\n */\nfunction findNewlineIndex(buffer, startIndex) {\n const newline = 0x0a; // \\n\n const carriage = 0x0d; // \\r\n for (let i = startIndex ?? 0; i < buffer.length; i++) {\n if (buffer[i] === newline) {\n return { preceding: i, index: i + 1, carriage: false };\n }\n if (buffer[i] === carriage) {\n return { preceding: i, index: i + 1, carriage: true };\n }\n }\n return null;\n}\nexport function findDoubleNewlineIndex(buffer) {\n // This function searches the buffer for the end patterns (\\r\\r, \\n\\n, \\r\\n\\r\\n)\n // and returns the index right after the first occurrence of any pattern,\n // or -1 if none of the patterns are found.\n const newline = 0x0a; // \\n\n const carriage = 0x0d; // \\r\n for (let i = 0; i < buffer.length - 1; i++) {\n if (buffer[i] === newline && buffer[i + 1] === newline) {\n // \\n\\n\n return i + 2;\n }\n if (buffer[i] === carriage && buffer[i + 1] === carriage) {\n // \\r\\r\n return i + 2;\n }\n if (buffer[i] === carriage &&\n buffer[i + 1] === newline &&\n i + 3 < buffer.length &&\n buffer[i + 2] === carriage &&\n buffer[i + 3] === newline) {\n // \\r\\n\\r\\n\n return i + 4;\n }\n }\n return -1;\n}\n//# sourceMappingURL=line.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { hasOwn } from \"./values.mjs\";\nconst levelNumbers = {\n off: 0,\n error: 200,\n warn: 300,\n info: 400,\n debug: 500,\n};\nexport const parseLogLevel = (maybeLevel, sourceName, client) => {\n if (!maybeLevel) {\n return undefined;\n }\n if (hasOwn(levelNumbers, maybeLevel)) {\n return maybeLevel;\n }\n loggerFor(client).warn(`${sourceName} was set to ${JSON.stringify(maybeLevel)}, expected one of ${JSON.stringify(Object.keys(levelNumbers))}`);\n return undefined;\n};\nfunction noop() { }\nfunction makeLogFn(fnLevel, logger, logLevel) {\n if (!logger || levelNumbers[fnLevel] > levelNumbers[logLevel]) {\n return noop;\n }\n else {\n // Don't wrap logger functions, we want the stacktrace intact!\n return logger[fnLevel].bind(logger);\n }\n}\nconst noopLogger = {\n error: noop,\n warn: noop,\n info: noop,\n debug: noop,\n};\nlet cachedLoggers = /* @__PURE__ */ new WeakMap();\nexport function loggerFor(client) {\n const logger = client.logger;\n const logLevel = client.logLevel ?? 'off';\n if (!logger) {\n return noopLogger;\n }\n const cachedLogger = cachedLoggers.get(logger);\n if (cachedLogger && cachedLogger[0] === logLevel) {\n return cachedLogger[1];\n }\n const levelLogger = {\n error: makeLogFn('error', logger, logLevel),\n warn: makeLogFn('warn', logger, logLevel),\n info: makeLogFn('info', logger, logLevel),\n debug: makeLogFn('debug', logger, logLevel),\n };\n cachedLoggers.set(logger, [logLevel, levelLogger]);\n return levelLogger;\n}\nexport const formatRequestDetails = (details) => {\n if (details.options) {\n details.options = { ...details.options };\n delete details.options['headers']; // redundant + leaks internals\n }\n if (details.headers) {\n details.headers = Object.fromEntries((details.headers instanceof Headers ? [...details.headers] : Object.entries(details.headers)).map(([name, value]) => [\n name,\n (name.toLowerCase() === 'authorization' ||\n name.toLowerCase() === 'cookie' ||\n name.toLowerCase() === 'set-cookie') ?\n '***'\n : value,\n ]));\n }\n if ('retryOfRequestLogID' in details) {\n if (details.retryOfRequestLogID) {\n details.retryOf = details.retryOfRequestLogID;\n }\n delete details.retryOfRequestLogID;\n }\n return details;\n};\n//# sourceMappingURL=log.mjs.map","var _Stream_client;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"../internal/tslib.mjs\";\nimport { OpenAIError } from \"./error.mjs\";\nimport { makeReadableStream } from \"../internal/shims.mjs\";\nimport { findDoubleNewlineIndex, LineDecoder } from \"../internal/decoders/line.mjs\";\nimport { ReadableStreamToAsyncIterable } from \"../internal/shims.mjs\";\nimport { isAbortError } from \"../internal/errors.mjs\";\nimport { encodeUTF8 } from \"../internal/utils/bytes.mjs\";\nimport { loggerFor } from \"../internal/utils/log.mjs\";\nimport { APIError } from \"./error.mjs\";\nexport class Stream {\n constructor(iterator, controller, client) {\n this.iterator = iterator;\n _Stream_client.set(this, void 0);\n this.controller = controller;\n __classPrivateFieldSet(this, _Stream_client, client, \"f\");\n }\n static fromSSEResponse(response, controller, client) {\n let consumed = false;\n const logger = client ? loggerFor(client) : console;\n async function* iterator() {\n if (consumed) {\n throw new OpenAIError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');\n }\n consumed = true;\n let done = false;\n try {\n for await (const sse of _iterSSEMessages(response, controller)) {\n if (done)\n continue;\n if (sse.data.startsWith('[DONE]')) {\n done = true;\n continue;\n }\n if (sse.event === null || !sse.event.startsWith('thread.')) {\n let data;\n try {\n data = JSON.parse(sse.data);\n }\n catch (e) {\n logger.error(`Could not parse message into JSON:`, sse.data);\n logger.error(`From chunk:`, sse.raw);\n throw e;\n }\n if (data && data.error) {\n throw new APIError(undefined, data.error, undefined, response.headers);\n }\n yield data;\n }\n else {\n let data;\n try {\n data = JSON.parse(sse.data);\n }\n catch (e) {\n console.error(`Could not parse message into JSON:`, sse.data);\n console.error(`From chunk:`, sse.raw);\n throw e;\n }\n // TODO: Is this where the error should be thrown?\n if (sse.event == 'error') {\n throw new APIError(undefined, data.error, data.message, undefined);\n }\n yield { event: sse.event, data: data };\n }\n }\n done = true;\n }\n catch (e) {\n // If the user calls `stream.controller.abort()`, we should exit without throwing.\n if (isAbortError(e))\n return;\n throw e;\n }\n finally {\n // If the user `break`s, abort the ongoing request.\n if (!done)\n controller.abort();\n }\n }\n return new Stream(iterator, controller, client);\n }\n /**\n * Generates a Stream from a newline-separated ReadableStream\n * where each item is a JSON value.\n */\n static fromReadableStream(readableStream, controller, client) {\n let consumed = false;\n async function* iterLines() {\n const lineDecoder = new LineDecoder();\n const iter = ReadableStreamToAsyncIterable(readableStream);\n for await (const chunk of iter) {\n for (const line of lineDecoder.decode(chunk)) {\n yield line;\n }\n }\n for (const line of lineDecoder.flush()) {\n yield line;\n }\n }\n async function* iterator() {\n if (consumed) {\n throw new OpenAIError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');\n }\n consumed = true;\n let done = false;\n try {\n for await (const line of iterLines()) {\n if (done)\n continue;\n if (line)\n yield JSON.parse(line);\n }\n done = true;\n }\n catch (e) {\n // If the user calls `stream.controller.abort()`, we should exit without throwing.\n if (isAbortError(e))\n return;\n throw e;\n }\n finally {\n // If the user `break`s, abort the ongoing request.\n if (!done)\n controller.abort();\n }\n }\n return new Stream(iterator, controller, client);\n }\n [(_Stream_client = new WeakMap(), Symbol.asyncIterator)]() {\n return this.iterator();\n }\n /**\n * Splits the stream into two streams which can be\n * independently read from at different speeds.\n */\n tee() {\n const left = [];\n const right = [];\n const iterator = this.iterator();\n const teeIterator = (queue) => {\n return {\n next: () => {\n if (queue.length === 0) {\n const result = iterator.next();\n left.push(result);\n right.push(result);\n }\n return queue.shift();\n },\n };\n };\n return [\n new Stream(() => teeIterator(left), this.controller, __classPrivateFieldGet(this, _Stream_client, \"f\")),\n new Stream(() => teeIterator(right), this.controller, __classPrivateFieldGet(this, _Stream_client, \"f\")),\n ];\n }\n /**\n * Converts this stream to a newline-separated ReadableStream of\n * JSON stringified values in the stream\n * which can be turned back into a Stream with `Stream.fromReadableStream()`.\n */\n toReadableStream() {\n const self = this;\n let iter;\n return makeReadableStream({\n async start() {\n iter = self[Symbol.asyncIterator]();\n },\n async pull(ctrl) {\n try {\n const { value, done } = await iter.next();\n if (done)\n return ctrl.close();\n const bytes = encodeUTF8(JSON.stringify(value) + '\\n');\n ctrl.enqueue(bytes);\n }\n catch (err) {\n ctrl.error(err);\n }\n },\n async cancel() {\n await iter.return?.();\n },\n });\n }\n}\nexport async function* _iterSSEMessages(response, controller) {\n if (!response.body) {\n controller.abort();\n if (typeof globalThis.navigator !== 'undefined' &&\n globalThis.navigator.product === 'ReactNative') {\n throw new OpenAIError(`The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`);\n }\n throw new OpenAIError(`Attempted to iterate over a response with no body`);\n }\n const sseDecoder = new SSEDecoder();\n const lineDecoder = new LineDecoder();\n const iter = ReadableStreamToAsyncIterable(response.body);\n for await (const sseChunk of iterSSEChunks(iter)) {\n for (const line of lineDecoder.decode(sseChunk)) {\n const sse = sseDecoder.decode(line);\n if (sse)\n yield sse;\n }\n }\n for (const line of lineDecoder.flush()) {\n const sse = sseDecoder.decode(line);\n if (sse)\n yield sse;\n }\n}\n/**\n * Given an async iterable iterator, iterates over it and yields full\n * SSE chunks, i.e. yields when a double new-line is encountered.\n */\nasync function* iterSSEChunks(iterator) {\n let data = new Uint8Array();\n for await (const chunk of iterator) {\n if (chunk == null) {\n continue;\n }\n const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk)\n : typeof chunk === 'string' ? encodeUTF8(chunk)\n : chunk;\n let newData = new Uint8Array(data.length + binaryChunk.length);\n newData.set(data);\n newData.set(binaryChunk, data.length);\n data = newData;\n let patternIndex;\n while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {\n yield data.slice(0, patternIndex);\n data = data.slice(patternIndex);\n }\n }\n if (data.length > 0) {\n yield data;\n }\n}\nclass SSEDecoder {\n constructor() {\n this.event = null;\n this.data = [];\n this.chunks = [];\n }\n decode(line) {\n if (line.endsWith('\\r')) {\n line = line.substring(0, line.length - 1);\n }\n if (!line) {\n // empty line and we didn't previously encounter any messages\n if (!this.event && !this.data.length)\n return null;\n const sse = {\n event: this.event,\n data: this.data.join('\\n'),\n raw: this.chunks,\n };\n this.event = null;\n this.data = [];\n this.chunks = [];\n return sse;\n }\n this.chunks.push(line);\n if (line.startsWith(':')) {\n return null;\n }\n let [fieldname, _, value] = partition(line, ':');\n if (value.startsWith(' ')) {\n value = value.substring(1);\n }\n if (fieldname === 'event') {\n this.event = value;\n }\n else if (fieldname === 'data') {\n this.data.push(value);\n }\n return null;\n }\n}\nfunction partition(str, delimiter) {\n const index = str.indexOf(delimiter);\n if (index !== -1) {\n return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];\n }\n return [str, '', ''];\n}\n//# sourceMappingURL=streaming.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { Stream } from \"../core/streaming.mjs\";\nimport { formatRequestDetails, loggerFor } from \"./utils/log.mjs\";\nexport async function defaultParseResponse(client, props) {\n const { response, requestLogID, retryOfRequestLogID, startTime } = props;\n const body = await (async () => {\n if (props.options.stream) {\n loggerFor(client).debug('response', response.status, response.url, response.headers, response.body);\n // Note: there is an invariant here that isn't represented in the type system\n // that if you set `stream: true` the response type must also be `Stream<T>`\n if (props.options.__streamClass) {\n return props.options.__streamClass.fromSSEResponse(response, props.controller, client);\n }\n return Stream.fromSSEResponse(response, props.controller, client);\n }\n // fetch refuses to read the body when the status code is 204.\n if (response.status === 204) {\n return null;\n }\n if (props.options.__binaryResponse) {\n return response;\n }\n const contentType = response.headers.get('content-type');\n const mediaType = contentType?.split(';')[0]?.trim();\n const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json');\n if (isJSON) {\n const json = await response.json();\n return addRequestID(json, response);\n }\n const text = await response.text();\n return text;\n })();\n loggerFor(client).debug(`[${requestLogID}] response parsed`, formatRequestDetails({\n retryOfRequestLogID,\n url: response.url,\n status: response.status,\n body,\n durationMs: Date.now() - startTime,\n }));\n return body;\n}\nexport function addRequestID(value, response) {\n if (!value || typeof value !== 'object' || Array.isArray(value)) {\n return value;\n }\n return Object.defineProperty(value, '_request_id', {\n value: response.headers.get('x-request-id'),\n enumerable: false,\n });\n}\n//# sourceMappingURL=parse.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nvar _APIPromise_client;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"../internal/tslib.mjs\";\nimport { defaultParseResponse, addRequestID, } from \"../internal/parse.mjs\";\n/**\n * A subclass of `Promise` providing additional helper methods\n * for interacting with the SDK.\n */\nexport class APIPromise extends Promise {\n constructor(client, responsePromise, parseResponse = defaultParseResponse) {\n super((resolve) => {\n // this is maybe a bit weird but this has to be a no-op to not implicitly\n // parse the response body; instead .then, .catch, .finally are overridden\n // to parse the response\n resolve(null);\n });\n this.responsePromise = responsePromise;\n this.parseResponse = parseResponse;\n _APIPromise_client.set(this, void 0);\n __classPrivateFieldSet(this, _APIPromise_client, client, \"f\");\n }\n _thenUnwrap(transform) {\n return new APIPromise(__classPrivateFieldGet(this, _APIPromise_client, \"f\"), this.responsePromise, async (client, props) => addRequestID(transform(await this.parseResponse(client, props), props), props.response));\n }\n /**\n * Gets the raw `Response` instance instead of parsing the response\n * data.\n *\n * If you want to parse the response body but still get the `Response`\n * instance, you can use {@link withResponse()}.\n *\n * 👋 Getting the wrong TypeScript type for `Response`?\n * Try setting `\"moduleResolution\": \"NodeNext\"` or add `\"lib\": [\"DOM\"]`\n * to your `tsconfig.json`.\n */\n asResponse() {\n return this.responsePromise.then((p) => p.response);\n }\n /**\n * Gets the parsed response data, the raw `Response` instance and the ID of the request,\n * returned via the X-Request-ID header which is useful for debugging requests and reporting\n * issues to OpenAI.\n *\n * If you just want to get the raw `Response` instance without parsing it,\n * you can use {@link asResponse()}.\n *\n * 👋 Getting the wrong TypeScript type for `Response`?\n * Try setting `\"moduleResolution\": \"NodeNext\"` or add `\"lib\": [\"DOM\"]`\n * to your `tsconfig.json`.\n */\n async withResponse() {\n const [data, response] = await Promise.all([this.parse(), this.asResponse()]);\n return { data, response, request_id: response.headers.get('x-request-id') };\n }\n parse() {\n if (!this.parsedPromise) {\n this.parsedPromise = this.responsePromise.then((data) => this.parseResponse(__classPrivateFieldGet(this, _APIPromise_client, \"f\"), data));\n }\n return this.parsedPromise;\n }\n then(onfulfilled, onrejected) {\n return this.parse().then(onfulfilled, onrejected);\n }\n catch(onrejected) {\n return this.parse().catch(onrejected);\n }\n finally(onfinally) {\n return this.parse().finally(onfinally);\n }\n}\n_APIPromise_client = new WeakMap();\n//# sourceMappingURL=api-promise.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nvar _AbstractPage_client;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"../internal/tslib.mjs\";\nimport { OpenAIError } from \"./error.mjs\";\nimport { defaultParseResponse } from \"../internal/parse.mjs\";\nimport { APIPromise } from \"./api-promise.mjs\";\nimport { maybeObj } from \"../internal/utils/values.mjs\";\nexport class AbstractPage {\n constructor(client, response, body, options) {\n _AbstractPage_client.set(this, void 0);\n __classPrivateFieldSet(this, _AbstractPage_client, client, \"f\");\n this.options = options;\n this.response = response;\n this.body = body;\n }\n hasNextPage() {\n const items = this.getPaginatedItems();\n if (!items.length)\n return false;\n return this.nextPageRequestOptions() != null;\n }\n async getNextPage() {\n const nextOptions = this.nextPageRequestOptions();\n if (!nextOptions) {\n throw new OpenAIError('No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.');\n }\n return await __classPrivateFieldGet(this, _AbstractPage_client, \"f\").requestAPIList(this.constructor, nextOptions);\n }\n async *iterPages() {\n let page = this;\n yield page;\n while (page.hasNextPage()) {\n page = await page.getNextPage();\n yield page;\n }\n }\n async *[(_AbstractPage_client = new WeakMap(), Symbol.asyncIterator)]() {\n for await (const page of this.iterPages()) {\n for (const item of page.getPaginatedItems()) {\n yield item;\n }\n }\n }\n}\n/**\n * This subclass of Promise will resolve to an instantiated Page once the request completes.\n *\n * It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:\n *\n * for await (const item of client.items.list()) {\n * console.log(item)\n * }\n */\nexport class PagePromise extends APIPromise {\n constructor(client, request, Page) {\n super(client, request, async (client, props) => new Page(client, props.response, await defaultParseResponse(client, props), props.options));\n }\n /**\n * Allow auto-paginating iteration on an unawaited list call, eg:\n *\n * for await (const item of client.items.list()) {\n * console.log(item)\n * }\n */\n async *[Symbol.asyncIterator]() {\n const page = await this;\n for await (const item of page) {\n yield item;\n }\n }\n}\n/**\n * Note: no pagination actually occurs yet, this is for forwards-compatibility.\n */\nexport class Page extends AbstractPage {\n constructor(client, response, body, options) {\n super(client, response, body, options);\n this.data = body.data || [];\n this.object = body.object;\n }\n getPaginatedItems() {\n return this.data ?? [];\n }\n nextPageRequestOptions() {\n return null;\n }\n}\nexport class CursorPage extends AbstractPage {\n constructor(client, response, body, options) {\n super(client, response, body, options);\n this.data = body.data || [];\n this.has_more = body.has_more || false;\n }\n getPaginatedItems() {\n return this.data ?? [];\n }\n hasNextPage() {\n if (this.has_more === false) {\n return false;\n }\n return super.hasNextPage();\n }\n nextPageRequestOptions() {\n const data = this.getPaginatedItems();\n const id = data[data.length - 1]?.id;\n if (!id) {\n return null;\n }\n return {\n ...this.options,\n query: {\n ...maybeObj(this.options.query),\n after: id,\n },\n };\n }\n}\nexport class ConversationCursorPage extends AbstractPage {\n constructor(client, response, body, options) {\n super(client, response, body, options);\n this.data = body.data || [];\n this.has_more = body.has_more || false;\n this.last_id = body.last_id || '';\n }\n getPaginatedItems() {\n return this.data ?? [];\n }\n hasNextPage() {\n if (this.has_more === false) {\n return false;\n }\n return super.hasNextPage();\n }\n nextPageRequestOptions() {\n const cursor = this.last_id;\n if (!cursor) {\n return null;\n }\n return {\n ...this.options,\n query: {\n ...maybeObj(this.options.query),\n after: cursor,\n },\n };\n }\n}\n//# sourceMappingURL=pagination.mjs.map","import { ReadableStreamFrom } from \"./shims.mjs\";\nexport const checkFileSupport = () => {\n if (typeof File === 'undefined') {\n const { process } = globalThis;\n const isOldNode = typeof process?.versions?.node === 'string' && parseInt(process.versions.node.split('.')) < 20;\n throw new Error('`File` is not defined as a global, which is required for file uploads.' +\n (isOldNode ?\n \" Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`.\"\n : ''));\n }\n};\n/**\n * Construct a `File` instance. This is used to ensure a helpful error is thrown\n * for environments that don't define a global `File` yet.\n */\nexport function makeFile(fileBits, fileName, options) {\n checkFileSupport();\n return new File(fileBits, fileName ?? 'unknown_file', options);\n}\nexport function getName(value) {\n return (((typeof value === 'object' &&\n value !== null &&\n (('name' in value && value.name && String(value.name)) ||\n ('url' in value && value.url && String(value.url)) ||\n ('filename' in value && value.filename && String(value.filename)) ||\n ('path' in value && value.path && String(value.path)))) ||\n '')\n .split(/[\\\\/]/)\n .pop() || undefined);\n}\nexport const isAsyncIterable = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';\n/**\n * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.\n * Otherwise returns the request as is.\n */\nexport const maybeMultipartFormRequestOptions = async (opts, fetch) => {\n if (!hasUploadableValue(opts.body))\n return opts;\n return { ...opts, body: await createForm(opts.body, fetch) };\n};\nexport const multipartFormRequestOptions = async (opts, fetch) => {\n return { ...opts, body: await createForm(opts.body, fetch) };\n};\nconst supportsFormDataMap = /* @__PURE__ */ new WeakMap();\n/**\n * node-fetch doesn't support the global FormData object in recent node versions. Instead of sending\n * properly-encoded form data, it just stringifies the object, resulting in a request body of \"[object FormData]\".\n * This function detects if the fetch function provided supports the global FormData object to avoid\n * confusing error messages later on.\n */\nfunction supportsFormData(fetchObject) {\n const fetch = typeof fetchObject === 'function' ? fetchObject : fetchObject.fetch;\n const cached = supportsFormDataMap.get(fetch);\n if (cached)\n return cached;\n const promise = (async () => {\n try {\n const FetchResponse = ('Response' in fetch ?\n fetch.Response\n : (await fetch('data:,')).constructor);\n const data = new FormData();\n if (data.toString() === (await new FetchResponse(data).text())) {\n return false;\n }\n return true;\n }\n catch {\n // avoid false negatives\n return true;\n }\n })();\n supportsFormDataMap.set(fetch, promise);\n return promise;\n}\nexport const createForm = async (body, fetch) => {\n if (!(await supportsFormData(fetch))) {\n throw new TypeError('The provided fetch function does not support file uploads with the current global FormData class.');\n }\n const form = new FormData();\n await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));\n return form;\n};\n// We check for Blob not File because Bun.File doesn't inherit from File,\n// but they both inherit from Blob and have a `name` property at runtime.\nconst isNamedBlob = (value) => value instanceof Blob && 'name' in value;\nconst isUploadable = (value) => typeof value === 'object' &&\n value !== null &&\n (value instanceof Response || isAsyncIterable(value) || isNamedBlob(value));\nconst hasUploadableValue = (value) => {\n if (isUploadable(value))\n return true;\n if (Array.isArray(value))\n return value.some(hasUploadableValue);\n if (value && typeof value === 'object') {\n for (const k in value) {\n if (hasUploadableValue(value[k]))\n return true;\n }\n }\n return false;\n};\nconst addFormValue = async (form, key, value) => {\n if (value === undefined)\n return;\n if (value == null) {\n throw new TypeError(`Received null for \"${key}\"; to pass null in FormData, you must use the string 'null'`);\n }\n // TODO: make nested formats configurable\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {\n form.append(key, String(value));\n }\n else if (value instanceof Response) {\n form.append(key, makeFile([await value.blob()], getName(value)));\n }\n else if (isAsyncIterable(value)) {\n form.append(key, makeFile([await new Response(ReadableStreamFrom(value)).blob()], getName(value)));\n }\n else if (isNamedBlob(value)) {\n form.append(key, value, getName(value));\n }\n else if (Array.isArray(value)) {\n await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));\n }\n else if (typeof value === 'object') {\n await Promise.all(Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)));\n }\n else {\n throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`);\n }\n};\n//# sourceMappingURL=uploads.mjs.map","import { getName, makeFile, isAsyncIterable } from \"./uploads.mjs\";\nimport { checkFileSupport } from \"./uploads.mjs\";\n/**\n * This check adds the arrayBuffer() method type because it is available and used at runtime\n */\nconst isBlobLike = (value) => value != null &&\n typeof value === 'object' &&\n typeof value.size === 'number' &&\n typeof value.type === 'string' &&\n typeof value.text === 'function' &&\n typeof value.slice === 'function' &&\n typeof value.arrayBuffer === 'function';\n/**\n * This check adds the arrayBuffer() method type because it is available and used at runtime\n */\nconst isFileLike = (value) => value != null &&\n typeof value === 'object' &&\n typeof value.name === 'string' &&\n typeof value.lastModified === 'number' &&\n isBlobLike(value);\nconst isResponseLike = (value) => value != null &&\n typeof value === 'object' &&\n typeof value.url === 'string' &&\n typeof value.blob === 'function';\n/**\n * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats\n * @param value the raw content of the file. Can be an {@link Uploadable}, BlobLikePart, or AsyncIterable of BlobLikeParts\n * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible\n * @param {Object=} options additional properties\n * @param {string=} options.type the MIME type of the content\n * @param {number=} options.lastModified the last modified timestamp\n * @returns a {@link File} with the given properties\n */\nexport async function toFile(value, name, options) {\n checkFileSupport();\n // If it's a promise, resolve it.\n value = await value;\n // If we've been given a `File` we don't need to do anything\n if (isFileLike(value)) {\n if (value instanceof File) {\n return value;\n }\n return makeFile([await value.arrayBuffer()], value.name);\n }\n if (isResponseLike(value)) {\n const blob = await value.blob();\n name || (name = new URL(value.url).pathname.split(/[\\\\/]/).pop());\n return makeFile(await getBytes(blob), name, options);\n }\n const parts = await getBytes(value);\n name || (name = getName(value));\n if (!options?.type) {\n const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type);\n if (typeof type === 'string') {\n options = { ...options, type };\n }\n }\n return makeFile(parts, name, options);\n}\nasync function getBytes(value) {\n let parts = [];\n if (typeof value === 'string' ||\n ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.\n value instanceof ArrayBuffer) {\n parts.push(value);\n }\n else if (isBlobLike(value)) {\n parts.push(value instanceof Blob ? value : await value.arrayBuffer());\n }\n else if (isAsyncIterable(value) // includes Readable, ReadableStream, etc.\n ) {\n for await (const chunk of value) {\n parts.push(...(await getBytes(chunk))); // TODO, consider validating?\n }\n }\n else {\n const constructor = value?.constructor?.name;\n throw new Error(`Unexpected data type: ${typeof value}${constructor ? `; constructor: ${constructor}` : ''}${propsForError(value)}`);\n }\n return parts;\n}\nfunction propsForError(value) {\n if (typeof value !== 'object' || value === null)\n return '';\n const props = Object.getOwnPropertyNames(value);\n return `; props: [${props.map((p) => `\"${p}\"`).join(', ')}]`;\n}\n//# sourceMappingURL=to-file.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nexport class APIResource {\n constructor(client) {\n this._client = client;\n }\n}\n//# sourceMappingURL=resource.mjs.map","import { OpenAIError } from \"../../core/error.mjs\";\n/**\n * Percent-encode everything that isn't safe to have in a path without encoding safe chars.\n *\n * Taken from https://datatracker.ietf.org/doc/html/rfc3986#section-3.3:\n * > unreserved = ALPHA / DIGIT / \"-\" / \".\" / \"_\" / \"~\"\n * > sub-delims = \"!\" / \"$\" / \"&\" / \"'\" / \"(\" / \")\" / \"*\" / \"+\" / \",\" / \";\" / \"=\"\n * > pchar = unreserved / pct-encoded / sub-delims / \":\" / \"@\"\n */\nexport function encodeURIPath(str) {\n return str.replace(/[^A-Za-z0-9\\-._~!$&'()*+,;=:@]+/g, encodeURIComponent);\n}\nconst EMPTY = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.create(null));\nexport const createPathTagFunction = (pathEncoder = encodeURIPath) => function path(statics, ...params) {\n // If there are no params, no processing is needed.\n if (statics.length === 1)\n return statics[0];\n let postPath = false;\n const invalidSegments = [];\n const path = statics.reduce((previousValue, currentValue, index) => {\n if (/[?#]/.test(currentValue)) {\n postPath = true;\n }\n const value = params[index];\n let encoded = (postPath ? encodeURIComponent : pathEncoder)('' + value);\n if (index !== params.length &&\n (value == null ||\n (typeof value === 'object' &&\n // handle values from other realms\n value.toString ===\n Object.getPrototypeOf(Object.getPrototypeOf(value.hasOwnProperty ?? EMPTY) ?? EMPTY)\n ?.toString))) {\n encoded = value + '';\n invalidSegments.push({\n start: previousValue.length + currentValue.length,\n length: encoded.length,\n error: `Value of type ${Object.prototype.toString\n .call(value)\n .slice(8, -1)} is not a valid path parameter`,\n });\n }\n return previousValue + currentValue + (index === params.length ? '' : encoded);\n }, '');\n const pathOnly = path.split(/[?#]/, 1)[0];\n const invalidSegmentPattern = /(?<=^|\\/)(?:\\.|%2e){1,2}(?=\\/|$)/gi;\n let match;\n // Find all invalid segments\n while ((match = invalidSegmentPattern.exec(pathOnly)) !== null) {\n invalidSegments.push({\n start: match.index,\n length: match[0].length,\n error: `Value \"${match[0]}\" can\\'t be safely passed as a path parameter`,\n });\n }\n invalidSegments.sort((a, b) => a.start - b.start);\n if (invalidSegments.length > 0) {\n let lastEnd = 0;\n const underline = invalidSegments.reduce((acc, segment) => {\n const spaces = ' '.repeat(segment.start - lastEnd);\n const arrows = '^'.repeat(segment.length);\n lastEnd = segment.start + segment.length;\n return acc + spaces + arrows;\n }, '');\n throw new OpenAIError(`Path parameters result in path with invalid segments:\\n${invalidSegments\n .map((e) => e.error)\n .join('\\n')}\\n${path}\\n${underline}`);\n }\n return path;\n};\n/**\n * URI-encodes path params and ensures no unsafe /./ or /../ path segments are introduced.\n */\nexport const path = /* @__PURE__ */ createPathTagFunction(encodeURIPath);\n//# sourceMappingURL=path.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { CursorPage } from \"../../../core/pagination.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Messages extends APIResource {\n /**\n * Get the messages in a stored chat completion. Only Chat Completions that have\n * been created with the `store` parameter set to `true` will be returned.\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const chatCompletionStoreMessage of client.chat.completions.messages.list(\n * 'completion_id',\n * )) {\n * // ...\n * }\n * ```\n */\n list(completionID, query = {}, options) {\n return this._client.getAPIList(path `/chat/completions/${completionID}/messages`, (CursorPage), { query, ...options });\n }\n}\n//# sourceMappingURL=messages.mjs.map","import { ContentFilterFinishReasonError, LengthFinishReasonError, OpenAIError } from \"../error.mjs\";\nexport function isChatCompletionFunctionTool(tool) {\n return tool !== undefined && 'function' in tool && tool.function !== undefined;\n}\nexport function makeParseableResponseFormat(response_format, parser) {\n const obj = { ...response_format };\n Object.defineProperties(obj, {\n $brand: {\n value: 'auto-parseable-response-format',\n enumerable: false,\n },\n $parseRaw: {\n value: parser,\n enumerable: false,\n },\n });\n return obj;\n}\nexport function makeParseableTextFormat(response_format, parser) {\n const obj = { ...response_format };\n Object.defineProperties(obj, {\n $brand: {\n value: 'auto-parseable-response-format',\n enumerable: false,\n },\n $parseRaw: {\n value: parser,\n enumerable: false,\n },\n });\n return obj;\n}\nexport function isAutoParsableResponseFormat(response_format) {\n return response_format?.['$brand'] === 'auto-parseable-response-format';\n}\nexport function makeParseableTool(tool, { parser, callback, }) {\n const obj = { ...tool };\n Object.defineProperties(obj, {\n $brand: {\n value: 'auto-parseable-tool',\n enumerable: false,\n },\n $parseRaw: {\n value: parser,\n enumerable: false,\n },\n $callback: {\n value: callback,\n enumerable: false,\n },\n });\n return obj;\n}\nexport function isAutoParsableTool(tool) {\n return tool?.['$brand'] === 'auto-parseable-tool';\n}\nexport function maybeParseChatCompletion(completion, params) {\n if (!params || !hasAutoParseableInput(params)) {\n return {\n ...completion,\n choices: completion.choices.map((choice) => {\n assertToolCallsAreChatCompletionFunctionToolCalls(choice.message.tool_calls);\n return {\n ...choice,\n message: {\n ...choice.message,\n parsed: null,\n ...(choice.message.tool_calls ?\n {\n tool_calls: choice.message.tool_calls,\n }\n : undefined),\n },\n };\n }),\n };\n }\n return parseChatCompletion(completion, params);\n}\nexport function parseChatCompletion(completion, params) {\n const choices = completion.choices.map((choice) => {\n if (choice.finish_reason === 'length') {\n throw new LengthFinishReasonError();\n }\n if (choice.finish_reason === 'content_filter') {\n throw new ContentFilterFinishReasonError();\n }\n assertToolCallsAreChatCompletionFunctionToolCalls(choice.message.tool_calls);\n return {\n ...choice,\n message: {\n ...choice.message,\n ...(choice.message.tool_calls ?\n {\n tool_calls: choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? undefined,\n }\n : undefined),\n parsed: choice.message.content && !choice.message.refusal ?\n parseResponseFormat(params, choice.message.content)\n : null,\n },\n };\n });\n return { ...completion, choices };\n}\nfunction parseResponseFormat(params, content) {\n if (params.response_format?.type !== 'json_schema') {\n return null;\n }\n if (params.response_format?.type === 'json_schema') {\n if ('$parseRaw' in params.response_format) {\n const response_format = params.response_format;\n return response_format.$parseRaw(content);\n }\n return JSON.parse(content);\n }\n return null;\n}\nfunction parseToolCall(params, toolCall) {\n const inputTool = params.tools?.find((inputTool) => isChatCompletionFunctionTool(inputTool) && inputTool.function?.name === toolCall.function.name); // TS doesn't narrow based on isChatCompletionTool\n return {\n ...toolCall,\n function: {\n ...toolCall.function,\n parsed_arguments: isAutoParsableTool(inputTool) ? inputTool.$parseRaw(toolCall.function.arguments)\n : inputTool?.function.strict ? JSON.parse(toolCall.function.arguments)\n : null,\n },\n };\n}\nexport function shouldParseToolCall(params, toolCall) {\n if (!params || !('tools' in params) || !params.tools) {\n return false;\n }\n const inputTool = params.tools?.find((inputTool) => isChatCompletionFunctionTool(inputTool) && inputTool.function?.name === toolCall.function.name);\n return (isChatCompletionFunctionTool(inputTool) &&\n (isAutoParsableTool(inputTool) || inputTool?.function.strict || false));\n}\nexport function hasAutoParseableInput(params) {\n if (isAutoParsableResponseFormat(params.response_format)) {\n return true;\n }\n return (params.tools?.some((t) => isAutoParsableTool(t) || (t.type === 'function' && t.function.strict === true)) ?? false);\n}\nexport function assertToolCallsAreChatCompletionFunctionToolCalls(toolCalls) {\n for (const toolCall of toolCalls || []) {\n if (toolCall.type !== 'function') {\n throw new OpenAIError(`Currently only \\`function\\` tool calls are supported; Received \\`${toolCall.type}\\``);\n }\n }\n}\nexport function validateInputTools(tools) {\n for (const tool of tools ?? []) {\n if (tool.type !== 'function') {\n throw new OpenAIError(`Currently only \\`function\\` tool types support auto-parsing; Received \\`${tool.type}\\``);\n }\n if (tool.function.strict !== true) {\n throw new OpenAIError(`The \\`${tool.function.name}\\` tool is not marked with \\`strict: true\\`. Only strict function tools can be auto-parsed`);\n }\n }\n}\n//# sourceMappingURL=parser.mjs.map","export const isAssistantMessage = (message) => {\n return message?.role === 'assistant';\n};\nexport const isToolMessage = (message) => {\n return message?.role === 'tool';\n};\nexport function isPresent(obj) {\n return obj != null;\n}\n//# sourceMappingURL=chatCompletionUtils.mjs.map","var _EventStream_instances, _EventStream_connectedPromise, _EventStream_resolveConnectedPromise, _EventStream_rejectConnectedPromise, _EventStream_endPromise, _EventStream_resolveEndPromise, _EventStream_rejectEndPromise, _EventStream_listeners, _EventStream_ended, _EventStream_errored, _EventStream_aborted, _EventStream_catchingPromiseCreated, _EventStream_handleError;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"../internal/tslib.mjs\";\nimport { APIUserAbortError, OpenAIError } from \"../error.mjs\";\nexport class EventStream {\n constructor() {\n _EventStream_instances.add(this);\n this.controller = new AbortController();\n _EventStream_connectedPromise.set(this, void 0);\n _EventStream_resolveConnectedPromise.set(this, () => { });\n _EventStream_rejectConnectedPromise.set(this, () => { });\n _EventStream_endPromise.set(this, void 0);\n _EventStream_resolveEndPromise.set(this, () => { });\n _EventStream_rejectEndPromise.set(this, () => { });\n _EventStream_listeners.set(this, {});\n _EventStream_ended.set(this, false);\n _EventStream_errored.set(this, false);\n _EventStream_aborted.set(this, false);\n _EventStream_catchingPromiseCreated.set(this, false);\n __classPrivateFieldSet(this, _EventStream_connectedPromise, new Promise((resolve, reject) => {\n __classPrivateFieldSet(this, _EventStream_resolveConnectedPromise, resolve, \"f\");\n __classPrivateFieldSet(this, _EventStream_rejectConnectedPromise, reject, \"f\");\n }), \"f\");\n __classPrivateFieldSet(this, _EventStream_endPromise, new Promise((resolve, reject) => {\n __classPrivateFieldSet(this, _EventStream_resolveEndPromise, resolve, \"f\");\n __classPrivateFieldSet(this, _EventStream_rejectEndPromise, reject, \"f\");\n }), \"f\");\n // Don't let these promises cause unhandled rejection errors.\n // we will manually cause an unhandled rejection error later\n // if the user hasn't registered any error listener or called\n // any promise-returning method.\n __classPrivateFieldGet(this, _EventStream_connectedPromise, \"f\").catch(() => { });\n __classPrivateFieldGet(this, _EventStream_endPromise, \"f\").catch(() => { });\n }\n _run(executor) {\n // Unfortunately if we call `executor()` immediately we get runtime errors about\n // references to `this` before the `super()` constructor call returns.\n setTimeout(() => {\n executor().then(() => {\n this._emitFinal();\n this._emit('end');\n }, __classPrivateFieldGet(this, _EventStream_instances, \"m\", _EventStream_handleError).bind(this));\n }, 0);\n }\n _connected() {\n if (this.ended)\n return;\n __classPrivateFieldGet(this, _EventStream_resolveConnectedPromise, \"f\").call(this);\n this._emit('connect');\n }\n get ended() {\n return __classPrivateFieldGet(this, _EventStream_ended, \"f\");\n }\n get errored() {\n return __classPrivateFieldGet(this, _EventStream_errored, \"f\");\n }\n get aborted() {\n return __classPrivateFieldGet(this, _EventStream_aborted, \"f\");\n }\n abort() {\n this.controller.abort();\n }\n /**\n * Adds the listener function to the end of the listeners array for the event.\n * No checks are made to see if the listener has already been added. Multiple calls passing\n * the same combination of event and listener will result in the listener being added, and\n * called, multiple times.\n * @returns this ChatCompletionStream, so that calls can be chained\n */\n on(event, listener) {\n const listeners = __classPrivateFieldGet(this, _EventStream_listeners, \"f\")[event] || (__classPrivateFieldGet(this, _EventStream_listeners, \"f\")[event] = []);\n listeners.push({ listener });\n return this;\n }\n /**\n * Removes the specified listener from the listener array for the event.\n * off() will remove, at most, one instance of a listener from the listener array. If any single\n * listener has been added multiple times to the listener array for the specified event, then\n * off() must be called multiple times to remove each instance.\n * @returns this ChatCompletionStream, so that calls can be chained\n */\n off(event, listener) {\n const listeners = __classPrivateFieldGet(this, _EventStream_listeners, \"f\")[event];\n if (!listeners)\n return this;\n const index = listeners.findIndex((l) => l.listener === listener);\n if (index >= 0)\n listeners.splice(index, 1);\n return this;\n }\n /**\n * Adds a one-time listener function for the event. The next time the event is triggered,\n * this listener is removed and then invoked.\n * @returns this ChatCompletionStream, so that calls can be chained\n */\n once(event, listener) {\n const listeners = __classPrivateFieldGet(this, _EventStream_listeners, \"f\")[event] || (__classPrivateFieldGet(this, _EventStream_listeners, \"f\")[event] = []);\n listeners.push({ listener, once: true });\n return this;\n }\n /**\n * This is similar to `.once()`, but returns a Promise that resolves the next time\n * the event is triggered, instead of calling a listener callback.\n * @returns a Promise that resolves the next time given event is triggered,\n * or rejects if an error is emitted. (If you request the 'error' event,\n * returns a promise that resolves with the error).\n *\n * Example:\n *\n * const message = await stream.emitted('message') // rejects if the stream errors\n */\n emitted(event) {\n return new Promise((resolve, reject) => {\n __classPrivateFieldSet(this, _EventStream_catchingPromiseCreated, true, \"f\");\n if (event !== 'error')\n this.once('error', reject);\n this.once(event, resolve);\n });\n }\n async done() {\n __classPrivateFieldSet(this, _EventStream_catchingPromiseCreated, true, \"f\");\n await __classPrivateFieldGet(this, _EventStream_endPromise, \"f\");\n }\n _emit(event, ...args) {\n // make sure we don't emit any events after end\n if (__classPrivateFieldGet(this, _EventStream_ended, \"f\")) {\n return;\n }\n if (event === 'end') {\n __classPrivateFieldSet(this, _EventStream_ended, true, \"f\");\n __classPrivateFieldGet(this, _EventStream_resolveEndPromise, \"f\").call(this);\n }\n const listeners = __classPrivateFieldGet(this, _EventStream_listeners, \"f\")[event];\n if (listeners) {\n __classPrivateFieldGet(this, _EventStream_listeners, \"f\")[event] = listeners.filter((l) => !l.once);\n listeners.forEach(({ listener }) => listener(...args));\n }\n if (event === 'abort') {\n const error = args[0];\n if (!__classPrivateFieldGet(this, _EventStream_catchingPromiseCreated, \"f\") && !listeners?.length) {\n Promise.reject(error);\n }\n __classPrivateFieldGet(this, _EventStream_rejectConnectedPromise, \"f\").call(this, error);\n __classPrivateFieldGet(this, _EventStream_rejectEndPromise, \"f\").call(this, error);\n this._emit('end');\n return;\n }\n if (event === 'error') {\n // NOTE: _emit('error', error) should only be called from #handleError().\n const error = args[0];\n if (!__classPrivateFieldGet(this, _EventStream_catchingPromiseCreated, \"f\") && !listeners?.length) {\n // Trigger an unhandled rejection if the user hasn't registered any error handlers.\n // If you are seeing stack traces here, make sure to handle errors via either:\n // - runner.on('error', () => ...)\n // - await runner.done()\n // - await runner.finalChatCompletion()\n // - etc.\n Promise.reject(error);\n }\n __classPrivateFieldGet(this, _EventStream_rejectConnectedPromise, \"f\").call(this, error);\n __classPrivateFieldGet(this, _EventStream_rejectEndPromise, \"f\").call(this, error);\n this._emit('end');\n }\n }\n _emitFinal() { }\n}\n_EventStream_connectedPromise = new WeakMap(), _EventStream_resolveConnectedPromise = new WeakMap(), _EventStream_rejectConnectedPromise = new WeakMap(), _EventStream_endPromise = new WeakMap(), _EventStream_resolveEndPromise = new WeakMap(), _EventStream_rejectEndPromise = new WeakMap(), _EventStream_listeners = new WeakMap(), _EventStream_ended = new WeakMap(), _EventStream_errored = new WeakMap(), _EventStream_aborted = new WeakMap(), _EventStream_catchingPromiseCreated = new WeakMap(), _EventStream_instances = new WeakSet(), _EventStream_handleError = function _EventStream_handleError(error) {\n __classPrivateFieldSet(this, _EventStream_errored, true, \"f\");\n if (error instanceof Error && error.name === 'AbortError') {\n error = new APIUserAbortError();\n }\n if (error instanceof APIUserAbortError) {\n __classPrivateFieldSet(this, _EventStream_aborted, true, \"f\");\n return this._emit('abort', error);\n }\n if (error instanceof OpenAIError) {\n return this._emit('error', error);\n }\n if (error instanceof Error) {\n const openAIError = new OpenAIError(error.message);\n // @ts-ignore\n openAIError.cause = error;\n return this._emit('error', openAIError);\n }\n return this._emit('error', new OpenAIError(String(error)));\n};\n//# sourceMappingURL=EventStream.mjs.map","export function isRunnableFunctionWithParse(fn) {\n return typeof fn.parse === 'function';\n}\n/**\n * This is helper class for passing a `function` and `parse` where the `function`\n * argument type matches the `parse` return type.\n */\nexport class ParsingToolFunction {\n constructor(input) {\n this.type = 'function';\n this.function = input;\n }\n}\n//# sourceMappingURL=RunnableFunction.mjs.map","var _AbstractChatCompletionRunner_instances, _AbstractChatCompletionRunner_getFinalContent, _AbstractChatCompletionRunner_getFinalMessage, _AbstractChatCompletionRunner_getFinalFunctionToolCall, _AbstractChatCompletionRunner_getFinalFunctionToolCallResult, _AbstractChatCompletionRunner_calculateTotalUsage, _AbstractChatCompletionRunner_validateParams, _AbstractChatCompletionRunner_stringifyFunctionCallResult;\nimport { __classPrivateFieldGet } from \"../internal/tslib.mjs\";\nimport { OpenAIError } from \"../error.mjs\";\nimport { isAutoParsableTool, parseChatCompletion } from \"../lib/parser.mjs\";\nimport { isAssistantMessage, isToolMessage } from \"./chatCompletionUtils.mjs\";\nimport { EventStream } from \"./EventStream.mjs\";\nimport { isRunnableFunctionWithParse, } from \"./RunnableFunction.mjs\";\nconst DEFAULT_MAX_CHAT_COMPLETIONS = 10;\nexport class AbstractChatCompletionRunner extends EventStream {\n constructor() {\n super(...arguments);\n _AbstractChatCompletionRunner_instances.add(this);\n this._chatCompletions = [];\n this.messages = [];\n }\n _addChatCompletion(chatCompletion) {\n this._chatCompletions.push(chatCompletion);\n this._emit('chatCompletion', chatCompletion);\n const message = chatCompletion.choices[0]?.message;\n if (message)\n this._addMessage(message);\n return chatCompletion;\n }\n _addMessage(message, emit = true) {\n if (!('content' in message))\n message.content = null;\n this.messages.push(message);\n if (emit) {\n this._emit('message', message);\n if (isToolMessage(message) && message.content) {\n // Note, this assumes that {role: 'tool', content: …} is always the result of a call of tool of type=function.\n this._emit('functionToolCallResult', message.content);\n }\n else if (isAssistantMessage(message) && message.tool_calls) {\n for (const tool_call of message.tool_calls) {\n if (tool_call.type === 'function') {\n this._emit('functionToolCall', tool_call.function);\n }\n }\n }\n }\n }\n /**\n * @returns a promise that resolves with the final ChatCompletion, or rejects\n * if an error occurred or the stream ended prematurely without producing a ChatCompletion.\n */\n async finalChatCompletion() {\n await this.done();\n const completion = this._chatCompletions[this._chatCompletions.length - 1];\n if (!completion)\n throw new OpenAIError('stream ended without producing a ChatCompletion');\n return completion;\n }\n /**\n * @returns a promise that resolves with the content of the final ChatCompletionMessage, or rejects\n * if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.\n */\n async finalContent() {\n await this.done();\n return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalContent).call(this);\n }\n /**\n * @returns a promise that resolves with the the final assistant ChatCompletionMessage response,\n * or rejects if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.\n */\n async finalMessage() {\n await this.done();\n return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalMessage).call(this);\n }\n /**\n * @returns a promise that resolves with the content of the final FunctionCall, or rejects\n * if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.\n */\n async finalFunctionToolCall() {\n await this.done();\n return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalFunctionToolCall).call(this);\n }\n async finalFunctionToolCallResult() {\n await this.done();\n return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalFunctionToolCallResult).call(this);\n }\n async totalUsage() {\n await this.done();\n return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_calculateTotalUsage).call(this);\n }\n allChatCompletions() {\n return [...this._chatCompletions];\n }\n _emitFinal() {\n const completion = this._chatCompletions[this._chatCompletions.length - 1];\n if (completion)\n this._emit('finalChatCompletion', completion);\n const finalMessage = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalMessage).call(this);\n if (finalMessage)\n this._emit('finalMessage', finalMessage);\n const finalContent = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalContent).call(this);\n if (finalContent)\n this._emit('finalContent', finalContent);\n const finalFunctionCall = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalFunctionToolCall).call(this);\n if (finalFunctionCall)\n this._emit('finalFunctionToolCall', finalFunctionCall);\n const finalFunctionCallResult = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalFunctionToolCallResult).call(this);\n if (finalFunctionCallResult != null)\n this._emit('finalFunctionToolCallResult', finalFunctionCallResult);\n if (this._chatCompletions.some((c) => c.usage)) {\n this._emit('totalUsage', __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_calculateTotalUsage).call(this));\n }\n }\n async _createChatCompletion(client, params, options) {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted)\n this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_validateParams).call(this, params);\n const chatCompletion = await client.chat.completions.create({ ...params, stream: false }, { ...options, signal: this.controller.signal });\n this._connected();\n return this._addChatCompletion(parseChatCompletion(chatCompletion, params));\n }\n async _runChatCompletion(client, params, options) {\n for (const message of params.messages) {\n this._addMessage(message, false);\n }\n return await this._createChatCompletion(client, params, options);\n }\n async _runTools(client, params, options) {\n const role = 'tool';\n const { tool_choice = 'auto', stream, ...restParams } = params;\n const singleFunctionToCall = typeof tool_choice !== 'string' && tool_choice.type === 'function' && tool_choice?.function?.name;\n const { maxChatCompletions = DEFAULT_MAX_CHAT_COMPLETIONS } = options || {};\n // TODO(someday): clean this logic up\n const inputTools = params.tools.map((tool) => {\n if (isAutoParsableTool(tool)) {\n if (!tool.$callback) {\n throw new OpenAIError('Tool given to `.runTools()` that does not have an associated function');\n }\n return {\n type: 'function',\n function: {\n function: tool.$callback,\n name: tool.function.name,\n description: tool.function.description || '',\n parameters: tool.function.parameters,\n parse: tool.$parseRaw,\n strict: true,\n },\n };\n }\n return tool;\n });\n const functionsByName = {};\n for (const f of inputTools) {\n if (f.type === 'function') {\n functionsByName[f.function.name || f.function.function.name] = f.function;\n }\n }\n const tools = 'tools' in params ?\n inputTools.map((t) => t.type === 'function' ?\n {\n type: 'function',\n function: {\n name: t.function.name || t.function.function.name,\n parameters: t.function.parameters,\n description: t.function.description,\n strict: t.function.strict,\n },\n }\n : t)\n : undefined;\n for (const message of params.messages) {\n this._addMessage(message, false);\n }\n for (let i = 0; i < maxChatCompletions; ++i) {\n const chatCompletion = await this._createChatCompletion(client, {\n ...restParams,\n tool_choice,\n tools,\n messages: [...this.messages],\n }, options);\n const message = chatCompletion.choices[0]?.message;\n if (!message) {\n throw new OpenAIError(`missing message in ChatCompletion response`);\n }\n if (!message.tool_calls?.length) {\n return;\n }\n for (const tool_call of message.tool_calls) {\n if (tool_call.type !== 'function')\n continue;\n const tool_call_id = tool_call.id;\n const { name, arguments: args } = tool_call.function;\n const fn = functionsByName[name];\n if (!fn) {\n const content = `Invalid tool_call: ${JSON.stringify(name)}. Available options are: ${Object.keys(functionsByName)\n .map((name) => JSON.stringify(name))\n .join(', ')}. Please try again`;\n this._addMessage({ role, tool_call_id, content });\n continue;\n }\n else if (singleFunctionToCall && singleFunctionToCall !== name) {\n const content = `Invalid tool_call: ${JSON.stringify(name)}. ${JSON.stringify(singleFunctionToCall)} requested. Please try again`;\n this._addMessage({ role, tool_call_id, content });\n continue;\n }\n let parsed;\n try {\n parsed = isRunnableFunctionWithParse(fn) ? await fn.parse(args) : args;\n }\n catch (error) {\n const content = error instanceof Error ? error.message : String(error);\n this._addMessage({ role, tool_call_id, content });\n continue;\n }\n // @ts-expect-error it can't rule out `never` type.\n const rawContent = await fn.function(parsed, this);\n const content = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_stringifyFunctionCallResult).call(this, rawContent);\n this._addMessage({ role, tool_call_id, content });\n if (singleFunctionToCall) {\n return;\n }\n }\n }\n return;\n }\n}\n_AbstractChatCompletionRunner_instances = new WeakSet(), _AbstractChatCompletionRunner_getFinalContent = function _AbstractChatCompletionRunner_getFinalContent() {\n return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, \"m\", _AbstractChatCompletionRunner_getFinalMessage).call(this).content ?? null;\n}, _AbstractChatCompletionRunner_getFinalMessage = function _AbstractChatCompletionRunner_getFinalMessage() {\n let i = this.messages.length;\n while (i-- > 0) {\n const message = this.messages[i];\n if (isAssistantMessage(message)) {\n // TODO: support audio here\n const ret = {\n ...message,\n content: message.content ?? null,\n refusal: message.refusal ?? null,\n };\n return ret;\n }\n }\n throw new OpenAIError('stream ended without producing a ChatCompletionMessage with role=assistant');\n}, _AbstractChatCompletionRunner_getFinalFunctionToolCall = function _AbstractChatCompletionRunner_getFinalFunctionToolCall() {\n for (let i = this.messages.length - 1; i >= 0; i--) {\n const message = this.messages[i];\n if (isAssistantMessage(message) && message?.tool_calls?.length) {\n return message.tool_calls.filter((x) => x.type === 'function').at(-1)?.function;\n }\n }\n return;\n}, _AbstractChatCompletionRunner_getFinalFunctionToolCallResult = function _AbstractChatCompletionRunner_getFinalFunctionToolCallResult() {\n for (let i = this.messages.length - 1; i >= 0; i--) {\n const message = this.messages[i];\n if (isToolMessage(message) &&\n message.content != null &&\n typeof message.content === 'string' &&\n this.messages.some((x) => x.role === 'assistant' &&\n x.tool_calls?.some((y) => y.type === 'function' && y.id === message.tool_call_id))) {\n return message.content;\n }\n }\n return;\n}, _AbstractChatCompletionRunner_calculateTotalUsage = function _AbstractChatCompletionRunner_calculateTotalUsage() {\n const total = {\n completion_tokens: 0,\n prompt_tokens: 0,\n total_tokens: 0,\n };\n for (const { usage } of this._chatCompletions) {\n if (usage) {\n total.completion_tokens += usage.completion_tokens;\n total.prompt_tokens += usage.prompt_tokens;\n total.total_tokens += usage.total_tokens;\n }\n }\n return total;\n}, _AbstractChatCompletionRunner_validateParams = function _AbstractChatCompletionRunner_validateParams(params) {\n if (params.n != null && params.n > 1) {\n throw new OpenAIError('ChatCompletion convenience helpers only support n=1 at this time. To use n>1, please use chat.completions.create() directly.');\n }\n}, _AbstractChatCompletionRunner_stringifyFunctionCallResult = function _AbstractChatCompletionRunner_stringifyFunctionCallResult(rawContent) {\n return (typeof rawContent === 'string' ? rawContent\n : rawContent === undefined ? 'undefined'\n : JSON.stringify(rawContent));\n};\n//# sourceMappingURL=AbstractChatCompletionRunner.mjs.map","import { AbstractChatCompletionRunner, } from \"./AbstractChatCompletionRunner.mjs\";\nimport { isAssistantMessage } from \"./chatCompletionUtils.mjs\";\nexport class ChatCompletionRunner extends AbstractChatCompletionRunner {\n static runTools(client, params, options) {\n const runner = new ChatCompletionRunner();\n const opts = {\n ...options,\n headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'runTools' },\n };\n runner._run(() => runner._runTools(client, params, opts));\n return runner;\n }\n _addMessage(message, emit = true) {\n super._addMessage(message, emit);\n if (isAssistantMessage(message) && message.content) {\n this._emit('content', message.content);\n }\n }\n}\n//# sourceMappingURL=ChatCompletionRunner.mjs.map","const STR = 0b000000001;\nconst NUM = 0b000000010;\nconst ARR = 0b000000100;\nconst OBJ = 0b000001000;\nconst NULL = 0b000010000;\nconst BOOL = 0b000100000;\nconst NAN = 0b001000000;\nconst INFINITY = 0b010000000;\nconst MINUS_INFINITY = 0b100000000;\nconst INF = INFINITY | MINUS_INFINITY;\nconst SPECIAL = NULL | BOOL | INF | NAN;\nconst ATOM = STR | NUM | SPECIAL;\nconst COLLECTION = ARR | OBJ;\nconst ALL = ATOM | COLLECTION;\nconst Allow = {\n STR,\n NUM,\n ARR,\n OBJ,\n NULL,\n BOOL,\n NAN,\n INFINITY,\n MINUS_INFINITY,\n INF,\n SPECIAL,\n ATOM,\n COLLECTION,\n ALL,\n};\n// The JSON string segment was unable to be parsed completely\nclass PartialJSON extends Error {\n}\nclass MalformedJSON extends Error {\n}\n/**\n * Parse incomplete JSON\n * @param {string} jsonString Partial JSON to be parsed\n * @param {number} allowPartial Specify what types are allowed to be partial, see {@link Allow} for details\n * @returns The parsed JSON\n * @throws {PartialJSON} If the JSON is incomplete (related to the `allow` parameter)\n * @throws {MalformedJSON} If the JSON is malformed\n */\nfunction parseJSON(jsonString, allowPartial = Allow.ALL) {\n if (typeof jsonString !== 'string') {\n throw new TypeError(`expecting str, got ${typeof jsonString}`);\n }\n if (!jsonString.trim()) {\n throw new Error(`${jsonString} is empty`);\n }\n return _parseJSON(jsonString.trim(), allowPartial);\n}\nconst _parseJSON = (jsonString, allow) => {\n const length = jsonString.length;\n let index = 0;\n const markPartialJSON = (msg) => {\n throw new PartialJSON(`${msg} at position ${index}`);\n };\n const throwMalformedError = (msg) => {\n throw new MalformedJSON(`${msg} at position ${index}`);\n };\n const parseAny = () => {\n skipBlank();\n if (index >= length)\n markPartialJSON('Unexpected end of input');\n if (jsonString[index] === '\"')\n return parseStr();\n if (jsonString[index] === '{')\n return parseObj();\n if (jsonString[index] === '[')\n return parseArr();\n if (jsonString.substring(index, index + 4) === 'null' ||\n (Allow.NULL & allow && length - index < 4 && 'null'.startsWith(jsonString.substring(index)))) {\n index += 4;\n return null;\n }\n if (jsonString.substring(index, index + 4) === 'true' ||\n (Allow.BOOL & allow && length - index < 4 && 'true'.startsWith(jsonString.substring(index)))) {\n index += 4;\n return true;\n }\n if (jsonString.substring(index, index + 5) === 'false' ||\n (Allow.BOOL & allow && length - index < 5 && 'false'.startsWith(jsonString.substring(index)))) {\n index += 5;\n return false;\n }\n if (jsonString.substring(index, index + 8) === 'Infinity' ||\n (Allow.INFINITY & allow && length - index < 8 && 'Infinity'.startsWith(jsonString.substring(index)))) {\n index += 8;\n return Infinity;\n }\n if (jsonString.substring(index, index + 9) === '-Infinity' ||\n (Allow.MINUS_INFINITY & allow &&\n 1 < length - index &&\n length - index < 9 &&\n '-Infinity'.startsWith(jsonString.substring(index)))) {\n index += 9;\n return -Infinity;\n }\n if (jsonString.substring(index, index + 3) === 'NaN' ||\n (Allow.NAN & allow && length - index < 3 && 'NaN'.startsWith(jsonString.substring(index)))) {\n index += 3;\n return NaN;\n }\n return parseNum();\n };\n const parseStr = () => {\n const start = index;\n let escape = false;\n index++; // skip initial quote\n while (index < length && (jsonString[index] !== '\"' || (escape && jsonString[index - 1] === '\\\\'))) {\n escape = jsonString[index] === '\\\\' ? !escape : false;\n index++;\n }\n if (jsonString.charAt(index) == '\"') {\n try {\n return JSON.parse(jsonString.substring(start, ++index - Number(escape)));\n }\n catch (e) {\n throwMalformedError(String(e));\n }\n }\n else if (Allow.STR & allow) {\n try {\n return JSON.parse(jsonString.substring(start, index - Number(escape)) + '\"');\n }\n catch (e) {\n // SyntaxError: Invalid escape sequence\n return JSON.parse(jsonString.substring(start, jsonString.lastIndexOf('\\\\')) + '\"');\n }\n }\n markPartialJSON('Unterminated string literal');\n };\n const parseObj = () => {\n index++; // skip initial brace\n skipBlank();\n const obj = {};\n try {\n while (jsonString[index] !== '}') {\n skipBlank();\n if (index >= length && Allow.OBJ & allow)\n return obj;\n const key = parseStr();\n skipBlank();\n index++; // skip colon\n try {\n const value = parseAny();\n Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });\n }\n catch (e) {\n if (Allow.OBJ & allow)\n return obj;\n else\n throw e;\n }\n skipBlank();\n if (jsonString[index] === ',')\n index++; // skip comma\n }\n }\n catch (e) {\n if (Allow.OBJ & allow)\n return obj;\n else\n markPartialJSON(\"Expected '}' at end of object\");\n }\n index++; // skip final brace\n return obj;\n };\n const parseArr = () => {\n index++; // skip initial bracket\n const arr = [];\n try {\n while (jsonString[index] !== ']') {\n arr.push(parseAny());\n skipBlank();\n if (jsonString[index] === ',') {\n index++; // skip comma\n }\n }\n }\n catch (e) {\n if (Allow.ARR & allow) {\n return arr;\n }\n markPartialJSON(\"Expected ']' at end of array\");\n }\n index++; // skip final bracket\n return arr;\n };\n const parseNum = () => {\n if (index === 0) {\n if (jsonString === '-' && Allow.NUM & allow)\n markPartialJSON(\"Not sure what '-' is\");\n try {\n return JSON.parse(jsonString);\n }\n catch (e) {\n if (Allow.NUM & allow) {\n try {\n if ('.' === jsonString[jsonString.length - 1])\n return JSON.parse(jsonString.substring(0, jsonString.lastIndexOf('.')));\n return JSON.parse(jsonString.substring(0, jsonString.lastIndexOf('e')));\n }\n catch (e) { }\n }\n throwMalformedError(String(e));\n }\n }\n const start = index;\n if (jsonString[index] === '-')\n index++;\n while (jsonString[index] && !',]}'.includes(jsonString[index]))\n index++;\n if (index == length && !(Allow.NUM & allow))\n markPartialJSON('Unterminated number literal');\n try {\n return JSON.parse(jsonString.substring(start, index));\n }\n catch (e) {\n if (jsonString.substring(start, index) === '-' && Allow.NUM & allow)\n markPartialJSON(\"Not sure what '-' is\");\n try {\n return JSON.parse(jsonString.substring(start, jsonString.lastIndexOf('e')));\n }\n catch (e) {\n throwMalformedError(String(e));\n }\n }\n };\n const skipBlank = () => {\n while (index < length && ' \\n\\r\\t'.includes(jsonString[index])) {\n index++;\n }\n };\n return parseAny();\n};\n// using this function with malformed JSON is undefined behavior\nconst partialParse = (input) => parseJSON(input, Allow.ALL ^ Allow.NUM);\nexport { partialParse, PartialJSON, MalformedJSON };\n//# sourceMappingURL=parser.mjs.map","var _ChatCompletionStream_instances, _ChatCompletionStream_params, _ChatCompletionStream_choiceEventStates, _ChatCompletionStream_currentChatCompletionSnapshot, _ChatCompletionStream_beginRequest, _ChatCompletionStream_getChoiceEventState, _ChatCompletionStream_addChunk, _ChatCompletionStream_emitToolCallDoneEvent, _ChatCompletionStream_emitContentDoneEvents, _ChatCompletionStream_endRequest, _ChatCompletionStream_getAutoParseableResponseFormat, _ChatCompletionStream_accumulateChatCompletion;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"../internal/tslib.mjs\";\nimport { partialParse } from \"../_vendor/partial-json-parser/parser.mjs\";\nimport { APIUserAbortError, ContentFilterFinishReasonError, LengthFinishReasonError, OpenAIError, } from \"../error.mjs\";\nimport { hasAutoParseableInput, isAutoParsableResponseFormat, isAutoParsableTool, isChatCompletionFunctionTool, maybeParseChatCompletion, shouldParseToolCall, } from \"../lib/parser.mjs\";\nimport { Stream } from \"../streaming.mjs\";\nimport { AbstractChatCompletionRunner, } from \"./AbstractChatCompletionRunner.mjs\";\nexport class ChatCompletionStream extends AbstractChatCompletionRunner {\n constructor(params) {\n super();\n _ChatCompletionStream_instances.add(this);\n _ChatCompletionStream_params.set(this, void 0);\n _ChatCompletionStream_choiceEventStates.set(this, void 0);\n _ChatCompletionStream_currentChatCompletionSnapshot.set(this, void 0);\n __classPrivateFieldSet(this, _ChatCompletionStream_params, params, \"f\");\n __classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], \"f\");\n }\n get currentChatCompletionSnapshot() {\n return __classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, \"f\");\n }\n /**\n * Intended for use on the frontend, consuming a stream produced with\n * `.toReadableStream()` on the backend.\n *\n * Note that messages sent to the model do not appear in `.on('message')`\n * in this context.\n */\n static fromReadableStream(stream) {\n const runner = new ChatCompletionStream(null);\n runner._run(() => runner._fromReadableStream(stream));\n return runner;\n }\n static createChatCompletion(client, params, options) {\n const runner = new ChatCompletionStream(params);\n runner._run(() => runner._runChatCompletion(client, { ...params, stream: true }, { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } }));\n return runner;\n }\n async _createChatCompletion(client, params, options) {\n super._createChatCompletion;\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted)\n this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_beginRequest).call(this);\n const stream = await client.chat.completions.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });\n this._connected();\n for await (const chunk of stream) {\n __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_addChunk).call(this, chunk);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n return this._addChatCompletion(__classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_endRequest).call(this));\n }\n async _fromReadableStream(readableStream, options) {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted)\n this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_beginRequest).call(this);\n this._connected();\n const stream = Stream.fromReadableStream(readableStream, this.controller);\n let chatId;\n for await (const chunk of stream) {\n if (chatId && chatId !== chunk.id) {\n // A new request has been made.\n this._addChatCompletion(__classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_endRequest).call(this));\n }\n __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_addChunk).call(this, chunk);\n chatId = chunk.id;\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n return this._addChatCompletion(__classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_endRequest).call(this));\n }\n [(_ChatCompletionStream_params = new WeakMap(), _ChatCompletionStream_choiceEventStates = new WeakMap(), _ChatCompletionStream_currentChatCompletionSnapshot = new WeakMap(), _ChatCompletionStream_instances = new WeakSet(), _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest() {\n if (this.ended)\n return;\n __classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, \"f\");\n }, _ChatCompletionStream_getChoiceEventState = function _ChatCompletionStream_getChoiceEventState(choice) {\n let state = __classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, \"f\")[choice.index];\n if (state) {\n return state;\n }\n state = {\n content_done: false,\n refusal_done: false,\n logprobs_content_done: false,\n logprobs_refusal_done: false,\n done_tool_calls: new Set(),\n current_tool_call_index: null,\n };\n __classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, \"f\")[choice.index] = state;\n return state;\n }, _ChatCompletionStream_addChunk = function _ChatCompletionStream_addChunk(chunk) {\n if (this.ended)\n return;\n const completion = __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk);\n this._emit('chunk', chunk, completion);\n for (const choice of chunk.choices) {\n const choiceSnapshot = completion.choices[choice.index];\n if (choice.delta.content != null &&\n choiceSnapshot.message?.role === 'assistant' &&\n choiceSnapshot.message?.content) {\n this._emit('content', choice.delta.content, choiceSnapshot.message.content);\n this._emit('content.delta', {\n delta: choice.delta.content,\n snapshot: choiceSnapshot.message.content,\n parsed: choiceSnapshot.message.parsed,\n });\n }\n if (choice.delta.refusal != null &&\n choiceSnapshot.message?.role === 'assistant' &&\n choiceSnapshot.message?.refusal) {\n this._emit('refusal.delta', {\n delta: choice.delta.refusal,\n snapshot: choiceSnapshot.message.refusal,\n });\n }\n if (choice.logprobs?.content != null && choiceSnapshot.message?.role === 'assistant') {\n this._emit('logprobs.content.delta', {\n content: choice.logprobs?.content,\n snapshot: choiceSnapshot.logprobs?.content ?? [],\n });\n }\n if (choice.logprobs?.refusal != null && choiceSnapshot.message?.role === 'assistant') {\n this._emit('logprobs.refusal.delta', {\n refusal: choice.logprobs?.refusal,\n snapshot: choiceSnapshot.logprobs?.refusal ?? [],\n });\n }\n const state = __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);\n if (choiceSnapshot.finish_reason) {\n __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);\n if (state.current_tool_call_index != null) {\n __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);\n }\n }\n for (const toolCall of choice.delta.tool_calls ?? []) {\n if (state.current_tool_call_index !== toolCall.index) {\n __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);\n // new tool call started, the previous one is done\n if (state.current_tool_call_index != null) {\n __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);\n }\n }\n state.current_tool_call_index = toolCall.index;\n }\n for (const toolCallDelta of choice.delta.tool_calls ?? []) {\n const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallDelta.index];\n if (!toolCallSnapshot?.type) {\n continue;\n }\n if (toolCallSnapshot?.type === 'function') {\n this._emit('tool_calls.function.arguments.delta', {\n name: toolCallSnapshot.function?.name,\n index: toolCallDelta.index,\n arguments: toolCallSnapshot.function.arguments,\n parsed_arguments: toolCallSnapshot.function.parsed_arguments,\n arguments_delta: toolCallDelta.function?.arguments ?? '',\n });\n }\n else {\n assertNever(toolCallSnapshot?.type);\n }\n }\n }\n }, _ChatCompletionStream_emitToolCallDoneEvent = function _ChatCompletionStream_emitToolCallDoneEvent(choiceSnapshot, toolCallIndex) {\n const state = __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);\n if (state.done_tool_calls.has(toolCallIndex)) {\n // we've already fired the done event\n return;\n }\n const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallIndex];\n if (!toolCallSnapshot) {\n throw new Error('no tool call snapshot');\n }\n if (!toolCallSnapshot.type) {\n throw new Error('tool call snapshot missing `type`');\n }\n if (toolCallSnapshot.type === 'function') {\n const inputTool = __classPrivateFieldGet(this, _ChatCompletionStream_params, \"f\")?.tools?.find((tool) => isChatCompletionFunctionTool(tool) && tool.function.name === toolCallSnapshot.function.name); // TS doesn't narrow based on isChatCompletionTool\n this._emit('tool_calls.function.arguments.done', {\n name: toolCallSnapshot.function.name,\n index: toolCallIndex,\n arguments: toolCallSnapshot.function.arguments,\n parsed_arguments: isAutoParsableTool(inputTool) ? inputTool.$parseRaw(toolCallSnapshot.function.arguments)\n : inputTool?.function.strict ? JSON.parse(toolCallSnapshot.function.arguments)\n : null,\n });\n }\n else {\n assertNever(toolCallSnapshot.type);\n }\n }, _ChatCompletionStream_emitContentDoneEvents = function _ChatCompletionStream_emitContentDoneEvents(choiceSnapshot) {\n const state = __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);\n if (choiceSnapshot.message.content && !state.content_done) {\n state.content_done = true;\n const responseFormat = __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_getAutoParseableResponseFormat).call(this);\n this._emit('content.done', {\n content: choiceSnapshot.message.content,\n parsed: responseFormat ? responseFormat.$parseRaw(choiceSnapshot.message.content) : null,\n });\n }\n if (choiceSnapshot.message.refusal && !state.refusal_done) {\n state.refusal_done = true;\n this._emit('refusal.done', { refusal: choiceSnapshot.message.refusal });\n }\n if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {\n state.logprobs_content_done = true;\n this._emit('logprobs.content.done', { content: choiceSnapshot.logprobs.content });\n }\n if (choiceSnapshot.logprobs?.refusal && !state.logprobs_refusal_done) {\n state.logprobs_refusal_done = true;\n this._emit('logprobs.refusal.done', { refusal: choiceSnapshot.logprobs.refusal });\n }\n }, _ChatCompletionStream_endRequest = function _ChatCompletionStream_endRequest() {\n if (this.ended) {\n throw new OpenAIError(`stream has ended, this shouldn't happen`);\n }\n const snapshot = __classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, \"f\");\n if (!snapshot) {\n throw new OpenAIError(`request ended without sending any chunks`);\n }\n __classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, \"f\");\n __classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], \"f\");\n return finalizeChatCompletion(snapshot, __classPrivateFieldGet(this, _ChatCompletionStream_params, \"f\"));\n }, _ChatCompletionStream_getAutoParseableResponseFormat = function _ChatCompletionStream_getAutoParseableResponseFormat() {\n const responseFormat = __classPrivateFieldGet(this, _ChatCompletionStream_params, \"f\")?.response_format;\n if (isAutoParsableResponseFormat(responseFormat)) {\n return responseFormat;\n }\n return null;\n }, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion(chunk) {\n var _a, _b, _c, _d;\n let snapshot = __classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, \"f\");\n const { choices, ...rest } = chunk;\n if (!snapshot) {\n snapshot = __classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, {\n ...rest,\n choices: [],\n }, \"f\");\n }\n else {\n Object.assign(snapshot, rest);\n }\n for (const { delta, finish_reason, index, logprobs = null, ...other } of chunk.choices) {\n let choice = snapshot.choices[index];\n if (!choice) {\n choice = snapshot.choices[index] = { finish_reason, index, message: {}, logprobs, ...other };\n }\n if (logprobs) {\n if (!choice.logprobs) {\n choice.logprobs = Object.assign({}, logprobs);\n }\n else {\n const { content, refusal, ...rest } = logprobs;\n assertIsEmpty(rest);\n Object.assign(choice.logprobs, rest);\n if (content) {\n (_a = choice.logprobs).content ?? (_a.content = []);\n choice.logprobs.content.push(...content);\n }\n if (refusal) {\n (_b = choice.logprobs).refusal ?? (_b.refusal = []);\n choice.logprobs.refusal.push(...refusal);\n }\n }\n }\n if (finish_reason) {\n choice.finish_reason = finish_reason;\n if (__classPrivateFieldGet(this, _ChatCompletionStream_params, \"f\") && hasAutoParseableInput(__classPrivateFieldGet(this, _ChatCompletionStream_params, \"f\"))) {\n if (finish_reason === 'length') {\n throw new LengthFinishReasonError();\n }\n if (finish_reason === 'content_filter') {\n throw new ContentFilterFinishReasonError();\n }\n }\n }\n Object.assign(choice, other);\n if (!delta)\n continue; // Shouldn't happen; just in case.\n const { content, refusal, function_call, role, tool_calls, ...rest } = delta;\n assertIsEmpty(rest);\n Object.assign(choice.message, rest);\n if (refusal) {\n choice.message.refusal = (choice.message.refusal || '') + refusal;\n }\n if (role)\n choice.message.role = role;\n if (function_call) {\n if (!choice.message.function_call) {\n choice.message.function_call = function_call;\n }\n else {\n if (function_call.name)\n choice.message.function_call.name = function_call.name;\n if (function_call.arguments) {\n (_c = choice.message.function_call).arguments ?? (_c.arguments = '');\n choice.message.function_call.arguments += function_call.arguments;\n }\n }\n }\n if (content) {\n choice.message.content = (choice.message.content || '') + content;\n if (!choice.message.refusal && __classPrivateFieldGet(this, _ChatCompletionStream_instances, \"m\", _ChatCompletionStream_getAutoParseableResponseFormat).call(this)) {\n choice.message.parsed = partialParse(choice.message.content);\n }\n }\n if (tool_calls) {\n if (!choice.message.tool_calls)\n choice.message.tool_calls = [];\n for (const { index, id, type, function: fn, ...rest } of tool_calls) {\n const tool_call = ((_d = choice.message.tool_calls)[index] ?? (_d[index] = {}));\n Object.assign(tool_call, rest);\n if (id)\n tool_call.id = id;\n if (type)\n tool_call.type = type;\n if (fn)\n tool_call.function ?? (tool_call.function = { name: fn.name ?? '', arguments: '' });\n if (fn?.name)\n tool_call.function.name = fn.name;\n if (fn?.arguments) {\n tool_call.function.arguments += fn.arguments;\n if (shouldParseToolCall(__classPrivateFieldGet(this, _ChatCompletionStream_params, \"f\"), tool_call)) {\n tool_call.function.parsed_arguments = partialParse(tool_call.function.arguments);\n }\n }\n }\n }\n }\n return snapshot;\n }, Symbol.asyncIterator)]() {\n const pushQueue = [];\n const readQueue = [];\n let done = false;\n this.on('chunk', (chunk) => {\n const reader = readQueue.shift();\n if (reader) {\n reader.resolve(chunk);\n }\n else {\n pushQueue.push(chunk);\n }\n });\n this.on('end', () => {\n done = true;\n for (const reader of readQueue) {\n reader.resolve(undefined);\n }\n readQueue.length = 0;\n });\n this.on('abort', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n this.on('error', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n return {\n next: async () => {\n if (!pushQueue.length) {\n if (done) {\n return { value: undefined, done: true };\n }\n return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));\n }\n const chunk = pushQueue.shift();\n return { value: chunk, done: false };\n },\n return: async () => {\n this.abort();\n return { value: undefined, done: true };\n },\n };\n }\n toReadableStream() {\n const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);\n return stream.toReadableStream();\n }\n}\nfunction finalizeChatCompletion(snapshot, params) {\n const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;\n const completion = {\n ...rest,\n id,\n choices: choices.map(({ message, finish_reason, index, logprobs, ...choiceRest }) => {\n if (!finish_reason) {\n throw new OpenAIError(`missing finish_reason for choice ${index}`);\n }\n const { content = null, function_call, tool_calls, ...messageRest } = message;\n const role = message.role; // this is what we expect; in theory it could be different which would make our types a slight lie but would be fine.\n if (!role) {\n throw new OpenAIError(`missing role for choice ${index}`);\n }\n if (function_call) {\n const { arguments: args, name } = function_call;\n if (args == null) {\n throw new OpenAIError(`missing function_call.arguments for choice ${index}`);\n }\n if (!name) {\n throw new OpenAIError(`missing function_call.name for choice ${index}`);\n }\n return {\n ...choiceRest,\n message: {\n content,\n function_call: { arguments: args, name },\n role,\n refusal: message.refusal ?? null,\n },\n finish_reason,\n index,\n logprobs,\n };\n }\n if (tool_calls) {\n return {\n ...choiceRest,\n index,\n finish_reason,\n logprobs,\n message: {\n ...messageRest,\n role,\n content,\n refusal: message.refusal ?? null,\n tool_calls: tool_calls.map((tool_call, i) => {\n const { function: fn, type, id, ...toolRest } = tool_call;\n const { arguments: args, name, ...fnRest } = fn || {};\n if (id == null) {\n throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].id\\n${str(snapshot)}`);\n }\n if (type == null) {\n throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].type\\n${str(snapshot)}`);\n }\n if (name == null) {\n throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name\\n${str(snapshot)}`);\n }\n if (args == null) {\n throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments\\n${str(snapshot)}`);\n }\n return { ...toolRest, id, type, function: { ...fnRest, name, arguments: args } };\n }),\n },\n };\n }\n return {\n ...choiceRest,\n message: { ...messageRest, content, role, refusal: message.refusal ?? null },\n finish_reason,\n index,\n logprobs,\n };\n }),\n created,\n model,\n object: 'chat.completion',\n ...(system_fingerprint ? { system_fingerprint } : {}),\n };\n return maybeParseChatCompletion(completion, params);\n}\nfunction str(x) {\n return JSON.stringify(x);\n}\n/**\n * Ensures the given argument is an empty object, useful for\n * asserting that all known properties on an object have been\n * destructured.\n */\nfunction assertIsEmpty(obj) {\n return;\n}\nfunction assertNever(_x) { }\n//# sourceMappingURL=ChatCompletionStream.mjs.map","import { ChatCompletionStream } from \"./ChatCompletionStream.mjs\";\nexport class ChatCompletionStreamingRunner extends ChatCompletionStream {\n static fromReadableStream(stream) {\n const runner = new ChatCompletionStreamingRunner(null);\n runner._run(() => runner._fromReadableStream(stream));\n return runner;\n }\n static runTools(client, params, options) {\n const runner = new ChatCompletionStreamingRunner(\n // @ts-expect-error TODO these types are incompatible\n params);\n const opts = {\n ...options,\n headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'runTools' },\n };\n runner._run(() => runner._runTools(client, params, opts));\n return runner;\n }\n}\n//# sourceMappingURL=ChatCompletionStreamingRunner.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as MessagesAPI from \"./messages.mjs\";\nimport { Messages } from \"./messages.mjs\";\nimport { CursorPage } from \"../../../core/pagination.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nimport { ChatCompletionRunner } from \"../../../lib/ChatCompletionRunner.mjs\";\nimport { ChatCompletionStreamingRunner } from \"../../../lib/ChatCompletionStreamingRunner.mjs\";\nimport { ChatCompletionStream } from \"../../../lib/ChatCompletionStream.mjs\";\nimport { parseChatCompletion, validateInputTools } from \"../../../lib/parser.mjs\";\nexport class Completions extends APIResource {\n constructor() {\n super(...arguments);\n this.messages = new MessagesAPI.Messages(this._client);\n }\n create(body, options) {\n return this._client.post('/chat/completions', { body, ...options, stream: body.stream ?? false });\n }\n /**\n * Get a stored chat completion. Only Chat Completions that have been created with\n * the `store` parameter set to `true` will be returned.\n *\n * @example\n * ```ts\n * const chatCompletion =\n * await client.chat.completions.retrieve('completion_id');\n * ```\n */\n retrieve(completionID, options) {\n return this._client.get(path `/chat/completions/${completionID}`, options);\n }\n /**\n * Modify a stored chat completion. Only Chat Completions that have been created\n * with the `store` parameter set to `true` can be modified. Currently, the only\n * supported modification is to update the `metadata` field.\n *\n * @example\n * ```ts\n * const chatCompletion = await client.chat.completions.update(\n * 'completion_id',\n * { metadata: { foo: 'string' } },\n * );\n * ```\n */\n update(completionID, body, options) {\n return this._client.post(path `/chat/completions/${completionID}`, { body, ...options });\n }\n /**\n * List stored Chat Completions. Only Chat Completions that have been stored with\n * the `store` parameter set to `true` will be returned.\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const chatCompletion of client.chat.completions.list()) {\n * // ...\n * }\n * ```\n */\n list(query = {}, options) {\n return this._client.getAPIList('/chat/completions', (CursorPage), { query, ...options });\n }\n /**\n * Delete a stored chat completion. Only Chat Completions that have been created\n * with the `store` parameter set to `true` can be deleted.\n *\n * @example\n * ```ts\n * const chatCompletionDeleted =\n * await client.chat.completions.delete('completion_id');\n * ```\n */\n delete(completionID, options) {\n return this._client.delete(path `/chat/completions/${completionID}`, options);\n }\n parse(body, options) {\n validateInputTools(body.tools);\n return this._client.chat.completions\n .create(body, {\n ...options,\n headers: {\n ...options?.headers,\n 'X-Stainless-Helper-Method': 'chat.completions.parse',\n },\n })\n ._thenUnwrap((completion) => parseChatCompletion(completion, body));\n }\n runTools(body, options) {\n if (body.stream) {\n return ChatCompletionStreamingRunner.runTools(this._client, body, options);\n }\n return ChatCompletionRunner.runTools(this._client, body, options);\n }\n /**\n * Creates a chat completion stream\n */\n stream(body, options) {\n return ChatCompletionStream.createChatCompletion(this._client, body, options);\n }\n}\nexport { ChatCompletionStreamingRunner } from \"../../../lib/ChatCompletionStreamingRunner.mjs\";\nexport { ParsingToolFunction, } from \"../../../lib/RunnableFunction.mjs\";\nexport { ChatCompletionStream } from \"../../../lib/ChatCompletionStream.mjs\";\nexport { ChatCompletionRunner } from \"../../../lib/ChatCompletionRunner.mjs\";\nCompletions.Messages = Messages;\n//# sourceMappingURL=completions.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as CompletionsAPI from \"./completions/completions.mjs\";\nimport { Completions, } from \"./completions/completions.mjs\";\nexport class Chat extends APIResource {\n constructor() {\n super(...arguments);\n this.completions = new CompletionsAPI.Completions(this._client);\n }\n}\nChat.Completions = Completions;\n//# sourceMappingURL=chat.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { isReadonlyArray } from \"./utils/values.mjs\";\nconst brand_privateNullableHeaders = /* @__PURE__ */ Symbol('brand.privateNullableHeaders');\nfunction* iterateHeaders(headers) {\n if (!headers)\n return;\n if (brand_privateNullableHeaders in headers) {\n const { values, nulls } = headers;\n yield* values.entries();\n for (const name of nulls) {\n yield [name, null];\n }\n return;\n }\n let shouldClear = false;\n let iter;\n if (headers instanceof Headers) {\n iter = headers.entries();\n }\n else if (isReadonlyArray(headers)) {\n iter = headers;\n }\n else {\n shouldClear = true;\n iter = Object.entries(headers ?? {});\n }\n for (let row of iter) {\n const name = row[0];\n if (typeof name !== 'string')\n throw new TypeError('expected header name to be a string');\n const values = isReadonlyArray(row[1]) ? row[1] : [row[1]];\n let didClear = false;\n for (const value of values) {\n if (value === undefined)\n continue;\n // Objects keys always overwrite older headers, they never append.\n // Yield a null to clear the header before adding the new values.\n if (shouldClear && !didClear) {\n didClear = true;\n yield [name, null];\n }\n yield [name, value];\n }\n }\n}\nexport const buildHeaders = (newHeaders) => {\n const targetHeaders = new Headers();\n const nullHeaders = new Set();\n for (const headers of newHeaders) {\n const seenHeaders = new Set();\n for (const [name, value] of iterateHeaders(headers)) {\n const lowerName = name.toLowerCase();\n if (!seenHeaders.has(lowerName)) {\n targetHeaders.delete(name);\n seenHeaders.add(lowerName);\n }\n if (value === null) {\n targetHeaders.delete(name);\n nullHeaders.add(lowerName);\n }\n else {\n targetHeaders.append(name, value);\n nullHeaders.delete(lowerName);\n }\n }\n }\n return { [brand_privateNullableHeaders]: true, values: targetHeaders, nulls: nullHeaders };\n};\nexport const isEmptyHeaders = (headers) => {\n for (const _ of iterateHeaders(headers))\n return false;\n return true;\n};\n//# sourceMappingURL=headers.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { buildHeaders } from \"../../internal/headers.mjs\";\nexport class Speech extends APIResource {\n /**\n * Generates audio from the input text.\n *\n * @example\n * ```ts\n * const speech = await client.audio.speech.create({\n * input: 'input',\n * model: 'string',\n * voice: 'ash',\n * });\n *\n * const content = await speech.blob();\n * console.log(content);\n * ```\n */\n create(body, options) {\n return this._client.post('/audio/speech', {\n body,\n ...options,\n headers: buildHeaders([{ Accept: 'application/octet-stream' }, options?.headers]),\n __binaryResponse: true,\n });\n }\n}\n//# sourceMappingURL=speech.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { multipartFormRequestOptions } from \"../../internal/uploads.mjs\";\nexport class Transcriptions extends APIResource {\n create(body, options) {\n return this._client.post('/audio/transcriptions', multipartFormRequestOptions({\n body,\n ...options,\n stream: body.stream ?? false,\n __metadata: { model: body.model },\n }, this._client));\n }\n}\n//# sourceMappingURL=transcriptions.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { multipartFormRequestOptions } from \"../../internal/uploads.mjs\";\nexport class Translations extends APIResource {\n create(body, options) {\n return this._client.post('/audio/translations', multipartFormRequestOptions({ body, ...options, __metadata: { model: body.model } }, this._client));\n }\n}\n//# sourceMappingURL=translations.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as SpeechAPI from \"./speech.mjs\";\nimport { Speech } from \"./speech.mjs\";\nimport * as TranscriptionsAPI from \"./transcriptions.mjs\";\nimport { Transcriptions, } from \"./transcriptions.mjs\";\nimport * as TranslationsAPI from \"./translations.mjs\";\nimport { Translations, } from \"./translations.mjs\";\nexport class Audio extends APIResource {\n constructor() {\n super(...arguments);\n this.transcriptions = new TranscriptionsAPI.Transcriptions(this._client);\n this.translations = new TranslationsAPI.Translations(this._client);\n this.speech = new SpeechAPI.Speech(this._client);\n }\n}\nAudio.Transcriptions = Transcriptions;\nAudio.Translations = Translations;\nAudio.Speech = Speech;\n//# sourceMappingURL=audio.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../core/resource.mjs\";\nimport { CursorPage } from \"../core/pagination.mjs\";\nimport { path } from \"../internal/utils/path.mjs\";\nexport class Batches extends APIResource {\n /**\n * Creates and executes a batch from an uploaded file of requests\n */\n create(body, options) {\n return this._client.post('/batches', { body, ...options });\n }\n /**\n * Retrieves a batch.\n */\n retrieve(batchID, options) {\n return this._client.get(path `/batches/${batchID}`, options);\n }\n /**\n * List your organization's batches.\n */\n list(query = {}, options) {\n return this._client.getAPIList('/batches', (CursorPage), { query, ...options });\n }\n /**\n * Cancels an in-progress batch. The batch will be in status `cancelling` for up to\n * 10 minutes, before changing to `cancelled`, where it will have partial results\n * (if any) available in the output file.\n */\n cancel(batchID, options) {\n return this._client.post(path `/batches/${batchID}/cancel`, options);\n }\n}\n//# sourceMappingURL=batches.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { CursorPage } from \"../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../internal/headers.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Assistants extends APIResource {\n /**\n * Create an assistant with a model and instructions.\n *\n * @example\n * ```ts\n * const assistant = await client.beta.assistants.create({\n * model: 'gpt-4o',\n * });\n * ```\n */\n create(body, options) {\n return this._client.post('/assistants', {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Retrieves an assistant.\n *\n * @example\n * ```ts\n * const assistant = await client.beta.assistants.retrieve(\n * 'assistant_id',\n * );\n * ```\n */\n retrieve(assistantID, options) {\n return this._client.get(path `/assistants/${assistantID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Modifies an assistant.\n *\n * @example\n * ```ts\n * const assistant = await client.beta.assistants.update(\n * 'assistant_id',\n * );\n * ```\n */\n update(assistantID, body, options) {\n return this._client.post(path `/assistants/${assistantID}`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Returns a list of assistants.\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const assistant of client.beta.assistants.list()) {\n * // ...\n * }\n * ```\n */\n list(query = {}, options) {\n return this._client.getAPIList('/assistants', (CursorPage), {\n query,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Delete an assistant.\n *\n * @example\n * ```ts\n * const assistantDeleted =\n * await client.beta.assistants.delete('assistant_id');\n * ```\n */\n delete(assistantID, options) {\n return this._client.delete(path `/assistants/${assistantID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n}\n//# sourceMappingURL=assistants.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { buildHeaders } from \"../../../internal/headers.mjs\";\nexport class Sessions extends APIResource {\n /**\n * Create an ephemeral API token for use in client-side applications with the\n * Realtime API. Can be configured with the same session parameters as the\n * `session.update` client event.\n *\n * It responds with a session object, plus a `client_secret` key which contains a\n * usable ephemeral API token that can be used to authenticate browser clients for\n * the Realtime API.\n *\n * @example\n * ```ts\n * const session =\n * await client.beta.realtime.sessions.create();\n * ```\n */\n create(body, options) {\n return this._client.post('/realtime/sessions', {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n}\n//# sourceMappingURL=sessions.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { buildHeaders } from \"../../../internal/headers.mjs\";\nexport class TranscriptionSessions extends APIResource {\n /**\n * Create an ephemeral API token for use in client-side applications with the\n * Realtime API specifically for realtime transcriptions. Can be configured with\n * the same session parameters as the `transcription_session.update` client event.\n *\n * It responds with a session object, plus a `client_secret` key which contains a\n * usable ephemeral API token that can be used to authenticate browser clients for\n * the Realtime API.\n *\n * @example\n * ```ts\n * const transcriptionSession =\n * await client.beta.realtime.transcriptionSessions.create();\n * ```\n */\n create(body, options) {\n return this._client.post('/realtime/transcription_sessions', {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n}\n//# sourceMappingURL=transcription-sessions.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as SessionsAPI from \"./sessions.mjs\";\nimport { Sessions, } from \"./sessions.mjs\";\nimport * as TranscriptionSessionsAPI from \"./transcription-sessions.mjs\";\nimport { TranscriptionSessions, } from \"./transcription-sessions.mjs\";\n/**\n * @deprecated Realtime has now launched and is generally available. The old beta API is now deprecated.\n */\nexport class Realtime extends APIResource {\n constructor() {\n super(...arguments);\n this.sessions = new SessionsAPI.Sessions(this._client);\n this.transcriptionSessions = new TranscriptionSessionsAPI.TranscriptionSessions(this._client);\n }\n}\nRealtime.Sessions = Sessions;\nRealtime.TranscriptionSessions = TranscriptionSessions;\n//# sourceMappingURL=realtime.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { buildHeaders } from \"../../../internal/headers.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Sessions extends APIResource {\n /**\n * Create a ChatKit session\n *\n * @example\n * ```ts\n * const chatSession =\n * await client.beta.chatkit.sessions.create({\n * user: 'x',\n * workflow: { id: 'id' },\n * });\n * ```\n */\n create(body, options) {\n return this._client.post('/chatkit/sessions', {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'chatkit_beta=v1' }, options?.headers]),\n });\n }\n /**\n * Cancel a ChatKit session\n *\n * @example\n * ```ts\n * const chatSession =\n * await client.beta.chatkit.sessions.cancel('cksess_123');\n * ```\n */\n cancel(sessionID, options) {\n return this._client.post(path `/chatkit/sessions/${sessionID}/cancel`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'chatkit_beta=v1' }, options?.headers]),\n });\n }\n}\n//# sourceMappingURL=sessions.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { ConversationCursorPage, } from \"../../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../../internal/headers.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Threads extends APIResource {\n /**\n * Retrieve a ChatKit thread\n *\n * @example\n * ```ts\n * const chatkitThread =\n * await client.beta.chatkit.threads.retrieve('cthr_123');\n * ```\n */\n retrieve(threadID, options) {\n return this._client.get(path `/chatkit/threads/${threadID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'chatkit_beta=v1' }, options?.headers]),\n });\n }\n /**\n * List ChatKit threads\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const chatkitThread of client.beta.chatkit.threads.list()) {\n * // ...\n * }\n * ```\n */\n list(query = {}, options) {\n return this._client.getAPIList('/chatkit/threads', (ConversationCursorPage), {\n query,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'chatkit_beta=v1' }, options?.headers]),\n });\n }\n /**\n * Delete a ChatKit thread\n *\n * @example\n * ```ts\n * const thread = await client.beta.chatkit.threads.delete(\n * 'cthr_123',\n * );\n * ```\n */\n delete(threadID, options) {\n return this._client.delete(path `/chatkit/threads/${threadID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'chatkit_beta=v1' }, options?.headers]),\n });\n }\n /**\n * List ChatKit thread items\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const thread of client.beta.chatkit.threads.listItems(\n * 'cthr_123',\n * )) {\n * // ...\n * }\n * ```\n */\n listItems(threadID, query = {}, options) {\n return this._client.getAPIList(path `/chatkit/threads/${threadID}/items`, (ConversationCursorPage), { query, ...options, headers: buildHeaders([{ 'OpenAI-Beta': 'chatkit_beta=v1' }, options?.headers]) });\n }\n}\n//# sourceMappingURL=threads.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as SessionsAPI from \"./sessions.mjs\";\nimport { Sessions } from \"./sessions.mjs\";\nimport * as ThreadsAPI from \"./threads.mjs\";\nimport { Threads, } from \"./threads.mjs\";\nexport class ChatKit extends APIResource {\n constructor() {\n super(...arguments);\n this.sessions = new SessionsAPI.Sessions(this._client);\n this.threads = new ThreadsAPI.Threads(this._client);\n }\n}\nChatKit.Sessions = Sessions;\nChatKit.Threads = Threads;\n//# sourceMappingURL=chatkit.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { CursorPage } from \"../../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../../internal/headers.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\n/**\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\nexport class Messages extends APIResource {\n /**\n * Create a message.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n create(threadID, body, options) {\n return this._client.post(path `/threads/${threadID}/messages`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Retrieve a message.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n retrieve(messageID, params, options) {\n const { thread_id } = params;\n return this._client.get(path `/threads/${thread_id}/messages/${messageID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Modifies a message.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n update(messageID, params, options) {\n const { thread_id, ...body } = params;\n return this._client.post(path `/threads/${thread_id}/messages/${messageID}`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Returns a list of messages for a given thread.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n list(threadID, query = {}, options) {\n return this._client.getAPIList(path `/threads/${threadID}/messages`, (CursorPage), {\n query,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Deletes a message.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n delete(messageID, params, options) {\n const { thread_id } = params;\n return this._client.delete(path `/threads/${thread_id}/messages/${messageID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n}\n//# sourceMappingURL=messages.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../../core/resource.mjs\";\nimport { CursorPage } from \"../../../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../../../internal/headers.mjs\";\nimport { path } from \"../../../../internal/utils/path.mjs\";\n/**\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\nexport class Steps extends APIResource {\n /**\n * Retrieves a run step.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n retrieve(stepID, params, options) {\n const { thread_id, run_id, ...query } = params;\n return this._client.get(path `/threads/${thread_id}/runs/${run_id}/steps/${stepID}`, {\n query,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Returns a list of run steps belonging to a run.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n list(runID, params, options) {\n const { thread_id, ...query } = params;\n return this._client.getAPIList(path `/threads/${thread_id}/runs/${runID}/steps`, (CursorPage), {\n query,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n}\n//# sourceMappingURL=steps.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { OpenAIError } from \"../../core/error.mjs\";\nimport { encodeUTF8 } from \"./bytes.mjs\";\nexport const toBase64 = (data) => {\n if (!data)\n return '';\n if (typeof globalThis.Buffer !== 'undefined') {\n return globalThis.Buffer.from(data).toString('base64');\n }\n if (typeof data === 'string') {\n data = encodeUTF8(data);\n }\n if (typeof btoa !== 'undefined') {\n return btoa(String.fromCharCode.apply(null, data));\n }\n throw new OpenAIError('Cannot generate base64 string; Expected `Buffer` or `btoa` to be defined');\n};\nexport const fromBase64 = (str) => {\n if (typeof globalThis.Buffer !== 'undefined') {\n const buf = globalThis.Buffer.from(str, 'base64');\n return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);\n }\n if (typeof atob !== 'undefined') {\n const bstr = atob(str);\n const buf = new Uint8Array(bstr.length);\n for (let i = 0; i < bstr.length; i++) {\n buf[i] = bstr.charCodeAt(i);\n }\n return buf;\n }\n throw new OpenAIError('Cannot decode base64 string; Expected `Buffer` or `atob` to be defined');\n};\n/**\n * Converts a Base64 encoded string to a Float32Array.\n * @param base64Str - The Base64 encoded string.\n * @returns An Array of numbers interpreted as Float32 values.\n */\nexport const toFloat32Array = (base64Str) => {\n if (typeof Buffer !== 'undefined') {\n // for Node.js environment\n const buf = Buffer.from(base64Str, 'base64');\n return Array.from(new Float32Array(buf.buffer, buf.byteOffset, buf.length / Float32Array.BYTES_PER_ELEMENT));\n }\n else {\n // for legacy web platform APIs\n const binaryStr = atob(base64Str);\n const len = binaryStr.length;\n const bytes = new Uint8Array(len);\n for (let i = 0; i < len; i++) {\n bytes[i] = binaryStr.charCodeAt(i);\n }\n return Array.from(new Float32Array(bytes.buffer));\n }\n};\n//# sourceMappingURL=base64.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\n/**\n * Read an environment variable.\n *\n * Trims beginning and trailing whitespace.\n *\n * Will return undefined if the environment variable doesn't exist or cannot be accessed.\n */\nexport const readEnv = (env) => {\n if (typeof globalThis.process !== 'undefined') {\n return globalThis.process.env?.[env]?.trim() ?? undefined;\n }\n if (typeof globalThis.Deno !== 'undefined') {\n return globalThis.Deno.env?.get?.(env)?.trim();\n }\n return undefined;\n};\n//# sourceMappingURL=env.mjs.map","var _AssistantStream_instances, _a, _AssistantStream_events, _AssistantStream_runStepSnapshots, _AssistantStream_messageSnapshots, _AssistantStream_messageSnapshot, _AssistantStream_finalRun, _AssistantStream_currentContentIndex, _AssistantStream_currentContent, _AssistantStream_currentToolCallIndex, _AssistantStream_currentToolCall, _AssistantStream_currentEvent, _AssistantStream_currentRunSnapshot, _AssistantStream_currentRunStepSnapshot, _AssistantStream_addEvent, _AssistantStream_endRequest, _AssistantStream_handleMessage, _AssistantStream_handleRunStep, _AssistantStream_handleEvent, _AssistantStream_accumulateRunStep, _AssistantStream_accumulateMessage, _AssistantStream_accumulateContent, _AssistantStream_handleRun;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"../internal/tslib.mjs\";\nimport { Stream } from \"../streaming.mjs\";\nimport { APIUserAbortError, OpenAIError } from \"../error.mjs\";\nimport { EventStream } from \"./EventStream.mjs\";\nimport { isObj } from \"../internal/utils.mjs\";\nexport class AssistantStream extends EventStream {\n constructor() {\n super(...arguments);\n _AssistantStream_instances.add(this);\n //Track all events in a single list for reference\n _AssistantStream_events.set(this, []);\n //Used to accumulate deltas\n //We are accumulating many types so the value here is not strict\n _AssistantStream_runStepSnapshots.set(this, {});\n _AssistantStream_messageSnapshots.set(this, {});\n _AssistantStream_messageSnapshot.set(this, void 0);\n _AssistantStream_finalRun.set(this, void 0);\n _AssistantStream_currentContentIndex.set(this, void 0);\n _AssistantStream_currentContent.set(this, void 0);\n _AssistantStream_currentToolCallIndex.set(this, void 0);\n _AssistantStream_currentToolCall.set(this, void 0);\n //For current snapshot methods\n _AssistantStream_currentEvent.set(this, void 0);\n _AssistantStream_currentRunSnapshot.set(this, void 0);\n _AssistantStream_currentRunStepSnapshot.set(this, void 0);\n }\n [(_AssistantStream_events = new WeakMap(), _AssistantStream_runStepSnapshots = new WeakMap(), _AssistantStream_messageSnapshots = new WeakMap(), _AssistantStream_messageSnapshot = new WeakMap(), _AssistantStream_finalRun = new WeakMap(), _AssistantStream_currentContentIndex = new WeakMap(), _AssistantStream_currentContent = new WeakMap(), _AssistantStream_currentToolCallIndex = new WeakMap(), _AssistantStream_currentToolCall = new WeakMap(), _AssistantStream_currentEvent = new WeakMap(), _AssistantStream_currentRunSnapshot = new WeakMap(), _AssistantStream_currentRunStepSnapshot = new WeakMap(), _AssistantStream_instances = new WeakSet(), Symbol.asyncIterator)]() {\n const pushQueue = [];\n const readQueue = [];\n let done = false;\n //Catch all for passing along all events\n this.on('event', (event) => {\n const reader = readQueue.shift();\n if (reader) {\n reader.resolve(event);\n }\n else {\n pushQueue.push(event);\n }\n });\n this.on('end', () => {\n done = true;\n for (const reader of readQueue) {\n reader.resolve(undefined);\n }\n readQueue.length = 0;\n });\n this.on('abort', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n this.on('error', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n return {\n next: async () => {\n if (!pushQueue.length) {\n if (done) {\n return { value: undefined, done: true };\n }\n return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((chunk) => (chunk ? { value: chunk, done: false } : { value: undefined, done: true }));\n }\n const chunk = pushQueue.shift();\n return { value: chunk, done: false };\n },\n return: async () => {\n this.abort();\n return { value: undefined, done: true };\n },\n };\n }\n static fromReadableStream(stream) {\n const runner = new _a();\n runner._run(() => runner._fromReadableStream(stream));\n return runner;\n }\n async _fromReadableStream(readableStream, options) {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted)\n this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n this._connected();\n const stream = Stream.fromReadableStream(readableStream, this.controller);\n for await (const event of stream) {\n __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_addEvent).call(this, event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n return this._addRun(__classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_endRequest).call(this));\n }\n toReadableStream() {\n const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);\n return stream.toReadableStream();\n }\n static createToolAssistantStream(runId, runs, params, options) {\n const runner = new _a();\n runner._run(() => runner._runToolAssistantStream(runId, runs, params, {\n ...options,\n headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },\n }));\n return runner;\n }\n async _createToolAssistantStream(run, runId, params, options) {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted)\n this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n const body = { ...params, stream: true };\n const stream = await run.submitToolOutputs(runId, body, {\n ...options,\n signal: this.controller.signal,\n });\n this._connected();\n for await (const event of stream) {\n __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_addEvent).call(this, event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n return this._addRun(__classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_endRequest).call(this));\n }\n static createThreadAssistantStream(params, thread, options) {\n const runner = new _a();\n runner._run(() => runner._threadAssistantStream(params, thread, {\n ...options,\n headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },\n }));\n return runner;\n }\n static createAssistantStream(threadId, runs, params, options) {\n const runner = new _a();\n runner._run(() => runner._runAssistantStream(threadId, runs, params, {\n ...options,\n headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },\n }));\n return runner;\n }\n currentEvent() {\n return __classPrivateFieldGet(this, _AssistantStream_currentEvent, \"f\");\n }\n currentRun() {\n return __classPrivateFieldGet(this, _AssistantStream_currentRunSnapshot, \"f\");\n }\n currentMessageSnapshot() {\n return __classPrivateFieldGet(this, _AssistantStream_messageSnapshot, \"f\");\n }\n currentRunStepSnapshot() {\n return __classPrivateFieldGet(this, _AssistantStream_currentRunStepSnapshot, \"f\");\n }\n async finalRunSteps() {\n await this.done();\n return Object.values(__classPrivateFieldGet(this, _AssistantStream_runStepSnapshots, \"f\"));\n }\n async finalMessages() {\n await this.done();\n return Object.values(__classPrivateFieldGet(this, _AssistantStream_messageSnapshots, \"f\"));\n }\n async finalRun() {\n await this.done();\n if (!__classPrivateFieldGet(this, _AssistantStream_finalRun, \"f\"))\n throw Error('Final run was not received.');\n return __classPrivateFieldGet(this, _AssistantStream_finalRun, \"f\");\n }\n async _createThreadAssistantStream(thread, params, options) {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted)\n this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n const body = { ...params, stream: true };\n const stream = await thread.createAndRun(body, { ...options, signal: this.controller.signal });\n this._connected();\n for await (const event of stream) {\n __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_addEvent).call(this, event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n return this._addRun(__classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_endRequest).call(this));\n }\n async _createAssistantStream(run, threadId, params, options) {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted)\n this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n const body = { ...params, stream: true };\n const stream = await run.create(threadId, body, { ...options, signal: this.controller.signal });\n this._connected();\n for await (const event of stream) {\n __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_addEvent).call(this, event);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n return this._addRun(__classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_endRequest).call(this));\n }\n static accumulateDelta(acc, delta) {\n for (const [key, deltaValue] of Object.entries(delta)) {\n if (!acc.hasOwnProperty(key)) {\n acc[key] = deltaValue;\n continue;\n }\n let accValue = acc[key];\n if (accValue === null || accValue === undefined) {\n acc[key] = deltaValue;\n continue;\n }\n // We don't accumulate these special properties\n if (key === 'index' || key === 'type') {\n acc[key] = deltaValue;\n continue;\n }\n // Type-specific accumulation logic\n if (typeof accValue === 'string' && typeof deltaValue === 'string') {\n accValue += deltaValue;\n }\n else if (typeof accValue === 'number' && typeof deltaValue === 'number') {\n accValue += deltaValue;\n }\n else if (isObj(accValue) && isObj(deltaValue)) {\n accValue = this.accumulateDelta(accValue, deltaValue);\n }\n else if (Array.isArray(accValue) && Array.isArray(deltaValue)) {\n if (accValue.every((x) => typeof x === 'string' || typeof x === 'number')) {\n accValue.push(...deltaValue); // Use spread syntax for efficient addition\n continue;\n }\n for (const deltaEntry of deltaValue) {\n if (!isObj(deltaEntry)) {\n throw new Error(`Expected array delta entry to be an object but got: ${deltaEntry}`);\n }\n const index = deltaEntry['index'];\n if (index == null) {\n console.error(deltaEntry);\n throw new Error('Expected array delta entry to have an `index` property');\n }\n if (typeof index !== 'number') {\n throw new Error(`Expected array delta entry \\`index\\` property to be a number but got ${index}`);\n }\n const accEntry = accValue[index];\n if (accEntry == null) {\n accValue.push(deltaEntry);\n }\n else {\n accValue[index] = this.accumulateDelta(accEntry, deltaEntry);\n }\n }\n continue;\n }\n else {\n throw Error(`Unhandled record type: ${key}, deltaValue: ${deltaValue}, accValue: ${accValue}`);\n }\n acc[key] = accValue;\n }\n return acc;\n }\n _addRun(run) {\n return run;\n }\n async _threadAssistantStream(params, thread, options) {\n return await this._createThreadAssistantStream(thread, params, options);\n }\n async _runAssistantStream(threadId, runs, params, options) {\n return await this._createAssistantStream(runs, threadId, params, options);\n }\n async _runToolAssistantStream(runId, runs, params, options) {\n return await this._createToolAssistantStream(runs, runId, params, options);\n }\n}\n_a = AssistantStream, _AssistantStream_addEvent = function _AssistantStream_addEvent(event) {\n if (this.ended)\n return;\n __classPrivateFieldSet(this, _AssistantStream_currentEvent, event, \"f\");\n __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_handleEvent).call(this, event);\n switch (event.event) {\n case 'thread.created':\n //No action on this event.\n break;\n case 'thread.run.created':\n case 'thread.run.queued':\n case 'thread.run.in_progress':\n case 'thread.run.requires_action':\n case 'thread.run.completed':\n case 'thread.run.incomplete':\n case 'thread.run.failed':\n case 'thread.run.cancelling':\n case 'thread.run.cancelled':\n case 'thread.run.expired':\n __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_handleRun).call(this, event);\n break;\n case 'thread.run.step.created':\n case 'thread.run.step.in_progress':\n case 'thread.run.step.delta':\n case 'thread.run.step.completed':\n case 'thread.run.step.failed':\n case 'thread.run.step.cancelled':\n case 'thread.run.step.expired':\n __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_handleRunStep).call(this, event);\n break;\n case 'thread.message.created':\n case 'thread.message.in_progress':\n case 'thread.message.delta':\n case 'thread.message.completed':\n case 'thread.message.incomplete':\n __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_handleMessage).call(this, event);\n break;\n case 'error':\n //This is included for completeness, but errors are processed in the SSE event processing so this should not occur\n throw new Error('Encountered an error event in event processing - errors should be processed earlier');\n default:\n assertNever(event);\n }\n}, _AssistantStream_endRequest = function _AssistantStream_endRequest() {\n if (this.ended) {\n throw new OpenAIError(`stream has ended, this shouldn't happen`);\n }\n if (!__classPrivateFieldGet(this, _AssistantStream_finalRun, \"f\"))\n throw Error('Final run has not been received');\n return __classPrivateFieldGet(this, _AssistantStream_finalRun, \"f\");\n}, _AssistantStream_handleMessage = function _AssistantStream_handleMessage(event) {\n const [accumulatedMessage, newContent] = __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_accumulateMessage).call(this, event, __classPrivateFieldGet(this, _AssistantStream_messageSnapshot, \"f\"));\n __classPrivateFieldSet(this, _AssistantStream_messageSnapshot, accumulatedMessage, \"f\");\n __classPrivateFieldGet(this, _AssistantStream_messageSnapshots, \"f\")[accumulatedMessage.id] = accumulatedMessage;\n for (const content of newContent) {\n const snapshotContent = accumulatedMessage.content[content.index];\n if (snapshotContent?.type == 'text') {\n this._emit('textCreated', snapshotContent.text);\n }\n }\n switch (event.event) {\n case 'thread.message.created':\n this._emit('messageCreated', event.data);\n break;\n case 'thread.message.in_progress':\n break;\n case 'thread.message.delta':\n this._emit('messageDelta', event.data.delta, accumulatedMessage);\n if (event.data.delta.content) {\n for (const content of event.data.delta.content) {\n //If it is text delta, emit a text delta event\n if (content.type == 'text' && content.text) {\n let textDelta = content.text;\n let snapshot = accumulatedMessage.content[content.index];\n if (snapshot && snapshot.type == 'text') {\n this._emit('textDelta', textDelta, snapshot.text);\n }\n else {\n throw Error('The snapshot associated with this text delta is not text or missing');\n }\n }\n if (content.index != __classPrivateFieldGet(this, _AssistantStream_currentContentIndex, \"f\")) {\n //See if we have in progress content\n if (__classPrivateFieldGet(this, _AssistantStream_currentContent, \"f\")) {\n switch (__classPrivateFieldGet(this, _AssistantStream_currentContent, \"f\").type) {\n case 'text':\n this._emit('textDone', __classPrivateFieldGet(this, _AssistantStream_currentContent, \"f\").text, __classPrivateFieldGet(this, _AssistantStream_messageSnapshot, \"f\"));\n break;\n case 'image_file':\n this._emit('imageFileDone', __classPrivateFieldGet(this, _AssistantStream_currentContent, \"f\").image_file, __classPrivateFieldGet(this, _AssistantStream_messageSnapshot, \"f\"));\n break;\n }\n }\n __classPrivateFieldSet(this, _AssistantStream_currentContentIndex, content.index, \"f\");\n }\n __classPrivateFieldSet(this, _AssistantStream_currentContent, accumulatedMessage.content[content.index], \"f\");\n }\n }\n break;\n case 'thread.message.completed':\n case 'thread.message.incomplete':\n //We emit the latest content we were working on on completion (including incomplete)\n if (__classPrivateFieldGet(this, _AssistantStream_currentContentIndex, \"f\") !== undefined) {\n const currentContent = event.data.content[__classPrivateFieldGet(this, _AssistantStream_currentContentIndex, \"f\")];\n if (currentContent) {\n switch (currentContent.type) {\n case 'image_file':\n this._emit('imageFileDone', currentContent.image_file, __classPrivateFieldGet(this, _AssistantStream_messageSnapshot, \"f\"));\n break;\n case 'text':\n this._emit('textDone', currentContent.text, __classPrivateFieldGet(this, _AssistantStream_messageSnapshot, \"f\"));\n break;\n }\n }\n }\n if (__classPrivateFieldGet(this, _AssistantStream_messageSnapshot, \"f\")) {\n this._emit('messageDone', event.data);\n }\n __classPrivateFieldSet(this, _AssistantStream_messageSnapshot, undefined, \"f\");\n }\n}, _AssistantStream_handleRunStep = function _AssistantStream_handleRunStep(event) {\n const accumulatedRunStep = __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_accumulateRunStep).call(this, event);\n __classPrivateFieldSet(this, _AssistantStream_currentRunStepSnapshot, accumulatedRunStep, \"f\");\n switch (event.event) {\n case 'thread.run.step.created':\n this._emit('runStepCreated', event.data);\n break;\n case 'thread.run.step.delta':\n const delta = event.data.delta;\n if (delta.step_details &&\n delta.step_details.type == 'tool_calls' &&\n delta.step_details.tool_calls &&\n accumulatedRunStep.step_details.type == 'tool_calls') {\n for (const toolCall of delta.step_details.tool_calls) {\n if (toolCall.index == __classPrivateFieldGet(this, _AssistantStream_currentToolCallIndex, \"f\")) {\n this._emit('toolCallDelta', toolCall, accumulatedRunStep.step_details.tool_calls[toolCall.index]);\n }\n else {\n if (__classPrivateFieldGet(this, _AssistantStream_currentToolCall, \"f\")) {\n this._emit('toolCallDone', __classPrivateFieldGet(this, _AssistantStream_currentToolCall, \"f\"));\n }\n __classPrivateFieldSet(this, _AssistantStream_currentToolCallIndex, toolCall.index, \"f\");\n __classPrivateFieldSet(this, _AssistantStream_currentToolCall, accumulatedRunStep.step_details.tool_calls[toolCall.index], \"f\");\n if (__classPrivateFieldGet(this, _AssistantStream_currentToolCall, \"f\"))\n this._emit('toolCallCreated', __classPrivateFieldGet(this, _AssistantStream_currentToolCall, \"f\"));\n }\n }\n }\n this._emit('runStepDelta', event.data.delta, accumulatedRunStep);\n break;\n case 'thread.run.step.completed':\n case 'thread.run.step.failed':\n case 'thread.run.step.cancelled':\n case 'thread.run.step.expired':\n __classPrivateFieldSet(this, _AssistantStream_currentRunStepSnapshot, undefined, \"f\");\n const details = event.data.step_details;\n if (details.type == 'tool_calls') {\n if (__classPrivateFieldGet(this, _AssistantStream_currentToolCall, \"f\")) {\n this._emit('toolCallDone', __classPrivateFieldGet(this, _AssistantStream_currentToolCall, \"f\"));\n __classPrivateFieldSet(this, _AssistantStream_currentToolCall, undefined, \"f\");\n }\n }\n this._emit('runStepDone', event.data, accumulatedRunStep);\n break;\n case 'thread.run.step.in_progress':\n break;\n }\n}, _AssistantStream_handleEvent = function _AssistantStream_handleEvent(event) {\n __classPrivateFieldGet(this, _AssistantStream_events, \"f\").push(event);\n this._emit('event', event);\n}, _AssistantStream_accumulateRunStep = function _AssistantStream_accumulateRunStep(event) {\n switch (event.event) {\n case 'thread.run.step.created':\n __classPrivateFieldGet(this, _AssistantStream_runStepSnapshots, \"f\")[event.data.id] = event.data;\n return event.data;\n case 'thread.run.step.delta':\n let snapshot = __classPrivateFieldGet(this, _AssistantStream_runStepSnapshots, \"f\")[event.data.id];\n if (!snapshot) {\n throw Error('Received a RunStepDelta before creation of a snapshot');\n }\n let data = event.data;\n if (data.delta) {\n const accumulated = _a.accumulateDelta(snapshot, data.delta);\n __classPrivateFieldGet(this, _AssistantStream_runStepSnapshots, \"f\")[event.data.id] = accumulated;\n }\n return __classPrivateFieldGet(this, _AssistantStream_runStepSnapshots, \"f\")[event.data.id];\n case 'thread.run.step.completed':\n case 'thread.run.step.failed':\n case 'thread.run.step.cancelled':\n case 'thread.run.step.expired':\n case 'thread.run.step.in_progress':\n __classPrivateFieldGet(this, _AssistantStream_runStepSnapshots, \"f\")[event.data.id] = event.data;\n break;\n }\n if (__classPrivateFieldGet(this, _AssistantStream_runStepSnapshots, \"f\")[event.data.id])\n return __classPrivateFieldGet(this, _AssistantStream_runStepSnapshots, \"f\")[event.data.id];\n throw new Error('No snapshot available');\n}, _AssistantStream_accumulateMessage = function _AssistantStream_accumulateMessage(event, snapshot) {\n let newContent = [];\n switch (event.event) {\n case 'thread.message.created':\n //On creation the snapshot is just the initial message\n return [event.data, newContent];\n case 'thread.message.delta':\n if (!snapshot) {\n throw Error('Received a delta with no existing snapshot (there should be one from message creation)');\n }\n let data = event.data;\n //If this delta does not have content, nothing to process\n if (data.delta.content) {\n for (const contentElement of data.delta.content) {\n if (contentElement.index in snapshot.content) {\n let currentContent = snapshot.content[contentElement.index];\n snapshot.content[contentElement.index] = __classPrivateFieldGet(this, _AssistantStream_instances, \"m\", _AssistantStream_accumulateContent).call(this, contentElement, currentContent);\n }\n else {\n snapshot.content[contentElement.index] = contentElement;\n // This is a new element\n newContent.push(contentElement);\n }\n }\n }\n return [snapshot, newContent];\n case 'thread.message.in_progress':\n case 'thread.message.completed':\n case 'thread.message.incomplete':\n //No changes on other thread events\n if (snapshot) {\n return [snapshot, newContent];\n }\n else {\n throw Error('Received thread message event with no existing snapshot');\n }\n }\n throw Error('Tried to accumulate a non-message event');\n}, _AssistantStream_accumulateContent = function _AssistantStream_accumulateContent(contentElement, currentContent) {\n return _a.accumulateDelta(currentContent, contentElement);\n}, _AssistantStream_handleRun = function _AssistantStream_handleRun(event) {\n __classPrivateFieldSet(this, _AssistantStream_currentRunSnapshot, event.data, \"f\");\n switch (event.event) {\n case 'thread.run.created':\n break;\n case 'thread.run.queued':\n break;\n case 'thread.run.in_progress':\n break;\n case 'thread.run.requires_action':\n case 'thread.run.cancelled':\n case 'thread.run.failed':\n case 'thread.run.completed':\n case 'thread.run.expired':\n case 'thread.run.incomplete':\n __classPrivateFieldSet(this, _AssistantStream_finalRun, event.data, \"f\");\n if (__classPrivateFieldGet(this, _AssistantStream_currentToolCall, \"f\")) {\n this._emit('toolCallDone', __classPrivateFieldGet(this, _AssistantStream_currentToolCall, \"f\"));\n __classPrivateFieldSet(this, _AssistantStream_currentToolCall, undefined, \"f\");\n }\n break;\n case 'thread.run.cancelling':\n break;\n }\n};\nfunction assertNever(_x) { }\n//# sourceMappingURL=AssistantStream.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../../core/resource.mjs\";\nimport * as StepsAPI from \"./steps.mjs\";\nimport { Steps, } from \"./steps.mjs\";\nimport { CursorPage } from \"../../../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../../../internal/headers.mjs\";\nimport { AssistantStream } from \"../../../../lib/AssistantStream.mjs\";\nimport { sleep } from \"../../../../internal/utils/sleep.mjs\";\nimport { path } from \"../../../../internal/utils/path.mjs\";\n/**\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\nexport class Runs extends APIResource {\n constructor() {\n super(...arguments);\n this.steps = new StepsAPI.Steps(this._client);\n }\n create(threadID, params, options) {\n const { include, ...body } = params;\n return this._client.post(path `/threads/${threadID}/runs`, {\n query: { include },\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n stream: params.stream ?? false,\n });\n }\n /**\n * Retrieves a run.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n retrieve(runID, params, options) {\n const { thread_id } = params;\n return this._client.get(path `/threads/${thread_id}/runs/${runID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Modifies a run.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n update(runID, params, options) {\n const { thread_id, ...body } = params;\n return this._client.post(path `/threads/${thread_id}/runs/${runID}`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Returns a list of runs belonging to a thread.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n list(threadID, query = {}, options) {\n return this._client.getAPIList(path `/threads/${threadID}/runs`, (CursorPage), {\n query,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Cancels a run that is `in_progress`.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n cancel(runID, params, options) {\n const { thread_id } = params;\n return this._client.post(path `/threads/${thread_id}/runs/${runID}/cancel`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * A helper to create a run an poll for a terminal state. More information on Run\n * lifecycles can be found here:\n * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps\n */\n async createAndPoll(threadId, body, options) {\n const run = await this.create(threadId, body, options);\n return await this.poll(run.id, { thread_id: threadId }, options);\n }\n /**\n * Create a Run stream\n *\n * @deprecated use `stream` instead\n */\n createAndStream(threadId, body, options) {\n return AssistantStream.createAssistantStream(threadId, this._client.beta.threads.runs, body, options);\n }\n /**\n * A helper to poll a run status until it reaches a terminal state. More\n * information on Run lifecycles can be found here:\n * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps\n */\n async poll(runId, params, options) {\n const headers = buildHeaders([\n options?.headers,\n {\n 'X-Stainless-Poll-Helper': 'true',\n 'X-Stainless-Custom-Poll-Interval': options?.pollIntervalMs?.toString() ?? undefined,\n },\n ]);\n while (true) {\n const { data: run, response } = await this.retrieve(runId, params, {\n ...options,\n headers: { ...options?.headers, ...headers },\n }).withResponse();\n switch (run.status) {\n //If we are in any sort of intermediate state we poll\n case 'queued':\n case 'in_progress':\n case 'cancelling':\n let sleepInterval = 5000;\n if (options?.pollIntervalMs) {\n sleepInterval = options.pollIntervalMs;\n }\n else {\n const headerInterval = response.headers.get('openai-poll-after-ms');\n if (headerInterval) {\n const headerIntervalMs = parseInt(headerInterval);\n if (!isNaN(headerIntervalMs)) {\n sleepInterval = headerIntervalMs;\n }\n }\n }\n await sleep(sleepInterval);\n break;\n //We return the run in any terminal state.\n case 'requires_action':\n case 'incomplete':\n case 'cancelled':\n case 'completed':\n case 'failed':\n case 'expired':\n return run;\n }\n }\n }\n /**\n * Create a Run stream\n */\n stream(threadId, body, options) {\n return AssistantStream.createAssistantStream(threadId, this._client.beta.threads.runs, body, options);\n }\n submitToolOutputs(runID, params, options) {\n const { thread_id, ...body } = params;\n return this._client.post(path `/threads/${thread_id}/runs/${runID}/submit_tool_outputs`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n stream: params.stream ?? false,\n });\n }\n /**\n * A helper to submit a tool output to a run and poll for a terminal run state.\n * More information on Run lifecycles can be found here:\n * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps\n */\n async submitToolOutputsAndPoll(runId, params, options) {\n const run = await this.submitToolOutputs(runId, params, options);\n return await this.poll(run.id, params, options);\n }\n /**\n * Submit the tool outputs from a previous run and stream the run to a terminal\n * state. More information on Run lifecycles can be found here:\n * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps\n */\n submitToolOutputsStream(runId, params, options) {\n return AssistantStream.createToolAssistantStream(runId, this._client.beta.threads.runs, params, options);\n }\n}\nRuns.Steps = Steps;\n//# sourceMappingURL=runs.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as MessagesAPI from \"./messages.mjs\";\nimport { Messages, } from \"./messages.mjs\";\nimport * as RunsAPI from \"./runs/runs.mjs\";\nimport { Runs, } from \"./runs/runs.mjs\";\nimport { buildHeaders } from \"../../../internal/headers.mjs\";\nimport { AssistantStream } from \"../../../lib/AssistantStream.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\n/**\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\nexport class Threads extends APIResource {\n constructor() {\n super(...arguments);\n this.runs = new RunsAPI.Runs(this._client);\n this.messages = new MessagesAPI.Messages(this._client);\n }\n /**\n * Create a thread.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n create(body = {}, options) {\n return this._client.post('/threads', {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Retrieves a thread.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n retrieve(threadID, options) {\n return this._client.get(path `/threads/${threadID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Modifies a thread.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n update(threadID, body, options) {\n return this._client.post(path `/threads/${threadID}`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Delete a thread.\n *\n * @deprecated The Assistants API is deprecated in favor of the Responses API\n */\n delete(threadID, options) {\n return this._client.delete(path `/threads/${threadID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n createAndRun(body, options) {\n return this._client.post('/threads/runs', {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n stream: body.stream ?? false,\n });\n }\n /**\n * A helper to create a thread, start a run and then poll for a terminal state.\n * More information on Run lifecycles can be found here:\n * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps\n */\n async createAndRunPoll(body, options) {\n const run = await this.createAndRun(body, options);\n return await this.runs.poll(run.id, { thread_id: run.thread_id }, options);\n }\n /**\n * Create a thread and stream the run back\n */\n createAndRunStream(body, options) {\n return AssistantStream.createThreadAssistantStream(body, this._client.beta.threads, options);\n }\n}\nThreads.Runs = Runs;\nThreads.Messages = Messages;\n//# sourceMappingURL=threads.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as AssistantsAPI from \"./assistants.mjs\";\nimport { Assistants, } from \"./assistants.mjs\";\nimport * as RealtimeAPI from \"./realtime/realtime.mjs\";\nimport { Realtime, } from \"./realtime/realtime.mjs\";\nimport * as ChatKitAPI from \"./chatkit/chatkit.mjs\";\nimport { ChatKit } from \"./chatkit/chatkit.mjs\";\nimport * as ThreadsAPI from \"./threads/threads.mjs\";\nimport { Threads, } from \"./threads/threads.mjs\";\nexport class Beta extends APIResource {\n constructor() {\n super(...arguments);\n this.realtime = new RealtimeAPI.Realtime(this._client);\n this.chatkit = new ChatKitAPI.ChatKit(this._client);\n this.assistants = new AssistantsAPI.Assistants(this._client);\n this.threads = new ThreadsAPI.Threads(this._client);\n }\n}\nBeta.Realtime = Realtime;\nBeta.ChatKit = ChatKit;\nBeta.Assistants = Assistants;\nBeta.Threads = Threads;\n//# sourceMappingURL=beta.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../core/resource.mjs\";\nexport class Completions extends APIResource {\n create(body, options) {\n return this._client.post('/completions', { body, ...options, stream: body.stream ?? false });\n }\n}\n//# sourceMappingURL=completions.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { buildHeaders } from \"../../../internal/headers.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Content extends APIResource {\n /**\n * Retrieve Container File Content\n */\n retrieve(fileID, params, options) {\n const { container_id } = params;\n return this._client.get(path `/containers/${container_id}/files/${fileID}/content`, {\n ...options,\n headers: buildHeaders([{ Accept: 'application/binary' }, options?.headers]),\n __binaryResponse: true,\n });\n }\n}\n//# sourceMappingURL=content.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as ContentAPI from \"./content.mjs\";\nimport { Content } from \"./content.mjs\";\nimport { CursorPage } from \"../../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../../internal/headers.mjs\";\nimport { multipartFormRequestOptions } from \"../../../internal/uploads.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Files extends APIResource {\n constructor() {\n super(...arguments);\n this.content = new ContentAPI.Content(this._client);\n }\n /**\n * Create a Container File\n *\n * You can send either a multipart/form-data request with the raw file content, or\n * a JSON request with a file ID.\n */\n create(containerID, body, options) {\n return this._client.post(path `/containers/${containerID}/files`, multipartFormRequestOptions({ body, ...options }, this._client));\n }\n /**\n * Retrieve Container File\n */\n retrieve(fileID, params, options) {\n const { container_id } = params;\n return this._client.get(path `/containers/${container_id}/files/${fileID}`, options);\n }\n /**\n * List Container files\n */\n list(containerID, query = {}, options) {\n return this._client.getAPIList(path `/containers/${containerID}/files`, (CursorPage), {\n query,\n ...options,\n });\n }\n /**\n * Delete Container File\n */\n delete(fileID, params, options) {\n const { container_id } = params;\n return this._client.delete(path `/containers/${container_id}/files/${fileID}`, {\n ...options,\n headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),\n });\n }\n}\nFiles.Content = Content;\n//# sourceMappingURL=files.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as FilesAPI from \"./files/files.mjs\";\nimport { Files, } from \"./files/files.mjs\";\nimport { CursorPage } from \"../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../internal/headers.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Containers extends APIResource {\n constructor() {\n super(...arguments);\n this.files = new FilesAPI.Files(this._client);\n }\n /**\n * Create Container\n */\n create(body, options) {\n return this._client.post('/containers', { body, ...options });\n }\n /**\n * Retrieve Container\n */\n retrieve(containerID, options) {\n return this._client.get(path `/containers/${containerID}`, options);\n }\n /**\n * List Containers\n */\n list(query = {}, options) {\n return this._client.getAPIList('/containers', (CursorPage), { query, ...options });\n }\n /**\n * Delete Container\n */\n delete(containerID, options) {\n return this._client.delete(path `/containers/${containerID}`, {\n ...options,\n headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),\n });\n }\n}\nContainers.Files = Files;\n//# sourceMappingURL=containers.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { ConversationCursorPage, } from \"../../core/pagination.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Items extends APIResource {\n /**\n * Create items in a conversation with the given ID.\n */\n create(conversationID, params, options) {\n const { include, ...body } = params;\n return this._client.post(path `/conversations/${conversationID}/items`, {\n query: { include },\n body,\n ...options,\n });\n }\n /**\n * Get a single item from a conversation with the given IDs.\n */\n retrieve(itemID, params, options) {\n const { conversation_id, ...query } = params;\n return this._client.get(path `/conversations/${conversation_id}/items/${itemID}`, { query, ...options });\n }\n /**\n * List all items for a conversation with the given ID.\n */\n list(conversationID, query = {}, options) {\n return this._client.getAPIList(path `/conversations/${conversationID}/items`, (ConversationCursorPage), { query, ...options });\n }\n /**\n * Delete an item from a conversation with the given IDs.\n */\n delete(itemID, params, options) {\n const { conversation_id } = params;\n return this._client.delete(path `/conversations/${conversation_id}/items/${itemID}`, options);\n }\n}\n//# sourceMappingURL=items.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as ItemsAPI from \"./items.mjs\";\nimport { Items, } from \"./items.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Conversations extends APIResource {\n constructor() {\n super(...arguments);\n this.items = new ItemsAPI.Items(this._client);\n }\n /**\n * Create a conversation.\n */\n create(body = {}, options) {\n return this._client.post('/conversations', { body, ...options });\n }\n /**\n * Get a conversation\n */\n retrieve(conversationID, options) {\n return this._client.get(path `/conversations/${conversationID}`, options);\n }\n /**\n * Update a conversation\n */\n update(conversationID, body, options) {\n return this._client.post(path `/conversations/${conversationID}`, { body, ...options });\n }\n /**\n * Delete a conversation. Items in the conversation will not be deleted.\n */\n delete(conversationID, options) {\n return this._client.delete(path `/conversations/${conversationID}`, options);\n }\n}\nConversations.Items = Items;\n//# sourceMappingURL=conversations.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../core/resource.mjs\";\nimport { loggerFor, toFloat32Array } from \"../internal/utils.mjs\";\nexport class Embeddings extends APIResource {\n /**\n * Creates an embedding vector representing the input text.\n *\n * @example\n * ```ts\n * const createEmbeddingResponse =\n * await client.embeddings.create({\n * input: 'The quick brown fox jumped over the lazy dog',\n * model: 'text-embedding-3-small',\n * });\n * ```\n */\n create(body, options) {\n const hasUserProvidedEncodingFormat = !!body.encoding_format;\n // No encoding_format specified, defaulting to base64 for performance reasons\n // See https://github.com/openai/openai-node/pull/1312\n let encoding_format = hasUserProvidedEncodingFormat ? body.encoding_format : 'base64';\n if (hasUserProvidedEncodingFormat) {\n loggerFor(this._client).debug('embeddings/user defined encoding_format:', body.encoding_format);\n }\n const response = this._client.post('/embeddings', {\n body: {\n ...body,\n encoding_format: encoding_format,\n },\n ...options,\n });\n // if the user specified an encoding_format, return the response as-is\n if (hasUserProvidedEncodingFormat) {\n return response;\n }\n // in this stage, we are sure the user did not specify an encoding_format\n // and we defaulted to base64 for performance reasons\n // we are sure then that the response is base64 encoded, let's decode it\n // the returned result will be a float32 array since this is OpenAI API's default encoding\n loggerFor(this._client).debug('embeddings/decoding base64 embeddings from base64');\n return response._thenUnwrap((response) => {\n if (response && response.data) {\n response.data.forEach((embeddingBase64Obj) => {\n const embeddingBase64Str = embeddingBase64Obj.embedding;\n embeddingBase64Obj.embedding = toFloat32Array(embeddingBase64Str);\n });\n }\n return response;\n });\n }\n}\n//# sourceMappingURL=embeddings.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { CursorPage } from \"../../../core/pagination.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class OutputItems extends APIResource {\n /**\n * Get an evaluation run output item by ID.\n */\n retrieve(outputItemID, params, options) {\n const { eval_id, run_id } = params;\n return this._client.get(path `/evals/${eval_id}/runs/${run_id}/output_items/${outputItemID}`, options);\n }\n /**\n * Get a list of output items for an evaluation run.\n */\n list(runID, params, options) {\n const { eval_id, ...query } = params;\n return this._client.getAPIList(path `/evals/${eval_id}/runs/${runID}/output_items`, (CursorPage), { query, ...options });\n }\n}\n//# sourceMappingURL=output-items.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as OutputItemsAPI from \"./output-items.mjs\";\nimport { OutputItems, } from \"./output-items.mjs\";\nimport { CursorPage } from \"../../../core/pagination.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Runs extends APIResource {\n constructor() {\n super(...arguments);\n this.outputItems = new OutputItemsAPI.OutputItems(this._client);\n }\n /**\n * Kicks off a new run for a given evaluation, specifying the data source, and what\n * model configuration to use to test. The datasource will be validated against the\n * schema specified in the config of the evaluation.\n */\n create(evalID, body, options) {\n return this._client.post(path `/evals/${evalID}/runs`, { body, ...options });\n }\n /**\n * Get an evaluation run by ID.\n */\n retrieve(runID, params, options) {\n const { eval_id } = params;\n return this._client.get(path `/evals/${eval_id}/runs/${runID}`, options);\n }\n /**\n * Get a list of runs for an evaluation.\n */\n list(evalID, query = {}, options) {\n return this._client.getAPIList(path `/evals/${evalID}/runs`, (CursorPage), {\n query,\n ...options,\n });\n }\n /**\n * Delete an eval run.\n */\n delete(runID, params, options) {\n const { eval_id } = params;\n return this._client.delete(path `/evals/${eval_id}/runs/${runID}`, options);\n }\n /**\n * Cancel an ongoing evaluation run.\n */\n cancel(runID, params, options) {\n const { eval_id } = params;\n return this._client.post(path `/evals/${eval_id}/runs/${runID}`, options);\n }\n}\nRuns.OutputItems = OutputItems;\n//# sourceMappingURL=runs.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as RunsAPI from \"./runs/runs.mjs\";\nimport { Runs, } from \"./runs/runs.mjs\";\nimport { CursorPage } from \"../../core/pagination.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Evals extends APIResource {\n constructor() {\n super(...arguments);\n this.runs = new RunsAPI.Runs(this._client);\n }\n /**\n * Create the structure of an evaluation that can be used to test a model's\n * performance. An evaluation is a set of testing criteria and the config for a\n * data source, which dictates the schema of the data used in the evaluation. After\n * creating an evaluation, you can run it on different models and model parameters.\n * We support several types of graders and datasources. For more information, see\n * the [Evals guide](https://platform.openai.com/docs/guides/evals).\n */\n create(body, options) {\n return this._client.post('/evals', { body, ...options });\n }\n /**\n * Get an evaluation by ID.\n */\n retrieve(evalID, options) {\n return this._client.get(path `/evals/${evalID}`, options);\n }\n /**\n * Update certain properties of an evaluation.\n */\n update(evalID, body, options) {\n return this._client.post(path `/evals/${evalID}`, { body, ...options });\n }\n /**\n * List evaluations for a project.\n */\n list(query = {}, options) {\n return this._client.getAPIList('/evals', (CursorPage), { query, ...options });\n }\n /**\n * Delete an evaluation.\n */\n delete(evalID, options) {\n return this._client.delete(path `/evals/${evalID}`, options);\n }\n}\nEvals.Runs = Runs;\n//# sourceMappingURL=evals.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../core/resource.mjs\";\nimport { CursorPage } from \"../core/pagination.mjs\";\nimport { buildHeaders } from \"../internal/headers.mjs\";\nimport { sleep } from \"../internal/utils/sleep.mjs\";\nimport { APIConnectionTimeoutError } from \"../error.mjs\";\nimport { multipartFormRequestOptions } from \"../internal/uploads.mjs\";\nimport { path } from \"../internal/utils/path.mjs\";\nexport class Files extends APIResource {\n /**\n * Upload a file that can be used across various endpoints. Individual files can be\n * up to 512 MB, and the size of all files uploaded by one organization can be up\n * to 1 TB.\n *\n * - The Assistants API supports files up to 2 million tokens and of specific file\n * types. See the\n * [Assistants Tools guide](https://platform.openai.com/docs/assistants/tools)\n * for details.\n * - The Fine-tuning API only supports `.jsonl` files. The input also has certain\n * required formats for fine-tuning\n * [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input)\n * or\n * [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input)\n * models.\n * - The Batch API only supports `.jsonl` files up to 200 MB in size. The input\n * also has a specific required\n * [format](https://platform.openai.com/docs/api-reference/batch/request-input).\n *\n * Please [contact us](https://help.openai.com/) if you need to increase these\n * storage limits.\n */\n create(body, options) {\n return this._client.post('/files', multipartFormRequestOptions({ body, ...options }, this._client));\n }\n /**\n * Returns information about a specific file.\n */\n retrieve(fileID, options) {\n return this._client.get(path `/files/${fileID}`, options);\n }\n /**\n * Returns a list of files.\n */\n list(query = {}, options) {\n return this._client.getAPIList('/files', (CursorPage), { query, ...options });\n }\n /**\n * Delete a file and remove it from all vector stores.\n */\n delete(fileID, options) {\n return this._client.delete(path `/files/${fileID}`, options);\n }\n /**\n * Returns the contents of the specified file.\n */\n content(fileID, options) {\n return this._client.get(path `/files/${fileID}/content`, {\n ...options,\n headers: buildHeaders([{ Accept: 'application/binary' }, options?.headers]),\n __binaryResponse: true,\n });\n }\n /**\n * Waits for the given file to be processed, default timeout is 30 mins.\n */\n async waitForProcessing(id, { pollInterval = 5000, maxWait = 30 * 60 * 1000 } = {}) {\n const TERMINAL_STATES = new Set(['processed', 'error', 'deleted']);\n const start = Date.now();\n let file = await this.retrieve(id);\n while (!file.status || !TERMINAL_STATES.has(file.status)) {\n await sleep(pollInterval);\n file = await this.retrieve(id);\n if (Date.now() - start > maxWait) {\n throw new APIConnectionTimeoutError({\n message: `Giving up on waiting for file ${id} to finish processing after ${maxWait} milliseconds.`,\n });\n }\n }\n return file;\n }\n}\n//# sourceMappingURL=files.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nexport class Methods extends APIResource {\n}\n//# sourceMappingURL=methods.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nexport class Graders extends APIResource {\n /**\n * Run a grader.\n *\n * @example\n * ```ts\n * const response = await client.fineTuning.alpha.graders.run({\n * grader: {\n * input: 'input',\n * name: 'name',\n * operation: 'eq',\n * reference: 'reference',\n * type: 'string_check',\n * },\n * model_sample: 'model_sample',\n * });\n * ```\n */\n run(body, options) {\n return this._client.post('/fine_tuning/alpha/graders/run', { body, ...options });\n }\n /**\n * Validate a grader.\n *\n * @example\n * ```ts\n * const response =\n * await client.fineTuning.alpha.graders.validate({\n * grader: {\n * input: 'input',\n * name: 'name',\n * operation: 'eq',\n * reference: 'reference',\n * type: 'string_check',\n * },\n * });\n * ```\n */\n validate(body, options) {\n return this._client.post('/fine_tuning/alpha/graders/validate', { body, ...options });\n }\n}\n//# sourceMappingURL=graders.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as GradersAPI from \"./graders.mjs\";\nimport { Graders, } from \"./graders.mjs\";\nexport class Alpha extends APIResource {\n constructor() {\n super(...arguments);\n this.graders = new GradersAPI.Graders(this._client);\n }\n}\nAlpha.Graders = Graders;\n//# sourceMappingURL=alpha.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { Page } from \"../../../core/pagination.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Permissions extends APIResource {\n /**\n * **NOTE:** Calling this endpoint requires an [admin API key](../admin-api-keys).\n *\n * This enables organization owners to share fine-tuned models with other projects\n * in their organization.\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const permissionCreateResponse of client.fineTuning.checkpoints.permissions.create(\n * 'ft:gpt-4o-mini-2024-07-18:org:weather:B7R9VjQd',\n * { project_ids: ['string'] },\n * )) {\n * // ...\n * }\n * ```\n */\n create(fineTunedModelCheckpoint, body, options) {\n return this._client.getAPIList(path `/fine_tuning/checkpoints/${fineTunedModelCheckpoint}/permissions`, (Page), { body, method: 'post', ...options });\n }\n /**\n * **NOTE:** This endpoint requires an [admin API key](../admin-api-keys).\n *\n * Organization owners can use this endpoint to view all permissions for a\n * fine-tuned model checkpoint.\n *\n * @example\n * ```ts\n * const permission =\n * await client.fineTuning.checkpoints.permissions.retrieve(\n * 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',\n * );\n * ```\n */\n retrieve(fineTunedModelCheckpoint, query = {}, options) {\n return this._client.get(path `/fine_tuning/checkpoints/${fineTunedModelCheckpoint}/permissions`, {\n query,\n ...options,\n });\n }\n /**\n * **NOTE:** This endpoint requires an [admin API key](../admin-api-keys).\n *\n * Organization owners can use this endpoint to delete a permission for a\n * fine-tuned model checkpoint.\n *\n * @example\n * ```ts\n * const permission =\n * await client.fineTuning.checkpoints.permissions.delete(\n * 'cp_zc4Q7MP6XxulcVzj4MZdwsAB',\n * {\n * fine_tuned_model_checkpoint:\n * 'ft:gpt-4o-mini-2024-07-18:org:weather:B7R9VjQd',\n * },\n * );\n * ```\n */\n delete(permissionID, params, options) {\n const { fine_tuned_model_checkpoint } = params;\n return this._client.delete(path `/fine_tuning/checkpoints/${fine_tuned_model_checkpoint}/permissions/${permissionID}`, options);\n }\n}\n//# sourceMappingURL=permissions.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as PermissionsAPI from \"./permissions.mjs\";\nimport { Permissions, } from \"./permissions.mjs\";\nexport class Checkpoints extends APIResource {\n constructor() {\n super(...arguments);\n this.permissions = new PermissionsAPI.Permissions(this._client);\n }\n}\nCheckpoints.Permissions = Permissions;\n//# sourceMappingURL=checkpoints.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport { CursorPage } from \"../../../core/pagination.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Checkpoints extends APIResource {\n /**\n * List checkpoints for a fine-tuning job.\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const fineTuningJobCheckpoint of client.fineTuning.jobs.checkpoints.list(\n * 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',\n * )) {\n * // ...\n * }\n * ```\n */\n list(fineTuningJobID, query = {}, options) {\n return this._client.getAPIList(path `/fine_tuning/jobs/${fineTuningJobID}/checkpoints`, (CursorPage), { query, ...options });\n }\n}\n//# sourceMappingURL=checkpoints.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../../core/resource.mjs\";\nimport * as CheckpointsAPI from \"./checkpoints.mjs\";\nimport { Checkpoints, } from \"./checkpoints.mjs\";\nimport { CursorPage } from \"../../../core/pagination.mjs\";\nimport { path } from \"../../../internal/utils/path.mjs\";\nexport class Jobs extends APIResource {\n constructor() {\n super(...arguments);\n this.checkpoints = new CheckpointsAPI.Checkpoints(this._client);\n }\n /**\n * Creates a fine-tuning job which begins the process of creating a new model from\n * a given dataset.\n *\n * Response includes details of the enqueued job including job status and the name\n * of the fine-tuned models once complete.\n *\n * [Learn more about fine-tuning](https://platform.openai.com/docs/guides/model-optimization)\n *\n * @example\n * ```ts\n * const fineTuningJob = await client.fineTuning.jobs.create({\n * model: 'gpt-4o-mini',\n * training_file: 'file-abc123',\n * });\n * ```\n */\n create(body, options) {\n return this._client.post('/fine_tuning/jobs', { body, ...options });\n }\n /**\n * Get info about a fine-tuning job.\n *\n * [Learn more about fine-tuning](https://platform.openai.com/docs/guides/model-optimization)\n *\n * @example\n * ```ts\n * const fineTuningJob = await client.fineTuning.jobs.retrieve(\n * 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',\n * );\n * ```\n */\n retrieve(fineTuningJobID, options) {\n return this._client.get(path `/fine_tuning/jobs/${fineTuningJobID}`, options);\n }\n /**\n * List your organization's fine-tuning jobs\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const fineTuningJob of client.fineTuning.jobs.list()) {\n * // ...\n * }\n * ```\n */\n list(query = {}, options) {\n return this._client.getAPIList('/fine_tuning/jobs', (CursorPage), { query, ...options });\n }\n /**\n * Immediately cancel a fine-tune job.\n *\n * @example\n * ```ts\n * const fineTuningJob = await client.fineTuning.jobs.cancel(\n * 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',\n * );\n * ```\n */\n cancel(fineTuningJobID, options) {\n return this._client.post(path `/fine_tuning/jobs/${fineTuningJobID}/cancel`, options);\n }\n /**\n * Get status updates for a fine-tuning job.\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const fineTuningJobEvent of client.fineTuning.jobs.listEvents(\n * 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',\n * )) {\n * // ...\n * }\n * ```\n */\n listEvents(fineTuningJobID, query = {}, options) {\n return this._client.getAPIList(path `/fine_tuning/jobs/${fineTuningJobID}/events`, (CursorPage), { query, ...options });\n }\n /**\n * Pause a fine-tune job.\n *\n * @example\n * ```ts\n * const fineTuningJob = await client.fineTuning.jobs.pause(\n * 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',\n * );\n * ```\n */\n pause(fineTuningJobID, options) {\n return this._client.post(path `/fine_tuning/jobs/${fineTuningJobID}/pause`, options);\n }\n /**\n * Resume a fine-tune job.\n *\n * @example\n * ```ts\n * const fineTuningJob = await client.fineTuning.jobs.resume(\n * 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',\n * );\n * ```\n */\n resume(fineTuningJobID, options) {\n return this._client.post(path `/fine_tuning/jobs/${fineTuningJobID}/resume`, options);\n }\n}\nJobs.Checkpoints = Checkpoints;\n//# sourceMappingURL=jobs.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as MethodsAPI from \"./methods.mjs\";\nimport { Methods, } from \"./methods.mjs\";\nimport * as AlphaAPI from \"./alpha/alpha.mjs\";\nimport { Alpha } from \"./alpha/alpha.mjs\";\nimport * as CheckpointsAPI from \"./checkpoints/checkpoints.mjs\";\nimport { Checkpoints } from \"./checkpoints/checkpoints.mjs\";\nimport * as JobsAPI from \"./jobs/jobs.mjs\";\nimport { Jobs, } from \"./jobs/jobs.mjs\";\nexport class FineTuning extends APIResource {\n constructor() {\n super(...arguments);\n this.methods = new MethodsAPI.Methods(this._client);\n this.jobs = new JobsAPI.Jobs(this._client);\n this.checkpoints = new CheckpointsAPI.Checkpoints(this._client);\n this.alpha = new AlphaAPI.Alpha(this._client);\n }\n}\nFineTuning.Methods = Methods;\nFineTuning.Jobs = Jobs;\nFineTuning.Checkpoints = Checkpoints;\nFineTuning.Alpha = Alpha;\n//# sourceMappingURL=fine-tuning.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nexport class GraderModels extends APIResource {\n}\n//# sourceMappingURL=grader-models.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as GraderModelsAPI from \"./grader-models.mjs\";\nimport { GraderModels, } from \"./grader-models.mjs\";\nexport class Graders extends APIResource {\n constructor() {\n super(...arguments);\n this.graderModels = new GraderModelsAPI.GraderModels(this._client);\n }\n}\nGraders.GraderModels = GraderModels;\n//# sourceMappingURL=graders.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../core/resource.mjs\";\nimport { multipartFormRequestOptions } from \"../internal/uploads.mjs\";\nexport class Images extends APIResource {\n /**\n * Creates a variation of a given image. This endpoint only supports `dall-e-2`.\n *\n * @example\n * ```ts\n * const imagesResponse = await client.images.createVariation({\n * image: fs.createReadStream('otter.png'),\n * });\n * ```\n */\n createVariation(body, options) {\n return this._client.post('/images/variations', multipartFormRequestOptions({ body, ...options }, this._client));\n }\n edit(body, options) {\n return this._client.post('/images/edits', multipartFormRequestOptions({ body, ...options, stream: body.stream ?? false }, this._client));\n }\n generate(body, options) {\n return this._client.post('/images/generations', { body, ...options, stream: body.stream ?? false });\n }\n}\n//# sourceMappingURL=images.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../core/resource.mjs\";\nimport { Page } from \"../core/pagination.mjs\";\nimport { path } from \"../internal/utils/path.mjs\";\nexport class Models extends APIResource {\n /**\n * Retrieves a model instance, providing basic information about the model such as\n * the owner and permissioning.\n */\n retrieve(model, options) {\n return this._client.get(path `/models/${model}`, options);\n }\n /**\n * Lists the currently available models, and provides basic information about each\n * one such as the owner and availability.\n */\n list(options) {\n return this._client.getAPIList('/models', (Page), options);\n }\n /**\n * Delete a fine-tuned model. You must have the Owner role in your organization to\n * delete a model.\n */\n delete(model, options) {\n return this._client.delete(path `/models/${model}`, options);\n }\n}\n//# sourceMappingURL=models.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../core/resource.mjs\";\nexport class Moderations extends APIResource {\n /**\n * Classifies if text and/or image inputs are potentially harmful. Learn more in\n * the [moderation guide](https://platform.openai.com/docs/guides/moderation).\n */\n create(body, options) {\n return this._client.post('/moderations', { body, ...options });\n }\n}\n//# sourceMappingURL=moderations.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { buildHeaders } from \"../../internal/headers.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Calls extends APIResource {\n /**\n * Accept an incoming SIP call and configure the realtime session that will handle\n * it.\n *\n * @example\n * ```ts\n * await client.realtime.calls.accept('call_id', {\n * type: 'realtime',\n * });\n * ```\n */\n accept(callID, body, options) {\n return this._client.post(path `/realtime/calls/${callID}/accept`, {\n body,\n ...options,\n headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),\n });\n }\n /**\n * End an active Realtime API call, whether it was initiated over SIP or WebRTC.\n *\n * @example\n * ```ts\n * await client.realtime.calls.hangup('call_id');\n * ```\n */\n hangup(callID, options) {\n return this._client.post(path `/realtime/calls/${callID}/hangup`, {\n ...options,\n headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),\n });\n }\n /**\n * Transfer an active SIP call to a new destination using the SIP REFER verb.\n *\n * @example\n * ```ts\n * await client.realtime.calls.refer('call_id', {\n * target_uri: 'tel:+14155550123',\n * });\n * ```\n */\n refer(callID, body, options) {\n return this._client.post(path `/realtime/calls/${callID}/refer`, {\n body,\n ...options,\n headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),\n });\n }\n /**\n * Decline an incoming SIP call by returning a SIP status code to the caller.\n *\n * @example\n * ```ts\n * await client.realtime.calls.reject('call_id');\n * ```\n */\n reject(callID, body = {}, options) {\n return this._client.post(path `/realtime/calls/${callID}/reject`, {\n body,\n ...options,\n headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),\n });\n }\n}\n//# sourceMappingURL=calls.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nexport class ClientSecrets extends APIResource {\n /**\n * Create a Realtime client secret with an associated session configuration.\n *\n * @example\n * ```ts\n * const clientSecret =\n * await client.realtime.clientSecrets.create();\n * ```\n */\n create(body, options) {\n return this._client.post('/realtime/client_secrets', { body, ...options });\n }\n}\n//# sourceMappingURL=client-secrets.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as CallsAPI from \"./calls.mjs\";\nimport { Calls } from \"./calls.mjs\";\nimport * as ClientSecretsAPI from \"./client-secrets.mjs\";\nimport { ClientSecrets, } from \"./client-secrets.mjs\";\nexport class Realtime extends APIResource {\n constructor() {\n super(...arguments);\n this.clientSecrets = new ClientSecretsAPI.ClientSecrets(this._client);\n this.calls = new CallsAPI.Calls(this._client);\n }\n}\nRealtime.ClientSecrets = ClientSecrets;\nRealtime.Calls = Calls;\n//# sourceMappingURL=realtime.mjs.map","import { OpenAIError } from \"../error.mjs\";\nimport { isAutoParsableResponseFormat } from \"../lib/parser.mjs\";\nexport function maybeParseResponse(response, params) {\n if (!params || !hasAutoParseableInput(params)) {\n return {\n ...response,\n output_parsed: null,\n output: response.output.map((item) => {\n if (item.type === 'function_call') {\n return {\n ...item,\n parsed_arguments: null,\n };\n }\n if (item.type === 'message') {\n return {\n ...item,\n content: item.content.map((content) => ({\n ...content,\n parsed: null,\n })),\n };\n }\n else {\n return item;\n }\n }),\n };\n }\n return parseResponse(response, params);\n}\nexport function parseResponse(response, params) {\n const output = response.output.map((item) => {\n if (item.type === 'function_call') {\n return {\n ...item,\n parsed_arguments: parseToolCall(params, item),\n };\n }\n if (item.type === 'message') {\n const content = item.content.map((content) => {\n if (content.type === 'output_text') {\n return {\n ...content,\n parsed: parseTextFormat(params, content.text),\n };\n }\n return content;\n });\n return {\n ...item,\n content,\n };\n }\n return item;\n });\n const parsed = Object.assign({}, response, { output });\n if (!Object.getOwnPropertyDescriptor(response, 'output_text')) {\n addOutputText(parsed);\n }\n Object.defineProperty(parsed, 'output_parsed', {\n enumerable: true,\n get() {\n for (const output of parsed.output) {\n if (output.type !== 'message') {\n continue;\n }\n for (const content of output.content) {\n if (content.type === 'output_text' && content.parsed !== null) {\n return content.parsed;\n }\n }\n }\n return null;\n },\n });\n return parsed;\n}\nfunction parseTextFormat(params, content) {\n if (params.text?.format?.type !== 'json_schema') {\n return null;\n }\n if ('$parseRaw' in params.text?.format) {\n const text_format = params.text?.format;\n return text_format.$parseRaw(content);\n }\n return JSON.parse(content);\n}\nexport function hasAutoParseableInput(params) {\n if (isAutoParsableResponseFormat(params.text?.format)) {\n return true;\n }\n return false;\n}\nexport function makeParseableResponseTool(tool, { parser, callback, }) {\n const obj = { ...tool };\n Object.defineProperties(obj, {\n $brand: {\n value: 'auto-parseable-tool',\n enumerable: false,\n },\n $parseRaw: {\n value: parser,\n enumerable: false,\n },\n $callback: {\n value: callback,\n enumerable: false,\n },\n });\n return obj;\n}\nexport function isAutoParsableTool(tool) {\n return tool?.['$brand'] === 'auto-parseable-tool';\n}\nfunction getInputToolByName(input_tools, name) {\n return input_tools.find((tool) => tool.type === 'function' && tool.name === name);\n}\nfunction parseToolCall(params, toolCall) {\n const inputTool = getInputToolByName(params.tools ?? [], toolCall.name);\n return {\n ...toolCall,\n ...toolCall,\n parsed_arguments: isAutoParsableTool(inputTool) ? inputTool.$parseRaw(toolCall.arguments)\n : inputTool?.strict ? JSON.parse(toolCall.arguments)\n : null,\n };\n}\nexport function shouldParseToolCall(params, toolCall) {\n if (!params) {\n return false;\n }\n const inputTool = getInputToolByName(params.tools ?? [], toolCall.name);\n return isAutoParsableTool(inputTool) || inputTool?.strict || false;\n}\nexport function validateInputTools(tools) {\n for (const tool of tools ?? []) {\n if (tool.type !== 'function') {\n throw new OpenAIError(`Currently only \\`function\\` tool types support auto-parsing; Received \\`${tool.type}\\``);\n }\n if (tool.function.strict !== true) {\n throw new OpenAIError(`The \\`${tool.function.name}\\` tool is not marked with \\`strict: true\\`. Only strict function tools can be auto-parsed`);\n }\n }\n}\nexport function addOutputText(rsp) {\n const texts = [];\n for (const output of rsp.output) {\n if (output.type !== 'message') {\n continue;\n }\n for (const content of output.content) {\n if (content.type === 'output_text') {\n texts.push(content.text);\n }\n }\n }\n rsp.output_text = texts.join('');\n}\n//# sourceMappingURL=ResponsesParser.mjs.map","var _ResponseStream_instances, _ResponseStream_params, _ResponseStream_currentResponseSnapshot, _ResponseStream_finalResponse, _ResponseStream_beginRequest, _ResponseStream_addEvent, _ResponseStream_endRequest, _ResponseStream_accumulateResponse;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"../../internal/tslib.mjs\";\nimport { APIUserAbortError, OpenAIError } from \"../../error.mjs\";\nimport { EventStream } from \"../EventStream.mjs\";\nimport { maybeParseResponse } from \"../ResponsesParser.mjs\";\nexport class ResponseStream extends EventStream {\n constructor(params) {\n super();\n _ResponseStream_instances.add(this);\n _ResponseStream_params.set(this, void 0);\n _ResponseStream_currentResponseSnapshot.set(this, void 0);\n _ResponseStream_finalResponse.set(this, void 0);\n __classPrivateFieldSet(this, _ResponseStream_params, params, \"f\");\n }\n static createResponse(client, params, options) {\n const runner = new ResponseStream(params);\n runner._run(() => runner._createOrRetrieveResponse(client, params, {\n ...options,\n headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },\n }));\n return runner;\n }\n async _createOrRetrieveResponse(client, params, options) {\n const signal = options?.signal;\n if (signal) {\n if (signal.aborted)\n this.controller.abort();\n signal.addEventListener('abort', () => this.controller.abort());\n }\n __classPrivateFieldGet(this, _ResponseStream_instances, \"m\", _ResponseStream_beginRequest).call(this);\n let stream;\n let starting_after = null;\n if ('response_id' in params) {\n stream = await client.responses.retrieve(params.response_id, { stream: true }, { ...options, signal: this.controller.signal, stream: true });\n starting_after = params.starting_after ?? null;\n }\n else {\n stream = await client.responses.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });\n }\n this._connected();\n for await (const event of stream) {\n __classPrivateFieldGet(this, _ResponseStream_instances, \"m\", _ResponseStream_addEvent).call(this, event, starting_after);\n }\n if (stream.controller.signal?.aborted) {\n throw new APIUserAbortError();\n }\n return __classPrivateFieldGet(this, _ResponseStream_instances, \"m\", _ResponseStream_endRequest).call(this);\n }\n [(_ResponseStream_params = new WeakMap(), _ResponseStream_currentResponseSnapshot = new WeakMap(), _ResponseStream_finalResponse = new WeakMap(), _ResponseStream_instances = new WeakSet(), _ResponseStream_beginRequest = function _ResponseStream_beginRequest() {\n if (this.ended)\n return;\n __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, undefined, \"f\");\n }, _ResponseStream_addEvent = function _ResponseStream_addEvent(event, starting_after) {\n if (this.ended)\n return;\n const maybeEmit = (name, event) => {\n if (starting_after == null || event.sequence_number > starting_after) {\n this._emit(name, event);\n }\n };\n const response = __classPrivateFieldGet(this, _ResponseStream_instances, \"m\", _ResponseStream_accumulateResponse).call(this, event);\n maybeEmit('event', event);\n switch (event.type) {\n case 'response.output_text.delta': {\n const output = response.output[event.output_index];\n if (!output) {\n throw new OpenAIError(`missing output at index ${event.output_index}`);\n }\n if (output.type === 'message') {\n const content = output.content[event.content_index];\n if (!content) {\n throw new OpenAIError(`missing content at index ${event.content_index}`);\n }\n if (content.type !== 'output_text') {\n throw new OpenAIError(`expected content to be 'output_text', got ${content.type}`);\n }\n maybeEmit('response.output_text.delta', {\n ...event,\n snapshot: content.text,\n });\n }\n break;\n }\n case 'response.function_call_arguments.delta': {\n const output = response.output[event.output_index];\n if (!output) {\n throw new OpenAIError(`missing output at index ${event.output_index}`);\n }\n if (output.type === 'function_call') {\n maybeEmit('response.function_call_arguments.delta', {\n ...event,\n snapshot: output.arguments,\n });\n }\n break;\n }\n default:\n maybeEmit(event.type, event);\n break;\n }\n }, _ResponseStream_endRequest = function _ResponseStream_endRequest() {\n if (this.ended) {\n throw new OpenAIError(`stream has ended, this shouldn't happen`);\n }\n const snapshot = __classPrivateFieldGet(this, _ResponseStream_currentResponseSnapshot, \"f\");\n if (!snapshot) {\n throw new OpenAIError(`request ended without sending any events`);\n }\n __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, undefined, \"f\");\n const parsedResponse = finalizeResponse(snapshot, __classPrivateFieldGet(this, _ResponseStream_params, \"f\"));\n __classPrivateFieldSet(this, _ResponseStream_finalResponse, parsedResponse, \"f\");\n return parsedResponse;\n }, _ResponseStream_accumulateResponse = function _ResponseStream_accumulateResponse(event) {\n let snapshot = __classPrivateFieldGet(this, _ResponseStream_currentResponseSnapshot, \"f\");\n if (!snapshot) {\n if (event.type !== 'response.created') {\n throw new OpenAIError(`When snapshot hasn't been set yet, expected 'response.created' event, got ${event.type}`);\n }\n snapshot = __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, event.response, \"f\");\n return snapshot;\n }\n switch (event.type) {\n case 'response.output_item.added': {\n snapshot.output.push(event.item);\n break;\n }\n case 'response.content_part.added': {\n const output = snapshot.output[event.output_index];\n if (!output) {\n throw new OpenAIError(`missing output at index ${event.output_index}`);\n }\n const type = output.type;\n const part = event.part;\n if (type === 'message' && part.type !== 'reasoning_text') {\n output.content.push(part);\n }\n else if (type === 'reasoning' && part.type === 'reasoning_text') {\n if (!output.content) {\n output.content = [];\n }\n output.content.push(part);\n }\n break;\n }\n case 'response.output_text.delta': {\n const output = snapshot.output[event.output_index];\n if (!output) {\n throw new OpenAIError(`missing output at index ${event.output_index}`);\n }\n if (output.type === 'message') {\n const content = output.content[event.content_index];\n if (!content) {\n throw new OpenAIError(`missing content at index ${event.content_index}`);\n }\n if (content.type !== 'output_text') {\n throw new OpenAIError(`expected content to be 'output_text', got ${content.type}`);\n }\n content.text += event.delta;\n }\n break;\n }\n case 'response.function_call_arguments.delta': {\n const output = snapshot.output[event.output_index];\n if (!output) {\n throw new OpenAIError(`missing output at index ${event.output_index}`);\n }\n if (output.type === 'function_call') {\n output.arguments += event.delta;\n }\n break;\n }\n case 'response.reasoning_text.delta': {\n const output = snapshot.output[event.output_index];\n if (!output) {\n throw new OpenAIError(`missing output at index ${event.output_index}`);\n }\n if (output.type === 'reasoning') {\n const content = output.content?.[event.content_index];\n if (!content) {\n throw new OpenAIError(`missing content at index ${event.content_index}`);\n }\n if (content.type !== 'reasoning_text') {\n throw new OpenAIError(`expected content to be 'reasoning_text', got ${content.type}`);\n }\n content.text += event.delta;\n }\n break;\n }\n case 'response.completed': {\n __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, event.response, \"f\");\n break;\n }\n }\n return snapshot;\n }, Symbol.asyncIterator)]() {\n const pushQueue = [];\n const readQueue = [];\n let done = false;\n this.on('event', (event) => {\n const reader = readQueue.shift();\n if (reader) {\n reader.resolve(event);\n }\n else {\n pushQueue.push(event);\n }\n });\n this.on('end', () => {\n done = true;\n for (const reader of readQueue) {\n reader.resolve(undefined);\n }\n readQueue.length = 0;\n });\n this.on('abort', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n this.on('error', (err) => {\n done = true;\n for (const reader of readQueue) {\n reader.reject(err);\n }\n readQueue.length = 0;\n });\n return {\n next: async () => {\n if (!pushQueue.length) {\n if (done) {\n return { value: undefined, done: true };\n }\n return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((event) => (event ? { value: event, done: false } : { value: undefined, done: true }));\n }\n const event = pushQueue.shift();\n return { value: event, done: false };\n },\n return: async () => {\n this.abort();\n return { value: undefined, done: true };\n },\n };\n }\n /**\n * @returns a promise that resolves with the final Response, or rejects\n * if an error occurred or the stream ended prematurely without producing a REsponse.\n */\n async finalResponse() {\n await this.done();\n const response = __classPrivateFieldGet(this, _ResponseStream_finalResponse, \"f\");\n if (!response)\n throw new OpenAIError('stream ended without producing a ChatCompletion');\n return response;\n }\n}\nfunction finalizeResponse(snapshot, params) {\n return maybeParseResponse(snapshot, params);\n}\n//# sourceMappingURL=ResponseStream.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { CursorPage } from \"../../core/pagination.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class InputItems extends APIResource {\n /**\n * Returns a list of input items for a given response.\n *\n * @example\n * ```ts\n * // Automatically fetches more pages as needed.\n * for await (const responseItem of client.responses.inputItems.list(\n * 'response_id',\n * )) {\n * // ...\n * }\n * ```\n */\n list(responseID, query = {}, options) {\n return this._client.getAPIList(path `/responses/${responseID}/input_items`, (CursorPage), { query, ...options });\n }\n}\n//# sourceMappingURL=input-items.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nexport class InputTokens extends APIResource {\n /**\n * Get input token counts\n *\n * @example\n * ```ts\n * const response = await client.responses.inputTokens.count();\n * ```\n */\n count(body = {}, options) {\n return this._client.post('/responses/input_tokens', { body, ...options });\n }\n}\n//# sourceMappingURL=input-tokens.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { parseResponse, addOutputText, } from \"../../lib/ResponsesParser.mjs\";\nimport { ResponseStream } from \"../../lib/responses/ResponseStream.mjs\";\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as InputItemsAPI from \"./input-items.mjs\";\nimport { InputItems } from \"./input-items.mjs\";\nimport * as InputTokensAPI from \"./input-tokens.mjs\";\nimport { InputTokens } from \"./input-tokens.mjs\";\nimport { buildHeaders } from \"../../internal/headers.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Responses extends APIResource {\n constructor() {\n super(...arguments);\n this.inputItems = new InputItemsAPI.InputItems(this._client);\n this.inputTokens = new InputTokensAPI.InputTokens(this._client);\n }\n create(body, options) {\n return this._client.post('/responses', { body, ...options, stream: body.stream ?? false })._thenUnwrap((rsp) => {\n if ('object' in rsp && rsp.object === 'response') {\n addOutputText(rsp);\n }\n return rsp;\n });\n }\n retrieve(responseID, query = {}, options) {\n return this._client.get(path `/responses/${responseID}`, {\n query,\n ...options,\n stream: query?.stream ?? false,\n })._thenUnwrap((rsp) => {\n if ('object' in rsp && rsp.object === 'response') {\n addOutputText(rsp);\n }\n return rsp;\n });\n }\n /**\n * Deletes a model response with the given ID.\n *\n * @example\n * ```ts\n * await client.responses.delete(\n * 'resp_677efb5139a88190b512bc3fef8e535d',\n * );\n * ```\n */\n delete(responseID, options) {\n return this._client.delete(path `/responses/${responseID}`, {\n ...options,\n headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),\n });\n }\n parse(body, options) {\n return this._client.responses\n .create(body, options)\n ._thenUnwrap((response) => parseResponse(response, body));\n }\n /**\n * Creates a model response stream\n */\n stream(body, options) {\n return ResponseStream.createResponse(this._client, body, options);\n }\n /**\n * Cancels a model response with the given ID. Only responses created with the\n * `background` parameter set to `true` can be cancelled.\n * [Learn more](https://platform.openai.com/docs/guides/background).\n *\n * @example\n * ```ts\n * const response = await client.responses.cancel(\n * 'resp_677efb5139a88190b512bc3fef8e535d',\n * );\n * ```\n */\n cancel(responseID, options) {\n return this._client.post(path `/responses/${responseID}/cancel`, options);\n }\n /**\n * Compact conversation\n *\n * @example\n * ```ts\n * const compactedResponse = await client.responses.compact({\n * model: 'gpt-5.2',\n * });\n * ```\n */\n compact(body, options) {\n return this._client.post('/responses/compact', { body, ...options });\n }\n}\nResponses.InputItems = InputItems;\nResponses.InputTokens = InputTokens;\n//# sourceMappingURL=responses.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { multipartFormRequestOptions } from \"../../internal/uploads.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Parts extends APIResource {\n /**\n * Adds a\n * [Part](https://platform.openai.com/docs/api-reference/uploads/part-object) to an\n * [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object.\n * A Part represents a chunk of bytes from the file you are trying to upload.\n *\n * Each Part can be at most 64 MB, and you can add Parts until you hit the Upload\n * maximum of 8 GB.\n *\n * It is possible to add multiple Parts in parallel. You can decide the intended\n * order of the Parts when you\n * [complete the Upload](https://platform.openai.com/docs/api-reference/uploads/complete).\n */\n create(uploadID, body, options) {\n return this._client.post(path `/uploads/${uploadID}/parts`, multipartFormRequestOptions({ body, ...options }, this._client));\n }\n}\n//# sourceMappingURL=parts.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as PartsAPI from \"./parts.mjs\";\nimport { Parts } from \"./parts.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Uploads extends APIResource {\n constructor() {\n super(...arguments);\n this.parts = new PartsAPI.Parts(this._client);\n }\n /**\n * Creates an intermediate\n * [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object\n * that you can add\n * [Parts](https://platform.openai.com/docs/api-reference/uploads/part-object) to.\n * Currently, an Upload can accept at most 8 GB in total and expires after an hour\n * after you create it.\n *\n * Once you complete the Upload, we will create a\n * [File](https://platform.openai.com/docs/api-reference/files/object) object that\n * contains all the parts you uploaded. This File is usable in the rest of our\n * platform as a regular File object.\n *\n * For certain `purpose` values, the correct `mime_type` must be specified. Please\n * refer to documentation for the\n * [supported MIME types for your use case](https://platform.openai.com/docs/assistants/tools/file-search#supported-files).\n *\n * For guidance on the proper filename extensions for each purpose, please follow\n * the documentation on\n * [creating a File](https://platform.openai.com/docs/api-reference/files/create).\n */\n create(body, options) {\n return this._client.post('/uploads', { body, ...options });\n }\n /**\n * Cancels the Upload. No Parts may be added after an Upload is cancelled.\n */\n cancel(uploadID, options) {\n return this._client.post(path `/uploads/${uploadID}/cancel`, options);\n }\n /**\n * Completes the\n * [Upload](https://platform.openai.com/docs/api-reference/uploads/object).\n *\n * Within the returned Upload object, there is a nested\n * [File](https://platform.openai.com/docs/api-reference/files/object) object that\n * is ready to use in the rest of the platform.\n *\n * You can specify the order of the Parts by passing in an ordered list of the Part\n * IDs.\n *\n * The number of bytes uploaded upon completion must match the number of bytes\n * initially specified when creating the Upload object. No Parts may be added after\n * an Upload is completed.\n */\n complete(uploadID, body, options) {\n return this._client.post(path `/uploads/${uploadID}/complete`, { body, ...options });\n }\n}\nUploads.Parts = Parts;\n//# sourceMappingURL=uploads.mjs.map","/**\n * Like `Promise.allSettled()` but throws an error if any promises are rejected.\n */\nexport const allSettledWithThrow = async (promises) => {\n const results = await Promise.allSettled(promises);\n const rejected = results.filter((result) => result.status === 'rejected');\n if (rejected.length) {\n for (const result of rejected) {\n console.error(result.reason);\n }\n throw new Error(`${rejected.length} promise(s) failed - see the above errors`);\n }\n // Note: TS was complaining about using `.filter().map()` here for some reason\n const values = [];\n for (const result of results) {\n if (result.status === 'fulfilled') {\n values.push(result.value);\n }\n }\n return values;\n};\n//# sourceMappingURL=Util.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { CursorPage } from \"../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../internal/headers.mjs\";\nimport { sleep } from \"../../internal/utils/sleep.mjs\";\nimport { allSettledWithThrow } from \"../../lib/Util.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class FileBatches extends APIResource {\n /**\n * Create a vector store file batch.\n */\n create(vectorStoreID, body, options) {\n return this._client.post(path `/vector_stores/${vectorStoreID}/file_batches`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Retrieves a vector store file batch.\n */\n retrieve(batchID, params, options) {\n const { vector_store_id } = params;\n return this._client.get(path `/vector_stores/${vector_store_id}/file_batches/${batchID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Cancel a vector store file batch. This attempts to cancel the processing of\n * files in this batch as soon as possible.\n */\n cancel(batchID, params, options) {\n const { vector_store_id } = params;\n return this._client.post(path `/vector_stores/${vector_store_id}/file_batches/${batchID}/cancel`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Create a vector store batch and poll until all files have been processed.\n */\n async createAndPoll(vectorStoreId, body, options) {\n const batch = await this.create(vectorStoreId, body);\n return await this.poll(vectorStoreId, batch.id, options);\n }\n /**\n * Returns a list of vector store files in a batch.\n */\n listFiles(batchID, params, options) {\n const { vector_store_id, ...query } = params;\n return this._client.getAPIList(path `/vector_stores/${vector_store_id}/file_batches/${batchID}/files`, (CursorPage), { query, ...options, headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]) });\n }\n /**\n * Wait for the given file batch to be processed.\n *\n * Note: this will return even if one of the files failed to process, you need to\n * check batch.file_counts.failed_count to handle this case.\n */\n async poll(vectorStoreID, batchID, options) {\n const headers = buildHeaders([\n options?.headers,\n {\n 'X-Stainless-Poll-Helper': 'true',\n 'X-Stainless-Custom-Poll-Interval': options?.pollIntervalMs?.toString() ?? undefined,\n },\n ]);\n while (true) {\n const { data: batch, response } = await this.retrieve(batchID, { vector_store_id: vectorStoreID }, {\n ...options,\n headers,\n }).withResponse();\n switch (batch.status) {\n case 'in_progress':\n let sleepInterval = 5000;\n if (options?.pollIntervalMs) {\n sleepInterval = options.pollIntervalMs;\n }\n else {\n const headerInterval = response.headers.get('openai-poll-after-ms');\n if (headerInterval) {\n const headerIntervalMs = parseInt(headerInterval);\n if (!isNaN(headerIntervalMs)) {\n sleepInterval = headerIntervalMs;\n }\n }\n }\n await sleep(sleepInterval);\n break;\n case 'failed':\n case 'cancelled':\n case 'completed':\n return batch;\n }\n }\n }\n /**\n * Uploads the given files concurrently and then creates a vector store file batch.\n *\n * The concurrency limit is configurable using the `maxConcurrency` parameter.\n */\n async uploadAndPoll(vectorStoreId, { files, fileIds = [] }, options) {\n if (files == null || files.length == 0) {\n throw new Error(`No \\`files\\` provided to process. If you've already uploaded files you should use \\`.createAndPoll()\\` instead`);\n }\n const configuredConcurrency = options?.maxConcurrency ?? 5;\n // We cap the number of workers at the number of files (so we don't start any unnecessary workers)\n const concurrencyLimit = Math.min(configuredConcurrency, files.length);\n const client = this._client;\n const fileIterator = files.values();\n const allFileIds = [...fileIds];\n // This code is based on this design. The libraries don't accommodate our environment limits.\n // https://stackoverflow.com/questions/40639432/what-is-the-best-way-to-limit-concurrency-when-using-es6s-promise-all\n async function processFiles(iterator) {\n for (let item of iterator) {\n const fileObj = await client.files.create({ file: item, purpose: 'assistants' }, options);\n allFileIds.push(fileObj.id);\n }\n }\n // Start workers to process results\n const workers = Array(concurrencyLimit).fill(fileIterator).map(processFiles);\n // Wait for all processing to complete.\n await allSettledWithThrow(workers);\n return await this.createAndPoll(vectorStoreId, {\n file_ids: allFileIds,\n });\n }\n}\n//# sourceMappingURL=file-batches.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport { CursorPage, Page } from \"../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../internal/headers.mjs\";\nimport { sleep } from \"../../internal/utils.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class Files extends APIResource {\n /**\n * Create a vector store file by attaching a\n * [File](https://platform.openai.com/docs/api-reference/files) to a\n * [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object).\n */\n create(vectorStoreID, body, options) {\n return this._client.post(path `/vector_stores/${vectorStoreID}/files`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Retrieves a vector store file.\n */\n retrieve(fileID, params, options) {\n const { vector_store_id } = params;\n return this._client.get(path `/vector_stores/${vector_store_id}/files/${fileID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Update attributes on a vector store file.\n */\n update(fileID, params, options) {\n const { vector_store_id, ...body } = params;\n return this._client.post(path `/vector_stores/${vector_store_id}/files/${fileID}`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Returns a list of vector store files.\n */\n list(vectorStoreID, query = {}, options) {\n return this._client.getAPIList(path `/vector_stores/${vectorStoreID}/files`, (CursorPage), {\n query,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Delete a vector store file. This will remove the file from the vector store but\n * the file itself will not be deleted. To delete the file, use the\n * [delete file](https://platform.openai.com/docs/api-reference/files/delete)\n * endpoint.\n */\n delete(fileID, params, options) {\n const { vector_store_id } = params;\n return this._client.delete(path `/vector_stores/${vector_store_id}/files/${fileID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Attach a file to the given vector store and wait for it to be processed.\n */\n async createAndPoll(vectorStoreId, body, options) {\n const file = await this.create(vectorStoreId, body, options);\n return await this.poll(vectorStoreId, file.id, options);\n }\n /**\n * Wait for the vector store file to finish processing.\n *\n * Note: this will return even if the file failed to process, you need to check\n * file.last_error and file.status to handle these cases\n */\n async poll(vectorStoreID, fileID, options) {\n const headers = buildHeaders([\n options?.headers,\n {\n 'X-Stainless-Poll-Helper': 'true',\n 'X-Stainless-Custom-Poll-Interval': options?.pollIntervalMs?.toString() ?? undefined,\n },\n ]);\n while (true) {\n const fileResponse = await this.retrieve(fileID, {\n vector_store_id: vectorStoreID,\n }, { ...options, headers }).withResponse();\n const file = fileResponse.data;\n switch (file.status) {\n case 'in_progress':\n let sleepInterval = 5000;\n if (options?.pollIntervalMs) {\n sleepInterval = options.pollIntervalMs;\n }\n else {\n const headerInterval = fileResponse.response.headers.get('openai-poll-after-ms');\n if (headerInterval) {\n const headerIntervalMs = parseInt(headerInterval);\n if (!isNaN(headerIntervalMs)) {\n sleepInterval = headerIntervalMs;\n }\n }\n }\n await sleep(sleepInterval);\n break;\n case 'failed':\n case 'completed':\n return file;\n }\n }\n }\n /**\n * Upload a file to the `files` API and then attach it to the given vector store.\n *\n * Note the file will be asynchronously processed (you can use the alternative\n * polling helper method to wait for processing to complete).\n */\n async upload(vectorStoreId, file, options) {\n const fileInfo = await this._client.files.create({ file: file, purpose: 'assistants' }, options);\n return this.create(vectorStoreId, { file_id: fileInfo.id }, options);\n }\n /**\n * Add a file to a vector store and poll until processing is complete.\n */\n async uploadAndPoll(vectorStoreId, file, options) {\n const fileInfo = await this.upload(vectorStoreId, file, options);\n return await this.poll(vectorStoreId, fileInfo.id, options);\n }\n /**\n * Retrieve the parsed contents of a vector store file.\n */\n content(fileID, params, options) {\n const { vector_store_id } = params;\n return this._client.getAPIList(path `/vector_stores/${vector_store_id}/files/${fileID}/content`, (Page), { ...options, headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]) });\n }\n}\n//# sourceMappingURL=files.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../../core/resource.mjs\";\nimport * as FileBatchesAPI from \"./file-batches.mjs\";\nimport { FileBatches, } from \"./file-batches.mjs\";\nimport * as FilesAPI from \"./files.mjs\";\nimport { Files, } from \"./files.mjs\";\nimport { CursorPage, Page } from \"../../core/pagination.mjs\";\nimport { buildHeaders } from \"../../internal/headers.mjs\";\nimport { path } from \"../../internal/utils/path.mjs\";\nexport class VectorStores extends APIResource {\n constructor() {\n super(...arguments);\n this.files = new FilesAPI.Files(this._client);\n this.fileBatches = new FileBatchesAPI.FileBatches(this._client);\n }\n /**\n * Create a vector store.\n */\n create(body, options) {\n return this._client.post('/vector_stores', {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Retrieves a vector store.\n */\n retrieve(vectorStoreID, options) {\n return this._client.get(path `/vector_stores/${vectorStoreID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Modifies a vector store.\n */\n update(vectorStoreID, body, options) {\n return this._client.post(path `/vector_stores/${vectorStoreID}`, {\n body,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Returns a list of vector stores.\n */\n list(query = {}, options) {\n return this._client.getAPIList('/vector_stores', (CursorPage), {\n query,\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Delete a vector store.\n */\n delete(vectorStoreID, options) {\n return this._client.delete(path `/vector_stores/${vectorStoreID}`, {\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n /**\n * Search a vector store for relevant chunks based on a query and file attributes\n * filter.\n */\n search(vectorStoreID, body, options) {\n return this._client.getAPIList(path `/vector_stores/${vectorStoreID}/search`, (Page), {\n body,\n method: 'post',\n ...options,\n headers: buildHeaders([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),\n });\n }\n}\nVectorStores.Files = Files;\nVectorStores.FileBatches = FileBatches;\n//# sourceMappingURL=vector-stores.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nimport { APIResource } from \"../core/resource.mjs\";\nimport { ConversationCursorPage } from \"../core/pagination.mjs\";\nimport { buildHeaders } from \"../internal/headers.mjs\";\nimport { maybeMultipartFormRequestOptions } from \"../internal/uploads.mjs\";\nimport { path } from \"../internal/utils/path.mjs\";\nexport class Videos extends APIResource {\n /**\n * Create a video\n */\n create(body, options) {\n return this._client.post('/videos', maybeMultipartFormRequestOptions({ body, ...options }, this._client));\n }\n /**\n * Retrieve a video\n */\n retrieve(videoID, options) {\n return this._client.get(path `/videos/${videoID}`, options);\n }\n /**\n * List videos\n */\n list(query = {}, options) {\n return this._client.getAPIList('/videos', (ConversationCursorPage), { query, ...options });\n }\n /**\n * Delete a video\n */\n delete(videoID, options) {\n return this._client.delete(path `/videos/${videoID}`, options);\n }\n /**\n * Download video content\n */\n downloadContent(videoID, query = {}, options) {\n return this._client.get(path `/videos/${videoID}/content`, {\n query,\n ...options,\n headers: buildHeaders([{ Accept: 'application/binary' }, options?.headers]),\n __binaryResponse: true,\n });\n }\n /**\n * Create a video remix\n */\n remix(videoID, body, options) {\n return this._client.post(path `/videos/${videoID}/remix`, maybeMultipartFormRequestOptions({ body, ...options }, this._client));\n }\n}\n//# sourceMappingURL=videos.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nvar _Webhooks_instances, _Webhooks_validateSecret, _Webhooks_getRequiredHeader;\nimport { __classPrivateFieldGet } from \"../internal/tslib.mjs\";\nimport { InvalidWebhookSignatureError } from \"../error.mjs\";\nimport { APIResource } from \"../core/resource.mjs\";\nimport { buildHeaders } from \"../internal/headers.mjs\";\nexport class Webhooks extends APIResource {\n constructor() {\n super(...arguments);\n _Webhooks_instances.add(this);\n }\n /**\n * Validates that the given payload was sent by OpenAI and parses the payload.\n */\n async unwrap(payload, headers, secret = this._client.webhookSecret, tolerance = 300) {\n await this.verifySignature(payload, headers, secret, tolerance);\n return JSON.parse(payload);\n }\n /**\n * Validates whether or not the webhook payload was sent by OpenAI.\n *\n * An error will be raised if the webhook payload was not sent by OpenAI.\n *\n * @param payload - The webhook payload\n * @param headers - The webhook headers\n * @param secret - The webhook secret (optional, will use client secret if not provided)\n * @param tolerance - Maximum age of the webhook in seconds (default: 300 = 5 minutes)\n */\n async verifySignature(payload, headers, secret = this._client.webhookSecret, tolerance = 300) {\n if (typeof crypto === 'undefined' ||\n typeof crypto.subtle.importKey !== 'function' ||\n typeof crypto.subtle.verify !== 'function') {\n throw new Error('Webhook signature verification is only supported when the `crypto` global is defined');\n }\n __classPrivateFieldGet(this, _Webhooks_instances, \"m\", _Webhooks_validateSecret).call(this, secret);\n const headersObj = buildHeaders([headers]).values;\n const signatureHeader = __classPrivateFieldGet(this, _Webhooks_instances, \"m\", _Webhooks_getRequiredHeader).call(this, headersObj, 'webhook-signature');\n const timestamp = __classPrivateFieldGet(this, _Webhooks_instances, \"m\", _Webhooks_getRequiredHeader).call(this, headersObj, 'webhook-timestamp');\n const webhookId = __classPrivateFieldGet(this, _Webhooks_instances, \"m\", _Webhooks_getRequiredHeader).call(this, headersObj, 'webhook-id');\n // Validate timestamp to prevent replay attacks\n const timestampSeconds = parseInt(timestamp, 10);\n if (isNaN(timestampSeconds)) {\n throw new InvalidWebhookSignatureError('Invalid webhook timestamp format');\n }\n const nowSeconds = Math.floor(Date.now() / 1000);\n if (nowSeconds - timestampSeconds > tolerance) {\n throw new InvalidWebhookSignatureError('Webhook timestamp is too old');\n }\n if (timestampSeconds > nowSeconds + tolerance) {\n throw new InvalidWebhookSignatureError('Webhook timestamp is too new');\n }\n // Extract signatures from v1,<base64> format\n // The signature header can have multiple values, separated by spaces.\n // Each value is in the format v1,<base64>. We should accept if any match.\n const signatures = signatureHeader\n .split(' ')\n .map((part) => (part.startsWith('v1,') ? part.substring(3) : part));\n // Decode the secret if it starts with whsec_\n const decodedSecret = secret.startsWith('whsec_') ?\n Buffer.from(secret.replace('whsec_', ''), 'base64')\n : Buffer.from(secret, 'utf-8');\n // Create the signed payload: {webhook_id}.{timestamp}.{payload}\n const signedPayload = webhookId ? `${webhookId}.${timestamp}.${payload}` : `${timestamp}.${payload}`;\n // Import the secret as a cryptographic key for HMAC\n const key = await crypto.subtle.importKey('raw', decodedSecret, { name: 'HMAC', hash: 'SHA-256' }, false, ['verify']);\n // Check if any signature matches using timing-safe WebCrypto verify\n for (const signature of signatures) {\n try {\n const signatureBytes = Buffer.from(signature, 'base64');\n const isValid = await crypto.subtle.verify('HMAC', key, signatureBytes, new TextEncoder().encode(signedPayload));\n if (isValid) {\n return; // Valid signature found\n }\n }\n catch {\n // Invalid base64 or signature format, continue to next signature\n continue;\n }\n }\n throw new InvalidWebhookSignatureError('The given webhook signature does not match the expected signature');\n }\n}\n_Webhooks_instances = new WeakSet(), _Webhooks_validateSecret = function _Webhooks_validateSecret(secret) {\n if (typeof secret !== 'string' || secret.length === 0) {\n throw new Error(`The webhook secret must either be set using the env var, OPENAI_WEBHOOK_SECRET, on the client class, OpenAI({ webhookSecret: '123' }), or passed to this function`);\n }\n}, _Webhooks_getRequiredHeader = function _Webhooks_getRequiredHeader(headers, name) {\n if (!headers) {\n throw new Error(`Headers are required`);\n }\n const value = headers.get(name);\n if (value === null || value === undefined) {\n throw new Error(`Missing required header: ${name}`);\n }\n return value;\n};\n//# sourceMappingURL=webhooks.mjs.map","// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.\nvar _OpenAI_instances, _a, _OpenAI_encoder, _OpenAI_baseURLOverridden;\nimport { __classPrivateFieldGet, __classPrivateFieldSet } from \"./internal/tslib.mjs\";\nimport { uuid4 } from \"./internal/utils/uuid.mjs\";\nimport { validatePositiveInteger, isAbsoluteURL, safeJSON } from \"./internal/utils/values.mjs\";\nimport { sleep } from \"./internal/utils/sleep.mjs\";\nimport { castToError, isAbortError } from \"./internal/errors.mjs\";\nimport { getPlatformHeaders } from \"./internal/detect-platform.mjs\";\nimport * as Shims from \"./internal/shims.mjs\";\nimport * as Opts from \"./internal/request-options.mjs\";\nimport * as qs from \"./internal/qs/index.mjs\";\nimport { VERSION } from \"./version.mjs\";\nimport * as Errors from \"./core/error.mjs\";\nimport * as Pagination from \"./core/pagination.mjs\";\nimport * as Uploads from \"./core/uploads.mjs\";\nimport * as API from \"./resources/index.mjs\";\nimport { APIPromise } from \"./core/api-promise.mjs\";\nimport { Batches, } from \"./resources/batches.mjs\";\nimport { Completions, } from \"./resources/completions.mjs\";\nimport { Embeddings, } from \"./resources/embeddings.mjs\";\nimport { Files, } from \"./resources/files.mjs\";\nimport { Images, } from \"./resources/images.mjs\";\nimport { Models } from \"./resources/models.mjs\";\nimport { Moderations, } from \"./resources/moderations.mjs\";\nimport { Videos, } from \"./resources/videos.mjs\";\nimport { Webhooks } from \"./resources/webhooks.mjs\";\nimport { Audio } from \"./resources/audio/audio.mjs\";\nimport { Beta } from \"./resources/beta/beta.mjs\";\nimport { Chat } from \"./resources/chat/chat.mjs\";\nimport { Containers, } from \"./resources/containers/containers.mjs\";\nimport { Conversations } from \"./resources/conversations/conversations.mjs\";\nimport { Evals, } from \"./resources/evals/evals.mjs\";\nimport { FineTuning } from \"./resources/fine-tuning/fine-tuning.mjs\";\nimport { Graders } from \"./resources/graders/graders.mjs\";\nimport { Realtime } from \"./resources/realtime/realtime.mjs\";\nimport { Responses } from \"./resources/responses/responses.mjs\";\nimport { Uploads as UploadsAPIUploads, } from \"./resources/uploads/uploads.mjs\";\nimport { VectorStores, } from \"./resources/vector-stores/vector-stores.mjs\";\nimport { isRunningInBrowser } from \"./internal/detect-platform.mjs\";\nimport { buildHeaders } from \"./internal/headers.mjs\";\nimport { readEnv } from \"./internal/utils/env.mjs\";\nimport { formatRequestDetails, loggerFor, parseLogLevel, } from \"./internal/utils/log.mjs\";\nimport { isEmptyObj } from \"./internal/utils/values.mjs\";\n/**\n * API Client for interfacing with the OpenAI API.\n */\nexport class OpenAI {\n /**\n * API Client for interfacing with the OpenAI API.\n *\n * @param {string | undefined} [opts.apiKey=process.env['OPENAI_API_KEY'] ?? undefined]\n * @param {string | null | undefined} [opts.organization=process.env['OPENAI_ORG_ID'] ?? null]\n * @param {string | null | undefined} [opts.project=process.env['OPENAI_PROJECT_ID'] ?? null]\n * @param {string | null | undefined} [opts.webhookSecret=process.env['OPENAI_WEBHOOK_SECRET'] ?? null]\n * @param {string} [opts.baseURL=process.env['OPENAI_BASE_URL'] ?? https://api.openai.com/v1] - Override the default base URL for the API.\n * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.\n * @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.\n * @param {Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.\n * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.\n * @param {HeadersLike} opts.defaultHeaders - Default headers to include with every request to the API.\n * @param {Record<string, string | undefined>} opts.defaultQuery - Default query parameters to include with every request to the API.\n * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.\n */\n constructor({ baseURL = readEnv('OPENAI_BASE_URL'), apiKey = readEnv('OPENAI_API_KEY'), organization = readEnv('OPENAI_ORG_ID') ?? null, project = readEnv('OPENAI_PROJECT_ID') ?? null, webhookSecret = readEnv('OPENAI_WEBHOOK_SECRET') ?? null, ...opts } = {}) {\n _OpenAI_instances.add(this);\n _OpenAI_encoder.set(this, void 0);\n this.completions = new API.Completions(this);\n this.chat = new API.Chat(this);\n this.embeddings = new API.Embeddings(this);\n this.files = new API.Files(this);\n this.images = new API.Images(this);\n this.audio = new API.Audio(this);\n this.moderations = new API.Moderations(this);\n this.models = new API.Models(this);\n this.fineTuning = new API.FineTuning(this);\n this.graders = new API.Graders(this);\n this.vectorStores = new API.VectorStores(this);\n this.webhooks = new API.Webhooks(this);\n this.beta = new API.Beta(this);\n this.batches = new API.Batches(this);\n this.uploads = new API.Uploads(this);\n this.responses = new API.Responses(this);\n this.realtime = new API.Realtime(this);\n this.conversations = new API.Conversations(this);\n this.evals = new API.Evals(this);\n this.containers = new API.Containers(this);\n this.videos = new API.Videos(this);\n if (apiKey === undefined) {\n throw new Errors.OpenAIError('Missing credentials. Please pass an `apiKey`, or set the `OPENAI_API_KEY` environment variable.');\n }\n const options = {\n apiKey,\n organization,\n project,\n webhookSecret,\n ...opts,\n baseURL: baseURL || `https://api.openai.com/v1`,\n };\n if (!options.dangerouslyAllowBrowser && isRunningInBrowser()) {\n throw new Errors.OpenAIError(\"It looks like you're running in a browser-like environment.\\n\\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\\nIf you understand the risks and have appropriate mitigations in place,\\nyou can set the `dangerouslyAllowBrowser` option to `true`, e.g.,\\n\\nnew OpenAI({ apiKey, dangerouslyAllowBrowser: true });\\n\\nhttps://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety\\n\");\n }\n this.baseURL = options.baseURL;\n this.timeout = options.timeout ?? _a.DEFAULT_TIMEOUT /* 10 minutes */;\n this.logger = options.logger ?? console;\n const defaultLogLevel = 'warn';\n // Set default logLevel early so that we can log a warning in parseLogLevel.\n this.logLevel = defaultLogLevel;\n this.logLevel =\n parseLogLevel(options.logLevel, 'ClientOptions.logLevel', this) ??\n parseLogLevel(readEnv('OPENAI_LOG'), \"process.env['OPENAI_LOG']\", this) ??\n defaultLogLevel;\n this.fetchOptions = options.fetchOptions;\n this.maxRetries = options.maxRetries ?? 2;\n this.fetch = options.fetch ?? Shims.getDefaultFetch();\n __classPrivateFieldSet(this, _OpenAI_encoder, Opts.FallbackEncoder, \"f\");\n this._options = options;\n this.apiKey = typeof apiKey === 'string' ? apiKey : 'Missing Key';\n this.organization = organization;\n this.project = project;\n this.webhookSecret = webhookSecret;\n }\n /**\n * Create a new client instance re-using the same options given to the current client with optional overriding.\n */\n withOptions(options) {\n const client = new this.constructor({\n ...this._options,\n baseURL: this.baseURL,\n maxRetries: this.maxRetries,\n timeout: this.timeout,\n logger: this.logger,\n logLevel: this.logLevel,\n fetch: this.fetch,\n fetchOptions: this.fetchOptions,\n apiKey: this.apiKey,\n organization: this.organization,\n project: this.project,\n webhookSecret: this.webhookSecret,\n ...options,\n });\n return client;\n }\n defaultQuery() {\n return this._options.defaultQuery;\n }\n validateHeaders({ values, nulls }) {\n return;\n }\n async authHeaders(opts) {\n return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);\n }\n stringifyQuery(query) {\n return qs.stringify(query, { arrayFormat: 'brackets' });\n }\n getUserAgent() {\n return `${this.constructor.name}/JS ${VERSION}`;\n }\n defaultIdempotencyKey() {\n return `stainless-node-retry-${uuid4()}`;\n }\n makeStatusError(status, error, message, headers) {\n return Errors.APIError.generate(status, error, message, headers);\n }\n async _callApiKey() {\n const apiKey = this._options.apiKey;\n if (typeof apiKey !== 'function')\n return false;\n let token;\n try {\n token = await apiKey();\n }\n catch (err) {\n if (err instanceof Errors.OpenAIError)\n throw err;\n throw new Errors.OpenAIError(`Failed to get token from 'apiKey' function: ${err.message}`, \n // @ts-ignore\n { cause: err });\n }\n if (typeof token !== 'string' || !token) {\n throw new Errors.OpenAIError(`Expected 'apiKey' function argument to return a string but it returned ${token}`);\n }\n this.apiKey = token;\n return true;\n }\n buildURL(path, query, defaultBaseURL) {\n const baseURL = (!__classPrivateFieldGet(this, _OpenAI_instances, \"m\", _OpenAI_baseURLOverridden).call(this) && defaultBaseURL) || this.baseURL;\n const url = isAbsoluteURL(path) ?\n new URL(path)\n : new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));\n const defaultQuery = this.defaultQuery();\n if (!isEmptyObj(defaultQuery)) {\n query = { ...defaultQuery, ...query };\n }\n if (typeof query === 'object' && query && !Array.isArray(query)) {\n url.search = this.stringifyQuery(query);\n }\n return url.toString();\n }\n /**\n * Used as a callback for mutating the given `FinalRequestOptions` object.\n */\n async prepareOptions(options) {\n await this._callApiKey();\n }\n /**\n * Used as a callback for mutating the given `RequestInit` object.\n *\n * This is useful for cases where you want to add certain headers based off of\n * the request properties, e.g. `method` or `url`.\n */\n async prepareRequest(request, { url, options }) { }\n get(path, opts) {\n return this.methodRequest('get', path, opts);\n }\n post(path, opts) {\n return this.methodRequest('post', path, opts);\n }\n patch(path, opts) {\n return this.methodRequest('patch', path, opts);\n }\n put(path, opts) {\n return this.methodRequest('put', path, opts);\n }\n delete(path, opts) {\n return this.methodRequest('delete', path, opts);\n }\n methodRequest(method, path, opts) {\n return this.request(Promise.resolve(opts).then((opts) => {\n return { method, path, ...opts };\n }));\n }\n request(options, remainingRetries = null) {\n return new APIPromise(this, this.makeRequest(options, remainingRetries, undefined));\n }\n async makeRequest(optionsInput, retriesRemaining, retryOfRequestLogID) {\n const options = await optionsInput;\n const maxRetries = options.maxRetries ?? this.maxRetries;\n if (retriesRemaining == null) {\n retriesRemaining = maxRetries;\n }\n await this.prepareOptions(options);\n const { req, url, timeout } = await this.buildRequest(options, {\n retryCount: maxRetries - retriesRemaining,\n });\n await this.prepareRequest(req, { url, options });\n /** Not an API request ID, just for correlating local log entries. */\n const requestLogID = 'log_' + ((Math.random() * (1 << 24)) | 0).toString(16).padStart(6, '0');\n const retryLogStr = retryOfRequestLogID === undefined ? '' : `, retryOf: ${retryOfRequestLogID}`;\n const startTime = Date.now();\n loggerFor(this).debug(`[${requestLogID}] sending request`, formatRequestDetails({\n retryOfRequestLogID,\n method: options.method,\n url,\n options,\n headers: req.headers,\n }));\n if (options.signal?.aborted) {\n throw new Errors.APIUserAbortError();\n }\n const controller = new AbortController();\n const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);\n const headersTime = Date.now();\n if (response instanceof globalThis.Error) {\n const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;\n if (options.signal?.aborted) {\n throw new Errors.APIUserAbortError();\n }\n // detect native connection timeout errors\n // deno throws \"TypeError: error sending request for url (https://example/): client error (Connect): tcp connect error: Operation timed out (os error 60): Operation timed out (os error 60)\"\n // undici throws \"TypeError: fetch failed\" with cause \"ConnectTimeoutError: Connect Timeout Error (attempted address: example:443, timeout: 1ms)\"\n // others do not provide enough information to distinguish timeouts from other connection errors\n const isTimeout = isAbortError(response) ||\n /timed? ?out/i.test(String(response) + ('cause' in response ? String(response.cause) : ''));\n if (retriesRemaining) {\n loggerFor(this).info(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - ${retryMessage}`);\n loggerFor(this).debug(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (${retryMessage})`, formatRequestDetails({\n retryOfRequestLogID,\n url,\n durationMs: headersTime - startTime,\n message: response.message,\n }));\n return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID);\n }\n loggerFor(this).info(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - error; no more retries left`);\n loggerFor(this).debug(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (error; no more retries left)`, formatRequestDetails({\n retryOfRequestLogID,\n url,\n durationMs: headersTime - startTime,\n message: response.message,\n }));\n if (isTimeout) {\n throw new Errors.APIConnectionTimeoutError();\n }\n throw new Errors.APIConnectionError({ cause: response });\n }\n const specialHeaders = [...response.headers.entries()]\n .filter(([name]) => name === 'x-request-id')\n .map(([name, value]) => ', ' + name + ': ' + JSON.stringify(value))\n .join('');\n const responseInfo = `[${requestLogID}${retryLogStr}${specialHeaders}] ${req.method} ${url} ${response.ok ? 'succeeded' : 'failed'} with status ${response.status} in ${headersTime - startTime}ms`;\n if (!response.ok) {\n const shouldRetry = await this.shouldRetry(response);\n if (retriesRemaining && shouldRetry) {\n const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;\n // We don't need the body of this response.\n await Shims.CancelReadableStream(response.body);\n loggerFor(this).info(`${responseInfo} - ${retryMessage}`);\n loggerFor(this).debug(`[${requestLogID}] response error (${retryMessage})`, formatRequestDetails({\n retryOfRequestLogID,\n url: response.url,\n status: response.status,\n headers: response.headers,\n durationMs: headersTime - startTime,\n }));\n return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID, response.headers);\n }\n const retryMessage = shouldRetry ? `error; no more retries left` : `error; not retryable`;\n loggerFor(this).info(`${responseInfo} - ${retryMessage}`);\n const errText = await response.text().catch((err) => castToError(err).message);\n const errJSON = safeJSON(errText);\n const errMessage = errJSON ? undefined : errText;\n loggerFor(this).debug(`[${requestLogID}] response error (${retryMessage})`, formatRequestDetails({\n retryOfRequestLogID,\n url: response.url,\n status: response.status,\n headers: response.headers,\n message: errMessage,\n durationMs: Date.now() - startTime,\n }));\n const err = this.makeStatusError(response.status, errJSON, errMessage, response.headers);\n throw err;\n }\n loggerFor(this).info(responseInfo);\n loggerFor(this).debug(`[${requestLogID}] response start`, formatRequestDetails({\n retryOfRequestLogID,\n url: response.url,\n status: response.status,\n headers: response.headers,\n durationMs: headersTime - startTime,\n }));\n return { response, options, controller, requestLogID, retryOfRequestLogID, startTime };\n }\n getAPIList(path, Page, opts) {\n return this.requestAPIList(Page, { method: 'get', path, ...opts });\n }\n requestAPIList(Page, options) {\n const request = this.makeRequest(options, null, undefined);\n return new Pagination.PagePromise(this, request, Page);\n }\n async fetchWithTimeout(url, init, ms, controller) {\n const { signal, method, ...options } = init || {};\n if (signal)\n signal.addEventListener('abort', () => controller.abort());\n const timeout = setTimeout(() => controller.abort(), ms);\n const isReadableBody = (globalThis.ReadableStream && options.body instanceof globalThis.ReadableStream) ||\n (typeof options.body === 'object' && options.body !== null && Symbol.asyncIterator in options.body);\n const fetchOptions = {\n signal: controller.signal,\n ...(isReadableBody ? { duplex: 'half' } : {}),\n method: 'GET',\n ...options,\n };\n if (method) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = method.toUpperCase();\n }\n try {\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n return await this.fetch.call(undefined, url, fetchOptions);\n }\n finally {\n clearTimeout(timeout);\n }\n }\n async shouldRetry(response) {\n // Note this is not a standard header.\n const shouldRetryHeader = response.headers.get('x-should-retry');\n // If the server explicitly says whether or not to retry, obey.\n if (shouldRetryHeader === 'true')\n return true;\n if (shouldRetryHeader === 'false')\n return false;\n // Retry on request timeouts.\n if (response.status === 408)\n return true;\n // Retry on lock timeouts.\n if (response.status === 409)\n return true;\n // Retry on rate limits.\n if (response.status === 429)\n return true;\n // Retry internal errors.\n if (response.status >= 500)\n return true;\n return false;\n }\n async retryRequest(options, retriesRemaining, requestLogID, responseHeaders) {\n let timeoutMillis;\n // Note the `retry-after-ms` header may not be standard, but is a good idea and we'd like proactive support for it.\n const retryAfterMillisHeader = responseHeaders?.get('retry-after-ms');\n if (retryAfterMillisHeader) {\n const timeoutMs = parseFloat(retryAfterMillisHeader);\n if (!Number.isNaN(timeoutMs)) {\n timeoutMillis = timeoutMs;\n }\n }\n // About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After\n const retryAfterHeader = responseHeaders?.get('retry-after');\n if (retryAfterHeader && !timeoutMillis) {\n const timeoutSeconds = parseFloat(retryAfterHeader);\n if (!Number.isNaN(timeoutSeconds)) {\n timeoutMillis = timeoutSeconds * 1000;\n }\n else {\n timeoutMillis = Date.parse(retryAfterHeader) - Date.now();\n }\n }\n // If the API asks us to wait a certain amount of time (and it's a reasonable amount),\n // just do what it says, but otherwise calculate a default\n if (!(timeoutMillis && 0 <= timeoutMillis && timeoutMillis < 60 * 1000)) {\n const maxRetries = options.maxRetries ?? this.maxRetries;\n timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);\n }\n await sleep(timeoutMillis);\n return this.makeRequest(options, retriesRemaining - 1, requestLogID);\n }\n calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries) {\n const initialRetryDelay = 0.5;\n const maxRetryDelay = 8.0;\n const numRetries = maxRetries - retriesRemaining;\n // Apply exponential backoff, but not more than the max.\n const sleepSeconds = Math.min(initialRetryDelay * Math.pow(2, numRetries), maxRetryDelay);\n // Apply some jitter, take up to at most 25 percent of the retry time.\n const jitter = 1 - Math.random() * 0.25;\n return sleepSeconds * jitter * 1000;\n }\n async buildRequest(inputOptions, { retryCount = 0 } = {}) {\n const options = { ...inputOptions };\n const { method, path, query, defaultBaseURL } = options;\n const url = this.buildURL(path, query, defaultBaseURL);\n if ('timeout' in options)\n validatePositiveInteger('timeout', options.timeout);\n options.timeout = options.timeout ?? this.timeout;\n const { bodyHeaders, body } = this.buildBody({ options });\n const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });\n const req = {\n method,\n headers: reqHeaders,\n ...(options.signal && { signal: options.signal }),\n ...(globalThis.ReadableStream &&\n body instanceof globalThis.ReadableStream && { duplex: 'half' }),\n ...(body && { body }),\n ...(this.fetchOptions ?? {}),\n ...(options.fetchOptions ?? {}),\n };\n return { req, url, timeout: options.timeout };\n }\n async buildHeaders({ options, method, bodyHeaders, retryCount, }) {\n let idempotencyHeaders = {};\n if (this.idempotencyHeader && method !== 'get') {\n if (!options.idempotencyKey)\n options.idempotencyKey = this.defaultIdempotencyKey();\n idempotencyHeaders[this.idempotencyHeader] = options.idempotencyKey;\n }\n const headers = buildHeaders([\n idempotencyHeaders,\n {\n Accept: 'application/json',\n 'User-Agent': this.getUserAgent(),\n 'X-Stainless-Retry-Count': String(retryCount),\n ...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),\n ...getPlatformHeaders(),\n 'OpenAI-Organization': this.organization,\n 'OpenAI-Project': this.project,\n },\n await this.authHeaders(options),\n this._options.defaultHeaders,\n bodyHeaders,\n options.headers,\n ]);\n this.validateHeaders(headers);\n return headers.values;\n }\n buildBody({ options: { body, headers: rawHeaders } }) {\n if (!body) {\n return { bodyHeaders: undefined, body: undefined };\n }\n const headers = buildHeaders([rawHeaders]);\n if (\n // Pass raw type verbatim\n ArrayBuffer.isView(body) ||\n body instanceof ArrayBuffer ||\n body instanceof DataView ||\n (typeof body === 'string' &&\n // Preserve legacy string encoding behavior for now\n headers.values.has('content-type')) ||\n // `Blob` is superset of `File`\n (globalThis.Blob && body instanceof globalThis.Blob) ||\n // `FormData` -> `multipart/form-data`\n body instanceof FormData ||\n // `URLSearchParams` -> `application/x-www-form-urlencoded`\n body instanceof URLSearchParams ||\n // Send chunked stream (each chunk has own `length`)\n (globalThis.ReadableStream && body instanceof globalThis.ReadableStream)) {\n return { bodyHeaders: undefined, body: body };\n }\n else if (typeof body === 'object' &&\n (Symbol.asyncIterator in body ||\n (Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))) {\n return { bodyHeaders: undefined, body: Shims.ReadableStreamFrom(body) };\n }\n else {\n return __classPrivateFieldGet(this, _OpenAI_encoder, \"f\").call(this, { body, headers });\n }\n }\n}\n_a = OpenAI, _OpenAI_encoder = new WeakMap(), _OpenAI_instances = new WeakSet(), _OpenAI_baseURLOverridden = function _OpenAI_baseURLOverridden() {\n return this.baseURL !== 'https://api.openai.com/v1';\n};\nOpenAI.OpenAI = _a;\nOpenAI.DEFAULT_TIMEOUT = 600000; // 10 minutes\nOpenAI.OpenAIError = Errors.OpenAIError;\nOpenAI.APIError = Errors.APIError;\nOpenAI.APIConnectionError = Errors.APIConnectionError;\nOpenAI.APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;\nOpenAI.APIUserAbortError = Errors.APIUserAbortError;\nOpenAI.NotFoundError = Errors.NotFoundError;\nOpenAI.ConflictError = Errors.ConflictError;\nOpenAI.RateLimitError = Errors.RateLimitError;\nOpenAI.BadRequestError = Errors.BadRequestError;\nOpenAI.AuthenticationError = Errors.AuthenticationError;\nOpenAI.InternalServerError = Errors.InternalServerError;\nOpenAI.PermissionDeniedError = Errors.PermissionDeniedError;\nOpenAI.UnprocessableEntityError = Errors.UnprocessableEntityError;\nOpenAI.InvalidWebhookSignatureError = Errors.InvalidWebhookSignatureError;\nOpenAI.toFile = Uploads.toFile;\nOpenAI.Completions = Completions;\nOpenAI.Chat = Chat;\nOpenAI.Embeddings = Embeddings;\nOpenAI.Files = Files;\nOpenAI.Images = Images;\nOpenAI.Audio = Audio;\nOpenAI.Moderations = Moderations;\nOpenAI.Models = Models;\nOpenAI.FineTuning = FineTuning;\nOpenAI.Graders = Graders;\nOpenAI.VectorStores = VectorStores;\nOpenAI.Webhooks = Webhooks;\nOpenAI.Beta = Beta;\nOpenAI.Batches = Batches;\nOpenAI.Uploads = UploadsAPIUploads;\nOpenAI.Responses = Responses;\nOpenAI.Realtime = Realtime;\nOpenAI.Conversations = Conversations;\nOpenAI.Evals = Evals;\nOpenAI.Containers = Containers;\nOpenAI.Videos = Videos;\n//# sourceMappingURL=client.mjs.map","import { ToolMessage } from \"@langchain/core/messages\";\nimport { tool } from \"@langchain/core/tools\";\nimport { z } from \"zod/v4\";\n\n//#region src/tools/computerUse.ts\nconst ComputerUseScreenshotActionSchema = z.object({ type: z.literal(\"screenshot\") });\nconst ComputerUseClickActionSchema = z.object({\n\ttype: z.literal(\"click\"),\n\tx: z.number(),\n\ty: z.number(),\n\tbutton: z.enum([\n\t\t\"left\",\n\t\t\"right\",\n\t\t\"wheel\",\n\t\t\"back\",\n\t\t\"forward\"\n\t]).default(\"left\")\n});\nconst ComputerUseDoubleClickActionSchema = z.object({\n\ttype: z.literal(\"double_click\"),\n\tx: z.number(),\n\ty: z.number(),\n\tbutton: z.enum([\n\t\t\"left\",\n\t\t\"right\",\n\t\t\"wheel\",\n\t\t\"back\",\n\t\t\"forward\"\n\t]).default(\"left\")\n});\nconst ComputerUseDragActionSchema = z.object({\n\ttype: z.literal(\"drag\"),\n\tpath: z.array(z.object({\n\t\tx: z.number(),\n\t\ty: z.number()\n\t}))\n});\nconst ComputerUseKeypressActionSchema = z.object({\n\ttype: z.literal(\"keypress\"),\n\tkeys: z.array(z.string())\n});\nconst ComputerUseMoveActionSchema = z.object({\n\ttype: z.literal(\"move\"),\n\tx: z.number(),\n\ty: z.number()\n});\nconst ComputerUseScrollActionSchema = z.object({\n\ttype: z.literal(\"scroll\"),\n\tx: z.number(),\n\ty: z.number(),\n\tscroll_x: z.number(),\n\tscroll_y: z.number()\n});\nconst ComputerUseTypeActionSchema = z.object({\n\ttype: z.literal(\"type\"),\n\ttext: z.string()\n});\nconst ComputerUseWaitActionSchema = z.object({\n\ttype: z.literal(\"wait\"),\n\tduration: z.number().optional()\n});\nconst ComputerUseActionUnionSchema = z.discriminatedUnion(\"type\", [\n\tComputerUseScreenshotActionSchema,\n\tComputerUseClickActionSchema,\n\tComputerUseDoubleClickActionSchema,\n\tComputerUseDragActionSchema,\n\tComputerUseKeypressActionSchema,\n\tComputerUseMoveActionSchema,\n\tComputerUseScrollActionSchema,\n\tComputerUseTypeActionSchema,\n\tComputerUseWaitActionSchema\n]);\nconst ComputerUseActionSchema = z.object({ action: ComputerUseActionUnionSchema });\nconst TOOL_NAME = \"computer_use\";\n/**\n* Creates a Computer Use tool that allows models to control computer interfaces\n* and perform tasks by simulating mouse clicks, keyboard input, scrolling, and more.\n*\n* **Computer Use** is a practical application of OpenAI's Computer-Using Agent (CUA)\n* model (`computer-use-preview`), which combines vision capabilities with advanced\n* reasoning to simulate controlling computer interfaces.\n*\n* **How it works**:\n* The tool operates in a continuous loop:\n* 1. Model sends computer actions (click, type, scroll, etc.)\n* 2. Your code executes these actions in a controlled environment\n* 3. You capture a screenshot of the result\n* 4. Send the screenshot back to the model\n* 5. Repeat until the task is complete\n*\n* **Important**: Computer use is in beta and requires careful consideration:\n* - Use in sandboxed environments only\n* - Do not use for high-stakes or authenticated tasks\n* - Always implement human-in-the-loop for important decisions\n* - Handle safety checks appropriately\n*\n* @see {@link https://platform.openai.com/docs/guides/tools-computer-use | OpenAI Computer Use Documentation}\n*\n* @param options - Configuration options for the Computer Use tool\n* @returns A Computer Use tool that can be passed to `bindTools`\n*\n* @example\n* ```typescript\n* import { ChatOpenAI, tools } from \"@langchain/openai\";\n*\n* const model = new ChatOpenAI({ model: \"computer-use-preview\" });\n*\n* // With execute callback for automatic action handling\n* const computer = tools.computerUse({\n* displayWidth: 1024,\n* displayHeight: 768,\n* environment: \"browser\",\n* execute: async (action) => {\n* if (action.type === \"screenshot\") {\n* return captureScreenshot();\n* }\n* if (action.type === \"click\") {\n* await page.mouse.click(action.x, action.y, { button: action.button });\n* return captureScreenshot();\n* }\n* if (action.type === \"type\") {\n* await page.keyboard.type(action.text);\n* return captureScreenshot();\n* }\n* // Handle other actions...\n* return captureScreenshot();\n* },\n* });\n*\n* const llmWithComputer = model.bindTools([computer]);\n* const response = await llmWithComputer.invoke(\n* \"Check the latest news on bing.com\"\n* );\n* ```\n*\n* @example\n* ```typescript\n* // Without execute callback (manual action handling)\n* const computer = tools.computerUse({\n* displayWidth: 1024,\n* displayHeight: 768,\n* environment: \"browser\",\n* });\n*\n* const response = await model.invoke(\"Check the news\", {\n* tools: [computer],\n* });\n*\n* // Access the computer call from the response\n* const computerCall = response.additional_kwargs.tool_outputs?.find(\n* (output) => output.type === \"computer_call\"\n* );\n* if (computerCall) {\n* console.log(\"Action to execute:\", computerCall.action);\n* // Execute the action manually, then send back a screenshot\n* }\n* ```\n*\n* @example\n* ```typescript\n* // For macOS desktop automation with Docker\n* const computer = tools.computerUse({\n* displayWidth: 1920,\n* displayHeight: 1080,\n* environment: \"mac\",\n* execute: async (action) => {\n* if (action.type === \"click\") {\n* await dockerExec(\n* `DISPLAY=:99 xdotool mousemove ${action.x} ${action.y} click 1`,\n* containerName\n* );\n* }\n* // Capture screenshot from container\n* return await getDockerScreenshot(containerName);\n* },\n* });\n* ```\n*\n* @remarks\n* - Only available through the Responses API (not Chat Completions)\n* - Requires `computer-use-preview` model\n* - Actions include: click, double_click, drag, keypress, move, screenshot, scroll, type, wait\n* - Safety checks may be returned that require acknowledgment before proceeding\n* - Use `truncation: \"auto\"` parameter when making requests\n* - Recommended to use with `reasoning.summary` for debugging\n*/\nfunction computerUse(options) {\n\tconst computerTool = tool(async (input, runtime) => {\n\t\t/**\n\t\t* get computer_use call id from runtime\n\t\t*/\n\t\tconst aiMessage = runtime.state?.messages.at(-1);\n\t\tconst computerToolCall = aiMessage?.tool_calls?.find((tc) => tc.name === \"computer_use\");\n\t\tconst computerToolCallId = computerToolCall?.id;\n\t\tif (!computerToolCallId) throw new Error(\"Computer use call id not found\");\n\t\tconst result = await options.execute(input.action, runtime);\n\t\t/**\n\t\t* make sure {@link ToolMessage} is returned with the correct additional kwargs\n\t\t*/\n\t\tif (typeof result === \"string\") return new ToolMessage({\n\t\t\tcontent: result,\n\t\t\ttool_call_id: computerToolCallId,\n\t\t\tadditional_kwargs: { type: \"computer_call_output\" }\n\t\t});\n\t\t/**\n\t\t* make sure {@link ToolMessage} is returned with the correct additional kwargs\n\t\t*/\n\t\treturn new ToolMessage({\n\t\t\t...result,\n\t\t\ttool_call_id: computerToolCallId,\n\t\t\tadditional_kwargs: {\n\t\t\t\ttype: \"computer_call_output\",\n\t\t\t\t...result.additional_kwargs\n\t\t\t}\n\t\t});\n\t}, {\n\t\tname: TOOL_NAME,\n\t\tdescription: \"Control a computer interface by executing mouse clicks, keyboard input, scrolling, and other actions.\",\n\t\tschema: ComputerUseActionSchema\n\t});\n\tcomputerTool.extras = {\n\t\t...computerTool.extras ?? {},\n\t\tproviderToolDefinition: {\n\t\t\ttype: \"computer_use_preview\",\n\t\t\tdisplay_width: options.displayWidth,\n\t\t\tdisplay_height: options.displayHeight,\n\t\t\tenvironment: options.environment\n\t\t}\n\t};\n\t/**\n\t* return as typed {@link DynamicStructuredTool} so we don't get any type\n\t* errors like \"can't export tool without reference\"\n\t*/\n\treturn computerTool;\n}\n\n//#endregion\nexport { computerUse };\n//# sourceMappingURL=computerUse.js.map","import { tool } from \"@langchain/core/tools\";\nimport { z } from \"zod/v4\";\n\n//#region src/tools/localShell.ts\nconst LocalShellExecActionSchema = z.object({\n\ttype: z.literal(\"exec\"),\n\tcommand: z.array(z.string()),\n\tenv: z.record(z.string(), z.string()).optional(),\n\tworking_directory: z.string().optional(),\n\ttimeout_ms: z.number().optional(),\n\tuser: z.string().optional()\n});\nconst LocalShellActionSchema = z.discriminatedUnion(\"type\", [LocalShellExecActionSchema]);\nconst TOOL_NAME = \"local_shell\";\n/**\n* Creates a Local Shell tool that allows models to run shell commands locally\n* on a machine you provide. Commands are executed inside your own runtime—\n* the API only returns the instructions, but does not execute them on OpenAI infrastructure.\n*\n* **Important**: The local shell tool is designed to work with\n* [Codex CLI](https://github.com/openai/codex) and the `codex-mini-latest` model.\n*\n* **How it works**:\n* The tool operates in a continuous loop:\n* 1. Model sends shell commands (`local_shell_call` with `exec` action)\n* 2. Your code executes the command locally\n* 3. You return the output back to the model\n* 4. Repeat until the task is complete\n*\n* **Security Warning**: Running arbitrary shell commands can be dangerous.\n* Always sandbox execution or add strict allow/deny-lists before forwarding\n* a command to the system shell.\n*\n* @see {@link https://platform.openai.com/docs/guides/tools-local-shell | OpenAI Local Shell Documentation}\n*\n* @param options - Optional configuration for the Local Shell tool\n* @returns A Local Shell tool that can be passed to `bindTools`\n*\n* @example\n* ```typescript\n* import { ChatOpenAI, tools } from \"@langchain/openai\";\n* import { exec } from \"child_process\";\n* import { promisify } from \"util\";\n*\n* const execAsync = promisify(exec);\n* const model = new ChatOpenAI({ model: \"codex-mini-latest\" });\n*\n* // With execute callback for automatic command handling\n* const shell = tools.localShell({\n* execute: async (action) => {\n* const { command, env, working_directory, timeout_ms } = action;\n* const result = await execAsync(command.join(' '), {\n* cwd: working_directory ?? process.cwd(),\n* env: { ...process.env, ...env },\n* timeout: timeout_ms ?? undefined,\n* });\n* return result.stdout + result.stderr;\n* },\n* });\n*\n* const llmWithShell = model.bindTools([shell]);\n* const response = await llmWithShell.invoke(\n* \"List files in the current directory\"\n* );\n* ```\n*\n* @example\n* ```typescript\n* // Without execute callback (manual handling)\n* const shell = tools.localShell();\n*\n* const response = await model.invoke(\"List files\", {\n* tools: [shell],\n* });\n*\n* // Access the shell call from the response\n* const shellCall = response.additional_kwargs.tool_outputs?.find(\n* (output) => output.type === \"local_shell_call\"\n* );\n* if (shellCall) {\n* console.log(\"Command to execute:\", shellCall.action.command);\n* // Execute the command manually, then send back the output\n* }\n* ```\n*\n* @example\n* ```typescript\n* // Full shell loop example\n* async function shellLoop(model, task) {\n* let response = await model.invoke(task, {\n* tools: [tools.localShell()],\n* });\n*\n* while (true) {\n* const shellCall = response.additional_kwargs.tool_outputs?.find(\n* (output) => output.type === \"local_shell_call\"\n* );\n*\n* if (!shellCall) break;\n*\n* // Execute command (with proper sandboxing!)\n* const output = await executeCommand(shellCall.action);\n*\n* // Send output back to model\n* response = await model.invoke([\n* response,\n* {\n* type: \"local_shell_call_output\",\n* id: shellCall.call_id,\n* output: output,\n* },\n* ], {\n* tools: [tools.localShell()],\n* });\n* }\n*\n* return response;\n* }\n* ```\n*\n* @remarks\n* - Only available through the Responses API (not Chat Completions)\n* - Designed for use with `codex-mini-latest` model\n* - Commands are provided as argv tokens in `action.command`\n* - Action includes: `command`, `env`, `working_directory`, `timeout_ms`, `user`\n* - Always sandbox or validate commands before execution\n* - The `timeout_ms` from the model is only a hint—enforce your own limits\n*/\nfunction localShell(options) {\n\tconst shellTool = tool(options.execute, {\n\t\tname: TOOL_NAME,\n\t\tdescription: \"Execute shell commands locally on the machine. Commands are provided as argv tokens.\",\n\t\tschema: LocalShellActionSchema\n\t});\n\tshellTool.extras = {\n\t\t...shellTool.extras ?? {},\n\t\tproviderToolDefinition: { type: \"local_shell\" }\n\t};\n\treturn shellTool;\n}\n\n//#endregion\nexport { localShell };\n//# sourceMappingURL=localShell.js.map","import { tool } from \"@langchain/core/tools\";\nimport { z } from \"zod/v4\";\n\n//#region src/tools/shell.ts\nconst ShellActionSchema = z.object({\n\tcommands: z.array(z.string()).describe(\"Array of shell commands to execute\"),\n\ttimeout_ms: z.number().optional().describe(\"Optional timeout in milliseconds for the commands\"),\n\tmax_output_length: z.number().optional().describe(\"Optional maximum number of characters to return from each command\")\n});\nconst TOOL_NAME = \"shell\";\n/**\n* Creates a Shell tool that allows models to run shell commands through your integration.\n*\n* The shell tool allows the model to interact with your local computer through a controlled\n* command-line interface. The model proposes shell commands; your integration executes them\n* and returns the outputs. This creates a simple plan-execute loop that lets models inspect\n* the system, run utilities, and gather data until they can finish the task.\n*\n* **Important**: The shell tool is available through the Responses API for use with `GPT-5.1`.\n* It is not available on other models, or via the Chat Completions API.\n*\n* **When to use**:\n* - **Automating filesystem or process diagnostics** – For example, \"find the largest PDF\n* under ~/Documents\" or \"show running gunicorn processes.\"\n* - **Extending the model's capabilities** – Using built-in UNIX utilities, python runtime\n* and other CLIs in your environment.\n* - **Running multi-step build and test flows** – Chaining commands like `pip install` and `pytest`.\n* - **Complex agentic coding workflows** – Using other tools like `apply_patch` to complete\n* workflows that involve complex file operations.\n*\n* **How it works**:\n* The tool operates in a continuous loop:\n* 1. Model sends shell commands (`shell_call` with `commands` array)\n* 2. Your code executes the commands (can be concurrent)\n* 3. You return stdout, stderr, and outcome for each command\n* 4. Repeat until the task is complete\n*\n* **Security Warning**: Running arbitrary shell commands can be dangerous.\n* Always sandbox execution or add strict allow/deny-lists before forwarding\n* a command to the system shell.\n*\n* @see {@link https://platform.openai.com/docs/guides/tools-shell | OpenAI Shell Documentation}\n* @see {@link https://github.com/openai/codex | Codex CLI} for reference implementation.\n*\n* @param options - Configuration for the Shell tool\n* @returns A Shell tool that can be passed to `bindTools`\n*\n* @example\n* ```typescript\n* import { ChatOpenAI, tools } from \"@langchain/openai\";\n* import { exec } from \"child_process/promises\";\n*\n* const model = new ChatOpenAI({ model: \"gpt-5.1\" });\n*\n* // With execute callback for automatic command handling\n* const shellTool = tools.shell({\n* execute: async (action) => {\n* const outputs = await Promise.all(\n* action.commands.map(async (cmd) => {\n* try {\n* const { stdout, stderr } = await exec(cmd, {\n* timeout: action.timeout_ms ?? undefined,\n* });\n* return {\n* stdout,\n* stderr,\n* outcome: { type: \"exit\" as const, exit_code: 0 },\n* };\n* } catch (error) {\n* const timedOut = error.killed && error.signal === \"SIGTERM\";\n* return {\n* stdout: error.stdout ?? \"\",\n* stderr: error.stderr ?? String(error),\n* outcome: timedOut\n* ? { type: \"timeout\" as const }\n* : { type: \"exit\" as const, exit_code: error.code ?? 1 },\n* };\n* }\n* })\n* );\n* return {\n* output: outputs,\n* maxOutputLength: action.max_output_length,\n* };\n* },\n* });\n*\n* const llmWithShell = model.bindTools([shellTool]);\n* const response = await llmWithShell.invoke(\n* \"Find the largest PDF file in ~/Documents\"\n* );\n* ```\n*\n* @example\n* ```typescript\n* // Full shell loop example\n* async function shellLoop(model, task) {\n* let response = await model.invoke(task, {\n* tools: [tools.shell({ execute: myExecutor })],\n* });\n*\n* while (true) {\n* const shellCall = response.additional_kwargs.tool_outputs?.find(\n* (output) => output.type === \"shell_call\"\n* );\n*\n* if (!shellCall) break;\n*\n* // Execute commands (with proper sandboxing!)\n* const result = await executeCommands(shellCall.action);\n*\n* // Send output back to model\n* response = await model.invoke([\n* response,\n* {\n* type: \"shell_call_output\",\n* call_id: shellCall.call_id,\n* output: result.output,\n* max_output_length: result.maxOutputLength,\n* },\n* ], {\n* tools: [tools.shell({ execute: myExecutor })],\n* });\n* }\n*\n* return response;\n* }\n* ```\n*\n* @remarks\n* - Only available through the Responses API (not Chat Completions)\n* - Designed for use with `gpt-5.1` model\n* - Commands are provided as an array of strings that can be executed concurrently\n* - Action includes: `commands`, `timeout_ms`, `max_output_length`\n* - Always sandbox or validate commands before execution\n* - The `timeout_ms` from the model is only a hint—enforce your own limits\n* - If `max_output_length` exists in the action, always pass it back in the output\n* - Many CLI tools return non-zero exit codes for warnings; still capture stdout/stderr\n*/\nfunction shell(options) {\n\tconst executeWrapper = async (action) => {\n\t\tconst result = await options.execute(action);\n\t\treturn JSON.stringify({\n\t\t\toutput: result.output,\n\t\t\tmax_output_length: result.maxOutputLength\n\t\t});\n\t};\n\tconst shellTool = tool(executeWrapper, {\n\t\tname: TOOL_NAME,\n\t\tdescription: \"Execute shell commands in a managed environment. Commands can be run concurrently.\",\n\t\tschema: ShellActionSchema\n\t});\n\tshellTool.extras = {\n\t\t...shellTool.extras ?? {},\n\t\tproviderToolDefinition: { type: \"shell\" }\n\t};\n\treturn shellTool;\n}\n\n//#endregion\nexport { shell };\n//# sourceMappingURL=shell.js.map","import { tool } from \"@langchain/core/tools\";\nimport { z } from \"zod/v4\";\n\n//#region src/tools/applyPatch.ts\nconst ApplyPatchCreateFileOperationSchema = z.object({\n\ttype: z.literal(\"create_file\"),\n\tpath: z.string(),\n\tdiff: z.string()\n});\nconst ApplyPatchUpdateFileOperationSchema = z.object({\n\ttype: z.literal(\"update_file\"),\n\tpath: z.string(),\n\tdiff: z.string()\n});\nconst ApplyPatchDeleteFileOperationSchema = z.object({\n\ttype: z.literal(\"delete_file\"),\n\tpath: z.string()\n});\nconst ApplyPatchOperationSchema = z.discriminatedUnion(\"type\", [\n\tApplyPatchCreateFileOperationSchema,\n\tApplyPatchUpdateFileOperationSchema,\n\tApplyPatchDeleteFileOperationSchema\n]);\nconst TOOL_NAME = \"apply_patch\";\n/**\n* Creates an Apply Patch tool that allows models to propose structured diffs\n* that your integration applies. This enables iterative, multi-step code\n* editing workflows.\n*\n* **Apply Patch** lets GPT-5.1 create, update, and delete files in your codebase\n* using structured diffs. Instead of just suggesting edits, the model emits\n* patch operations that your application applies and then reports back on.\n*\n* **When to use**:\n* - **Multi-file refactors** – Rename symbols, extract helpers, or reorganize modules\n* - **Bug fixes** – Have the model both diagnose issues and emit precise patches\n* - **Tests & docs generation** – Create new test files, fixtures, and documentation\n* - **Migrations & mechanical edits** – Apply repetitive, structured updates\n*\n* **How it works**:\n* The tool operates in a continuous loop:\n* 1. Model sends patch operations (`apply_patch_call` with operation type)\n* 2. Your code applies the patch to your working directory or repo\n* 3. You return success/failure status and optional output\n* 4. Repeat until the task is complete\n*\n* **Security Warning**: Applying patches can modify files in your codebase.\n* Always validate paths, implement backups, and consider sandboxing.\n*\n* @see {@link https://platform.openai.com/docs/guides/tools-apply-patch | OpenAI Apply Patch Documentation}\n*\n* @param options - Configuration options for the Apply Patch tool\n* @returns An Apply Patch tool that can be passed to `bindTools`\n*\n* @example\n* ```typescript\n* import { ChatOpenAI, tools } from \"@langchain/openai\";\n* import { applyDiff } from \"@openai/agents\";\n* import * as fs from \"fs/promises\";\n*\n* const model = new ChatOpenAI({ model: \"gpt-5.1\" });\n*\n* // With execute callback for automatic patch handling\n* const patchTool = tools.applyPatch({\n* execute: async (operation) => {\n* if (operation.type === \"create_file\") {\n* const content = applyDiff(\"\", operation.diff, \"create\");\n* await fs.writeFile(operation.path, content);\n* return `Created ${operation.path}`;\n* }\n* if (operation.type === \"update_file\") {\n* const current = await fs.readFile(operation.path, \"utf-8\");\n* const newContent = applyDiff(current, operation.diff);\n* await fs.writeFile(operation.path, newContent);\n* return `Updated ${operation.path}`;\n* }\n* if (operation.type === \"delete_file\") {\n* await fs.unlink(operation.path);\n* return `Deleted ${operation.path}`;\n* }\n* return \"Unknown operation type\";\n* },\n* });\n*\n* const llmWithPatch = model.bindTools([patchTool]);\n* const response = await llmWithPatch.invoke(\n* \"Rename the fib() function to fibonacci() in lib/fib.py\"\n* );\n* ```\n*\n* @remarks\n* - Only available through the Responses API (not Chat Completions)\n* - Designed for use with `gpt-5.1` model\n* - Operations include: `create_file`, `update_file`, `delete_file`\n* - Patches use V4A diff format for updates\n* - Always validate paths to prevent directory traversal attacks\n* - Consider backing up files before applying patches\n* - Implement \"all-or-nothing\" semantics if atomicity is required\n*/\nfunction applyPatch(options) {\n\tconst patchTool = tool(options.execute, {\n\t\tname: TOOL_NAME,\n\t\tdescription: \"Apply structured diffs to create, update, or delete files in the codebase.\",\n\t\tschema: ApplyPatchOperationSchema\n\t});\n\tpatchTool.extras = {\n\t\t...patchTool.extras ?? {},\n\t\tproviderToolDefinition: { type: \"apply_patch\" }\n\t};\n\treturn patchTool;\n}\n\n//#endregion\nexport { applyPatch };\n//# sourceMappingURL=applyPatch.js.map","/* eslint-disable no-console */\n/**\n * Summarization middleware with backend support for conversation history offloading.\n *\n * This module extends the base LangChain summarization middleware with additional\n * backend-based features for persisting conversation history before summarization.\n *\n * ## Usage\n *\n * ```typescript\n * import { createSummarizationMiddleware } from \"@anthropic/deepagents\";\n * import { FilesystemBackend } from \"@anthropic/deepagents\";\n *\n * const backend = new FilesystemBackend({ rootDir: \"/data\" });\n *\n * const middleware = createSummarizationMiddleware({\n * model: \"gpt-4o-mini\",\n * backend,\n * trigger: { type: \"fraction\", value: 0.85 },\n * keep: { type: \"fraction\", value: 0.10 },\n * });\n *\n * const agent = createDeepAgent({ middleware: [middleware] });\n * ```\n *\n * ## Storage\n *\n * Offloaded messages are stored as markdown at `/conversation_history/{thread_id}.md`.\n *\n * Each summarization event appends a new section to this file, creating a running log\n * of all evicted messages.\n *\n * ## Relationship to LangChain Summarization Middleware\n *\n * The base `summarizationMiddleware` from `langchain` provides core summarization\n * functionality. This middleware adds:\n * - Backend-based conversation history offloading\n * - Tool argument truncation for old messages\n *\n * For simple use cases without backend offloading, use `summarizationMiddleware`\n * from `langchain` directly.\n */\n\nimport { z } from \"zod\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport {\n createMiddleware,\n countTokensApproximately,\n HumanMessage,\n AIMessage,\n BaseMessage,\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { getBufferString } from \"@langchain/core/messages\";\nimport type { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport { ChatOpenAI } from \"@langchain/openai\";\n\nimport type { BackendProtocol, BackendFactory } from \"../backends/protocol.js\";\nimport type { StateBackend } from \"../backends/state.js\";\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\n// Re-export the base summarization middleware from langchain for users who don't need backend offloading\nexport { summarizationMiddleware } from \"langchain\";\n\n/**\n * Context size specification for summarization triggers and retention policies.\n */\nexport interface ContextSize {\n /** Type of context measurement */\n type: \"messages\" | \"tokens\" | \"fraction\";\n /** Threshold value */\n value: number;\n}\n\n/**\n * Settings for truncating large tool arguments in old messages.\n */\nexport interface TruncateArgsSettings {\n /**\n * Threshold to trigger argument truncation.\n * If not provided, truncation is disabled.\n */\n trigger?: ContextSize;\n\n /**\n * Context retention policy for message truncation.\n * Defaults to keeping last 20 messages.\n */\n keep?: ContextSize;\n\n /**\n * Maximum character length for tool arguments before truncation.\n * Defaults to 2000.\n */\n maxLength?: number;\n\n /**\n * Text to replace truncated arguments with.\n * Defaults to \"...(argument truncated)\".\n */\n truncationText?: string;\n}\n\n/**\n * Options for the summarization middleware.\n */\nexport interface SummarizationMiddlewareOptions {\n /**\n * The language model to use for generating summaries.\n * Can be a model string (e.g., \"gpt-4o-mini\") or a BaseChatModel instance.\n */\n model: string | BaseChatModel;\n\n /**\n * Backend instance or factory for persisting conversation history.\n */\n backend:\n | BackendProtocol\n | BackendFactory\n | ((config: { state: unknown; store?: BaseStore }) => StateBackend);\n\n /**\n * Threshold(s) that trigger summarization.\n * Can be a single ContextSize or an array for multiple triggers.\n */\n trigger?: ContextSize | ContextSize[];\n\n /**\n * Context retention policy after summarization.\n * Defaults to keeping last 20 messages.\n */\n keep?: ContextSize;\n\n /**\n * Prompt template for generating summaries.\n */\n summaryPrompt?: string;\n\n /**\n * Max tokens to include when generating summary.\n * Defaults to 4000.\n */\n trimTokensToSummarize?: number;\n\n /**\n * Path prefix for storing conversation history.\n * Defaults to \"/conversation_history\".\n */\n historyPathPrefix?: string;\n\n /**\n * Settings for truncating large tool arguments in old messages.\n * If not provided, argument truncation is disabled.\n */\n truncateArgsSettings?: TruncateArgsSettings;\n}\n\n// Default values\nconst DEFAULT_MESSAGES_TO_KEEP = 20;\nconst DEFAULT_TRIM_TOKEN_LIMIT = 4000;\nconst DEFAULT_SUMMARY_PROMPT = `You are a conversation summarizer. Your task is to create a concise summary of the conversation that captures:\n1. The main topics discussed\n2. Key decisions or conclusions reached\n3. Any important context that would be needed for continuing the conversation\n\nKeep the summary focused and informative. Do not include unnecessary details.\n\nConversation to summarize:\n{conversation}\n\nSummary:`;\n\n/**\n * State schema for summarization middleware.\n */\nconst SummarizationStateSchema = z.object({\n /** Session ID for history file naming */\n _summarizationSessionId: z.string().optional(),\n});\n\n/**\n * Check if a message is a previous summarization message.\n * Summary messages are HumanMessage objects with lc_source='summarization' in additional_kwargs.\n */\nfunction isSummaryMessage(msg: BaseMessage): boolean {\n if (!HumanMessage.isInstance(msg)) {\n return false;\n }\n return msg.additional_kwargs?.lc_source === \"summarization\";\n}\n\n/**\n * Create summarization middleware with backend support for conversation history offloading.\n *\n * This middleware:\n * 1. Monitors conversation length against configured thresholds\n * 2. When triggered, offloads old messages to backend storage\n * 3. Generates a summary of offloaded messages\n * 4. Replaces old messages with the summary, preserving recent context\n *\n * @param options - Configuration options\n * @returns AgentMiddleware for summarization and history offloading\n */\nexport function createSummarizationMiddleware(\n options: SummarizationMiddlewareOptions,\n) {\n const {\n model,\n backend,\n trigger,\n keep = { type: \"messages\", value: DEFAULT_MESSAGES_TO_KEEP },\n summaryPrompt = DEFAULT_SUMMARY_PROMPT,\n trimTokensToSummarize = DEFAULT_TRIM_TOKEN_LIMIT,\n historyPathPrefix = \"/conversation_history\",\n truncateArgsSettings,\n } = options;\n\n // Parse truncate settings\n const truncateTrigger = truncateArgsSettings?.trigger;\n const truncateKeep = truncateArgsSettings?.keep || {\n type: \"messages\" as const,\n value: 20,\n };\n const maxArgLength = truncateArgsSettings?.maxLength || 2000;\n const truncationText =\n truncateArgsSettings?.truncationText || \"...(argument truncated)\";\n\n // Session ID for this middleware instance (fallback if no thread_id)\n let sessionId: string | null = null;\n\n /**\n * Resolve backend from instance or factory.\n */\n function getBackend(state: unknown): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend({ state }) as BackendProtocol;\n }\n return backend;\n }\n\n /**\n * Get or create session ID for history file naming.\n */\n function getSessionId(state: Record<string, unknown>): string {\n if (state._summarizationSessionId) {\n return state._summarizationSessionId as string;\n }\n if (!sessionId) {\n sessionId = `session_${uuidv4().substring(0, 8)}`;\n }\n return sessionId;\n }\n\n /**\n * Get the history file path.\n */\n function getHistoryPath(state: Record<string, unknown>): string {\n const id = getSessionId(state);\n return `${historyPathPrefix}/${id}.md`;\n }\n\n /**\n * Resolve the chat model.\n */\n function getChatModel(): BaseChatModel {\n if (typeof model === \"string\") {\n return new ChatOpenAI({ modelName: model });\n }\n return model;\n }\n\n /**\n * Check if summarization should be triggered.\n */\n function shouldSummarize(\n messages: BaseMessage[],\n totalTokens: number,\n maxInputTokens?: number,\n ): boolean {\n if (!trigger) {\n return false;\n }\n\n const triggers = Array.isArray(trigger) ? trigger : [trigger];\n\n for (const t of triggers) {\n if (t.type === \"messages\" && messages.length >= t.value) {\n return true;\n }\n if (t.type === \"tokens\" && totalTokens >= t.value) {\n return true;\n }\n if (t.type === \"fraction\" && maxInputTokens) {\n const threshold = Math.floor(maxInputTokens * t.value);\n if (totalTokens >= threshold) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n /**\n * Determine cutoff index for messages to summarize.\n * Messages at index < cutoff will be summarized.\n * Messages at index >= cutoff will be preserved.\n */\n function determineCutoffIndex(\n messages: BaseMessage[],\n maxInputTokens?: number,\n ): number {\n if (keep.type === \"messages\") {\n if (messages.length <= keep.value) {\n return 0;\n }\n return messages.length - keep.value;\n }\n\n if (keep.type === \"tokens\" || keep.type === \"fraction\") {\n const targetTokenCount =\n keep.type === \"fraction\" && maxInputTokens\n ? Math.floor(maxInputTokens * keep.value)\n : keep.value;\n\n let tokensKept = 0;\n for (let i = messages.length - 1; i >= 0; i--) {\n const msgTokens = countTokensApproximately([messages[i]]);\n if (tokensKept + msgTokens > targetTokenCount) {\n return i + 1;\n }\n tokensKept += msgTokens;\n }\n return 0;\n }\n\n return 0;\n }\n\n /**\n * Check if argument truncation should be triggered.\n */\n function shouldTruncateArgs(\n messages: BaseMessage[],\n totalTokens: number,\n maxInputTokens?: number,\n ): boolean {\n if (!truncateTrigger) {\n return false;\n }\n\n if (truncateTrigger.type === \"messages\") {\n return messages.length >= truncateTrigger.value;\n }\n if (truncateTrigger.type === \"tokens\") {\n return totalTokens >= truncateTrigger.value;\n }\n if (truncateTrigger.type === \"fraction\" && maxInputTokens) {\n const threshold = Math.floor(maxInputTokens * truncateTrigger.value);\n return totalTokens >= threshold;\n }\n\n return false;\n }\n\n /**\n * Determine cutoff index for argument truncation.\n */\n function determineTruncateCutoffIndex(\n messages: BaseMessage[],\n maxInputTokens?: number,\n ): number {\n if (truncateKeep.type === \"messages\") {\n if (messages.length <= truncateKeep.value) {\n return messages.length;\n }\n return messages.length - truncateKeep.value;\n }\n\n if (truncateKeep.type === \"tokens\" || truncateKeep.type === \"fraction\") {\n const targetTokenCount =\n truncateKeep.type === \"fraction\" && maxInputTokens\n ? Math.floor(maxInputTokens * truncateKeep.value)\n : truncateKeep.value;\n\n let tokensKept = 0;\n for (let i = messages.length - 1; i >= 0; i--) {\n const msgTokens = countTokensApproximately([messages[i]]);\n if (tokensKept + msgTokens > targetTokenCount) {\n return i + 1;\n }\n tokensKept += msgTokens;\n }\n return 0;\n }\n\n return messages.length;\n }\n\n /**\n * Truncate large tool arguments in old messages.\n */\n function truncateArgs(\n messages: BaseMessage[],\n maxInputTokens?: number,\n ): { messages: BaseMessage[]; modified: boolean } {\n const totalTokens = countTokensApproximately(messages);\n if (!shouldTruncateArgs(messages, totalTokens, maxInputTokens)) {\n return { messages, modified: false };\n }\n\n const cutoffIndex = determineTruncateCutoffIndex(messages, maxInputTokens);\n if (cutoffIndex >= messages.length) {\n return { messages, modified: false };\n }\n\n const truncatedMessages: BaseMessage[] = [];\n let modified = false;\n\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n\n if (i < cutoffIndex && AIMessage.isInstance(msg) && msg.tool_calls) {\n const truncatedToolCalls = msg.tool_calls.map((toolCall) => {\n const args = toolCall.args || {};\n const truncatedArgs: Record<string, unknown> = {};\n let toolModified = false;\n\n for (const [key, value] of Object.entries(args)) {\n if (\n typeof value === \"string\" &&\n value.length > maxArgLength &&\n (toolCall.name === \"write_file\" || toolCall.name === \"edit_file\")\n ) {\n truncatedArgs[key] = value.substring(0, 20) + truncationText;\n toolModified = true;\n } else {\n truncatedArgs[key] = value;\n }\n }\n\n if (toolModified) {\n modified = true;\n return { ...toolCall, args: truncatedArgs };\n }\n return toolCall;\n });\n\n if (modified) {\n const truncatedMsg = new AIMessage({\n content: msg.content,\n tool_calls: truncatedToolCalls,\n additional_kwargs: msg.additional_kwargs,\n });\n truncatedMessages.push(truncatedMsg);\n } else {\n truncatedMessages.push(msg);\n }\n } else {\n truncatedMessages.push(msg);\n }\n }\n\n return { messages: truncatedMessages, modified };\n }\n\n /**\n * Filter out previous summary messages.\n */\n function filterSummaryMessages(messages: BaseMessage[]): BaseMessage[] {\n return messages.filter((msg) => !isSummaryMessage(msg));\n }\n\n /**\n * Offload messages to backend.\n */\n async function offloadToBackend(\n resolvedBackend: BackendProtocol,\n messages: BaseMessage[],\n state: Record<string, unknown>,\n ): Promise<string | null> {\n const path = getHistoryPath(state);\n const filteredMessages = filterSummaryMessages(messages);\n\n const timestamp = new Date().toISOString();\n const newSection = `## Summarized at ${timestamp}\\n\\n${getBufferString(filteredMessages)}\\n\\n`;\n\n // Read existing content\n let existingContent = \"\";\n try {\n if (resolvedBackend.downloadFiles) {\n const responses = await resolvedBackend.downloadFiles([path]);\n if (\n responses.length > 0 &&\n responses[0].content &&\n !responses[0].error\n ) {\n existingContent = new TextDecoder().decode(responses[0].content);\n }\n }\n } catch {\n // File doesn't exist yet, that's fine\n }\n\n const combinedContent = existingContent + newSection;\n\n try {\n let result;\n if (existingContent) {\n result = await resolvedBackend.edit(\n path,\n existingContent,\n combinedContent,\n );\n } else {\n result = await resolvedBackend.write(path, combinedContent);\n }\n\n if (result.error) {\n console.warn(\n `Failed to offload conversation history to ${path}: ${result.error}`,\n );\n return null;\n }\n\n return path;\n } catch (e) {\n console.warn(`Exception offloading conversation history to ${path}:`, e);\n return null;\n }\n }\n\n /**\n * Create summary of messages.\n */\n async function createSummary(messages: BaseMessage[]): Promise<string> {\n const chatModel = getChatModel();\n\n // Trim messages if too long\n let messagesToSummarize = messages;\n const tokens = countTokensApproximately(messages);\n if (tokens > trimTokensToSummarize) {\n // Keep only recent messages that fit\n let kept = 0;\n const trimmedMessages: BaseMessage[] = [];\n for (let i = messages.length - 1; i >= 0; i--) {\n const msgTokens = countTokensApproximately([messages[i]]);\n if (kept + msgTokens > trimTokensToSummarize) {\n break;\n }\n trimmedMessages.unshift(messages[i]);\n kept += msgTokens;\n }\n messagesToSummarize = trimmedMessages;\n }\n\n const conversation = getBufferString(messagesToSummarize);\n const prompt = summaryPrompt.replace(\"{conversation}\", conversation);\n\n const response = await chatModel.invoke([\n new HumanMessage({ content: prompt }),\n ]);\n\n return typeof response.content === \"string\"\n ? response.content\n : JSON.stringify(response.content);\n }\n\n /**\n * Build the summary message with file path reference.\n */\n function buildSummaryMessage(\n summary: string,\n filePath: string | null,\n ): HumanMessage {\n let content: string;\n if (filePath) {\n content = `You are in the middle of a conversation that has been summarized.\n\nThe full conversation history has been saved to ${filePath} should you need to refer back to it for details.\n\nA condensed summary follows:\n\n<summary>\n${summary}\n</summary>`;\n } else {\n content = `Here is a summary of the conversation to date:\\n\\n${summary}`;\n }\n\n return new HumanMessage({\n content,\n additional_kwargs: { lc_source: \"summarization\" },\n });\n }\n\n return createMiddleware({\n name: \"SummarizationMiddleware\",\n stateSchema: SummarizationStateSchema,\n\n async beforeModel(state) {\n const messages = state.messages ?? [];\n\n if (messages.length === 0) {\n return undefined;\n }\n\n // Step 1: Truncate args if configured\n const { messages: truncatedMessages, modified: argsWereTruncated } =\n truncateArgs(messages);\n\n // Step 2: Check if summarization should happen\n const totalTokens = countTokensApproximately(truncatedMessages);\n const shouldDoSummarization = shouldSummarize(\n truncatedMessages,\n totalTokens,\n );\n\n // If only truncation happened (no summarization)\n if (argsWereTruncated && !shouldDoSummarization) {\n return { messages: truncatedMessages };\n }\n\n // If no truncation and no summarization\n if (!shouldDoSummarization) {\n return undefined;\n }\n\n // Step 3: Perform summarization\n const cutoffIndex = determineCutoffIndex(truncatedMessages);\n if (cutoffIndex <= 0) {\n if (argsWereTruncated) {\n return { messages: truncatedMessages };\n }\n return undefined;\n }\n\n const messagesToSummarize = truncatedMessages.slice(0, cutoffIndex);\n const preservedMessages = truncatedMessages.slice(cutoffIndex);\n\n // Offload to backend first\n const resolvedBackend = getBackend(state);\n const filePath = await offloadToBackend(\n resolvedBackend,\n messagesToSummarize,\n state,\n );\n\n if (filePath === null) {\n // Offloading failed - don't proceed with summarization\n return undefined;\n }\n\n // Generate summary\n const summary = await createSummary(messagesToSummarize);\n\n // Build summary message\n const summaryMessage = buildSummaryMessage(summary, filePath);\n\n return {\n messages: [summaryMessage, ...preservedMessages],\n _summarizationSessionId: getSessionId(state),\n };\n },\n });\n}\n","/**\n * StoreBackend: Adapter for LangGraph's BaseStore (persistent, cross-thread).\n */\n\nimport type { Item } from \"@langchain/langgraph\";\nimport type {\n BackendProtocol,\n EditResult,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n StateAndStore,\n WriteResult,\n} from \"./protocol.js\";\nimport {\n createFileData,\n fileDataToString,\n formatReadResponse,\n globSearchFiles,\n grepMatchesFromFiles,\n performStringReplacement,\n updateFileData,\n} from \"./utils.js\";\n\n/**\n * Backend that stores files in LangGraph's BaseStore (persistent).\n *\n * Uses LangGraph's Store for persistent, cross-conversation storage.\n * Files are organized via namespaces and persist across all threads.\n *\n * The namespace can include an optional assistant_id for multi-agent isolation.\n */\nexport class StoreBackend implements BackendProtocol {\n private stateAndStore: StateAndStore;\n\n constructor(stateAndStore: StateAndStore) {\n this.stateAndStore = stateAndStore;\n }\n\n /**\n * Get the store instance.\n *\n * @returns BaseStore instance\n * @throws Error if no store is available\n */\n private getStore() {\n const store = this.stateAndStore.store;\n if (!store) {\n throw new Error(\"Store is required but not available in StateAndStore\");\n }\n return store;\n }\n\n /**\n * Get the namespace for store operations.\n *\n * If an assistant_id is available in stateAndStore, return\n * [assistant_id, \"filesystem\"] to provide per-assistant isolation.\n * Otherwise return [\"filesystem\"].\n */\n protected getNamespace(): string[] {\n const namespace = \"filesystem\";\n const assistantId = this.stateAndStore.assistantId;\n\n if (assistantId) {\n return [assistantId, namespace];\n }\n\n return [namespace];\n }\n\n /**\n * Convert a store Item to FileData format.\n *\n * @param storeItem - The store Item containing file data\n * @returns FileData object\n * @throws Error if required fields are missing or have incorrect types\n */\n private convertStoreItemToFileData(storeItem: Item): FileData {\n const value = storeItem.value as any;\n\n if (\n !value.content ||\n !Array.isArray(value.content) ||\n typeof value.created_at !== \"string\" ||\n typeof value.modified_at !== \"string\"\n ) {\n throw new Error(\n `Store item does not contain valid FileData fields. Got keys: ${Object.keys(value).join(\", \")}`,\n );\n }\n\n return {\n content: value.content,\n created_at: value.created_at,\n modified_at: value.modified_at,\n };\n }\n\n /**\n * Convert FileData to a value suitable for store.put().\n *\n * @param fileData - The FileData to convert\n * @returns Object with content, created_at, and modified_at fields\n */\n private convertFileDataToStoreValue(fileData: FileData): Record<string, any> {\n return {\n content: fileData.content,\n created_at: fileData.created_at,\n modified_at: fileData.modified_at,\n };\n }\n\n /**\n * Search store with automatic pagination to retrieve all results.\n *\n * @param store - The store to search\n * @param namespace - Hierarchical path prefix to search within\n * @param options - Optional query, filter, and page_size\n * @returns List of all items matching the search criteria\n */\n private async searchStorePaginated(\n store: any,\n namespace: string[],\n options: {\n query?: string;\n filter?: Record<string, any>;\n pageSize?: number;\n } = {},\n ): Promise<Item[]> {\n const { query, filter, pageSize = 100 } = options;\n const allItems: Item[] = [];\n let offset = 0;\n\n while (true) {\n const pageItems = await store.search(namespace, {\n query,\n filter,\n limit: pageSize,\n offset,\n });\n\n if (!pageItems || pageItems.length === 0) {\n break;\n }\n\n allItems.push(...pageItems);\n\n if (pageItems.length < pageSize) {\n break;\n }\n\n offset += pageSize;\n }\n\n return allItems;\n }\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects for files and directories directly in the directory.\n * Directories have a trailing / in their path and is_dir=true.\n */\n async lsInfo(path: string): Promise<FileInfo[]> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n\n // Retrieve all items and filter by path prefix locally to avoid\n // coupling to store-specific filter semantics\n const items = await this.searchStorePaginated(store, namespace);\n const infos: FileInfo[] = [];\n const subdirs = new Set<string>();\n\n // Normalize path to have trailing slash for proper prefix matching\n const normalizedPath = path.endsWith(\"/\") ? path : path + \"/\";\n\n for (const item of items) {\n const itemKey = String(item.key);\n\n // Check if file is in the specified directory or a subdirectory\n if (!itemKey.startsWith(normalizedPath)) {\n continue;\n }\n\n // Get the relative path after the directory\n const relative = itemKey.substring(normalizedPath.length);\n\n // If relative path contains '/', it's in a subdirectory\n if (relative.includes(\"/\")) {\n // Extract the immediate subdirectory name\n const subdirName = relative.split(\"/\")[0];\n subdirs.add(normalizedPath + subdirName + \"/\");\n continue;\n }\n\n // This is a file directly in the current directory\n try {\n const fd = this.convertStoreItemToFileData(item);\n const size = fd.content.join(\"\\n\").length;\n infos.push({\n path: itemKey,\n is_dir: false,\n size: size,\n modified_at: fd.modified_at,\n });\n } catch {\n // Skip invalid items\n continue;\n }\n }\n\n // Add directories to the results\n for (const subdir of Array.from(subdirs).sort()) {\n infos.push({\n path: subdir,\n is_dir: true,\n size: 0,\n modified_at: \"\",\n });\n }\n\n infos.sort((a, b) => a.path.localeCompare(b.path));\n return infos;\n }\n\n /**\n * Read file content with line numbers.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n try {\n const fileData = await this.readRaw(filePath);\n return formatReadResponse(fileData, offset, limit);\n } catch (e: any) {\n return `Error: ${e.message}`;\n }\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n async readRaw(filePath: string): Promise<FileData> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const item = await store.get(namespace, filePath);\n\n if (!item) throw new Error(`File '${filePath}' not found`);\n return this.convertStoreItemToFileData(item);\n }\n\n /**\n * Create a new file with content.\n * Returns WriteResult. External storage sets filesUpdate=null.\n */\n async write(filePath: string, content: string): Promise<WriteResult> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n\n // Check if file exists\n const existing = await store.get(namespace, filePath);\n if (existing) {\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n }\n\n // Create new file\n const fileData = createFileData(content);\n const storeValue = this.convertFileDataToStoreValue(fileData);\n await store.put(namespace, filePath, storeValue);\n return { path: filePath, filesUpdate: null };\n }\n\n /**\n * Edit a file by replacing string occurrences.\n * Returns EditResult. External storage sets filesUpdate=null.\n */\n async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n\n // Get existing file\n const item = await store.get(namespace, filePath);\n if (!item) {\n return { error: `Error: File '${filePath}' not found` };\n }\n\n try {\n const fileData = this.convertStoreItemToFileData(item);\n const content = fileDataToString(fileData);\n const result = performStringReplacement(\n content,\n oldString,\n newString,\n replaceAll,\n );\n\n if (typeof result === \"string\") {\n return { error: result };\n }\n\n const [newContent, occurrences] = result;\n const newFileData = updateFileData(fileData, newContent);\n\n // Update file in store\n const storeValue = this.convertFileDataToStoreValue(newFileData);\n await store.put(namespace, filePath, storeValue);\n return { path: filePath, filesUpdate: null, occurrences: occurrences };\n } catch (e: any) {\n return { error: `Error: ${e.message}` };\n }\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n async grepRaw(\n pattern: string,\n path: string = \"/\",\n glob: string | null = null,\n ): Promise<GrepMatch[] | string> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const items = await this.searchStorePaginated(store, namespace);\n\n const files: Record<string, FileData> = {};\n for (const item of items) {\n try {\n files[item.key] = this.convertStoreItemToFileData(item);\n } catch {\n // Skip invalid items\n continue;\n }\n }\n\n return grepMatchesFromFiles(files, pattern, path, glob);\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n async globInfo(pattern: string, path: string = \"/\"): Promise<FileInfo[]> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const items = await this.searchStorePaginated(store, namespace);\n\n const files: Record<string, FileData> = {};\n for (const item of items) {\n try {\n files[item.key] = this.convertStoreItemToFileData(item);\n } catch {\n // Skip invalid items\n continue;\n }\n }\n\n const result = globSearchFiles(files, pattern, path);\n if (result === \"No files found\") {\n return [];\n }\n\n const paths = result.split(\"\\n\");\n const infos: FileInfo[] = [];\n for (const p of paths) {\n const fd = files[p];\n const size = fd ? fd.content.join(\"\\n\").length : 0;\n infos.push({\n path: p,\n is_dir: false,\n size: size,\n modified_at: fd?.modified_at || \"\",\n });\n }\n return infos;\n }\n\n /**\n * Upload multiple files.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const responses: FileUploadResponse[] = [];\n\n for (const [path, content] of files) {\n try {\n const contentStr = new TextDecoder().decode(content);\n const fileData = createFileData(contentStr);\n const storeValue = this.convertFileDataToStoreValue(fileData);\n await store.put(namespace, path, storeValue);\n responses.push({ path, error: null });\n } catch {\n responses.push({ path, error: \"invalid_path\" });\n }\n }\n\n return responses;\n }\n\n /**\n * Download multiple files.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const responses: FileDownloadResponse[] = [];\n\n for (const path of paths) {\n try {\n const item = await store.get(namespace, path);\n if (!item) {\n responses.push({ path, content: null, error: \"file_not_found\" });\n continue;\n }\n\n const fileData = this.convertStoreItemToFileData(item);\n const contentStr = fileDataToString(fileData);\n const content = new TextEncoder().encode(contentStr);\n responses.push({ path, content, error: null });\n } catch {\n responses.push({ path, content: null, error: \"file_not_found\" });\n }\n }\n\n return responses;\n }\n}\n","/**\n * FilesystemBackend: Read and write files directly from the filesystem.\n *\n * Security and search upgrades:\n * - Secure path resolution with root containment when in virtual_mode (sandboxed to cwd)\n * - Prevent symlink-following on file I/O using O_NOFOLLOW when available\n * - Ripgrep-powered grep with JSON parsing, plus regex fallback\n * and optional glob include filtering, while preserving virtual path behavior\n */\n\nimport fs from \"node:fs/promises\";\nimport fsSync from \"node:fs\";\nimport path from \"node:path\";\nimport { spawn } from \"node:child_process\";\n\nimport fg from \"fast-glob\";\nimport micromatch from \"micromatch\";\nimport type {\n BackendProtocol,\n EditResult,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n WriteResult,\n} from \"./protocol.js\";\nimport {\n checkEmptyContent,\n formatContentWithLineNumbers,\n performStringReplacement,\n} from \"./utils.js\";\n\nconst SUPPORTS_NOFOLLOW = fsSync.constants.O_NOFOLLOW !== undefined;\n\n/**\n * Backend that reads and writes files directly from the filesystem.\n *\n * Files are accessed using their actual filesystem paths. Relative paths are\n * resolved relative to the current working directory. Content is read/written\n * as plain text, and metadata (timestamps) are derived from filesystem stats.\n */\nexport class FilesystemBackend implements BackendProtocol {\n private cwd: string;\n private virtualMode: boolean;\n private maxFileSizeBytes: number;\n\n constructor(\n options: {\n rootDir?: string;\n virtualMode?: boolean;\n maxFileSizeMb?: number;\n } = {},\n ) {\n const { rootDir, virtualMode = false, maxFileSizeMb = 10 } = options;\n this.cwd = rootDir ? path.resolve(rootDir) : process.cwd();\n this.virtualMode = virtualMode;\n this.maxFileSizeBytes = maxFileSizeMb * 1024 * 1024;\n }\n\n /**\n * Resolve a file path with security checks.\n *\n * When virtualMode=true, treat incoming paths as virtual absolute paths under\n * this.cwd, disallow traversal (.., ~) and ensure resolved path stays within root.\n * When virtualMode=false, preserve legacy behavior: absolute paths are allowed\n * as-is; relative paths resolve under cwd.\n *\n * @param key - File path (absolute, relative, or virtual when virtualMode=true)\n * @returns Resolved absolute path string\n * @throws Error if path traversal detected or path outside root\n */\n private resolvePath(key: string): string {\n if (this.virtualMode) {\n const vpath = key.startsWith(\"/\") ? key : \"/\" + key;\n if (vpath.includes(\"..\") || vpath.startsWith(\"~\")) {\n throw new Error(\"Path traversal not allowed\");\n }\n const full = path.resolve(this.cwd, vpath.substring(1));\n const relative = path.relative(this.cwd, full);\n if (relative.startsWith(\"..\") || path.isAbsolute(relative)) {\n throw new Error(`Path: ${full} outside root directory: ${this.cwd}`);\n }\n return full;\n }\n\n if (path.isAbsolute(key)) {\n return key;\n }\n return path.resolve(this.cwd, key);\n }\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param dirPath - Absolute directory path to list files from\n * @returns List of FileInfo objects for files and directories directly in the directory.\n * Directories have a trailing / in their path and is_dir=true.\n */\n async lsInfo(dirPath: string): Promise<FileInfo[]> {\n try {\n const resolvedPath = this.resolvePath(dirPath);\n const stat = await fs.stat(resolvedPath);\n\n if (!stat.isDirectory()) {\n return [];\n }\n\n const entries = await fs.readdir(resolvedPath, { withFileTypes: true });\n const results: FileInfo[] = [];\n\n const cwdStr = this.cwd.endsWith(path.sep)\n ? this.cwd\n : this.cwd + path.sep;\n\n for (const entry of entries) {\n const fullPath = path.join(resolvedPath, entry.name);\n\n try {\n const entryStat = await fs.stat(fullPath);\n const isFile = entryStat.isFile();\n const isDir = entryStat.isDirectory();\n\n if (!this.virtualMode) {\n // Non-virtual mode: use absolute paths\n if (isFile) {\n results.push({\n path: fullPath,\n is_dir: false,\n size: entryStat.size,\n modified_at: entryStat.mtime.toISOString(),\n });\n } else if (isDir) {\n results.push({\n path: fullPath + path.sep,\n is_dir: true,\n size: 0,\n modified_at: entryStat.mtime.toISOString(),\n });\n }\n } else {\n let relativePath: string;\n if (fullPath.startsWith(cwdStr)) {\n relativePath = fullPath.substring(cwdStr.length);\n } else if (fullPath.startsWith(this.cwd)) {\n relativePath = fullPath\n .substring(this.cwd.length)\n .replace(/^[/\\\\]/, \"\");\n } else {\n relativePath = fullPath;\n }\n\n relativePath = relativePath.split(path.sep).join(\"/\");\n const virtPath = \"/\" + relativePath;\n\n if (isFile) {\n results.push({\n path: virtPath,\n is_dir: false,\n size: entryStat.size,\n modified_at: entryStat.mtime.toISOString(),\n });\n } else if (isDir) {\n results.push({\n path: virtPath + \"/\",\n is_dir: true,\n size: 0,\n modified_at: entryStat.mtime.toISOString(),\n });\n }\n }\n } catch {\n // Skip entries we can't stat\n continue;\n }\n }\n\n results.sort((a, b) => a.path.localeCompare(b.path));\n return results;\n } catch {\n return [];\n }\n }\n\n /**\n * Read file content with line numbers.\n *\n * @param filePath - Absolute or relative file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n try {\n const resolvedPath = this.resolvePath(filePath);\n\n let content: string;\n\n if (SUPPORTS_NOFOLLOW) {\n const stat = await fs.stat(resolvedPath);\n if (!stat.isFile()) {\n return `Error: File '${filePath}' not found`;\n }\n const fd = await fs.open(\n resolvedPath,\n fsSync.constants.O_RDONLY | fsSync.constants.O_NOFOLLOW,\n );\n try {\n content = await fd.readFile({ encoding: \"utf-8\" });\n } finally {\n await fd.close();\n }\n } else {\n const stat = await fs.lstat(resolvedPath);\n if (stat.isSymbolicLink()) {\n return `Error: Symlinks are not allowed: ${filePath}`;\n }\n if (!stat.isFile()) {\n return `Error: File '${filePath}' not found`;\n }\n content = await fs.readFile(resolvedPath, \"utf-8\");\n }\n\n const emptyMsg = checkEmptyContent(content);\n if (emptyMsg) {\n return emptyMsg;\n }\n\n const lines = content.split(\"\\n\");\n const startIdx = offset;\n const endIdx = Math.min(startIdx + limit, lines.length);\n\n if (startIdx >= lines.length) {\n return `Error: Line offset ${offset} exceeds file length (${lines.length} lines)`;\n }\n\n const selectedLines = lines.slice(startIdx, endIdx);\n return formatContentWithLineNumbers(selectedLines, startIdx + 1);\n } catch (e: any) {\n return `Error reading file '${filePath}': ${e.message}`;\n }\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n async readRaw(filePath: string): Promise<FileData> {\n const resolvedPath = this.resolvePath(filePath);\n\n let content: string;\n let stat: fsSync.Stats;\n\n if (SUPPORTS_NOFOLLOW) {\n stat = await fs.stat(resolvedPath);\n if (!stat.isFile()) throw new Error(`File '${filePath}' not found`);\n const fd = await fs.open(\n resolvedPath,\n fsSync.constants.O_RDONLY | fsSync.constants.O_NOFOLLOW,\n );\n try {\n content = await fd.readFile({ encoding: \"utf-8\" });\n } finally {\n await fd.close();\n }\n } else {\n stat = await fs.lstat(resolvedPath);\n if (stat.isSymbolicLink()) {\n throw new Error(`Symlinks are not allowed: ${filePath}`);\n }\n if (!stat.isFile()) throw new Error(`File '${filePath}' not found`);\n content = await fs.readFile(resolvedPath, \"utf-8\");\n }\n\n return {\n content: content.split(\"\\n\"),\n created_at: stat.ctime.toISOString(),\n modified_at: stat.mtime.toISOString(),\n };\n }\n\n /**\n * Create a new file with content.\n * Returns WriteResult. External storage sets filesUpdate=null.\n */\n async write(filePath: string, content: string): Promise<WriteResult> {\n try {\n const resolvedPath = this.resolvePath(filePath);\n\n try {\n const stat = await fs.lstat(resolvedPath);\n if (stat.isSymbolicLink()) {\n return {\n error: `Cannot write to ${filePath} because it is a symlink. Symlinks are not allowed.`,\n };\n }\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n } catch {\n // File doesn't exist, good to proceed\n }\n\n await fs.mkdir(path.dirname(resolvedPath), { recursive: true });\n\n if (SUPPORTS_NOFOLLOW) {\n const flags =\n fsSync.constants.O_WRONLY |\n fsSync.constants.O_CREAT |\n fsSync.constants.O_TRUNC |\n fsSync.constants.O_NOFOLLOW;\n\n const fd = await fs.open(resolvedPath, flags, 0o644);\n try {\n await fd.writeFile(content, \"utf-8\");\n } finally {\n await fd.close();\n }\n } else {\n await fs.writeFile(resolvedPath, content, \"utf-8\");\n }\n\n return { path: filePath, filesUpdate: null };\n } catch (e: any) {\n return { error: `Error writing file '${filePath}': ${e.message}` };\n }\n }\n\n /**\n * Edit a file by replacing string occurrences.\n * Returns EditResult. External storage sets filesUpdate=null.\n */\n async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n try {\n const resolvedPath = this.resolvePath(filePath);\n\n let content: string;\n\n if (SUPPORTS_NOFOLLOW) {\n const stat = await fs.stat(resolvedPath);\n if (!stat.isFile()) {\n return { error: `Error: File '${filePath}' not found` };\n }\n\n const fd = await fs.open(\n resolvedPath,\n fsSync.constants.O_RDONLY | fsSync.constants.O_NOFOLLOW,\n );\n try {\n content = await fd.readFile({ encoding: \"utf-8\" });\n } finally {\n await fd.close();\n }\n } else {\n const stat = await fs.lstat(resolvedPath);\n if (stat.isSymbolicLink()) {\n return { error: `Error: Symlinks are not allowed: ${filePath}` };\n }\n if (!stat.isFile()) {\n return { error: `Error: File '${filePath}' not found` };\n }\n content = await fs.readFile(resolvedPath, \"utf-8\");\n }\n\n const result = performStringReplacement(\n content,\n oldString,\n newString,\n replaceAll,\n );\n\n if (typeof result === \"string\") {\n return { error: result };\n }\n\n const [newContent, occurrences] = result;\n\n // Write securely\n if (SUPPORTS_NOFOLLOW) {\n const flags =\n fsSync.constants.O_WRONLY |\n fsSync.constants.O_TRUNC |\n fsSync.constants.O_NOFOLLOW;\n\n const fd = await fs.open(resolvedPath, flags);\n try {\n await fd.writeFile(newContent, \"utf-8\");\n } finally {\n await fd.close();\n }\n } else {\n await fs.writeFile(resolvedPath, newContent, \"utf-8\");\n }\n\n return { path: filePath, filesUpdate: null, occurrences: occurrences };\n } catch (e: any) {\n return { error: `Error editing file '${filePath}': ${e.message}` };\n }\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n async grepRaw(\n pattern: string,\n dirPath: string = \"/\",\n glob: string | null = null,\n ): Promise<GrepMatch[] | string> {\n // Validate regex\n try {\n new RegExp(pattern);\n } catch (e: any) {\n return `Invalid regex pattern: ${e.message}`;\n }\n\n // Resolve base path\n let baseFull: string;\n try {\n baseFull = this.resolvePath(dirPath || \".\");\n } catch {\n return [];\n }\n\n try {\n await fs.stat(baseFull);\n } catch {\n return [];\n }\n\n // Try ripgrep first, fallback to regex search\n let results = await this.ripgrepSearch(pattern, baseFull, glob);\n if (results === null) {\n results = await this.pythonSearch(pattern, baseFull, glob);\n }\n\n const matches: GrepMatch[] = [];\n for (const [fpath, items] of Object.entries(results)) {\n for (const [lineNum, lineText] of items) {\n matches.push({ path: fpath, line: lineNum, text: lineText });\n }\n }\n return matches;\n }\n\n /**\n * Try to use ripgrep for fast searching.\n * Returns null if ripgrep is not available or fails.\n */\n private async ripgrepSearch(\n pattern: string,\n baseFull: string,\n includeGlob: string | null,\n ): Promise<Record<string, Array<[number, string]>> | null> {\n return new Promise((resolve) => {\n const args = [\"--json\"];\n if (includeGlob) {\n args.push(\"--glob\", includeGlob);\n }\n args.push(\"--\", pattern, baseFull);\n\n const proc = spawn(\"rg\", args, { timeout: 30000 });\n const results: Record<string, Array<[number, string]>> = {};\n let output = \"\";\n\n proc.stdout.on(\"data\", (data) => {\n output += data.toString();\n });\n\n proc.on(\"close\", (code) => {\n if (code !== 0 && code !== 1) {\n // Error (code 1 means no matches, which is ok)\n resolve(null);\n return;\n }\n\n for (const line of output.split(\"\\n\")) {\n if (!line.trim()) continue;\n try {\n const data = JSON.parse(line);\n if (data.type !== \"match\") continue;\n\n const pdata = data.data || {};\n const ftext = pdata.path?.text;\n if (!ftext) continue;\n\n let virtPath: string;\n if (this.virtualMode) {\n try {\n const resolved = path.resolve(ftext);\n const relative = path.relative(this.cwd, resolved);\n if (relative.startsWith(\"..\")) continue;\n const normalizedRelative = relative.split(path.sep).join(\"/\");\n virtPath = \"/\" + normalizedRelative;\n } catch {\n continue;\n }\n } else {\n virtPath = ftext;\n }\n\n const ln = pdata.line_number;\n const lt = pdata.lines?.text?.replace(/\\n$/, \"\") || \"\";\n if (ln === undefined) continue;\n\n if (!results[virtPath]) {\n results[virtPath] = [];\n }\n results[virtPath].push([ln, lt]);\n } catch {\n // Skip invalid JSON\n continue;\n }\n }\n\n resolve(results);\n });\n\n proc.on(\"error\", () => {\n resolve(null);\n });\n });\n }\n\n /**\n * Fallback regex search implementation.\n */\n private async pythonSearch(\n pattern: string,\n baseFull: string,\n includeGlob: string | null,\n ): Promise<Record<string, Array<[number, string]>>> {\n let regex: RegExp;\n try {\n regex = new RegExp(pattern);\n } catch {\n return {};\n }\n\n const results: Record<string, Array<[number, string]>> = {};\n const stat = await fs.stat(baseFull);\n const root = stat.isDirectory() ? baseFull : path.dirname(baseFull);\n\n // Use fast-glob to recursively find all files\n const files = await fg(\"**/*\", {\n cwd: root,\n absolute: true,\n onlyFiles: true,\n dot: true,\n });\n\n for (const fp of files) {\n try {\n // Filter by glob if provided\n if (\n includeGlob &&\n !micromatch.isMatch(path.basename(fp), includeGlob)\n ) {\n continue;\n }\n\n // Check file size\n const stat = await fs.stat(fp);\n if (stat.size > this.maxFileSizeBytes) {\n continue;\n }\n\n // Read and search\n const content = await fs.readFile(fp, \"utf-8\");\n const lines = content.split(\"\\n\");\n\n for (let i = 0; i < lines.length; i++) {\n const line = lines[i];\n if (regex.test(line)) {\n let virtPath: string;\n if (this.virtualMode) {\n try {\n const relative = path.relative(this.cwd, fp);\n if (relative.startsWith(\"..\")) continue;\n const normalizedRelative = relative.split(path.sep).join(\"/\");\n virtPath = \"/\" + normalizedRelative;\n } catch {\n continue;\n }\n } else {\n virtPath = fp;\n }\n\n if (!results[virtPath]) {\n results[virtPath] = [];\n }\n results[virtPath].push([i + 1, line]);\n }\n }\n } catch {\n // Skip files we can't read\n continue;\n }\n }\n\n return results;\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n async globInfo(\n pattern: string,\n searchPath: string = \"/\",\n ): Promise<FileInfo[]> {\n if (pattern.startsWith(\"/\")) {\n pattern = pattern.substring(1);\n }\n\n const resolvedSearchPath =\n searchPath === \"/\" ? this.cwd : this.resolvePath(searchPath);\n\n try {\n const stat = await fs.stat(resolvedSearchPath);\n if (!stat.isDirectory()) {\n return [];\n }\n } catch {\n return [];\n }\n\n const results: FileInfo[] = [];\n\n try {\n // Use fast-glob for pattern matching\n const matches = await fg(pattern, {\n cwd: resolvedSearchPath,\n absolute: true,\n onlyFiles: true,\n dot: true,\n });\n\n for (const matchedPath of matches) {\n try {\n const stat = await fs.stat(matchedPath);\n if (!stat.isFile()) continue;\n\n // Normalize fast-glob paths to platform separators\n // fast-glob returns forward slashes on all platforms, but we need\n // platform-native separators for path comparisons on Windows\n const normalizedPath = matchedPath.split(\"/\").join(path.sep);\n\n if (!this.virtualMode) {\n results.push({\n path: normalizedPath,\n is_dir: false,\n size: stat.size,\n modified_at: stat.mtime.toISOString(),\n });\n } else {\n const cwdStr = this.cwd.endsWith(path.sep)\n ? this.cwd\n : this.cwd + path.sep;\n let relativePath: string;\n\n if (normalizedPath.startsWith(cwdStr)) {\n relativePath = normalizedPath.substring(cwdStr.length);\n } else if (normalizedPath.startsWith(this.cwd)) {\n relativePath = normalizedPath\n .substring(this.cwd.length)\n .replace(/^[/\\\\]/, \"\");\n } else {\n relativePath = normalizedPath;\n }\n\n relativePath = relativePath.split(path.sep).join(\"/\");\n const virt = \"/\" + relativePath;\n results.push({\n path: virt,\n is_dir: false,\n size: stat.size,\n modified_at: stat.mtime.toISOString(),\n });\n }\n } catch {\n // Skip files we can't stat\n continue;\n }\n }\n } catch {\n // Ignore glob errors\n }\n\n results.sort((a, b) => a.path.localeCompare(b.path));\n return results;\n }\n\n /**\n * Upload multiple files to the filesystem.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const responses: FileUploadResponse[] = [];\n\n for (const [filePath, content] of files) {\n try {\n const resolvedPath = this.resolvePath(filePath);\n\n // Ensure parent directory exists\n await fs.mkdir(path.dirname(resolvedPath), { recursive: true });\n\n // Write file\n await fs.writeFile(resolvedPath, content);\n responses.push({ path: filePath, error: null });\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n responses.push({ path: filePath, error: \"file_not_found\" });\n } else if (e.code === \"EACCES\") {\n responses.push({ path: filePath, error: \"permission_denied\" });\n } else if (e.code === \"EISDIR\") {\n responses.push({ path: filePath, error: \"is_directory\" });\n } else {\n responses.push({ path: filePath, error: \"invalid_path\" });\n }\n }\n }\n\n return responses;\n }\n\n /**\n * Download multiple files from the filesystem.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const responses: FileDownloadResponse[] = [];\n\n for (const filePath of paths) {\n try {\n const resolvedPath = this.resolvePath(filePath);\n const content = await fs.readFile(resolvedPath);\n responses.push({ path: filePath, content, error: null });\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n responses.push({\n path: filePath,\n content: null,\n error: \"file_not_found\",\n });\n } else if (e.code === \"EACCES\") {\n responses.push({\n path: filePath,\n content: null,\n error: \"permission_denied\",\n });\n } else if (e.code === \"EISDIR\") {\n responses.push({\n path: filePath,\n content: null,\n error: \"is_directory\",\n });\n } else {\n responses.push({\n path: filePath,\n content: null,\n error: \"invalid_path\",\n });\n }\n }\n }\n\n return responses;\n }\n}\n","/**\n * CompositeBackend: Route operations to different backends based on path prefix.\n */\n\nimport type {\n BackendProtocol,\n EditResult,\n ExecuteResponse,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n WriteResult,\n} from \"./protocol.js\";\nimport { isSandboxBackend } from \"./protocol.js\";\n\n/**\n * Backend that routes file operations to different backends based on path prefix.\n *\n * This enables hybrid storage strategies like:\n * - `/memories/` → StoreBackend (persistent, cross-thread)\n * - Everything else → StateBackend (ephemeral, per-thread)\n *\n * The CompositeBackend handles path prefix stripping/re-adding transparently.\n */\nexport class CompositeBackend implements BackendProtocol {\n private default: BackendProtocol;\n private routes: Record<string, BackendProtocol>;\n private sortedRoutes: Array<[string, BackendProtocol]>;\n\n constructor(\n defaultBackend: BackendProtocol,\n routes: Record<string, BackendProtocol>,\n ) {\n this.default = defaultBackend;\n this.routes = routes;\n\n // Sort routes by length (longest first) for correct prefix matching\n this.sortedRoutes = Object.entries(routes).sort(\n (a, b) => b[0].length - a[0].length,\n );\n }\n\n /**\n * Determine which backend handles this key and strip prefix.\n *\n * @param key - Original file path\n * @returns Tuple of [backend, stripped_key] where stripped_key has the route\n * prefix removed (but keeps leading slash).\n */\n private getBackendAndKey(key: string): [BackendProtocol, string] {\n // Check routes in order of length (longest first)\n for (const [prefix, backend] of this.sortedRoutes) {\n if (key.startsWith(prefix)) {\n // Strip full prefix and ensure a leading slash remains\n // e.g., \"/memories/notes.txt\" → \"/notes.txt\"; \"/memories/\" → \"/\"\n const suffix = key.substring(prefix.length);\n const strippedKey = suffix ? \"/\" + suffix : \"/\";\n return [backend, strippedKey];\n }\n }\n\n return [this.default, key];\n }\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects with route prefixes added, for files and directories\n * directly in the directory. Directories have a trailing / in their path and is_dir=true.\n */\n async lsInfo(path: string): Promise<FileInfo[]> {\n // Check if path matches a specific route\n for (const [routePrefix, backend] of this.sortedRoutes) {\n if (path.startsWith(routePrefix.replace(/\\/$/, \"\"))) {\n // Query only the matching routed backend\n const suffix = path.substring(routePrefix.length);\n const searchPath = suffix ? \"/\" + suffix : \"/\";\n const infos = await backend.lsInfo(searchPath);\n\n // Add route prefix back to paths\n const prefixed: FileInfo[] = [];\n for (const fi of infos) {\n prefixed.push({\n ...fi,\n path: routePrefix.slice(0, -1) + fi.path,\n });\n }\n return prefixed;\n }\n }\n\n // At root, aggregate default and all routed backends\n if (path === \"/\") {\n const results: FileInfo[] = [];\n const defaultInfos = await this.default.lsInfo(path);\n results.push(...defaultInfos);\n\n // Add the route itself as a directory (e.g., /memories/)\n for (const [routePrefix] of this.sortedRoutes) {\n results.push({\n path: routePrefix,\n is_dir: true,\n size: 0,\n modified_at: \"\",\n });\n }\n\n results.sort((a, b) => a.path.localeCompare(b.path));\n return results;\n }\n\n // Path doesn't match a route: query only default backend\n return await this.default.lsInfo(path);\n }\n\n /**\n * Read file content, routing to appropriate backend.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n const [backend, strippedKey] = this.getBackendAndKey(filePath);\n return await backend.read(strippedKey, offset, limit);\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n async readRaw(filePath: string): Promise<FileData> {\n const [backend, strippedKey] = this.getBackendAndKey(filePath);\n return await backend.readRaw(strippedKey);\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n async grepRaw(\n pattern: string,\n path: string = \"/\",\n glob: string | null = null,\n ): Promise<GrepMatch[] | string> {\n // If path targets a specific route, search only that backend\n for (const [routePrefix, backend] of this.sortedRoutes) {\n if (path.startsWith(routePrefix.replace(/\\/$/, \"\"))) {\n const searchPath = path.substring(routePrefix.length - 1);\n const raw = await backend.grepRaw(pattern, searchPath || \"/\", glob);\n\n if (typeof raw === \"string\") {\n return raw;\n }\n\n // Add route prefix back\n return raw.map((m) => ({\n ...m,\n path: routePrefix.slice(0, -1) + m.path,\n }));\n }\n }\n\n // Otherwise, search default and all routed backends and merge\n const allMatches: GrepMatch[] = [];\n const rawDefault = await this.default.grepRaw(pattern, path, glob);\n\n if (typeof rawDefault === \"string\") {\n return rawDefault;\n }\n\n allMatches.push(...rawDefault);\n\n // Search all routes\n for (const [routePrefix, backend] of Object.entries(this.routes)) {\n const raw = await backend.grepRaw(pattern, \"/\", glob);\n\n if (typeof raw === \"string\") {\n return raw;\n }\n\n // Add route prefix back\n allMatches.push(\n ...raw.map((m) => ({\n ...m,\n path: routePrefix.slice(0, -1) + m.path,\n })),\n );\n }\n\n return allMatches;\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n async globInfo(pattern: string, path: string = \"/\"): Promise<FileInfo[]> {\n const results: FileInfo[] = [];\n\n // Route based on path, not pattern\n for (const [routePrefix, backend] of this.sortedRoutes) {\n if (path.startsWith(routePrefix.replace(/\\/$/, \"\"))) {\n const searchPath = path.substring(routePrefix.length - 1);\n const infos = await backend.globInfo(pattern, searchPath || \"/\");\n\n // Add route prefix back\n return infos.map((fi) => ({\n ...fi,\n path: routePrefix.slice(0, -1) + fi.path,\n }));\n }\n }\n\n // Path doesn't match any specific route - search default backend AND all routed backends\n const defaultInfos = await this.default.globInfo(pattern, path);\n results.push(...defaultInfos);\n\n for (const [routePrefix, backend] of Object.entries(this.routes)) {\n const infos = await backend.globInfo(pattern, \"/\");\n results.push(\n ...infos.map((fi) => ({\n ...fi,\n path: routePrefix.slice(0, -1) + fi.path,\n })),\n );\n }\n\n // Deterministic ordering\n results.sort((a, b) => a.path.localeCompare(b.path));\n return results;\n }\n\n /**\n * Create a new file, routing to appropriate backend.\n *\n * @param filePath - Absolute file path\n * @param content - File content as string\n * @returns WriteResult with path or error\n */\n async write(filePath: string, content: string): Promise<WriteResult> {\n const [backend, strippedKey] = this.getBackendAndKey(filePath);\n return await backend.write(strippedKey, content);\n }\n\n /**\n * Edit a file, routing to appropriate backend.\n *\n * @param filePath - Absolute file path\n * @param oldString - String to find and replace\n * @param newString - Replacement string\n * @param replaceAll - If true, replace all occurrences\n * @returns EditResult with path, occurrences, or error\n */\n async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n const [backend, strippedKey] = this.getBackendAndKey(filePath);\n return await backend.edit(strippedKey, oldString, newString, replaceAll);\n }\n\n /**\n * Execute a command via the default backend.\n * Execution is not path-specific, so it always delegates to the default backend.\n *\n * @param command - Full shell command string to execute\n * @returns ExecuteResponse with combined output, exit code, and truncation flag\n * @throws Error if the default backend doesn't support command execution\n */\n execute(command: string): Promise<ExecuteResponse> {\n if (!isSandboxBackend(this.default)) {\n throw new Error(\n \"Default backend doesn't support command execution (SandboxBackendProtocol). \" +\n \"To enable execution, provide a default backend that implements SandboxBackendProtocol.\",\n );\n }\n return Promise.resolve(this.default.execute(command));\n }\n\n /**\n * Upload multiple files, batching by backend for efficiency.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const results: Array<FileUploadResponse | null> = Array.from(\n { length: files.length },\n () => null,\n );\n const batchesByBackend = new Map<\n BackendProtocol,\n Array<{ idx: number; path: string; content: Uint8Array }>\n >();\n\n for (let idx = 0; idx < files.length; idx++) {\n const [path, content] = files[idx];\n const [backend, strippedPath] = this.getBackendAndKey(path);\n\n if (!batchesByBackend.has(backend)) {\n batchesByBackend.set(backend, []);\n }\n batchesByBackend.get(backend)!.push({ idx, path: strippedPath, content });\n }\n\n for (const [backend, batch] of batchesByBackend) {\n if (!backend.uploadFiles) {\n throw new Error(\"Backend does not support uploadFiles\");\n }\n\n const batchFiles = batch.map(\n (b) => [b.path, b.content] as [string, Uint8Array],\n );\n const batchResponses = await backend.uploadFiles(batchFiles);\n\n for (let i = 0; i < batch.length; i++) {\n const originalIdx = batch[i].idx;\n results[originalIdx] = {\n path: files[originalIdx][0], // Original path\n error: batchResponses[i]?.error ?? null,\n };\n }\n }\n\n return results as FileUploadResponse[];\n }\n\n /**\n * Download multiple files, batching by backend for efficiency.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const results: Array<FileDownloadResponse | null> = Array.from(\n { length: paths.length },\n () => null,\n );\n const batchesByBackend = new Map<\n BackendProtocol,\n Array<{ idx: number; path: string }>\n >();\n\n for (let idx = 0; idx < paths.length; idx++) {\n const path = paths[idx];\n const [backend, strippedPath] = this.getBackendAndKey(path);\n\n if (!batchesByBackend.has(backend)) {\n batchesByBackend.set(backend, []);\n }\n batchesByBackend.get(backend)!.push({ idx, path: strippedPath });\n }\n\n for (const [backend, batch] of batchesByBackend) {\n if (!backend.downloadFiles) {\n throw new Error(\"Backend does not support downloadFiles\");\n }\n\n const batchPaths = batch.map((b) => b.path);\n const batchResponses = await backend.downloadFiles(batchPaths);\n\n for (let i = 0; i < batch.length; i++) {\n const originalIdx = batch[i].idx;\n results[originalIdx] = {\n path: paths[originalIdx], // Original path\n content: batchResponses[i]?.content ?? null,\n error: batchResponses[i]?.error ?? null,\n };\n }\n }\n\n return results as FileDownloadResponse[];\n }\n}\n","/**\n * BaseSandbox: Abstract base class for sandbox backends with command execution.\n *\n * This class provides default implementations for all SandboxBackendProtocol\n * methods using shell commands executed via execute(). Concrete implementations\n * only need to implement the execute() method.\n *\n * Requires Node.js 20+ on the sandbox host.\n */\n\nimport type {\n EditResult,\n ExecuteResponse,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n MaybePromise,\n SandboxBackendProtocol,\n WriteResult,\n} from \"./protocol.js\";\n\n/**\n * Node.js command template for glob operations.\n * Uses web-standard atob() for base64 decoding.\n */\nfunction buildGlobCommand(searchPath: string, pattern: string): string {\n const pathB64 = btoa(searchPath);\n const patternB64 = btoa(pattern);\n\n return `node -e \"\nconst fs = require('fs');\nconst path = require('path');\n\nconst searchPath = atob('${pathB64}');\nconst pattern = atob('${patternB64}');\n\nfunction globMatch(relativePath, pattern) {\n const regexPattern = pattern\n .replace(/\\\\*\\\\*/g, '<<<GLOBSTAR>>>')\n .replace(/\\\\*/g, '[^/]*')\n .replace(/\\\\?/g, '.')\n .replace(/<<<GLOBSTAR>>>/g, '.*');\n return new RegExp('^' + regexPattern + '$').test(relativePath);\n}\n\nfunction walkDir(dir, baseDir, results) {\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n const fullPath = path.join(dir, entry.name);\n const relativePath = path.relative(baseDir, fullPath);\n if (entry.isDirectory()) {\n walkDir(fullPath, baseDir, results);\n } else if (globMatch(relativePath, pattern)) {\n const stat = fs.statSync(fullPath);\n console.log(JSON.stringify({\n path: relativePath,\n size: stat.size,\n mtime: stat.mtimeMs,\n isDir: false\n }));\n }\n }\n } catch (e) {\n // Silent failure for non-existent paths\n }\n}\n\ntry {\n process.chdir(searchPath);\n walkDir('.', '.', []);\n} catch (e) {\n // Silent failure for non-existent paths\n}\n\"`;\n}\n\n/**\n * Node.js command template for listing directory contents.\n */\nfunction buildLsCommand(dirPath: string): string {\n const pathB64 = btoa(dirPath);\n\n return `node -e \"\nconst fs = require('fs');\nconst path = require('path');\n\nconst dirPath = atob('${pathB64}');\n\ntry {\n const entries = fs.readdirSync(dirPath, { withFileTypes: true });\n for (const entry of entries) {\n const fullPath = path.join(dirPath, entry.name);\n const stat = fs.statSync(fullPath);\n console.log(JSON.stringify({\n path: entry.isDirectory() ? fullPath + '/' : fullPath,\n size: stat.size,\n mtime: stat.mtimeMs,\n isDir: entry.isDirectory()\n }));\n }\n} catch (e) {\n console.error('Error: ' + e.message);\n process.exit(1);\n}\n\"`;\n}\n\n/**\n * Node.js command template for reading files.\n */\nfunction buildReadCommand(\n filePath: string,\n offset: number,\n limit: number,\n): string {\n const pathB64 = btoa(filePath);\n // Coerce offset and limit to safe non-negative integers before embedding in the shell command.\n const safeOffset =\n Number.isFinite(offset) && offset > 0 ? Math.floor(offset) : 0;\n const safeLimit =\n Number.isFinite(limit) && limit > 0 && limit < Number.MAX_SAFE_INTEGER\n ? Math.floor(limit)\n : 0;\n\n return `node -e \"\nconst fs = require('fs');\n\nconst filePath = atob('${pathB64}');\nconst offset = ${safeOffset};\nconst limit = ${safeLimit};\n\nif (!fs.existsSync(filePath)) {\n console.log('Error: File not found');\n process.exit(1);\n}\n\nconst stat = fs.statSync(filePath);\nif (stat.size === 0) {\n console.log('System reminder: File exists but has empty contents');\n process.exit(0);\n}\n\nconst content = fs.readFileSync(filePath, 'utf-8');\nconst lines = content.split('\\\\n');\nconst selected = lines.slice(offset, offset + limit);\n\nfor (let i = 0; i < selected.length; i++) {\n const lineNum = offset + i + 1;\n console.log(String(lineNum).padStart(6) + '\\\\t' + selected[i]);\n}\n\"`;\n}\n\n/**\n * Node.js command template for writing files.\n */\nfunction buildWriteCommand(filePath: string, content: string): string {\n const pathB64 = btoa(filePath);\n const contentB64 = btoa(content);\n\n return `node -e \"\nconst fs = require('fs');\nconst path = require('path');\n\nconst filePath = atob('${pathB64}');\nconst content = atob('${contentB64}');\n\nif (fs.existsSync(filePath)) {\n console.error('Error: File already exists');\n process.exit(1);\n}\n\nconst parentDir = path.dirname(filePath) || '.';\nfs.mkdirSync(parentDir, { recursive: true });\n\nfs.writeFileSync(filePath, content, 'utf-8');\nconsole.log('OK');\n\"`;\n}\n\n/**\n * Node.js command template for editing files.\n */\nfunction buildEditCommand(\n filePath: string,\n oldStr: string,\n newStr: string,\n replaceAll: boolean,\n): string {\n const pathB64 = btoa(filePath);\n const oldB64 = btoa(oldStr);\n const newB64 = btoa(newStr);\n\n return `node -e \"\nconst fs = require('fs');\n\nconst filePath = atob('${pathB64}');\nconst oldStr = atob('${oldB64}');\nconst newStr = atob('${newB64}');\nconst replaceAll = ${Boolean(replaceAll)};\n\nlet text;\ntry {\n text = fs.readFileSync(filePath, 'utf-8');\n} catch (e) {\n process.exit(3);\n}\n\nconst count = text.split(oldStr).length - 1;\n\nif (count === 0) {\n process.exit(1);\n}\nif (count > 1 && !replaceAll) {\n process.exit(2);\n}\n\nconst result = text.split(oldStr).join(newStr);\nfs.writeFileSync(filePath, result, 'utf-8');\nconsole.log(count);\n\"`;\n}\n\n/**\n * Node.js command template for grep operations.\n */\nfunction buildGrepCommand(\n pattern: string,\n searchPath: string,\n globPattern: string | null,\n): string {\n const patternB64 = btoa(pattern);\n const pathB64 = btoa(searchPath);\n const globB64 = globPattern ? btoa(globPattern) : \"\";\n\n return `node -e \"\nconst fs = require('fs');\nconst path = require('path');\n\nconst pattern = atob('${patternB64}');\nconst searchPath = atob('${pathB64}');\nconst globPattern = ${globPattern ? `atob('${globB64}')` : \"null\"};\n\nlet regex;\ntry {\n regex = new RegExp(pattern);\n} catch (e) {\n console.error('Invalid regex: ' + e.message);\n process.exit(1);\n}\n\nfunction globMatch(filePath, pattern) {\n if (!pattern) return true;\n const regexPattern = pattern\n .replace(/\\\\*\\\\*/g, '<<<GLOBSTAR>>>')\n .replace(/\\\\*/g, '[^/]*')\n .replace(/\\\\?/g, '.')\n .replace(/<<<GLOBSTAR>>>/g, '.*');\n return new RegExp('^' + regexPattern + '$').test(filePath);\n}\n\nfunction walkDir(dir, results) {\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n walkDir(fullPath, results);\n } else {\n const relativePath = path.relative(searchPath, fullPath);\n if (globMatch(relativePath, globPattern)) {\n try {\n const content = fs.readFileSync(fullPath, 'utf-8');\n const lines = content.split('\\\\n');\n for (let i = 0; i < lines.length; i++) {\n if (regex.test(lines[i])) {\n console.log(JSON.stringify({\n path: fullPath,\n line: i + 1,\n text: lines[i]\n }));\n }\n }\n } catch (e) {\n // Skip unreadable files\n }\n }\n }\n }\n } catch (e) {\n // Skip unreadable directories\n }\n}\n\ntry {\n walkDir(searchPath, []);\n} catch (e) {\n // Silent failure\n}\n\"`;\n}\n\n/**\n * Base sandbox implementation with execute() as the only abstract method.\n *\n * This class provides default implementations for all SandboxBackendProtocol\n * methods using shell commands executed via execute(). Concrete implementations\n * only need to implement the execute() method.\n *\n * Requires Node.js 20+ on the sandbox host.\n */\nexport abstract class BaseSandbox implements SandboxBackendProtocol {\n /** Unique identifier for the sandbox backend */\n abstract readonly id: string;\n\n /**\n * Execute a command in the sandbox.\n * This is the only method concrete implementations must provide.\n */\n abstract execute(command: string): MaybePromise<ExecuteResponse>;\n\n /**\n * Upload multiple files to the sandbox.\n * Implementations must support partial success.\n */\n abstract uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): MaybePromise<FileUploadResponse[]>;\n\n /**\n * Download multiple files from the sandbox.\n * Implementations must support partial success.\n */\n abstract downloadFiles(paths: string[]): MaybePromise<FileDownloadResponse[]>;\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects for files and directories directly in the directory.\n */\n async lsInfo(path: string): Promise<FileInfo[]> {\n const command = buildLsCommand(path);\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n return [];\n }\n\n const infos: FileInfo[] = [];\n const lines = result.output.trim().split(\"\\n\").filter(Boolean);\n\n for (const line of lines) {\n try {\n const parsed = JSON.parse(line);\n infos.push({\n path: parsed.path,\n is_dir: parsed.isDir,\n size: parsed.size,\n modified_at: parsed.mtime\n ? new Date(parsed.mtime).toISOString()\n : undefined,\n });\n } catch {\n // Skip invalid JSON lines\n }\n }\n\n return infos;\n }\n\n /**\n * Read file content with line numbers.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n const command = buildReadCommand(filePath, offset, limit);\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n return `Error: File '${filePath}' not found`;\n }\n\n return result.output;\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n async readRaw(filePath: string): Promise<FileData> {\n const command = buildReadCommand(filePath, 0, Number.MAX_SAFE_INTEGER);\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n throw new Error(`File '${filePath}' not found`);\n }\n\n // Parse the line-numbered output back to content\n const lines: string[] = [];\n for (const line of result.output.split(\"\\n\")) {\n // Format is \" 123\\tContent\"\n const tabIndex = line.indexOf(\"\\t\");\n if (tabIndex !== -1) {\n lines.push(line.substring(tabIndex + 1));\n }\n }\n\n const now = new Date().toISOString();\n return {\n content: lines,\n created_at: now,\n modified_at: now,\n };\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n async grepRaw(\n pattern: string,\n path: string = \"/\",\n glob: string | null = null,\n ): Promise<GrepMatch[] | string> {\n const command = buildGrepCommand(pattern, path, glob);\n const result = await this.execute(command);\n\n if (result.exitCode === 1) {\n // Check if it's a regex error\n if (result.output.includes(\"Invalid regex:\")) {\n return result.output.trim();\n }\n }\n\n const matches: GrepMatch[] = [];\n const lines = result.output.trim().split(\"\\n\").filter(Boolean);\n\n for (const line of lines) {\n try {\n const parsed = JSON.parse(line);\n matches.push({\n path: parsed.path,\n line: parsed.line,\n text: parsed.text,\n });\n } catch {\n // Skip invalid JSON lines\n }\n }\n\n return matches;\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n async globInfo(pattern: string, path: string = \"/\"): Promise<FileInfo[]> {\n const command = buildGlobCommand(path, pattern);\n const result = await this.execute(command);\n\n const infos: FileInfo[] = [];\n const lines = result.output.trim().split(\"\\n\").filter(Boolean);\n\n for (const line of lines) {\n try {\n const parsed = JSON.parse(line);\n infos.push({\n path: parsed.path,\n is_dir: parsed.isDir,\n size: parsed.size,\n modified_at: parsed.mtime\n ? new Date(parsed.mtime).toISOString()\n : undefined,\n });\n } catch {\n // Skip invalid JSON lines\n }\n }\n\n return infos;\n }\n\n /**\n * Create a new file with content.\n */\n async write(filePath: string, content: string): Promise<WriteResult> {\n const command = buildWriteCommand(filePath, content);\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n }\n\n return { path: filePath, filesUpdate: null };\n }\n\n /**\n * Edit a file by replacing string occurrences.\n */\n async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n const command = buildEditCommand(\n filePath,\n oldString,\n newString,\n replaceAll,\n );\n const result = await this.execute(command);\n\n switch (result.exitCode) {\n case 0: {\n const occurrences = parseInt(result.output.trim(), 10) || 1;\n return { path: filePath, filesUpdate: null, occurrences };\n }\n case 1:\n return { error: `String not found in file '${filePath}'` };\n case 2:\n return {\n error: `Multiple occurrences found in '${filePath}'. Use replaceAll=true to replace all.`,\n };\n case 3:\n return { error: `Error: File '${filePath}' not found` };\n default:\n return { error: `Unknown error editing file '${filePath}'` };\n }\n }\n}\n","import {\n createAgent,\n humanInTheLoopMiddleware,\n anthropicPromptCachingMiddleware,\n todoListMiddleware,\n summarizationMiddleware,\n SystemMessage,\n type AgentMiddleware,\n type ResponseFormat,\n} from \"langchain\";\nimport type {\n ClientTool,\n ServerTool,\n StructuredTool,\n} from \"@langchain/core/tools\";\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\nimport {\n createFilesystemMiddleware,\n createSubAgentMiddleware,\n createPatchToolCallsMiddleware,\n createMemoryMiddleware,\n createSkillsMiddleware,\n type SubAgent,\n} from \"./middleware/index.js\";\nimport { StateBackend } from \"./backends/index.js\";\nimport { InteropZodObject } from \"@langchain/core/utils/types\";\nimport { CompiledSubAgent } from \"./middleware/subagents.js\";\nimport type {\n CreateDeepAgentParams,\n DeepAgent,\n DeepAgentTypeConfig,\n FlattenSubAgentMiddleware,\n} from \"./types.js\";\n\n/**\n * required for type inference\n */\nimport type * as _messages from \"@langchain/core/messages\";\nimport type * as _Command from \"@langchain/langgraph\";\n\nconst BASE_PROMPT = `In order to complete the objective that the user asks of you, you have access to a number of standard tools.`;\n\n/**\n * Create a Deep Agent with middleware-based architecture.\n *\n * Matches Python's create_deep_agent function, using middleware for all features:\n * - Todo management (todoListMiddleware)\n * - Filesystem tools (createFilesystemMiddleware)\n * - Subagent delegation (createSubAgentMiddleware)\n * - Conversation summarization (summarizationMiddleware)\n * - Prompt caching (anthropicPromptCachingMiddleware)\n * - Tool call patching (createPatchToolCallsMiddleware)\n * - Human-in-the-loop (humanInTheLoopMiddleware) - optional\n *\n * @param params Configuration parameters for the agent\n * @returns ReactAgent instance ready for invocation with properly inferred state types\n *\n * @example\n * ```typescript\n * // Middleware with custom state\n * const ResearchMiddleware = createMiddleware({\n * name: \"ResearchMiddleware\",\n * stateSchema: z.object({ research: z.string().default(\"\") }),\n * });\n *\n * const agent = createDeepAgent({\n * middleware: [ResearchMiddleware],\n * });\n *\n * const result = await agent.invoke({ messages: [...] });\n * // result.research is properly typed as string\n * ```\n */\nexport function createDeepAgent<\n TResponse extends ResponseFormat = ResponseFormat,\n ContextSchema extends InteropZodObject = InteropZodObject,\n const TMiddleware extends readonly AgentMiddleware[] = readonly [],\n const TSubagents extends readonly (SubAgent | CompiledSubAgent)[] =\n readonly [],\n const TTools extends readonly (ClientTool | ServerTool)[] = readonly [],\n>(\n params: CreateDeepAgentParams<\n TResponse,\n ContextSchema,\n TMiddleware,\n TSubagents,\n TTools\n > = {} as CreateDeepAgentParams<\n TResponse,\n ContextSchema,\n TMiddleware,\n TSubagents,\n TTools\n >,\n) {\n const {\n model = \"claude-sonnet-4-5-20250929\",\n tools = [],\n systemPrompt,\n middleware: customMiddleware = [],\n subagents = [],\n responseFormat,\n contextSchema,\n checkpointer,\n store,\n backend,\n interruptOn,\n name,\n memory,\n skills,\n } = params;\n\n // Combine system prompt with base prompt like Python implementation\n const finalSystemPrompt = systemPrompt\n ? typeof systemPrompt === \"string\"\n ? `${systemPrompt}\\n\\n${BASE_PROMPT}`\n : new SystemMessage({\n content: [\n {\n type: \"text\",\n text: BASE_PROMPT,\n },\n ...(typeof systemPrompt.content === \"string\"\n ? [{ type: \"text\", text: systemPrompt.content }]\n : systemPrompt.content),\n ],\n })\n : BASE_PROMPT;\n\n // Create backend configuration for filesystem middleware\n // If no backend is provided, use a factory that creates a StateBackend\n const filesystemBackend = backend\n ? backend\n : (config: { state: unknown; store?: BaseStore }) =>\n new StateBackend(config);\n\n // Add skills middleware if skill sources provided\n const skillsMiddleware =\n skills != null && skills.length > 0\n ? [\n createSkillsMiddleware({\n backend: filesystemBackend,\n sources: skills,\n }),\n ]\n : [];\n\n // Built-in middleware array\n const builtInMiddleware = [\n // Provides todo list management capabilities for tracking tasks\n todoListMiddleware(),\n // Add skills middleware if skill sources provided\n ...skillsMiddleware,\n // Enables filesystem operations and optional long-term memory storage\n createFilesystemMiddleware({ backend: filesystemBackend }),\n // Enables delegation to specialized subagents for complex tasks\n createSubAgentMiddleware({\n defaultModel: model,\n defaultTools: tools as StructuredTool[],\n defaultMiddleware: [\n // Subagent middleware: Todo list management\n todoListMiddleware(),\n // Subagent middleware: Skills (if provided)\n ...skillsMiddleware,\n // Subagent middleware: Filesystem operations\n createFilesystemMiddleware({\n backend: filesystemBackend,\n }),\n // Subagent middleware: Automatic conversation summarization when token limits are approached\n summarizationMiddleware({\n model,\n trigger: { tokens: 170_000 },\n keep: { messages: 6 },\n }),\n // Subagent middleware: Anthropic prompt caching for improved performance\n anthropicPromptCachingMiddleware({\n unsupportedModelBehavior: \"ignore\",\n }),\n // Subagent middleware: Patches tool calls for compatibility\n createPatchToolCallsMiddleware(),\n ],\n defaultInterruptOn: interruptOn,\n subagents: subagents as unknown as (SubAgent | CompiledSubAgent)[],\n generalPurposeAgent: true,\n }),\n // Automatically summarizes conversation history when token limits are approached\n summarizationMiddleware({\n model,\n trigger: { tokens: 170_000 },\n keep: { messages: 6 },\n }),\n // Enables Anthropic prompt caching for improved performance and reduced costs\n anthropicPromptCachingMiddleware({\n unsupportedModelBehavior: \"ignore\",\n }),\n // Patches tool calls to ensure compatibility across different model providers\n createPatchToolCallsMiddleware(),\n // Add memory middleware if memory sources provided\n ...(memory != null && memory.length > 0\n ? [\n createMemoryMiddleware({\n backend: filesystemBackend,\n sources: memory,\n }),\n ]\n : []),\n ] as const;\n\n // Add human-in-the-loop middleware if interrupt config provided\n if (interruptOn) {\n // builtInMiddleware is typed as readonly to enable type inference\n // however, we need to push to it to add the middleware, so let's ignore the type error\n // @ts-expect-error - builtInMiddleware is readonly\n builtInMiddleware.push(humanInTheLoopMiddleware({ interruptOn }));\n }\n\n // Combine built-in middleware with custom middleware\n // The custom middleware is typed as TMiddleware to preserve type information\n const allMiddleware = [\n ...builtInMiddleware,\n ...(customMiddleware as unknown as TMiddleware),\n ] as const;\n\n // Note: Recursion limit of 1000 (matching Python behavior) should be passed\n // at invocation time: agent.invoke(input, { recursionLimit: 1000 })\n const agent = createAgent({\n model,\n systemPrompt: finalSystemPrompt,\n tools: tools as StructuredTool[],\n middleware: allMiddleware as unknown as AgentMiddleware[],\n responseFormat: responseFormat as ResponseFormat,\n contextSchema,\n checkpointer,\n store,\n name,\n });\n\n // Combine custom middleware with flattened subagent middleware for complete type inference\n // This ensures InferMiddlewareStates captures state from both sources\n type AllMiddleware = readonly [\n ...typeof builtInMiddleware,\n ...TMiddleware,\n ...FlattenSubAgentMiddleware<TSubagents>,\n ];\n\n // Return as DeepAgent with proper DeepAgentTypeConfig\n // - Response: TResponse (from responseFormat parameter)\n // - State: undefined (state comes from middleware)\n // - Context: ContextSchema\n // - Middleware: AllMiddleware (built-in + custom + subagent middleware for state inference)\n // - Tools: TTools\n // - Subagents: TSubagents (for type-safe streaming)\n return agent as unknown as DeepAgent<\n DeepAgentTypeConfig<\n TResponse,\n undefined,\n ContextSchema,\n AllMiddleware,\n TTools,\n TSubagents\n >\n >;\n}\n","/**\n * Configuration and settings for deepagents.\n *\n * Provides project detection, path management, and environment configuration\n * for skills and agent memory middleware.\n */\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport os from \"node:os\";\n\n/**\n * Options for creating a Settings instance.\n */\nexport interface SettingsOptions {\n /** Starting directory for project detection (defaults to cwd) */\n startPath?: string;\n}\n\n/**\n * Settings interface for project detection and path management.\n *\n * Provides access to:\n * - Project root detection (via .git directory)\n * - User-level deepagents directory (~/.deepagents)\n * - Agent-specific directories and files\n * - Skills directories (user and project level)\n */\nexport interface Settings {\n /** Detected project root directory, or null if not in a git project */\n readonly projectRoot: string | null;\n\n /** Base user-level .deepagents directory (~/.deepagents) */\n readonly userDeepagentsDir: string;\n\n /** Check if currently in a git project */\n readonly hasProject: boolean;\n\n /**\n * Get the agent directory path.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}\n * @throws Error if agent name is invalid\n */\n getAgentDir(agentName: string): string;\n\n /**\n * Ensure agent directory exists and return path.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}\n * @throws Error if agent name is invalid\n */\n ensureAgentDir(agentName: string): string;\n\n /**\n * Get user-level agent.md path for a specific agent.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}/agent.md\n */\n getUserAgentMdPath(agentName: string): string;\n\n /**\n * Get project-level agent.md path.\n * @returns Path to {projectRoot}/.deepagents/agent.md, or null if not in a project\n */\n getProjectAgentMdPath(): string | null;\n\n /**\n * Get user-level skills directory path for a specific agent.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}/skills/\n */\n getUserSkillsDir(agentName: string): string;\n\n /**\n * Ensure user-level skills directory exists and return path.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}/skills/\n */\n ensureUserSkillsDir(agentName: string): string;\n\n /**\n * Get project-level skills directory path.\n * @returns Path to {projectRoot}/.deepagents/skills/, or null if not in a project\n */\n getProjectSkillsDir(): string | null;\n\n /**\n * Ensure project-level skills directory exists and return path.\n * @returns Path to {projectRoot}/.deepagents/skills/, or null if not in a project\n */\n ensureProjectSkillsDir(): string | null;\n\n /**\n * Ensure project .deepagents directory exists.\n * @returns Path to {projectRoot}/.deepagents/, or null if not in a project\n */\n ensureProjectDeepagentsDir(): string | null;\n}\n\n/**\n * Find the project root by looking for .git directory.\n *\n * Walks up the directory tree from startPath (or cwd) looking for a .git\n * directory, which indicates the project root.\n *\n * @param startPath - Directory to start searching from. Defaults to current working directory.\n * @returns Path to the project root if found, null otherwise.\n */\nexport function findProjectRoot(startPath?: string): string | null {\n let current = path.resolve(startPath || process.cwd());\n\n // Walk up the directory tree\n while (current !== path.dirname(current)) {\n const gitDir = path.join(current, \".git\");\n if (fs.existsSync(gitDir)) {\n return current;\n }\n current = path.dirname(current);\n }\n\n // Check root directory as well\n const rootGitDir = path.join(current, \".git\");\n if (fs.existsSync(rootGitDir)) {\n return current;\n }\n\n return null;\n}\n\n/**\n * Validate agent name to prevent invalid filesystem paths and security issues.\n *\n * @param agentName - The agent name to validate\n * @returns True if valid, false otherwise\n */\nfunction isValidAgentName(agentName: string): boolean {\n if (!agentName || !agentName.trim()) {\n return false;\n }\n // Allow only alphanumeric, hyphens, underscores, and whitespace\n return /^[a-zA-Z0-9_\\-\\s]+$/.test(agentName);\n}\n\n/**\n * Create a Settings instance with detected environment.\n *\n * @param options - Configuration options\n * @returns Settings instance with project detection and path management\n */\nexport function createSettings(options: SettingsOptions = {}): Settings {\n const projectRoot = findProjectRoot(options.startPath);\n const userDeepagentsDir = path.join(os.homedir(), \".deepagents\");\n\n return {\n projectRoot,\n userDeepagentsDir,\n hasProject: projectRoot !== null,\n\n getAgentDir(agentName: string): string {\n if (!isValidAgentName(agentName)) {\n throw new Error(\n `Invalid agent name: ${JSON.stringify(agentName)}. ` +\n \"Agent names can only contain letters, numbers, hyphens, underscores, and spaces.\",\n );\n }\n return path.join(userDeepagentsDir, agentName);\n },\n\n ensureAgentDir(agentName: string): string {\n const agentDir = this.getAgentDir(agentName);\n fs.mkdirSync(agentDir, { recursive: true });\n return agentDir;\n },\n\n getUserAgentMdPath(agentName: string): string {\n return path.join(this.getAgentDir(agentName), \"agent.md\");\n },\n\n getProjectAgentMdPath(): string | null {\n if (!projectRoot) {\n return null;\n }\n return path.join(projectRoot, \".deepagents\", \"agent.md\");\n },\n\n getUserSkillsDir(agentName: string): string {\n return path.join(this.getAgentDir(agentName), \"skills\");\n },\n\n ensureUserSkillsDir(agentName: string): string {\n const skillsDir = this.getUserSkillsDir(agentName);\n fs.mkdirSync(skillsDir, { recursive: true });\n return skillsDir;\n },\n\n getProjectSkillsDir(): string | null {\n if (!projectRoot) {\n return null;\n }\n return path.join(projectRoot, \".deepagents\", \"skills\");\n },\n\n ensureProjectSkillsDir(): string | null {\n const skillsDir = this.getProjectSkillsDir();\n if (!skillsDir) {\n return null;\n }\n fs.mkdirSync(skillsDir, { recursive: true });\n return skillsDir;\n },\n\n ensureProjectDeepagentsDir(): string | null {\n if (!projectRoot) {\n return null;\n }\n const deepagentsDir = path.join(projectRoot, \".deepagents\");\n fs.mkdirSync(deepagentsDir, { recursive: true });\n return deepagentsDir;\n },\n };\n}\n","/**\n * Middleware for loading agent-specific long-term memory into the system prompt.\n *\n * This middleware loads the agent's long-term memory from agent.md files\n * and injects it into the system prompt. Memory is loaded from:\n * - User memory: ~/.deepagents/{agent_name}/agent.md\n * - Project memory: {project_root}/.deepagents/agent.md\n *\n * @deprecated Use `createMemoryMiddleware` from `./memory.js` instead.\n * This middleware uses direct filesystem access (Node.js fs module) which is not\n * portable across backends. The `createMemoryMiddleware` function uses the\n * `BackendProtocol` abstraction and follows the AGENTS.md specification.\n *\n * Migration example:\n * ```typescript\n * // Before (deprecated):\n * import { createAgentMemoryMiddleware } from \"./agent-memory.js\";\n * const middleware = createAgentMemoryMiddleware({ settings, assistantId });\n *\n * // After (recommended):\n * import { createMemoryMiddleware } from \"./memory.js\";\n * import { FilesystemBackend } from \"../backends/filesystem.js\";\n *\n * const middleware = createMemoryMiddleware({\n * backend: new FilesystemBackend({ rootDir: \"/\" }),\n * sources: [\n * `~/.deepagents/${assistantId}/AGENTS.md`,\n * `${projectRoot}/.deepagents/AGENTS.md`,\n * ],\n * });\n * ```\n */\n\nimport fs from \"node:fs\";\nimport { z } from \"zod\";\nimport {\n createMiddleware,\n /**\n * required for type inference\n */\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\n\nimport type { Settings } from \"../config.js\";\n\n/**\n * Options for the agent memory middleware.\n */\nexport interface AgentMemoryMiddlewareOptions {\n /** Settings instance with project detection and paths */\n settings: Settings;\n\n /** The agent identifier */\n assistantId: string;\n\n /** Optional custom template for injecting agent memory into system prompt */\n systemPromptTemplate?: string;\n}\n\n/**\n * State schema for agent memory middleware.\n */\nconst AgentMemoryStateSchema = z.object({\n /** Personal preferences from ~/.deepagents/{agent}/ (applies everywhere) */\n userMemory: z.string().optional(),\n\n /** Project-specific context (loaded from project root) */\n projectMemory: z.string().optional(),\n});\n\n/**\n * Default template for memory injection.\n */\nconst DEFAULT_MEMORY_TEMPLATE = `<user_memory>\n{user_memory}\n</user_memory>\n\n<project_memory>\n{project_memory}\n</project_memory>`;\n\n/**\n * Long-term Memory Documentation system prompt.\n */\nconst LONGTERM_MEMORY_SYSTEM_PROMPT = `\n\n## Long-term Memory\n\nYour long-term memory is stored in files on the filesystem and persists across sessions.\n\n**User Memory Location**: \\`{agent_dir_absolute}\\` (displays as \\`{agent_dir_display}\\`)\n**Project Memory Location**: {project_memory_info}\n\nYour system prompt is loaded from TWO sources at startup:\n1. **User agent.md**: \\`{agent_dir_absolute}/agent.md\\` - Your personal preferences across all projects\n2. **Project agent.md**: Loaded from project root if available - Project-specific instructions\n\nProject-specific agent.md is loaded from these locations (both combined if both exist):\n- \\`[project-root]/.deepagents/agent.md\\` (preferred)\n- \\`[project-root]/agent.md\\` (fallback, but also included if both exist)\n\n**When to CHECK/READ memories (CRITICAL - do this FIRST):**\n- **At the start of ANY new session**: Check both user and project memories\n - User: \\`ls {agent_dir_absolute}\\`\n - Project: \\`ls {project_deepagents_dir}\\` (if in a project)\n- **BEFORE answering questions**: If asked \"what do you know about X?\" or \"how do I do Y?\", check project memories FIRST, then user\n- **When user asks you to do something**: Check if you have project-specific guides or examples\n- **When user references past work**: Search project memory files for related context\n\n**Memory-first response pattern:**\n1. User asks a question → Check project directory first: \\`ls {project_deepagents_dir}\\`\n2. If relevant files exist → Read them with \\`read_file '{project_deepagents_dir}/[filename]'\\`\n3. Check user memory if needed → \\`ls {agent_dir_absolute}\\`\n4. Base your answer on saved knowledge supplemented by general knowledge\n\n**When to update memories:**\n- **IMMEDIATELY when the user describes your role or how you should behave**\n- **IMMEDIATELY when the user gives feedback on your work** - Update memories to capture what was wrong and how to do it better\n- When the user explicitly asks you to remember something\n- When patterns or preferences emerge (coding styles, conventions, workflows)\n- After significant work where context would help in future sessions\n\n**Learning from feedback:**\n- When user says something is better/worse, capture WHY and encode it as a pattern\n- Each correction is a chance to improve permanently - don't just fix the immediate issue, update your instructions\n- When user says \"you should remember X\" or \"be careful about Y\", treat this as HIGH PRIORITY - update memories IMMEDIATELY\n- Look for the underlying principle behind corrections, not just the specific mistake\n\n## Deciding Where to Store Memory\n\nWhen writing or updating agent memory, decide whether each fact, configuration, or behavior belongs in:\n\n### User Agent File: \\`{agent_dir_absolute}/agent.md\\`\n→ Describes the agent's **personality, style, and universal behavior** across all projects.\n\n**Store here:**\n- Your general tone and communication style\n- Universal coding preferences (formatting, comment style, etc.)\n- General workflows and methodologies you follow\n- Tool usage patterns that apply everywhere\n- Personal preferences that don't change per-project\n\n**Examples:**\n- \"Be concise and direct in responses\"\n- \"Always use type hints in Python\"\n- \"Prefer functional programming patterns\"\n\n### Project Agent File: \\`{project_deepagents_dir}/agent.md\\`\n→ Describes **how this specific project works** and **how the agent should behave here only.**\n\n**Store here:**\n- Project-specific architecture and design patterns\n- Coding conventions specific to this codebase\n- Project structure and organization\n- Testing strategies for this project\n- Deployment processes and workflows\n- Team conventions and guidelines\n\n**Examples:**\n- \"This project uses FastAPI with SQLAlchemy\"\n- \"Tests go in tests/ directory mirroring src/ structure\"\n- \"All API changes require updating OpenAPI spec\"\n\n### Project Memory Files: \\`{project_deepagents_dir}/*.md\\`\n→ Use for **project-specific reference information** and structured notes.\n\n**Store here:**\n- API design documentation\n- Architecture decisions and rationale\n- Deployment procedures\n- Common debugging patterns\n- Onboarding information\n\n**Examples:**\n- \\`{project_deepagents_dir}/api-design.md\\` - REST API patterns used\n- \\`{project_deepagents_dir}/architecture.md\\` - System architecture overview\n- \\`{project_deepagents_dir}/deployment.md\\` - How to deploy this project\n\n### File Operations:\n\n**User memory:**\n\\`\\`\\`\nls {agent_dir_absolute} # List user memory files\nread_file '{agent_dir_absolute}/agent.md' # Read user preferences\nedit_file '{agent_dir_absolute}/agent.md' ... # Update user preferences\n\\`\\`\\`\n\n**Project memory (preferred for project-specific information):**\n\\`\\`\\`\nls {project_deepagents_dir} # List project memory files\nread_file '{project_deepagents_dir}/agent.md' # Read project instructions\nedit_file '{project_deepagents_dir}/agent.md' ... # Update project instructions\nwrite_file '{project_deepagents_dir}/agent.md' ... # Create project memory file\n\\`\\`\\`\n\n**Important**:\n- Project memory files are stored in \\`.deepagents/\\` inside the project root\n- Always use absolute paths for file operations\n- Check project memories BEFORE user when answering project-specific questions`;\n\n/**\n * Create middleware for loading agent-specific long-term memory.\n *\n * This middleware loads the agent's long-term memory from a file (agent.md)\n * and injects it into the system prompt. The memory is loaded once at the\n * start of the conversation and stored in state.\n *\n * @param options - Configuration options\n * @returns AgentMiddleware for memory loading and injection\n *\n * @deprecated Use `createMemoryMiddleware` from `./memory.js` instead.\n * This function uses direct filesystem access which limits portability.\n */\nexport function createAgentMemoryMiddleware(\n options: AgentMemoryMiddlewareOptions,\n) {\n const { settings, assistantId, systemPromptTemplate } = options;\n\n // Compute paths\n const agentDir = settings.getAgentDir(assistantId);\n const agentDirDisplay = `~/.deepagents/${assistantId}`;\n const agentDirAbsolute = agentDir;\n const projectRoot = settings.projectRoot;\n\n // Build project memory info for documentation\n const projectMemoryInfo = projectRoot\n ? `\\`${projectRoot}\\` (detected)`\n : \"None (not in a git project)\";\n\n // Build project deepagents directory path\n const projectDeepagentsDir = projectRoot\n ? `${projectRoot}/.deepagents`\n : \"[project-root]/.deepagents (not in a project)\";\n\n const template = systemPromptTemplate || DEFAULT_MEMORY_TEMPLATE;\n\n return createMiddleware({\n name: \"AgentMemoryMiddleware\",\n stateSchema: AgentMemoryStateSchema as any,\n\n beforeAgent(state: any) {\n const result: Record<string, string> = {};\n\n // Load user memory if not already in state\n if (!(\"userMemory\" in state)) {\n const userPath = settings.getUserAgentMdPath(assistantId);\n if (fs.existsSync(userPath)) {\n try {\n result.userMemory = fs.readFileSync(userPath, \"utf-8\");\n } catch {\n // Ignore read errors\n }\n }\n }\n\n // Load project memory if not already in state\n if (!(\"projectMemory\" in state)) {\n const projectPath = settings.getProjectAgentMdPath();\n if (projectPath && fs.existsSync(projectPath)) {\n try {\n result.projectMemory = fs.readFileSync(projectPath, \"utf-8\");\n } catch {\n // Ignore read errors\n }\n }\n }\n\n return Object.keys(result).length > 0 ? result : undefined;\n },\n\n wrapModelCall(request: any, handler: any) {\n // Extract memory from state\n const userMemory = request.state?.userMemory;\n const projectMemory = request.state?.projectMemory;\n const baseSystemPrompt = request.systemPrompt || \"\";\n\n // Format memory section with both memories\n const memorySection = template\n .replace(\"{user_memory}\", userMemory || \"(No user agent.md)\")\n .replace(\"{project_memory}\", projectMemory || \"(No project agent.md)\");\n\n // Format long-term memory documentation\n const memoryDocs = LONGTERM_MEMORY_SYSTEM_PROMPT.replaceAll(\n \"{agent_dir_absolute}\",\n agentDirAbsolute,\n )\n .replaceAll(\"{agent_dir_display}\", agentDirDisplay)\n .replaceAll(\"{project_memory_info}\", projectMemoryInfo)\n .replaceAll(\"{project_deepagents_dir}\", projectDeepagentsDir);\n\n // Memory content at start, base prompt in middle, documentation at end\n let systemPrompt = memorySection;\n if (baseSystemPrompt) {\n systemPrompt += \"\\n\\n\" + baseSystemPrompt;\n }\n systemPrompt += \"\\n\\n\" + memoryDocs;\n\n return handler({ ...request, systemPrompt });\n },\n });\n}\n","/**\n * Skill loader for parsing and loading agent skills from SKILL.md files.\n *\n * This module implements Anthropic's agent skills pattern with YAML frontmatter parsing.\n * Each skill is a directory containing a SKILL.md file with:\n * - YAML frontmatter (name, description required)\n * - Markdown instructions for the agent\n * - Optional supporting files (scripts, configs, etc.)\n *\n * @example\n * ```markdown\n * ---\n * name: web-research\n * description: Structured approach to conducting thorough web research\n * ---\n *\n * # Web Research Skill\n *\n * ## When to Use\n * - User asks you to research a topic\n * ...\n * ```\n *\n * @see https://agentskills.io/specification\n */\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport yaml from \"yaml\";\n\n/** Maximum size for SKILL.md files (10MB) */\nexport const MAX_SKILL_FILE_SIZE = 10 * 1024 * 1024;\n\n/** Agent Skills spec constraints */\nexport const MAX_SKILL_NAME_LENGTH = 64;\nexport const MAX_SKILL_DESCRIPTION_LENGTH = 1024;\n\n/** Pattern for validating skill names per Agent Skills spec */\nconst SKILL_NAME_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;\n\n/** Pattern for extracting YAML frontmatter */\nconst FRONTMATTER_PATTERN = /^---\\s*\\n([\\s\\S]*?)\\n---\\s*\\n/;\n\n/**\n * Metadata for a skill per Agent Skills spec.\n * @see https://agentskills.io/specification\n */\nexport interface SkillMetadata {\n /** Name of the skill (max 64 chars, lowercase alphanumeric and hyphens) */\n name: string;\n\n /** Description of what the skill does (max 1024 chars) */\n description: string;\n\n /** Absolute path to the SKILL.md file */\n path: string;\n\n /** Source of the skill ('user' or 'project') */\n source: \"user\" | \"project\";\n\n /** Optional: License name or reference to bundled license file */\n license?: string;\n\n /** Optional: Environment requirements (max 500 chars) */\n compatibility?: string;\n\n /** Optional: Arbitrary key-value mapping for additional metadata */\n metadata?: Record<string, string>;\n\n /** Optional: Space-delimited list of pre-approved tools */\n allowedTools?: string;\n}\n\n/**\n * Options for listing skills.\n */\nexport interface ListSkillsOptions {\n /** Path to user-level skills directory */\n userSkillsDir?: string | null;\n\n /** Path to project-level skills directory */\n projectSkillsDir?: string | null;\n}\n\n/**\n * Result of skill name validation.\n */\ninterface ValidationResult {\n valid: boolean;\n error?: string;\n}\n\n/**\n * Check if a path is safely contained within base_dir.\n *\n * This prevents directory traversal attacks via symlinks or path manipulation.\n * The function resolves both paths to their canonical form (following symlinks)\n * and verifies that the target path is within the base directory.\n *\n * @param targetPath - The path to validate\n * @param baseDir - The base directory that should contain the path\n * @returns True if the path is safely within baseDir, false otherwise\n */\nfunction isSafePath(targetPath: string, baseDir: string): boolean {\n try {\n // Resolve both paths to their canonical form (follows symlinks)\n const resolvedPath = fs.realpathSync(targetPath);\n const resolvedBase = fs.realpathSync(baseDir);\n\n // Check if the resolved path is within the base directory\n return (\n resolvedPath.startsWith(resolvedBase + path.sep) ||\n resolvedPath === resolvedBase\n );\n } catch {\n // Error resolving paths (e.g., circular symlinks, too many levels)\n return false;\n }\n}\n\n/**\n * Validate skill name per Agent Skills spec.\n *\n * Requirements:\n * - Max 64 characters\n * - Lowercase alphanumeric and hyphens only (a-z, 0-9, -)\n * - Cannot start or end with hyphen\n * - No consecutive hyphens\n * - Must match parent directory name\n *\n * @param name - The skill name from YAML frontmatter\n * @param directoryName - The parent directory name\n * @returns Validation result with error message if invalid\n */\nfunction validateSkillName(\n name: string,\n directoryName: string,\n): ValidationResult {\n if (!name) {\n return { valid: false, error: \"name is required\" };\n }\n if (name.length > MAX_SKILL_NAME_LENGTH) {\n return { valid: false, error: \"name exceeds 64 characters\" };\n }\n // Pattern: lowercase alphanumeric, single hyphens between segments, no start/end hyphen\n if (!SKILL_NAME_PATTERN.test(name)) {\n return {\n valid: false,\n error: \"name must be lowercase alphanumeric with single hyphens only\",\n };\n }\n if (name !== directoryName) {\n return {\n valid: false,\n error: `name '${name}' must match directory name '${directoryName}'`,\n };\n }\n return { valid: true };\n}\n\n/**\n * Parse YAML frontmatter from content.\n *\n * @param content - The file content\n * @returns Parsed frontmatter object, or null if parsing fails\n */\nfunction parseFrontmatter(content: string): Record<string, unknown> | null {\n const match = content.match(FRONTMATTER_PATTERN);\n if (!match) {\n return null;\n }\n\n try {\n const parsed = yaml.parse(match[1]);\n return typeof parsed === \"object\" && parsed !== null ? parsed : null;\n } catch {\n return null;\n }\n}\n\n/**\n * Parse YAML frontmatter from a SKILL.md file per Agent Skills spec.\n *\n * @param skillMdPath - Path to the SKILL.md file\n * @param source - Source of the skill ('user' or 'project')\n * @returns SkillMetadata with all fields, or null if parsing fails\n */\nexport function parseSkillMetadata(\n skillMdPath: string,\n source: \"user\" | \"project\",\n): SkillMetadata | null {\n try {\n // Security: Check file size to prevent DoS attacks\n const stats = fs.statSync(skillMdPath);\n if (stats.size > MAX_SKILL_FILE_SIZE) {\n // eslint-disable-next-line no-console\n console.warn(\n `Skipping ${skillMdPath}: file too large (${stats.size} bytes)`,\n );\n return null;\n }\n\n const content = fs.readFileSync(skillMdPath, \"utf-8\");\n const frontmatter = parseFrontmatter(content);\n\n if (!frontmatter) {\n // eslint-disable-next-line no-console\n console.warn(`Skipping ${skillMdPath}: no valid YAML frontmatter found`);\n return null;\n }\n\n // Validate required fields\n const name = frontmatter.name;\n const description = frontmatter.description;\n\n if (!name || !description) {\n // eslint-disable-next-line no-console\n console.warn(\n `Skipping ${skillMdPath}: missing required 'name' or 'description'`,\n );\n return null;\n }\n\n // Validate name format per spec (warn but still load for backwards compatibility)\n const directoryName = path.basename(path.dirname(skillMdPath));\n const validation = validateSkillName(String(name), directoryName);\n if (!validation.valid) {\n // eslint-disable-next-line no-console\n console.warn(\n `Skill '${name}' in ${skillMdPath} does not follow Agent Skills spec: ${validation.error}. ` +\n \"Consider renaming to be spec-compliant.\",\n );\n }\n\n // Truncate description if too long (spec: max 1024 chars)\n let descriptionStr = String(description);\n if (descriptionStr.length > MAX_SKILL_DESCRIPTION_LENGTH) {\n // eslint-disable-next-line no-console\n console.warn(\n `Description exceeds ${MAX_SKILL_DESCRIPTION_LENGTH} chars in ${skillMdPath}, truncating`,\n );\n descriptionStr = descriptionStr.slice(0, MAX_SKILL_DESCRIPTION_LENGTH);\n }\n\n return {\n name: String(name),\n description: descriptionStr,\n path: skillMdPath,\n source,\n license: frontmatter.license ? String(frontmatter.license) : undefined,\n compatibility: frontmatter.compatibility\n ? String(frontmatter.compatibility)\n : undefined,\n metadata:\n frontmatter.metadata && typeof frontmatter.metadata === \"object\"\n ? (frontmatter.metadata as Record<string, string>)\n : undefined,\n allowedTools: frontmatter[\"allowed-tools\"]\n ? String(frontmatter[\"allowed-tools\"])\n : undefined,\n };\n } catch (error) {\n // eslint-disable-next-line no-console\n console.warn(`Error reading ${skillMdPath}: ${error}`);\n return null;\n }\n}\n\n/**\n * List all skills from a single skills directory (internal helper).\n *\n * Scans the skills directory for subdirectories containing SKILL.md files,\n * parses YAML frontmatter, and returns skill metadata.\n *\n * Skills are organized as:\n * ```\n * skills/\n * ├── skill-name/\n * │ ├── SKILL.md # Required: instructions with YAML frontmatter\n * │ ├── script.py # Optional: supporting files\n * │ └── config.json # Optional: supporting files\n * ```\n *\n * @param skillsDir - Path to the skills directory\n * @param source - Source of the skills ('user' or 'project')\n * @returns List of skill metadata\n */\nfunction listSkillsFromDir(\n skillsDir: string,\n source: \"user\" | \"project\",\n): SkillMetadata[] {\n // Check if skills directory exists\n const expandedDir = skillsDir.startsWith(\"~\")\n ? path.join(\n process.env.HOME || process.env.USERPROFILE || \"\",\n skillsDir.slice(1),\n )\n : skillsDir;\n\n if (!fs.existsSync(expandedDir)) {\n return [];\n }\n\n // Resolve base directory to canonical path for security checks\n let resolvedBase: string;\n try {\n resolvedBase = fs.realpathSync(expandedDir);\n } catch {\n // Can't resolve base directory, fail safe\n return [];\n }\n\n const skills: SkillMetadata[] = [];\n\n // Iterate through subdirectories\n let entries: fs.Dirent[];\n try {\n entries = fs.readdirSync(resolvedBase, { withFileTypes: true });\n } catch {\n return [];\n }\n\n for (const entry of entries) {\n const skillDir = path.join(resolvedBase, entry.name);\n\n // Security: Catch symlinks pointing outside the skills directory\n if (!isSafePath(skillDir, resolvedBase)) {\n continue;\n }\n\n if (!entry.isDirectory()) {\n continue;\n }\n\n // Look for SKILL.md file\n const skillMdPath = path.join(skillDir, \"SKILL.md\");\n if (!fs.existsSync(skillMdPath)) {\n continue;\n }\n\n // Security: Validate SKILL.md path is safe before reading\n if (!isSafePath(skillMdPath, resolvedBase)) {\n continue;\n }\n\n // Parse metadata\n const metadata = parseSkillMetadata(skillMdPath, source);\n if (metadata) {\n skills.push(metadata);\n }\n }\n\n return skills;\n}\n\n/**\n * List skills from user and/or project directories.\n *\n * When both directories are provided, project skills with the same name as\n * user skills will override them.\n *\n * @param options - Options specifying which directories to search\n * @returns Merged list of skill metadata from both sources, with project skills\n * taking precedence over user skills when names conflict\n */\nexport function listSkills(options: ListSkillsOptions): SkillMetadata[] {\n const allSkills: Map<string, SkillMetadata> = new Map();\n\n // Load user skills first (foundation)\n if (options.userSkillsDir) {\n const userSkills = listSkillsFromDir(options.userSkillsDir, \"user\");\n for (const skill of userSkills) {\n allSkills.set(skill.name, skill);\n }\n }\n\n // Load project skills second (override/augment)\n if (options.projectSkillsDir) {\n const projectSkills = listSkillsFromDir(\n options.projectSkillsDir,\n \"project\",\n );\n for (const skill of projectSkills) {\n // Project skills override user skills with the same name\n allSkills.set(skill.name, skill);\n }\n }\n\n return Array.from(allSkills.values());\n}\n"],"x_google_ignoreList":[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],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuRA,SAAgB,iBACd,SACmC;AACnC,QACE,OAAQ,QAAmC,YAAY,cACvD,OAAQ,QAAmC,OAAO;;;;;;;;;;;;AC/QtD,M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C,UAAU,MAAM,EAAE,CACnB,GACD;AAEJ,KAAI,CAACD,gBAAG,WAAW,YAAY,CAC7B,QAAO,EAAE;CAIX,IAAI;AACJ,KAAI;AACF,iBAAeA,gBAAG,aAAa,YAAY;SACrC;AAEN,SAAO,EAAE;;CAGX,MAAM,SAA0B,EAAE;CAGlC,IAAI;AACJ,KAAI;AACF,YAAUA,gBAAG,YAAY,cAAc,EAAE,eAAe,MAAM,CAAC;SACzD;AACN,SAAO,EAAE;;AAGX,MAAK,MAAM,SAAS,SAAS;EAC3B,MAAM,WAAWC,kBAAK,KAAK,cAAc,MAAM,KAAK;AAGpD,MAAI,CAAC,WAAW,UAAU,aAAa,CACrC;AAGF,MAAI,CAAC,MAAM,aAAa,CACtB;EAIF,MAAM,cAAcA,kBAAK,KAAK,UAAU,WAAW;AACnD,MAAI,CAACD,gBAAG,WAAW,YAAY,CAC7B;AAIF,MAAI,CAAC,WAAW,aAAa,aAAa,CACxC;EAIF,MAAM,WAAW,mBAAmB,aAAa,OAAO;AACxD,MAAI,SACF,QAAO,KAAK,SAAS;;AAIzB,QAAO;;;;;;;;;;;;AAaT,SAAgB,WAAW,SAA6C;CACtE,MAAM,4BAAwC,IAAI,KAAK;AAGvD,KAAI,QAAQ,eAAe;EACzB,MAAM,aAAa,kBAAkB,QAAQ,eAAe,OAAO;AACnE,OAAK,MAAM,SAAS,WAClB,WAAU,IAAI,MAAM,MAAM,MAAM;;AAKpC,KAAI,QAAQ,kBAAkB;EAC5B,MAAM,gBAAgB,kBACpB,QAAQ,kBACR,UACD;AACD,OAAK,MAAM,SAAS,cAElB,WAAU,IAAI,MAAM,MAAM,MAAM;;AAIpC,QAAO,MAAM,KAAK,UAAU,QAAQ,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["z","StateSchema","ReducedValue","ToolMessage","Command","Command","ToolMessage","HumanMessage","z","SystemMessage","AIMessage","ToolMessage","RemoveMessage","REMOVE_ALL_MESSAGES","z","z","StateSchema","ReducedValue","validateSkillName","z","fsSync","path","fs","SystemMessage","path","fs","os","z","fs","MAX_SKILL_FILE_SIZE","MAX_SKILL_NAME_LENGTH","MAX_SKILL_DESCRIPTION_LENGTH","fs","path"],"sources":["../src/backends/protocol.ts","../src/backends/utils.ts","../src/backends/state.ts","../src/middleware/fs.ts","../src/middleware/subagents.ts","../src/middleware/patch_tool_calls.ts","../src/middleware/memory.ts","../src/middleware/skills.ts","../src/middleware/utils.ts","../src/middleware/summarization.ts","../src/backends/store.ts","../src/backends/filesystem.ts","../src/backends/composite.ts","../src/backends/sandbox.ts","../src/agent.ts","../src/config.ts","../src/middleware/agent-memory.ts","../src/skills/loader.ts"],"sourcesContent":["/**\n * Protocol definition for pluggable memory backends.\n *\n * This module defines the BackendProtocol that all backend implementations\n * must follow. Backends can store files in different locations (state, filesystem,\n * database, etc.) and provide a uniform interface for file operations.\n */\n\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\nexport type MaybePromise<T> = T | Promise<T>;\n\n/**\n * Structured file listing info.\n *\n * Minimal contract used across backends. Only \"path\" is required.\n * Other fields are best-effort and may be absent depending on backend.\n */\nexport interface FileInfo {\n /** File path */\n path: string;\n /** Whether this is a directory */\n is_dir?: boolean;\n /** File size in bytes (approximate) */\n size?: number;\n /** ISO 8601 timestamp of last modification */\n modified_at?: string;\n}\n\n/**\n * Structured grep match entry.\n */\nexport interface GrepMatch {\n /** File path where match was found */\n path: string;\n /** Line number (1-indexed) */\n line: number;\n /** The matching line text */\n text: string;\n}\n\n/**\n * File data structure used by backends.\n *\n * All file data is represented as objects with this structure:\n */\nexport interface FileData {\n /** Lines of text content */\n content: string[];\n /** ISO format timestamp of creation */\n created_at: string;\n /** ISO format timestamp of last modification */\n modified_at: string;\n}\n\n/**\n * Result from backend write operations.\n *\n * Checkpoint backends populate filesUpdate with {file_path: file_data} for LangGraph state.\n * External backends set filesUpdate to null (already persisted to disk/S3/database/etc).\n */\nexport interface WriteResult {\n /** Error message on failure, undefined on success */\n error?: string;\n /** File path of written file, undefined on failure */\n path?: string;\n /**\n * State update dict for checkpoint backends, null for external storage.\n * Checkpoint backends populate this with {file_path: file_data} for LangGraph state.\n * External backends set null (already persisted to disk/S3/database/etc).\n */\n filesUpdate?: Record<string, FileData> | null;\n /** Metadata for the write operation, attached to the ToolMessage */\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Result from backend edit operations.\n *\n * Checkpoint backends populate filesUpdate with {file_path: file_data} for LangGraph state.\n * External backends set filesUpdate to null (already persisted to disk/S3/database/etc).\n */\nexport interface EditResult {\n /** Error message on failure, undefined on success */\n error?: string;\n /** File path of edited file, undefined on failure */\n path?: string;\n /**\n * State update dict for checkpoint backends, null for external storage.\n * Checkpoint backends populate this with {file_path: file_data} for LangGraph state.\n * External backends set null (already persisted to disk/S3/database/etc).\n */\n filesUpdate?: Record<string, FileData> | null;\n /** Number of replacements made, undefined on failure */\n occurrences?: number;\n /** Metadata for the edit operation, attached to the ToolMessage */\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Result of code execution.\n * Simplified schema optimized for LLM consumption.\n */\nexport interface ExecuteResponse {\n /** Combined stdout and stderr output of the executed command */\n output: string;\n /** The process exit code. 0 indicates success, non-zero indicates failure */\n exitCode: number | null;\n /** Whether the output was truncated due to backend limitations */\n truncated: boolean;\n}\n\n/**\n * Standardized error codes for file upload/download operations.\n */\nexport type FileOperationError =\n | \"file_not_found\"\n | \"permission_denied\"\n | \"is_directory\"\n | \"invalid_path\";\n\n/**\n * Result of a single file download operation.\n */\nexport interface FileDownloadResponse {\n /** The file path that was requested */\n path: string;\n /** File contents as Uint8Array on success, null on failure */\n content: Uint8Array | null;\n /** Standardized error code on failure, null on success */\n error: FileOperationError | null;\n}\n\n/**\n * Result of a single file upload operation.\n */\nexport interface FileUploadResponse {\n /** The file path that was requested */\n path: string;\n /** Standardized error code on failure, null on success */\n error: FileOperationError | null;\n}\n\n/**\n * Protocol for pluggable memory backends (single, unified).\n *\n * Backends can store files in different locations (state, filesystem, database, etc.)\n * and provide a uniform interface for file operations.\n *\n * All file data is represented as objects with the FileData structure.\n *\n * Methods can return either direct values or Promises, allowing both\n * synchronous and asynchronous implementations.\n */\nexport interface BackendProtocol {\n /**\n * Structured listing with file metadata.\n *\n * Lists files and directories in the specified directory (non-recursive).\n * Directories have a trailing / in their path and is_dir=true.\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects for files and directories directly in the directory\n */\n lsInfo(path: string): MaybePromise<FileInfo[]>;\n\n /**\n * Read file content with line numbers or an error string.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed), default 0\n * @param limit - Maximum number of lines to read, default 500\n * @returns Formatted file content with line numbers, or error message\n */\n read(filePath: string, offset?: number, limit?: number): MaybePromise<string>;\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n readRaw(filePath: string): MaybePromise<FileData>;\n\n /**\n * Structured search results or error string for invalid input.\n *\n * Searches file contents for a regex pattern.\n *\n * @param pattern - Regex pattern to search for\n * @param path - Base path to search from (default: null)\n * @param glob - Optional glob pattern to filter files (e.g., \"*.py\")\n * @returns List of GrepMatch objects or error string for invalid regex\n */\n grepRaw(\n pattern: string,\n path?: string | null,\n glob?: string | null,\n ): MaybePromise<GrepMatch[] | string>;\n\n /**\n * Structured glob matching returning FileInfo objects.\n *\n * @param pattern - Glob pattern (e.g., `*.py`, `**\\/*.ts`)\n * @param path - Base path to search from (default: \"/\")\n * @returns List of FileInfo objects matching the pattern\n */\n globInfo(pattern: string, path?: string): MaybePromise<FileInfo[]>;\n\n /**\n * Create a new file.\n *\n * @param filePath - Absolute file path\n * @param content - File content as string\n * @returns WriteResult with error populated on failure\n */\n write(filePath: string, content: string): MaybePromise<WriteResult>;\n\n /**\n * Edit a file by replacing string occurrences.\n *\n * @param filePath - Absolute file path\n * @param oldString - String to find and replace\n * @param newString - Replacement string\n * @param replaceAll - If true, replace all occurrences (default: false)\n * @returns EditResult with error, path, filesUpdate, and occurrences\n */\n edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll?: boolean,\n ): MaybePromise<EditResult>;\n\n /**\n * Upload multiple files.\n * Optional - backends that don't support file upload can omit this.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n uploadFiles?(\n files: Array<[string, Uint8Array]>,\n ): MaybePromise<FileUploadResponse[]>;\n\n /**\n * Download multiple files.\n * Optional - backends that don't support file download can omit this.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n downloadFiles?(paths: string[]): MaybePromise<FileDownloadResponse[]>;\n}\n\n/**\n * Protocol for sandboxed backends with isolated runtime.\n * Sandboxed backends run in isolated environments (e.g., containers)\n * and communicate via defined interfaces.\n */\nexport interface SandboxBackendProtocol extends BackendProtocol {\n /**\n * Execute a command in the sandbox.\n *\n * @param command - Full shell command string to execute\n * @returns ExecuteResponse with combined output, exit code, and truncation flag\n */\n execute(command: string): MaybePromise<ExecuteResponse>;\n\n /** Unique identifier for the sandbox backend instance */\n readonly id: string;\n}\n\n/**\n * Type guard to check if a backend supports execution.\n *\n * @param backend - Backend instance to check\n * @returns True if the backend implements SandboxBackendProtocol\n */\nexport function isSandboxBackend(\n backend: BackendProtocol,\n): backend is SandboxBackendProtocol {\n return (\n typeof (backend as SandboxBackendProtocol).execute === \"function\" &&\n typeof (backend as SandboxBackendProtocol).id === \"string\"\n );\n}\n\n/**\n * State and store container for backend initialization.\n *\n * This provides a clean interface for what backends need to access:\n * - state: Current agent state (with files, messages, etc.)\n * - store: Optional persistent store for cross-conversation data\n *\n * Different contexts build this differently:\n * - Tools: Extract state via getCurrentTaskInput(config)\n * - Middleware: Use request.state directly\n */\nexport interface StateAndStore {\n /** Current agent state with files, messages, etc. */\n state: unknown;\n /** Optional BaseStore for persistent cross-conversation storage */\n store?: BaseStore;\n /** Optional assistant ID for per-assistant isolation in store */\n assistantId?: string;\n}\n\n/**\n * Factory function type for creating backend instances.\n *\n * Backends receive StateAndStore which contains the current state\n * and optional store, extracted from the execution context.\n *\n * @example\n * ```typescript\n * // Using in middleware\n * const middleware = createFilesystemMiddleware({\n * backend: (stateAndStore) => new StateBackend(stateAndStore)\n * });\n * ```\n */\nexport type BackendFactory = (stateAndStore: StateAndStore) => BackendProtocol;\n","/**\n * Shared utility functions for memory backend implementations.\n *\n * This module contains both user-facing string formatters and structured\n * helpers used by backends and the composite router. Structured helpers\n * enable composition without fragile string parsing.\n */\n\nimport micromatch from \"micromatch\";\nimport { basename } from \"path\";\nimport type { FileData, GrepMatch } from \"./protocol.js\";\n\n// Constants\nexport const EMPTY_CONTENT_WARNING =\n \"System reminder: File exists but has empty contents\";\nexport const MAX_LINE_LENGTH = 10000;\nexport const LINE_NUMBER_WIDTH = 6;\nexport const TOOL_RESULT_TOKEN_LIMIT = 20000; // Same threshold as eviction\nexport const TRUNCATION_GUIDANCE =\n \"... [results truncated, try being more specific with your parameters]\";\n\n/**\n * Sanitize tool_call_id to prevent path traversal and separator issues.\n *\n * Replaces dangerous characters (., /, \\) with underscores.\n */\nexport function sanitizeToolCallId(toolCallId: string): string {\n return toolCallId.replace(/\\./g, \"_\").replace(/\\//g, \"_\").replace(/\\\\/g, \"_\");\n}\n\n/**\n * Format file content with line numbers (cat -n style).\n *\n * Chunks lines longer than MAX_LINE_LENGTH with continuation markers (e.g., 5.1, 5.2).\n *\n * @param content - File content as string or list of lines\n * @param startLine - Starting line number (default: 1)\n * @returns Formatted content with line numbers and continuation markers\n */\nexport function formatContentWithLineNumbers(\n content: string | string[],\n startLine: number = 1,\n): string {\n let lines: string[];\n if (typeof content === \"string\") {\n lines = content.split(\"\\n\");\n if (lines.length > 0 && lines[lines.length - 1] === \"\") {\n lines = lines.slice(0, -1);\n }\n } else {\n lines = content;\n }\n\n const resultLines: string[] = [];\n for (let i = 0; i < lines.length; i++) {\n const line = lines[i];\n const lineNum = i + startLine;\n\n if (line.length <= MAX_LINE_LENGTH) {\n resultLines.push(\n `${lineNum.toString().padStart(LINE_NUMBER_WIDTH)}\\t${line}`,\n );\n } else {\n // Split long line into chunks with continuation markers\n const numChunks = Math.ceil(line.length / MAX_LINE_LENGTH);\n for (let chunkIdx = 0; chunkIdx < numChunks; chunkIdx++) {\n const start = chunkIdx * MAX_LINE_LENGTH;\n const end = Math.min(start + MAX_LINE_LENGTH, line.length);\n const chunk = line.substring(start, end);\n if (chunkIdx === 0) {\n // First chunk: use normal line number\n resultLines.push(\n `${lineNum.toString().padStart(LINE_NUMBER_WIDTH)}\\t${chunk}`,\n );\n } else {\n // Continuation chunks: use decimal notation (e.g., 5.1, 5.2)\n const continuationMarker = `${lineNum}.${chunkIdx}`;\n resultLines.push(\n `${continuationMarker.padStart(LINE_NUMBER_WIDTH)}\\t${chunk}`,\n );\n }\n }\n }\n }\n\n return resultLines.join(\"\\n\");\n}\n\n/**\n * Check if content is empty and return warning message.\n *\n * @param content - Content to check\n * @returns Warning message if empty, null otherwise\n */\nexport function checkEmptyContent(content: string): string | null {\n if (!content || content.trim() === \"\") {\n return EMPTY_CONTENT_WARNING;\n }\n return null;\n}\n\n/**\n * Convert FileData to plain string content.\n *\n * @param fileData - FileData object with 'content' key\n * @returns Content as string with lines joined by newlines\n */\nexport function fileDataToString(fileData: FileData): string {\n return fileData.content.join(\"\\n\");\n}\n\n/**\n * Create a FileData object with timestamps.\n *\n * @param content - File content as string\n * @param createdAt - Optional creation timestamp (ISO format)\n * @returns FileData object with content and timestamps\n */\nexport function createFileData(content: string, createdAt?: string): FileData {\n const lines = typeof content === \"string\" ? content.split(\"\\n\") : content;\n const now = new Date().toISOString();\n\n return {\n content: lines,\n created_at: createdAt || now,\n modified_at: now,\n };\n}\n\n/**\n * Update FileData with new content, preserving creation timestamp.\n *\n * @param fileData - Existing FileData object\n * @param content - New content as string\n * @returns Updated FileData object\n */\nexport function updateFileData(fileData: FileData, content: string): FileData {\n const lines = typeof content === \"string\" ? content.split(\"\\n\") : content;\n const now = new Date().toISOString();\n\n return {\n content: lines,\n created_at: fileData.created_at,\n modified_at: now,\n };\n}\n\n/**\n * Format file data for read response with line numbers.\n *\n * @param fileData - FileData object\n * @param offset - Line offset (0-indexed)\n * @param limit - Maximum number of lines\n * @returns Formatted content or error message\n */\nexport function formatReadResponse(\n fileData: FileData,\n offset: number,\n limit: number,\n): string {\n const content = fileDataToString(fileData);\n const emptyMsg = checkEmptyContent(content);\n if (emptyMsg) {\n return emptyMsg;\n }\n\n const lines = content.split(\"\\n\");\n const startIdx = offset;\n const endIdx = Math.min(startIdx + limit, lines.length);\n\n if (startIdx >= lines.length) {\n return `Error: Line offset ${offset} exceeds file length (${lines.length} lines)`;\n }\n\n const selectedLines = lines.slice(startIdx, endIdx);\n return formatContentWithLineNumbers(selectedLines, startIdx + 1);\n}\n\n/**\n * Perform string replacement with occurrence validation.\n *\n * @param content - Original content\n * @param oldString - String to replace\n * @param newString - Replacement string\n * @param replaceAll - Whether to replace all occurrences\n * @returns Tuple of [new_content, occurrences] on success, or error message string\n */\nexport function performStringReplacement(\n content: string,\n oldString: string,\n newString: string,\n replaceAll: boolean,\n): [string, number] | string {\n // Use split to count occurrences (simpler than regex)\n const occurrences = content.split(oldString).length - 1;\n\n if (occurrences === 0) {\n return `Error: String not found in file: '${oldString}'`;\n }\n\n if (occurrences > 1 && !replaceAll) {\n return `Error: String '${oldString}' appears ${occurrences} times in file. Use replace_all=True to replace all instances, or provide a more specific string with surrounding context.`;\n }\n\n // Python's str.replace() replaces ALL occurrences\n // Use split/join for consistent behavior\n const newContent = content.split(oldString).join(newString);\n\n return [newContent, occurrences];\n}\n\n/**\n * Truncate list or string result if it exceeds token limit (rough estimate: 4 chars/token).\n */\nexport function truncateIfTooLong(\n result: string[] | string,\n): string[] | string {\n if (Array.isArray(result)) {\n const totalChars = result.reduce((sum, item) => sum + item.length, 0);\n if (totalChars > TOOL_RESULT_TOKEN_LIMIT * 4) {\n const truncateAt = Math.floor(\n (result.length * TOOL_RESULT_TOKEN_LIMIT * 4) / totalChars,\n );\n return [...result.slice(0, truncateAt), TRUNCATION_GUIDANCE];\n }\n return result;\n }\n // string\n if (result.length > TOOL_RESULT_TOKEN_LIMIT * 4) {\n return (\n result.substring(0, TOOL_RESULT_TOKEN_LIMIT * 4) +\n \"\\n\" +\n TRUNCATION_GUIDANCE\n );\n }\n return result;\n}\n\n/**\n * Validate and normalize a directory path.\n *\n * Ensures paths are safe to use by preventing directory traversal attacks\n * and enforcing consistent formatting. All paths are normalized to use\n * forward slashes and start with a leading slash.\n *\n * This function is designed for virtual filesystem paths and rejects\n * Windows absolute paths (e.g., C:/..., F:/...) to maintain consistency\n * and prevent path format ambiguity.\n *\n * @param path - Path to validate\n * @returns Normalized path starting with / and ending with /\n * @throws Error if path is invalid\n *\n * @example\n * ```typescript\n * validatePath(\"foo/bar\") // Returns: \"/foo/bar/\"\n * validatePath(\"/./foo//bar\") // Returns: \"/foo/bar/\"\n * validatePath(\"../etc/passwd\") // Throws: Path traversal not allowed\n * validatePath(\"C:\\\\Users\\\\file\") // Throws: Windows absolute paths not supported\n * ```\n */\nexport function validatePath(path: string | null | undefined): string {\n const pathStr = path || \"/\";\n if (!pathStr || pathStr.trim() === \"\") {\n throw new Error(\"Path cannot be empty\");\n }\n\n let normalized = pathStr.startsWith(\"/\") ? pathStr : \"/\" + pathStr;\n\n if (!normalized.endsWith(\"/\")) {\n normalized += \"/\";\n }\n\n return normalized;\n}\n\n/**\n * Validate and normalize a file path for security.\n *\n * Ensures paths are safe to use by preventing directory traversal attacks\n * and enforcing consistent formatting. All paths are normalized to use\n * forward slashes and start with a leading slash.\n *\n * This function is designed for virtual filesystem paths and rejects\n * Windows absolute paths (e.g., C:/..., F:/...) to maintain consistency\n * and prevent path format ambiguity.\n *\n * @param path - The path to validate and normalize.\n * @param allowedPrefixes - Optional list of allowed path prefixes. If provided,\n * the normalized path must start with one of these prefixes.\n * @returns Normalized canonical path starting with `/` and using forward slashes.\n * @throws Error if path contains traversal sequences (`..` or `~`), is a Windows\n * absolute path (e.g., C:/...), or does not start with an allowed prefix\n * when `allowedPrefixes` is specified.\n *\n * @example\n * ```typescript\n * validateFilePath(\"foo/bar\") // Returns: \"/foo/bar\"\n * validateFilePath(\"/./foo//bar\") // Returns: \"/foo/bar\"\n * validateFilePath(\"../etc/passwd\") // Throws: Path traversal not allowed\n * validateFilePath(\"C:\\\\Users\\\\file.txt\") // Throws: Windows absolute paths not supported\n * validateFilePath(\"/data/file.txt\", [\"/data/\"]) // Returns: \"/data/file.txt\"\n * validateFilePath(\"/etc/file.txt\", [\"/data/\"]) // Throws: Path must start with...\n * ```\n */\nexport function validateFilePath(\n path: string,\n allowedPrefixes?: string[],\n): string {\n // Check for path traversal\n if (path.includes(\"..\") || path.startsWith(\"~\")) {\n throw new Error(`Path traversal not allowed: ${path}`);\n }\n\n // Reject Windows absolute paths (e.g., C:\\..., D:/...)\n // This maintains consistency in virtual filesystem paths\n if (/^[a-zA-Z]:/.test(path)) {\n throw new Error(\n `Windows absolute paths are not supported: ${path}. Please use virtual paths starting with / (e.g., /workspace/file.txt)`,\n );\n }\n\n // Normalize path separators and remove redundant slashes\n let normalized = path.replace(/\\\\/g, \"/\");\n\n // Remove redundant path components (./foo becomes foo, foo//bar becomes foo/bar)\n const parts: string[] = [];\n for (const part of normalized.split(\"/\")) {\n if (part === \".\" || part === \"\") {\n continue;\n }\n parts.push(part);\n }\n normalized = \"/\" + parts.join(\"/\");\n\n // Check allowed prefixes if provided\n if (\n allowedPrefixes &&\n !allowedPrefixes.some((prefix) => normalized.startsWith(prefix))\n ) {\n throw new Error(\n `Path must start with one of ${JSON.stringify(allowedPrefixes)}: ${path}`,\n );\n }\n\n return normalized;\n}\n\n/**\n * Search files dict for paths matching glob pattern.\n *\n * @param files - Dictionary of file paths to FileData\n * @param pattern - Glob pattern (e.g., `*.py`, `**\\/*.ts`)\n * @param path - Base path to search from\n * @returns Newline-separated file paths, sorted by modification time (most recent first).\n * Returns \"No files found\" if no matches.\n *\n * @example\n * ```typescript\n * const files = {\"/src/main.py\": FileData(...), \"/test.py\": FileData(...)};\n * globSearchFiles(files, \"*.py\", \"/\");\n * // Returns: \"/test.py\\n/src/main.py\" (sorted by modified_at)\n * ```\n */\nexport function globSearchFiles(\n files: Record<string, FileData>,\n pattern: string,\n path: string = \"/\",\n): string {\n let normalizedPath: string;\n try {\n normalizedPath = validatePath(path);\n } catch {\n return \"No files found\";\n }\n\n const filtered = Object.fromEntries(\n Object.entries(files).filter(([fp]) => fp.startsWith(normalizedPath)),\n );\n\n // Respect standard glob semantics:\n // - Patterns without path separators (e.g., \"*.py\") match only in the current\n // directory (non-recursive) relative to `path`.\n // - Use \"**\" explicitly for recursive matching.\n const effectivePattern = pattern;\n\n const matches: Array<[string, string]> = [];\n for (const [filePath, fileData] of Object.entries(filtered)) {\n let relative = filePath.substring(normalizedPath.length);\n if (relative.startsWith(\"/\")) {\n relative = relative.substring(1);\n }\n if (!relative) {\n const parts = filePath.split(\"/\");\n relative = parts[parts.length - 1] || \"\";\n }\n\n if (\n micromatch.isMatch(relative, effectivePattern, {\n dot: true,\n nobrace: false,\n })\n ) {\n matches.push([filePath, fileData.modified_at]);\n }\n }\n\n matches.sort((a, b) => b[1].localeCompare(a[1])); // Sort by modified_at descending\n\n if (matches.length === 0) {\n return \"No files found\";\n }\n\n return matches.map(([fp]) => fp).join(\"\\n\");\n}\n\n/**\n * Format grep search results based on output mode.\n *\n * @param results - Dictionary mapping file paths to list of [line_num, line_content] tuples\n * @param outputMode - Output format - \"files_with_matches\", \"content\", or \"count\"\n * @returns Formatted string output\n */\nexport function formatGrepResults(\n results: Record<string, Array<[number, string]>>,\n outputMode: \"files_with_matches\" | \"content\" | \"count\",\n): string {\n if (outputMode === \"files_with_matches\") {\n return Object.keys(results).sort().join(\"\\n\");\n }\n if (outputMode === \"count\") {\n const lines: string[] = [];\n for (const filePath of Object.keys(results).sort()) {\n const count = results[filePath].length;\n lines.push(`${filePath}: ${count}`);\n }\n return lines.join(\"\\n\");\n }\n // content mode\n const lines: string[] = [];\n for (const filePath of Object.keys(results).sort()) {\n lines.push(`${filePath}:`);\n for (const [lineNum, line] of results[filePath]) {\n lines.push(` ${lineNum}: ${line}`);\n }\n }\n return lines.join(\"\\n\");\n}\n\n/**\n * Search file contents for regex pattern.\n *\n * @param files - Dictionary of file paths to FileData\n * @param pattern - Regex pattern to search for\n * @param path - Base path to search from\n * @param glob - Optional glob pattern to filter files (e.g., \"*.py\")\n * @param outputMode - Output format - \"files_with_matches\", \"content\", or \"count\"\n * @returns Formatted search results. Returns \"No matches found\" if no results.\n *\n * @example\n * ```typescript\n * const files = {\"/file.py\": FileData({content: [\"import os\", \"print('hi')\"], ...})};\n * grepSearchFiles(files, \"import\", \"/\");\n * // Returns: \"/file.py\" (with output_mode=\"files_with_matches\")\n * ```\n */\nexport function grepSearchFiles(\n files: Record<string, FileData>,\n pattern: string,\n path: string | null = null,\n glob: string | null = null,\n outputMode: \"files_with_matches\" | \"content\" | \"count\" = \"files_with_matches\",\n): string {\n let regex: RegExp;\n try {\n regex = new RegExp(pattern);\n } catch (e: any) {\n return `Invalid regex pattern: ${e.message}`;\n }\n\n let normalizedPath: string;\n try {\n normalizedPath = validatePath(path);\n } catch {\n return \"No matches found\";\n }\n\n let filtered = Object.fromEntries(\n Object.entries(files).filter(([fp]) => fp.startsWith(normalizedPath)),\n );\n\n if (glob) {\n filtered = Object.fromEntries(\n Object.entries(filtered).filter(([fp]) =>\n micromatch.isMatch(basename(fp), glob, { dot: true, nobrace: false }),\n ),\n );\n }\n\n const results: Record<string, Array<[number, string]>> = {};\n for (const [filePath, fileData] of Object.entries(filtered)) {\n for (let i = 0; i < fileData.content.length; i++) {\n const line = fileData.content[i];\n const lineNum = i + 1;\n if (regex.test(line)) {\n if (!results[filePath]) {\n results[filePath] = [];\n }\n results[filePath].push([lineNum, line]);\n }\n }\n }\n\n if (Object.keys(results).length === 0) {\n return \"No matches found\";\n }\n return formatGrepResults(results, outputMode);\n}\n\n// -------- Structured helpers for composition --------\n\n/**\n * Return structured grep matches from an in-memory files mapping.\n *\n * Returns a list of GrepMatch on success, or a string for invalid inputs\n * (e.g., invalid regex). We deliberately do not raise here to keep backends\n * non-throwing in tool contexts and preserve user-facing error messages.\n */\nexport function grepMatchesFromFiles(\n files: Record<string, FileData>,\n pattern: string,\n path: string | null = null,\n glob: string | null = null,\n): GrepMatch[] | string {\n let regex: RegExp;\n try {\n regex = new RegExp(pattern);\n } catch (e: any) {\n return `Invalid regex pattern: ${e.message}`;\n }\n\n let normalizedPath: string;\n try {\n normalizedPath = validatePath(path);\n } catch {\n return [];\n }\n\n let filtered = Object.fromEntries(\n Object.entries(files).filter(([fp]) => fp.startsWith(normalizedPath)),\n );\n\n if (glob) {\n filtered = Object.fromEntries(\n Object.entries(filtered).filter(([fp]) =>\n micromatch.isMatch(basename(fp), glob, { dot: true, nobrace: false }),\n ),\n );\n }\n\n const matches: GrepMatch[] = [];\n for (const [filePath, fileData] of Object.entries(filtered)) {\n for (let i = 0; i < fileData.content.length; i++) {\n const line = fileData.content[i];\n const lineNum = i + 1;\n if (regex.test(line)) {\n matches.push({ path: filePath, line: lineNum, text: line });\n }\n }\n }\n\n return matches;\n}\n\n/**\n * Group structured matches into the legacy dict form used by formatters.\n */\nexport function buildGrepResultsDict(\n matches: GrepMatch[],\n): Record<string, Array<[number, string]>> {\n const grouped: Record<string, Array<[number, string]>> = {};\n for (const m of matches) {\n if (!grouped[m.path]) {\n grouped[m.path] = [];\n }\n grouped[m.path].push([m.line, m.text]);\n }\n return grouped;\n}\n\n/**\n * Format structured grep matches using existing formatting logic.\n */\nexport function formatGrepMatches(\n matches: GrepMatch[],\n outputMode: \"files_with_matches\" | \"content\" | \"count\",\n): string {\n if (matches.length === 0) {\n return \"No matches found\";\n }\n return formatGrepResults(buildGrepResultsDict(matches), outputMode);\n}\n","/**\n * StateBackend: Store files in LangGraph agent state (ephemeral).\n */\n\nimport type {\n BackendProtocol,\n EditResult,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n StateAndStore,\n WriteResult,\n} from \"./protocol.js\";\nimport {\n createFileData,\n fileDataToString,\n formatReadResponse,\n globSearchFiles,\n grepMatchesFromFiles,\n performStringReplacement,\n updateFileData,\n} from \"./utils.js\";\n\n/**\n * Backend that stores files in agent state (ephemeral).\n *\n * Uses LangGraph's state management and checkpointing. Files persist within\n * a conversation thread but not across threads. State is automatically\n * checkpointed after each agent step.\n *\n * Special handling: Since LangGraph state must be updated via Command objects\n * (not direct mutation), operations return filesUpdate in WriteResult/EditResult\n * for the middleware to apply via Command.\n */\nexport class StateBackend implements BackendProtocol {\n private stateAndStore: StateAndStore;\n\n constructor(stateAndStore: StateAndStore) {\n this.stateAndStore = stateAndStore;\n }\n\n /**\n * Get files from current state.\n */\n private getFiles(): Record<string, FileData> {\n return (\n ((this.stateAndStore.state as any).files as Record<string, FileData>) ||\n {}\n );\n }\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects for files and directories directly in the directory.\n * Directories have a trailing / in their path and is_dir=true.\n */\n lsInfo(path: string): FileInfo[] {\n const files = this.getFiles();\n const infos: FileInfo[] = [];\n const subdirs = new Set<string>();\n\n // Normalize path to have trailing slash for proper prefix matching\n const normalizedPath = path.endsWith(\"/\") ? path : path + \"/\";\n\n for (const [k, fd] of Object.entries(files)) {\n // Check if file is in the specified directory or a subdirectory\n if (!k.startsWith(normalizedPath)) {\n continue;\n }\n\n // Get the relative path after the directory\n const relative = k.substring(normalizedPath.length);\n\n // If relative path contains '/', it's in a subdirectory\n if (relative.includes(\"/\")) {\n // Extract the immediate subdirectory name\n const subdirName = relative.split(\"/\")[0];\n subdirs.add(normalizedPath + subdirName + \"/\");\n continue;\n }\n\n // This is a file directly in the current directory\n const size = fd.content.join(\"\\n\").length;\n infos.push({\n path: k,\n is_dir: false,\n size: size,\n modified_at: fd.modified_at,\n });\n }\n\n // Add directories to the results\n for (const subdir of Array.from(subdirs).sort()) {\n infos.push({\n path: subdir,\n is_dir: true,\n size: 0,\n modified_at: \"\",\n });\n }\n\n infos.sort((a, b) => a.path.localeCompare(b.path));\n return infos;\n }\n\n /**\n * Read file content with line numbers.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n read(filePath: string, offset: number = 0, limit: number = 500): string {\n const files = this.getFiles();\n const fileData = files[filePath];\n\n if (!fileData) {\n return `Error: File '${filePath}' not found`;\n }\n\n return formatReadResponse(fileData, offset, limit);\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n readRaw(filePath: string): FileData {\n const files = this.getFiles();\n const fileData = files[filePath];\n\n if (!fileData) throw new Error(`File '${filePath}' not found`);\n return fileData;\n }\n\n /**\n * Create a new file with content.\n * Returns WriteResult with filesUpdate to update LangGraph state.\n */\n write(filePath: string, content: string): WriteResult {\n const files = this.getFiles();\n\n if (filePath in files) {\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n }\n\n const newFileData = createFileData(content);\n return {\n path: filePath,\n filesUpdate: { [filePath]: newFileData },\n };\n }\n\n /**\n * Edit a file by replacing string occurrences.\n * Returns EditResult with filesUpdate and occurrences.\n */\n edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): EditResult {\n const files = this.getFiles();\n const fileData = files[filePath];\n\n if (!fileData) {\n return { error: `Error: File '${filePath}' not found` };\n }\n\n const content = fileDataToString(fileData);\n const result = performStringReplacement(\n content,\n oldString,\n newString,\n replaceAll,\n );\n\n if (typeof result === \"string\") {\n return { error: result };\n }\n\n const [newContent, occurrences] = result;\n const newFileData = updateFileData(fileData, newContent);\n return {\n path: filePath,\n filesUpdate: { [filePath]: newFileData },\n occurrences: occurrences,\n };\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n grepRaw(\n pattern: string,\n path: string = \"/\",\n glob: string | null = null,\n ): GrepMatch[] | string {\n const files = this.getFiles();\n return grepMatchesFromFiles(files, pattern, path, glob);\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n globInfo(pattern: string, path: string = \"/\"): FileInfo[] {\n const files = this.getFiles();\n const result = globSearchFiles(files, pattern, path);\n\n if (result === \"No files found\") {\n return [];\n }\n\n const paths = result.split(\"\\n\");\n const infos: FileInfo[] = [];\n for (const p of paths) {\n const fd = files[p];\n const size = fd ? fd.content.join(\"\\n\").length : 0;\n infos.push({\n path: p,\n is_dir: false,\n size: size,\n modified_at: fd?.modified_at || \"\",\n });\n }\n return infos;\n }\n\n /**\n * Upload multiple files.\n *\n * Note: Since LangGraph state must be updated via Command objects,\n * the caller must apply filesUpdate via Command after calling this method.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): FileUploadResponse[] & { filesUpdate?: Record<string, FileData> } {\n const responses: FileUploadResponse[] = [];\n const updates: Record<string, FileData> = {};\n\n for (const [path, content] of files) {\n try {\n const contentStr = new TextDecoder().decode(content);\n const fileData = createFileData(contentStr);\n updates[path] = fileData;\n responses.push({ path, error: null });\n } catch {\n responses.push({ path, error: \"invalid_path\" });\n }\n }\n\n // Attach filesUpdate for the caller to apply via Command\n const result = responses as FileUploadResponse[] & {\n filesUpdate?: Record<string, FileData>;\n };\n result.filesUpdate = updates;\n return result;\n }\n\n /**\n * Download multiple files.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n downloadFiles(paths: string[]): FileDownloadResponse[] {\n const files = this.getFiles();\n const responses: FileDownloadResponse[] = [];\n\n for (const path of paths) {\n const fileData = files[path];\n if (!fileData) {\n responses.push({ path, content: null, error: \"file_not_found\" });\n continue;\n }\n\n const contentStr = fileDataToString(fileData);\n const content = new TextEncoder().encode(contentStr);\n responses.push({ path, content, error: null });\n }\n\n return responses;\n }\n}\n","/**\n * Middleware for providing filesystem tools to an agent.\n *\n * Provides ls, read_file, write_file, edit_file, glob, and grep tools with support for:\n * - Pluggable backends (StateBackend, StoreBackend, FilesystemBackend, CompositeBackend)\n * - Tool result eviction for large outputs\n */\n\nimport {\n createMiddleware,\n tool,\n ToolMessage,\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport {\n Command,\n isCommand,\n getCurrentTaskInput,\n StateSchema,\n ReducedValue,\n} from \"@langchain/langgraph\";\nimport { z } from \"zod/v4\";\nimport type {\n BackendProtocol,\n BackendFactory,\n FileData,\n StateAndStore,\n} from \"../backends/protocol.js\";\nimport { isSandboxBackend } from \"../backends/protocol.js\";\nimport { StateBackend } from \"../backends/state.js\";\nimport {\n sanitizeToolCallId,\n formatContentWithLineNumbers,\n} from \"../backends/utils.js\";\n\n/**\n * Tools that should be excluded from the large result eviction logic.\n *\n * This array contains tools that should NOT have their results evicted to the filesystem\n * when they exceed token limits. Tools are excluded for different reasons:\n *\n * 1. Tools with built-in truncation (ls, glob, grep):\n * These tools truncate their own output when it becomes too large. When these tools\n * produce truncated output due to many matches, it typically indicates the query\n * needs refinement rather than full result preservation. In such cases, the truncated\n * matches are potentially more like noise and the LLM should be prompted to narrow\n * its search criteria instead.\n *\n * 2. Tools with problematic truncation behavior (read_file):\n * read_file is tricky to handle as the failure mode here is single long lines\n * (e.g., imagine a jsonl file with very long payloads on each line). If we try to\n * truncate the result of read_file, the agent may then attempt to re-read the\n * truncated file using read_file again, which won't help.\n *\n * 3. Tools that never exceed limits (edit_file, write_file):\n * These tools return minimal confirmation messages and are never expected to produce\n * output large enough to exceed token limits, so checking them would be unnecessary.\n */\nexport const TOOLS_EXCLUDED_FROM_EVICTION = [\n \"ls\",\n \"glob\",\n \"grep\",\n \"read_file\",\n \"edit_file\",\n \"write_file\",\n] as const;\n\n/**\n * Approximate number of characters per token for truncation calculations.\n * Using 4 chars per token as a conservative approximation (actual ratio varies by content)\n * This errs on the high side to avoid premature eviction of content that might fit.\n */\nexport const NUM_CHARS_PER_TOKEN = 4;\n\n/**\n * Message template for evicted tool results.\n */\nconst TOO_LARGE_TOOL_MSG = `Tool result too large, the result of this tool call {tool_call_id} was saved in the filesystem at this path: {file_path}\nYou can read the result from the filesystem by using the read_file tool, but make sure to only read part of the result at a time.\nYou can do this by specifying an offset and limit in the read_file tool call.\nFor example, to read the first 100 lines, you can use the read_file tool with offset=0 and limit=100.\n\nHere is a preview showing the head and tail of the result (lines of the form\n... [N lines truncated] ...\nindicate omitted lines in the middle of the content):\n\n{content_sample}`;\n\n/**\n * Create a preview of content showing head and tail with truncation marker.\n *\n * @param contentStr - The full content string to preview.\n * @param headLines - Number of lines to show from the start (default: 5).\n * @param tailLines - Number of lines to show from the end (default: 5).\n * @returns Formatted preview string with line numbers.\n */\nexport function createContentPreview(\n contentStr: string,\n headLines: number = 5,\n tailLines: number = 5,\n): string {\n const lines = contentStr.split(\"\\n\");\n\n if (lines.length <= headLines + tailLines) {\n // If file is small enough, show all lines\n const previewLines = lines.map((line) => line.substring(0, 1000));\n return formatContentWithLineNumbers(previewLines, 1);\n }\n\n // Show head and tail with truncation marker\n const head = lines.slice(0, headLines).map((line) => line.substring(0, 1000));\n const tail = lines.slice(-tailLines).map((line) => line.substring(0, 1000));\n\n const headSample = formatContentWithLineNumbers(head, 1);\n const truncationNotice = `\\n... [${lines.length - headLines - tailLines} lines truncated] ...\\n`;\n const tailSample = formatContentWithLineNumbers(\n tail,\n lines.length - tailLines + 1,\n );\n\n return headSample + truncationNotice + tailSample;\n}\n\n/**\n * required for type inference\n */\nimport type * as _zodTypes from \"@langchain/core/utils/types\";\nimport type * as _zodMeta from \"@langchain/langgraph/zod\";\nimport type * as _messages from \"@langchain/core/messages\";\n\n/**\n * Zod v3 schema for FileData (re-export from backends)\n */\nconst FileDataSchema = z.object({\n content: z.array(z.string()),\n created_at: z.string(),\n modified_at: z.string(),\n});\n\nexport type { FileData };\n\n/**\n * Type for the files state record.\n */\nexport type FilesRecord = Record<string, FileData>;\n\n/**\n * Type for file updates, where null indicates deletion.\n */\nexport type FilesRecordUpdate = Record<string, FileData | null>;\n\n/**\n * Reducer for files state that merges file updates with support for deletions.\n * When a file value is null, the file is deleted from state.\n * When a file value is non-null, it is added or updated in state.\n *\n * This reducer enables concurrent updates from parallel subagents by properly\n * merging their file changes instead of requiring LastValue semantics.\n *\n * @param current - The current files record (from state)\n * @param update - The new files record (from a subagent update), with null values for deletions\n * @returns Merged files record with deletions applied\n */\nexport function fileDataReducer(\n current: FilesRecord | undefined,\n update: FilesRecordUpdate | undefined,\n): FilesRecord {\n // If no update, return current (or empty object)\n if (update === undefined) {\n return current || {};\n }\n\n // If no current, filter out null values from update\n if (current === undefined) {\n const result: FilesRecord = {};\n for (const [key, value] of Object.entries(update)) {\n if (value !== null) {\n result[key] = value;\n }\n }\n return result;\n }\n\n // Merge: apply updates and deletions\n const result = { ...current };\n for (const [key, value] of Object.entries(update)) {\n if (value === null) {\n delete result[key];\n } else {\n result[key] = value;\n }\n }\n return result;\n}\n\n/**\n * Shared filesystem state schema.\n * Defined at module level to ensure the same object identity is used across all agents,\n * preventing \"Channel already exists with different type\" errors when multiple agents\n * use createFilesystemMiddleware.\n *\n * Uses ReducedValue for files to allow concurrent updates from parallel subagents.\n */\nconst FilesystemStateSchema = new StateSchema({\n files: new ReducedValue(\n z.record(z.string(), FileDataSchema).default(() => ({})),\n {\n inputSchema: z.record(z.string(), FileDataSchema.nullable()).optional(),\n reducer: fileDataReducer,\n },\n ),\n});\n\n/**\n * Resolve backend from factory or instance.\n *\n * @param backend - Backend instance or factory function\n * @param stateAndStore - State and store container for backend initialization\n */\nfunction getBackend(\n backend: BackendProtocol | BackendFactory,\n stateAndStore: StateAndStore,\n): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend(stateAndStore);\n }\n return backend;\n}\n\n// System prompts\nconst FILESYSTEM_SYSTEM_PROMPT = `## Filesystem Tools \\`ls\\`, \\`read_file\\`, \\`write_file\\`, \\`edit_file\\`, \\`glob\\`, \\`grep\\`\n\nYou have access to a filesystem which you can interact with using these tools.\nAll file paths must start with a /.\n\n- ls: list files in a directory (requires absolute path)\n- read_file: read a file from the filesystem\n- write_file: write to a file in the filesystem\n- edit_file: edit a file in the filesystem\n- glob: find files matching a pattern (e.g., \"**/*.py\")\n- grep: search for text within files`;\n\n// Tool descriptions - ported from Python for comprehensive LLM guidance\nexport const LS_TOOL_DESCRIPTION = `Lists all files in a directory.\n\nThis is useful for exploring the filesystem and finding the right file to read or edit.\nYou should almost ALWAYS use this tool before using the read_file or edit_file tools.`;\n\nexport const READ_FILE_TOOL_DESCRIPTION = `Reads a file from the filesystem.\n\nAssume this tool is able to read all files. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.\n\nUsage:\n- By default, it reads up to 500 lines starting from the beginning of the file\n- **IMPORTANT for large files and codebase exploration**: Use pagination with offset and limit parameters to avoid context overflow\n - First scan: read_file(path, limit=100) to see file structure\n - Read more sections: read_file(path, offset=100, limit=200) for next 200 lines\n - Only omit limit (read full file) when necessary for editing\n- Specify offset and limit: read_file(path, offset=0, limit=100) reads first 100 lines\n- Results are returned using cat -n format, with line numbers starting at 1\n- Lines longer than 10,000 characters will be split into multiple lines with continuation markers (e.g., 5.1, 5.2, etc.). When you specify a limit, these continuation lines count towards the limit.\n- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.\n- If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents.\n- You should ALWAYS make sure a file has been read before editing it.`;\n\nexport const WRITE_FILE_TOOL_DESCRIPTION = `Writes to a new file in the filesystem.\n\nUsage:\n- The write_file tool will create a new file.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.`;\n\nexport const EDIT_FILE_TOOL_DESCRIPTION = `Performs exact string replacements in files.\n\nUsage:\n- You must read the file before editing. This tool will error if you attempt an edit without reading the file first.\n- When editing, preserve the exact indentation (tabs/spaces) from the read output. Never include line number prefixes in old_string or new_string.\n- ALWAYS prefer editing existing files over creating new ones.\n- Only use emojis if the user explicitly requests it.`;\n\nexport const GLOB_TOOL_DESCRIPTION = `Find files matching a glob pattern.\n\nSupports standard glob patterns: \\`*\\` (any characters), \\`**\\` (any directories), \\`?\\` (single character).\nReturns a list of absolute file paths that match the pattern.\n\nExamples:\n- \\`**/*.py\\` - Find all Python files\n- \\`*.txt\\` - Find all text files in root\n- \\`/subdir/**/*.md\\` - Find all markdown files under /subdir`;\n\nexport const GREP_TOOL_DESCRIPTION = `Search for a text pattern across files.\n\nSearches for literal text (not regex) and returns matching files or content based on output_mode.\n\nExamples:\n- Search all files: \\`grep(pattern=\"TODO\")\\`\n- Search Python files only: \\`grep(pattern=\"import\", glob=\"*.py\")\\`\n- Show matching lines: \\`grep(pattern=\"error\", output_mode=\"content\")\\``;\nexport const EXECUTE_TOOL_DESCRIPTION = `Executes a shell command in an isolated sandbox environment.\n\nUsage:\nExecutes a given command in the sandbox environment with proper handling and security measures.\nBefore executing the command, please follow these steps:\n\n1. Directory Verification:\n - If the command will create new directories or files, first use the ls tool to verify the parent directory exists and is the correct location\n - For example, before running \"mkdir foo/bar\", first use ls to check that \"foo\" exists and is the intended parent directory\n\n2. Command Execution:\n - Always quote file paths that contain spaces with double quotes (e.g., cd \"path with spaces/file.txt\")\n - Examples of proper quoting:\n - cd \"/Users/name/My Documents\" (correct)\n - cd /Users/name/My Documents (incorrect - will fail)\n - python \"/path/with spaces/script.py\" (correct)\n - python /path/with spaces/script.py (incorrect - will fail)\n - After ensuring proper quoting, execute the command\n - Capture the output of the command\n\nUsage notes:\n - Commands run in an isolated sandbox environment\n - Returns combined stdout/stderr output with exit code\n - If the output is very large, it may be truncated\n - VERY IMPORTANT: You MUST avoid using search commands like find and grep. Instead use the grep, glob tools to search. You MUST avoid read tools like cat, head, tail, and use read_file to read files.\n - When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings)\n - Use '&&' when commands depend on each other (e.g., \"mkdir dir && cd dir\")\n - Use ';' only when you need to run commands sequentially but don't care if earlier commands fail\n - Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of cd\n\nExamples:\n Good examples:\n - execute(command=\"pytest /foo/bar/tests\")\n - execute(command=\"python /path/to/script.py\")\n - execute(command=\"npm install && npm test\")\n\n Bad examples (avoid these):\n - execute(command=\"cd /foo/bar && pytest tests\") # Use absolute path instead\n - execute(command=\"cat file.txt\") # Use read_file tool instead\n - execute(command=\"find . -name '*.py'\") # Use glob tool instead\n - execute(command=\"grep -r 'pattern' .\") # Use grep tool instead\n\nNote: This tool is only available if the backend supports execution (SandboxBackendProtocol).\nIf execution is not supported, the tool will return an error message.`;\n\n// System prompt for execution capability\nexport const EXECUTION_SYSTEM_PROMPT = `## Execute Tool \\`execute\\`\n\nYou have access to an \\`execute\\` tool for running shell commands in a sandboxed environment.\nUse this tool to run commands, scripts, tests, builds, and other shell operations.\n\n- execute: run a shell command in the sandbox (returns output and exit code)`;\n\n/**\n * Create ls tool using backend.\n */\nfunction createLsTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const path = input.path || \"/\";\n const infos = await resolvedBackend.lsInfo(path);\n\n if (infos.length === 0) {\n return `No files found in ${path}`;\n }\n\n // Format output\n const lines: string[] = [];\n for (const info of infos) {\n if (info.is_dir) {\n lines.push(`${info.path} (directory)`);\n } else {\n const size = info.size ? ` (${info.size} bytes)` : \"\";\n lines.push(`${info.path}${size}`);\n }\n }\n return lines.join(\"\\n\");\n },\n {\n name: \"ls\",\n description: customDescription || LS_TOOL_DESCRIPTION,\n schema: z.object({\n path: z\n .string()\n .optional()\n .default(\"/\")\n .describe(\"Directory path to list (default: /)\"),\n }),\n },\n );\n}\n\n/**\n * Create read_file tool using backend.\n */\nfunction createReadFileTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { file_path, offset = 0, limit = 500 } = input;\n return await resolvedBackend.read(file_path, offset, limit);\n },\n {\n name: \"read_file\",\n description: customDescription || READ_FILE_TOOL_DESCRIPTION,\n schema: z.object({\n file_path: z.string().describe(\"Absolute path to the file to read\"),\n offset: z.coerce\n .number()\n .optional()\n .default(0)\n .describe(\"Line offset to start reading from (0-indexed)\"),\n limit: z.coerce\n .number()\n .optional()\n .default(500)\n .describe(\"Maximum number of lines to read\"),\n }),\n },\n );\n}\n\n/**\n * Create write_file tool using backend.\n */\nfunction createWriteFileTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { file_path, content } = input;\n const result = await resolvedBackend.write(file_path, content);\n\n if (result.error) {\n return result.error;\n }\n\n // If filesUpdate is present, return Command to update state\n const message = new ToolMessage({\n content: `Successfully wrote to '${file_path}'`,\n tool_call_id: config.toolCall?.id as string,\n name: \"write_file\",\n metadata: result.metadata,\n });\n\n if (result.filesUpdate) {\n return new Command({\n update: { files: result.filesUpdate, messages: [message] },\n });\n }\n\n return message;\n },\n {\n name: \"write_file\",\n description: customDescription || WRITE_FILE_TOOL_DESCRIPTION,\n schema: z.object({\n file_path: z.string().describe(\"Absolute path to the file to write\"),\n content: z.string().describe(\"Content to write to the file\"),\n }),\n },\n );\n}\n\n/**\n * Create edit_file tool using backend.\n */\nfunction createEditFileTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { file_path, old_string, new_string, replace_all = false } = input;\n const result = await resolvedBackend.edit(\n file_path,\n old_string,\n new_string,\n replace_all,\n );\n\n if (result.error) {\n return result.error;\n }\n\n const message = new ToolMessage({\n content: `Successfully replaced ${result.occurrences} occurrence(s) in '${file_path}'`,\n tool_call_id: config.toolCall?.id as string,\n name: \"edit_file\",\n metadata: result.metadata,\n });\n\n // If filesUpdate is present, return Command to update state\n if (result.filesUpdate) {\n return new Command({\n update: { files: result.filesUpdate, messages: [message] },\n });\n }\n\n // External storage (filesUpdate is null)\n return message;\n },\n {\n name: \"edit_file\",\n description: customDescription || EDIT_FILE_TOOL_DESCRIPTION,\n schema: z.object({\n file_path: z.string().describe(\"Absolute path to the file to edit\"),\n old_string: z\n .string()\n .describe(\"String to be replaced (must match exactly)\"),\n new_string: z.string().describe(\"String to replace with\"),\n replace_all: z\n .boolean()\n .optional()\n .default(false)\n .describe(\"Whether to replace all occurrences\"),\n }),\n },\n );\n}\n\n/**\n * Create glob tool using backend.\n */\nfunction createGlobTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { pattern, path = \"/\" } = input;\n const infos = await resolvedBackend.globInfo(pattern, path);\n\n if (infos.length === 0) {\n return `No files found matching pattern '${pattern}'`;\n }\n\n return infos.map((info) => info.path).join(\"\\n\");\n },\n {\n name: \"glob\",\n description: customDescription || GLOB_TOOL_DESCRIPTION,\n schema: z.object({\n pattern: z.string().describe(\"Glob pattern (e.g., '*.py', '**/*.ts')\"),\n path: z\n .string()\n .optional()\n .default(\"/\")\n .describe(\"Base path to search from (default: /)\"),\n }),\n },\n );\n}\n\n/**\n * Create grep tool using backend.\n */\nfunction createGrepTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const { pattern, path = \"/\", glob = null } = input;\n const result = await resolvedBackend.grepRaw(pattern, path, glob);\n\n // If string, it's an error\n if (typeof result === \"string\") {\n return result;\n }\n\n if (result.length === 0) {\n return `No matches found for pattern '${pattern}'`;\n }\n\n // Format output: group by file\n const lines: string[] = [];\n let currentFile: string | null = null;\n for (const match of result) {\n if (match.path !== currentFile) {\n currentFile = match.path;\n lines.push(`\\n${currentFile}:`);\n }\n lines.push(` ${match.line}: ${match.text}`);\n }\n\n return lines.join(\"\\n\");\n },\n {\n name: \"grep\",\n description: customDescription || GREP_TOOL_DESCRIPTION,\n schema: z.object({\n pattern: z.string().describe(\"Regex pattern to search for\"),\n path: z\n .string()\n .optional()\n .default(\"/\")\n .describe(\"Base path to search from (default: /)\"),\n glob: z\n .string()\n .optional()\n .nullable()\n .describe(\"Optional glob pattern to filter files (e.g., '*.py')\"),\n }),\n },\n );\n}\n\n/**\n * Create execute tool using backend.\n */\nfunction createExecuteTool(\n backend: BackendProtocol | BackendFactory,\n options: { customDescription: string | undefined },\n) {\n const { customDescription } = options;\n return tool(\n async (input, config) => {\n const stateAndStore: StateAndStore = {\n state: getCurrentTaskInput(config),\n store: (config as any).store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n\n // Runtime check - fail gracefully if not supported\n if (!isSandboxBackend(resolvedBackend)) {\n return (\n \"Error: Execution not available. This agent's backend \" +\n \"does not support command execution (SandboxBackendProtocol). \" +\n \"To use the execute tool, provide a backend that implements SandboxBackendProtocol.\"\n );\n }\n\n const result = await resolvedBackend.execute(input.command);\n\n // Format output for LLM consumption\n const parts = [result.output];\n\n if (result.exitCode !== null) {\n const status = result.exitCode === 0 ? \"succeeded\" : \"failed\";\n parts.push(`\\n[Command ${status} with exit code ${result.exitCode}]`);\n }\n\n if (result.truncated) {\n parts.push(\"\\n[Output was truncated due to size limits]\");\n }\n\n return parts.join(\"\");\n },\n {\n name: \"execute\",\n description: customDescription || EXECUTE_TOOL_DESCRIPTION,\n schema: z.object({\n command: z.string().describe(\"The shell command to execute\"),\n }),\n },\n );\n}\n\n/**\n * Options for creating filesystem middleware.\n */\nexport interface FilesystemMiddlewareOptions {\n /** Backend instance or factory (default: StateBackend) */\n backend?: BackendProtocol | BackendFactory;\n /** Optional custom system prompt override */\n systemPrompt?: string | null;\n /** Optional custom tool descriptions override */\n customToolDescriptions?: Record<string, string> | null;\n /** Optional token limit before evicting a tool result to the filesystem (default: 20000 tokens, ~80KB) */\n toolTokenLimitBeforeEvict?: number | null;\n}\n\n/**\n * Create filesystem middleware with all tools and features.\n */\nexport function createFilesystemMiddleware(\n options: FilesystemMiddlewareOptions = {},\n) {\n const {\n backend = (stateAndStore: StateAndStore) => new StateBackend(stateAndStore),\n systemPrompt: customSystemPrompt = null,\n customToolDescriptions = null,\n toolTokenLimitBeforeEvict = 20000,\n } = options;\n\n const baseSystemPrompt = customSystemPrompt || FILESYSTEM_SYSTEM_PROMPT;\n\n // All tools including execute (execute will be filtered at runtime if backend doesn't support it)\n const allTools = [\n createLsTool(backend, {\n customDescription: customToolDescriptions?.ls,\n }),\n createReadFileTool(backend, {\n customDescription: customToolDescriptions?.read_file,\n }),\n createWriteFileTool(backend, {\n customDescription: customToolDescriptions?.write_file,\n }),\n createEditFileTool(backend, {\n customDescription: customToolDescriptions?.edit_file,\n }),\n createGlobTool(backend, {\n customDescription: customToolDescriptions?.glob,\n }),\n createGrepTool(backend, {\n customDescription: customToolDescriptions?.grep,\n }),\n createExecuteTool(backend, {\n customDescription: customToolDescriptions?.execute,\n }),\n ];\n\n return createMiddleware({\n name: \"FilesystemMiddleware\",\n stateSchema: FilesystemStateSchema,\n tools: allTools,\n wrapModelCall: async (request, handler) => {\n // Check if backend supports execution\n const stateAndStore: StateAndStore = {\n state: request.state || {},\n // @ts-expect-error - request.config is incorrect typed\n store: request.config?.store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const supportsExecution = isSandboxBackend(resolvedBackend);\n\n // Filter tools based on backend capabilities\n let tools = request.tools;\n if (!supportsExecution) {\n tools = tools.filter((t: { name: string }) => t.name !== \"execute\");\n }\n\n // Build system prompt - add execution instructions if available\n let systemPrompt = baseSystemPrompt;\n if (supportsExecution) {\n systemPrompt = `${systemPrompt}\\n\\n${EXECUTION_SYSTEM_PROMPT}`;\n }\n\n // Combine with existing system prompt\n const currentSystemPrompt = request.systemPrompt || \"\";\n const newSystemPrompt = currentSystemPrompt\n ? `${currentSystemPrompt}\\n\\n${systemPrompt}`\n : systemPrompt;\n\n return handler({ ...request, tools, systemPrompt: newSystemPrompt });\n },\n wrapToolCall: async (request, handler) => {\n // Return early if eviction is disabled\n if (!toolTokenLimitBeforeEvict) {\n return handler(request);\n }\n\n // Check if this tool is excluded from eviction\n const toolName = request.toolCall?.name;\n if (\n toolName &&\n TOOLS_EXCLUDED_FROM_EVICTION.includes(\n toolName as (typeof TOOLS_EXCLUDED_FROM_EVICTION)[number],\n )\n ) {\n return handler(request);\n }\n\n const result = await handler(request);\n\n async function processToolMessage(\n msg: ToolMessage,\n toolTokenLimitBeforeEvict: number,\n ) {\n if (\n typeof msg.content === \"string\" &&\n msg.content.length > toolTokenLimitBeforeEvict * NUM_CHARS_PER_TOKEN\n ) {\n // Build StateAndStore from request\n const stateAndStore: StateAndStore = {\n state: request.state || {},\n // @ts-expect-error - request.config is incorrect typed\n store: request.config?.store,\n };\n const resolvedBackend = getBackend(backend, stateAndStore);\n const sanitizedId = sanitizeToolCallId(\n request.toolCall?.id || msg.tool_call_id,\n );\n const evictPath = `/large_tool_results/${sanitizedId}`;\n\n const writeResult = await resolvedBackend.write(\n evictPath,\n msg.content,\n );\n\n if (writeResult.error) {\n return { message: msg, filesUpdate: null };\n }\n\n // Create preview showing head and tail of the result\n const contentSample = createContentPreview(msg.content);\n const replacementText = TOO_LARGE_TOOL_MSG.replace(\n \"{tool_call_id}\",\n msg.tool_call_id,\n )\n .replace(\"{file_path}\", evictPath)\n .replace(\"{content_sample}\", contentSample);\n\n const truncatedMessage = new ToolMessage({\n content: replacementText,\n tool_call_id: msg.tool_call_id,\n name: msg.name,\n });\n\n return {\n message: truncatedMessage,\n filesUpdate: writeResult.filesUpdate,\n };\n }\n return { message: msg, filesUpdate: null };\n }\n\n if (ToolMessage.isInstance(result)) {\n const processed = await processToolMessage(\n result,\n toolTokenLimitBeforeEvict,\n );\n\n if (processed.filesUpdate) {\n return new Command({\n update: {\n files: processed.filesUpdate,\n messages: [processed.message],\n },\n });\n }\n\n return processed.message;\n }\n\n if (isCommand(result)) {\n const update = result.update as any;\n if (!update?.messages) {\n return result;\n }\n\n let hasLargeResults = false;\n const accumulatedFiles: Record<string, FileData> = update.files\n ? { ...update.files }\n : {};\n const processedMessages: ToolMessage[] = [];\n\n for (const msg of update.messages) {\n if (ToolMessage.isInstance(msg)) {\n const processed = await processToolMessage(\n msg,\n toolTokenLimitBeforeEvict,\n );\n processedMessages.push(processed.message);\n\n if (processed.filesUpdate) {\n hasLargeResults = true;\n Object.assign(accumulatedFiles, processed.filesUpdate);\n }\n } else {\n processedMessages.push(msg);\n }\n }\n\n if (hasLargeResults) {\n return new Command({\n update: {\n ...update,\n messages: processedMessages,\n files: accumulatedFiles,\n },\n });\n }\n }\n\n return result;\n },\n });\n}\n","import { z } from \"zod/v4\";\nimport {\n createMiddleware,\n createAgent,\n AgentMiddleware,\n tool,\n ToolMessage,\n humanInTheLoopMiddleware,\n SystemMessage,\n type InterruptOnConfig,\n type ReactAgent,\n StructuredTool,\n} from \"langchain\";\nimport { Command, getCurrentTaskInput } from \"@langchain/langgraph\";\nimport type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport type { Runnable } from \"@langchain/core/runnables\";\nimport { HumanMessage } from \"@langchain/core/messages\";\n\nexport type { AgentMiddleware };\n\n// Constants\nconst DEFAULT_SUBAGENT_PROMPT =\n \"In order to complete the objective that the user asks of you, you have access to a number of standard tools.\";\n\n// State keys that are excluded when passing state to subagents and when returning\n// updates from subagents.\n// When returning updates:\n// 1. The messages key is handled explicitly to ensure only the final message is included\n// 2. The todos and structuredResponse keys are excluded as they do not have a defined reducer\n// and no clear meaning for returning them from a subagent to the main agent.\nconst EXCLUDED_STATE_KEYS = [\n \"messages\",\n \"todos\",\n \"structuredResponse\",\n] as const;\n\nconst DEFAULT_GENERAL_PURPOSE_DESCRIPTION =\n \"General-purpose agent for researching complex questions, searching for files and content, and executing multi-step tasks. When you are searching for a keyword or file and are not confident that you will find the right match in the first few tries use this agent to perform the search for you. This agent has access to all tools as the main agent.\";\n\n// Comprehensive task tool description from Python\nfunction getTaskToolDescription(subagentDescriptions: string[]): string {\n return `\nLaunch an ephemeral subagent to handle complex, multi-step independent tasks with isolated context windows.\n\nAvailable agent types and the tools they have access to:\n${subagentDescriptions.join(\"\\n\")}\n\nWhen using the Task tool, you must specify a subagent_type parameter to select which agent type to use.\n\n## Usage notes:\n1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses\n2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.\n3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.\n4. The agent's outputs should generally be trusted\n5. Clearly tell the agent whether you expect it to create content, perform analysis, or just do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent\n6. If the agent description mentions that it should be used proactively, then you should try your best to use it without the user having to ask for it first. Use your judgement.\n7. When only the general-purpose agent is provided, you should use it for all tasks. It is great for isolating context and token usage, and completing specific, complex tasks, as it has all the same capabilities as the main agent.\n\n### Example usage of the general-purpose agent:\n\n<example_agent_descriptions>\n\"general-purpose\": use this agent for general purpose tasks, it has access to all tools as the main agent.\n</example_agent_descriptions>\n\n<example>\nUser: \"I want to conduct research on the accomplishments of Lebron James, Michael Jordan, and Kobe Bryant, and then compare them.\"\nAssistant: *Uses the task tool in parallel to conduct isolated research on each of the three players*\nAssistant: *Synthesizes the results of the three isolated research tasks and responds to the User*\n<commentary>\nResearch is a complex, multi-step task in it of itself.\nThe research of each individual player is not dependent on the research of the other players.\nThe assistant uses the task tool to break down the complex objective into three isolated tasks.\nEach research task only needs to worry about context and tokens about one player, then returns synthesized information about each player as the Tool Result.\nThis means each research task can dive deep and spend tokens and context deeply researching each player, but the final result is synthesized information, and saves us tokens in the long run when comparing the players to each other.\n</commentary>\n</example>\n\n<example>\nUser: \"Analyze a single large code repository for security vulnerabilities and generate a report.\"\nAssistant: *Launches a single \\`task\\` subagent for the repository analysis*\nAssistant: *Receives report and integrates results into final summary*\n<commentary>\nSubagent is used to isolate a large, context-heavy task, even though there is only one. This prevents the main thread from being overloaded with details.\nIf the user then asks followup questions, we have a concise report to reference instead of the entire history of analysis and tool calls, which is good and saves us time and money.\n</commentary>\n</example>\n\n<example>\nUser: \"Schedule two meetings for me and prepare agendas for each.\"\nAssistant: *Calls the task tool in parallel to launch two \\`task\\` subagents (one per meeting) to prepare agendas*\nAssistant: *Returns final schedules and agendas*\n<commentary>\nTasks are simple individually, but subagents help silo agenda preparation.\nEach subagent only needs to worry about the agenda for one meeting.\n</commentary>\n</example>\n\n<example>\nUser: \"I want to order a pizza from Dominos, order a burger from McDonald's, and order a salad from Subway.\"\nAssistant: *Calls tools directly in parallel to order a pizza from Dominos, a burger from McDonald's, and a salad from Subway*\n<commentary>\nThe assistant did not use the task tool because the objective is super simple and clear and only requires a few trivial tool calls.\nIt is better to just complete the task directly and NOT use the \\`task\\`tool.\n</commentary>\n</example>\n\n### Example usage with custom agents:\n\n<example_agent_descriptions>\n\"content-reviewer\": use this agent after you are done creating significant content or documents\n\"greeting-responder\": use this agent when to respond to user greetings with a friendly joke\n\"research-analyst\": use this agent to conduct thorough research on complex topics\n</example_agent_description>\n\n<example>\nuser: \"Please write a function that checks if a number is prime\"\nassistant: Sure let me write a function that checks if a number is prime\nassistant: First let me use the Write tool to write a function that checks if a number is prime\nassistant: I'm going to use the Write tool to write the following code:\n<code>\nfunction isPrime(n) {\n if (n <= 1) return false\n for (let i = 2; i * i <= n; i++) {\n if (n % i === 0) return false\n }\n return true\n}\n</code>\n<commentary>\nSince significant content was created and the task was completed, now use the content-reviewer agent to review the work\n</commentary>\nassistant: Now let me use the content-reviewer agent to review the code\nassistant: Uses the Task tool to launch with the content-reviewer agent\n</example>\n\n<example>\nuser: \"Can you help me research the environmental impact of different renewable energy sources and create a comprehensive report?\"\n<commentary>\nThis is a complex research task that would benefit from using the research-analyst agent to conduct thorough analysis\n</commentary>\nassistant: I'll help you research the environmental impact of renewable energy sources. Let me use the research-analyst agent to conduct comprehensive research on this topic.\nassistant: Uses the Task tool to launch with the research-analyst agent, providing detailed instructions about what research to conduct and what format the report should take\n</example>\n\n<example>\nuser: \"Hello\"\n<commentary>\nSince the user is greeting, use the greeting-responder agent to respond with a friendly joke\n</commentary>\nassistant: \"I'm going to use the Task tool to launch with the greeting-responder agent\"\n</example>\n `.trim();\n}\n\nconst TASK_SYSTEM_PROMPT = `## \\`task\\` (subagent spawner)\n\nYou have access to a \\`task\\` tool to launch short-lived subagents that handle isolated tasks. These agents are ephemeral — they live only for the duration of the task and return a single result.\n\nWhen to use the task tool:\n- When a task is complex and multi-step, and can be fully delegated in isolation\n- When a task is independent of other tasks and can run in parallel\n- When a task requires focused reasoning or heavy token/context usage that would bloat the orchestrator thread\n- When sandboxing improves reliability (e.g. code execution, structured searches, data formatting)\n- When you only care about the output of the subagent, and not the intermediate steps (ex. performing a lot of research and then returned a synthesized report, performing a series of computations or lookups to achieve a concise, relevant answer.)\n\nSubagent lifecycle:\n1. **Spawn** → Provide clear role, instructions, and expected output\n2. **Run** → The subagent completes the task autonomously\n3. **Return** → The subagent provides a single structured result\n4. **Reconcile** → Incorporate or synthesize the result into the main thread\n\nWhen NOT to use the task tool:\n- If you need to see the intermediate reasoning or steps after the subagent has completed (the task tool hides them)\n- If the task is trivial (a few tool calls or simple lookup)\n- If delegating does not reduce token usage, complexity, or context switching\n- If splitting would add latency without benefit\n\n## Important Task Tool Usage Notes to Remember\n- Whenever possible, parallelize the work that you do. This is true for both tool_calls, and for tasks. Whenever you have independent steps to complete - make tool_calls, or kick off tasks (subagents) in parallel to accomplish them faster. This saves time for the user, which is incredibly important.\n- Remember to use the \\`task\\` tool to silo independent tasks within a multi-part objective.\n- You should use the \\`task\\` tool whenever you have a complex task that will take multiple steps, and is independent from other tasks that the agent needs to complete. These agents are highly competent and efficient.`;\n\n/**\n * Type definitions for pre-compiled agents.\n *\n * @typeParam TRunnable - The type of the runnable (ReactAgent or Runnable).\n * When using `createAgent`, this preserves the middleware types for type inference.\n */\nexport interface CompiledSubAgent<\n TRunnable extends ReactAgent | Runnable = ReactAgent | Runnable,\n> {\n /** The name of the agent */\n name: string;\n /** The description of the agent */\n description: string;\n /** The agent instance */\n runnable: TRunnable;\n}\n\n/**\n * Type definitions for subagents\n */\nexport interface SubAgent {\n /** The name of the agent */\n name: string;\n /** The description of the agent */\n description: string;\n /** The system prompt to use for the agent */\n systemPrompt: string;\n /** The tools to use for the agent (tool instances, not names). Defaults to defaultTools */\n tools?: StructuredTool[];\n /** The model for the agent. Defaults to default_model */\n model?: LanguageModelLike | string;\n /** Additional middleware to append after default_middleware */\n middleware?: readonly AgentMiddleware[];\n /** The tool configs to use for the agent */\n interruptOn?: Record<string, boolean | InterruptOnConfig>;\n}\n\n/**\n * Filter state to exclude certain keys when passing to subagents\n */\nfunction filterStateForSubagent(\n state: Record<string, unknown>,\n): Record<string, unknown> {\n const filtered: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(state)) {\n if (!EXCLUDED_STATE_KEYS.includes(key as never)) {\n filtered[key] = value;\n }\n }\n return filtered;\n}\n\n/**\n * Create Command with filtered state update from subagent result\n */\nfunction returnCommandWithStateUpdate(\n result: Record<string, unknown>,\n toolCallId: string,\n): Command {\n const stateUpdate = filterStateForSubagent(result);\n const messages = result.messages as Array<{ content: string }>;\n const lastMessage = messages?.[messages.length - 1];\n\n return new Command({\n update: {\n ...stateUpdate,\n messages: [\n new ToolMessage({\n content: lastMessage?.content || \"Task completed\",\n tool_call_id: toolCallId,\n name: \"task\",\n }),\n ],\n },\n });\n}\n\n/**\n * Create subagent instances from specifications\n */\nfunction getSubagents(options: {\n defaultModel: LanguageModelLike | string;\n defaultTools: StructuredTool[];\n defaultMiddleware: AgentMiddleware[] | null;\n defaultInterruptOn: Record<string, boolean | InterruptOnConfig> | null;\n subagents: (SubAgent | CompiledSubAgent)[];\n generalPurposeAgent: boolean;\n}): {\n agents: Record<string, ReactAgent | Runnable>;\n descriptions: string[];\n} {\n const {\n defaultModel,\n defaultTools,\n defaultMiddleware,\n defaultInterruptOn,\n subagents,\n generalPurposeAgent,\n } = options;\n\n const defaultSubagentMiddleware = defaultMiddleware || [];\n const agents: Record<string, ReactAgent | Runnable> = {};\n const subagentDescriptions: string[] = [];\n\n // Create general-purpose agent if enabled\n if (generalPurposeAgent) {\n const generalPurposeMiddleware = [...defaultSubagentMiddleware];\n if (defaultInterruptOn) {\n generalPurposeMiddleware.push(\n humanInTheLoopMiddleware({ interruptOn: defaultInterruptOn }),\n );\n }\n\n const generalPurposeSubagent = createAgent({\n model: defaultModel,\n systemPrompt: DEFAULT_SUBAGENT_PROMPT,\n tools: defaultTools as any,\n middleware: generalPurposeMiddleware,\n });\n\n agents[\"general-purpose\"] = generalPurposeSubagent;\n subagentDescriptions.push(\n `- general-purpose: ${DEFAULT_GENERAL_PURPOSE_DESCRIPTION}`,\n );\n }\n\n // Process custom subagents\n for (const agentParams of subagents) {\n subagentDescriptions.push(\n `- ${agentParams.name}: ${agentParams.description}`,\n );\n\n if (\"runnable\" in agentParams) {\n agents[agentParams.name] = agentParams.runnable;\n } else {\n const middleware = agentParams.middleware\n ? [...defaultSubagentMiddleware, ...agentParams.middleware]\n : [...defaultSubagentMiddleware];\n\n const interruptOn = agentParams.interruptOn || defaultInterruptOn;\n if (interruptOn)\n middleware.push(humanInTheLoopMiddleware({ interruptOn }));\n\n agents[agentParams.name] = createAgent({\n model: agentParams.model ?? defaultModel,\n systemPrompt: agentParams.systemPrompt,\n tools: agentParams.tools ?? defaultTools,\n middleware,\n });\n }\n }\n\n return { agents, descriptions: subagentDescriptions };\n}\n\n/**\n * Create the task tool for invoking subagents\n */\nfunction createTaskTool(options: {\n defaultModel: LanguageModelLike | string;\n defaultTools: StructuredTool[];\n defaultMiddleware: AgentMiddleware[] | null;\n defaultInterruptOn: Record<string, boolean | InterruptOnConfig> | null;\n subagents: (SubAgent | CompiledSubAgent)[];\n generalPurposeAgent: boolean;\n taskDescription: string | null;\n}) {\n const {\n defaultModel,\n defaultTools,\n defaultMiddleware,\n defaultInterruptOn,\n subagents,\n generalPurposeAgent,\n taskDescription,\n } = options;\n\n const { agents: subagentGraphs, descriptions: subagentDescriptions } =\n getSubagents({\n defaultModel,\n defaultTools,\n defaultMiddleware,\n defaultInterruptOn,\n subagents,\n generalPurposeAgent,\n });\n\n const finalTaskDescription = taskDescription\n ? taskDescription\n : getTaskToolDescription(subagentDescriptions);\n\n return tool(\n async (\n input: { description: string; subagent_type: string },\n config,\n ): Promise<Command | string> => {\n const { description, subagent_type } = input;\n\n // Validate subagent type\n if (!(subagent_type in subagentGraphs)) {\n const allowedTypes = Object.keys(subagentGraphs)\n .map((k) => `\\`${k}\\``)\n .join(\", \");\n throw new Error(\n `Error: invoked agent of type ${subagent_type}, the only allowed types are ${allowedTypes}`,\n );\n }\n\n const subagent = subagentGraphs[subagent_type];\n\n // Get current state and filter it for subagent\n const currentState = getCurrentTaskInput<Record<string, unknown>>();\n const subagentState = filterStateForSubagent(currentState);\n subagentState.messages = [new HumanMessage({ content: description })];\n\n // Invoke the subagent\n const result = (await subagent.invoke(subagentState, config)) as Record<\n string,\n unknown\n >;\n\n // Return command with filtered state update\n if (!config.toolCall?.id) {\n throw new Error(\"Tool call ID is required for subagent invocation\");\n }\n\n return returnCommandWithStateUpdate(result, config.toolCall.id);\n },\n {\n name: \"task\",\n description: finalTaskDescription,\n schema: z.object({\n description: z\n .string()\n .describe(\"The task to execute with the selected agent\"),\n subagent_type: z\n .string()\n .describe(\n `Name of the agent to use. Available: ${Object.keys(subagentGraphs).join(\", \")}`,\n ),\n }),\n },\n );\n}\n\n/**\n * Options for creating subagent middleware\n */\nexport interface SubAgentMiddlewareOptions {\n /** The model to use for subagents */\n defaultModel: LanguageModelLike | string;\n /** The tools to use for the default general-purpose subagent */\n defaultTools?: StructuredTool[];\n /** Default middleware to apply to all subagents */\n defaultMiddleware?: AgentMiddleware[] | null;\n /** The tool configs for the default general-purpose subagent */\n defaultInterruptOn?: Record<string, boolean | InterruptOnConfig> | null;\n /** A list of additional subagents to provide to the agent */\n subagents?: (SubAgent | CompiledSubAgent)[];\n /** Full system prompt override */\n systemPrompt?: string | null;\n /** Whether to include the general-purpose agent */\n generalPurposeAgent?: boolean;\n /** Custom description for the task tool */\n taskDescription?: string | null;\n}\n\n/**\n * Create subagent middleware with task tool\n */\nexport function createSubAgentMiddleware(options: SubAgentMiddlewareOptions) {\n const {\n defaultModel,\n defaultTools = [],\n defaultMiddleware = null,\n defaultInterruptOn = null,\n subagents = [],\n systemPrompt = TASK_SYSTEM_PROMPT,\n generalPurposeAgent = true,\n taskDescription = null,\n } = options;\n\n const taskTool = createTaskTool({\n defaultModel,\n defaultTools,\n defaultMiddleware,\n defaultInterruptOn,\n subagents,\n generalPurposeAgent,\n taskDescription,\n });\n\n return createMiddleware({\n name: \"subAgentMiddleware\",\n tools: [taskTool],\n wrapModelCall: async (request, handler) => {\n if (systemPrompt !== null) {\n return handler({\n ...request,\n systemMessage: request.systemMessage.concat(\n new SystemMessage({ content: systemPrompt }),\n ),\n });\n }\n return handler(request);\n },\n });\n}\n","import {\n createMiddleware,\n ToolMessage,\n AIMessage,\n /**\n * required for type inference\n */\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { RemoveMessage } from \"@langchain/core/messages\";\nimport { REMOVE_ALL_MESSAGES } from \"@langchain/langgraph\";\n\n/**\n * Create middleware that patches dangling tool calls in the messages history.\n *\n * When an AI message contains tool_calls but subsequent messages don't include\n * the corresponding ToolMessage responses, this middleware adds synthetic\n * ToolMessages saying the tool call was cancelled.\n *\n * @returns AgentMiddleware that patches dangling tool calls\n *\n * @example\n * ```typescript\n * import { createAgent } from \"langchain\";\n * import { createPatchToolCallsMiddleware } from \"./middleware/patch_tool_calls\";\n *\n * const agent = createAgent({\n * model: \"claude-sonnet-4-5-20250929\",\n * middleware: [createPatchToolCallsMiddleware()],\n * });\n * ```\n */\nexport function createPatchToolCallsMiddleware() {\n return createMiddleware({\n name: \"patchToolCallsMiddleware\",\n beforeAgent: async (state) => {\n const messages = state.messages;\n\n if (!messages || messages.length === 0) {\n return;\n }\n\n const patchedMessages: any[] = [];\n\n // Iterate over the messages and add any dangling tool calls\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n patchedMessages.push(msg);\n\n // Check if this is an AI message with tool calls\n if (AIMessage.isInstance(msg) && msg.tool_calls != null) {\n for (const toolCall of msg.tool_calls) {\n // Look for a corresponding ToolMessage in the messages after this one\n const correspondingToolMsg = messages\n .slice(i)\n .find(\n (m: any) =>\n ToolMessage.isInstance(m) && m.tool_call_id === toolCall.id,\n );\n\n if (!correspondingToolMsg) {\n // We have a dangling tool call which needs a ToolMessage\n const toolMsg = `Tool call ${toolCall.name} with id ${toolCall.id} was cancelled - another message came in before it could be completed.`;\n patchedMessages.push(\n new ToolMessage({\n content: toolMsg,\n name: toolCall.name,\n tool_call_id: toolCall.id!,\n }),\n );\n }\n }\n }\n }\n\n // Return state update with RemoveMessage followed by patched messages\n return {\n messages: [\n new RemoveMessage({ id: REMOVE_ALL_MESSAGES }),\n ...patchedMessages,\n ],\n };\n },\n });\n}\n","/**\n * Middleware for loading agent memory/context from AGENTS.md files.\n *\n * This module implements support for the AGENTS.md specification (https://agents.md/),\n * loading memory/context from configurable sources and injecting into the system prompt.\n *\n * ## Overview\n *\n * AGENTS.md files provide project-specific context and instructions to help AI agents\n * work effectively. Unlike skills (which are on-demand workflows), memory is always\n * loaded and provides persistent context.\n *\n * ## Usage\n *\n * ```typescript\n * import { createMemoryMiddleware } from \"@anthropic/deepagents\";\n * import { FilesystemBackend } from \"@anthropic/deepagents\";\n *\n * // Security: FilesystemBackend allows reading/writing from the entire filesystem.\n * // Either ensure the agent is running within a sandbox OR add human-in-the-loop (HIL)\n * // approval to file operations.\n * const backend = new FilesystemBackend({ rootDir: \"/\" });\n *\n * const middleware = createMemoryMiddleware({\n * backend,\n * sources: [\n * \"~/.deepagents/AGENTS.md\",\n * \"./.deepagents/AGENTS.md\",\n * ],\n * });\n *\n * const agent = createDeepAgent({ middleware: [middleware] });\n * ```\n *\n * ## Memory Sources\n *\n * Sources are simply paths to AGENTS.md files that are loaded in order and combined.\n * Multiple sources are concatenated in order, with all content included.\n * Later sources appear after earlier ones in the combined prompt.\n *\n * ## File Format\n *\n * AGENTS.md files are standard Markdown with no required structure.\n * Common sections include:\n * - Project overview\n * - Build/test commands\n * - Code style guidelines\n * - Architecture notes\n */\n\nimport { z } from \"zod\";\nimport {\n createMiddleware,\n /**\n * required for type inference\n */\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\n\nimport type { BackendProtocol, BackendFactory } from \"../backends/protocol.js\";\nimport type { StateBackend } from \"../backends/state.js\";\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\n/**\n * Options for the memory middleware.\n */\nexport interface MemoryMiddlewareOptions {\n /**\n * Backend instance or factory function for file operations.\n * Use a factory for StateBackend since it requires runtime state.\n */\n backend:\n | BackendProtocol\n | BackendFactory\n | ((config: { state: unknown; store?: BaseStore }) => StateBackend);\n\n /**\n * List of memory file paths to load (e.g., [\"~/.deepagents/AGENTS.md\", \"./.deepagents/AGENTS.md\"]).\n * Display names are automatically derived from the paths.\n * Sources are loaded in order.\n */\n sources: string[];\n}\n\n/**\n * State schema for memory middleware.\n */\nconst MemoryStateSchema = z.object({\n /**\n * Dict mapping source paths to their loaded content.\n * Marked as private so it's not included in the final agent state.\n */\n memoryContents: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Default system prompt template for memory.\n * Ported from Python's comprehensive memory guidelines.\n */\nconst MEMORY_SYSTEM_PROMPT = `<agent_memory>\n{memory_contents}\n</agent_memory>\n\n<memory_guidelines>\n The above <agent_memory> was loaded in from files in your filesystem. As you learn from your interactions with the user, you can save new knowledge by calling the \\`edit_file\\` tool.\n\n **Learning from feedback:**\n - One of your MAIN PRIORITIES is to learn from your interactions with the user. These learnings can be implicit or explicit. This means that in the future, you will remember this important information.\n - When you need to remember something, updating memory must be your FIRST, IMMEDIATE action - before responding to the user, before calling other tools, before doing anything else. Just update memory immediately.\n - When user says something is better/worse, capture WHY and encode it as a pattern.\n - Each correction is a chance to improve permanently - don't just fix the immediate issue, update your instructions.\n - A great opportunity to update your memories is when the user interrupts a tool call and provides feedback. You should update your memories immediately before revising the tool call.\n - Look for the underlying principle behind corrections, not just the specific mistake.\n - The user might not explicitly ask you to remember something, but if they provide information that is useful for future use, you should update your memories immediately.\n\n **Asking for information:**\n - If you lack context to perform an action (e.g. send a Slack DM, requires a user ID/email) you should explicitly ask the user for this information.\n - It is preferred for you to ask for information, don't assume anything that you do not know!\n - When the user provides information that is useful for future use, you should update your memories immediately.\n\n **When to update memories:**\n - When the user explicitly asks you to remember something (e.g., \"remember my email\", \"save this preference\")\n - When the user describes your role or how you should behave (e.g., \"you are a web researcher\", \"always do X\")\n - When the user gives feedback on your work - capture what was wrong and how to improve\n - When the user provides information required for tool use (e.g., slack channel ID, email addresses)\n - When the user provides context useful for future tasks, such as how to use tools, or which actions to take in a particular situation\n - When you discover new patterns or preferences (coding styles, conventions, workflows)\n\n **When to NOT update memories:**\n - When the information is temporary or transient (e.g., \"I'm running late\", \"I'm on my phone right now\")\n - When the information is a one-time task request (e.g., \"Find me a recipe\", \"What's 25 * 4?\")\n - When the information is a simple question that doesn't reveal lasting preferences (e.g., \"What day is it?\", \"Can you explain X?\")\n - When the information is an acknowledgment or small talk (e.g., \"Sounds good!\", \"Hello\", \"Thanks for that\")\n - When the information is stale or irrelevant in future conversations\n - Never store API keys, access tokens, passwords, or any other credentials in any file, memory, or system prompt.\n - If the user asks where to put API keys or provides an API key, do NOT echo or save it.\n\n **Examples:**\n Example 1 (remembering user information):\n User: Can you connect to my google account?\n Agent: Sure, I'll connect to your google account, what's your google account email?\n User: john@example.com\n Agent: Let me save this to my memory.\n Tool Call: edit_file(...) -> remembers that the user's google account email is john@example.com\n\n Example 2 (remembering implicit user preferences):\n User: Can you write me an example for creating a deep agent in LangChain?\n Agent: Sure, I'll write you an example for creating a deep agent in LangChain <example code in Python>\n User: Can you do this in JavaScript\n Agent: Let me save this to my memory.\n Tool Call: edit_file(...) -> remembers that the user prefers to get LangChain code examples in JavaScript\n Agent: Sure, here is the JavaScript example<example code in JavaScript>\n\n Example 3 (do not remember transient information):\n User: I'm going to play basketball tonight so I will be offline for a few hours.\n Agent: Okay I'll add a block to your calendar.\n Tool Call: create_calendar_event(...) -> just calls a tool, does not commit anything to memory, as it is transient information\n</memory_guidelines>`;\n\n/**\n * Format loaded memory contents for injection into prompt.\n * Pairs memory locations with their contents for clarity.\n */\nfunction formatMemoryContents(\n contents: Record<string, string>,\n sources: string[],\n): string {\n if (Object.keys(contents).length === 0) {\n return \"(No memory loaded)\";\n }\n\n const sections: string[] = [];\n for (const path of sources) {\n if (contents[path]) {\n sections.push(`${path}\\n${contents[path]}`);\n }\n }\n\n if (sections.length === 0) {\n return \"(No memory loaded)\";\n }\n\n return sections.join(\"\\n\\n\");\n}\n\n/**\n * Load memory content from a backend path.\n *\n * @param backend - Backend to load from.\n * @param path - Path to the AGENTS.md file.\n * @returns File content if found, null otherwise.\n */\nasync function loadMemoryFromBackend(\n backend: BackendProtocol,\n path: string,\n): Promise<string | null> {\n // Use downloadFiles if available, otherwise fall back to read\n if (!backend.downloadFiles) {\n const content = await backend.read(path);\n if (content.startsWith(\"Error:\")) {\n return null;\n }\n return content;\n }\n\n const results = await backend.downloadFiles([path]);\n\n // Should get exactly one response for one path\n if (results.length !== 1) {\n throw new Error(\n `Expected 1 response for path ${path}, got ${results.length}`,\n );\n }\n const response = results[0];\n\n if (response.error != null) {\n // For now, memory files are treated as optional. file_not_found is expected\n // and we skip silently to allow graceful degradation.\n if (response.error === \"file_not_found\") {\n return null;\n }\n // Other errors should be raised\n throw new Error(`Failed to download ${path}: ${response.error}`);\n }\n\n if (response.content != null) {\n // Content is a Uint8Array, decode to string\n return new TextDecoder().decode(response.content);\n }\n\n return null;\n}\n\n/**\n * Create middleware for loading agent memory from AGENTS.md files.\n *\n * Loads memory content from configured sources and injects into the system prompt.\n * Supports multiple sources that are combined together.\n *\n * @param options - Configuration options\n * @returns AgentMiddleware for memory loading and injection\n *\n * @example\n * ```typescript\n * const middleware = createMemoryMiddleware({\n * backend: new FilesystemBackend({ rootDir: \"/\" }),\n * sources: [\n * \"~/.deepagents/AGENTS.md\",\n * \"./.deepagents/AGENTS.md\",\n * ],\n * });\n * ```\n */\nexport function createMemoryMiddleware(options: MemoryMiddlewareOptions) {\n const { backend, sources } = options;\n\n /**\n * Resolve backend from instance or factory.\n */\n function getBackend(state: unknown): BackendProtocol {\n if (typeof backend === \"function\") {\n // It's a factory - call it with state\n return backend({ state }) as BackendProtocol;\n }\n return backend;\n }\n\n return createMiddleware({\n name: \"MemoryMiddleware\",\n stateSchema: MemoryStateSchema,\n\n async beforeAgent(state) {\n // Skip if already loaded\n if (\"memoryContents\" in state && state.memoryContents != null) {\n return undefined;\n }\n\n const resolvedBackend = getBackend(state);\n const contents: Record<string, string> = {};\n\n for (const path of sources) {\n try {\n const content = await loadMemoryFromBackend(resolvedBackend, path);\n if (content) {\n contents[path] = content;\n }\n } catch (error) {\n // Log but continue - memory is optional\n // eslint-disable-next-line no-console\n console.debug(`Failed to load memory from ${path}:`, error);\n }\n }\n\n return { memoryContents: contents };\n },\n\n wrapModelCall(request, handler) {\n // Get memory contents from state\n const memoryContents: Record<string, string> =\n request.state?.memoryContents || {};\n\n // Format memory section\n const formattedContents = formatMemoryContents(memoryContents, sources);\n\n const memorySection = MEMORY_SYSTEM_PROMPT.replace(\n \"{memory_contents}\",\n formattedContents,\n );\n\n // Prepend memory section to system prompt\n const currentSystemPrompt = request.systemPrompt || \"\";\n const newSystemPrompt = currentSystemPrompt\n ? `${memorySection}\\n\\n${currentSystemPrompt}`\n : memorySection;\n\n return handler({ ...request, systemPrompt: newSystemPrompt });\n },\n });\n}\n","/* eslint-disable no-console */\n/**\n * Backend-agnostic skills middleware for loading agent skills from any backend.\n *\n * This middleware implements Anthropic's agent skills pattern with progressive disclosure,\n * loading skills from backend storage via configurable sources.\n *\n * ## Architecture\n *\n * Skills are loaded from one or more **sources** - paths in a backend where skills are\n * organized. Sources are loaded in order, with later sources overriding earlier ones\n * when skills have the same name (last one wins). This enables layering: base -> user\n * -> project -> team skills.\n *\n * The middleware uses backend APIs exclusively (no direct filesystem access), making it\n * portable across different storage backends (filesystem, state, remote storage, etc.).\n *\n * ## Usage\n *\n * ```typescript\n * import { createSkillsMiddleware, FilesystemBackend } from \"@anthropic/deepagents\";\n *\n * const middleware = createSkillsMiddleware({\n * backend: new FilesystemBackend({ rootDir: \"/\" }),\n * sources: [\n * \"/skills/user/\",\n * \"/skills/project/\",\n * ],\n * });\n *\n * const agent = createDeepAgent({ middleware: [middleware] });\n * ```\n *\n * Or use the `skills` parameter on createDeepAgent:\n *\n * ```typescript\n * const agent = createDeepAgent({\n * skills: [\"/skills/user/\", \"/skills/project/\"],\n * });\n * ```\n */\n\nimport { z } from \"zod\";\nimport yaml from \"yaml\";\nimport {\n createMiddleware,\n /**\n * required for type inference\n */\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { StateSchema, ReducedValue } from \"@langchain/langgraph\";\n\nimport type { BackendProtocol, BackendFactory } from \"../backends/protocol.js\";\nimport type { StateBackend } from \"../backends/state.js\";\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\n// Security: Maximum size for SKILL.md files to prevent DoS attacks (10MB)\nexport const MAX_SKILL_FILE_SIZE = 10 * 1024 * 1024;\n\n// Agent Skills specification constraints (https://agentskills.io/specification)\nexport const MAX_SKILL_NAME_LENGTH = 64;\nexport const MAX_SKILL_DESCRIPTION_LENGTH = 1024;\n\n/**\n * Metadata for a skill per Agent Skills specification.\n */\nexport interface SkillMetadata {\n /** Skill identifier (max 64 chars, lowercase alphanumeric and hyphens) */\n name: string;\n\n /** What the skill does (max 1024 chars) */\n description: string;\n\n /** Path to the SKILL.md file in the backend */\n path: string;\n\n /** License name or reference to bundled license file */\n license?: string | null;\n\n /** Environment requirements (max 500 chars) */\n compatibility?: string | null;\n\n /** Arbitrary key-value mapping for additional metadata */\n metadata?: Record<string, string>;\n\n /** List of pre-approved tools (experimental) */\n allowedTools?: string[];\n}\n\n/**\n * Options for the skills middleware.\n */\nexport interface SkillsMiddlewareOptions {\n /**\n * Backend instance or factory function for file operations.\n * Use a factory for StateBackend since it requires runtime state.\n */\n backend:\n | BackendProtocol\n | BackendFactory\n | ((config: { state: unknown; store?: BaseStore }) => StateBackend);\n\n /**\n * List of skill source paths to load (e.g., [\"/skills/user/\", \"/skills/project/\"]).\n * Paths must use POSIX conventions (forward slashes).\n * Later sources override earlier ones for skills with the same name (last one wins).\n */\n sources: string[];\n}\n\n/**\n * Zod schema for a single skill metadata entry.\n */\nexport const SkillMetadataEntrySchema = z.object({\n name: z.string(),\n description: z.string(),\n path: z.string(),\n license: z.string().nullable().optional(),\n compatibility: z.string().nullable().optional(),\n metadata: z.record(z.string(), z.string()).optional(),\n allowedTools: z.array(z.string()).optional(),\n});\n\n/**\n * Type for a single skill metadata entry.\n */\nexport type SkillMetadataEntry = z.infer<typeof SkillMetadataEntrySchema>;\n\n/**\n * Reducer for skillsMetadata that merges arrays from parallel subagents.\n * Skills are deduplicated by name, with later values overriding earlier ones.\n *\n * @param current - The current skillsMetadata array (from state)\n * @param update - The new skillsMetadata array (from a subagent update)\n * @returns Merged array with duplicates resolved by name (later values win)\n */\nexport function skillsMetadataReducer(\n current: SkillMetadataEntry[] | undefined,\n update: SkillMetadataEntry[] | undefined,\n): SkillMetadataEntry[] {\n // If no update, return current (or empty array)\n if (!update || update.length === 0) {\n return current || [];\n }\n // If no current, return update\n if (!current || current.length === 0) {\n return update;\n }\n // Merge by skill name (later values override earlier ones)\n const merged = new Map<string, SkillMetadataEntry>();\n for (const skill of current) {\n merged.set(skill.name, skill);\n }\n for (const skill of update) {\n merged.set(skill.name, skill);\n }\n return Array.from(merged.values());\n}\n\n/**\n * State schema for skills middleware.\n * Uses ReducedValue for skillsMetadata to allow concurrent updates from parallel subagents.\n */\nconst SkillsStateSchema = new StateSchema({\n skillsMetadata: new ReducedValue(\n z.array(SkillMetadataEntrySchema).default(() => []),\n {\n inputSchema: z.array(SkillMetadataEntrySchema).optional(),\n reducer: skillsMetadataReducer,\n },\n ),\n});\n\n/**\n * Skills System Documentation prompt template.\n */\nconst SKILLS_SYSTEM_PROMPT = `\n## Skills System\n\nYou have access to a skills library that provides specialized capabilities and domain knowledge.\n\n{skills_locations}\n\n**Available Skills:**\n\n{skills_list}\n\n**How to Use Skills (Progressive Disclosure):**\n\nSkills follow a **progressive disclosure** pattern - you know they exist (name + description above), but you only read the full instructions when needed:\n\n1. **Recognize when a skill applies**: Check if the user's task matches any skill's description\n2. **Read the skill's full instructions**: The skill list above shows the exact path to use with read_file\n3. **Follow the skill's instructions**: SKILL.md contains step-by-step workflows, best practices, and examples\n4. **Access supporting files**: Skills may include Python scripts, configs, or reference docs - use absolute paths\n\n**When to Use Skills:**\n- When the user's request matches a skill's domain (e.g., \"research X\" → web-research skill)\n- When you need specialized knowledge or structured workflows\n- When a skill provides proven patterns for complex tasks\n\n**Skills are Self-Documenting:**\n- Each SKILL.md tells you exactly what the skill does and how to use it\n- The skill list above shows the full path for each skill's SKILL.md file\n\n**Executing Skill Scripts:**\nSkills may contain Python scripts or other executable files. Always use absolute paths from the skill list.\n\n**Example Workflow:**\n\nUser: \"Can you research the latest developments in quantum computing?\"\n\n1. Check available skills above → See \"web-research\" skill with its full path\n2. Read the skill using the path shown in the list\n3. Follow the skill's research workflow (search → organize → synthesize)\n4. Use any helper scripts with absolute paths\n\nRemember: Skills are tools to make you more capable and consistent. When in doubt, check if a skill exists for the task!\n`;\n\n/**\n * Validate skill name per Agent Skills specification.\n */\nfunction validateSkillName(\n name: string,\n directoryName: string,\n): { valid: boolean; error: string } {\n if (!name) {\n return { valid: false, error: \"name is required\" };\n }\n if (name.length > MAX_SKILL_NAME_LENGTH) {\n return { valid: false, error: \"name exceeds 64 characters\" };\n }\n // Pattern: lowercase alphanumeric, single hyphens between segments, no start/end hyphen\n if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(name)) {\n return {\n valid: false,\n error: \"name must be lowercase alphanumeric with single hyphens only\",\n };\n }\n if (name !== directoryName) {\n return {\n valid: false,\n error: `name '${name}' must match directory name '${directoryName}'`,\n };\n }\n return { valid: true, error: \"\" };\n}\n\n/**\n * Parse YAML frontmatter from SKILL.md content.\n */\nfunction parseSkillMetadataFromContent(\n content: string,\n skillPath: string,\n directoryName: string,\n): SkillMetadata | null {\n if (content.length > MAX_SKILL_FILE_SIZE) {\n console.warn(\n `Skipping ${skillPath}: content too large (${content.length} bytes)`,\n );\n return null;\n }\n\n // Match YAML frontmatter between --- delimiters\n const frontmatterPattern = /^---\\s*\\n([\\s\\S]*?)\\n---\\s*\\n/;\n const match = content.match(frontmatterPattern);\n\n if (!match) {\n console.warn(`Skipping ${skillPath}: no valid YAML frontmatter found`);\n return null;\n }\n\n const frontmatterStr = match[1];\n\n // Parse YAML\n let frontmatterData: Record<string, unknown>;\n try {\n frontmatterData = yaml.parse(frontmatterStr);\n } catch (e) {\n console.warn(`Invalid YAML in ${skillPath}:`, e);\n return null;\n }\n\n if (!frontmatterData || typeof frontmatterData !== \"object\") {\n console.warn(`Skipping ${skillPath}: frontmatter is not a mapping`);\n return null;\n }\n\n // Validate required fields\n const name = frontmatterData.name as string | undefined;\n const description = frontmatterData.description as string | undefined;\n\n if (!name || !description) {\n console.warn(\n `Skipping ${skillPath}: missing required 'name' or 'description'`,\n );\n return null;\n }\n\n // Validate name format per spec (warn but continue for backwards compatibility)\n const validation = validateSkillName(String(name), directoryName);\n if (!validation.valid) {\n console.warn(\n `Skill '${name}' in ${skillPath} does not follow Agent Skills specification: ${validation.error}. Consider renaming for spec compliance.`,\n );\n }\n\n // Validate description length per spec (max 1024 chars)\n let descriptionStr = String(description).trim();\n if (descriptionStr.length > MAX_SKILL_DESCRIPTION_LENGTH) {\n console.warn(\n `Description exceeds ${MAX_SKILL_DESCRIPTION_LENGTH} characters in ${skillPath}, truncating`,\n );\n descriptionStr = descriptionStr.slice(0, MAX_SKILL_DESCRIPTION_LENGTH);\n }\n\n // Parse allowed-tools\n const allowedToolsStr = frontmatterData[\"allowed-tools\"] as\n | string\n | undefined;\n const allowedTools = allowedToolsStr ? allowedToolsStr.split(\" \") : [];\n\n return {\n name: String(name),\n description: descriptionStr,\n path: skillPath,\n metadata: (frontmatterData.metadata as Record<string, string>) || {},\n license:\n typeof frontmatterData.license === \"string\"\n ? frontmatterData.license.trim() || null\n : null,\n compatibility:\n typeof frontmatterData.compatibility === \"string\"\n ? frontmatterData.compatibility.trim() || null\n : null,\n allowedTools,\n };\n}\n\n/**\n * List all skills from a backend source.\n */\nasync function listSkillsFromBackend(\n backend: BackendProtocol,\n sourcePath: string,\n): Promise<SkillMetadata[]> {\n const skills: SkillMetadata[] = [];\n\n // Normalize path to ensure it ends with / (handle both Unix and Windows paths)\n const normalizedPath =\n sourcePath.endsWith(\"/\") || sourcePath.endsWith(\"\\\\\")\n ? sourcePath\n : `${sourcePath}/`;\n\n // List directories in the source path using lsInfo\n let fileInfos: { path: string; is_dir?: boolean }[];\n try {\n fileInfos = await backend.lsInfo(normalizedPath);\n } catch {\n // Source path doesn't exist or can't be listed\n return [];\n }\n\n // Convert FileInfo[] to entries format\n // Handle both forward slashes (Unix) and backslashes (Windows) in paths\n const entries = fileInfos.map((info) => ({\n name:\n info.path\n .replace(/[/\\\\]$/, \"\") // Remove trailing slash or backslash\n .split(/[/\\\\]/) // Split on either separator\n .pop() || \"\",\n type: (info.is_dir ? \"directory\" : \"file\") as \"file\" | \"directory\",\n }));\n\n // Look for subdirectories containing SKILL.md\n for (const entry of entries) {\n if (entry.type !== \"directory\") {\n continue;\n }\n\n const skillMdPath = `${normalizedPath}${entry.name}/SKILL.md`;\n\n // Try to download the SKILL.md file\n let content: string;\n if (backend.downloadFiles) {\n const results = await backend.downloadFiles([skillMdPath]);\n if (results.length !== 1) {\n continue;\n }\n\n const response = results[0];\n if (response.error != null || response.content == null) {\n continue;\n }\n\n // Decode content\n content = new TextDecoder().decode(response.content);\n } else {\n // Fall back to read if downloadFiles is not available\n const readResult = await backend.read(skillMdPath);\n if (readResult.startsWith(\"Error:\")) {\n continue;\n }\n content = readResult;\n }\n const metadata = parseSkillMetadataFromContent(\n content,\n skillMdPath,\n entry.name,\n );\n\n if (metadata) {\n skills.push(metadata);\n }\n }\n\n return skills;\n}\n\n/**\n * Format skills locations for display in system prompt.\n * Shows priority indicator for the last source (highest priority).\n */\nfunction formatSkillsLocations(sources: string[]): string {\n if (sources.length === 0) {\n return \"**Skills Sources:** None configured\";\n }\n\n const lines: string[] = [];\n for (let i = 0; i < sources.length; i++) {\n const sourcePath = sources[i];\n // Extract a friendly name from the path (last non-empty component)\n // Handle both Unix (/) and Windows (\\) path separators\n const name =\n sourcePath\n .replace(/[/\\\\]$/, \"\")\n .split(/[/\\\\]/)\n .filter(Boolean)\n .pop()\n ?.replace(/^./, (c) => c.toUpperCase()) || \"Skills\";\n const suffix = i === sources.length - 1 ? \" (higher priority)\" : \"\";\n lines.push(`**${name} Skills**: \\`${sourcePath}\\`${suffix}`);\n }\n return lines.join(\"\\n\");\n}\n\n/**\n * Format skills metadata for display in system prompt.\n * Shows allowed tools for each skill if specified.\n */\nfunction formatSkillsList(skills: SkillMetadata[], sources: string[]): string {\n if (skills.length === 0) {\n const paths = sources.map((s) => `\\`${s}\\``).join(\" or \");\n return `(No skills available yet. You can create skills in ${paths})`;\n }\n\n const lines: string[] = [];\n for (const skill of skills) {\n lines.push(`- **${skill.name}**: ${skill.description}`);\n if (skill.allowedTools && skill.allowedTools.length > 0) {\n lines.push(` → Allowed tools: ${skill.allowedTools.join(\", \")}`);\n }\n lines.push(` → Read \\`${skill.path}\\` for full instructions`);\n }\n\n return lines.join(\"\\n\");\n}\n\n/**\n * Create backend-agnostic middleware for loading and exposing agent skills.\n *\n * This middleware loads skills from configurable backend sources and injects\n * skill metadata into the system prompt. It implements the progressive disclosure\n * pattern: skill names and descriptions are shown in the prompt, but the agent\n * reads full SKILL.md content only when needed.\n *\n * @param options - Configuration options\n * @returns AgentMiddleware for skills loading and injection\n *\n * @example\n * ```typescript\n * const middleware = createSkillsMiddleware({\n * backend: new FilesystemBackend({ rootDir: \"/\" }),\n * sources: [\"/skills/user/\", \"/skills/project/\"],\n * });\n * ```\n */\nexport function createSkillsMiddleware(options: SkillsMiddlewareOptions) {\n const { backend, sources } = options;\n\n // Closure variable to store loaded skills - wrapModelCall can access this\n // directly since beforeAgent state updates aren't immediately available\n let loadedSkills: SkillMetadata[] = [];\n\n /**\n * Resolve backend from instance or factory.\n */\n function getBackend(state: unknown): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend({ state }) as BackendProtocol;\n }\n return backend;\n }\n\n return createMiddleware({\n name: \"SkillsMiddleware\",\n stateSchema: SkillsStateSchema,\n\n async beforeAgent(state) {\n // Skip if already loaded (check both closure and state)\n if (loadedSkills.length > 0) {\n return undefined;\n }\n if (\"skillsMetadata\" in state && state.skillsMetadata != null) {\n // Restore from state (e.g., after checkpoint restore)\n loadedSkills = state.skillsMetadata as SkillMetadata[];\n return undefined;\n }\n\n const resolvedBackend = getBackend(state);\n const allSkills: Map<string, SkillMetadata> = new Map();\n\n // Load skills from each source in order (later sources override earlier)\n for (const sourcePath of sources) {\n try {\n const skills = await listSkillsFromBackend(\n resolvedBackend,\n sourcePath,\n );\n for (const skill of skills) {\n allSkills.set(skill.name, skill);\n }\n } catch (error) {\n // Log but continue - individual source failures shouldn't break everything\n console.debug(\n `[BackendSkillsMiddleware] Failed to load skills from ${sourcePath}:`,\n error,\n );\n }\n }\n\n // Store in closure for immediate access by wrapModelCall\n loadedSkills = Array.from(allSkills.values());\n\n return { skillsMetadata: loadedSkills };\n },\n\n wrapModelCall(request, handler) {\n // Use closure variable which is populated by beforeAgent\n // Fall back to state for checkpoint restore scenarios\n const skillsMetadata: SkillMetadata[] =\n loadedSkills.length > 0\n ? loadedSkills\n : (request.state?.skillsMetadata as SkillMetadata[]) || [];\n\n // Format skills section\n const skillsLocations = formatSkillsLocations(sources);\n const skillsList = formatSkillsList(skillsMetadata, sources);\n\n const skillsSection = SKILLS_SYSTEM_PROMPT.replace(\n \"{skills_locations}\",\n skillsLocations,\n ).replace(\"{skills_list}\", skillsList);\n\n // Append to existing system prompt\n const currentSystemPrompt = request.systemPrompt || \"\";\n const newSystemPrompt = currentSystemPrompt\n ? `${currentSystemPrompt}\\n\\n${skillsSection}`\n : skillsSection;\n\n return handler({ ...request, systemPrompt: newSystemPrompt });\n },\n });\n}\n","/**\n * Utility functions for middleware.\n *\n * This module provides shared helpers used across middleware implementations.\n */\n\nimport { SystemMessage } from \"@langchain/core/messages\";\n\n/**\n * Append text to a system message.\n *\n * Creates a new SystemMessage with the text appended to the existing content.\n * If the original message has content, the new text is separated by two newlines.\n *\n * @param systemMessage - Existing system message or null/undefined.\n * @param text - Text to add to the system message.\n * @returns New SystemMessage with the text appended.\n *\n * @example\n * ```typescript\n * const original = new SystemMessage({ content: \"You are a helpful assistant.\" });\n * const updated = appendToSystemMessage(original, \"Always be concise.\");\n * // Result: SystemMessage with content \"You are a helpful assistant.\\n\\nAlways be concise.\"\n * ```\n */\nexport function appendToSystemMessage(\n systemMessage: SystemMessage | null | undefined,\n text: string,\n): SystemMessage {\n if (!systemMessage) {\n return new SystemMessage({ content: text });\n }\n\n // Handle both string and array content formats\n const existingContent = systemMessage.content;\n\n if (typeof existingContent === \"string\") {\n const newContent = existingContent ? `${existingContent}\\n\\n${text}` : text;\n return new SystemMessage({ content: newContent });\n }\n\n // For array content (content blocks), append as a new text block\n if (Array.isArray(existingContent)) {\n const newContent = [...existingContent];\n const textToAdd = newContent.length > 0 ? `\\n\\n${text}` : text;\n newContent.push({ type: \"text\", text: textToAdd });\n return new SystemMessage({ content: newContent });\n }\n\n // Fallback for unknown content type\n return new SystemMessage({ content: text });\n}\n\n/**\n * Prepend text to a system message.\n *\n * Creates a new SystemMessage with the text prepended to the existing content.\n * If the original message has content, the new text is separated by two newlines.\n *\n * @param systemMessage - Existing system message or null/undefined.\n * @param text - Text to prepend to the system message.\n * @returns New SystemMessage with the text prepended.\n *\n * @example\n * ```typescript\n * const original = new SystemMessage({ content: \"Always be concise.\" });\n * const updated = prependToSystemMessage(original, \"You are a helpful assistant.\");\n * // Result: SystemMessage with content \"You are a helpful assistant.\\n\\nAlways be concise.\"\n * ```\n */\nexport function prependToSystemMessage(\n systemMessage: SystemMessage | null | undefined,\n text: string,\n): SystemMessage {\n if (!systemMessage) {\n return new SystemMessage({ content: text });\n }\n\n // Handle both string and array content formats\n const existingContent = systemMessage.content;\n\n if (typeof existingContent === \"string\") {\n const newContent = existingContent ? `${text}\\n\\n${existingContent}` : text;\n return new SystemMessage({ content: newContent });\n }\n\n // For array content (content blocks), prepend as a new text block\n if (Array.isArray(existingContent)) {\n const textToAdd = existingContent.length > 0 ? `${text}\\n\\n` : text;\n const newContent = [{ type: \"text\", text: textToAdd }, ...existingContent];\n return new SystemMessage({ content: newContent });\n }\n\n // Fallback for unknown content type\n return new SystemMessage({ content: text });\n}\n","/* eslint-disable no-console */\n/**\n * Summarization middleware with backend support for conversation history offloading.\n *\n * This module extends the base LangChain summarization middleware with additional\n * backend-based features for persisting conversation history before summarization.\n *\n * ## Usage\n *\n * ```typescript\n * import { createSummarizationMiddleware } from \"@anthropic/deepagents\";\n * import { FilesystemBackend } from \"@anthropic/deepagents\";\n *\n * const backend = new FilesystemBackend({ rootDir: \"/data\" });\n *\n * const middleware = createSummarizationMiddleware({\n * model: \"gpt-4o-mini\",\n * backend,\n * trigger: { type: \"fraction\", value: 0.85 },\n * keep: { type: \"fraction\", value: 0.10 },\n * });\n *\n * const agent = createDeepAgent({ middleware: [middleware] });\n * ```\n *\n * ## Storage\n *\n * Offloaded messages are stored as markdown at `/conversation_history/{thread_id}.md`.\n *\n * Each summarization event appends a new section to this file, creating a running log\n * of all evicted messages.\n *\n * ## Relationship to LangChain Summarization Middleware\n *\n * The base `summarizationMiddleware` from `langchain` provides core summarization\n * functionality. This middleware adds:\n * - Backend-based conversation history offloading\n * - Tool argument truncation for old messages\n *\n * For simple use cases without backend offloading, use `summarizationMiddleware`\n * from `langchain` directly.\n */\n\nimport { z } from \"zod\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport {\n createMiddleware,\n countTokensApproximately,\n HumanMessage,\n AIMessage,\n BaseMessage,\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\nimport { getBufferString } from \"@langchain/core/messages\";\nimport type { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport { ChatOpenAI } from \"@langchain/openai\";\n\nimport type { BackendProtocol, BackendFactory } from \"../backends/protocol.js\";\nimport type { StateBackend } from \"../backends/state.js\";\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\n// Re-export the base summarization middleware from langchain for users who don't need backend offloading\nexport { summarizationMiddleware } from \"langchain\";\n\n/**\n * Context size specification for summarization triggers and retention policies.\n */\nexport interface ContextSize {\n /** Type of context measurement */\n type: \"messages\" | \"tokens\" | \"fraction\";\n /** Threshold value */\n value: number;\n}\n\n/**\n * Settings for truncating large tool arguments in old messages.\n */\nexport interface TruncateArgsSettings {\n /**\n * Threshold to trigger argument truncation.\n * If not provided, truncation is disabled.\n */\n trigger?: ContextSize;\n\n /**\n * Context retention policy for message truncation.\n * Defaults to keeping last 20 messages.\n */\n keep?: ContextSize;\n\n /**\n * Maximum character length for tool arguments before truncation.\n * Defaults to 2000.\n */\n maxLength?: number;\n\n /**\n * Text to replace truncated arguments with.\n * Defaults to \"...(argument truncated)\".\n */\n truncationText?: string;\n}\n\n/**\n * Options for the summarization middleware.\n */\nexport interface SummarizationMiddlewareOptions {\n /**\n * The language model to use for generating summaries.\n * Can be a model string (e.g., \"gpt-4o-mini\") or a BaseChatModel instance.\n */\n model: string | BaseChatModel;\n\n /**\n * Backend instance or factory for persisting conversation history.\n */\n backend:\n | BackendProtocol\n | BackendFactory\n | ((config: { state: unknown; store?: BaseStore }) => StateBackend);\n\n /**\n * Threshold(s) that trigger summarization.\n * Can be a single ContextSize or an array for multiple triggers.\n */\n trigger?: ContextSize | ContextSize[];\n\n /**\n * Context retention policy after summarization.\n * Defaults to keeping last 20 messages.\n */\n keep?: ContextSize;\n\n /**\n * Prompt template for generating summaries.\n */\n summaryPrompt?: string;\n\n /**\n * Max tokens to include when generating summary.\n * Defaults to 4000.\n */\n trimTokensToSummarize?: number;\n\n /**\n * Path prefix for storing conversation history.\n * Defaults to \"/conversation_history\".\n */\n historyPathPrefix?: string;\n\n /**\n * Settings for truncating large tool arguments in old messages.\n * If not provided, argument truncation is disabled.\n */\n truncateArgsSettings?: TruncateArgsSettings;\n}\n\n// Default values\nconst DEFAULT_MESSAGES_TO_KEEP = 20;\nconst DEFAULT_TRIM_TOKEN_LIMIT = 4000;\nconst DEFAULT_SUMMARY_PROMPT = `You are a conversation summarizer. Your task is to create a concise summary of the conversation that captures:\n1. The main topics discussed\n2. Key decisions or conclusions reached\n3. Any important context that would be needed for continuing the conversation\n\nKeep the summary focused and informative. Do not include unnecessary details.\n\nConversation to summarize:\n{conversation}\n\nSummary:`;\n\n/**\n * State schema for summarization middleware.\n */\nconst SummarizationStateSchema = z.object({\n /** Session ID for history file naming */\n _summarizationSessionId: z.string().optional(),\n});\n\n/**\n * Check if a message is a previous summarization message.\n * Summary messages are HumanMessage objects with lc_source='summarization' in additional_kwargs.\n */\nfunction isSummaryMessage(msg: BaseMessage): boolean {\n if (!HumanMessage.isInstance(msg)) {\n return false;\n }\n return msg.additional_kwargs?.lc_source === \"summarization\";\n}\n\n/**\n * Create summarization middleware with backend support for conversation history offloading.\n *\n * This middleware:\n * 1. Monitors conversation length against configured thresholds\n * 2. When triggered, offloads old messages to backend storage\n * 3. Generates a summary of offloaded messages\n * 4. Replaces old messages with the summary, preserving recent context\n *\n * @param options - Configuration options\n * @returns AgentMiddleware for summarization and history offloading\n */\nexport function createSummarizationMiddleware(\n options: SummarizationMiddlewareOptions,\n) {\n const {\n model,\n backend,\n trigger,\n keep = { type: \"messages\", value: DEFAULT_MESSAGES_TO_KEEP },\n summaryPrompt = DEFAULT_SUMMARY_PROMPT,\n trimTokensToSummarize = DEFAULT_TRIM_TOKEN_LIMIT,\n historyPathPrefix = \"/conversation_history\",\n truncateArgsSettings,\n } = options;\n\n // Parse truncate settings\n const truncateTrigger = truncateArgsSettings?.trigger;\n const truncateKeep = truncateArgsSettings?.keep || {\n type: \"messages\" as const,\n value: 20,\n };\n const maxArgLength = truncateArgsSettings?.maxLength || 2000;\n const truncationText =\n truncateArgsSettings?.truncationText || \"...(argument truncated)\";\n\n // Session ID for this middleware instance (fallback if no thread_id)\n let sessionId: string | null = null;\n\n /**\n * Resolve backend from instance or factory.\n */\n function getBackend(state: unknown): BackendProtocol {\n if (typeof backend === \"function\") {\n return backend({ state }) as BackendProtocol;\n }\n return backend;\n }\n\n /**\n * Get or create session ID for history file naming.\n */\n function getSessionId(state: Record<string, unknown>): string {\n if (state._summarizationSessionId) {\n return state._summarizationSessionId as string;\n }\n if (!sessionId) {\n sessionId = `session_${uuidv4().substring(0, 8)}`;\n }\n return sessionId;\n }\n\n /**\n * Get the history file path.\n */\n function getHistoryPath(state: Record<string, unknown>): string {\n const id = getSessionId(state);\n return `${historyPathPrefix}/${id}.md`;\n }\n\n /**\n * Resolve the chat model.\n */\n function getChatModel(): BaseChatModel {\n if (typeof model === \"string\") {\n return new ChatOpenAI({ modelName: model });\n }\n return model;\n }\n\n /**\n * Check if summarization should be triggered.\n */\n function shouldSummarize(\n messages: BaseMessage[],\n totalTokens: number,\n maxInputTokens?: number,\n ): boolean {\n if (!trigger) {\n return false;\n }\n\n const triggers = Array.isArray(trigger) ? trigger : [trigger];\n\n for (const t of triggers) {\n if (t.type === \"messages\" && messages.length >= t.value) {\n return true;\n }\n if (t.type === \"tokens\" && totalTokens >= t.value) {\n return true;\n }\n if (t.type === \"fraction\" && maxInputTokens) {\n const threshold = Math.floor(maxInputTokens * t.value);\n if (totalTokens >= threshold) {\n return true;\n }\n }\n }\n\n return false;\n }\n\n /**\n * Determine cutoff index for messages to summarize.\n * Messages at index < cutoff will be summarized.\n * Messages at index >= cutoff will be preserved.\n */\n function determineCutoffIndex(\n messages: BaseMessage[],\n maxInputTokens?: number,\n ): number {\n if (keep.type === \"messages\") {\n if (messages.length <= keep.value) {\n return 0;\n }\n return messages.length - keep.value;\n }\n\n if (keep.type === \"tokens\" || keep.type === \"fraction\") {\n const targetTokenCount =\n keep.type === \"fraction\" && maxInputTokens\n ? Math.floor(maxInputTokens * keep.value)\n : keep.value;\n\n let tokensKept = 0;\n for (let i = messages.length - 1; i >= 0; i--) {\n const msgTokens = countTokensApproximately([messages[i]]);\n if (tokensKept + msgTokens > targetTokenCount) {\n return i + 1;\n }\n tokensKept += msgTokens;\n }\n return 0;\n }\n\n return 0;\n }\n\n /**\n * Check if argument truncation should be triggered.\n */\n function shouldTruncateArgs(\n messages: BaseMessage[],\n totalTokens: number,\n maxInputTokens?: number,\n ): boolean {\n if (!truncateTrigger) {\n return false;\n }\n\n if (truncateTrigger.type === \"messages\") {\n return messages.length >= truncateTrigger.value;\n }\n if (truncateTrigger.type === \"tokens\") {\n return totalTokens >= truncateTrigger.value;\n }\n if (truncateTrigger.type === \"fraction\" && maxInputTokens) {\n const threshold = Math.floor(maxInputTokens * truncateTrigger.value);\n return totalTokens >= threshold;\n }\n\n return false;\n }\n\n /**\n * Determine cutoff index for argument truncation.\n */\n function determineTruncateCutoffIndex(\n messages: BaseMessage[],\n maxInputTokens?: number,\n ): number {\n if (truncateKeep.type === \"messages\") {\n if (messages.length <= truncateKeep.value) {\n return messages.length;\n }\n return messages.length - truncateKeep.value;\n }\n\n if (truncateKeep.type === \"tokens\" || truncateKeep.type === \"fraction\") {\n const targetTokenCount =\n truncateKeep.type === \"fraction\" && maxInputTokens\n ? Math.floor(maxInputTokens * truncateKeep.value)\n : truncateKeep.value;\n\n let tokensKept = 0;\n for (let i = messages.length - 1; i >= 0; i--) {\n const msgTokens = countTokensApproximately([messages[i]]);\n if (tokensKept + msgTokens > targetTokenCount) {\n return i + 1;\n }\n tokensKept += msgTokens;\n }\n return 0;\n }\n\n return messages.length;\n }\n\n /**\n * Truncate large tool arguments in old messages.\n */\n function truncateArgs(\n messages: BaseMessage[],\n maxInputTokens?: number,\n ): { messages: BaseMessage[]; modified: boolean } {\n const totalTokens = countTokensApproximately(messages);\n if (!shouldTruncateArgs(messages, totalTokens, maxInputTokens)) {\n return { messages, modified: false };\n }\n\n const cutoffIndex = determineTruncateCutoffIndex(messages, maxInputTokens);\n if (cutoffIndex >= messages.length) {\n return { messages, modified: false };\n }\n\n const truncatedMessages: BaseMessage[] = [];\n let modified = false;\n\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n\n if (i < cutoffIndex && AIMessage.isInstance(msg) && msg.tool_calls) {\n const truncatedToolCalls = msg.tool_calls.map((toolCall) => {\n const args = toolCall.args || {};\n const truncatedArgs: Record<string, unknown> = {};\n let toolModified = false;\n\n for (const [key, value] of Object.entries(args)) {\n if (\n typeof value === \"string\" &&\n value.length > maxArgLength &&\n (toolCall.name === \"write_file\" || toolCall.name === \"edit_file\")\n ) {\n truncatedArgs[key] = value.substring(0, 20) + truncationText;\n toolModified = true;\n } else {\n truncatedArgs[key] = value;\n }\n }\n\n if (toolModified) {\n modified = true;\n return { ...toolCall, args: truncatedArgs };\n }\n return toolCall;\n });\n\n if (modified) {\n const truncatedMsg = new AIMessage({\n content: msg.content,\n tool_calls: truncatedToolCalls,\n additional_kwargs: msg.additional_kwargs,\n });\n truncatedMessages.push(truncatedMsg);\n } else {\n truncatedMessages.push(msg);\n }\n } else {\n truncatedMessages.push(msg);\n }\n }\n\n return { messages: truncatedMessages, modified };\n }\n\n /**\n * Filter out previous summary messages.\n */\n function filterSummaryMessages(messages: BaseMessage[]): BaseMessage[] {\n return messages.filter((msg) => !isSummaryMessage(msg));\n }\n\n /**\n * Offload messages to backend.\n */\n async function offloadToBackend(\n resolvedBackend: BackendProtocol,\n messages: BaseMessage[],\n state: Record<string, unknown>,\n ): Promise<string | null> {\n const path = getHistoryPath(state);\n const filteredMessages = filterSummaryMessages(messages);\n\n const timestamp = new Date().toISOString();\n const newSection = `## Summarized at ${timestamp}\\n\\n${getBufferString(filteredMessages)}\\n\\n`;\n\n // Read existing content\n let existingContent = \"\";\n try {\n if (resolvedBackend.downloadFiles) {\n const responses = await resolvedBackend.downloadFiles([path]);\n if (\n responses.length > 0 &&\n responses[0].content &&\n !responses[0].error\n ) {\n existingContent = new TextDecoder().decode(responses[0].content);\n }\n }\n } catch {\n // File doesn't exist yet, that's fine\n }\n\n const combinedContent = existingContent + newSection;\n\n try {\n let result;\n if (existingContent) {\n result = await resolvedBackend.edit(\n path,\n existingContent,\n combinedContent,\n );\n } else {\n result = await resolvedBackend.write(path, combinedContent);\n }\n\n if (result.error) {\n console.warn(\n `Failed to offload conversation history to ${path}: ${result.error}`,\n );\n return null;\n }\n\n return path;\n } catch (e) {\n console.warn(`Exception offloading conversation history to ${path}:`, e);\n return null;\n }\n }\n\n /**\n * Create summary of messages.\n */\n async function createSummary(messages: BaseMessage[]): Promise<string> {\n const chatModel = getChatModel();\n\n // Trim messages if too long\n let messagesToSummarize = messages;\n const tokens = countTokensApproximately(messages);\n if (tokens > trimTokensToSummarize) {\n // Keep only recent messages that fit\n let kept = 0;\n const trimmedMessages: BaseMessage[] = [];\n for (let i = messages.length - 1; i >= 0; i--) {\n const msgTokens = countTokensApproximately([messages[i]]);\n if (kept + msgTokens > trimTokensToSummarize) {\n break;\n }\n trimmedMessages.unshift(messages[i]);\n kept += msgTokens;\n }\n messagesToSummarize = trimmedMessages;\n }\n\n const conversation = getBufferString(messagesToSummarize);\n const prompt = summaryPrompt.replace(\"{conversation}\", conversation);\n\n const response = await chatModel.invoke([\n new HumanMessage({ content: prompt }),\n ]);\n\n return typeof response.content === \"string\"\n ? response.content\n : JSON.stringify(response.content);\n }\n\n /**\n * Build the summary message with file path reference.\n */\n function buildSummaryMessage(\n summary: string,\n filePath: string | null,\n ): HumanMessage {\n let content: string;\n if (filePath) {\n content = `You are in the middle of a conversation that has been summarized.\n\nThe full conversation history has been saved to ${filePath} should you need to refer back to it for details.\n\nA condensed summary follows:\n\n<summary>\n${summary}\n</summary>`;\n } else {\n content = `Here is a summary of the conversation to date:\\n\\n${summary}`;\n }\n\n return new HumanMessage({\n content,\n additional_kwargs: { lc_source: \"summarization\" },\n });\n }\n\n return createMiddleware({\n name: \"SummarizationMiddleware\",\n stateSchema: SummarizationStateSchema,\n\n async beforeModel(state) {\n const messages = state.messages ?? [];\n\n if (messages.length === 0) {\n return undefined;\n }\n\n // Step 1: Truncate args if configured\n const { messages: truncatedMessages, modified: argsWereTruncated } =\n truncateArgs(messages);\n\n // Step 2: Check if summarization should happen\n const totalTokens = countTokensApproximately(truncatedMessages);\n const shouldDoSummarization = shouldSummarize(\n truncatedMessages,\n totalTokens,\n );\n\n // If only truncation happened (no summarization)\n if (argsWereTruncated && !shouldDoSummarization) {\n return { messages: truncatedMessages };\n }\n\n // If no truncation and no summarization\n if (!shouldDoSummarization) {\n return undefined;\n }\n\n // Step 3: Perform summarization\n const cutoffIndex = determineCutoffIndex(truncatedMessages);\n if (cutoffIndex <= 0) {\n if (argsWereTruncated) {\n return { messages: truncatedMessages };\n }\n return undefined;\n }\n\n const messagesToSummarize = truncatedMessages.slice(0, cutoffIndex);\n const preservedMessages = truncatedMessages.slice(cutoffIndex);\n\n // Offload to backend first\n const resolvedBackend = getBackend(state);\n const filePath = await offloadToBackend(\n resolvedBackend,\n messagesToSummarize,\n state,\n );\n\n if (filePath === null) {\n // Offloading failed - don't proceed with summarization\n return undefined;\n }\n\n // Generate summary\n const summary = await createSummary(messagesToSummarize);\n\n // Build summary message\n const summaryMessage = buildSummaryMessage(summary, filePath);\n\n return {\n messages: [summaryMessage, ...preservedMessages],\n _summarizationSessionId: getSessionId(state),\n };\n },\n });\n}\n","/**\n * StoreBackend: Adapter for LangGraph's BaseStore (persistent, cross-thread).\n */\n\nimport type { Item } from \"@langchain/langgraph\";\nimport type {\n BackendProtocol,\n EditResult,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n StateAndStore,\n WriteResult,\n} from \"./protocol.js\";\nimport {\n createFileData,\n fileDataToString,\n formatReadResponse,\n globSearchFiles,\n grepMatchesFromFiles,\n performStringReplacement,\n updateFileData,\n} from \"./utils.js\";\n\n/**\n * Backend that stores files in LangGraph's BaseStore (persistent).\n *\n * Uses LangGraph's Store for persistent, cross-conversation storage.\n * Files are organized via namespaces and persist across all threads.\n *\n * The namespace can include an optional assistant_id for multi-agent isolation.\n */\nexport class StoreBackend implements BackendProtocol {\n private stateAndStore: StateAndStore;\n\n constructor(stateAndStore: StateAndStore) {\n this.stateAndStore = stateAndStore;\n }\n\n /**\n * Get the store instance.\n *\n * @returns BaseStore instance\n * @throws Error if no store is available\n */\n private getStore() {\n const store = this.stateAndStore.store;\n if (!store) {\n throw new Error(\"Store is required but not available in StateAndStore\");\n }\n return store;\n }\n\n /**\n * Get the namespace for store operations.\n *\n * If an assistant_id is available in stateAndStore, return\n * [assistant_id, \"filesystem\"] to provide per-assistant isolation.\n * Otherwise return [\"filesystem\"].\n */\n protected getNamespace(): string[] {\n const namespace = \"filesystem\";\n const assistantId = this.stateAndStore.assistantId;\n\n if (assistantId) {\n return [assistantId, namespace];\n }\n\n return [namespace];\n }\n\n /**\n * Convert a store Item to FileData format.\n *\n * @param storeItem - The store Item containing file data\n * @returns FileData object\n * @throws Error if required fields are missing or have incorrect types\n */\n private convertStoreItemToFileData(storeItem: Item): FileData {\n const value = storeItem.value as any;\n\n if (\n !value.content ||\n !Array.isArray(value.content) ||\n typeof value.created_at !== \"string\" ||\n typeof value.modified_at !== \"string\"\n ) {\n throw new Error(\n `Store item does not contain valid FileData fields. Got keys: ${Object.keys(value).join(\", \")}`,\n );\n }\n\n return {\n content: value.content,\n created_at: value.created_at,\n modified_at: value.modified_at,\n };\n }\n\n /**\n * Convert FileData to a value suitable for store.put().\n *\n * @param fileData - The FileData to convert\n * @returns Object with content, created_at, and modified_at fields\n */\n private convertFileDataToStoreValue(fileData: FileData): Record<string, any> {\n return {\n content: fileData.content,\n created_at: fileData.created_at,\n modified_at: fileData.modified_at,\n };\n }\n\n /**\n * Search store with automatic pagination to retrieve all results.\n *\n * @param store - The store to search\n * @param namespace - Hierarchical path prefix to search within\n * @param options - Optional query, filter, and page_size\n * @returns List of all items matching the search criteria\n */\n private async searchStorePaginated(\n store: any,\n namespace: string[],\n options: {\n query?: string;\n filter?: Record<string, any>;\n pageSize?: number;\n } = {},\n ): Promise<Item[]> {\n const { query, filter, pageSize = 100 } = options;\n const allItems: Item[] = [];\n let offset = 0;\n\n while (true) {\n const pageItems = await store.search(namespace, {\n query,\n filter,\n limit: pageSize,\n offset,\n });\n\n if (!pageItems || pageItems.length === 0) {\n break;\n }\n\n allItems.push(...pageItems);\n\n if (pageItems.length < pageSize) {\n break;\n }\n\n offset += pageSize;\n }\n\n return allItems;\n }\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects for files and directories directly in the directory.\n * Directories have a trailing / in their path and is_dir=true.\n */\n async lsInfo(path: string): Promise<FileInfo[]> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n\n // Retrieve all items and filter by path prefix locally to avoid\n // coupling to store-specific filter semantics\n const items = await this.searchStorePaginated(store, namespace);\n const infos: FileInfo[] = [];\n const subdirs = new Set<string>();\n\n // Normalize path to have trailing slash for proper prefix matching\n const normalizedPath = path.endsWith(\"/\") ? path : path + \"/\";\n\n for (const item of items) {\n const itemKey = String(item.key);\n\n // Check if file is in the specified directory or a subdirectory\n if (!itemKey.startsWith(normalizedPath)) {\n continue;\n }\n\n // Get the relative path after the directory\n const relative = itemKey.substring(normalizedPath.length);\n\n // If relative path contains '/', it's in a subdirectory\n if (relative.includes(\"/\")) {\n // Extract the immediate subdirectory name\n const subdirName = relative.split(\"/\")[0];\n subdirs.add(normalizedPath + subdirName + \"/\");\n continue;\n }\n\n // This is a file directly in the current directory\n try {\n const fd = this.convertStoreItemToFileData(item);\n const size = fd.content.join(\"\\n\").length;\n infos.push({\n path: itemKey,\n is_dir: false,\n size: size,\n modified_at: fd.modified_at,\n });\n } catch {\n // Skip invalid items\n continue;\n }\n }\n\n // Add directories to the results\n for (const subdir of Array.from(subdirs).sort()) {\n infos.push({\n path: subdir,\n is_dir: true,\n size: 0,\n modified_at: \"\",\n });\n }\n\n infos.sort((a, b) => a.path.localeCompare(b.path));\n return infos;\n }\n\n /**\n * Read file content with line numbers.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n try {\n const fileData = await this.readRaw(filePath);\n return formatReadResponse(fileData, offset, limit);\n } catch (e: any) {\n return `Error: ${e.message}`;\n }\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n async readRaw(filePath: string): Promise<FileData> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const item = await store.get(namespace, filePath);\n\n if (!item) throw new Error(`File '${filePath}' not found`);\n return this.convertStoreItemToFileData(item);\n }\n\n /**\n * Create a new file with content.\n * Returns WriteResult. External storage sets filesUpdate=null.\n */\n async write(filePath: string, content: string): Promise<WriteResult> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n\n // Check if file exists\n const existing = await store.get(namespace, filePath);\n if (existing) {\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n }\n\n // Create new file\n const fileData = createFileData(content);\n const storeValue = this.convertFileDataToStoreValue(fileData);\n await store.put(namespace, filePath, storeValue);\n return { path: filePath, filesUpdate: null };\n }\n\n /**\n * Edit a file by replacing string occurrences.\n * Returns EditResult. External storage sets filesUpdate=null.\n */\n async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n\n // Get existing file\n const item = await store.get(namespace, filePath);\n if (!item) {\n return { error: `Error: File '${filePath}' not found` };\n }\n\n try {\n const fileData = this.convertStoreItemToFileData(item);\n const content = fileDataToString(fileData);\n const result = performStringReplacement(\n content,\n oldString,\n newString,\n replaceAll,\n );\n\n if (typeof result === \"string\") {\n return { error: result };\n }\n\n const [newContent, occurrences] = result;\n const newFileData = updateFileData(fileData, newContent);\n\n // Update file in store\n const storeValue = this.convertFileDataToStoreValue(newFileData);\n await store.put(namespace, filePath, storeValue);\n return { path: filePath, filesUpdate: null, occurrences: occurrences };\n } catch (e: any) {\n return { error: `Error: ${e.message}` };\n }\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n async grepRaw(\n pattern: string,\n path: string = \"/\",\n glob: string | null = null,\n ): Promise<GrepMatch[] | string> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const items = await this.searchStorePaginated(store, namespace);\n\n const files: Record<string, FileData> = {};\n for (const item of items) {\n try {\n files[item.key] = this.convertStoreItemToFileData(item);\n } catch {\n // Skip invalid items\n continue;\n }\n }\n\n return grepMatchesFromFiles(files, pattern, path, glob);\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n async globInfo(pattern: string, path: string = \"/\"): Promise<FileInfo[]> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const items = await this.searchStorePaginated(store, namespace);\n\n const files: Record<string, FileData> = {};\n for (const item of items) {\n try {\n files[item.key] = this.convertStoreItemToFileData(item);\n } catch {\n // Skip invalid items\n continue;\n }\n }\n\n const result = globSearchFiles(files, pattern, path);\n if (result === \"No files found\") {\n return [];\n }\n\n const paths = result.split(\"\\n\");\n const infos: FileInfo[] = [];\n for (const p of paths) {\n const fd = files[p];\n const size = fd ? fd.content.join(\"\\n\").length : 0;\n infos.push({\n path: p,\n is_dir: false,\n size: size,\n modified_at: fd?.modified_at || \"\",\n });\n }\n return infos;\n }\n\n /**\n * Upload multiple files.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const responses: FileUploadResponse[] = [];\n\n for (const [path, content] of files) {\n try {\n const contentStr = new TextDecoder().decode(content);\n const fileData = createFileData(contentStr);\n const storeValue = this.convertFileDataToStoreValue(fileData);\n await store.put(namespace, path, storeValue);\n responses.push({ path, error: null });\n } catch {\n responses.push({ path, error: \"invalid_path\" });\n }\n }\n\n return responses;\n }\n\n /**\n * Download multiple files.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const store = this.getStore();\n const namespace = this.getNamespace();\n const responses: FileDownloadResponse[] = [];\n\n for (const path of paths) {\n try {\n const item = await store.get(namespace, path);\n if (!item) {\n responses.push({ path, content: null, error: \"file_not_found\" });\n continue;\n }\n\n const fileData = this.convertStoreItemToFileData(item);\n const contentStr = fileDataToString(fileData);\n const content = new TextEncoder().encode(contentStr);\n responses.push({ path, content, error: null });\n } catch {\n responses.push({ path, content: null, error: \"file_not_found\" });\n }\n }\n\n return responses;\n }\n}\n","/**\n * FilesystemBackend: Read and write files directly from the filesystem.\n *\n * Security and search upgrades:\n * - Secure path resolution with root containment when in virtual_mode (sandboxed to cwd)\n * - Prevent symlink-following on file I/O using O_NOFOLLOW when available\n * - Ripgrep-powered grep with JSON parsing, plus regex fallback\n * and optional glob include filtering, while preserving virtual path behavior\n */\n\nimport fs from \"node:fs/promises\";\nimport fsSync from \"node:fs\";\nimport path from \"node:path\";\nimport { spawn } from \"node:child_process\";\n\nimport fg from \"fast-glob\";\nimport micromatch from \"micromatch\";\nimport type {\n BackendProtocol,\n EditResult,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n WriteResult,\n} from \"./protocol.js\";\nimport {\n checkEmptyContent,\n formatContentWithLineNumbers,\n performStringReplacement,\n} from \"./utils.js\";\n\nconst SUPPORTS_NOFOLLOW = fsSync.constants.O_NOFOLLOW !== undefined;\n\n/**\n * Backend that reads and writes files directly from the filesystem.\n *\n * Files are accessed using their actual filesystem paths. Relative paths are\n * resolved relative to the current working directory. Content is read/written\n * as plain text, and metadata (timestamps) are derived from filesystem stats.\n */\nexport class FilesystemBackend implements BackendProtocol {\n private cwd: string;\n private virtualMode: boolean;\n private maxFileSizeBytes: number;\n\n constructor(\n options: {\n rootDir?: string;\n virtualMode?: boolean;\n maxFileSizeMb?: number;\n } = {},\n ) {\n const { rootDir, virtualMode = false, maxFileSizeMb = 10 } = options;\n this.cwd = rootDir ? path.resolve(rootDir) : process.cwd();\n this.virtualMode = virtualMode;\n this.maxFileSizeBytes = maxFileSizeMb * 1024 * 1024;\n }\n\n /**\n * Resolve a file path with security checks.\n *\n * When virtualMode=true, treat incoming paths as virtual absolute paths under\n * this.cwd, disallow traversal (.., ~) and ensure resolved path stays within root.\n * When virtualMode=false, preserve legacy behavior: absolute paths are allowed\n * as-is; relative paths resolve under cwd.\n *\n * @param key - File path (absolute, relative, or virtual when virtualMode=true)\n * @returns Resolved absolute path string\n * @throws Error if path traversal detected or path outside root\n */\n private resolvePath(key: string): string {\n if (this.virtualMode) {\n const vpath = key.startsWith(\"/\") ? key : \"/\" + key;\n if (vpath.includes(\"..\") || vpath.startsWith(\"~\")) {\n throw new Error(\"Path traversal not allowed\");\n }\n const full = path.resolve(this.cwd, vpath.substring(1));\n const relative = path.relative(this.cwd, full);\n if (relative.startsWith(\"..\") || path.isAbsolute(relative)) {\n throw new Error(`Path: ${full} outside root directory: ${this.cwd}`);\n }\n return full;\n }\n\n if (path.isAbsolute(key)) {\n return key;\n }\n return path.resolve(this.cwd, key);\n }\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param dirPath - Absolute directory path to list files from\n * @returns List of FileInfo objects for files and directories directly in the directory.\n * Directories have a trailing / in their path and is_dir=true.\n */\n async lsInfo(dirPath: string): Promise<FileInfo[]> {\n try {\n const resolvedPath = this.resolvePath(dirPath);\n const stat = await fs.stat(resolvedPath);\n\n if (!stat.isDirectory()) {\n return [];\n }\n\n const entries = await fs.readdir(resolvedPath, { withFileTypes: true });\n const results: FileInfo[] = [];\n\n const cwdStr = this.cwd.endsWith(path.sep)\n ? this.cwd\n : this.cwd + path.sep;\n\n for (const entry of entries) {\n const fullPath = path.join(resolvedPath, entry.name);\n\n try {\n const entryStat = await fs.stat(fullPath);\n const isFile = entryStat.isFile();\n const isDir = entryStat.isDirectory();\n\n if (!this.virtualMode) {\n // Non-virtual mode: use absolute paths\n if (isFile) {\n results.push({\n path: fullPath,\n is_dir: false,\n size: entryStat.size,\n modified_at: entryStat.mtime.toISOString(),\n });\n } else if (isDir) {\n results.push({\n path: fullPath + path.sep,\n is_dir: true,\n size: 0,\n modified_at: entryStat.mtime.toISOString(),\n });\n }\n } else {\n let relativePath: string;\n if (fullPath.startsWith(cwdStr)) {\n relativePath = fullPath.substring(cwdStr.length);\n } else if (fullPath.startsWith(this.cwd)) {\n relativePath = fullPath\n .substring(this.cwd.length)\n .replace(/^[/\\\\]/, \"\");\n } else {\n relativePath = fullPath;\n }\n\n relativePath = relativePath.split(path.sep).join(\"/\");\n const virtPath = \"/\" + relativePath;\n\n if (isFile) {\n results.push({\n path: virtPath,\n is_dir: false,\n size: entryStat.size,\n modified_at: entryStat.mtime.toISOString(),\n });\n } else if (isDir) {\n results.push({\n path: virtPath + \"/\",\n is_dir: true,\n size: 0,\n modified_at: entryStat.mtime.toISOString(),\n });\n }\n }\n } catch {\n // Skip entries we can't stat\n continue;\n }\n }\n\n results.sort((a, b) => a.path.localeCompare(b.path));\n return results;\n } catch {\n return [];\n }\n }\n\n /**\n * Read file content with line numbers.\n *\n * @param filePath - Absolute or relative file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n try {\n const resolvedPath = this.resolvePath(filePath);\n\n let content: string;\n\n if (SUPPORTS_NOFOLLOW) {\n const stat = await fs.stat(resolvedPath);\n if (!stat.isFile()) {\n return `Error: File '${filePath}' not found`;\n }\n const fd = await fs.open(\n resolvedPath,\n fsSync.constants.O_RDONLY | fsSync.constants.O_NOFOLLOW,\n );\n try {\n content = await fd.readFile({ encoding: \"utf-8\" });\n } finally {\n await fd.close();\n }\n } else {\n const stat = await fs.lstat(resolvedPath);\n if (stat.isSymbolicLink()) {\n return `Error: Symlinks are not allowed: ${filePath}`;\n }\n if (!stat.isFile()) {\n return `Error: File '${filePath}' not found`;\n }\n content = await fs.readFile(resolvedPath, \"utf-8\");\n }\n\n const emptyMsg = checkEmptyContent(content);\n if (emptyMsg) {\n return emptyMsg;\n }\n\n const lines = content.split(\"\\n\");\n const startIdx = offset;\n const endIdx = Math.min(startIdx + limit, lines.length);\n\n if (startIdx >= lines.length) {\n return `Error: Line offset ${offset} exceeds file length (${lines.length} lines)`;\n }\n\n const selectedLines = lines.slice(startIdx, endIdx);\n return formatContentWithLineNumbers(selectedLines, startIdx + 1);\n } catch (e: any) {\n return `Error reading file '${filePath}': ${e.message}`;\n }\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n async readRaw(filePath: string): Promise<FileData> {\n const resolvedPath = this.resolvePath(filePath);\n\n let content: string;\n let stat: fsSync.Stats;\n\n if (SUPPORTS_NOFOLLOW) {\n stat = await fs.stat(resolvedPath);\n if (!stat.isFile()) throw new Error(`File '${filePath}' not found`);\n const fd = await fs.open(\n resolvedPath,\n fsSync.constants.O_RDONLY | fsSync.constants.O_NOFOLLOW,\n );\n try {\n content = await fd.readFile({ encoding: \"utf-8\" });\n } finally {\n await fd.close();\n }\n } else {\n stat = await fs.lstat(resolvedPath);\n if (stat.isSymbolicLink()) {\n throw new Error(`Symlinks are not allowed: ${filePath}`);\n }\n if (!stat.isFile()) throw new Error(`File '${filePath}' not found`);\n content = await fs.readFile(resolvedPath, \"utf-8\");\n }\n\n return {\n content: content.split(\"\\n\"),\n created_at: stat.ctime.toISOString(),\n modified_at: stat.mtime.toISOString(),\n };\n }\n\n /**\n * Create a new file with content.\n * Returns WriteResult. External storage sets filesUpdate=null.\n */\n async write(filePath: string, content: string): Promise<WriteResult> {\n try {\n const resolvedPath = this.resolvePath(filePath);\n\n try {\n const stat = await fs.lstat(resolvedPath);\n if (stat.isSymbolicLink()) {\n return {\n error: `Cannot write to ${filePath} because it is a symlink. Symlinks are not allowed.`,\n };\n }\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n } catch {\n // File doesn't exist, good to proceed\n }\n\n await fs.mkdir(path.dirname(resolvedPath), { recursive: true });\n\n if (SUPPORTS_NOFOLLOW) {\n const flags =\n fsSync.constants.O_WRONLY |\n fsSync.constants.O_CREAT |\n fsSync.constants.O_TRUNC |\n fsSync.constants.O_NOFOLLOW;\n\n const fd = await fs.open(resolvedPath, flags, 0o644);\n try {\n await fd.writeFile(content, \"utf-8\");\n } finally {\n await fd.close();\n }\n } else {\n await fs.writeFile(resolvedPath, content, \"utf-8\");\n }\n\n return { path: filePath, filesUpdate: null };\n } catch (e: any) {\n return { error: `Error writing file '${filePath}': ${e.message}` };\n }\n }\n\n /**\n * Edit a file by replacing string occurrences.\n * Returns EditResult. External storage sets filesUpdate=null.\n */\n async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n try {\n const resolvedPath = this.resolvePath(filePath);\n\n let content: string;\n\n if (SUPPORTS_NOFOLLOW) {\n const stat = await fs.stat(resolvedPath);\n if (!stat.isFile()) {\n return { error: `Error: File '${filePath}' not found` };\n }\n\n const fd = await fs.open(\n resolvedPath,\n fsSync.constants.O_RDONLY | fsSync.constants.O_NOFOLLOW,\n );\n try {\n content = await fd.readFile({ encoding: \"utf-8\" });\n } finally {\n await fd.close();\n }\n } else {\n const stat = await fs.lstat(resolvedPath);\n if (stat.isSymbolicLink()) {\n return { error: `Error: Symlinks are not allowed: ${filePath}` };\n }\n if (!stat.isFile()) {\n return { error: `Error: File '${filePath}' not found` };\n }\n content = await fs.readFile(resolvedPath, \"utf-8\");\n }\n\n const result = performStringReplacement(\n content,\n oldString,\n newString,\n replaceAll,\n );\n\n if (typeof result === \"string\") {\n return { error: result };\n }\n\n const [newContent, occurrences] = result;\n\n // Write securely\n if (SUPPORTS_NOFOLLOW) {\n const flags =\n fsSync.constants.O_WRONLY |\n fsSync.constants.O_TRUNC |\n fsSync.constants.O_NOFOLLOW;\n\n const fd = await fs.open(resolvedPath, flags);\n try {\n await fd.writeFile(newContent, \"utf-8\");\n } finally {\n await fd.close();\n }\n } else {\n await fs.writeFile(resolvedPath, newContent, \"utf-8\");\n }\n\n return { path: filePath, filesUpdate: null, occurrences: occurrences };\n } catch (e: any) {\n return { error: `Error editing file '${filePath}': ${e.message}` };\n }\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n async grepRaw(\n pattern: string,\n dirPath: string = \"/\",\n glob: string | null = null,\n ): Promise<GrepMatch[] | string> {\n // Validate regex\n try {\n new RegExp(pattern);\n } catch (e: any) {\n return `Invalid regex pattern: ${e.message}`;\n }\n\n // Resolve base path\n let baseFull: string;\n try {\n baseFull = this.resolvePath(dirPath || \".\");\n } catch {\n return [];\n }\n\n try {\n await fs.stat(baseFull);\n } catch {\n return [];\n }\n\n // Try ripgrep first, fallback to regex search\n let results = await this.ripgrepSearch(pattern, baseFull, glob);\n if (results === null) {\n results = await this.pythonSearch(pattern, baseFull, glob);\n }\n\n const matches: GrepMatch[] = [];\n for (const [fpath, items] of Object.entries(results)) {\n for (const [lineNum, lineText] of items) {\n matches.push({ path: fpath, line: lineNum, text: lineText });\n }\n }\n return matches;\n }\n\n /**\n * Try to use ripgrep for fast searching.\n * Returns null if ripgrep is not available or fails.\n */\n private async ripgrepSearch(\n pattern: string,\n baseFull: string,\n includeGlob: string | null,\n ): Promise<Record<string, Array<[number, string]>> | null> {\n return new Promise((resolve) => {\n const args = [\"--json\"];\n if (includeGlob) {\n args.push(\"--glob\", includeGlob);\n }\n args.push(\"--\", pattern, baseFull);\n\n const proc = spawn(\"rg\", args, { timeout: 30000 });\n const results: Record<string, Array<[number, string]>> = {};\n let output = \"\";\n\n proc.stdout.on(\"data\", (data) => {\n output += data.toString();\n });\n\n proc.on(\"close\", (code) => {\n if (code !== 0 && code !== 1) {\n // Error (code 1 means no matches, which is ok)\n resolve(null);\n return;\n }\n\n for (const line of output.split(\"\\n\")) {\n if (!line.trim()) continue;\n try {\n const data = JSON.parse(line);\n if (data.type !== \"match\") continue;\n\n const pdata = data.data || {};\n const ftext = pdata.path?.text;\n if (!ftext) continue;\n\n let virtPath: string;\n if (this.virtualMode) {\n try {\n const resolved = path.resolve(ftext);\n const relative = path.relative(this.cwd, resolved);\n if (relative.startsWith(\"..\")) continue;\n const normalizedRelative = relative.split(path.sep).join(\"/\");\n virtPath = \"/\" + normalizedRelative;\n } catch {\n continue;\n }\n } else {\n virtPath = ftext;\n }\n\n const ln = pdata.line_number;\n const lt = pdata.lines?.text?.replace(/\\n$/, \"\") || \"\";\n if (ln === undefined) continue;\n\n if (!results[virtPath]) {\n results[virtPath] = [];\n }\n results[virtPath].push([ln, lt]);\n } catch {\n // Skip invalid JSON\n continue;\n }\n }\n\n resolve(results);\n });\n\n proc.on(\"error\", () => {\n resolve(null);\n });\n });\n }\n\n /**\n * Fallback regex search implementation.\n */\n private async pythonSearch(\n pattern: string,\n baseFull: string,\n includeGlob: string | null,\n ): Promise<Record<string, Array<[number, string]>>> {\n let regex: RegExp;\n try {\n regex = new RegExp(pattern);\n } catch {\n return {};\n }\n\n const results: Record<string, Array<[number, string]>> = {};\n const stat = await fs.stat(baseFull);\n const root = stat.isDirectory() ? baseFull : path.dirname(baseFull);\n\n // Use fast-glob to recursively find all files\n const files = await fg(\"**/*\", {\n cwd: root,\n absolute: true,\n onlyFiles: true,\n dot: true,\n });\n\n for (const fp of files) {\n try {\n // Filter by glob if provided\n if (\n includeGlob &&\n !micromatch.isMatch(path.basename(fp), includeGlob)\n ) {\n continue;\n }\n\n // Check file size\n const stat = await fs.stat(fp);\n if (stat.size > this.maxFileSizeBytes) {\n continue;\n }\n\n // Read and search\n const content = await fs.readFile(fp, \"utf-8\");\n const lines = content.split(\"\\n\");\n\n for (let i = 0; i < lines.length; i++) {\n const line = lines[i];\n if (regex.test(line)) {\n let virtPath: string;\n if (this.virtualMode) {\n try {\n const relative = path.relative(this.cwd, fp);\n if (relative.startsWith(\"..\")) continue;\n const normalizedRelative = relative.split(path.sep).join(\"/\");\n virtPath = \"/\" + normalizedRelative;\n } catch {\n continue;\n }\n } else {\n virtPath = fp;\n }\n\n if (!results[virtPath]) {\n results[virtPath] = [];\n }\n results[virtPath].push([i + 1, line]);\n }\n }\n } catch {\n // Skip files we can't read\n continue;\n }\n }\n\n return results;\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n async globInfo(\n pattern: string,\n searchPath: string = \"/\",\n ): Promise<FileInfo[]> {\n if (pattern.startsWith(\"/\")) {\n pattern = pattern.substring(1);\n }\n\n const resolvedSearchPath =\n searchPath === \"/\" ? this.cwd : this.resolvePath(searchPath);\n\n try {\n const stat = await fs.stat(resolvedSearchPath);\n if (!stat.isDirectory()) {\n return [];\n }\n } catch {\n return [];\n }\n\n const results: FileInfo[] = [];\n\n try {\n // Use fast-glob for pattern matching\n const matches = await fg(pattern, {\n cwd: resolvedSearchPath,\n absolute: true,\n onlyFiles: true,\n dot: true,\n });\n\n for (const matchedPath of matches) {\n try {\n const stat = await fs.stat(matchedPath);\n if (!stat.isFile()) continue;\n\n // Normalize fast-glob paths to platform separators\n // fast-glob returns forward slashes on all platforms, but we need\n // platform-native separators for path comparisons on Windows\n const normalizedPath = matchedPath.split(\"/\").join(path.sep);\n\n if (!this.virtualMode) {\n results.push({\n path: normalizedPath,\n is_dir: false,\n size: stat.size,\n modified_at: stat.mtime.toISOString(),\n });\n } else {\n const cwdStr = this.cwd.endsWith(path.sep)\n ? this.cwd\n : this.cwd + path.sep;\n let relativePath: string;\n\n if (normalizedPath.startsWith(cwdStr)) {\n relativePath = normalizedPath.substring(cwdStr.length);\n } else if (normalizedPath.startsWith(this.cwd)) {\n relativePath = normalizedPath\n .substring(this.cwd.length)\n .replace(/^[/\\\\]/, \"\");\n } else {\n relativePath = normalizedPath;\n }\n\n relativePath = relativePath.split(path.sep).join(\"/\");\n const virt = \"/\" + relativePath;\n results.push({\n path: virt,\n is_dir: false,\n size: stat.size,\n modified_at: stat.mtime.toISOString(),\n });\n }\n } catch {\n // Skip files we can't stat\n continue;\n }\n }\n } catch {\n // Ignore glob errors\n }\n\n results.sort((a, b) => a.path.localeCompare(b.path));\n return results;\n }\n\n /**\n * Upload multiple files to the filesystem.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const responses: FileUploadResponse[] = [];\n\n for (const [filePath, content] of files) {\n try {\n const resolvedPath = this.resolvePath(filePath);\n\n // Ensure parent directory exists\n await fs.mkdir(path.dirname(resolvedPath), { recursive: true });\n\n // Write file\n await fs.writeFile(resolvedPath, content);\n responses.push({ path: filePath, error: null });\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n responses.push({ path: filePath, error: \"file_not_found\" });\n } else if (e.code === \"EACCES\") {\n responses.push({ path: filePath, error: \"permission_denied\" });\n } else if (e.code === \"EISDIR\") {\n responses.push({ path: filePath, error: \"is_directory\" });\n } else {\n responses.push({ path: filePath, error: \"invalid_path\" });\n }\n }\n }\n\n return responses;\n }\n\n /**\n * Download multiple files from the filesystem.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const responses: FileDownloadResponse[] = [];\n\n for (const filePath of paths) {\n try {\n const resolvedPath = this.resolvePath(filePath);\n const content = await fs.readFile(resolvedPath);\n responses.push({ path: filePath, content, error: null });\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n responses.push({\n path: filePath,\n content: null,\n error: \"file_not_found\",\n });\n } else if (e.code === \"EACCES\") {\n responses.push({\n path: filePath,\n content: null,\n error: \"permission_denied\",\n });\n } else if (e.code === \"EISDIR\") {\n responses.push({\n path: filePath,\n content: null,\n error: \"is_directory\",\n });\n } else {\n responses.push({\n path: filePath,\n content: null,\n error: \"invalid_path\",\n });\n }\n }\n }\n\n return responses;\n }\n}\n","/**\n * CompositeBackend: Route operations to different backends based on path prefix.\n */\n\nimport type {\n BackendProtocol,\n EditResult,\n ExecuteResponse,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n WriteResult,\n} from \"./protocol.js\";\nimport { isSandboxBackend } from \"./protocol.js\";\n\n/**\n * Backend that routes file operations to different backends based on path prefix.\n *\n * This enables hybrid storage strategies like:\n * - `/memories/` → StoreBackend (persistent, cross-thread)\n * - Everything else → StateBackend (ephemeral, per-thread)\n *\n * The CompositeBackend handles path prefix stripping/re-adding transparently.\n */\nexport class CompositeBackend implements BackendProtocol {\n private default: BackendProtocol;\n private routes: Record<string, BackendProtocol>;\n private sortedRoutes: Array<[string, BackendProtocol]>;\n\n constructor(\n defaultBackend: BackendProtocol,\n routes: Record<string, BackendProtocol>,\n ) {\n this.default = defaultBackend;\n this.routes = routes;\n\n // Sort routes by length (longest first) for correct prefix matching\n this.sortedRoutes = Object.entries(routes).sort(\n (a, b) => b[0].length - a[0].length,\n );\n }\n\n /**\n * Determine which backend handles this key and strip prefix.\n *\n * @param key - Original file path\n * @returns Tuple of [backend, stripped_key] where stripped_key has the route\n * prefix removed (but keeps leading slash).\n */\n private getBackendAndKey(key: string): [BackendProtocol, string] {\n // Check routes in order of length (longest first)\n for (const [prefix, backend] of this.sortedRoutes) {\n if (key.startsWith(prefix)) {\n // Strip full prefix and ensure a leading slash remains\n // e.g., \"/memories/notes.txt\" → \"/notes.txt\"; \"/memories/\" → \"/\"\n const suffix = key.substring(prefix.length);\n const strippedKey = suffix ? \"/\" + suffix : \"/\";\n return [backend, strippedKey];\n }\n }\n\n return [this.default, key];\n }\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects with route prefixes added, for files and directories\n * directly in the directory. Directories have a trailing / in their path and is_dir=true.\n */\n async lsInfo(path: string): Promise<FileInfo[]> {\n // Check if path matches a specific route\n for (const [routePrefix, backend] of this.sortedRoutes) {\n if (path.startsWith(routePrefix.replace(/\\/$/, \"\"))) {\n // Query only the matching routed backend\n const suffix = path.substring(routePrefix.length);\n const searchPath = suffix ? \"/\" + suffix : \"/\";\n const infos = await backend.lsInfo(searchPath);\n\n // Add route prefix back to paths\n const prefixed: FileInfo[] = [];\n for (const fi of infos) {\n prefixed.push({\n ...fi,\n path: routePrefix.slice(0, -1) + fi.path,\n });\n }\n return prefixed;\n }\n }\n\n // At root, aggregate default and all routed backends\n if (path === \"/\") {\n const results: FileInfo[] = [];\n const defaultInfos = await this.default.lsInfo(path);\n results.push(...defaultInfos);\n\n // Add the route itself as a directory (e.g., /memories/)\n for (const [routePrefix] of this.sortedRoutes) {\n results.push({\n path: routePrefix,\n is_dir: true,\n size: 0,\n modified_at: \"\",\n });\n }\n\n results.sort((a, b) => a.path.localeCompare(b.path));\n return results;\n }\n\n // Path doesn't match a route: query only default backend\n return await this.default.lsInfo(path);\n }\n\n /**\n * Read file content, routing to appropriate backend.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n const [backend, strippedKey] = this.getBackendAndKey(filePath);\n return await backend.read(strippedKey, offset, limit);\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n async readRaw(filePath: string): Promise<FileData> {\n const [backend, strippedKey] = this.getBackendAndKey(filePath);\n return await backend.readRaw(strippedKey);\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n async grepRaw(\n pattern: string,\n path: string = \"/\",\n glob: string | null = null,\n ): Promise<GrepMatch[] | string> {\n // If path targets a specific route, search only that backend\n for (const [routePrefix, backend] of this.sortedRoutes) {\n if (path.startsWith(routePrefix.replace(/\\/$/, \"\"))) {\n const searchPath = path.substring(routePrefix.length - 1);\n const raw = await backend.grepRaw(pattern, searchPath || \"/\", glob);\n\n if (typeof raw === \"string\") {\n return raw;\n }\n\n // Add route prefix back\n return raw.map((m) => ({\n ...m,\n path: routePrefix.slice(0, -1) + m.path,\n }));\n }\n }\n\n // Otherwise, search default and all routed backends and merge\n const allMatches: GrepMatch[] = [];\n const rawDefault = await this.default.grepRaw(pattern, path, glob);\n\n if (typeof rawDefault === \"string\") {\n return rawDefault;\n }\n\n allMatches.push(...rawDefault);\n\n // Search all routes\n for (const [routePrefix, backend] of Object.entries(this.routes)) {\n const raw = await backend.grepRaw(pattern, \"/\", glob);\n\n if (typeof raw === \"string\") {\n return raw;\n }\n\n // Add route prefix back\n allMatches.push(\n ...raw.map((m) => ({\n ...m,\n path: routePrefix.slice(0, -1) + m.path,\n })),\n );\n }\n\n return allMatches;\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n async globInfo(pattern: string, path: string = \"/\"): Promise<FileInfo[]> {\n const results: FileInfo[] = [];\n\n // Route based on path, not pattern\n for (const [routePrefix, backend] of this.sortedRoutes) {\n if (path.startsWith(routePrefix.replace(/\\/$/, \"\"))) {\n const searchPath = path.substring(routePrefix.length - 1);\n const infos = await backend.globInfo(pattern, searchPath || \"/\");\n\n // Add route prefix back\n return infos.map((fi) => ({\n ...fi,\n path: routePrefix.slice(0, -1) + fi.path,\n }));\n }\n }\n\n // Path doesn't match any specific route - search default backend AND all routed backends\n const defaultInfos = await this.default.globInfo(pattern, path);\n results.push(...defaultInfos);\n\n for (const [routePrefix, backend] of Object.entries(this.routes)) {\n const infos = await backend.globInfo(pattern, \"/\");\n results.push(\n ...infos.map((fi) => ({\n ...fi,\n path: routePrefix.slice(0, -1) + fi.path,\n })),\n );\n }\n\n // Deterministic ordering\n results.sort((a, b) => a.path.localeCompare(b.path));\n return results;\n }\n\n /**\n * Create a new file, routing to appropriate backend.\n *\n * @param filePath - Absolute file path\n * @param content - File content as string\n * @returns WriteResult with path or error\n */\n async write(filePath: string, content: string): Promise<WriteResult> {\n const [backend, strippedKey] = this.getBackendAndKey(filePath);\n return await backend.write(strippedKey, content);\n }\n\n /**\n * Edit a file, routing to appropriate backend.\n *\n * @param filePath - Absolute file path\n * @param oldString - String to find and replace\n * @param newString - Replacement string\n * @param replaceAll - If true, replace all occurrences\n * @returns EditResult with path, occurrences, or error\n */\n async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n const [backend, strippedKey] = this.getBackendAndKey(filePath);\n return await backend.edit(strippedKey, oldString, newString, replaceAll);\n }\n\n /**\n * Execute a command via the default backend.\n * Execution is not path-specific, so it always delegates to the default backend.\n *\n * @param command - Full shell command string to execute\n * @returns ExecuteResponse with combined output, exit code, and truncation flag\n * @throws Error if the default backend doesn't support command execution\n */\n execute(command: string): Promise<ExecuteResponse> {\n if (!isSandboxBackend(this.default)) {\n throw new Error(\n \"Default backend doesn't support command execution (SandboxBackendProtocol). \" +\n \"To enable execution, provide a default backend that implements SandboxBackendProtocol.\",\n );\n }\n return Promise.resolve(this.default.execute(command));\n }\n\n /**\n * Upload multiple files, batching by backend for efficiency.\n *\n * @param files - List of [path, content] tuples to upload\n * @returns List of FileUploadResponse objects, one per input file\n */\n async uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): Promise<FileUploadResponse[]> {\n const results: Array<FileUploadResponse | null> = Array.from(\n { length: files.length },\n () => null,\n );\n const batchesByBackend = new Map<\n BackendProtocol,\n Array<{ idx: number; path: string; content: Uint8Array }>\n >();\n\n for (let idx = 0; idx < files.length; idx++) {\n const [path, content] = files[idx];\n const [backend, strippedPath] = this.getBackendAndKey(path);\n\n if (!batchesByBackend.has(backend)) {\n batchesByBackend.set(backend, []);\n }\n batchesByBackend.get(backend)!.push({ idx, path: strippedPath, content });\n }\n\n for (const [backend, batch] of batchesByBackend) {\n if (!backend.uploadFiles) {\n throw new Error(\"Backend does not support uploadFiles\");\n }\n\n const batchFiles = batch.map(\n (b) => [b.path, b.content] as [string, Uint8Array],\n );\n const batchResponses = await backend.uploadFiles(batchFiles);\n\n for (let i = 0; i < batch.length; i++) {\n const originalIdx = batch[i].idx;\n results[originalIdx] = {\n path: files[originalIdx][0], // Original path\n error: batchResponses[i]?.error ?? null,\n };\n }\n }\n\n return results as FileUploadResponse[];\n }\n\n /**\n * Download multiple files, batching by backend for efficiency.\n *\n * @param paths - List of file paths to download\n * @returns List of FileDownloadResponse objects, one per input path\n */\n async downloadFiles(paths: string[]): Promise<FileDownloadResponse[]> {\n const results: Array<FileDownloadResponse | null> = Array.from(\n { length: paths.length },\n () => null,\n );\n const batchesByBackend = new Map<\n BackendProtocol,\n Array<{ idx: number; path: string }>\n >();\n\n for (let idx = 0; idx < paths.length; idx++) {\n const path = paths[idx];\n const [backend, strippedPath] = this.getBackendAndKey(path);\n\n if (!batchesByBackend.has(backend)) {\n batchesByBackend.set(backend, []);\n }\n batchesByBackend.get(backend)!.push({ idx, path: strippedPath });\n }\n\n for (const [backend, batch] of batchesByBackend) {\n if (!backend.downloadFiles) {\n throw new Error(\"Backend does not support downloadFiles\");\n }\n\n const batchPaths = batch.map((b) => b.path);\n const batchResponses = await backend.downloadFiles(batchPaths);\n\n for (let i = 0; i < batch.length; i++) {\n const originalIdx = batch[i].idx;\n results[originalIdx] = {\n path: paths[originalIdx], // Original path\n content: batchResponses[i]?.content ?? null,\n error: batchResponses[i]?.error ?? null,\n };\n }\n }\n\n return results as FileDownloadResponse[];\n }\n}\n","/**\n * BaseSandbox: Abstract base class for sandbox backends with command execution.\n *\n * This class provides default implementations for all SandboxBackendProtocol\n * methods using shell commands executed via execute(). Concrete implementations\n * only need to implement the execute() method.\n *\n * Requires Node.js 20+ on the sandbox host.\n */\n\nimport type {\n EditResult,\n ExecuteResponse,\n FileData,\n FileDownloadResponse,\n FileInfo,\n FileUploadResponse,\n GrepMatch,\n MaybePromise,\n SandboxBackendProtocol,\n WriteResult,\n} from \"./protocol.js\";\n\n/**\n * Node.js command template for glob operations.\n * Uses web-standard atob() for base64 decoding.\n */\nfunction buildGlobCommand(searchPath: string, pattern: string): string {\n const pathB64 = btoa(searchPath);\n const patternB64 = btoa(pattern);\n\n return `node -e \"\nconst fs = require('fs');\nconst path = require('path');\n\nconst searchPath = atob('${pathB64}');\nconst pattern = atob('${patternB64}');\n\nfunction globMatch(relativePath, pattern) {\n const regexPattern = pattern\n .replace(/\\\\*\\\\*/g, '<<<GLOBSTAR>>>')\n .replace(/\\\\*/g, '[^/]*')\n .replace(/\\\\?/g, '.')\n .replace(/<<<GLOBSTAR>>>/g, '.*');\n return new RegExp('^' + regexPattern + '$').test(relativePath);\n}\n\nfunction walkDir(dir, baseDir, results) {\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n const fullPath = path.join(dir, entry.name);\n const relativePath = path.relative(baseDir, fullPath);\n if (entry.isDirectory()) {\n walkDir(fullPath, baseDir, results);\n } else if (globMatch(relativePath, pattern)) {\n const stat = fs.statSync(fullPath);\n console.log(JSON.stringify({\n path: relativePath,\n size: stat.size,\n mtime: stat.mtimeMs,\n isDir: false\n }));\n }\n }\n } catch (e) {\n // Silent failure for non-existent paths\n }\n}\n\ntry {\n process.chdir(searchPath);\n walkDir('.', '.', []);\n} catch (e) {\n // Silent failure for non-existent paths\n}\n\"`;\n}\n\n/**\n * Node.js command template for listing directory contents.\n */\nfunction buildLsCommand(dirPath: string): string {\n const pathB64 = btoa(dirPath);\n\n return `node -e \"\nconst fs = require('fs');\nconst path = require('path');\n\nconst dirPath = atob('${pathB64}');\n\ntry {\n const entries = fs.readdirSync(dirPath, { withFileTypes: true });\n for (const entry of entries) {\n const fullPath = path.join(dirPath, entry.name);\n const stat = fs.statSync(fullPath);\n console.log(JSON.stringify({\n path: entry.isDirectory() ? fullPath + '/' : fullPath,\n size: stat.size,\n mtime: stat.mtimeMs,\n isDir: entry.isDirectory()\n }));\n }\n} catch (e) {\n console.error('Error: ' + e.message);\n process.exit(1);\n}\n\"`;\n}\n\n/**\n * Node.js command template for reading files.\n */\nfunction buildReadCommand(\n filePath: string,\n offset: number,\n limit: number,\n): string {\n const pathB64 = btoa(filePath);\n // Coerce offset and limit to safe non-negative integers before embedding in the shell command.\n const safeOffset =\n Number.isFinite(offset) && offset > 0 ? Math.floor(offset) : 0;\n const safeLimit =\n Number.isFinite(limit) && limit > 0 && limit < Number.MAX_SAFE_INTEGER\n ? Math.floor(limit)\n : 0;\n\n return `node -e \"\nconst fs = require('fs');\n\nconst filePath = atob('${pathB64}');\nconst offset = ${safeOffset};\nconst limit = ${safeLimit};\n\nif (!fs.existsSync(filePath)) {\n console.log('Error: File not found');\n process.exit(1);\n}\n\nconst stat = fs.statSync(filePath);\nif (stat.size === 0) {\n console.log('System reminder: File exists but has empty contents');\n process.exit(0);\n}\n\nconst content = fs.readFileSync(filePath, 'utf-8');\nconst lines = content.split('\\\\n');\nconst selected = lines.slice(offset, offset + limit);\n\nfor (let i = 0; i < selected.length; i++) {\n const lineNum = offset + i + 1;\n console.log(String(lineNum).padStart(6) + '\\\\t' + selected[i]);\n}\n\"`;\n}\n\n/**\n * Node.js command template for writing files.\n */\nfunction buildWriteCommand(filePath: string, content: string): string {\n const pathB64 = btoa(filePath);\n const contentB64 = btoa(content);\n\n return `node -e \"\nconst fs = require('fs');\nconst path = require('path');\n\nconst filePath = atob('${pathB64}');\nconst content = atob('${contentB64}');\n\nif (fs.existsSync(filePath)) {\n console.error('Error: File already exists');\n process.exit(1);\n}\n\nconst parentDir = path.dirname(filePath) || '.';\nfs.mkdirSync(parentDir, { recursive: true });\n\nfs.writeFileSync(filePath, content, 'utf-8');\nconsole.log('OK');\n\"`;\n}\n\n/**\n * Node.js command template for editing files.\n */\nfunction buildEditCommand(\n filePath: string,\n oldStr: string,\n newStr: string,\n replaceAll: boolean,\n): string {\n const pathB64 = btoa(filePath);\n const oldB64 = btoa(oldStr);\n const newB64 = btoa(newStr);\n\n return `node -e \"\nconst fs = require('fs');\n\nconst filePath = atob('${pathB64}');\nconst oldStr = atob('${oldB64}');\nconst newStr = atob('${newB64}');\nconst replaceAll = ${Boolean(replaceAll)};\n\nlet text;\ntry {\n text = fs.readFileSync(filePath, 'utf-8');\n} catch (e) {\n process.exit(3);\n}\n\nconst count = text.split(oldStr).length - 1;\n\nif (count === 0) {\n process.exit(1);\n}\nif (count > 1 && !replaceAll) {\n process.exit(2);\n}\n\nconst result = text.split(oldStr).join(newStr);\nfs.writeFileSync(filePath, result, 'utf-8');\nconsole.log(count);\n\"`;\n}\n\n/**\n * Node.js command template for grep operations.\n */\nfunction buildGrepCommand(\n pattern: string,\n searchPath: string,\n globPattern: string | null,\n): string {\n const patternB64 = btoa(pattern);\n const pathB64 = btoa(searchPath);\n const globB64 = globPattern ? btoa(globPattern) : \"\";\n\n return `node -e \"\nconst fs = require('fs');\nconst path = require('path');\n\nconst pattern = atob('${patternB64}');\nconst searchPath = atob('${pathB64}');\nconst globPattern = ${globPattern ? `atob('${globB64}')` : \"null\"};\n\nlet regex;\ntry {\n regex = new RegExp(pattern);\n} catch (e) {\n console.error('Invalid regex: ' + e.message);\n process.exit(1);\n}\n\nfunction globMatch(filePath, pattern) {\n if (!pattern) return true;\n const regexPattern = pattern\n .replace(/\\\\*\\\\*/g, '<<<GLOBSTAR>>>')\n .replace(/\\\\*/g, '[^/]*')\n .replace(/\\\\?/g, '.')\n .replace(/<<<GLOBSTAR>>>/g, '.*');\n return new RegExp('^' + regexPattern + '$').test(filePath);\n}\n\nfunction walkDir(dir, results) {\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n for (const entry of entries) {\n const fullPath = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n walkDir(fullPath, results);\n } else {\n const relativePath = path.relative(searchPath, fullPath);\n if (globMatch(relativePath, globPattern)) {\n try {\n const content = fs.readFileSync(fullPath, 'utf-8');\n const lines = content.split('\\\\n');\n for (let i = 0; i < lines.length; i++) {\n if (regex.test(lines[i])) {\n console.log(JSON.stringify({\n path: fullPath,\n line: i + 1,\n text: lines[i]\n }));\n }\n }\n } catch (e) {\n // Skip unreadable files\n }\n }\n }\n }\n } catch (e) {\n // Skip unreadable directories\n }\n}\n\ntry {\n walkDir(searchPath, []);\n} catch (e) {\n // Silent failure\n}\n\"`;\n}\n\n/**\n * Base sandbox implementation with execute() as the only abstract method.\n *\n * This class provides default implementations for all SandboxBackendProtocol\n * methods using shell commands executed via execute(). Concrete implementations\n * only need to implement the execute() method.\n *\n * Requires Node.js 20+ on the sandbox host.\n */\nexport abstract class BaseSandbox implements SandboxBackendProtocol {\n /** Unique identifier for the sandbox backend */\n abstract readonly id: string;\n\n /**\n * Execute a command in the sandbox.\n * This is the only method concrete implementations must provide.\n */\n abstract execute(command: string): MaybePromise<ExecuteResponse>;\n\n /**\n * Upload multiple files to the sandbox.\n * Implementations must support partial success.\n */\n abstract uploadFiles(\n files: Array<[string, Uint8Array]>,\n ): MaybePromise<FileUploadResponse[]>;\n\n /**\n * Download multiple files from the sandbox.\n * Implementations must support partial success.\n */\n abstract downloadFiles(paths: string[]): MaybePromise<FileDownloadResponse[]>;\n\n /**\n * List files and directories in the specified directory (non-recursive).\n *\n * @param path - Absolute path to directory\n * @returns List of FileInfo objects for files and directories directly in the directory.\n */\n async lsInfo(path: string): Promise<FileInfo[]> {\n const command = buildLsCommand(path);\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n return [];\n }\n\n const infos: FileInfo[] = [];\n const lines = result.output.trim().split(\"\\n\").filter(Boolean);\n\n for (const line of lines) {\n try {\n const parsed = JSON.parse(line);\n infos.push({\n path: parsed.path,\n is_dir: parsed.isDir,\n size: parsed.size,\n modified_at: parsed.mtime\n ? new Date(parsed.mtime).toISOString()\n : undefined,\n });\n } catch {\n // Skip invalid JSON lines\n }\n }\n\n return infos;\n }\n\n /**\n * Read file content with line numbers.\n *\n * @param filePath - Absolute file path\n * @param offset - Line offset to start reading from (0-indexed)\n * @param limit - Maximum number of lines to read\n * @returns Formatted file content with line numbers, or error message\n */\n async read(\n filePath: string,\n offset: number = 0,\n limit: number = 500,\n ): Promise<string> {\n const command = buildReadCommand(filePath, offset, limit);\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n return `Error: File '${filePath}' not found`;\n }\n\n return result.output;\n }\n\n /**\n * Read file content as raw FileData.\n *\n * @param filePath - Absolute file path\n * @returns Raw file content as FileData\n */\n async readRaw(filePath: string): Promise<FileData> {\n const command = buildReadCommand(filePath, 0, Number.MAX_SAFE_INTEGER);\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n throw new Error(`File '${filePath}' not found`);\n }\n\n // Parse the line-numbered output back to content\n const lines: string[] = [];\n for (const line of result.output.split(\"\\n\")) {\n // Format is \" 123\\tContent\"\n const tabIndex = line.indexOf(\"\\t\");\n if (tabIndex !== -1) {\n lines.push(line.substring(tabIndex + 1));\n }\n }\n\n const now = new Date().toISOString();\n return {\n content: lines,\n created_at: now,\n modified_at: now,\n };\n }\n\n /**\n * Structured search results or error string for invalid input.\n */\n async grepRaw(\n pattern: string,\n path: string = \"/\",\n glob: string | null = null,\n ): Promise<GrepMatch[] | string> {\n const command = buildGrepCommand(pattern, path, glob);\n const result = await this.execute(command);\n\n if (result.exitCode === 1) {\n // Check if it's a regex error\n if (result.output.includes(\"Invalid regex:\")) {\n return result.output.trim();\n }\n }\n\n const matches: GrepMatch[] = [];\n const lines = result.output.trim().split(\"\\n\").filter(Boolean);\n\n for (const line of lines) {\n try {\n const parsed = JSON.parse(line);\n matches.push({\n path: parsed.path,\n line: parsed.line,\n text: parsed.text,\n });\n } catch {\n // Skip invalid JSON lines\n }\n }\n\n return matches;\n }\n\n /**\n * Structured glob matching returning FileInfo objects.\n */\n async globInfo(pattern: string, path: string = \"/\"): Promise<FileInfo[]> {\n const command = buildGlobCommand(path, pattern);\n const result = await this.execute(command);\n\n const infos: FileInfo[] = [];\n const lines = result.output.trim().split(\"\\n\").filter(Boolean);\n\n for (const line of lines) {\n try {\n const parsed = JSON.parse(line);\n infos.push({\n path: parsed.path,\n is_dir: parsed.isDir,\n size: parsed.size,\n modified_at: parsed.mtime\n ? new Date(parsed.mtime).toISOString()\n : undefined,\n });\n } catch {\n // Skip invalid JSON lines\n }\n }\n\n return infos;\n }\n\n /**\n * Create a new file with content.\n */\n async write(filePath: string, content: string): Promise<WriteResult> {\n const command = buildWriteCommand(filePath, content);\n const result = await this.execute(command);\n\n if (result.exitCode !== 0) {\n return {\n error: `Cannot write to ${filePath} because it already exists. Read and then make an edit, or write to a new path.`,\n };\n }\n\n return { path: filePath, filesUpdate: null };\n }\n\n /**\n * Edit a file by replacing string occurrences.\n */\n async edit(\n filePath: string,\n oldString: string,\n newString: string,\n replaceAll: boolean = false,\n ): Promise<EditResult> {\n const command = buildEditCommand(\n filePath,\n oldString,\n newString,\n replaceAll,\n );\n const result = await this.execute(command);\n\n switch (result.exitCode) {\n case 0: {\n const occurrences = parseInt(result.output.trim(), 10) || 1;\n return { path: filePath, filesUpdate: null, occurrences };\n }\n case 1:\n return { error: `String not found in file '${filePath}'` };\n case 2:\n return {\n error: `Multiple occurrences found in '${filePath}'. Use replaceAll=true to replace all.`,\n };\n case 3:\n return { error: `Error: File '${filePath}' not found` };\n default:\n return { error: `Unknown error editing file '${filePath}'` };\n }\n }\n}\n","import {\n createAgent,\n humanInTheLoopMiddleware,\n anthropicPromptCachingMiddleware,\n todoListMiddleware,\n summarizationMiddleware,\n SystemMessage,\n type AgentMiddleware,\n type ResponseFormat,\n} from \"langchain\";\nimport type {\n ClientTool,\n ServerTool,\n StructuredTool,\n} from \"@langchain/core/tools\";\nimport type { BaseStore } from \"@langchain/langgraph-checkpoint\";\n\nimport {\n createFilesystemMiddleware,\n createSubAgentMiddleware,\n createPatchToolCallsMiddleware,\n createMemoryMiddleware,\n createSkillsMiddleware,\n type SubAgent,\n} from \"./middleware/index.js\";\nimport { StateBackend } from \"./backends/index.js\";\nimport { InteropZodObject } from \"@langchain/core/utils/types\";\nimport { CompiledSubAgent } from \"./middleware/subagents.js\";\nimport type {\n CreateDeepAgentParams,\n DeepAgent,\n DeepAgentTypeConfig,\n FlattenSubAgentMiddleware,\n} from \"./types.js\";\n\n/**\n * required for type inference\n */\nimport type * as _messages from \"@langchain/core/messages\";\nimport type * as _Command from \"@langchain/langgraph\";\n\nconst BASE_PROMPT = `In order to complete the objective that the user asks of you, you have access to a number of standard tools.`;\n\n/**\n * Create a Deep Agent with middleware-based architecture.\n *\n * Matches Python's create_deep_agent function, using middleware for all features:\n * - Todo management (todoListMiddleware)\n * - Filesystem tools (createFilesystemMiddleware)\n * - Subagent delegation (createSubAgentMiddleware)\n * - Conversation summarization (summarizationMiddleware)\n * - Prompt caching (anthropicPromptCachingMiddleware)\n * - Tool call patching (createPatchToolCallsMiddleware)\n * - Human-in-the-loop (humanInTheLoopMiddleware) - optional\n *\n * @param params Configuration parameters for the agent\n * @returns ReactAgent instance ready for invocation with properly inferred state types\n *\n * @example\n * ```typescript\n * // Middleware with custom state\n * const ResearchMiddleware = createMiddleware({\n * name: \"ResearchMiddleware\",\n * stateSchema: z.object({ research: z.string().default(\"\") }),\n * });\n *\n * const agent = createDeepAgent({\n * middleware: [ResearchMiddleware],\n * });\n *\n * const result = await agent.invoke({ messages: [...] });\n * // result.research is properly typed as string\n * ```\n */\nexport function createDeepAgent<\n TResponse extends ResponseFormat = ResponseFormat,\n ContextSchema extends InteropZodObject = InteropZodObject,\n const TMiddleware extends readonly AgentMiddleware[] = readonly [],\n const TSubagents extends readonly (SubAgent | CompiledSubAgent)[] =\n readonly [],\n const TTools extends readonly (ClientTool | ServerTool)[] = readonly [],\n>(\n params: CreateDeepAgentParams<\n TResponse,\n ContextSchema,\n TMiddleware,\n TSubagents,\n TTools\n > = {} as CreateDeepAgentParams<\n TResponse,\n ContextSchema,\n TMiddleware,\n TSubagents,\n TTools\n >,\n) {\n const {\n model = \"claude-sonnet-4-5-20250929\",\n tools = [],\n systemPrompt,\n middleware: customMiddleware = [],\n subagents = [],\n responseFormat,\n contextSchema,\n checkpointer,\n store,\n backend,\n interruptOn,\n name,\n memory,\n skills,\n } = params;\n\n // Combine system prompt with base prompt like Python implementation\n const finalSystemPrompt = systemPrompt\n ? typeof systemPrompt === \"string\"\n ? `${systemPrompt}\\n\\n${BASE_PROMPT}`\n : new SystemMessage({\n content: [\n {\n type: \"text\",\n text: BASE_PROMPT,\n },\n ...(typeof systemPrompt.content === \"string\"\n ? [{ type: \"text\", text: systemPrompt.content }]\n : systemPrompt.content),\n ],\n })\n : BASE_PROMPT;\n\n // Create backend configuration for filesystem middleware\n // If no backend is provided, use a factory that creates a StateBackend\n const filesystemBackend = backend\n ? backend\n : (config: { state: unknown; store?: BaseStore }) =>\n new StateBackend(config);\n\n // Add skills middleware if skill sources provided\n const skillsMiddleware =\n skills != null && skills.length > 0\n ? [\n createSkillsMiddleware({\n backend: filesystemBackend,\n sources: skills,\n }),\n ]\n : [];\n\n // Built-in middleware array\n const builtInMiddleware = [\n // Provides todo list management capabilities for tracking tasks\n todoListMiddleware(),\n // Add skills middleware if skill sources provided\n ...skillsMiddleware,\n // Enables filesystem operations and optional long-term memory storage\n createFilesystemMiddleware({ backend: filesystemBackend }),\n // Enables delegation to specialized subagents for complex tasks\n createSubAgentMiddleware({\n defaultModel: model,\n defaultTools: tools as StructuredTool[],\n defaultMiddleware: [\n // Subagent middleware: Todo list management\n todoListMiddleware(),\n // Subagent middleware: Skills (if provided)\n ...skillsMiddleware,\n // Subagent middleware: Filesystem operations\n createFilesystemMiddleware({\n backend: filesystemBackend,\n }),\n // Subagent middleware: Automatic conversation summarization when token limits are approached\n summarizationMiddleware({\n model,\n trigger: { tokens: 170_000 },\n keep: { messages: 6 },\n }),\n // Subagent middleware: Anthropic prompt caching for improved performance\n anthropicPromptCachingMiddleware({\n unsupportedModelBehavior: \"ignore\",\n }),\n // Subagent middleware: Patches tool calls for compatibility\n createPatchToolCallsMiddleware(),\n ],\n defaultInterruptOn: interruptOn,\n subagents: subagents as unknown as (SubAgent | CompiledSubAgent)[],\n generalPurposeAgent: true,\n }),\n // Automatically summarizes conversation history when token limits are approached\n summarizationMiddleware({\n model,\n trigger: { tokens: 170_000 },\n keep: { messages: 6 },\n }),\n // Enables Anthropic prompt caching for improved performance and reduced costs\n anthropicPromptCachingMiddleware({\n unsupportedModelBehavior: \"ignore\",\n }),\n // Patches tool calls to ensure compatibility across different model providers\n createPatchToolCallsMiddleware(),\n // Add memory middleware if memory sources provided\n ...(memory != null && memory.length > 0\n ? [\n createMemoryMiddleware({\n backend: filesystemBackend,\n sources: memory,\n }),\n ]\n : []),\n ] as const;\n\n // Add human-in-the-loop middleware if interrupt config provided\n if (interruptOn) {\n // builtInMiddleware is typed as readonly to enable type inference\n // however, we need to push to it to add the middleware, so let's ignore the type error\n // @ts-expect-error - builtInMiddleware is readonly\n builtInMiddleware.push(humanInTheLoopMiddleware({ interruptOn }));\n }\n\n // Combine built-in middleware with custom middleware\n // The custom middleware is typed as TMiddleware to preserve type information\n const allMiddleware = [\n ...builtInMiddleware,\n ...(customMiddleware as unknown as TMiddleware),\n ] as const;\n\n // Note: Recursion limit of 1000 (matching Python behavior) should be passed\n // at invocation time: agent.invoke(input, { recursionLimit: 1000 })\n const agent = createAgent({\n model,\n systemPrompt: finalSystemPrompt,\n tools: tools as StructuredTool[],\n middleware: allMiddleware as unknown as AgentMiddleware[],\n responseFormat: responseFormat as ResponseFormat,\n contextSchema,\n checkpointer,\n store,\n name,\n });\n\n // Combine custom middleware with flattened subagent middleware for complete type inference\n // This ensures InferMiddlewareStates captures state from both sources\n type AllMiddleware = readonly [\n ...typeof builtInMiddleware,\n ...TMiddleware,\n ...FlattenSubAgentMiddleware<TSubagents>,\n ];\n\n // Return as DeepAgent with proper DeepAgentTypeConfig\n // - Response: TResponse (from responseFormat parameter)\n // - State: undefined (state comes from middleware)\n // - Context: ContextSchema\n // - Middleware: AllMiddleware (built-in + custom + subagent middleware for state inference)\n // - Tools: TTools\n // - Subagents: TSubagents (for type-safe streaming)\n return agent as unknown as DeepAgent<\n DeepAgentTypeConfig<\n TResponse,\n undefined,\n ContextSchema,\n AllMiddleware,\n TTools,\n TSubagents\n >\n >;\n}\n","/**\n * Configuration and settings for deepagents.\n *\n * Provides project detection, path management, and environment configuration\n * for skills and agent memory middleware.\n */\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport os from \"node:os\";\n\n/**\n * Options for creating a Settings instance.\n */\nexport interface SettingsOptions {\n /** Starting directory for project detection (defaults to cwd) */\n startPath?: string;\n}\n\n/**\n * Settings interface for project detection and path management.\n *\n * Provides access to:\n * - Project root detection (via .git directory)\n * - User-level deepagents directory (~/.deepagents)\n * - Agent-specific directories and files\n * - Skills directories (user and project level)\n */\nexport interface Settings {\n /** Detected project root directory, or null if not in a git project */\n readonly projectRoot: string | null;\n\n /** Base user-level .deepagents directory (~/.deepagents) */\n readonly userDeepagentsDir: string;\n\n /** Check if currently in a git project */\n readonly hasProject: boolean;\n\n /**\n * Get the agent directory path.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}\n * @throws Error if agent name is invalid\n */\n getAgentDir(agentName: string): string;\n\n /**\n * Ensure agent directory exists and return path.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}\n * @throws Error if agent name is invalid\n */\n ensureAgentDir(agentName: string): string;\n\n /**\n * Get user-level agent.md path for a specific agent.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}/agent.md\n */\n getUserAgentMdPath(agentName: string): string;\n\n /**\n * Get project-level agent.md path.\n * @returns Path to {projectRoot}/.deepagents/agent.md, or null if not in a project\n */\n getProjectAgentMdPath(): string | null;\n\n /**\n * Get user-level skills directory path for a specific agent.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}/skills/\n */\n getUserSkillsDir(agentName: string): string;\n\n /**\n * Ensure user-level skills directory exists and return path.\n * @param agentName - Name of the agent\n * @returns Path to ~/.deepagents/{agentName}/skills/\n */\n ensureUserSkillsDir(agentName: string): string;\n\n /**\n * Get project-level skills directory path.\n * @returns Path to {projectRoot}/.deepagents/skills/, or null if not in a project\n */\n getProjectSkillsDir(): string | null;\n\n /**\n * Ensure project-level skills directory exists and return path.\n * @returns Path to {projectRoot}/.deepagents/skills/, or null if not in a project\n */\n ensureProjectSkillsDir(): string | null;\n\n /**\n * Ensure project .deepagents directory exists.\n * @returns Path to {projectRoot}/.deepagents/, or null if not in a project\n */\n ensureProjectDeepagentsDir(): string | null;\n}\n\n/**\n * Find the project root by looking for .git directory.\n *\n * Walks up the directory tree from startPath (or cwd) looking for a .git\n * directory, which indicates the project root.\n *\n * @param startPath - Directory to start searching from. Defaults to current working directory.\n * @returns Path to the project root if found, null otherwise.\n */\nexport function findProjectRoot(startPath?: string): string | null {\n let current = path.resolve(startPath || process.cwd());\n\n // Walk up the directory tree\n while (current !== path.dirname(current)) {\n const gitDir = path.join(current, \".git\");\n if (fs.existsSync(gitDir)) {\n return current;\n }\n current = path.dirname(current);\n }\n\n // Check root directory as well\n const rootGitDir = path.join(current, \".git\");\n if (fs.existsSync(rootGitDir)) {\n return current;\n }\n\n return null;\n}\n\n/**\n * Validate agent name to prevent invalid filesystem paths and security issues.\n *\n * @param agentName - The agent name to validate\n * @returns True if valid, false otherwise\n */\nfunction isValidAgentName(agentName: string): boolean {\n if (!agentName || !agentName.trim()) {\n return false;\n }\n // Allow only alphanumeric, hyphens, underscores, and whitespace\n return /^[a-zA-Z0-9_\\-\\s]+$/.test(agentName);\n}\n\n/**\n * Create a Settings instance with detected environment.\n *\n * @param options - Configuration options\n * @returns Settings instance with project detection and path management\n */\nexport function createSettings(options: SettingsOptions = {}): Settings {\n const projectRoot = findProjectRoot(options.startPath);\n const userDeepagentsDir = path.join(os.homedir(), \".deepagents\");\n\n return {\n projectRoot,\n userDeepagentsDir,\n hasProject: projectRoot !== null,\n\n getAgentDir(agentName: string): string {\n if (!isValidAgentName(agentName)) {\n throw new Error(\n `Invalid agent name: ${JSON.stringify(agentName)}. ` +\n \"Agent names can only contain letters, numbers, hyphens, underscores, and spaces.\",\n );\n }\n return path.join(userDeepagentsDir, agentName);\n },\n\n ensureAgentDir(agentName: string): string {\n const agentDir = this.getAgentDir(agentName);\n fs.mkdirSync(agentDir, { recursive: true });\n return agentDir;\n },\n\n getUserAgentMdPath(agentName: string): string {\n return path.join(this.getAgentDir(agentName), \"agent.md\");\n },\n\n getProjectAgentMdPath(): string | null {\n if (!projectRoot) {\n return null;\n }\n return path.join(projectRoot, \".deepagents\", \"agent.md\");\n },\n\n getUserSkillsDir(agentName: string): string {\n return path.join(this.getAgentDir(agentName), \"skills\");\n },\n\n ensureUserSkillsDir(agentName: string): string {\n const skillsDir = this.getUserSkillsDir(agentName);\n fs.mkdirSync(skillsDir, { recursive: true });\n return skillsDir;\n },\n\n getProjectSkillsDir(): string | null {\n if (!projectRoot) {\n return null;\n }\n return path.join(projectRoot, \".deepagents\", \"skills\");\n },\n\n ensureProjectSkillsDir(): string | null {\n const skillsDir = this.getProjectSkillsDir();\n if (!skillsDir) {\n return null;\n }\n fs.mkdirSync(skillsDir, { recursive: true });\n return skillsDir;\n },\n\n ensureProjectDeepagentsDir(): string | null {\n if (!projectRoot) {\n return null;\n }\n const deepagentsDir = path.join(projectRoot, \".deepagents\");\n fs.mkdirSync(deepagentsDir, { recursive: true });\n return deepagentsDir;\n },\n };\n}\n","/**\n * Middleware for loading agent-specific long-term memory into the system prompt.\n *\n * This middleware loads the agent's long-term memory from agent.md files\n * and injects it into the system prompt. Memory is loaded from:\n * - User memory: ~/.deepagents/{agent_name}/agent.md\n * - Project memory: {project_root}/.deepagents/agent.md\n *\n * @deprecated Use `createMemoryMiddleware` from `./memory.js` instead.\n * This middleware uses direct filesystem access (Node.js fs module) which is not\n * portable across backends. The `createMemoryMiddleware` function uses the\n * `BackendProtocol` abstraction and follows the AGENTS.md specification.\n *\n * Migration example:\n * ```typescript\n * // Before (deprecated):\n * import { createAgentMemoryMiddleware } from \"./agent-memory.js\";\n * const middleware = createAgentMemoryMiddleware({ settings, assistantId });\n *\n * // After (recommended):\n * import { createMemoryMiddleware } from \"./memory.js\";\n * import { FilesystemBackend } from \"../backends/filesystem.js\";\n *\n * const middleware = createMemoryMiddleware({\n * backend: new FilesystemBackend({ rootDir: \"/\" }),\n * sources: [\n * `~/.deepagents/${assistantId}/AGENTS.md`,\n * `${projectRoot}/.deepagents/AGENTS.md`,\n * ],\n * });\n * ```\n */\n\nimport fs from \"node:fs\";\nimport { z } from \"zod\";\nimport {\n createMiddleware,\n /**\n * required for type inference\n */\n type AgentMiddleware as _AgentMiddleware,\n} from \"langchain\";\n\nimport type { Settings } from \"../config.js\";\n\n/**\n * Options for the agent memory middleware.\n */\nexport interface AgentMemoryMiddlewareOptions {\n /** Settings instance with project detection and paths */\n settings: Settings;\n\n /** The agent identifier */\n assistantId: string;\n\n /** Optional custom template for injecting agent memory into system prompt */\n systemPromptTemplate?: string;\n}\n\n/**\n * State schema for agent memory middleware.\n */\nconst AgentMemoryStateSchema = z.object({\n /** Personal preferences from ~/.deepagents/{agent}/ (applies everywhere) */\n userMemory: z.string().optional(),\n\n /** Project-specific context (loaded from project root) */\n projectMemory: z.string().optional(),\n});\n\n/**\n * Default template for memory injection.\n */\nconst DEFAULT_MEMORY_TEMPLATE = `<user_memory>\n{user_memory}\n</user_memory>\n\n<project_memory>\n{project_memory}\n</project_memory>`;\n\n/**\n * Long-term Memory Documentation system prompt.\n */\nconst LONGTERM_MEMORY_SYSTEM_PROMPT = `\n\n## Long-term Memory\n\nYour long-term memory is stored in files on the filesystem and persists across sessions.\n\n**User Memory Location**: \\`{agent_dir_absolute}\\` (displays as \\`{agent_dir_display}\\`)\n**Project Memory Location**: {project_memory_info}\n\nYour system prompt is loaded from TWO sources at startup:\n1. **User agent.md**: \\`{agent_dir_absolute}/agent.md\\` - Your personal preferences across all projects\n2. **Project agent.md**: Loaded from project root if available - Project-specific instructions\n\nProject-specific agent.md is loaded from these locations (both combined if both exist):\n- \\`[project-root]/.deepagents/agent.md\\` (preferred)\n- \\`[project-root]/agent.md\\` (fallback, but also included if both exist)\n\n**When to CHECK/READ memories (CRITICAL - do this FIRST):**\n- **At the start of ANY new session**: Check both user and project memories\n - User: \\`ls {agent_dir_absolute}\\`\n - Project: \\`ls {project_deepagents_dir}\\` (if in a project)\n- **BEFORE answering questions**: If asked \"what do you know about X?\" or \"how do I do Y?\", check project memories FIRST, then user\n- **When user asks you to do something**: Check if you have project-specific guides or examples\n- **When user references past work**: Search project memory files for related context\n\n**Memory-first response pattern:**\n1. User asks a question → Check project directory first: \\`ls {project_deepagents_dir}\\`\n2. If relevant files exist → Read them with \\`read_file '{project_deepagents_dir}/[filename]'\\`\n3. Check user memory if needed → \\`ls {agent_dir_absolute}\\`\n4. Base your answer on saved knowledge supplemented by general knowledge\n\n**When to update memories:**\n- **IMMEDIATELY when the user describes your role or how you should behave**\n- **IMMEDIATELY when the user gives feedback on your work** - Update memories to capture what was wrong and how to do it better\n- When the user explicitly asks you to remember something\n- When patterns or preferences emerge (coding styles, conventions, workflows)\n- After significant work where context would help in future sessions\n\n**Learning from feedback:**\n- When user says something is better/worse, capture WHY and encode it as a pattern\n- Each correction is a chance to improve permanently - don't just fix the immediate issue, update your instructions\n- When user says \"you should remember X\" or \"be careful about Y\", treat this as HIGH PRIORITY - update memories IMMEDIATELY\n- Look for the underlying principle behind corrections, not just the specific mistake\n\n## Deciding Where to Store Memory\n\nWhen writing or updating agent memory, decide whether each fact, configuration, or behavior belongs in:\n\n### User Agent File: \\`{agent_dir_absolute}/agent.md\\`\n→ Describes the agent's **personality, style, and universal behavior** across all projects.\n\n**Store here:**\n- Your general tone and communication style\n- Universal coding preferences (formatting, comment style, etc.)\n- General workflows and methodologies you follow\n- Tool usage patterns that apply everywhere\n- Personal preferences that don't change per-project\n\n**Examples:**\n- \"Be concise and direct in responses\"\n- \"Always use type hints in Python\"\n- \"Prefer functional programming patterns\"\n\n### Project Agent File: \\`{project_deepagents_dir}/agent.md\\`\n→ Describes **how this specific project works** and **how the agent should behave here only.**\n\n**Store here:**\n- Project-specific architecture and design patterns\n- Coding conventions specific to this codebase\n- Project structure and organization\n- Testing strategies for this project\n- Deployment processes and workflows\n- Team conventions and guidelines\n\n**Examples:**\n- \"This project uses FastAPI with SQLAlchemy\"\n- \"Tests go in tests/ directory mirroring src/ structure\"\n- \"All API changes require updating OpenAPI spec\"\n\n### Project Memory Files: \\`{project_deepagents_dir}/*.md\\`\n→ Use for **project-specific reference information** and structured notes.\n\n**Store here:**\n- API design documentation\n- Architecture decisions and rationale\n- Deployment procedures\n- Common debugging patterns\n- Onboarding information\n\n**Examples:**\n- \\`{project_deepagents_dir}/api-design.md\\` - REST API patterns used\n- \\`{project_deepagents_dir}/architecture.md\\` - System architecture overview\n- \\`{project_deepagents_dir}/deployment.md\\` - How to deploy this project\n\n### File Operations:\n\n**User memory:**\n\\`\\`\\`\nls {agent_dir_absolute} # List user memory files\nread_file '{agent_dir_absolute}/agent.md' # Read user preferences\nedit_file '{agent_dir_absolute}/agent.md' ... # Update user preferences\n\\`\\`\\`\n\n**Project memory (preferred for project-specific information):**\n\\`\\`\\`\nls {project_deepagents_dir} # List project memory files\nread_file '{project_deepagents_dir}/agent.md' # Read project instructions\nedit_file '{project_deepagents_dir}/agent.md' ... # Update project instructions\nwrite_file '{project_deepagents_dir}/agent.md' ... # Create project memory file\n\\`\\`\\`\n\n**Important**:\n- Project memory files are stored in \\`.deepagents/\\` inside the project root\n- Always use absolute paths for file operations\n- Check project memories BEFORE user when answering project-specific questions`;\n\n/**\n * Create middleware for loading agent-specific long-term memory.\n *\n * This middleware loads the agent's long-term memory from a file (agent.md)\n * and injects it into the system prompt. The memory is loaded once at the\n * start of the conversation and stored in state.\n *\n * @param options - Configuration options\n * @returns AgentMiddleware for memory loading and injection\n *\n * @deprecated Use `createMemoryMiddleware` from `./memory.js` instead.\n * This function uses direct filesystem access which limits portability.\n */\nexport function createAgentMemoryMiddleware(\n options: AgentMemoryMiddlewareOptions,\n) {\n const { settings, assistantId, systemPromptTemplate } = options;\n\n // Compute paths\n const agentDir = settings.getAgentDir(assistantId);\n const agentDirDisplay = `~/.deepagents/${assistantId}`;\n const agentDirAbsolute = agentDir;\n const projectRoot = settings.projectRoot;\n\n // Build project memory info for documentation\n const projectMemoryInfo = projectRoot\n ? `\\`${projectRoot}\\` (detected)`\n : \"None (not in a git project)\";\n\n // Build project deepagents directory path\n const projectDeepagentsDir = projectRoot\n ? `${projectRoot}/.deepagents`\n : \"[project-root]/.deepagents (not in a project)\";\n\n const template = systemPromptTemplate || DEFAULT_MEMORY_TEMPLATE;\n\n return createMiddleware({\n name: \"AgentMemoryMiddleware\",\n stateSchema: AgentMemoryStateSchema as any,\n\n beforeAgent(state: any) {\n const result: Record<string, string> = {};\n\n // Load user memory if not already in state\n if (!(\"userMemory\" in state)) {\n const userPath = settings.getUserAgentMdPath(assistantId);\n if (fs.existsSync(userPath)) {\n try {\n result.userMemory = fs.readFileSync(userPath, \"utf-8\");\n } catch {\n // Ignore read errors\n }\n }\n }\n\n // Load project memory if not already in state\n if (!(\"projectMemory\" in state)) {\n const projectPath = settings.getProjectAgentMdPath();\n if (projectPath && fs.existsSync(projectPath)) {\n try {\n result.projectMemory = fs.readFileSync(projectPath, \"utf-8\");\n } catch {\n // Ignore read errors\n }\n }\n }\n\n return Object.keys(result).length > 0 ? result : undefined;\n },\n\n wrapModelCall(request: any, handler: any) {\n // Extract memory from state\n const userMemory = request.state?.userMemory;\n const projectMemory = request.state?.projectMemory;\n const baseSystemPrompt = request.systemPrompt || \"\";\n\n // Format memory section with both memories\n const memorySection = template\n .replace(\"{user_memory}\", userMemory || \"(No user agent.md)\")\n .replace(\"{project_memory}\", projectMemory || \"(No project agent.md)\");\n\n // Format long-term memory documentation\n const memoryDocs = LONGTERM_MEMORY_SYSTEM_PROMPT.replaceAll(\n \"{agent_dir_absolute}\",\n agentDirAbsolute,\n )\n .replaceAll(\"{agent_dir_display}\", agentDirDisplay)\n .replaceAll(\"{project_memory_info}\", projectMemoryInfo)\n .replaceAll(\"{project_deepagents_dir}\", projectDeepagentsDir);\n\n // Memory content at start, base prompt in middle, documentation at end\n let systemPrompt = memorySection;\n if (baseSystemPrompt) {\n systemPrompt += \"\\n\\n\" + baseSystemPrompt;\n }\n systemPrompt += \"\\n\\n\" + memoryDocs;\n\n return handler({ ...request, systemPrompt });\n },\n });\n}\n","/**\n * Skill loader for parsing and loading agent skills from SKILL.md files.\n *\n * This module implements Anthropic's agent skills pattern with YAML frontmatter parsing.\n * Each skill is a directory containing a SKILL.md file with:\n * - YAML frontmatter (name, description required)\n * - Markdown instructions for the agent\n * - Optional supporting files (scripts, configs, etc.)\n *\n * @example\n * ```markdown\n * ---\n * name: web-research\n * description: Structured approach to conducting thorough web research\n * ---\n *\n * # Web Research Skill\n *\n * ## When to Use\n * - User asks you to research a topic\n * ...\n * ```\n *\n * @see https://agentskills.io/specification\n */\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport yaml from \"yaml\";\n\n/** Maximum size for SKILL.md files (10MB) */\nexport const MAX_SKILL_FILE_SIZE = 10 * 1024 * 1024;\n\n/** Agent Skills spec constraints */\nexport const MAX_SKILL_NAME_LENGTH = 64;\nexport const MAX_SKILL_DESCRIPTION_LENGTH = 1024;\n\n/** Pattern for validating skill names per Agent Skills spec */\nconst SKILL_NAME_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;\n\n/** Pattern for extracting YAML frontmatter */\nconst FRONTMATTER_PATTERN = /^---\\s*\\n([\\s\\S]*?)\\n---\\s*\\n/;\n\n/**\n * Metadata for a skill per Agent Skills spec.\n * @see https://agentskills.io/specification\n */\nexport interface SkillMetadata {\n /** Name of the skill (max 64 chars, lowercase alphanumeric and hyphens) */\n name: string;\n\n /** Description of what the skill does (max 1024 chars) */\n description: string;\n\n /** Absolute path to the SKILL.md file */\n path: string;\n\n /** Source of the skill ('user' or 'project') */\n source: \"user\" | \"project\";\n\n /** Optional: License name or reference to bundled license file */\n license?: string;\n\n /** Optional: Environment requirements (max 500 chars) */\n compatibility?: string;\n\n /** Optional: Arbitrary key-value mapping for additional metadata */\n metadata?: Record<string, string>;\n\n /** Optional: Space-delimited list of pre-approved tools */\n allowedTools?: string;\n}\n\n/**\n * Options for listing skills.\n */\nexport interface ListSkillsOptions {\n /** Path to user-level skills directory */\n userSkillsDir?: string | null;\n\n /** Path to project-level skills directory */\n projectSkillsDir?: string | null;\n}\n\n/**\n * Result of skill name validation.\n */\ninterface ValidationResult {\n valid: boolean;\n error?: string;\n}\n\n/**\n * Check if a path is safely contained within base_dir.\n *\n * This prevents directory traversal attacks via symlinks or path manipulation.\n * The function resolves both paths to their canonical form (following symlinks)\n * and verifies that the target path is within the base directory.\n *\n * @param targetPath - The path to validate\n * @param baseDir - The base directory that should contain the path\n * @returns True if the path is safely within baseDir, false otherwise\n */\nfunction isSafePath(targetPath: string, baseDir: string): boolean {\n try {\n // Resolve both paths to their canonical form (follows symlinks)\n const resolvedPath = fs.realpathSync(targetPath);\n const resolvedBase = fs.realpathSync(baseDir);\n\n // Check if the resolved path is within the base directory\n return (\n resolvedPath.startsWith(resolvedBase + path.sep) ||\n resolvedPath === resolvedBase\n );\n } catch {\n // Error resolving paths (e.g., circular symlinks, too many levels)\n return false;\n }\n}\n\n/**\n * Validate skill name per Agent Skills spec.\n *\n * Requirements:\n * - Max 64 characters\n * - Lowercase alphanumeric and hyphens only (a-z, 0-9, -)\n * - Cannot start or end with hyphen\n * - No consecutive hyphens\n * - Must match parent directory name\n *\n * @param name - The skill name from YAML frontmatter\n * @param directoryName - The parent directory name\n * @returns Validation result with error message if invalid\n */\nfunction validateSkillName(\n name: string,\n directoryName: string,\n): ValidationResult {\n if (!name) {\n return { valid: false, error: \"name is required\" };\n }\n if (name.length > MAX_SKILL_NAME_LENGTH) {\n return { valid: false, error: \"name exceeds 64 characters\" };\n }\n // Pattern: lowercase alphanumeric, single hyphens between segments, no start/end hyphen\n if (!SKILL_NAME_PATTERN.test(name)) {\n return {\n valid: false,\n error: \"name must be lowercase alphanumeric with single hyphens only\",\n };\n }\n if (name !== directoryName) {\n return {\n valid: false,\n error: `name '${name}' must match directory name '${directoryName}'`,\n };\n }\n return { valid: true };\n}\n\n/**\n * Parse YAML frontmatter from content.\n *\n * @param content - The file content\n * @returns Parsed frontmatter object, or null if parsing fails\n */\nfunction parseFrontmatter(content: string): Record<string, unknown> | null {\n const match = content.match(FRONTMATTER_PATTERN);\n if (!match) {\n return null;\n }\n\n try {\n const parsed = yaml.parse(match[1]);\n return typeof parsed === \"object\" && parsed !== null ? parsed : null;\n } catch {\n return null;\n }\n}\n\n/**\n * Parse YAML frontmatter from a SKILL.md file per Agent Skills spec.\n *\n * @param skillMdPath - Path to the SKILL.md file\n * @param source - Source of the skill ('user' or 'project')\n * @returns SkillMetadata with all fields, or null if parsing fails\n */\nexport function parseSkillMetadata(\n skillMdPath: string,\n source: \"user\" | \"project\",\n): SkillMetadata | null {\n try {\n // Security: Check file size to prevent DoS attacks\n const stats = fs.statSync(skillMdPath);\n if (stats.size > MAX_SKILL_FILE_SIZE) {\n // eslint-disable-next-line no-console\n console.warn(\n `Skipping ${skillMdPath}: file too large (${stats.size} bytes)`,\n );\n return null;\n }\n\n const content = fs.readFileSync(skillMdPath, \"utf-8\");\n const frontmatter = parseFrontmatter(content);\n\n if (!frontmatter) {\n // eslint-disable-next-line no-console\n console.warn(`Skipping ${skillMdPath}: no valid YAML frontmatter found`);\n return null;\n }\n\n // Validate required fields\n const name = frontmatter.name;\n const description = frontmatter.description;\n\n if (!name || !description) {\n // eslint-disable-next-line no-console\n console.warn(\n `Skipping ${skillMdPath}: missing required 'name' or 'description'`,\n );\n return null;\n }\n\n // Validate name format per spec (warn but still load for backwards compatibility)\n const directoryName = path.basename(path.dirname(skillMdPath));\n const validation = validateSkillName(String(name), directoryName);\n if (!validation.valid) {\n // eslint-disable-next-line no-console\n console.warn(\n `Skill '${name}' in ${skillMdPath} does not follow Agent Skills spec: ${validation.error}. ` +\n \"Consider renaming to be spec-compliant.\",\n );\n }\n\n // Truncate description if too long (spec: max 1024 chars)\n let descriptionStr = String(description);\n if (descriptionStr.length > MAX_SKILL_DESCRIPTION_LENGTH) {\n // eslint-disable-next-line no-console\n console.warn(\n `Description exceeds ${MAX_SKILL_DESCRIPTION_LENGTH} chars in ${skillMdPath}, truncating`,\n );\n descriptionStr = descriptionStr.slice(0, MAX_SKILL_DESCRIPTION_LENGTH);\n }\n\n return {\n name: String(name),\n description: descriptionStr,\n path: skillMdPath,\n source,\n license: frontmatter.license ? String(frontmatter.license) : undefined,\n compatibility: frontmatter.compatibility\n ? String(frontmatter.compatibility)\n : undefined,\n metadata:\n frontmatter.metadata && typeof frontmatter.metadata === \"object\"\n ? (frontmatter.metadata as Record<string, string>)\n : undefined,\n allowedTools: frontmatter[\"allowed-tools\"]\n ? String(frontmatter[\"allowed-tools\"])\n : undefined,\n };\n } catch (error) {\n // eslint-disable-next-line no-console\n console.warn(`Error reading ${skillMdPath}: ${error}`);\n return null;\n }\n}\n\n/**\n * List all skills from a single skills directory (internal helper).\n *\n * Scans the skills directory for subdirectories containing SKILL.md files,\n * parses YAML frontmatter, and returns skill metadata.\n *\n * Skills are organized as:\n * ```\n * skills/\n * ├── skill-name/\n * │ ├── SKILL.md # Required: instructions with YAML frontmatter\n * │ ├── script.py # Optional: supporting files\n * │ └── config.json # Optional: supporting files\n * ```\n *\n * @param skillsDir - Path to the skills directory\n * @param source - Source of the skills ('user' or 'project')\n * @returns List of skill metadata\n */\nfunction listSkillsFromDir(\n skillsDir: string,\n source: \"user\" | \"project\",\n): SkillMetadata[] {\n // Check if skills directory exists\n const expandedDir = skillsDir.startsWith(\"~\")\n ? path.join(\n process.env.HOME || process.env.USERPROFILE || \"\",\n skillsDir.slice(1),\n )\n : skillsDir;\n\n if (!fs.existsSync(expandedDir)) {\n return [];\n }\n\n // Resolve base directory to canonical path for security checks\n let resolvedBase: string;\n try {\n resolvedBase = fs.realpathSync(expandedDir);\n } catch {\n // Can't resolve base directory, fail safe\n return [];\n }\n\n const skills: SkillMetadata[] = [];\n\n // Iterate through subdirectories\n let entries: fs.Dirent[];\n try {\n entries = fs.readdirSync(resolvedBase, { withFileTypes: true });\n } catch {\n return [];\n }\n\n for (const entry of entries) {\n const skillDir = path.join(resolvedBase, entry.name);\n\n // Security: Catch symlinks pointing outside the skills directory\n if (!isSafePath(skillDir, resolvedBase)) {\n continue;\n }\n\n if (!entry.isDirectory()) {\n continue;\n }\n\n // Look for SKILL.md file\n const skillMdPath = path.join(skillDir, \"SKILL.md\");\n if (!fs.existsSync(skillMdPath)) {\n continue;\n }\n\n // Security: Validate SKILL.md path is safe before reading\n if (!isSafePath(skillMdPath, resolvedBase)) {\n continue;\n }\n\n // Parse metadata\n const metadata = parseSkillMetadata(skillMdPath, source);\n if (metadata) {\n skills.push(metadata);\n }\n }\n\n return skills;\n}\n\n/**\n * List skills from user and/or project directories.\n *\n * When both directories are provided, project skills with the same name as\n * user skills will override them.\n *\n * @param options - Options specifying which directories to search\n * @returns Merged list of skill metadata from both sources, with project skills\n * taking precedence over user skills when names conflict\n */\nexport function listSkills(options: ListSkillsOptions): SkillMetadata[] {\n const allSkills: Map<string, SkillMetadata> = new Map();\n\n // Load user skills first (foundation)\n if (options.userSkillsDir) {\n const userSkills = listSkillsFromDir(options.userSkillsDir, \"user\");\n for (const skill of userSkills) {\n allSkills.set(skill.name, skill);\n }\n }\n\n // Load project skills second (override/augment)\n if (options.projectSkillsDir) {\n const projectSkills = listSkillsFromDir(\n options.projectSkillsDir,\n \"project\",\n );\n for (const skill of projectSkills) {\n // Project skills override user skills with the same name\n allSkills.set(skill.name, skill);\n }\n }\n\n return Array.from(allSkills.values());\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuRA,SAAgB,iBACd,SACmC;AACnC,QACE,OAAQ,QAAmC,YAAY,cACvD,OAAQ,QAAmC,OAAO;;;;;;;;;;;;AC/QtD,MAAa,wBACX;AACF,MAAa,kBAAkB;AAC/B,MAAa,oBAAoB;;;;;;AAUjC,SAAgB,mBAAmB,YAA4B;AAC7D,QAAO,WAAW,QAAQ,OAAO,IAAI,CAAC,QAAQ,OAAO,IAAI,CAAC,QAAQ,OAAO,I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