@xopcai/xopc 0.0.362 → 0.0.364
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/dist/browser-ext/manifest.json +1 -1
- package/dist/extensions/telegram/xopc.extension.json +1 -1
- package/dist/gateway/static/root/assets/{activity-store-_2aJ4ozN.js → activity-store-GvR2gZ0T.js} +1 -1
- package/dist/gateway/static/root/assets/{agent-avatar-dicebear-CSIn4ghK.js → agent-avatar-dicebear-7Ct-ZlfL.js} +1 -1
- package/dist/gateway/static/root/assets/{agent-defaults-settings-panel-DBNFaHYa.js → agent-defaults-settings-panel-wYj-iyqv.js} +1 -1
- package/dist/gateway/static/root/assets/{agent-plugin-dialog-DJkGbMVg.js → agent-plugin-dialog-C9HYMhnJ.js} +1 -1
- package/dist/gateway/static/root/assets/{agents-admin-api-Dl0V7UQq.js → agents-admin-api-BkXb7VEZ.js} +1 -1
- package/dist/gateway/static/root/assets/{agents-CuQayYYr.js → agents-hPkFQ81z.js} +3 -3
- package/dist/gateway/static/root/assets/{app-management-settings-panel-CSczq9QV.js → app-management-settings-panel-CjfsIn9A.js} +1 -1
- package/dist/gateway/static/root/assets/{appearance-settings-W0vc1J1Q.js → appearance-settings-qqHZ15GU.js} +1 -1
- package/dist/gateway/static/root/assets/{apps-page-esnvb3hh.js → apps-page-Ba2VkQk6.js} +1 -1
- package/dist/gateway/static/root/assets/{archive-plugin-CBR91gHK.js → archive-plugin-CKe3qAaL.js} +1 -1
- package/dist/gateway/static/root/assets/{automations-page-B9RdzKUS.js → automations-page-Bt8Q58ut.js} +1 -1
- package/dist/gateway/static/root/assets/{automations-workspace-U9TEZd_t.js → automations-workspace-Ch0ViAJW.js} +1 -1
- package/dist/gateway/static/root/assets/{binary-plugins-C9KIjPph.js → binary-plugins-J51oS7-D.js} +1 -1
- package/dist/gateway/static/root/assets/{block-editor-C0bammwh.js → block-editor-ClRFbHP7.js} +1 -1
- package/dist/gateway/static/root/assets/{browser-automation-inputs-D2E0co_S.js → browser-automation-inputs-5B3qAdmb.js} +1 -1
- package/dist/gateway/static/root/assets/{browser-automations-page-8kqdhAOz.js → browser-automations-page-BjMub7JM.js} +1 -1
- package/dist/gateway/static/root/assets/{browser-settings-page-Dl4idi13.js → browser-settings-page-CYrPA-Fp.js} +1 -1
- package/dist/gateway/static/root/assets/{capabilities-Bfktn_7E.js → capabilities-BKu9BqPe.js} +1 -1
- package/dist/gateway/static/root/assets/{capabilities-BmMMlNmY.js → capabilities-CT_ZW9fT.js} +1 -1
- package/dist/gateway/static/root/assets/{capabilities-page-3ImhKPBx.js → capabilities-page-dW6FXtOy.js} +2 -2
- package/dist/gateway/static/root/assets/{capabilities-settings-panel-4VDEB_ob.js → capabilities-settings-panel-gZ8auLwa.js} +1 -1
- package/dist/gateway/static/root/assets/{capability-header-actions-D1gxCGi8.js → capability-header-actions-L1NvoOUG.js} +1 -1
- package/dist/gateway/static/root/assets/{channels-Drbr2JR-.js → channels-Dx5T2G73.js} +1 -1
- package/dist/gateway/static/root/assets/{chat-terminal-dock-CSkksCaa.js → chat-terminal-dock-DsCpKPBe.js} +1 -1
- package/dist/gateway/static/root/assets/{computer-settings-page-BIXpOJAk.js → computer-settings-page-6FWwxYLe.js} +1 -1
- package/dist/gateway/static/root/assets/{connector-service-page-CTnlVEE1.js → connector-service-page-mtH9mEBf.js} +1 -1
- package/dist/gateway/static/root/assets/{connectors-page-BluT2l4d.js → connectors-page-DgDLaws0.js} +1 -1
- package/dist/gateway/static/root/assets/{date-picker-Bw-CydDe.js → date-picker-BDcwEaIx.js} +1 -1
- package/dist/gateway/static/root/assets/{dependency-picker-CgHzhtAc.js → dependency-picker-B_fPQBTM.js} +1 -1
- package/dist/gateway/static/root/assets/{desktop-pet-YahEnyfd.js → desktop-pet-CJrO9Vd_.js} +1 -1
- package/dist/gateway/static/root/assets/{desktop-pet-settings-47Z4bqJr.js → desktop-pet-settings-DYAfri9w.js} +1 -1
- package/dist/gateway/static/root/assets/{device-pairing-wizard-C3aPEEaM.js → device-pairing-wizard-CqRys2R8.js} +1 -1
- package/dist/gateway/static/root/assets/{directory-picker-path-field-DH_ok92y.js → directory-picker-path-field-D7_qSxNZ.js} +1 -1
- package/dist/gateway/static/root/assets/{extension-debug-page-t1TiLQrR.js → extension-debug-page-BGyJpRv4.js} +1 -1
- package/dist/gateway/static/root/assets/{extension-page-BeV-7G1h.js → extension-page-D1zBmd8F.js} +1 -1
- package/dist/gateway/static/root/assets/{extension-provider-CFCxzqna.js → extension-provider-F9JzOWZ3.js} +1 -1
- package/dist/gateway/static/root/assets/{extension-settings-page-DCODJHOu.js → extension-settings-page-CTR8AjCG.js} +1 -1
- package/dist/gateway/static/root/assets/{fallback-models-CJzvASNV.js → fallback-models-Chhv4Nfv.js} +1 -1
- package/dist/gateway/static/root/assets/{gateway-config-swr-B3LfyXpc.js → gateway-config-swr-CoRoOG1l.js} +1 -1
- package/dist/gateway/static/root/assets/{gateway-realtime-CWxXXAe_.js → gateway-realtime-D0ygm6bc.js} +1 -1
- package/dist/gateway/static/root/assets/gateway-realtime-store-DBvCRclh.js +1 -0
- package/dist/gateway/static/root/assets/{gateway-settings-Q5NFzAJ_.js → gateway-settings-BVfz4Tla.js} +1 -1
- package/dist/gateway/static/root/assets/{gateway-startup-retry-BzQP02wp.js → gateway-startup-retry-FDS6S6qa.js} +1 -1
- package/dist/gateway/static/root/assets/{home-page-DndTkAw8.js → home-page-BF_9C9yW.js} +1 -1
- package/dist/gateway/static/root/assets/{image-generation-api-BO-Xs__S.js → image-generation-api-BPd76jW5.js} +1 -1
- package/dist/gateway/static/root/assets/{imports-page-Bb6Gm30L.js → imports-page-9EnECtcl.js} +1 -1
- package/dist/gateway/static/root/assets/{index-qOHWhsEK.js → index-BpXej9jJ.js} +7 -7
- package/dist/gateway/static/root/assets/index-CbJNcWAn.css +1 -0
- package/dist/gateway/static/root/assets/{keyboard-shortcuts-settings-D2u6sk5l.js → keyboard-shortcuts-settings-MC7mG2h2.js} +1 -1
- package/dist/gateway/static/root/assets/{local-app-workbench-page-CHeMfXrB.js → local-app-workbench-page-CytNu0-_.js} +1 -1
- package/dist/gateway/static/root/assets/{local-apps-page-BanU1SLr.js → local-apps-page-c3HncHZ3.js} +1 -1
- package/dist/gateway/static/root/assets/{local-session-drafts-DLZQBcZY.js → local-session-drafts-gbemPamv.js} +1 -1
- package/dist/gateway/static/root/assets/{locale-store-BbwWB0ra.js → locale-store-DId6-v2R.js} +1 -1
- package/dist/gateway/static/root/assets/{logs-page-CQdHKCL0.js → logs-page-D4aXtitd.js} +1 -1
- package/dist/gateway/static/root/assets/{management-settings-D0V5kCx0.js → management-settings-CRzWi_iJ.js} +1 -1
- package/dist/gateway/static/root/assets/{markdown-view-DIBLXsPm.js → markdown-view-DQZ51j9e.js} +1 -1
- package/dist/gateway/static/root/assets/{media-plugins-XPzvDZPz.js → media-plugins-XLewwq2q.js} +1 -1
- package/dist/gateway/static/root/assets/{messages-B4AhAW4B.js → messages-BHvpZNe9.js} +2 -2
- package/dist/gateway/static/root/assets/models-hub-cache-B9SKjidc.js +1 -0
- package/dist/gateway/static/root/assets/{note-detail-panel-CodeKI_W.js → note-detail-panel-BgbByI-4.js} +3 -3
- package/dist/gateway/static/root/assets/{notes-api-CLn4r03Z.js → notes-api-Byt65v3r.js} +1 -1
- package/dist/gateway/static/root/assets/{notes-page-liRQoHbF.js → notes-page-R2BX5XCE.js} +1 -1
- package/dist/gateway/static/root/assets/{oauth-api-D9zkd42J.js → oauth-api-BYsCbyGZ.js} +1 -1
- package/dist/gateway/static/root/assets/{page-context-capture-button-DO6DB9pi.js → page-context-capture-button-RBp1QK0F.js} +1 -1
- package/dist/gateway/static/root/assets/{personal-page-DdfdwOv5.js → personal-page-F0jTBWva.js} +1 -1
- package/dist/gateway/static/root/assets/{product-open-page-C9a3sm6Z.js → product-open-page-BtDK68qb.js} +1 -1
- package/dist/gateway/static/root/assets/{project-detail-page-BVe5R-Nj.js → project-detail-page-DmQEm0yH.js} +1 -1
- package/dist/gateway/static/root/assets/{projects-page-D_rCwi7w.js → projects-page-CrLQvkHX.js} +1 -1
- package/dist/gateway/static/root/assets/{registry-api-BFxQTbXQ.js → registry-api-7F2WNEZC.js} +1 -1
- package/dist/gateway/static/root/assets/{remote-access-hub-B3imt2du.js → remote-access-hub-C25_hQAd.js} +1 -1
- package/dist/gateway/static/root/assets/{runtime-tools-settings-panel-C9WC8hJM.js → runtime-tools-settings-panel-B0c120iI.js} +1 -1
- package/dist/gateway/static/root/assets/{scenes-page-DL95V1UP.js → scenes-page-DM64yMnx.js} +1 -1
- package/dist/gateway/static/root/assets/{schema-form-CDgGmgrq.js → schema-form-DQpQc9tH.js} +1 -1
- package/dist/gateway/static/root/assets/{send-session-input-tkAQXGBG.js → send-session-input-D1oc6BC6.js} +1 -1
- package/dist/gateway/static/root/assets/{session-api-9X7qep91.js → session-api-N3N37MrR.js} +1 -1
- package/dist/gateway/static/root/assets/{session-manager-Dy22bbJT.js → session-manager-CMGUwtbN.js} +1 -1
- package/dist/gateway/static/root/assets/{sessions-page-CeeKIVsM.js → sessions-page-DvDO8LtA.js} +1 -1
- package/dist/gateway/static/root/assets/settings-advanced-gate-BNhgbmcX.js +1 -0
- package/dist/gateway/static/root/assets/{settings-page-xmUGYSOG.js → settings-page-GgBEp6O7.js} +1 -1
- package/dist/gateway/static/root/assets/{setup-status-panel-CmjT9Q4P.js → setup-status-panel-10bMi6yL.js} +1 -1
- package/dist/gateway/static/root/assets/{share-link-dialog-DeOe9qfQ.js → share-link-dialog-TBBGJPfT.js} +1 -1
- package/dist/gateway/static/root/assets/{share-preview-page-Ck27gozJ.js → share-preview-page-DyRLPsPv.js} +1 -1
- package/dist/gateway/static/root/assets/{shares-api-D-GuMkPu.js → shares-api-DU0fHP2G.js} +1 -1
- package/dist/gateway/static/root/assets/{shares-settings-DpvM2OyO.js → shares-settings-DJ1ME-XM.js} +1 -1
- package/dist/gateway/static/root/assets/{skill-api-BpD02td8.js → skill-api-1Puv6wOz.js} +1 -1
- package/dist/gateway/static/root/assets/{skill-reload-api-CezAwYxM.js → skill-reload-api-ClyFqHk7.js} +1 -1
- package/dist/gateway/static/root/assets/{skills-page-BCgbVI-P.js → skills-page-D52pEr5x.js} +1 -1
- package/dist/gateway/static/root/assets/{system-settings-panel-UODcpzsP.js → system-settings-panel--yV67a8L.js} +1 -1
- package/dist/gateway/static/root/assets/{task-detail-page-BbqQnNes.js → task-detail-page-D-tcS2bS.js} +1 -1
- package/dist/gateway/static/root/assets/{theme-store-6IK2Ik2J.js → theme-store-OhcbFEUR.js} +1 -1
- package/dist/gateway/static/root/assets/tracing-settings-page-DoBJ0TYY.js +1 -0
- package/dist/gateway/static/root/assets/{tunnel-api-Eey2aZIz.js → tunnel-api-COLdiP_E.js} +1 -1
- package/dist/gateway/static/root/assets/{usage-page-BrWj5mk3.js → usage-page-CtM5IfrL.js} +1 -1
- package/dist/gateway/static/root/assets/{use-autosave-B-3BJbnn.js → use-autosave-veoM0dHn.js} +1 -1
- package/dist/gateway/static/root/assets/{use-composer-attachments-ByAHuR8v.js → use-composer-attachments-DB0bBGzZ.js} +1 -1
- package/dist/gateway/static/root/assets/{use-file-preview-expanded-CXVwWJeP.js → use-file-preview-expanded-Bope6MW6.js} +2 -2
- package/dist/gateway/static/root/assets/{user-model-page-Cg-oM8nY.js → user-model-page-CSh61kZ5.js} +1 -1
- package/dist/gateway/static/root/assets/{voice-api-key-field-BtffIKZD.js → voice-api-key-field-CwD6a3KQ.js} +1 -1
- package/dist/gateway/static/root/assets/{voice-config-api-BMm9dSKX.js → voice-config-api-Daexehmc.js} +1 -1
- package/dist/gateway/static/root/assets/{work-discovery-overlay-ByHhF_4r.js → work-discovery-overlay-C9u7tuEc.js} +1 -1
- package/dist/gateway/static/root/assets/{workflow-run-setup-panel-DfNn5VTy.js → workflow-run-setup-panel-BMOBbf33.js} +1 -1
- package/dist/gateway/static/root/assets/{workflows-page-B67D-7Wt.js → workflows-page-BrD9vXqO.js} +1 -1
- package/dist/gateway/static/root/assets/{workspace-api-TAD7LYsc.js → workspace-api-Cs4sjV2A.js} +2 -2
- package/dist/gateway/static/root/index.html +28 -28
- package/dist/package.js +1 -1
- package/dist/src/agent/agent-manager.js +11 -7
- package/dist/src/agent/background-review/run-background-review.js +1 -1
- package/dist/src/agent/embedded/model-runtime.js +1 -1
- package/dist/src/agent/embedded/run-for-session.js +28 -3
- package/dist/src/agent/embedded/run-turn.js +18 -3
- package/dist/src/agent/external-tools/endpoint-provider.js +2 -2
- package/dist/src/agent/image/tool-model-config.js +1 -1
- package/dist/src/agent/mcp/oauth/mcp-oauth-store.js +1 -1
- package/dist/src/agent/service.js +6 -4
- package/dist/src/agent/skills/hub-lock.js +1 -1
- package/dist/src/agent/skills/index.js +1 -1
- package/dist/src/agent/skills/managed-store.js +1 -1
- package/dist/src/agent/skills/skill-manage-ops.js +1 -1
- package/dist/src/agent/tools/xopc-use-tool.js +2 -2
- package/dist/src/agent-catalog/capabilities.js +2 -2
- package/dist/src/auth/credentials.js +2 -2
- package/dist/src/auth/oauth-provider-lock.js +1 -1
- package/dist/src/auth/profiles/store.js +1 -1
- package/dist/src/auth/sync-provider-auth.js +1 -1
- package/dist/src/automations/capabilities/drafts.js +1 -1
- package/dist/src/bootstrap/application-state.js +1 -1
- package/dist/src/browser/providers/browser-ext-install.js +1 -1
- package/dist/src/capabilities/runtime/dispatcher.js +1 -1
- package/dist/src/capabilities/runtime/product.js +1 -1
- package/dist/src/chat-commands/agent-edit.js +1 -1
- package/dist/src/chat-commands/builtins/config.js +1 -1
- package/dist/src/cli/commands/agent.js +1 -1
- package/dist/src/cli/commands/agents.js +1 -1
- package/dist/src/cli/commands/doctor/checks/database-schema.js +1 -1
- package/dist/src/cli/commands/doctor/checks/image-providers.js +1 -1
- package/dist/src/cli/commands/doctor/checks/provider-auth.js +3 -3
- package/dist/src/cli/commands/gateway/lifecycle.js +1 -1
- package/dist/src/cli/commands/models.js +2 -2
- package/dist/src/cli/commands/onboard/model.js +4 -4
- package/dist/src/cli/utils/init-workspace-core.js +1 -1
- package/dist/src/config/index.js +4 -4
- package/dist/src/config/loader.js +2 -2
- package/dist/src/config/models-json.js +1 -1
- package/dist/src/config/reload.d.ts +1 -0
- package/dist/src/config/reload.js +4 -0
- package/dist/src/config/rules.js +5 -0
- package/dist/src/config/schema.d.ts +23 -0
- package/dist/src/config/schema.js +6 -3
- package/dist/src/extensions/activation-context.js +1 -1
- package/dist/src/extensions/agent-plugins/validation.js +1 -1
- package/dist/src/extensions/lockfile.js +1 -1
- package/dist/src/gateway/agents-admin.js +2 -2
- package/dist/src/gateway/file-path-classifier.js +1 -1
- package/dist/src/gateway/hono/lib/config-payload.js +2 -2
- package/dist/src/gateway/hono/routes/config-patch/misc.js +3 -3
- package/dist/src/gateway/hono/routes/connectors.js +1 -1
- package/dist/src/gateway/hono/routes/lazy-bundles.js +8 -0
- package/dist/src/gateway/hono/routes/models.js +2 -2
- package/dist/src/gateway/hono/routes/notes.js +2 -2
- package/dist/src/gateway/hono/routes/observability.d.ts +3 -0
- package/dist/src/gateway/hono/routes/observability.js +106 -0
- package/dist/src/gateway/hono/routes/projects.js +2 -2
- package/dist/src/gateway/hono/routes/session-input-handler.js +1 -1
- package/dist/src/gateway/hono/routes/tasks.js +1 -1
- package/dist/src/gateway/image-generation-setup.js +2 -2
- package/dist/src/gateway/lock.js +1 -1
- package/dist/src/gateway/security/gateway-scopes.js +1 -0
- package/dist/src/gateway/service/config-coordinator.d.ts +0 -25
- package/dist/src/gateway/service/config-coordinator.js +31 -0
- package/dist/src/gateway/service.js +18 -8
- package/dist/src/gateway/xopc-cloud-capability-setup.js +1 -1
- package/dist/src/home-intelligence/host.js +2 -2
- package/dist/src/infra/update-restart.js +1 -1
- package/dist/src/local-apps/service.js +1 -1
- package/dist/src/observability/config.d.ts +40 -0
- package/dist/src/observability/config.js +31 -0
- package/dist/src/observability/langfuse.d.ts +31 -0
- package/dist/src/observability/langfuse.js +180 -0
- package/dist/src/observability/runtime.d.ts +57 -0
- package/dist/src/observability/runtime.js +404 -0
- package/dist/src/observability/sanitize.d.ts +3 -0
- package/dist/src/observability/sanitize.js +74 -0
- package/dist/src/observability/store.d.ts +20 -0
- package/dist/src/observability/store.js +128 -0
- package/dist/src/observability/storeWorker.d.ts +2 -0
- package/dist/src/observability/storeWorker.js +155 -0
- package/dist/src/observability/types.d.ts +21 -0
- package/dist/src/observability/types.js +1 -0
- package/dist/src/personal-agent/communication.js +1 -1
- package/dist/src/personal-agent/reply-composer.d.ts +22 -0
- package/dist/src/personal-agent/reply-composer.js +276 -0
- package/dist/src/personal-agent/reply-style.d.ts +2 -0
- package/dist/src/personal-agent/reply-style.js +14 -0
- package/dist/src/personal-agent/request-delivery.d.ts +1 -1
- package/dist/src/personal-agent/request-delivery.js +18 -4
- package/dist/src/personal-agent/request-service.js +1 -1
- package/dist/src/personal-agent/service.js +10 -2
- package/dist/src/platform/index.js +1 -1
- package/dist/src/providers/auth-runtime/resolve-auth.js +1 -1
- package/dist/src/providers/index.js +3 -3
- package/dist/src/providers/model-call.js +19 -5
- package/dist/src/providers/model-catalog-persistence.js +1 -1
- package/dist/src/providers/model-registry.js +1 -1
- package/dist/src/providers/provider-disconnect-core.js +1 -1
- package/dist/src/providers/xopc-cloud-catalog-coordinator.js +1 -1
- package/dist/src/storage/sqlite/ai-usage-repository.d.ts +6 -0
- package/dist/src/storage/sqlite/ai-usage-repository.js +4 -2
- package/dist/src/storage/sqlite/migrations/233_observability_usage_links.sql +3 -0
- package/dist/src/storage/sqlite/migrations/234_task_result_reply.sql +14 -0
- package/dist/src/storage/sqlite/migrations/runner.d.ts +1 -1
- package/dist/src/storage/sqlite/migrations/runner.js +3 -3
- package/dist/src/storage/sqlite/schema.js +1 -1
- package/dist/src/tasks/capabilities/read.js +2 -2
- package/dist/src/tasks/task-result-delivery-service.js +20 -4
- package/dist/src/tui/backends/embedded-backend.js +3 -3
- package/dist/src/tui/tui-commands.js +1 -1
- package/dist/src/tui/tui-startup-resources.js +1 -1
- package/dist/src/tui/tui.js +1 -1
- package/dist/src/usage/recorder.d.ts +4 -2
- package/dist/src/usage/recorder.js +54 -26
- package/dist/src/usage/types.d.ts +3 -0
- package/dist/src/utils/logger/context.d.ts +1 -1
- package/dist/src/utils/logger/context.js +4 -1
- package/dist/src/utils/logger/types.d.ts +3 -0
- package/dist/src/voice/realtime/agentBroker.js +11 -2
- package/dist/src/voice/realtime/agentEngine.js +18 -10
- package/dist/src/voice/realtime/omniEngine.js +11 -2
- package/dist/src/voice/realtime/persona-context.js +1 -1
- package/dist/src/voice/realtime/playback-echo.d.ts +7 -0
- package/dist/src/voice/realtime/playback-echo.js +23 -1
- package/package.json +7 -4
- package/dist/gateway/static/root/assets/gateway-realtime-store-DS4WzQyF.js +0 -1
- package/dist/gateway/static/root/assets/index-Byk2QKIS.css +0 -1
- package/dist/gateway/static/root/assets/models-hub-cache-DN5MnhL8.js +0 -1
- package/dist/gateway/static/root/assets/settings-advanced-gate-DuvJrDU2.js +0 -1
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { init_context, runWithLogContext } from "../utils/logger/context.js";
|
|
2
|
+
import { createLogger } from "../utils/logger/index.js";
|
|
3
|
+
import { init_logger } from "../utils/logger.js";
|
|
4
|
+
import { CredentialResolver, init_credentials } from "../auth/credentials.js";
|
|
5
|
+
import { TracingConfigSchema, defaultTracingConfig, init_config } from "./config.js";
|
|
6
|
+
import { LangfuseTarget } from "./langfuse.js";
|
|
7
|
+
import { boundedJson, redactString } from "./sanitize.js";
|
|
8
|
+
import { LocalTraceStore } from "./store.js";
|
|
9
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
10
|
+
import { SpanStatusCode, context, createContextKey, trace } from "@opentelemetry/api";
|
|
11
|
+
import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
|
|
12
|
+
import { BasicTracerProvider } from "@opentelemetry/sdk-trace-base";
|
|
13
|
+
//#region src/observability/runtime.ts
|
|
14
|
+
init_credentials();
|
|
15
|
+
init_logger();
|
|
16
|
+
init_context();
|
|
17
|
+
init_config();
|
|
18
|
+
const log = createLogger("Observability");
|
|
19
|
+
let lastStorageWarning = 0;
|
|
20
|
+
function storageWarning(message) {
|
|
21
|
+
if (Date.now() - lastStorageWarning < 6e4) return;
|
|
22
|
+
lastStorageWarning = Date.now();
|
|
23
|
+
log.warn({
|
|
24
|
+
errorMessage: message,
|
|
25
|
+
phase: "local_trace_write"
|
|
26
|
+
}, `Local trace recording unavailable: ${message}`);
|
|
27
|
+
}
|
|
28
|
+
const localEpochKey = createContextKey("xopc.trace.localEpoch");
|
|
29
|
+
const scopes = new AsyncLocalStorage();
|
|
30
|
+
let config = defaultTracingConfig();
|
|
31
|
+
let store;
|
|
32
|
+
let target;
|
|
33
|
+
let provider;
|
|
34
|
+
let revision = 0;
|
|
35
|
+
let active = 0;
|
|
36
|
+
let dropped = 0;
|
|
37
|
+
const pendingLocal = /* @__PURE__ */ new Set();
|
|
38
|
+
let pendingLocalBytes = 0;
|
|
39
|
+
let remoteState = "disabled";
|
|
40
|
+
let knownSecrets = [];
|
|
41
|
+
let configureVersion = 0;
|
|
42
|
+
let targetSignature = "";
|
|
43
|
+
const targets = /* @__PURE__ */ new WeakMap();
|
|
44
|
+
const localEpochs = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
const captured = /* @__PURE__ */ new WeakMap();
|
|
46
|
+
function secretSafe(value) {
|
|
47
|
+
let result = redactString(value);
|
|
48
|
+
for (const secret of knownSecrets) if (secret.length >= 4) result = result.split(secret).join("[REDACTED]");
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
function record(span, ended = true) {
|
|
52
|
+
const attrs = {};
|
|
53
|
+
for (const [key, value] of Object.entries(span.attributes)) if ([
|
|
54
|
+
"string",
|
|
55
|
+
"number",
|
|
56
|
+
"boolean"
|
|
57
|
+
].includes(typeof value)) attrs[key] = typeof value === "string" ? secretSafe(value) : value;
|
|
58
|
+
return {
|
|
59
|
+
traceId: span.spanContext().traceId,
|
|
60
|
+
spanId: span.spanContext().spanId,
|
|
61
|
+
parentSpanId: span.parentSpanContext?.spanId,
|
|
62
|
+
name: secretSafe(span.name),
|
|
63
|
+
type: String(attrs["langfuse.observation.type"] ?? "span"),
|
|
64
|
+
startedAt: Math.floor(span.startTime[0] * 1e3 + span.startTime[1] / 1e6),
|
|
65
|
+
...ended ? { endedAt: Math.floor(span.endTime[0] * 1e3 + span.endTime[1] / 1e6) } : {},
|
|
66
|
+
status: ended ? String(attrs["xopc.status"] ?? (span.status.code === SpanStatusCode.ERROR ? "error" : "success")) : "running",
|
|
67
|
+
attributes: attrs
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
var TraceProcessor = class {
|
|
71
|
+
onStart(span, parentContext) {
|
|
72
|
+
const scope = scopes.getStore();
|
|
73
|
+
if (!(scope?.config ?? config).enabled) return;
|
|
74
|
+
captured.set(span, true);
|
|
75
|
+
if (scope?.target ?? target) targets.set(span, scope?.target ?? target);
|
|
76
|
+
if (!span.parentSpanContext || span.attributes["xopc.localRoot"] === true) {
|
|
77
|
+
const epoch = localStore().begin(record(span, false)).catch(() => {
|
|
78
|
+
storageWarning("Trace root could not be persisted");
|
|
79
|
+
return -1;
|
|
80
|
+
});
|
|
81
|
+
localEpochs.set(span, epoch);
|
|
82
|
+
if (scope) scope.epoch = epoch;
|
|
83
|
+
} else {
|
|
84
|
+
const epoch = scope?.epoch ?? parentContext.getValue(localEpochKey);
|
|
85
|
+
if (epoch) localEpochs.set(span, epoch);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
onEnd(span) {
|
|
89
|
+
if (!captured.has(span)) return;
|
|
90
|
+
try {
|
|
91
|
+
const value = record(span);
|
|
92
|
+
const bytes = Buffer.byteLength(JSON.stringify(value));
|
|
93
|
+
if (pendingLocal.size < 2e3 && pendingLocalBytes + bytes <= 8 * 1048576) {
|
|
94
|
+
const destination = localStore();
|
|
95
|
+
pendingLocalBytes += bytes;
|
|
96
|
+
const pending = (localEpochs.get(span) ?? Promise.resolve(void 0)).then(async (epoch) => {
|
|
97
|
+
await destination.write(value, epoch);
|
|
98
|
+
if (destination.lastError) storageWarning(destination.lastError);
|
|
99
|
+
}).catch(() => {
|
|
100
|
+
dropped++;
|
|
101
|
+
}).finally(() => {
|
|
102
|
+
pendingLocal.delete(pending);
|
|
103
|
+
pendingLocalBytes -= bytes;
|
|
104
|
+
});
|
|
105
|
+
pendingLocal.add(pending);
|
|
106
|
+
} else dropped++;
|
|
107
|
+
targets.get(span)?.enqueue(value);
|
|
108
|
+
} catch {
|
|
109
|
+
dropped++;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async forceFlush() {
|
|
113
|
+
await Promise.allSettled([...pendingLocal]);
|
|
114
|
+
await Promise.allSettled([store?.flush(), target?.flush()]);
|
|
115
|
+
}
|
|
116
|
+
async shutdown() {
|
|
117
|
+
await this.forceFlush();
|
|
118
|
+
target?.close();
|
|
119
|
+
await store?.close();
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
function tracer() {
|
|
123
|
+
if (!provider) {
|
|
124
|
+
context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());
|
|
125
|
+
process.once("beforeExit", () => {
|
|
126
|
+
flushTracing();
|
|
127
|
+
});
|
|
128
|
+
provider = new BasicTracerProvider({
|
|
129
|
+
spanProcessors: [new TraceProcessor()],
|
|
130
|
+
spanLimits: {
|
|
131
|
+
attributeCountLimit: 64,
|
|
132
|
+
attributeValueLengthLimit: 65536,
|
|
133
|
+
eventCountLimit: 0
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return provider.getTracer("xopc.observability");
|
|
138
|
+
}
|
|
139
|
+
function localStore() {
|
|
140
|
+
return store ??= new LocalTraceStore(config);
|
|
141
|
+
}
|
|
142
|
+
function tracingConfig() {
|
|
143
|
+
return structuredClone(config);
|
|
144
|
+
}
|
|
145
|
+
async function configureTracing(value) {
|
|
146
|
+
const version = ++configureVersion;
|
|
147
|
+
const next = TracingConfigSchema.parse(value ?? {});
|
|
148
|
+
config = next;
|
|
149
|
+
revision++;
|
|
150
|
+
if (store) await store.request("configure", config).catch(() => {});
|
|
151
|
+
remoteState = !next.enabled || !next.langfuse.enabled ? "disabled" : "missing_credentials";
|
|
152
|
+
if (remoteState === "disabled") {
|
|
153
|
+
target?.close();
|
|
154
|
+
target = void 0;
|
|
155
|
+
targetSignature = "";
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const secretKey = process.env.LANGFUSE_SECRET_KEY || await new CredentialResolver().revealGatewayStoredApiKey("langfuse");
|
|
160
|
+
const publicKey = process.env.LANGFUSE_PUBLIC_KEY || await new CredentialResolver().revealGatewayStoredApiKey("langfuse-public");
|
|
161
|
+
if (version !== configureVersion) return;
|
|
162
|
+
if (secretKey && publicKey) {
|
|
163
|
+
const baseUrl = process.env.LANGFUSE_BASE_URL || next.langfuse.baseUrl;
|
|
164
|
+
const signature = JSON.stringify([
|
|
165
|
+
baseUrl,
|
|
166
|
+
publicKey,
|
|
167
|
+
secretKey
|
|
168
|
+
]);
|
|
169
|
+
knownSecrets = [...new Set([
|
|
170
|
+
...knownSecrets,
|
|
171
|
+
secretKey,
|
|
172
|
+
publicKey
|
|
173
|
+
])].slice(-64);
|
|
174
|
+
if (!target || signature !== targetSignature) {
|
|
175
|
+
target?.close();
|
|
176
|
+
target = new LangfuseTarget(baseUrl, publicKey, secretKey);
|
|
177
|
+
targetSignature = signature;
|
|
178
|
+
}
|
|
179
|
+
remoteState = "ready";
|
|
180
|
+
} else {
|
|
181
|
+
target?.close();
|
|
182
|
+
target = void 0;
|
|
183
|
+
targetSignature = "";
|
|
184
|
+
}
|
|
185
|
+
} catch {
|
|
186
|
+
remoteState = "missing_credentials";
|
|
187
|
+
target?.close();
|
|
188
|
+
target = void 0;
|
|
189
|
+
targetSignature = "";
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async function tracingStatus() {
|
|
193
|
+
return {
|
|
194
|
+
local: {
|
|
195
|
+
...await localStore().request("status").catch(() => ({ unavailable: true })),
|
|
196
|
+
pending: pendingLocal.size,
|
|
197
|
+
pendingBytes: pendingLocalBytes,
|
|
198
|
+
queueDropped: localStore().dropped,
|
|
199
|
+
lastError: localStore().lastError
|
|
200
|
+
},
|
|
201
|
+
langfuse: target?.status() ?? {
|
|
202
|
+
state: remoteState,
|
|
203
|
+
pending: 0,
|
|
204
|
+
dropped: 0
|
|
205
|
+
},
|
|
206
|
+
active,
|
|
207
|
+
dropped
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function traceIds() {
|
|
211
|
+
const current = trace.getSpan(context.active())?.spanContext();
|
|
212
|
+
return current ? {
|
|
213
|
+
traceId: current.traceId,
|
|
214
|
+
spanId: current.spanId
|
|
215
|
+
} : {};
|
|
216
|
+
}
|
|
217
|
+
function attrs(identity, type) {
|
|
218
|
+
const result = {
|
|
219
|
+
...type === "agent" ? { "xopc.localRoot": true } : {},
|
|
220
|
+
"langfuse.observation.type": type,
|
|
221
|
+
"xopc.configRevision": revision
|
|
222
|
+
};
|
|
223
|
+
for (const [key, value] of Object.entries(identity)) if (value) {
|
|
224
|
+
result[`xopc.${key}`] = value;
|
|
225
|
+
result[`langfuse.trace.metadata.${key}`] = value;
|
|
226
|
+
}
|
|
227
|
+
if (scopes.getStore()?.name) result["langfuse.trace.name"] = scopes.getStore().name;
|
|
228
|
+
if (identity.conversationId) result["langfuse.session.id"] = identity.conversationId;
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
function startObservation(name, type, identity = {}) {
|
|
232
|
+
const scope = scopes.getStore();
|
|
233
|
+
if (!(scope?.config ?? config).enabled || scope && ++scope.count > 1e3) {
|
|
234
|
+
if (scope?.count > 1e3) scope.root?.setAttribute("xopc.partialReason", "span_quota");
|
|
235
|
+
dropped++;
|
|
236
|
+
return {
|
|
237
|
+
run: (fn) => fn(),
|
|
238
|
+
finish() {},
|
|
239
|
+
input() {},
|
|
240
|
+
update() {}
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
const standalone = !scope;
|
|
244
|
+
if (standalone && active >= 256) {
|
|
245
|
+
dropped++;
|
|
246
|
+
return {
|
|
247
|
+
run: (fn) => fn(),
|
|
248
|
+
finish() {},
|
|
249
|
+
input() {},
|
|
250
|
+
update() {}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
if (standalone) active++;
|
|
254
|
+
const span = tracer().startSpan(name.slice(0, 120), { attributes: attrs({
|
|
255
|
+
...scope?.identity,
|
|
256
|
+
...identity
|
|
257
|
+
}, type) });
|
|
258
|
+
const spanContext = trace.setSpan(context.active(), span).setValue(localEpochKey, localEpochs.get(span) ?? context.active().getValue(localEpochKey));
|
|
259
|
+
let ended = false;
|
|
260
|
+
const put = (key, value) => {
|
|
261
|
+
const policy = scope?.config ?? config;
|
|
262
|
+
if (policy.capture === "metadata") return;
|
|
263
|
+
const json = secretSafe(boundedJson(value, policy.capture === "detailed" && (policy.detailedUntil ?? 0) > Date.now() ? 65536 : 8192));
|
|
264
|
+
if (scope && (scope.bytes += Buffer.byteLength(json)) > policy.local.maxTraceKiB * 1024) {
|
|
265
|
+
span.setAttribute("xopc.partialReason", "capture_quota");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
span.setAttribute(key, json);
|
|
269
|
+
};
|
|
270
|
+
return {
|
|
271
|
+
span,
|
|
272
|
+
run: (fn) => context.with(spanContext, () => runWithLogContext({
|
|
273
|
+
...identity,
|
|
274
|
+
...traceIds()
|
|
275
|
+
}, fn)),
|
|
276
|
+
input: (value) => put("langfuse.observation.input", value),
|
|
277
|
+
update: (values) => {
|
|
278
|
+
for (const [key, value] of Object.entries(values)) if (value !== void 0) span.setAttribute(key, typeof value === "string" ? secretSafe(value.slice(0, 1024)) : value);
|
|
279
|
+
},
|
|
280
|
+
finish(status = "success", output) {
|
|
281
|
+
if (ended) return;
|
|
282
|
+
ended = true;
|
|
283
|
+
if (standalone) active--;
|
|
284
|
+
if (output !== void 0) put("langfuse.observation.output", output);
|
|
285
|
+
span.setAttribute("xopc.status", status);
|
|
286
|
+
span.setStatus({ code: status === "error" ? SpanStatusCode.ERROR : SpanStatusCode.OK });
|
|
287
|
+
span.end();
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
async function traceRun(name, identity, fn, input) {
|
|
292
|
+
if (scopes.getStore() || !config.enabled) return fn();
|
|
293
|
+
if (active >= 256) {
|
|
294
|
+
dropped++;
|
|
295
|
+
return scopes.run({
|
|
296
|
+
config: {
|
|
297
|
+
...config,
|
|
298
|
+
enabled: false
|
|
299
|
+
},
|
|
300
|
+
identity,
|
|
301
|
+
name,
|
|
302
|
+
count: 0,
|
|
303
|
+
bytes: 0
|
|
304
|
+
}, fn);
|
|
305
|
+
}
|
|
306
|
+
active++;
|
|
307
|
+
const scope = {
|
|
308
|
+
config: structuredClone(config),
|
|
309
|
+
target,
|
|
310
|
+
identity,
|
|
311
|
+
name,
|
|
312
|
+
count: 0,
|
|
313
|
+
bytes: 0
|
|
314
|
+
};
|
|
315
|
+
try {
|
|
316
|
+
return await scopes.run(scope, async () => {
|
|
317
|
+
const observation = startObservation(name, "agent", identity);
|
|
318
|
+
scope.root = observation.span;
|
|
319
|
+
observation.input(input);
|
|
320
|
+
try {
|
|
321
|
+
const result = await observation.run(fn);
|
|
322
|
+
observation.finish(result.stopReason ? "suspended" : result.ok === false ? result.errorMessage === "aborted" ? "cancelled" : "error" : "success", result.lastAssistantText ?? result.errorMessage);
|
|
323
|
+
return result;
|
|
324
|
+
} catch (error) {
|
|
325
|
+
observation.finish("error", error instanceof Error ? error.message : String(error));
|
|
326
|
+
throw error;
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
} finally {
|
|
330
|
+
active--;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const tracedExecutions = /* @__PURE__ */ new WeakSet();
|
|
334
|
+
function traceTools(tools) {
|
|
335
|
+
return tools.map((tool) => {
|
|
336
|
+
if (tracedExecutions.has(tool.execute)) return tool;
|
|
337
|
+
const execute = (...args) => {
|
|
338
|
+
const observation = startObservation(`tool.${tool.name}`, "tool");
|
|
339
|
+
observation.update({ "xopc.toolCallId": String(args[0]) });
|
|
340
|
+
observation.input(args[1]);
|
|
341
|
+
return observation.run(async () => {
|
|
342
|
+
try {
|
|
343
|
+
const result = await tool.execute(...args);
|
|
344
|
+
observation.finish(result?.isError || result?.details?.isError ? "error" : "success", result);
|
|
345
|
+
return result;
|
|
346
|
+
} catch (error) {
|
|
347
|
+
observation.finish(args[2]?.aborted ? "cancelled" : "error", error instanceof Error ? error.message : String(error));
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
};
|
|
352
|
+
tracedExecutions.add(execute);
|
|
353
|
+
return {
|
|
354
|
+
...tool,
|
|
355
|
+
execute
|
|
356
|
+
};
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
async function flushTracing() {
|
|
360
|
+
const timer = new Promise((resolve) => {
|
|
361
|
+
setTimeout(resolve, 3e3).unref();
|
|
362
|
+
});
|
|
363
|
+
await Promise.race([(async () => {
|
|
364
|
+
await Promise.allSettled([...pendingLocal]);
|
|
365
|
+
await Promise.allSettled([store?.flush(), target?.flush()]);
|
|
366
|
+
})(), timer]);
|
|
367
|
+
}
|
|
368
|
+
async function testLangfuse() {
|
|
369
|
+
if (!target) return false;
|
|
370
|
+
const result = await traceRun("diagnostic.langfuse", {}, async () => ({ ok: true }));
|
|
371
|
+
await target.flush();
|
|
372
|
+
return result.ok && !target.status().lastError;
|
|
373
|
+
}
|
|
374
|
+
async function closeTracing() {
|
|
375
|
+
await flushTracing();
|
|
376
|
+
target?.close();
|
|
377
|
+
target = void 0;
|
|
378
|
+
targetSignature = "";
|
|
379
|
+
await store?.close();
|
|
380
|
+
store = void 0;
|
|
381
|
+
}
|
|
382
|
+
async function traceOperation(name, fn, attributes = {}) {
|
|
383
|
+
const observation = startObservation(name, "span");
|
|
384
|
+
observation.update(attributes);
|
|
385
|
+
try {
|
|
386
|
+
const result = await observation.run(fn);
|
|
387
|
+
observation.finish();
|
|
388
|
+
return result;
|
|
389
|
+
} catch (error) {
|
|
390
|
+
observation.finish("error", error instanceof Error ? error.message : String(error));
|
|
391
|
+
throw error;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
function setTraceIdentity(identity) {
|
|
395
|
+
const scope = scopes.getStore();
|
|
396
|
+
if (!scope) return;
|
|
397
|
+
scope.identity = {
|
|
398
|
+
...scope.identity,
|
|
399
|
+
...identity
|
|
400
|
+
};
|
|
401
|
+
scope.root?.setAttributes(attrs(scope.identity, "agent"));
|
|
402
|
+
}
|
|
403
|
+
//#endregion
|
|
404
|
+
export { closeTracing, configureTracing, flushTracing, localStore, setTraceIdentity, startObservation, testLangfuse, traceIds, traceOperation, traceRun, traceTools, tracingConfig, tracingStatus };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//#region src/observability/sanitize.ts
|
|
2
|
+
const SECRET_KEY = /authorization|api.?key|password|secret|access.?token|refresh.?token|bot.?token|cookie/i;
|
|
3
|
+
const OMIT_KEY = /^(data|base64|headers|image_url|audio|video)$/i;
|
|
4
|
+
function redactString(value) {
|
|
5
|
+
return value.replace(/Bearer\s+[^\s"']+/gi, "Bearer [REDACTED]").replace(/\b(?:sk-|pk-lf-|sk-lf-)[A-Za-z0-9_-]+/g, "[REDACTED]").replace(/((?:api[_-]?key|token|secret|password)\s*[=:]\s*)[^\s,;"']+/gi, "$1[REDACTED]").replace(/data:[^\s"']+;base64,[^\s"']+/gi, "[MEDIA OMITTED]");
|
|
6
|
+
}
|
|
7
|
+
/** Bounds traversal before serializing; never materializes an entire tool/file payload. */
|
|
8
|
+
function boundedJson(value, maxBytes = 8192) {
|
|
9
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
10
|
+
let budget = Math.max(64, maxBytes - 256);
|
|
11
|
+
let truncated = false;
|
|
12
|
+
const walk = (v, depth) => {
|
|
13
|
+
if (budget < 32 || depth > 5) {
|
|
14
|
+
truncated = true;
|
|
15
|
+
return "[TRUNCATED]";
|
|
16
|
+
}
|
|
17
|
+
if (v === null || typeof v === "number" || typeof v === "boolean") {
|
|
18
|
+
budget -= 16;
|
|
19
|
+
return v;
|
|
20
|
+
}
|
|
21
|
+
if (typeof v === "string") {
|
|
22
|
+
const slice = v.slice(0, Math.min(budget, 4096));
|
|
23
|
+
const clean = redactString(slice);
|
|
24
|
+
const buf = Buffer.from(clean);
|
|
25
|
+
const size = Math.min(budget, 4096);
|
|
26
|
+
budget -= Math.min(buf.length, size);
|
|
27
|
+
if (slice.length < v.length || buf.length > size) truncated = true;
|
|
28
|
+
return buf.subarray(0, size).toString("utf8");
|
|
29
|
+
}
|
|
30
|
+
if (typeof v !== "object") return void 0;
|
|
31
|
+
if (seen.has(v)) return "[CIRCULAR]";
|
|
32
|
+
seen.add(v);
|
|
33
|
+
if (Array.isArray(v)) {
|
|
34
|
+
const values = [];
|
|
35
|
+
for (let i = 0; i < Math.min(v.length, 40) && budget > 32; i++) values.push(walk(v[i], depth + 1));
|
|
36
|
+
if (values.length < v.length) truncated = true;
|
|
37
|
+
return values;
|
|
38
|
+
}
|
|
39
|
+
const result = {};
|
|
40
|
+
let count = 0;
|
|
41
|
+
for (const key in v) {
|
|
42
|
+
if (!Object.hasOwn(v, key)) continue;
|
|
43
|
+
if (++count > 40 || budget < 32) {
|
|
44
|
+
truncated = true;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
const safeKey = key.slice(0, 80);
|
|
48
|
+
budget -= Buffer.byteLength(safeKey) + 8;
|
|
49
|
+
try {
|
|
50
|
+
result[safeKey] = SECRET_KEY.test(key) ? "[REDACTED]" : OMIT_KEY.test(key) ? "[OMITTED]" : walk(v[key], depth + 1);
|
|
51
|
+
} catch {
|
|
52
|
+
result[safeKey] = "[UNAVAILABLE]";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
};
|
|
57
|
+
let data;
|
|
58
|
+
try {
|
|
59
|
+
data = walk(value, 0);
|
|
60
|
+
} catch {
|
|
61
|
+
data = "[UNAVAILABLE]";
|
|
62
|
+
}
|
|
63
|
+
const text = JSON.stringify({
|
|
64
|
+
preview: data,
|
|
65
|
+
truncated
|
|
66
|
+
});
|
|
67
|
+
if (Buffer.byteLength(text) <= maxBytes) return text;
|
|
68
|
+
return JSON.stringify({
|
|
69
|
+
preview: Buffer.from(redactString(text)).subarray(0, Math.floor(maxBytes / 8)).toString("utf8"),
|
|
70
|
+
truncated: true
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
export { boundedJson, redactString };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { TracingConfig } from './config.js';
|
|
2
|
+
import type { TraceRecord } from './types.js';
|
|
3
|
+
export declare class LocalTraceStore {
|
|
4
|
+
private worker?;
|
|
5
|
+
private sequence;
|
|
6
|
+
private requests;
|
|
7
|
+
private pendingBytes;
|
|
8
|
+
private epoch;
|
|
9
|
+
private epochReady;
|
|
10
|
+
dropped: number;
|
|
11
|
+
lastError?: string;
|
|
12
|
+
constructor(config: TracingConfig, file?: string);
|
|
13
|
+
private fail;
|
|
14
|
+
request(op: string, data?: unknown): Promise<any>;
|
|
15
|
+
begin(record: TraceRecord): Promise<number>;
|
|
16
|
+
write(record: TraceRecord, epoch?: number): Promise<void>;
|
|
17
|
+
clear(): Promise<void>;
|
|
18
|
+
flush(): Promise<void>;
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { init_paths_state, resolveStateDir } from "../config/paths-state.js";
|
|
2
|
+
import { redactString } from "./sanitize.js";
|
|
3
|
+
import { traceStoreWorkerSource } from "./storeWorker.js";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import { Worker } from "node:worker_threads";
|
|
7
|
+
//#region src/observability/store.ts
|
|
8
|
+
init_paths_state();
|
|
9
|
+
var LocalTraceStore = class {
|
|
10
|
+
worker;
|
|
11
|
+
sequence = 0;
|
|
12
|
+
requests = /* @__PURE__ */ new Map();
|
|
13
|
+
pendingBytes = 0;
|
|
14
|
+
epoch = 0;
|
|
15
|
+
epochReady;
|
|
16
|
+
dropped = 0;
|
|
17
|
+
lastError;
|
|
18
|
+
constructor(config, file = join(resolveStateDir(), "traces", "traces.db")) {
|
|
19
|
+
try {
|
|
20
|
+
this.worker = new Worker(traceStoreWorkerSource, {
|
|
21
|
+
eval: true,
|
|
22
|
+
execArgv: process.execArgv.filter((arg, index, args) => !arg.startsWith("--input-type") && args[index - 1] !== "--input-type"),
|
|
23
|
+
workerData: {
|
|
24
|
+
path: file,
|
|
25
|
+
config,
|
|
26
|
+
owner: randomUUID()
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
this.worker.on("message", ({ id, value, error }) => {
|
|
30
|
+
const request = this.requests.get(id);
|
|
31
|
+
if (!request) return;
|
|
32
|
+
clearTimeout(request.timer);
|
|
33
|
+
this.requests.delete(id);
|
|
34
|
+
this.pendingBytes -= request.bytes;
|
|
35
|
+
if (error) {
|
|
36
|
+
this.lastError = error;
|
|
37
|
+
request.reject(new Error(error));
|
|
38
|
+
} else request.resolve(value);
|
|
39
|
+
if (!this.requests.size) this.worker?.unref();
|
|
40
|
+
});
|
|
41
|
+
this.worker.on("error", (error) => this.fail(`Trace worker unavailable: ${redactString(error instanceof Error ? error.message : "Worker startup failed").slice(0, 200)}`));
|
|
42
|
+
this.worker.on("exit", () => {
|
|
43
|
+
if (this.worker) this.fail("Trace worker stopped");
|
|
44
|
+
});
|
|
45
|
+
this.worker.unref();
|
|
46
|
+
this.epochReady = this.request("status").then((s) => {
|
|
47
|
+
this.epoch = s.epoch;
|
|
48
|
+
}).catch(() => {});
|
|
49
|
+
} catch {
|
|
50
|
+
this.lastError = "Trace worker unavailable";
|
|
51
|
+
this.epochReady = Promise.resolve();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
fail(message) {
|
|
55
|
+
this.lastError = message;
|
|
56
|
+
for (const r of this.requests.values()) {
|
|
57
|
+
clearTimeout(r.timer);
|
|
58
|
+
r.reject(new Error(message));
|
|
59
|
+
}
|
|
60
|
+
this.requests.clear();
|
|
61
|
+
this.pendingBytes = 0;
|
|
62
|
+
this.worker = void 0;
|
|
63
|
+
}
|
|
64
|
+
async request(op, data) {
|
|
65
|
+
if (!this.worker) throw new Error(this.lastError ?? "Trace storage unavailable");
|
|
66
|
+
const bytes = Buffer.byteLength(JSON.stringify(data ?? null));
|
|
67
|
+
if (this.requests.size >= 2e3 || this.pendingBytes + bytes > 8 * 1048576) {
|
|
68
|
+
this.dropped++;
|
|
69
|
+
throw new Error("Trace queue full");
|
|
70
|
+
}
|
|
71
|
+
const id = ++this.sequence;
|
|
72
|
+
this.pendingBytes += bytes;
|
|
73
|
+
this.worker.ref();
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const timer = setTimeout(() => {
|
|
76
|
+
this.requests.delete(id);
|
|
77
|
+
this.pendingBytes -= bytes;
|
|
78
|
+
this.dropped++;
|
|
79
|
+
reject(/* @__PURE__ */ new Error("Trace storage timeout"));
|
|
80
|
+
this.worker?.terminate();
|
|
81
|
+
}, 5e3);
|
|
82
|
+
this.requests.set(id, {
|
|
83
|
+
resolve,
|
|
84
|
+
reject,
|
|
85
|
+
timer,
|
|
86
|
+
bytes
|
|
87
|
+
});
|
|
88
|
+
this.worker.postMessage({
|
|
89
|
+
id,
|
|
90
|
+
op,
|
|
91
|
+
data
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async begin(record) {
|
|
96
|
+
await this.epochReady;
|
|
97
|
+
return this.request("put", {
|
|
98
|
+
records: [record],
|
|
99
|
+
epoch: null
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async write(record, epoch) {
|
|
103
|
+
await this.epochReady;
|
|
104
|
+
try {
|
|
105
|
+
await this.request("put", {
|
|
106
|
+
records: [record],
|
|
107
|
+
epoch: epoch ?? this.epoch
|
|
108
|
+
});
|
|
109
|
+
this.lastError = void 0;
|
|
110
|
+
} catch (error) {
|
|
111
|
+
this.dropped++;
|
|
112
|
+
this.lastError = error instanceof Error ? error.message : "Trace write failed";
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async clear() {
|
|
116
|
+
this.epoch = await this.request("clear");
|
|
117
|
+
}
|
|
118
|
+
async flush() {
|
|
119
|
+
await this.request("status");
|
|
120
|
+
}
|
|
121
|
+
async close() {
|
|
122
|
+
const worker = this.worker;
|
|
123
|
+
this.fail("Trace store closed");
|
|
124
|
+
await worker?.terminate();
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
//#endregion
|
|
128
|
+
export { LocalTraceStore };
|