veryfront 0.1.149 → 0.1.150
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/esm/cli/commands/dev/command.js +3 -3
- package/esm/cli/commands/workflow/command-help.js +1 -1
- package/esm/cli/commands/workflow/command.js +2 -2
- package/esm/cli/shared/config.d.ts.map +1 -1
- package/esm/cli/shared/config.js +1 -3
- package/esm/deno.d.ts +5 -11
- package/esm/deno.js +16 -16
- package/esm/src/agent/chat-handler.js +1 -1
- package/esm/src/agent/composition/composition.d.ts +1 -1
- package/esm/src/agent/composition/composition.d.ts.map +1 -1
- package/esm/src/agent/composition/composition.js +2 -2
- package/esm/src/agent/{ai-defaults.d.ts → defaults.d.ts} +1 -1
- package/esm/src/agent/defaults.d.ts.map +1 -0
- package/esm/src/agent/factory.js +1 -1
- package/esm/src/agent/react/index.d.ts +1 -1
- package/esm/src/agent/react/index.d.ts.map +1 -1
- package/esm/src/agent/react/use-chat/browser-inference/browser-engine.d.ts +5 -6
- package/esm/src/agent/react/use-chat/browser-inference/browser-engine.d.ts.map +1 -1
- package/esm/src/agent/react/use-chat/browser-inference/browser-engine.js +1 -1
- package/esm/src/agent/react/use-chat/index.d.ts +2 -1
- package/esm/src/agent/react/use-chat/index.d.ts.map +1 -1
- package/esm/src/agent/react/use-chat/stream-protocol.d.ts +2 -0
- package/esm/src/agent/react/use-chat/stream-protocol.d.ts.map +1 -0
- package/esm/src/agent/react/use-chat/stream-protocol.js +1 -0
- package/esm/src/agent/react/use-chat/streaming/parts-builder.d.ts +2 -2
- package/esm/src/agent/react/use-chat/streaming/parts-builder.d.ts.map +1 -1
- package/esm/src/agent/react/use-chat/streaming/types.d.ts +4 -4
- package/esm/src/agent/react/use-chat/streaming/types.d.ts.map +1 -1
- package/esm/src/agent/react/use-chat/types.d.ts +7 -55
- package/esm/src/agent/react/use-chat/types.d.ts.map +1 -1
- package/esm/src/agent/react/use-chat/use-chat.d.ts +4 -4
- package/esm/src/agent/react/use-chat/use-chat.d.ts.map +1 -1
- package/esm/src/agent/react/use-chat/use-chat.js +2 -2
- package/esm/src/agent/react/use-chat/utils.d.ts +2 -2
- package/esm/src/agent/react/use-chat/utils.d.ts.map +1 -1
- package/esm/src/agent/runtime/{ai-stream-handler.d.ts → chat-stream-handler.d.ts} +13 -13
- package/esm/src/agent/runtime/chat-stream-handler.d.ts.map +1 -0
- package/esm/src/agent/runtime/{ai-stream-handler.js → chat-stream-handler.js} +91 -89
- package/esm/src/agent/runtime/constants.js +1 -1
- package/esm/src/agent/runtime/index.d.ts +7 -5
- package/esm/src/agent/runtime/index.d.ts.map +1 -1
- package/esm/src/agent/runtime/index.js +67 -30
- package/esm/src/agent/runtime/model-message-converter.d.ts +7 -7
- package/esm/src/agent/runtime/model-message-converter.d.ts.map +1 -1
- package/esm/src/agent/runtime/model-message-converter.js +8 -6
- package/esm/src/agent/runtime/model-runtime-types.d.ts +44 -0
- package/esm/src/agent/runtime/model-runtime-types.d.ts.map +1 -0
- package/esm/src/agent/runtime/model-runtime-types.js +8 -0
- package/esm/src/agent/runtime/model-tool-converter.d.ts +9 -9
- package/esm/src/agent/runtime/model-tool-converter.d.ts.map +1 -1
- package/esm/src/agent/runtime/model-tool-converter.js +10 -22
- package/esm/src/agent/runtime/provider-native-tools.d.ts +3 -0
- package/esm/src/agent/runtime/provider-native-tools.d.ts.map +1 -0
- package/esm/src/agent/runtime/provider-native-tools.js +56 -0
- package/esm/src/agent/runtime/repair-tool-call.d.ts +2 -2
- package/esm/src/agent/runtime/repair-tool-call.d.ts.map +1 -1
- package/esm/src/agent/runtime/repair-tool-call.js +3 -3
- package/esm/src/agent/runtime/runtime-tool-builder.d.ts +48 -0
- package/esm/src/agent/runtime/runtime-tool-builder.d.ts.map +1 -0
- package/esm/src/agent/runtime/runtime-tool-builder.js +28 -0
- package/esm/src/agent/runtime/runtime-tool-errors.d.ts +16 -0
- package/esm/src/agent/runtime/runtime-tool-errors.d.ts.map +1 -0
- package/esm/src/agent/runtime/runtime-tool-errors.js +28 -0
- package/esm/src/agent/runtime/runtime-tool-types.d.ts +104 -0
- package/esm/src/agent/runtime/runtime-tool-types.d.ts.map +1 -0
- package/esm/src/agent/runtime/runtime-tool-types.js +7 -0
- package/esm/src/chat/index.d.ts +9 -9
- package/esm/src/chat/index.d.ts.map +1 -1
- package/esm/src/chat/index.js +7 -7
- package/esm/src/chat/protocol.d.ts +156 -0
- package/esm/src/chat/protocol.d.ts.map +1 -0
- package/esm/src/chat/protocol.js +7 -0
- package/esm/src/discovery/import-rewriter.d.ts.map +1 -1
- package/esm/src/discovery/import-rewriter.js +1 -14
- package/esm/src/discovery/index.d.ts +1 -1
- package/esm/src/discovery/index.d.ts.map +1 -1
- package/esm/src/discovery/index.js +2 -2
- package/esm/src/discovery/provider-config-validator.d.ts +14 -0
- package/esm/src/discovery/provider-config-validator.d.ts.map +1 -0
- package/esm/src/discovery/{config-validator.js → provider-config-validator.js} +4 -4
- package/esm/src/discovery/transpiler.d.ts.map +1 -1
- package/esm/src/discovery/transpiler.js +0 -3
- package/esm/src/embedding/embedding.d.ts.map +1 -1
- package/esm/src/embedding/embedding.js +1 -1
- package/esm/src/embedding/index.d.ts +3 -3
- package/esm/src/embedding/index.d.ts.map +1 -1
- package/esm/src/embedding/index.js +3 -3
- package/esm/src/embedding/rag-store.d.ts.map +1 -1
- package/esm/src/embedding/rag-store.js +1 -1
- package/esm/src/embedding/resolve.d.ts +5 -7
- package/esm/src/embedding/resolve.d.ts.map +1 -1
- package/esm/src/embedding/resolve.js +6 -9
- package/esm/src/embedding/vector-store.d.ts.map +1 -1
- package/esm/src/embedding/vector-store.js +1 -1
- package/esm/src/embedding/veryfront-cloud/provider.d.ts +2 -2
- package/esm/src/embedding/veryfront-cloud/provider.d.ts.map +1 -1
- package/esm/src/embedding/veryfront-cloud/provider.js +5 -6
- package/esm/src/markdown/index.d.ts +1 -1
- package/esm/src/markdown/index.d.ts.map +1 -1
- package/esm/src/markdown/index.js +1 -1
- package/esm/src/modules/import-map/default-import-map.js +2 -2
- package/esm/src/prompt/registry.d.ts +1 -1
- package/esm/src/prompt/registry.d.ts.map +1 -1
- package/esm/src/prompt/registry.js +2 -2
- package/esm/src/provider/index.d.ts +7 -8
- package/esm/src/provider/index.d.ts.map +1 -1
- package/esm/src/provider/index.js +6 -8
- package/esm/src/provider/local/embedding-runtime-adapter.d.ts +19 -0
- package/esm/src/provider/local/embedding-runtime-adapter.d.ts.map +1 -0
- package/esm/src/provider/local/{local-embedding-adapter.js → embedding-runtime-adapter.js} +5 -5
- package/esm/src/provider/local/model-runtime-adapter.d.ts +18 -0
- package/esm/src/provider/local/model-runtime-adapter.d.ts.map +1 -0
- package/esm/src/provider/local/{ai-sdk-adapter.js → model-runtime-adapter.js} +10 -12
- package/esm/src/provider/model-registry.d.ts +11 -12
- package/esm/src/provider/model-registry.d.ts.map +1 -1
- package/esm/src/provider/model-registry.js +17 -20
- package/esm/src/provider/runtime-inspection.d.ts +6 -0
- package/esm/src/provider/runtime-inspection.d.ts.map +1 -0
- package/esm/src/provider/runtime-inspection.js +19 -0
- package/esm/src/provider/runtime-loader.d.ts +27 -0
- package/esm/src/provider/runtime-loader.d.ts.map +1 -0
- package/esm/src/provider/runtime-loader.js +1307 -0
- package/esm/src/provider/types.d.ts +35 -0
- package/esm/src/provider/types.d.ts.map +1 -0
- package/esm/src/provider/types.js +1 -0
- package/esm/src/provider/veryfront-cloud/provider.d.ts +2 -2
- package/esm/src/provider/veryfront-cloud/provider.d.ts.map +1 -1
- package/esm/src/provider/veryfront-cloud/provider.js +7 -9
- package/esm/src/provider/veryfront-cloud/shared.d.ts +1 -1
- package/esm/src/provider/veryfront-cloud/shared.js +1 -1
- package/esm/src/react/components/chat/agent-card.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/animations.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/attachment-pill.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/branch-picker.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/code-block.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/drop-zone.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/empty-state.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/inference-badge.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/components/inference-badge.js +1 -1
- package/esm/src/react/components/chat/chat/components/inline-citation.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/message-actions.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/message-edit-form.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/message-feedback.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/quick-actions.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/reasoning.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/sidebar.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/components/skill-badge.d.ts +3 -3
- package/esm/src/react/components/chat/chat/components/skill-badge.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/components/skill-badge.js +1 -1
- package/esm/src/react/components/chat/chat/components/sources.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/step-indicator.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/tab-switcher.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/components/tool-ui.d.ts +3 -3
- package/esm/src/react/components/chat/chat/components/tool-ui.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/components/tool-ui.js +1 -1
- package/esm/src/react/components/chat/chat/components/upgrade-cta.d.ts.map +1 -0
- package/esm/src/react/components/chat/chat/components/uploads-panel.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/api.d.ts +1 -1
- package/esm/src/react/components/chat/chat/composition/api.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/api.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-composer.d.ts +3 -3
- package/esm/src/react/components/chat/chat/composition/chat-composer.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-composer.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-empty.d.ts +1 -1
- package/esm/src/react/components/chat/chat/composition/chat-empty.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-empty.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-if.d.ts +1 -1
- package/esm/src/react/components/chat/chat/composition/chat-if.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-if.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-message-list.d.ts +5 -5
- package/esm/src/react/components/chat/chat/composition/chat-message-list.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-message-list.js +2 -2
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-root.d.ts +3 -3
- package/esm/src/react/components/chat/chat/composition/chat-root.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/chat-root.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/composition/error-banner.d.ts +1 -1
- package/esm/src/react/components/chat/chat/composition/error-banner.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/error-banner.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/composition/message.d.ts +4 -4
- package/esm/src/react/components/chat/chat/composition/message.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/message.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/composition/model-avatar.d.ts +1 -1
- package/esm/src/react/components/chat/chat/composition/model-avatar.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/composition/model-avatar.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/contexts/chat-context.d.ts +3 -3
- package/esm/src/react/components/chat/chat/contexts/chat-context.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/contexts/chat-context.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/contexts/composer-context.d.ts +1 -1
- package/esm/src/react/components/chat/chat/contexts/composer-context.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/contexts/composer-context.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/contexts/index.d.ts +1 -1
- package/esm/src/react/components/chat/chat/contexts/index.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/contexts/index.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/contexts/message-context.d.ts +3 -3
- package/esm/src/react/components/chat/chat/contexts/message-context.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/contexts/message-context.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/contexts/thread-list-context.d.ts +1 -1
- package/esm/src/react/components/chat/chat/contexts/thread-list-context.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/contexts/thread-list-context.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/hooks/use-threads.d.ts +2 -2
- package/esm/src/react/components/chat/chat/hooks/use-threads.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/index.d.ts +5 -5
- package/esm/src/react/components/chat/chat/index.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/index.js +1 -1
- package/esm/src/react/components/chat/chat/utils/export.d.ts +10 -0
- package/esm/src/react/components/chat/chat/utils/export.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/utils/export.js +1 -1
- package/esm/src/react/components/{ai → chat}/chat/utils/message-parts.d.ts +10 -10
- package/esm/src/react/components/chat/chat/utils/message-parts.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat/utils/message-parts.js +2 -2
- package/esm/src/react/components/chat/chat-with-sidebar.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat.d.ts +1 -1
- package/esm/src/react/components/chat/chat.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/chat.js +1 -1
- package/esm/src/react/components/chat/csp-nonce.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/error-boundary.d.ts +6 -6
- package/esm/src/react/components/chat/error-boundary.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/error-boundary.js +5 -5
- package/esm/src/react/components/{ai → chat}/icons/index.d.ts +1 -1
- package/esm/src/react/components/chat/icons/index.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/icons/index.js +1 -1
- package/esm/src/react/components/chat/markdown.d.ts.map +1 -0
- package/esm/src/react/components/{ai → chat}/message.d.ts +8 -8
- package/esm/src/react/components/chat/message.d.ts.map +1 -0
- package/esm/src/react/components/chat/model-selector.d.ts.map +1 -0
- package/esm/src/react/components/chat/theme.d.ts.map +1 -0
- package/esm/src/react/primitives/message-list.d.ts +2 -2
- package/esm/src/react/primitives/message-list.d.ts.map +1 -1
- package/esm/src/react/primitives/tool-primitives.d.ts +3 -3
- package/esm/src/react/primitives/tool-primitives.d.ts.map +1 -1
- package/esm/src/{ai/registry-manager.d.ts → registry/project-scoped-registry-manager.d.ts} +5 -4
- package/esm/src/registry/project-scoped-registry-manager.d.ts.map +1 -0
- package/esm/src/{ai/registry-manager.js → registry/project-scoped-registry-manager.js} +5 -4
- package/esm/src/{ai/registry-facade.d.ts → registry/scoped-registry-facade.d.ts} +2 -2
- package/esm/src/registry/scoped-registry-facade.d.ts.map +1 -0
- package/esm/src/rendering/script-page-handling.d.ts.map +1 -1
- package/esm/src/rendering/script-page-handling.js +0 -2
- package/esm/src/resource/registry.d.ts +1 -1
- package/esm/src/resource/registry.d.ts.map +1 -1
- package/esm/src/resource/registry.js +2 -2
- package/esm/src/routing/api/module-loader/loader.js +3 -19
- package/esm/src/runtime/runtime-bridge.d.ts +82 -0
- package/esm/src/runtime/runtime-bridge.d.ts.map +1 -0
- package/esm/src/runtime/runtime-bridge.js +367 -0
- package/esm/src/server/dev-server/file-watch-setup.d.ts +4 -4
- package/esm/src/server/dev-server/file-watch-setup.d.ts.map +1 -1
- package/esm/src/server/dev-server/file-watch-setup.js +14 -14
- package/esm/src/server/dev-server/server.d.ts +2 -2
- package/esm/src/server/dev-server/server.d.ts.map +1 -1
- package/esm/src/server/dev-server/server.js +20 -18
- package/esm/src/server/dev-ui/manifest.d.ts +1 -1
- package/esm/src/server/dev-ui/manifest.js +2 -2
- package/esm/src/server/handlers/dev/framework-candidates.generated.d.ts.map +1 -1
- package/esm/src/server/handlers/dev/framework-candidates.generated.js +58 -61
- package/esm/src/server/handlers/request/api/api-handler-wrapper.js +1 -1
- package/esm/src/server/handlers/request/api/project-discovery.d.ts +1 -1
- package/esm/src/server/handlers/request/api/project-discovery.d.ts.map +1 -1
- package/esm/src/server/handlers/request/api/project-discovery.js +12 -10
- package/esm/src/server/production-server.js +2 -2
- package/esm/src/server/services/rsc/endpoints/rsc-bundles.generated.d.ts.map +1 -1
- package/esm/src/server/services/rsc/endpoints/rsc-bundles.generated.js +2 -2
- package/esm/src/skill/registry.d.ts +1 -1
- package/esm/src/skill/registry.d.ts.map +1 -1
- package/esm/src/skill/registry.js +2 -2
- package/esm/src/tool/registry.d.ts +1 -1
- package/esm/src/tool/registry.d.ts.map +1 -1
- package/esm/src/tool/registry.js +2 -2
- package/esm/src/transforms/npm-import-rewrites.d.ts +1 -1
- package/esm/src/transforms/npm-import-rewrites.d.ts.map +1 -1
- package/esm/src/transforms/npm-import-rewrites.js +5 -13
- package/esm/src/utils/constants/server.d.ts +4 -4
- package/esm/src/utils/constants/server.d.ts.map +1 -1
- package/esm/src/utils/constants/server.js +4 -4
- package/esm/src/utils/id.js +1 -1
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/esm/src/workflow/discovery/workflow-discovery.d.ts +3 -3
- package/esm/src/workflow/discovery/workflow-discovery.d.ts.map +1 -1
- package/esm/src/workflow/discovery/workflow-discovery.js +3 -3
- package/esm/src/workflow/registry.d.ts +1 -1
- package/esm/src/workflow/registry.d.ts.map +1 -1
- package/esm/src/workflow/registry.js +2 -2
- package/package.json +4 -5
- package/src/cli/commands/dev/command.ts +3 -3
- package/src/cli/commands/workflow/command-help.ts +1 -1
- package/src/cli/commands/workflow/command.ts +2 -2
- package/src/cli/shared/config.ts +1 -3
- package/src/deno.js +16 -16
- package/src/src/agent/chat-handler.ts +1 -1
- package/src/src/agent/composition/composition.ts +2 -2
- package/src/src/agent/factory.ts +1 -1
- package/src/src/agent/react/index.ts +11 -9
- package/src/src/agent/react/use-chat/browser-inference/browser-engine.ts +9 -10
- package/src/src/agent/react/use-chat/index.ts +10 -9
- package/src/src/agent/react/use-chat/stream-protocol.ts +1 -0
- package/src/src/agent/react/use-chat/streaming/handler.ts +17 -17
- package/src/src/agent/react/use-chat/streaming/parts-builder.ts +5 -5
- package/src/src/agent/react/use-chat/streaming/types.ts +4 -4
- package/src/src/agent/react/use-chat/types.ts +27 -73
- package/src/src/agent/react/use-chat/use-chat.ts +13 -13
- package/src/src/agent/react/use-chat/utils.ts +2 -2
- package/src/src/agent/runtime/{ai-stream-handler.ts → chat-stream-handler.ts} +100 -96
- package/src/src/agent/runtime/constants.ts +1 -1
- package/src/src/agent/runtime/index.ts +88 -40
- package/src/src/agent/runtime/model-message-converter.ts +22 -33
- package/src/src/agent/runtime/model-runtime-types.ts +55 -0
- package/src/src/agent/runtime/model-tool-converter.ts +29 -26
- package/src/src/agent/runtime/provider-native-tools.ts +61 -0
- package/src/src/agent/runtime/repair-tool-call.ts +6 -9
- package/src/src/agent/runtime/runtime-tool-builder.ts +82 -0
- package/src/src/agent/runtime/runtime-tool-errors.ts +46 -0
- package/src/src/agent/runtime/runtime-tool-types.ts +114 -0
- package/src/src/chat/index.ts +22 -20
- package/src/src/chat/protocol.ts +210 -0
- package/src/src/discovery/import-rewriter.ts +1 -14
- package/src/src/discovery/index.ts +2 -2
- package/src/src/discovery/{config-validator.ts → provider-config-validator.ts} +4 -4
- package/src/src/discovery/transpiler.ts +0 -3
- package/src/src/embedding/embedding.ts +1 -1
- package/src/src/embedding/index.ts +3 -3
- package/src/src/embedding/rag-store.ts +1 -1
- package/src/src/embedding/resolve.ts +15 -12
- package/src/src/embedding/vector-store.ts +1 -1
- package/src/src/embedding/veryfront-cloud/provider.ts +10 -8
- package/src/src/markdown/index.ts +1 -1
- package/src/src/modules/import-map/default-import-map.ts +2 -2
- package/src/src/prompt/registry.ts +2 -2
- package/src/src/provider/index.ts +7 -8
- package/src/src/provider/local/{local-embedding-adapter.ts → embedding-runtime-adapter.ts} +8 -8
- package/src/src/provider/local/{ai-sdk-adapter.ts → model-runtime-adapter.ts} +13 -16
- package/src/src/provider/model-registry.ts +31 -26
- package/src/src/provider/runtime-inspection.ts +25 -0
- package/src/src/provider/runtime-loader.ts +1854 -0
- package/src/src/provider/types.ts +38 -0
- package/src/src/provider/veryfront-cloud/provider.ts +13 -11
- package/src/src/provider/veryfront-cloud/shared.ts +1 -1
- package/src/src/react/components/{ai → chat}/chat/components/inference-badge.tsx +1 -1
- package/src/src/react/components/{ai → chat}/chat/components/skill-badge.tsx +3 -3
- package/src/src/react/components/{ai → chat}/chat/components/tool-ui.tsx +3 -3
- package/src/src/react/components/{ai → chat}/chat/composition/api.tsx +1 -1
- package/src/src/react/components/{ai → chat}/chat/composition/chat-composer.tsx +3 -3
- package/src/src/react/components/{ai → chat}/chat/composition/chat-empty.tsx +1 -1
- package/src/src/react/components/{ai → chat}/chat/composition/chat-if.tsx +1 -1
- package/src/src/react/components/{ai → chat}/chat/composition/chat-message-list.tsx +11 -11
- package/src/src/react/components/{ai → chat}/chat/composition/chat-root.tsx +3 -3
- package/src/src/react/components/{ai → chat}/chat/composition/error-banner.tsx +1 -1
- package/src/src/react/components/{ai → chat}/chat/composition/message.tsx +9 -4
- package/src/src/react/components/{ai → chat}/chat/composition/model-avatar.tsx +1 -1
- package/src/src/react/components/{ai → chat}/chat/contexts/chat-context.tsx +3 -3
- package/src/src/react/components/{ai → chat}/chat/contexts/composer-context.tsx +1 -1
- package/src/src/react/components/{ai → chat}/chat/contexts/index.ts +1 -1
- package/src/src/react/components/{ai → chat}/chat/contexts/message-context.tsx +3 -3
- package/src/src/react/components/{ai → chat}/chat/contexts/thread-list-context.tsx +1 -1
- package/src/src/react/components/{ai → chat}/chat/hooks/use-threads.ts +2 -2
- package/src/src/react/components/{ai → chat}/chat/index.tsx +7 -7
- package/src/src/react/components/{ai → chat}/chat/utils/export.ts +4 -4
- package/src/src/react/components/{ai → chat}/chat/utils/message-parts.ts +13 -13
- package/src/src/react/components/{ai → chat}/chat.tsx +1 -1
- package/src/src/react/components/{ai → chat}/error-boundary.tsx +11 -11
- package/src/src/react/components/{ai → chat}/icons/index.ts +1 -1
- package/src/src/react/components/{ai → chat}/message.tsx +11 -11
- package/src/src/react/primitives/message-list.tsx +2 -2
- package/src/src/react/primitives/tool-primitives.tsx +4 -4
- package/src/src/{ai/registry-manager.ts → registry/project-scoped-registry-manager.ts} +5 -4
- package/src/src/{ai/registry-facade.ts → registry/scoped-registry-facade.ts} +1 -1
- package/src/src/rendering/script-page-handling.ts +0 -2
- package/src/src/resource/registry.ts +2 -2
- package/src/src/routing/api/module-loader/loader.ts +3 -19
- package/src/src/runtime/runtime-bridge.ts +608 -0
- package/src/src/server/dev-server/file-watch-setup.ts +14 -14
- package/src/src/server/dev-server/server.ts +20 -18
- package/src/src/server/dev-ui/manifest.js +2 -2
- package/src/src/server/handlers/dev/framework-candidates.generated.ts +58 -61
- package/src/src/server/handlers/request/api/api-handler-wrapper.ts +1 -1
- package/src/src/server/handlers/request/api/project-discovery.ts +12 -10
- package/src/src/server/production-server.ts +2 -2
- package/src/src/server/services/rsc/endpoints/rsc-bundles.generated.ts +2 -2
- package/src/src/skill/registry.ts +2 -2
- package/src/src/tool/registry.ts +2 -2
- package/src/src/transforms/npm-import-rewrites.ts +5 -13
- package/src/src/utils/constants/server.ts +4 -4
- package/src/src/utils/id.ts +1 -1
- package/src/src/utils/version-constant.ts +1 -1
- package/src/src/workflow/discovery/workflow-discovery.ts +4 -4
- package/src/src/workflow/registry.ts +2 -2
- package/esm/src/agent/ai-defaults.d.ts.map +0 -1
- package/esm/src/agent/runtime/ai-stream-handler.d.ts.map +0 -1
- package/esm/src/ai/registry-facade.d.ts.map +0 -1
- package/esm/src/ai/registry-manager.d.ts.map +0 -1
- package/esm/src/discovery/config-validator.d.ts +0 -14
- package/esm/src/discovery/config-validator.d.ts.map +0 -1
- package/esm/src/provider/local/ai-sdk-adapter.d.ts +0 -19
- package/esm/src/provider/local/ai-sdk-adapter.d.ts.map +0 -1
- package/esm/src/provider/local/local-embedding-adapter.d.ts +0 -19
- package/esm/src/provider/local/local-embedding-adapter.d.ts.map +0 -1
- package/esm/src/react/components/ai/agent-card.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/animations.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/attachment-pill.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/branch-picker.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/code-block.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/drop-zone.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/empty-state.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/inference-badge.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/inline-citation.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/message-actions.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/message-edit-form.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/message-feedback.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/quick-actions.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/reasoning.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/sidebar.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/skill-badge.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/sources.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/step-indicator.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/tab-switcher.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/tool-ui.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/upgrade-cta.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/components/uploads-panel.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/api.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/chat-composer.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/chat-empty.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/chat-if.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/chat-message-list.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/chat-root.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/error-banner.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/message.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/composition/model-avatar.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/contexts/chat-context.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/contexts/composer-context.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/contexts/index.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/contexts/message-context.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/contexts/thread-list-context.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/hooks/use-threads.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/index.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/utils/export.d.ts +0 -10
- package/esm/src/react/components/ai/chat/utils/export.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat/utils/message-parts.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat-with-sidebar.d.ts.map +0 -1
- package/esm/src/react/components/ai/chat.d.ts.map +0 -1
- package/esm/src/react/components/ai/csp-nonce.d.ts.map +0 -1
- package/esm/src/react/components/ai/error-boundary.d.ts.map +0 -1
- package/esm/src/react/components/ai/icons/index.d.ts.map +0 -1
- package/esm/src/react/components/ai/markdown.d.ts.map +0 -1
- package/esm/src/react/components/ai/message.d.ts.map +0 -1
- package/esm/src/react/components/ai/model-selector.d.ts.map +0 -1
- package/esm/src/react/components/ai/theme.d.ts.map +0 -1
- /package/esm/src/agent/{ai-defaults.js → defaults.js} +0 -0
- /package/esm/src/react/components/{ai → chat}/agent-card.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/agent-card.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/animations.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/animations.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/attachment-pill.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/attachment-pill.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/branch-picker.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/branch-picker.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/code-block.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/code-block.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/drop-zone.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/drop-zone.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/empty-state.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/empty-state.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/inference-badge.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/inline-citation.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/inline-citation.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/message-actions.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/message-actions.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/message-edit-form.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/message-edit-form.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/message-feedback.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/message-feedback.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/quick-actions.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/quick-actions.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/reasoning.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/reasoning.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/sidebar.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/sidebar.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/sources.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/sources.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/step-indicator.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/step-indicator.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/tab-switcher.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/tab-switcher.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/upgrade-cta.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/upgrade-cta.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/uploads-panel.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/components/uploads-panel.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat/hooks/use-threads.js +0 -0
- /package/esm/src/react/components/{ai → chat}/chat-with-sidebar.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/chat-with-sidebar.js +0 -0
- /package/esm/src/react/components/{ai → chat}/csp-nonce.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/csp-nonce.js +0 -0
- /package/esm/src/react/components/{ai → chat}/markdown.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/markdown.js +0 -0
- /package/esm/src/react/components/{ai → chat}/message.js +0 -0
- /package/esm/src/react/components/{ai → chat}/model-selector.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/model-selector.js +0 -0
- /package/esm/src/react/components/{ai → chat}/theme.d.ts +0 -0
- /package/esm/src/react/components/{ai → chat}/theme.js +0 -0
- /package/esm/src/{ai/registry-facade.js → registry/scoped-registry-facade.js} +0 -0
- /package/src/src/agent/{ai-defaults.ts → defaults.ts} +0 -0
- /package/src/src/react/components/{ai → chat}/agent-card.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/animations.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/attachment-pill.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/branch-picker.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/code-block.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/drop-zone.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/empty-state.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/inline-citation.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/message-actions.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/message-edit-form.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/message-feedback.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/quick-actions.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/reasoning.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/sidebar.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/sources.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/step-indicator.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/tab-switcher.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/upgrade-cta.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat/components/uploads-panel.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/chat-with-sidebar.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/csp-nonce.ts +0 -0
- /package/src/src/react/components/{ai → chat}/markdown.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/model-selector.tsx +0 -0
- /package/src/src/react/components/{ai → chat}/theme.ts +0 -0
|
@@ -0,0 +1,1307 @@
|
|
|
1
|
+
import * as dntShim from "../../_dnt.shims.js";
|
|
2
|
+
const DEFAULT_ANTHROPIC_BASE_URL = "https://api.anthropic.com/v1";
|
|
3
|
+
const DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
4
|
+
const DEFAULT_GOOGLE_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
5
|
+
function joinUrl(base, path) {
|
|
6
|
+
return `${base.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
|
|
7
|
+
}
|
|
8
|
+
function getOpenAIEmbeddingUrl(baseURL) {
|
|
9
|
+
return joinUrl(baseURL ?? DEFAULT_OPENAI_BASE_URL, "embeddings");
|
|
10
|
+
}
|
|
11
|
+
function getAnthropicMessagesUrl(baseURL) {
|
|
12
|
+
return joinUrl(baseURL ?? DEFAULT_ANTHROPIC_BASE_URL, "messages");
|
|
13
|
+
}
|
|
14
|
+
function getOpenAIChatCompletionsUrl(baseURL) {
|
|
15
|
+
return joinUrl(baseURL ?? DEFAULT_OPENAI_BASE_URL, "chat/completions");
|
|
16
|
+
}
|
|
17
|
+
function getGoogleGenerateContentUrl(baseURL, modelId) {
|
|
18
|
+
return joinUrl(baseURL ?? DEFAULT_GOOGLE_BASE_URL, `models/${encodeURIComponent(modelId)}:generateContent`);
|
|
19
|
+
}
|
|
20
|
+
function getGoogleStreamGenerateContentUrl(baseURL, modelId) {
|
|
21
|
+
return joinUrl(baseURL ?? DEFAULT_GOOGLE_BASE_URL, `models/${encodeURIComponent(modelId)}:streamGenerateContent?alt=sse`);
|
|
22
|
+
}
|
|
23
|
+
function getGoogleEmbeddingUrl(baseURL, modelId) {
|
|
24
|
+
return joinUrl(baseURL ?? DEFAULT_GOOGLE_BASE_URL, `models/${encodeURIComponent(modelId)}:embedContent`);
|
|
25
|
+
}
|
|
26
|
+
function isNumberArray(value) {
|
|
27
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "number");
|
|
28
|
+
}
|
|
29
|
+
function readRecord(value) {
|
|
30
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
return Object.fromEntries(Object.entries(value));
|
|
34
|
+
}
|
|
35
|
+
function extractOpenAIEmbeddings(payload) {
|
|
36
|
+
const record = readRecord(payload);
|
|
37
|
+
const data = record?.data;
|
|
38
|
+
if (!Array.isArray(data)) {
|
|
39
|
+
throw new Error("Invalid OpenAI embedding response: data array missing");
|
|
40
|
+
}
|
|
41
|
+
const embeddings = [];
|
|
42
|
+
for (const item of data) {
|
|
43
|
+
const itemRecord = readRecord(item);
|
|
44
|
+
const embedding = itemRecord?.embedding;
|
|
45
|
+
if (!isNumberArray(embedding)) {
|
|
46
|
+
throw new Error("Invalid OpenAI embedding response: embedding vector missing");
|
|
47
|
+
}
|
|
48
|
+
embeddings.push(embedding);
|
|
49
|
+
}
|
|
50
|
+
return embeddings;
|
|
51
|
+
}
|
|
52
|
+
function extractOpenAIUsageTokens(payload) {
|
|
53
|
+
const record = readRecord(payload);
|
|
54
|
+
const usage = readRecord(record?.usage);
|
|
55
|
+
const totalTokens = usage?.total_tokens;
|
|
56
|
+
return typeof totalTokens === "number" ? totalTokens : undefined;
|
|
57
|
+
}
|
|
58
|
+
function extractGoogleEmbedding(payload) {
|
|
59
|
+
const record = readRecord(payload);
|
|
60
|
+
const embeddings = record?.embeddings;
|
|
61
|
+
if (Array.isArray(embeddings) && embeddings.length > 0) {
|
|
62
|
+
const firstEmbedding = readRecord(embeddings[0]);
|
|
63
|
+
const values = firstEmbedding?.values;
|
|
64
|
+
if (isNumberArray(values)) {
|
|
65
|
+
return values;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const embedding = readRecord(record?.embedding);
|
|
69
|
+
const values = embedding?.values;
|
|
70
|
+
if (isNumberArray(values)) {
|
|
71
|
+
return values;
|
|
72
|
+
}
|
|
73
|
+
throw new Error("Invalid Google embedding response: embedding vector missing");
|
|
74
|
+
}
|
|
75
|
+
function extractGoogleUsageTokens(payload) {
|
|
76
|
+
const record = readRecord(payload);
|
|
77
|
+
const usageMetadata = readRecord(record?.usageMetadata);
|
|
78
|
+
const promptTokenCount = usageMetadata?.promptTokenCount;
|
|
79
|
+
return typeof promptTokenCount === "number" ? promptTokenCount : undefined;
|
|
80
|
+
}
|
|
81
|
+
async function readErrorMessage(response) {
|
|
82
|
+
const text = await response.text();
|
|
83
|
+
return text.trim() || `${response.status} ${response.statusText}`.trim();
|
|
84
|
+
}
|
|
85
|
+
async function requestJson(options) {
|
|
86
|
+
const response = await options.fetchImpl(options.url, options.init);
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
const message = await readErrorMessage(response);
|
|
89
|
+
throw new Error(`${options.providerLabel} request failed: ${message}`);
|
|
90
|
+
}
|
|
91
|
+
return response.json();
|
|
92
|
+
}
|
|
93
|
+
async function requestStream(options) {
|
|
94
|
+
const response = await options.fetchImpl(options.url, options.init);
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
const message = await readErrorMessage(response);
|
|
97
|
+
throw new Error(`${options.providerLabel} request failed: ${message}`);
|
|
98
|
+
}
|
|
99
|
+
if (!response.body) {
|
|
100
|
+
throw new Error(`${options.providerLabel} request failed: stream body missing`);
|
|
101
|
+
}
|
|
102
|
+
return response.body;
|
|
103
|
+
}
|
|
104
|
+
function stringifyJsonValue(value) {
|
|
105
|
+
if (typeof value === "string") {
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
return JSON.stringify(value);
|
|
109
|
+
}
|
|
110
|
+
function readTextParts(parts) {
|
|
111
|
+
let text = "";
|
|
112
|
+
for (const part of parts) {
|
|
113
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
114
|
+
text += part.text;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return text;
|
|
118
|
+
}
|
|
119
|
+
function toOpenAICompatibleMessages(prompt) {
|
|
120
|
+
const messages = [];
|
|
121
|
+
for (const message of prompt) {
|
|
122
|
+
switch (message.role) {
|
|
123
|
+
case "system":
|
|
124
|
+
messages.push({ role: "system", content: message.content });
|
|
125
|
+
break;
|
|
126
|
+
case "user":
|
|
127
|
+
messages.push({ role: "user", content: readTextParts(message.content) });
|
|
128
|
+
break;
|
|
129
|
+
case "assistant": {
|
|
130
|
+
let text = "";
|
|
131
|
+
const toolCalls = [];
|
|
132
|
+
for (const part of message.content) {
|
|
133
|
+
if (part.type === "text") {
|
|
134
|
+
text += part.text;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
toolCalls.push({
|
|
138
|
+
id: part.toolCallId,
|
|
139
|
+
type: "function",
|
|
140
|
+
function: {
|
|
141
|
+
name: part.toolName,
|
|
142
|
+
arguments: stringifyJsonValue(part.input),
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
messages.push({
|
|
147
|
+
role: "assistant",
|
|
148
|
+
content: text.length > 0 ? text : null,
|
|
149
|
+
...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),
|
|
150
|
+
});
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case "tool":
|
|
154
|
+
for (const part of message.content) {
|
|
155
|
+
messages.push({
|
|
156
|
+
role: "tool",
|
|
157
|
+
tool_call_id: part.toolCallId,
|
|
158
|
+
content: stringifyJsonValue(part.output.value),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return messages;
|
|
165
|
+
}
|
|
166
|
+
function toOpenAICompatibleTools(tools) {
|
|
167
|
+
if (!tools) {
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
const functions = tools.flatMap((tool) => tool.type === "function"
|
|
171
|
+
? [{
|
|
172
|
+
type: "function",
|
|
173
|
+
function: {
|
|
174
|
+
name: tool.name,
|
|
175
|
+
parameters: tool.inputSchema,
|
|
176
|
+
...(typeof tool.description === "string" ? { description: tool.description } : {}),
|
|
177
|
+
},
|
|
178
|
+
}]
|
|
179
|
+
: []);
|
|
180
|
+
return functions.length > 0 ? functions : undefined;
|
|
181
|
+
}
|
|
182
|
+
function readProviderOptions(providerOptions, ...providerNames) {
|
|
183
|
+
if (!providerOptions) {
|
|
184
|
+
return {};
|
|
185
|
+
}
|
|
186
|
+
const merged = {};
|
|
187
|
+
for (const key of providerNames) {
|
|
188
|
+
const value = providerOptions[key];
|
|
189
|
+
const record = readRecord(value);
|
|
190
|
+
if (record) {
|
|
191
|
+
Object.assign(merged, record);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return merged;
|
|
195
|
+
}
|
|
196
|
+
function createRequestHeaders(options) {
|
|
197
|
+
const headers = new dntShim.Headers(options.extraHeaders);
|
|
198
|
+
headers.set("content-type", "application/json");
|
|
199
|
+
headers.set(options.apiKeyHeaderName, options.apiKey);
|
|
200
|
+
return headers;
|
|
201
|
+
}
|
|
202
|
+
function normalizeAnthropicFinishReason(raw) {
|
|
203
|
+
if (typeof raw !== "string") {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
switch (raw) {
|
|
207
|
+
case "tool_use":
|
|
208
|
+
return { unified: "tool-calls", raw };
|
|
209
|
+
case "end_turn":
|
|
210
|
+
case "stop_sequence":
|
|
211
|
+
return { unified: "stop", raw };
|
|
212
|
+
case "max_tokens":
|
|
213
|
+
return { unified: "length", raw };
|
|
214
|
+
default:
|
|
215
|
+
return raw;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function extractAnthropicUsage(payload) {
|
|
219
|
+
const record = readRecord(payload);
|
|
220
|
+
const usage = readRecord(record?.usage);
|
|
221
|
+
if (!usage) {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
const inputTokens = usage.input_tokens;
|
|
225
|
+
const outputTokens = usage.output_tokens;
|
|
226
|
+
return {
|
|
227
|
+
inputTokens: typeof inputTokens === "number" ? inputTokens : undefined,
|
|
228
|
+
outputTokens: typeof outputTokens === "number" ? outputTokens : undefined,
|
|
229
|
+
totalTokens: typeof inputTokens === "number" || typeof outputTokens === "number"
|
|
230
|
+
? (typeof inputTokens === "number" ? inputTokens : 0) +
|
|
231
|
+
(typeof outputTokens === "number" ? outputTokens : 0)
|
|
232
|
+
: undefined,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function mergeUsage(current, next) {
|
|
236
|
+
if (!current) {
|
|
237
|
+
return next;
|
|
238
|
+
}
|
|
239
|
+
if (!next) {
|
|
240
|
+
return current;
|
|
241
|
+
}
|
|
242
|
+
const inputTokens = next.inputTokens ?? current.inputTokens;
|
|
243
|
+
const outputTokens = next.outputTokens ?? current.outputTokens;
|
|
244
|
+
return {
|
|
245
|
+
inputTokens,
|
|
246
|
+
outputTokens,
|
|
247
|
+
totalTokens: (inputTokens ?? 0) + (outputTokens ?? 0),
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function normalizeAnthropicToolChoice(toolChoice) {
|
|
251
|
+
if (typeof toolChoice === "string") {
|
|
252
|
+
return { type: toolChoice };
|
|
253
|
+
}
|
|
254
|
+
return toolChoice;
|
|
255
|
+
}
|
|
256
|
+
function toSnakeCaseRecord(record) {
|
|
257
|
+
return Object.fromEntries(Object.entries(record).map(([key, value]) => [
|
|
258
|
+
key.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`),
|
|
259
|
+
value,
|
|
260
|
+
]));
|
|
261
|
+
}
|
|
262
|
+
function toAnthropicMessages(prompt) {
|
|
263
|
+
const systemParts = [];
|
|
264
|
+
const messages = [];
|
|
265
|
+
for (const message of prompt) {
|
|
266
|
+
switch (message.role) {
|
|
267
|
+
case "system":
|
|
268
|
+
if (message.content.length > 0) {
|
|
269
|
+
systemParts.push(message.content);
|
|
270
|
+
}
|
|
271
|
+
break;
|
|
272
|
+
case "user":
|
|
273
|
+
messages.push({
|
|
274
|
+
role: "user",
|
|
275
|
+
content: [{ type: "text", text: readTextParts(message.content) }],
|
|
276
|
+
});
|
|
277
|
+
break;
|
|
278
|
+
case "assistant":
|
|
279
|
+
messages.push({
|
|
280
|
+
role: "assistant",
|
|
281
|
+
content: message.content.map((part) => part.type === "text" ? { type: "text", text: part.text } : {
|
|
282
|
+
type: "tool_use",
|
|
283
|
+
id: part.toolCallId,
|
|
284
|
+
name: part.toolName,
|
|
285
|
+
input: part.input,
|
|
286
|
+
}),
|
|
287
|
+
});
|
|
288
|
+
break;
|
|
289
|
+
case "tool":
|
|
290
|
+
messages.push({
|
|
291
|
+
role: "user",
|
|
292
|
+
content: message.content.map((part) => ({
|
|
293
|
+
type: "tool_result",
|
|
294
|
+
tool_use_id: part.toolCallId,
|
|
295
|
+
content: stringifyJsonValue(part.output.value),
|
|
296
|
+
})),
|
|
297
|
+
});
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
...(systemParts.length > 0 ? { system: systemParts.join("\n\n") } : {}),
|
|
303
|
+
messages,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function toAnthropicTools(tools) {
|
|
307
|
+
if (!tools) {
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
const normalized = [];
|
|
311
|
+
for (const tool of tools) {
|
|
312
|
+
if (tool.type === "function") {
|
|
313
|
+
normalized.push({
|
|
314
|
+
name: tool.name,
|
|
315
|
+
...(typeof tool.description === "string" ? { description: tool.description } : {}),
|
|
316
|
+
input_schema: tool.inputSchema,
|
|
317
|
+
});
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
if (!tool.id.startsWith("anthropic.")) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
const providerType = tool.id.slice("anthropic.".length);
|
|
324
|
+
if (providerType.length === 0) {
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
normalized.push({
|
|
328
|
+
type: providerType,
|
|
329
|
+
name: tool.name,
|
|
330
|
+
...toSnakeCaseRecord(tool.args),
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
334
|
+
}
|
|
335
|
+
function createAnthropicRequestHeaders(options) {
|
|
336
|
+
const headers = new dntShim.Headers(options.extraHeaders);
|
|
337
|
+
headers.set("content-type", "application/json");
|
|
338
|
+
headers.set("anthropic-version", headers.get("anthropic-version") ?? "2023-06-01");
|
|
339
|
+
if (options.authToken) {
|
|
340
|
+
headers.set("authorization", `Bearer ${options.authToken}`);
|
|
341
|
+
}
|
|
342
|
+
else if (options.apiKey) {
|
|
343
|
+
headers.set("x-api-key", options.apiKey);
|
|
344
|
+
}
|
|
345
|
+
return headers;
|
|
346
|
+
}
|
|
347
|
+
function buildAnthropicMessagesRequest(modelId, providerName, options, stream) {
|
|
348
|
+
const { system, messages } = toAnthropicMessages(options.prompt);
|
|
349
|
+
const body = {
|
|
350
|
+
model: modelId,
|
|
351
|
+
messages,
|
|
352
|
+
max_tokens: options.maxOutputTokens ?? 1024,
|
|
353
|
+
...(stream ? { stream: true } : {}),
|
|
354
|
+
...(system ? { system } : {}),
|
|
355
|
+
...(options.temperature !== undefined ? { temperature: options.temperature } : {}),
|
|
356
|
+
...(options.topP !== undefined ? { top_p: options.topP } : {}),
|
|
357
|
+
...(options.stopSequences && options.stopSequences.length > 0
|
|
358
|
+
? { stop_sequences: options.stopSequences }
|
|
359
|
+
: {}),
|
|
360
|
+
...(toAnthropicTools(options.tools) ? { tools: toAnthropicTools(options.tools) } : {}),
|
|
361
|
+
...(options.toolChoice !== undefined
|
|
362
|
+
? { tool_choice: normalizeAnthropicToolChoice(options.toolChoice) }
|
|
363
|
+
: {}),
|
|
364
|
+
};
|
|
365
|
+
Object.assign(body, readProviderOptions(options.providerOptions, "anthropic", providerName));
|
|
366
|
+
return body;
|
|
367
|
+
}
|
|
368
|
+
function buildAnthropicGenerateResult(payload) {
|
|
369
|
+
const record = readRecord(payload);
|
|
370
|
+
const content = Array.isArray(record?.content) ? record.content : [];
|
|
371
|
+
const normalized = [];
|
|
372
|
+
for (const blockValue of content) {
|
|
373
|
+
const block = readRecord(blockValue);
|
|
374
|
+
const blockType = typeof block?.type === "string" ? block.type : undefined;
|
|
375
|
+
if (blockType === "text" && typeof block?.text === "string" && block.text.length > 0) {
|
|
376
|
+
normalized.push({ type: "text", text: block.text });
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if ((blockType === "tool_use" || blockType === "server_tool_use") &&
|
|
380
|
+
typeof block?.id === "string" &&
|
|
381
|
+
typeof block?.name === "string") {
|
|
382
|
+
normalized.push({
|
|
383
|
+
type: "tool-call",
|
|
384
|
+
toolCallId: block.id,
|
|
385
|
+
toolName: block.name,
|
|
386
|
+
input: stringifyJsonValue(block.input ?? {}),
|
|
387
|
+
});
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
if (blockType === "web_search_tool_result" &&
|
|
391
|
+
typeof block?.tool_use_id === "string" &&
|
|
392
|
+
Array.isArray(block?.content)) {
|
|
393
|
+
normalized.push({
|
|
394
|
+
type: "tool-result",
|
|
395
|
+
toolCallId: block.tool_use_id,
|
|
396
|
+
toolName: "web_search",
|
|
397
|
+
result: block.content,
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return {
|
|
402
|
+
content: normalized,
|
|
403
|
+
finishReason: normalizeAnthropicFinishReason(record?.stop_reason),
|
|
404
|
+
usage: extractAnthropicUsage(payload),
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function parseAnthropicSseChunk(chunk) {
|
|
408
|
+
const blocks = chunk.split("\n\n");
|
|
409
|
+
const remainder = blocks.pop() ?? "";
|
|
410
|
+
const events = blocks.flatMap((block) => {
|
|
411
|
+
const dataLines = block.split("\n")
|
|
412
|
+
.filter((line) => line.startsWith("data:"))
|
|
413
|
+
.map((line) => line.slice(5).trimStart());
|
|
414
|
+
if (!dataLines.length) {
|
|
415
|
+
return [];
|
|
416
|
+
}
|
|
417
|
+
const payload = dataLines.join("\n").trim();
|
|
418
|
+
if (payload === "[DONE]") {
|
|
419
|
+
return ["[DONE]"];
|
|
420
|
+
}
|
|
421
|
+
try {
|
|
422
|
+
return [JSON.parse(payload)];
|
|
423
|
+
}
|
|
424
|
+
catch {
|
|
425
|
+
return [];
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
return { events, remainder };
|
|
429
|
+
}
|
|
430
|
+
async function* streamAnthropicCompatibleParts(stream) {
|
|
431
|
+
const decoder = new TextDecoder();
|
|
432
|
+
let buffer = "";
|
|
433
|
+
const toolCalls = new Map();
|
|
434
|
+
let finishReason = null;
|
|
435
|
+
let usage;
|
|
436
|
+
for await (const chunk of stream) {
|
|
437
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
438
|
+
const parsed = parseAnthropicSseChunk(buffer);
|
|
439
|
+
buffer = parsed.remainder;
|
|
440
|
+
for (const event of parsed.events) {
|
|
441
|
+
if (event === "[DONE]") {
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
const record = readRecord(event);
|
|
445
|
+
const eventType = typeof record?.type === "string" ? record.type : undefined;
|
|
446
|
+
usage = mergeUsage(usage, extractAnthropicUsage(record));
|
|
447
|
+
if (eventType === "message_start") {
|
|
448
|
+
usage = mergeUsage(usage, extractAnthropicUsage(record?.message));
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
if (eventType === "content_block_start") {
|
|
452
|
+
const index = typeof record?.index === "number" ? record.index : 0;
|
|
453
|
+
const contentBlock = readRecord(record?.content_block);
|
|
454
|
+
const blockType = typeof contentBlock?.type === "string" ? contentBlock.type : undefined;
|
|
455
|
+
if (blockType === "text" && typeof contentBlock?.text === "string" &&
|
|
456
|
+
contentBlock.text.length > 0) {
|
|
457
|
+
yield { type: "text-delta", delta: contentBlock.text };
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if ((blockType === "tool_use" || blockType === "server_tool_use") &&
|
|
461
|
+
typeof contentBlock?.id === "string" &&
|
|
462
|
+
typeof contentBlock?.name === "string") {
|
|
463
|
+
const providerExecuted = blockType === "server_tool_use" ? true : undefined;
|
|
464
|
+
const current = {
|
|
465
|
+
id: contentBlock.id,
|
|
466
|
+
name: contentBlock.name,
|
|
467
|
+
input: "",
|
|
468
|
+
...(providerExecuted ? { providerExecuted } : {}),
|
|
469
|
+
};
|
|
470
|
+
toolCalls.set(index, current);
|
|
471
|
+
yield {
|
|
472
|
+
type: "tool-input-start",
|
|
473
|
+
id: current.id,
|
|
474
|
+
toolName: current.name,
|
|
475
|
+
...(providerExecuted ? { providerExecuted } : {}),
|
|
476
|
+
};
|
|
477
|
+
const initialInput = contentBlock.input;
|
|
478
|
+
if (initialInput !== undefined) {
|
|
479
|
+
const serializedInput = stringifyJsonValue(initialInput);
|
|
480
|
+
current.input += serializedInput;
|
|
481
|
+
yield {
|
|
482
|
+
type: "tool-input-delta",
|
|
483
|
+
id: current.id,
|
|
484
|
+
delta: serializedInput,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
if (blockType === "web_search_tool_result" &&
|
|
490
|
+
typeof contentBlock?.tool_use_id === "string" &&
|
|
491
|
+
Array.isArray(contentBlock?.content)) {
|
|
492
|
+
yield {
|
|
493
|
+
type: "tool-result",
|
|
494
|
+
toolCallId: contentBlock.tool_use_id,
|
|
495
|
+
toolName: "web_search",
|
|
496
|
+
result: contentBlock.content,
|
|
497
|
+
providerExecuted: true,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
if (eventType === "content_block_delta") {
|
|
503
|
+
const index = typeof record?.index === "number" ? record.index : 0;
|
|
504
|
+
const delta = readRecord(record?.delta);
|
|
505
|
+
const deltaType = typeof delta?.type === "string" ? delta.type : undefined;
|
|
506
|
+
if (deltaType === "text_delta" && typeof delta?.text === "string" && delta.text.length > 0) {
|
|
507
|
+
yield { type: "text-delta", delta: delta.text };
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (deltaType === "input_json_delta" && typeof delta?.partial_json === "string") {
|
|
511
|
+
const current = toolCalls.get(index);
|
|
512
|
+
if (!current) {
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
current.input += delta.partial_json;
|
|
516
|
+
yield {
|
|
517
|
+
type: "tool-input-delta",
|
|
518
|
+
id: current.id,
|
|
519
|
+
delta: delta.partial_json,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
if (eventType === "content_block_stop") {
|
|
525
|
+
const index = typeof record?.index === "number" ? record.index : 0;
|
|
526
|
+
const current = toolCalls.get(index);
|
|
527
|
+
if (!current) {
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
yield {
|
|
531
|
+
type: "tool-call",
|
|
532
|
+
toolCallId: current.id,
|
|
533
|
+
toolName: current.name,
|
|
534
|
+
input: current.input.length > 0 ? current.input : "{}",
|
|
535
|
+
...(current.providerExecuted ? { providerExecuted: true } : {}),
|
|
536
|
+
};
|
|
537
|
+
toolCalls.delete(index);
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
if (eventType === "message_delta") {
|
|
541
|
+
const delta = readRecord(record?.delta);
|
|
542
|
+
const normalizedFinishReason = normalizeAnthropicFinishReason(delta?.stop_reason);
|
|
543
|
+
if (normalizedFinishReason) {
|
|
544
|
+
finishReason = normalizedFinishReason;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (buffer.trim().length > 0) {
|
|
550
|
+
const parsed = parseAnthropicSseChunk(`${buffer}\n\n`);
|
|
551
|
+
for (const event of parsed.events) {
|
|
552
|
+
if (event === "[DONE]") {
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
const record = readRecord(event);
|
|
556
|
+
usage = mergeUsage(usage, extractAnthropicUsage(record));
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
yield {
|
|
560
|
+
type: "finish",
|
|
561
|
+
finishReason,
|
|
562
|
+
...(usage ? { usage } : {}),
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
function normalizeOpenAIFinishReason(raw) {
|
|
566
|
+
if (typeof raw !== "string") {
|
|
567
|
+
return null;
|
|
568
|
+
}
|
|
569
|
+
if (raw === "tool_calls") {
|
|
570
|
+
return { unified: "tool-calls", raw };
|
|
571
|
+
}
|
|
572
|
+
if (raw === "content_filter") {
|
|
573
|
+
return { unified: "content-filter", raw };
|
|
574
|
+
}
|
|
575
|
+
return raw;
|
|
576
|
+
}
|
|
577
|
+
function extractOpenAIUsage(payload) {
|
|
578
|
+
const record = readRecord(payload);
|
|
579
|
+
const usage = readRecord(record?.usage);
|
|
580
|
+
if (!usage) {
|
|
581
|
+
return undefined;
|
|
582
|
+
}
|
|
583
|
+
const inputTokens = usage.prompt_tokens;
|
|
584
|
+
const outputTokens = usage.completion_tokens;
|
|
585
|
+
const totalTokens = usage.total_tokens;
|
|
586
|
+
return {
|
|
587
|
+
inputTokens: typeof inputTokens === "number" ? inputTokens : undefined,
|
|
588
|
+
outputTokens: typeof outputTokens === "number" ? outputTokens : undefined,
|
|
589
|
+
totalTokens: typeof totalTokens === "number" ? totalTokens : undefined,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
function extractOpenAIContentText(content) {
|
|
593
|
+
if (typeof content === "string") {
|
|
594
|
+
return content;
|
|
595
|
+
}
|
|
596
|
+
if (!Array.isArray(content)) {
|
|
597
|
+
return "";
|
|
598
|
+
}
|
|
599
|
+
let text = "";
|
|
600
|
+
for (const part of content) {
|
|
601
|
+
const record = readRecord(part);
|
|
602
|
+
const type = record?.type;
|
|
603
|
+
if (type === "text" && typeof record?.text === "string") {
|
|
604
|
+
text += record.text;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
return text;
|
|
608
|
+
}
|
|
609
|
+
function extractOpenAIToolCalls(message) {
|
|
610
|
+
const toolCalls = message.tool_calls;
|
|
611
|
+
if (!Array.isArray(toolCalls)) {
|
|
612
|
+
return [];
|
|
613
|
+
}
|
|
614
|
+
const normalized = [];
|
|
615
|
+
for (const entry of toolCalls) {
|
|
616
|
+
const record = readRecord(entry);
|
|
617
|
+
const id = typeof record?.id === "string" ? record.id : undefined;
|
|
618
|
+
const fn = readRecord(record?.function);
|
|
619
|
+
const name = typeof fn?.name === "string" ? fn.name : undefined;
|
|
620
|
+
const argumentsText = typeof fn?.arguments === "string" ? fn.arguments : undefined;
|
|
621
|
+
if (!id || !name || argumentsText === undefined) {
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
normalized.push({
|
|
625
|
+
toolCallId: id,
|
|
626
|
+
toolName: name,
|
|
627
|
+
input: argumentsText,
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
return normalized;
|
|
631
|
+
}
|
|
632
|
+
function buildOpenAIChatRequest(modelId, providerName, options, stream) {
|
|
633
|
+
const body = {
|
|
634
|
+
model: modelId,
|
|
635
|
+
messages: toOpenAICompatibleMessages(options.prompt),
|
|
636
|
+
...(stream ? { stream: true, stream_options: { include_usage: true } } : {}),
|
|
637
|
+
...(options.maxOutputTokens !== undefined ? { max_tokens: options.maxOutputTokens } : {}),
|
|
638
|
+
...(options.temperature !== undefined ? { temperature: options.temperature } : {}),
|
|
639
|
+
...(options.topP !== undefined ? { top_p: options.topP } : {}),
|
|
640
|
+
...(options.stopSequences && options.stopSequences.length > 0
|
|
641
|
+
? { stop: options.stopSequences }
|
|
642
|
+
: {}),
|
|
643
|
+
...(toOpenAICompatibleTools(options.tools)
|
|
644
|
+
? { tools: toOpenAICompatibleTools(options.tools) }
|
|
645
|
+
: {}),
|
|
646
|
+
...(options.toolChoice !== undefined ? { tool_choice: options.toolChoice } : {}),
|
|
647
|
+
...(options.seed !== undefined ? { seed: options.seed } : {}),
|
|
648
|
+
...(options.presencePenalty !== undefined ? { presence_penalty: options.presencePenalty } : {}),
|
|
649
|
+
...(options.frequencyPenalty !== undefined
|
|
650
|
+
? { frequency_penalty: options.frequencyPenalty }
|
|
651
|
+
: {}),
|
|
652
|
+
};
|
|
653
|
+
Object.assign(body, readProviderOptions(options.providerOptions, "openai", providerName));
|
|
654
|
+
return body;
|
|
655
|
+
}
|
|
656
|
+
function normalizeGoogleFinishReason(raw) {
|
|
657
|
+
if (typeof raw !== "string") {
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
switch (raw) {
|
|
661
|
+
case "STOP":
|
|
662
|
+
return { unified: "stop", raw };
|
|
663
|
+
case "MAX_TOKENS":
|
|
664
|
+
return { unified: "length", raw };
|
|
665
|
+
case "SAFETY":
|
|
666
|
+
case "RECITATION":
|
|
667
|
+
return { unified: "content-filter", raw };
|
|
668
|
+
default:
|
|
669
|
+
return raw.toLowerCase();
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
function extractGoogleUsage(payload) {
|
|
673
|
+
const record = readRecord(payload);
|
|
674
|
+
const usage = readRecord(record?.usageMetadata);
|
|
675
|
+
if (!usage) {
|
|
676
|
+
return undefined;
|
|
677
|
+
}
|
|
678
|
+
const inputTokens = usage.promptTokenCount;
|
|
679
|
+
const outputTokens = usage.candidatesTokenCount;
|
|
680
|
+
const totalTokens = usage.totalTokenCount;
|
|
681
|
+
return {
|
|
682
|
+
inputTokens: typeof inputTokens === "number" ? inputTokens : undefined,
|
|
683
|
+
outputTokens: typeof outputTokens === "number" ? outputTokens : undefined,
|
|
684
|
+
totalTokens: typeof totalTokens === "number" ? totalTokens : undefined,
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
function toGoogleContents(prompt) {
|
|
688
|
+
const systemParts = [];
|
|
689
|
+
const contents = [];
|
|
690
|
+
for (const message of prompt) {
|
|
691
|
+
switch (message.role) {
|
|
692
|
+
case "system":
|
|
693
|
+
if (message.content.length > 0) {
|
|
694
|
+
systemParts.push(message.content);
|
|
695
|
+
}
|
|
696
|
+
break;
|
|
697
|
+
case "user":
|
|
698
|
+
contents.push({
|
|
699
|
+
role: "user",
|
|
700
|
+
parts: [{ text: readTextParts(message.content) }],
|
|
701
|
+
});
|
|
702
|
+
break;
|
|
703
|
+
case "assistant":
|
|
704
|
+
contents.push({
|
|
705
|
+
role: "model",
|
|
706
|
+
parts: message.content.map((part) => part.type === "text" ? { text: part.text } : {
|
|
707
|
+
functionCall: {
|
|
708
|
+
id: part.toolCallId,
|
|
709
|
+
name: part.toolName,
|
|
710
|
+
args: part.input,
|
|
711
|
+
},
|
|
712
|
+
}),
|
|
713
|
+
});
|
|
714
|
+
break;
|
|
715
|
+
case "tool":
|
|
716
|
+
contents.push({
|
|
717
|
+
role: "user",
|
|
718
|
+
parts: message.content.map((part) => ({
|
|
719
|
+
functionResponse: {
|
|
720
|
+
id: part.toolCallId,
|
|
721
|
+
name: part.toolName,
|
|
722
|
+
response: {
|
|
723
|
+
result: part.output.value,
|
|
724
|
+
},
|
|
725
|
+
},
|
|
726
|
+
})),
|
|
727
|
+
});
|
|
728
|
+
break;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
return {
|
|
732
|
+
...(systemParts.length > 0
|
|
733
|
+
? { systemInstruction: { parts: systemParts.map((text) => ({ text })) } }
|
|
734
|
+
: {}),
|
|
735
|
+
contents,
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
function toGoogleTools(tools) {
|
|
739
|
+
if (!tools) {
|
|
740
|
+
return undefined;
|
|
741
|
+
}
|
|
742
|
+
const functionDeclarations = tools.flatMap((tool) => tool.type === "function"
|
|
743
|
+
? [{
|
|
744
|
+
name: tool.name,
|
|
745
|
+
...(typeof tool.description === "string" ? { description: tool.description } : {}),
|
|
746
|
+
parameters: tool.inputSchema,
|
|
747
|
+
}]
|
|
748
|
+
: []);
|
|
749
|
+
return functionDeclarations.length > 0 ? [{ functionDeclarations }] : undefined;
|
|
750
|
+
}
|
|
751
|
+
function normalizeGoogleToolChoice(toolChoice) {
|
|
752
|
+
if (toolChoice === undefined) {
|
|
753
|
+
return undefined;
|
|
754
|
+
}
|
|
755
|
+
if (typeof toolChoice === "string") {
|
|
756
|
+
switch (toolChoice) {
|
|
757
|
+
case "none":
|
|
758
|
+
return { functionCallingConfig: { mode: "NONE" } };
|
|
759
|
+
case "any":
|
|
760
|
+
case "required":
|
|
761
|
+
return { functionCallingConfig: { mode: "ANY" } };
|
|
762
|
+
default:
|
|
763
|
+
return { functionCallingConfig: { mode: "AUTO" } };
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
const record = readRecord(toolChoice);
|
|
767
|
+
if (record?.type === "tool" && typeof record.name === "string") {
|
|
768
|
+
return {
|
|
769
|
+
functionCallingConfig: {
|
|
770
|
+
mode: "ANY",
|
|
771
|
+
allowedFunctionNames: [record.name],
|
|
772
|
+
},
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
return undefined;
|
|
776
|
+
}
|
|
777
|
+
function buildGoogleGenerationConfig(options) {
|
|
778
|
+
const config = {
|
|
779
|
+
...(options.maxOutputTokens !== undefined ? { maxOutputTokens: options.maxOutputTokens } : {}),
|
|
780
|
+
...(options.temperature !== undefined ? { temperature: options.temperature } : {}),
|
|
781
|
+
...(options.topP !== undefined ? { topP: options.topP } : {}),
|
|
782
|
+
...(options.topK !== undefined ? { topK: options.topK } : {}),
|
|
783
|
+
...(options.stopSequences && options.stopSequences.length > 0
|
|
784
|
+
? { stopSequences: options.stopSequences }
|
|
785
|
+
: {}),
|
|
786
|
+
...(options.seed !== undefined ? { seed: options.seed } : {}),
|
|
787
|
+
};
|
|
788
|
+
return Object.keys(config).length > 0 ? config : undefined;
|
|
789
|
+
}
|
|
790
|
+
function buildGoogleGenerateContentRequest(providerName, options) {
|
|
791
|
+
const { systemInstruction, contents } = toGoogleContents(options.prompt);
|
|
792
|
+
const body = {
|
|
793
|
+
contents,
|
|
794
|
+
...(systemInstruction ? { systemInstruction } : {}),
|
|
795
|
+
...(toGoogleTools(options.tools) ? { tools: toGoogleTools(options.tools) } : {}),
|
|
796
|
+
...(normalizeGoogleToolChoice(options.toolChoice)
|
|
797
|
+
? { toolConfig: normalizeGoogleToolChoice(options.toolChoice) }
|
|
798
|
+
: {}),
|
|
799
|
+
...(buildGoogleGenerationConfig(options)
|
|
800
|
+
? { generationConfig: buildGoogleGenerationConfig(options) }
|
|
801
|
+
: {}),
|
|
802
|
+
};
|
|
803
|
+
Object.assign(body, readProviderOptions(options.providerOptions, "google", providerName));
|
|
804
|
+
return body;
|
|
805
|
+
}
|
|
806
|
+
function extractFirstGoogleCandidate(payload) {
|
|
807
|
+
const record = readRecord(payload);
|
|
808
|
+
const candidates = record?.candidates;
|
|
809
|
+
if (!Array.isArray(candidates) || candidates.length === 0) {
|
|
810
|
+
return undefined;
|
|
811
|
+
}
|
|
812
|
+
return readRecord(candidates[0]);
|
|
813
|
+
}
|
|
814
|
+
function extractGoogleCandidateParts(payload) {
|
|
815
|
+
const candidate = extractFirstGoogleCandidate(payload);
|
|
816
|
+
const content = readRecord(candidate?.content);
|
|
817
|
+
const parts = content?.parts;
|
|
818
|
+
if (!Array.isArray(parts)) {
|
|
819
|
+
return [];
|
|
820
|
+
}
|
|
821
|
+
return parts.flatMap((part) => {
|
|
822
|
+
const record = readRecord(part);
|
|
823
|
+
return record ? [record] : [];
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
function buildGoogleGenerateResult(payload) {
|
|
827
|
+
const parts = extractGoogleCandidateParts(payload);
|
|
828
|
+
const content = [];
|
|
829
|
+
for (const [index, part] of parts.entries()) {
|
|
830
|
+
if (typeof part.text === "string" && part.text.length > 0) {
|
|
831
|
+
content.push({ type: "text", text: part.text });
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
const functionCall = readRecord(part.functionCall);
|
|
835
|
+
if (typeof functionCall?.name === "string") {
|
|
836
|
+
content.push({
|
|
837
|
+
type: "tool-call",
|
|
838
|
+
toolCallId: typeof functionCall.id === "string" ? functionCall.id : `tool-${index}`,
|
|
839
|
+
toolName: functionCall.name,
|
|
840
|
+
input: stringifyJsonValue(functionCall.args ?? {}),
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
return {
|
|
845
|
+
content,
|
|
846
|
+
finishReason: normalizeGoogleFinishReason(extractFirstGoogleCandidate(payload)?.finishReason),
|
|
847
|
+
usage: extractGoogleUsage(payload),
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
async function* streamGoogleCompatibleParts(stream) {
|
|
851
|
+
const decoder = new TextDecoder();
|
|
852
|
+
let buffer = "";
|
|
853
|
+
const seenToolCalls = new Set();
|
|
854
|
+
let finishReason = null;
|
|
855
|
+
let usage;
|
|
856
|
+
for await (const chunk of stream) {
|
|
857
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
858
|
+
const parsed = parseOpenAISseChunk(buffer);
|
|
859
|
+
buffer = parsed.remainder;
|
|
860
|
+
for (const event of parsed.events) {
|
|
861
|
+
if (event === "[DONE]") {
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
usage = extractGoogleUsage(event) ?? usage;
|
|
865
|
+
const candidate = extractFirstGoogleCandidate(event);
|
|
866
|
+
const normalizedFinishReason = normalizeGoogleFinishReason(candidate?.finishReason);
|
|
867
|
+
if (normalizedFinishReason) {
|
|
868
|
+
finishReason = normalizedFinishReason;
|
|
869
|
+
}
|
|
870
|
+
for (const [index, part] of extractGoogleCandidateParts(event).entries()) {
|
|
871
|
+
if (typeof part.text === "string" && part.text.length > 0) {
|
|
872
|
+
yield { type: "text-delta", delta: part.text };
|
|
873
|
+
continue;
|
|
874
|
+
}
|
|
875
|
+
const functionCall = readRecord(part.functionCall);
|
|
876
|
+
if (typeof functionCall?.name !== "string") {
|
|
877
|
+
continue;
|
|
878
|
+
}
|
|
879
|
+
const toolCallId = typeof functionCall.id === "string" ? functionCall.id : `tool-${index}`;
|
|
880
|
+
if (seenToolCalls.has(toolCallId)) {
|
|
881
|
+
continue;
|
|
882
|
+
}
|
|
883
|
+
const serializedInput = stringifyJsonValue(functionCall.args ?? {});
|
|
884
|
+
seenToolCalls.add(toolCallId);
|
|
885
|
+
yield {
|
|
886
|
+
type: "tool-input-start",
|
|
887
|
+
id: toolCallId,
|
|
888
|
+
toolName: functionCall.name,
|
|
889
|
+
};
|
|
890
|
+
yield {
|
|
891
|
+
type: "tool-input-delta",
|
|
892
|
+
id: toolCallId,
|
|
893
|
+
delta: serializedInput,
|
|
894
|
+
};
|
|
895
|
+
yield {
|
|
896
|
+
type: "tool-call",
|
|
897
|
+
toolCallId,
|
|
898
|
+
toolName: functionCall.name,
|
|
899
|
+
input: serializedInput,
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
if (buffer.trim().length > 0) {
|
|
905
|
+
const parsed = parseOpenAISseChunk(`${buffer}\n\n`);
|
|
906
|
+
for (const event of parsed.events) {
|
|
907
|
+
if (event === "[DONE]") {
|
|
908
|
+
continue;
|
|
909
|
+
}
|
|
910
|
+
usage = extractGoogleUsage(event) ?? usage;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
yield {
|
|
914
|
+
type: "finish",
|
|
915
|
+
finishReason,
|
|
916
|
+
...(usage ? { usage } : {}),
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
function extractFirstChoice(payload) {
|
|
920
|
+
const record = readRecord(payload);
|
|
921
|
+
const choices = record?.choices;
|
|
922
|
+
if (!Array.isArray(choices) || choices.length === 0) {
|
|
923
|
+
return undefined;
|
|
924
|
+
}
|
|
925
|
+
const first = readRecord(choices[0]);
|
|
926
|
+
if (!first) {
|
|
927
|
+
return undefined;
|
|
928
|
+
}
|
|
929
|
+
return first;
|
|
930
|
+
}
|
|
931
|
+
function buildOpenAIGenerateResult(payload) {
|
|
932
|
+
const choice = extractFirstChoice(payload);
|
|
933
|
+
const message = readRecord(choice?.message);
|
|
934
|
+
const text = extractOpenAIContentText(message?.content);
|
|
935
|
+
const toolCalls = message ? extractOpenAIToolCalls(message) : [];
|
|
936
|
+
return {
|
|
937
|
+
content: [
|
|
938
|
+
...(text.length > 0 ? [{ type: "text", text }] : []),
|
|
939
|
+
...toolCalls.map((toolCall) => ({
|
|
940
|
+
type: "tool-call",
|
|
941
|
+
toolCallId: toolCall.toolCallId,
|
|
942
|
+
toolName: toolCall.toolName,
|
|
943
|
+
input: toolCall.input,
|
|
944
|
+
})),
|
|
945
|
+
],
|
|
946
|
+
finishReason: normalizeOpenAIFinishReason(choice?.finish_reason),
|
|
947
|
+
usage: extractOpenAIUsage(payload),
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
function parseOpenAISseChunk(chunk) {
|
|
951
|
+
const blocks = chunk.split("\n\n");
|
|
952
|
+
const remainder = blocks.pop() ?? "";
|
|
953
|
+
const events = blocks.flatMap((block) => {
|
|
954
|
+
const dataLines = block.split("\n")
|
|
955
|
+
.filter((line) => line.startsWith("data:"))
|
|
956
|
+
.map((line) => line.slice(5).trimStart());
|
|
957
|
+
if (!dataLines.length) {
|
|
958
|
+
return [];
|
|
959
|
+
}
|
|
960
|
+
const payload = dataLines.join("\n").trim();
|
|
961
|
+
if (payload === "[DONE]") {
|
|
962
|
+
return ["[DONE]"];
|
|
963
|
+
}
|
|
964
|
+
try {
|
|
965
|
+
return [JSON.parse(payload)];
|
|
966
|
+
}
|
|
967
|
+
catch {
|
|
968
|
+
return [];
|
|
969
|
+
}
|
|
970
|
+
});
|
|
971
|
+
return { events, remainder };
|
|
972
|
+
}
|
|
973
|
+
async function* streamOpenAICompatibleParts(stream) {
|
|
974
|
+
const decoder = new TextDecoder();
|
|
975
|
+
let buffer = "";
|
|
976
|
+
const toolCalls = new Map();
|
|
977
|
+
let finishReason = null;
|
|
978
|
+
let usage;
|
|
979
|
+
for await (const chunk of stream) {
|
|
980
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
981
|
+
const parsed = parseOpenAISseChunk(buffer);
|
|
982
|
+
buffer = parsed.remainder;
|
|
983
|
+
for (const event of parsed.events) {
|
|
984
|
+
if (event === "[DONE]") {
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
const record = readRecord(event);
|
|
988
|
+
usage = extractOpenAIUsage(record) ?? usage;
|
|
989
|
+
const choices = Array.isArray(record?.choices) ? record.choices : [];
|
|
990
|
+
for (const choiceValue of choices) {
|
|
991
|
+
const choice = readRecord(choiceValue);
|
|
992
|
+
if (!choice) {
|
|
993
|
+
continue;
|
|
994
|
+
}
|
|
995
|
+
const delta = readRecord(choice.delta);
|
|
996
|
+
const textDelta = extractOpenAIContentText(delta?.content);
|
|
997
|
+
if (textDelta.length > 0) {
|
|
998
|
+
yield { type: "text-delta", delta: textDelta };
|
|
999
|
+
}
|
|
1000
|
+
const rawToolCalls = Array.isArray(delta?.tool_calls) ? delta.tool_calls : [];
|
|
1001
|
+
for (const rawToolCall of rawToolCalls) {
|
|
1002
|
+
const toolCallRecord = readRecord(rawToolCall);
|
|
1003
|
+
const index = typeof toolCallRecord?.index === "number" ? toolCallRecord.index : 0;
|
|
1004
|
+
const current = toolCalls.get(index) ?? {
|
|
1005
|
+
id: typeof toolCallRecord?.id === "string" ? toolCallRecord.id : `tool-${index}`,
|
|
1006
|
+
name: "",
|
|
1007
|
+
arguments: "",
|
|
1008
|
+
started: false,
|
|
1009
|
+
};
|
|
1010
|
+
if (typeof toolCallRecord?.id === "string") {
|
|
1011
|
+
current.id = toolCallRecord.id;
|
|
1012
|
+
}
|
|
1013
|
+
const fn = readRecord(toolCallRecord?.function);
|
|
1014
|
+
if (typeof fn?.name === "string") {
|
|
1015
|
+
current.name = fn.name;
|
|
1016
|
+
}
|
|
1017
|
+
if (!current.started && current.name.length > 0) {
|
|
1018
|
+
current.started = true;
|
|
1019
|
+
yield {
|
|
1020
|
+
type: "tool-input-start",
|
|
1021
|
+
id: current.id,
|
|
1022
|
+
toolName: current.name,
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
if (typeof fn?.arguments === "string" && fn.arguments.length > 0) {
|
|
1026
|
+
current.arguments += fn.arguments;
|
|
1027
|
+
yield {
|
|
1028
|
+
type: "tool-input-delta",
|
|
1029
|
+
id: current.id,
|
|
1030
|
+
delta: fn.arguments,
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
toolCalls.set(index, current);
|
|
1034
|
+
}
|
|
1035
|
+
const normalizedFinishReason = normalizeOpenAIFinishReason(choice.finish_reason);
|
|
1036
|
+
if (normalizedFinishReason) {
|
|
1037
|
+
finishReason = normalizedFinishReason;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
if (buffer.trim().length > 0) {
|
|
1043
|
+
const parsed = parseOpenAISseChunk(`${buffer}\n\n`);
|
|
1044
|
+
for (const event of parsed.events) {
|
|
1045
|
+
if (event === "[DONE]") {
|
|
1046
|
+
continue;
|
|
1047
|
+
}
|
|
1048
|
+
const record = readRecord(event);
|
|
1049
|
+
usage = extractOpenAIUsage(record) ?? usage;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
if (finishReason &&
|
|
1053
|
+
typeof finishReason === "object" &&
|
|
1054
|
+
finishReason.unified === "tool-calls") {
|
|
1055
|
+
for (const toolCall of toolCalls.values()) {
|
|
1056
|
+
yield {
|
|
1057
|
+
type: "tool-call",
|
|
1058
|
+
toolCallId: toolCall.id,
|
|
1059
|
+
toolName: toolCall.name,
|
|
1060
|
+
input: toolCall.arguments,
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
yield {
|
|
1065
|
+
type: "finish",
|
|
1066
|
+
finishReason,
|
|
1067
|
+
...(usage ? { usage } : {}),
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
export function createOpenAIModelRuntime(config, modelId) {
|
|
1071
|
+
const fetchImpl = config.fetch ?? dntShim.dntGlobalThis.fetch;
|
|
1072
|
+
return {
|
|
1073
|
+
provider: config.name ?? "openai",
|
|
1074
|
+
modelId,
|
|
1075
|
+
specificationVersion: "v3",
|
|
1076
|
+
supportedUrls: {},
|
|
1077
|
+
doGenerate(optionsForRuntime) {
|
|
1078
|
+
const options = optionsForRuntime;
|
|
1079
|
+
const url = getOpenAIChatCompletionsUrl(config.baseURL);
|
|
1080
|
+
const body = buildOpenAIChatRequest(modelId, config.name ?? "openai", options, false);
|
|
1081
|
+
return requestJson({
|
|
1082
|
+
url,
|
|
1083
|
+
fetchImpl,
|
|
1084
|
+
providerLabel: config.name ?? "openai",
|
|
1085
|
+
init: {
|
|
1086
|
+
method: "POST",
|
|
1087
|
+
headers: createRequestHeaders({
|
|
1088
|
+
apiKeyHeaderName: "authorization",
|
|
1089
|
+
apiKey: `Bearer ${config.apiKey}`,
|
|
1090
|
+
extraHeaders: options.headers,
|
|
1091
|
+
}),
|
|
1092
|
+
body: JSON.stringify(body),
|
|
1093
|
+
signal: options.abortSignal,
|
|
1094
|
+
},
|
|
1095
|
+
}).then(buildOpenAIGenerateResult);
|
|
1096
|
+
},
|
|
1097
|
+
doStream(optionsForRuntime) {
|
|
1098
|
+
const options = optionsForRuntime;
|
|
1099
|
+
const url = getOpenAIChatCompletionsUrl(config.baseURL);
|
|
1100
|
+
const body = buildOpenAIChatRequest(modelId, config.name ?? "openai", options, true);
|
|
1101
|
+
return requestStream({
|
|
1102
|
+
url,
|
|
1103
|
+
fetchImpl,
|
|
1104
|
+
providerLabel: config.name ?? "openai",
|
|
1105
|
+
init: {
|
|
1106
|
+
method: "POST",
|
|
1107
|
+
headers: createRequestHeaders({
|
|
1108
|
+
apiKeyHeaderName: "authorization",
|
|
1109
|
+
apiKey: `Bearer ${config.apiKey}`,
|
|
1110
|
+
extraHeaders: options.headers,
|
|
1111
|
+
}),
|
|
1112
|
+
body: JSON.stringify(body),
|
|
1113
|
+
signal: options.abortSignal,
|
|
1114
|
+
},
|
|
1115
|
+
}).then((responseStream) => ({
|
|
1116
|
+
stream: ReadableStream.from(streamOpenAICompatibleParts(responseStream)),
|
|
1117
|
+
}));
|
|
1118
|
+
},
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
export function createAnthropicModelRuntime(config, modelId) {
|
|
1122
|
+
const fetchImpl = config.fetch ?? dntShim.dntGlobalThis.fetch;
|
|
1123
|
+
return {
|
|
1124
|
+
provider: config.name ?? "anthropic",
|
|
1125
|
+
modelId,
|
|
1126
|
+
specificationVersion: "v3",
|
|
1127
|
+
supportedUrls: {},
|
|
1128
|
+
doGenerate(optionsForRuntime) {
|
|
1129
|
+
const options = optionsForRuntime;
|
|
1130
|
+
const url = getAnthropicMessagesUrl(config.baseURL);
|
|
1131
|
+
const body = buildAnthropicMessagesRequest(modelId, config.name ?? "anthropic", options, false);
|
|
1132
|
+
return requestJson({
|
|
1133
|
+
url,
|
|
1134
|
+
fetchImpl,
|
|
1135
|
+
providerLabel: config.name ?? "anthropic",
|
|
1136
|
+
init: {
|
|
1137
|
+
method: "POST",
|
|
1138
|
+
headers: createAnthropicRequestHeaders({
|
|
1139
|
+
apiKey: config.apiKey,
|
|
1140
|
+
authToken: config.authToken,
|
|
1141
|
+
extraHeaders: options.headers,
|
|
1142
|
+
}),
|
|
1143
|
+
body: JSON.stringify(body),
|
|
1144
|
+
signal: options.abortSignal,
|
|
1145
|
+
},
|
|
1146
|
+
}).then(buildAnthropicGenerateResult);
|
|
1147
|
+
},
|
|
1148
|
+
doStream(optionsForRuntime) {
|
|
1149
|
+
const options = optionsForRuntime;
|
|
1150
|
+
const url = getAnthropicMessagesUrl(config.baseURL);
|
|
1151
|
+
const body = buildAnthropicMessagesRequest(modelId, config.name ?? "anthropic", options, true);
|
|
1152
|
+
return requestStream({
|
|
1153
|
+
url,
|
|
1154
|
+
fetchImpl,
|
|
1155
|
+
providerLabel: config.name ?? "anthropic",
|
|
1156
|
+
init: {
|
|
1157
|
+
method: "POST",
|
|
1158
|
+
headers: createAnthropicRequestHeaders({
|
|
1159
|
+
apiKey: config.apiKey,
|
|
1160
|
+
authToken: config.authToken,
|
|
1161
|
+
extraHeaders: options.headers,
|
|
1162
|
+
}),
|
|
1163
|
+
body: JSON.stringify(body),
|
|
1164
|
+
signal: options.abortSignal,
|
|
1165
|
+
},
|
|
1166
|
+
}).then((responseStream) => ({
|
|
1167
|
+
stream: ReadableStream.from(streamAnthropicCompatibleParts(responseStream)),
|
|
1168
|
+
}));
|
|
1169
|
+
},
|
|
1170
|
+
};
|
|
1171
|
+
}
|
|
1172
|
+
export function createGoogleModelRuntime(config, modelId) {
|
|
1173
|
+
const fetchImpl = config.fetch ?? dntShim.dntGlobalThis.fetch;
|
|
1174
|
+
return {
|
|
1175
|
+
provider: config.name ?? "google",
|
|
1176
|
+
modelId,
|
|
1177
|
+
specificationVersion: "v3",
|
|
1178
|
+
supportedUrls: {},
|
|
1179
|
+
doGenerate(optionsForRuntime) {
|
|
1180
|
+
const options = optionsForRuntime;
|
|
1181
|
+
const url = getGoogleGenerateContentUrl(config.baseURL, modelId);
|
|
1182
|
+
const body = buildGoogleGenerateContentRequest(config.name ?? "google", options);
|
|
1183
|
+
return requestJson({
|
|
1184
|
+
url,
|
|
1185
|
+
fetchImpl,
|
|
1186
|
+
providerLabel: config.name ?? "google",
|
|
1187
|
+
init: {
|
|
1188
|
+
method: "POST",
|
|
1189
|
+
headers: createRequestHeaders({
|
|
1190
|
+
apiKeyHeaderName: "x-goog-api-key",
|
|
1191
|
+
apiKey: config.apiKey,
|
|
1192
|
+
extraHeaders: options.headers,
|
|
1193
|
+
}),
|
|
1194
|
+
body: JSON.stringify(body),
|
|
1195
|
+
signal: options.abortSignal,
|
|
1196
|
+
},
|
|
1197
|
+
}).then(buildGoogleGenerateResult);
|
|
1198
|
+
},
|
|
1199
|
+
doStream(optionsForRuntime) {
|
|
1200
|
+
const options = optionsForRuntime;
|
|
1201
|
+
const url = getGoogleStreamGenerateContentUrl(config.baseURL, modelId);
|
|
1202
|
+
const body = buildGoogleGenerateContentRequest(config.name ?? "google", options);
|
|
1203
|
+
return requestStream({
|
|
1204
|
+
url,
|
|
1205
|
+
fetchImpl,
|
|
1206
|
+
providerLabel: config.name ?? "google",
|
|
1207
|
+
init: {
|
|
1208
|
+
method: "POST",
|
|
1209
|
+
headers: createRequestHeaders({
|
|
1210
|
+
apiKeyHeaderName: "x-goog-api-key",
|
|
1211
|
+
apiKey: config.apiKey,
|
|
1212
|
+
extraHeaders: options.headers,
|
|
1213
|
+
}),
|
|
1214
|
+
body: JSON.stringify(body),
|
|
1215
|
+
signal: options.abortSignal,
|
|
1216
|
+
},
|
|
1217
|
+
}).then((responseStream) => ({
|
|
1218
|
+
stream: ReadableStream.from(streamGoogleCompatibleParts(responseStream)),
|
|
1219
|
+
}));
|
|
1220
|
+
},
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
export function createOpenAIEmbeddingRuntime(config, modelId) {
|
|
1224
|
+
const fetchImpl = config.fetch ?? dntShim.dntGlobalThis.fetch;
|
|
1225
|
+
return {
|
|
1226
|
+
provider: config.name ?? "openai",
|
|
1227
|
+
modelId,
|
|
1228
|
+
supportsParallelCalls: true,
|
|
1229
|
+
doEmbed({ values, abortSignal }) {
|
|
1230
|
+
if (values.length === 0) {
|
|
1231
|
+
return Promise.resolve({
|
|
1232
|
+
embeddings: [],
|
|
1233
|
+
warnings: [],
|
|
1234
|
+
rawResponse: { data: [] },
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
const url = getOpenAIEmbeddingUrl(config.baseURL);
|
|
1238
|
+
return requestJson({
|
|
1239
|
+
url,
|
|
1240
|
+
fetchImpl,
|
|
1241
|
+
providerLabel: config.name ?? "openai",
|
|
1242
|
+
init: {
|
|
1243
|
+
method: "POST",
|
|
1244
|
+
headers: {
|
|
1245
|
+
"content-type": "application/json",
|
|
1246
|
+
authorization: `Bearer ${config.apiKey}`,
|
|
1247
|
+
},
|
|
1248
|
+
body: JSON.stringify({
|
|
1249
|
+
model: modelId,
|
|
1250
|
+
input: values,
|
|
1251
|
+
}),
|
|
1252
|
+
signal: abortSignal,
|
|
1253
|
+
},
|
|
1254
|
+
}).then((payload) => ({
|
|
1255
|
+
embeddings: extractOpenAIEmbeddings(payload),
|
|
1256
|
+
usage: {
|
|
1257
|
+
tokens: extractOpenAIUsageTokens(payload),
|
|
1258
|
+
},
|
|
1259
|
+
rawResponse: payload,
|
|
1260
|
+
warnings: [],
|
|
1261
|
+
}));
|
|
1262
|
+
},
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
export function createGoogleEmbeddingRuntime(config, modelId) {
|
|
1266
|
+
const fetchImpl = config.fetch ?? dntShim.dntGlobalThis.fetch;
|
|
1267
|
+
return {
|
|
1268
|
+
provider: config.name ?? "google",
|
|
1269
|
+
modelId,
|
|
1270
|
+
supportsParallelCalls: true,
|
|
1271
|
+
doEmbed({ values, abortSignal }) {
|
|
1272
|
+
if (values.length === 0) {
|
|
1273
|
+
return Promise.resolve({
|
|
1274
|
+
embeddings: [],
|
|
1275
|
+
warnings: [],
|
|
1276
|
+
rawResponse: { embeddings: [] },
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
const url = getGoogleEmbeddingUrl(config.baseURL, modelId);
|
|
1280
|
+
return Promise.all(values.map((value) => requestJson({
|
|
1281
|
+
url,
|
|
1282
|
+
fetchImpl,
|
|
1283
|
+
providerLabel: config.name ?? "google",
|
|
1284
|
+
init: {
|
|
1285
|
+
method: "POST",
|
|
1286
|
+
headers: {
|
|
1287
|
+
"content-type": "application/json",
|
|
1288
|
+
"x-goog-api-key": config.apiKey,
|
|
1289
|
+
},
|
|
1290
|
+
body: JSON.stringify({
|
|
1291
|
+
content: {
|
|
1292
|
+
parts: [{ text: value }],
|
|
1293
|
+
},
|
|
1294
|
+
}),
|
|
1295
|
+
signal: abortSignal,
|
|
1296
|
+
},
|
|
1297
|
+
}))).then((payloads) => ({
|
|
1298
|
+
embeddings: payloads.map(extractGoogleEmbedding),
|
|
1299
|
+
usage: {
|
|
1300
|
+
tokens: payloads.reduce((total, payload) => total + (extractGoogleUsageTokens(payload) ?? 0), 0),
|
|
1301
|
+
},
|
|
1302
|
+
rawResponse: payloads,
|
|
1303
|
+
warnings: [],
|
|
1304
|
+
}));
|
|
1305
|
+
},
|
|
1306
|
+
};
|
|
1307
|
+
}
|