@capybara-hq/kernel 0.1.0-rc.104 → 0.1.0-rc.106
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/agents/agents.schemas.d.ts +9 -9
- package/dist/{schedules/schedules.controller.d.ts → automations/automations.controller.d.ts} +37 -32
- package/dist/{schedules/schedules.controller.js → automations/automations.controller.js} +68 -57
- package/dist/automations/automations.module.d.ts +16 -0
- package/dist/{schedules/schedules.module.js → automations/automations.module.js} +18 -15
- package/dist/{schedules/schedules.schemas.d.ts → automations/automations.schemas.d.ts} +151 -50
- package/dist/automations/automations.schemas.js +253 -0
- package/dist/{schedules/scheduled-tasks.service.d.ts → automations/automations.service.d.ts} +99 -52
- package/dist/{schedules/scheduled-tasks.service.js → automations/automations.service.js} +303 -134
- package/dist/{schedules → automations}/calendar.d.ts +8 -8
- package/dist/{schedules → automations}/calendar.js +2 -2
- package/dist/automations/effective-settings.d.ts +28 -0
- package/dist/automations/effective-settings.js +25 -0
- package/dist/automations/errors.d.ts +19 -0
- package/dist/automations/errors.js +19 -0
- package/dist/automations/events/coalesce.d.ts +28 -0
- package/dist/automations/events/coalesce.js +131 -0
- package/dist/automations/events/deadline.d.ts +10 -0
- package/dist/automations/events/deadline.js +24 -0
- package/dist/automations/events/dispatcher.d.ts +74 -0
- package/dist/automations/events/dispatcher.js +353 -0
- package/dist/automations/events/events.module.d.ts +7 -0
- package/dist/automations/events/events.module.js +26 -0
- package/dist/automations/events/guards.d.ts +32 -0
- package/dist/automations/events/guards.js +87 -0
- package/dist/automations/events/sweep.job.d.ts +49 -0
- package/dist/automations/events/sweep.job.js +202 -0
- package/dist/automations/events/token.d.ts +5 -0
- package/dist/automations/events/token.js +5 -0
- package/dist/automations/events/trigger-events.service.d.ts +47 -0
- package/dist/automations/events/trigger-events.service.js +254 -0
- package/dist/{schedules → automations}/fire.d.ts +57 -17
- package/dist/{schedules → automations}/fire.js +311 -79
- package/dist/{schedules → automations}/grant-rules.d.ts +1 -1
- package/dist/{schedules → automations}/grant-rules.js +5 -5
- package/dist/automations/job-ids.d.ts +113 -0
- package/dist/automations/job-ids.js +117 -0
- package/dist/{schedules → automations}/pause.d.ts +3 -3
- package/dist/{schedules → automations}/pause.js +3 -3
- package/dist/{schedules → automations}/run-tracker.d.ts +21 -10
- package/dist/{schedules → automations}/run-tracker.js +68 -56
- package/dist/{schedules → automations}/scan.job.d.ts +12 -8
- package/dist/{schedules → automations}/scan.job.js +39 -23
- package/dist/{schedules → automations}/schedule-math.d.ts +19 -12
- package/dist/{schedules → automations}/schedule-math.js +17 -7
- package/dist/{schedules → automations}/store.d.ts +57 -27
- package/dist/{schedules → automations}/store.js +102 -52
- package/dist/automations/store.module.d.ts +6 -0
- package/dist/{schedules → automations}/store.module.js +9 -9
- package/dist/automations/types.d.ts +178 -0
- package/dist/{schedules → automations}/unattended.d.ts +22 -11
- package/dist/{schedules → automations}/unattended.js +53 -30
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/channels.module.js +3 -3
- package/dist/channels/outbound.d.ts +5 -5
- package/dist/channels/outbound.js +17 -17
- package/dist/channels/threads.d.ts +1 -1
- package/dist/channels/threads.js +1 -1
- package/dist/config/schema.d.ts +5 -1
- package/dist/config/schema.js +44 -6
- package/dist/conversations/approval-profile.d.ts +2 -2
- package/dist/conversations/approval-profile.js +3 -3
- package/dist/conversations/conversations.controller.d.ts +7 -1
- package/dist/conversations/conversations.controller.js +26 -10
- package/dist/conversations/conversations.service.d.ts +103 -25
- package/dist/conversations/conversations.service.js +138 -33
- package/dist/conversations/events.d.ts +48 -1
- package/dist/conversations/events.js +61 -9
- package/dist/conversations/message-context.d.ts +2 -2
- package/dist/conversations/message-context.js +1 -1
- package/dist/conversations/permission-profile-store.d.ts +2 -1
- package/dist/conversations/permission-profiles.controller.d.ts +3 -0
- package/dist/conversations/permission-profiles.service.js +1 -1
- package/dist/conversations/prompt-suggestion-request.d.ts +1 -1
- package/dist/conversations/prompt-suggestion-request.js +2 -1
- package/dist/conversations/queue.d.ts +7 -4
- package/dist/conversations/queue.js +6 -2
- package/dist/conversations/queue.service.d.ts +5 -5
- package/dist/conversations/queue.service.js +20 -14
- package/dist/ctx/create-ctx.d.ts +20 -1
- package/dist/ctx/create-ctx.js +59 -2
- package/dist/db/kernel-schema.d.ts +104 -25
- package/dist/devices/device-project-files.service.d.ts +88 -0
- package/dist/devices/device-project-files.service.js +249 -0
- package/dist/devices/device-projects.service.d.ts +40 -0
- package/dist/devices/device-projects.service.js +16 -13
- package/dist/devices/devices.controller.d.ts +9 -3
- package/dist/devices/devices.controller.js +27 -5
- package/dist/devices/devices.module.js +9 -1
- package/dist/devices/devices.schemas.js +6 -1
- package/dist/devices/dto.d.ts +15 -0
- package/dist/devices/dto.js +11 -0
- package/dist/devices/project-paths.d.ts +29 -0
- package/dist/devices/project-paths.js +66 -0
- package/dist/devices/project-rules.d.ts +5 -1
- package/dist/devices/project-rules.js +5 -1
- package/dist/errors.d.ts +47 -13
- package/dist/errors.js +47 -13
- package/dist/evals/reconstruct.d.ts +10 -1
- package/dist/evals/reconstruct.js +7 -0
- package/dist/extensions/host.d.ts +6 -1
- package/dist/extensions/host.js +10 -0
- package/dist/extensions/permission-set.d.ts +17 -1
- package/dist/extensions/permission-set.js +13 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/git/gateway.js +2 -2
- package/dist/handoff/app-schemes.d.ts +28 -0
- package/dist/handoff/app-schemes.js +41 -0
- package/dist/handoff/handoff-paths.d.ts +26 -0
- package/dist/handoff/handoff-paths.js +25 -0
- package/dist/handoff/handoff-tickets.d.ts +28 -0
- package/dist/handoff/handoff-tickets.js +111 -0
- package/dist/handoff/handoff.controller.d.ts +24 -0
- package/dist/handoff/handoff.controller.js +74 -0
- package/dist/handoff/handoff.module.d.ts +7 -0
- package/dist/handoff/handoff.module.js +26 -0
- package/dist/handoff/handoff.schemas.d.ts +13 -0
- package/dist/handoff/handoff.schemas.js +3 -0
- package/dist/handoff/handoff.service.d.ts +49 -0
- package/dist/handoff/handoff.service.js +96 -0
- package/dist/http/api.module.js +13 -5
- package/dist/http/meta.controller.d.ts +1 -0
- package/dist/http/meta.controller.js +7 -0
- package/dist/http/request-signal.d.ts +8 -0
- package/dist/http/request-signal.js +14 -0
- package/dist/identity/sso.controller.d.ts +30 -6
- package/dist/identity/sso.controller.js +51 -17
- package/dist/identity/sso.d.ts +10 -2
- package/dist/identity/sso.js +15 -3
- package/dist/inbox/inbox.module.js +2 -2
- package/dist/inbox/inbox.service.d.ts +8 -8
- package/dist/inbox/inbox.service.js +7 -7
- package/dist/infra/config/kernel-config.service.js +10 -5
- package/dist/infra/outbox/registry.d.ts +29 -2
- package/dist/infra/outbox/registry.js +30 -7
- package/dist/kernel.module.js +3 -0
- package/dist/orgs/roles.d.ts +1 -1
- package/dist/orgs/roles.js +3 -3
- package/dist/orgs/settings.controller.d.ts +1 -1
- package/dist/orgs/settings.service.d.ts +27 -11
- package/dist/orgs/settings.service.js +42 -24
- package/dist/overrides.d.ts +4 -4
- package/dist/overrides.js +1 -1
- package/dist/platform/exclusions.js +5 -0
- package/dist/platform/operation.js +2 -2
- package/dist/platform/ops/{schedules.ops.d.ts → automations.ops.d.ts} +4 -4
- package/dist/platform/ops/automations.ops.js +560 -0
- package/dist/platform/ops/connectors.ops.js +4 -2
- package/dist/platform/ops/devices.ops.d.ts +3 -1
- package/dist/platform/ops/devices.ops.js +77 -3
- package/dist/platform/ops/inbox.ops.js +4 -4
- package/dist/platform/ops/orgs.ops.js +1 -1
- package/dist/platform/ops/platform.ops.js +4 -4
- package/dist/platform/platform-ops.module.d.ts +2 -2
- package/dist/platform/platform-ops.module.js +5 -5
- package/dist/platform/route-schemas.js +3 -3
- package/dist/push/jobs.d.ts +3 -3
- package/dist/push/jobs.js +2 -2
- package/dist/push/preferences.service.js +1 -1
- package/dist/push/push-owed.d.ts +3 -3
- package/dist/push/push-owed.js +6 -6
- package/dist/push/push.schemas.d.ts +4 -4
- package/dist/push/push.schemas.js +3 -3
- package/dist/push/recipients.d.ts +4 -4
- package/dist/push/recipients.js +23 -23
- package/dist/push/texts.d.ts +3 -3
- package/dist/push/texts.js +2 -2
- package/dist/push/triggers.d.ts +1 -1
- package/dist/roles/worker.module.js +3 -3
- package/dist/runtime/compaction.service.d.ts +3 -1
- package/dist/runtime/compaction.service.js +3 -1
- package/dist/runtime/context/build.d.ts +6 -0
- package/dist/runtime/context/build.js +14 -3
- package/dist/runtime/context/compaction.d.ts +27 -1
- package/dist/runtime/context/compaction.js +102 -37
- package/dist/runtime/context/run-context.d.ts +38 -0
- package/dist/runtime/context/run-context.js +54 -0
- package/dist/runtime/maintenance.d.ts +8 -4
- package/dist/runtime/maintenance.js +37 -16
- package/dist/runtime/model-call.d.ts +6 -4
- package/dist/runtime/model-call.js +17 -7
- package/dist/runtime/project/knowledge.service.d.ts +16 -7
- package/dist/runtime/project/knowledge.service.js +31 -13
- package/dist/runtime/run-finish.d.ts +4 -4
- package/dist/runtime/run-finish.js +3 -3
- package/dist/runtime/run-queue.js +12 -7
- package/dist/runtime/run-worker.js +7 -1
- package/dist/runtime/runtime.module.js +3 -3
- package/dist/runtime/subagents/dispatch.js +11 -3
- package/dist/sandbox/browse-roots.d.ts +8 -0
- package/dist/sandbox/browse-roots.js +22 -0
- package/dist/sandbox/network-grants.d.ts +4 -4
- package/dist/sandbox/network-grants.js +7 -7
- package/dist/sandbox/operation-authority.d.ts +10 -3
- package/dist/sandbox/operation-authority.js +20 -10
- package/dist/sandbox/operation-intents.d.ts +3 -1
- package/dist/sandbox/operation-intents.js +11 -4
- package/dist/sandbox/permission-call-grants.js +2 -2
- package/dist/sandbox/permission-facts.js +1 -1
- package/dist/sandbox/review/classifier.d.ts +2 -2
- package/dist/sandbox/review/classifier.js +22 -11
- package/dist/sandbox/review/permission-candidate.d.ts +1 -1
- package/dist/sandbox/review/permission-candidate.js +1 -1
- package/dist/sandbox/sandbox.service.d.ts +15 -5
- package/dist/sandbox/sandbox.service.js +14 -8
- package/dist/storage/purge.d.ts +1 -1
- package/dist/storage/purge.js +2 -2
- package/dist/tools/approvals.service.d.ts +6 -6
- package/dist/tools/approvals.service.js +19 -19
- package/dist/tools/builtin-names.d.ts +12 -12
- package/dist/tools/builtin-names.js +29 -29
- package/dist/tools/builtins/{schedules.d.ts → automations.d.ts} +35 -33
- package/dist/tools/builtins/{schedules.js → automations.js} +236 -105
- package/dist/tools/builtins/builtin-tools.d.ts +8 -8
- package/dist/tools/builtins/builtin-tools.js +16 -16
- package/dist/tools/call-scope.d.ts +1 -1
- package/dist/tools/connectors.service.d.ts +4 -13
- package/dist/tools/connectors.service.js +12 -21
- package/dist/tools/git-asks.js +1 -1
- package/dist/tools/git-push-policy.d.ts +3 -3
- package/dist/tools/git-push-policy.js +6 -6
- package/dist/tools/network-asks.d.ts +3 -3
- package/dist/tools/network-asks.js +3 -3
- package/dist/tools/oauth/flow.d.ts +16 -2
- package/dist/tools/oauth/flow.js +23 -4
- package/dist/tools/oauth/oauth.controller.d.ts +19 -7
- package/dist/tools/oauth/oauth.controller.js +42 -12
- package/dist/tools/oauth/oauth.schemas.d.ts +14 -1
- package/dist/tools/oauth/oauth.schemas.js +4 -1
- package/dist/tools/pipeline.d.ts +4 -4
- package/dist/tools/pipeline.js +2 -2
- package/dist/tools/policy.d.ts +3 -3
- package/dist/tools/policy.js +3 -3
- package/dist/tools/tools.module.js +9 -9
- package/package.json +3 -3
- package/src/migrations/0188_automations_rename.sql +309 -0
- package/src/migrations/0189_event_automations.sql +136 -0
- package/dist/platform/ops/schedules.ops.js +0 -509
- package/dist/schedules/effective-settings.d.ts +0 -22
- package/dist/schedules/effective-settings.js +0 -18
- package/dist/schedules/errors.d.ts +0 -8
- package/dist/schedules/errors.js +0 -8
- package/dist/schedules/job-ids.d.ts +0 -81
- package/dist/schedules/job-ids.js +0 -95
- package/dist/schedules/schedules.module.d.ts +0 -16
- package/dist/schedules/schedules.schemas.js +0 -213
- package/dist/schedules/store.module.d.ts +0 -6
- package/dist/schedules/types.d.ts +0 -110
- /package/dist/{schedules → automations}/types.js +0 -0
|
@@ -10,24 +10,24 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
10
10
|
import { ToolError } from "@capybara-hq/plugin-sdk";
|
|
11
11
|
import { Inject, Injectable } from "@nestjs/common";
|
|
12
12
|
import { z } from "zod";
|
|
13
|
+
import { readAutomationSettings } from "../../automations/effective-settings.js";
|
|
14
|
+
import { nextTick } from "../../automations/schedule-math.js";
|
|
15
|
+
import { AutomationStore } from "../../automations/store.js";
|
|
13
16
|
import { ApiError } from "../../errors.js";
|
|
14
17
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
15
18
|
import { TransactionHost } from "../../infra/database/transaction-host.js";
|
|
16
19
|
import { Transactional } from "../../infra/database/transactional.decorator.js";
|
|
17
|
-
import { readScheduleSettings } from "../../schedules/effective-settings.js";
|
|
18
|
-
import { nextTick } from "../../schedules/schedule-math.js";
|
|
19
|
-
import { ScheduledTaskStore } from "../../schedules/store.js";
|
|
20
20
|
import { BUILTIN } from "../builtin-names.js";
|
|
21
21
|
import { localTime } from "./current-time.js";
|
|
22
22
|
import { clip, display } from "./sandbox/common.js";
|
|
23
23
|
/**
|
|
24
|
-
* Where the
|
|
24
|
+
* Where the automation tools find the task service: a registration port. `AutomationsModule` imports
|
|
25
25
|
* `ToolsModule` (its grants resolve the agent's tools through `ToolRegistry`, which lists these
|
|
26
|
-
* builtins), so the service cannot be injected into the builtins; `
|
|
26
|
+
* builtins), so the service cannot be injected into the builtins; `AutomationsModule` binds it here
|
|
27
27
|
* from `onModuleInit` instead, as the domain operations register into `PlatformOperations`. Null
|
|
28
|
-
* until bound (an application without `
|
|
28
|
+
* until bound (an application without `AutomationsModule`).
|
|
29
29
|
*/
|
|
30
|
-
let
|
|
30
|
+
let AutomationToolsPort = class AutomationToolsPort {
|
|
31
31
|
service = null;
|
|
32
32
|
bind(service) {
|
|
33
33
|
this.service = service;
|
|
@@ -36,10 +36,10 @@ let ScheduleToolsPort = class ScheduleToolsPort {
|
|
|
36
36
|
return this.service;
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
|
-
|
|
39
|
+
AutomationToolsPort = __decorate([
|
|
40
40
|
Injectable()
|
|
41
|
-
],
|
|
42
|
-
export {
|
|
41
|
+
], AutomationToolsPort);
|
|
42
|
+
export { AutomationToolsPort };
|
|
43
43
|
const NAME_MAX = 200;
|
|
44
44
|
const PROMPT_MAX = 20_000;
|
|
45
45
|
const TOOLS_MAX = 50;
|
|
@@ -51,7 +51,7 @@ const TOOL_KEYS_SCHEMA = {
|
|
|
51
51
|
items: { type: "string", minLength: 1, maxLength: 200 },
|
|
52
52
|
description: "Tool keys the task may use without asking when it runs unattended, e.g. " +
|
|
53
53
|
'["WebSearch", "mcp__docs__search"]. Only tools this agent has now; tools that always ask a ' +
|
|
54
|
-
"person (
|
|
54
|
+
"person (AutomationCreate, AutomationUpdate, ...) cannot be listed. A run that needs a tool not listed " +
|
|
55
55
|
"fails that call. The person sees and approves this list.",
|
|
56
56
|
};
|
|
57
57
|
const CRON_SCHEMA = {
|
|
@@ -71,6 +71,58 @@ const RUN_AT_SCHEMA = {
|
|
|
71
71
|
'offset, e.g. "2026-12-01T15:00". Call CurrentTime first to know the date and time now. ' +
|
|
72
72
|
"Pass cron or runAt, not both.",
|
|
73
73
|
};
|
|
74
|
+
const CREATE_MODE_SCHEMA = {
|
|
75
|
+
type: "string",
|
|
76
|
+
enum: ["new", "here", "chat", "subject"],
|
|
77
|
+
description: '"new": every run starts a new conversation with this agent. "here": every run posts the ' +
|
|
78
|
+
"prompt into this conversation (in an IM direct chat: the chat's current conversation), " +
|
|
79
|
+
'e.g. for "check this again at 3 pm". "chat": every run posts into the IM chat named by ' +
|
|
80
|
+
'chat. "subject": one conversation per event subject, reused for later events of the same ' +
|
|
81
|
+
'subject; only with an event trigger, which cannot use "chat".',
|
|
82
|
+
};
|
|
83
|
+
const EVENT_TRIGGER_SCHEMA = {
|
|
84
|
+
type: "object",
|
|
85
|
+
description: "Run when events happen instead of on a schedule. Pass trigger, or cron, or runAt: exactly one.",
|
|
86
|
+
properties: {
|
|
87
|
+
kind: { type: "string", enum: ["event"] },
|
|
88
|
+
source: {
|
|
89
|
+
type: "string",
|
|
90
|
+
minLength: 1,
|
|
91
|
+
maxLength: 214,
|
|
92
|
+
description: "The extension that emits the events, as the Platform operation automations.event-sources lists it.",
|
|
93
|
+
},
|
|
94
|
+
types: {
|
|
95
|
+
type: "array",
|
|
96
|
+
minItems: 1,
|
|
97
|
+
maxItems: 64,
|
|
98
|
+
items: { type: "string", minLength: 1, maxLength: 100 },
|
|
99
|
+
description: 'Event types of that source, e.g. ["item.created"].',
|
|
100
|
+
},
|
|
101
|
+
filter: {
|
|
102
|
+
type: "array",
|
|
103
|
+
maxItems: 20,
|
|
104
|
+
description: "Optional: keep only events whose facts match every clause. Filter only on filterable facts.",
|
|
105
|
+
items: {
|
|
106
|
+
type: "object",
|
|
107
|
+
properties: {
|
|
108
|
+
fact: { type: "string", minLength: 1, maxLength: 100 },
|
|
109
|
+
op: {
|
|
110
|
+
type: "string",
|
|
111
|
+
enum: ["eq", "in", "contains"],
|
|
112
|
+
description: "eq: the fact equals a scalar; in: a string fact is one of a list of strings; contains: a string[] fact contains a string.",
|
|
113
|
+
},
|
|
114
|
+
value: {
|
|
115
|
+
description: "A string, number or boolean, or a list of strings for in.",
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
required: ["fact", "op", "value"],
|
|
119
|
+
additionalProperties: false,
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
required: ["kind", "source", "types"],
|
|
124
|
+
additionalProperties: false,
|
|
125
|
+
};
|
|
74
126
|
const MODE_SCHEMA = {
|
|
75
127
|
type: "string",
|
|
76
128
|
enum: ["new", "here", "chat"],
|
|
@@ -111,9 +163,10 @@ const CHAT_SCHEMA = {
|
|
|
111
163
|
additionalProperties: false,
|
|
112
164
|
};
|
|
113
165
|
const CREATE_SPEC = {
|
|
114
|
-
name: BUILTIN.
|
|
115
|
-
description: "Create
|
|
116
|
-
"once at a time (runAt), in the user's time zone,
|
|
166
|
+
name: BUILTIN.AutomationCreate,
|
|
167
|
+
description: "Create an automation for the user: this agent runs the prompt on a schedule (cron), " +
|
|
168
|
+
"once at a time (runAt), in the user's time zone, or when events happen (trigger: an event " +
|
|
169
|
+
"source, types and an optional filter on their facts), acting as the user. Each run starts a new " +
|
|
117
170
|
"conversation, posts into this conversation, or posts into an IM chat the user names with " +
|
|
118
171
|
'chat. Use it when the user asks for something recurring or later ("every weekday at 9 ' +
|
|
119
172
|
'send me a brief", "remind me at 3 pm"). Every call asks the user to approve the whole task ' +
|
|
@@ -137,20 +190,27 @@ const CREATE_SPEC = {
|
|
|
137
190
|
},
|
|
138
191
|
cron: CRON_SCHEMA,
|
|
139
192
|
runAt: RUN_AT_SCHEMA,
|
|
140
|
-
|
|
193
|
+
trigger: EVENT_TRIGGER_SCHEMA,
|
|
194
|
+
mode: CREATE_MODE_SCHEMA,
|
|
141
195
|
chat: {
|
|
142
196
|
...CHAT_SCHEMA,
|
|
143
197
|
description: 'For mode "chat": the IM chat every run posts into.',
|
|
144
198
|
},
|
|
145
199
|
approvedTools: TOOL_KEYS_SCHEMA,
|
|
200
|
+
unattendedAsk: {
|
|
201
|
+
type: "string",
|
|
202
|
+
enum: ["fail", "wait"],
|
|
203
|
+
description: "What a question asked in an unattended run does: fail the call, or wait for the user. " +
|
|
204
|
+
"Omit to follow the organization's default.",
|
|
205
|
+
},
|
|
146
206
|
},
|
|
147
207
|
required: ["name", "prompt", "mode"],
|
|
148
208
|
additionalProperties: false,
|
|
149
209
|
},
|
|
150
210
|
};
|
|
151
211
|
const UPDATE_SPEC = {
|
|
152
|
-
name: BUILTIN.
|
|
153
|
-
description: "Change one of the user's
|
|
212
|
+
name: BUILTIN.AutomationUpdate,
|
|
213
|
+
description: "Change one of the user's automations: its name, prompt, schedule, where it runs (a new " +
|
|
154
214
|
"conversation, this conversation or an IM chat, and the agent) or its approved tools. Pass " +
|
|
155
215
|
"the task id and only the fields to change; approvedTools replaces the whole list. Every " +
|
|
156
216
|
"call asks the user to approve the change first. It does not pause, resume or delete a task.",
|
|
@@ -183,21 +243,21 @@ const UPDATE_SPEC = {
|
|
|
183
243
|
},
|
|
184
244
|
};
|
|
185
245
|
const LIST_SPEC = {
|
|
186
|
-
name: BUILTIN.
|
|
187
|
-
description: "List the user's
|
|
246
|
+
name: BUILTIN.AutomationList,
|
|
247
|
+
description: "List the user's automations in this organization: id, name, schedule with its time " +
|
|
188
248
|
"zone, status, next run and where runs go. Read-only.",
|
|
189
249
|
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
190
250
|
};
|
|
191
251
|
const TARGETS_SPEC = {
|
|
192
|
-
name: BUILTIN.
|
|
193
|
-
description: "List the IM chats
|
|
252
|
+
name: BUILTIN.AutomationTargets,
|
|
253
|
+
description: "List the IM chats automation runs can be delivered to for the user in this " +
|
|
194
254
|
"organization: each chat's connection id, chat id, chat type and the connection's agent id, " +
|
|
195
255
|
"with a label naming the connection. Read-only.",
|
|
196
256
|
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
197
257
|
};
|
|
198
258
|
const DELETE_SPEC = {
|
|
199
|
-
name: BUILTIN.
|
|
200
|
-
description: "Delete one of the user's
|
|
259
|
+
name: BUILTIN.AutomationDelete,
|
|
260
|
+
description: "Delete one of the user's automations for good: it never runs again; past conversations " +
|
|
201
261
|
"stay. Use it when the user asks to stop or remove a task. Only the user's own tasks can be " +
|
|
202
262
|
"deleted.",
|
|
203
263
|
keyArgs: ["id"],
|
|
@@ -220,15 +280,39 @@ const Chat = z
|
|
|
220
280
|
agentId: z.string().min(1).max(100).optional(),
|
|
221
281
|
})
|
|
222
282
|
.strict();
|
|
283
|
+
const EventTrigger = z
|
|
284
|
+
.object({
|
|
285
|
+
kind: z.literal("event"),
|
|
286
|
+
source: z.string().min(1).max(214),
|
|
287
|
+
types: z.array(z.string().min(1).max(100)).min(1).max(64),
|
|
288
|
+
filter: z
|
|
289
|
+
.array(z
|
|
290
|
+
.object({
|
|
291
|
+
fact: z.string().min(1).max(100),
|
|
292
|
+
op: z.enum(["eq", "in", "contains"]),
|
|
293
|
+
value: z.union([
|
|
294
|
+
z.string().max(500),
|
|
295
|
+
z.number(),
|
|
296
|
+
z.boolean(),
|
|
297
|
+
z.array(z.string().max(500)).max(100),
|
|
298
|
+
]),
|
|
299
|
+
})
|
|
300
|
+
.strict())
|
|
301
|
+
.max(20)
|
|
302
|
+
.optional(),
|
|
303
|
+
})
|
|
304
|
+
.strict();
|
|
223
305
|
const CreateInput = z
|
|
224
306
|
.object({
|
|
225
307
|
name: z.string().min(1).max(NAME_MAX),
|
|
226
308
|
prompt: z.string().min(1).max(PROMPT_MAX),
|
|
227
309
|
cron: z.string().min(1).max(100).optional(),
|
|
228
310
|
runAt: z.string().min(1).max(40).optional(),
|
|
229
|
-
|
|
311
|
+
trigger: EventTrigger.optional(),
|
|
312
|
+
mode: z.enum(["new", "here", "chat", "subject"]),
|
|
230
313
|
chat: Chat.optional(),
|
|
231
314
|
approvedTools: ToolKeys.optional(),
|
|
315
|
+
unattendedAsk: z.enum(["fail", "wait"]).optional(),
|
|
232
316
|
})
|
|
233
317
|
.strict();
|
|
234
318
|
const UpdateInput = z
|
|
@@ -245,54 +329,61 @@ const UpdateInput = z
|
|
|
245
329
|
.strict();
|
|
246
330
|
const IdInput = z.object({ id: z.string().min(1).max(100) }).strict();
|
|
247
331
|
// ---- model-facing texts (English, actionable) ----
|
|
248
|
-
const INVALID_CREATE_TEXT = "Invalid input; pass name, prompt, mode (new, here or
|
|
249
|
-
"
|
|
250
|
-
"as an array of tool keys
|
|
332
|
+
const INVALID_CREATE_TEXT = "Invalid input; pass name, prompt, mode (new, here, chat or subject), exactly one of cron, runAt " +
|
|
333
|
+
"or trigger (an event trigger: kind event, source, types, optional filter), chat with mode chat " +
|
|
334
|
+
"(connectionId, chatId, chatType, agentId), and optionally approvedTools as an array of tool keys " +
|
|
335
|
+
"and unattendedAsk (fail or wait).";
|
|
251
336
|
const INVALID_UPDATE_TEXT = "Invalid input; pass the task id and the fields to change (name, prompt, cron or runAt, mode, " +
|
|
252
337
|
"chat, approvedTools).";
|
|
253
|
-
const INVALID_ID_TEXT = "Invalid input; pass the task id from
|
|
254
|
-
const ONE_TRIGGER_TEXT = "Pass exactly one of cron (a repeating schedule)
|
|
338
|
+
const INVALID_ID_TEXT = "Invalid input; pass the task id from AutomationList.";
|
|
339
|
+
const ONE_TRIGGER_TEXT = "Pass exactly one of cron (a repeating schedule), runAt (a single run) or trigger (run on events), not several and not none.";
|
|
255
340
|
const BOTH_TRIGGERS_TEXT = "Pass at most one of cron or runAt: cron makes the task repeat, runAt makes it run once.";
|
|
256
341
|
const NOTHING_TO_UPDATE_TEXT = "Nothing to change; pass at least one of name, prompt, cron, runAt, mode, chat or " +
|
|
257
342
|
"approvedTools with the id.";
|
|
258
343
|
const CHAT_NEEDS_CHAT_TEXT = 'mode "chat" needs a chat; pass chat with connectionId and chatId (and optionally chatType and agentId).';
|
|
259
344
|
const CHAT_ONLY_TEXT = 'chat applies only to mode "chat"; pass mode "chat" with it, or omit it.';
|
|
260
|
-
export const
|
|
261
|
-
export const
|
|
345
|
+
export const AUTOMATIONS_UNAVAILABLE_TEXT = "Automations are not available on this server; ask the user to manage the task on the automations page instead.";
|
|
346
|
+
export const AUTOMATIONS_OFF_TEXT = "Automations are turned off in this organization; ask an org admin to turn them on.";
|
|
262
347
|
/** The card recomputed at run time differs from the one the person approved. */
|
|
263
348
|
export const cardChangedText = (tool) => `The task's time zone, target, tools or current settings changed after the user approved this ${tool} call, so nothing was changed; call ${tool} again so the user can approve the current version.`;
|
|
264
|
-
export const NOT_A_MEMBER_TEXT = "The user is no longer a member of this organization, so their
|
|
349
|
+
export const NOT_A_MEMBER_TEXT = "The user is no longer a member of this organization, so their automations cannot be managed here; ask them to check their membership.";
|
|
265
350
|
/** No task with that id is the person's own (missing, deleted, or another member's). */
|
|
266
|
-
export const notYoursText = (id) => `No
|
|
351
|
+
export const notYoursText = (id) => `No automation with id ${clip(id, 100)} is the user's own; call AutomationList to see the ids of the user's tasks.`;
|
|
267
352
|
/** The service's refusal as an actionable English tool error. */
|
|
268
|
-
export function
|
|
353
|
+
export function automationErrorText(e, ctx) {
|
|
269
354
|
const m = e.message.replace(/[.\s]+$/, "");
|
|
270
355
|
switch (e.code) {
|
|
271
|
-
case "
|
|
272
|
-
case "
|
|
356
|
+
case "automation.not_found":
|
|
357
|
+
case "automation.forbidden":
|
|
273
358
|
return notYoursText(ctx.id ?? "");
|
|
274
|
-
case "
|
|
275
|
-
return
|
|
276
|
-
case "
|
|
277
|
-
return `${m}. Call
|
|
278
|
-
case "
|
|
359
|
+
case "automation.disabled":
|
|
360
|
+
return AUTOMATIONS_OFF_TEXT;
|
|
361
|
+
case "automation.limit_reached":
|
|
362
|
+
return `${m}. Call AutomationList and ask the user which task to delete with AutomationDelete.`;
|
|
363
|
+
case "automation.interval_too_short":
|
|
279
364
|
return `${m}. Use a less frequent cron expression, or ask an org admin to lower the minimum interval.`;
|
|
280
|
-
case "
|
|
365
|
+
case "automation.invalid_cron":
|
|
281
366
|
return `${m}. Pass 5 fields (minute hour day-of-month month day-of-week), e.g. "0 9 * * 1-5" for 09:00 on weekdays.`;
|
|
282
|
-
case "
|
|
367
|
+
case "automation.invalid_once_time":
|
|
283
368
|
return "runAt is not a local date-time; pass YYYY-MM-DDTHH:mm in the user's time zone without an offset, e.g. 2026-12-01T09:00.";
|
|
284
|
-
case "
|
|
369
|
+
case "automation.once_in_past":
|
|
285
370
|
return `runAt is not in the future in the user's time zone (${ctx.zone ?? "UTC"}); call CurrentTime and pass a later time.`;
|
|
286
|
-
case "
|
|
371
|
+
case "automation.invalid_time_zone":
|
|
287
372
|
return `${m}. Ask the user to set their time zone in their profile, then retry.`;
|
|
288
|
-
case "
|
|
373
|
+
case "automation.target_invalid":
|
|
289
374
|
return `${m}. Fix the target and retry, or use mode "new" to start a new conversation for every run instead.`;
|
|
290
|
-
case "
|
|
375
|
+
case "automation.event_source_unavailable":
|
|
376
|
+
return `${m}. Ask the user which source to use, or use a schedule (cron or runAt) instead.`;
|
|
377
|
+
case "automation.event_type_unknown":
|
|
378
|
+
return `${m}. Pass event types of that source only, and do not use directed types.`;
|
|
379
|
+
case "automation.filter_invalid":
|
|
380
|
+
return `${m}. Filter only on filterable facts of every selected event type, with a value of the fact's type (eq: a scalar, in: strings on a string fact, contains: a string on a string[] fact), or drop the filter.`;
|
|
381
|
+
case "automation.grant_not_allowed":
|
|
291
382
|
return `${m}. Remove it from approvedTools and call again.`;
|
|
292
383
|
case "forbidden":
|
|
293
|
-
return "The user's role in this organization cannot manage
|
|
384
|
+
return "The user's role in this organization cannot manage automations (guests cannot); ask an org admin.";
|
|
294
385
|
case "conflict":
|
|
295
|
-
return `${m}. Check the task with
|
|
386
|
+
return `${m}. Check the task with AutomationList before trying again.`;
|
|
296
387
|
default:
|
|
297
388
|
return `${m}. Check the arguments and retry.`;
|
|
298
389
|
}
|
|
@@ -303,6 +394,10 @@ const DAYS_ZH = ["周日", "周一", "周二", "周三", "周四", "周五", "
|
|
|
303
394
|
const INT = /^\d{1,2}$/;
|
|
304
395
|
/** A trigger in words, its zone included; an unusual cron reads as the expression itself. */
|
|
305
396
|
export function scheduleWords(t) {
|
|
397
|
+
if (t.kind === "event") {
|
|
398
|
+
const types = t.types.join(", ");
|
|
399
|
+
return { zh: `${t.source} 事件(${types})发生时`, en: `on ${t.source} events (${types})` };
|
|
400
|
+
}
|
|
306
401
|
const zone = t.timeZone;
|
|
307
402
|
if (t.kind === "once") {
|
|
308
403
|
const at = t.at.replace("T", " ").slice(0, 16);
|
|
@@ -339,12 +434,17 @@ export function scheduleWords(t) {
|
|
|
339
434
|
}
|
|
340
435
|
/** The next fire time in the trigger's zone, `YYYY-MM-DD HH:mm`, or null when there is none. */
|
|
341
436
|
function nextRunWords(t, now) {
|
|
437
|
+
if (t.kind === "event")
|
|
438
|
+
return null;
|
|
342
439
|
const next = nextTick(t, new Date(now));
|
|
343
440
|
return next === null ? null : localTime(next, t.timeZone).local.slice(0, 16).replace("T", " ");
|
|
344
441
|
}
|
|
345
442
|
function targetWords(t, currentConversationId) {
|
|
346
443
|
if (t.mode === "new")
|
|
347
444
|
return { zh: "每次运行新建对话", en: "a new conversation for every run" };
|
|
445
|
+
if (t.mode === "subject") {
|
|
446
|
+
return { zh: "每个事项一条会话", en: "one conversation per event subject" };
|
|
447
|
+
}
|
|
348
448
|
if (t.mode === "chat") {
|
|
349
449
|
const n = clip(t.connectionName, 60);
|
|
350
450
|
return t.chat.chatType === "group"
|
|
@@ -355,6 +455,14 @@ function targetWords(t, currentConversationId) {
|
|
|
355
455
|
? { zh: "当前对话", en: "this conversation" }
|
|
356
456
|
: { zh: `对话 ${t.conversationId}`, en: `conversation ${t.conversationId}` };
|
|
357
457
|
}
|
|
458
|
+
/** An event filter as `fact op value` clauses (the same text in both languages); null when empty. */
|
|
459
|
+
function filterWords(filter) {
|
|
460
|
+
if (filter.length === 0)
|
|
461
|
+
return null;
|
|
462
|
+
return filter
|
|
463
|
+
.map((c) => `${clip(c.fact, 60)} ${c.op} ${clip(JSON.stringify(c.value), 120)}`)
|
|
464
|
+
.join(" AND ");
|
|
465
|
+
}
|
|
358
466
|
function toolsWords(keys) {
|
|
359
467
|
return keys.length === 0
|
|
360
468
|
? { zh: "无", en: "none" }
|
|
@@ -420,7 +528,9 @@ const targetFields = (t) => t.mode === "chat"
|
|
|
420
528
|
? { mode: "chat", chat: t.chat }
|
|
421
529
|
: t.mode === "conversation"
|
|
422
530
|
? { mode: "conversation", conversationId: t.conversationId }
|
|
423
|
-
:
|
|
531
|
+
: t.mode === "subject"
|
|
532
|
+
? { mode: "subject" }
|
|
533
|
+
: { mode: "new" };
|
|
424
534
|
const sameTarget = (a, b) => a.mode === b.mode &&
|
|
425
535
|
(a.mode !== "conversation" ||
|
|
426
536
|
a.conversationId === b.conversationId) &&
|
|
@@ -429,14 +539,20 @@ const sameTarget = (a, b) => a.mode === b.mode &&
|
|
|
429
539
|
a.chat.chatId === b.chat.chatId &&
|
|
430
540
|
a.chat.chatType === b.chat.chatType));
|
|
431
541
|
const uniqueSorted = (keys) => [...new Set(keys)].sort();
|
|
432
|
-
/** cron / runAt as the service's trigger; undefined when
|
|
542
|
+
/** cron / runAt / an event trigger as the service's trigger; undefined when none is given. */
|
|
433
543
|
function triggerOf(i) {
|
|
544
|
+
if (i.trigger !== undefined) {
|
|
545
|
+
const { filter, ...rest } = i.trigger;
|
|
546
|
+
return { ...rest, ...(filter !== undefined && { filter }) };
|
|
547
|
+
}
|
|
434
548
|
if (i.cron !== undefined)
|
|
435
549
|
return { kind: "cron", expr: i.cron.trim() };
|
|
436
550
|
if (i.runAt !== undefined)
|
|
437
551
|
return { kind: "once", at: i.runAt.trim() };
|
|
438
552
|
return undefined;
|
|
439
553
|
}
|
|
554
|
+
/** How many of cron, runAt and trigger are given (exactly one is valid). */
|
|
555
|
+
const triggerCount = (i) => [i.cron, i.runAt, i.trigger].filter((v) => v !== undefined).length;
|
|
440
556
|
/** `chat` against the mode on create, as a refusal; null when they agree. */
|
|
441
557
|
function createChatError(i) {
|
|
442
558
|
if (i.mode !== "chat" && i.chat !== undefined)
|
|
@@ -451,9 +567,11 @@ function updateChatError(i) {
|
|
|
451
567
|
return null;
|
|
452
568
|
return i.mode !== "chat" ? new ToolError(CHAT_ONLY_TEXT) : null;
|
|
453
569
|
}
|
|
454
|
-
const withZone = (t, timeZone) => t.kind === "
|
|
455
|
-
? { kind: "
|
|
456
|
-
:
|
|
570
|
+
const withZone = (t, timeZone) => t.kind === "event"
|
|
571
|
+
? { kind: "event", source: t.source, types: t.types, filter: t.filter ?? [] }
|
|
572
|
+
: t.kind === "cron"
|
|
573
|
+
? { kind: "cron", expr: t.expr, timeZone }
|
|
574
|
+
: { kind: "once", at: t.at, timeZone };
|
|
457
575
|
/** A card as stored on the approval (the same clipping at ask time and at run time). */
|
|
458
576
|
const cardOf = (w) => ({ zh: clip(w.zh, 1000), en: clip(w.en, 1000) });
|
|
459
577
|
/** The fixed ask of a create or update: every call asks, only "once", with the kernel's card. */
|
|
@@ -480,11 +598,11 @@ async function roleOf(db, orgId, accountId) {
|
|
|
480
598
|
return (m?.role ?? null);
|
|
481
599
|
}
|
|
482
600
|
/**
|
|
483
|
-
* `
|
|
601
|
+
* `AutomationUpdate`'s write in one transaction: the field change, then (with `approvedTools`) the
|
|
484
602
|
* task's grants replaced by exactly that list. Each service call is `@Transactional` and joins
|
|
485
603
|
* this one, so a refused grant rolls the field change back too.
|
|
486
604
|
*/
|
|
487
|
-
let
|
|
605
|
+
let AutomationUpdates = class AutomationUpdates {
|
|
488
606
|
txHost;
|
|
489
607
|
db;
|
|
490
608
|
store;
|
|
@@ -511,38 +629,38 @@ let ScheduleUpdates = class ScheduleUpdates {
|
|
|
511
629
|
};
|
|
512
630
|
__decorate([
|
|
513
631
|
Transactional()
|
|
514
|
-
],
|
|
515
|
-
|
|
632
|
+
], AutomationUpdates.prototype, "apply", null);
|
|
633
|
+
AutomationUpdates = __decorate([
|
|
516
634
|
Injectable(),
|
|
517
635
|
__param(0, Inject(TransactionHost)),
|
|
518
636
|
__param(1, Inject(KernelDb)),
|
|
519
|
-
__param(2, Inject(
|
|
520
|
-
],
|
|
521
|
-
export {
|
|
522
|
-
/** The five
|
|
523
|
-
export function
|
|
637
|
+
__param(2, Inject(AutomationStore))
|
|
638
|
+
], AutomationUpdates);
|
|
639
|
+
export { AutomationUpdates };
|
|
640
|
+
/** The five automation builtins. */
|
|
641
|
+
export function automationTools(deps) {
|
|
524
642
|
/**
|
|
525
643
|
* The service, or a refusal (the tools are hidden then too: {@link unavailable}). Defensive:
|
|
526
|
-
* `
|
|
644
|
+
* `AutomationsModule` is reachable on every role (through ChannelsModule / KernelModule), so the
|
|
527
645
|
* port is expected to be bound; the path stays for an application built without it.
|
|
528
646
|
*/
|
|
529
647
|
const service = async () => {
|
|
530
648
|
const s = deps.service();
|
|
531
649
|
if (s === null)
|
|
532
|
-
throw new ToolError(
|
|
650
|
+
throw new ToolError(AUTOMATIONS_UNAVAILABLE_TEXT);
|
|
533
651
|
return s;
|
|
534
652
|
};
|
|
535
653
|
/** Not loaded on this replica, or turned off in the org: one read per listing. */
|
|
536
654
|
const unavailable = async (scope) => {
|
|
537
655
|
if (deps.service() === null)
|
|
538
|
-
return
|
|
539
|
-
const memoKey = "
|
|
656
|
+
return AUTOMATIONS_UNAVAILABLE_TEXT;
|
|
657
|
+
const memoKey = "automations:enabled";
|
|
540
658
|
let enabled = scope.memo?.get(memoKey);
|
|
541
659
|
if (enabled === undefined) {
|
|
542
|
-
enabled =
|
|
660
|
+
enabled = readAutomationSettings(deps.db(), deps.config, scope.orgId).then((s) => s.enabled);
|
|
543
661
|
scope.memo?.set(memoKey, enabled);
|
|
544
662
|
}
|
|
545
|
-
return (await enabled) ? null :
|
|
663
|
+
return (await enabled) ? null : AUTOMATIONS_OFF_TEXT;
|
|
546
664
|
};
|
|
547
665
|
/**
|
|
548
666
|
* The card the person approved for this call (`approvals.summary` of the call's latest
|
|
@@ -589,7 +707,7 @@ export function scheduleTools(deps) {
|
|
|
589
707
|
}
|
|
590
708
|
catch (e) {
|
|
591
709
|
if (e instanceof ApiError)
|
|
592
|
-
throw new ToolError(
|
|
710
|
+
throw new ToolError(automationErrorText(e, ctx));
|
|
593
711
|
throw e;
|
|
594
712
|
}
|
|
595
713
|
}
|
|
@@ -600,7 +718,9 @@ export function scheduleTools(deps) {
|
|
|
600
718
|
? await hereTarget(deps.db(), ctx.orgId, ctx.accountId, ctx.conversationId)
|
|
601
719
|
: input.mode === "chat"
|
|
602
720
|
? await chatTargetOf(deps.db(), ctx.orgId, normalizedChat(input.chat))
|
|
603
|
-
:
|
|
721
|
+
: input.mode === "subject"
|
|
722
|
+
? { mode: "subject" }
|
|
723
|
+
: { mode: "new" };
|
|
604
724
|
return {
|
|
605
725
|
trigger,
|
|
606
726
|
zone,
|
|
@@ -613,13 +733,15 @@ export function scheduleTools(deps) {
|
|
|
613
733
|
const when = scheduleWords(withZone(p.trigger, p.zone));
|
|
614
734
|
const where = targetWords(p.target, conversationId);
|
|
615
735
|
const tools = toolsWords(p.tools);
|
|
736
|
+
const filter = filterWords(input.trigger?.filter ?? []);
|
|
737
|
+
const ask = input.unattendedAsk;
|
|
616
738
|
const name = clip(input.name, NAME_MAX);
|
|
617
739
|
const agent = p.agentId === runAgentId
|
|
618
740
|
? { zh: "", en: "" }
|
|
619
741
|
: { zh: `;以智能体 ${p.agentId} 运行`, en: `; runs as agent ${p.agentId}` };
|
|
620
742
|
return {
|
|
621
|
-
zh:
|
|
622
|
-
en: `Create the
|
|
743
|
+
zh: `创建自动化「${name}」:${when.zh}${filter === null ? "" : `;筛选:${filter}`};运行位置:${where.zh}${agent.zh};预先批准的工具:${tools.zh}${ask === undefined ? "" : `;无人值守时的提问:${ask === "fail" ? "直接失败" : "等待回答"}`}`,
|
|
744
|
+
en: `Create the automation "${name}": ${when.en}${filter === null ? "" : `; filter: ${filter}`}; runs in ${where.en}${agent.en}; approved tools: ${tools.en}${ask === undefined ? "" : `; a question in an unattended run ${ask === "fail" ? "fails the call" : "waits for the user"}`}`,
|
|
623
745
|
};
|
|
624
746
|
}
|
|
625
747
|
const create = {
|
|
@@ -627,13 +749,12 @@ export function scheduleTools(deps) {
|
|
|
627
749
|
defaultPolicy: "ask",
|
|
628
750
|
unavailable,
|
|
629
751
|
async callPolicy(raw, ctx) {
|
|
630
|
-
const key = BUILTIN.
|
|
752
|
+
const key = BUILTIN.AutomationCreate;
|
|
631
753
|
const parsed = CreateInput.safeParse(raw);
|
|
632
754
|
if (!parsed.success)
|
|
633
755
|
return deny(key, INVALID_CREATE_TEXT);
|
|
634
|
-
if ((parsed.data
|
|
756
|
+
if (triggerCount(parsed.data) !== 1)
|
|
635
757
|
return deny(key, ONE_TRIGGER_TEXT);
|
|
636
|
-
}
|
|
637
758
|
const bad = createChatError(parsed.data);
|
|
638
759
|
if (bad !== null)
|
|
639
760
|
return deny(key, bad.message);
|
|
@@ -650,11 +771,11 @@ export function scheduleTools(deps) {
|
|
|
650
771
|
throw bad;
|
|
651
772
|
const svc = await service();
|
|
652
773
|
const p = await createPlan(input, ctx);
|
|
653
|
-
if (p.trigger === undefined || (input
|
|
774
|
+
if (p.trigger === undefined || triggerCount(input) !== 1) {
|
|
654
775
|
throw new ToolError(ONE_TRIGGER_TEXT);
|
|
655
776
|
}
|
|
656
777
|
// the zone and target are recomputed now: they must be the ones the person approved
|
|
657
|
-
await sameAsApproved(ctx, BUILTIN.
|
|
778
|
+
await sameAsApproved(ctx, BUILTIN.AutomationCreate, createCard(input, p, ctx.conversationId, ctx.agentId));
|
|
658
779
|
const trigger = p.trigger;
|
|
659
780
|
const task = await call(() => svc.create(ctx.orgId, ctx.accountId, {
|
|
660
781
|
agentId: p.agentId,
|
|
@@ -664,6 +785,7 @@ export function scheduleTools(deps) {
|
|
|
664
785
|
timeZone: p.zone,
|
|
665
786
|
...targetFields(p.target),
|
|
666
787
|
grants: p.tools.map((toolKey) => ({ toolKey })),
|
|
788
|
+
...(input.unattendedAsk !== undefined && { unattendedAsk: input.unattendedAsk }),
|
|
667
789
|
}), { zone: p.zone });
|
|
668
790
|
const when = scheduleWords(task.trigger);
|
|
669
791
|
const where = targetWords(p.target, ctx.conversationId);
|
|
@@ -672,18 +794,25 @@ export function scheduleTools(deps) {
|
|
|
672
794
|
content: [
|
|
673
795
|
{
|
|
674
796
|
type: "text",
|
|
675
|
-
text: `Created
|
|
797
|
+
text: `Created automation "${task.name}" (id ${task.id}): runs ${when.en} in ` +
|
|
676
798
|
`${where.en}${next === null ? "" : `; next run ${next}`}. Approved tools: ` +
|
|
677
799
|
`${toolsWords(p.tools).en}.`,
|
|
678
800
|
},
|
|
679
801
|
],
|
|
680
|
-
display: display(
|
|
802
|
+
display: display(`已创建自动化「${task.name}」 · ${when.zh}`, `Created automation "${task.name}" · ${when.en}`),
|
|
681
803
|
};
|
|
682
804
|
},
|
|
683
805
|
};
|
|
684
|
-
// ----
|
|
806
|
+
// ---- AutomationUpdate ----
|
|
685
807
|
/** The task's approved tool keys, sorted. */
|
|
686
808
|
const grantKeys = async (taskId) => uniqueSorted((await deps.store.listGrants(deps.db(), taskId)).map((g) => g.toolKey));
|
|
809
|
+
/**
|
|
810
|
+
* The zone a task's time trigger is read in: its own, or for an event task (no zone) the
|
|
811
|
+
* conversation's, as on create.
|
|
812
|
+
*/
|
|
813
|
+
const zoneOf = (task, ctx) => task.trigger.kind === "event"
|
|
814
|
+
? deps.timeZoneOf(ctx.orgId, ctx.conversationId)
|
|
815
|
+
: Promise.resolve(task.trigger.timeZone);
|
|
687
816
|
/** The person's own live task, or null. */
|
|
688
817
|
async function ownTask(orgId, accountId, id) {
|
|
689
818
|
const task = await deps.store.getTask(deps.db(), orgId, id);
|
|
@@ -704,7 +833,7 @@ export function scheduleTools(deps) {
|
|
|
704
833
|
const trigger = triggerOf(input);
|
|
705
834
|
if (trigger !== undefined) {
|
|
706
835
|
const before = scheduleWords(task.trigger);
|
|
707
|
-
const after = scheduleWords(withZone(trigger, task
|
|
836
|
+
const after = scheduleWords(withZone(trigger, await zoneOf(task, ctx)));
|
|
708
837
|
zh.push(`时间 ${before.zh} → ${after.zh}`);
|
|
709
838
|
en.push(`schedule ${before.en} → ${after.en}`);
|
|
710
839
|
}
|
|
@@ -754,8 +883,8 @@ export function scheduleTools(deps) {
|
|
|
754
883
|
const name = clip(task.name, 80);
|
|
755
884
|
const none = { zh: "没有改动", en: "no changes" };
|
|
756
885
|
return {
|
|
757
|
-
zh:
|
|
758
|
-
en: `Change the
|
|
886
|
+
zh: `修改自动化「${name}」:${zh.length > 0 ? zh.join(";") : none.zh}`,
|
|
887
|
+
en: `Change the automation "${name}": ${en.length > 0 ? en.join("; ") : none.en}`,
|
|
759
888
|
};
|
|
760
889
|
}
|
|
761
890
|
const nothingToUpdate = (i) => i.name === undefined &&
|
|
@@ -770,7 +899,7 @@ export function scheduleTools(deps) {
|
|
|
770
899
|
defaultPolicy: "ask",
|
|
771
900
|
unavailable,
|
|
772
901
|
async callPolicy(raw, ctx) {
|
|
773
|
-
const key = BUILTIN.
|
|
902
|
+
const key = BUILTIN.AutomationUpdate;
|
|
774
903
|
const parsed = UpdateInput.safeParse(raw);
|
|
775
904
|
if (!parsed.success)
|
|
776
905
|
return deny(key, INVALID_UPDATE_TEXT);
|
|
@@ -806,15 +935,20 @@ export function scheduleTools(deps) {
|
|
|
806
935
|
const task = await ownTask(ctx.orgId, ctx.accountId, input.id);
|
|
807
936
|
if (task === null)
|
|
808
937
|
throw new ToolError(notYoursText(input.id));
|
|
809
|
-
await sameAsApproved(ctx, BUILTIN.
|
|
938
|
+
await sameAsApproved(ctx, BUILTIN.AutomationUpdate, await updateCard(input, task, await grantKeys(task.id), ctx));
|
|
810
939
|
const patch = {};
|
|
811
940
|
if (input.name !== undefined)
|
|
812
941
|
patch.name = input.name;
|
|
813
942
|
if (input.prompt !== undefined)
|
|
814
943
|
patch.prompt = input.prompt;
|
|
944
|
+
const zone = await zoneOf(task, ctx);
|
|
815
945
|
const trigger = triggerOf(input);
|
|
816
|
-
if (trigger !== undefined)
|
|
946
|
+
if (trigger !== undefined) {
|
|
817
947
|
patch.trigger = trigger;
|
|
948
|
+
// an event task has no zone of its own: a time trigger replacing it takes the conversation's
|
|
949
|
+
if (task.trigger.kind === "event")
|
|
950
|
+
patch.timeZone = zone;
|
|
951
|
+
}
|
|
818
952
|
if (input.mode !== undefined) {
|
|
819
953
|
const target = input.mode === "here"
|
|
820
954
|
? await hereTarget(deps.db(), ctx.orgId, ctx.accountId, ctx.conversationId)
|
|
@@ -829,7 +963,6 @@ export function scheduleTools(deps) {
|
|
|
829
963
|
if (input.chat?.agentId !== undefined)
|
|
830
964
|
patch.agentId = input.chat.agentId;
|
|
831
965
|
}
|
|
832
|
-
const zone = task.trigger.timeZone;
|
|
833
966
|
const updated = await call(() => deps.updates.apply(svc, ctx.orgId, ctx.accountId, task, patch, input.approvedTools), { id: input.id, zone });
|
|
834
967
|
const grants = await grantKeys(updated.id);
|
|
835
968
|
const when = scheduleWords(updated.trigger);
|
|
@@ -839,16 +972,16 @@ export function scheduleTools(deps) {
|
|
|
839
972
|
content: [
|
|
840
973
|
{
|
|
841
974
|
type: "text",
|
|
842
|
-
text: `Updated
|
|
975
|
+
text: `Updated automation "${updated.name}" (id ${updated.id}): runs ${when.en} in ` +
|
|
843
976
|
`${where.en}; status ${updated.status}${next === null ? "" : `; next run ${next}`}. ` +
|
|
844
977
|
`Approved tools: ${toolsWords(grants).en}.`,
|
|
845
978
|
},
|
|
846
979
|
],
|
|
847
|
-
display: display(
|
|
980
|
+
display: display(`已修改自动化「${updated.name}」`, `Updated automation "${updated.name}"`),
|
|
848
981
|
};
|
|
849
982
|
},
|
|
850
983
|
};
|
|
851
|
-
// ----
|
|
984
|
+
// ---- AutomationList ----
|
|
852
985
|
const list = {
|
|
853
986
|
spec: LIST_SPEC,
|
|
854
987
|
defaultPolicy: "allow",
|
|
@@ -859,10 +992,8 @@ export function scheduleTools(deps) {
|
|
|
859
992
|
const tasks = await call(() => svc.list(ctx.orgId, { accountId: ctx.accountId }), {});
|
|
860
993
|
if (tasks.length === 0) {
|
|
861
994
|
return {
|
|
862
|
-
content: [
|
|
863
|
-
|
|
864
|
-
],
|
|
865
|
-
display: display("没有定时任务", "No scheduled tasks"),
|
|
995
|
+
content: [{ type: "text", text: "The user has no automations in this organization." }],
|
|
996
|
+
display: display("没有自动化", "No automations"),
|
|
866
997
|
};
|
|
867
998
|
}
|
|
868
999
|
const db = deps.db();
|
|
@@ -870,7 +1001,7 @@ export function scheduleTools(deps) {
|
|
|
870
1001
|
for (const t of tasks) {
|
|
871
1002
|
const where = targetWords(await storedTarget(db, t), ctx.conversationId).en;
|
|
872
1003
|
const status = t.status === "paused" && t.pauseReason !== null ? `paused (${t.pauseReason})` : t.status;
|
|
873
|
-
const next = t.nextRunAt === null
|
|
1004
|
+
const next = t.nextRunAt === null || t.trigger.kind === "event"
|
|
874
1005
|
? "none"
|
|
875
1006
|
: localTime(t.nextRunAt, t.trigger.timeZone).local.slice(0, 16).replace("T", " ");
|
|
876
1007
|
lines.push(`- ${t.id}: "${t.name}", ${scheduleWords(t.trigger).en}, ${status}, next run ${next}, ` +
|
|
@@ -880,15 +1011,15 @@ export function scheduleTools(deps) {
|
|
|
880
1011
|
content: [
|
|
881
1012
|
{
|
|
882
1013
|
type: "text",
|
|
883
|
-
text: `The user's
|
|
1014
|
+
text: `The user's automations (${tasks.length}):\n${lines.join("\n")}`,
|
|
884
1015
|
},
|
|
885
1016
|
],
|
|
886
|
-
display: display(
|
|
1017
|
+
display: display(`列出自动化(${tasks.length} 个)`, `Listed ${tasks.length} automations`),
|
|
887
1018
|
};
|
|
888
1019
|
},
|
|
889
1020
|
};
|
|
890
|
-
// ----
|
|
891
|
-
/** A target's fields in the model-facing line (`
|
|
1021
|
+
// ---- AutomationTargets ----
|
|
1022
|
+
/** A target's fields in the model-facing line (`AutomationTargets`). */
|
|
892
1023
|
const targetLine = (t) => `- ${clip(t.label, 120)}: connectionId=${t.connectionId}, chatId=${t.chatId}, ` +
|
|
893
1024
|
`chatType=${t.chatType}, agentId=${t.agentId} (${clip(t.connectionName, 80)}, ${t.plugin})`;
|
|
894
1025
|
const targets = {
|
|
@@ -904,7 +1035,7 @@ export function scheduleTools(deps) {
|
|
|
904
1035
|
content: [
|
|
905
1036
|
{
|
|
906
1037
|
type: "text",
|
|
907
|
-
text: "The user has no IM chats
|
|
1038
|
+
text: "The user has no IM chats automation runs can be delivered to in this " +
|
|
908
1039
|
"organization; a chat appears once the user has messaged the channel's bot or " +
|
|
909
1040
|
"talked in a group.",
|
|
910
1041
|
},
|
|
@@ -916,7 +1047,7 @@ export function scheduleTools(deps) {
|
|
|
916
1047
|
content: [
|
|
917
1048
|
{
|
|
918
1049
|
type: "text",
|
|
919
|
-
text: `The IM chats this user can deliver
|
|
1050
|
+
text: `The IM chats this user can deliver automation runs to (${items.length}):\n` +
|
|
920
1051
|
items.map(targetLine).join("\n"),
|
|
921
1052
|
},
|
|
922
1053
|
],
|
|
@@ -924,7 +1055,7 @@ export function scheduleTools(deps) {
|
|
|
924
1055
|
};
|
|
925
1056
|
},
|
|
926
1057
|
};
|
|
927
|
-
// ----
|
|
1058
|
+
// ---- AutomationDelete ----
|
|
928
1059
|
const remove = {
|
|
929
1060
|
spec: DELETE_SPEC,
|
|
930
1061
|
// deleting one's own task only narrows what runs (spec §11)
|
|
@@ -943,8 +1074,8 @@ export function scheduleTools(deps) {
|
|
|
943
1074
|
await liveRole(ctx);
|
|
944
1075
|
await call(() => svc.remove(ctx.orgId, { accountId: ctx.accountId }, task.id), { id });
|
|
945
1076
|
return {
|
|
946
|
-
content: [{ type: "text", text: `Deleted
|
|
947
|
-
display: display(
|
|
1077
|
+
content: [{ type: "text", text: `Deleted automation "${task.name}" (id ${task.id}).` }],
|
|
1078
|
+
display: display(`已删除自动化「${task.name}」`, `Deleted automation "${task.name}"`),
|
|
948
1079
|
};
|
|
949
1080
|
},
|
|
950
1081
|
};
|