@strands-agents/sdk 0.0.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/LICENSE +175 -0
- package/README.md +185 -0
- package/dist/__fixtures__/model-test-helpers.d.ts +56 -0
- package/dist/__fixtures__/model-test-helpers.d.ts.map +1 -0
- package/dist/__fixtures__/model-test-helpers.js +85 -0
- package/dist/__fixtures__/model-test-helpers.js.map +1 -0
- package/dist/__fixtures__/tool-helpers.d.ts +15 -0
- package/dist/__fixtures__/tool-helpers.d.ts.map +1 -0
- package/dist/__fixtures__/tool-helpers.js +22 -0
- package/dist/__fixtures__/tool-helpers.js.map +1 -0
- package/dist/__tests__/errors.test.d.ts +2 -0
- package/dist/__tests__/errors.test.d.ts.map +1 -0
- package/dist/__tests__/errors.test.js +20 -0
- package/dist/__tests__/errors.test.js.map +1 -0
- package/dist/__tests__/index.test.d.ts +2 -0
- package/dist/__tests__/index.test.d.ts.map +1 -0
- package/dist/__tests__/index.test.js +27 -0
- package/dist/__tests__/index.test.js.map +1 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +25 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/models/__tests__/bedrock.test.d.ts +2 -0
- package/dist/models/__tests__/bedrock.test.d.ts.map +1 -0
- package/dist/models/__tests__/bedrock.test.js +1161 -0
- package/dist/models/__tests__/bedrock.test.js.map +1 -0
- package/dist/models/__tests__/model.test.d.ts +2 -0
- package/dist/models/__tests__/model.test.d.ts.map +1 -0
- package/dist/models/__tests__/model.test.js +297 -0
- package/dist/models/__tests__/model.test.js.map +1 -0
- package/dist/models/__tests__/openai.test.d.ts +2 -0
- package/dist/models/__tests__/openai.test.d.ts.map +1 -0
- package/dist/models/__tests__/openai.test.js +1016 -0
- package/dist/models/__tests__/openai.test.js.map +1 -0
- package/dist/models/__tests__/test-utils.d.ts +10 -0
- package/dist/models/__tests__/test-utils.d.ts.map +1 -0
- package/dist/models/__tests__/test-utils.js +17 -0
- package/dist/models/__tests__/test-utils.js.map +1 -0
- package/dist/models/bedrock.d.ts +272 -0
- package/dist/models/bedrock.d.ts.map +1 -0
- package/dist/models/bedrock.js +679 -0
- package/dist/models/bedrock.js.map +1 -0
- package/dist/models/model.d.ts +89 -0
- package/dist/models/model.d.ts.map +1 -0
- package/dist/models/model.js +122 -0
- package/dist/models/model.js.map +1 -0
- package/dist/models/openai.d.ts +262 -0
- package/dist/models/openai.d.ts.map +1 -0
- package/dist/models/openai.js +625 -0
- package/dist/models/openai.js.map +1 -0
- package/dist/models/streaming.d.ts +226 -0
- package/dist/models/streaming.d.ts.map +1 -0
- package/dist/models/streaming.js +2 -0
- package/dist/models/streaming.js.map +1 -0
- package/dist/src/__fixtures__/agent-helpers.d.ts +29 -0
- package/dist/src/__fixtures__/agent-helpers.d.ts.map +1 -0
- package/dist/src/__fixtures__/agent-helpers.js +19 -0
- package/dist/src/__fixtures__/agent-helpers.js.map +1 -0
- package/dist/src/__fixtures__/environment.d.ts +12 -0
- package/dist/src/__fixtures__/environment.d.ts.map +1 -0
- package/dist/src/__fixtures__/environment.js +12 -0
- package/dist/src/__fixtures__/environment.js.map +1 -0
- package/dist/src/__fixtures__/mock-hook-provider.d.ts +14 -0
- package/dist/src/__fixtures__/mock-hook-provider.d.ts.map +1 -0
- package/dist/src/__fixtures__/mock-hook-provider.js +33 -0
- package/dist/src/__fixtures__/mock-hook-provider.js.map +1 -0
- package/dist/src/__fixtures__/mock-message-model.d.ts +93 -0
- package/dist/src/__fixtures__/mock-message-model.d.ts.map +1 -0
- package/dist/src/__fixtures__/mock-message-model.js +226 -0
- package/dist/src/__fixtures__/mock-message-model.js.map +1 -0
- package/dist/src/__fixtures__/model-test-helpers.d.ts +56 -0
- package/dist/src/__fixtures__/model-test-helpers.d.ts.map +1 -0
- package/dist/src/__fixtures__/model-test-helpers.js +85 -0
- package/dist/src/__fixtures__/model-test-helpers.js.map +1 -0
- package/dist/src/__fixtures__/tool-helpers.d.ts +37 -0
- package/dist/src/__fixtures__/tool-helpers.d.ts.map +1 -0
- package/dist/src/__fixtures__/tool-helpers.js +78 -0
- package/dist/src/__fixtures__/tool-helpers.js.map +1 -0
- package/dist/src/__tests__/errors.test.d.ts +2 -0
- package/dist/src/__tests__/errors.test.d.ts.map +1 -0
- package/dist/src/__tests__/errors.test.js +64 -0
- package/dist/src/__tests__/errors.test.js.map +1 -0
- package/dist/src/__tests__/index.test.d.ts +2 -0
- package/dist/src/__tests__/index.test.d.ts.map +1 -0
- package/dist/src/__tests__/index.test.js +27 -0
- package/dist/src/__tests__/index.test.js.map +1 -0
- package/dist/src/__tests__/mcp.test.d.ts +2 -0
- package/dist/src/__tests__/mcp.test.d.ts.map +1 -0
- package/dist/src/__tests__/mcp.test.js +166 -0
- package/dist/src/__tests__/mcp.test.js.map +1 -0
- package/dist/src/agent/__tests__/agent.hook.test.d.ts +2 -0
- package/dist/src/agent/__tests__/agent.hook.test.d.ts.map +1 -0
- package/dist/src/agent/__tests__/agent.hook.test.js +250 -0
- package/dist/src/agent/__tests__/agent.hook.test.js.map +1 -0
- package/dist/src/agent/__tests__/agent.test.d.ts +2 -0
- package/dist/src/agent/__tests__/agent.test.d.ts.map +1 -0
- package/dist/src/agent/__tests__/agent.test.js +414 -0
- package/dist/src/agent/__tests__/agent.test.js.map +1 -0
- package/dist/src/agent/__tests__/printer.test.d.ts +2 -0
- package/dist/src/agent/__tests__/printer.test.d.ts.map +1 -0
- package/dist/src/agent/__tests__/printer.test.js +152 -0
- package/dist/src/agent/__tests__/printer.test.js.map +1 -0
- package/dist/src/agent/__tests__/state.test.d.ts +2 -0
- package/dist/src/agent/__tests__/state.test.d.ts.map +1 -0
- package/dist/src/agent/__tests__/state.test.js +231 -0
- package/dist/src/agent/__tests__/state.test.js.map +1 -0
- package/dist/src/agent/agent.d.ts +207 -0
- package/dist/src/agent/agent.d.ts.map +1 -0
- package/dist/src/agent/agent.js +481 -0
- package/dist/src/agent/agent.js.map +1 -0
- package/dist/src/agent/printer.d.ts +73 -0
- package/dist/src/agent/printer.d.ts.map +1 -0
- package/dist/src/agent/printer.js +145 -0
- package/dist/src/agent/printer.js.map +1 -0
- package/dist/src/agent/state.d.ts +102 -0
- package/dist/src/agent/state.d.ts.map +1 -0
- package/dist/src/agent/state.js +73 -0
- package/dist/src/agent/state.js.map +1 -0
- package/dist/src/agent/streaming.d.ts +91 -0
- package/dist/src/agent/streaming.d.ts.map +1 -0
- package/dist/src/agent/streaming.js +2 -0
- package/dist/src/agent/streaming.js.map +1 -0
- package/dist/src/conversation-manager/__tests__/conversation-manager.test.d.ts +2 -0
- package/dist/src/conversation-manager/__tests__/conversation-manager.test.d.ts.map +1 -0
- package/dist/src/conversation-manager/__tests__/conversation-manager.test.js +10 -0
- package/dist/src/conversation-manager/__tests__/conversation-manager.test.js.map +1 -0
- package/dist/src/conversation-manager/__tests__/null-conversation-manager.test.d.ts +2 -0
- package/dist/src/conversation-manager/__tests__/null-conversation-manager.test.d.ts.map +1 -0
- package/dist/src/conversation-manager/__tests__/null-conversation-manager.test.js +35 -0
- package/dist/src/conversation-manager/__tests__/null-conversation-manager.test.js.map +1 -0
- package/dist/src/conversation-manager/__tests__/sliding-window-conversation-manager.test.d.ts +2 -0
- package/dist/src/conversation-manager/__tests__/sliding-window-conversation-manager.test.d.ts.map +1 -0
- package/dist/src/conversation-manager/__tests__/sliding-window-conversation-manager.test.js +553 -0
- package/dist/src/conversation-manager/__tests__/sliding-window-conversation-manager.test.js.map +1 -0
- package/dist/src/conversation-manager/conversation-manager.d.ts +73 -0
- package/dist/src/conversation-manager/conversation-manager.d.ts.map +1 -0
- package/dist/src/conversation-manager/conversation-manager.js +24 -0
- package/dist/src/conversation-manager/conversation-manager.js.map +1 -0
- package/dist/src/conversation-manager/index.d.ts +8 -0
- package/dist/src/conversation-manager/index.d.ts.map +1 -0
- package/dist/src/conversation-manager/index.js +8 -0
- package/dist/src/conversation-manager/index.js.map +1 -0
- package/dist/src/conversation-manager/null-conversation-manager.d.ts +23 -0
- package/dist/src/conversation-manager/null-conversation-manager.d.ts.map +1 -0
- package/dist/src/conversation-manager/null-conversation-manager.js +23 -0
- package/dist/src/conversation-manager/null-conversation-manager.js.map +1 -0
- package/dist/src/conversation-manager/sliding-window-conversation-manager.d.ts +105 -0
- package/dist/src/conversation-manager/sliding-window-conversation-manager.d.ts.map +1 -0
- package/dist/src/conversation-manager/sliding-window-conversation-manager.js +212 -0
- package/dist/src/conversation-manager/sliding-window-conversation-manager.js.map +1 -0
- package/dist/src/errors.d.ts +83 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +97 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/hooks/__tests__/events.test.d.ts +2 -0
- package/dist/src/hooks/__tests__/events.test.d.ts.map +1 -0
- package/dist/src/hooks/__tests__/events.test.js +347 -0
- package/dist/src/hooks/__tests__/events.test.js.map +1 -0
- package/dist/src/hooks/__tests__/registry.test.d.ts +2 -0
- package/dist/src/hooks/__tests__/registry.test.d.ts.map +1 -0
- package/dist/src/hooks/__tests__/registry.test.js +154 -0
- package/dist/src/hooks/__tests__/registry.test.js.map +1 -0
- package/dist/src/hooks/events.d.ts +199 -0
- package/dist/src/hooks/events.d.ts.map +1 -0
- package/dist/src/hooks/events.js +191 -0
- package/dist/src/hooks/events.js.map +1 -0
- package/dist/src/hooks/index.d.ts +11 -0
- package/dist/src/hooks/index.d.ts.map +1 -0
- package/dist/src/hooks/index.js +11 -0
- package/dist/src/hooks/index.js.map +1 -0
- package/dist/src/hooks/registry.d.ts +65 -0
- package/dist/src/hooks/registry.d.ts.map +1 -0
- package/dist/src/hooks/registry.js +65 -0
- package/dist/src/hooks/registry.js.map +1 -0
- package/dist/src/hooks/types.d.ts +49 -0
- package/dist/src/hooks/types.d.ts.map +1 -0
- package/dist/src/hooks/types.js +2 -0
- package/dist/src/hooks/types.js.map +1 -0
- package/dist/src/index.d.ts +32 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +29 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/mcp.d.ts +51 -0
- package/dist/src/mcp.d.ts.map +1 -0
- package/dist/src/mcp.js +91 -0
- package/dist/src/mcp.js.map +1 -0
- package/dist/src/models/__tests__/bedrock.test.d.ts +2 -0
- package/dist/src/models/__tests__/bedrock.test.d.ts.map +1 -0
- package/dist/src/models/__tests__/bedrock.test.js +1388 -0
- package/dist/src/models/__tests__/bedrock.test.js.map +1 -0
- package/dist/src/models/__tests__/model.test.d.ts +2 -0
- package/dist/src/models/__tests__/model.test.d.ts.map +1 -0
- package/dist/src/models/__tests__/model.test.js +342 -0
- package/dist/src/models/__tests__/model.test.js.map +1 -0
- package/dist/src/models/__tests__/openai.test.d.ts +2 -0
- package/dist/src/models/__tests__/openai.test.d.ts.map +1 -0
- package/dist/src/models/__tests__/openai.test.js +1189 -0
- package/dist/src/models/__tests__/openai.test.js.map +1 -0
- package/dist/src/models/__tests__/test-utils.d.ts +10 -0
- package/dist/src/models/__tests__/test-utils.d.ts.map +1 -0
- package/dist/src/models/__tests__/test-utils.js +17 -0
- package/dist/src/models/__tests__/test-utils.js.map +1 -0
- package/dist/src/models/bedrock.d.ts +289 -0
- package/dist/src/models/bedrock.d.ts.map +1 -0
- package/dist/src/models/bedrock.js +804 -0
- package/dist/src/models/bedrock.js.map +1 -0
- package/dist/src/models/model.d.ts +99 -0
- package/dist/src/models/model.d.ts.map +1 -0
- package/dist/src/models/model.js +169 -0
- package/dist/src/models/model.js.map +1 -0
- package/dist/src/models/openai.d.ts +262 -0
- package/dist/src/models/openai.d.ts.map +1 -0
- package/dist/src/models/openai.js +752 -0
- package/dist/src/models/openai.js.map +1 -0
- package/dist/src/models/streaming.d.ts +318 -0
- package/dist/src/models/streaming.d.ts.map +1 -0
- package/dist/src/models/streaming.js +122 -0
- package/dist/src/models/streaming.js.map +1 -0
- package/dist/src/registry/registry.d.ts +117 -0
- package/dist/src/registry/registry.d.ts.map +1 -0
- package/dist/src/registry/registry.js +298 -0
- package/dist/src/registry/registry.js.map +1 -0
- package/dist/src/registry/tool-registry.d.ts +34 -0
- package/dist/src/registry/tool-registry.d.ts.map +1 -0
- package/dist/src/registry/tool-registry.js +178 -0
- package/dist/src/registry/tool-registry.js.map +1 -0
- package/dist/src/tools/__tests__/tool.test.d.ts +2 -0
- package/dist/src/tools/__tests__/tool.test.d.ts.map +1 -0
- package/dist/src/tools/__tests__/tool.test.js +877 -0
- package/dist/src/tools/__tests__/tool.test.js.map +1 -0
- package/dist/src/tools/__tests__/zod-tool.test-d.d.ts +2 -0
- package/dist/src/tools/__tests__/zod-tool.test-d.d.ts.map +1 -0
- package/dist/src/tools/__tests__/zod-tool.test-d.js +227 -0
- package/dist/src/tools/__tests__/zod-tool.test-d.js.map +1 -0
- package/dist/src/tools/__tests__/zod-tool.test.d.ts +2 -0
- package/dist/src/tools/__tests__/zod-tool.test.d.ts.map +1 -0
- package/dist/src/tools/__tests__/zod-tool.test.js +372 -0
- package/dist/src/tools/__tests__/zod-tool.test.js.map +1 -0
- package/dist/src/tools/function-tool.d.ts +146 -0
- package/dist/src/tools/function-tool.d.ts.map +1 -0
- package/dist/src/tools/function-tool.js +188 -0
- package/dist/src/tools/function-tool.js.map +1 -0
- package/dist/src/tools/mcp-tool.d.ts +36 -0
- package/dist/src/tools/mcp-tool.d.ts.map +1 -0
- package/dist/src/tools/mcp-tool.js +78 -0
- package/dist/src/tools/mcp-tool.js.map +1 -0
- package/dist/src/tools/tool.d.ts +167 -0
- package/dist/src/tools/tool.d.ts.map +1 -0
- package/dist/src/tools/tool.js +68 -0
- package/dist/src/tools/tool.js.map +1 -0
- package/dist/src/tools/types.d.ts +62 -0
- package/dist/src/tools/types.d.ts.map +1 -0
- package/dist/src/tools/types.js +2 -0
- package/dist/src/tools/types.js.map +1 -0
- package/dist/src/tools/zod-tool.d.ts +70 -0
- package/dist/src/tools/zod-tool.d.ts.map +1 -0
- package/dist/src/tools/zod-tool.js +149 -0
- package/dist/src/tools/zod-tool.js.map +1 -0
- package/dist/src/types/__tests__/agent.test.d.ts +2 -0
- package/dist/src/types/__tests__/agent.test.d.ts.map +1 -0
- package/dist/src/types/__tests__/agent.test.js +155 -0
- package/dist/src/types/__tests__/agent.test.js.map +1 -0
- package/dist/src/types/__tests__/json.test.d.ts +2 -0
- package/dist/src/types/__tests__/json.test.d.ts.map +1 -0
- package/dist/src/types/__tests__/json.test.js +298 -0
- package/dist/src/types/__tests__/json.test.js.map +1 -0
- package/dist/src/types/__tests__/media.test.d.ts +2 -0
- package/dist/src/types/__tests__/media.test.d.ts.map +1 -0
- package/dist/src/types/__tests__/media.test.js +257 -0
- package/dist/src/types/__tests__/media.test.js.map +1 -0
- package/dist/src/types/__tests__/messages.test.d.ts +2 -0
- package/dist/src/types/__tests__/messages.test.d.ts.map +1 -0
- package/dist/src/types/__tests__/messages.test.js +364 -0
- package/dist/src/types/__tests__/messages.test.js.map +1 -0
- package/dist/src/types/__tests__/validation.test.d.ts +2 -0
- package/dist/src/types/__tests__/validation.test.d.ts.map +1 -0
- package/dist/src/types/__tests__/validation.test.js +30 -0
- package/dist/src/types/__tests__/validation.test.js.map +1 -0
- package/dist/src/types/agent.d.ts +57 -0
- package/dist/src/types/agent.d.ts.map +1 -0
- package/dist/src/types/agent.js +47 -0
- package/dist/src/types/agent.js.map +1 -0
- package/dist/src/types/json.d.ts +55 -0
- package/dist/src/types/json.d.ts.map +1 -0
- package/dist/src/types/json.js +72 -0
- package/dist/src/types/json.js.map +1 -0
- package/dist/src/types/media.d.ts +249 -0
- package/dist/src/types/media.d.ts.map +1 -0
- package/dist/src/types/media.js +173 -0
- package/dist/src/types/media.js.map +1 -0
- package/dist/src/types/messages.d.ts +438 -0
- package/dist/src/types/messages.d.ts.map +1 -0
- package/dist/src/types/messages.js +286 -0
- package/dist/src/types/messages.js.map +1 -0
- package/dist/src/types/validation.d.ts +10 -0
- package/dist/src/types/validation.d.ts.map +1 -0
- package/dist/src/types/validation.js +15 -0
- package/dist/src/types/validation.js.map +1 -0
- package/dist/tools/__tests__/registry.test.d.ts +2 -0
- package/dist/tools/__tests__/registry.test.d.ts.map +1 -0
- package/dist/tools/__tests__/registry.test.js +253 -0
- package/dist/tools/__tests__/registry.test.js.map +1 -0
- package/dist/tools/__tests__/tool.test.d.ts +2 -0
- package/dist/tools/__tests__/tool.test.d.ts.map +1 -0
- package/dist/tools/__tests__/tool.test.js +761 -0
- package/dist/tools/__tests__/tool.test.js.map +1 -0
- package/dist/tools/__tests__/zod-tool.test-d.d.ts +2 -0
- package/dist/tools/__tests__/zod-tool.test-d.d.ts.map +1 -0
- package/dist/tools/__tests__/zod-tool.test-d.js +227 -0
- package/dist/tools/__tests__/zod-tool.test-d.js.map +1 -0
- package/dist/tools/__tests__/zod-tool.test.d.ts +2 -0
- package/dist/tools/__tests__/zod-tool.test.d.ts.map +1 -0
- package/dist/tools/__tests__/zod-tool.test.js +342 -0
- package/dist/tools/__tests__/zod-tool.test.js.map +1 -0
- package/dist/tools/function-tool.d.ts +156 -0
- package/dist/tools/function-tool.d.ts.map +1 -0
- package/dist/tools/function-tool.js +237 -0
- package/dist/tools/function-tool.js.map +1 -0
- package/dist/tools/registry.d.ts +43 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +82 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/tool.d.ts +157 -0
- package/dist/tools/tool.d.ts.map +1 -0
- package/dist/tools/tool.js +2 -0
- package/dist/tools/tool.js.map +1 -0
- package/dist/tools/types.d.ts +119 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/tools/zod-tool.d.ts +70 -0
- package/dist/tools/zod-tool.d.ts.map +1 -0
- package/dist/tools/zod-tool.js +96 -0
- package/dist/tools/zod-tool.js.map +1 -0
- package/dist/types/__tests__/json.test.d.ts +2 -0
- package/dist/types/__tests__/json.test.d.ts.map +1 -0
- package/dist/types/__tests__/json.test.js +129 -0
- package/dist/types/__tests__/json.test.js.map +1 -0
- package/dist/types/__tests__/validation.test.d.ts +2 -0
- package/dist/types/__tests__/validation.test.d.ts.map +1 -0
- package/dist/types/__tests__/validation.test.js +30 -0
- package/dist/types/__tests__/validation.test.js.map +1 -0
- package/dist/types/json.d.ts +45 -0
- package/dist/types/json.d.ts.map +1 -0
- package/dist/types/json.js +17 -0
- package/dist/types/json.js.map +1 -0
- package/dist/types/messages.d.ts +160 -0
- package/dist/types/messages.d.ts.map +1 -0
- package/dist/types/messages.js +2 -0
- package/dist/types/messages.js.map +1 -0
- package/dist/types/validation.d.ts +10 -0
- package/dist/types/validation.d.ts.map +1 -0
- package/dist/types/validation.js +15 -0
- package/dist/types/validation.js.map +1 -0
- package/dist/vended_tools/bash/__tests__/bash.test.d.ts +2 -0
- package/dist/vended_tools/bash/__tests__/bash.test.d.ts.map +1 -0
- package/dist/vended_tools/bash/__tests__/bash.test.js +333 -0
- package/dist/vended_tools/bash/__tests__/bash.test.js.map +1 -0
- package/dist/vended_tools/bash/bash.d.ts +33 -0
- package/dist/vended_tools/bash/bash.d.ts.map +1 -0
- package/dist/vended_tools/bash/bash.js +264 -0
- package/dist/vended_tools/bash/bash.js.map +1 -0
- package/dist/vended_tools/bash/index.d.ts +7 -0
- package/dist/vended_tools/bash/index.d.ts.map +1 -0
- package/dist/vended_tools/bash/index.js +6 -0
- package/dist/vended_tools/bash/index.js.map +1 -0
- package/dist/vended_tools/bash/types.d.ts +65 -0
- package/dist/vended_tools/bash/types.d.ts.map +1 -0
- package/dist/vended_tools/bash/types.js +22 -0
- package/dist/vended_tools/bash/types.js.map +1 -0
- package/dist/vended_tools/file_editor/__tests__/file-editor.test.d.ts +2 -0
- package/dist/vended_tools/file_editor/__tests__/file-editor.test.d.ts.map +1 -0
- package/dist/vended_tools/file_editor/__tests__/file-editor.test.js +359 -0
- package/dist/vended_tools/file_editor/__tests__/file-editor.test.js.map +1 -0
- package/dist/vended_tools/file_editor/file-editor.d.ts +31 -0
- package/dist/vended_tools/file_editor/file-editor.d.ts.map +1 -0
- package/dist/vended_tools/file_editor/file-editor.js +353 -0
- package/dist/vended_tools/file_editor/file-editor.js.map +1 -0
- package/dist/vended_tools/file_editor/index.d.ts +6 -0
- package/dist/vended_tools/file_editor/index.d.ts.map +1 -0
- package/dist/vended_tools/file_editor/index.js +5 -0
- package/dist/vended_tools/file_editor/index.js.map +1 -0
- package/dist/vended_tools/file_editor/types.d.ts +61 -0
- package/dist/vended_tools/file_editor/types.d.ts.map +1 -0
- package/dist/vended_tools/file_editor/types.js +2 -0
- package/dist/vended_tools/file_editor/types.js.map +1 -0
- package/dist/vended_tools/http_request/__tests__/http-request.test.d.ts +2 -0
- package/dist/vended_tools/http_request/__tests__/http-request.test.d.ts.map +1 -0
- package/dist/vended_tools/http_request/__tests__/http-request.test.js +189 -0
- package/dist/vended_tools/http_request/__tests__/http-request.test.js.map +1 -0
- package/dist/vended_tools/http_request/http-request.d.ts +35 -0
- package/dist/vended_tools/http_request/http-request.d.ts.map +1 -0
- package/dist/vended_tools/http_request/http-request.js +95 -0
- package/dist/vended_tools/http_request/http-request.js.map +1 -0
- package/dist/vended_tools/http_request/index.d.ts +6 -0
- package/dist/vended_tools/http_request/index.d.ts.map +1 -0
- package/dist/vended_tools/http_request/index.js +5 -0
- package/dist/vended_tools/http_request/index.js.map +1 -0
- package/dist/vended_tools/http_request/types.d.ts +47 -0
- package/dist/vended_tools/http_request/types.d.ts.map +1 -0
- package/dist/vended_tools/http_request/types.js +2 -0
- package/dist/vended_tools/http_request/types.js.map +1 -0
- package/dist/vended_tools/notebook/__tests__/notebook.test.d.ts +2 -0
- package/dist/vended_tools/notebook/__tests__/notebook.test.d.ts.map +1 -0
- package/dist/vended_tools/notebook/__tests__/notebook.test.js +371 -0
- package/dist/vended_tools/notebook/__tests__/notebook.test.js.map +1 -0
- package/dist/vended_tools/notebook/index.d.ts +6 -0
- package/dist/vended_tools/notebook/index.d.ts.map +1 -0
- package/dist/vended_tools/notebook/index.js +5 -0
- package/dist/vended_tools/notebook/index.js.map +1 -0
- package/dist/vended_tools/notebook/notebook.d.ts +29 -0
- package/dist/vended_tools/notebook/notebook.d.ts.map +1 -0
- package/dist/vended_tools/notebook/notebook.js +215 -0
- package/dist/vended_tools/notebook/notebook.js.map +1 -0
- package/dist/vended_tools/notebook/types.d.ts +79 -0
- package/dist/vended_tools/notebook/types.d.ts.map +1 -0
- package/dist/vended_tools/notebook/types.js +2 -0
- package/dist/vended_tools/notebook/types.js.map +1 -0
- package/package.json +112 -0
|
@@ -0,0 +1,1016 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
import { OpenAIModel } from '../openai';
|
|
4
|
+
import { ContextWindowOverflowError } from '../../errors';
|
|
5
|
+
import { collectIterator } from '../../__fixtures__/model-test-helpers';
|
|
6
|
+
// Environment detection
|
|
7
|
+
const isNode = typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node;
|
|
8
|
+
/**
|
|
9
|
+
* Helper to create a mock OpenAI client with streaming support
|
|
10
|
+
*/
|
|
11
|
+
function createMockClient(streamGenerator) {
|
|
12
|
+
return {
|
|
13
|
+
chat: {
|
|
14
|
+
completions: {
|
|
15
|
+
create: vi.fn(async () => streamGenerator()),
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// Mock the OpenAI SDK
|
|
21
|
+
vi.mock('openai', () => {
|
|
22
|
+
const mockConstructor = vi.fn().mockImplementation(() => ({}));
|
|
23
|
+
return {
|
|
24
|
+
default: mockConstructor,
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
describe('OpenAIModel', () => {
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
vi.clearAllMocks();
|
|
30
|
+
vi.restoreAllMocks();
|
|
31
|
+
// Set default env var for most tests using Vitest's stubEnv (Node.js only)
|
|
32
|
+
if (isNode) {
|
|
33
|
+
vi.stubEnv('OPENAI_API_KEY', 'sk-test-env');
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
vi.clearAllMocks();
|
|
38
|
+
// Restore all environment variables to their original state (Node.js only)
|
|
39
|
+
if (isNode) {
|
|
40
|
+
vi.unstubAllEnvs();
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
describe('constructor', () => {
|
|
44
|
+
it('creates an instance with required modelId', () => {
|
|
45
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', apiKey: 'sk-test' });
|
|
46
|
+
const config = provider.getConfig();
|
|
47
|
+
expect(config.modelId).toBe('gpt-4o');
|
|
48
|
+
});
|
|
49
|
+
it('uses custom model ID', () => {
|
|
50
|
+
const customModelId = 'gpt-3.5-turbo';
|
|
51
|
+
const provider = new OpenAIModel({ modelId: customModelId, apiKey: 'sk-test' });
|
|
52
|
+
expect(provider.getConfig()).toStrictEqual({
|
|
53
|
+
modelId: customModelId,
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
it('uses API key from constructor parameter', () => {
|
|
57
|
+
const apiKey = 'sk-explicit';
|
|
58
|
+
new OpenAIModel({ modelId: 'gpt-4o', apiKey });
|
|
59
|
+
expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({
|
|
60
|
+
apiKey: apiKey,
|
|
61
|
+
}));
|
|
62
|
+
});
|
|
63
|
+
// Node.js-specific test: environment variable usage
|
|
64
|
+
if (isNode) {
|
|
65
|
+
it('uses API key from environment variable', () => {
|
|
66
|
+
vi.stubEnv('OPENAI_API_KEY', 'sk-from-env');
|
|
67
|
+
new OpenAIModel({ modelId: 'gpt-4o' });
|
|
68
|
+
// OpenAI client should be called without explicit apiKey (uses env var internally)
|
|
69
|
+
expect(OpenAI).toHaveBeenCalled();
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
it('explicit API key takes precedence over environment variable', () => {
|
|
73
|
+
if (isNode) {
|
|
74
|
+
vi.stubEnv('OPENAI_API_KEY', 'sk-from-env');
|
|
75
|
+
}
|
|
76
|
+
const explicitKey = 'sk-explicit';
|
|
77
|
+
new OpenAIModel({ modelId: 'gpt-4o', apiKey: explicitKey });
|
|
78
|
+
expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({
|
|
79
|
+
apiKey: explicitKey,
|
|
80
|
+
}));
|
|
81
|
+
});
|
|
82
|
+
it('throws error when no API key is available', () => {
|
|
83
|
+
if (isNode) {
|
|
84
|
+
vi.stubEnv('OPENAI_API_KEY', '');
|
|
85
|
+
}
|
|
86
|
+
expect(() => new OpenAIModel({ modelId: 'gpt-4o' })).toThrow("OpenAI API key is required. Provide it via the 'apiKey' option or set the OPENAI_API_KEY environment variable.");
|
|
87
|
+
});
|
|
88
|
+
it('uses custom client configuration', () => {
|
|
89
|
+
const timeout = 30000;
|
|
90
|
+
new OpenAIModel({ modelId: 'gpt-4o', apiKey: 'sk-test', clientConfig: { timeout } });
|
|
91
|
+
expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({
|
|
92
|
+
timeout: timeout,
|
|
93
|
+
}));
|
|
94
|
+
});
|
|
95
|
+
it('uses provided client instance', () => {
|
|
96
|
+
vi.clearAllMocks();
|
|
97
|
+
const mockClient = {};
|
|
98
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
99
|
+
// Should not create a new OpenAI client
|
|
100
|
+
expect(OpenAI).not.toHaveBeenCalled();
|
|
101
|
+
expect(provider).toBeDefined();
|
|
102
|
+
});
|
|
103
|
+
it('provided client takes precedence over apiKey and clientConfig', () => {
|
|
104
|
+
vi.clearAllMocks();
|
|
105
|
+
const mockClient = {};
|
|
106
|
+
new OpenAIModel({
|
|
107
|
+
modelId: 'gpt-4o',
|
|
108
|
+
apiKey: 'sk-test',
|
|
109
|
+
client: mockClient,
|
|
110
|
+
clientConfig: { timeout: 30000 },
|
|
111
|
+
});
|
|
112
|
+
// Should not create a new OpenAI client when client is provided
|
|
113
|
+
expect(OpenAI).not.toHaveBeenCalled();
|
|
114
|
+
});
|
|
115
|
+
it('does not require API key when client is provided', () => {
|
|
116
|
+
vi.clearAllMocks();
|
|
117
|
+
if (isNode) {
|
|
118
|
+
vi.stubEnv('OPENAI_API_KEY', '');
|
|
119
|
+
}
|
|
120
|
+
const mockClient = {};
|
|
121
|
+
expect(() => new OpenAIModel({ modelId: 'gpt-4o', client: mockClient })).not.toThrow();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
describe('updateConfig', () => {
|
|
125
|
+
it('merges new config with existing config', () => {
|
|
126
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', apiKey: 'sk-test', temperature: 0.5 });
|
|
127
|
+
provider.updateConfig({ modelId: 'gpt-4o', temperature: 0.8, maxTokens: 2048 });
|
|
128
|
+
expect(provider.getConfig()).toStrictEqual({
|
|
129
|
+
modelId: 'gpt-4o',
|
|
130
|
+
temperature: 0.8,
|
|
131
|
+
maxTokens: 2048,
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
it('preserves fields not included in the update', () => {
|
|
135
|
+
const provider = new OpenAIModel({
|
|
136
|
+
apiKey: 'sk-test',
|
|
137
|
+
modelId: 'gpt-3.5-turbo',
|
|
138
|
+
temperature: 0.5,
|
|
139
|
+
maxTokens: 1024,
|
|
140
|
+
});
|
|
141
|
+
provider.updateConfig({ modelId: 'gpt-3.5-turbo', temperature: 0.8 });
|
|
142
|
+
expect(provider.getConfig()).toStrictEqual({
|
|
143
|
+
modelId: 'gpt-3.5-turbo',
|
|
144
|
+
temperature: 0.8,
|
|
145
|
+
maxTokens: 1024,
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
describe('getConfig', () => {
|
|
150
|
+
it('returns the current configuration', () => {
|
|
151
|
+
const provider = new OpenAIModel({
|
|
152
|
+
modelId: 'gpt-4o',
|
|
153
|
+
apiKey: 'sk-test',
|
|
154
|
+
maxTokens: 1024,
|
|
155
|
+
temperature: 0.7,
|
|
156
|
+
});
|
|
157
|
+
expect(provider.getConfig()).toStrictEqual({
|
|
158
|
+
modelId: 'gpt-4o',
|
|
159
|
+
maxTokens: 1024,
|
|
160
|
+
temperature: 0.7,
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
describe('stream', () => {
|
|
165
|
+
describe('validation', () => {
|
|
166
|
+
it('throws error when messages array is empty', async () => {
|
|
167
|
+
const mockClient = createMockClient(async function* () { });
|
|
168
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
169
|
+
await expect(async () => {
|
|
170
|
+
await collectIterator(provider.stream([]));
|
|
171
|
+
}).rejects.toThrow('At least one message is required');
|
|
172
|
+
});
|
|
173
|
+
it('validates system prompt is not empty', async () => {
|
|
174
|
+
const mockClient = createMockClient(async function* () {
|
|
175
|
+
yield {
|
|
176
|
+
choices: [{ delta: { role: 'assistant', content: 'Hello' }, index: 0 }],
|
|
177
|
+
};
|
|
178
|
+
yield {
|
|
179
|
+
choices: [{ finish_reason: 'stop', delta: {}, index: 0 }],
|
|
180
|
+
};
|
|
181
|
+
});
|
|
182
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
183
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
184
|
+
// System prompt that's only whitespace should not be sent
|
|
185
|
+
const events = await collectIterator(provider.stream(messages, { systemPrompt: ' ' }));
|
|
186
|
+
// Should still get valid events
|
|
187
|
+
expect(events.length).toBeGreaterThan(0);
|
|
188
|
+
expect(events[0]?.type).toBe('modelMessageStartEvent');
|
|
189
|
+
});
|
|
190
|
+
it('throws error for streaming with n > 1', async () => {
|
|
191
|
+
const mockClient = createMockClient(async function* () { });
|
|
192
|
+
const provider = new OpenAIModel({
|
|
193
|
+
modelId: 'gpt-4o',
|
|
194
|
+
client: mockClient,
|
|
195
|
+
params: { n: 2 },
|
|
196
|
+
});
|
|
197
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
198
|
+
await expect(async () => {
|
|
199
|
+
for await (const _ of provider.stream(messages)) {
|
|
200
|
+
// Should not reach here
|
|
201
|
+
}
|
|
202
|
+
}).rejects.toThrow('Streaming with n > 1 is not supported');
|
|
203
|
+
});
|
|
204
|
+
it('throws error for tool spec without name or description', async () => {
|
|
205
|
+
const mockClient = createMockClient(async function* () { });
|
|
206
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
207
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
208
|
+
await expect(async () => {
|
|
209
|
+
for await (const _ of provider.stream(messages, {
|
|
210
|
+
toolSpecs: [{ name: '', description: 'test', inputSchema: {} }],
|
|
211
|
+
})) {
|
|
212
|
+
// Should not reach here
|
|
213
|
+
}
|
|
214
|
+
}).rejects.toThrow('Tool specification must have both name and description');
|
|
215
|
+
});
|
|
216
|
+
it('throws error for empty tool result content', async () => {
|
|
217
|
+
const mockClient = createMockClient(async function* () { });
|
|
218
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
219
|
+
const messages = [
|
|
220
|
+
{
|
|
221
|
+
type: 'message',
|
|
222
|
+
role: 'user',
|
|
223
|
+
content: [{ type: 'toolResultBlock', toolUseId: 'tool-123', status: 'success', content: [] }],
|
|
224
|
+
},
|
|
225
|
+
];
|
|
226
|
+
await expect(async () => {
|
|
227
|
+
for await (const _ of provider.stream(messages)) {
|
|
228
|
+
// Should not reach here
|
|
229
|
+
}
|
|
230
|
+
}).rejects.toThrow('Tool result for toolUseId "tool-123" has empty content');
|
|
231
|
+
});
|
|
232
|
+
it('handles tool result with error status', async () => {
|
|
233
|
+
const mockClient = createMockClient(async function* () {
|
|
234
|
+
yield {
|
|
235
|
+
choices: [{ delta: { role: 'assistant', content: 'Ok' }, index: 0 }],
|
|
236
|
+
};
|
|
237
|
+
yield {
|
|
238
|
+
choices: [{ finish_reason: 'stop', delta: {}, index: 0 }],
|
|
239
|
+
};
|
|
240
|
+
});
|
|
241
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
242
|
+
const messages = [
|
|
243
|
+
{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Run tool' }] },
|
|
244
|
+
{
|
|
245
|
+
type: 'message',
|
|
246
|
+
role: 'assistant',
|
|
247
|
+
content: [
|
|
248
|
+
{
|
|
249
|
+
type: 'toolUseBlock',
|
|
250
|
+
name: 'calculator',
|
|
251
|
+
toolUseId: 'tool-123',
|
|
252
|
+
input: { expr: 'invalid' },
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
type: 'message',
|
|
258
|
+
role: 'user',
|
|
259
|
+
content: [
|
|
260
|
+
{
|
|
261
|
+
type: 'toolResultBlock',
|
|
262
|
+
toolUseId: 'tool-123',
|
|
263
|
+
status: 'error',
|
|
264
|
+
content: [{ type: 'toolResultTextContent', text: 'Division by zero' }],
|
|
265
|
+
},
|
|
266
|
+
],
|
|
267
|
+
},
|
|
268
|
+
];
|
|
269
|
+
// Should not throw - error status is handled by prepending [ERROR]
|
|
270
|
+
const events = await collectIterator(provider.stream(messages));
|
|
271
|
+
// Verify we got a response
|
|
272
|
+
expect(events.length).toBeGreaterThan(0);
|
|
273
|
+
expect(events[0]?.type).toBe('modelMessageStartEvent');
|
|
274
|
+
});
|
|
275
|
+
it('throws error for circular reference in tool input', async () => {
|
|
276
|
+
const mockClient = createMockClient(async function* () { });
|
|
277
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
278
|
+
const circular = { a: 1 };
|
|
279
|
+
circular.self = circular;
|
|
280
|
+
const messages = [
|
|
281
|
+
{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] },
|
|
282
|
+
{
|
|
283
|
+
type: 'message',
|
|
284
|
+
role: 'assistant',
|
|
285
|
+
content: [
|
|
286
|
+
{
|
|
287
|
+
type: 'toolUseBlock',
|
|
288
|
+
name: 'test',
|
|
289
|
+
toolUseId: 'tool-1',
|
|
290
|
+
input: circular,
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
},
|
|
294
|
+
];
|
|
295
|
+
await expect(async () => {
|
|
296
|
+
for await (const _ of provider.stream(messages)) {
|
|
297
|
+
// Should not reach here
|
|
298
|
+
}
|
|
299
|
+
}).rejects.toThrow('Failed to serialize tool input');
|
|
300
|
+
});
|
|
301
|
+
it('throws error for reasoning blocks (OpenAI does not support them)', async () => {
|
|
302
|
+
const mockClient = createMockClient(async function* () { });
|
|
303
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
304
|
+
const messages = [
|
|
305
|
+
{
|
|
306
|
+
type: 'message',
|
|
307
|
+
role: 'user',
|
|
308
|
+
content: [{ type: 'reasoningBlock', reasoning: 'Some reasoning' }],
|
|
309
|
+
},
|
|
310
|
+
];
|
|
311
|
+
await expect(async () => {
|
|
312
|
+
for await (const _ of provider.stream(messages)) {
|
|
313
|
+
// Should not reach here
|
|
314
|
+
}
|
|
315
|
+
}).rejects.toThrow('Reasoning blocks are not supported by OpenAI');
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
describe('basic streaming', () => {
|
|
319
|
+
it('yields correct event sequence for simple text response', async () => {
|
|
320
|
+
const mockClient = createMockClient(async function* () {
|
|
321
|
+
yield {
|
|
322
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
323
|
+
};
|
|
324
|
+
yield {
|
|
325
|
+
choices: [{ delta: { content: 'Hello' }, index: 0 }],
|
|
326
|
+
};
|
|
327
|
+
yield {
|
|
328
|
+
choices: [{ delta: { content: ' world' }, index: 0 }],
|
|
329
|
+
};
|
|
330
|
+
yield {
|
|
331
|
+
choices: [{ finish_reason: 'stop', delta: {}, index: 0 }],
|
|
332
|
+
};
|
|
333
|
+
});
|
|
334
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
335
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
336
|
+
const events = await collectIterator(provider.stream(messages));
|
|
337
|
+
// Now includes complete content block lifecycle: start, deltas, stop
|
|
338
|
+
expect(events).toHaveLength(6);
|
|
339
|
+
expect(events[0]).toEqual({ type: 'modelMessageStartEvent', role: 'assistant' });
|
|
340
|
+
expect(events[1]).toEqual({
|
|
341
|
+
type: 'modelContentBlockStartEvent',
|
|
342
|
+
contentBlockIndex: 0,
|
|
343
|
+
});
|
|
344
|
+
expect(events[2]).toEqual({
|
|
345
|
+
type: 'modelContentBlockDeltaEvent',
|
|
346
|
+
contentBlockIndex: 0,
|
|
347
|
+
delta: { type: 'textDelta', text: 'Hello' },
|
|
348
|
+
});
|
|
349
|
+
expect(events[3]).toEqual({
|
|
350
|
+
type: 'modelContentBlockDeltaEvent',
|
|
351
|
+
contentBlockIndex: 0,
|
|
352
|
+
delta: { type: 'textDelta', text: ' world' },
|
|
353
|
+
});
|
|
354
|
+
expect(events[4]).toEqual({
|
|
355
|
+
type: 'modelContentBlockStopEvent',
|
|
356
|
+
contentBlockIndex: 0,
|
|
357
|
+
});
|
|
358
|
+
expect(events[5]).toEqual({ type: 'modelMessageStopEvent', stopReason: 'endTurn' });
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
it('emits modelMetadataEvent with usage information', async () => {
|
|
362
|
+
const mockClient = createMockClient(async function* () {
|
|
363
|
+
yield {
|
|
364
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
365
|
+
};
|
|
366
|
+
yield {
|
|
367
|
+
choices: [{ finish_reason: 'stop', delta: {}, index: 0 }],
|
|
368
|
+
};
|
|
369
|
+
yield {
|
|
370
|
+
choices: [],
|
|
371
|
+
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
|
|
372
|
+
};
|
|
373
|
+
});
|
|
374
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
375
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
376
|
+
const events = await collectIterator(provider.stream(messages));
|
|
377
|
+
const metadataEvent = events.find((e) => e.type === 'modelMetadataEvent');
|
|
378
|
+
expect(metadataEvent).toBeDefined();
|
|
379
|
+
expect(metadataEvent).toEqual({
|
|
380
|
+
type: 'modelMetadataEvent',
|
|
381
|
+
usage: {
|
|
382
|
+
inputTokens: 10,
|
|
383
|
+
outputTokens: 5,
|
|
384
|
+
totalTokens: 15,
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
it('handles usage with undefined properties', async () => {
|
|
389
|
+
const mockClient = createMockClient(async function* () {
|
|
390
|
+
yield {
|
|
391
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
392
|
+
};
|
|
393
|
+
yield {
|
|
394
|
+
choices: [{ finish_reason: 'stop', delta: {}, index: 0 }],
|
|
395
|
+
};
|
|
396
|
+
yield {
|
|
397
|
+
choices: [],
|
|
398
|
+
usage: {}, // Empty usage object
|
|
399
|
+
};
|
|
400
|
+
});
|
|
401
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
402
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
403
|
+
const events = await collectIterator(provider.stream(messages));
|
|
404
|
+
const metadataEvent = events.find((e) => e.type === 'modelMetadataEvent');
|
|
405
|
+
expect(metadataEvent).toBeDefined();
|
|
406
|
+
expect(metadataEvent).toEqual({
|
|
407
|
+
type: 'modelMetadataEvent',
|
|
408
|
+
usage: {
|
|
409
|
+
inputTokens: 0,
|
|
410
|
+
outputTokens: 0,
|
|
411
|
+
totalTokens: 0,
|
|
412
|
+
},
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
it('filters out empty string content deltas', async () => {
|
|
416
|
+
const mockClient = createMockClient(async function* () {
|
|
417
|
+
yield {
|
|
418
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
419
|
+
};
|
|
420
|
+
yield {
|
|
421
|
+
choices: [{ delta: { content: '' }, index: 0 }], // Empty content
|
|
422
|
+
};
|
|
423
|
+
yield {
|
|
424
|
+
choices: [{ delta: { content: 'Hello' }, index: 0 }],
|
|
425
|
+
};
|
|
426
|
+
yield {
|
|
427
|
+
choices: [{ finish_reason: 'stop', delta: {}, index: 0 }],
|
|
428
|
+
};
|
|
429
|
+
});
|
|
430
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
431
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
432
|
+
const events = await collectIterator(provider.stream(messages));
|
|
433
|
+
// Should not emit event for empty content
|
|
434
|
+
const contentEvents = events.filter((e) => e.type === 'modelContentBlockDeltaEvent');
|
|
435
|
+
expect(contentEvents).toHaveLength(1);
|
|
436
|
+
expect(contentEvents[0].delta.text).toBe('Hello');
|
|
437
|
+
});
|
|
438
|
+
it('prevents duplicate message start events', async () => {
|
|
439
|
+
const mockClient = createMockClient(async function* () {
|
|
440
|
+
yield {
|
|
441
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
442
|
+
};
|
|
443
|
+
yield {
|
|
444
|
+
choices: [{ delta: { role: 'assistant', content: 'Hello' }, index: 0 }], // Duplicate role
|
|
445
|
+
};
|
|
446
|
+
yield {
|
|
447
|
+
choices: [{ finish_reason: 'stop', delta: {}, index: 0 }],
|
|
448
|
+
};
|
|
449
|
+
});
|
|
450
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
451
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
452
|
+
// Suppress console.warn for this test
|
|
453
|
+
vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
454
|
+
const events = await collectIterator(provider.stream(messages));
|
|
455
|
+
// Should only have one message start event
|
|
456
|
+
const startEvents = events.filter((e) => e.type === 'modelMessageStartEvent');
|
|
457
|
+
expect(startEvents).toHaveLength(1);
|
|
458
|
+
});
|
|
459
|
+
});
|
|
460
|
+
describe('tool calling', () => {
|
|
461
|
+
it('handles tool use request with contentBlockStart and contentBlockStop events', async () => {
|
|
462
|
+
const mockClient = createMockClient(async function* () {
|
|
463
|
+
yield {
|
|
464
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
465
|
+
};
|
|
466
|
+
yield {
|
|
467
|
+
choices: [
|
|
468
|
+
{
|
|
469
|
+
delta: {
|
|
470
|
+
tool_calls: [
|
|
471
|
+
{
|
|
472
|
+
index: 0,
|
|
473
|
+
id: 'call_123',
|
|
474
|
+
type: 'function',
|
|
475
|
+
function: { name: 'calculator', arguments: '' },
|
|
476
|
+
},
|
|
477
|
+
],
|
|
478
|
+
},
|
|
479
|
+
index: 0,
|
|
480
|
+
},
|
|
481
|
+
],
|
|
482
|
+
};
|
|
483
|
+
yield {
|
|
484
|
+
choices: [
|
|
485
|
+
{
|
|
486
|
+
delta: {
|
|
487
|
+
tool_calls: [{ index: 0, function: { arguments: '{"expr' } }],
|
|
488
|
+
},
|
|
489
|
+
index: 0,
|
|
490
|
+
},
|
|
491
|
+
],
|
|
492
|
+
};
|
|
493
|
+
yield {
|
|
494
|
+
choices: [
|
|
495
|
+
{
|
|
496
|
+
delta: {
|
|
497
|
+
tool_calls: [{ index: 0, function: { arguments: '":"2+2"}' } }],
|
|
498
|
+
},
|
|
499
|
+
index: 0,
|
|
500
|
+
},
|
|
501
|
+
],
|
|
502
|
+
};
|
|
503
|
+
yield {
|
|
504
|
+
choices: [{ finish_reason: 'tool_calls', delta: {}, index: 0 }],
|
|
505
|
+
};
|
|
506
|
+
});
|
|
507
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
508
|
+
const messages = [
|
|
509
|
+
{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Calculate 2+2' }] },
|
|
510
|
+
];
|
|
511
|
+
const events = await collectIterator(provider.stream(messages));
|
|
512
|
+
// Verify key events in sequence
|
|
513
|
+
expect(events[0]).toEqual({ type: 'modelMessageStartEvent', role: 'assistant' });
|
|
514
|
+
expect(events[1]).toEqual({
|
|
515
|
+
type: 'modelContentBlockStartEvent',
|
|
516
|
+
contentBlockIndex: 0,
|
|
517
|
+
start: {
|
|
518
|
+
type: 'toolUseStart',
|
|
519
|
+
name: 'calculator',
|
|
520
|
+
toolUseId: 'call_123',
|
|
521
|
+
},
|
|
522
|
+
});
|
|
523
|
+
expect(events[2]).toEqual({
|
|
524
|
+
type: 'modelContentBlockDeltaEvent',
|
|
525
|
+
contentBlockIndex: 0,
|
|
526
|
+
delta: {
|
|
527
|
+
type: 'toolUseInputDelta',
|
|
528
|
+
input: '{"expr',
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
expect(events[3]).toEqual({
|
|
532
|
+
type: 'modelContentBlockDeltaEvent',
|
|
533
|
+
contentBlockIndex: 0,
|
|
534
|
+
delta: {
|
|
535
|
+
type: 'toolUseInputDelta',
|
|
536
|
+
input: '":"2+2"}',
|
|
537
|
+
},
|
|
538
|
+
});
|
|
539
|
+
expect(events[4]).toEqual({
|
|
540
|
+
type: 'modelContentBlockStopEvent',
|
|
541
|
+
contentBlockIndex: 0,
|
|
542
|
+
});
|
|
543
|
+
expect(events[5]).toEqual({ type: 'modelMessageStopEvent', stopReason: 'toolUse' });
|
|
544
|
+
});
|
|
545
|
+
it('handles multiple tool calls with correct contentBlockIndex', async () => {
|
|
546
|
+
const mockClient = createMockClient(async function* () {
|
|
547
|
+
yield {
|
|
548
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
549
|
+
};
|
|
550
|
+
yield {
|
|
551
|
+
choices: [
|
|
552
|
+
{
|
|
553
|
+
delta: {
|
|
554
|
+
tool_calls: [
|
|
555
|
+
{
|
|
556
|
+
index: 0,
|
|
557
|
+
id: 'call_1',
|
|
558
|
+
type: 'function',
|
|
559
|
+
function: { name: 'tool1', arguments: '{}' },
|
|
560
|
+
},
|
|
561
|
+
],
|
|
562
|
+
},
|
|
563
|
+
index: 0,
|
|
564
|
+
},
|
|
565
|
+
],
|
|
566
|
+
};
|
|
567
|
+
yield {
|
|
568
|
+
choices: [
|
|
569
|
+
{
|
|
570
|
+
delta: {
|
|
571
|
+
tool_calls: [
|
|
572
|
+
{
|
|
573
|
+
index: 1,
|
|
574
|
+
id: 'call_2',
|
|
575
|
+
type: 'function',
|
|
576
|
+
function: { name: 'tool2', arguments: '{}' },
|
|
577
|
+
},
|
|
578
|
+
],
|
|
579
|
+
},
|
|
580
|
+
index: 0,
|
|
581
|
+
},
|
|
582
|
+
],
|
|
583
|
+
};
|
|
584
|
+
yield {
|
|
585
|
+
choices: [{ finish_reason: 'tool_calls', delta: {}, index: 0 }],
|
|
586
|
+
};
|
|
587
|
+
});
|
|
588
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
589
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
590
|
+
const events = await collectIterator(provider.stream(messages));
|
|
591
|
+
// Should emit stop events for both tool calls
|
|
592
|
+
const stopEvents = events.filter((e) => e.type === 'modelContentBlockStopEvent');
|
|
593
|
+
expect(stopEvents).toHaveLength(2);
|
|
594
|
+
expect(stopEvents[0]).toEqual({ type: 'modelContentBlockStopEvent', contentBlockIndex: 0 });
|
|
595
|
+
expect(stopEvents[1]).toEqual({ type: 'modelContentBlockStopEvent', contentBlockIndex: 1 });
|
|
596
|
+
});
|
|
597
|
+
it('skips tool calls with invalid index', async () => {
|
|
598
|
+
const mockClient = createMockClient(async function* () {
|
|
599
|
+
yield {
|
|
600
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
601
|
+
};
|
|
602
|
+
yield {
|
|
603
|
+
choices: [
|
|
604
|
+
{
|
|
605
|
+
delta: {
|
|
606
|
+
tool_calls: [
|
|
607
|
+
{
|
|
608
|
+
index: undefined, // Invalid index
|
|
609
|
+
id: 'call_123',
|
|
610
|
+
type: 'function',
|
|
611
|
+
function: { name: 'tool', arguments: '{}' },
|
|
612
|
+
},
|
|
613
|
+
],
|
|
614
|
+
},
|
|
615
|
+
index: 0,
|
|
616
|
+
},
|
|
617
|
+
],
|
|
618
|
+
};
|
|
619
|
+
yield {
|
|
620
|
+
choices: [{ finish_reason: 'stop', delta: {}, index: 0 }],
|
|
621
|
+
};
|
|
622
|
+
});
|
|
623
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
624
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
625
|
+
// Suppress console.warn for this test
|
|
626
|
+
vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
627
|
+
const events = await collectIterator(provider.stream(messages));
|
|
628
|
+
// Should not emit any tool-related events
|
|
629
|
+
const toolEvents = events.filter((e) => e.type === 'modelContentBlockStartEvent' || e.type === 'modelContentBlockDeltaEvent');
|
|
630
|
+
expect(toolEvents).toHaveLength(0);
|
|
631
|
+
// The important thing is that invalid tool calls don't crash the stream
|
|
632
|
+
// and are properly skipped
|
|
633
|
+
expect(events.length).toBeGreaterThan(0); // Still got message events
|
|
634
|
+
});
|
|
635
|
+
it('tool argument deltas can be reassembled into valid JSON', async () => {
|
|
636
|
+
const mockClient = createMockClient(async function* () {
|
|
637
|
+
yield { choices: [{ delta: { role: 'assistant' }, index: 0 }] };
|
|
638
|
+
yield {
|
|
639
|
+
choices: [
|
|
640
|
+
{
|
|
641
|
+
delta: {
|
|
642
|
+
tool_calls: [
|
|
643
|
+
{
|
|
644
|
+
index: 0,
|
|
645
|
+
id: 'call_123',
|
|
646
|
+
type: 'function',
|
|
647
|
+
function: { name: 'calculator', arguments: '' },
|
|
648
|
+
},
|
|
649
|
+
],
|
|
650
|
+
},
|
|
651
|
+
index: 0,
|
|
652
|
+
},
|
|
653
|
+
],
|
|
654
|
+
};
|
|
655
|
+
// Split JSON across multiple chunks in realistic ways
|
|
656
|
+
yield { choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '{"' } }] }, index: 0 }] };
|
|
657
|
+
yield { choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: 'x":' } }] }, index: 0 }] };
|
|
658
|
+
yield { choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '10,' } }] }, index: 0 }] };
|
|
659
|
+
yield { choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '"y":' } }] }, index: 0 }] };
|
|
660
|
+
yield { choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '20}' } }] }, index: 0 }] };
|
|
661
|
+
yield { choices: [{ finish_reason: 'tool_calls', delta: {}, index: 0 }] };
|
|
662
|
+
});
|
|
663
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
664
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
665
|
+
const events = await collectIterator(provider.stream(messages));
|
|
666
|
+
// Extract and concatenate all tool input deltas
|
|
667
|
+
const inputDeltas = events
|
|
668
|
+
.filter((e) => e.type === 'modelContentBlockDeltaEvent' && e.delta.type === 'toolUseInputDelta')
|
|
669
|
+
.map((e) => e.delta.input);
|
|
670
|
+
const reassembled = inputDeltas.join('');
|
|
671
|
+
// Should be valid JSON
|
|
672
|
+
expect(() => JSON.parse(reassembled)).not.toThrow();
|
|
673
|
+
expect(JSON.parse(reassembled)).toEqual({ x: 10, y: 20 });
|
|
674
|
+
});
|
|
675
|
+
it('handles messages with both text and tool calls', async () => {
|
|
676
|
+
const mockClient = createMockClient(async function* () {
|
|
677
|
+
yield { choices: [{ delta: { role: 'assistant' }, index: 0 }] };
|
|
678
|
+
// Text content first
|
|
679
|
+
yield { choices: [{ delta: { content: 'Let me calculate ' }, index: 0 }] };
|
|
680
|
+
yield { choices: [{ delta: { content: 'that for you.' }, index: 0 }] };
|
|
681
|
+
// Then tool call
|
|
682
|
+
yield {
|
|
683
|
+
choices: [
|
|
684
|
+
{
|
|
685
|
+
delta: {
|
|
686
|
+
tool_calls: [
|
|
687
|
+
{
|
|
688
|
+
index: 0,
|
|
689
|
+
id: 'call_123',
|
|
690
|
+
type: 'function',
|
|
691
|
+
function: { name: 'calculator', arguments: '{"expr":"2+2"}' },
|
|
692
|
+
},
|
|
693
|
+
],
|
|
694
|
+
},
|
|
695
|
+
index: 0,
|
|
696
|
+
},
|
|
697
|
+
],
|
|
698
|
+
};
|
|
699
|
+
yield { choices: [{ finish_reason: 'tool_calls', delta: {}, index: 0 }] };
|
|
700
|
+
});
|
|
701
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
702
|
+
const messages = [
|
|
703
|
+
{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Calculate 2+2' }] },
|
|
704
|
+
];
|
|
705
|
+
const events = await collectIterator(provider.stream(messages));
|
|
706
|
+
// Should have text deltas followed by tool events
|
|
707
|
+
expect(events[0]?.type).toBe('modelMessageStartEvent');
|
|
708
|
+
// Text content block start
|
|
709
|
+
expect(events[1]?.type).toBe('modelContentBlockStartEvent');
|
|
710
|
+
expect(events[1].contentBlockIndex).toBe(0);
|
|
711
|
+
// Text deltas
|
|
712
|
+
expect(events[2]?.type).toBe('modelContentBlockDeltaEvent');
|
|
713
|
+
expect(events[2].delta.type).toBe('textDelta');
|
|
714
|
+
expect(events[2].delta.text).toBe('Let me calculate ');
|
|
715
|
+
// Tool events should follow
|
|
716
|
+
const toolStartEvent = events.find((e) => e.type === 'modelContentBlockStartEvent' && e.start?.type === 'toolUseStart');
|
|
717
|
+
expect(toolStartEvent).toBeDefined();
|
|
718
|
+
// Both text and tool blocks should have stop events
|
|
719
|
+
const stopEvents = events.filter((e) => e.type === 'modelContentBlockStopEvent');
|
|
720
|
+
expect(stopEvents.length).toBeGreaterThan(0);
|
|
721
|
+
});
|
|
722
|
+
});
|
|
723
|
+
describe('stop reasons', () => {
|
|
724
|
+
it('maps OpenAI stop reasons to SDK stop reasons', async () => {
|
|
725
|
+
const stopReasons = [
|
|
726
|
+
{ openai: 'stop', sdk: 'endTurn' },
|
|
727
|
+
{ openai: 'tool_calls', sdk: 'toolUse' },
|
|
728
|
+
{ openai: 'length', sdk: 'maxTokens' },
|
|
729
|
+
{ openai: 'content_filter', sdk: 'contentFiltered' },
|
|
730
|
+
];
|
|
731
|
+
for (const { openai, sdk } of stopReasons) {
|
|
732
|
+
const mockClient = createMockClient(async function* () {
|
|
733
|
+
yield {
|
|
734
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
735
|
+
};
|
|
736
|
+
yield {
|
|
737
|
+
choices: [{ finish_reason: openai, delta: {}, index: 0 }],
|
|
738
|
+
};
|
|
739
|
+
});
|
|
740
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
741
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
742
|
+
const events = await collectIterator(provider.stream(messages));
|
|
743
|
+
const stopEvent = events.find((e) => e.type === 'modelMessageStopEvent');
|
|
744
|
+
expect(stopEvent).toBeDefined();
|
|
745
|
+
expect(stopEvent.stopReason).toBe(sdk);
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
it('handles unknown stop reasons with warning', async () => {
|
|
749
|
+
const mockClient = createMockClient(async function* () {
|
|
750
|
+
yield {
|
|
751
|
+
choices: [{ delta: { role: 'assistant' }, index: 0 }],
|
|
752
|
+
};
|
|
753
|
+
yield {
|
|
754
|
+
choices: [{ finish_reason: 'new_unknown_reason', delta: {}, index: 0 }],
|
|
755
|
+
};
|
|
756
|
+
});
|
|
757
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
758
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
759
|
+
const events = await collectIterator(provider.stream(messages));
|
|
760
|
+
// Should convert unknown stop reason to camelCase
|
|
761
|
+
const stopEvent = events.find((e) => e.type === 'modelMessageStopEvent');
|
|
762
|
+
expect(stopEvent).toBeDefined();
|
|
763
|
+
expect(stopEvent.stopReason).toBe('newUnknownReason');
|
|
764
|
+
// Note: Warning logging is verified manually/visually since console.warn spying
|
|
765
|
+
// has test isolation issues when running the full test suite
|
|
766
|
+
});
|
|
767
|
+
});
|
|
768
|
+
describe('API request formatting', () => {
|
|
769
|
+
it('formats API request correctly with all options', async () => {
|
|
770
|
+
let capturedRequest = null;
|
|
771
|
+
let callCount = 0;
|
|
772
|
+
const mockClient = {
|
|
773
|
+
chat: {
|
|
774
|
+
completions: {
|
|
775
|
+
create: vi.fn(async (request) => {
|
|
776
|
+
capturedRequest = request;
|
|
777
|
+
callCount++;
|
|
778
|
+
// Return an async generator
|
|
779
|
+
return (async function* () {
|
|
780
|
+
yield { choices: [{ delta: { role: 'assistant' }, index: 0 }] };
|
|
781
|
+
yield { choices: [{ finish_reason: 'stop', delta: {}, index: 0 }] };
|
|
782
|
+
})();
|
|
783
|
+
}),
|
|
784
|
+
},
|
|
785
|
+
},
|
|
786
|
+
};
|
|
787
|
+
const provider = new OpenAIModel({
|
|
788
|
+
modelId: 'gpt-4o',
|
|
789
|
+
client: mockClient,
|
|
790
|
+
temperature: 0.7,
|
|
791
|
+
maxTokens: 1000,
|
|
792
|
+
});
|
|
793
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
794
|
+
const toolSpecs = [
|
|
795
|
+
{
|
|
796
|
+
name: 'calculator',
|
|
797
|
+
description: 'Calculate expressions',
|
|
798
|
+
inputSchema: { type: 'object', properties: { expr: { type: 'string' } } },
|
|
799
|
+
},
|
|
800
|
+
];
|
|
801
|
+
await collectIterator(provider.stream(messages, {
|
|
802
|
+
systemPrompt: 'You are a helpful assistant',
|
|
803
|
+
toolSpecs,
|
|
804
|
+
toolChoice: { auto: {} },
|
|
805
|
+
}));
|
|
806
|
+
// Verify create was called with correct structure
|
|
807
|
+
expect(callCount).toBe(1);
|
|
808
|
+
expect(capturedRequest).toBeDefined();
|
|
809
|
+
expect(capturedRequest).toMatchObject({
|
|
810
|
+
model: 'gpt-4o',
|
|
811
|
+
stream: true,
|
|
812
|
+
stream_options: { include_usage: true },
|
|
813
|
+
temperature: 0.7,
|
|
814
|
+
max_tokens: 1000,
|
|
815
|
+
messages: [
|
|
816
|
+
{ role: 'system', content: 'You are a helpful assistant' },
|
|
817
|
+
{ role: 'user', content: 'Hi' },
|
|
818
|
+
],
|
|
819
|
+
tools: [
|
|
820
|
+
{
|
|
821
|
+
type: 'function',
|
|
822
|
+
function: {
|
|
823
|
+
name: 'calculator',
|
|
824
|
+
description: 'Calculate expressions',
|
|
825
|
+
parameters: { type: 'object', properties: { expr: { type: 'string' } } },
|
|
826
|
+
},
|
|
827
|
+
},
|
|
828
|
+
],
|
|
829
|
+
tool_choice: 'auto',
|
|
830
|
+
});
|
|
831
|
+
});
|
|
832
|
+
});
|
|
833
|
+
describe('systemPrompt handling', () => {
|
|
834
|
+
// Create mock client factory that captures request in provided container
|
|
835
|
+
const createMockClientWithCapture = (captureContainer) => {
|
|
836
|
+
return {
|
|
837
|
+
chat: {
|
|
838
|
+
completions: {
|
|
839
|
+
create: vi.fn(async (request) => {
|
|
840
|
+
captureContainer.request = request;
|
|
841
|
+
return (async function* () {
|
|
842
|
+
yield { choices: [{ delta: { role: 'assistant' }, index: 0 }] };
|
|
843
|
+
yield { choices: [{ finish_reason: 'stop', delta: {}, index: 0 }] };
|
|
844
|
+
})();
|
|
845
|
+
}),
|
|
846
|
+
},
|
|
847
|
+
},
|
|
848
|
+
};
|
|
849
|
+
};
|
|
850
|
+
it('formats array system prompt with text blocks only', async () => {
|
|
851
|
+
const captured = { request: null };
|
|
852
|
+
const mockClient = createMockClientWithCapture(captured);
|
|
853
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
854
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hello' }] }];
|
|
855
|
+
await collectIterator(provider.stream(messages, {
|
|
856
|
+
systemPrompt: [
|
|
857
|
+
{ type: 'textBlock', text: 'You are a helpful assistant' },
|
|
858
|
+
{ type: 'textBlock', text: 'Additional context here' },
|
|
859
|
+
],
|
|
860
|
+
}));
|
|
861
|
+
expect(captured.request).toBeDefined();
|
|
862
|
+
expect(captured.request.messages).toEqual([
|
|
863
|
+
{ role: 'system', content: 'You are a helpful assistantAdditional context here' },
|
|
864
|
+
{ role: 'user', content: 'Hello' },
|
|
865
|
+
]);
|
|
866
|
+
});
|
|
867
|
+
it('formats array system prompt with cache points', async () => {
|
|
868
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
869
|
+
const captured = { request: null };
|
|
870
|
+
const mockClient = createMockClientWithCapture(captured);
|
|
871
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
872
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hello' }] }];
|
|
873
|
+
collectIterator(provider.stream(messages, {
|
|
874
|
+
systemPrompt: [
|
|
875
|
+
{ type: 'textBlock', text: 'You are a helpful assistant' },
|
|
876
|
+
{ type: 'textBlock', text: 'Large context document' },
|
|
877
|
+
{ type: 'cachePointBlock', cacheType: 'default' },
|
|
878
|
+
],
|
|
879
|
+
}));
|
|
880
|
+
// Verify warning was logged
|
|
881
|
+
expect(warnSpy).toHaveBeenCalledWith('Cache points are not supported in OpenAI system prompts and will be ignored.');
|
|
882
|
+
// Verify system message contains only text (cache points ignored)
|
|
883
|
+
expect(captured.request).toBeDefined();
|
|
884
|
+
expect(captured.request.messages).toEqual([
|
|
885
|
+
{ role: 'system', content: 'You are a helpful assistantLarge context document' },
|
|
886
|
+
{ role: 'user', content: 'Hello' },
|
|
887
|
+
]);
|
|
888
|
+
warnSpy.mockRestore();
|
|
889
|
+
});
|
|
890
|
+
it('handles empty array system prompt', async () => {
|
|
891
|
+
const captured = { request: null };
|
|
892
|
+
const mockClient = createMockClientWithCapture(captured);
|
|
893
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
894
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hello' }] }];
|
|
895
|
+
await collectIterator(provider.stream(messages, {
|
|
896
|
+
systemPrompt: [],
|
|
897
|
+
}));
|
|
898
|
+
// Empty array should not add system message
|
|
899
|
+
expect(captured.request).toBeDefined();
|
|
900
|
+
expect(captured.request.messages).toEqual([{ role: 'user', content: 'Hello' }]);
|
|
901
|
+
});
|
|
902
|
+
it('formats array system prompt with single text block', async () => {
|
|
903
|
+
const captured = { request: null };
|
|
904
|
+
const mockClient = createMockClientWithCapture(captured);
|
|
905
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
906
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hello' }] }];
|
|
907
|
+
await collectIterator(provider.stream(messages, {
|
|
908
|
+
systemPrompt: [{ type: 'textBlock', text: 'You are a helpful assistant' }],
|
|
909
|
+
}));
|
|
910
|
+
expect(captured.request).toBeDefined();
|
|
911
|
+
expect(captured.request.messages).toEqual([
|
|
912
|
+
{ role: 'system', content: 'You are a helpful assistant' },
|
|
913
|
+
{ role: 'user', content: 'Hello' },
|
|
914
|
+
]);
|
|
915
|
+
});
|
|
916
|
+
});
|
|
917
|
+
describe('error handling', () => {
|
|
918
|
+
it('throws ContextWindowOverflowError for structured error with code', async () => {
|
|
919
|
+
const mockClient = {
|
|
920
|
+
chat: {
|
|
921
|
+
completions: {
|
|
922
|
+
create: vi.fn(async () => {
|
|
923
|
+
const error = new Error('Context length exceeded');
|
|
924
|
+
error.code = 'context_length_exceeded';
|
|
925
|
+
throw error;
|
|
926
|
+
}),
|
|
927
|
+
},
|
|
928
|
+
},
|
|
929
|
+
};
|
|
930
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
931
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
932
|
+
await expect(async () => {
|
|
933
|
+
for await (const _ of provider.stream(messages)) {
|
|
934
|
+
// Should not reach here
|
|
935
|
+
}
|
|
936
|
+
}).rejects.toThrow(ContextWindowOverflowError);
|
|
937
|
+
});
|
|
938
|
+
it('throws ContextWindowOverflowError for error with message pattern', async () => {
|
|
939
|
+
const mockClient = {
|
|
940
|
+
chat: {
|
|
941
|
+
completions: {
|
|
942
|
+
create: vi.fn(async () => {
|
|
943
|
+
throw new Error('maximum context length exceeded');
|
|
944
|
+
}),
|
|
945
|
+
},
|
|
946
|
+
},
|
|
947
|
+
};
|
|
948
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
949
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
950
|
+
await expect(async () => {
|
|
951
|
+
for await (const _ of provider.stream(messages)) {
|
|
952
|
+
// Should not reach here
|
|
953
|
+
}
|
|
954
|
+
}).rejects.toThrow(ContextWindowOverflowError);
|
|
955
|
+
});
|
|
956
|
+
it('throws ContextWindowOverflowError for APIError instance', async () => {
|
|
957
|
+
const mockClient = {
|
|
958
|
+
chat: {
|
|
959
|
+
completions: {
|
|
960
|
+
create: vi.fn(async () => {
|
|
961
|
+
// Simulate APIError from openai package
|
|
962
|
+
const error = new Error('Context length exceeded');
|
|
963
|
+
error.name = 'APIError';
|
|
964
|
+
error.status = 400;
|
|
965
|
+
error.code = 'context_length_exceeded';
|
|
966
|
+
// Make it behave like an APIError instance
|
|
967
|
+
Object.setPrototypeOf(error, Error.prototype);
|
|
968
|
+
throw error;
|
|
969
|
+
}),
|
|
970
|
+
},
|
|
971
|
+
},
|
|
972
|
+
};
|
|
973
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
974
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
975
|
+
await expect(async () => {
|
|
976
|
+
for await (const _ of provider.stream(messages)) {
|
|
977
|
+
// Should not reach here
|
|
978
|
+
}
|
|
979
|
+
}).rejects.toThrow(ContextWindowOverflowError);
|
|
980
|
+
});
|
|
981
|
+
it('passes through other errors unchanged', async () => {
|
|
982
|
+
const mockClient = {
|
|
983
|
+
chat: {
|
|
984
|
+
completions: {
|
|
985
|
+
create: vi.fn(async () => {
|
|
986
|
+
throw new Error('Invalid API key');
|
|
987
|
+
}),
|
|
988
|
+
},
|
|
989
|
+
},
|
|
990
|
+
};
|
|
991
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
992
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
993
|
+
await expect(async () => {
|
|
994
|
+
for await (const _ of provider.stream(messages)) {
|
|
995
|
+
// Should not reach here
|
|
996
|
+
}
|
|
997
|
+
}).rejects.toThrow('Invalid API key');
|
|
998
|
+
});
|
|
999
|
+
it('handles stream interruption errors', async () => {
|
|
1000
|
+
const mockClient = createMockClient(async function* () {
|
|
1001
|
+
yield { choices: [{ delta: { role: 'assistant' }, index: 0 }] };
|
|
1002
|
+
yield { choices: [{ delta: { content: 'Hello' }, index: 0 }] };
|
|
1003
|
+
// Stream interruption
|
|
1004
|
+
throw new Error('Network connection lost');
|
|
1005
|
+
});
|
|
1006
|
+
const provider = new OpenAIModel({ modelId: 'gpt-4o', client: mockClient });
|
|
1007
|
+
const messages = [{ type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hi' }] }];
|
|
1008
|
+
await expect(async () => {
|
|
1009
|
+
for await (const _ of provider.stream(messages)) {
|
|
1010
|
+
// Stream will be interrupted
|
|
1011
|
+
}
|
|
1012
|
+
}).rejects.toThrow('Network connection lost');
|
|
1013
|
+
});
|
|
1014
|
+
});
|
|
1015
|
+
});
|
|
1016
|
+
//# sourceMappingURL=openai.test.js.map
|