@aislop-inc/opencode-mem 0.0.0-stage → 2.41.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +811 -2
- package/dist/config.d.ts +577 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +1141 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1268 -0
- package/dist/plugin.d.ts +10 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +10 -0
- package/dist/services/ai/ai-provider-factory.d.ts +8 -0
- package/dist/services/ai/ai-provider-factory.d.ts.map +1 -0
- package/dist/services/ai/ai-provider-factory.js +61 -0
- package/dist/services/ai/api-key-placeholder.d.ts +2 -0
- package/dist/services/ai/api-key-placeholder.d.ts.map +1 -0
- package/dist/services/ai/api-key-placeholder.js +14 -0
- package/dist/services/ai/internal-capture-sessions.d.ts +7 -0
- package/dist/services/ai/internal-capture-sessions.d.ts.map +1 -0
- package/dist/services/ai/internal-capture-sessions.js +31 -0
- package/dist/services/ai/opencode-diagnostics.d.ts +8 -0
- package/dist/services/ai/opencode-diagnostics.d.ts.map +1 -0
- package/dist/services/ai/opencode-diagnostics.js +36 -0
- package/dist/services/ai/opencode-host-config.d.ts +11 -0
- package/dist/services/ai/opencode-host-config.d.ts.map +1 -0
- package/dist/services/ai/opencode-host-config.js +51 -0
- package/dist/services/ai/opencode-provider-loader.d.ts +4 -0
- package/dist/services/ai/opencode-provider-loader.d.ts.map +1 -0
- package/dist/services/ai/opencode-provider-loader.js +4 -0
- package/dist/services/ai/opencode-provider.d.ts +154 -0
- package/dist/services/ai/opencode-provider.d.ts.map +1 -0
- package/dist/services/ai/opencode-provider.js +731 -0
- package/dist/services/ai/opencode-sdk-client.d.ts +7 -0
- package/dist/services/ai/opencode-sdk-client.d.ts.map +1 -0
- package/dist/services/ai/opencode-sdk-client.js +38 -0
- package/dist/services/ai/profile-llm-client.d.ts +3 -0
- package/dist/services/ai/profile-llm-client.d.ts.map +1 -0
- package/dist/services/ai/profile-llm-client.js +29 -0
- package/dist/services/ai/provider-config.d.ts +19 -0
- package/dist/services/ai/provider-config.d.ts.map +1 -0
- package/dist/services/ai/provider-config.js +36 -0
- package/dist/services/ai/providers/anthropic-messages.d.ts +35 -0
- package/dist/services/ai/providers/anthropic-messages.d.ts.map +1 -0
- package/dist/services/ai/providers/anthropic-messages.js +220 -0
- package/dist/services/ai/providers/api-route.d.ts +49 -0
- package/dist/services/ai/providers/api-route.d.ts.map +1 -0
- package/dist/services/ai/providers/api-route.js +57 -0
- package/dist/services/ai/providers/atlas-cloud.d.ts +51 -0
- package/dist/services/ai/providers/atlas-cloud.d.ts.map +1 -0
- package/dist/services/ai/providers/atlas-cloud.js +59 -0
- package/dist/services/ai/providers/base-provider.d.ts +33 -0
- package/dist/services/ai/providers/base-provider.d.ts.map +1 -0
- package/dist/services/ai/providers/base-provider.js +24 -0
- package/dist/services/ai/providers/cheaper-inference.d.ts +50 -0
- package/dist/services/ai/providers/cheaper-inference.d.ts.map +1 -0
- package/dist/services/ai/providers/cheaper-inference.js +58 -0
- package/dist/services/ai/providers/cloudgpu.d.ts +52 -0
- package/dist/services/ai/providers/cloudgpu.d.ts.map +1 -0
- package/dist/services/ai/providers/cloudgpu.js +60 -0
- package/dist/services/ai/providers/google-gemini.d.ts +16 -0
- package/dist/services/ai/providers/google-gemini.d.ts.map +1 -0
- package/dist/services/ai/providers/google-gemini.js +228 -0
- package/dist/services/ai/providers/minimax.d.ts +38 -0
- package/dist/services/ai/providers/minimax.d.ts.map +1 -0
- package/dist/services/ai/providers/minimax.js +57 -0
- package/dist/services/ai/providers/openai-chat-completion.d.ts +30 -0
- package/dist/services/ai/providers/openai-chat-completion.d.ts.map +1 -0
- package/dist/services/ai/providers/openai-chat-completion.js +532 -0
- package/dist/services/ai/providers/openai-responses.d.ts +14 -0
- package/dist/services/ai/providers/openai-responses.d.ts.map +1 -0
- package/dist/services/ai/providers/openai-responses.js +182 -0
- package/dist/services/ai/providers/orcarouter.d.ts +52 -0
- package/dist/services/ai/providers/orcarouter.d.ts.map +1 -0
- package/dist/services/ai/providers/orcarouter.js +66 -0
- package/dist/services/ai/providers/runcrate.d.ts +49 -0
- package/dist/services/ai/providers/runcrate.d.ts.map +1 -0
- package/dist/services/ai/providers/runcrate.js +57 -0
- package/dist/services/ai/session/ai-session-manager.d.ts +25 -0
- package/dist/services/ai/session/ai-session-manager.d.ts.map +1 -0
- package/dist/services/ai/session/ai-session-manager.js +180 -0
- package/dist/services/ai/session/session-types.d.ts +43 -0
- package/dist/services/ai/session/session-types.d.ts.map +1 -0
- package/dist/services/ai/session/session-types.js +1 -0
- package/dist/services/ai/tools/tool-schema.d.ts +41 -0
- package/dist/services/ai/tools/tool-schema.d.ts.map +1 -0
- package/dist/services/ai/tools/tool-schema.js +24 -0
- package/dist/services/ai/validators/user-profile-validator.d.ts +13 -0
- package/dist/services/ai/validators/user-profile-validator.d.ts.map +1 -0
- package/dist/services/ai/validators/user-profile-validator.js +114 -0
- package/dist/services/api-handlers.d.ts +277 -0
- package/dist/services/api-handlers.d.ts.map +1 -0
- package/dist/services/api-handlers.js +1740 -0
- package/dist/services/auth-token.d.ts +11 -0
- package/dist/services/auth-token.d.ts.map +1 -0
- package/dist/services/auth-token.js +44 -0
- package/dist/services/auto-capture-config-status.d.ts +15 -0
- package/dist/services/auto-capture-config-status.d.ts.map +1 -0
- package/dist/services/auto-capture-config-status.js +31 -0
- package/dist/services/auto-capture.d.ts +33 -0
- package/dist/services/auto-capture.d.ts.map +1 -0
- package/dist/services/auto-capture.js +906 -0
- package/dist/services/auto-update.d.ts +28 -0
- package/dist/services/auto-update.d.ts.map +1 -0
- package/dist/services/auto-update.js +197 -0
- package/dist/services/capture-evidence-events.d.ts +3 -0
- package/dist/services/capture-evidence-events.d.ts.map +1 -0
- package/dist/services/capture-evidence-events.js +15 -0
- package/dist/services/capture-evidence.d.ts +21 -0
- package/dist/services/capture-evidence.d.ts.map +1 -0
- package/dist/services/capture-evidence.js +168 -0
- package/dist/services/capture-tool-evidence.d.ts +21 -0
- package/dist/services/capture-tool-evidence.d.ts.map +1 -0
- package/dist/services/capture-tool-evidence.js +109 -0
- package/dist/services/cleanup-service.d.ts +26 -0
- package/dist/services/cleanup-service.d.ts.map +1 -0
- package/dist/services/cleanup-service.js +199 -0
- package/dist/services/client.d.ts +167 -0
- package/dist/services/client.d.ts.map +1 -0
- package/dist/services/client.js +535 -0
- package/dist/services/context.d.ts +15 -0
- package/dist/services/context.d.ts.map +1 -0
- package/dist/services/context.js +44 -0
- package/dist/services/cors.d.ts +13 -0
- package/dist/services/cors.d.ts.map +1 -0
- package/dist/services/cors.js +52 -0
- package/dist/services/deduplication-service.d.ts +29 -0
- package/dist/services/deduplication-service.d.ts.map +1 -0
- package/dist/services/deduplication-service.js +113 -0
- package/dist/services/dreaming-lock.d.ts +6 -0
- package/dist/services/dreaming-lock.d.ts.map +1 -0
- package/dist/services/dreaming-lock.js +84 -0
- package/dist/services/dreaming-service.d.ts +96 -0
- package/dist/services/dreaming-service.d.ts.map +1 -0
- package/dist/services/dreaming-service.js +626 -0
- package/dist/services/embedding.d.ts +92 -0
- package/dist/services/embedding.d.ts.map +1 -0
- package/dist/services/embedding.js +467 -0
- package/dist/services/injected-prompt-filter.d.ts +49 -0
- package/dist/services/injected-prompt-filter.d.ts.map +1 -0
- package/dist/services/injected-prompt-filter.js +79 -0
- package/dist/services/jsonc.d.ts +7 -0
- package/dist/services/jsonc.d.ts.map +1 -0
- package/dist/services/jsonc.js +110 -0
- package/dist/services/language-detector.d.ts +3 -0
- package/dist/services/language-detector.d.ts.map +1 -0
- package/dist/services/language-detector.js +33 -0
- package/dist/services/logger.d.ts +15 -0
- package/dist/services/logger.d.ts.map +1 -0
- package/dist/services/logger.js +171 -0
- package/dist/services/markdown/backend.d.ts +35 -0
- package/dist/services/markdown/backend.d.ts.map +1 -0
- package/dist/services/markdown/backend.js +111 -0
- package/dist/services/markdown/document.d.ts +44 -0
- package/dist/services/markdown/document.d.ts.map +1 -0
- package/dist/services/markdown/document.js +159 -0
- package/dist/services/markdown/reconcile.d.ts +26 -0
- package/dist/services/markdown/reconcile.d.ts.map +1 -0
- package/dist/services/markdown/reconcile.js +333 -0
- package/dist/services/markdown/vault.d.ts +32 -0
- package/dist/services/markdown/vault.d.ts.map +1 -0
- package/dist/services/markdown/vault.js +142 -0
- package/dist/services/markdown/watcher.d.ts +22 -0
- package/dist/services/markdown/watcher.d.ts.map +1 -0
- package/dist/services/markdown/watcher.js +131 -0
- package/dist/services/memory-portability-service.d.ts +38 -0
- package/dist/services/memory-portability-service.d.ts.map +1 -0
- package/dist/services/memory-portability-service.js +372 -0
- package/dist/services/memory-provenance.d.ts +16 -0
- package/dist/services/memory-provenance.d.ts.map +1 -0
- package/dist/services/memory-provenance.js +115 -0
- package/dist/services/memory-recall.d.ts +49 -0
- package/dist/services/memory-recall.d.ts.map +1 -0
- package/dist/services/memory-recall.js +196 -0
- package/dist/services/memory-scope.d.ts +17 -0
- package/dist/services/memory-scope.d.ts.map +1 -0
- package/dist/services/memory-scope.js +43 -0
- package/dist/services/migration-service.d.ts +43 -0
- package/dist/services/migration-service.d.ts.map +1 -0
- package/dist/services/migration-service.js +333 -0
- package/dist/services/onnxruntime-resolve.d.ts +25 -0
- package/dist/services/onnxruntime-resolve.d.ts.map +1 -0
- package/dist/services/onnxruntime-resolve.js +176 -0
- package/dist/services/privacy.d.ts +38 -0
- package/dist/services/privacy.d.ts.map +1 -0
- package/dist/services/privacy.js +156 -0
- package/dist/services/request-timeouts.d.ts +4 -0
- package/dist/services/request-timeouts.d.ts.map +1 -0
- package/dist/services/request-timeouts.js +5 -0
- package/dist/services/runtime-require.d.ts +18 -0
- package/dist/services/runtime-require.d.ts.map +1 -0
- package/dist/services/runtime-require.js +75 -0
- package/dist/services/secret-resolver.d.ts +2 -0
- package/dist/services/secret-resolver.d.ts.map +1 -0
- package/dist/services/secret-resolver.js +65 -0
- package/dist/services/shard-inventory-service.d.ts +70 -0
- package/dist/services/shard-inventory-service.d.ts.map +1 -0
- package/dist/services/shard-inventory-service.js +315 -0
- package/dist/services/shard-path-migration-service.d.ts +32 -0
- package/dist/services/shard-path-migration-service.d.ts.map +1 -0
- package/dist/services/shard-path-migration-service.js +386 -0
- package/dist/services/skills/skills-manager.d.ts +39 -0
- package/dist/services/skills/skills-manager.d.ts.map +1 -0
- package/dist/services/skills/skills-manager.js +231 -0
- package/dist/services/skills/types.d.ts +32 -0
- package/dist/services/skills/types.d.ts.map +1 -0
- package/dist/services/skills/types.js +5 -0
- package/dist/services/stats-service.d.ts +71 -0
- package/dist/services/stats-service.d.ts.map +1 -0
- package/dist/services/stats-service.js +288 -0
- package/dist/services/tags.d.ts +30 -0
- package/dist/services/tags.d.ts.map +1 -0
- package/dist/services/tags.js +245 -0
- package/dist/services/transformers-resolve.d.ts +17 -0
- package/dist/services/transformers-resolve.d.ts.map +1 -0
- package/dist/services/transformers-resolve.js +142 -0
- package/dist/services/turso/connection-manager.d.ts +66 -0
- package/dist/services/turso/connection-manager.d.ts.map +1 -0
- package/dist/services/turso/connection-manager.js +408 -0
- package/dist/services/turso/encryption-key.d.ts +17 -0
- package/dist/services/turso/encryption-key.d.ts.map +1 -0
- package/dist/services/turso/encryption-key.js +111 -0
- package/dist/services/turso/encryption-migrator.d.ts +6 -0
- package/dist/services/turso/encryption-migrator.d.ts.map +1 -0
- package/dist/services/turso/encryption-migrator.js +234 -0
- package/dist/services/turso/engine-migrator.d.ts +6 -0
- package/dist/services/turso/engine-migrator.d.ts.map +1 -0
- package/dist/services/turso/engine-migrator.js +318 -0
- package/dist/services/turso/error-classifiers.d.ts +4 -0
- package/dist/services/turso/error-classifiers.d.ts.map +1 -0
- package/dist/services/turso/error-classifiers.js +19 -0
- package/dist/services/turso/legacy-migrator.d.ts +2 -0
- package/dist/services/turso/legacy-migrator.d.ts.map +1 -0
- package/dist/services/turso/legacy-migrator.js +535 -0
- package/dist/services/turso/lifecycle.d.ts +11 -0
- package/dist/services/turso/lifecycle.d.ts.map +1 -0
- package/dist/services/turso/lifecycle.js +32 -0
- package/dist/services/turso/operation-lock.d.ts +26 -0
- package/dist/services/turso/operation-lock.d.ts.map +1 -0
- package/dist/services/turso/operation-lock.js +188 -0
- package/dist/services/turso/plugin-refs.d.ts +13 -0
- package/dist/services/turso/plugin-refs.d.ts.map +1 -0
- package/dist/services/turso/plugin-refs.js +43 -0
- package/dist/services/turso/ready.d.ts +3 -0
- package/dist/services/turso/ready.d.ts.map +1 -0
- package/dist/services/turso/ready.js +32 -0
- package/dist/services/turso/schema-migrations.d.ts +24 -0
- package/dist/services/turso/schema-migrations.d.ts.map +1 -0
- package/dist/services/turso/schema-migrations.js +367 -0
- package/dist/services/turso/shard-manager.d.ts +41 -0
- package/dist/services/turso/shard-manager.d.ts.map +1 -0
- package/dist/services/turso/shard-manager.js +419 -0
- package/dist/services/turso/sqlite-handle-release.d.ts +31 -0
- package/dist/services/turso/sqlite-handle-release.d.ts.map +1 -0
- package/dist/services/turso/sqlite-handle-release.js +101 -0
- package/dist/services/turso/turso-db.d.ts +61 -0
- package/dist/services/turso/turso-db.d.ts.map +1 -0
- package/dist/services/turso/turso-db.js +214 -0
- package/dist/services/turso/types.d.ts +52 -0
- package/dist/services/turso/types.d.ts.map +1 -0
- package/dist/services/turso/types.js +1 -0
- package/dist/services/turso/vector-search.d.ts +63 -0
- package/dist/services/turso/vector-search.d.ts.map +1 -0
- package/dist/services/turso/vector-search.js +510 -0
- package/dist/services/turso/vector-utils.d.ts +16 -0
- package/dist/services/turso/vector-utils.d.ts.map +1 -0
- package/dist/services/turso/vector-utils.js +98 -0
- package/dist/services/turso/wal-index-recovery.d.ts +51 -0
- package/dist/services/turso/wal-index-recovery.d.ts.map +1 -0
- package/dist/services/turso/wal-index-recovery.js +248 -0
- package/dist/services/update-on-capture.d.ts +77 -0
- package/dist/services/update-on-capture.d.ts.map +1 -0
- package/dist/services/update-on-capture.js +116 -0
- package/dist/services/user-memory-learning.d.ts +137 -0
- package/dist/services/user-memory-learning.d.ts.map +1 -0
- package/dist/services/user-memory-learning.js +787 -0
- package/dist/services/user-profile/ai-cleanup.d.ts +59 -0
- package/dist/services/user-profile/ai-cleanup.d.ts.map +1 -0
- package/dist/services/user-profile/ai-cleanup.js +488 -0
- package/dist/services/user-profile/learning-lock.d.ts +41 -0
- package/dist/services/user-profile/learning-lock.d.ts.map +1 -0
- package/dist/services/user-profile/learning-lock.js +619 -0
- package/dist/services/user-profile/profile-context.d.ts +2 -0
- package/dist/services/user-profile/profile-context.d.ts.map +1 -0
- package/dist/services/user-profile/profile-context.js +105 -0
- package/dist/services/user-profile/profile-utils.d.ts +17 -0
- package/dist/services/user-profile/profile-utils.d.ts.map +1 -0
- package/dist/services/user-profile/profile-utils.js +77 -0
- package/dist/services/user-profile/types.d.ts +93 -0
- package/dist/services/user-profile/types.d.ts.map +1 -0
- package/dist/services/user-profile/types.js +1 -0
- package/dist/services/user-profile/user-profile-manager.d.ts +106 -0
- package/dist/services/user-profile/user-profile-manager.d.ts.map +1 -0
- package/dist/services/user-profile/user-profile-manager.js +1793 -0
- package/dist/services/user-prompt/user-prompt-manager.d.ts +64 -0
- package/dist/services/user-prompt/user-prompt-manager.d.ts.map +1 -0
- package/dist/services/user-prompt/user-prompt-manager.js +428 -0
- package/dist/services/web-api-auth.d.ts +4 -0
- package/dist/services/web-api-auth.d.ts.map +1 -0
- package/dist/services/web-api-auth.js +24 -0
- package/dist/services/web-auth.d.ts +47 -0
- package/dist/services/web-auth.d.ts.map +1 -0
- package/dist/services/web-auth.js +105 -0
- package/dist/services/web-server.d.ts +58 -0
- package/dist/services/web-server.d.ts.map +1 -0
- package/dist/services/web-server.js +774 -0
- package/dist/shared/api/index.d.ts +5 -0
- package/dist/shared/api/index.d.ts.map +1 -0
- package/dist/shared/api/index.js +2 -0
- package/dist/shared/api/portability-schemas.d.ts +57 -0
- package/dist/shared/api/portability-schemas.d.ts.map +1 -0
- package/dist/shared/api/portability-schemas.js +39 -0
- package/dist/shared/api/schemas.d.ts +296 -0
- package/dist/shared/api/schemas.d.ts.map +1 -0
- package/dist/shared/api/schemas.js +134 -0
- package/dist/shared/api/types.d.ts +29 -0
- package/dist/shared/api/types.d.ts.map +1 -0
- package/dist/shared/api/types.js +1 -0
- package/dist/tui.d.ts +10 -0
- package/dist/tui.d.ts.map +1 -0
- package/dist/tui.js +17 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/utils/context-limit.d.ts +11 -0
- package/dist/utils/context-limit.d.ts.map +1 -0
- package/dist/utils/context-limit.js +43 -0
- package/dist/utils/math.d.ts +8 -0
- package/dist/utils/math.d.ts.map +1 -0
- package/dist/utils/math.js +48 -0
- package/dist/utils/profile.d.ts +3 -0
- package/dist/utils/profile.d.ts.map +1 -0
- package/dist/utils/profile.js +15 -0
- package/dist/v2/adapter.d.ts +3 -0
- package/dist/v2/adapter.d.ts.map +1 -0
- package/dist/v2/adapter.js +217 -0
- package/dist/v2/legacy-client.d.ts +22 -0
- package/dist/v2/legacy-client.d.ts.map +1 -0
- package/dist/v2/legacy-client.js +410 -0
- package/dist/v2/plugin.d.ts +4 -0
- package/dist/v2/plugin.d.ts.map +1 -0
- package/dist/v2/plugin.js +34 -0
- package/dist/v2/toast-rpc.d.ts +43 -0
- package/dist/v2/toast-rpc.d.ts.map +1 -0
- package/dist/v2/toast-rpc.js +26 -0
- package/dist/web/accent-preview.html +444 -0
- package/dist/web/assets/index-mTgxOb5l.css +2 -0
- package/dist/web/assets/index-ysje17yK.js +109 -0
- package/dist/web/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/web/favicon.ico +0 -0
- package/dist/web/favicon.svg +11 -0
- package/dist/web/index.html +29 -0
- package/dist/web/opencode-mem-icon.png +0 -0
- package/package.json +122 -4
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { BaseAIProvider, type ProviderConfig, type ToolCallResult } from "./base-provider.js";
|
|
2
|
+
import type { AISessionManager } from "../session/ai-session-manager.js";
|
|
3
|
+
import type { AIMessage, AIProviderType } from "../session/session-types.js";
|
|
4
|
+
import type { ChatCompletionTool } from "../tools/tool-schema.js";
|
|
5
|
+
/** Some OpenAI-compatible gateways reject non-streaming chat/completions (e.g. codebuddy). */
|
|
6
|
+
export declare function isStreamRequiredError(status: number, errorText: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Collapse an OpenAI chat-completions SSE body into the non-streaming response
|
|
9
|
+
* shape expected by executeToolCall (choices[0].message with merged tool_calls).
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseChatCompletionStream(response: Response): Promise<unknown>;
|
|
12
|
+
export declare class OpenAIChatCompletionProvider extends BaseAIProvider {
|
|
13
|
+
private readonly aiSessionManager;
|
|
14
|
+
constructor(config: ProviderConfig, aiSessionManager: AISessionManager);
|
|
15
|
+
getProviderName(): string;
|
|
16
|
+
supportsSession(): boolean;
|
|
17
|
+
/** Provider tag used for AI session storage and diagnostics. */
|
|
18
|
+
protected sessionProviderTag(): AIProviderType;
|
|
19
|
+
/**
|
|
20
|
+
* Resolve the OpenAI-compatible API base URL.
|
|
21
|
+
* Trailing slashes are stripped so `${base}/chat/completions` is well-formed.
|
|
22
|
+
*/
|
|
23
|
+
protected resolveEndpoint(): string;
|
|
24
|
+
/** Resolve the model ID sent in the request body. */
|
|
25
|
+
protected resolveModel(): string;
|
|
26
|
+
private addToolResponse;
|
|
27
|
+
protected filterIncompleteToolCallSequences(messages: AIMessage[]): AIMessage[];
|
|
28
|
+
executeToolCall(systemPrompt: string, userPrompt: string, toolSchema: ChatCompletionTool, sessionId: string): Promise<ToolCallResult>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=openai-chat-completion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-chat-completion.d.ts","sourceRoot":"","sources":["../../../../src/services/ai/providers/openai-chat-completion.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,cAAc,EACnB,KAAK,cAAc,EAEpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAC7E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAwFlE,8FAA8F;AAC9F,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAGhF;AAQD;;;GAGG;AACH,wBAAsB,yBAAyB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAsGpF;AAED,qBAAa,4BAA6B,SAAQ,cAAc;IAC9D,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAmB;gBAExC,MAAM,EAAE,cAAc,EAAE,gBAAgB,EAAE,gBAAgB;IAKtE,eAAe,IAAI,MAAM;IAIzB,eAAe,IAAI,OAAO;IAI1B,gEAAgE;IAChE,SAAS,CAAC,kBAAkB,IAAI,cAAc;IAI9C;;;OAGG;IACH,SAAS,CAAC,eAAe,IAAI,MAAM;IAInC,qDAAqD;IACrD,SAAS,CAAC,YAAY,IAAI,MAAM;YAIlB,eAAe;IAqB7B,SAAS,CAAC,iCAAiC,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE;IAwCzE,eAAe,CACnB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,kBAAkB,EAC9B,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,cAAc,CAAC;CAkX3B"}
|
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import { BaseAIProvider, applySafeExtraParams, } from "./base-provider.js";
|
|
2
|
+
import { log } from "../../logger.js";
|
|
3
|
+
import { UserProfileValidator } from "../validators/user-profile-validator.js";
|
|
4
|
+
function isErrorResponseBody(data) {
|
|
5
|
+
return (typeof data === "object" &&
|
|
6
|
+
data !== null &&
|
|
7
|
+
typeof data.status === "string" &&
|
|
8
|
+
typeof data.msg === "string");
|
|
9
|
+
}
|
|
10
|
+
function hasNonEmptyChoices(data) {
|
|
11
|
+
if (typeof data !== "object" || data === null)
|
|
12
|
+
return false;
|
|
13
|
+
const { choices } = data;
|
|
14
|
+
if (!Array.isArray(choices) || choices.length === 0)
|
|
15
|
+
return false;
|
|
16
|
+
const first = choices[0];
|
|
17
|
+
if (typeof first !== "object" || first === null)
|
|
18
|
+
return false;
|
|
19
|
+
if (typeof first.message !== "object" || first.message === null)
|
|
20
|
+
return false;
|
|
21
|
+
const { content, tool_calls } = first.message;
|
|
22
|
+
if (content !== undefined && content !== null && typeof content !== "string")
|
|
23
|
+
return false;
|
|
24
|
+
if (tool_calls !== undefined && !Array.isArray(tool_calls))
|
|
25
|
+
return false;
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
function extractFirstJSON(raw) {
|
|
29
|
+
let depth = 0;
|
|
30
|
+
let start = -1;
|
|
31
|
+
for (let i = 0; i < raw.length; i++) {
|
|
32
|
+
if (raw[i] === "{" || raw[i] === "[") {
|
|
33
|
+
if (depth === 0)
|
|
34
|
+
start = i;
|
|
35
|
+
depth++;
|
|
36
|
+
}
|
|
37
|
+
else if (raw[i] === "}" || raw[i] === "]") {
|
|
38
|
+
depth--;
|
|
39
|
+
if (depth === 0)
|
|
40
|
+
return raw.slice(start, i + 1);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
function repairInnerQuotes(json) {
|
|
46
|
+
let result = json.replace(/([\u4e00-\u9fff\u3000-\u303f\uff00-\uffef])"([\u4e00-\u9fff\u3000-\u303f\uff00-\uffef])/g, '$1\\"$2');
|
|
47
|
+
result = result.replace(/([\u4e00-\u9fff\u3000-\u303f\uff00-\uffef])"(?=\s*[,}\]])/g, '$1\\"');
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
/** Some OpenAI-compatible gateways reject non-streaming chat/completions (e.g. codebuddy). */
|
|
51
|
+
export function isStreamRequiredError(status, errorText) {
|
|
52
|
+
if (status !== 400)
|
|
53
|
+
return false;
|
|
54
|
+
return /non[- ]?stream/i.test(errorText) || /only\s+supports?\s+stream/i.test(errorText);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Collapse an OpenAI chat-completions SSE body into the non-streaming response
|
|
58
|
+
* shape expected by executeToolCall (choices[0].message with merged tool_calls).
|
|
59
|
+
*/
|
|
60
|
+
export async function parseChatCompletionStream(response) {
|
|
61
|
+
const text = await response.text();
|
|
62
|
+
const toolCallsByIndex = new Map();
|
|
63
|
+
let content = "";
|
|
64
|
+
let finishReason;
|
|
65
|
+
for (const line of text.split(/\r?\n/)) {
|
|
66
|
+
const trimmed = line.trim();
|
|
67
|
+
if (!trimmed.startsWith("data:"))
|
|
68
|
+
continue;
|
|
69
|
+
const payload = trimmed.slice(5).trim();
|
|
70
|
+
if (!payload || payload === "[DONE]") {
|
|
71
|
+
if (payload === "[DONE]")
|
|
72
|
+
break;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
let chunk;
|
|
76
|
+
try {
|
|
77
|
+
chunk = JSON.parse(payload);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const choice = chunk.choices?.[0];
|
|
83
|
+
if (!choice)
|
|
84
|
+
continue;
|
|
85
|
+
if (choice.finish_reason)
|
|
86
|
+
finishReason = choice.finish_reason;
|
|
87
|
+
// Some gateways emit a full message object instead of deltas.
|
|
88
|
+
if (choice.message) {
|
|
89
|
+
if (typeof choice.message.content === "string") {
|
|
90
|
+
content = choice.message.content;
|
|
91
|
+
}
|
|
92
|
+
if (Array.isArray(choice.message.tool_calls)) {
|
|
93
|
+
choice.message.tool_calls.forEach((tc, index) => {
|
|
94
|
+
toolCallsByIndex.set(index, {
|
|
95
|
+
id: tc.id,
|
|
96
|
+
type: "function",
|
|
97
|
+
function: {
|
|
98
|
+
name: tc.function?.name ?? "",
|
|
99
|
+
arguments: tc.function?.arguments ?? "",
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const delta = choice.delta;
|
|
107
|
+
if (!delta)
|
|
108
|
+
continue;
|
|
109
|
+
if (typeof delta.content === "string")
|
|
110
|
+
content += delta.content;
|
|
111
|
+
if (!Array.isArray(delta.tool_calls))
|
|
112
|
+
continue;
|
|
113
|
+
for (const tc of delta.tool_calls) {
|
|
114
|
+
const index = typeof tc.index === "number" ? tc.index : 0;
|
|
115
|
+
let acc = toolCallsByIndex.get(index);
|
|
116
|
+
if (!acc) {
|
|
117
|
+
acc = {
|
|
118
|
+
id: tc.id ?? `call_${index}`,
|
|
119
|
+
type: "function",
|
|
120
|
+
function: { name: "", arguments: "" },
|
|
121
|
+
};
|
|
122
|
+
toolCallsByIndex.set(index, acc);
|
|
123
|
+
}
|
|
124
|
+
if (tc.id)
|
|
125
|
+
acc.id = tc.id;
|
|
126
|
+
if (tc.function?.name)
|
|
127
|
+
acc.function.name += tc.function.name;
|
|
128
|
+
if (typeof tc.function?.arguments === "string") {
|
|
129
|
+
acc.function.arguments += tc.function.arguments;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const tool_calls = [...toolCallsByIndex.entries()]
|
|
134
|
+
.sort((a, b) => a[0] - b[0])
|
|
135
|
+
.map(([, value]) => value);
|
|
136
|
+
return {
|
|
137
|
+
choices: [
|
|
138
|
+
{
|
|
139
|
+
message: {
|
|
140
|
+
content: content || null,
|
|
141
|
+
...(tool_calls.length > 0 ? { tool_calls } : {}),
|
|
142
|
+
},
|
|
143
|
+
finish_reason: finishReason,
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
export class OpenAIChatCompletionProvider extends BaseAIProvider {
|
|
149
|
+
aiSessionManager;
|
|
150
|
+
constructor(config, aiSessionManager) {
|
|
151
|
+
super(config);
|
|
152
|
+
this.aiSessionManager = aiSessionManager;
|
|
153
|
+
}
|
|
154
|
+
getProviderName() {
|
|
155
|
+
return this.sessionProviderTag();
|
|
156
|
+
}
|
|
157
|
+
supportsSession() {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
/** Provider tag used for AI session storage and diagnostics. */
|
|
161
|
+
sessionProviderTag() {
|
|
162
|
+
return "openai-chat";
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Resolve the OpenAI-compatible API base URL.
|
|
166
|
+
* Trailing slashes are stripped so `${base}/chat/completions` is well-formed.
|
|
167
|
+
*/
|
|
168
|
+
resolveEndpoint() {
|
|
169
|
+
return (this.config.apiUrl || "").trim().replace(/\/+$/, "");
|
|
170
|
+
}
|
|
171
|
+
/** Resolve the model ID sent in the request body. */
|
|
172
|
+
resolveModel() {
|
|
173
|
+
return this.config.model;
|
|
174
|
+
}
|
|
175
|
+
async addToolResponse(sessionId, messages, toolCallId, content) {
|
|
176
|
+
const sequence = (await this.aiSessionManager.getLastSequence(sessionId)) + 1;
|
|
177
|
+
await this.aiSessionManager.addMessage({
|
|
178
|
+
aiSessionId: sessionId,
|
|
179
|
+
sequence,
|
|
180
|
+
role: "tool",
|
|
181
|
+
content,
|
|
182
|
+
toolCallId,
|
|
183
|
+
});
|
|
184
|
+
messages.push({
|
|
185
|
+
role: "tool",
|
|
186
|
+
tool_call_id: toolCallId,
|
|
187
|
+
content,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
filterIncompleteToolCallSequences(messages) {
|
|
191
|
+
const result = [];
|
|
192
|
+
let i = 0;
|
|
193
|
+
while (i < messages.length) {
|
|
194
|
+
const msg = messages[i];
|
|
195
|
+
if (!msg) {
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0) {
|
|
199
|
+
const toolCallIds = new Set(msg.toolCalls.map((tc) => tc.id));
|
|
200
|
+
const toolResponses = [];
|
|
201
|
+
let j = i + 1;
|
|
202
|
+
while (j < messages.length && messages[j]?.role === "tool") {
|
|
203
|
+
const toolMessage = messages[j];
|
|
204
|
+
if (toolMessage?.toolCallId && toolCallIds.has(toolMessage.toolCallId)) {
|
|
205
|
+
toolResponses.push(toolMessage);
|
|
206
|
+
toolCallIds.delete(toolMessage.toolCallId);
|
|
207
|
+
}
|
|
208
|
+
j++;
|
|
209
|
+
}
|
|
210
|
+
if (toolCallIds.size === 0) {
|
|
211
|
+
result.push(msg);
|
|
212
|
+
toolResponses.forEach((tr) => result.push(tr));
|
|
213
|
+
i = j;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
result.push(msg);
|
|
221
|
+
i++;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
async executeToolCall(systemPrompt, userPrompt, toolSchema, sessionId) {
|
|
227
|
+
let session = await this.aiSessionManager.getSession(sessionId, this.sessionProviderTag());
|
|
228
|
+
if (!session) {
|
|
229
|
+
session = await this.aiSessionManager.createSession({
|
|
230
|
+
provider: this.sessionProviderTag(),
|
|
231
|
+
sessionId,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
const existingMessages = await this.aiSessionManager.getMessages(session.id);
|
|
235
|
+
const messages = [];
|
|
236
|
+
const validatedMessages = this.filterIncompleteToolCallSequences(existingMessages);
|
|
237
|
+
for (const msg of validatedMessages) {
|
|
238
|
+
const apiMsg = {
|
|
239
|
+
role: msg.role,
|
|
240
|
+
content: msg.content,
|
|
241
|
+
};
|
|
242
|
+
if (msg.toolCalls) {
|
|
243
|
+
apiMsg.tool_calls = msg.toolCalls;
|
|
244
|
+
}
|
|
245
|
+
if (msg.toolCallId) {
|
|
246
|
+
apiMsg.tool_call_id = msg.toolCallId;
|
|
247
|
+
}
|
|
248
|
+
messages.push(apiMsg);
|
|
249
|
+
}
|
|
250
|
+
if (messages.length === 0) {
|
|
251
|
+
const sequence = (await this.aiSessionManager.getLastSequence(session.id)) + 1;
|
|
252
|
+
await this.aiSessionManager.addMessage({
|
|
253
|
+
aiSessionId: session.id,
|
|
254
|
+
sequence,
|
|
255
|
+
role: "system",
|
|
256
|
+
content: systemPrompt,
|
|
257
|
+
});
|
|
258
|
+
messages.push({ role: "system", content: systemPrompt });
|
|
259
|
+
}
|
|
260
|
+
const userSequence = (await this.aiSessionManager.getLastSequence(session.id)) + 1;
|
|
261
|
+
await this.aiSessionManager.addMessage({
|
|
262
|
+
aiSessionId: session.id,
|
|
263
|
+
sequence: userSequence,
|
|
264
|
+
role: "user",
|
|
265
|
+
content: userPrompt,
|
|
266
|
+
});
|
|
267
|
+
messages.push({ role: "user", content: userPrompt });
|
|
268
|
+
let iterations = 0;
|
|
269
|
+
const maxIterations = this.config.maxIterations ?? 5;
|
|
270
|
+
const iterationTimeout = this.config.iterationTimeout ?? 30000;
|
|
271
|
+
let lastErrorMessage = "";
|
|
272
|
+
while (iterations < maxIterations) {
|
|
273
|
+
iterations++;
|
|
274
|
+
const controller = new AbortController();
|
|
275
|
+
const timeout = setTimeout(() => controller.abort(), iterationTimeout);
|
|
276
|
+
try {
|
|
277
|
+
const buildRequestBody = (stream) => {
|
|
278
|
+
const requestBody = {
|
|
279
|
+
model: this.resolveModel(),
|
|
280
|
+
messages,
|
|
281
|
+
tools: [toolSchema],
|
|
282
|
+
tool_choice: this.config.forceToolChoice === false ? "auto" : "required",
|
|
283
|
+
};
|
|
284
|
+
if (this.config.memoryTemperature !== false) {
|
|
285
|
+
requestBody.temperature = this.config.memoryTemperature ?? 0.3;
|
|
286
|
+
}
|
|
287
|
+
if (this.config.extraParams) {
|
|
288
|
+
applySafeExtraParams(requestBody, this.config.extraParams);
|
|
289
|
+
}
|
|
290
|
+
if (stream) {
|
|
291
|
+
requestBody.stream = true;
|
|
292
|
+
}
|
|
293
|
+
return requestBody;
|
|
294
|
+
};
|
|
295
|
+
const headers = {
|
|
296
|
+
"Content-Type": "application/json",
|
|
297
|
+
};
|
|
298
|
+
if (this.config.apiKey) {
|
|
299
|
+
headers.Authorization = `Bearer ${this.config.apiKey}`;
|
|
300
|
+
}
|
|
301
|
+
const postCompletion = async (stream) => {
|
|
302
|
+
const response = await fetch(`${this.resolveEndpoint()}/chat/completions`, {
|
|
303
|
+
method: "POST",
|
|
304
|
+
headers,
|
|
305
|
+
body: JSON.stringify(buildRequestBody(stream)),
|
|
306
|
+
signal: controller.signal,
|
|
307
|
+
});
|
|
308
|
+
return response;
|
|
309
|
+
};
|
|
310
|
+
let usedStream = false;
|
|
311
|
+
let response = await postCompletion(false);
|
|
312
|
+
if (!response.ok) {
|
|
313
|
+
const errorText = await response.text().catch(() => response.statusText);
|
|
314
|
+
if (isStreamRequiredError(response.status, errorText)) {
|
|
315
|
+
log("OpenAI Chat Completion API requires streaming; retrying with stream:true", {
|
|
316
|
+
provider: this.getProviderName(),
|
|
317
|
+
model: this.config.model,
|
|
318
|
+
status: response.status,
|
|
319
|
+
error: errorText,
|
|
320
|
+
iteration: iterations,
|
|
321
|
+
});
|
|
322
|
+
usedStream = true;
|
|
323
|
+
response = await postCompletion(true);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
clearTimeout(timeout);
|
|
327
|
+
log("OpenAI Chat Completion API error", {
|
|
328
|
+
provider: this.getProviderName(),
|
|
329
|
+
model: this.config.model,
|
|
330
|
+
status: response.status,
|
|
331
|
+
error: errorText,
|
|
332
|
+
iteration: iterations,
|
|
333
|
+
});
|
|
334
|
+
let errorMessage = `API error: ${response.status} - ${errorText}`;
|
|
335
|
+
if (response.status === 400 &&
|
|
336
|
+
errorText.includes("unsupported_value") &&
|
|
337
|
+
errorText.includes("temperature")) {
|
|
338
|
+
errorMessage =
|
|
339
|
+
'Your model does not support the temperature parameter. Add "memoryTemperature": false to your config file to disable it.';
|
|
340
|
+
}
|
|
341
|
+
else if (response.status === 400 &&
|
|
342
|
+
/tool_choice/i.test(errorText) &&
|
|
343
|
+
(/Thinking mode does not support/i.test(errorText) ||
|
|
344
|
+
/unsupported/i.test(errorText) ||
|
|
345
|
+
/not support/i.test(errorText))) {
|
|
346
|
+
errorMessage =
|
|
347
|
+
'Your model does not support tool_choice "required". Add "forceToolChoice": false to your config file to fall back to "auto".';
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
success: false,
|
|
351
|
+
error: errorMessage,
|
|
352
|
+
iterations,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
clearTimeout(timeout);
|
|
357
|
+
if (!response.ok) {
|
|
358
|
+
const errorText = await response.text().catch(() => response.statusText);
|
|
359
|
+
log("OpenAI Chat Completion API error", {
|
|
360
|
+
provider: this.getProviderName(),
|
|
361
|
+
model: this.config.model,
|
|
362
|
+
status: response.status,
|
|
363
|
+
error: errorText,
|
|
364
|
+
iteration: iterations,
|
|
365
|
+
streamed: usedStream,
|
|
366
|
+
});
|
|
367
|
+
return {
|
|
368
|
+
success: false,
|
|
369
|
+
error: `API error: ${response.status} - ${errorText}`,
|
|
370
|
+
iterations,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
const data = usedStream
|
|
374
|
+
? await parseChatCompletionStream(response)
|
|
375
|
+
: await response.json();
|
|
376
|
+
if (isErrorResponseBody(data)) {
|
|
377
|
+
log("API returned error in response body", {
|
|
378
|
+
provider: this.getProviderName(),
|
|
379
|
+
model: this.config.model,
|
|
380
|
+
status: data.status,
|
|
381
|
+
msg: data.msg,
|
|
382
|
+
});
|
|
383
|
+
return {
|
|
384
|
+
success: false,
|
|
385
|
+
error: `API error: ${data.status} - ${data.msg}`,
|
|
386
|
+
iterations,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
if (!hasNonEmptyChoices(data)) {
|
|
390
|
+
const choices = typeof data === "object" && data !== null
|
|
391
|
+
? data.choices
|
|
392
|
+
: undefined;
|
|
393
|
+
log("Invalid API response format", {
|
|
394
|
+
provider: this.getProviderName(),
|
|
395
|
+
model: this.config.model,
|
|
396
|
+
response: JSON.stringify(data).slice(0, 1000),
|
|
397
|
+
hasChoices: Array.isArray(choices),
|
|
398
|
+
choicesLength: Array.isArray(choices) ? choices.length : undefined,
|
|
399
|
+
});
|
|
400
|
+
return {
|
|
401
|
+
success: false,
|
|
402
|
+
error: "Invalid API response format",
|
|
403
|
+
iterations,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
const choice = data.choices[0];
|
|
407
|
+
if (!choice) {
|
|
408
|
+
return {
|
|
409
|
+
success: false,
|
|
410
|
+
error: "Invalid API response format",
|
|
411
|
+
iterations,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
const assistantSequence = (await this.aiSessionManager.getLastSequence(session.id)) + 1;
|
|
415
|
+
const assistantMsg = {
|
|
416
|
+
aiSessionId: session.id,
|
|
417
|
+
sequence: assistantSequence,
|
|
418
|
+
role: "assistant",
|
|
419
|
+
content: choice.message.content ?? "",
|
|
420
|
+
};
|
|
421
|
+
if (choice.message.tool_calls) {
|
|
422
|
+
assistantMsg.toolCalls = choice.message.tool_calls;
|
|
423
|
+
}
|
|
424
|
+
await this.aiSessionManager.addMessage(assistantMsg);
|
|
425
|
+
messages.push({
|
|
426
|
+
role: "assistant",
|
|
427
|
+
content: choice.message.content ?? null,
|
|
428
|
+
tool_calls: choice.message.tool_calls,
|
|
429
|
+
});
|
|
430
|
+
if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
|
|
431
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
432
|
+
const toolCallId = toolCall.id;
|
|
433
|
+
if (toolCall.function.name === toolSchema.function.name) {
|
|
434
|
+
try {
|
|
435
|
+
const parsed = (() => {
|
|
436
|
+
const raw = toolCall.function.arguments;
|
|
437
|
+
if (typeof raw !== "string") {
|
|
438
|
+
return JSON.parse(JSON.stringify(raw));
|
|
439
|
+
}
|
|
440
|
+
try {
|
|
441
|
+
return JSON.parse(raw);
|
|
442
|
+
}
|
|
443
|
+
catch (e1) {
|
|
444
|
+
const fixed = extractFirstJSON(raw);
|
|
445
|
+
if (fixed) {
|
|
446
|
+
try {
|
|
447
|
+
return JSON.parse(fixed);
|
|
448
|
+
}
|
|
449
|
+
catch {
|
|
450
|
+
const repaired = repairInnerQuotes(fixed);
|
|
451
|
+
if (repaired !== fixed) {
|
|
452
|
+
try {
|
|
453
|
+
return JSON.parse(repaired);
|
|
454
|
+
}
|
|
455
|
+
catch {
|
|
456
|
+
// ignore and fall through to the original parse error
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
throw e1;
|
|
462
|
+
}
|
|
463
|
+
})();
|
|
464
|
+
const result = UserProfileValidator.validate(parsed);
|
|
465
|
+
if (!result.valid) {
|
|
466
|
+
throw new Error(result.errors.join(", "));
|
|
467
|
+
}
|
|
468
|
+
await this.addToolResponse(session.id, messages, toolCallId, JSON.stringify({ success: true }));
|
|
469
|
+
return {
|
|
470
|
+
success: true,
|
|
471
|
+
data: result.data,
|
|
472
|
+
iterations,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
catch (validationError) {
|
|
476
|
+
const errorStack = validationError instanceof Error ? validationError.stack : undefined;
|
|
477
|
+
log("OpenAI tool response validation failed", {
|
|
478
|
+
error: String(validationError),
|
|
479
|
+
stack: errorStack,
|
|
480
|
+
errorType: validationError instanceof Error
|
|
481
|
+
? validationError.constructor.name
|
|
482
|
+
: typeof validationError,
|
|
483
|
+
toolName: toolSchema.function.name,
|
|
484
|
+
iteration: iterations,
|
|
485
|
+
rawArguments: toolCall.function.arguments.slice(0, 500),
|
|
486
|
+
});
|
|
487
|
+
const errorMessage = `Validation failed: ${String(validationError)}`;
|
|
488
|
+
lastErrorMessage = errorMessage;
|
|
489
|
+
await this.addToolResponse(session.id, messages, toolCallId, JSON.stringify({ success: false, error: errorMessage }));
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
const wrongToolMessage = `Wrong tool called. Please use ${toolSchema.function.name} instead.`;
|
|
494
|
+
await this.addToolResponse(session.id, messages, toolCallId, JSON.stringify({ success: false, error: wrongToolMessage }));
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
const retrySequence = (await this.aiSessionManager.getLastSequence(session.id)) + 1;
|
|
499
|
+
const retryPrompt = lastErrorMessage
|
|
500
|
+
? `Your previous attempt failed. Error: ${lastErrorMessage}. Please fix the JSON in your tool call arguments and try again. Output ONLY valid JSON, no extra text outside the JSON structure.`
|
|
501
|
+
: "Please use the tool to extract and save the data as instructed.";
|
|
502
|
+
await this.aiSessionManager.addMessage({
|
|
503
|
+
aiSessionId: session.id,
|
|
504
|
+
sequence: retrySequence,
|
|
505
|
+
role: "user",
|
|
506
|
+
content: retryPrompt,
|
|
507
|
+
});
|
|
508
|
+
messages.push({ role: "user", content: retryPrompt });
|
|
509
|
+
}
|
|
510
|
+
catch (error) {
|
|
511
|
+
clearTimeout(timeout);
|
|
512
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
513
|
+
return {
|
|
514
|
+
success: false,
|
|
515
|
+
error: `API request timeout (${iterationTimeout}ms)`,
|
|
516
|
+
iterations,
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
return {
|
|
520
|
+
success: false,
|
|
521
|
+
error: String(error),
|
|
522
|
+
iterations,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return {
|
|
527
|
+
success: false,
|
|
528
|
+
error: `Max iterations (${maxIterations}) reached without tool call`,
|
|
529
|
+
iterations,
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseAIProvider, type ToolCallResult } from "./base-provider.js";
|
|
2
|
+
import { AISessionManager } from "../session/ai-session-manager.js";
|
|
3
|
+
import { type ChatCompletionTool } from "../tools/tool-schema.js";
|
|
4
|
+
export declare class OpenAIResponsesProvider extends BaseAIProvider {
|
|
5
|
+
private aiSessionManager;
|
|
6
|
+
constructor(config: any, aiSessionManager: AISessionManager);
|
|
7
|
+
getProviderName(): string;
|
|
8
|
+
supportsSession(): boolean;
|
|
9
|
+
executeToolCall(systemPrompt: string, userPrompt: string, toolSchema: ChatCompletionTool, sessionId: string): Promise<ToolCallResult>;
|
|
10
|
+
private extractToolCall;
|
|
11
|
+
private buildRetryPrompt;
|
|
12
|
+
private validateResponse;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=openai-responses.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-responses.d.ts","sourceRoot":"","sources":["../../../../src/services/ai/providers/openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,cAAc,EAAwB,MAAM,oBAAoB,CAAC;AAC/F,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAuB,KAAK,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAsBvF,qBAAa,uBAAwB,SAAQ,cAAc;IACzD,OAAO,CAAC,gBAAgB,CAAmB;gBAE/B,MAAM,EAAE,GAAG,EAAE,gBAAgB,EAAE,gBAAgB;IAK3D,eAAe,IAAI,MAAM;IAIzB,eAAe,IAAI,OAAO;IAIpB,eAAe,CACnB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,kBAAkB,EAC9B,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,cAAc,CAAC;IA0H1B,OAAO,CAAC,eAAe;IA+BvB,OAAO,CAAC,gBAAgB;IAgBxB,OAAO,CAAC,gBAAgB;CAsBzB"}
|