@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,677 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { validateParams } from "@capybara-hq/plugin-sdk";
|
|
3
|
+
import { sql } from "kysely";
|
|
4
|
+
import { ApiError } from "../errors.js";
|
|
5
|
+
import { cancelEvalRuns } from "../evals/budget.js";
|
|
6
|
+
import { audit } from "../identity/audit.js";
|
|
7
|
+
import { rejectAnonymousGuest } from "../orgs/members.service.js";
|
|
8
|
+
import { lockOrgAndAuthorize } from "../orgs/org-lock.js";
|
|
9
|
+
import { roleAtLeast } from "../orgs/roles.js";
|
|
10
|
+
import { assertOrgSkills, assertPinnedSkills } from "../skills/skills.service.js";
|
|
11
|
+
import { normalizeStoredDefinition, SUBAGENT_NAME_RE } from "./definition.js";
|
|
12
|
+
export const AGENT_VISIBILITIES = ["private", "org", "members"];
|
|
13
|
+
/** 删除 agent 时每个销毁任务带的对话数。 */
|
|
14
|
+
const SANDBOX_DESTROY_BATCH = 500;
|
|
15
|
+
const notFound = () => new ApiError("agent.not_found", "Agent not found");
|
|
16
|
+
/** 按模型声明严格校验一组参数,返回 `{ path: "<field>.<key>", message }[]`(评测候选也用它)。 */
|
|
17
|
+
export function paramIssues(field, decl, params) {
|
|
18
|
+
const r = validateParams(decl, params);
|
|
19
|
+
return r.ok ? [] : r.issues.map((i) => ({ path: `${field}.${i.key}`, message: i.message }));
|
|
20
|
+
}
|
|
21
|
+
/** 键排序后的 JSON:jsonb 不保留键顺序,比较定义是否变化时用。 */
|
|
22
|
+
function canonical(v) {
|
|
23
|
+
if (Array.isArray(v))
|
|
24
|
+
return `[${v.map(canonical).join(",")}]`;
|
|
25
|
+
if (v !== null && typeof v === "object") {
|
|
26
|
+
const entries = Object.entries(v)
|
|
27
|
+
.filter(([, x]) => x !== undefined)
|
|
28
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
29
|
+
return `{${entries.map(([k, x]) => `${JSON.stringify(k)}:${canonical(x)}`).join(",")}}`;
|
|
30
|
+
}
|
|
31
|
+
return JSON.stringify(v);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 可见性判定(`canUse`)的 SQL 条件:agent 未删除、所属组织未删除,且
|
|
35
|
+
* - viewer 为账号:仍是该组织成员(含访客),并且是负责人 / visibility='org' / 在 agent_members 名单中;
|
|
36
|
+
* - viewer 为 null(该组织的服务令牌):只认 visibility='org'。
|
|
37
|
+
*/
|
|
38
|
+
export function visibleTo(viewerAccountId) {
|
|
39
|
+
return (eb) => {
|
|
40
|
+
const orgLive = eb.exists(eb
|
|
41
|
+
.selectFrom("orgs")
|
|
42
|
+
.select(sql `1`.as("one"))
|
|
43
|
+
.whereRef("orgs.id", "=", "agents.org_id")
|
|
44
|
+
.where("orgs.deleted_at", "is", null));
|
|
45
|
+
if (viewerAccountId === null) {
|
|
46
|
+
return eb.and([orgLive, eb("agents.visibility", "=", "org")]);
|
|
47
|
+
}
|
|
48
|
+
return eb.and([
|
|
49
|
+
orgLive,
|
|
50
|
+
eb.exists(eb
|
|
51
|
+
.selectFrom("memberships")
|
|
52
|
+
.select(sql `1`.as("one"))
|
|
53
|
+
.whereRef("memberships.org_id", "=", "agents.org_id")
|
|
54
|
+
.where("memberships.account_id", "=", viewerAccountId)),
|
|
55
|
+
eb.or([
|
|
56
|
+
eb("agents.owner_account_id", "=", viewerAccountId),
|
|
57
|
+
eb("agents.visibility", "=", "org"),
|
|
58
|
+
eb.and([
|
|
59
|
+
eb("agents.visibility", "=", "members"),
|
|
60
|
+
eb.exists(eb
|
|
61
|
+
.selectFrom("agent_members")
|
|
62
|
+
.select(sql `1`.as("one"))
|
|
63
|
+
.whereRef("agent_members.agent_id", "=", "agents.id")
|
|
64
|
+
.where("agent_members.account_id", "=", viewerAccountId)),
|
|
65
|
+
]),
|
|
66
|
+
]),
|
|
67
|
+
]);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Whether the live agent `agentId` is visible to `accountId` under the normal visibility rule
|
|
72
|
+
* ({@link visibleTo}: owner, `visibility = 'org'`, or listed in `agent_members`, while a member of
|
|
73
|
+
* the live org). Deleted agents are never visible.
|
|
74
|
+
*/
|
|
75
|
+
export async function isAgentVisibleTo(db, agentId, accountId) {
|
|
76
|
+
const row = await db
|
|
77
|
+
.selectFrom("agents")
|
|
78
|
+
.select("id")
|
|
79
|
+
.where("id", "=", agentId)
|
|
80
|
+
.where("deleted_at", "is", null)
|
|
81
|
+
.where(visibleTo(accountId))
|
|
82
|
+
.executeTakeFirst();
|
|
83
|
+
return row !== undefined;
|
|
84
|
+
}
|
|
85
|
+
/** Roster entries an agent may list (spec §4.3), general-purpose included. */
|
|
86
|
+
export const SUBAGENT_ROSTER_MAX = 20;
|
|
87
|
+
/**
|
|
88
|
+
* Checks `subagents` of an agent definition in the caller's transaction (spec §5.2); throws 400
|
|
89
|
+
* `agent.subagents_invalid` with one detail per problem (`{ reason, path, value? }`):
|
|
90
|
+
* - `not_found`: a template id that is not a live agent template of the org;
|
|
91
|
+
* - `invalid_name`: an inline name or `agents[].as` outside {@link SUBAGENT_NAME_RE}, or
|
|
92
|
+
* `general-purpose`; `duplicate`: a roster type listed twice (a template id twice included);
|
|
93
|
+
* - `tool_not_selected`: an inline template's `tools`, `disallowedTools` or `toolPolicies` key that
|
|
94
|
+
* is not a tool key of this agent (`toolKeys`, from `ToolRegistry.checkRefs`);
|
|
95
|
+
* - `skill_not_selected`: an inline skill id that is neither in `skills.orgSkills` nor a live
|
|
96
|
+
* skill of this agent;
|
|
97
|
+
* - `self`: this agent allowlisted; `agent_not_available`: an allowlisted agent that is not a live
|
|
98
|
+
* agent of the org visible to the person saving with `subagents.dispatchable: true` in its
|
|
99
|
+
* current version;
|
|
100
|
+
* - `too_many`: more than {@link SUBAGENT_ROSTER_MAX} roster entries.
|
|
101
|
+
* Template ids and allowlisted agents already in the current version (`kept`) are not re-checked
|
|
102
|
+
* for `not_found` / `agent_not_available`: a template uninstalled or an agent switched off later
|
|
103
|
+
* does not block other edits (the dispatch refuses it at run time).
|
|
104
|
+
*/
|
|
105
|
+
export async function assertSubagents(t, a) {
|
|
106
|
+
const sub = a.definition.subagents;
|
|
107
|
+
if (sub === undefined)
|
|
108
|
+
return;
|
|
109
|
+
const details = [];
|
|
110
|
+
const add = (reason, path, value) => {
|
|
111
|
+
details.push({ reason, path, ...(value !== undefined && { value }) });
|
|
112
|
+
};
|
|
113
|
+
const types = new Set(sub.general === false ? [] : ["general-purpose"]);
|
|
114
|
+
const claim = (type, path) => {
|
|
115
|
+
if (types.has(type))
|
|
116
|
+
add("duplicate", path, type);
|
|
117
|
+
types.add(type);
|
|
118
|
+
};
|
|
119
|
+
const name = (value, path) => {
|
|
120
|
+
if (!SUBAGENT_NAME_RE.test(value) || value === "general-purpose") {
|
|
121
|
+
add("invalid_name", path, value);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
claim(value, path);
|
|
125
|
+
};
|
|
126
|
+
const templates = sub.templates ?? [];
|
|
127
|
+
const keptTemplates = new Set(a.kept?.templates ?? []);
|
|
128
|
+
const ids = [...new Set(templates)];
|
|
129
|
+
// every listed template, deleted ones included (a kept one may have been uninstalled since)
|
|
130
|
+
const rows = new Map(ids.length === 0
|
|
131
|
+
? []
|
|
132
|
+
: (await t
|
|
133
|
+
.selectFrom("agent_templates")
|
|
134
|
+
.select(["id", "name", "plugin_name", "deleted_at"])
|
|
135
|
+
.where("org_id", "=", a.orgId)
|
|
136
|
+
.where("id", "in", ids)
|
|
137
|
+
.execute()).map((r) => [r.id, r]));
|
|
138
|
+
templates.forEach((id, i) => {
|
|
139
|
+
const path = `subagents.templates.${i}`;
|
|
140
|
+
const row = rows.get(id);
|
|
141
|
+
if (!keptTemplates.has(id) && (row === undefined || row.deleted_at !== null)) {
|
|
142
|
+
add("not_found", path, id);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
// claimed by its roster type (`<plugin>:<name>`, roster.ts templateType), so nothing shadows it
|
|
146
|
+
claim(row === undefined ? `template:${id}` : `${row.plugin_name}:${row.name}`, path);
|
|
147
|
+
});
|
|
148
|
+
const inline = sub.inline ?? [];
|
|
149
|
+
const selected = new Set(a.definition.skills?.orgSkills ?? []);
|
|
150
|
+
const skillIds = [...new Set(inline.flatMap((x) => x.skills ?? []))].filter((id) => !selected.has(id));
|
|
151
|
+
const own = a.agentId === null || skillIds.length === 0
|
|
152
|
+
? new Set()
|
|
153
|
+
: new Set((await t
|
|
154
|
+
.selectFrom("skills")
|
|
155
|
+
.select("id")
|
|
156
|
+
.where("org_id", "=", a.orgId)
|
|
157
|
+
.where("scope", "=", "agent")
|
|
158
|
+
.where("agent_id", "=", a.agentId)
|
|
159
|
+
.where("deleted_at", "is", null)
|
|
160
|
+
.where("id", "in", skillIds)
|
|
161
|
+
.execute()).map((r) => r.id));
|
|
162
|
+
inline.forEach((x, i) => {
|
|
163
|
+
const base = `subagents.inline.${i}`;
|
|
164
|
+
name(x.name, `${base}.name`);
|
|
165
|
+
const tool = (key, path) => {
|
|
166
|
+
if (!a.toolKeys.has(key))
|
|
167
|
+
add("tool_not_selected", path, key);
|
|
168
|
+
};
|
|
169
|
+
for (const [j, key] of (x.tools ?? []).entries())
|
|
170
|
+
tool(key, `${base}.tools.${j}`);
|
|
171
|
+
for (const [j, key] of (x.disallowedTools ?? []).entries()) {
|
|
172
|
+
tool(key, `${base}.disallowedTools.${j}`);
|
|
173
|
+
}
|
|
174
|
+
for (const key of Object.keys(x.toolPolicies ?? {}))
|
|
175
|
+
tool(key, `${base}.toolPolicies.${key}`);
|
|
176
|
+
(x.skills ?? []).forEach((id, j) => {
|
|
177
|
+
if (!selected.has(id) && !own.has(id))
|
|
178
|
+
add("skill_not_selected", `${base}.skills.${j}`, id);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
const keptAgents = new Set((a.kept?.agents ?? []).map((x) => x.agentId));
|
|
182
|
+
for (const [i, x] of (sub.agents ?? []).entries()) {
|
|
183
|
+
const base = `subagents.agents.${i}`;
|
|
184
|
+
name(x.as, `${base}.as`);
|
|
185
|
+
if (x.agentId === a.agentId) {
|
|
186
|
+
add("self", `${base}.agentId`, x.agentId);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (keptAgents.has(x.agentId))
|
|
190
|
+
continue;
|
|
191
|
+
const row = await t
|
|
192
|
+
.selectFrom("agents")
|
|
193
|
+
.innerJoin("agent_versions", (j) => j
|
|
194
|
+
.onRef("agent_versions.agent_id", "=", "agents.id")
|
|
195
|
+
.onRef("agent_versions.version", "=", "agents.current_version"))
|
|
196
|
+
.select("agent_versions.definition")
|
|
197
|
+
.where("agents.id", "=", x.agentId)
|
|
198
|
+
.where("agents.org_id", "=", a.orgId)
|
|
199
|
+
.where("agents.deleted_at", "is", null)
|
|
200
|
+
.executeTakeFirst();
|
|
201
|
+
const dispatchable = row?.definition?.subagents?.dispatchable === true;
|
|
202
|
+
if (!dispatchable || !(await isAgentVisibleTo(t, x.agentId, a.actorAccountId))) {
|
|
203
|
+
add("agent_not_available", `${base}.agentId`, x.agentId);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const count = (sub.general === false ? 0 : 1) + templates.length + inline.length + (sub.agents?.length ?? 0);
|
|
207
|
+
if (count > SUBAGENT_ROSTER_MAX)
|
|
208
|
+
add("too_many", "subagents");
|
|
209
|
+
if (details.length > 0) {
|
|
210
|
+
throw new ApiError("agent.subagents_invalid", `Invalid sub-agents: ${details.map((d) => `${d.path} (${d.reason})`).join(", ")}`, details);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Agent 定义、版本与可见范围。写操作都在组织行锁内实时复核发起人角色(spec 3.6 R5);
|
|
215
|
+
* 服务只接收发起人账号 id。不可见的 agent 一律 `agent.not_found`。
|
|
216
|
+
*/
|
|
217
|
+
export class AgentsService {
|
|
218
|
+
db;
|
|
219
|
+
registry;
|
|
220
|
+
tools;
|
|
221
|
+
skillChanges;
|
|
222
|
+
sandboxes;
|
|
223
|
+
log;
|
|
224
|
+
constructor(db, registry, tools,
|
|
225
|
+
/** 删除 agent 时取消它待处理的技能改动(T15)。 */
|
|
226
|
+
skillChanges,
|
|
227
|
+
/**
|
|
228
|
+
* 删除 agent 后销毁它各对话的沙箱(内核投递一次性维护任务 `sandbox-destroy-for`,worker 在锁内逐个销毁,
|
|
229
|
+
* 幂等)。不给时留给不活跃销毁扫描。
|
|
230
|
+
*/
|
|
231
|
+
sandboxes, log) {
|
|
232
|
+
this.db = db;
|
|
233
|
+
this.registry = registry;
|
|
234
|
+
this.tools = tools;
|
|
235
|
+
this.skillChanges = skillChanges;
|
|
236
|
+
this.sandboxes = sandboxes;
|
|
237
|
+
this.log = log;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* 校验工具引用与工具策略:引用的内置 / 插件 / 连接器工具必须存在(见 `ToolRegistry.checkRefs`);
|
|
241
|
+
* `toolPolicies` 的键必须是这些引用产生的工具 key(取值只能收紧,已由 schema 保证)。
|
|
242
|
+
* 不合法 → `validation_failed`,path 为 `tools.<i>` / `toolPolicies.<key>`。
|
|
243
|
+
*/
|
|
244
|
+
async checkTools(orgId, def) {
|
|
245
|
+
const { keys, issues } = await this.tools.checkRefs(orgId, def.tools);
|
|
246
|
+
const details = [...issues];
|
|
247
|
+
for (const key of Object.keys(def.toolPolicies)) {
|
|
248
|
+
if (!keys.has(key)) {
|
|
249
|
+
details.push({
|
|
250
|
+
path: `toolPolicies.${key}`,
|
|
251
|
+
message: `"${key}" is not a tool of this agent`,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (details.length > 0) {
|
|
256
|
+
throw new ApiError("validation_failed", "Invalid tool references", details);
|
|
257
|
+
}
|
|
258
|
+
return keys;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 校验主模型与兜底都可解析,并用各自模型的参数声明严格校验 `modelParams` / `fallbackParams`
|
|
262
|
+
* (没有兜底时 `fallbackParams` 必须为空);不合法 → `validation_failed`,details 逐键点名。返回提示。
|
|
263
|
+
*/
|
|
264
|
+
async checkModels(def) {
|
|
265
|
+
const main = await this.registry.resolve(def.model);
|
|
266
|
+
const fallback = def.fallback === null ? null : await this.registry.resolve(def.fallback);
|
|
267
|
+
const details = [
|
|
268
|
+
...paramIssues("modelParams", main.info.parameters, def.modelParams),
|
|
269
|
+
...paramIssues("fallbackParams", fallback?.info.parameters ?? [], def.fallbackParams),
|
|
270
|
+
];
|
|
271
|
+
if (details.length > 0) {
|
|
272
|
+
throw new ApiError("validation_failed", "Invalid model parameters", details);
|
|
273
|
+
}
|
|
274
|
+
if (def.fallback === null)
|
|
275
|
+
return [];
|
|
276
|
+
return def.fallback.provider === def.model.provider ? ["fallback_same_provider"] : [];
|
|
277
|
+
}
|
|
278
|
+
/** 名单里的账号必须都是该组织当前成员,否则 `member.not_found`;返回去重后的 id。 */
|
|
279
|
+
async checkMembers(t, orgId, ids) {
|
|
280
|
+
const unique = [...new Set(ids)];
|
|
281
|
+
if (unique.length === 0)
|
|
282
|
+
return unique;
|
|
283
|
+
const found = await t
|
|
284
|
+
.selectFrom("memberships")
|
|
285
|
+
.select("account_id")
|
|
286
|
+
.where("org_id", "=", orgId)
|
|
287
|
+
.where("account_id", "in", unique)
|
|
288
|
+
.execute();
|
|
289
|
+
if (found.length !== unique.length) {
|
|
290
|
+
throw new ApiError("member.not_found", "Agent member is not a member of the organization");
|
|
291
|
+
}
|
|
292
|
+
return unique;
|
|
293
|
+
}
|
|
294
|
+
async setMembers(t, agentId, ids) {
|
|
295
|
+
await t.deleteFrom("agent_members").where("agent_id", "=", agentId).execute();
|
|
296
|
+
if (ids.length > 0) {
|
|
297
|
+
await t
|
|
298
|
+
.insertInto("agent_members")
|
|
299
|
+
.values(ids.map((account_id) => ({ agent_id: agentId, account_id })))
|
|
300
|
+
.execute();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async view(db, agentId) {
|
|
304
|
+
const views = await this.views(db, [agentId]);
|
|
305
|
+
const v = views[0];
|
|
306
|
+
if (!v)
|
|
307
|
+
throw notFound();
|
|
308
|
+
return v;
|
|
309
|
+
}
|
|
310
|
+
/** 按给定 id 顺序组装视图(调用方已完成可见性判定)。 */
|
|
311
|
+
async views(db, ids) {
|
|
312
|
+
if (ids.length === 0)
|
|
313
|
+
return [];
|
|
314
|
+
const rows = await db
|
|
315
|
+
.selectFrom("agents")
|
|
316
|
+
.innerJoin("agent_versions", (j) => j
|
|
317
|
+
.onRef("agent_versions.agent_id", "=", "agents.id")
|
|
318
|
+
.onRef("agent_versions.version", "=", "agents.current_version"))
|
|
319
|
+
.select([
|
|
320
|
+
"agents.id",
|
|
321
|
+
"agents.org_id",
|
|
322
|
+
"agents.owner_account_id",
|
|
323
|
+
"agents.visibility",
|
|
324
|
+
"agents.current_version",
|
|
325
|
+
"agents.needs_reassignment",
|
|
326
|
+
"agents.created_at",
|
|
327
|
+
"agents.updated_at",
|
|
328
|
+
"agent_versions.definition",
|
|
329
|
+
])
|
|
330
|
+
.where("agents.id", "in", ids)
|
|
331
|
+
.execute();
|
|
332
|
+
const memberRows = await db
|
|
333
|
+
.selectFrom("agent_members")
|
|
334
|
+
.select(["agent_id", "account_id"])
|
|
335
|
+
.where("agent_id", "in", ids)
|
|
336
|
+
.orderBy("account_id")
|
|
337
|
+
.execute();
|
|
338
|
+
const byId = new Map(rows.map((r) => [r.id, r]));
|
|
339
|
+
return ids.flatMap((id) => {
|
|
340
|
+
const r = byId.get(id);
|
|
341
|
+
if (!r)
|
|
342
|
+
return [];
|
|
343
|
+
return [
|
|
344
|
+
{
|
|
345
|
+
id: r.id,
|
|
346
|
+
orgId: r.org_id,
|
|
347
|
+
ownerAccountId: r.owner_account_id,
|
|
348
|
+
visibility: r.visibility,
|
|
349
|
+
currentVersion: r.current_version,
|
|
350
|
+
definition: normalizeStoredDefinition(r.definition),
|
|
351
|
+
memberIds: r.visibility === "members"
|
|
352
|
+
? memberRows.filter((m) => m.agent_id === id).map((m) => m.account_id)
|
|
353
|
+
: null,
|
|
354
|
+
needsReassignment: r.needs_reassignment,
|
|
355
|
+
createdAt: r.created_at,
|
|
356
|
+
updatedAt: r.updated_at,
|
|
357
|
+
},
|
|
358
|
+
];
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* 新建 agent(版本 1):在组织行锁内授权 `agents.create`;主模型与兜底都必须可解析
|
|
363
|
+
* (`agent.model_unavailable`);兜底与主模型同一供应商只提示 `fallback_same_provider`。
|
|
364
|
+
*/
|
|
365
|
+
async create(orgId, actorAccountId, input, actorTokenId) {
|
|
366
|
+
// 先解析模型(可能访问供应商),不在持有组织行锁时做
|
|
367
|
+
const warnings = await this.checkModels(input.definition);
|
|
368
|
+
const toolKeys = await this.checkTools(orgId, input.definition);
|
|
369
|
+
if (input.visibility !== "members" && (input.memberIds?.length ?? 0) > 0) {
|
|
370
|
+
throw new ApiError("validation_failed", "memberIds requires visibility 'members'");
|
|
371
|
+
}
|
|
372
|
+
const agent = await this.db.transaction().execute(async (t) => {
|
|
373
|
+
await lockOrgAndAuthorize(t, orgId, actorAccountId, "agents.create");
|
|
374
|
+
await assertOrgSkills(t, orgId, input.definition.skills?.orgSkills ?? []);
|
|
375
|
+
await assertPinnedSkills(t, orgId, null, input.definition.skills);
|
|
376
|
+
await assertSubagents(t, {
|
|
377
|
+
orgId,
|
|
378
|
+
agentId: null,
|
|
379
|
+
actorAccountId,
|
|
380
|
+
definition: input.definition,
|
|
381
|
+
toolKeys,
|
|
382
|
+
});
|
|
383
|
+
const memberIds = input.visibility === "members"
|
|
384
|
+
? await this.checkMembers(t, orgId, input.memberIds ?? [])
|
|
385
|
+
: [];
|
|
386
|
+
const id = randomUUID();
|
|
387
|
+
await t
|
|
388
|
+
.insertInto("agents")
|
|
389
|
+
.values({
|
|
390
|
+
id,
|
|
391
|
+
org_id: orgId,
|
|
392
|
+
owner_account_id: actorAccountId,
|
|
393
|
+
visibility: input.visibility,
|
|
394
|
+
current_version: 1,
|
|
395
|
+
})
|
|
396
|
+
.execute();
|
|
397
|
+
await t
|
|
398
|
+
.insertInto("agent_versions")
|
|
399
|
+
.values({
|
|
400
|
+
agent_id: id,
|
|
401
|
+
version: 1,
|
|
402
|
+
definition: JSON.stringify(input.definition),
|
|
403
|
+
created_by: actorAccountId,
|
|
404
|
+
})
|
|
405
|
+
.execute();
|
|
406
|
+
await this.setMembers(t, id, memberIds);
|
|
407
|
+
await audit(t, {
|
|
408
|
+
actorAccountId,
|
|
409
|
+
...(actorTokenId !== undefined && { actorTokenId }),
|
|
410
|
+
orgId,
|
|
411
|
+
action: "agent.created",
|
|
412
|
+
target: id,
|
|
413
|
+
detail: { version: 1, visibility: input.visibility },
|
|
414
|
+
});
|
|
415
|
+
return this.view(t, id);
|
|
416
|
+
});
|
|
417
|
+
return { agent, warnings };
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* 锁组织行并复核发起人,再锁 agent 行:发起人须仍具备 `agents.create`(member 以上),且是负责人或组织管理员以上;
|
|
421
|
+
* 对发起人不可见的 agent(非管理员)→ `agent.not_found`,可见但无权 → `forbidden`。
|
|
422
|
+
*/
|
|
423
|
+
async lockForWrite(t, orgId, agentId, actorAccountId) {
|
|
424
|
+
const { role } = await lockOrgAndAuthorize(t, orgId, actorAccountId, "agents.create");
|
|
425
|
+
// NO KEY UPDATE: a soft delete then appends revert snapshots to conversations; FOR UPDATE would
|
|
426
|
+
// block the agent foreign key check of an append already holding one of them (40P01)
|
|
427
|
+
const agent = await t
|
|
428
|
+
.selectFrom("agents")
|
|
429
|
+
.select(["owner_account_id", "visibility", "current_version"])
|
|
430
|
+
.where("id", "=", agentId)
|
|
431
|
+
.where("org_id", "=", orgId)
|
|
432
|
+
.where("deleted_at", "is", null)
|
|
433
|
+
.forNoKeyUpdate()
|
|
434
|
+
.executeTakeFirst();
|
|
435
|
+
if (!agent)
|
|
436
|
+
throw notFound();
|
|
437
|
+
if (agent.owner_account_id === actorAccountId || roleAtLeast(role, "admin"))
|
|
438
|
+
return agent;
|
|
439
|
+
const visible = await t
|
|
440
|
+
.selectFrom("agents")
|
|
441
|
+
.select("id")
|
|
442
|
+
.where("id", "=", agentId)
|
|
443
|
+
.where(visibleTo(actorAccountId))
|
|
444
|
+
.executeTakeFirst();
|
|
445
|
+
if (!visible)
|
|
446
|
+
throw notFound();
|
|
447
|
+
throw new ApiError("forbidden", "Only the agent owner or an organization admin can modify it");
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* 修改 agent:`definition` 变化时写新版本(`current_version + 1`),只改 visibility / memberIds 不产生新版本。
|
|
451
|
+
* 只有负责人或组织管理员以上可改,在组织行锁内复核。
|
|
452
|
+
*/
|
|
453
|
+
async update(orgId, agentId, actorAccountId, patch, actorTokenId) {
|
|
454
|
+
const warnings = patch.definition ? await this.checkModels(patch.definition) : [];
|
|
455
|
+
const toolKeys = patch.definition ? await this.checkTools(orgId, patch.definition) : null;
|
|
456
|
+
const agent = await this.db.transaction().execute(async (t) => {
|
|
457
|
+
const before = await this.lockForWrite(t, orgId, agentId, actorAccountId);
|
|
458
|
+
if (patch.expectedVersion !== undefined && patch.expectedVersion !== before.current_version) {
|
|
459
|
+
throw new ApiError("agent.version_conflict", `The agent was changed by someone else (now version ${before.current_version}, expected ${patch.expectedVersion}); reload it and apply your changes again`);
|
|
460
|
+
}
|
|
461
|
+
const visibility = patch.visibility ?? before.visibility;
|
|
462
|
+
if (visibility !== "members" && (patch.memberIds?.length ?? 0) > 0) {
|
|
463
|
+
throw new ApiError("validation_failed", "memberIds requires visibility 'members'");
|
|
464
|
+
}
|
|
465
|
+
let toVersion = before.current_version;
|
|
466
|
+
if (patch.definition) {
|
|
467
|
+
const current = await t
|
|
468
|
+
.selectFrom("agent_versions")
|
|
469
|
+
.select("definition")
|
|
470
|
+
.where("agent_id", "=", agentId)
|
|
471
|
+
.where("version", "=", before.current_version)
|
|
472
|
+
.executeTakeFirstOrThrow();
|
|
473
|
+
// 当前版本已有的技能引用保存时合法,不再核对(被删的技能不挡住其他改动);新加的照常核对
|
|
474
|
+
const kept = new Set(current.definition.skills?.orgSkills ?? []);
|
|
475
|
+
await assertOrgSkills(t, orgId, patch.definition.skills?.orgSkills ?? [], kept);
|
|
476
|
+
await assertPinnedSkills(t, orgId, agentId, patch.definition.skills, current.definition.skills?.pinned ?? []);
|
|
477
|
+
await assertSubagents(t, {
|
|
478
|
+
orgId,
|
|
479
|
+
agentId,
|
|
480
|
+
actorAccountId,
|
|
481
|
+
definition: patch.definition,
|
|
482
|
+
toolKeys: toolKeys ?? new Set(),
|
|
483
|
+
kept: current.definition.subagents,
|
|
484
|
+
});
|
|
485
|
+
if (canonical(current.definition) !== canonical(patch.definition)) {
|
|
486
|
+
toVersion = before.current_version + 1;
|
|
487
|
+
await t
|
|
488
|
+
.insertInto("agent_versions")
|
|
489
|
+
.values({
|
|
490
|
+
agent_id: agentId,
|
|
491
|
+
version: toVersion,
|
|
492
|
+
definition: JSON.stringify(patch.definition),
|
|
493
|
+
created_by: actorAccountId,
|
|
494
|
+
})
|
|
495
|
+
.execute();
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (visibility !== "members") {
|
|
499
|
+
await this.setMembers(t, agentId, []);
|
|
500
|
+
}
|
|
501
|
+
else if (patch.memberIds !== undefined) {
|
|
502
|
+
await this.setMembers(t, agentId, await this.checkMembers(t, orgId, patch.memberIds));
|
|
503
|
+
}
|
|
504
|
+
await t
|
|
505
|
+
.updateTable("agents")
|
|
506
|
+
.set({ visibility, current_version: toVersion, updated_at: sql `now()` })
|
|
507
|
+
.where("id", "=", agentId)
|
|
508
|
+
.execute();
|
|
509
|
+
await audit(t, {
|
|
510
|
+
actorAccountId,
|
|
511
|
+
...(actorTokenId !== undefined && { actorTokenId }),
|
|
512
|
+
orgId,
|
|
513
|
+
action: "agent.updated",
|
|
514
|
+
target: agentId,
|
|
515
|
+
detail: {
|
|
516
|
+
fromVersion: before.current_version,
|
|
517
|
+
toVersion,
|
|
518
|
+
...(visibility !== before.visibility && {
|
|
519
|
+
visibility: { from: before.visibility, to: visibility },
|
|
520
|
+
}),
|
|
521
|
+
},
|
|
522
|
+
});
|
|
523
|
+
return this.view(t, agentId);
|
|
524
|
+
});
|
|
525
|
+
return { agent, warnings };
|
|
526
|
+
}
|
|
527
|
+
/** 软删除:权限同 update。待处理的技能改动与该 agent 评测集在执行的评测运行一并取消,提交后删除草稿(T15)。 */
|
|
528
|
+
async softDelete(orgId, agentId, actorAccountId, actorTokenId) {
|
|
529
|
+
const cancelled = await this.db.transaction().execute(async (t) => {
|
|
530
|
+
await this.lockForWrite(t, orgId, agentId, actorAccountId);
|
|
531
|
+
await t
|
|
532
|
+
.updateTable("agents")
|
|
533
|
+
.set({ deleted_at: sql `now()`, updated_at: sql `now()` })
|
|
534
|
+
.where("id", "=", agentId)
|
|
535
|
+
.execute();
|
|
536
|
+
await audit(t, {
|
|
537
|
+
actorAccountId,
|
|
538
|
+
...(actorTokenId !== undefined && { actorTokenId }),
|
|
539
|
+
orgId,
|
|
540
|
+
action: "agent.deleted",
|
|
541
|
+
target: agentId,
|
|
542
|
+
});
|
|
543
|
+
// 评测集的 agent 删除后其评测运行不再花钱:显式取消(审计带原因)
|
|
544
|
+
await cancelEvalRuns(t, { orgId, agentId }, { accountId: actorAccountId, ...(actorTokenId !== undefined && { tokenId: actorTokenId }) }, "agent_deleted");
|
|
545
|
+
return this.skillChanges.cancelPendingForAgent(t, orgId, agentId, actorAccountId, "agent_deleted", actorTokenId);
|
|
546
|
+
});
|
|
547
|
+
await this.skillChanges.finishCancel(orgId, cancelled);
|
|
548
|
+
await this.destroySandboxes(orgId, agentId);
|
|
549
|
+
}
|
|
550
|
+
/** 提交后尽力而为:投递销毁这个 agent 各对话还没销毁的沙箱(失败只记日志,留给不活跃销毁扫描)。 */
|
|
551
|
+
async destroySandboxes(orgId, agentId) {
|
|
552
|
+
if (!this.sandboxes)
|
|
553
|
+
return;
|
|
554
|
+
try {
|
|
555
|
+
const rows = await this.db
|
|
556
|
+
.selectFrom("sandboxes as s")
|
|
557
|
+
.innerJoin("conversations as c", "c.id", "s.conversation_id")
|
|
558
|
+
.select("s.conversation_id")
|
|
559
|
+
.where("c.org_id", "=", orgId)
|
|
560
|
+
.where("c.agent_id", "=", agentId)
|
|
561
|
+
.where("s.status", "<>", "destroyed")
|
|
562
|
+
.execute();
|
|
563
|
+
const ids = rows.map((r) => r.conversation_id);
|
|
564
|
+
for (let i = 0; i < ids.length; i += SANDBOX_DESTROY_BATCH) {
|
|
565
|
+
await this.sandboxes.destroy(ids.slice(i, i + SANDBOX_DESTROY_BATCH));
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
catch (err) {
|
|
569
|
+
this.log?.warn("cannot schedule sandbox destruction for a deleted agent", { agentId, err });
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* 重新指派负责人(`agents.manage`,admin):在组织行锁内授权;agent 不存在 / 已删除 → `agent.not_found`;
|
|
574
|
+
* 新负责人不是该组织成员 → `member.not_found`;是匿名访客 → `member.anonymous_guest`。
|
|
575
|
+
* 清除 `needs_reassignment`,审计 `agent.owner_changed`。
|
|
576
|
+
*/
|
|
577
|
+
async reassignOwner(orgId, agentId, newOwnerId, actorAccountId, actorTokenId) {
|
|
578
|
+
return this.db.transaction().execute(async (t) => {
|
|
579
|
+
await lockOrgAndAuthorize(t, orgId, actorAccountId, "agents.manage");
|
|
580
|
+
const agent = await t
|
|
581
|
+
.selectFrom("agents")
|
|
582
|
+
.select(["owner_account_id", "needs_reassignment"])
|
|
583
|
+
.where("id", "=", agentId)
|
|
584
|
+
.where("org_id", "=", orgId)
|
|
585
|
+
.where("deleted_at", "is", null)
|
|
586
|
+
.forUpdate()
|
|
587
|
+
.executeTakeFirst();
|
|
588
|
+
if (!agent)
|
|
589
|
+
throw notFound();
|
|
590
|
+
const member = await t
|
|
591
|
+
.selectFrom("memberships")
|
|
592
|
+
.select("role")
|
|
593
|
+
.where("org_id", "=", orgId)
|
|
594
|
+
.where("account_id", "=", newOwnerId)
|
|
595
|
+
.executeTakeFirst();
|
|
596
|
+
if (!member)
|
|
597
|
+
throw new ApiError("member.not_found", "Member not found");
|
|
598
|
+
await rejectAnonymousGuest(t, newOwnerId);
|
|
599
|
+
await t
|
|
600
|
+
.updateTable("agents")
|
|
601
|
+
.set({ owner_account_id: newOwnerId, needs_reassignment: false, updated_at: sql `now()` })
|
|
602
|
+
.where("id", "=", agentId)
|
|
603
|
+
.execute();
|
|
604
|
+
await audit(t, {
|
|
605
|
+
actorAccountId,
|
|
606
|
+
...(actorTokenId !== undefined && { actorTokenId }),
|
|
607
|
+
orgId,
|
|
608
|
+
action: "agent.owner_changed",
|
|
609
|
+
target: agentId,
|
|
610
|
+
detail: {
|
|
611
|
+
from: agent.owner_account_id,
|
|
612
|
+
to: newOwnerId,
|
|
613
|
+
neededReassignment: agent.needs_reassignment,
|
|
614
|
+
},
|
|
615
|
+
});
|
|
616
|
+
return this.view(t, agentId);
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
/** 取对 viewer 可见的 agent;`viewerAccountId = null` 表示该组织的服务令牌。不可见 → `agent.not_found`。 */
|
|
620
|
+
async get(orgId, agentId, viewerAccountId) {
|
|
621
|
+
const row = await this.db
|
|
622
|
+
.selectFrom("agents")
|
|
623
|
+
.select("id")
|
|
624
|
+
.where("id", "=", agentId)
|
|
625
|
+
.where("org_id", "=", orgId)
|
|
626
|
+
.where("deleted_at", "is", null)
|
|
627
|
+
.where(visibleTo(viewerAccountId))
|
|
628
|
+
.executeTakeFirst();
|
|
629
|
+
if (!row)
|
|
630
|
+
throw notFound();
|
|
631
|
+
return this.view(this.db, agentId);
|
|
632
|
+
}
|
|
633
|
+
/** 组织内对 viewer 可见的全部 agent(按创建时间)。 */
|
|
634
|
+
async listVisible(orgId, viewerAccountId) {
|
|
635
|
+
const rows = await this.db
|
|
636
|
+
.selectFrom("agents")
|
|
637
|
+
.select("id")
|
|
638
|
+
.where("org_id", "=", orgId)
|
|
639
|
+
.where("deleted_at", "is", null)
|
|
640
|
+
.where(visibleTo(viewerAccountId))
|
|
641
|
+
.orderBy("created_at")
|
|
642
|
+
.orderBy("id")
|
|
643
|
+
.execute();
|
|
644
|
+
return this.views(this.db, rows.map((r) => r.id));
|
|
645
|
+
}
|
|
646
|
+
/** 读某个版本(不做可见性判定,调用方负责);不存在 → `agent.not_found`。 */
|
|
647
|
+
async getVersion(agentId, version) {
|
|
648
|
+
const r = await this.db
|
|
649
|
+
.selectFrom("agent_versions")
|
|
650
|
+
.select(["version", "definition", "created_by", "created_at"])
|
|
651
|
+
.where("agent_id", "=", agentId)
|
|
652
|
+
.where("version", "=", version)
|
|
653
|
+
.executeTakeFirst();
|
|
654
|
+
if (!r)
|
|
655
|
+
throw notFound();
|
|
656
|
+
return {
|
|
657
|
+
version: r.version,
|
|
658
|
+
definition: normalizeStoredDefinition(r.definition),
|
|
659
|
+
createdBy: r.created_by,
|
|
660
|
+
createdAt: r.created_at,
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
/** 版本列表,新的在前(不做可见性判定,调用方负责)。 */
|
|
664
|
+
async listVersions(agentId) {
|
|
665
|
+
const rows = await this.db
|
|
666
|
+
.selectFrom("agent_versions")
|
|
667
|
+
.select(["version", "created_by", "created_at"])
|
|
668
|
+
.where("agent_id", "=", agentId)
|
|
669
|
+
.orderBy("version", "desc")
|
|
670
|
+
.execute();
|
|
671
|
+
return rows.map((r) => ({
|
|
672
|
+
version: r.version,
|
|
673
|
+
createdBy: r.created_by,
|
|
674
|
+
createdAt: r.created_at,
|
|
675
|
+
}));
|
|
676
|
+
}
|
|
677
|
+
}
|