@capybara-hq/kernel 0.1.0-rc.135 → 0.1.0-rc.137
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin/admin.module.js +3 -0
- package/dist/admin/audit-log.service.d.ts +96 -12
- package/dist/admin/audit-log.service.js +508 -44
- package/dist/admin/audit-targets.d.ts +17 -0
- package/dist/admin/audit-targets.js +84 -0
- package/dist/admin/audit.controller.d.ts +12 -9
- package/dist/admin/audit.controller.js +29 -8
- package/dist/admin/audit.schemas.d.ts +20 -4
- package/dist/admin/audit.schemas.js +42 -17
- package/dist/admin/health.service.d.ts +2 -2
- package/dist/admin/health.service.js +6 -2
- package/dist/agents/templates.js +3 -10
- package/dist/app.d.ts +3 -1
- package/dist/app.js +1 -0
- package/dist/automations/automations.service.js +4 -5
- package/dist/catalog/catalog-content.d.ts +1 -1
- package/dist/catalog/catalog-content.js +17 -22
- package/dist/catalog/catalog-items.d.ts +5 -10
- package/dist/catalog/catalog-items.js +27 -51
- package/dist/catalog/catalog-scope.service.d.ts +4 -2
- package/dist/catalog/catalog-scope.service.js +105 -141
- package/dist/catalog/catalog.module.js +2 -0
- package/dist/catalog/catalog.service.d.ts +3 -3
- package/dist/catalog/catalog.service.js +33 -8
- package/dist/catalog/cli.js +17 -0
- package/dist/catalog/component-promotion.js +8 -4
- package/dist/catalog/component-scope.service.d.ts +3 -1
- package/dist/catalog/component-scope.service.js +101 -140
- package/dist/catalog/component-state.d.ts +10 -8
- package/dist/catalog/component-state.js +42 -30
- package/dist/catalog/dependency-edges.d.ts +25 -0
- package/dist/catalog/dependency-edges.js +125 -0
- package/dist/catalog/install-status.d.ts +2 -3
- package/dist/catalog/install-status.js +2 -3
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/org-catalog.service.d.ts +3 -1
- package/dist/catalog/org-catalog.service.js +33 -94
- package/dist/catalog/org-intent.d.ts +43 -0
- package/dist/catalog/org-intent.js +184 -0
- package/dist/catalog/owned-items.d.ts +1 -1
- package/dist/catalog/owned-items.js +9 -29
- package/dist/catalog/ownership.d.ts +2 -3
- package/dist/catalog/ownership.js +10 -28
- package/dist/catalog/platform-components.d.ts +3 -1
- package/dist/catalog/platform-components.js +9 -1
- package/dist/catalog/plugin-import.js +12 -16
- package/dist/catalog/plugin-install.d.ts +3 -4
- package/dist/catalog/plugin-install.js +25 -49
- package/dist/catalog/plugin-upgrade.d.ts +2 -2
- package/dist/catalog/plugin-upgrade.js +18 -34
- package/dist/catalog/request-rules.js +20 -8
- package/dist/catalog/scope-orgs.js +2 -7
- package/dist/catalog/scope-state.d.ts +6 -6
- package/dist/catalog/scope-state.js +24 -43
- package/dist/catalog/scope-sync.d.ts +0 -2
- package/dist/catalog/scope-sync.js +11 -39
- package/dist/catalog/scope-writer.d.ts +78 -0
- package/dist/catalog/scope-writer.js +181 -0
- package/dist/catalog/unshare.js +8 -41
- package/dist/channels/connections.service.js +6 -6
- package/dist/channels/supervisor.js +3 -7
- package/dist/client/client-access.service.js +2 -12
- package/dist/config/schema.d.ts +6 -0
- package/dist/config/schema.js +29 -1
- package/dist/conversations/access.d.ts +15 -7
- package/dist/conversations/access.js +59 -10
- package/dist/conversations/conversation-agent.d.ts +18 -0
- package/dist/conversations/conversation-agent.js +23 -0
- package/dist/conversations/conversation-ref.d.ts +3 -2
- package/dist/conversations/conversation-ref.js +6 -1
- package/dist/conversations/conversations.controller.d.ts +28 -5
- package/dist/conversations/conversations.controller.js +99 -10
- package/dist/conversations/conversations.module.js +10 -0
- package/dist/conversations/conversations.schemas.d.ts +6 -0
- package/dist/conversations/conversations.schemas.js +15 -0
- package/dist/conversations/conversations.service.d.ts +125 -21
- package/dist/conversations/conversations.service.js +260 -90
- package/dist/conversations/event-log.d.ts +9 -1
- package/dist/conversations/event-log.js +2 -0
- package/dist/conversations/events.d.ts +57 -0
- package/dist/conversations/events.js +73 -1
- package/dist/conversations/external/external-calls.d.ts +75 -0
- package/dist/conversations/external/external-calls.js +91 -0
- package/dist/conversations/external/external-calls.processor.d.ts +45 -0
- package/dist/conversations/external/external-calls.processor.js +144 -0
- package/dist/conversations/external/external-conversations.service.d.ts +266 -0
- package/dist/conversations/external/external-conversations.service.js +1138 -0
- package/dist/conversations/external/external.schemas.d.ts +63 -0
- package/dist/conversations/external/external.schemas.js +52 -0
- package/dist/conversations/external/token.d.ts +6 -0
- package/dist/conversations/external/token.js +6 -0
- package/dist/conversations/permission-profile-store.d.ts +4 -1
- package/dist/conversations/permission-profiles.controller.d.ts +33 -9
- package/dist/conversations/permission-profiles.controller.js +86 -6
- package/dist/conversations/prompt-suggestions.js +2 -1
- package/dist/conversations/queue.js +2 -1
- package/dist/conversations/titles.js +4 -3
- package/dist/ctx/create-ctx.d.ts +9 -2
- package/dist/ctx/create-ctx.js +47 -62
- package/dist/ctx/guard-db.d.ts +6 -0
- package/dist/ctx/guard-db.js +55 -0
- package/dist/db/extension-handoff-schema.d.ts +1 -1
- package/dist/db/extension-handoff-schema.js +48 -10
- package/dist/db/extension-schema-transfer.js +5 -2
- package/dist/db/kernel-schema.d.ts +42 -5
- package/dist/device-access/host-services.d.ts +2 -2
- package/dist/device-access/host-services.js +8 -8
- package/dist/device-access/person-handles.d.ts +16 -7
- package/dist/device-access/person-handles.js +14 -9
- package/dist/devices/conversation-devices.d.ts +5 -0
- package/dist/devices/conversation-devices.js +16 -0
- package/dist/erasure/org-catalog.js +2 -2
- package/dist/erasure/org-rules.js +5 -0
- package/dist/errors.d.ts +34 -0
- package/dist/errors.js +34 -0
- package/dist/evals/snapshot.js +3 -1
- package/dist/evals/trajectory.service.d.ts +2 -2
- package/dist/evals/trajectory.service.js +11 -7
- package/dist/extensions/admin-ops.d.ts +9 -5
- package/dist/extensions/admin-ops.js +52 -8
- package/dist/extensions/admin-view.js +1 -4
- package/dist/extensions/bootstrap.js +4 -1
- package/dist/extensions/cli.js +1 -0
- package/dist/extensions/effective-config.js +1 -4
- package/dist/extensions/enabled.d.ts +23 -29
- package/dist/extensions/enabled.js +141 -62
- package/dist/extensions/extensions.module.js +3 -0
- package/dist/extensions/host.d.ts +71 -45
- package/dist/extensions/host.js +454 -128
- package/dist/extensions/installs.d.ts +9 -0
- package/dist/extensions/installs.js +23 -0
- package/dist/extensions/isolated/admission.d.ts +10 -0
- package/dist/extensions/isolated/admission.js +206 -0
- package/dist/extensions/isolated/authorization.d.ts +37 -0
- package/dist/extensions/isolated/authorization.js +146 -0
- package/dist/extensions/isolated/broker.d.ts +15 -0
- package/dist/extensions/isolated/broker.js +452 -0
- package/dist/extensions/isolated/child-protocol.d.ts +84 -0
- package/dist/extensions/isolated/child-protocol.js +21 -0
- package/dist/extensions/isolated/child-registration.d.ts +22 -0
- package/dist/extensions/isolated/child-registration.js +152 -0
- package/dist/extensions/isolated/child-session.d.ts +6 -0
- package/dist/extensions/isolated/child-session.js +343 -0
- package/dist/extensions/isolated/child-streams.d.ts +4 -0
- package/dist/extensions/isolated/child-streams.js +142 -0
- package/dist/extensions/isolated/child.d.ts +1 -0
- package/dist/extensions/isolated/child.js +10 -0
- package/dist/extensions/isolated/configured-package.d.ts +15 -0
- package/dist/extensions/isolated/configured-package.js +73 -0
- package/dist/extensions/isolated/lanes.d.ts +100 -0
- package/dist/extensions/isolated/lanes.js +699 -0
- package/dist/extensions/isolated/postgres-gate.d.ts +14 -0
- package/dist/extensions/isolated/postgres-gate.js +255 -0
- package/dist/extensions/isolated/revision.d.ts +32 -0
- package/dist/extensions/isolated/revision.js +358 -0
- package/dist/extensions/isolated/rpc-frame.d.ts +50 -0
- package/dist/extensions/isolated/rpc-frame.js +193 -0
- package/dist/extensions/isolated/rpc.d.ts +66 -0
- package/dist/extensions/isolated/rpc.js +314 -0
- package/dist/extensions/isolated/sandbox.d.ts +35 -0
- package/dist/extensions/isolated/sandbox.js +372 -0
- package/dist/extensions/isolated/tier.d.ts +6 -0
- package/dist/extensions/isolated/tier.js +45 -0
- package/dist/extensions/ledger.d.ts +1 -1
- package/dist/extensions/lifecycle-topics.d.ts +1 -0
- package/dist/extensions/lifecycle-topics.js +1 -0
- package/dist/extensions/lifecycle.d.ts +3 -1
- package/dist/extensions/lifecycle.js +32 -5
- package/dist/extensions/locks.d.ts +2 -0
- package/dist/extensions/locks.js +7 -0
- package/dist/extensions/org-config.d.ts +17 -27
- package/dist/extensions/org-config.js +114 -129
- package/dist/extensions/permission-view.d.ts +3 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/extensions/plugin-guides.d.ts +5 -10
- package/dist/extensions/plugin-guides.js +18 -37
- package/dist/extensions/plugin-icons.d.ts +3 -21
- package/dist/extensions/plugin-icons.js +9 -44
- package/dist/extensions/plugin-items.d.ts +10 -30
- package/dist/extensions/plugin-items.js +32 -73
- package/dist/extensions/point-defaults.d.ts +13 -21
- package/dist/extensions/point-defaults.js +32 -72
- package/dist/extensions/point-serving.d.ts +56 -0
- package/dist/extensions/point-serving.js +120 -0
- package/dist/extensions/source-intent.d.ts +14 -0
- package/dist/extensions/source-intent.js +77 -0
- package/dist/inbox/inbox-items.service.d.ts +11 -0
- package/dist/inbox/inbox-items.service.js +39 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/infra/database/database.module.js +6 -1
- package/dist/infra/database/kernel-db.d.ts +3 -1
- package/dist/infra/database/kernel-db.js +4 -0
- package/dist/infra/outbox/registry.d.ts +70 -0
- package/dist/infra/outbox/registry.js +44 -0
- package/dist/infra/paging/paged-read.d.ts +56 -0
- package/dist/infra/paging/paged-read.js +78 -0
- package/dist/infra/queue/queue-names.d.ts +1 -1
- package/dist/infra/queue/queue-names.js +1 -0
- package/dist/kernel.d.ts +3 -2
- package/dist/kernel.js +21 -2
- package/dist/kernel.module.js +4 -0
- package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
- package/dist/lifecycle/kernel-lifecycle.js +22 -12
- package/dist/lifecycle/tokens.d.ts +8 -0
- package/dist/lifecycle/tokens.js +2 -0
- package/dist/orgs/directory-sync.js +1 -4
- package/dist/platform/exclusions.js +10 -0
- package/dist/platform/ops/admin.ops.js +14 -3
- package/dist/platform/ops/conversations.ops.d.ts +8 -1
- package/dist/platform/ops/conversations.ops.js +52 -14
- package/dist/platform/ops/orgs.ops.js +13 -3
- package/dist/plugin-reads/plugin-reads.service.js +4 -1
- package/dist/push/recipients.js +5 -2
- package/dist/retention/worker.js +2 -1
- package/dist/roles/worker-role.d.ts +3 -1
- package/dist/roles/worker-role.js +8 -2
- package/dist/roles/worker.module.js +3 -0
- package/dist/runtime/context/build.js +8 -0
- package/dist/runtime/context/compaction.js +7 -0
- package/dist/runtime/idle-arm.js +3 -1
- package/dist/runtime/idle-compaction.js +2 -1
- package/dist/runtime/run-finish.js +2 -1
- package/dist/runtime/run-jobs.js +2 -1
- package/dist/runtime/run-queue.js +4 -2
- package/dist/runtime/run-worker.js +5 -2
- package/dist/runtime/stream.js +10 -0
- package/dist/runtime/usage-report.js +2 -1
- package/dist/runtime/waits.js +2 -1
- package/dist/setup/setup-checks.service.js +3 -2
- package/dist/skills/usable.js +41 -30
- package/dist/themes/theme-state.js +3 -3
- package/dist/tools/approvals.service.js +6 -2
- package/dist/tools/builtins/skill-load.js +3 -2
- package/dist/tools/registry.d.ts +1 -1
- package/dist/tools/registry.js +1 -1
- package/dist/tools/tool-watch.service.js +3 -1
- package/package.json +3 -3
- package/src/migrations/0227_external_conversations.sql +33 -0
- package/src/migrations/0228_extension_org_switches.sql +56 -0
- package/src/migrations/0229_catalog_dependency_edges.sql +17 -0
- package/src/migrations/0230_audit_filter_idx.sql +18 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The schemas of external conversations (local agents spec Part 2): the routes' params and bodies,
|
|
4
|
+
* and what a `conversation-target@1` provider's `targets()` must return to be listed.
|
|
5
|
+
*/
|
|
6
|
+
/** A target's key (`ConversationTargetInfo.key`). */
|
|
7
|
+
export declare const TARGET_KEY_RE: RegExp;
|
|
8
|
+
/** The target a first message starts an external conversation with. */
|
|
9
|
+
export declare const ExternalTargetRef: z.ZodObject<{
|
|
10
|
+
extension: z.ZodString;
|
|
11
|
+
targetKey: z.ZodString;
|
|
12
|
+
}, z.core.$strict>;
|
|
13
|
+
/** A target's option id (a model, effort or mode the target lists). */
|
|
14
|
+
export declare const TargetOptionId: z.ZodString;
|
|
15
|
+
/** `POST …/conversations/:id/external-requests/:requestId`: the route params. */
|
|
16
|
+
export declare const ExternalRequestParams: z.ZodObject<{
|
|
17
|
+
id: z.ZodString;
|
|
18
|
+
requestId: z.ZodString;
|
|
19
|
+
}, z.core.$strip>;
|
|
20
|
+
/** `POST …/conversations/:id/external-requests/:requestId`: the chosen option. */
|
|
21
|
+
export declare const ExternalAnswerBody: z.ZodObject<{
|
|
22
|
+
optionId: z.ZodString;
|
|
23
|
+
}, z.core.$strict>;
|
|
24
|
+
/**
|
|
25
|
+
* One target as a provider's `targets()` returns it (`ConversationTargetInfo`); a target that does
|
|
26
|
+
* not parse is left out of the list (logged).
|
|
27
|
+
*/
|
|
28
|
+
export declare const ConversationTargetInfoSchema: z.ZodObject<{
|
|
29
|
+
key: z.ZodString;
|
|
30
|
+
label: z.ZodObject<{
|
|
31
|
+
zh: z.ZodString;
|
|
32
|
+
en: z.ZodString;
|
|
33
|
+
}, z.core.$strip>;
|
|
34
|
+
description: z.ZodOptional<z.ZodObject<{
|
|
35
|
+
zh: z.ZodString;
|
|
36
|
+
en: z.ZodString;
|
|
37
|
+
}, z.core.$strip>>;
|
|
38
|
+
icon: z.ZodOptional<z.ZodString>;
|
|
39
|
+
devices: z.ZodArray<z.ZodObject<{
|
|
40
|
+
deviceId: z.ZodString;
|
|
41
|
+
ready: z.ZodNullable<z.ZodBoolean>;
|
|
42
|
+
models: z.ZodArray<z.ZodObject<{
|
|
43
|
+
id: z.ZodString;
|
|
44
|
+
label: z.ZodObject<{
|
|
45
|
+
zh: z.ZodString;
|
|
46
|
+
en: z.ZodString;
|
|
47
|
+
}, z.core.$strip>;
|
|
48
|
+
}, z.core.$strip>>;
|
|
49
|
+
efforts: z.ZodArray<z.ZodString>;
|
|
50
|
+
modes: z.ZodArray<z.ZodObject<{
|
|
51
|
+
id: z.ZodString;
|
|
52
|
+
label: z.ZodObject<{
|
|
53
|
+
zh: z.ZodString;
|
|
54
|
+
en: z.ZodString;
|
|
55
|
+
}, z.core.$strip>;
|
|
56
|
+
}, z.core.$strip>>;
|
|
57
|
+
defaults: z.ZodObject<{
|
|
58
|
+
model: z.ZodOptional<z.ZodString>;
|
|
59
|
+
effort: z.ZodOptional<z.ZodString>;
|
|
60
|
+
mode: z.ZodOptional<z.ZodString>;
|
|
61
|
+
}, z.core.$strip>;
|
|
62
|
+
}, z.core.$strip>>;
|
|
63
|
+
}, z.core.$strip>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { EXTERNAL_EVENT_ID_RE, EXTERNAL_OPTION_ID_RE } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
/**
|
|
4
|
+
* The schemas of external conversations (local agents spec Part 2): the routes' params and bodies,
|
|
5
|
+
* and what a `conversation-target@1` provider's `targets()` must return to be listed.
|
|
6
|
+
*/
|
|
7
|
+
/** A target's key (`ConversationTargetInfo.key`). */
|
|
8
|
+
export const TARGET_KEY_RE = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
9
|
+
/** The target a first message starts an external conversation with. */
|
|
10
|
+
export const ExternalTargetRef = z
|
|
11
|
+
.object({
|
|
12
|
+
extension: z.string().min(1).max(100),
|
|
13
|
+
targetKey: z.string().regex(TARGET_KEY_RE, "targetKey must be a target key"),
|
|
14
|
+
})
|
|
15
|
+
.strict();
|
|
16
|
+
/** A target's option id (a model, effort or mode the target lists). */
|
|
17
|
+
export const TargetOptionId = z.string().min(1).max(200);
|
|
18
|
+
/** `POST …/conversations/:id/external-requests/:requestId`: the route params. */
|
|
19
|
+
export const ExternalRequestParams = z.object({
|
|
20
|
+
id: z.string().min(1).max(100),
|
|
21
|
+
requestId: z.string().regex(EXTERNAL_EVENT_ID_RE, "requestId is not a request id"),
|
|
22
|
+
});
|
|
23
|
+
/** `POST …/conversations/:id/external-requests/:requestId`: the chosen option. */
|
|
24
|
+
export const ExternalAnswerBody = z
|
|
25
|
+
.object({ optionId: z.string().regex(EXTERNAL_OPTION_ID_RE, "optionId is not an option id") })
|
|
26
|
+
.strict();
|
|
27
|
+
const LocalizedNameSchema = z.object({ zh: z.string().max(200), en: z.string().max(200) });
|
|
28
|
+
const TargetOptionSchema = z.object({ id: TargetOptionId, label: LocalizedNameSchema });
|
|
29
|
+
/**
|
|
30
|
+
* One target as a provider's `targets()` returns it (`ConversationTargetInfo`); a target that does
|
|
31
|
+
* not parse is left out of the list (logged).
|
|
32
|
+
*/
|
|
33
|
+
export const ConversationTargetInfoSchema = z.object({
|
|
34
|
+
key: z.string().regex(TARGET_KEY_RE),
|
|
35
|
+
label: LocalizedNameSchema,
|
|
36
|
+
description: LocalizedNameSchema.optional(),
|
|
37
|
+
icon: z.string().max(2000).optional(),
|
|
38
|
+
devices: z
|
|
39
|
+
.array(z.object({
|
|
40
|
+
deviceId: z.string().min(1).max(100),
|
|
41
|
+
ready: z.boolean().nullable(),
|
|
42
|
+
models: z.array(TargetOptionSchema).max(200),
|
|
43
|
+
efforts: z.array(z.string().max(200)).max(50),
|
|
44
|
+
modes: z.array(TargetOptionSchema).max(50),
|
|
45
|
+
defaults: z.object({
|
|
46
|
+
model: z.string().max(200).optional(),
|
|
47
|
+
effort: z.string().max(200).optional(),
|
|
48
|
+
mode: z.string().max(200).optional(),
|
|
49
|
+
}),
|
|
50
|
+
}))
|
|
51
|
+
.max(50),
|
|
52
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The token the extension host finds `ExternalConversationsService` by at the time of use
|
|
3
|
+
* (`ModuleRef`, `ctx.conversations`' external methods): a symbol in a file of its own, so the
|
|
4
|
+
* host never imports the service's module graph.
|
|
5
|
+
*/
|
|
6
|
+
export declare const EXTERNAL_CONVERSATIONS_BACKEND: unique symbol;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The token the extension host finds `ExternalConversationsService` by at the time of use
|
|
3
|
+
* (`ModuleRef`, `ctx.conversations`' external methods): a symbol in a file of its own, so the
|
|
4
|
+
* host never imports the service's module graph.
|
|
5
|
+
*/
|
|
6
|
+
export const EXTERNAL_CONVERSATIONS_BACKEND = Symbol("EXTERNAL_CONVERSATIONS_BACKEND");
|
|
@@ -10,7 +10,7 @@ export declare class PermissionProfileStore {
|
|
|
10
10
|
constructor(db: KernelDb, access: ConversationAccess);
|
|
11
11
|
own(orgId: string, id: string, accountId: string): Promise<{
|
|
12
12
|
account_id: string;
|
|
13
|
-
agent_id: string;
|
|
13
|
+
agent_id: string | null;
|
|
14
14
|
archived_at: Date | null;
|
|
15
15
|
automation_id: string | null;
|
|
16
16
|
consumed_seq: string;
|
|
@@ -20,6 +20,9 @@ export declare class PermissionProfileStore {
|
|
|
20
20
|
deleted_at: Date | null;
|
|
21
21
|
device_id: string | null;
|
|
22
22
|
eval_run_id: string | null;
|
|
23
|
+
external: import("../db/kernel-schema.js").ExternalRef | null;
|
|
24
|
+
external_delivered_seq: string;
|
|
25
|
+
external_state: import("@capybara-hq/plugin-sdk").ExternalConversationState;
|
|
23
26
|
id: string;
|
|
24
27
|
kind: import("../db/kernel-schema.js").ConversationKind;
|
|
25
28
|
last_seq: string;
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import type { FastifyReply } from "fastify";
|
|
2
|
-
import
|
|
2
|
+
import { z } from "zod";
|
|
3
3
|
import { type Principal } from "../identity/principal.js";
|
|
4
4
|
import { type OrgContext } from "../orgs/org.guard.js";
|
|
5
|
-
import { ConversationsService } from "./conversations.service.js";
|
|
5
|
+
import { ConversationsService, type ConversationView } from "./conversations.service.js";
|
|
6
|
+
import type { ConversationEvent } from "./events.js";
|
|
7
|
+
import { type ConversationTargetItem, ExternalConversationsService } from "./external/external-conversations.service.js";
|
|
6
8
|
import { PermissionGetParams, PermissionPatchBody, PermissionPreviewQuery } from "./permission-profiles.schemas.js";
|
|
7
9
|
import { PermissionProfilesService } from "./permission-profiles.service.js";
|
|
10
|
+
/**
|
|
11
|
+
* `POST …/conversations/first-message`: exactly one of `agentId` (with `permission`) or `target`
|
|
12
|
+
* (local agents spec Part 2: an external conversation, with the target's `model`, `effort` and
|
|
13
|
+
* `mode`; `deviceId` and `project` as for an agent). Mixing the two kinds' fields is
|
|
14
|
+
* `conversation.target_fields`.
|
|
15
|
+
*/
|
|
8
16
|
export declare const FirstMessageBody: z.ZodObject<{
|
|
9
|
-
agentId: z.ZodString;
|
|
10
17
|
deviceId: z.ZodOptional<z.ZodString>;
|
|
11
18
|
project: z.ZodOptional<z.ZodString & z.ZodType<string, string, z.core.$ZodTypeInternals<string, string>>>;
|
|
12
19
|
message: z.ZodNonOptional<z.ZodOptional<z.ZodObject<{
|
|
@@ -50,19 +57,36 @@ export declare const FirstMessageBody: z.ZodObject<{
|
|
|
50
57
|
}>>;
|
|
51
58
|
clientMessageId: z.ZodString;
|
|
52
59
|
}, z.core.$strict>>>;
|
|
53
|
-
|
|
60
|
+
agentId: z.ZodOptional<z.ZodString>;
|
|
61
|
+
permission: z.ZodOptional<z.ZodObject<{
|
|
54
62
|
profile: z.ZodEnum<{
|
|
55
63
|
"approve-for-me": "approve-for-me";
|
|
56
64
|
"full-access": "full-access";
|
|
57
65
|
"request-approval": "request-approval";
|
|
58
66
|
}>;
|
|
59
67
|
consentScopeRevision: z.ZodOptional<z.ZodString>;
|
|
60
|
-
}, z.core.$strict
|
|
68
|
+
}, z.core.$strict>>;
|
|
69
|
+
target: z.ZodOptional<z.ZodObject<{
|
|
70
|
+
extension: z.ZodString;
|
|
71
|
+
targetKey: z.ZodString;
|
|
72
|
+
}, z.core.$strict>>;
|
|
73
|
+
model: z.ZodOptional<z.ZodString>;
|
|
74
|
+
effort: z.ZodOptional<z.ZodString>;
|
|
75
|
+
mode: z.ZodOptional<z.ZodString>;
|
|
61
76
|
}, z.core.$strict>;
|
|
62
77
|
export declare class PermissionProfilesController {
|
|
63
78
|
private readonly permissions;
|
|
64
79
|
private readonly conversations;
|
|
65
|
-
|
|
80
|
+
private readonly external;
|
|
81
|
+
constructor(permissions: PermissionProfilesService, conversations: ConversationsService, external: Pick<ExternalConversationsService, "targets" | "createWithMessage">);
|
|
82
|
+
/**
|
|
83
|
+
* The conversation targets (local agents) the caller can start a conversation with, from every
|
|
84
|
+
* `conversation-target@1` provider serving the org (local agents spec Part 2); a provider that
|
|
85
|
+
* fails or does not answer within `tools.timeoutMs` is left out.
|
|
86
|
+
*/
|
|
87
|
+
targets(org: OrgContext, p: Principal): Promise<{
|
|
88
|
+
targets: ConversationTargetItem[];
|
|
89
|
+
}>;
|
|
66
90
|
get(org: OrgContext, p: Principal, { id }: z.infer<typeof PermissionGetParams>): Promise<{
|
|
67
91
|
availability: import("../sandbox/permission-availability.js").PermissionAvailability;
|
|
68
92
|
profile: import("@capybara-hq/plugin-sdk").PermissionProfile;
|
|
@@ -107,8 +131,8 @@ export declare class PermissionProfilesController {
|
|
|
107
131
|
availability: import("../sandbox/permission-availability.js").PermissionAvailability;
|
|
108
132
|
}>;
|
|
109
133
|
first(org: OrgContext, p: Principal, body: z.infer<typeof FirstMessageBody>, reply: FastifyReply): Promise<{
|
|
110
|
-
conversation:
|
|
111
|
-
event:
|
|
112
|
-
events:
|
|
134
|
+
conversation: ConversationView;
|
|
135
|
+
event: ConversationEvent;
|
|
136
|
+
events: ConversationEvent[];
|
|
113
137
|
}>;
|
|
114
138
|
}
|
|
@@ -8,6 +8,8 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
8
8
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
9
|
};
|
|
10
10
|
import { Controller, Get, Inject, Patch, Post, Res } from "@nestjs/common";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import { ApiError } from "../errors.js";
|
|
11
13
|
import { requestSignal } from "../http/request-signal.js";
|
|
12
14
|
import { CurrentPrincipal, requireUser, SessionOnly, } from "../identity/principal.js";
|
|
13
15
|
import { createZodDto } from "../infra/validation/create-zod-dto.js";
|
|
@@ -15,11 +17,42 @@ import { ZBody, ZParam, ZQuery } from "../infra/validation/zod-params.js";
|
|
|
15
17
|
import { CurrentOrg, OrgAction } from "../orgs/org.guard.js";
|
|
16
18
|
import { CreateBody } from "./conversations.controller.js";
|
|
17
19
|
import { ConversationsService } from "./conversations.service.js";
|
|
20
|
+
import { ExternalTargetRef, TargetOptionId } from "./external/external.schemas.js";
|
|
21
|
+
import { ExternalConversationsService, } from "./external/external-conversations.service.js";
|
|
18
22
|
import { PermissionGetParams, PermissionPatchBody, PermissionPreviewQuery, PermissionSelectionSchema, } from "./permission-profiles.schemas.js";
|
|
19
23
|
import { PermissionProfilesService } from "./permission-profiles.service.js";
|
|
24
|
+
/**
|
|
25
|
+
* `POST …/conversations/first-message`: exactly one of `agentId` (with `permission`) or `target`
|
|
26
|
+
* (local agents spec Part 2: an external conversation, with the target's `model`, `effort` and
|
|
27
|
+
* `mode`; `deviceId` and `project` as for an agent). Mixing the two kinds' fields is
|
|
28
|
+
* `conversation.target_fields`.
|
|
29
|
+
*/
|
|
20
30
|
export const FirstMessageBody = CreateBody.required({ message: true })
|
|
21
|
-
.extend({
|
|
22
|
-
.
|
|
31
|
+
.extend({
|
|
32
|
+
agentId: z.string().min(1).max(100).optional(),
|
|
33
|
+
permission: PermissionSelectionSchema.optional(),
|
|
34
|
+
target: ExternalTargetRef.optional(),
|
|
35
|
+
model: TargetOptionId.optional(),
|
|
36
|
+
effort: TargetOptionId.optional(),
|
|
37
|
+
mode: TargetOptionId.optional(),
|
|
38
|
+
})
|
|
39
|
+
.strict()
|
|
40
|
+
.superRefine((b, ctx) => {
|
|
41
|
+
if ((b.agentId === undefined) === (b.target === undefined))
|
|
42
|
+
ctx.addIssue({
|
|
43
|
+
code: "custom",
|
|
44
|
+
message: "Give exactly one of agentId or target",
|
|
45
|
+
path: ["agentId"],
|
|
46
|
+
});
|
|
47
|
+
else if (b.agentId !== undefined && b.permission === undefined)
|
|
48
|
+
ctx.addIssue({
|
|
49
|
+
code: "custom",
|
|
50
|
+
message: "permission is required with agentId",
|
|
51
|
+
path: ["permission"],
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
/** The message fields a first message to a target does not take (it has its own options). */
|
|
55
|
+
const NOT_TARGET_MESSAGE_FIELDS = ["tokens", "files", "model", "effort", "skill"];
|
|
23
56
|
class PermissionParamsDto extends createZodDto(PermissionGetParams, "PermissionParams") {
|
|
24
57
|
}
|
|
25
58
|
class PermissionPatchDto extends createZodDto(PermissionPatchBody, "PermissionPatch") {
|
|
@@ -31,9 +64,19 @@ class FirstMessageDto extends createZodDto(FirstMessageBody, "ConversationFirstM
|
|
|
31
64
|
let PermissionProfilesController = class PermissionProfilesController {
|
|
32
65
|
permissions;
|
|
33
66
|
conversations;
|
|
34
|
-
|
|
67
|
+
external;
|
|
68
|
+
constructor(permissions, conversations, external) {
|
|
35
69
|
this.permissions = permissions;
|
|
36
70
|
this.conversations = conversations;
|
|
71
|
+
this.external = external;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The conversation targets (local agents) the caller can start a conversation with, from every
|
|
75
|
+
* `conversation-target@1` provider serving the org (local agents spec Part 2); a provider that
|
|
76
|
+
* fails or does not answer within `tools.timeoutMs` is left out.
|
|
77
|
+
*/
|
|
78
|
+
async targets(org, p) {
|
|
79
|
+
return { targets: await this.external.targets(org.orgId, requireUser(p).account.id) };
|
|
37
80
|
}
|
|
38
81
|
get(org, p, { id }) {
|
|
39
82
|
return this.permissions.get(org.orgId, id, requireUser(p).account.id);
|
|
@@ -45,8 +88,37 @@ let PermissionProfilesController = class PermissionProfilesController {
|
|
|
45
88
|
return this.permissions.preview(org.orgId, requireUser(p).account.id, q.deviceId, q.project);
|
|
46
89
|
}
|
|
47
90
|
async first(org, p, body, reply) {
|
|
48
|
-
const { permission, agentId, message, deviceId, project } = body;
|
|
49
|
-
const
|
|
91
|
+
const { permission, agentId, target, message, deviceId, project } = body;
|
|
92
|
+
const accountId = requireUser(p).account.id;
|
|
93
|
+
if (target !== undefined) {
|
|
94
|
+
const mixed = [
|
|
95
|
+
...(permission !== undefined ? ["permission"] : []),
|
|
96
|
+
...NOT_TARGET_MESSAGE_FIELDS.filter((k) => message[k] !== undefined).map((k) => `message.${k}`),
|
|
97
|
+
];
|
|
98
|
+
if (mixed.length > 0)
|
|
99
|
+
throw new ApiError("conversation.target_fields", `A first message to a target takes no ${mixed.join(", ")}`);
|
|
100
|
+
const out = await this.external.createWithMessage(org.orgId, accountId, {
|
|
101
|
+
extension: target.extension,
|
|
102
|
+
targetKey: target.targetKey,
|
|
103
|
+
text: message.text,
|
|
104
|
+
clientMessageId: message.clientMessageId,
|
|
105
|
+
...(deviceId !== undefined && { deviceId }),
|
|
106
|
+
...(project !== undefined && { project }),
|
|
107
|
+
...(body.model !== undefined && { model: body.model }),
|
|
108
|
+
...(body.effort !== undefined && { effort: body.effort }),
|
|
109
|
+
...(body.mode !== undefined && { mode: body.mode }),
|
|
110
|
+
...(message.locale !== undefined && { locale: message.locale }),
|
|
111
|
+
});
|
|
112
|
+
reply.status(out.created ? 201 : 200);
|
|
113
|
+
return { conversation: out.conversation, event: out.event, events: out.events };
|
|
114
|
+
}
|
|
115
|
+
const mixed = ["model", "effort", "mode"].filter((k) => body[k] !== undefined);
|
|
116
|
+
if (mixed.length > 0)
|
|
117
|
+
throw new ApiError("conversation.target_fields", `A first message to an agent takes no ${mixed.join(", ")}`);
|
|
118
|
+
// the refinement made both present
|
|
119
|
+
if (agentId === undefined || permission === undefined)
|
|
120
|
+
throw new ApiError("validation_failed", "agentId and permission are required");
|
|
121
|
+
const out = await this.conversations.createWithMessage(org.orgId, accountId, agentId, {
|
|
50
122
|
text: message.text,
|
|
51
123
|
clientMessageId: message.clientMessageId,
|
|
52
124
|
...(message.tokens !== undefined ? { tokens: message.tokens } : {}),
|
|
@@ -64,6 +136,13 @@ let PermissionProfilesController = class PermissionProfilesController {
|
|
|
64
136
|
return { conversation: out.conversation, event: out.event, events: out.events };
|
|
65
137
|
}
|
|
66
138
|
};
|
|
139
|
+
__decorate([
|
|
140
|
+
Get("conversation-targets"),
|
|
141
|
+
SessionOnly(),
|
|
142
|
+
OrgAction("agents.use"),
|
|
143
|
+
__param(0, CurrentOrg()),
|
|
144
|
+
__param(1, CurrentPrincipal())
|
|
145
|
+
], PermissionProfilesController.prototype, "targets", null);
|
|
67
146
|
__decorate([
|
|
68
147
|
Get("conversations/:id/permissions"),
|
|
69
148
|
SessionOnly(),
|
|
@@ -101,6 +180,7 @@ __decorate([
|
|
|
101
180
|
PermissionProfilesController = __decorate([
|
|
102
181
|
Controller("api/v1/orgs/:orgId"),
|
|
103
182
|
__param(0, Inject(PermissionProfilesService)),
|
|
104
|
-
__param(1, Inject(ConversationsService))
|
|
183
|
+
__param(1, Inject(ConversationsService)),
|
|
184
|
+
__param(2, Inject(ExternalConversationsService))
|
|
105
185
|
], PermissionProfilesController);
|
|
106
186
|
export { PermissionProfilesController };
|
|
@@ -35,6 +35,7 @@ import { fireObserve, hookDeniedAudit, hookDeniedEvent, } from "../runtime/hook-
|
|
|
35
35
|
import { consumeModelStream, deltaChars } from "../runtime/model-call.js";
|
|
36
36
|
import { ModelRequestDenied, ProviderRegistry, ProvidersModule, prepareModelStream, } from "../runtime/providers.js";
|
|
37
37
|
import { UsageReportJobs, UsageReportJobsModule } from "../runtime/usage-report-jobs.js";
|
|
38
|
+
import { agentIdOf } from "./conversation-agent.js";
|
|
38
39
|
import { EventLog, EventLogModule } from "./event-log.js";
|
|
39
40
|
import { resolveConversationOrg } from "./job-org.js";
|
|
40
41
|
import { buildSuggestionRequest, cleanSuggestion, lastModelCallOf, PROMPT_SUGGESTION_TIMEOUT_MS, suggestionEligible, suggestionStale, } from "./prompt-suggestion-request.js";
|
|
@@ -326,7 +327,7 @@ let PromptSuggestionGenerator = class PromptSuggestionGenerator {
|
|
|
326
327
|
return "not_eligible";
|
|
327
328
|
return {
|
|
328
329
|
orgId: conv.org_id,
|
|
329
|
-
agentId: conv
|
|
330
|
+
agentId: agentIdOf(conv),
|
|
330
331
|
accountId: s.triggerAccountId,
|
|
331
332
|
agentVersion: s.agentVersion,
|
|
332
333
|
afterSeq: Number(finished.seq),
|
|
@@ -2,6 +2,7 @@ import { sql } from "kysely";
|
|
|
2
2
|
import { normalizeStoredDefinition } from "../agents/definition.js";
|
|
3
3
|
import { ApiError } from "../errors.js";
|
|
4
4
|
import { recordedSkillBlocked, } from "../skills/invoke.js";
|
|
5
|
+
import { agentIdOf } from "./conversation-agent.js";
|
|
5
6
|
import { payloadJson } from "./event-log.js";
|
|
6
7
|
import { userMessageIdemKey } from "./idem-keys.js";
|
|
7
8
|
import { readMessageContext } from "./message-context.js";
|
|
@@ -194,7 +195,7 @@ async function currentAgent(t, conversationId) {
|
|
|
194
195
|
.executeTakeFirstOrThrow();
|
|
195
196
|
return {
|
|
196
197
|
orgId: row.org_id,
|
|
197
|
-
agentId: row
|
|
198
|
+
agentId: agentIdOf(row),
|
|
198
199
|
definition: normalizeStoredDefinition(row.definition),
|
|
199
200
|
};
|
|
200
201
|
}
|
|
@@ -26,6 +26,7 @@ import { ModelPurposesService, PurposeAttemptError } from "../models/purposes.js
|
|
|
26
26
|
import { fireObserve, hookDeniedAudit, hookDeniedEvent, } from "../runtime/hook-calls.js";
|
|
27
27
|
import { UsageReportJobs, UsageReportJobsModule } from "../runtime/usage-report-jobs.js";
|
|
28
28
|
import { HIDDEN_FORMAT_RE } from "../text.js";
|
|
29
|
+
import { agentIdOf } from "./conversation-agent.js";
|
|
29
30
|
import { EventLog, EventLogModule } from "./event-log.js";
|
|
30
31
|
import { typedInvocation } from "./events.js";
|
|
31
32
|
import { resolveConversationOrg } from "./job-org.js";
|
|
@@ -198,6 +199,7 @@ export async function runTitleJob(d, conversationId, attempt) {
|
|
|
198
199
|
.executeTakeFirst();
|
|
199
200
|
if (!claimed)
|
|
200
201
|
return;
|
|
202
|
+
const agentId = agentIdOf(claimed);
|
|
201
203
|
const started = await d.eventLog.latestOfType(conversationId, "run_started");
|
|
202
204
|
const fallback = started?.payload.model;
|
|
203
205
|
const signal = AbortSignal.any([
|
|
@@ -207,9 +209,8 @@ export async function runTitleJob(d, conversationId, attempt) {
|
|
|
207
209
|
const scope = {
|
|
208
210
|
orgId: claimed.org_id,
|
|
209
211
|
accountId: claimed.account_id,
|
|
210
|
-
agentId
|
|
211
|
-
agentVersion: started?.payload.agentVersion ??
|
|
212
|
-
(await agentVersionOf(d.db, claimed.org_id, claimed.agent_id)),
|
|
212
|
+
agentId,
|
|
213
|
+
agentVersion: started?.payload.agentVersion ?? (await agentVersionOf(d.db, claimed.org_id, agentId)),
|
|
213
214
|
conversationId,
|
|
214
215
|
runId: "",
|
|
215
216
|
};
|
package/dist/ctx/create-ctx.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type EmbedClaims, type ExtensionContext, type ExtensionManifest, type ExtensionPointRef, type HttpAccess, type Logger, type RouteCaller, resolveUiSlots, type SetupCheckArgs, type SetupGuidePlace, type ToolHandler, type ToolSpec, type ToolWatcher, type TriggerEvent } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
import type { Redis } from "ioredis";
|
|
3
|
-
import { Kysely } from "kysely";
|
|
3
|
+
import type { Kysely } from "kysely";
|
|
4
4
|
import type { TriggerEventsService } from "../automations/events/trigger-events.service.js";
|
|
5
|
+
import type { ExternalConversationsBackend } from "../conversations/external/external-conversations.service.js";
|
|
5
6
|
import { pluginSchemaName } from "../db/extension-db.js";
|
|
6
7
|
import type { DeviceAccessService } from "../device-access/device-access.service.js";
|
|
7
8
|
import type { ServiceEventHandler, ServiceEventsBackend } from "../device-access/service-events.js";
|
|
@@ -107,6 +108,12 @@ export interface KernelDeps {
|
|
|
107
108
|
* found at the time of use like `inbox`. Absent, or resolving to undefined: those calls reject.
|
|
108
109
|
*/
|
|
109
110
|
pluginReads?: () => PluginReadsBackend | undefined;
|
|
111
|
+
/**
|
|
112
|
+
* What `ctx.conversations`' external methods call through (`ExternalConversationsService`),
|
|
113
|
+
* found at the time of use like `inbox`. Absent, or resolving to undefined: those calls reject
|
|
114
|
+
* (`streamExternalText` drops its hint).
|
|
115
|
+
*/
|
|
116
|
+
externalConversations?: () => ExternalConversationsBackend | undefined;
|
|
110
117
|
runWorkers: boolean;
|
|
111
118
|
/** 内核 logger(`debug` 可选:插件 Logger 没有它,测试可不给)。 */
|
|
112
119
|
log: Logger & {
|
|
@@ -170,7 +177,7 @@ export interface ExtensionPointAccess {
|
|
|
170
177
|
plugin: string;
|
|
171
178
|
impl: unknown;
|
|
172
179
|
}[];
|
|
173
|
-
/** Names of the extensions enabled in the organization (
|
|
180
|
+
/** Names of the extensions enabled in the organization (the org intent projection, `enabled.ts`); queried every time, not cached. */
|
|
174
181
|
enabledPlugins(orgId: string): Promise<Set<string>>;
|
|
175
182
|
/**
|
|
176
183
|
* Whether the extension's effective config is valid in the organization (layering spec A §6):
|
package/dist/ctx/create-ctx.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { CtxReadError, DEVICE_PROVIDER_EXTENSION_POINT, DeviceAccessError, EXTENSION_POINTS, ExtensionPermissionError, ExtensionPointUnavailableError, FILE_PREVIEW_EXTENSION_POINT, InboxError, OBJECT_STORE_EXTENSION_POINT, parseExtensionPoint, resolveUiSlots, SETUP_CHECK_ID_RE, SETUP_GUIDE_PLACES, TriggerError, validateFilePreviewProvider, validateToolSpec, } from "@capybara-hq/plugin-sdk";
|
|
2
|
-
import { Command, ConnectionBuilder, ControlledTransactionBuilder, Kysely, TransactionBuilder, } from "kysely";
|
|
1
|
+
import { CONVERSATION_TARGET_EXTENSION_POINT, CtxReadError, DEVICE_PROVIDER_EXTENSION_POINT, DeviceAccessError, EXTENSION_POINTS, ExtensionPermissionError, ExtensionPointUnavailableError, FILE_PREVIEW_EXTENSION_POINT, InboxError, OBJECT_STORE_EXTENSION_POINT, parseExtensionPoint, resolveUiSlots, SETUP_CHECK_ID_RE, SETUP_GUIDE_PLACES, TriggerError, validateFilePreviewProvider, validateToolSpec, } from "@capybara-hq/plugin-sdk";
|
|
3
2
|
import { conversationTopic } from "../conversations/event-log.js";
|
|
3
|
+
import { checkExternalText } from "../conversations/external/external-calls.js";
|
|
4
4
|
import { pluginSchemaName } from "../db/extension-db.js";
|
|
5
5
|
import { kernelDb } from "../db/kernel-schema.js";
|
|
6
6
|
import { ApiError } from "../errors.js";
|
|
@@ -8,7 +8,7 @@ import { enabledExtensionsQuery } from "../extensions/enabled.js";
|
|
|
8
8
|
import { checkHookRegistration } from "../extensions/hooks.js";
|
|
9
9
|
import { RegistrationLedger } from "../extensions/ledger.js";
|
|
10
10
|
import { audit } from "../identity/audit.js";
|
|
11
|
-
import { appendEffect
|
|
11
|
+
import { appendEffect } from "../infra/database/extension-handoff.js";
|
|
12
12
|
import { currentScope, withOrgScope } from "../infra/database/org-scope.js";
|
|
13
13
|
import { can } from "../orgs/roles.js";
|
|
14
14
|
import { PrefixedStorage } from "../storage/storage.service.js";
|
|
@@ -16,6 +16,7 @@ import { canUseSlot, orgUiTopic, PLATFORM_UI_TOPIC, PluginUiRegistration, } from
|
|
|
16
16
|
import { createBus } from "./bus.js";
|
|
17
17
|
import { checkDeviceProvider } from "./device-provider-check.js";
|
|
18
18
|
import { assertEffectKey, } from "./durable-effects.service.js";
|
|
19
|
+
import { guardDb } from "./guard-db.js";
|
|
19
20
|
import { createKv } from "./kv.js";
|
|
20
21
|
import { createLock } from "./lock.js";
|
|
21
22
|
import { createExtensionNet } from "./net.js";
|
|
@@ -149,64 +150,6 @@ async function recordDenial(deps, plugin, item) {
|
|
|
149
150
|
deps.log.warn("plugin permission denial audit failed", { plugin, item, err: e });
|
|
150
151
|
}
|
|
151
152
|
}
|
|
152
|
-
/**
|
|
153
|
-
* 包装插件拿到的 Kysely:`destroy`(及 `Symbol.asyncDispose`)换成抛错的函数(连接池归内核所有,插件销毁会拖垮整个进程的 DB 访问),
|
|
154
|
-
* 其余属性透传。方法绑定到原对象(Kysely 用私有字段,以 Proxy 作 this 调用会失败)。
|
|
155
|
-
* 递归包裹一切能再拿到 Kysely 的路径,使 `destroy` 在任何路径都被拦截:
|
|
156
|
-
* - 方法返回的新 Kysely(`withSchema` / `withPlugin` 等);
|
|
157
|
-
* - 方法返回的构建器(`transaction()` / `connection()` / `startTransaction()` 及其链式方法、
|
|
158
|
-
* 受控事务的 `Command`):其方法收到的回调拿到的 Kysely(`transaction().execute(cb)`、
|
|
159
|
-
* `connection().execute(cb)`)与返回的 Promise 结果(`startTransaction().execute()` 得到的受控事务)。
|
|
160
|
-
*/
|
|
161
|
-
function guardDb(db, plugin, transactions) {
|
|
162
|
-
const guarded = new Proxy(db, {
|
|
163
|
-
get(target, prop) {
|
|
164
|
-
// `await using` 经 Symbol.asyncDispose 调用 destroy,同样拦截
|
|
165
|
-
if (prop === "destroy" || prop === Symbol.asyncDispose) {
|
|
166
|
-
return () => {
|
|
167
|
-
throw new Error(`plugin ${plugin}: ctx.db.destroy() is not allowed; the kernel owns the connection pool`);
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
const value = Reflect.get(target, prop, target);
|
|
171
|
-
if (typeof value !== "function")
|
|
172
|
-
return value;
|
|
173
|
-
return (...args) => guardValue(value.apply(target, args), plugin, transactions);
|
|
174
|
-
},
|
|
175
|
-
});
|
|
176
|
-
if (isExtensionTransaction(db))
|
|
177
|
-
transactions.set(guarded, db);
|
|
178
|
-
return guarded;
|
|
179
|
-
}
|
|
180
|
-
/** 能(经回调或返回值)再拿到 Kysely 的构建器类。 */
|
|
181
|
-
const BUILDERS = [ConnectionBuilder, TransactionBuilder, ControlledTransactionBuilder, Command];
|
|
182
|
-
const isBuilder = (v) => BUILDERS.some((B) => v instanceof B);
|
|
183
|
-
/** Kysely → guardDb;构建器 → guardBuilder;其余原样返回。 */
|
|
184
|
-
function guardValue(v, plugin, transactions) {
|
|
185
|
-
if (v instanceof Kysely)
|
|
186
|
-
return guardDb(v, plugin, transactions);
|
|
187
|
-
if (isBuilder(v))
|
|
188
|
-
return guardBuilder(v, plugin, transactions);
|
|
189
|
-
return v;
|
|
190
|
-
}
|
|
191
|
-
/** 包装构建器:回调参数里的 Kysely、返回值与 Promise 结果都经 guardValue。 */
|
|
192
|
-
function guardBuilder(builder, plugin, transactions) {
|
|
193
|
-
return new Proxy(builder, {
|
|
194
|
-
get(target, prop) {
|
|
195
|
-
const value = Reflect.get(target, prop, target);
|
|
196
|
-
if (typeof value !== "function")
|
|
197
|
-
return value;
|
|
198
|
-
return (...args) => {
|
|
199
|
-
const wrapped = args.map((a) => typeof a === "function"
|
|
200
|
-
? (...cbArgs) => a(...cbArgs.map((c) => guardValue(c, plugin, transactions)))
|
|
201
|
-
: a);
|
|
202
|
-
const out = guardValue(value.apply(target, wrapped), plugin, transactions);
|
|
203
|
-
return out instanceof Promise
|
|
204
|
-
? out.then((r) => guardValue(r, plugin, transactions))
|
|
205
|
-
: out;
|
|
206
|
-
};
|
|
207
|
-
},
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
153
|
/** 未注入能力访问时(单独构造 ctx 的测试等):没有任何提供方,组织启用照常查库。 */
|
|
211
154
|
const noProviders = (deps) => ({
|
|
212
155
|
providers: () => [],
|
|
@@ -644,9 +587,23 @@ identity = { sha256: null }) {
|
|
|
644
587
|
},
|
|
645
588
|
}
|
|
646
589
|
: undefined;
|
|
647
|
-
|
|
590
|
+
// bySubject needs `permissions.conversations`; the external-conversation methods belong to a
|
|
591
|
+
// `conversation-target@1` provider, on its own conversations only (the backend checks that and
|
|
592
|
+
// that it serves the org); every call runs in the named org's scope
|
|
593
|
+
const providesTarget = manifest.provides.includes(CONVERSATION_TARGET_EXTENSION_POINT);
|
|
594
|
+
const externalBackend = () => {
|
|
595
|
+
if (!providesTarget)
|
|
596
|
+
deny(`provides ${CONVERSATION_TARGET_EXTENSION_POINT}`);
|
|
597
|
+
const b = deps.externalConversations?.();
|
|
598
|
+
if (!b)
|
|
599
|
+
throw new Error("External conversations are not available in this process.");
|
|
600
|
+
return b;
|
|
601
|
+
};
|
|
602
|
+
const conversationsApi = perms.conversations || providesTarget
|
|
648
603
|
? {
|
|
649
604
|
bySubject: async (args) => {
|
|
605
|
+
if (!perms.conversations)
|
|
606
|
+
deny("permissions.conversations");
|
|
650
607
|
const a = readArgs(args);
|
|
651
608
|
const s = a.subject;
|
|
652
609
|
if (typeof s !== "object" ||
|
|
@@ -659,6 +616,34 @@ identity = { sha256: null }) {
|
|
|
659
616
|
const subject = { kind: s.kind, id: s.id };
|
|
660
617
|
return withOrgScope(a.orgId, () => readsBackend().bySubject(name, a.orgId, subject));
|
|
661
618
|
},
|
|
619
|
+
appendExternal: async (args) => {
|
|
620
|
+
const b = externalBackend();
|
|
621
|
+
const a = readArgs(args);
|
|
622
|
+
return withOrgScope(a.orgId, () => b.appendExternal(name, {
|
|
623
|
+
orgId: a.orgId,
|
|
624
|
+
conversationId: a.conversationId,
|
|
625
|
+
events: a.events,
|
|
626
|
+
}));
|
|
627
|
+
},
|
|
628
|
+
setExternalState: async (args) => {
|
|
629
|
+
const b = externalBackend();
|
|
630
|
+
const a = readArgs(args);
|
|
631
|
+
return withOrgScope(a.orgId, () => b.setExternalState(name, {
|
|
632
|
+
orgId: a.orgId,
|
|
633
|
+
conversationId: a.conversationId,
|
|
634
|
+
state: a.state,
|
|
635
|
+
}));
|
|
636
|
+
},
|
|
637
|
+
streamExternalText: (args) => {
|
|
638
|
+
const b = externalBackend();
|
|
639
|
+
// checked now (a wrong call throws); the publish is a hint, its failure only logged
|
|
640
|
+
const a = checkExternalText(args);
|
|
641
|
+
void withOrgScope(a.orgId, () => b.streamExternalText(name, a)).catch((err) => deps.log.warn("external partial text failed", {
|
|
642
|
+
plugin: name,
|
|
643
|
+
conversationId: a.conversationId,
|
|
644
|
+
err,
|
|
645
|
+
}));
|
|
646
|
+
},
|
|
662
647
|
}
|
|
663
648
|
: undefined;
|
|
664
649
|
const peopleApi = perms.people
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Kysely } from "kysely";
|
|
2
|
+
/**
|
|
3
|
+
* Protects the owned pool and records the underlying transactions used for durable effects.
|
|
4
|
+
* Kysely methods use private fields, so they are always called on the underlying object.
|
|
5
|
+
*/
|
|
6
|
+
export declare function guardDb(db: Kysely<unknown>, plugin: string, transactions: WeakMap<object, Kysely<unknown>>): Kysely<unknown>;
|