@capybara-hq/kernel 0.0.0-stage → 0.1.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +20 -2
- package/bootstrap.json +22 -0
- package/dist/admin/admin.controller.d.ts +139 -0
- package/dist/admin/admin.controller.js +238 -0
- package/dist/admin/extensions.controller.d.ts +100 -0
- package/dist/admin/extensions.controller.js +226 -0
- package/dist/admin/setup.controller.d.ts +36 -0
- package/dist/admin/setup.controller.js +106 -0
- package/dist/admin/setup.service.d.ts +47 -0
- package/dist/admin/setup.service.js +90 -0
- package/dist/agents/agents.controller.d.ts +205 -0
- package/dist/agents/agents.controller.js +144 -0
- package/dist/agents/agents.service.d.ts +177 -0
- package/dist/agents/agents.service.js +677 -0
- package/dist/agents/definition.d.ts +198 -0
- package/dist/agents/definition.js +152 -0
- package/dist/agents/models.controller.d.ts +13 -0
- package/dist/agents/models.controller.js +31 -0
- package/dist/agents/templates.controller.d.ts +15 -0
- package/dist/agents/templates.controller.js +37 -0
- package/dist/agents/templates.d.ts +83 -0
- package/dist/agents/templates.js +173 -0
- package/dist/catalog/admin-catalog.controller.d.ts +143 -0
- package/dist/catalog/admin-catalog.controller.js +245 -0
- package/dist/catalog/catalog.service.d.ts +430 -0
- package/dist/catalog/catalog.service.js +882 -0
- package/dist/catalog/claude/agents.d.ts +27 -0
- package/dist/catalog/claude/agents.js +270 -0
- package/dist/catalog/claude/commands.d.ts +50 -0
- package/dist/catalog/claude/commands.js +179 -0
- package/dist/catalog/claude/manifest.d.ts +69 -0
- package/dist/catalog/claude/manifest.js +134 -0
- package/dist/catalog/claude/marketplace.d.ts +35 -0
- package/dist/catalog/claude/marketplace.js +194 -0
- package/dist/catalog/claude/mcp.d.ts +33 -0
- package/dist/catalog/claude/mcp.js +226 -0
- package/dist/catalog/claude/report.d.ts +86 -0
- package/dist/catalog/claude/report.js +121 -0
- package/dist/catalog/claude/translate.d.ts +56 -0
- package/dist/catalog/claude/translate.js +857 -0
- package/dist/catalog/cli.d.ts +46 -0
- package/dist/catalog/cli.js +211 -0
- package/dist/catalog/git-source.d.ts +127 -0
- package/dist/catalog/git-source.js +520 -0
- package/dist/catalog/marketplace-fetch.d.ts +99 -0
- package/dist/catalog/marketplace-fetch.js +203 -0
- package/dist/catalog/marketplaces.controller.d.ts +97 -0
- package/dist/catalog/marketplaces.controller.js +197 -0
- package/dist/catalog/marketplaces.service.d.ts +340 -0
- package/dist/catalog/marketplaces.service.js +1217 -0
- package/dist/catalog/org-catalog.controller.d.ts +128 -0
- package/dist/catalog/org-catalog.controller.js +277 -0
- package/dist/catalog/org-catalog.service.d.ts +246 -0
- package/dist/catalog/org-catalog.service.js +625 -0
- package/dist/catalog/plugin-import.d.ts +436 -0
- package/dist/catalog/plugin-import.js +1069 -0
- package/dist/catalog/plugin-install.d.ts +447 -0
- package/dist/catalog/plugin-install.js +1333 -0
- package/dist/catalog/plugin-package.d.ts +143 -0
- package/dist/catalog/plugin-package.js +382 -0
- package/dist/catalog/plugin-upgrade.d.ts +222 -0
- package/dist/catalog/plugin-upgrade.js +1219 -0
- package/dist/catalog/requests.service.d.ts +162 -0
- package/dist/catalog/requests.service.js +504 -0
- package/dist/catalog/schema-path.d.ts +7 -0
- package/dist/catalog/schema-path.js +63 -0
- package/dist/catalog/upgrade-targets.d.ts +59 -0
- package/dist/catalog/upgrade-targets.js +90 -0
- package/dist/catalog/versions.d.ts +31 -0
- package/dist/catalog/versions.js +44 -0
- package/dist/channels/actions.d.ts +37 -0
- package/dist/channels/actions.js +121 -0
- package/dist/channels/cards.d.ts +44 -0
- package/dist/channels/cards.js +156 -0
- package/dist/channels/connections.controller.d.ts +59 -0
- package/dist/channels/connections.controller.js +115 -0
- package/dist/channels/connections.service.d.ts +187 -0
- package/dist/channels/connections.service.js +534 -0
- package/dist/channels/features.d.ts +12 -0
- package/dist/channels/features.js +22 -0
- package/dist/channels/inbound.d.ts +120 -0
- package/dist/channels/inbound.js +332 -0
- package/dist/channels/outbound.d.ts +140 -0
- package/dist/channels/outbound.js +1125 -0
- package/dist/channels/phrases.d.ts +100 -0
- package/dist/channels/phrases.js +81 -0
- package/dist/channels/supervisor.d.ts +182 -0
- package/dist/channels/supervisor.js +625 -0
- package/dist/channels/threads.d.ts +29 -0
- package/dist/channels/threads.js +104 -0
- package/dist/channels/webhook-url.d.ts +16 -0
- package/dist/channels/webhook-url.js +34 -0
- package/dist/channels/webhook.controller.d.ts +19 -0
- package/dist/channels/webhook.controller.js +72 -0
- package/dist/channels/webhook.d.ts +73 -0
- package/dist/channels/webhook.js +274 -0
- package/dist/config/docs.d.ts +9 -0
- package/dist/config/docs.js +85 -0
- package/dist/config/env.d.ts +6 -0
- package/dist/config/env.js +73 -0
- package/dist/config/fields.d.ts +42 -0
- package/dist/config/fields.js +214 -0
- package/dist/config/file-source.d.ts +3 -0
- package/dist/config/file-source.js +31 -0
- package/dist/config/load.d.ts +20 -0
- package/dist/config/load.js +98 -0
- package/dist/config/nacos-source.d.ts +31 -0
- package/dist/config/nacos-source.js +229 -0
- package/dist/config/schema.d.ts +223 -0
- package/dist/config/schema.js +1151 -0
- package/dist/config/sources.d.ts +18 -0
- package/dist/config/sources.js +59 -0
- package/dist/config/types.d.ts +9 -0
- package/dist/config/types.js +7 -0
- package/dist/conversations/artifacts.d.ts +52 -0
- package/dist/conversations/artifacts.js +137 -0
- package/dist/conversations/attachments.controller.d.ts +28 -0
- package/dist/conversations/attachments.controller.js +96 -0
- package/dist/conversations/attachments.service.d.ts +180 -0
- package/dist/conversations/attachments.service.js +578 -0
- package/dist/conversations/conversations.controller.d.ts +288 -0
- package/dist/conversations/conversations.controller.js +607 -0
- package/dist/conversations/conversations.service.d.ts +321 -0
- package/dist/conversations/conversations.service.js +754 -0
- package/dist/conversations/event-log.d.ts +122 -0
- package/dist/conversations/event-log.js +259 -0
- package/dist/conversations/events.d.ts +856 -0
- package/dist/conversations/events.js +639 -0
- package/dist/conversations/files.d.ts +92 -0
- package/dist/conversations/files.js +213 -0
- package/dist/conversations/outputs-read.d.ts +34 -0
- package/dist/conversations/outputs-read.js +37 -0
- package/dist/conversations/queue.d.ts +169 -0
- package/dist/conversations/queue.js +527 -0
- package/dist/conversations/sandbox-status.d.ts +21 -0
- package/dist/conversations/sandbox-status.js +24 -0
- package/dist/conversations/selection.d.ts +46 -0
- package/dist/conversations/selection.js +75 -0
- package/dist/conversations/titles.d.ts +93 -0
- package/dist/conversations/titles.js +422 -0
- package/dist/ctx/bus.d.ts +16 -0
- package/dist/ctx/bus.js +101 -0
- package/dist/ctx/create-ctx.d.ts +109 -0
- package/dist/ctx/create-ctx.js +592 -0
- package/dist/ctx/kv.d.ts +4 -0
- package/dist/ctx/kv.js +31 -0
- package/dist/ctx/lock.d.ts +4 -0
- package/dist/ctx/lock.js +39 -0
- package/dist/ctx/namespace.d.ts +6 -0
- package/dist/ctx/namespace.js +12 -0
- package/dist/ctx/queue.d.ts +25 -0
- package/dist/ctx/queue.js +150 -0
- package/dist/ctx/secrets.d.ts +45 -0
- package/dist/ctx/secrets.js +175 -0
- package/dist/db/kernel-schema.d.ts +1122 -0
- package/dist/db/kernel-schema.js +5 -0
- package/dist/db/migrator.d.ts +28 -0
- package/dist/db/migrator.js +140 -0
- package/dist/db/pool.d.ts +21 -0
- package/dist/db/pool.js +28 -0
- package/dist/db/sql-statements.d.ts +34 -0
- package/dist/db/sql-statements.js +168 -0
- package/dist/errors.d.ts +210 -0
- package/dist/errors.js +212 -0
- package/dist/evals/budget.d.ts +61 -0
- package/dist/evals/budget.js +181 -0
- package/dist/evals/diff.d.ts +55 -0
- package/dist/evals/diff.js +133 -0
- package/dist/evals/judge.d.ts +53 -0
- package/dist/evals/judge.js +149 -0
- package/dist/evals/plan.d.ts +47 -0
- package/dist/evals/plan.js +77 -0
- package/dist/evals/reconstruct.d.ts +137 -0
- package/dist/evals/reconstruct.js +322 -0
- package/dist/evals/recorded-tools.d.ts +54 -0
- package/dist/evals/recorded-tools.js +167 -0
- package/dist/evals/run-replay.d.ts +86 -0
- package/dist/evals/run-replay.js +502 -0
- package/dist/evals/runs.controller.d.ts +100 -0
- package/dist/evals/runs.controller.js +199 -0
- package/dist/evals/runs.service.d.ts +275 -0
- package/dist/evals/runs.service.js +836 -0
- package/dist/evals/sets.controller.d.ts +61 -0
- package/dist/evals/sets.controller.js +183 -0
- package/dist/evals/sets.service.d.ts +128 -0
- package/dist/evals/sets.service.js +558 -0
- package/dist/evals/snapshot.d.ts +76 -0
- package/dist/evals/snapshot.js +207 -0
- package/dist/evals/step-replay.d.ts +113 -0
- package/dist/evals/step-replay.js +181 -0
- package/dist/evals/trajectory.controller.d.ts +41 -0
- package/dist/evals/trajectory.controller.js +88 -0
- package/dist/evals/trajectory.d.ts +144 -0
- package/dist/evals/trajectory.js +338 -0
- package/dist/evals/trajectory.service.d.ts +108 -0
- package/dist/evals/trajectory.service.js +323 -0
- package/dist/evals/worker.d.ts +101 -0
- package/dist/evals/worker.js +789 -0
- package/dist/extensions/admin-ops.d.ts +105 -0
- package/dist/extensions/admin-ops.js +353 -0
- package/dist/extensions/admin-view.d.ts +79 -0
- package/dist/extensions/admin-view.js +327 -0
- package/dist/extensions/approvals.d.ts +119 -0
- package/dist/extensions/approvals.js +205 -0
- package/dist/extensions/blobs.d.ts +45 -0
- package/dist/extensions/blobs.js +160 -0
- package/dist/extensions/bootstrap.d.ts +149 -0
- package/dist/extensions/bootstrap.js +441 -0
- package/dist/extensions/cli.d.ts +61 -0
- package/dist/extensions/cli.js +165 -0
- package/dist/extensions/config-json-schema.d.ts +9 -0
- package/dist/extensions/config-json-schema.js +73 -0
- package/dist/extensions/deployment-config.d.ts +138 -0
- package/dist/extensions/deployment-config.js +394 -0
- package/dist/extensions/enabled.d.ts +31 -0
- package/dist/extensions/enabled.js +55 -0
- package/dist/extensions/hooks.d.ts +161 -0
- package/dist/extensions/hooks.js +425 -0
- package/dist/extensions/host.d.ts +577 -0
- package/dist/extensions/host.js +2071 -0
- package/dist/extensions/inflight.d.ts +69 -0
- package/dist/extensions/inflight.js +277 -0
- package/dist/extensions/installs.d.ts +166 -0
- package/dist/extensions/installs.js +253 -0
- package/dist/extensions/instance.d.ts +167 -0
- package/dist/extensions/instance.js +329 -0
- package/dist/extensions/ledger.d.ts +30 -0
- package/dist/extensions/ledger.js +50 -0
- package/dist/extensions/lifecycle.d.ts +80 -0
- package/dist/extensions/lifecycle.js +152 -0
- package/dist/extensions/migrate-lock.d.ts +51 -0
- package/dist/extensions/migrate-lock.js +100 -0
- package/dist/extensions/org-config.controller.d.ts +24 -0
- package/dist/extensions/org-config.controller.js +59 -0
- package/dist/extensions/org-config.d.ts +169 -0
- package/dist/extensions/org-config.js +520 -0
- package/dist/extensions/package.d.ts +115 -0
- package/dist/extensions/package.js +246 -0
- package/dist/extensions/permission-set.d.ts +117 -0
- package/dist/extensions/permission-set.js +184 -0
- package/dist/extensions/permission-view.d.ts +194 -0
- package/dist/extensions/permission-view.js +284 -0
- package/dist/extensions/registry.d.ts +24 -0
- package/dist/extensions/registry.js +56 -0
- package/dist/extensions/resolve.d.ts +12 -0
- package/dist/extensions/resolve.js +71 -0
- package/dist/extensions/sources.d.ts +69 -0
- package/dist/extensions/sources.js +120 -0
- package/dist/extensions/sync.d.ts +40 -0
- package/dist/extensions/sync.js +242 -0
- package/dist/files/previewers.controller.d.ts +46 -0
- package/dist/files/previewers.controller.js +105 -0
- package/dist/files/previewers.d.ts +100 -0
- package/dist/files/previewers.js +250 -0
- package/dist/http/api.module.d.ts +114 -0
- package/dist/http/api.module.js +448 -0
- package/dist/http/app.d.ts +77 -0
- package/dist/http/app.js +170 -0
- package/dist/http/error.filter.d.ts +21 -0
- package/dist/http/error.filter.js +85 -0
- package/dist/http/gateway.module.d.ts +15 -0
- package/dist/http/gateway.module.js +13 -0
- package/dist/http/health.controller.d.ts +26 -0
- package/dist/http/health.controller.js +79 -0
- package/dist/http/nest-logger.d.ts +20 -0
- package/dist/http/nest-logger.js +28 -0
- package/dist/http/plugin-dispatch.d.ts +68 -0
- package/dist/http/plugin-dispatch.js +200 -0
- package/dist/http/tokens.d.ts +131 -0
- package/dist/http/tokens.js +131 -0
- package/dist/http/zod.pipe.d.ts +8 -0
- package/dist/http/zod.pipe.js +14 -0
- package/dist/identity/accounts.d.ts +93 -0
- package/dist/identity/accounts.js +367 -0
- package/dist/identity/api-tokens.d.ts +73 -0
- package/dist/identity/api-tokens.js +197 -0
- package/dist/identity/audit.d.ts +23 -0
- package/dist/identity/audit.js +41 -0
- package/dist/identity/auth.controller.d.ts +40 -0
- package/dist/identity/auth.controller.js +130 -0
- package/dist/identity/auth.guard.d.ts +41 -0
- package/dist/identity/auth.guard.js +162 -0
- package/dist/identity/binding.d.ts +47 -0
- package/dist/identity/binding.js +84 -0
- package/dist/identity/cookies.d.ts +16 -0
- package/dist/identity/cookies.js +32 -0
- package/dist/identity/credential-check.d.ts +17 -0
- package/dist/identity/credential-check.js +24 -0
- package/dist/identity/enterprise-admin.d.ts +14 -0
- package/dist/identity/enterprise-admin.js +27 -0
- package/dist/identity/external.d.ts +106 -0
- package/dist/identity/external.js +337 -0
- package/dist/identity/hash.d.ts +2 -0
- package/dist/identity/hash.js +3 -0
- package/dist/identity/login.d.ts +6 -0
- package/dist/identity/login.js +20 -0
- package/dist/identity/me.controller.d.ts +61 -0
- package/dist/identity/me.controller.js +159 -0
- package/dist/identity/passwords.d.ts +6 -0
- package/dist/identity/passwords.js +37 -0
- package/dist/identity/principal.d.ts +56 -0
- package/dist/identity/principal.js +38 -0
- package/dist/identity/rate-limit.d.ts +55 -0
- package/dist/identity/rate-limit.js +121 -0
- package/dist/identity/sessions.d.ts +84 -0
- package/dist/identity/sessions.js +306 -0
- package/dist/identity/settings.d.ts +121 -0
- package/dist/identity/settings.js +248 -0
- package/dist/identity/sso.controller.d.ts +59 -0
- package/dist/identity/sso.controller.js +192 -0
- package/dist/identity/sso.d.ts +83 -0
- package/dist/identity/sso.js +202 -0
- package/dist/inbox/inbox.service.d.ts +43 -0
- package/dist/inbox/inbox.service.js +57 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.js +65 -0
- package/dist/kernel.d.ts +256 -0
- package/dist/kernel.js +1326 -0
- package/dist/log.d.ts +48 -0
- package/dist/log.js +229 -0
- package/dist/models/prices.d.ts +53 -0
- package/dist/models/prices.js +121 -0
- package/dist/models/purposes.controller.d.ts +19 -0
- package/dist/models/purposes.controller.js +56 -0
- package/dist/models/purposes.d.ts +140 -0
- package/dist/models/purposes.js +267 -0
- package/dist/net/proxy.d.ts +44 -0
- package/dist/net/proxy.js +162 -0
- package/dist/net/public-address.d.ts +60 -0
- package/dist/net/public-address.js +271 -0
- package/dist/net/safe-fetch.d.ts +38 -0
- package/dist/net/safe-fetch.js +140 -0
- package/dist/orgs/directory-sync.controller.d.ts +14 -0
- package/dist/orgs/directory-sync.controller.js +40 -0
- package/dist/orgs/directory-sync.d.ts +166 -0
- package/dist/orgs/directory-sync.js +558 -0
- package/dist/orgs/invitations.controller.d.ts +58 -0
- package/dist/orgs/invitations.controller.js +137 -0
- package/dist/orgs/invitations.service.d.ts +80 -0
- package/dist/orgs/invitations.service.js +217 -0
- package/dist/orgs/members.service.d.ts +63 -0
- package/dist/orgs/members.service.js +234 -0
- package/dist/orgs/offboarding.d.ts +141 -0
- package/dist/orgs/offboarding.js +398 -0
- package/dist/orgs/org-lock.d.ts +32 -0
- package/dist/orgs/org-lock.js +46 -0
- package/dist/orgs/org.guard.d.ts +35 -0
- package/dist/orgs/org.guard.js +95 -0
- package/dist/orgs/orgs.controller.d.ts +78 -0
- package/dist/orgs/orgs.controller.js +192 -0
- package/dist/orgs/orgs.service.d.ts +61 -0
- package/dist/orgs/orgs.service.js +209 -0
- package/dist/orgs/roles.d.ts +13 -0
- package/dist/orgs/roles.js +58 -0
- package/dist/orgs/settings.controller.d.ts +20 -0
- package/dist/orgs/settings.controller.js +53 -0
- package/dist/orgs/settings.service.d.ts +120 -0
- package/dist/orgs/settings.service.js +178 -0
- package/dist/packages/npm.d.ts +50 -0
- package/dist/packages/npm.js +218 -0
- package/dist/packages/provenance.d.ts +47 -0
- package/dist/packages/provenance.js +147 -0
- package/dist/packages/tgz.d.ts +76 -0
- package/dist/packages/tgz.js +225 -0
- package/dist/runtime/abort.d.ts +32 -0
- package/dist/runtime/abort.js +71 -0
- package/dist/runtime/checkpointer.d.ts +32 -0
- package/dist/runtime/checkpointer.js +243 -0
- package/dist/runtime/context/build.d.ts +254 -0
- package/dist/runtime/context/build.js +693 -0
- package/dist/runtime/context/claude-tools.d.ts +11 -0
- package/dist/runtime/context/claude-tools.js +24 -0
- package/dist/runtime/context/compaction.d.ts +26 -0
- package/dist/runtime/context/compaction.js +195 -0
- package/dist/runtime/context/pairing.d.ts +11 -0
- package/dist/runtime/context/pairing.js +82 -0
- package/dist/runtime/context/reminders.d.ts +5 -0
- package/dist/runtime/context/reminders.js +11 -0
- package/dist/runtime/context/skills.d.ts +105 -0
- package/dist/runtime/context/skills.js +283 -0
- package/dist/runtime/context/tokens.d.ts +19 -0
- package/dist/runtime/context/tokens.js +104 -0
- package/dist/runtime/conversation-lock.d.ts +40 -0
- package/dist/runtime/conversation-lock.js +97 -0
- package/dist/runtime/hook-calls.d.ts +84 -0
- package/dist/runtime/hook-calls.js +136 -0
- package/dist/runtime/langgraph-loop.d.ts +52 -0
- package/dist/runtime/langgraph-loop.js +247 -0
- package/dist/runtime/loop.d.ts +93 -0
- package/dist/runtime/loop.js +7 -0
- package/dist/runtime/maintenance.d.ts +80 -0
- package/dist/runtime/maintenance.js +147 -0
- package/dist/runtime/media-cache.d.ts +21 -0
- package/dist/runtime/media-cache.js +51 -0
- package/dist/runtime/model-call.d.ts +176 -0
- package/dist/runtime/model-call.js +1049 -0
- package/dist/runtime/providers.d.ts +86 -0
- package/dist/runtime/providers.js +224 -0
- package/dist/runtime/quota.d.ts +29 -0
- package/dist/runtime/quota.js +70 -0
- package/dist/runtime/run-finish.d.ts +45 -0
- package/dist/runtime/run-finish.js +69 -0
- package/dist/runtime/run-queue.d.ts +121 -0
- package/dist/runtime/run-queue.js +493 -0
- package/dist/runtime/run-worker.d.ts +138 -0
- package/dist/runtime/run-worker.js +805 -0
- package/dist/runtime/stream.controller.d.ts +34 -0
- package/dist/runtime/stream.controller.js +122 -0
- package/dist/runtime/stream.d.ts +63 -0
- package/dist/runtime/stream.js +262 -0
- package/dist/runtime/subagents/child.d.ts +80 -0
- package/dist/runtime/subagents/child.js +225 -0
- package/dist/runtime/subagents/definition.d.ts +103 -0
- package/dist/runtime/subagents/definition.js +392 -0
- package/dist/runtime/subagents/dispatch.d.ts +69 -0
- package/dist/runtime/subagents/dispatch.js +502 -0
- package/dist/runtime/subagents/limits.d.ts +23 -0
- package/dist/runtime/subagents/limits.js +34 -0
- package/dist/runtime/subagents/org-agents.d.ts +50 -0
- package/dist/runtime/subagents/org-agents.js +70 -0
- package/dist/runtime/subagents/preamble.d.ts +7 -0
- package/dist/runtime/subagents/preamble.js +11 -0
- package/dist/runtime/subagents/progress.d.ts +26 -0
- package/dist/runtime/subagents/progress.js +36 -0
- package/dist/runtime/subagents/roster.d.ts +55 -0
- package/dist/runtime/subagents/roster.js +111 -0
- package/dist/runtime/subagents/skill-fork.d.ts +123 -0
- package/dist/runtime/subagents/skill-fork.js +259 -0
- package/dist/runtime/usage-report.d.ts +163 -0
- package/dist/runtime/usage-report.js +550 -0
- package/dist/runtime/waits.d.ts +233 -0
- package/dist/runtime/waits.js +883 -0
- package/dist/sandbox/leases.d.ts +169 -0
- package/dist/sandbox/leases.js +515 -0
- package/dist/sandbox/lifecycle.d.ts +21 -0
- package/dist/sandbox/lifecycle.js +68 -0
- package/dist/sandbox/sandbox.service.d.ts +249 -0
- package/dist/sandbox/sandbox.service.js +908 -0
- package/dist/skills/catalog.d.ts +149 -0
- package/dist/skills/catalog.js +405 -0
- package/dist/skills/changes.controller.d.ts +56 -0
- package/dist/skills/changes.controller.js +136 -0
- package/dist/skills/changes.service.d.ts +175 -0
- package/dist/skills/changes.service.js +805 -0
- package/dist/skills/invocable.controller.d.ts +23 -0
- package/dist/skills/invocable.controller.js +50 -0
- package/dist/skills/invocation-keys.d.ts +23 -0
- package/dist/skills/invocation-keys.js +44 -0
- package/dist/skills/invoke.d.ts +135 -0
- package/dist/skills/invoke.js +286 -0
- package/dist/skills/package.d.ts +87 -0
- package/dist/skills/package.js +227 -0
- package/dist/skills/skills.controller.d.ts +40 -0
- package/dist/skills/skills.controller.js +110 -0
- package/dist/skills/skills.service.d.ts +192 -0
- package/dist/skills/skills.service.js +567 -0
- package/dist/storage/prefix.d.ts +36 -0
- package/dist/storage/prefix.js +104 -0
- package/dist/storage/purge.d.ts +27 -0
- package/dist/storage/purge.js +75 -0
- package/dist/storage/storage.service.d.ts +44 -0
- package/dist/storage/storage.service.js +145 -0
- package/dist/text.d.ts +13 -0
- package/dist/text.js +13 -0
- package/dist/tools/approvals.controller.d.ts +53 -0
- package/dist/tools/approvals.controller.js +128 -0
- package/dist/tools/approvals.service.d.ts +172 -0
- package/dist/tools/approvals.service.js +568 -0
- package/dist/tools/breaker.d.ts +28 -0
- package/dist/tools/breaker.js +72 -0
- package/dist/tools/builtins/agent.d.ts +81 -0
- package/dist/tools/builtins/agent.js +223 -0
- package/dist/tools/builtins/ask-user.d.ts +28 -0
- package/dist/tools/builtins/ask-user.js +139 -0
- package/dist/tools/builtins/read-tool-output.d.ts +18 -0
- package/dist/tools/builtins/read-tool-output.js +108 -0
- package/dist/tools/builtins/sandbox/bash.d.ts +16 -0
- package/dist/tools/builtins/sandbox/bash.js +137 -0
- package/dist/tools/builtins/sandbox/common.d.ts +114 -0
- package/dist/tools/builtins/sandbox/common.js +304 -0
- package/dist/tools/builtins/sandbox/deliver.d.ts +5 -0
- package/dist/tools/builtins/sandbox/deliver.js +180 -0
- package/dist/tools/builtins/sandbox/edit.d.ts +4 -0
- package/dist/tools/builtins/sandbox/edit.js +98 -0
- package/dist/tools/builtins/sandbox/glob-match.d.ts +25 -0
- package/dist/tools/builtins/sandbox/glob-match.js +240 -0
- package/dist/tools/builtins/sandbox/glob.d.ts +4 -0
- package/dist/tools/builtins/sandbox/glob.js +104 -0
- package/dist/tools/builtins/sandbox/grep.d.ts +9 -0
- package/dist/tools/builtins/sandbox/grep.js +133 -0
- package/dist/tools/builtins/sandbox/index.d.ts +12 -0
- package/dist/tools/builtins/sandbox/index.js +27 -0
- package/dist/tools/builtins/sandbox/names.d.ts +7 -0
- package/dist/tools/builtins/sandbox/names.js +20 -0
- package/dist/tools/builtins/sandbox/read.d.ts +8 -0
- package/dist/tools/builtins/sandbox/read.js +121 -0
- package/dist/tools/builtins/sandbox/write.d.ts +4 -0
- package/dist/tools/builtins/sandbox/write.js +88 -0
- package/dist/tools/builtins/skill-load.d.ts +53 -0
- package/dist/tools/builtins/skill-load.js +446 -0
- package/dist/tools/builtins/skill-write.d.ts +59 -0
- package/dist/tools/builtins/skill-write.js +244 -0
- package/dist/tools/builtins/tool-search.d.ts +31 -0
- package/dist/tools/builtins/tool-search.js +171 -0
- package/dist/tools/connector-content.controller.d.ts +34 -0
- package/dist/tools/connector-content.controller.js +78 -0
- package/dist/tools/connector-content.d.ts +104 -0
- package/dist/tools/connector-content.js +267 -0
- package/dist/tools/connectors.controller.d.ts +210 -0
- package/dist/tools/connectors.controller.js +234 -0
- package/dist/tools/connectors.service.d.ts +303 -0
- package/dist/tools/connectors.service.js +1102 -0
- package/dist/tools/credentials.d.ts +139 -0
- package/dist/tools/credentials.js +351 -0
- package/dist/tools/drift.d.ts +141 -0
- package/dist/tools/drift.js +539 -0
- package/dist/tools/elicitation.d.ts +81 -0
- package/dist/tools/elicitation.js +266 -0
- package/dist/tools/endpoint.d.ts +18 -0
- package/dist/tools/endpoint.js +115 -0
- package/dist/tools/info.d.ts +67 -0
- package/dist/tools/info.js +113 -0
- package/dist/tools/naming.d.ts +27 -0
- package/dist/tools/naming.js +47 -0
- package/dist/tools/oauth/discovery.d.ts +65 -0
- package/dist/tools/oauth/discovery.js +307 -0
- package/dist/tools/oauth/flow.d.ts +63 -0
- package/dist/tools/oauth/flow.js +237 -0
- package/dist/tools/oauth/oauth.controller.d.ts +31 -0
- package/dist/tools/oauth/oauth.controller.js +95 -0
- package/dist/tools/oauth/tokens.d.ts +111 -0
- package/dist/tools/oauth/tokens.js +258 -0
- package/dist/tools/outputs.d.ts +61 -0
- package/dist/tools/outputs.js +156 -0
- package/dist/tools/pipeline.d.ts +322 -0
- package/dist/tools/pipeline.js +1236 -0
- package/dist/tools/policy.d.ts +60 -0
- package/dist/tools/policy.js +236 -0
- package/dist/tools/questions.controller.d.ts +24 -0
- package/dist/tools/questions.controller.js +58 -0
- package/dist/tools/questions.service.d.ts +103 -0
- package/dist/tools/questions.service.js +295 -0
- package/dist/tools/redact.d.ts +20 -0
- package/dist/tools/redact.js +60 -0
- package/dist/tools/registry.d.ts +246 -0
- package/dist/tools/registry.js +481 -0
- package/dist/tools/resources.d.ts +16 -0
- package/dist/tools/resources.js +76 -0
- package/dist/tools/specs.d.ts +14 -0
- package/dist/tools/specs.js +31 -0
- package/dist/tools/validate.d.ts +20 -0
- package/dist/tools/validate.js +183 -0
- package/dist/tools/view.d.ts +43 -0
- package/dist/tools/view.js +225 -0
- package/dist/ui/embed-token.d.ts +31 -0
- package/dist/ui/embed-token.js +132 -0
- package/dist/ui/registry.d.ts +58 -0
- package/dist/ui/registry.js +90 -0
- package/dist/ui/ui-stream.d.ts +37 -0
- package/dist/ui/ui-stream.js +162 -0
- package/dist/ui/views.controller.d.ts +81 -0
- package/dist/ui/views.controller.js +261 -0
- package/dist/ui/views.d.ts +155 -0
- package/dist/ui/views.js +306 -0
- package/dist/version.d.ts +5 -0
- package/dist/version.js +6 -0
- package/package.json +67 -3
- package/src/migrations/0001_init.sql +8 -0
- package/src/migrations/0002_secrets.sql +8 -0
- package/src/migrations/0003_identity.sql +117 -0
- package/src/migrations/0004_plugin_org_config_nullable.sql +3 -0
- package/src/migrations/0005_agents.sql +27 -0
- package/src/migrations/0006_conversations.sql +37 -0
- package/src/migrations/0007_checkpoints.sql +26 -0
- package/src/migrations/0008_runs_trigger_seq.sql +3 -0
- package/src/migrations/0009_runs_active_unique.sql +4 -0
- package/src/migrations/0010_tools.sql +125 -0
- package/src/migrations/0011_approvals_spec.sql +10 -0
- package/src/migrations/0012_run_waits.sql +24 -0
- package/src/migrations/0013_app_tools_source.sql +4 -0
- package/src/migrations/0014_tool_specs.sql +8 -0
- package/src/migrations/0015_tool_drift.sql +20 -0
- package/src/migrations/0016_plugin_permissions.sql +11 -0
- package/src/migrations/0017_identity_sources.sql +6 -0
- package/src/migrations/0018_channels.sql +74 -0
- package/src/migrations/0019_identity_trust.sql +17 -0
- package/src/migrations/0020_channel_card_lookup.sql +10 -0
- package/src/migrations/0021_org_storage_purge.sql +9 -0
- package/src/migrations/0022_attachments.sql +29 -0
- package/src/migrations/0023_sandboxes.sql +37 -0
- package/src/migrations/0024_skills.sql +46 -0
- package/src/migrations/0025_skills_catalog_idx.sql +6 -0
- package/src/migrations/0026_skill_changes.sql +34 -0
- package/src/migrations/0027_model_prices.sql +18 -0
- package/src/migrations/0028_evals.sql +105 -0
- package/src/migrations/0029_model_purposes.sql +18 -0
- package/src/migrations/0030_plugin_installs.sql +73 -0
- package/src/migrations/0031_plugin_snapshot_source.sql +6 -0
- package/src/migrations/0032_plugin_deployment_configs.sql +10 -0
- package/src/migrations/0033_rename_apps_to_connectors.sql +25 -0
- package/src/migrations/0034_directory.sql +49 -0
- package/src/migrations/0035_catalogs.sql +40 -0
- package/src/migrations/0036_app_import_staging.sql +17 -0
- package/src/migrations/0037_directory_installs.sql +92 -0
- package/src/migrations/0038_channel_inbound_mode.sql +6 -0
- package/src/migrations/0039_channel_card_pending.sql +8 -0
- package/src/migrations/0040_usage_report_sweep.sql +10 -0
- package/src/migrations/0041_rename_plugins_to_extensions.sql +38 -0
- package/src/migrations/0042_rename_directory_to_catalog.sql +80 -0
- package/src/migrations/0043_rename_app_installs_to_plugin_installs.sql +20 -0
- package/src/migrations/0044_catalog_one_kind.sql +90 -0
- package/src/migrations/0045_plugin_dependencies.sql +9 -0
- package/src/migrations/0046_git_sources.sql +25 -0
- package/src/migrations/0047_marketplace_entry_heads.sql +7 -0
- package/src/migrations/0048_marketplace_entry_trees.sql +8 -0
- package/src/migrations/0049_plugin_upgrades.sql +37 -0
- package/src/migrations/0050_bootstrap.sql +53 -0
- package/src/migrations/0051_claude_compat.sql +26 -0
- package/src/migrations/0052_conversations_account_agent_idx.sql +5 -0
- package/src/migrations/0053_subagents.sql +73 -0
- package/src/migrations/0054_subagent_wait_expiry.sql +7 -0
- package/src/migrations/0055_subagent_parent_indexes.sql +10 -0
- package/src/migrations/0056_approval_reason.sql +3 -0
- package/src/migrations/0057_conversation_titles.sql +11 -0
- package/src/migrations/0058_conversation_selection.sql +4 -0
- package/src/migrations/0059_conversation_queue.sql +22 -0
- package/src/migrations/0060_file_previewers.sql +10 -0
- package/src/migrations/0061_queue_selection.sql +5 -0
- package/src/migrations/0062_queued_skill.sql +8 -0
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { sql } from "kysely";
|
|
3
|
+
import { ApiError } from "../errors.js";
|
|
4
|
+
import { audit } from "../identity/audit.js";
|
|
5
|
+
import { ORG_ROLES, roleAtLeast } from "../orgs/roles.js";
|
|
6
|
+
export { APPROVAL_DENY_REASON_MAX_CHARS } from "../conversations/events.js";
|
|
7
|
+
/** `list` 一次最多返回的审批数(收件箱据此判断这个来源是否取满)。 */
|
|
8
|
+
export const APPROVALS_LIST_MAX = 100;
|
|
9
|
+
const notFound = () => new ApiError("approval.not_found", "Approval not found");
|
|
10
|
+
const iso = (v) => new Date(v).toISOString();
|
|
11
|
+
const isRole = (v) => v !== null && ORG_ROLES.includes(v);
|
|
12
|
+
function toRecord(r) {
|
|
13
|
+
return {
|
|
14
|
+
id: r.id,
|
|
15
|
+
orgId: r.org_id,
|
|
16
|
+
conversationId: r.conversation_id,
|
|
17
|
+
runId: r.run_id,
|
|
18
|
+
callId: r.call_id,
|
|
19
|
+
attempt: r.attempt,
|
|
20
|
+
toolKey: r.tool_key,
|
|
21
|
+
specHash: r.spec_hash,
|
|
22
|
+
connectorId: r.connector_id,
|
|
23
|
+
input: r.input,
|
|
24
|
+
requestedFor: r.requested_for,
|
|
25
|
+
approverRole: isRole(r.approver_role) ? r.approver_role : null,
|
|
26
|
+
status: r.status,
|
|
27
|
+
scope: r.scope,
|
|
28
|
+
decidedBy: r.decided_by,
|
|
29
|
+
decidedAt: r.decided_at === null ? null : iso(r.decided_at),
|
|
30
|
+
denyReason: r.deny_reason,
|
|
31
|
+
expiresAt: iso(r.expires_at),
|
|
32
|
+
createdAt: iso(r.created_at),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 能处理该审批的人(spec 3.6 R2):
|
|
37
|
+
* - 指定了 `requested_for` → 只有那个人;
|
|
38
|
+
* - 按角色升级(`approver_role`)→ 当前角色不低于它、且不是该运行发起人的成员(发起人不能批准按角色升级到别人的请求)。
|
|
39
|
+
* agent / 服务令牌不能处理任何审批(路由动作 `approvals.decide` 仅限用户主体)。
|
|
40
|
+
*/
|
|
41
|
+
function canHandle(a, initiator, accountId, role) {
|
|
42
|
+
if (a.requestedFor !== null)
|
|
43
|
+
return a.requestedFor === accountId;
|
|
44
|
+
return (a.approverRole !== null &&
|
|
45
|
+
role !== null &&
|
|
46
|
+
roleAtLeast(role, a.approverRole) &&
|
|
47
|
+
initiator !== accountId);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 工具调用审批:新建(loop 调用)、列出 / 查看 / 处理(HTTP),以及「以后都允许」授权的查看与撤销。
|
|
51
|
+
* 状态全部在 PG:处理决定在事务里 `SELECT … FOR UPDATE` 审批行,多副本并发处理同一审批时恰好一个生效。
|
|
52
|
+
*/
|
|
53
|
+
export class ApprovalsService {
|
|
54
|
+
deps;
|
|
55
|
+
constructor(deps) {
|
|
56
|
+
this.deps = deps;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 新建 pending 审批(`expires_at` = 现在 + `tools.approvalTimeoutHours`)并写 `approval_requested`,同一事务。
|
|
60
|
+
* 按 (run_id, call_id, attempt) 幂等:已存在则返回已有记录(不覆盖),事件按 `approval_requested:<approvalId>` 幂等。
|
|
61
|
+
*/
|
|
62
|
+
async request(input) {
|
|
63
|
+
if (input.requestedFor === null && input.approverRole === null) {
|
|
64
|
+
throw new Error("approval requires requestedFor or approverRole");
|
|
65
|
+
}
|
|
66
|
+
const { db, eventLog, config } = this.deps;
|
|
67
|
+
const hours = config().tools.approvalTimeoutHours;
|
|
68
|
+
const { record, toSeq } = await db.transaction().execute(async (t) => {
|
|
69
|
+
// Lock the conversation row (the append's lock) before inserting the approval: the insert's
|
|
70
|
+
// foreign key check takes FOR KEY SHARE on that row, and two requests that each held it and
|
|
71
|
+
// then asked for the append's FOR UPDATE waited for each other (40P01)
|
|
72
|
+
await t
|
|
73
|
+
.selectFrom("conversations")
|
|
74
|
+
.select("id")
|
|
75
|
+
.where("id", "=", input.conversationId)
|
|
76
|
+
.forUpdate()
|
|
77
|
+
.execute();
|
|
78
|
+
await t
|
|
79
|
+
.insertInto("approvals")
|
|
80
|
+
.values({
|
|
81
|
+
id: randomUUID(),
|
|
82
|
+
org_id: input.orgId,
|
|
83
|
+
conversation_id: input.conversationId,
|
|
84
|
+
run_id: input.runId,
|
|
85
|
+
call_id: input.callId,
|
|
86
|
+
attempt: input.attempt ?? 1,
|
|
87
|
+
tool_key: input.toolKey,
|
|
88
|
+
spec_hash: input.specHash,
|
|
89
|
+
connector_id: input.connectorId,
|
|
90
|
+
input: JSON.stringify(input.input ?? {}),
|
|
91
|
+
requested_for: input.requestedFor,
|
|
92
|
+
approver_role: input.approverRole,
|
|
93
|
+
scope: null,
|
|
94
|
+
decided_by: null,
|
|
95
|
+
expires_at: sql `now() + ${hours}::integer * interval '1 hour'`,
|
|
96
|
+
})
|
|
97
|
+
.onConflict((oc) => oc.columns(["run_id", "call_id", "attempt"]).doNothing())
|
|
98
|
+
.execute();
|
|
99
|
+
const row = await t
|
|
100
|
+
.selectFrom("approvals")
|
|
101
|
+
.selectAll()
|
|
102
|
+
.where("run_id", "=", input.runId)
|
|
103
|
+
.where("call_id", "=", input.callId)
|
|
104
|
+
.where("attempt", "=", input.attempt ?? 1)
|
|
105
|
+
.executeTakeFirstOrThrow();
|
|
106
|
+
const rec = toRecord(row);
|
|
107
|
+
const written = await eventLog.append(rec.conversationId, [
|
|
108
|
+
{
|
|
109
|
+
type: "approval_requested",
|
|
110
|
+
runId: rec.runId,
|
|
111
|
+
idemKey: `approval_requested:${rec.id}`,
|
|
112
|
+
payload: {
|
|
113
|
+
step: input.step,
|
|
114
|
+
callId: rec.callId,
|
|
115
|
+
approvalId: rec.id,
|
|
116
|
+
toolKey: rec.toolKey,
|
|
117
|
+
expiresAt: rec.expiresAt,
|
|
118
|
+
...(input.policy !== undefined && { policy: input.policy }),
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
], t);
|
|
122
|
+
return { record: rec, toSeq: Math.max(...written.map((e) => e.seq)) };
|
|
123
|
+
});
|
|
124
|
+
await eventLog.notify(record.conversationId, toSeq);
|
|
125
|
+
return record;
|
|
126
|
+
}
|
|
127
|
+
/** 当前用户能处理的审批(新的在前,最多 {@link APPROVALS_LIST_MAX} 条);`status` 缺省为全部。 */
|
|
128
|
+
async list(orgId, accountId, status) {
|
|
129
|
+
const role = await this.roleOf(this.deps.db, orgId, accountId);
|
|
130
|
+
const roles = role === null ? [] : ORG_ROLES.filter((r) => roleAtLeast(role, r));
|
|
131
|
+
let q = this.deps.db
|
|
132
|
+
.selectFrom("approvals")
|
|
133
|
+
.innerJoin("runs", "runs.id", "approvals.run_id")
|
|
134
|
+
.selectAll("approvals")
|
|
135
|
+
.where("approvals.org_id", "=", orgId)
|
|
136
|
+
.where((eb) => eb.or([
|
|
137
|
+
eb("approvals.requested_for", "=", accountId),
|
|
138
|
+
eb.and([
|
|
139
|
+
eb("approvals.requested_for", "is", null),
|
|
140
|
+
roles.length === 0 ? eb.lit(false) : eb("approvals.approver_role", "in", [...roles]),
|
|
141
|
+
eb("runs.trigger_account_id", "!=", accountId),
|
|
142
|
+
]),
|
|
143
|
+
]));
|
|
144
|
+
if (status !== undefined)
|
|
145
|
+
q = q.where("approvals.status", "=", status);
|
|
146
|
+
// 已到期、超时任务尚未处理的审批不再列为待处理(处理它会得到 approval.already_decided)
|
|
147
|
+
if (status === "pending")
|
|
148
|
+
q = q.where("approvals.expires_at", ">", sql `now()`);
|
|
149
|
+
const rows = await q
|
|
150
|
+
.orderBy("approvals.created_at", "desc")
|
|
151
|
+
.limit(APPROVALS_LIST_MAX)
|
|
152
|
+
.execute();
|
|
153
|
+
return this.views(rows.map(toRecord));
|
|
154
|
+
}
|
|
155
|
+
/** 详情;当前用户不能处理的一律 `approval.not_found`(不泄露存在)。 */
|
|
156
|
+
async get(orgId, id, accountId) {
|
|
157
|
+
const row = await this.deps.db
|
|
158
|
+
.selectFrom("approvals")
|
|
159
|
+
.innerJoin("runs", "runs.id", "approvals.run_id")
|
|
160
|
+
.selectAll("approvals")
|
|
161
|
+
.select("runs.trigger_account_id")
|
|
162
|
+
.where("approvals.id", "=", id)
|
|
163
|
+
.where("approvals.org_id", "=", orgId)
|
|
164
|
+
.executeTakeFirst();
|
|
165
|
+
if (!row)
|
|
166
|
+
throw notFound();
|
|
167
|
+
const role = await this.roleOf(this.deps.db, orgId, accountId);
|
|
168
|
+
const rec = toRecord(row);
|
|
169
|
+
if (!canHandle(rec, row.trigger_account_id, accountId, role))
|
|
170
|
+
throw notFound();
|
|
171
|
+
const [view] = await this.views([rec]);
|
|
172
|
+
if (!view)
|
|
173
|
+
throw notFound();
|
|
174
|
+
return view;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 处理审批:事务内 `SELECT … FOR UPDATE` 审批行并实时重读处理人角色(R5);不能处理 → `approval.not_found`,
|
|
178
|
+
* 非 pending 或已过期 → `approval.already_decided`。同一事务写状态、授权(`session` → 会话授权,
|
|
179
|
+
* `always` → 发起人 × agent × 工具)、`approval_decided` 事件(幂等键 `approval_decided:<id>`)与审计;
|
|
180
|
+
* 提交后发布事件并调用 `onDecided`。
|
|
181
|
+
*/
|
|
182
|
+
async decide(orgId, id, accountId, body, actorTokenId) {
|
|
183
|
+
const { db, eventLog } = this.deps;
|
|
184
|
+
const { record, toSeq } = await db.transaction().execute(async (t) => {
|
|
185
|
+
const locked = await t
|
|
186
|
+
.selectFrom("approvals")
|
|
187
|
+
.selectAll()
|
|
188
|
+
.select(sql `expires_at <= now()`.as("expired"))
|
|
189
|
+
.where("id", "=", id)
|
|
190
|
+
.where("org_id", "=", orgId)
|
|
191
|
+
.forUpdate()
|
|
192
|
+
.executeTakeFirst();
|
|
193
|
+
if (!locked)
|
|
194
|
+
throw notFound();
|
|
195
|
+
const ctx = await t
|
|
196
|
+
.selectFrom("runs")
|
|
197
|
+
.innerJoin("conversations", "conversations.id", "runs.conversation_id")
|
|
198
|
+
.select([
|
|
199
|
+
"runs.trigger_account_id",
|
|
200
|
+
"conversations.agent_id",
|
|
201
|
+
"conversations.parent_conversation_id",
|
|
202
|
+
])
|
|
203
|
+
.where("runs.id", "=", locked.run_id)
|
|
204
|
+
.executeTakeFirstOrThrow();
|
|
205
|
+
const before = toRecord(locked);
|
|
206
|
+
const role = await this.roleOf(t, orgId, accountId);
|
|
207
|
+
if (!canHandle(before, ctx.trigger_account_id, accountId, role))
|
|
208
|
+
throw notFound();
|
|
209
|
+
if (before.status !== "pending") {
|
|
210
|
+
throw new ApiError("approval.already_decided", "Approval has already been decided");
|
|
211
|
+
}
|
|
212
|
+
if (locked.expired) {
|
|
213
|
+
throw new ApiError("approval.already_decided", "Approval has expired");
|
|
214
|
+
}
|
|
215
|
+
const approved = body.decision === "approve";
|
|
216
|
+
const reason = approved ? undefined : body.reason?.trim() || undefined;
|
|
217
|
+
const scope = approved ? (body.scope ?? "once") : null;
|
|
218
|
+
// 按角色升级的批准只对这一次有效:授权记在发起人名下会让其以后绕过角色要求
|
|
219
|
+
if (scope !== null && scope !== "once" && before.requestedFor === null) {
|
|
220
|
+
throw new ApiError("validation_failed", 'Only scope "once" is allowed for role-escalated approvals', [{ path: "scope", message: 'Only "once" is allowed for role-escalated approvals' }]);
|
|
221
|
+
}
|
|
222
|
+
const updated = await t
|
|
223
|
+
.updateTable("approvals")
|
|
224
|
+
.set({
|
|
225
|
+
status: approved ? "approved" : "denied",
|
|
226
|
+
scope,
|
|
227
|
+
decided_by: accountId,
|
|
228
|
+
decided_at: sql `now()`,
|
|
229
|
+
deny_reason: reason ?? null,
|
|
230
|
+
})
|
|
231
|
+
.where("id", "=", id)
|
|
232
|
+
.returningAll()
|
|
233
|
+
.executeTakeFirstOrThrow();
|
|
234
|
+
// 授权绑定所批准的定义指纹与来源连接器(同 slug 重新安装的连接器 id 不同,不继承)
|
|
235
|
+
const hash = before.specHash;
|
|
236
|
+
const connectorId = before.connectorId;
|
|
237
|
+
if (scope === "session") {
|
|
238
|
+
// a sub-agent's grant is its parent conversation's (sub-agents spec §9): its siblings and
|
|
239
|
+
// the parent itself are not asked again for the same call
|
|
240
|
+
await t
|
|
241
|
+
.insertInto("conversation_tool_grants")
|
|
242
|
+
.values({
|
|
243
|
+
conversation_id: ctx.parent_conversation_id ?? before.conversationId,
|
|
244
|
+
tool_key: before.toolKey,
|
|
245
|
+
spec_hash: hash,
|
|
246
|
+
connector_id: connectorId,
|
|
247
|
+
})
|
|
248
|
+
.onConflict((oc) => oc
|
|
249
|
+
.columns(["conversation_id", "tool_key"])
|
|
250
|
+
.doUpdateSet({ spec_hash: hash, connector_id: connectorId }))
|
|
251
|
+
.execute();
|
|
252
|
+
}
|
|
253
|
+
else if (scope === "always" && before.requestedFor !== null) {
|
|
254
|
+
await t
|
|
255
|
+
.insertInto("tool_grants")
|
|
256
|
+
.values({
|
|
257
|
+
org_id: orgId,
|
|
258
|
+
account_id: before.requestedFor,
|
|
259
|
+
agent_id: ctx.agent_id,
|
|
260
|
+
tool_key: before.toolKey,
|
|
261
|
+
spec_hash: hash,
|
|
262
|
+
connector_id: connectorId,
|
|
263
|
+
})
|
|
264
|
+
.onConflict((oc) => oc.columns(["org_id", "account_id", "agent_id", "tool_key"]).doUpdateSet({
|
|
265
|
+
spec_hash: hash,
|
|
266
|
+
connector_id: connectorId,
|
|
267
|
+
created_at: sql `now()`,
|
|
268
|
+
}))
|
|
269
|
+
.execute();
|
|
270
|
+
}
|
|
271
|
+
const written = await eventLog.append(before.conversationId, [
|
|
272
|
+
{
|
|
273
|
+
type: "approval_decided",
|
|
274
|
+
runId: before.runId,
|
|
275
|
+
idemKey: `approval_decided:${id}`,
|
|
276
|
+
payload: {
|
|
277
|
+
approvalId: id,
|
|
278
|
+
callId: before.callId,
|
|
279
|
+
decision: approved ? "approved" : "denied",
|
|
280
|
+
...(scope !== null && { scope }),
|
|
281
|
+
byAccountId: accountId,
|
|
282
|
+
...(reason !== undefined && { reason }),
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
], t);
|
|
286
|
+
await audit(t, {
|
|
287
|
+
actorAccountId: accountId,
|
|
288
|
+
...(actorTokenId !== undefined && { actorTokenId }),
|
|
289
|
+
orgId,
|
|
290
|
+
action: "approval.decided",
|
|
291
|
+
target: id,
|
|
292
|
+
detail: {
|
|
293
|
+
toolKey: before.toolKey,
|
|
294
|
+
conversationId: before.conversationId,
|
|
295
|
+
runId: before.runId,
|
|
296
|
+
agentId: ctx.agent_id,
|
|
297
|
+
initiatorAccountId: ctx.trigger_account_id,
|
|
298
|
+
decision: approved ? "approved" : "denied",
|
|
299
|
+
scope,
|
|
300
|
+
...(body.note !== undefined && { note: body.note }),
|
|
301
|
+
...(reason !== undefined && { reason }),
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
return { record: toRecord(updated), toSeq: Math.max(...written.map((e) => e.seq)) };
|
|
305
|
+
});
|
|
306
|
+
await eventLog.notify(record.conversationId, toSeq);
|
|
307
|
+
if (this.deps.onDecided) {
|
|
308
|
+
try {
|
|
309
|
+
await this.deps.onDecided(record);
|
|
310
|
+
}
|
|
311
|
+
catch (err) {
|
|
312
|
+
this.deps.log.warn("approval onDecided hook failed", { approvalId: record.id, err });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return record;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* 详情,不做处理人校验(内核内部用:渠道出站投递组装 IM 审批卡片);不存在返回 null。
|
|
319
|
+
* 不得直接回给请求方——HTTP 接口用 {@link get}。
|
|
320
|
+
*/
|
|
321
|
+
async describe(id) {
|
|
322
|
+
const row = await this.deps.db
|
|
323
|
+
.selectFrom("approvals")
|
|
324
|
+
.selectAll()
|
|
325
|
+
.where("id", "=", id)
|
|
326
|
+
.executeTakeFirst();
|
|
327
|
+
if (!row)
|
|
328
|
+
return null;
|
|
329
|
+
const [view] = await this.views([toRecord(row)]);
|
|
330
|
+
return view ?? null;
|
|
331
|
+
}
|
|
332
|
+
/** 某个工具调用最近一次的审批(attempt 最大者);没有则返回 null。 */
|
|
333
|
+
async forCall(runId, callId) {
|
|
334
|
+
const row = await this.deps.db
|
|
335
|
+
.selectFrom("approvals")
|
|
336
|
+
.selectAll()
|
|
337
|
+
.where("run_id", "=", runId)
|
|
338
|
+
.where("call_id", "=", callId)
|
|
339
|
+
.orderBy("attempt", "desc")
|
|
340
|
+
.limit(1)
|
|
341
|
+
.executeTakeFirst();
|
|
342
|
+
return row ? toRecord(row) : null;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* 超时(等待机制的超时任务调用):仍 pending 且已到期 → 置 expired、写 `approval_decided{expired}`(处理人为 null)。
|
|
346
|
+
* 与人工决定都锁同一行,恰好一个生效。返回 `expired`(本次生效)/ `not_due`(尚未到期)/
|
|
347
|
+
* `settled`(已处理、已取消或不存在)。
|
|
348
|
+
*/
|
|
349
|
+
async expire(id) {
|
|
350
|
+
const { db, eventLog } = this.deps;
|
|
351
|
+
const r = await db.transaction().execute(async (t) => {
|
|
352
|
+
const locked = await t
|
|
353
|
+
.selectFrom("approvals")
|
|
354
|
+
.selectAll()
|
|
355
|
+
.select(sql `expires_at <= now()`.as("expired"))
|
|
356
|
+
.where("id", "=", id)
|
|
357
|
+
.forUpdate()
|
|
358
|
+
.executeTakeFirst();
|
|
359
|
+
if (!locked)
|
|
360
|
+
return { outcome: "settled" };
|
|
361
|
+
const before = toRecord(locked);
|
|
362
|
+
if (before.status !== "pending")
|
|
363
|
+
return { outcome: "settled", record: before };
|
|
364
|
+
if (!locked.expired)
|
|
365
|
+
return { outcome: "not_due", record: before };
|
|
366
|
+
const updated = await t
|
|
367
|
+
.updateTable("approvals")
|
|
368
|
+
.set({ status: "expired", decided_at: sql `now()` })
|
|
369
|
+
.where("id", "=", id)
|
|
370
|
+
.returningAll()
|
|
371
|
+
.executeTakeFirstOrThrow();
|
|
372
|
+
const toSeq = await this.decidedEvent(t, before, "expired");
|
|
373
|
+
return { outcome: "expired", record: toRecord(updated), toSeq };
|
|
374
|
+
});
|
|
375
|
+
if ("toSeq" in r && r.toSeq !== undefined && r.record) {
|
|
376
|
+
await eventLog.notify(r.record.conversationId, r.toSeq);
|
|
377
|
+
}
|
|
378
|
+
return r.record ? { outcome: r.outcome, record: r.record } : { outcome: r.outcome };
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* 在事务 `t` 内取消运行的全部 pending 审批(运行在等待中被中止),各写 `approval_decided{cancelled}`
|
|
382
|
+
* (处理人为 null)。返回被取消的记录;调用方提交后发布事件。
|
|
383
|
+
*/
|
|
384
|
+
async cancelPendingIn(t, runId) {
|
|
385
|
+
const rows = await t
|
|
386
|
+
.selectFrom("approvals")
|
|
387
|
+
.selectAll()
|
|
388
|
+
.where("run_id", "=", runId)
|
|
389
|
+
.where("status", "=", "pending")
|
|
390
|
+
.orderBy("created_at")
|
|
391
|
+
.forUpdate()
|
|
392
|
+
.execute();
|
|
393
|
+
const out = [];
|
|
394
|
+
for (const row of rows) {
|
|
395
|
+
const updated = await t
|
|
396
|
+
.updateTable("approvals")
|
|
397
|
+
.set({ status: "cancelled", decided_at: sql `now()` })
|
|
398
|
+
.where("id", "=", row.id)
|
|
399
|
+
.returningAll()
|
|
400
|
+
.executeTakeFirstOrThrow();
|
|
401
|
+
await this.decidedEvent(t, toRecord(row), "cancelled");
|
|
402
|
+
out.push(toRecord(updated));
|
|
403
|
+
}
|
|
404
|
+
return out;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* 在事务 `t` 内取消某组织里指定由 `accountId` 处理(`requested_for`)的全部 pending 审批(成员离职),
|
|
408
|
+
* 走与 {@link cancelPendingIn} 相同的取消路径:置 cancelled、写 `approval_decided{cancelled}`(处理人为 null)。
|
|
409
|
+
* 返回被取消的记录与各会话写到的最大 seq;调用方提交后发布事件(并可续跑对应运行)。
|
|
410
|
+
*/
|
|
411
|
+
async cancelPendingFor(t, orgId, accountId) {
|
|
412
|
+
const rows = await t
|
|
413
|
+
.selectFrom("approvals")
|
|
414
|
+
.selectAll()
|
|
415
|
+
.where("org_id", "=", orgId)
|
|
416
|
+
.where("requested_for", "=", accountId)
|
|
417
|
+
.where("status", "=", "pending")
|
|
418
|
+
.orderBy("created_at")
|
|
419
|
+
.forUpdate()
|
|
420
|
+
.execute();
|
|
421
|
+
const records = [];
|
|
422
|
+
const seqs = new Map();
|
|
423
|
+
for (const row of rows) {
|
|
424
|
+
const updated = await t
|
|
425
|
+
.updateTable("approvals")
|
|
426
|
+
.set({ status: "cancelled", decided_at: sql `now()` })
|
|
427
|
+
.where("id", "=", row.id)
|
|
428
|
+
.returningAll()
|
|
429
|
+
.executeTakeFirstOrThrow();
|
|
430
|
+
const seq = await this.decidedEvent(t, toRecord(row), "cancelled");
|
|
431
|
+
seqs.set(row.conversation_id, Math.max(seqs.get(row.conversation_id) ?? 0, seq));
|
|
432
|
+
records.push(toRecord(updated));
|
|
433
|
+
}
|
|
434
|
+
return { records, seqs };
|
|
435
|
+
}
|
|
436
|
+
/** 系统写的 `approval_decided`(超时 / 取消,处理人为 null),返回事件 seq。 */
|
|
437
|
+
async decidedEvent(t, a, decision) {
|
|
438
|
+
const written = await this.deps.eventLog.append(a.conversationId, [
|
|
439
|
+
{
|
|
440
|
+
type: "approval_decided",
|
|
441
|
+
runId: a.runId,
|
|
442
|
+
idemKey: `approval_decided:${a.id}`,
|
|
443
|
+
payload: { approvalId: a.id, callId: a.callId, decision, byAccountId: null },
|
|
444
|
+
},
|
|
445
|
+
], t);
|
|
446
|
+
return Math.max(...written.map((e) => e.seq));
|
|
447
|
+
}
|
|
448
|
+
/** 本人在该组织的「以后都允许」授权。 */
|
|
449
|
+
async listGrants(orgId, accountId) {
|
|
450
|
+
const rows = await this.deps.db
|
|
451
|
+
.selectFrom("tool_grants")
|
|
452
|
+
.select(["agent_id", "tool_key", "spec_hash", "created_at"])
|
|
453
|
+
.where("org_id", "=", orgId)
|
|
454
|
+
.where("account_id", "=", accountId)
|
|
455
|
+
.orderBy("created_at", "desc")
|
|
456
|
+
.execute();
|
|
457
|
+
const names = await this.agentNames([...new Set(rows.map((r) => r.agent_id))]);
|
|
458
|
+
return rows.map((r) => ({
|
|
459
|
+
agentId: r.agent_id,
|
|
460
|
+
agentName: names.get(r.agent_id) ?? "",
|
|
461
|
+
toolKey: r.tool_key,
|
|
462
|
+
specHash: r.spec_hash,
|
|
463
|
+
createdAt: iso(r.created_at),
|
|
464
|
+
}));
|
|
465
|
+
}
|
|
466
|
+
/** 撤销本人的一条授权;不存在时什么也不做(幂等)。 */
|
|
467
|
+
async revokeGrant(orgId, accountId, agentId, toolKey, actorTokenId) {
|
|
468
|
+
await this.deps.db.transaction().execute(async (t) => {
|
|
469
|
+
const res = await t
|
|
470
|
+
.deleteFrom("tool_grants")
|
|
471
|
+
.where("org_id", "=", orgId)
|
|
472
|
+
.where("account_id", "=", accountId)
|
|
473
|
+
.where("agent_id", "=", agentId)
|
|
474
|
+
.where("tool_key", "=", toolKey)
|
|
475
|
+
.executeTakeFirst();
|
|
476
|
+
if (Number(res.numDeletedRows) === 0)
|
|
477
|
+
return;
|
|
478
|
+
await audit(t, {
|
|
479
|
+
actorAccountId: accountId,
|
|
480
|
+
...(actorTokenId !== undefined && { actorTokenId }),
|
|
481
|
+
orgId,
|
|
482
|
+
action: "tool_grant.revoked",
|
|
483
|
+
target: agentId,
|
|
484
|
+
detail: { toolKey },
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
async roleOf(db, orgId, accountId) {
|
|
489
|
+
const m = await db
|
|
490
|
+
.selectFrom("memberships")
|
|
491
|
+
.select("role")
|
|
492
|
+
.where("org_id", "=", orgId)
|
|
493
|
+
.where("account_id", "=", accountId)
|
|
494
|
+
.executeTakeFirst();
|
|
495
|
+
return m?.role ?? null;
|
|
496
|
+
}
|
|
497
|
+
/** agent id → 当前版本定义里的名字。 */
|
|
498
|
+
async agentNames(ids) {
|
|
499
|
+
if (ids.length === 0)
|
|
500
|
+
return new Map();
|
|
501
|
+
const rows = await this.deps.db
|
|
502
|
+
.selectFrom("agents")
|
|
503
|
+
.innerJoin("agent_versions", (j) => j
|
|
504
|
+
.onRef("agent_versions.agent_id", "=", "agents.id")
|
|
505
|
+
.onRef("agent_versions.version", "=", "agents.current_version"))
|
|
506
|
+
.select(["agents.id", "agent_versions.definition"])
|
|
507
|
+
.where("agents.id", "in", ids)
|
|
508
|
+
.execute();
|
|
509
|
+
return new Map(rows.map((r) => {
|
|
510
|
+
const name = r.definition?.name;
|
|
511
|
+
return [r.id, typeof name === "string" ? name : ""];
|
|
512
|
+
}));
|
|
513
|
+
}
|
|
514
|
+
/** 补齐详情字段(批量读会话、运行、agent 名与连接器)。 */
|
|
515
|
+
async views(recs) {
|
|
516
|
+
if (recs.length === 0)
|
|
517
|
+
return [];
|
|
518
|
+
const { db } = this.deps;
|
|
519
|
+
const ctxRows = await db
|
|
520
|
+
.selectFrom("runs")
|
|
521
|
+
.innerJoin("conversations", "conversations.id", "runs.conversation_id")
|
|
522
|
+
.select([
|
|
523
|
+
"runs.id",
|
|
524
|
+
"runs.trigger_account_id",
|
|
525
|
+
"conversations.agent_id",
|
|
526
|
+
"conversations.parent_conversation_id",
|
|
527
|
+
])
|
|
528
|
+
.where("runs.id", "in", [...new Set(recs.map((r) => r.runId))])
|
|
529
|
+
.execute();
|
|
530
|
+
const ctx = new Map(ctxRows.map((r) => [r.id, r]));
|
|
531
|
+
const names = await this.agentNames([...new Set(ctxRows.map((r) => r.agent_id))]);
|
|
532
|
+
const connectorIds = [
|
|
533
|
+
...new Set(recs.flatMap((r) => (r.connectorId === null ? [] : [r.connectorId]))),
|
|
534
|
+
];
|
|
535
|
+
const apps = connectorIds.length === 0
|
|
536
|
+
? []
|
|
537
|
+
: await db
|
|
538
|
+
.selectFrom("connectors")
|
|
539
|
+
.select(["id", "name", "slug"])
|
|
540
|
+
.where("id", "in", connectorIds)
|
|
541
|
+
.execute();
|
|
542
|
+
const connectorById = new Map(apps.map((a) => [a.id, a]));
|
|
543
|
+
return recs.flatMap((r) => {
|
|
544
|
+
const c = ctx.get(r.runId);
|
|
545
|
+
if (!c)
|
|
546
|
+
return [];
|
|
547
|
+
const app = r.connectorId === null ? undefined : connectorById.get(r.connectorId);
|
|
548
|
+
const prefix = app ? `${app.slug}__` : undefined;
|
|
549
|
+
return [
|
|
550
|
+
{
|
|
551
|
+
...r,
|
|
552
|
+
toolName: prefix !== undefined && r.toolKey.startsWith(prefix)
|
|
553
|
+
? r.toolKey.slice(prefix.length)
|
|
554
|
+
: r.toolKey,
|
|
555
|
+
connector: app ? { id: app.id, name: app.name, slug: app.slug } : null,
|
|
556
|
+
agent: { id: c.agent_id, name: names.get(c.agent_id) ?? "" },
|
|
557
|
+
...(c.parent_conversation_id !== null
|
|
558
|
+
? {
|
|
559
|
+
conversationPath: `/chat/${c.parent_conversation_id}?subagent=${r.conversationId}`,
|
|
560
|
+
parentConversationId: c.parent_conversation_id,
|
|
561
|
+
}
|
|
562
|
+
: { conversationPath: `/chat/${r.conversationId}` }),
|
|
563
|
+
initiatorAccountId: c.trigger_account_id,
|
|
564
|
+
},
|
|
565
|
+
];
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Redis } from "ioredis";
|
|
2
|
+
import type { KernelConfig } from "../config/schema.js";
|
|
3
|
+
/** 熔断器键:每个组织 × 连接器一个 hash(`f` = 连续失败次数,`o` = 打开到期的毫秒时间戳)。 */
|
|
4
|
+
export declare const breakerKey: (orgId: string, connectorId: string) => string;
|
|
5
|
+
export type BreakerState = {
|
|
6
|
+
open: false;
|
|
7
|
+
} | {
|
|
8
|
+
open: true;
|
|
9
|
+
failures: number;
|
|
10
|
+
retryAfterSeconds: number;
|
|
11
|
+
};
|
|
12
|
+
/** 熔断打开时给模型的说明(英文)。 */
|
|
13
|
+
export declare function breakerOpenMessage(connectorName: string, failures: number, retryAfterSeconds: number): string;
|
|
14
|
+
/**
|
|
15
|
+
* 连接器级熔断器(spec 5.4;只用于连接器工具):同一组织同一连接器连续失败 `tools.breakerFailures` 次后打开
|
|
16
|
+
* `tools.breakerOpenSeconds` 秒,期间直接拒绝;到期后半开放行,成功清零、失败立即重新打开。
|
|
17
|
+
* 状态只在 Redis(多副本共享),计数与打开状态都带 TTL。
|
|
18
|
+
*/
|
|
19
|
+
export declare class Breaker {
|
|
20
|
+
private readonly redis;
|
|
21
|
+
private readonly config;
|
|
22
|
+
constructor(redis: Redis, config: () => KernelConfig);
|
|
23
|
+
check(orgId: string, connectorId: string): Promise<BreakerState>;
|
|
24
|
+
/** 记一次失败,返回当前连续失败次数。 */
|
|
25
|
+
failure(orgId: string, connectorId: string): Promise<number>;
|
|
26
|
+
/** 调用成功:清零。 */
|
|
27
|
+
success(orgId: string, connectorId: string): Promise<void>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** 熔断器键:每个组织 × 连接器一个 hash(`f` = 连续失败次数,`o` = 打开到期的毫秒时间戳)。 */
|
|
2
|
+
export const breakerKey = (orgId, connectorId) => `capk:breaker:${orgId}:${connectorId}`;
|
|
3
|
+
/**
|
|
4
|
+
* 连续失败计数的保留时间:没有新的失败也没有成功时,计数在这之后过期(多副本纪律:Redis 写入带 TTL)。
|
|
5
|
+
* 打开后整个键的 TTL = 打开时长 + 该值,打开到期后进入半开(放行调用;再失败立即重新打开)。
|
|
6
|
+
*/
|
|
7
|
+
const COUNT_TTL_MS = 3_600_000;
|
|
8
|
+
/** 时间统一取 Redis 服务端时间(多副本时钟不一致也不影响判定)。 */
|
|
9
|
+
const NOW = `local t = redis.call('TIME'); local now = tonumber(t[1]) * 1000 + math.floor(tonumber(t[2]) / 1000)`;
|
|
10
|
+
/**
|
|
11
|
+
* 记一次失败:计数 +1;达到阈值、或处于半开(曾打开且已到期)时打开 / 重新打开;
|
|
12
|
+
* 仍在打开期内的失败(打开前已发出的调用)只计数,不延长打开时间。返回失败次数。
|
|
13
|
+
*/
|
|
14
|
+
const FAILURE = `
|
|
15
|
+
${NOW}
|
|
16
|
+
local n = redis.call('HINCRBY', KEYS[1], 'f', 1)
|
|
17
|
+
local o = tonumber(redis.call('HGET', KEYS[1], 'o') or '0')
|
|
18
|
+
local openMs = tonumber(ARGV[2])
|
|
19
|
+
local countMs = tonumber(ARGV[3])
|
|
20
|
+
if o > now then
|
|
21
|
+
return n
|
|
22
|
+
end
|
|
23
|
+
if n >= tonumber(ARGV[1]) or o > 0 then
|
|
24
|
+
redis.call('HSET', KEYS[1], 'o', now + openMs)
|
|
25
|
+
redis.call('PEXPIRE', KEYS[1], openMs + countMs)
|
|
26
|
+
else
|
|
27
|
+
redis.call('PEXPIRE', KEYS[1], countMs)
|
|
28
|
+
end
|
|
29
|
+
return n
|
|
30
|
+
`;
|
|
31
|
+
/** 读状态:返回 {失败次数, 剩余打开毫秒}(未打开为 0)。 */
|
|
32
|
+
const CHECK = `
|
|
33
|
+
${NOW}
|
|
34
|
+
local v = redis.call('HMGET', KEYS[1], 'f', 'o')
|
|
35
|
+
local f = tonumber(v[1] or '0')
|
|
36
|
+
local o = tonumber(v[2] or '0')
|
|
37
|
+
local left = 0
|
|
38
|
+
if o > now then left = o - now end
|
|
39
|
+
return {f, left}
|
|
40
|
+
`;
|
|
41
|
+
/** 熔断打开时给模型的说明(英文)。 */
|
|
42
|
+
export function breakerOpenMessage(connectorName, failures, retryAfterSeconds) {
|
|
43
|
+
return `Tool temporarily unavailable: ${connectorName} failed ${failures} times in a row; retry after ${retryAfterSeconds}s.`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 连接器级熔断器(spec 5.4;只用于连接器工具):同一组织同一连接器连续失败 `tools.breakerFailures` 次后打开
|
|
47
|
+
* `tools.breakerOpenSeconds` 秒,期间直接拒绝;到期后半开放行,成功清零、失败立即重新打开。
|
|
48
|
+
* 状态只在 Redis(多副本共享),计数与打开状态都带 TTL。
|
|
49
|
+
*/
|
|
50
|
+
export class Breaker {
|
|
51
|
+
redis;
|
|
52
|
+
config;
|
|
53
|
+
constructor(redis, config) {
|
|
54
|
+
this.redis = redis;
|
|
55
|
+
this.config = config;
|
|
56
|
+
}
|
|
57
|
+
async check(orgId, connectorId) {
|
|
58
|
+
const [failures, left] = (await this.redis.eval(CHECK, 1, breakerKey(orgId, connectorId)));
|
|
59
|
+
if (left <= 0)
|
|
60
|
+
return { open: false };
|
|
61
|
+
return { open: true, failures, retryAfterSeconds: Math.ceil(left / 1000) };
|
|
62
|
+
}
|
|
63
|
+
/** 记一次失败,返回当前连续失败次数。 */
|
|
64
|
+
async failure(orgId, connectorId) {
|
|
65
|
+
const { breakerFailures, breakerOpenSeconds } = this.config().tools;
|
|
66
|
+
return Number(await this.redis.eval(FAILURE, 1, breakerKey(orgId, connectorId), breakerFailures, breakerOpenSeconds * 1000, COUNT_TTL_MS));
|
|
67
|
+
}
|
|
68
|
+
/** 调用成功:清零。 */
|
|
69
|
+
async success(orgId, connectorId) {
|
|
70
|
+
await this.redis.del(breakerKey(orgId, connectorId));
|
|
71
|
+
}
|
|
72
|
+
}
|