@capybara-hq/kernel 0.1.0-rc.41 → 0.1.0-rc.43
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/bootstrap.json +3 -3
- package/dist/admin/admin.schemas.d.ts +6 -0
- package/dist/admin/org-network.service.d.ts +3 -1
- package/dist/admin/org-network.service.js +10 -2
- package/dist/app.d.ts +4 -0
- package/dist/app.js +2 -0
- package/dist/background/finish.d.ts +9 -1
- package/dist/background/finish.js +57 -4
- package/dist/background/launch.d.ts +23 -0
- package/dist/background/launch.js +55 -1
- package/dist/background/managed-output.d.ts +110 -0
- package/dist/background/managed-output.js +219 -0
- package/dist/background/managed.d.ts +52 -0
- package/dist/background/managed.js +128 -0
- package/dist/background/poll.job.d.ts +35 -1
- package/dist/background/poll.job.js +174 -9
- package/dist/background/render.js +1 -0
- package/dist/background/retire-legacy-tasks.job.d.ts +57 -0
- package/dist/background/retire-legacy-tasks.job.js +149 -0
- package/dist/background/session.d.ts +15 -5
- package/dist/background/session.js +22 -6
- package/dist/background/stop.d.ts +35 -5
- package/dist/background/stop.js +94 -8
- package/dist/background/store.d.ts +110 -3
- package/dist/background/store.js +205 -3
- package/dist/background/tasks.service.d.ts +18 -1
- package/dist/background/tasks.service.js +99 -9
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/cards.js +1 -1
- package/dist/channels/inbound.js +2 -0
- package/dist/channels/phrases.d.ts +4 -0
- package/dist/channels/phrases.js +4 -0
- package/dist/config/load.d.ts +18 -0
- package/dist/config/load.js +39 -1
- package/dist/config/permission-migration.d.ts +26 -0
- package/dist/config/permission-migration.js +64 -0
- package/dist/config/schema.d.ts +4 -1
- package/dist/config/schema.js +41 -10
- package/dist/conversations/conversations.controller.d.ts +1 -1
- package/dist/conversations/conversations.controller.js +1 -1
- package/dist/conversations/conversations.module.js +12 -0
- package/dist/conversations/conversations.service.d.ts +11 -2
- package/dist/conversations/conversations.service.js +97 -27
- package/dist/conversations/event-log.d.ts +1 -1
- package/dist/conversations/event-log.js +10 -2
- package/dist/conversations/events.d.ts +307 -4
- package/dist/conversations/events.js +286 -32
- package/dist/conversations/permission-profile-store.d.ts +50 -0
- package/dist/conversations/permission-profile-store.js +67 -0
- package/dist/conversations/permission-profile-store.module.d.ts +2 -0
- package/dist/conversations/permission-profile-store.module.js +19 -0
- package/dist/conversations/permission-profiles.controller.d.ts +84 -0
- package/dist/conversations/permission-profiles.controller.js +105 -0
- package/dist/conversations/permission-profiles.schemas.d.ts +31 -0
- package/dist/conversations/permission-profiles.schemas.js +23 -0
- package/dist/conversations/permission-profiles.service.d.ts +56 -0
- package/dist/conversations/permission-profiles.service.js +179 -0
- package/dist/ctx/create-ctx.d.ts +12 -5
- package/dist/ctx/create-ctx.js +21 -5
- package/dist/db/kernel-schema.d.ts +248 -3
- package/dist/devices/access.js +1 -1
- package/dist/devices/access.module.d.ts +2 -0
- package/dist/devices/access.module.js +19 -0
- package/dist/devices/conversation-devices.d.ts +8 -29
- package/dist/devices/conversation-devices.js +37 -73
- package/dist/devices/device-projects.service.d.ts +26 -28
- package/dist/devices/device-projects.service.js +123 -127
- package/dist/devices/devices.controller.js +3 -1
- package/dist/devices/devices.module.js +11 -9
- package/dist/devices/devices.service.d.ts +116 -8
- package/dist/devices/devices.service.js +252 -20
- package/dist/devices/dto.d.ts +16 -0
- package/dist/devices/dto.js +34 -6
- package/dist/devices/errors.d.ts +8 -0
- package/dist/devices/errors.js +10 -0
- package/dist/devices/project-rules.d.ts +44 -0
- package/dist/devices/project-rules.js +27 -0
- package/dist/devices/working-root.d.ts +6 -0
- package/dist/devices/working-root.js +12 -1
- package/dist/errors.d.ts +31 -0
- package/dist/errors.js +31 -0
- package/dist/evals/reconstruct.d.ts +19 -2
- package/dist/evals/reconstruct.js +40 -2
- package/dist/evals/recorded-permission-context.d.ts +77 -0
- package/dist/evals/recorded-permission-context.js +151 -0
- package/dist/evals/recorded-tools.d.ts +10 -1
- package/dist/evals/recorded-tools.js +27 -3
- package/dist/evals/run-replay.d.ts +8 -2
- package/dist/evals/run-replay.js +18 -2
- package/dist/evals/trajectory.d.ts +1 -1
- package/dist/evals/worker.d.ts +10 -1
- package/dist/evals/worker.js +23 -14
- package/dist/extensions/host.d.ts +4 -1
- package/dist/extensions/host.js +10 -6
- package/dist/http/api.module.js +2 -0
- package/dist/identity/accounts.d.ts +14 -1
- package/dist/identity/accounts.js +61 -5
- package/dist/identity/identity.module.js +2 -0
- package/dist/identity/settings.d.ts +28 -12
- package/dist/identity/settings.js +89 -33
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/infra/cli/kernel-cli.module.d.ts +2 -2
- package/dist/infra/cli/kernel-cli.module.js +5 -2
- package/dist/infra/cli/permission-cutover.d.ts +59 -0
- package/dist/infra/cli/permission-cutover.js +107 -0
- package/dist/infra/config/kernel-config.service.d.ts +2 -2
- package/dist/infra/config/kernel-config.service.js +35 -11
- package/dist/infra/database/startup-activation-transaction.d.ts +36 -0
- package/dist/infra/database/startup-activation-transaction.js +242 -0
- package/dist/infra/database/transaction-host.d.ts +47 -0
- package/dist/infra/database/transaction-host.js +119 -0
- package/dist/infra/database/xact-lock.d.ts +1 -0
- package/dist/infra/database/xact-lock.js +6 -0
- package/dist/infra/outbox/registry.d.ts +39 -0
- package/dist/infra/outbox/registry.js +18 -0
- package/dist/kernel.module.js +2 -0
- package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
- package/dist/lifecycle/kernel-lifecycle.js +55 -28
- package/dist/local-devices/device-auth.js +1 -1
- package/dist/local-devices/device-policy.service.d.ts +35 -5
- package/dist/local-devices/device-policy.service.js +188 -13
- package/dist/local-devices/instance-bindings.d.ts +82 -0
- package/dist/local-devices/instance-bindings.js +304 -0
- package/dist/local-devices/keys.d.ts +5 -0
- package/dist/local-devices/keys.js +5 -0
- package/dist/local-devices/local-device-gateway.d.ts +19 -4
- package/dist/local-devices/local-device-gateway.js +185 -17
- package/dist/local-devices/local-device-provider.d.ts +42 -7
- package/dist/local-devices/local-device-provider.js +185 -10
- package/dist/local-devices/local-devices.module.d.ts +17 -2
- package/dist/local-devices/local-devices.module.js +49 -8
- package/dist/local-devices/machines.d.ts +35 -12
- package/dist/local-devices/machines.js +203 -8
- package/dist/local-devices/network-grants.js +3 -3
- package/dist/local-devices/network-grants.module.d.ts +2 -0
- package/dist/local-devices/network-grants.module.js +14 -0
- package/dist/local-devices/owner-report.d.ts +79 -0
- package/dist/local-devices/owner-report.js +155 -0
- package/dist/local-devices/owner-reports.d.ts +48 -0
- package/dist/local-devices/owner-reports.js +190 -0
- package/dist/local-devices/policy-summary.d.ts +24 -1
- package/dist/local-devices/policy-summary.js +48 -0
- package/dist/local-devices/relay.d.ts +82 -6
- package/dist/local-devices/relay.js +187 -16
- package/dist/local-devices/targets.d.ts +16 -1
- package/dist/local-devices/targets.js +35 -2
- package/dist/models/image-usable.d.ts +22 -0
- package/dist/models/image-usable.js +29 -0
- package/dist/models/images.d.ts +125 -0
- package/dist/models/images.js +440 -0
- package/dist/models/models.module.d.ts +3 -2
- package/dist/models/models.module.js +16 -5
- package/dist/models/org-models.schemas.js +1 -0
- package/dist/models/purposes.controller.d.ts +7 -0
- package/dist/models/purposes.controller.js +9 -0
- package/dist/models/purposes.d.ts +28 -3
- package/dist/models/purposes.js +40 -13
- package/dist/orgs/cloud-network-setting.d.ts +33 -0
- package/dist/orgs/cloud-network-setting.js +47 -0
- package/dist/orgs/offboarding.d.ts +3 -1
- package/dist/orgs/offboarding.js +8 -2
- package/dist/orgs/org-lock.d.ts +10 -4
- package/dist/orgs/org-lock.js +10 -4
- package/dist/orgs/orgs.service.d.ts +3 -1
- package/dist/orgs/orgs.service.js +8 -2
- package/dist/orgs/settings.controller.d.ts +4 -1
- package/dist/orgs/settings.controller.js +3 -3
- package/dist/orgs/settings.service.d.ts +48 -56
- package/dist/orgs/settings.service.js +144 -101
- package/dist/overrides.d.ts +6 -1
- package/dist/overrides.js +2 -0
- package/dist/platform/exclusions.js +20 -0
- package/dist/platform/ops/admin.ops.js +14 -11
- package/dist/platform/ops/agent-skills.ops.js +2 -0
- package/dist/platform/ops/devices.ops.js +10 -2
- package/dist/platform/ops/models.ops.js +10 -0
- package/dist/platform/ops/org-settings.ops.js +6 -6
- package/dist/platform/ops/orgs.ops.js +1 -0
- package/dist/roles/worker.module.js +4 -0
- package/dist/runtime/context/build.d.ts +3 -1
- package/dist/runtime/context/build.js +19 -3
- package/dist/runtime/context/compaction.js +5 -0
- package/dist/runtime/context/device-paths.d.ts +9 -2
- package/dist/runtime/context/device-paths.js +20 -0
- package/dist/runtime/context/permission-profile.d.ts +46 -0
- package/dist/runtime/context/permission-profile.js +194 -0
- package/dist/runtime/hook-calls.d.ts +11 -1
- package/dist/runtime/hook-calls.js +24 -1
- package/dist/runtime/maintenance.d.ts +7 -1
- package/dist/runtime/maintenance.js +43 -15
- package/dist/runtime/model-call.d.ts +32 -0
- package/dist/runtime/model-call.js +67 -0
- package/dist/runtime/permission-context-errors.d.ts +39 -0
- package/dist/runtime/permission-context-errors.js +67 -0
- package/dist/runtime/permission-context.service.d.ts +60 -0
- package/dist/runtime/permission-context.service.js +286 -0
- package/dist/runtime/project/knowledge.d.ts +7 -2
- package/dist/runtime/project/knowledge.js +5 -2
- package/dist/runtime/project/knowledge.service.d.ts +17 -2
- package/dist/runtime/project/knowledge.service.js +76 -12
- package/dist/runtime/providers.d.ts +9 -5
- package/dist/runtime/providers.js +22 -3
- package/dist/runtime/run-execution.d.ts +4 -1
- package/dist/runtime/run-execution.js +14 -5
- package/dist/runtime/run-steps.d.ts +11 -3
- package/dist/runtime/run-steps.js +27 -4
- package/dist/runtime/runtime.module.js +11 -0
- package/dist/runtime/usage-report.d.ts +10 -6
- package/dist/runtime/usage-report.js +31 -11
- package/dist/sandbox/attachments-path.d.ts +15 -0
- package/dist/sandbox/attachments-path.js +15 -0
- package/dist/sandbox/cloud-permission-snapshot.d.ts +159 -0
- package/dist/sandbox/cloud-permission-snapshot.js +407 -0
- package/dist/sandbox/cloud-permission-snapshot.module.d.ts +8 -0
- package/dist/sandbox/cloud-permission-snapshot.module.js +34 -0
- package/dist/sandbox/held-operations.d.ts +39 -0
- package/dist/sandbox/held-operations.js +119 -0
- package/dist/sandbox/jobs.d.ts +14 -3
- package/dist/sandbox/jobs.js +20 -2
- package/dist/sandbox/kernel-read.d.ts +55 -0
- package/dist/sandbox/kernel-read.js +57 -0
- package/dist/sandbox/legacy-retirement.d.ts +336 -0
- package/dist/sandbox/legacy-retirement.js +1063 -0
- package/dist/sandbox/legacy-retirement.module.d.ts +8 -0
- package/dist/sandbox/legacy-retirement.module.js +25 -0
- package/dist/sandbox/managed-device-context.d.ts +94 -0
- package/dist/sandbox/managed-device-context.js +308 -0
- package/dist/sandbox/managed-device-context.module.d.ts +6 -0
- package/dist/sandbox/managed-device-context.module.js +33 -0
- package/dist/sandbox/modes.d.ts +56 -8
- package/dist/sandbox/modes.js +92 -8
- package/dist/sandbox/operation-authority-types.d.ts +8 -0
- package/dist/sandbox/operation-authority-types.js +32 -0
- package/dist/sandbox/operation-authority.d.ts +145 -0
- package/dist/sandbox/operation-authority.js +783 -0
- package/dist/sandbox/operation-authority.module.d.ts +2 -0
- package/dist/sandbox/operation-authority.module.js +29 -0
- package/dist/sandbox/operation-intents.d.ts +206 -0
- package/dist/sandbox/operation-intents.js +1252 -0
- package/dist/sandbox/operation-intents.module.d.ts +2 -0
- package/dist/sandbox/operation-intents.module.js +21 -0
- package/dist/sandbox/operation-payloads.d.ts +33 -0
- package/dist/sandbox/operation-payloads.js +332 -0
- package/dist/sandbox/operation-reconcile.job.d.ts +49 -0
- package/dist/sandbox/operation-reconcile.job.js +247 -0
- package/dist/sandbox/permission-authority-epochs.d.ts +18 -0
- package/dist/sandbox/permission-authority-epochs.js +83 -0
- package/dist/sandbox/permission-authority-lock.d.ts +38 -0
- package/dist/sandbox/permission-authority-lock.js +147 -0
- package/dist/sandbox/permission-authority.module.d.ts +9 -0
- package/dist/sandbox/permission-authority.module.js +55 -0
- package/dist/sandbox/permission-availability.d.ts +18 -0
- package/dist/sandbox/permission-availability.js +19 -0
- package/dist/sandbox/permission-call-grants.d.ts +34 -0
- package/dist/sandbox/permission-call-grants.js +349 -0
- package/dist/sandbox/permission-call-grants.module.d.ts +2 -0
- package/dist/sandbox/permission-call-grants.module.js +21 -0
- package/dist/sandbox/permission-call-types.d.ts +82 -0
- package/dist/sandbox/permission-call-types.js +1 -0
- package/dist/sandbox/permission-ceiling-writes.d.ts +47 -0
- package/dist/sandbox/permission-ceiling-writes.js +223 -0
- package/dist/sandbox/permission-config-publication.d.ts +76 -0
- package/dist/sandbox/permission-config-publication.js +339 -0
- package/dist/sandbox/permission-context-types.d.ts +64 -0
- package/dist/sandbox/permission-context-types.js +24 -0
- package/dist/sandbox/permission-facts.d.ts +206 -0
- package/dist/sandbox/permission-facts.js +627 -0
- package/dist/sandbox/permission-facts.module.d.ts +2 -0
- package/dist/sandbox/permission-facts.module.js +20 -0
- package/dist/sandbox/permission-instance-writes.d.ts +90 -0
- package/dist/sandbox/permission-instance-writes.js +305 -0
- package/dist/sandbox/permission-runtime-reports.d.ts +83 -0
- package/dist/sandbox/permission-runtime-reports.js +354 -0
- package/dist/sandbox/permission-transitions.d.ts +31 -0
- package/dist/sandbox/permission-transitions.js +155 -0
- package/dist/sandbox/platform-full-access.d.ts +128 -0
- package/dist/sandbox/platform-full-access.js +210 -0
- package/dist/sandbox/providers.d.ts +22 -0
- package/dist/sandbox/providers.js +10 -2
- package/dist/sandbox/providers.module.d.ts +2 -0
- package/dist/sandbox/providers.module.js +19 -0
- package/dist/sandbox/request-not-sent.d.ts +13 -0
- package/dist/sandbox/request-not-sent.js +13 -0
- package/dist/sandbox/review/classifier.d.ts +2 -0
- package/dist/sandbox/review/classifier.js +8 -0
- package/dist/sandbox/review/permission-candidate.d.ts +12 -0
- package/dist/sandbox/review/permission-candidate.js +145 -0
- package/dist/sandbox/review/review.service.d.ts +5 -2
- package/dist/sandbox/review/review.service.js +60 -15
- package/dist/sandbox/sandbox-file-reader.d.ts +18 -1
- package/dist/sandbox/sandbox-file-reader.js +51 -7
- package/dist/sandbox/sandbox.module.js +26 -6
- package/dist/sandbox/sandbox.service.d.ts +230 -34
- package/dist/sandbox/sandbox.service.js +1368 -199
- package/dist/sandbox/unreachable.js +17 -3
- package/dist/sandbox/use-context.d.ts +17 -0
- package/dist/sandbox/use-context.js +1 -0
- package/dist/skills/invoke.d.ts +27 -4
- package/dist/skills/invoke.js +72 -7
- package/dist/tools/approvals.controller.d.ts +1 -0
- package/dist/tools/approvals.schemas.d.ts +1 -0
- package/dist/tools/approvals.schemas.js +3 -1
- package/dist/tools/approvals.service.d.ts +23 -10
- package/dist/tools/approvals.service.js +104 -26
- package/dist/tools/builtin-names.d.ts +3 -1
- package/dist/tools/builtin-names.js +5 -1
- package/dist/tools/builtins/artifacts.d.ts +43 -0
- package/dist/tools/builtins/artifacts.js +67 -0
- package/dist/tools/builtins/builtin-tools.d.ts +20 -2
- package/dist/tools/builtins/builtin-tools.js +61 -10
- package/dist/tools/builtins/image/generate.d.ts +59 -0
- package/dist/tools/builtins/image/generate.js +535 -0
- package/dist/tools/builtins/image/index.d.ts +2 -0
- package/dist/tools/builtins/image/index.js +2 -0
- package/dist/tools/builtins/image/paths.d.ts +12 -0
- package/dist/tools/builtins/image/paths.js +26 -0
- package/dist/tools/builtins/sandbox/bash.d.ts +25 -1
- package/dist/tools/builtins/sandbox/bash.js +201 -5
- package/dist/tools/builtins/sandbox/common.d.ts +92 -20
- package/dist/tools/builtins/sandbox/common.js +201 -17
- package/dist/tools/builtins/sandbox/deliver.js +9 -51
- package/dist/tools/builtins/sandbox/edit.js +2 -2
- package/dist/tools/builtins/sandbox/fetch-attachment.js +11 -4
- package/dist/tools/builtins/sandbox/glob.js +2 -2
- package/dist/tools/builtins/sandbox/grep.js +2 -2
- package/dist/tools/builtins/sandbox/read.js +2 -2
- package/dist/tools/builtins/sandbox/task-list.js +7 -1
- package/dist/tools/builtins/sandbox/task-output.d.ts +1 -1
- package/dist/tools/builtins/sandbox/task-output.js +172 -0
- package/dist/tools/builtins/sandbox/task-stop.js +25 -3
- package/dist/tools/builtins/sandbox/write.js +2 -2
- package/dist/tools/builtins/skill-load.js +9 -6
- package/dist/tools/device-op-resolver.d.ts +1 -1
- package/dist/tools/device-op-resolver.js +13 -3
- package/dist/tools/permission-approval-jobs.d.ts +18 -0
- package/dist/tools/permission-approval-jobs.js +92 -0
- package/dist/tools/pipeline.d.ts +17 -2
- package/dist/tools/pipeline.js +315 -26
- package/dist/tools/registry.d.ts +9 -0
- package/dist/tools/tools.module.js +15 -0
- package/package.json +4 -3
- package/src/migrations/0146_permission_authority_epochs.sql +6 -0
- package/src/migrations/0147_conversation_permission_profiles.sql +4 -0
- package/src/migrations/0148_first_message_request_hash.sql +2 -0
- package/src/migrations/0149_sandbox_operation_intents.sql +66 -0
- package/src/migrations/0150_permission_call_grants_and_approvals.sql +36 -0
- package/src/migrations/0151_starting_execution_grants.sql +8 -0
- package/src/migrations/0152_background_launch_identity.sql +66 -0
- package/src/migrations/0153_background_cancel_requester.sql +13 -0
- package/src/migrations/0154_cloud_owner_ceiling.sql +14 -0
- package/src/migrations/0155_device_instance_generations.sql +30 -0
- package/src/migrations/0156_device_owner_report.sql +7 -0
- package/src/migrations/0157_legacy_retirement.sql +116 -0
- package/src/migrations/0158_legacy_freeze_enumerated.sql +5 -0
- package/src/migrations/0159_retirement_exact_cancel.sql +9 -0
- package/src/migrations/0160_model_usage_image_kind.sql +10 -0
- package/dist/sandbox/platform-auto-mode.d.ts +0 -63
- package/dist/sandbox/platform-auto-mode.js +0 -63
|
@@ -10,7 +10,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
10
10
|
import { randomUUID } from "node:crypto";
|
|
11
11
|
import { isDeepStrictEqual } from "node:util";
|
|
12
12
|
import { connectorToolKey, parseToolKey, validateAllowHost, } from "@capybara-hq/plugin-sdk";
|
|
13
|
-
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
+
import { Inject, Injectable, Optional } from "@nestjs/common";
|
|
14
14
|
import { sql } from "kysely";
|
|
15
15
|
import { normalizeStoredDefinition } from "../agents/definition.js";
|
|
16
16
|
import { EventLog } from "../conversations/event-log.js";
|
|
@@ -23,7 +23,7 @@ import { sha256Hex } from "../identity/hash.js";
|
|
|
23
23
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
24
24
|
import { afterCommit } from "../infra/database/after-commit.js";
|
|
25
25
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
26
|
-
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
26
|
+
import { TransactionHost, withOrderedLocks, withTransactionFencePrelude, } from "../infra/database/transaction-host.js";
|
|
27
27
|
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
28
28
|
import { KernelEvents } from "../infra/events/kernel-events.js";
|
|
29
29
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
@@ -33,6 +33,8 @@ import { pendingPolicyOpsOf } from "../local-devices/policy-summary.js";
|
|
|
33
33
|
import { ORG_ROLES, roleAtLeast } from "../orgs/roles.js";
|
|
34
34
|
import { RunResume } from "../runtime/run-jobs.js";
|
|
35
35
|
import { rootConversationOf } from "../runtime/subagents/child.js";
|
|
36
|
+
import { PermissionAuthorityLock } from "../sandbox/permission-authority-lock.js";
|
|
37
|
+
import { PermissionCallGrants } from "../sandbox/permission-call-grants.js";
|
|
36
38
|
import { isSessionGrantableRule } from "../sandbox/review/types.js";
|
|
37
39
|
import { deviceNotFound, SandboxService } from "../sandbox/sandbox.service.js";
|
|
38
40
|
import { readScheduleSettings } from "../schedules/effective-settings.js";
|
|
@@ -121,6 +123,8 @@ function toRecord(r) {
|
|
|
121
123
|
requestedFor: r.requested_for,
|
|
122
124
|
approverRole: isRole(r.approver_role) ? r.approver_role : null,
|
|
123
125
|
status: r.status,
|
|
126
|
+
supersededRevision: r.superseded_revision,
|
|
127
|
+
supersededReason: r.superseded_reason,
|
|
124
128
|
scope: r.scope,
|
|
125
129
|
decidedBy: r.decided_by,
|
|
126
130
|
decidedAt: r.decided_at === null ? null : iso(r.decided_at),
|
|
@@ -168,6 +172,8 @@ let ApprovalsService = class ApprovalsService {
|
|
|
168
172
|
lockService;
|
|
169
173
|
sandbox;
|
|
170
174
|
resumes;
|
|
175
|
+
permissionLocks;
|
|
176
|
+
permissionGrants;
|
|
171
177
|
constructor(db, config, eventLog, events, txHost, schedules,
|
|
172
178
|
/** A trial approval is kept only for a tool the task may hold as a grant (its agent's tools). */
|
|
173
179
|
registry,
|
|
@@ -181,7 +187,7 @@ let ApprovalsService = class ApprovalsService {
|
|
|
181
187
|
/** A trial run's 「这次允许」 becomes a task grant only within the org's egress. */
|
|
182
188
|
sandbox,
|
|
183
189
|
/** A decision resumes its waiting run in the decision's transaction. */
|
|
184
|
-
resumes) {
|
|
190
|
+
resumes, permissionLocks, permissionGrants) {
|
|
185
191
|
this.db = db;
|
|
186
192
|
this.config = config;
|
|
187
193
|
this.eventLog = eventLog;
|
|
@@ -193,6 +199,8 @@ let ApprovalsService = class ApprovalsService {
|
|
|
193
199
|
this.lockService = lockService;
|
|
194
200
|
this.sandbox = sandbox;
|
|
195
201
|
this.resumes = resumes;
|
|
202
|
+
this.permissionLocks = permissionLocks;
|
|
203
|
+
this.permissionGrants = permissionGrants;
|
|
196
204
|
}
|
|
197
205
|
/**
|
|
198
206
|
* 新建 pending 审批(`expires_at` = 现在 + `tools.approvalTimeoutHours`)并写 `approval_requested`,同一事务。
|
|
@@ -294,7 +302,7 @@ let ApprovalsService = class ApprovalsService {
|
|
|
294
302
|
async requestNetworkIn(input) {
|
|
295
303
|
const hours = this.config.get().tools.approvalTimeoutHours;
|
|
296
304
|
const t = this.db.db;
|
|
297
|
-
// the conversation row
|
|
305
|
+
// Authority precedes the conversation row (as `requestIn`): it also serializes two asks of one call
|
|
298
306
|
await t
|
|
299
307
|
.selectFrom("conversations")
|
|
300
308
|
.select("id")
|
|
@@ -389,7 +397,7 @@ let ApprovalsService = class ApprovalsService {
|
|
|
389
397
|
async requestGitIn(input) {
|
|
390
398
|
const hours = this.config.get().tools.approvalTimeoutHours;
|
|
391
399
|
const t = this.db.db;
|
|
392
|
-
// the conversation row
|
|
400
|
+
// Authority precedes the conversation row (as `requestIn`): it also serializes two asks of one call
|
|
393
401
|
await t
|
|
394
402
|
.selectFrom("conversations")
|
|
395
403
|
.select("id")
|
|
@@ -480,15 +488,32 @@ let ApprovalsService = class ApprovalsService {
|
|
|
480
488
|
if (d.command.length === 0 || d.reason.length === 0) {
|
|
481
489
|
throw new Error("a command ask names the command and the review's reason");
|
|
482
490
|
}
|
|
483
|
-
const
|
|
491
|
+
const subject = await this.commandSubject(input.orgId, input.conversationId, input.detail.deviceId);
|
|
492
|
+
const permissionLocks = this.permissionLocks;
|
|
493
|
+
const { record, toSeq } = permissionLocks
|
|
494
|
+
? await withTransactionFencePrelude(() => permissionLocks.acquire(subject, { kind: "admit" }), () => this.requestCommandIn(input))
|
|
495
|
+
: await this.requestCommandIn(input);
|
|
484
496
|
if (toSeq !== null)
|
|
485
497
|
await this.eventLog.notify(record.conversationId, toSeq);
|
|
486
498
|
return record;
|
|
487
499
|
}
|
|
488
500
|
async requestCommandIn(input) {
|
|
501
|
+
if (input.detail.permissionCandidate) {
|
|
502
|
+
const b = input.detail.permissionCandidate.binding;
|
|
503
|
+
if (b.orgId !== input.orgId ||
|
|
504
|
+
b.conversationId !== input.conversationId ||
|
|
505
|
+
b.runId !== input.runId ||
|
|
506
|
+
b.callId !== input.callId ||
|
|
507
|
+
b.accountId !== input.requestedFor ||
|
|
508
|
+
b.deviceId !== input.detail.deviceId)
|
|
509
|
+
throw new ApiError("forbidden", "The approval does not match its exact permission candidate");
|
|
510
|
+
if (!this.permissionGrants)
|
|
511
|
+
throw new Error("Permission grants are unavailable");
|
|
512
|
+
await this.permissionGrants.assertCurrent(b);
|
|
513
|
+
}
|
|
489
514
|
const hours = this.config.get().tools.approvalTimeoutHours;
|
|
490
515
|
const t = this.db.db;
|
|
491
|
-
// the conversation row
|
|
516
|
+
// Authority precedes the conversation row (as `requestIn`): it also serializes two asks of one call
|
|
492
517
|
await t
|
|
493
518
|
.selectFrom("conversations")
|
|
494
519
|
.select("id")
|
|
@@ -501,7 +526,11 @@ let ApprovalsService = class ApprovalsService {
|
|
|
501
526
|
throw new Error(`a command ask is for the owner of a live device of the org (device ${input.detail.deviceId})`);
|
|
502
527
|
}
|
|
503
528
|
const existing = await this.commandForCall(input.runId, input.callId);
|
|
504
|
-
if (existing !== null
|
|
529
|
+
if (existing !== null &&
|
|
530
|
+
existing.status !== "superseded" &&
|
|
531
|
+
(!input.detail.permissionCandidate ||
|
|
532
|
+
existing.command?.permissionCandidate?.candidateHash ===
|
|
533
|
+
input.detail.permissionCandidate.candidateHash))
|
|
505
534
|
return { record: existing, toSeq: null };
|
|
506
535
|
const last = await t
|
|
507
536
|
.selectFrom("approvals")
|
|
@@ -509,7 +538,19 @@ let ApprovalsService = class ApprovalsService {
|
|
|
509
538
|
.where("run_id", "=", input.runId)
|
|
510
539
|
.where("call_id", "=", input.callId)
|
|
511
540
|
.executeTakeFirst();
|
|
512
|
-
const
|
|
541
|
+
const candidate = input.detail.permissionCandidate;
|
|
542
|
+
const detail = {
|
|
543
|
+
...input.detail,
|
|
544
|
+
...(candidate
|
|
545
|
+
? {
|
|
546
|
+
permissionScope: candidate.permissionScope,
|
|
547
|
+
candidateHash: candidate.candidateHash,
|
|
548
|
+
...(candidate.targetPath
|
|
549
|
+
? { targetPath: candidate.targetPath, path: candidate.targetPath }
|
|
550
|
+
: {}),
|
|
551
|
+
}
|
|
552
|
+
: {}),
|
|
553
|
+
};
|
|
513
554
|
const row = await t
|
|
514
555
|
.insertInto("approvals")
|
|
515
556
|
.values({
|
|
@@ -547,6 +588,15 @@ let ApprovalsService = class ApprovalsService {
|
|
|
547
588
|
toolKey: rec.toolKey,
|
|
548
589
|
expiresAt: rec.expiresAt,
|
|
549
590
|
command: {
|
|
591
|
+
...(detail.permissionCandidate
|
|
592
|
+
? {
|
|
593
|
+
permissionScope: detail.permissionCandidate.permissionScope,
|
|
594
|
+
candidateHash: detail.permissionCandidate.candidateHash,
|
|
595
|
+
...(detail.permissionCandidate.targetPath
|
|
596
|
+
? { targetPath: detail.permissionCandidate.targetPath }
|
|
597
|
+
: {}),
|
|
598
|
+
}
|
|
599
|
+
: {}),
|
|
550
600
|
kind: detail.kind,
|
|
551
601
|
// the approval's detail holds the whole command; the event is display only and clamped
|
|
552
602
|
command: clipText(detail.command, COMMAND_ASK_TEXT_MAX_CHARS),
|
|
@@ -823,10 +873,22 @@ let ApprovalsService = class ApprovalsService {
|
|
|
823
873
|
}
|
|
824
874
|
command = { scope, via };
|
|
825
875
|
}
|
|
826
|
-
const
|
|
876
|
+
const decide = () => this.decideIn(orgId, id, accountId, body, actorTokenId, undefined, undefined, command);
|
|
877
|
+
const subject = await this.commandSubject(orgId, ask.conversationId, ask.command.deviceId);
|
|
878
|
+
const permissionLocks = this.permissionLocks;
|
|
879
|
+
const decided = permissionLocks
|
|
880
|
+
? await withTransactionFencePrelude(() => permissionLocks.acquire(subject, { kind: "admit" }), decide)
|
|
881
|
+
: await decide();
|
|
827
882
|
await this.announceDecided(decided.record, decided.toSeq);
|
|
828
883
|
return decided.record;
|
|
829
884
|
}
|
|
885
|
+
async commandSubject(orgId, conversationId, deviceId) {
|
|
886
|
+
return {
|
|
887
|
+
orgId,
|
|
888
|
+
deviceId,
|
|
889
|
+
rootConversationId: await rootConversationOf(this.db.db, conversationId),
|
|
890
|
+
};
|
|
891
|
+
}
|
|
830
892
|
/**
|
|
831
893
|
* 「这台设备一直允许」 (transactional outbox spec §7): the intent of the device change (naming the
|
|
832
894
|
* ask) is recorded, the `policy` op sent (no transaction open), then {@link settleNetworkOp}
|
|
@@ -883,7 +945,7 @@ let ApprovalsService = class ApprovalsService {
|
|
|
883
945
|
? // the device's owner takes no remote grants: nothing is allowed, not even this run
|
|
884
946
|
{ wanted: granted, granted: [], outcome: "refused", fallback: "refused", via }
|
|
885
947
|
: { wanted: granted, granted, outcome: "once_fallback", fallback: "offline", via };
|
|
886
|
-
const settled = await this.
|
|
948
|
+
const settled = await this.settleNetworkOpOrdered(orgId, ask.id, accountId, body, network, {
|
|
887
949
|
machineId: intent.machineId,
|
|
888
950
|
opId: intent.opId,
|
|
889
951
|
result: sent.result,
|
|
@@ -922,6 +984,14 @@ let ApprovalsService = class ApprovalsService {
|
|
|
922
984
|
async settleOpOnly(op, result) {
|
|
923
985
|
await this.policies.settleIn(op, result);
|
|
924
986
|
}
|
|
987
|
+
/**
|
|
988
|
+
* {@link settleNetworkOp} with the device's permission authority lock as its ordered prelude
|
|
989
|
+
* (`DevicePolicyService.lockMachineDevice`): the global order puts it before the ask's
|
|
990
|
+
* conversation row, as a fenced device writer of the same tree takes it (device project writes).
|
|
991
|
+
*/
|
|
992
|
+
settleNetworkOpOrdered(orgId, id, accountId, body, network, op) {
|
|
993
|
+
return withOrderedLocks(() => this.policies.lockMachineDevice(op.machineId), () => this.settleNetworkOp(orgId, id, accountId, body, network, op));
|
|
994
|
+
}
|
|
925
995
|
/**
|
|
926
996
|
* The resolve transaction of a network ask's device change: the ask decided as `network` says
|
|
927
997
|
* ({@link decideTx}: lock order conversation → approval → run), then the op's outcome recorded
|
|
@@ -1006,7 +1076,8 @@ let ApprovalsService = class ApprovalsService {
|
|
|
1006
1076
|
* late) and the ask left pending with `applied_undecided`. Not applied (refused, never received,
|
|
1007
1077
|
* interrupted) or still unknown → the op's outcome recorded and the ask left pending with the
|
|
1008
1078
|
* reason (`detail.attempt`) and an `approval_updated` event so the card shows it: no person is
|
|
1009
|
-
* there to fall back to a run grant (D2).
|
|
1079
|
+
* there to fall back to a run grant (D2). The caller locked the machine's device first
|
|
1080
|
+
* (`DevicePolicyService.lockMachineDevice`, before the ask's conversation row).
|
|
1010
1081
|
*/
|
|
1011
1082
|
async settleFromResolver(ref, result) {
|
|
1012
1083
|
const t = this.db.db;
|
|
@@ -1168,6 +1239,11 @@ let ApprovalsService = class ApprovalsService {
|
|
|
1168
1239
|
(before.kind === "command") !== (command !== undefined)) {
|
|
1169
1240
|
throw new Error(`approval ${id} is of kind ${before.kind}`);
|
|
1170
1241
|
}
|
|
1242
|
+
if (before.command?.permissionCandidate) {
|
|
1243
|
+
if (!this.permissionGrants)
|
|
1244
|
+
throw new Error("Permission grants are unavailable");
|
|
1245
|
+
await this.permissionGrants.assertCurrent(before.command.permissionCandidate.binding);
|
|
1246
|
+
}
|
|
1171
1247
|
const approved = body.decision === "approve";
|
|
1172
1248
|
const reason = approved ? undefined : body.reason?.trim() || undefined;
|
|
1173
1249
|
const scope = !approved
|
|
@@ -1224,6 +1300,15 @@ let ApprovalsService = class ApprovalsService {
|
|
|
1224
1300
|
.where("id", "=", id)
|
|
1225
1301
|
.returningAll()
|
|
1226
1302
|
.executeTakeFirstOrThrow();
|
|
1303
|
+
if (approved && before.command?.permissionCandidate) {
|
|
1304
|
+
if (!this.permissionGrants)
|
|
1305
|
+
throw new Error("Permission grants are unavailable");
|
|
1306
|
+
await this.permissionGrants.issueLocked(before.command.permissionCandidate, {
|
|
1307
|
+
source: "human",
|
|
1308
|
+
approvalId: id,
|
|
1309
|
+
decidedBy: accountId,
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1227
1312
|
// 授权绑定所批准的定义指纹与来源连接器(同 slug 重新安装的连接器 id 不同,不继承)
|
|
1228
1313
|
const hash = before.specHash;
|
|
1229
1314
|
const connectorId = before.connectorId;
|
|
@@ -1357,8 +1442,8 @@ let ApprovalsService = class ApprovalsService {
|
|
|
1357
1442
|
...(reason !== undefined && { reason }),
|
|
1358
1443
|
},
|
|
1359
1444
|
});
|
|
1360
|
-
//
|
|
1361
|
-
|
|
1445
|
+
// The decision owes continuation in its own commit, even if this process dies immediately.
|
|
1446
|
+
await this.resumes.resumeIn(before.runId, id);
|
|
1362
1447
|
return { record: toRecord(updated), toSeq: Math.max(...written.map((e) => e.seq)) };
|
|
1363
1448
|
}
|
|
1364
1449
|
await audit(t, {
|
|
@@ -1392,14 +1477,6 @@ let ApprovalsService = class ApprovalsService {
|
|
|
1392
1477
|
await this.resumes.resumeIn(before.runId, id);
|
|
1393
1478
|
return { record: toRecord(updated), toSeq: Math.max(...written.map((e) => e.seq)) };
|
|
1394
1479
|
}
|
|
1395
|
-
/**
|
|
1396
|
-
* Resumes the run of a decided command ask in its own transaction after the decision committed
|
|
1397
|
-
* (`afterCommit` runs outside any transaction; `RunResume.resumeIn` requires one). Its outcome
|
|
1398
|
-
* is the suspend's re-check if the run was still running.
|
|
1399
|
-
*/
|
|
1400
|
-
async resumeDecided(runId, approvalId) {
|
|
1401
|
-
await this.resumes.resumeIn(runId, approvalId);
|
|
1402
|
-
}
|
|
1403
1480
|
/**
|
|
1404
1481
|
* A trial run of a scheduled task (scheduled tasks spec §7): an approval its creator granted, in
|
|
1405
1482
|
* any scope, is also added to the task's grants with the approved definition's hash, so its
|
|
@@ -2041,9 +2118,6 @@ __decorate([
|
|
|
2041
2118
|
__decorate([
|
|
2042
2119
|
Transactional({ propagation: "mandatory" })
|
|
2043
2120
|
], ApprovalsService.prototype, "decideTx", null);
|
|
2044
|
-
__decorate([
|
|
2045
|
-
Transactional()
|
|
2046
|
-
], ApprovalsService.prototype, "resumeDecided", null);
|
|
2047
2121
|
__decorate([
|
|
2048
2122
|
Transactional({ propagation: "mandatory" })
|
|
2049
2123
|
], ApprovalsService.prototype, "keepTrialGrant", null);
|
|
@@ -2083,6 +2157,10 @@ ApprovalsService = __decorate([
|
|
|
2083
2157
|
__param(7, Inject(DevicePolicyService)),
|
|
2084
2158
|
__param(8, Inject(LockService)),
|
|
2085
2159
|
__param(9, Inject(SandboxService)),
|
|
2086
|
-
__param(10, Inject(RunResume))
|
|
2160
|
+
__param(10, Inject(RunResume)),
|
|
2161
|
+
__param(11, Optional()),
|
|
2162
|
+
__param(11, Inject(PermissionAuthorityLock)),
|
|
2163
|
+
__param(12, Optional()),
|
|
2164
|
+
__param(12, Inject(PermissionCallGrants))
|
|
2087
2165
|
], ApprovalsService);
|
|
2088
2166
|
export { ApprovalsService };
|
|
@@ -26,6 +26,7 @@ export declare const BUILTIN: {
|
|
|
26
26
|
readonly TaskList: "TaskList";
|
|
27
27
|
readonly WebSearch: "WebSearch";
|
|
28
28
|
readonly WebFetch: "WebFetch";
|
|
29
|
+
readonly GenerateImage: "GenerateImage";
|
|
29
30
|
readonly Platform: "Platform";
|
|
30
31
|
readonly CurrentTime: "CurrentTime";
|
|
31
32
|
readonly ScheduleCreate: "ScheduleCreate";
|
|
@@ -67,7 +68,8 @@ export declare const SCHEDULE_TOOL_NAMES: readonly BuiltinName[];
|
|
|
67
68
|
* by the registry; `Agent` dispatches sub-agents and its description is built per agent;
|
|
68
69
|
* `SendMessage` and `SubagentHandback` are added by the registry ({@link SUBAGENT_MESSAGE_TOOLS}).
|
|
69
70
|
* `Platform` calls the platform's own operations as the run's person (Platform tool spec).
|
|
70
|
-
* `CurrentTime` reads the clock (the run context carries only the date).
|
|
71
|
+
* `CurrentTime` reads the clock (the run context carries only the date). `GenerateImage` makes
|
|
72
|
+
* images with the org's `image` purpose model (image generation spec §5.3). The schedule tools
|
|
71
73
|
* manage the person's scheduled tasks ({@link SCHEDULE_TOOL_NAMES}).
|
|
72
74
|
*/
|
|
73
75
|
export declare const BUILTIN_TOOL_NAMES: readonly BuiltinName[];
|
|
@@ -25,6 +25,7 @@ export const BUILTIN = {
|
|
|
25
25
|
TaskList: "TaskList",
|
|
26
26
|
WebSearch: "WebSearch",
|
|
27
27
|
WebFetch: "WebFetch",
|
|
28
|
+
GenerateImage: "GenerateImage",
|
|
28
29
|
Platform: "Platform",
|
|
29
30
|
CurrentTime: "CurrentTime",
|
|
30
31
|
ScheduleCreate: "ScheduleCreate",
|
|
@@ -92,7 +93,8 @@ export const SCHEDULE_TOOL_NAMES = [
|
|
|
92
93
|
* by the registry; `Agent` dispatches sub-agents and its description is built per agent;
|
|
93
94
|
* `SendMessage` and `SubagentHandback` are added by the registry ({@link SUBAGENT_MESSAGE_TOOLS}).
|
|
94
95
|
* `Platform` calls the platform's own operations as the run's person (Platform tool spec).
|
|
95
|
-
* `CurrentTime` reads the clock (the run context carries only the date).
|
|
96
|
+
* `CurrentTime` reads the clock (the run context carries only the date). `GenerateImage` makes
|
|
97
|
+
* images with the org's `image` purpose model (image generation spec §5.3). The schedule tools
|
|
96
98
|
* manage the person's scheduled tasks ({@link SCHEDULE_TOOL_NAMES}).
|
|
97
99
|
*/
|
|
98
100
|
export const BUILTIN_TOOL_NAMES = [
|
|
@@ -105,6 +107,7 @@ export const BUILTIN_TOOL_NAMES = [
|
|
|
105
107
|
BUILTIN.SendMessage,
|
|
106
108
|
BUILTIN.SubagentHandback,
|
|
107
109
|
...WEB_TOOL_NAMES,
|
|
110
|
+
BUILTIN.GenerateImage,
|
|
108
111
|
BUILTIN.Platform,
|
|
109
112
|
BUILTIN.CurrentTime,
|
|
110
113
|
...SCHEDULE_TOOL_NAMES,
|
|
@@ -188,6 +191,7 @@ export const BUILTIN_TOOL_KINDS = {
|
|
|
188
191
|
[BUILTIN.SubagentHandback]: "meta",
|
|
189
192
|
[BUILTIN.WebSearch]: "search",
|
|
190
193
|
[BUILTIN.WebFetch]: "read",
|
|
194
|
+
[BUILTIN.GenerateImage]: "tool",
|
|
191
195
|
[BUILTIN.Platform]: "tool",
|
|
192
196
|
[BUILTIN.CurrentTime]: "tool",
|
|
193
197
|
[BUILTIN.ScheduleCreate]: "tool",
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { EventLog } from "../../conversations/event-log.js";
|
|
2
|
+
import type { StorageService } from "../../storage/storage.service.js";
|
|
3
|
+
import type { BuiltinCallContext } from "../registry.js";
|
|
4
|
+
/**
|
|
5
|
+
* The tool error when the deployment has no object store (or it went away): files cannot be
|
|
6
|
+
* delivered. `Deliver` and `GenerateImage` share it.
|
|
7
|
+
*/
|
|
8
|
+
export declare const NO_STORAGE = "File storage is not configured in this deployment, so files cannot be delivered; ask an administrator, or provide the result in your reply instead.";
|
|
9
|
+
/** The same as the "tool unavailable" reason (the kernel adds the final period). */
|
|
10
|
+
export declare const NO_STORAGE_REASON: string;
|
|
11
|
+
/** What {@link storeArtifacts} needs. */
|
|
12
|
+
export interface ArtifactStoreDeps {
|
|
13
|
+
storage: Pick<StorageService, "kernel">;
|
|
14
|
+
eventLog: Pick<EventLog, "append"> & Partial<Pick<EventLog, "publishChanged">>;
|
|
15
|
+
}
|
|
16
|
+
/** One file to deliver as an artifact. */
|
|
17
|
+
export interface ArtifactFile {
|
|
18
|
+
/** The name shown to the user (already checked with `isValidArtifactName`). */
|
|
19
|
+
name: string;
|
|
20
|
+
contentType: string;
|
|
21
|
+
data: Uint8Array;
|
|
22
|
+
/**
|
|
23
|
+
* Where the file came from, which groups deliveries into versions in the conversation's files:
|
|
24
|
+
* the device path for `Deliver`, `generated/<callId>/<name>` for a generated file.
|
|
25
|
+
*/
|
|
26
|
+
sourcePath: string;
|
|
27
|
+
}
|
|
28
|
+
/** A stored artifact as the tool result names it. */
|
|
29
|
+
export interface StoredArtifact {
|
|
30
|
+
artifactId: string;
|
|
31
|
+
name: string;
|
|
32
|
+
size: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Stores `files` as artifacts of the call's conversation, all or none: every object is uploaded
|
|
36
|
+
* under the kernel prefix (`artifacts/<conversationId>/<artifactId>`), then all their
|
|
37
|
+
* `artifact_created` events (the call's run and `callId`) go into the log in one append. Any
|
|
38
|
+
* failure, an abort of the call before the append included, deletes every object uploaded so far
|
|
39
|
+
* (best effort; one left behind is unreferenced and goes with the conversation's purge) and
|
|
40
|
+
* rethrows; a store that is not there is the {@link NO_STORAGE} tool error. The conversation's
|
|
41
|
+
* files are announced changed once.
|
|
42
|
+
*/
|
|
43
|
+
export declare function storeArtifacts(deps: ArtifactStoreDeps, ctx: BuiltinCallContext, files: readonly ArtifactFile[]): Promise<StoredArtifact[]>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { ExtensionPointUnavailableError, ToolError } from "@capybara-hq/plugin-sdk";
|
|
3
|
+
import { ApiError } from "../../errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* The tool error when the deployment has no object store (or it went away): files cannot be
|
|
6
|
+
* delivered. `Deliver` and `GenerateImage` share it.
|
|
7
|
+
*/
|
|
8
|
+
export const NO_STORAGE = "File storage is not configured in this deployment, so files cannot be delivered; ask an administrator, or provide the result in your reply instead.";
|
|
9
|
+
/** The same as the "tool unavailable" reason (the kernel adds the final period). */
|
|
10
|
+
export const NO_STORAGE_REASON = NO_STORAGE.slice(0, -1);
|
|
11
|
+
const isStorageGone = (e) => (e instanceof ApiError && e.code === "storage.unavailable") ||
|
|
12
|
+
e instanceof ExtensionPointUnavailableError;
|
|
13
|
+
/**
|
|
14
|
+
* Stores `files` as artifacts of the call's conversation, all or none: every object is uploaded
|
|
15
|
+
* under the kernel prefix (`artifacts/<conversationId>/<artifactId>`), then all their
|
|
16
|
+
* `artifact_created` events (the call's run and `callId`) go into the log in one append. Any
|
|
17
|
+
* failure, an abort of the call before the append included, deletes every object uploaded so far
|
|
18
|
+
* (best effort; one left behind is unreferenced and goes with the conversation's purge) and
|
|
19
|
+
* rethrows; a store that is not there is the {@link NO_STORAGE} tool error. The conversation's
|
|
20
|
+
* files are announced changed once.
|
|
21
|
+
*/
|
|
22
|
+
export async function storeArtifacts(deps, ctx, files) {
|
|
23
|
+
const uploaded = [];
|
|
24
|
+
const events = [];
|
|
25
|
+
const out = [];
|
|
26
|
+
try {
|
|
27
|
+
const store = deps.storage.kernel(ctx.orgId);
|
|
28
|
+
for (const f of files) {
|
|
29
|
+
const artifactId = randomUUID();
|
|
30
|
+
const objectKey = `artifacts/${ctx.conversationId}/${artifactId}`;
|
|
31
|
+
await store.put(objectKey, f.data, {
|
|
32
|
+
contentType: f.contentType,
|
|
33
|
+
size: f.data.byteLength,
|
|
34
|
+
signal: ctx.signal,
|
|
35
|
+
});
|
|
36
|
+
uploaded.push(objectKey);
|
|
37
|
+
events.push({
|
|
38
|
+
type: "artifact_created",
|
|
39
|
+
...(ctx.runId !== undefined && { runId: ctx.runId }),
|
|
40
|
+
payload: {
|
|
41
|
+
artifactId,
|
|
42
|
+
name: f.name,
|
|
43
|
+
contentType: f.contentType,
|
|
44
|
+
size: f.data.byteLength,
|
|
45
|
+
objectKey,
|
|
46
|
+
sourcePath: f.sourcePath,
|
|
47
|
+
callId: ctx.callId,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
out.push({ artifactId, name: f.name, size: f.data.byteLength });
|
|
51
|
+
}
|
|
52
|
+
// a call stopped while uploading writes no event: its objects are removed below
|
|
53
|
+
ctx.signal.throwIfAborted();
|
|
54
|
+
await deps.eventLog.append(ctx.conversationId, events);
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
// no event references them: remove what was uploaded (best effort)
|
|
58
|
+
await Promise.all(uploaded.map((key) => Promise.resolve()
|
|
59
|
+
.then(() => deps.storage.kernel(ctx.orgId).delete(key))
|
|
60
|
+
.catch(() => { })));
|
|
61
|
+
if (isStorageGone(e))
|
|
62
|
+
throw new ToolError(NO_STORAGE);
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
await deps.eventLog.publishChanged?.(ctx.conversationId, "files");
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
@@ -8,17 +8,22 @@ import { ConversationDevices } from "../../devices/conversation-devices.js";
|
|
|
8
8
|
import { ExtensionPointDefaults } from "../../extensions/point-defaults.js";
|
|
9
9
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
10
10
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
11
|
+
import { ModelImages } from "../../models/images.js";
|
|
11
12
|
import { ModelPurposesService } from "../../models/purposes.js";
|
|
12
13
|
import { PlatformActor } from "../../platform/actor.js";
|
|
13
14
|
import { AgentDocs } from "../../platform/agent-docs.js";
|
|
14
15
|
import { PlatformOperations } from "../../platform/registry.js";
|
|
15
16
|
import type { HookRunner } from "../../runtime/hook-calls.js";
|
|
17
|
+
import { ProviderRegistry } from "../../runtime/providers.js";
|
|
16
18
|
import { StepLog } from "../../runtime/step-log.js";
|
|
19
|
+
import { LegacyRetirementStore } from "../../sandbox/legacy-retirement.js";
|
|
20
|
+
import { type ManagedDeviceContextPort, ManagedDeviceContextRecorder } from "../../sandbox/managed-device-context.js";
|
|
17
21
|
import { SandboxService } from "../../sandbox/sandbox.service.js";
|
|
18
22
|
import { ScheduledTaskStore } from "../../schedules/store.js";
|
|
19
23
|
import { StorageService } from "../../storage/storage.service.js";
|
|
20
24
|
import { ToolOutputs } from "../outputs.js";
|
|
21
25
|
import type { BuiltinTool } from "../registry.js";
|
|
26
|
+
import type { SandboxToolDeps } from "./sandbox/common.js";
|
|
22
27
|
import { ScheduleToolsPort, ScheduleUpdates } from "./schedules.js";
|
|
23
28
|
import { WebPages } from "./web/index.js";
|
|
24
29
|
/**
|
|
@@ -33,15 +38,28 @@ export declare class BuiltinTools {
|
|
|
33
38
|
/** The WebFetch extraction call: the `web_fetch` purpose, its hooks, usage and denials. */
|
|
34
39
|
purposes: ModelPurposesService, host: {
|
|
35
40
|
readonly hooks?: HookRunner;
|
|
36
|
-
} | undefined, stepLog: StepLog, log: Logger, background: BackgroundTaskStore, finisher: BackgroundTaskFinisher, stopper: BackgroundTaskStopper,
|
|
41
|
+
} | undefined, stepLog: StepLog, log: Logger, background: BackgroundTaskStore, finisher: BackgroundTaskFinisher, stopper: BackgroundTaskStopper,
|
|
42
|
+
/** The legacy-launch freeze every legacy background launch checks before dispatch (8.5). */
|
|
43
|
+
legacyLaunch: LegacyRetirementStore, platformOps: PlatformOperations, platformActor: PlatformActor, agentDocs: AgentDocs,
|
|
37
44
|
/** The schedule tools read tasks and grants; their writes go through the task service. */
|
|
38
45
|
scheduleStore: ScheduledTaskStore,
|
|
39
46
|
/** `ScheduleUpdate`'s write: its field change and grant changes in one transaction. */
|
|
40
47
|
scheduleUpdates: ScheduleUpdates,
|
|
41
48
|
/** The task service, bound by `SchedulesModule` at start (a registration port: no cycle). */
|
|
42
49
|
schedulePort: ScheduleToolsPort,
|
|
50
|
+
/** `GenerateImage`: the image calls, and the image model's availability per listing. */
|
|
51
|
+
images: ModelImages, providers: ProviderRegistry,
|
|
43
52
|
/** The clock `CurrentTime` reads (the run context's clock seam; `Date.now` by default). */
|
|
44
|
-
|
|
53
|
+
/** The managed sandbox route's device context writer (permission context replay spec B3). */
|
|
54
|
+
managedDeviceContext: ManagedDeviceContextRecorder, now?: () => number,
|
|
55
|
+
/** The paired activation gate (one switch with the runtime's request boundary). */
|
|
56
|
+
activation?: boolean);
|
|
45
57
|
/** The builtin tools, in a fixed order. */
|
|
46
58
|
list(): readonly BuiltinTool[];
|
|
47
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* The managed device context fields of the builtin sandbox tools' deps (spec B3): the injected
|
|
62
|
+
* writer, required, only when the one activation gate is on; otherwise no keys at all, and the
|
|
63
|
+
* legacy writer stays.
|
|
64
|
+
*/
|
|
65
|
+
export declare function managedSandboxDeps(recorder: ManagedDeviceContextPort, activation: boolean | undefined): Pick<SandboxToolDeps, "managedDeviceContext" | "managedDeviceContextRequired">;
|
|
@@ -21,13 +21,18 @@ import { ExtensionPointDefaults } from "../../extensions/point-defaults.js";
|
|
|
21
21
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
22
22
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
23
23
|
import { KERNEL_LOGGER } from "../../infra/tokens.js";
|
|
24
|
+
import { ModelImages } from "../../models/images.js";
|
|
24
25
|
import { ModelPurposesService } from "../../models/purposes.js";
|
|
25
26
|
import { readOrgSettings } from "../../orgs/settings.service.js";
|
|
26
27
|
import { PlatformActor } from "../../platform/actor.js";
|
|
27
28
|
import { AgentDocs } from "../../platform/agent-docs.js";
|
|
28
29
|
import { PlatformOperations } from "../../platform/registry.js";
|
|
30
|
+
import { ProviderRegistry } from "../../runtime/providers.js";
|
|
29
31
|
import { conversationTimeZone, RUN_CONTEXT_CLOCK } from "../../runtime/run-context.service.js";
|
|
30
32
|
import { StepLog } from "../../runtime/step-log.js";
|
|
33
|
+
import { LegacyRetirementStore } from "../../sandbox/legacy-retirement.js";
|
|
34
|
+
import { ManagedDeviceContextRecorder, } from "../../sandbox/managed-device-context.js";
|
|
35
|
+
import { PERMISSION_CONTEXT_ACTIVATION, permissionContextActive, } from "../../sandbox/permission-context-types.js";
|
|
31
36
|
import { SandboxService } from "../../sandbox/sandbox.service.js";
|
|
32
37
|
import { ScheduledTaskStore } from "../../schedules/store.js";
|
|
33
38
|
import { StorageService } from "../../storage/storage.service.js";
|
|
@@ -36,6 +41,7 @@ import { agentTool } from "./agent.js";
|
|
|
36
41
|
import { askUserTool } from "./ask-user.js";
|
|
37
42
|
import { currentTimeTool } from "./current-time.js";
|
|
38
43
|
import { handbackTool } from "./handback.js";
|
|
44
|
+
import { generateImageTool } from "./image/index.js";
|
|
39
45
|
import { platformTool } from "./platform.js";
|
|
40
46
|
import { readToolOutputTool } from "./read-tool-output.js";
|
|
41
47
|
import { sandboxTools } from "./sandbox/index.js";
|
|
@@ -54,19 +60,30 @@ let BuiltinTools = class BuiltinTools {
|
|
|
54
60
|
tools;
|
|
55
61
|
constructor(db, config, outputs, storage, attachments, eventLog, sandbox, devices, points, webPages,
|
|
56
62
|
/** The WebFetch extraction call: the `web_fetch` purpose, its hooks, usage and denials. */
|
|
57
|
-
purposes, host, stepLog, log, background, finisher, stopper,
|
|
63
|
+
purposes, host, stepLog, log, background, finisher, stopper,
|
|
64
|
+
/** The legacy-launch freeze every legacy background launch checks before dispatch (8.5). */
|
|
65
|
+
legacyLaunch, platformOps, platformActor, agentDocs,
|
|
58
66
|
/** The schedule tools read tasks and grants; their writes go through the task service. */
|
|
59
67
|
scheduleStore,
|
|
60
68
|
/** `ScheduleUpdate`'s write: its field change and grant changes in one transaction. */
|
|
61
69
|
scheduleUpdates,
|
|
62
70
|
/** The task service, bound by `SchedulesModule` at start (a registration port: no cycle). */
|
|
63
71
|
schedulePort,
|
|
72
|
+
/** `GenerateImage`: the image calls, and the image model's availability per listing. */
|
|
73
|
+
images, providers,
|
|
64
74
|
/** The clock `CurrentTime` reads (the run context's clock seam; `Date.now` by default). */
|
|
65
|
-
|
|
75
|
+
/** The managed sandbox route's device context writer (permission context replay spec B3). */
|
|
76
|
+
managedDeviceContext, now,
|
|
77
|
+
/** The paired activation gate (one switch with the runtime's request boundary). */
|
|
78
|
+
activation) {
|
|
79
|
+
// dormant: the builtins keep the legacy route until the paired activation turns the gate on;
|
|
80
|
+
// then every builtin entering a device (the skill tool too) takes the managed route
|
|
81
|
+
const managed = managedSandboxDeps(managedDeviceContext, activation);
|
|
66
82
|
const sandboxDeps = {
|
|
67
83
|
sandbox,
|
|
68
84
|
devices,
|
|
69
85
|
background,
|
|
86
|
+
legacyLaunch,
|
|
70
87
|
tasks: { finisher, stopper },
|
|
71
88
|
subagents: {
|
|
72
89
|
inTree: async (root, id) => id !== root && (await conversationAncestors(db.db, id)).includes(root),
|
|
@@ -77,6 +94,7 @@ let BuiltinTools = class BuiltinTools {
|
|
|
77
94
|
eventLog,
|
|
78
95
|
config,
|
|
79
96
|
log,
|
|
97
|
+
...managed,
|
|
80
98
|
};
|
|
81
99
|
this.tools = [
|
|
82
100
|
askUserTool({ config }),
|
|
@@ -98,6 +116,7 @@ let BuiltinTools = class BuiltinTools {
|
|
|
98
116
|
attachments,
|
|
99
117
|
eventLog,
|
|
100
118
|
log,
|
|
119
|
+
...managed,
|
|
101
120
|
get db() {
|
|
102
121
|
return db.db;
|
|
103
122
|
},
|
|
@@ -110,6 +129,22 @@ let BuiltinTools = class BuiltinTools {
|
|
|
110
129
|
cache: webPages,
|
|
111
130
|
extract: (ctx, page, prompt) => extractAnswer({ purposes, eventLog, stepLog, config, log, hooks: host?.hooks }, ctx, page, prompt),
|
|
112
131
|
}),
|
|
132
|
+
// images from the org's `image` purpose model, delivered as artifacts (image generation
|
|
133
|
+
// spec §5.3); written into the device too with `file_path`
|
|
134
|
+
generateImageTool({
|
|
135
|
+
images,
|
|
136
|
+
purposes,
|
|
137
|
+
providers,
|
|
138
|
+
storage,
|
|
139
|
+
eventLog,
|
|
140
|
+
stepLog,
|
|
141
|
+
config,
|
|
142
|
+
log,
|
|
143
|
+
hooks: host?.hooks,
|
|
144
|
+
db: () => db.db,
|
|
145
|
+
attachments,
|
|
146
|
+
sandbox: sandboxDeps,
|
|
147
|
+
}),
|
|
113
148
|
// the platform's own operations and docs, as the run's person (Platform tool spec)
|
|
114
149
|
platformTool({
|
|
115
150
|
ops: platformOps,
|
|
@@ -159,13 +194,29 @@ BuiltinTools = __decorate([
|
|
|
159
194
|
__param(14, Inject(BackgroundTaskStore)),
|
|
160
195
|
__param(15, Inject(BackgroundTaskFinisher)),
|
|
161
196
|
__param(16, Inject(BackgroundTaskStopper)),
|
|
162
|
-
__param(17, Inject(
|
|
163
|
-
__param(18, Inject(
|
|
164
|
-
__param(19, Inject(
|
|
165
|
-
__param(20, Inject(
|
|
166
|
-
__param(21, Inject(
|
|
167
|
-
__param(22, Inject(
|
|
168
|
-
__param(23,
|
|
169
|
-
__param(
|
|
197
|
+
__param(17, Inject(LegacyRetirementStore)),
|
|
198
|
+
__param(18, Inject(PlatformOperations)),
|
|
199
|
+
__param(19, Inject(PlatformActor)),
|
|
200
|
+
__param(20, Inject(AgentDocs)),
|
|
201
|
+
__param(21, Inject(ScheduledTaskStore)),
|
|
202
|
+
__param(22, Inject(ScheduleUpdates)),
|
|
203
|
+
__param(23, Inject(ScheduleToolsPort)),
|
|
204
|
+
__param(24, Inject(ModelImages)),
|
|
205
|
+
__param(25, Inject(ProviderRegistry)),
|
|
206
|
+
__param(26, Inject(ManagedDeviceContextRecorder)),
|
|
207
|
+
__param(27, Optional()),
|
|
208
|
+
__param(27, Inject(RUN_CONTEXT_CLOCK)),
|
|
209
|
+
__param(28, Optional()),
|
|
210
|
+
__param(28, Inject(PERMISSION_CONTEXT_ACTIVATION))
|
|
170
211
|
], BuiltinTools);
|
|
171
212
|
export { BuiltinTools };
|
|
213
|
+
/**
|
|
214
|
+
* The managed device context fields of the builtin sandbox tools' deps (spec B3): the injected
|
|
215
|
+
* writer, required, only when the one activation gate is on; otherwise no keys at all, and the
|
|
216
|
+
* legacy writer stays.
|
|
217
|
+
*/
|
|
218
|
+
export function managedSandboxDeps(recorder, activation) {
|
|
219
|
+
if (!permissionContextActive(activation))
|
|
220
|
+
return {};
|
|
221
|
+
return { managedDeviceContext: recorder, managedDeviceContextRequired: true };
|
|
222
|
+
}
|