@truefoundry/trueforge-core 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/agent-session/ITurnResourceResolver.d.ts +69 -0
- package/dist/agent-session/ITurnResourceResolver.d.ts.map +1 -0
- package/dist/agent-session/ITurnResourceResolver.js +19 -0
- package/dist/agent-session/ITurnResourceResolver.js.map +1 -0
- package/dist/agent-session/ITurnResourceResolver.mjs +1 -0
- package/dist/agent-session/ITurnResourceResolver.mjs.map +1 -0
- package/dist/agent-session/SessionHandle.d.ts +81 -0
- package/dist/agent-session/SessionHandle.d.ts.map +1 -0
- package/dist/agent-session/SessionHandle.js +392 -0
- package/dist/agent-session/SessionHandle.js.map +1 -0
- package/dist/agent-session/SessionHandle.mjs +371 -0
- package/dist/agent-session/SessionHandle.mjs.map +1 -0
- package/dist/agent-session/Sessions.d.ts +25 -0
- package/dist/agent-session/Sessions.d.ts.map +1 -0
- package/dist/agent-session/Sessions.js +71 -0
- package/dist/agent-session/Sessions.js.map +1 -0
- package/dist/agent-session/Sessions.mjs +46 -0
- package/dist/agent-session/Sessions.mjs.map +1 -0
- package/dist/agent-session/TurnHandle.d.ts +88 -0
- package/dist/agent-session/TurnHandle.d.ts.map +1 -0
- package/dist/agent-session/TurnHandle.js +490 -0
- package/dist/agent-session/TurnHandle.js.map +1 -0
- package/dist/agent-session/TurnHandle.mjs +469 -0
- package/dist/agent-session/TurnHandle.mjs.map +1 -0
- package/dist/agent-session/TurnResourceResolver.d.ts +154 -0
- package/dist/agent-session/TurnResourceResolver.d.ts.map +1 -0
- package/dist/agent-session/TurnResourceResolver.js +178 -0
- package/dist/agent-session/TurnResourceResolver.js.map +1 -0
- package/dist/agent-session/TurnResourceResolver.mjs +153 -0
- package/dist/agent-session/TurnResourceResolver.mjs.map +1 -0
- package/dist/agent-session/builtinsFromSpec.d.ts +19 -0
- package/dist/agent-session/builtinsFromSpec.d.ts.map +1 -0
- package/dist/agent-session/builtinsFromSpec.js +73 -0
- package/dist/agent-session/builtinsFromSpec.js.map +1 -0
- package/dist/agent-session/builtinsFromSpec.mjs +48 -0
- package/dist/agent-session/builtinsFromSpec.mjs.map +1 -0
- package/dist/agent-session/index.d.ts +29 -0
- package/dist/agent-session/index.d.ts.map +1 -0
- package/dist/agent-session/index.js +117 -0
- package/dist/agent-session/index.js.map +1 -0
- package/dist/agent-session/index.mjs +84 -0
- package/dist/agent-session/index.mjs.map +1 -0
- package/dist/agent-session/models/SessionRecord.d.ts +34 -0
- package/dist/agent-session/models/SessionRecord.d.ts.map +1 -0
- package/dist/agent-session/models/SessionRecord.js +19 -0
- package/dist/agent-session/models/SessionRecord.js.map +1 -0
- package/dist/agent-session/models/SessionRecord.mjs +1 -0
- package/dist/agent-session/models/SessionRecord.mjs.map +1 -0
- package/dist/agent-session/models/TurnRecord.d.ts +37 -0
- package/dist/agent-session/models/TurnRecord.d.ts.map +1 -0
- package/dist/agent-session/models/TurnRecord.js +31 -0
- package/dist/agent-session/models/TurnRecord.js.map +1 -0
- package/dist/agent-session/models/TurnRecord.mjs +6 -0
- package/dist/agent-session/models/TurnRecord.mjs.map +1 -0
- package/dist/agent-session/schemas/agentSpec.d.ts +142 -0
- package/dist/agent-session/schemas/agentSpec.d.ts.map +1 -0
- package/dist/agent-session/schemas/agentSpec.js +170 -0
- package/dist/agent-session/schemas/agentSpec.js.map +1 -0
- package/dist/agent-session/schemas/agentSpec.mjs +154 -0
- package/dist/agent-session/schemas/agentSpec.mjs.map +1 -0
- package/dist/agent-session/schemas/events.d.ts +1037 -0
- package/dist/agent-session/schemas/events.d.ts.map +1 -0
- package/dist/agent-session/schemas/events.js +80 -0
- package/dist/agent-session/schemas/events.js.map +1 -0
- package/dist/agent-session/schemas/events.mjs +69 -0
- package/dist/agent-session/schemas/events.mjs.map +1 -0
- package/dist/agent-session/schemas/pagination.d.ts +9 -0
- package/dist/agent-session/schemas/pagination.d.ts.map +1 -0
- package/dist/agent-session/schemas/pagination.js +36 -0
- package/dist/agent-session/schemas/pagination.js.map +1 -0
- package/dist/agent-session/schemas/pagination.mjs +11 -0
- package/dist/agent-session/schemas/pagination.mjs.map +1 -0
- package/dist/agent-session/schemas/session.d.ts +319 -0
- package/dist/agent-session/schemas/session.d.ts.map +1 -0
- package/dist/agent-session/schemas/session.js +58 -0
- package/dist/agent-session/schemas/session.js.map +1 -0
- package/dist/agent-session/schemas/session.mjs +30 -0
- package/dist/agent-session/schemas/session.mjs.map +1 -0
- package/dist/agent-session/schemas/turn.d.ts +494 -0
- package/dist/agent-session/schemas/turn.d.ts.map +1 -0
- package/dist/agent-session/schemas/turn.js +131 -0
- package/dist/agent-session/schemas/turn.js.map +1 -0
- package/dist/agent-session/schemas/turn.mjs +102 -0
- package/dist/agent-session/schemas/turn.mjs.map +1 -0
- package/dist/agent-session/store/ISessionStore.d.ts +310 -0
- package/dist/agent-session/store/ISessionStore.d.ts.map +1 -0
- package/dist/agent-session/store/ISessionStore.js +19 -0
- package/dist/agent-session/store/ISessionStore.js.map +1 -0
- package/dist/agent-session/store/ISessionStore.mjs +1 -0
- package/dist/agent-session/store/ISessionStore.mjs.map +1 -0
- package/dist/agent-session/store/InMemorySessionStore.d.ts +55 -0
- package/dist/agent-session/store/InMemorySessionStore.d.ts.map +1 -0
- package/dist/agent-session/store/InMemorySessionStore.js +475 -0
- package/dist/agent-session/store/InMemorySessionStore.js.map +1 -0
- package/dist/agent-session/store/InMemorySessionStore.mjs +461 -0
- package/dist/agent-session/store/InMemorySessionStore.mjs.map +1 -0
- package/dist/agent-session/store/OffsetPageToken.d.ts +13 -0
- package/dist/agent-session/store/OffsetPageToken.d.ts.map +1 -0
- package/dist/agent-session/store/OffsetPageToken.js +60 -0
- package/dist/agent-session/store/OffsetPageToken.js.map +1 -0
- package/dist/agent-session/store/OffsetPageToken.mjs +32 -0
- package/dist/agent-session/store/OffsetPageToken.mjs.map +1 -0
- package/dist/agent-session/store/PageToken.d.ts +4 -0
- package/dist/agent-session/store/PageToken.d.ts.map +1 -0
- package/dist/agent-session/store/PageToken.js +45 -0
- package/dist/agent-session/store/PageToken.js.map +1 -0
- package/dist/agent-session/store/PageToken.mjs +19 -0
- package/dist/agent-session/store/PageToken.mjs.map +1 -0
- package/dist/agent-session/store/SessionEventPageToken.d.ts +14 -0
- package/dist/agent-session/store/SessionEventPageToken.d.ts.map +1 -0
- package/dist/agent-session/store/SessionEventPageToken.js +70 -0
- package/dist/agent-session/store/SessionEventPageToken.js.map +1 -0
- package/dist/agent-session/store/SessionEventPageToken.mjs +42 -0
- package/dist/agent-session/store/SessionEventPageToken.mjs.map +1 -0
- package/dist/agent-session/store/SessionListPageToken.d.ts +24 -0
- package/dist/agent-session/store/SessionListPageToken.d.ts.map +1 -0
- package/dist/agent-session/store/SessionListPageToken.js +66 -0
- package/dist/agent-session/store/SessionListPageToken.js.map +1 -0
- package/dist/agent-session/store/SessionListPageToken.mjs +38 -0
- package/dist/agent-session/store/SessionListPageToken.mjs.map +1 -0
- package/dist/agent-session/store/SessionStoreErrors.d.ts +43 -0
- package/dist/agent-session/store/SessionStoreErrors.d.ts.map +1 -0
- package/dist/agent-session/store/SessionStoreErrors.js +126 -0
- package/dist/agent-session/store/SessionStoreErrors.js.map +1 -0
- package/dist/agent-session/store/SessionStoreErrors.mjs +92 -0
- package/dist/agent-session/store/SessionStoreErrors.mjs.map +1 -0
- package/dist/agent-session/store/assertCreateTurnThreadDelta.d.ts +18 -0
- package/dist/agent-session/store/assertCreateTurnThreadDelta.d.ts.map +1 -0
- package/dist/agent-session/store/assertCreateTurnThreadDelta.js +67 -0
- package/dist/agent-session/store/assertCreateTurnThreadDelta.js.map +1 -0
- package/dist/agent-session/store/assertCreateTurnThreadDelta.mjs +42 -0
- package/dist/agent-session/store/assertCreateTurnThreadDelta.mjs.map +1 -0
- package/dist/core/InstructionBuilder.d.ts +71 -0
- package/dist/core/InstructionBuilder.d.ts.map +1 -0
- package/dist/core/InstructionBuilder.js +95 -0
- package/dist/core/InstructionBuilder.js.map +1 -0
- package/dist/core/InstructionBuilder.mjs +69 -0
- package/dist/core/InstructionBuilder.mjs.map +1 -0
- package/dist/core/capabilities/AgentCapability.d.ts +32 -0
- package/dist/core/capabilities/AgentCapability.d.ts.map +1 -0
- package/dist/core/capabilities/AgentCapability.js +19 -0
- package/dist/core/capabilities/AgentCapability.js.map +1 -0
- package/dist/core/capabilities/AgentCapability.mjs +1 -0
- package/dist/core/capabilities/AgentCapability.mjs.map +1 -0
- package/dist/core/capabilities/AgentContextProcessor.d.ts +30 -0
- package/dist/core/capabilities/AgentContextProcessor.d.ts.map +1 -0
- package/dist/core/capabilities/AgentContextProcessor.js +19 -0
- package/dist/core/capabilities/AgentContextProcessor.js.map +1 -0
- package/dist/core/capabilities/AgentContextProcessor.mjs +1 -0
- package/dist/core/capabilities/AgentContextProcessor.mjs.map +1 -0
- package/dist/core/capabilities/ToolResponseProcessor.d.ts +12 -0
- package/dist/core/capabilities/ToolResponseProcessor.d.ts.map +1 -0
- package/dist/core/capabilities/ToolResponseProcessor.js +19 -0
- package/dist/core/capabilities/ToolResponseProcessor.js.map +1 -0
- package/dist/core/capabilities/ToolResponseProcessor.mjs +1 -0
- package/dist/core/capabilities/ToolResponseProcessor.mjs.map +1 -0
- package/dist/core/capabilities/builtins/AskUserQuestion.d.ts +11 -0
- package/dist/core/capabilities/builtins/AskUserQuestion.d.ts.map +1 -0
- package/dist/core/capabilities/builtins/AskUserQuestion.js +90 -0
- package/dist/core/capabilities/builtins/AskUserQuestion.js.map +1 -0
- package/dist/core/capabilities/builtins/AskUserQuestion.mjs +64 -0
- package/dist/core/capabilities/builtins/AskUserQuestion.mjs.map +1 -0
- package/dist/core/capabilities/builtins/ContextCompaction.d.ts +23 -0
- package/dist/core/capabilities/builtins/ContextCompaction.d.ts.map +1 -0
- package/dist/core/capabilities/builtins/ContextCompaction.js +235 -0
- package/dist/core/capabilities/builtins/ContextCompaction.js.map +1 -0
- package/dist/core/capabilities/builtins/ContextCompaction.mjs +208 -0
- package/dist/core/capabilities/builtins/ContextCompaction.mjs.map +1 -0
- package/dist/core/capabilities/builtins/CurrentDateTime.d.ts +17 -0
- package/dist/core/capabilities/builtins/CurrentDateTime.d.ts.map +1 -0
- package/dist/core/capabilities/builtins/CurrentDateTime.js +75 -0
- package/dist/core/capabilities/builtins/CurrentDateTime.js.map +1 -0
- package/dist/core/capabilities/builtins/CurrentDateTime.mjs +47 -0
- package/dist/core/capabilities/builtins/CurrentDateTime.mjs.map +1 -0
- package/dist/core/capabilities/builtins/DynamicSubAgents.d.ts +39 -0
- package/dist/core/capabilities/builtins/DynamicSubAgents.d.ts.map +1 -0
- package/dist/core/capabilities/builtins/DynamicSubAgents.js +176 -0
- package/dist/core/capabilities/builtins/DynamicSubAgents.js.map +1 -0
- package/dist/core/capabilities/builtins/DynamicSubAgents.mjs +134 -0
- package/dist/core/capabilities/builtins/DynamicSubAgents.mjs.map +1 -0
- package/dist/core/capabilities/builtins/LargeToolResponse.d.ts +40 -0
- package/dist/core/capabilities/builtins/LargeToolResponse.d.ts.map +1 -0
- package/dist/core/capabilities/builtins/LargeToolResponse.js +225 -0
- package/dist/core/capabilities/builtins/LargeToolResponse.js.map +1 -0
- package/dist/core/capabilities/builtins/LargeToolResponse.mjs +186 -0
- package/dist/core/capabilities/builtins/LargeToolResponse.mjs.map +1 -0
- package/dist/core/capabilities/builtins/OpenUI.d.ts +31 -0
- package/dist/core/capabilities/builtins/OpenUI.d.ts.map +1 -0
- package/dist/core/capabilities/builtins/OpenUI.js +376 -0
- package/dist/core/capabilities/builtins/OpenUI.js.map +1 -0
- package/dist/core/capabilities/builtins/OpenUI.mjs +326 -0
- package/dist/core/capabilities/builtins/OpenUI.mjs.map +1 -0
- package/dist/core/errors.d.ts +33 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +88 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/errors.mjs +57 -0
- package/dist/core/errors.mjs.map +1 -0
- package/dist/core/events/PassthroughEvents.d.ts +30 -0
- package/dist/core/events/PassthroughEvents.d.ts.map +1 -0
- package/dist/core/events/PassthroughEvents.js +19 -0
- package/dist/core/events/PassthroughEvents.js.map +1 -0
- package/dist/core/events/PassthroughEvents.mjs +1 -0
- package/dist/core/events/PassthroughEvents.mjs.map +1 -0
- package/dist/core/events/schema.d.ts +1103 -0
- package/dist/core/events/schema.d.ts.map +1 -0
- package/dist/core/events/schema.js +337 -0
- package/dist/core/events/schema.js.map +1 -0
- package/dist/core/events/schema.mjs +283 -0
- package/dist/core/events/schema.mjs.map +1 -0
- package/dist/core/index.d.ts +66 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +234 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/index.mjs +166 -0
- package/dist/core/index.mjs.map +1 -0
- package/dist/core/llm/ILLM.d.ts +15 -0
- package/dist/core/llm/ILLM.d.ts.map +1 -0
- package/dist/core/llm/ILLM.js +19 -0
- package/dist/core/llm/ILLM.js.map +1 -0
- package/dist/core/llm/ILLM.mjs +1 -0
- package/dist/core/llm/ILLM.mjs.map +1 -0
- package/dist/core/llm/LLMTypes.d.ts +369 -0
- package/dist/core/llm/LLMTypes.d.ts.map +1 -0
- package/dist/core/llm/LLMTypes.js +170 -0
- package/dist/core/llm/LLMTypes.js.map +1 -0
- package/dist/core/llm/LLMTypes.mjs +133 -0
- package/dist/core/llm/LLMTypes.mjs.map +1 -0
- package/dist/core/llm/VercelAILLM.d.ts +229 -0
- package/dist/core/llm/VercelAILLM.d.ts.map +1 -0
- package/dist/core/llm/VercelAILLM.js +1037 -0
- package/dist/core/llm/VercelAILLM.js.map +1 -0
- package/dist/core/llm/VercelAILLM.mjs +993 -0
- package/dist/core/llm/VercelAILLM.mjs.map +1 -0
- package/dist/core/llm/openaiSchemas.d.ts +190 -0
- package/dist/core/llm/openaiSchemas.d.ts.map +1 -0
- package/dist/core/llm/openaiSchemas.js +144 -0
- package/dist/core/llm/openaiSchemas.js.map +1 -0
- package/dist/core/llm/openaiSchemas.mjs +107 -0
- package/dist/core/llm/openaiSchemas.mjs.map +1 -0
- package/dist/core/llm/responseFormat.d.ts +25 -0
- package/dist/core/llm/responseFormat.d.ts.map +1 -0
- package/dist/core/llm/responseFormat.js +58 -0
- package/dist/core/llm/responseFormat.js.map +1 -0
- package/dist/core/llm/responseFormat.mjs +32 -0
- package/dist/core/llm/responseFormat.mjs.map +1 -0
- package/dist/core/llm/toOpenAIChatMessage.d.ts +11 -0
- package/dist/core/llm/toOpenAIChatMessage.d.ts.map +1 -0
- package/dist/core/llm/toOpenAIChatMessage.js +95 -0
- package/dist/core/llm/toOpenAIChatMessage.js.map +1 -0
- package/dist/core/llm/toOpenAIChatMessage.mjs +70 -0
- package/dist/core/llm/toOpenAIChatMessage.mjs.map +1 -0
- package/dist/core/llm/usage.d.ts +4 -0
- package/dist/core/llm/usage.d.ts.map +1 -0
- package/dist/core/llm/usage.js +52 -0
- package/dist/core/llm/usage.js.map +1 -0
- package/dist/core/llm/usage.mjs +26 -0
- package/dist/core/llm/usage.mjs.map +1 -0
- package/dist/core/mcp/ClientSideTool.d.ts +10 -0
- package/dist/core/mcp/ClientSideTool.d.ts.map +1 -0
- package/dist/core/mcp/ClientSideTool.js +56 -0
- package/dist/core/mcp/ClientSideTool.js.map +1 -0
- package/dist/core/mcp/ClientSideTool.mjs +31 -0
- package/dist/core/mcp/ClientSideTool.mjs.map +1 -0
- package/dist/core/mcp/IMCPServer.d.ts +78 -0
- package/dist/core/mcp/IMCPServer.d.ts.map +1 -0
- package/dist/core/mcp/IMCPServer.js +63 -0
- package/dist/core/mcp/IMCPServer.js.map +1 -0
- package/dist/core/mcp/IMCPServer.mjs +33 -0
- package/dist/core/mcp/IMCPServer.mjs.map +1 -0
- package/dist/core/mcp/LocalToolMCP.d.ts +39 -0
- package/dist/core/mcp/LocalToolMCP.d.ts.map +1 -0
- package/dist/core/mcp/LocalToolMCP.js +121 -0
- package/dist/core/mcp/LocalToolMCP.js.map +1 -0
- package/dist/core/mcp/LocalToolMCP.mjs +98 -0
- package/dist/core/mcp/LocalToolMCP.mjs.map +1 -0
- package/dist/core/mcp/RemoteMCP.d.ts +67 -0
- package/dist/core/mcp/RemoteMCP.d.ts.map +1 -0
- package/dist/core/mcp/RemoteMCP.js +244 -0
- package/dist/core/mcp/RemoteMCP.js.map +1 -0
- package/dist/core/mcp/RemoteMCP.mjs +222 -0
- package/dist/core/mcp/RemoteMCP.mjs.map +1 -0
- package/dist/core/mcp/ToolSelectorPolicy.d.ts +52 -0
- package/dist/core/mcp/ToolSelectorPolicy.d.ts.map +1 -0
- package/dist/core/mcp/ToolSelectorPolicy.js +108 -0
- package/dist/core/mcp/ToolSelectorPolicy.js.map +1 -0
- package/dist/core/mcp/ToolSelectorPolicy.mjs +90 -0
- package/dist/core/mcp/ToolSelectorPolicy.mjs.map +1 -0
- package/dist/core/mcp/ToolSet.d.ts +31 -0
- package/dist/core/mcp/ToolSet.d.ts.map +1 -0
- package/dist/core/mcp/ToolSet.js +119 -0
- package/dist/core/mcp/ToolSet.js.map +1 -0
- package/dist/core/mcp/ToolSet.mjs +98 -0
- package/dist/core/mcp/ToolSet.mjs.map +1 -0
- package/dist/core/mcp/convertMCPServers.d.ts +22 -0
- package/dist/core/mcp/convertMCPServers.d.ts.map +1 -0
- package/dist/core/mcp/convertMCPServers.js +89 -0
- package/dist/core/mcp/convertMCPServers.js.map +1 -0
- package/dist/core/mcp/convertMCPServers.mjs +64 -0
- package/dist/core/mcp/convertMCPServers.mjs.map +1 -0
- package/dist/core/mcp/executeToolCalls.d.ts +31 -0
- package/dist/core/mcp/executeToolCalls.d.ts.map +1 -0
- package/dist/core/mcp/executeToolCalls.js +168 -0
- package/dist/core/mcp/executeToolCalls.js.map +1 -0
- package/dist/core/mcp/executeToolCalls.mjs +149 -0
- package/dist/core/mcp/executeToolCalls.mjs.map +1 -0
- package/dist/core/mcp/pagination.d.ts +11 -0
- package/dist/core/mcp/pagination.d.ts.map +1 -0
- package/dist/core/mcp/pagination.js +49 -0
- package/dist/core/mcp/pagination.js.map +1 -0
- package/dist/core/mcp/pagination.mjs +24 -0
- package/dist/core/mcp/pagination.mjs.map +1 -0
- package/dist/core/mcp/remoteMcpClient.d.ts +36 -0
- package/dist/core/mcp/remoteMcpClient.d.ts.map +1 -0
- package/dist/core/mcp/remoteMcpClient.js +138 -0
- package/dist/core/mcp/remoteMcpClient.js.map +1 -0
- package/dist/core/mcp/remoteMcpClient.mjs +112 -0
- package/dist/core/mcp/remoteMcpClient.mjs.map +1 -0
- package/dist/core/mcp/toolNames.d.ts +6 -0
- package/dist/core/mcp/toolNames.d.ts.map +1 -0
- package/dist/core/mcp/toolNames.js +59 -0
- package/dist/core/mcp/toolNames.js.map +1 -0
- package/dist/core/mcp/toolNames.mjs +33 -0
- package/dist/core/mcp/toolNames.mjs.map +1 -0
- package/dist/core/mcp/toolSelectors.d.ts +28 -0
- package/dist/core/mcp/toolSelectors.d.ts.map +1 -0
- package/dist/core/mcp/toolSelectors.js +113 -0
- package/dist/core/mcp/toolSelectors.js.map +1 -0
- package/dist/core/mcp/toolSelectors.mjs +78 -0
- package/dist/core/mcp/toolSelectors.mjs.map +1 -0
- package/dist/core/runtime/AgentDefinition.d.ts +23 -0
- package/dist/core/runtime/AgentDefinition.d.ts.map +1 -0
- package/dist/core/runtime/AgentDefinition.js +19 -0
- package/dist/core/runtime/AgentDefinition.js.map +1 -0
- package/dist/core/runtime/AgentDefinition.mjs +1 -0
- package/dist/core/runtime/AgentDefinition.mjs.map +1 -0
- package/dist/core/runtime/AgentThread.d.ts +64 -0
- package/dist/core/runtime/AgentThread.d.ts.map +1 -0
- package/dist/core/runtime/AgentThread.js +1067 -0
- package/dist/core/runtime/AgentThread.js.map +1 -0
- package/dist/core/runtime/AgentThread.mjs +1064 -0
- package/dist/core/runtime/AgentThread.mjs.map +1 -0
- package/dist/core/runtime/AgentThread.types.d.ts +123 -0
- package/dist/core/runtime/AgentThread.types.d.ts.map +1 -0
- package/dist/core/runtime/AgentThread.types.js +46 -0
- package/dist/core/runtime/AgentThread.types.js.map +1 -0
- package/dist/core/runtime/AgentThread.types.mjs +20 -0
- package/dist/core/runtime/AgentThread.types.mjs.map +1 -0
- package/dist/core/runtime/AgentThreadOrchestrator.d.ts +35 -0
- package/dist/core/runtime/AgentThreadOrchestrator.d.ts.map +1 -0
- package/dist/core/runtime/AgentThreadOrchestrator.js +433 -0
- package/dist/core/runtime/AgentThreadOrchestrator.js.map +1 -0
- package/dist/core/runtime/AgentThreadOrchestrator.mjs +418 -0
- package/dist/core/runtime/AgentThreadOrchestrator.mjs.map +1 -0
- package/dist/core/runtime/CreateDynamicSubAgentThread.d.ts +11 -0
- package/dist/core/runtime/CreateDynamicSubAgentThread.d.ts.map +1 -0
- package/dist/core/runtime/CreateDynamicSubAgentThread.js +19 -0
- package/dist/core/runtime/CreateDynamicSubAgentThread.js.map +1 -0
- package/dist/core/runtime/CreateDynamicSubAgentThread.mjs +1 -0
- package/dist/core/runtime/CreateDynamicSubAgentThread.mjs.map +1 -0
- package/dist/core/runtime/DeferredTool.d.ts +34 -0
- package/dist/core/runtime/DeferredTool.d.ts.map +1 -0
- package/dist/core/runtime/DeferredTool.js +321 -0
- package/dist/core/runtime/DeferredTool.js.map +1 -0
- package/dist/core/runtime/DeferredTool.mjs +287 -0
- package/dist/core/runtime/DeferredTool.mjs.map +1 -0
- package/dist/core/runtime/OpenToolCallCloser.d.ts +21 -0
- package/dist/core/runtime/OpenToolCallCloser.d.ts.map +1 -0
- package/dist/core/runtime/OpenToolCallCloser.js +92 -0
- package/dist/core/runtime/OpenToolCallCloser.js.map +1 -0
- package/dist/core/runtime/OpenToolCallCloser.mjs +66 -0
- package/dist/core/runtime/OpenToolCallCloser.mjs.map +1 -0
- package/dist/core/runtime/UserInputMessage.d.ts +16 -0
- package/dist/core/runtime/UserInputMessage.d.ts.map +1 -0
- package/dist/core/runtime/UserInputMessage.js +183 -0
- package/dist/core/runtime/UserInputMessage.js.map +1 -0
- package/dist/core/runtime/UserInputMessage.mjs +157 -0
- package/dist/core/runtime/UserInputMessage.mjs.map +1 -0
- package/dist/core/runtime/contextUsage.d.ts +17 -0
- package/dist/core/runtime/contextUsage.d.ts.map +1 -0
- package/dist/core/runtime/contextUsage.js +56 -0
- package/dist/core/runtime/contextUsage.js.map +1 -0
- package/dist/core/runtime/contextUsage.mjs +28 -0
- package/dist/core/runtime/contextUsage.mjs.map +1 -0
- package/dist/core/runtime/contextUtils.d.ts +30 -0
- package/dist/core/runtime/contextUtils.d.ts.map +1 -0
- package/dist/core/runtime/contextUtils.js +198 -0
- package/dist/core/runtime/contextUtils.js.map +1 -0
- package/dist/core/runtime/contextUtils.mjs +148 -0
- package/dist/core/runtime/contextUtils.mjs.map +1 -0
- package/dist/core/runtime/metrics.d.ts +22 -0
- package/dist/core/runtime/metrics.d.ts.map +1 -0
- package/dist/core/runtime/metrics.js +89 -0
- package/dist/core/runtime/metrics.js.map +1 -0
- package/dist/core/runtime/metrics.mjs +61 -0
- package/dist/core/runtime/metrics.mjs.map +1 -0
- package/dist/core/sandbox/Sandbox.d.ts +119 -0
- package/dist/core/sandbox/Sandbox.d.ts.map +1 -0
- package/dist/core/sandbox/Sandbox.js +556 -0
- package/dist/core/sandbox/Sandbox.js.map +1 -0
- package/dist/core/sandbox/Sandbox.mjs +520 -0
- package/dist/core/sandbox/Sandbox.mjs.map +1 -0
- package/dist/core/sandbox/SandboxErrors.d.ts +30 -0
- package/dist/core/sandbox/SandboxErrors.d.ts.map +1 -0
- package/dist/core/sandbox/SandboxErrors.js +102 -0
- package/dist/core/sandbox/SandboxErrors.js.map +1 -0
- package/dist/core/sandbox/SandboxErrors.mjs +71 -0
- package/dist/core/sandbox/SandboxErrors.mjs.map +1 -0
- package/dist/core/sandbox/codeMode/CodeModeDispatcher.d.ts +29 -0
- package/dist/core/sandbox/codeMode/CodeModeDispatcher.d.ts.map +1 -0
- package/dist/core/sandbox/codeMode/CodeModeDispatcher.js +128 -0
- package/dist/core/sandbox/codeMode/CodeModeDispatcher.js.map +1 -0
- package/dist/core/sandbox/codeMode/CodeModeDispatcher.mjs +107 -0
- package/dist/core/sandbox/codeMode/CodeModeDispatcher.mjs.map +1 -0
- package/dist/core/sandbox/codeMode/CodeModeTransport.d.ts +21 -0
- package/dist/core/sandbox/codeMode/CodeModeTransport.d.ts.map +1 -0
- package/dist/core/sandbox/codeMode/CodeModeTransport.js +19 -0
- package/dist/core/sandbox/codeMode/CodeModeTransport.js.map +1 -0
- package/dist/core/sandbox/codeMode/CodeModeTransport.mjs +1 -0
- package/dist/core/sandbox/codeMode/CodeModeTransport.mjs.map +1 -0
- package/dist/core/sandbox/codeMode/nats/CodeModeNatsTransport.d.ts +34 -0
- package/dist/core/sandbox/codeMode/nats/CodeModeNatsTransport.d.ts.map +1 -0
- package/dist/core/sandbox/codeMode/nats/CodeModeNatsTransport.js +187 -0
- package/dist/core/sandbox/codeMode/nats/CodeModeNatsTransport.js.map +1 -0
- package/dist/core/sandbox/codeMode/nats/CodeModeNatsTransport.mjs +162 -0
- package/dist/core/sandbox/codeMode/nats/CodeModeNatsTransport.mjs.map +1 -0
- package/dist/core/sandbox/codeMode/types.d.ts +34 -0
- package/dist/core/sandbox/codeMode/types.d.ts.map +1 -0
- package/dist/core/sandbox/codeMode/types.js +59 -0
- package/dist/core/sandbox/codeMode/types.js.map +1 -0
- package/dist/core/sandbox/codeMode/types.mjs +32 -0
- package/dist/core/sandbox/codeMode/types.mjs.map +1 -0
- package/dist/core/sandbox/constants.d.ts +10 -0
- package/dist/core/sandbox/constants.d.ts.map +1 -0
- package/dist/core/sandbox/constants.js +37 -0
- package/dist/core/sandbox/constants.js.map +1 -0
- package/dist/core/sandbox/constants.mjs +10 -0
- package/dist/core/sandbox/constants.mjs.map +1 -0
- package/dist/core/sandbox/provider/DaytonaProvider.d.ts +81 -0
- package/dist/core/sandbox/provider/DaytonaProvider.d.ts.map +1 -0
- package/dist/core/sandbox/provider/DaytonaProvider.js +353 -0
- package/dist/core/sandbox/provider/DaytonaProvider.js.map +1 -0
- package/dist/core/sandbox/provider/DaytonaProvider.mjs +333 -0
- package/dist/core/sandbox/provider/DaytonaProvider.mjs.map +1 -0
- package/dist/core/sandbox/provider/Provider.d.ts +84 -0
- package/dist/core/sandbox/provider/Provider.d.ts.map +1 -0
- package/dist/core/sandbox/provider/Provider.js +43 -0
- package/dist/core/sandbox/provider/Provider.js.map +1 -0
- package/dist/core/sandbox/provider/Provider.mjs +17 -0
- package/dist/core/sandbox/provider/Provider.mjs.map +1 -0
- package/dist/core/sandbox/provider/TFYSandboxProvider.d.ts +42 -0
- package/dist/core/sandbox/provider/TFYSandboxProvider.d.ts.map +1 -0
- package/dist/core/sandbox/provider/TFYSandboxProvider.js +186 -0
- package/dist/core/sandbox/provider/TFYSandboxProvider.js.map +1 -0
- package/dist/core/sandbox/provider/TFYSandboxProvider.mjs +153 -0
- package/dist/core/sandbox/provider/TFYSandboxProvider.mjs.map +1 -0
- package/dist/core/sandbox/sandboxImage.d.ts +6 -0
- package/dist/core/sandbox/sandboxImage.d.ts.map +1 -0
- package/dist/core/sandbox/sandboxImage.js +31 -0
- package/dist/core/sandbox/sandboxImage.js.map +1 -0
- package/dist/core/sandbox/sandboxImage.mjs +6 -0
- package/dist/core/sandbox/sandboxImage.mjs.map +1 -0
- package/dist/core/sandbox/sandboxScripts.gen.d.ts +5 -0
- package/dist/core/sandbox/sandboxScripts.gen.d.ts.map +1 -0
- package/dist/core/sandbox/sandboxScripts.gen.js +343 -0
- package/dist/core/sandbox/sandboxScripts.gen.js.map +1 -0
- package/dist/core/sandbox/sandboxScripts.gen.mjs +318 -0
- package/dist/core/sandbox/sandboxScripts.gen.mjs.map +1 -0
- package/dist/core/sandbox/skills/ISkillMounter.d.ts +7 -0
- package/dist/core/sandbox/skills/ISkillMounter.d.ts.map +1 -0
- package/dist/core/sandbox/skills/ISkillMounter.js +19 -0
- package/dist/core/sandbox/skills/ISkillMounter.js.map +1 -0
- package/dist/core/sandbox/skills/ISkillMounter.mjs +1 -0
- package/dist/core/sandbox/skills/ISkillMounter.mjs.map +1 -0
- package/dist/core/sandbox/skills/SkillMounter.d.ts +17 -0
- package/dist/core/sandbox/skills/SkillMounter.d.ts.map +1 -0
- package/dist/core/sandbox/skills/SkillMounter.js +73 -0
- package/dist/core/sandbox/skills/SkillMounter.js.map +1 -0
- package/dist/core/sandbox/skills/SkillMounter.mjs +48 -0
- package/dist/core/sandbox/skills/SkillMounter.mjs.map +1 -0
- package/dist/core/sandbox/skills/constants.d.ts +10 -0
- package/dist/core/sandbox/skills/constants.d.ts.map +1 -0
- package/dist/core/sandbox/skills/constants.js +56 -0
- package/dist/core/sandbox/skills/constants.js.map +1 -0
- package/dist/core/sandbox/skills/constants.mjs +28 -0
- package/dist/core/sandbox/skills/constants.mjs.map +1 -0
- package/dist/core/sandbox/skills/index.d.ts +5 -0
- package/dist/core/sandbox/skills/index.d.ts.map +1 -0
- package/dist/core/sandbox/skills/index.js +40 -0
- package/dist/core/sandbox/skills/index.js.map +1 -0
- package/dist/core/sandbox/skills/index.mjs +11 -0
- package/dist/core/sandbox/skills/index.mjs.map +1 -0
- package/dist/core/tracing/AgentTracing.d.ts +38 -0
- package/dist/core/tracing/AgentTracing.d.ts.map +1 -0
- package/dist/core/tracing/AgentTracing.js +19 -0
- package/dist/core/tracing/AgentTracing.js.map +1 -0
- package/dist/core/tracing/AgentTracing.mjs +1 -0
- package/dist/core/tracing/AgentTracing.mjs.map +1 -0
- package/dist/core/tracing/NoopAgentTracing.d.ts +3 -0
- package/dist/core/tracing/NoopAgentTracing.d.ts.map +1 -0
- package/dist/core/tracing/NoopAgentTracing.js +79 -0
- package/dist/core/tracing/NoopAgentTracing.js.map +1 -0
- package/dist/core/tracing/NoopAgentTracing.mjs +54 -0
- package/dist/core/tracing/NoopAgentTracing.mjs.map +1 -0
- package/dist/core/util/abort.d.ts +2 -0
- package/dist/core/util/abort.d.ts.map +1 -0
- package/dist/core/util/abort.js +38 -0
- package/dist/core/util/abort.js.map +1 -0
- package/dist/core/util/abort.mjs +13 -0
- package/dist/core/util/abort.mjs.map +1 -0
- package/dist/core/util/errorLogFields.d.ts +11 -0
- package/dist/core/util/errorLogFields.d.ts.map +1 -0
- package/dist/core/util/errorLogFields.js +72 -0
- package/dist/core/util/errorLogFields.js.map +1 -0
- package/dist/core/util/errorLogFields.mjs +46 -0
- package/dist/core/util/errorLogFields.mjs.map +1 -0
- package/dist/core/util/promiseUtils.d.ts +7 -0
- package/dist/core/util/promiseUtils.d.ts.map +1 -0
- package/dist/core/util/promiseUtils.js +82 -0
- package/dist/core/util/promiseUtils.js.map +1 -0
- package/dist/core/util/promiseUtils.mjs +55 -0
- package/dist/core/util/promiseUtils.mjs.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +8 -0
- package/dist/index.mjs.map +1 -0
- package/dist/package.json +3 -0
- package/dist/request-reply/client.d.ts +21 -0
- package/dist/request-reply/client.d.ts.map +1 -0
- package/dist/request-reply/client.js +92 -0
- package/dist/request-reply/client.js.map +1 -0
- package/dist/request-reply/client.mjs +67 -0
- package/dist/request-reply/client.mjs.map +1 -0
- package/dist/request-reply/errors.d.ts +23 -0
- package/dist/request-reply/errors.d.ts.map +1 -0
- package/dist/request-reply/errors.js +66 -0
- package/dist/request-reply/errors.js.map +1 -0
- package/dist/request-reply/errors.mjs +39 -0
- package/dist/request-reply/errors.mjs.map +1 -0
- package/dist/request-reply/executor.d.ts +70 -0
- package/dist/request-reply/executor.d.ts.map +1 -0
- package/dist/request-reply/executor.js +267 -0
- package/dist/request-reply/executor.js.map +1 -0
- package/dist/request-reply/executor.mjs +232 -0
- package/dist/request-reply/executor.mjs.map +1 -0
- package/dist/request-reply/index.d.ts +16 -0
- package/dist/request-reply/index.d.ts.map +1 -0
- package/dist/request-reply/index.js +47 -0
- package/dist/request-reply/index.js.map +1 -0
- package/dist/request-reply/index.mjs +16 -0
- package/dist/request-reply/index.mjs.map +1 -0
- package/dist/request-reply/router.d.ts +20 -0
- package/dist/request-reply/router.d.ts.map +1 -0
- package/dist/request-reply/router.js +55 -0
- package/dist/request-reply/router.js.map +1 -0
- package/dist/request-reply/router.mjs +30 -0
- package/dist/request-reply/router.mjs.map +1 -0
- package/dist/request-reply/types.d.ts +25 -0
- package/dist/request-reply/types.d.ts.map +1 -0
- package/dist/request-reply/types.js +54 -0
- package/dist/request-reply/types.js.map +1 -0
- package/dist/request-reply/types.mjs +28 -0
- package/dist/request-reply/types.mjs.map +1 -0
- package/dist/request-reply/utils.d.ts +9 -0
- package/dist/request-reply/utils.d.ts.map +1 -0
- package/dist/request-reply/utils.js +51 -0
- package/dist/request-reply/utils.js.map +1 -0
- package/dist/request-reply/utils.mjs +22 -0
- package/dist/request-reply/utils.mjs.map +1 -0
- package/package.json +127 -6
- package/README.md +0 -4
- package/index.js +0 -1
|
@@ -0,0 +1,1067 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/core/runtime/AgentThread.ts
|
|
21
|
+
var AgentThread_exports = {};
|
|
22
|
+
__export(AgentThread_exports, {
|
|
23
|
+
AgentThread: () => AgentThread
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(AgentThread_exports);
|
|
26
|
+
var import_DynamicSubAgents = require("../capabilities/builtins/DynamicSubAgents.js");
|
|
27
|
+
var import_errors = require("../errors.js");
|
|
28
|
+
var import_schema = require("../events/schema.js");
|
|
29
|
+
var import_InstructionBuilder = require("../InstructionBuilder.js");
|
|
30
|
+
var import_LLMTypes = require("../llm/LLMTypes.js");
|
|
31
|
+
var import_responseFormat = require("../llm/responseFormat.js");
|
|
32
|
+
var import_toOpenAIChatMessage = require("../llm/toOpenAIChatMessage.js");
|
|
33
|
+
var import_usage = require("../llm/usage.js");
|
|
34
|
+
var import_convertMCPServers = require("../mcp/convertMCPServers.js");
|
|
35
|
+
var import_executeToolCalls = require("../mcp/executeToolCalls.js");
|
|
36
|
+
var import_errorLogFields = require("../util/errorLogFields.js");
|
|
37
|
+
var import_AgentThread = require("./AgentThread.types.js");
|
|
38
|
+
var import_contextUsage = require("./contextUsage.js");
|
|
39
|
+
var import_contextUtils = require("./contextUtils.js");
|
|
40
|
+
var import_DeferredTool = require("./DeferredTool.js");
|
|
41
|
+
var import_metrics = require("./metrics.js");
|
|
42
|
+
var import_OpenToolCallCloser = require("./OpenToolCallCloser.js");
|
|
43
|
+
var import_UserInputMessage = require("./UserInputMessage.js");
|
|
44
|
+
var DEFAULT_ITERATION_LIMIT = 25;
|
|
45
|
+
var VALID_TRANSITIONS = {
|
|
46
|
+
"llm-call-required": ["tool-response-required", "user-input-required"],
|
|
47
|
+
"tool-response-required": ["llm-call-required"],
|
|
48
|
+
// user-input-required transitions to:
|
|
49
|
+
// tool-response-required — after approval decisions are appended (approval flow)
|
|
50
|
+
// llm-call-required — after client-side tool responses resolve tool calls directly
|
|
51
|
+
"user-input-required": ["tool-response-required", "llm-call-required"]
|
|
52
|
+
};
|
|
53
|
+
function assertValidTransition(from, to) {
|
|
54
|
+
if (!VALID_TRANSITIONS[from].includes(to)) {
|
|
55
|
+
throw new Error(`Invalid state transition: ${from} \u2192 ${to}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function deriveAgentThreadState(context) {
|
|
59
|
+
const openToolCallIds = getOpenToolCallIds(context);
|
|
60
|
+
if (openToolCallIds.size === 0) return "llm-call-required";
|
|
61
|
+
const assistant = lastAssistantInContext(context);
|
|
62
|
+
const decisions = (0, import_contextUtils.scanApprovalDecisions)(context);
|
|
63
|
+
const hasPendingApproval = assistant?.tool_calls?.some(
|
|
64
|
+
(tc) => openToolCallIds.has(tc.id) && tc.tool_info.is_approval_required === true && !decisions.has(tc.id)
|
|
65
|
+
);
|
|
66
|
+
const hasPendingClientSideTool = assistant?.tool_calls?.some(
|
|
67
|
+
(tc) => openToolCallIds.has(tc.id) && tc.tool_info.is_client_side === true
|
|
68
|
+
);
|
|
69
|
+
return hasPendingApproval || hasPendingClientSideTool ? "user-input-required" : "tool-response-required";
|
|
70
|
+
}
|
|
71
|
+
function hasToolCalls(message) {
|
|
72
|
+
return Boolean(message.tool_calls && message.tool_calls.length > 0);
|
|
73
|
+
}
|
|
74
|
+
function getOpenToolCallIds(context) {
|
|
75
|
+
const resolvedToolCallIds = /* @__PURE__ */ new Set();
|
|
76
|
+
const openToolCallIds = /* @__PURE__ */ new Set();
|
|
77
|
+
for (let i = context.length - 1; i >= 0; i--) {
|
|
78
|
+
const msg = context[i];
|
|
79
|
+
if (msg === void 0) continue;
|
|
80
|
+
if (!(0, import_contextUtils.isLLMContextMessage)(msg)) continue;
|
|
81
|
+
if (msg.role === "tool") {
|
|
82
|
+
resolvedToolCallIds.add(msg.tool_call_id);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (msg.role === "assistant") {
|
|
86
|
+
for (const tc of msg.tool_calls ?? []) openToolCallIds.add(tc.id);
|
|
87
|
+
for (const id of resolvedToolCallIds) openToolCallIds.delete(id);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return openToolCallIds;
|
|
92
|
+
}
|
|
93
|
+
function lastAssistantInContext(context) {
|
|
94
|
+
return context.findLast(
|
|
95
|
+
(msg) => (0, import_contextUtils.isLLMContextMessage)(msg) && msg.role === "assistant"
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
function getUnclosableOpenToolCallIds(context, openToolCallIds) {
|
|
99
|
+
const blockingOpenToolCallIds = new Set(openToolCallIds);
|
|
100
|
+
for (const id of (0, import_OpenToolCallCloser.getClosableOpenToolCallIds)(context)) {
|
|
101
|
+
blockingOpenToolCallIds.delete(id);
|
|
102
|
+
}
|
|
103
|
+
return blockingOpenToolCallIds;
|
|
104
|
+
}
|
|
105
|
+
function buildMCPInitializeEvent(initInfo, threadId) {
|
|
106
|
+
return {
|
|
107
|
+
type: import_schema.EventType.MCP_INITIALIZE,
|
|
108
|
+
id: (0, import_schema.newEventId)(),
|
|
109
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
110
|
+
thread_id: threadId,
|
|
111
|
+
mcp_servers: initInfo
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function buildMCPAuthRequiredEvent(authRequirementInfo, threadId) {
|
|
115
|
+
return {
|
|
116
|
+
type: import_AgentThread.InternalEventType.MCP_AUTH_REQUIRED,
|
|
117
|
+
id: (0, import_schema.newEventId)(),
|
|
118
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
119
|
+
mcp_servers: authRequirementInfo.flatMap((or) => or.servers.map((s) => ({ ...s, thread_ids: [threadId] }))),
|
|
120
|
+
// threadId identifies which thread triggered auth for each server so AgentThreadOrchestrator can merge
|
|
121
|
+
// auth events across parallel sub-agents by server.
|
|
122
|
+
thread_id: null
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function buildSandboxCreatedEvent(info) {
|
|
126
|
+
return {
|
|
127
|
+
type: import_schema.EventType.SANDBOX_CREATED,
|
|
128
|
+
id: (0, import_schema.newEventId)(),
|
|
129
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
130
|
+
...info,
|
|
131
|
+
thread_id: null
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function buildModelMessageEvent({
|
|
135
|
+
assistantMessage,
|
|
136
|
+
threadId,
|
|
137
|
+
finishReason,
|
|
138
|
+
usage,
|
|
139
|
+
id
|
|
140
|
+
}) {
|
|
141
|
+
const { role, tool_calls, thinking_blocks, source, ...rest } = assistantMessage;
|
|
142
|
+
void role;
|
|
143
|
+
void thinking_blocks;
|
|
144
|
+
void source;
|
|
145
|
+
const event = {
|
|
146
|
+
...rest,
|
|
147
|
+
tool_calls: tool_calls?.map(import_contextUtils.toEnrichedToolCall),
|
|
148
|
+
type: import_schema.EventType.MODEL_MESSAGE,
|
|
149
|
+
id,
|
|
150
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
151
|
+
thread_id: threadId,
|
|
152
|
+
finish_reason: finishReason,
|
|
153
|
+
...usage && { usage }
|
|
154
|
+
};
|
|
155
|
+
return event;
|
|
156
|
+
}
|
|
157
|
+
function validateUserMessage(message, blockingOpenToolCallIds, index) {
|
|
158
|
+
if ((0, import_UserInputMessage.isEmptyMessageContent)(message.content)) {
|
|
159
|
+
throw new import_errors.InvalidAgentSendInputError(`messages[${String(index)}] user message has empty content`);
|
|
160
|
+
}
|
|
161
|
+
if (blockingOpenToolCallIds.size > 0) {
|
|
162
|
+
throw new import_errors.InvalidAgentSendInputError("user message cannot be sent while approvals or questions are pending");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function validateToolMessage(message, openToolCallIds, index) {
|
|
166
|
+
if (!openToolCallIds.has(message.tool_call_id)) {
|
|
167
|
+
throw new import_errors.InvalidAgentSendInputError(
|
|
168
|
+
`messages[${String(index)}] no open tool call for tool_call_id '${message.tool_call_id}'`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function validateApprovalMessage(message, pendingApprovalIds, index) {
|
|
173
|
+
if (!pendingApprovalIds.has(message.tool_call_id)) {
|
|
174
|
+
throw new import_errors.InvalidAgentSendInputError(
|
|
175
|
+
`messages[${String(index)}] no pending approval for tool_call_id '${message.tool_call_id}'`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function getPendingApprovalToolCalls(context) {
|
|
180
|
+
const openToolCallIds = getOpenToolCallIds(context);
|
|
181
|
+
const decisions = (0, import_contextUtils.scanApprovalDecisions)(context);
|
|
182
|
+
const assistant = lastAssistantInContext(context);
|
|
183
|
+
return (assistant?.tool_calls ?? []).filter(
|
|
184
|
+
(tc) => openToolCallIds.has(tc.id) && tc.tool_info.is_approval_required === true && !decisions.has(tc.id)
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
function getPendingClientSideToolCalls(context) {
|
|
188
|
+
const openToolCallIds = getOpenToolCallIds(context);
|
|
189
|
+
const assistant = lastAssistantInContext(context);
|
|
190
|
+
return (assistant?.tool_calls ?? []).filter((tc) => openToolCallIds.has(tc.id) && tc.tool_info.is_client_side === true);
|
|
191
|
+
}
|
|
192
|
+
function validateInputMessageTypesGivenContext(context, messages) {
|
|
193
|
+
const openToolCallIds = getOpenToolCallIds(context);
|
|
194
|
+
const blockingOpenToolCallIds = getUnclosableOpenToolCallIds(context, openToolCallIds);
|
|
195
|
+
const pendingApprovalIds = new Set(getPendingApprovalToolCalls(context).map((tc) => tc.id));
|
|
196
|
+
const pendingClientSideIds = new Set(getPendingClientSideToolCalls(context).map((tc) => tc.id));
|
|
197
|
+
for (let i = 0; i < messages.length; i++) {
|
|
198
|
+
const m = messages[i];
|
|
199
|
+
if (m === void 0) continue;
|
|
200
|
+
if ((0, import_contextUtils.isApprovalDecisionMessage)(m)) {
|
|
201
|
+
validateApprovalMessage(m, pendingApprovalIds, i);
|
|
202
|
+
pendingApprovalIds.delete(m.tool_call_id);
|
|
203
|
+
} else if ((0, import_contextUtils.isInputUserMessage)(m)) {
|
|
204
|
+
validateUserMessage(m, blockingOpenToolCallIds, i);
|
|
205
|
+
} else if ((0, import_contextUtils.isClientSideToolResponseMessage)(m) || (0, import_contextUtils.isLLMToolMessage)(m)) {
|
|
206
|
+
validateToolMessage(m, openToolCallIds, i);
|
|
207
|
+
openToolCallIds.delete(m.tool_call_id);
|
|
208
|
+
blockingOpenToolCallIds.delete(m.tool_call_id);
|
|
209
|
+
pendingClientSideIds.delete(m.tool_call_id);
|
|
210
|
+
} else {
|
|
211
|
+
const _exhaustive = m;
|
|
212
|
+
throw new import_errors.InvalidAgentSendInputError(
|
|
213
|
+
`messages[${String(i)}] has unsupported type: ${JSON.stringify(_exhaustive)}`
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (pendingApprovalIds.size > 0 || pendingClientSideIds.size > 0) {
|
|
218
|
+
const missing = [...pendingApprovalIds, ...pendingClientSideIds];
|
|
219
|
+
throw new import_errors.InvalidAgentSendInputError(
|
|
220
|
+
`Send batch must resolve all pending tool calls awaiting user input. Missing: ${missing.join(", ")}`
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async function buildModelMessageDeltaEvent({
|
|
225
|
+
chunk,
|
|
226
|
+
threadId,
|
|
227
|
+
toolMapping,
|
|
228
|
+
usage,
|
|
229
|
+
id,
|
|
230
|
+
logger
|
|
231
|
+
}) {
|
|
232
|
+
const choice = chunk.choices[0];
|
|
233
|
+
const delta = choice?.delta;
|
|
234
|
+
const finishReason = choice?.finish_reason;
|
|
235
|
+
if (!delta || Object.keys(delta).length === 0 && !finishReason && !usage) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (!delta.tool_calls?.length && !delta.content && !delta.reasoning_content && !finishReason && !usage) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const toolCalls = delta.tool_calls ?? [];
|
|
242
|
+
for (const toolCall of toolCalls) {
|
|
243
|
+
if (toolCall.function?.name) {
|
|
244
|
+
const toolInfo = toolMapping.get(toolCall.function.name);
|
|
245
|
+
if (!toolInfo) {
|
|
246
|
+
logger.warn(`LLM called unknown tool: Tool ${toolCall.function.name} not found in tool mapping.`);
|
|
247
|
+
toolCall.tool_info = (0, import_contextUtils.toToolCallInfo)((0, import_contextUtils.makeUnknownToolInfo)(toolCall.function.name));
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
toolCall.tool_info = (0, import_contextUtils.toToolCallInfo)(
|
|
251
|
+
await toolInfo.toolSet.toolCallInfo(
|
|
252
|
+
{ name: toolInfo.originalToolName },
|
|
253
|
+
false
|
|
254
|
+
/* resolveUnderlyingTool */
|
|
255
|
+
)
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const { role, function_call, thinking_blocks, ...deltaWithoutRole } = delta;
|
|
260
|
+
void role;
|
|
261
|
+
void function_call;
|
|
262
|
+
void thinking_blocks;
|
|
263
|
+
return {
|
|
264
|
+
...deltaWithoutRole,
|
|
265
|
+
type: import_schema.EventType.MODEL_MESSAGE_DELTA,
|
|
266
|
+
id,
|
|
267
|
+
thread_id: threadId,
|
|
268
|
+
...finishReason && { finish_reason: finishReason },
|
|
269
|
+
...usage && { usage }
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
async function buildContextAssistantMessage(assistantMessage, toolMapping) {
|
|
273
|
+
if (!assistantMessage.tool_calls?.length) {
|
|
274
|
+
const { tool_calls, ...rest } = assistantMessage;
|
|
275
|
+
void tool_calls;
|
|
276
|
+
return rest;
|
|
277
|
+
}
|
|
278
|
+
const enrichedToolCalls = [];
|
|
279
|
+
for (const toolCall of assistantMessage.tool_calls) {
|
|
280
|
+
const toolInfo = toolMapping.get(toolCall.function.name);
|
|
281
|
+
if (!toolInfo) {
|
|
282
|
+
enrichedToolCalls.push({ ...toolCall, tool_info: (0, import_contextUtils.makeUnknownToolInfo)(toolCall.function.name) });
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const tool_info = await toolInfo.toolSet.toolCallInfo(
|
|
286
|
+
{
|
|
287
|
+
name: toolInfo.originalToolName,
|
|
288
|
+
arguments: tryParseToolArgs(toolCall.function.arguments)
|
|
289
|
+
},
|
|
290
|
+
// During context persistence, we have the complete message and hence the tool arguments are available.
|
|
291
|
+
// Hence, we resolve the underlying tool to get the tool information.
|
|
292
|
+
true
|
|
293
|
+
);
|
|
294
|
+
enrichedToolCalls.push({ ...toolCall, tool_info });
|
|
295
|
+
}
|
|
296
|
+
return { ...assistantMessage, tool_calls: enrichedToolCalls };
|
|
297
|
+
}
|
|
298
|
+
function isJsonObject(value) {
|
|
299
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
300
|
+
}
|
|
301
|
+
function tryParseToolArgs(args) {
|
|
302
|
+
if (!args) return {};
|
|
303
|
+
try {
|
|
304
|
+
const parsed = JSON.parse(args);
|
|
305
|
+
return isJsonObject(parsed) ? parsed : {};
|
|
306
|
+
} catch {
|
|
307
|
+
return {};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
var AgentThread = class {
|
|
311
|
+
threadId;
|
|
312
|
+
parent;
|
|
313
|
+
definition;
|
|
314
|
+
title;
|
|
315
|
+
agentInfo;
|
|
316
|
+
context;
|
|
317
|
+
currentContextUsage;
|
|
318
|
+
preSendContextProcessors;
|
|
319
|
+
preLLMContextProcessors;
|
|
320
|
+
preEphemeralLLMContextProcessors;
|
|
321
|
+
postToolCallContextProcessor;
|
|
322
|
+
instruction;
|
|
323
|
+
toolResponseProcessors;
|
|
324
|
+
instructionBuilders;
|
|
325
|
+
systemToolSets;
|
|
326
|
+
deferredTool;
|
|
327
|
+
convertedTools;
|
|
328
|
+
pendingSandboxCreatedEvents = [];
|
|
329
|
+
sandbox;
|
|
330
|
+
tracing;
|
|
331
|
+
logger;
|
|
332
|
+
metrics = (0, import_metrics.createEmptyAgentThreadMetrics)();
|
|
333
|
+
tfyManagedServerNames = /* @__PURE__ */ new Set();
|
|
334
|
+
contextBusy = false;
|
|
335
|
+
preSendRanThisTurn = false;
|
|
336
|
+
currentState = null;
|
|
337
|
+
preComputedCompletion;
|
|
338
|
+
/** Mirrored capability KV — source for toSnapshot().capability_state. */
|
|
339
|
+
capabilityState = {};
|
|
340
|
+
capabilityStateKeys;
|
|
341
|
+
constructor(input) {
|
|
342
|
+
this.tracing = input.tracing;
|
|
343
|
+
this.logger = input.logger.child({ module: "AgentThread" });
|
|
344
|
+
this.threadId = input.threadId;
|
|
345
|
+
this.definition = input.definition;
|
|
346
|
+
this.context = input.context ? [...input.context] : [];
|
|
347
|
+
this.title = input.title;
|
|
348
|
+
this.parent = input.parent;
|
|
349
|
+
this.currentContextUsage = input.currentContextUsage ?? (0, import_contextUsage.getEmptyCurrentContextUsage)();
|
|
350
|
+
this.agentInfo = input.agentInfo;
|
|
351
|
+
this.preComputedCompletion = input.preComputedCompletion;
|
|
352
|
+
this.sandbox = input.sandbox;
|
|
353
|
+
const capabilities = input.capabilities ?? [];
|
|
354
|
+
this.systemToolSets = capabilities.flatMap((c) => c.systemToolSets ?? []);
|
|
355
|
+
this.preLLMContextProcessors = capabilities.flatMap((c) => c.preLLMProcessors ?? []);
|
|
356
|
+
this.preEphemeralLLMContextProcessors = capabilities.flatMap((c) => c.preLLMEphemeralProcessors ?? []);
|
|
357
|
+
this.postToolCallContextProcessor = capabilities.flatMap((c) => c.postToolCallProcessors ?? []);
|
|
358
|
+
this.toolResponseProcessors = capabilities.flatMap((c) => c.toolResponseProcessors ?? []);
|
|
359
|
+
this.instructionBuilders = capabilities.flatMap((c) => c.instructionBuilders ?? []);
|
|
360
|
+
const capabilityPreSend = capabilities.flatMap((c) => c.preSendProcessors ?? []);
|
|
361
|
+
this.preSendContextProcessors = [new import_OpenToolCallCloser.OpenToolCallCloser(), ...capabilityPreSend];
|
|
362
|
+
assertUniqueStateKeys(capabilities);
|
|
363
|
+
const claimed = claimCapabilityState(capabilities, input.capabilityState, this.logger);
|
|
364
|
+
this.capabilityState = claimed;
|
|
365
|
+
this.capabilityStateKeys = new Set(
|
|
366
|
+
capabilities.map((c) => c.state?.key).filter((k) => typeof k === "string")
|
|
367
|
+
);
|
|
368
|
+
for (const capability of capabilities) {
|
|
369
|
+
if (!capability.state) continue;
|
|
370
|
+
const value = claimed[capability.state.key];
|
|
371
|
+
if (value !== void 0) {
|
|
372
|
+
capability.state.load(value);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (this.definition.toolSets?.length) {
|
|
376
|
+
this.deferredTool = new import_DeferredTool.DeferredTool(this.definition.toolSets, {
|
|
377
|
+
tracing: this.tracing,
|
|
378
|
+
logger: input.logger
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
if (this.sandbox) {
|
|
382
|
+
this.sandbox.configureCodeMode(this.definition.toolSets ?? []);
|
|
383
|
+
}
|
|
384
|
+
this.instruction = this.buildInstruction(this.definition.instruction);
|
|
385
|
+
}
|
|
386
|
+
buildInstruction(userInstruction) {
|
|
387
|
+
const identity = this.parent ? import_DynamicSubAgents.SUB_AGENT_IDENTITY : import_InstructionBuilder.ROOT_AGENT_IDENTITY;
|
|
388
|
+
const builder = import_InstructionBuilder.InstructionBuilder.createSystemPrompt(identity);
|
|
389
|
+
const capabilities = builder.beginSection("agent-capabilities");
|
|
390
|
+
capabilities.addSection("internal-messages", import_contextUtils.INTERNAL_SYSTEM_PROMPT);
|
|
391
|
+
if (this.sandbox) {
|
|
392
|
+
this.sandbox.buildInstruction(capabilities);
|
|
393
|
+
}
|
|
394
|
+
if (this.deferredTool) {
|
|
395
|
+
this.deferredTool.buildInstruction(capabilities);
|
|
396
|
+
}
|
|
397
|
+
for (const build of this.instructionBuilders) {
|
|
398
|
+
build(capabilities);
|
|
399
|
+
}
|
|
400
|
+
if (!this.parent && userInstruction?.trim()) {
|
|
401
|
+
builder.addSection("user-instructions", userInstruction.trim(), true);
|
|
402
|
+
}
|
|
403
|
+
return builder.build();
|
|
404
|
+
}
|
|
405
|
+
async *send(messages) {
|
|
406
|
+
if (messages.length === 0 && !this.isAwaitingUserInput()) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
this.throwIfContextBusy();
|
|
410
|
+
this.contextBusy = true;
|
|
411
|
+
try {
|
|
412
|
+
for await (const event of this.executeContextProcessors("preSend")) {
|
|
413
|
+
yield event;
|
|
414
|
+
}
|
|
415
|
+
this.preSendRanThisTurn = true;
|
|
416
|
+
validateInputMessageTypesGivenContext(this.context, messages);
|
|
417
|
+
const approvals = [];
|
|
418
|
+
const clientSideToolResponses = [];
|
|
419
|
+
const contextMessages = [];
|
|
420
|
+
for (const m of messages) {
|
|
421
|
+
if ((0, import_contextUtils.isApprovalDecisionMessage)(m)) {
|
|
422
|
+
approvals.push(m);
|
|
423
|
+
} else if ((0, import_contextUtils.isClientSideToolResponseMessage)(m)) {
|
|
424
|
+
clientSideToolResponses.push(m);
|
|
425
|
+
} else if ((0, import_contextUtils.isInputUserMessage)(m)) {
|
|
426
|
+
const result = await (0, import_UserInputMessage.processAgentUserInput)(m, this.sandbox);
|
|
427
|
+
contextMessages.push(result.message);
|
|
428
|
+
if (result.sandboxCreated) {
|
|
429
|
+
this.pendingSandboxCreatedEvents.push(buildSandboxCreatedEvent(result.sandboxCreated));
|
|
430
|
+
}
|
|
431
|
+
} else if ((0, import_contextUtils.isLLMToolMessage)(m)) {
|
|
432
|
+
contextMessages.push(m);
|
|
433
|
+
} else {
|
|
434
|
+
const _exhaustive = m;
|
|
435
|
+
throw new import_errors.InvalidAgentSendInputError(`Unsupported send input: ${JSON.stringify(_exhaustive)}`);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (approvals.length > 0 || clientSideToolResponses.length > 0) {
|
|
439
|
+
const approvalContext = approvals.map((a) => ({
|
|
440
|
+
type: import_schema.EventType.USER_TOOL_APPROVAL,
|
|
441
|
+
tool_call_id: a.tool_call_id,
|
|
442
|
+
approval: a.approval
|
|
443
|
+
}));
|
|
444
|
+
const toolResponseContext = clientSideToolResponses.map((m) => ({
|
|
445
|
+
role: "tool",
|
|
446
|
+
tool_call_id: m.tool_call_id,
|
|
447
|
+
content: m.content
|
|
448
|
+
}));
|
|
449
|
+
yield* this.appendToContext({
|
|
450
|
+
context: [...approvalContext, ...toolResponseContext],
|
|
451
|
+
output: [],
|
|
452
|
+
currentContextUsage: void 0,
|
|
453
|
+
usage: void 0
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
if (contextMessages.length > 0) {
|
|
457
|
+
yield* this.appendToContext({
|
|
458
|
+
context: contextMessages,
|
|
459
|
+
output: [],
|
|
460
|
+
currentContextUsage: void 0,
|
|
461
|
+
usage: void 0
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
} finally {
|
|
465
|
+
this.contextBusy = false;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
throwIfContextBusy() {
|
|
469
|
+
if (this.contextBusy) {
|
|
470
|
+
throw new Error(`context is busy for thread ${this.threadId}`);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
deriveState() {
|
|
474
|
+
const next = deriveAgentThreadState(this.context);
|
|
475
|
+
if (this.currentState) {
|
|
476
|
+
assertValidTransition(this.currentState, next);
|
|
477
|
+
}
|
|
478
|
+
this.currentState = next;
|
|
479
|
+
return next;
|
|
480
|
+
}
|
|
481
|
+
*appendToContext(opts) {
|
|
482
|
+
const { context, output, currentContextUsage, usage, completion } = opts;
|
|
483
|
+
const newCurrentContextUsage = currentContextUsage ?? (0, import_contextUsage.mergeCurrentContextUsage)(this.currentContextUsage, (0, import_contextUtils.estimateTokensForContextMessages)(context));
|
|
484
|
+
const event = {
|
|
485
|
+
type: import_AgentThread.InternalEventType.AGENT_CONTEXT_APPEND,
|
|
486
|
+
thread_id: this.threadId,
|
|
487
|
+
context,
|
|
488
|
+
output,
|
|
489
|
+
current_context_usage: newCurrentContextUsage,
|
|
490
|
+
...completion && { completion }
|
|
491
|
+
};
|
|
492
|
+
if (usage) {
|
|
493
|
+
(0, import_metrics.updateMetricsFromUsage)(this.metrics, usage);
|
|
494
|
+
}
|
|
495
|
+
yield event;
|
|
496
|
+
this.context = this.context.concat(context);
|
|
497
|
+
this.currentContextUsage = newCurrentContextUsage;
|
|
498
|
+
}
|
|
499
|
+
*overwriteContext(payload) {
|
|
500
|
+
const event = {
|
|
501
|
+
thread_id: this.threadId,
|
|
502
|
+
...payload
|
|
503
|
+
};
|
|
504
|
+
(0, import_metrics.updateMetricsFromUsage)(this.metrics, payload.usage);
|
|
505
|
+
yield event;
|
|
506
|
+
this.context = payload.context;
|
|
507
|
+
this.currentContextUsage = payload.current_context_usage;
|
|
508
|
+
this.metrics.total_summarizations++;
|
|
509
|
+
}
|
|
510
|
+
generateErrorEvent(message, output) {
|
|
511
|
+
return {
|
|
512
|
+
type: import_AgentThread.InternalEventType.AGENT_DONE,
|
|
513
|
+
status: "error",
|
|
514
|
+
thread_id: this.threadId,
|
|
515
|
+
title: this.title,
|
|
516
|
+
error: message,
|
|
517
|
+
parent: this.parent,
|
|
518
|
+
send_to_parent: this.parent ? {
|
|
519
|
+
role: "tool",
|
|
520
|
+
content: message,
|
|
521
|
+
tool_call_id: this.parent.tool_call_id
|
|
522
|
+
} : void 0,
|
|
523
|
+
...output && { output }
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
hasOpenToolCallId(toolCallId) {
|
|
527
|
+
return getOpenToolCallIds(this.context).has(toolCallId);
|
|
528
|
+
}
|
|
529
|
+
// True when this thread is paused waiting on the user to resolve a pending tool
|
|
530
|
+
// approval or a client-side tool response.
|
|
531
|
+
isAwaitingUserInput() {
|
|
532
|
+
return getPendingApprovalToolCalls(this.context).length > 0 || getPendingClientSideToolCalls(this.context).length > 0;
|
|
533
|
+
}
|
|
534
|
+
// Pure validation of an input batch against this thread's current context; throws
|
|
535
|
+
// on an invalid/incomplete batch without mutating the context.
|
|
536
|
+
validateSendInput(messages) {
|
|
537
|
+
validateInputMessageTypesGivenContext(this.context, messages);
|
|
538
|
+
}
|
|
539
|
+
toSnapshot() {
|
|
540
|
+
const capability_state = Object.keys(this.capabilityState).length > 0 ? { ...this.capabilityState } : null;
|
|
541
|
+
return {
|
|
542
|
+
thread_id: this.threadId,
|
|
543
|
+
context: this.context,
|
|
544
|
+
current_context_usage: this.currentContextUsage,
|
|
545
|
+
parent: this.parent ?? null,
|
|
546
|
+
agent_info: this.agentInfo ?? null,
|
|
547
|
+
completion: this.preComputedCompletion ?? null,
|
|
548
|
+
capability_state
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
getAgentThreadMetrics() {
|
|
552
|
+
return { ...this.metrics };
|
|
553
|
+
}
|
|
554
|
+
transformToLLMRequest(tools) {
|
|
555
|
+
let messages = [];
|
|
556
|
+
if (this.instruction) {
|
|
557
|
+
messages.push({
|
|
558
|
+
role: "system",
|
|
559
|
+
content: this.instruction
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
if (this.definition.messages) {
|
|
563
|
+
for (const msg of this.definition.messages) {
|
|
564
|
+
if ((0, import_contextUtils.isLLMContextMessage)(msg)) {
|
|
565
|
+
messages.push((0, import_toOpenAIChatMessage.toOpenAIChatMessage)(msg));
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const contextMessages = this.context.filter(import_contextUtils.isLLMContextMessage).map((msg) => (0, import_toOpenAIChatMessage.toOpenAIChatMessage)(msg));
|
|
570
|
+
messages.push(...contextMessages);
|
|
571
|
+
for (const processor of this.preEphemeralLLMContextProcessors) {
|
|
572
|
+
messages = processor.processPreLLMEphemeral(messages) ?? messages;
|
|
573
|
+
}
|
|
574
|
+
const requestBody = {
|
|
575
|
+
messages,
|
|
576
|
+
stream: true,
|
|
577
|
+
...this.definition.responseFormat !== void 0 ? { response_format: (0, import_responseFormat.toOpenAIResponseFormat)(this.definition.responseFormat) } : {}
|
|
578
|
+
};
|
|
579
|
+
if (this.definition.modelParams) {
|
|
580
|
+
Object.assign(requestBody, this.definition.modelParams);
|
|
581
|
+
}
|
|
582
|
+
if (tools.length > 0) {
|
|
583
|
+
requestBody.tools = tools;
|
|
584
|
+
}
|
|
585
|
+
return requestBody;
|
|
586
|
+
}
|
|
587
|
+
computeAssistantMessageUsage(params) {
|
|
588
|
+
const { requestBody, usage, toolMapping } = params;
|
|
589
|
+
const trimmedInstruction = this.definition.instruction?.trim() ?? "";
|
|
590
|
+
const instructionsEstimate = this.parent ? 0 : Math.round((0, import_usage.estimateTokensForString)(trimmedInstruction));
|
|
591
|
+
const skillsEstimate = this.sandbox?.estimateSkillsTokens() ?? 0;
|
|
592
|
+
let tfyManagedToolTokens = 0;
|
|
593
|
+
let userToolTokens = 0;
|
|
594
|
+
if (requestBody.tools) {
|
|
595
|
+
for (const tool of requestBody.tools) {
|
|
596
|
+
const toolTokens = Math.round((0, import_usage.estimateTokensForString)(JSON.stringify(tool)));
|
|
597
|
+
const info = "function" in tool ? toolMapping.get(tool.function.name) : void 0;
|
|
598
|
+
if (info && this.tfyManagedServerNames.has(info.toolSet.name)) {
|
|
599
|
+
tfyManagedToolTokens += toolTokens;
|
|
600
|
+
} else {
|
|
601
|
+
userToolTokens += toolTokens;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
const totalInstructionTokens = Math.round((0, import_usage.estimateTokensForString)(this.instruction ?? ""));
|
|
606
|
+
const harnessInstructionTokens = Math.max(0, totalInstructionTokens - instructionsEstimate - skillsEstimate);
|
|
607
|
+
const harnessEstimate = harnessInstructionTokens + tfyManagedToolTokens;
|
|
608
|
+
const estimatedComponentsTotal = harnessEstimate + skillsEstimate + instructionsEstimate + userToolTokens;
|
|
609
|
+
const messagesEstimate = Math.max(0, usage.input_tokens - estimatedComponentsTotal);
|
|
610
|
+
return {
|
|
611
|
+
input_tokens: usage.input_tokens,
|
|
612
|
+
output_tokens: usage.output_tokens,
|
|
613
|
+
cache_read_tokens: usage.cache_read_tokens,
|
|
614
|
+
cache_write_tokens: usage.cache_write_tokens,
|
|
615
|
+
input_tokens_breakdown: {
|
|
616
|
+
harness: harnessEstimate,
|
|
617
|
+
skills: skillsEstimate,
|
|
618
|
+
instructions: instructionsEstimate,
|
|
619
|
+
tool_definitions: userToolTokens,
|
|
620
|
+
messages: messagesEstimate
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
async *executeContextProcessors(hook) {
|
|
625
|
+
let processors;
|
|
626
|
+
switch (hook) {
|
|
627
|
+
case "preSend":
|
|
628
|
+
processors = this.preSendContextProcessors.map((p) => p.processPreSend.bind(p));
|
|
629
|
+
break;
|
|
630
|
+
case "preLLM":
|
|
631
|
+
processors = this.preLLMContextProcessors.map((p) => p.processPreLLM.bind(p));
|
|
632
|
+
break;
|
|
633
|
+
case "postToolCall":
|
|
634
|
+
processors = this.postToolCallContextProcessor.map((p) => p.processPostToolCall.bind(p));
|
|
635
|
+
break;
|
|
636
|
+
default:
|
|
637
|
+
throw new Error(`Unknown hook: ${String(hook)}`);
|
|
638
|
+
}
|
|
639
|
+
const execution = {
|
|
640
|
+
threadId: this.threadId,
|
|
641
|
+
sandbox: this.sandbox,
|
|
642
|
+
currentContextUsage: this.currentContextUsage,
|
|
643
|
+
context: this.context
|
|
644
|
+
};
|
|
645
|
+
for (const processor of processors) {
|
|
646
|
+
for await (const response of processor(execution)) {
|
|
647
|
+
switch (response.type) {
|
|
648
|
+
case import_AgentThread.InternalEventType.AGENT_CONTEXT_APPEND:
|
|
649
|
+
yield* this.appendToContext({
|
|
650
|
+
context: response.context,
|
|
651
|
+
output: response.output,
|
|
652
|
+
currentContextUsage: response.current_context_usage ?? this.currentContextUsage,
|
|
653
|
+
usage: void 0
|
|
654
|
+
});
|
|
655
|
+
break;
|
|
656
|
+
case import_schema.EventType.AGENT_CONTEXT_OVERWRITE:
|
|
657
|
+
yield* this.overwriteContext(response);
|
|
658
|
+
break;
|
|
659
|
+
case import_AgentThread.InternalEventType.PASSTHROUGH:
|
|
660
|
+
yield response;
|
|
661
|
+
break;
|
|
662
|
+
case import_AgentThread.InternalEventType.CAPABILITY_STATE:
|
|
663
|
+
if (!this.capabilityStateKeys.has(response.key)) {
|
|
664
|
+
throw new import_errors.AgentHarnessError(
|
|
665
|
+
"capability_state_error",
|
|
666
|
+
`CAPABILITY_STATE key '${response.key}' is not declared by any capability on thread '${this.threadId}'`
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
yield {
|
|
670
|
+
type: import_AgentThread.InternalEventType.CAPABILITY_STATE,
|
|
671
|
+
thread_id: this.threadId,
|
|
672
|
+
key: response.key,
|
|
673
|
+
state: response.state
|
|
674
|
+
};
|
|
675
|
+
this.capabilityState[response.key] = response.state;
|
|
676
|
+
break;
|
|
677
|
+
default: {
|
|
678
|
+
const _exhaustive = response;
|
|
679
|
+
return _exhaustive;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
async init() {
|
|
686
|
+
if (this.convertedTools) {
|
|
687
|
+
return { initializationInfo: [], authRequirementInfo: [] };
|
|
688
|
+
}
|
|
689
|
+
return this.initTools();
|
|
690
|
+
}
|
|
691
|
+
async initTools() {
|
|
692
|
+
const tfyManagedServers = [...this.systemToolSets];
|
|
693
|
+
if (this.sandbox) {
|
|
694
|
+
tfyManagedServers.push(this.sandbox);
|
|
695
|
+
}
|
|
696
|
+
if (this.deferredTool) {
|
|
697
|
+
tfyManagedServers.push(this.deferredTool);
|
|
698
|
+
}
|
|
699
|
+
const userServers = [...this.definition.toolSets ?? []];
|
|
700
|
+
const { convertedTools, initializationInfo, authRequirementInfo } = await (0, import_convertMCPServers.convertMCPServersToTools)({
|
|
701
|
+
tfyManagedServers,
|
|
702
|
+
userServers
|
|
703
|
+
});
|
|
704
|
+
this.convertedTools = convertedTools;
|
|
705
|
+
this.tfyManagedServerNames = new Set(tfyManagedServers.map((s) => s.name));
|
|
706
|
+
return { initializationInfo, authRequirementInfo };
|
|
707
|
+
}
|
|
708
|
+
getConvertedTools() {
|
|
709
|
+
if (!this.convertedTools) {
|
|
710
|
+
throw new Error("unreachable");
|
|
711
|
+
}
|
|
712
|
+
return this.convertedTools;
|
|
713
|
+
}
|
|
714
|
+
async *stepLLMCall(tools, toolMapping, signal) {
|
|
715
|
+
const modelMessageEventId = (0, import_schema.newEventId)();
|
|
716
|
+
if (signal?.aborted) {
|
|
717
|
+
return { outcome: "exit", modelMessageEventId };
|
|
718
|
+
}
|
|
719
|
+
for await (const event of this.executeContextProcessors("preLLM")) {
|
|
720
|
+
yield event;
|
|
721
|
+
}
|
|
722
|
+
const requestBody = this.transformToLLMRequest(tools);
|
|
723
|
+
const llmStream = this.definition.modelClient.create(requestBody);
|
|
724
|
+
yield {
|
|
725
|
+
type: import_schema.EventType.MODEL_MESSAGE,
|
|
726
|
+
thread_id: this.threadId,
|
|
727
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
728
|
+
id: modelMessageEventId
|
|
729
|
+
};
|
|
730
|
+
let firstDeltaTimestamped = false;
|
|
731
|
+
let result = await llmStream.next();
|
|
732
|
+
let modelMessageUsage = void 0;
|
|
733
|
+
while (!result.done) {
|
|
734
|
+
const chunk = result.value;
|
|
735
|
+
if (chunk.usage) {
|
|
736
|
+
modelMessageUsage = this.computeAssistantMessageUsage({
|
|
737
|
+
requestBody,
|
|
738
|
+
usage: chunk.usage,
|
|
739
|
+
toolMapping
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
const event = await buildModelMessageDeltaEvent({
|
|
743
|
+
chunk,
|
|
744
|
+
threadId: this.threadId,
|
|
745
|
+
toolMapping,
|
|
746
|
+
usage: modelMessageUsage,
|
|
747
|
+
id: modelMessageEventId,
|
|
748
|
+
logger: this.logger
|
|
749
|
+
});
|
|
750
|
+
if (event) {
|
|
751
|
+
if (!firstDeltaTimestamped) {
|
|
752
|
+
firstDeltaTimestamped = true;
|
|
753
|
+
yield { ...event, created_at: (/* @__PURE__ */ new Date()).toISOString() };
|
|
754
|
+
} else {
|
|
755
|
+
yield event;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
result = await llmStream.next();
|
|
759
|
+
}
|
|
760
|
+
if (signal?.aborted) {
|
|
761
|
+
return { outcome: "exit", modelMessageEventId };
|
|
762
|
+
}
|
|
763
|
+
modelMessageUsage ??= this.computeAssistantMessageUsage({
|
|
764
|
+
requestBody,
|
|
765
|
+
usage: result.value.usage,
|
|
766
|
+
toolMapping
|
|
767
|
+
});
|
|
768
|
+
const assistantMessage = await buildContextAssistantMessage(
|
|
769
|
+
result.value.output,
|
|
770
|
+
toolMapping
|
|
771
|
+
);
|
|
772
|
+
const finishReason = result.value.finish_reason;
|
|
773
|
+
const agentAssistantMessage = buildModelMessageEvent({
|
|
774
|
+
assistantMessage,
|
|
775
|
+
threadId: this.threadId,
|
|
776
|
+
finishReason,
|
|
777
|
+
usage: modelMessageUsage,
|
|
778
|
+
id: modelMessageEventId
|
|
779
|
+
});
|
|
780
|
+
let completion;
|
|
781
|
+
if (this.parent) {
|
|
782
|
+
if (finishReason === "length") {
|
|
783
|
+
const errorMessage = (0, import_contextUtils.assistantMessageContentToStringForSubAgent)(
|
|
784
|
+
assistantMessage.content,
|
|
785
|
+
"max_tokens breached"
|
|
786
|
+
);
|
|
787
|
+
completion = {
|
|
788
|
+
type: "error",
|
|
789
|
+
output: agentAssistantMessage,
|
|
790
|
+
error_message: errorMessage,
|
|
791
|
+
send_to_parent: { role: "tool", tool_call_id: this.parent.tool_call_id, content: errorMessage }
|
|
792
|
+
};
|
|
793
|
+
} else if (!hasToolCalls(assistantMessage)) {
|
|
794
|
+
const content = (0, import_contextUtils.assistantMessageContentToStringForSubAgent)(assistantMessage.content);
|
|
795
|
+
completion = {
|
|
796
|
+
type: "done",
|
|
797
|
+
output: agentAssistantMessage,
|
|
798
|
+
send_to_parent: { role: "tool", tool_call_id: this.parent.tool_call_id, content }
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
yield* this.appendToContext({
|
|
803
|
+
context: [assistantMessage],
|
|
804
|
+
output: [agentAssistantMessage],
|
|
805
|
+
currentContextUsage: (0, import_contextUsage.currentContextUsageFromCompletion)(result.value.usage),
|
|
806
|
+
usage: result.value.usage,
|
|
807
|
+
completion
|
|
808
|
+
});
|
|
809
|
+
if (finishReason === "length") {
|
|
810
|
+
const errorContent = completion?.error_message ?? "max_tokens breached";
|
|
811
|
+
yield this.generateErrorEvent(errorContent, agentAssistantMessage);
|
|
812
|
+
return { outcome: "exit", modelMessageEventId };
|
|
813
|
+
}
|
|
814
|
+
if (!hasToolCalls(assistantMessage)) {
|
|
815
|
+
yield {
|
|
816
|
+
type: import_AgentThread.InternalEventType.AGENT_DONE,
|
|
817
|
+
status: "done",
|
|
818
|
+
thread_id: this.threadId,
|
|
819
|
+
title: this.title,
|
|
820
|
+
output: agentAssistantMessage,
|
|
821
|
+
parent: this.parent,
|
|
822
|
+
send_to_parent: completion?.send_to_parent
|
|
823
|
+
};
|
|
824
|
+
return { outcome: "exit", modelMessageEventId };
|
|
825
|
+
}
|
|
826
|
+
return { outcome: "continue", modelMessageEventId };
|
|
827
|
+
}
|
|
828
|
+
async *stepToolResponse(toolMapping) {
|
|
829
|
+
const assistantMessage = lastAssistantInContext(this.context);
|
|
830
|
+
if (!assistantMessage) {
|
|
831
|
+
throw new Error("unreachable");
|
|
832
|
+
}
|
|
833
|
+
const openIds = getOpenToolCallIds(this.context);
|
|
834
|
+
const openCalls = assistantMessage.tool_calls?.filter((tc) => openIds.has(tc.id)) ?? [];
|
|
835
|
+
const decisions = (0, import_contextUtils.scanApprovalDecisions)(this.context);
|
|
836
|
+
const assistantForExecution = {
|
|
837
|
+
...assistantMessage,
|
|
838
|
+
tool_calls: openCalls
|
|
839
|
+
};
|
|
840
|
+
const {
|
|
841
|
+
toolCallResults,
|
|
842
|
+
initializationInfo,
|
|
843
|
+
createThreadEvents,
|
|
844
|
+
sandboxCreated,
|
|
845
|
+
authRequirementInfo,
|
|
846
|
+
approvalRequiredToolCalls,
|
|
847
|
+
clientSideToolCalls,
|
|
848
|
+
events: passthroughEvents
|
|
849
|
+
} = await (0, import_executeToolCalls.executeToolCalls)({
|
|
850
|
+
assistantMessage: assistantForExecution,
|
|
851
|
+
toolMapping,
|
|
852
|
+
threadId: this.threadId,
|
|
853
|
+
approvalDecisions: decisions
|
|
854
|
+
});
|
|
855
|
+
void clientSideToolCalls;
|
|
856
|
+
if (approvalRequiredToolCalls.length > 0) {
|
|
857
|
+
throw new Error("Unreachable");
|
|
858
|
+
}
|
|
859
|
+
this.metrics.total_tool_calls += toolCallResults.length;
|
|
860
|
+
this.metrics.total_sub_agents += createThreadEvents.length;
|
|
861
|
+
if (initializationInfo.length > 0) {
|
|
862
|
+
yield buildMCPInitializeEvent(initializationInfo, this.threadId);
|
|
863
|
+
}
|
|
864
|
+
if (sandboxCreated) {
|
|
865
|
+
yield buildSandboxCreatedEvent(sandboxCreated);
|
|
866
|
+
}
|
|
867
|
+
for (const event of passthroughEvents) {
|
|
868
|
+
yield { type: import_AgentThread.InternalEventType.PASSTHROUGH, event };
|
|
869
|
+
}
|
|
870
|
+
const toolIdsBeforeProcessing = toolCallResults.map((t) => t.message.tool_call_id);
|
|
871
|
+
const toolResponseExecution = {
|
|
872
|
+
threadId: this.threadId,
|
|
873
|
+
sandbox: this.sandbox,
|
|
874
|
+
currentContextUsage: this.currentContextUsage,
|
|
875
|
+
context: this.context
|
|
876
|
+
};
|
|
877
|
+
for (const processor of this.toolResponseProcessors) {
|
|
878
|
+
const processorResult = await processor.process(toolCallResults, toolResponseExecution);
|
|
879
|
+
if (processorResult.sandboxCreated) {
|
|
880
|
+
yield buildSandboxCreatedEvent(processorResult.sandboxCreated);
|
|
881
|
+
}
|
|
882
|
+
for (const event of processorResult.events ?? []) {
|
|
883
|
+
yield { type: import_AgentThread.InternalEventType.PASSTHROUGH, event };
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
if (toolIdsBeforeProcessing.length !== toolCallResults.length) {
|
|
887
|
+
throw Error("Some tool call processor has removed tool call.");
|
|
888
|
+
}
|
|
889
|
+
const toolIdsAfterProcessing = new Set(toolCallResults.map((t) => t.message.tool_call_id));
|
|
890
|
+
if (!toolIdsBeforeProcessing.every((id) => toolIdsAfterProcessing.has(id))) {
|
|
891
|
+
throw Error("Tool call IDs do not match after processing.");
|
|
892
|
+
}
|
|
893
|
+
const agentToolMessages = toolCallResults.map((t) => {
|
|
894
|
+
const { role, ...toolMessage } = t.message;
|
|
895
|
+
void role;
|
|
896
|
+
return {
|
|
897
|
+
...toolMessage,
|
|
898
|
+
type: import_schema.EventType.TOOL_RESPONSE,
|
|
899
|
+
id: (0, import_schema.newEventId)(),
|
|
900
|
+
created_at: t.completedAt,
|
|
901
|
+
thread_id: this.threadId
|
|
902
|
+
};
|
|
903
|
+
});
|
|
904
|
+
for (const msg of agentToolMessages) {
|
|
905
|
+
yield msg;
|
|
906
|
+
}
|
|
907
|
+
yield* this.appendToContext({
|
|
908
|
+
context: toolCallResults.map((t) => t.message),
|
|
909
|
+
output: [],
|
|
910
|
+
currentContextUsage: void 0,
|
|
911
|
+
usage: void 0
|
|
912
|
+
});
|
|
913
|
+
if (authRequirementInfo.length > 0) {
|
|
914
|
+
yield buildMCPAuthRequiredEvent(authRequirementInfo, this.threadId);
|
|
915
|
+
return "exit";
|
|
916
|
+
}
|
|
917
|
+
for await (const event of this.executeContextProcessors("postToolCall")) {
|
|
918
|
+
yield event;
|
|
919
|
+
}
|
|
920
|
+
for (const event of createThreadEvents) {
|
|
921
|
+
yield event;
|
|
922
|
+
}
|
|
923
|
+
if (createThreadEvents.length > 0) {
|
|
924
|
+
return "exit";
|
|
925
|
+
}
|
|
926
|
+
return "continue";
|
|
927
|
+
}
|
|
928
|
+
*stepUserInputRequired(modelMessageEventId) {
|
|
929
|
+
const pendingClientSideToolCalls = getPendingClientSideToolCalls(this.context);
|
|
930
|
+
if (pendingClientSideToolCalls.length > 0) {
|
|
931
|
+
const responseRequiredEvent = {
|
|
932
|
+
type: import_schema.EventType.TOOL_RESPONSE_REQUIRED,
|
|
933
|
+
id: (0, import_schema.newEventId)(),
|
|
934
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
935
|
+
thread_id: this.threadId,
|
|
936
|
+
tool_calls: pendingClientSideToolCalls.map((tc) => ({ id: tc.id, source_event_id: modelMessageEventId }))
|
|
937
|
+
};
|
|
938
|
+
yield responseRequiredEvent;
|
|
939
|
+
}
|
|
940
|
+
const pendingApprovalToolCalls = getPendingApprovalToolCalls(this.context);
|
|
941
|
+
if (pendingApprovalToolCalls.length > 0) {
|
|
942
|
+
const event = {
|
|
943
|
+
type: import_schema.EventType.TOOL_APPROVAL_REQUIRED,
|
|
944
|
+
id: (0, import_schema.newEventId)(),
|
|
945
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
946
|
+
thread_id: this.threadId,
|
|
947
|
+
tool_calls: pendingApprovalToolCalls.map(({ id }) => ({ id, source_event_id: modelMessageEventId }))
|
|
948
|
+
};
|
|
949
|
+
yield event;
|
|
950
|
+
}
|
|
951
|
+
return "exit";
|
|
952
|
+
}
|
|
953
|
+
buildReplayEvent(c) {
|
|
954
|
+
const base = {
|
|
955
|
+
type: import_AgentThread.InternalEventType.AGENT_DONE,
|
|
956
|
+
thread_id: this.threadId,
|
|
957
|
+
title: this.title,
|
|
958
|
+
parent: this.parent,
|
|
959
|
+
send_to_parent: c.send_to_parent
|
|
960
|
+
};
|
|
961
|
+
return c.type === "done" ? { ...base, status: "done", output: c.output } : { ...base, status: "error", error: c.error_message ?? "Sub-agent errored", output: c.output };
|
|
962
|
+
}
|
|
963
|
+
async *execute(options) {
|
|
964
|
+
const signal = options?.signal;
|
|
965
|
+
this.throwIfContextBusy();
|
|
966
|
+
this.contextBusy = true;
|
|
967
|
+
this.currentState = null;
|
|
968
|
+
try {
|
|
969
|
+
if (this.preComputedCompletion) {
|
|
970
|
+
yield this.buildReplayEvent(this.preComputedCompletion);
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
if (!this.preSendRanThisTurn) {
|
|
974
|
+
for await (const event of this.executeContextProcessors("preSend")) {
|
|
975
|
+
yield event;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
this.preSendRanThisTurn = false;
|
|
979
|
+
const { initializationInfo, authRequirementInfo } = await this.tracing.withInitSpan(() => this.init());
|
|
980
|
+
if (initializationInfo.length > 0) {
|
|
981
|
+
yield buildMCPInitializeEvent(initializationInfo, this.threadId);
|
|
982
|
+
}
|
|
983
|
+
if (authRequirementInfo.length > 0) {
|
|
984
|
+
yield buildMCPAuthRequiredEvent(authRequirementInfo, this.threadId);
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
const { tools, toolMapping } = this.getConvertedTools();
|
|
988
|
+
const iterationLimit = this.definition.iterationLimit ?? DEFAULT_ITERATION_LIMIT;
|
|
989
|
+
let currentModelMessageEventId = "";
|
|
990
|
+
for (; ; ) {
|
|
991
|
+
for (; ; ) {
|
|
992
|
+
const sandboxCreatedEvent = this.pendingSandboxCreatedEvents.shift();
|
|
993
|
+
if (sandboxCreatedEvent === void 0) break;
|
|
994
|
+
yield sandboxCreatedEvent;
|
|
995
|
+
}
|
|
996
|
+
const state = this.deriveState();
|
|
997
|
+
let outcome;
|
|
998
|
+
switch (state) {
|
|
999
|
+
case "llm-call-required": {
|
|
1000
|
+
if (signal?.aborted) return;
|
|
1001
|
+
if (this.metrics.iterations >= iterationLimit) {
|
|
1002
|
+
yield this.generateErrorEvent(
|
|
1003
|
+
`You have reached iteration limit of ${String(iterationLimit)}, please request again`
|
|
1004
|
+
);
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
this.metrics.iterations++;
|
|
1008
|
+
const llmResult = yield* this.stepLLMCall(tools, toolMapping, signal);
|
|
1009
|
+
outcome = llmResult.outcome;
|
|
1010
|
+
currentModelMessageEventId = llmResult.modelMessageEventId;
|
|
1011
|
+
break;
|
|
1012
|
+
}
|
|
1013
|
+
case "tool-response-required": {
|
|
1014
|
+
if (signal?.aborted) return;
|
|
1015
|
+
outcome = yield* this.stepToolResponse(toolMapping);
|
|
1016
|
+
break;
|
|
1017
|
+
}
|
|
1018
|
+
case "user-input-required": {
|
|
1019
|
+
outcome = yield* this.stepUserInputRequired(currentModelMessageEventId);
|
|
1020
|
+
break;
|
|
1021
|
+
}
|
|
1022
|
+
default: {
|
|
1023
|
+
throw new Error("unreachable");
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
if (outcome === "exit") return;
|
|
1027
|
+
}
|
|
1028
|
+
} catch (error) {
|
|
1029
|
+
this.logger.error("Agent thread execution failed", (0, import_errorLogFields.extractErrorLogFields)(error));
|
|
1030
|
+
yield this.generateErrorEvent((0, import_errorLogFields.describeUnknownError)(error));
|
|
1031
|
+
} finally {
|
|
1032
|
+
this.contextBusy = false;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
function assertUniqueStateKeys(capabilities) {
|
|
1037
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1038
|
+
for (const capability of capabilities) {
|
|
1039
|
+
const key = capability.state?.key;
|
|
1040
|
+
if (!key) continue;
|
|
1041
|
+
if (seen.has(key)) {
|
|
1042
|
+
throw new import_errors.AgentHarnessError(
|
|
1043
|
+
"capability_state_error",
|
|
1044
|
+
`Duplicate capability state key '${key}' \u2014 each key may be claimed by at most one capability`
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
seen.add(key);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
function claimCapabilityState(capabilities, input, logger) {
|
|
1051
|
+
if (!input) return {};
|
|
1052
|
+
const claimedKeys = new Set(capabilities.map((c) => c.state?.key).filter((k) => typeof k === "string"));
|
|
1053
|
+
const claimed = {};
|
|
1054
|
+
for (const [key, value] of Object.entries(input)) {
|
|
1055
|
+
if (!claimedKeys.has(key)) {
|
|
1056
|
+
logger.warn(`Dropping unclaimed capability_state key '${key}' \u2014 no capability declares it`);
|
|
1057
|
+
continue;
|
|
1058
|
+
}
|
|
1059
|
+
claimed[key] = value;
|
|
1060
|
+
}
|
|
1061
|
+
return claimed;
|
|
1062
|
+
}
|
|
1063
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1064
|
+
0 && (module.exports = {
|
|
1065
|
+
AgentThread
|
|
1066
|
+
});
|
|
1067
|
+
//# sourceMappingURL=AgentThread.js.map
|