@resolveio/server-lib 22.3.220 → 22.3.221
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/.nodemon.json +5 -0
- package/.vscode/settings.json +21 -0
- package/AGENTS.md +195 -0
- package/README.md +22 -0
- package/build_package.sh +5 -0
- package/compileDTS.pl +64 -0
- package/docs/ai-assistant-nightly-eval.md +65 -0
- package/docs/ai-assistant-preflight-checklist.md +23 -0
- package/docs/ai-assistant-report-builder-bridge-playbook.md +115 -0
- package/eslint-plugin-custom/index.js +7 -0
- package/eslint-plugin-custom/rules/no-filter-zero-index.js +44 -0
- package/eslint.config.js +103 -0
- package/gulpfile.js +216 -0
- package/methodAndPublicationListGenerator.py +375 -0
- package/mongodbensurers.js +2 -0
- package/mongostop.js +3 -0
- package/package.json +1 -1
- package/scripts/cleanup-bypassed-callmethod-logs.js +616 -0
- package/settings.development.json +25 -0
- package/settings.development.redacted.json +25 -0
- package/src/.env +12 -0
- package/src/ai/assistant-core-heuristics.ts +379 -0
- package/src/ai/resolveio-platform-intelligence-memory-corpus.ts +185 -0
- package/src/ai/resolveio-platform-intelligence-memory.ts +325 -0
- package/{ai/resolveio-platform-intelligence-types.d.ts → src/ai/resolveio-platform-intelligence-types.ts} +20 -15
- package/src/ai/resolveio-platform-intelligence.ts +462 -0
- package/src/client-server-app.ts +12 -0
- package/src/collections/ai-run.collection.ts +117 -0
- package/src/collections/ai-terminal-conversation.collection.ts +91 -0
- package/src/collections/ai-terminal-issue-report.collection.ts +99 -0
- package/src/collections/ai-terminal-message.collection.ts +77 -0
- package/src/collections/app-setting.collection.ts +104 -0
- package/src/collections/app-status.collection.ts +58 -0
- package/src/collections/communication-metric.collection.ts +84 -0
- package/src/collections/counter.collection.ts +56 -0
- package/src/collections/cron-job-history.collection.ts +94 -0
- package/src/collections/cron-job.collection.ts +92 -0
- package/src/collections/customer-notification.collection.ts +131 -0
- package/src/collections/customer-portal-password.collection.ts +76 -0
- package/src/collections/email-history.collection.ts +134 -0
- package/src/collections/email-verified.collection.ts +62 -0
- package/src/collections/file.collection.ts +74 -0
- package/src/collections/flag-update.collection.ts +57 -0
- package/src/collections/flag.collection.ts +57 -0
- package/src/collections/log-method-latency.collection.ts +77 -0
- package/src/collections/log-subscription.collection.ts +80 -0
- package/src/collections/log.collection.ts +93 -0
- package/src/collections/logged-in-users.collection.ts +67 -0
- package/src/collections/monitor-cpu.collection.ts +65 -0
- package/src/collections/monitor-function.collection.ts +74 -0
- package/src/collections/monitor-memory.collection.ts +77 -0
- package/src/collections/monitor-mongo.collection.ts +71 -0
- package/src/collections/notification.collection.ts +57 -0
- package/src/collections/openai-usage-ledger.collection.ts +131 -0
- package/src/collections/report-builder-dashboard-builder.collection.ts +109 -0
- package/src/collections/report-builder-library.collection.ts +89 -0
- package/src/collections/report-builder-report.collection.ts +184 -0
- package/src/collections/user-group.collection.ts +89 -0
- package/src/collections/user-guide.collection.ts +57 -0
- package/src/collections/user.collection.ts +181 -0
- package/src/cron/cron.ts +117 -0
- package/src/fixtures/cron-jobs.ts +95 -0
- package/src/fixtures/init.ts +35 -0
- package/src/http/auth.ts +818 -0
- package/src/http/health.ts +7 -0
- package/src/http/home.ts +90 -0
- package/src/http/slow-query-publication.ts +49 -0
- package/src/index.ts +1 -0
- package/src/managers/ai-assistant-codex-manager.manager.ts +1131 -0
- package/src/managers/ai-run-evidence.manager.ts +264 -0
- package/src/managers/communication-metric.manager.ts +82 -0
- package/src/managers/cron.manager.ts +333 -0
- package/src/managers/customer-notification-content.manager.ts +236 -0
- package/src/managers/diagnostic-manager-bootstrap.ts +165 -0
- package/src/managers/error-auto-fix.manager.ts +2767 -0
- package/src/managers/local-log.manager.ts +113 -0
- package/src/managers/method.manager.ts +1857 -0
- package/src/managers/mongo.manager.ts +4575 -0
- package/src/managers/monitor.manager.ts +507 -0
- package/src/managers/openai-usage-ledger.manager.ts +112 -0
- package/src/managers/slow-query-verifier.manager.ts +3590 -0
- package/src/managers/slow-query.manager.ts +519 -0
- package/src/managers/subscription.manager.ts +3128 -0
- package/src/managers/websocket.manager.ts +746 -0
- package/src/managers/worker-dispatcher.manager.ts +1360 -0
- package/src/managers/worker-server.manager.ts +536 -0
- package/src/methods/accounts.ts +532 -0
- package/src/methods/ai-terminal.ts +29070 -0
- package/src/methods/app-settings.ts +114 -0
- package/src/methods/aws.ts +649 -0
- package/src/methods/collections.ts +641 -0
- package/src/methods/counters.ts +69 -0
- package/src/methods/cron-jobs.ts +2614 -0
- package/src/methods/customer-notifications.ts +458 -0
- package/src/methods/diagnostics.ts +616 -0
- package/src/methods/flag-updates.ts +7 -0
- package/src/methods/flags.ts +7 -0
- package/src/methods/logs.ts +657 -0
- package/src/methods/mongo-explorer.ts +1880 -0
- package/src/methods/monitor.ts +540 -0
- package/src/methods/pdf.ts +1236 -0
- package/src/methods/publications.ts +129 -0
- package/src/methods/report-builder.ts +3300 -0
- package/src/methods/support.ts +335 -0
- package/src/models/ai-run.model.ts +27 -0
- package/src/models/ai-terminal-conversation.model.ts +19 -0
- package/src/models/ai-terminal-issue-report.model.ts +21 -0
- package/src/models/ai-terminal-message.model.ts +24 -0
- package/src/models/app-setting.model.ts +17 -0
- package/{models/app-status.model.d.ts → src/models/app-status.model.ts} +3 -2
- package/{models/billing-logged-in-users.model.d.ts → src/models/billing-logged-in-users.model.ts} +5 -4
- package/src/models/collection-document.model.ts +24 -0
- package/src/models/communication-metric.model.ts +23 -0
- package/{models/counter.model.d.ts → src/models/counter.model.ts} +4 -3
- package/src/models/cron-job-history.model.ts +16 -0
- package/src/models/cron-job.model.ts +15 -0
- package/src/models/customer-notification.model.ts +28 -0
- package/src/models/customer-portal-password.model.ts +12 -0
- package/src/models/dialog.model.ts +25 -0
- package/{models/email-history.model.js → src/models/email-history.model.ts} +36 -4
- package/{models/email-verified.model.d.ts → src/models/email-verified.model.ts} +6 -5
- package/{models/file.model.d.ts → src/models/file.model.ts} +8 -7
- package/{models/flag-update.model.d.ts → src/models/flag-update.model.ts} +4 -3
- package/{models/flag.model.d.ts → src/models/flag.model.ts} +4 -3
- package/src/models/log-method-latency.model.ts +11 -0
- package/{models/log-subscription.model.d.ts → src/models/log-subscription.model.ts} +11 -9
- package/src/models/log.model.ts +19 -0
- package/{models/logged-in-users.model.d.ts → src/models/logged-in-users.model.ts} +6 -5
- package/{models/method-response.model.d.ts → src/models/method-response.model.ts} +7 -6
- package/src/models/method.model.ts +25 -0
- package/{models/monitor-cpu.model.d.ts → src/models/monitor-cpu.model.ts} +9 -7
- package/src/models/monitor-function.model.ts +16 -0
- package/src/models/monitor-memory.model.ts +17 -0
- package/src/models/monitor-mongo.model.ts +15 -0
- package/{models/notification.model.d.ts → src/models/notification.model.ts} +6 -4
- package/src/models/openai-usage-ledger.model.ts +56 -0
- package/src/models/pagination.model.ts +35 -0
- package/src/models/permission.model.ts +14 -0
- package/src/models/report-builder-dashboard-builder.model.ts +29 -0
- package/src/models/report-builder-library.model.ts +20 -0
- package/src/models/report-builder-report.model.ts +136 -0
- package/src/models/report-builder.model.ts +68 -0
- package/src/models/select-data-label.model.ts +9 -0
- package/src/models/server-message.model.ts +31 -0
- package/src/models/slow-query-report.model.ts +23 -0
- package/src/models/subscription.model.ts +73 -0
- package/src/models/support-ticket.model.ts +104 -0
- package/src/models/user-group.model.ts +24 -0
- package/{models/user-guide.model.d.ts → src/models/user-guide.model.ts} +5 -4
- package/src/models/user.model.ts +96 -0
- package/src/private/images/ResolveIO.png +0 -0
- package/src/publications/ai-terminal.ts +73 -0
- package/src/publications/app-settings.ts +25 -0
- package/src/publications/app-status.ts +13 -0
- package/src/publications/cron-jobs.ts +40 -0
- package/src/publications/customer-notifications.ts +101 -0
- package/src/publications/files.ts +33 -0
- package/src/publications/flags-update.ts +19 -0
- package/src/publications/flags.ts +19 -0
- package/src/publications/logs.ts +163 -0
- package/src/publications/notifications.ts +13 -0
- package/src/publications/report-builder-dashboard-builders.ts +39 -0
- package/src/publications/report-builder-libraries.ts +41 -0
- package/src/publications/report-builder-reports.ts +47 -0
- package/src/publications/super-admin.ts +13 -0
- package/src/publications/user-groups.ts +12 -0
- package/src/publications/user-guides.ts +12 -0
- package/src/resolveio-server-app.ts +617 -0
- package/src/server-app.ts +3354 -0
- package/src/services/codex-client.ts +1231 -0
- package/src/services/openai-client.ts +265 -0
- package/src/types/error-report.ts +26 -0
- package/src/types/js-tiktoken.d.ts +11 -0
- package/src/types/slow-query-report.ts +28 -0
- package/src/util/ai-qa-policy.ts +925 -0
- package/src/util/ai-run-evidence-adapters.ts +8347 -0
- package/src/util/ai-run-evidence-dashboard.ts +323 -0
- package/src/util/ai-run-evidence-eval.ts +1057 -0
- package/src/util/ai-run-evidence.ts +1430 -0
- package/src/util/ai-runner-artifacts.ts +586 -0
- package/src/util/ai-runner-manager-autopilot.ts +961 -0
- package/src/util/ai-runner-manager-policy.ts +5011 -0
- package/src/util/ai-runner-qa-auth.ts +838 -0
- package/src/util/ai-runner-qa-tools.ts +3536 -0
- package/src/util/aicoder-runner-v6.ts +3121 -0
- package/src/util/common.ts +649 -0
- package/src/util/customer-portal-password.ts +183 -0
- package/src/util/error-reporter.ts +332 -0
- package/src/util/error-tracking.ts +79 -0
- package/src/util/openai-usage-cost.ts +114 -0
- package/src/util/report-builder-unwinds.ts +180 -0
- package/src/util/runner-process-janitor.ts +219 -0
- package/src/util/schema-report-builder.ts +448 -0
- package/src/util/slow-query-reporter.ts +216 -0
- package/src/util/subscription-dependency-context.ts +1096 -0
- package/src/util/support-runner-v5.ts +10040 -0
- package/src/util/tokenizer.ts +38 -0
- package/src/workers/codex-runner.worker.ts +142 -0
- package/start_server.sh +5 -0
- package/tests/ai-assistant-corpus-build.ts +484 -0
- package/tests/ai-assistant-corpus-replay-e2e.ts +774 -0
- package/tests/ai-assistant-data-parity-e2e.ts +1989 -0
- package/tests/ai-assistant-eval-triage.ts +831 -0
- package/tests/ai-assistant-openai-e2e.ts +1061 -0
- package/tests/ai-assistant-openai-git-e2e.ts +155 -0
- package/tests/ai-assistant-preflight-matrix.ts +215 -0
- package/tests/ai-assistant-routing-eval.test.ts +585 -0
- package/tests/ai-assistant-snf-live-eval.ts +975 -0
- package/tests/ai-assistant-utils.test.ts +4834 -0
- package/tests/ai-manager-autopilot-snapshot.test.ts +193 -0
- package/tests/ai-manager-recovery-checkpoint.test.ts +1383 -0
- package/tests/ai-run-eval.test.ts +132 -0
- package/tests/ai-run-evidence.test.ts +3773 -0
- package/tests/ai-runner-contract.test.ts +515 -0
- package/tests/aicoder-runner-v6.test.ts +822 -0
- package/tests/error-reporter.test.ts +145 -0
- package/tests/method-publication-generator.test.ts +46 -0
- package/tests/report-builder-linking.test.ts +79 -0
- package/tests/resolveio-platform-intelligence.test.ts +352 -0
- package/tests/server-app-cron-owner.test.ts +127 -0
- package/tests/subscription-connect-race.test.ts +158 -0
- package/tests/subscription-dependency-context.test.ts +324 -0
- package/tests/subscription-manager-collection-tracking.test.ts +86 -0
- package/tests/subscription-manager-invalidation.test.ts +86 -0
- package/tests/support-runner-v5.test.ts +3201 -0
- package/tsconfig.json +34 -0
- package/ai/assistant-core-heuristics.d.ts +0 -11
- package/ai/assistant-core-heuristics.js +0 -356
- package/ai/assistant-core-heuristics.js.map +0 -1
- package/ai/resolveio-platform-intelligence-memory-corpus.d.ts +0 -3
- package/ai/resolveio-platform-intelligence-memory-corpus.js +0 -214
- package/ai/resolveio-platform-intelligence-memory-corpus.js.map +0 -1
- package/ai/resolveio-platform-intelligence-memory.d.ts +0 -20
- package/ai/resolveio-platform-intelligence-memory.js +0 -341
- package/ai/resolveio-platform-intelligence-memory.js.map +0 -1
- package/ai/resolveio-platform-intelligence-types.js +0 -4
- package/ai/resolveio-platform-intelligence-types.js.map +0 -1
- package/ai/resolveio-platform-intelligence.d.ts +0 -6
- package/ai/resolveio-platform-intelligence.js +0 -463
- package/ai/resolveio-platform-intelligence.js.map +0 -1
- package/client-server-app.d.ts +0 -1
- package/client-server-app.js +0 -68
- package/client-server-app.js.map +0 -1
- package/collections/ai-run.collection.d.ts +0 -3
- package/collections/ai-run.collection.js +0 -170
- package/collections/ai-run.collection.js.map +0 -1
- package/collections/ai-terminal-conversation.collection.d.ts +0 -2
- package/collections/ai-terminal-conversation.collection.js +0 -140
- package/collections/ai-terminal-conversation.collection.js.map +0 -1
- package/collections/ai-terminal-issue-report.collection.d.ts +0 -2
- package/collections/ai-terminal-issue-report.collection.js +0 -148
- package/collections/ai-terminal-issue-report.collection.js.map +0 -1
- package/collections/ai-terminal-message.collection.d.ts +0 -2
- package/collections/ai-terminal-message.collection.js +0 -121
- package/collections/ai-terminal-message.collection.js.map +0 -1
- package/collections/app-setting.collection.d.ts +0 -3
- package/collections/app-setting.collection.js +0 -103
- package/collections/app-setting.collection.js.map +0 -1
- package/collections/app-status.collection.d.ts +0 -3
- package/collections/app-status.collection.js +0 -57
- package/collections/app-status.collection.js.map +0 -1
- package/collections/communication-metric.collection.d.ts +0 -2
- package/collections/communication-metric.collection.js +0 -133
- package/collections/communication-metric.collection.js.map +0 -1
- package/collections/counter.collection.d.ts +0 -3
- package/collections/counter.collection.js +0 -56
- package/collections/counter.collection.js.map +0 -1
- package/collections/cron-job-history.collection.d.ts +0 -3
- package/collections/cron-job-history.collection.js +0 -137
- package/collections/cron-job-history.collection.js.map +0 -1
- package/collections/cron-job.collection.d.ts +0 -3
- package/collections/cron-job.collection.js +0 -92
- package/collections/cron-job.collection.js.map +0 -1
- package/collections/customer-notification.collection.d.ts +0 -3
- package/collections/customer-notification.collection.js +0 -130
- package/collections/customer-notification.collection.js.map +0 -1
- package/collections/customer-portal-password.collection.d.ts +0 -3
- package/collections/customer-portal-password.collection.js +0 -75
- package/collections/customer-portal-password.collection.js.map +0 -1
- package/collections/email-history.collection.d.ts +0 -3
- package/collections/email-history.collection.js +0 -134
- package/collections/email-history.collection.js.map +0 -1
- package/collections/email-verified.collection.d.ts +0 -3
- package/collections/email-verified.collection.js +0 -62
- package/collections/email-verified.collection.js.map +0 -1
- package/collections/file.collection.d.ts +0 -3
- package/collections/file.collection.js +0 -74
- package/collections/file.collection.js.map +0 -1
- package/collections/flag-update.collection.d.ts +0 -3
- package/collections/flag-update.collection.js +0 -57
- package/collections/flag-update.collection.js.map +0 -1
- package/collections/flag.collection.d.ts +0 -3
- package/collections/flag.collection.js +0 -57
- package/collections/flag.collection.js.map +0 -1
- package/collections/log-method-latency.collection.d.ts +0 -3
- package/collections/log-method-latency.collection.js +0 -77
- package/collections/log-method-latency.collection.js.map +0 -1
- package/collections/log-subscription.collection.d.ts +0 -3
- package/collections/log-subscription.collection.js +0 -80
- package/collections/log-subscription.collection.js.map +0 -1
- package/collections/log.collection.d.ts +0 -3
- package/collections/log.collection.js +0 -93
- package/collections/log.collection.js.map +0 -1
- package/collections/logged-in-users.collection.d.ts +0 -3
- package/collections/logged-in-users.collection.js +0 -67
- package/collections/logged-in-users.collection.js.map +0 -1
- package/collections/monitor-cpu.collection.d.ts +0 -3
- package/collections/monitor-cpu.collection.js +0 -65
- package/collections/monitor-cpu.collection.js.map +0 -1
- package/collections/monitor-function.collection.d.ts +0 -3
- package/collections/monitor-function.collection.js +0 -74
- package/collections/monitor-function.collection.js.map +0 -1
- package/collections/monitor-memory.collection.d.ts +0 -3
- package/collections/monitor-memory.collection.js +0 -77
- package/collections/monitor-memory.collection.js.map +0 -1
- package/collections/monitor-mongo.collection.d.ts +0 -3
- package/collections/monitor-mongo.collection.js +0 -71
- package/collections/monitor-mongo.collection.js.map +0 -1
- package/collections/notification.collection.d.ts +0 -3
- package/collections/notification.collection.js +0 -57
- package/collections/notification.collection.js.map +0 -1
- package/collections/openai-usage-ledger.collection.d.ts +0 -2
- package/collections/openai-usage-ledger.collection.js +0 -188
- package/collections/openai-usage-ledger.collection.js.map +0 -1
- package/collections/report-builder-dashboard-builder.collection.d.ts +0 -3
- package/collections/report-builder-dashboard-builder.collection.js +0 -109
- package/collections/report-builder-dashboard-builder.collection.js.map +0 -1
- package/collections/report-builder-library.collection.d.ts +0 -3
- package/collections/report-builder-library.collection.js +0 -87
- package/collections/report-builder-library.collection.js.map +0 -1
- package/collections/report-builder-report.collection.d.ts +0 -4
- package/collections/report-builder-report.collection.js +0 -184
- package/collections/report-builder-report.collection.js.map +0 -1
- package/collections/user-group.collection.d.ts +0 -4
- package/collections/user-group.collection.js +0 -89
- package/collections/user-group.collection.js.map +0 -1
- package/collections/user-guide.collection.d.ts +0 -3
- package/collections/user-guide.collection.js +0 -57
- package/collections/user-guide.collection.js.map +0 -1
- package/collections/user.collection.d.ts +0 -4
- package/collections/user.collection.js +0 -180
- package/collections/user.collection.js.map +0 -1
- package/cron/cron.d.ts +0 -14
- package/cron/cron.js +0 -216
- package/cron/cron.js.map +0 -1
- package/fixtures/cron-jobs.d.ts +0 -1
- package/fixtures/cron-jobs.js +0 -150
- package/fixtures/cron-jobs.js.map +0 -1
- package/fixtures/init.d.ts +0 -1
- package/fixtures/init.js +0 -91
- package/fixtures/init.js.map +0 -1
- package/http/auth.d.ts +0 -2
- package/http/auth.js +0 -951
- package/http/auth.js.map +0 -1
- package/http/health.d.ts +0 -1
- package/http/health.js +0 -11
- package/http/health.js.map +0 -1
- package/http/home.d.ts +0 -1
- package/http/home.js +0 -134
- package/http/home.js.map +0 -1
- package/http/slow-query-publication.d.ts +0 -2
- package/http/slow-query-publication.js +0 -99
- package/http/slow-query-publication.js.map +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -19
- package/index.js.map +0 -1
- package/managers/ai-assistant-codex-manager.manager.d.ts +0 -67
- package/managers/ai-assistant-codex-manager.manager.js +0 -1113
- package/managers/ai-assistant-codex-manager.manager.js.map +0 -1
- package/managers/ai-run-evidence.manager.d.ts +0 -36
- package/managers/ai-run-evidence.manager.js +0 -377
- package/managers/ai-run-evidence.manager.js.map +0 -1
- package/managers/communication-metric.manager.d.ts +0 -16
- package/managers/communication-metric.manager.js +0 -134
- package/managers/communication-metric.manager.js.map +0 -1
- package/managers/cron.manager.d.ts +0 -20
- package/managers/cron.manager.js +0 -534
- package/managers/cron.manager.js.map +0 -1
- package/managers/customer-notification-content.manager.d.ts +0 -55
- package/managers/customer-notification-content.manager.js +0 -158
- package/managers/customer-notification-content.manager.js.map +0 -1
- package/managers/diagnostic-manager-bootstrap.d.ts +0 -9
- package/managers/diagnostic-manager-bootstrap.js +0 -260
- package/managers/diagnostic-manager-bootstrap.js.map +0 -1
- package/managers/error-auto-fix.manager.d.ts +0 -149
- package/managers/error-auto-fix.manager.js +0 -3064
- package/managers/error-auto-fix.manager.js.map +0 -1
- package/managers/local-log.manager.d.ts +0 -18
- package/managers/local-log.manager.js +0 -88
- package/managers/local-log.manager.js.map +0 -1
- package/managers/method.manager.d.ts +0 -84
- package/managers/method.manager.js +0 -1964
- package/managers/method.manager.js.map +0 -1
- package/managers/mongo.manager.d.ts +0 -224
- package/managers/mongo.manager.js +0 -5000
- package/managers/mongo.manager.js.map +0 -1
- package/managers/monitor.manager.d.ts +0 -70
- package/managers/monitor.manager.js +0 -550
- package/managers/monitor.manager.js.map +0 -1
- package/managers/openai-usage-ledger.manager.d.ts +0 -30
- package/managers/openai-usage-ledger.manager.js +0 -142
- package/managers/openai-usage-ledger.manager.js.map +0 -1
- package/managers/slow-query-verifier.manager.d.ts +0 -144
- package/managers/slow-query-verifier.manager.js +0 -3857
- package/managers/slow-query-verifier.manager.js.map +0 -1
- package/managers/slow-query.manager.d.ts +0 -28
- package/managers/slow-query.manager.js +0 -468
- package/managers/slow-query.manager.js.map +0 -1
- package/managers/subscription.manager.d.ts +0 -169
- package/managers/subscription.manager.js +0 -3434
- package/managers/subscription.manager.js.map +0 -1
- package/managers/websocket.manager.d.ts +0 -73
- package/managers/websocket.manager.js +0 -673
- package/managers/websocket.manager.js.map +0 -1
- package/managers/worker-dispatcher.manager.d.ts +0 -120
- package/managers/worker-dispatcher.manager.js +0 -1266
- package/managers/worker-dispatcher.manager.js.map +0 -1
- package/managers/worker-server.manager.d.ts +0 -35
- package/managers/worker-server.manager.js +0 -582
- package/managers/worker-server.manager.js.map +0 -1
- package/methods/accounts.d.ts +0 -2
- package/methods/accounts.js +0 -624
- package/methods/accounts.js.map +0 -1
- package/methods/ai-terminal.d.ts +0 -458
- package/methods/ai-terminal.js +0 -27991
- package/methods/ai-terminal.js.map +0 -1
- package/methods/app-settings.d.ts +0 -2
- package/methods/app-settings.js +0 -169
- package/methods/app-settings.js.map +0 -1
- package/methods/aws.d.ts +0 -2
- package/methods/aws.js +0 -877
- package/methods/aws.js.map +0 -1
- package/methods/collections.d.ts +0 -2
- package/methods/collections.js +0 -719
- package/methods/collections.js.map +0 -1
- package/methods/counters.d.ts +0 -2
- package/methods/counters.js +0 -113
- package/methods/counters.js.map +0 -1
- package/methods/cron-jobs.d.ts +0 -2
- package/methods/cron-jobs.js +0 -2475
- package/methods/cron-jobs.js.map +0 -1
- package/methods/customer-notifications.d.ts +0 -2
- package/methods/customer-notifications.js +0 -528
- package/methods/customer-notifications.js.map +0 -1
- package/methods/diagnostics.d.ts +0 -2
- package/methods/diagnostics.js +0 -703
- package/methods/diagnostics.js.map +0 -1
- package/methods/flag-updates.d.ts +0 -2
- package/methods/flag-updates.js +0 -8
- package/methods/flag-updates.js.map +0 -1
- package/methods/flags.d.ts +0 -2
- package/methods/flags.js +0 -8
- package/methods/flags.js.map +0 -1
- package/methods/logs.d.ts +0 -2
- package/methods/logs.js +0 -751
- package/methods/logs.js.map +0 -1
- package/methods/mongo-explorer.d.ts +0 -2
- package/methods/mongo-explorer.js +0 -1808
- package/methods/mongo-explorer.js.map +0 -1
- package/methods/monitor.d.ts +0 -2
- package/methods/monitor.js +0 -543
- package/methods/monitor.js.map +0 -1
- package/methods/pdf.d.ts +0 -2
- package/methods/pdf.js +0 -1216
- package/methods/pdf.js.map +0 -1
- package/methods/publications.d.ts +0 -1
- package/methods/publications.js +0 -183
- package/methods/publications.js.map +0 -1
- package/methods/report-builder.d.ts +0 -2
- package/methods/report-builder.js +0 -3094
- package/methods/report-builder.js.map +0 -1
- package/methods/support.d.ts +0 -2
- package/methods/support.js +0 -430
- package/methods/support.js.map +0 -1
- package/models/ai-run.model.d.ts +0 -19
- package/models/ai-run.model.js +0 -4
- package/models/ai-run.model.js.map +0 -1
- package/models/ai-terminal-conversation.model.d.ts +0 -17
- package/models/ai-terminal-conversation.model.js +0 -4
- package/models/ai-terminal-conversation.model.js.map +0 -1
- package/models/ai-terminal-issue-report.model.d.ts +0 -19
- package/models/ai-terminal-issue-report.model.js +0 -4
- package/models/ai-terminal-issue-report.model.js.map +0 -1
- package/models/ai-terminal-message.model.d.ts +0 -22
- package/models/ai-terminal-message.model.js +0 -4
- package/models/ai-terminal-message.model.js.map +0 -1
- package/models/app-setting.model.d.ts +0 -16
- package/models/app-setting.model.js +0 -4
- package/models/app-setting.model.js.map +0 -1
- package/models/app-status.model.js +0 -4
- package/models/app-status.model.js.map +0 -1
- package/models/billing-logged-in-users.model.js +0 -4
- package/models/billing-logged-in-users.model.js.map +0 -1
- package/models/collection-document.model.d.ts +0 -21
- package/models/collection-document.model.js +0 -4
- package/models/collection-document.model.js.map +0 -1
- package/models/communication-metric.model.d.ts +0 -20
- package/models/communication-metric.model.js +0 -4
- package/models/communication-metric.model.js.map +0 -1
- package/models/counter.model.js +0 -4
- package/models/counter.model.js.map +0 -1
- package/models/cron-job-history.model.d.ts +0 -15
- package/models/cron-job-history.model.js +0 -4
- package/models/cron-job-history.model.js.map +0 -1
- package/models/cron-job.model.d.ts +0 -14
- package/models/cron-job.model.js +0 -4
- package/models/cron-job.model.js.map +0 -1
- package/models/customer-notification.model.d.ts +0 -26
- package/models/customer-notification.model.js +0 -4
- package/models/customer-notification.model.js.map +0 -1
- package/models/customer-portal-password.model.d.ts +0 -11
- package/models/customer-portal-password.model.js +0 -4
- package/models/customer-portal-password.model.js.map +0 -1
- package/models/dialog.model.d.ts +0 -23
- package/models/dialog.model.js +0 -4
- package/models/dialog.model.js.map +0 -1
- package/models/email-history.model.d.ts +0 -32
- package/models/email-history.model.js.map +0 -1
- package/models/email-verified.model.js +0 -4
- package/models/email-verified.model.js.map +0 -1
- package/models/file.model.js +0 -4
- package/models/file.model.js.map +0 -1
- package/models/flag-update.model.js +0 -4
- package/models/flag-update.model.js.map +0 -1
- package/models/flag.model.js +0 -4
- package/models/flag.model.js.map +0 -1
- package/models/log-method-latency.model.d.ts +0 -10
- package/models/log-method-latency.model.js +0 -4
- package/models/log-method-latency.model.js.map +0 -1
- package/models/log-subscription.model.js +0 -4
- package/models/log-subscription.model.js.map +0 -1
- package/models/log.model.d.ts +0 -17
- package/models/log.model.js +0 -4
- package/models/log.model.js.map +0 -1
- package/models/logged-in-users.model.js +0 -4
- package/models/logged-in-users.model.js.map +0 -1
- package/models/method-response.model.js +0 -4
- package/models/method-response.model.js.map +0 -1
- package/models/method.model.d.ts +0 -26
- package/models/method.model.js +0 -4
- package/models/method.model.js.map +0 -1
- package/models/monitor-cpu.model.js +0 -4
- package/models/monitor-cpu.model.js.map +0 -1
- package/models/monitor-function.model.d.ts +0 -14
- package/models/monitor-function.model.js +0 -4
- package/models/monitor-function.model.js.map +0 -1
- package/models/monitor-memory.model.d.ts +0 -15
- package/models/monitor-memory.model.js +0 -4
- package/models/monitor-memory.model.js.map +0 -1
- package/models/monitor-mongo.model.d.ts +0 -13
- package/models/monitor-mongo.model.js +0 -4
- package/models/monitor-mongo.model.js.map +0 -1
- package/models/notification.model.js +0 -4
- package/models/notification.model.js.map +0 -1
- package/models/openai-usage-ledger.model.d.ts +0 -30
- package/models/openai-usage-ledger.model.js +0 -4
- package/models/openai-usage-ledger.model.js.map +0 -1
- package/models/pagination.model.d.ts +0 -11
- package/models/pagination.model.js +0 -28
- package/models/pagination.model.js.map +0 -1
- package/models/permission.model.d.ts +0 -12
- package/models/permission.model.js +0 -4
- package/models/permission.model.js.map +0 -1
- package/models/report-builder-dashboard-builder.model.d.ts +0 -25
- package/models/report-builder-dashboard-builder.model.js +0 -4
- package/models/report-builder-dashboard-builder.model.js.map +0 -1
- package/models/report-builder-library.model.d.ts +0 -17
- package/models/report-builder-library.model.js +0 -4
- package/models/report-builder-library.model.js.map +0 -1
- package/models/report-builder-report.model.d.ts +0 -121
- package/models/report-builder-report.model.js +0 -4
- package/models/report-builder-report.model.js.map +0 -1
- package/models/report-builder.model.d.ts +0 -61
- package/models/report-builder.model.js +0 -4
- package/models/report-builder.model.js.map +0 -1
- package/models/select-data-label.model.d.ts +0 -9
- package/models/select-data-label.model.js +0 -4
- package/models/select-data-label.model.js.map +0 -1
- package/models/server-message.model.d.ts +0 -32
- package/models/server-message.model.js +0 -4
- package/models/server-message.model.js.map +0 -1
- package/models/slow-query-report.model.d.ts +0 -23
- package/models/slow-query-report.model.js +0 -4
- package/models/slow-query-report.model.js.map +0 -1
- package/models/subscription.model.d.ts +0 -31
- package/models/subscription.model.js +0 -4
- package/models/subscription.model.js.map +0 -1
- package/models/support-ticket.model.d.ts +0 -87
- package/models/support-ticket.model.js +0 -4
- package/models/support-ticket.model.js.map +0 -1
- package/models/user-group.model.d.ts +0 -20
- package/models/user-group.model.js +0 -4
- package/models/user-group.model.js.map +0 -1
- package/models/user-guide.model.js +0 -4
- package/models/user-guide.model.js.map +0 -1
- package/models/user.model.d.ts +0 -84
- package/models/user.model.js +0 -4
- package/models/user.model.js.map +0 -1
- package/private/images/ResolveIO.png +0 -0
- package/public_api.js +0 -127
- package/public_api.js.map +0 -1
- package/publications/ai-terminal.d.ts +0 -1
- package/publications/ai-terminal.js +0 -122
- package/publications/ai-terminal.js.map +0 -1
- package/publications/app-settings.d.ts +0 -2
- package/publications/app-settings.js +0 -28
- package/publications/app-settings.js.map +0 -1
- package/publications/app-status.d.ts +0 -2
- package/publications/app-status.js +0 -16
- package/publications/app-status.js.map +0 -1
- package/publications/cron-jobs.d.ts +0 -2
- package/publications/cron-jobs.js +0 -88
- package/publications/cron-jobs.js.map +0 -1
- package/publications/customer-notifications.d.ts +0 -2
- package/publications/customer-notifications.js +0 -161
- package/publications/customer-notifications.js.map +0 -1
- package/publications/files.d.ts +0 -2
- package/publications/files.js +0 -36
- package/publications/files.js.map +0 -1
- package/publications/flags-update.d.ts +0 -2
- package/publications/flags-update.js +0 -22
- package/publications/flags-update.js.map +0 -1
- package/publications/flags.d.ts +0 -2
- package/publications/flags.js +0 -22
- package/publications/flags.js.map +0 -1
- package/publications/logs.d.ts +0 -2
- package/publications/logs.js +0 -164
- package/publications/logs.js.map +0 -1
- package/publications/notifications.d.ts +0 -2
- package/publications/notifications.js +0 -16
- package/publications/notifications.js.map +0 -1
- package/publications/report-builder-dashboard-builders.d.ts +0 -2
- package/publications/report-builder-dashboard-builders.js +0 -42
- package/publications/report-builder-dashboard-builders.js.map +0 -1
- package/publications/report-builder-libraries.d.ts +0 -2
- package/publications/report-builder-libraries.js +0 -90
- package/publications/report-builder-libraries.js.map +0 -1
- package/publications/report-builder-reports.d.ts +0 -2
- package/publications/report-builder-reports.js +0 -50
- package/publications/report-builder-reports.js.map +0 -1
- package/publications/super-admin.d.ts +0 -2
- package/publications/super-admin.js +0 -16
- package/publications/super-admin.js.map +0 -1
- package/publications/user-groups.d.ts +0 -1
- package/publications/user-groups.js +0 -16
- package/publications/user-groups.js.map +0 -1
- package/publications/user-guides.d.ts +0 -1
- package/publications/user-guides.js +0 -16
- package/publications/user-guides.js.map +0 -1
- package/resolveio-server-app.d.ts +0 -70
- package/resolveio-server-app.js +0 -801
- package/resolveio-server-app.js.map +0 -1
- package/server-app.d.ts +0 -228
- package/server-app.js +0 -3566
- package/server-app.js.map +0 -1
- package/services/codex-client.d.ts +0 -128
- package/services/codex-client.js +0 -1629
- package/services/codex-client.js.map +0 -1
- package/services/openai-client.d.ts +0 -46
- package/services/openai-client.js +0 -318
- package/services/openai-client.js.map +0 -1
- package/types/error-report.d.ts +0 -25
- package/types/error-report.js +0 -4
- package/types/error-report.js.map +0 -1
- package/types/slow-query-report.d.ts +0 -27
- package/types/slow-query-report.js +0 -6
- package/types/slow-query-report.js.map +0 -1
- package/util/ai-qa-policy.d.ts +0 -124
- package/util/ai-qa-policy.js +0 -736
- package/util/ai-qa-policy.js.map +0 -1
- package/util/ai-run-evidence-adapters.d.ts +0 -109
- package/util/ai-run-evidence-adapters.js +0 -7234
- package/util/ai-run-evidence-adapters.js.map +0 -1
- package/util/ai-run-evidence-dashboard.d.ts +0 -88
- package/util/ai-run-evidence-dashboard.js +0 -343
- package/util/ai-run-evidence-dashboard.js.map +0 -1
- package/util/ai-run-evidence-eval.d.ts +0 -86
- package/util/ai-run-evidence-eval.js +0 -1018
- package/util/ai-run-evidence-eval.js.map +0 -1
- package/util/ai-run-evidence.d.ts +0 -244
- package/util/ai-run-evidence.js +0 -1096
- package/util/ai-run-evidence.js.map +0 -1
- package/util/ai-runner-artifacts.d.ts +0 -82
- package/util/ai-runner-artifacts.js +0 -713
- package/util/ai-runner-artifacts.js.map +0 -1
- package/util/ai-runner-manager-autopilot.d.ts +0 -210
- package/util/ai-runner-manager-autopilot.js +0 -642
- package/util/ai-runner-manager-autopilot.js.map +0 -1
- package/util/ai-runner-manager-policy.d.ts +0 -807
- package/util/ai-runner-manager-policy.js +0 -3501
- package/util/ai-runner-manager-policy.js.map +0 -1
- package/util/ai-runner-qa-auth.d.ts +0 -5
- package/util/ai-runner-qa-auth.js +0 -839
- package/util/ai-runner-qa-auth.js.map +0 -1
- package/util/ai-runner-qa-tools.d.ts +0 -26
- package/util/ai-runner-qa-tools.js +0 -3520
- package/util/ai-runner-qa-tools.js.map +0 -1
- package/util/aicoder-runner-v6.d.ts +0 -426
- package/util/aicoder-runner-v6.js +0 -2464
- package/util/aicoder-runner-v6.js.map +0 -1
- package/util/common.d.ts +0 -31
- package/util/common.js +0 -683
- package/util/common.js.map +0 -1
- package/util/customer-portal-password.d.ts +0 -13
- package/util/customer-portal-password.js +0 -209
- package/util/customer-portal-password.js.map +0 -1
- package/util/error-reporter.d.ts +0 -52
- package/util/error-reporter.js +0 -326
- package/util/error-reporter.js.map +0 -1
- package/util/error-tracking.d.ts +0 -13
- package/util/error-tracking.js +0 -120
- package/util/error-tracking.js.map +0 -1
- package/util/openai-usage-cost.d.ts +0 -6
- package/util/openai-usage-cost.js +0 -103
- package/util/openai-usage-cost.js.map +0 -1
- package/util/report-builder-unwinds.d.ts +0 -15
- package/util/report-builder-unwinds.js +0 -156
- package/util/report-builder-unwinds.js.map +0 -1
- package/util/runner-process-janitor.d.ts +0 -27
- package/util/runner-process-janitor.js +0 -208
- package/util/runner-process-janitor.js.map +0 -1
- package/util/schema-report-builder.d.ts +0 -6
- package/util/schema-report-builder.js +0 -481
- package/util/schema-report-builder.js.map +0 -1
- package/util/slow-query-reporter.d.ts +0 -28
- package/util/slow-query-reporter.js +0 -226
- package/util/slow-query-reporter.js.map +0 -1
- package/util/subscription-dependency-context.d.ts +0 -34
- package/util/subscription-dependency-context.js +0 -1283
- package/util/subscription-dependency-context.js.map +0 -1
- package/util/support-runner-v5.d.ts +0 -1426
- package/util/support-runner-v5.js +0 -7631
- package/util/support-runner-v5.js.map +0 -1
- package/util/tokenizer.d.ts +0 -5
- package/util/tokenizer.js +0 -41
- package/util/tokenizer.js.map +0 -1
- package/workers/codex-runner.worker.d.ts +0 -1
- package/workers/codex-runner.worker.js +0 -192
- package/workers/codex-runner.worker.js.map +0 -1
- /package/{private → src/private}/email-templates/enrollment.html +0 -0
- /package/{private → src/private}/email-templates/forgot-password.html +0 -0
- /package/{private → src/private}/email-templates/support-ticket-deleted.html +0 -0
- /package/{private → src/private}/email-templates/support-ticket-modified.html +0 -0
- /package/{private → src/private}/email-templates/support-ticket.html +0 -0
- /package/{public_api.d.ts → src/public_api.ts} +0 -0
|
@@ -0,0 +1,1131 @@
|
|
|
1
|
+
import { AiTerminalConversations } from '../collections/ai-terminal-conversation.collection';
|
|
2
|
+
import { AiTerminalIssueReports } from '../collections/ai-terminal-issue-report.collection';
|
|
3
|
+
import { AiTerminalMessages } from '../collections/ai-terminal-message.collection';
|
|
4
|
+
import { AiTerminalMessageModel } from '../models/ai-terminal-message.model';
|
|
5
|
+
import { AiTerminalConversationModel } from '../models/ai-terminal-conversation.model';
|
|
6
|
+
import { promises as fs } from 'fs';
|
|
7
|
+
import * as os from 'os';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
import { execFile } from 'child_process';
|
|
10
|
+
import { promisify } from 'util';
|
|
11
|
+
|
|
12
|
+
type AiAssistantCodexManagerRuntime = {
|
|
13
|
+
isWorkersEnabled: boolean;
|
|
14
|
+
isWorkerInstance: boolean;
|
|
15
|
+
workerIndex?: string | null;
|
|
16
|
+
workerInstance?: string | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type AiAssistantCodexManagerConfig = {
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
runOnAnyWorker: boolean;
|
|
22
|
+
targetWorkerIndex: string;
|
|
23
|
+
targetWorkerInstance: string | null;
|
|
24
|
+
pollIntervalMs: number;
|
|
25
|
+
lookbackMinutes: number;
|
|
26
|
+
maxMessagesPerPoll: number;
|
|
27
|
+
maxRemediationsPerPoll: number;
|
|
28
|
+
autoReportEnabled: boolean;
|
|
29
|
+
autoRemediationEnabled: boolean;
|
|
30
|
+
remediationHookPath: string;
|
|
31
|
+
remediationTimeoutMs: number;
|
|
32
|
+
remediationMaxBufferBytes: number;
|
|
33
|
+
remediationMaxAttemptsPerIssue: number;
|
|
34
|
+
remediationCooldownMinutes: number;
|
|
35
|
+
autoReplayEnabled: boolean;
|
|
36
|
+
replayCommand: string;
|
|
37
|
+
replayTimeoutMs: number;
|
|
38
|
+
replayMaxBufferBytes: number;
|
|
39
|
+
replayMaxAttemptsPerIssue: number;
|
|
40
|
+
replayCooldownMinutes: number;
|
|
41
|
+
debug: boolean;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type AiAssistantQualityDecision = {
|
|
45
|
+
status: 'pass' | 'needs_review';
|
|
46
|
+
reason: string;
|
|
47
|
+
code: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const DEFAULT_POLL_INTERVAL_MS = 15000;
|
|
51
|
+
const DEFAULT_LOOKBACK_MINUTES = 120;
|
|
52
|
+
const DEFAULT_MAX_MESSAGES_PER_POLL = 100;
|
|
53
|
+
const DEFAULT_MAX_REMEDIATIONS_PER_POLL = 3;
|
|
54
|
+
const DEFAULT_REMEDIATION_TIMEOUT_MS = 20 * 60 * 1000;
|
|
55
|
+
const DEFAULT_REMEDIATION_MAX_BUFFER_BYTES = 1024 * 1024;
|
|
56
|
+
const DEFAULT_REMEDIATION_MAX_ATTEMPTS_PER_ISSUE = 2;
|
|
57
|
+
const DEFAULT_REMEDIATION_COOLDOWN_MINUTES = 15;
|
|
58
|
+
const DEFAULT_REPLAY_TIMEOUT_MS = 5 * 60 * 1000;
|
|
59
|
+
const DEFAULT_REPLAY_MAX_BUFFER_BYTES = 1024 * 1024;
|
|
60
|
+
const DEFAULT_REPLAY_MAX_ATTEMPTS_PER_ISSUE = 2;
|
|
61
|
+
const DEFAULT_REPLAY_COOLDOWN_MINUTES = 10;
|
|
62
|
+
const MAX_STORED_OUTPUT_CHARS = 4000;
|
|
63
|
+
const execFileAsync = promisify(execFile);
|
|
64
|
+
|
|
65
|
+
function normalizeOptionalString(value: any): string {
|
|
66
|
+
return typeof value === 'string' ? value.trim() : '';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function parseBooleanEnv(value: any, fallback: boolean): boolean {
|
|
70
|
+
const normalized = normalizeOptionalString(value).toLowerCase();
|
|
71
|
+
if (!normalized) {
|
|
72
|
+
return fallback;
|
|
73
|
+
}
|
|
74
|
+
if (['1', 'true', 'yes', 'on', 'enabled'].includes(normalized)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
if (['0', 'false', 'no', 'off', 'disabled'].includes(normalized)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return fallback;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function parsePositiveInt(value: any, fallback: number, min = 1): number {
|
|
84
|
+
const parsed = parseInt(String(value ?? '').trim(), 10);
|
|
85
|
+
if (!Number.isFinite(parsed) || Number.isNaN(parsed) || parsed < min) {
|
|
86
|
+
return fallback;
|
|
87
|
+
}
|
|
88
|
+
return parsed;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function parsePositiveIntWithMax(value: any, fallback: number, min: number, max: number): number {
|
|
92
|
+
const parsed = parsePositiveInt(value, fallback, min);
|
|
93
|
+
return Math.min(parsed, max);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function resolveEnabledFlag(): boolean {
|
|
97
|
+
const explicitValue = process.env.AI_ASSISTANT_CODEX_MANAGER_ENABLED
|
|
98
|
+
?? process.env.AI_ASSISTANT_QUALITY_MANAGER_ENABLED;
|
|
99
|
+
if (explicitValue !== undefined && explicitValue !== null && String(explicitValue).trim() !== '') {
|
|
100
|
+
return parseBooleanEnv(explicitValue, false);
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export class AiAssistantCodexManager {
|
|
106
|
+
private readonly config: AiAssistantCodexManagerConfig;
|
|
107
|
+
private readonly runtime: AiAssistantCodexManagerRuntime;
|
|
108
|
+
private _interval: NodeJS.Timeout | null = null;
|
|
109
|
+
private _inFlight = false;
|
|
110
|
+
private _cursorCreatedAt: Date;
|
|
111
|
+
private _cursorMessageId = '';
|
|
112
|
+
|
|
113
|
+
constructor(config: AiAssistantCodexManagerConfig, runtime: AiAssistantCodexManagerRuntime) {
|
|
114
|
+
this.config = config;
|
|
115
|
+
this.runtime = runtime;
|
|
116
|
+
this._cursorCreatedAt = new Date(Date.now() - (config.lookbackMinutes * 60 * 1000));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static createIfEnabled(runtime: AiAssistantCodexManagerRuntime): AiAssistantCodexManager | null {
|
|
120
|
+
const config = this.resolveConfig();
|
|
121
|
+
if (!config.enabled) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
if (!this.shouldRunOnCurrentProcess(config, runtime)) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const manager = new AiAssistantCodexManager(config, runtime);
|
|
128
|
+
manager.initialize();
|
|
129
|
+
return manager;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private static resolveConfig(): AiAssistantCodexManagerConfig {
|
|
133
|
+
const targetWorkerIndex = normalizeOptionalString(
|
|
134
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_WORKER_INDEX
|
|
135
|
+
|| process.env.AI_ASSISTANT_CODEX_WORKER_INDEX
|
|
136
|
+
|| process.env.CODEX_WORKER_INDEX
|
|
137
|
+
|| 'ai-assistant-manager'
|
|
138
|
+
) || 'ai-assistant-manager';
|
|
139
|
+
const targetWorkerInstance = normalizeOptionalString(
|
|
140
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_WORKER_INSTANCE
|
|
141
|
+
) || null;
|
|
142
|
+
return {
|
|
143
|
+
enabled: resolveEnabledFlag(),
|
|
144
|
+
runOnAnyWorker: parseBooleanEnv(process.env.AI_ASSISTANT_CODEX_MANAGER_RUN_ANY_WORKER, false),
|
|
145
|
+
targetWorkerIndex,
|
|
146
|
+
targetWorkerInstance,
|
|
147
|
+
pollIntervalMs: parsePositiveInt(process.env.AI_ASSISTANT_CODEX_MANAGER_INTERVAL_MS, DEFAULT_POLL_INTERVAL_MS, 1000),
|
|
148
|
+
lookbackMinutes: parsePositiveInt(process.env.AI_ASSISTANT_CODEX_MANAGER_LOOKBACK_MINUTES, DEFAULT_LOOKBACK_MINUTES, 1),
|
|
149
|
+
maxMessagesPerPoll: parsePositiveIntWithMax(
|
|
150
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_MAX_MESSAGES_PER_POLL,
|
|
151
|
+
DEFAULT_MAX_MESSAGES_PER_POLL,
|
|
152
|
+
1,
|
|
153
|
+
1000
|
|
154
|
+
),
|
|
155
|
+
maxRemediationsPerPoll: parsePositiveIntWithMax(
|
|
156
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_MAX_REMEDIATIONS_PER_POLL,
|
|
157
|
+
DEFAULT_MAX_REMEDIATIONS_PER_POLL,
|
|
158
|
+
1,
|
|
159
|
+
100
|
|
160
|
+
),
|
|
161
|
+
autoReportEnabled: parseBooleanEnv(process.env.AI_ASSISTANT_CODEX_MANAGER_AUTO_REPORT, true),
|
|
162
|
+
autoRemediationEnabled: parseBooleanEnv(process.env.AI_ASSISTANT_CODEX_MANAGER_AUTO_REMEDIATE, false),
|
|
163
|
+
remediationHookPath: normalizeOptionalString(process.env.AI_ASSISTANT_CODEX_MANAGER_REMEDIATE_HOOK),
|
|
164
|
+
remediationTimeoutMs: parsePositiveInt(
|
|
165
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_REMEDIATE_TIMEOUT_MS,
|
|
166
|
+
DEFAULT_REMEDIATION_TIMEOUT_MS,
|
|
167
|
+
1000
|
|
168
|
+
),
|
|
169
|
+
remediationMaxBufferBytes: parsePositiveInt(
|
|
170
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_REMEDIATE_MAX_BUFFER_BYTES,
|
|
171
|
+
DEFAULT_REMEDIATION_MAX_BUFFER_BYTES,
|
|
172
|
+
1024
|
|
173
|
+
),
|
|
174
|
+
remediationMaxAttemptsPerIssue: parsePositiveInt(
|
|
175
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_REMEDIATE_MAX_ATTEMPTS_PER_ISSUE,
|
|
176
|
+
DEFAULT_REMEDIATION_MAX_ATTEMPTS_PER_ISSUE,
|
|
177
|
+
1
|
|
178
|
+
),
|
|
179
|
+
remediationCooldownMinutes: parsePositiveInt(
|
|
180
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_REMEDIATE_COOLDOWN_MINUTES,
|
|
181
|
+
DEFAULT_REMEDIATION_COOLDOWN_MINUTES,
|
|
182
|
+
1
|
|
183
|
+
),
|
|
184
|
+
autoReplayEnabled: parseBooleanEnv(process.env.AI_ASSISTANT_CODEX_MANAGER_REPLAY_ENABLED, false),
|
|
185
|
+
replayCommand: normalizeOptionalString(process.env.AI_ASSISTANT_CODEX_MANAGER_REPLAY_COMMAND),
|
|
186
|
+
replayTimeoutMs: parsePositiveInt(
|
|
187
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_REPLAY_TIMEOUT_MS,
|
|
188
|
+
DEFAULT_REPLAY_TIMEOUT_MS,
|
|
189
|
+
1000
|
|
190
|
+
),
|
|
191
|
+
replayMaxBufferBytes: parsePositiveInt(
|
|
192
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_REPLAY_MAX_BUFFER_BYTES,
|
|
193
|
+
DEFAULT_REPLAY_MAX_BUFFER_BYTES,
|
|
194
|
+
1024
|
|
195
|
+
),
|
|
196
|
+
replayMaxAttemptsPerIssue: parsePositiveInt(
|
|
197
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_REPLAY_MAX_ATTEMPTS_PER_ISSUE,
|
|
198
|
+
DEFAULT_REPLAY_MAX_ATTEMPTS_PER_ISSUE,
|
|
199
|
+
1
|
|
200
|
+
),
|
|
201
|
+
replayCooldownMinutes: parsePositiveInt(
|
|
202
|
+
process.env.AI_ASSISTANT_CODEX_MANAGER_REPLAY_COOLDOWN_MINUTES,
|
|
203
|
+
DEFAULT_REPLAY_COOLDOWN_MINUTES,
|
|
204
|
+
1
|
|
205
|
+
),
|
|
206
|
+
debug: parseBooleanEnv(process.env.AI_ASSISTANT_CODEX_MANAGER_DEBUG, false)
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private static shouldRunOnCurrentProcess(config: AiAssistantCodexManagerConfig, runtime: AiAssistantCodexManagerRuntime): boolean {
|
|
211
|
+
const workerIndex = normalizeOptionalString(runtime.workerIndex);
|
|
212
|
+
const workerInstance = normalizeOptionalString(runtime.workerInstance);
|
|
213
|
+
if (!runtime.isWorkersEnabled) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
if (!runtime.isWorkerInstance) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
if (config.runOnAnyWorker) {
|
|
220
|
+
if (config.targetWorkerInstance) {
|
|
221
|
+
return workerInstance === config.targetWorkerInstance;
|
|
222
|
+
}
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
if (workerIndex !== config.targetWorkerIndex) {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
if (config.targetWorkerInstance && workerInstance !== config.targetWorkerInstance) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private initialize(): void {
|
|
235
|
+
console.log(new Date(), '[AI Assistant Codex Manager] started', {
|
|
236
|
+
workerIndex: process.env.WORKER_INDEX || null,
|
|
237
|
+
workerInstance: process.env.NODE_APP_INSTANCE || null,
|
|
238
|
+
pollIntervalMs: this.config.pollIntervalMs,
|
|
239
|
+
maxMessagesPerPoll: this.config.maxMessagesPerPoll,
|
|
240
|
+
lookbackMinutes: this.config.lookbackMinutes,
|
|
241
|
+
autoRemediationEnabled: this.config.autoRemediationEnabled,
|
|
242
|
+
remediationHookPath: this.config.remediationHookPath || null,
|
|
243
|
+
autoReplayEnabled: this.config.autoReplayEnabled
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
setTimeout(async () => {
|
|
247
|
+
try {
|
|
248
|
+
await this.pollOnce();
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
console.error(new Date(), '[AI Assistant Codex Manager] initial poll failed', error);
|
|
252
|
+
}
|
|
253
|
+
}, 2500);
|
|
254
|
+
|
|
255
|
+
this._interval = setInterval(async () => {
|
|
256
|
+
try {
|
|
257
|
+
await this.pollOnce();
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
console.error(new Date(), '[AI Assistant Codex Manager] poll failed', error);
|
|
261
|
+
}
|
|
262
|
+
}, this.config.pollIntervalMs);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private async pollOnce(): Promise<void> {
|
|
266
|
+
if (this._inFlight) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
this._inFlight = true;
|
|
270
|
+
try {
|
|
271
|
+
const query: Record<string, any> = {
|
|
272
|
+
role: 'assistant',
|
|
273
|
+
$or: [
|
|
274
|
+
{ createdAt: { $gt: this._cursorCreatedAt } },
|
|
275
|
+
{ createdAt: this._cursorCreatedAt, _id: { $gt: this._cursorMessageId || '' } }
|
|
276
|
+
]
|
|
277
|
+
};
|
|
278
|
+
const messages = await AiTerminalMessages.find(
|
|
279
|
+
query,
|
|
280
|
+
{
|
|
281
|
+
sort: { createdAt: 1, _id: 1 },
|
|
282
|
+
limit: this.config.maxMessagesPerPoll
|
|
283
|
+
}
|
|
284
|
+
) as AiTerminalMessageModel[];
|
|
285
|
+
if (Array.isArray(messages) && messages.length) {
|
|
286
|
+
for (const message of messages) {
|
|
287
|
+
const createdAt = message?.createdAt ? new Date(message.createdAt) : null;
|
|
288
|
+
if (createdAt && Number.isFinite(createdAt.getTime())) {
|
|
289
|
+
this._cursorCreatedAt = createdAt;
|
|
290
|
+
}
|
|
291
|
+
this._cursorMessageId = normalizeOptionalString(message?._id);
|
|
292
|
+
await this.processMessage(message);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (this.config.autoRemediationEnabled) {
|
|
297
|
+
await this.pollRemediationQueue();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
finally {
|
|
301
|
+
this._inFlight = false;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private async processMessage(message: AiTerminalMessageModel): Promise<void> {
|
|
306
|
+
const messageId = normalizeOptionalString(message?._id);
|
|
307
|
+
const conversationId = normalizeOptionalString(message?.id_conversation);
|
|
308
|
+
if (!messageId || !conversationId) {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const metadata = this.normalizeMetadata(message?.metadata);
|
|
312
|
+
if (metadata.pending === true) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
if (metadata.quality_manager?.checked_at) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const decision = this.evaluateMessageQuality(message, metadata);
|
|
320
|
+
let issueReportId = '';
|
|
321
|
+
if (decision.status === 'needs_review' && this.config.autoReportEnabled) {
|
|
322
|
+
issueReportId = await this.ensureAutomatedIssueReport(message, decision);
|
|
323
|
+
}
|
|
324
|
+
await this.markMessageQualityReviewed(messageId, decision, issueReportId);
|
|
325
|
+
if (issueReportId && this.config.autoRemediationEnabled) {
|
|
326
|
+
await this.runIssueRemediation(issueReportId, { source: 'auto', trigger: 'message_quality' });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private evaluateMessageQuality(message: AiTerminalMessageModel, metadata: Record<string, any>): AiAssistantQualityDecision {
|
|
331
|
+
const content = normalizeOptionalString(message?.content);
|
|
332
|
+
const toolError = normalizeOptionalString(metadata?.tool_execution?.outcome?.error);
|
|
333
|
+
if (metadata.failed === true) {
|
|
334
|
+
return {
|
|
335
|
+
status: 'needs_review',
|
|
336
|
+
reason: 'Assistant response finalized in failed state.',
|
|
337
|
+
code: 'metadata_failed'
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
if (/I ran into an internal error while preparing that response/i.test(content)) {
|
|
341
|
+
return {
|
|
342
|
+
status: 'needs_review',
|
|
343
|
+
reason: 'Assistant returned internal error text.',
|
|
344
|
+
code: 'internal_error_copy'
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
if (/That took longer than expected\./i.test(content)) {
|
|
348
|
+
return {
|
|
349
|
+
status: 'needs_review',
|
|
350
|
+
reason: 'Assistant response timed out.',
|
|
351
|
+
code: 'timeout_copy'
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
if (/I could not finish this request within the assistant time budget/i.test(content)) {
|
|
355
|
+
return {
|
|
356
|
+
status: 'needs_review',
|
|
357
|
+
reason: 'Assistant run exceeded configured time budget.',
|
|
358
|
+
code: 'run_budget_exceeded_copy'
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
if (toolError) {
|
|
362
|
+
return {
|
|
363
|
+
status: 'needs_review',
|
|
364
|
+
reason: `Assistant tool execution error: ${toolError}`.slice(0, 500),
|
|
365
|
+
code: 'tool_execution_error'
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
return {
|
|
369
|
+
status: 'pass',
|
|
370
|
+
reason: 'Quality check passed.',
|
|
371
|
+
code: 'ok'
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
private async ensureAutomatedIssueReport(message: AiTerminalMessageModel, decision: AiAssistantQualityDecision): Promise<string> {
|
|
376
|
+
const messageId = normalizeOptionalString(message?._id);
|
|
377
|
+
const conversationId = normalizeOptionalString(message?.id_conversation);
|
|
378
|
+
if (!messageId || !conversationId) {
|
|
379
|
+
return '';
|
|
380
|
+
}
|
|
381
|
+
const existing = await AiTerminalIssueReports.findOne({
|
|
382
|
+
id_message: messageId,
|
|
383
|
+
source: 'automated'
|
|
384
|
+
});
|
|
385
|
+
if (existing?._id) {
|
|
386
|
+
return normalizeOptionalString(existing._id);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const conversation = await AiTerminalConversations.findById(conversationId) as AiTerminalConversationModel | null;
|
|
390
|
+
if (!conversation) {
|
|
391
|
+
return '';
|
|
392
|
+
}
|
|
393
|
+
const assistantContent = normalizeOptionalString(message?.content);
|
|
394
|
+
const requestId = normalizeOptionalString((message as any)?.metadata?.request_id);
|
|
395
|
+
const userPrompt = await this.resolveClosestUserPrompt(conversationId, message?.createdAt);
|
|
396
|
+
const now = new Date();
|
|
397
|
+
const doc: Record<string, any> = {
|
|
398
|
+
id_conversation: conversationId,
|
|
399
|
+
status: 'open',
|
|
400
|
+
source: 'automated',
|
|
401
|
+
reason: decision.reason,
|
|
402
|
+
...(userPrompt ? { user_prompt: userPrompt } : {}),
|
|
403
|
+
...(assistantContent ? { assistant_content: assistantContent } : {}),
|
|
404
|
+
...(requestId ? { request_id: requestId } : {}),
|
|
405
|
+
id_message: messageId,
|
|
406
|
+
...(normalizeOptionalString(conversation?.id_client) ? { id_client: normalizeOptionalString(conversation?.id_client) } : {}),
|
|
407
|
+
...(normalizeOptionalString(conversation?.id_app) ? { id_app: normalizeOptionalString(conversation?.id_app) } : {}),
|
|
408
|
+
...(normalizeOptionalString(conversation?.id_user) ? { id_user: normalizeOptionalString(conversation?.id_user) } : {}),
|
|
409
|
+
metadata: {
|
|
410
|
+
quality_manager: {
|
|
411
|
+
code: decision.code,
|
|
412
|
+
checked_at: now,
|
|
413
|
+
worker_index: normalizeOptionalString(process.env.WORKER_INDEX),
|
|
414
|
+
worker_instance: normalizeOptionalString(process.env.NODE_APP_INSTANCE)
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
createdAt: now,
|
|
418
|
+
updatedAt: now
|
|
419
|
+
};
|
|
420
|
+
const insertResult = await AiTerminalIssueReports.insertOne(doc as any);
|
|
421
|
+
const issueId = normalizeOptionalString(insertResult?._id);
|
|
422
|
+
console.log(new Date(), '[AI Assistant Codex Manager] created automated issue report', {
|
|
423
|
+
id_issue_report: issueId || null,
|
|
424
|
+
id_conversation: conversationId,
|
|
425
|
+
id_message: messageId,
|
|
426
|
+
code: decision.code
|
|
427
|
+
});
|
|
428
|
+
return issueId;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
private async pollRemediationQueue(): Promise<void> {
|
|
432
|
+
const issues = await AiTerminalIssueReports.find(
|
|
433
|
+
{
|
|
434
|
+
source: 'automated',
|
|
435
|
+
status: { $in: ['open', 'triaged'] },
|
|
436
|
+
$or: [
|
|
437
|
+
{ 'metadata.quality_manager.remediation.status': { $exists: false } },
|
|
438
|
+
{ 'metadata.quality_manager.remediation.status': { $in: ['queued', 'failed', 'skipped'] } }
|
|
439
|
+
]
|
|
440
|
+
},
|
|
441
|
+
{
|
|
442
|
+
sort: { updatedAt: 1, createdAt: 1 },
|
|
443
|
+
limit: this.config.maxRemediationsPerPoll
|
|
444
|
+
}
|
|
445
|
+
) as Array<Record<string, any>>;
|
|
446
|
+
if (!Array.isArray(issues) || !issues.length) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
for (const issue of issues) {
|
|
450
|
+
const issueId = normalizeOptionalString(issue?._id);
|
|
451
|
+
if (!issueId) {
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
const remediationResult = await this.runIssueRemediation(issueId, { source: 'auto', trigger: 'poll' });
|
|
455
|
+
if (remediationResult?.status === 'success' || remediationResult?.status === 'running') {
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
public async runIssueRemediation(
|
|
462
|
+
issueReportId: string,
|
|
463
|
+
options: { force?: boolean; source?: 'auto' | 'manual'; trigger?: string } = {}
|
|
464
|
+
): Promise<Record<string, any>> {
|
|
465
|
+
const issueId = normalizeOptionalString(issueReportId);
|
|
466
|
+
if (!issueId) {
|
|
467
|
+
return {
|
|
468
|
+
status: 'failed',
|
|
469
|
+
reason: 'Issue report id is required.'
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
if (!this.config.autoRemediationEnabled) {
|
|
473
|
+
return {
|
|
474
|
+
status: 'skipped',
|
|
475
|
+
reason: 'Auto remediation is disabled.'
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
if (!this.config.remediationHookPath) {
|
|
479
|
+
return {
|
|
480
|
+
status: 'skipped',
|
|
481
|
+
reason: 'Remediation hook path is not configured.'
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const issue = await AiTerminalIssueReports.findById(issueId) as Record<string, any> | null;
|
|
486
|
+
if (!issue) {
|
|
487
|
+
return {
|
|
488
|
+
status: 'not_found',
|
|
489
|
+
reason: 'Issue report not found.'
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const force = options.force === true;
|
|
494
|
+
const source = options.source === 'manual' ? 'manual' : 'auto';
|
|
495
|
+
const now = new Date();
|
|
496
|
+
const metadata = this.normalizeMetadata(issue.metadata);
|
|
497
|
+
const qualityManager = this.normalizeMetadata(metadata.quality_manager);
|
|
498
|
+
const remediation = this.normalizeMetadata(qualityManager.remediation);
|
|
499
|
+
const existingStatus = normalizeOptionalString(remediation.status).toLowerCase();
|
|
500
|
+
const attemptsUsed = Number.isFinite(Number(remediation.attempt_count)) ? Number(remediation.attempt_count) : 0;
|
|
501
|
+
const maxAttempts = this.config.remediationMaxAttemptsPerIssue;
|
|
502
|
+
const cooldownMinutes = this.config.remediationCooldownMinutes;
|
|
503
|
+
const cooldownMs = cooldownMinutes * 60 * 1000;
|
|
504
|
+
const lastAttemptAt = this.parseDate(remediation.last_attempt_at);
|
|
505
|
+
if (!force && existingStatus === 'running') {
|
|
506
|
+
return {
|
|
507
|
+
status: 'running',
|
|
508
|
+
reason: 'Remediation already running.',
|
|
509
|
+
issueId
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
if (!force && maxAttempts > 0 && attemptsUsed >= maxAttempts) {
|
|
513
|
+
return {
|
|
514
|
+
status: 'skipped',
|
|
515
|
+
reason: `Remediation attempt budget reached (${attemptsUsed}/${maxAttempts}).`,
|
|
516
|
+
issueId
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
if (!force && existingStatus === 'failed' && lastAttemptAt) {
|
|
520
|
+
const ageMs = now.getTime() - lastAttemptAt.getTime();
|
|
521
|
+
if (ageMs >= 0 && ageMs < cooldownMs) {
|
|
522
|
+
return {
|
|
523
|
+
status: 'skipped',
|
|
524
|
+
reason: 'Remediation cooldown active.',
|
|
525
|
+
issueId,
|
|
526
|
+
nextAttemptAt: new Date(lastAttemptAt.getTime() + cooldownMs)
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const attemptCount = attemptsUsed + 1;
|
|
532
|
+
await AiTerminalIssueReports.updateOne(
|
|
533
|
+
{ _id: issueId },
|
|
534
|
+
{
|
|
535
|
+
$set: {
|
|
536
|
+
'metadata.quality_manager.remediation': {
|
|
537
|
+
...remediation,
|
|
538
|
+
status: 'running',
|
|
539
|
+
source,
|
|
540
|
+
trigger: normalizeOptionalString(options.trigger) || 'unspecified',
|
|
541
|
+
attempt_count: attemptCount,
|
|
542
|
+
max_attempts: maxAttempts,
|
|
543
|
+
cooldown_minutes: cooldownMinutes,
|
|
544
|
+
hook: this.config.remediationHookPath,
|
|
545
|
+
last_attempt_at: now,
|
|
546
|
+
updated_at: now,
|
|
547
|
+
last_error: '',
|
|
548
|
+
last_exit_code: null
|
|
549
|
+
},
|
|
550
|
+
updatedAt: now
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
);
|
|
554
|
+
|
|
555
|
+
const startedAtMs = Date.now();
|
|
556
|
+
const contextPath = await this.writeRemediationContextFile(issue, {
|
|
557
|
+
issueId,
|
|
558
|
+
attemptCount,
|
|
559
|
+
source,
|
|
560
|
+
trigger: normalizeOptionalString(options.trigger) || 'unspecified'
|
|
561
|
+
});
|
|
562
|
+
let parsedOutput: Record<string, any> | null = null;
|
|
563
|
+
try {
|
|
564
|
+
const execResult = await execFileAsync(
|
|
565
|
+
this.config.remediationHookPath,
|
|
566
|
+
[contextPath],
|
|
567
|
+
{
|
|
568
|
+
timeout: this.config.remediationTimeoutMs,
|
|
569
|
+
maxBuffer: this.config.remediationMaxBufferBytes,
|
|
570
|
+
env: {
|
|
571
|
+
...process.env,
|
|
572
|
+
RIO_AI_ASSISTANT_REMEDIATION_CONTEXT: contextPath,
|
|
573
|
+
RIO_AI_ASSISTANT_REMEDIATION_ISSUE_ID: issueId
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
);
|
|
577
|
+
const finishedAt = new Date();
|
|
578
|
+
const durationMs = Date.now() - startedAtMs;
|
|
579
|
+
const stdout = this.toSafeOutput(execResult?.stdout);
|
|
580
|
+
const stderr = this.toSafeOutput(execResult?.stderr);
|
|
581
|
+
parsedOutput = this.parseOutputJson(stdout);
|
|
582
|
+
const outputStatus = normalizeOptionalString(parsedOutput?.status).toLowerCase();
|
|
583
|
+
const success = outputStatus ? ['success', 'ok', 'completed', 'pass'].includes(outputStatus) : true;
|
|
584
|
+
const remediationStatus = success ? 'success' : 'failed';
|
|
585
|
+
const remediationReason = normalizeOptionalString(parsedOutput?.reason)
|
|
586
|
+
|| (success ? 'Remediation hook completed successfully.' : 'Remediation hook reported a non-success status.');
|
|
587
|
+
|
|
588
|
+
await AiTerminalIssueReports.updateOne(
|
|
589
|
+
{ _id: issueId },
|
|
590
|
+
{
|
|
591
|
+
$set: {
|
|
592
|
+
'metadata.quality_manager.remediation': {
|
|
593
|
+
...remediation,
|
|
594
|
+
status: remediationStatus,
|
|
595
|
+
source,
|
|
596
|
+
trigger: normalizeOptionalString(options.trigger) || 'unspecified',
|
|
597
|
+
attempt_count: attemptCount,
|
|
598
|
+
max_attempts: maxAttempts,
|
|
599
|
+
cooldown_minutes: cooldownMinutes,
|
|
600
|
+
hook: this.config.remediationHookPath,
|
|
601
|
+
last_attempt_at: now,
|
|
602
|
+
last_success_at: success ? finishedAt : remediation.last_success_at,
|
|
603
|
+
last_error: success ? '' : remediationReason,
|
|
604
|
+
last_exit_code: success ? 0 : null,
|
|
605
|
+
last_duration_ms: durationMs,
|
|
606
|
+
last_stdout: stdout,
|
|
607
|
+
last_stderr: stderr,
|
|
608
|
+
last_output: parsedOutput || undefined,
|
|
609
|
+
updated_at: finishedAt
|
|
610
|
+
},
|
|
611
|
+
...(success ? { status: 'triaged' } : {}),
|
|
612
|
+
updatedAt: finishedAt
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
);
|
|
616
|
+
|
|
617
|
+
let replayResult: Record<string, any> | null = null;
|
|
618
|
+
if (success && this.config.autoReplayEnabled) {
|
|
619
|
+
replayResult = await this.runIssueReplay(issueId, {
|
|
620
|
+
source,
|
|
621
|
+
trigger: normalizeOptionalString(options.trigger) || 'post_remediation'
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
console.log(new Date(), '[AI Assistant Codex Manager] remediation completed', {
|
|
626
|
+
id_issue_report: issueId,
|
|
627
|
+
status: remediationStatus,
|
|
628
|
+
attempt: attemptCount,
|
|
629
|
+
durationMs,
|
|
630
|
+
replayStatus: replayResult?.status || null
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
return {
|
|
634
|
+
status: remediationStatus,
|
|
635
|
+
reason: remediationReason,
|
|
636
|
+
issueId,
|
|
637
|
+
attemptCount,
|
|
638
|
+
durationMs,
|
|
639
|
+
replayStatus: replayResult?.status || '',
|
|
640
|
+
replayReason: replayResult?.reason || ''
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
catch (error) {
|
|
644
|
+
const finishedAt = new Date();
|
|
645
|
+
const durationMs = Date.now() - startedAtMs;
|
|
646
|
+
const err: any = error || {};
|
|
647
|
+
const stdout = this.toSafeOutput(err.stdout);
|
|
648
|
+
const stderr = this.toSafeOutput(err.stderr);
|
|
649
|
+
const exitCode = Number.isFinite(Number(err.code)) ? Number(err.code) : null;
|
|
650
|
+
const timeoutHit = err?.killed === true || normalizeOptionalString(err?.signal).toUpperCase() === 'SIGTERM';
|
|
651
|
+
const reason = timeoutHit
|
|
652
|
+
? `Remediation hook timed out after ${this.config.remediationTimeoutMs}ms.`
|
|
653
|
+
: (normalizeOptionalString(err?.message) || 'Remediation hook failed.');
|
|
654
|
+
const nextAttemptAt = new Date(finishedAt.getTime() + cooldownMs);
|
|
655
|
+
|
|
656
|
+
await AiTerminalIssueReports.updateOne(
|
|
657
|
+
{ _id: issueId },
|
|
658
|
+
{
|
|
659
|
+
$set: {
|
|
660
|
+
'metadata.quality_manager.remediation': {
|
|
661
|
+
...remediation,
|
|
662
|
+
status: 'failed',
|
|
663
|
+
source,
|
|
664
|
+
trigger: normalizeOptionalString(options.trigger) || 'unspecified',
|
|
665
|
+
attempt_count: attemptCount,
|
|
666
|
+
max_attempts: maxAttempts,
|
|
667
|
+
cooldown_minutes: cooldownMinutes,
|
|
668
|
+
hook: this.config.remediationHookPath,
|
|
669
|
+
last_attempt_at: now,
|
|
670
|
+
last_error: reason,
|
|
671
|
+
last_exit_code: exitCode,
|
|
672
|
+
last_duration_ms: durationMs,
|
|
673
|
+
last_stdout: stdout,
|
|
674
|
+
last_stderr: stderr,
|
|
675
|
+
last_output: parsedOutput || undefined,
|
|
676
|
+
next_attempt_at: nextAttemptAt,
|
|
677
|
+
updated_at: finishedAt
|
|
678
|
+
},
|
|
679
|
+
updatedAt: finishedAt
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
);
|
|
683
|
+
|
|
684
|
+
console.error(new Date(), '[AI Assistant Codex Manager] remediation failed', {
|
|
685
|
+
id_issue_report: issueId,
|
|
686
|
+
attempt: attemptCount,
|
|
687
|
+
reason,
|
|
688
|
+
exitCode,
|
|
689
|
+
durationMs
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
return {
|
|
693
|
+
status: 'failed',
|
|
694
|
+
reason,
|
|
695
|
+
issueId,
|
|
696
|
+
attemptCount,
|
|
697
|
+
durationMs,
|
|
698
|
+
exitCode
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
finally {
|
|
702
|
+
if (contextPath) {
|
|
703
|
+
try {
|
|
704
|
+
await fs.unlink(contextPath);
|
|
705
|
+
}
|
|
706
|
+
catch {
|
|
707
|
+
// Ignore cleanup failures.
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
public async runIssueReplay(
|
|
714
|
+
issueReportId: string,
|
|
715
|
+
options: { force?: boolean; source?: 'auto' | 'manual'; trigger?: string } = {}
|
|
716
|
+
): Promise<Record<string, any>> {
|
|
717
|
+
const issueId = normalizeOptionalString(issueReportId);
|
|
718
|
+
if (!issueId) {
|
|
719
|
+
return {
|
|
720
|
+
status: 'failed',
|
|
721
|
+
reason: 'Issue report id is required.'
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
const source = options.source === 'manual' ? 'manual' : 'auto';
|
|
726
|
+
if (!this.config.autoReplayEnabled && source !== 'manual') {
|
|
727
|
+
return {
|
|
728
|
+
status: 'skipped',
|
|
729
|
+
reason: 'Auto replay is disabled.'
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
if (!this.config.replayCommand) {
|
|
733
|
+
return {
|
|
734
|
+
status: 'skipped',
|
|
735
|
+
reason: 'Replay command is not configured.'
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
const issue = await AiTerminalIssueReports.findById(issueId) as Record<string, any> | null;
|
|
740
|
+
if (!issue) {
|
|
741
|
+
return {
|
|
742
|
+
status: 'not_found',
|
|
743
|
+
reason: 'Issue report not found.'
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
const force = options.force === true;
|
|
748
|
+
const now = new Date();
|
|
749
|
+
const metadata = this.normalizeMetadata(issue.metadata);
|
|
750
|
+
const qualityManager = this.normalizeMetadata(metadata.quality_manager);
|
|
751
|
+
const replay = this.normalizeMetadata(qualityManager.replay);
|
|
752
|
+
const existingStatus = normalizeOptionalString(replay.status).toLowerCase();
|
|
753
|
+
const attemptsUsed = Number.isFinite(Number(replay.attempt_count)) ? Number(replay.attempt_count) : 0;
|
|
754
|
+
const maxAttempts = this.config.replayMaxAttemptsPerIssue;
|
|
755
|
+
const cooldownMinutes = this.config.replayCooldownMinutes;
|
|
756
|
+
const cooldownMs = cooldownMinutes * 60 * 1000;
|
|
757
|
+
const lastAttemptAt = this.parseDate(replay.last_attempt_at);
|
|
758
|
+
if (!force && existingStatus === 'running') {
|
|
759
|
+
return {
|
|
760
|
+
status: 'running',
|
|
761
|
+
reason: 'Replay already running.',
|
|
762
|
+
issueId
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
if (!force && existingStatus === 'success') {
|
|
766
|
+
return {
|
|
767
|
+
status: 'skipped',
|
|
768
|
+
reason: 'Replay already succeeded.',
|
|
769
|
+
issueId
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
if (!force && maxAttempts > 0 && attemptsUsed >= maxAttempts) {
|
|
773
|
+
return {
|
|
774
|
+
status: 'skipped',
|
|
775
|
+
reason: `Replay attempt budget reached (${attemptsUsed}/${maxAttempts}).`,
|
|
776
|
+
issueId
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
if (!force && existingStatus === 'failed' && lastAttemptAt) {
|
|
780
|
+
const ageMs = now.getTime() - lastAttemptAt.getTime();
|
|
781
|
+
if (ageMs >= 0 && ageMs < cooldownMs) {
|
|
782
|
+
return {
|
|
783
|
+
status: 'skipped',
|
|
784
|
+
reason: 'Replay cooldown active.',
|
|
785
|
+
issueId,
|
|
786
|
+
nextAttemptAt: new Date(lastAttemptAt.getTime() + cooldownMs)
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
const attemptCount = attemptsUsed + 1;
|
|
792
|
+
await AiTerminalIssueReports.updateOne(
|
|
793
|
+
{ _id: issueId },
|
|
794
|
+
{
|
|
795
|
+
$set: {
|
|
796
|
+
'metadata.quality_manager.replay': {
|
|
797
|
+
...replay,
|
|
798
|
+
status: 'running',
|
|
799
|
+
source,
|
|
800
|
+
trigger: normalizeOptionalString(options.trigger) || 'unspecified',
|
|
801
|
+
attempt_count: attemptCount,
|
|
802
|
+
max_attempts: maxAttempts,
|
|
803
|
+
cooldown_minutes: cooldownMinutes,
|
|
804
|
+
command: this.config.replayCommand,
|
|
805
|
+
last_attempt_at: now,
|
|
806
|
+
updated_at: now,
|
|
807
|
+
last_error: '',
|
|
808
|
+
last_exit_code: null
|
|
809
|
+
},
|
|
810
|
+
updatedAt: now
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
);
|
|
814
|
+
|
|
815
|
+
const startedAtMs = Date.now();
|
|
816
|
+
const contextPath = await this.writeReplayContextFile(issue, {
|
|
817
|
+
issueId,
|
|
818
|
+
attemptCount,
|
|
819
|
+
source,
|
|
820
|
+
trigger: normalizeOptionalString(options.trigger) || 'unspecified'
|
|
821
|
+
});
|
|
822
|
+
let parsedOutput: Record<string, any> | null = null;
|
|
823
|
+
try {
|
|
824
|
+
const execResult = await execFileAsync(
|
|
825
|
+
'/bin/bash',
|
|
826
|
+
['-lc', this.config.replayCommand],
|
|
827
|
+
{
|
|
828
|
+
timeout: this.config.replayTimeoutMs,
|
|
829
|
+
maxBuffer: this.config.replayMaxBufferBytes,
|
|
830
|
+
env: {
|
|
831
|
+
...process.env,
|
|
832
|
+
RIO_AI_ASSISTANT_REPLAY_CONTEXT: contextPath,
|
|
833
|
+
RIO_AI_ASSISTANT_REPLAY_ISSUE_ID: issueId
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
);
|
|
837
|
+
const finishedAt = new Date();
|
|
838
|
+
const durationMs = Date.now() - startedAtMs;
|
|
839
|
+
const stdout = this.toSafeOutput(execResult?.stdout);
|
|
840
|
+
const stderr = this.toSafeOutput(execResult?.stderr);
|
|
841
|
+
parsedOutput = this.parseOutputJson(stdout);
|
|
842
|
+
const outputStatus = normalizeOptionalString(parsedOutput?.status).toLowerCase();
|
|
843
|
+
const success = outputStatus ? ['success', 'ok', 'completed', 'pass'].includes(outputStatus) : true;
|
|
844
|
+
const replayStatus = success ? 'success' : 'failed';
|
|
845
|
+
const replayReason = normalizeOptionalString(parsedOutput?.reason)
|
|
846
|
+
|| (success ? 'Replay command completed successfully.' : 'Replay command reported a non-success status.');
|
|
847
|
+
|
|
848
|
+
await AiTerminalIssueReports.updateOne(
|
|
849
|
+
{ _id: issueId },
|
|
850
|
+
{
|
|
851
|
+
$set: {
|
|
852
|
+
'metadata.quality_manager.replay': {
|
|
853
|
+
...replay,
|
|
854
|
+
status: replayStatus,
|
|
855
|
+
source,
|
|
856
|
+
trigger: normalizeOptionalString(options.trigger) || 'unspecified',
|
|
857
|
+
attempt_count: attemptCount,
|
|
858
|
+
max_attempts: maxAttempts,
|
|
859
|
+
cooldown_minutes: cooldownMinutes,
|
|
860
|
+
command: this.config.replayCommand,
|
|
861
|
+
last_attempt_at: now,
|
|
862
|
+
last_success_at: success ? finishedAt : replay.last_success_at,
|
|
863
|
+
last_error: success ? '' : replayReason,
|
|
864
|
+
last_exit_code: success ? 0 : null,
|
|
865
|
+
last_duration_ms: durationMs,
|
|
866
|
+
last_stdout: stdout,
|
|
867
|
+
last_stderr: stderr,
|
|
868
|
+
last_output: parsedOutput || undefined,
|
|
869
|
+
updated_at: finishedAt
|
|
870
|
+
},
|
|
871
|
+
updatedAt: finishedAt
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
);
|
|
875
|
+
|
|
876
|
+
console.log(new Date(), '[AI Assistant Codex Manager] replay completed', {
|
|
877
|
+
id_issue_report: issueId,
|
|
878
|
+
status: replayStatus,
|
|
879
|
+
attempt: attemptCount,
|
|
880
|
+
durationMs
|
|
881
|
+
});
|
|
882
|
+
|
|
883
|
+
return {
|
|
884
|
+
status: replayStatus,
|
|
885
|
+
reason: replayReason,
|
|
886
|
+
issueId,
|
|
887
|
+
attemptCount,
|
|
888
|
+
durationMs
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
catch (error) {
|
|
892
|
+
const finishedAt = new Date();
|
|
893
|
+
const durationMs = Date.now() - startedAtMs;
|
|
894
|
+
const err: any = error || {};
|
|
895
|
+
const stdout = this.toSafeOutput(err.stdout);
|
|
896
|
+
const stderr = this.toSafeOutput(err.stderr);
|
|
897
|
+
const exitCode = Number.isFinite(Number(err.code)) ? Number(err.code) : null;
|
|
898
|
+
const timeoutHit = err?.killed === true || normalizeOptionalString(err?.signal).toUpperCase() === 'SIGTERM';
|
|
899
|
+
const reason = timeoutHit
|
|
900
|
+
? `Replay command timed out after ${this.config.replayTimeoutMs}ms.`
|
|
901
|
+
: (normalizeOptionalString(err?.message) || 'Replay command failed.');
|
|
902
|
+
const nextAttemptAt = new Date(finishedAt.getTime() + cooldownMs);
|
|
903
|
+
|
|
904
|
+
await AiTerminalIssueReports.updateOne(
|
|
905
|
+
{ _id: issueId },
|
|
906
|
+
{
|
|
907
|
+
$set: {
|
|
908
|
+
'metadata.quality_manager.replay': {
|
|
909
|
+
...replay,
|
|
910
|
+
status: 'failed',
|
|
911
|
+
source,
|
|
912
|
+
trigger: normalizeOptionalString(options.trigger) || 'unspecified',
|
|
913
|
+
attempt_count: attemptCount,
|
|
914
|
+
max_attempts: maxAttempts,
|
|
915
|
+
cooldown_minutes: cooldownMinutes,
|
|
916
|
+
command: this.config.replayCommand,
|
|
917
|
+
last_attempt_at: now,
|
|
918
|
+
last_error: reason,
|
|
919
|
+
last_exit_code: exitCode,
|
|
920
|
+
last_duration_ms: durationMs,
|
|
921
|
+
last_stdout: stdout,
|
|
922
|
+
last_stderr: stderr,
|
|
923
|
+
last_output: parsedOutput || undefined,
|
|
924
|
+
next_attempt_at: nextAttemptAt,
|
|
925
|
+
updated_at: finishedAt
|
|
926
|
+
},
|
|
927
|
+
updatedAt: finishedAt
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
);
|
|
931
|
+
|
|
932
|
+
console.error(new Date(), '[AI Assistant Codex Manager] replay failed', {
|
|
933
|
+
id_issue_report: issueId,
|
|
934
|
+
attempt: attemptCount,
|
|
935
|
+
reason,
|
|
936
|
+
exitCode,
|
|
937
|
+
durationMs
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
return {
|
|
941
|
+
status: 'failed',
|
|
942
|
+
reason,
|
|
943
|
+
issueId,
|
|
944
|
+
attemptCount,
|
|
945
|
+
durationMs,
|
|
946
|
+
exitCode
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
finally {
|
|
950
|
+
if (contextPath) {
|
|
951
|
+
try {
|
|
952
|
+
await fs.unlink(contextPath);
|
|
953
|
+
}
|
|
954
|
+
catch {
|
|
955
|
+
// Ignore cleanup failures.
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
private async markMessageQualityReviewed(messageId: string, decision: AiAssistantQualityDecision, issueReportId = ''): Promise<void> {
|
|
962
|
+
const now = new Date();
|
|
963
|
+
const qualityPayload: Record<string, any> = {
|
|
964
|
+
status: decision.status,
|
|
965
|
+
reason: decision.reason,
|
|
966
|
+
code: decision.code,
|
|
967
|
+
checked_at: now,
|
|
968
|
+
worker_index: normalizeOptionalString(process.env.WORKER_INDEX),
|
|
969
|
+
worker_instance: normalizeOptionalString(process.env.NODE_APP_INSTANCE)
|
|
970
|
+
};
|
|
971
|
+
if (issueReportId) {
|
|
972
|
+
qualityPayload.id_issue_report = issueReportId;
|
|
973
|
+
}
|
|
974
|
+
await AiTerminalMessages.updateOne(
|
|
975
|
+
{ _id: messageId },
|
|
976
|
+
{
|
|
977
|
+
$set: {
|
|
978
|
+
'metadata.quality_manager': qualityPayload,
|
|
979
|
+
updatedAt: now
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
);
|
|
983
|
+
if (this.config.debug) {
|
|
984
|
+
console.log(new Date(), '[AI Assistant Codex Manager] checked message', {
|
|
985
|
+
id_message: messageId,
|
|
986
|
+
status: decision.status,
|
|
987
|
+
code: decision.code,
|
|
988
|
+
id_issue_report: issueReportId || null
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
private async resolveClosestUserPrompt(conversationId: string, assistantCreatedAt?: Date): Promise<string> {
|
|
994
|
+
const query: Record<string, any> = {
|
|
995
|
+
id_conversation: conversationId,
|
|
996
|
+
role: 'user'
|
|
997
|
+
};
|
|
998
|
+
const createdAt = assistantCreatedAt ? new Date(assistantCreatedAt) : null;
|
|
999
|
+
if (createdAt && Number.isFinite(createdAt.getTime())) {
|
|
1000
|
+
query.createdAt = { $lte: createdAt };
|
|
1001
|
+
}
|
|
1002
|
+
const userMessage = await AiTerminalMessages.findOne(query, { sort: { createdAt: -1 } }) as AiTerminalMessageModel | null;
|
|
1003
|
+
return normalizeOptionalString(userMessage?.content);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
private async writeRemediationContextFile(issue: Record<string, any>, options: Record<string, any>): Promise<string> {
|
|
1007
|
+
const issueId = normalizeOptionalString(issue?._id);
|
|
1008
|
+
const filePath = path.join(os.tmpdir(), `resolveio-ai-assistant-remediation-${issueId}-${Date.now()}.json`);
|
|
1009
|
+
const payload = {
|
|
1010
|
+
created_at: new Date().toISOString(),
|
|
1011
|
+
issue_report: {
|
|
1012
|
+
_id: issueId,
|
|
1013
|
+
status: normalizeOptionalString(issue?.status),
|
|
1014
|
+
source: normalizeOptionalString(issue?.source),
|
|
1015
|
+
reason: normalizeOptionalString(issue?.reason),
|
|
1016
|
+
id_conversation: normalizeOptionalString(issue?.id_conversation),
|
|
1017
|
+
id_message: normalizeOptionalString(issue?.id_message),
|
|
1018
|
+
id_client: normalizeOptionalString(issue?.id_client),
|
|
1019
|
+
id_app: normalizeOptionalString(issue?.id_app),
|
|
1020
|
+
id_user: normalizeOptionalString(issue?.id_user),
|
|
1021
|
+
request_id: normalizeOptionalString(issue?.request_id),
|
|
1022
|
+
user_prompt: normalizeOptionalString(issue?.user_prompt),
|
|
1023
|
+
assistant_content: normalizeOptionalString(issue?.assistant_content)
|
|
1024
|
+
},
|
|
1025
|
+
remediation: {
|
|
1026
|
+
source: normalizeOptionalString(options?.source),
|
|
1027
|
+
trigger: normalizeOptionalString(options?.trigger),
|
|
1028
|
+
attempt_count: Number(options?.attemptCount) || 0,
|
|
1029
|
+
max_attempts: this.config.remediationMaxAttemptsPerIssue,
|
|
1030
|
+
cooldown_minutes: this.config.remediationCooldownMinutes
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
await fs.writeFile(filePath, JSON.stringify(payload, null, 2), 'utf8');
|
|
1034
|
+
return filePath;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
private async writeReplayContextFile(issue: Record<string, any>, options: Record<string, any>): Promise<string> {
|
|
1038
|
+
const issueId = normalizeOptionalString(issue?._id);
|
|
1039
|
+
const metadata = this.normalizeMetadata(issue?.metadata);
|
|
1040
|
+
const qualityManager = this.normalizeMetadata(metadata.quality_manager);
|
|
1041
|
+
const remediation = this.normalizeMetadata(qualityManager.remediation);
|
|
1042
|
+
const filePath = path.join(os.tmpdir(), `resolveio-ai-assistant-replay-${issueId}-${Date.now()}.json`);
|
|
1043
|
+
const payload = {
|
|
1044
|
+
created_at: new Date().toISOString(),
|
|
1045
|
+
issue_report: {
|
|
1046
|
+
_id: issueId,
|
|
1047
|
+
status: normalizeOptionalString(issue?.status),
|
|
1048
|
+
source: normalizeOptionalString(issue?.source),
|
|
1049
|
+
reason: normalizeOptionalString(issue?.reason),
|
|
1050
|
+
id_conversation: normalizeOptionalString(issue?.id_conversation),
|
|
1051
|
+
id_message: normalizeOptionalString(issue?.id_message),
|
|
1052
|
+
id_client: normalizeOptionalString(issue?.id_client),
|
|
1053
|
+
id_app: normalizeOptionalString(issue?.id_app),
|
|
1054
|
+
id_user: normalizeOptionalString(issue?.id_user),
|
|
1055
|
+
request_id: normalizeOptionalString(issue?.request_id),
|
|
1056
|
+
user_prompt: normalizeOptionalString(issue?.user_prompt),
|
|
1057
|
+
assistant_content: normalizeOptionalString(issue?.assistant_content)
|
|
1058
|
+
},
|
|
1059
|
+
remediation: {
|
|
1060
|
+
status: normalizeOptionalString(remediation.status),
|
|
1061
|
+
last_attempt_at: remediation.last_attempt_at || null,
|
|
1062
|
+
last_success_at: remediation.last_success_at || null,
|
|
1063
|
+
last_error: normalizeOptionalString(remediation.last_error),
|
|
1064
|
+
attempt_count: Number(remediation.attempt_count) || 0
|
|
1065
|
+
},
|
|
1066
|
+
replay: {
|
|
1067
|
+
source: normalizeOptionalString(options?.source),
|
|
1068
|
+
trigger: normalizeOptionalString(options?.trigger),
|
|
1069
|
+
attempt_count: Number(options?.attemptCount) || 0,
|
|
1070
|
+
max_attempts: this.config.replayMaxAttemptsPerIssue,
|
|
1071
|
+
cooldown_minutes: this.config.replayCooldownMinutes,
|
|
1072
|
+
command: this.config.replayCommand
|
|
1073
|
+
}
|
|
1074
|
+
};
|
|
1075
|
+
await fs.writeFile(filePath, JSON.stringify(payload, null, 2), 'utf8');
|
|
1076
|
+
return filePath;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
private parseOutputJson(stdout: string): Record<string, any> | null {
|
|
1080
|
+
const text = normalizeOptionalString(stdout);
|
|
1081
|
+
if (!text) {
|
|
1082
|
+
return null;
|
|
1083
|
+
}
|
|
1084
|
+
const firstChar = text.charAt(0);
|
|
1085
|
+
if (firstChar !== '{' && firstChar !== '[') {
|
|
1086
|
+
return null;
|
|
1087
|
+
}
|
|
1088
|
+
try {
|
|
1089
|
+
const parsed = JSON.parse(text);
|
|
1090
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
1091
|
+
return null;
|
|
1092
|
+
}
|
|
1093
|
+
return parsed;
|
|
1094
|
+
}
|
|
1095
|
+
catch {
|
|
1096
|
+
return null;
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
private toSafeOutput(value: any): string {
|
|
1101
|
+
const asString = typeof value === 'string'
|
|
1102
|
+
? value
|
|
1103
|
+
: (Buffer.isBuffer(value) ? value.toString('utf8') : String(value || ''));
|
|
1104
|
+
const normalized = asString.trim();
|
|
1105
|
+
if (!normalized) {
|
|
1106
|
+
return '';
|
|
1107
|
+
}
|
|
1108
|
+
if (normalized.length <= MAX_STORED_OUTPUT_CHARS) {
|
|
1109
|
+
return normalized;
|
|
1110
|
+
}
|
|
1111
|
+
return normalized.slice(0, MAX_STORED_OUTPUT_CHARS);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
private parseDate(value: any): Date | null {
|
|
1115
|
+
if (!value) {
|
|
1116
|
+
return null;
|
|
1117
|
+
}
|
|
1118
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
1119
|
+
if (!Number.isFinite(date.getTime())) {
|
|
1120
|
+
return null;
|
|
1121
|
+
}
|
|
1122
|
+
return date;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
private normalizeMetadata(value: any): Record<string, any> {
|
|
1126
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
1127
|
+
return {};
|
|
1128
|
+
}
|
|
1129
|
+
return value;
|
|
1130
|
+
}
|
|
1131
|
+
}
|