@capybara-hq/kernel 0.1.0-rc.105 → 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.
Files changed (211) hide show
  1. package/dist/agents/agents.schemas.d.ts +9 -9
  2. package/dist/{schedules/schedules.controller.d.ts → automations/automations.controller.d.ts} +37 -32
  3. package/dist/{schedules/schedules.controller.js → automations/automations.controller.js} +68 -57
  4. package/dist/automations/automations.module.d.ts +16 -0
  5. package/dist/{schedules/schedules.module.js → automations/automations.module.js} +18 -15
  6. package/dist/{schedules/schedules.schemas.d.ts → automations/automations.schemas.d.ts} +151 -50
  7. package/dist/automations/automations.schemas.js +253 -0
  8. package/dist/{schedules/scheduled-tasks.service.d.ts → automations/automations.service.d.ts} +99 -52
  9. package/dist/{schedules/scheduled-tasks.service.js → automations/automations.service.js} +303 -134
  10. package/dist/{schedules → automations}/calendar.d.ts +8 -8
  11. package/dist/{schedules → automations}/calendar.js +2 -2
  12. package/dist/automations/effective-settings.d.ts +28 -0
  13. package/dist/automations/effective-settings.js +25 -0
  14. package/dist/automations/errors.d.ts +19 -0
  15. package/dist/automations/errors.js +19 -0
  16. package/dist/automations/events/coalesce.d.ts +28 -0
  17. package/dist/automations/events/coalesce.js +131 -0
  18. package/dist/automations/events/deadline.d.ts +10 -0
  19. package/dist/automations/events/deadline.js +24 -0
  20. package/dist/automations/events/dispatcher.d.ts +74 -0
  21. package/dist/automations/events/dispatcher.js +353 -0
  22. package/dist/automations/events/events.module.d.ts +7 -0
  23. package/dist/automations/events/events.module.js +26 -0
  24. package/dist/automations/events/guards.d.ts +32 -0
  25. package/dist/automations/events/guards.js +87 -0
  26. package/dist/automations/events/sweep.job.d.ts +49 -0
  27. package/dist/automations/events/sweep.job.js +202 -0
  28. package/dist/automations/events/token.d.ts +5 -0
  29. package/dist/automations/events/token.js +5 -0
  30. package/dist/automations/events/trigger-events.service.d.ts +47 -0
  31. package/dist/automations/events/trigger-events.service.js +254 -0
  32. package/dist/{schedules → automations}/fire.d.ts +57 -17
  33. package/dist/{schedules → automations}/fire.js +311 -79
  34. package/dist/{schedules → automations}/grant-rules.d.ts +1 -1
  35. package/dist/{schedules → automations}/grant-rules.js +5 -5
  36. package/dist/automations/job-ids.d.ts +113 -0
  37. package/dist/automations/job-ids.js +117 -0
  38. package/dist/{schedules → automations}/pause.d.ts +3 -3
  39. package/dist/{schedules → automations}/pause.js +3 -3
  40. package/dist/{schedules → automations}/run-tracker.d.ts +21 -10
  41. package/dist/{schedules → automations}/run-tracker.js +68 -56
  42. package/dist/{schedules → automations}/scan.job.d.ts +12 -8
  43. package/dist/{schedules → automations}/scan.job.js +39 -23
  44. package/dist/{schedules → automations}/schedule-math.d.ts +19 -12
  45. package/dist/{schedules → automations}/schedule-math.js +17 -7
  46. package/dist/{schedules → automations}/store.d.ts +57 -27
  47. package/dist/{schedules → automations}/store.js +102 -52
  48. package/dist/automations/store.module.d.ts +6 -0
  49. package/dist/{schedules → automations}/store.module.js +9 -9
  50. package/dist/automations/types.d.ts +178 -0
  51. package/dist/{schedules → automations}/unattended.d.ts +22 -11
  52. package/dist/{schedules → automations}/unattended.js +53 -30
  53. package/dist/catalog/marketplaces.service.d.ts +1 -1
  54. package/dist/channels/channels.module.js +3 -3
  55. package/dist/channels/outbound.d.ts +5 -5
  56. package/dist/channels/outbound.js +17 -17
  57. package/dist/channels/threads.d.ts +1 -1
  58. package/dist/channels/threads.js +1 -1
  59. package/dist/config/schema.d.ts +5 -1
  60. package/dist/config/schema.js +44 -6
  61. package/dist/conversations/approval-profile.d.ts +2 -2
  62. package/dist/conversations/approval-profile.js +3 -3
  63. package/dist/conversations/conversations.controller.d.ts +7 -1
  64. package/dist/conversations/conversations.controller.js +26 -10
  65. package/dist/conversations/conversations.service.d.ts +101 -25
  66. package/dist/conversations/conversations.service.js +132 -32
  67. package/dist/conversations/events.d.ts +46 -0
  68. package/dist/conversations/events.js +54 -3
  69. package/dist/conversations/message-context.d.ts +2 -2
  70. package/dist/conversations/message-context.js +1 -1
  71. package/dist/conversations/permission-profile-store.d.ts +2 -1
  72. package/dist/conversations/permission-profiles.controller.d.ts +3 -0
  73. package/dist/conversations/permission-profiles.service.js +1 -1
  74. package/dist/conversations/prompt-suggestion-request.d.ts +1 -1
  75. package/dist/conversations/prompt-suggestion-request.js +2 -1
  76. package/dist/conversations/queue.d.ts +7 -4
  77. package/dist/conversations/queue.js +6 -2
  78. package/dist/conversations/queue.service.d.ts +5 -5
  79. package/dist/conversations/queue.service.js +20 -14
  80. package/dist/ctx/create-ctx.d.ts +20 -1
  81. package/dist/ctx/create-ctx.js +59 -2
  82. package/dist/db/kernel-schema.d.ts +104 -25
  83. package/dist/devices/device-project-files.service.d.ts +88 -0
  84. package/dist/devices/device-project-files.service.js +249 -0
  85. package/dist/devices/device-projects.service.d.ts +40 -0
  86. package/dist/devices/device-projects.service.js +16 -13
  87. package/dist/devices/devices.controller.d.ts +9 -3
  88. package/dist/devices/devices.controller.js +27 -5
  89. package/dist/devices/devices.module.js +9 -1
  90. package/dist/devices/devices.schemas.js +6 -1
  91. package/dist/devices/dto.d.ts +15 -0
  92. package/dist/devices/dto.js +11 -0
  93. package/dist/devices/project-paths.d.ts +29 -0
  94. package/dist/devices/project-paths.js +66 -0
  95. package/dist/devices/project-rules.d.ts +5 -1
  96. package/dist/devices/project-rules.js +5 -1
  97. package/dist/errors.d.ts +34 -13
  98. package/dist/errors.js +34 -13
  99. package/dist/evals/reconstruct.d.ts +10 -1
  100. package/dist/evals/reconstruct.js +7 -0
  101. package/dist/extensions/host.d.ts +6 -1
  102. package/dist/extensions/host.js +10 -0
  103. package/dist/extensions/permission-set.d.ts +17 -1
  104. package/dist/extensions/permission-set.js +13 -1
  105. package/dist/extensions/permission-view.d.ts +1 -1
  106. package/dist/extensions/permission-view.js +4 -1
  107. package/dist/git/gateway.js +2 -2
  108. package/dist/http/api.module.js +5 -5
  109. package/dist/http/request-signal.d.ts +8 -0
  110. package/dist/http/request-signal.js +14 -0
  111. package/dist/inbox/inbox.module.js +2 -2
  112. package/dist/inbox/inbox.service.d.ts +8 -8
  113. package/dist/inbox/inbox.service.js +7 -7
  114. package/dist/infra/config/kernel-config.service.js +10 -5
  115. package/dist/infra/outbox/registry.d.ts +29 -2
  116. package/dist/infra/outbox/registry.js +30 -7
  117. package/dist/kernel.module.js +3 -0
  118. package/dist/orgs/roles.d.ts +1 -1
  119. package/dist/orgs/roles.js +3 -3
  120. package/dist/orgs/settings.controller.d.ts +1 -1
  121. package/dist/orgs/settings.service.d.ts +27 -11
  122. package/dist/orgs/settings.service.js +42 -24
  123. package/dist/platform/operation.js +2 -2
  124. package/dist/platform/ops/{schedules.ops.d.ts → automations.ops.d.ts} +4 -4
  125. package/dist/platform/ops/automations.ops.js +560 -0
  126. package/dist/platform/ops/devices.ops.d.ts +3 -1
  127. package/dist/platform/ops/devices.ops.js +77 -3
  128. package/dist/platform/ops/inbox.ops.js +4 -4
  129. package/dist/platform/ops/orgs.ops.js +1 -1
  130. package/dist/platform/ops/platform.ops.js +4 -4
  131. package/dist/platform/platform-ops.module.d.ts +2 -2
  132. package/dist/platform/platform-ops.module.js +5 -5
  133. package/dist/platform/route-schemas.js +3 -3
  134. package/dist/push/jobs.d.ts +3 -3
  135. package/dist/push/jobs.js +2 -2
  136. package/dist/push/preferences.service.js +1 -1
  137. package/dist/push/push-owed.d.ts +3 -3
  138. package/dist/push/push-owed.js +6 -6
  139. package/dist/push/push.schemas.d.ts +4 -4
  140. package/dist/push/push.schemas.js +3 -3
  141. package/dist/push/recipients.d.ts +4 -4
  142. package/dist/push/recipients.js +23 -23
  143. package/dist/push/texts.d.ts +3 -3
  144. package/dist/push/texts.js +2 -2
  145. package/dist/push/triggers.d.ts +1 -1
  146. package/dist/roles/worker.module.js +3 -3
  147. package/dist/runtime/context/build.d.ts +6 -0
  148. package/dist/runtime/context/build.js +14 -3
  149. package/dist/runtime/context/compaction.js +15 -5
  150. package/dist/runtime/context/run-context.d.ts +38 -0
  151. package/dist/runtime/context/run-context.js +54 -0
  152. package/dist/runtime/maintenance.d.ts +8 -4
  153. package/dist/runtime/maintenance.js +37 -16
  154. package/dist/runtime/run-finish.d.ts +4 -4
  155. package/dist/runtime/run-finish.js +3 -3
  156. package/dist/runtime/run-queue.js +12 -7
  157. package/dist/runtime/run-worker.js +7 -1
  158. package/dist/runtime/runtime.module.js +3 -3
  159. package/dist/runtime/subagents/dispatch.js +11 -3
  160. package/dist/sandbox/browse-roots.d.ts +8 -0
  161. package/dist/sandbox/browse-roots.js +22 -0
  162. package/dist/sandbox/network-grants.d.ts +4 -4
  163. package/dist/sandbox/network-grants.js +7 -7
  164. package/dist/sandbox/operation-authority.d.ts +10 -3
  165. package/dist/sandbox/operation-authority.js +20 -10
  166. package/dist/sandbox/operation-intents.d.ts +3 -1
  167. package/dist/sandbox/operation-intents.js +11 -4
  168. package/dist/sandbox/permission-call-grants.js +2 -2
  169. package/dist/sandbox/permission-facts.js +1 -1
  170. package/dist/sandbox/review/classifier.d.ts +2 -2
  171. package/dist/sandbox/review/classifier.js +22 -11
  172. package/dist/sandbox/review/permission-candidate.d.ts +1 -1
  173. package/dist/sandbox/review/permission-candidate.js +1 -1
  174. package/dist/sandbox/sandbox.service.d.ts +15 -5
  175. package/dist/sandbox/sandbox.service.js +14 -8
  176. package/dist/storage/purge.d.ts +1 -1
  177. package/dist/storage/purge.js +2 -2
  178. package/dist/tools/approvals.service.d.ts +6 -6
  179. package/dist/tools/approvals.service.js +19 -19
  180. package/dist/tools/builtin-names.d.ts +12 -12
  181. package/dist/tools/builtin-names.js +29 -29
  182. package/dist/tools/builtins/{schedules.d.ts → automations.d.ts} +35 -33
  183. package/dist/tools/builtins/{schedules.js → automations.js} +236 -105
  184. package/dist/tools/builtins/builtin-tools.d.ts +8 -8
  185. package/dist/tools/builtins/builtin-tools.js +16 -16
  186. package/dist/tools/call-scope.d.ts +1 -1
  187. package/dist/tools/git-asks.js +1 -1
  188. package/dist/tools/git-push-policy.d.ts +3 -3
  189. package/dist/tools/git-push-policy.js +6 -6
  190. package/dist/tools/network-asks.d.ts +3 -3
  191. package/dist/tools/network-asks.js +3 -3
  192. package/dist/tools/pipeline.d.ts +4 -4
  193. package/dist/tools/pipeline.js +2 -2
  194. package/dist/tools/policy.d.ts +3 -3
  195. package/dist/tools/policy.js +3 -3
  196. package/dist/tools/tools.module.js +9 -9
  197. package/package.json +3 -3
  198. package/src/migrations/0188_automations_rename.sql +309 -0
  199. package/src/migrations/0189_event_automations.sql +136 -0
  200. package/dist/platform/ops/schedules.ops.js +0 -509
  201. package/dist/schedules/effective-settings.d.ts +0 -22
  202. package/dist/schedules/effective-settings.js +0 -18
  203. package/dist/schedules/errors.d.ts +0 -8
  204. package/dist/schedules/errors.js +0 -8
  205. package/dist/schedules/job-ids.d.ts +0 -81
  206. package/dist/schedules/job-ids.js +0 -95
  207. package/dist/schedules/schedules.module.d.ts +0 -16
  208. package/dist/schedules/schedules.schemas.js +0 -213
  209. package/dist/schedules/store.module.d.ts +0 -6
  210. package/dist/schedules/types.d.ts +0 -110
  211. /package/dist/{schedules → automations}/types.js +0 -0
@@ -1,509 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var __param = (this && this.__param) || function (paramIndex, decorator) {
8
- return function (target, key) { decorator(target, key, paramIndex); }
9
- };
10
- import { Inject, Injectable } from "@nestjs/common";
11
- import { z } from "zod";
12
- import { ScheduledTasksService, } from "../../schedules/scheduled-tasks.service.js";
13
- import { calendarInputOf, createInputOf, grantsOf, ScheduleCalendarQuery, ScheduleChatTargetsQuery, ScheduleCreateBody, ScheduleGrantableQuery, ScheduleGrantParams, ScheduleGrantsBody, ScheduleListQuery, ScheduleParams, ScheduleRunBody, ScheduleRunsQuery, ScheduleUpdateBody, updateInputOf, } from "../../schedules/schedules.schemas.js";
14
- import { defineOperation } from "../operation.js";
15
- import { PlatformOperations } from "../registry.js";
16
- import { named, orElse } from "./summary.js";
17
- const ListQuery = ScheduleListQuery;
18
- const CreateBody = ScheduleCreateBody;
19
- const UpdateBody = ScheduleUpdateBody;
20
- const RunBody = ScheduleRunBody;
21
- const RunsQuery = ScheduleRunsQuery;
22
- const GrantsBody = ScheduleGrantsBody;
23
- const Id = ScheduleParams.shape.id;
24
- const ToolKey = ScheduleGrantParams.shape.toolKey;
25
- const TaskId = z.object({ scheduleId: Id }).strict();
26
- const EXAMPLE_ID = "7b2d4f60-3a1e-4c85-9e07-5d8f1a2b3c4d";
27
- /** A task as `schedules.list` shows it: the schedule, the state and the next run. */
28
- function row(t) {
29
- return {
30
- ...(t.lastRun !== undefined && { lastRun: t.lastRun }),
31
- id: t.id,
32
- name: t.name,
33
- agentId: t.agentId,
34
- status: t.status,
35
- pauseReason: t.pauseReason,
36
- trigger: t.trigger,
37
- mode: t.mode,
38
- nextRunAt: t.nextRunAt,
39
- creator: t.accountId,
40
- };
41
- }
42
- const runRow = (r) => ({
43
- id: r.id,
44
- scheduledFor: r.scheduledFor,
45
- firedAt: r.firedAt,
46
- trial: r.trial,
47
- status: r.status,
48
- skipReason: r.skipReason,
49
- error: r.error,
50
- conversationId: r.conversationId,
51
- missingGrants: r.missingGrants,
52
- });
53
- /**
54
- * Scheduled-task operations of the `Platform` tool (scheduled tasks spec §10): the same service
55
- * as the HTTP routes. Writes are `personal`. Creating or changing a task and its approved tools
56
- * are never remembered (`rememberable: false`): an approval, remembered or granted to a task,
57
- * must never let an unattended run create or widen a task. Delete, run and resume are never
58
- * remembered either. List, pause and delete are `org.read` like their routes: a creator demoted
59
- * to guest can still see and stop their own tasks (the service decides by ownership).
60
- */
61
- let SchedulesOperations = class SchedulesOperations {
62
- registry;
63
- tasks;
64
- constructor(registry, tasks) {
65
- this.registry = registry;
66
- this.tasks = tasks;
67
- }
68
- /** The caller as the service expects (it reads the live role itself). */
69
- actor(ctx) {
70
- return { accountId: ctx.actorAccountId };
71
- }
72
- /** The task's name for a card; its id when it cannot be read. */
73
- label(ctx, id) {
74
- return orElse(async () => named((await this.tasks.get(ctx.orgId, this.actor(ctx), id)).name), id);
75
- }
76
- onModuleInit() {
77
- this.registry.register([
78
- defineOperation({
79
- id: "schedules.list",
80
- description: "List your scheduled tasks: one compact row each (name, agent, status and why it is paused, schedule with its time zone, where runs post, next run, last settled run). all=true lists every task of the org and needs the org admin role; conversationId keeps the tasks that post into that conversation.",
81
- input: ListQuery,
82
- example: {},
83
- // a creator demoted to guest still lists their own tasks (to pause or delete them)
84
- authz: { orgAction: "org.read" },
85
- mirrors: ["GET /api/v1/orgs/:orgId/schedules"],
86
- confirm: "read",
87
- errors: [
88
- {
89
- code: "forbidden",
90
- next: "Listing every task needs the org admin role: call without all, or ask an org admin.",
91
- },
92
- ],
93
- run: async (input, ctx) => {
94
- const items = await this.tasks.list(ctx.orgId, this.actor(ctx), {
95
- all: input.all === "true",
96
- ...(input.conversationId !== undefined && { conversationId: input.conversationId }),
97
- });
98
- return { items: items.map(row) };
99
- },
100
- }),
101
- defineOperation({
102
- id: "schedules.calendar",
103
- description: "Your scheduled task runs and planned occurrences between two instants (at most 43 days): recorded runs with their status, planned runs computed from each active task's schedule up to 42 days ahead, and dense tasks summarized per day. Org admins see every task with all=true. from and to are ISO instants with an offset; timeZone (IANA) sets the local days used to summarize dense tasks; taskId lists one task's items, never summarized (then at most 2 days). truncated means a limit cut the range at horizon.",
104
- input: ScheduleCalendarQuery,
105
- example: {
106
- from: "2026-11-09T00:00:00+08:00",
107
- to: "2026-11-16T00:00:00+08:00",
108
- timeZone: "Asia/Shanghai",
109
- },
110
- authz: { orgAction: "org.read" },
111
- mirrors: ["GET /api/v1/orgs/:orgId/schedules/calendar"],
112
- confirm: "read",
113
- errors: [
114
- {
115
- code: "validation_failed",
116
- next: "Narrow the range to 43 days or less (2 days with taskId), with from before to.",
117
- },
118
- {
119
- code: "forbidden",
120
- next: "Reading every task needs the org admin role: call without all, or ask an org admin.",
121
- },
122
- {
123
- code: "schedule.not_found",
124
- next: "Check taskId with schedules.list: members see only their own tasks; an org admin finds another member's task id with schedules.list all=true, then reads it here by taskId (all is not needed).",
125
- },
126
- ],
127
- run: async (input, ctx) => {
128
- const answer = await this.tasks.calendar(ctx.orgId, this.actor(ctx), calendarInputOf(input));
129
- return {
130
- ...answer,
131
- tasks: answer.tasks.map((t) => ({
132
- id: t.id,
133
- name: t.name,
134
- agentId: t.agentId,
135
- status: t.status,
136
- })),
137
- };
138
- },
139
- }),
140
- defineOperation({
141
- id: "schedules.chat-targets",
142
- description: "The IM chats you can point a chat-mode scheduled task at: your direct chats with each active channel connection's bot (one you have messaged before), and the groups you have talked in when the channel has threads. Each gives { connectionId, chatId, chatType } for schedules.create, and the connection's agentId, which the task must use.",
143
- input: ScheduleChatTargetsQuery,
144
- example: {},
145
- authz: { orgAction: "schedules.use" },
146
- mirrors: ["GET /api/v1/orgs/:orgId/schedules/chat-targets"],
147
- confirm: "read",
148
- errors: [],
149
- run: async (_input, ctx) => ({
150
- items: await this.tasks.chatTargets(ctx.orgId, ctx.actorAccountId),
151
- }),
152
- }),
153
- defineOperation({
154
- id: "schedules.grantable-tools",
155
- description: "An agent's tools as a scheduled task can approve them in advance: each { key, title, source, connectorId, grantable, reason }; reason says why not (forbidden by the org, not_grantable by design, needs_role, fixed_ask, not_available). Takes the agent id from the agents list.",
156
- input: ScheduleGrantableQuery,
157
- example: { agentId: "4e8a0c1d-6b2f-4a37-8d19-3c5e7f9a1b20" },
158
- authz: { orgAction: "schedules.use" },
159
- mirrors: ["GET /api/v1/orgs/:orgId/schedules/grantable-tools"],
160
- confirm: "read",
161
- errors: [
162
- {
163
- code: "agent.not_found",
164
- next: "Check the agent id: it must be an agent you can use in this org.",
165
- },
166
- ],
167
- run: async (input, ctx) => ({
168
- items: await this.tasks.grantableTools(ctx.orgId, ctx.actorAccountId, input.agentId),
169
- }),
170
- }),
171
- defineOperation({
172
- id: "schedules.get",
173
- description: "Read one scheduled task in full: prompt, schedule, target, model, effort, device, status, next run, expiry. Takes the id from schedules.list.",
174
- input: TaskId,
175
- example: { scheduleId: EXAMPLE_ID },
176
- authz: { orgAction: "schedules.use" },
177
- mirrors: ["GET /api/v1/orgs/:orgId/schedules/:id"],
178
- renames: { scheduleId: "id" },
179
- confirm: "read",
180
- errors: [
181
- {
182
- code: "schedule.not_found",
183
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
184
- },
185
- ],
186
- run: async (input, ctx) => ({
187
- task: await this.tasks.get(ctx.orgId, this.actor(ctx), input.scheduleId),
188
- }),
189
- }),
190
- defineOperation({
191
- id: "schedules.create",
192
- description: "Create a scheduled task: an agent runs the prompt on a 5-field cron (or once at a local date-time) as you, unattended. The task's time zone is yours; timeZone is only the fallback when you have none set. mode: new (a conversation per run), conversation (post into one conversation) or chat (an IM chat). model ({ provider, model } like an agent definition's), effort and deviceId are for mode new only; a conversation or chat task runs with that conversation's model. Without expiresAt a cron task ends after the org's default expiry, when the org sets one. grants pre-approve tools the unattended run may use.",
193
- input: CreateBody,
194
- example: {
195
- agentId: "4e8a0c1d-6b2f-4a37-8d19-3c5e7f9a1b20",
196
- name: "Daily digest",
197
- prompt: "Summarize yesterday's activity.",
198
- trigger: { kind: "cron", expr: "0 9 * * *" },
199
- mode: "new",
200
- },
201
- authz: { orgAction: "schedules.use" },
202
- mirrors: ["POST /api/v1/orgs/:orgId/schedules"],
203
- confirm: "personal",
204
- // an unattended run must never create or widen a task through a remembered approval
205
- rememberable: false,
206
- errors: [
207
- {
208
- code: "schedule.disabled",
209
- next: {
210
- orgAdmin: "Turn scheduled tasks on in the org settings.",
211
- member: "Scheduled tasks are off here; ask an org admin.",
212
- },
213
- },
214
- {
215
- code: "schedule.limit_reached",
216
- next: "Delete a task you no longer need with schedules.delete, or pause and edit an existing one.",
217
- },
218
- {
219
- code: "schedule.target_invalid",
220
- next: "Fix the target named in the message (conversation, chat or device) and call again.",
221
- },
222
- {
223
- code: "schedule.grant_not_allowed",
224
- next: "Drop that grant: the task will ask for it as a person when it runs attended, or ask an org admin.",
225
- },
226
- ],
227
- summary: (i) => ({
228
- zh: `创建定时任务「${named(i.name)}」(${i.trigger.kind === "cron" ? `cron ${named(i.trigger.expr)}` : `一次性 ${named(i.trigger.at)}`}),之后无人值守地以你的身份运行`,
229
- en: `Create scheduled task "${named(i.name)}" (${i.trigger.kind === "cron" ? `cron ${named(i.trigger.expr)}` : `once at ${named(i.trigger.at)}`}), then run unattended as you`,
230
- }),
231
- run: async (input, ctx) => {
232
- const { input: create, browserTimeZone } = createInputOf(input);
233
- return {
234
- task: await this.tasks.create(ctx.orgId, ctx.actorAccountId, create, browserTimeZone),
235
- };
236
- },
237
- }),
238
- defineOperation({
239
- id: "schedules.update",
240
- description: "Change one of your own scheduled tasks (name, prompt, schedule, time zone, target, model, effort, device, expiry; null clears an optional field, and a null expiry on a cron task takes the org's default expiry when set). model, effort and deviceId are for mode new only; switching to another mode clears them. Only the creator can; an org admin can pause it instead. Takes the id from schedules.list.",
241
- input: UpdateBody.extend({ scheduleId: Id }).strict(),
242
- example: { scheduleId: EXAMPLE_ID, prompt: "Summarize the last two days." },
243
- authz: { orgAction: "schedules.use" },
244
- mirrors: ["PATCH /api/v1/orgs/:orgId/schedules/:id"],
245
- renames: { scheduleId: "id" },
246
- confirm: "personal",
247
- rememberable: false,
248
- errors: [
249
- {
250
- code: "schedule.not_found",
251
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
252
- },
253
- {
254
- code: "schedule.forbidden",
255
- next: "Only the creator can change a task: ask them, or an org admin can pause it.",
256
- },
257
- {
258
- code: "schedule.target_invalid",
259
- next: "Fix the target named in the message and call again.",
260
- },
261
- ],
262
- summary: async (i, ctx) => {
263
- const n = await this.label(ctx, i.scheduleId);
264
- return {
265
- zh: `修改定时任务「${n}」`,
266
- en: `Change scheduled task "${n}"`,
267
- };
268
- },
269
- run: async (input, ctx) => {
270
- const { scheduleId, ...patch } = input;
271
- return {
272
- task: await this.tasks.update(ctx.orgId, ctx.actorAccountId, scheduleId, updateInputOf(patch)),
273
- };
274
- },
275
- }),
276
- defineOperation({
277
- id: "schedules.delete",
278
- description: "Delete a scheduled task (yours; an org admin can delete any). Takes the id from schedules.list.",
279
- input: TaskId,
280
- example: { scheduleId: EXAMPLE_ID },
281
- authz: { orgAction: "org.read" },
282
- mirrors: ["DELETE /api/v1/orgs/:orgId/schedules/:id"],
283
- renames: { scheduleId: "id" },
284
- confirm: "personal",
285
- rememberable: false,
286
- errors: [
287
- {
288
- code: "schedule.not_found",
289
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
290
- },
291
- ],
292
- summary: async (i, ctx) => {
293
- const n = await this.label(ctx, i.scheduleId);
294
- return { zh: `删除定时任务「${n}」`, en: `Delete scheduled task "${n}"` };
295
- },
296
- run: async (input, ctx) => {
297
- await this.tasks.remove(ctx.orgId, this.actor(ctx), input.scheduleId);
298
- return { id: input.scheduleId, deleted: true };
299
- },
300
- }),
301
- defineOperation({
302
- id: "schedules.pause",
303
- description: "Pause a scheduled task so it stops firing (yours; an org admin can pause any, and the creator is told). Resume it with schedules.resume. Takes the id from schedules.list.",
304
- input: TaskId,
305
- example: { scheduleId: EXAMPLE_ID },
306
- authz: { orgAction: "org.read" },
307
- mirrors: ["POST /api/v1/orgs/:orgId/schedules/:id/pause"],
308
- renames: { scheduleId: "id" },
309
- confirm: "personal",
310
- errors: [
311
- {
312
- code: "schedule.not_found",
313
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
314
- },
315
- ],
316
- summary: async (i, ctx) => {
317
- const n = await this.label(ctx, i.scheduleId);
318
- return { zh: `暂停定时任务「${n}」`, en: `Pause scheduled task "${n}"` };
319
- },
320
- run: async (input, ctx) => ({
321
- task: row(await this.tasks.pause(ctx.orgId, this.actor(ctx), input.scheduleId)),
322
- }),
323
- }),
324
- defineOperation({
325
- id: "schedules.resume",
326
- description: "Resume a paused scheduled task: the next run is computed from now (missed ticks are not caught up). An ended task cannot be resumed. Takes the id from schedules.list.",
327
- input: TaskId,
328
- example: { scheduleId: EXAMPLE_ID },
329
- authz: { orgAction: "schedules.use" },
330
- mirrors: ["POST /api/v1/orgs/:orgId/schedules/:id/resume"],
331
- renames: { scheduleId: "id" },
332
- confirm: "personal",
333
- // a remembered resume would let an unattended run restart a task a person stopped
334
- rememberable: false,
335
- errors: [
336
- {
337
- code: "schedule.not_found",
338
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
339
- },
340
- {
341
- code: "schedule.once_in_past",
342
- next: "Its time has passed: set a new time with schedules.update, then resume.",
343
- },
344
- {
345
- code: "schedule.disabled",
346
- next: {
347
- orgAdmin: "Turn scheduled tasks on in the org settings.",
348
- member: "Scheduled tasks are off here; ask an org admin.",
349
- },
350
- },
351
- ],
352
- summary: async (i, ctx) => {
353
- const n = await this.label(ctx, i.scheduleId);
354
- return { zh: `恢复定时任务「${n}」`, en: `Resume scheduled task "${n}"` };
355
- },
356
- run: async (input, ctx) => ({
357
- task: row(await this.tasks.resume(ctx.orgId, this.actor(ctx), input.scheduleId)),
358
- }),
359
- }),
360
- defineOperation({
361
- id: "schedules.run",
362
- description: "Run one of your own scheduled tasks now, outside its schedule (the schedule does not move). trial=true is an attended trial: you approve its tool calls and they become the task's approved tools. Counts toward the hourly run limit. Takes the id from schedules.list.",
363
- input: RunBody.extend({ scheduleId: Id }).strict(),
364
- example: { scheduleId: EXAMPLE_ID, trial: true },
365
- authz: { orgAction: "schedules.use" },
366
- mirrors: ["POST /api/v1/orgs/:orgId/schedules/:id/run"],
367
- renames: { scheduleId: "id" },
368
- confirm: "personal",
369
- // a remembered run would let an unattended run trigger other tasks
370
- rememberable: false,
371
- errors: [
372
- {
373
- code: "schedule.not_found",
374
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
375
- },
376
- {
377
- code: "schedule.forbidden",
378
- next: "Only the creator can change a task: ask them, or an org admin can pause it.",
379
- },
380
- { code: "conflict", next: "The task has ended or is being run: wait, or edit it first." },
381
- ],
382
- summary: async (i, ctx) => {
383
- const n = await this.label(ctx, i.scheduleId);
384
- return {
385
- zh: `立即运行定时任务「${n}」${i.trial === true ? "(试运行)" : ""}`,
386
- en: `Run scheduled task "${n}" now${i.trial === true ? " as a trial" : ""}`,
387
- };
388
- },
389
- run: async (input, ctx) => ({
390
- run: runRow(await this.tasks.runNow(ctx.orgId, ctx.actorAccountId, input.scheduleId, {
391
- trial: input.trial === true,
392
- })),
393
- }),
394
- }),
395
- defineOperation({
396
- id: "schedules.runs",
397
- description: "A task's run history, latest first (default 50, at most 200): each tick with its status, skip reason, error, conversation and the tools refused for lack of an approval. Takes the id from schedules.list.",
398
- input: RunsQuery.extend({ scheduleId: Id }).strict(),
399
- example: { scheduleId: EXAMPLE_ID },
400
- authz: { orgAction: "schedules.use" },
401
- mirrors: ["GET /api/v1/orgs/:orgId/schedules/:id/runs"],
402
- renames: { scheduleId: "id" },
403
- confirm: "read",
404
- errors: [
405
- {
406
- code: "schedule.not_found",
407
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
408
- },
409
- ],
410
- run: async (input, ctx) => ({
411
- items: (await this.tasks.runs(ctx.orgId, this.actor(ctx), input.scheduleId, input.limit)).map(runRow),
412
- }),
413
- }),
414
- defineOperation({
415
- id: "schedules.grants-list",
416
- description: "The tools approved in advance for a scheduled task (what an unattended run may call without asking), each as { toolKey, connectorId, stale }; a stale approval no longer applies because the tool changed. Takes the id from schedules.list.",
417
- input: TaskId,
418
- example: { scheduleId: EXAMPLE_ID },
419
- authz: { orgAction: "schedules.use" },
420
- mirrors: ["GET /api/v1/orgs/:orgId/schedules/:id/grants"],
421
- renames: { scheduleId: "id" },
422
- confirm: "read",
423
- errors: [
424
- {
425
- code: "schedule.not_found",
426
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
427
- },
428
- ],
429
- run: async (input, ctx) => ({
430
- items: (await this.tasks.grants(ctx.orgId, this.actor(ctx), input.scheduleId)).map((g) => ({ toolKey: g.toolKey, connectorId: g.connectorId, stale: g.stale })),
431
- }),
432
- }),
433
- defineOperation({
434
- id: "schedules.grants-add",
435
- description: "Approve tools in advance for one of your own scheduled tasks: an unattended run may then call them without asking. Only tools you could approve with always-allow yourself; never a Platform operation (each is decided per call) and nothing the org forbids. Returns the task's approved tools as { toolKey, connectorId }. Takes the id from schedules.list.",
436
- input: GrantsBody.extend({ scheduleId: Id }).strict(),
437
- example: { scheduleId: EXAMPLE_ID, grants: [{ toolKey: "WebFetch" }] },
438
- authz: { orgAction: "schedules.use" },
439
- mirrors: ["POST /api/v1/orgs/:orgId/schedules/:id/grants"],
440
- renames: { scheduleId: "id" },
441
- confirm: "personal",
442
- rememberable: false,
443
- errors: [
444
- {
445
- code: "schedule.not_found",
446
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
447
- },
448
- {
449
- code: "schedule.forbidden",
450
- next: "Only the creator can change a task: ask them, or an org admin can pause it.",
451
- },
452
- {
453
- code: "schedule.grant_not_allowed",
454
- next: "Drop that tool: it cannot be approved in advance for a task.",
455
- },
456
- ],
457
- summary: async (i, ctx) => {
458
- const n = await this.label(ctx, i.scheduleId);
459
- const keys = i.grants.map((g) => named(g.toolKey)).join(", ");
460
- return {
461
- zh: `为定时任务「${n}」预先批准工具:${keys}`,
462
- en: `Approve tools in advance for scheduled task "${n}": ${keys}`,
463
- };
464
- },
465
- run: async (input, ctx) => ({
466
- items: (await this.tasks.addGrants(ctx.orgId, ctx.actorAccountId, input.scheduleId, grantsOf(input.grants))).map((g) => ({ toolKey: g.toolKey, connectorId: g.connectorId })),
467
- }),
468
- }),
469
- defineOperation({
470
- id: "schedules.grants-remove",
471
- description: "Remove a tool's advance approval from one of your own scheduled tasks (nothing happens if it had none): the unattended run is then refused that tool. Takes the id from schedules.list.",
472
- input: z.object({ scheduleId: Id, toolKey: ToolKey }).strict(),
473
- example: { scheduleId: EXAMPLE_ID, toolKey: "WebFetch" },
474
- authz: { orgAction: "schedules.use" },
475
- mirrors: ["DELETE /api/v1/orgs/:orgId/schedules/:id/grants/:toolKey"],
476
- renames: { scheduleId: "id" },
477
- confirm: "personal",
478
- rememberable: false,
479
- errors: [
480
- {
481
- code: "schedule.not_found",
482
- next: "Check the id with schedules.list: you only see your own tasks (org admins see all with all=true).",
483
- },
484
- {
485
- code: "schedule.forbidden",
486
- next: "Only the creator can change a task: ask them, or an org admin can pause it.",
487
- },
488
- ],
489
- summary: async (i, ctx) => {
490
- const n = await this.label(ctx, i.scheduleId);
491
- return {
492
- zh: `撤销定时任务「${n}」对工具 ${named(i.toolKey)} 的预先批准`,
493
- en: `Remove the advance approval of ${named(i.toolKey)} from scheduled task "${n}"`,
494
- };
495
- },
496
- run: async (input, ctx) => {
497
- await this.tasks.removeGrant(ctx.orgId, ctx.actorAccountId, input.scheduleId, input.toolKey);
498
- return { id: input.scheduleId, toolKey: input.toolKey, removed: true };
499
- },
500
- }),
501
- ]);
502
- }
503
- };
504
- SchedulesOperations = __decorate([
505
- Injectable(),
506
- __param(0, Inject(PlatformOperations)),
507
- __param(1, Inject(ScheduledTasksService))
508
- ], SchedulesOperations);
509
- export { SchedulesOperations };
@@ -1,22 +0,0 @@
1
- import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
- import type { KernelConfigService } from "../infra/config/kernel-config.service.js";
3
- import { type ScheduledTasksSettings } from "../orgs/settings.service.js";
4
- /** The platform's bounds for the org settings (kernel config `schedules.*`). */
5
- export interface ScheduleBounds {
6
- minIntervalFloorMinutes: number;
7
- maxTasksPerMemberCap: number;
8
- maxTasksPerOrgCap: number;
9
- maxRunsPerHourCap: number;
10
- }
11
- /**
12
- * The org's scheduled-task settings after clamping to the platform bounds. `forbiddenToolsUnreadable`:
13
- * the stored list cannot be read (`invalidStoredForbiddenTools`); `forbiddenTools` is then `[]`
14
- * and unattended runs of the org must fail instead of running with nothing forbidden.
15
- */
16
- export type EffectiveScheduleSettings = ScheduledTasksSettings & {
17
- forbiddenToolsUnreadable: boolean;
18
- };
19
- /** An org value outside the platform bounds is clamped: the interval up, the counts down. */
20
- export declare function effectiveScheduleSettings(org: ScheduledTasksSettings, bounds: ScheduleBounds, forbiddenToolsUnreadable?: boolean): EffectiveScheduleSettings;
21
- /** Read the org's effective settings live (PG and the current kernel config). */
22
- export declare function readScheduleSettings(db: KernelSchemaDb, config: Pick<KernelConfigService, "get">, orgId: string): Promise<EffectiveScheduleSettings>;
@@ -1,18 +0,0 @@
1
- import { invalidStoredForbiddenTools, parseOrgSettings, readStoredOrgSettings, } from "../orgs/settings.service.js";
2
- /** An org value outside the platform bounds is clamped: the interval up, the counts down. */
3
- export function effectiveScheduleSettings(org, bounds, forbiddenToolsUnreadable = false) {
4
- return {
5
- ...org,
6
- forbiddenTools: [...org.forbiddenTools],
7
- forbiddenToolsUnreadable,
8
- minIntervalMinutes: Math.max(org.minIntervalMinutes, bounds.minIntervalFloorMinutes),
9
- maxTasksPerMember: Math.min(org.maxTasksPerMember, bounds.maxTasksPerMemberCap),
10
- maxTasksPerOrg: Math.min(org.maxTasksPerOrg, bounds.maxTasksPerOrgCap),
11
- maxRunsPerHour: Math.min(org.maxRunsPerHour, bounds.maxRunsPerHourCap),
12
- };
13
- }
14
- /** Read the org's effective settings live (PG and the current kernel config). */
15
- export async function readScheduleSettings(db, config, orgId) {
16
- const raw = await readStoredOrgSettings(db, orgId);
17
- return effectiveScheduleSettings(parseOrgSettings(raw).scheduledTasks, config.get().schedules, invalidStoredForbiddenTools(raw));
18
- }
@@ -1,8 +0,0 @@
1
- import { ApiError } from "../errors.js";
2
- /** The task is missing, deleted, in another org or not the caller's to see (never 403). */
3
- export declare const scheduleNotFound: () => ApiError;
4
- /** An org admin acting on another person's task in a way only its creator may (spec §10). */
5
- export declare const scheduleForbidden: () => ApiError;
6
- export declare const scheduleDisabled: () => ApiError;
7
- export declare const targetInvalid: (message: string) => ApiError;
8
- export declare const grantNotAllowed: (message: string) => ApiError;
@@ -1,8 +0,0 @@
1
- import { ApiError } from "../errors.js";
2
- /** The task is missing, deleted, in another org or not the caller's to see (never 403). */
3
- export const scheduleNotFound = () => new ApiError("schedule.not_found", "Scheduled task not found");
4
- /** An org admin acting on another person's task in a way only its creator may (spec §10). */
5
- export const scheduleForbidden = () => new ApiError("schedule.forbidden", "Only the task's creator can change its prompt, schedule, target or approved tools");
6
- export const scheduleDisabled = () => new ApiError("schedule.disabled", "Scheduled tasks are turned off in this organization");
7
- export const targetInvalid = (message) => new ApiError("schedule.target_invalid", message);
8
- export const grantNotAllowed = (message) => new ApiError("schedule.grant_not_allowed", message);
@@ -1,81 +0,0 @@
1
- import type { OutboxService } from "../infra/outbox/outbox.service.js";
2
- /** The scheduled tasks' jobs (a leaf module: type imports only). */
3
- /**
4
- * The once-a-minute `@LockedCron` that turns due tasks into task runs (spec §6); its name enqueued
5
- * on the maintenance queue runs one scan (`ScheduledRuns.runNow`).
6
- */
7
- export declare const SCHED_SCAN_JOB = "scheduled-task-scan";
8
- /** The one-off job that checks and starts one task run, on `cap-kernel-maintenance`. */
9
- export declare const SCHED_FIRE_JOB = "sched-fire";
10
- /** The data of a {@link SCHED_FIRE_JOB}. */
11
- export interface SchedFireJobData {
12
- taskRunId: string;
13
- }
14
- /** The fire job's id: `sched-fire-<taskRunId>` (no `:`, task run ids are UUIDs). */
15
- export declare const schedFireJobId: (taskRunId: string) => string;
16
- /**
17
- * Owes the fire job of task run `taskRunId` in the transaction in context (transactional outbox
18
- * spec §8.1): the transaction that writes a pending task run (the scan's tick, a person's "run
19
- * now") owes its fire, so a tick is never left unfired. The job's options are its outbox registry
20
- * entry's; the fire is idempotent on the task run's status.
21
- */
22
- export declare function oweSchedFire(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string): Promise<boolean>;
23
- /**
24
- * The delayed job that settles a task run left pending (ruling R26, fix round 1), on
25
- * `cap-kernel-maintenance`. Two kinds:
26
- * - the claimed check, owed by the fire job's claim: the run tracker links, settles or releases a
27
- * run claimed but never started (`sched-settle-<taskRunId>`, then `-<check>`);
28
- * - the unfired check, owed with the task run itself (the scan's tick, "run now"): a run its fire
29
- * job never claimed (the job failed for good before its claim) is skipped, `missed` for a tick,
30
- * `removed` for a "run now", so it never blocks the task's later ticks as an overlap
31
- * (`sched-settle-<taskRunId>-unfired`).
32
- */
33
- export declare const SCHED_SETTLE_JOB = "sched-settle";
34
- /**
35
- * The data of a {@link SCHED_SETTLE_JOB}: the claimed check (`check` counts the checks before this
36
- * one) or the unfired check.
37
- */
38
- export type SchedSettleJobData = {
39
- taskRunId: string;
40
- check: number;
41
- } | {
42
- taskRunId: string;
43
- unfired: true;
44
- };
45
- /**
46
- * The first claimed check: 10 minutes after the claim, when the start's retries (seconds) are long
47
- * over and the run tracker's start grace (`START_GRACE_MS`, 5 minutes) has passed, so a run that
48
- * left no trace is released at once. Also the unfired check's delay after a "run now".
49
- */
50
- export declare const SCHED_SETTLE_AFTER_MS: number;
51
- /**
52
- * A claimed run still in flight at a check (its item queued, its message posted and not yet taken
53
- * by a run) is checked again hourly. The chain ends with the first check that finds the run
54
- * started, ended or released: the queued item is consumed or removed, the message taken by a run.
55
- * A chain past {@link SCHED_SETTLE_LONG_CHECKS} checks (a week) is logged as a warning, once.
56
- */
57
- export declare const SCHED_SETTLE_RECHECK_MS: number;
58
- /** A week of hourly checks: the claimed check warns at this one (a run in flight that long). */
59
- export declare const SCHED_SETTLE_LONG_CHECKS = 168;
60
- /**
61
- * How long past the org's catch-up window a tick's fire may come before the tick is missed (the
62
- * fire job's own rule, and when a tick's unfired check runs): the scan fires a tick still inside
63
- * the window, and its fire job follows within seconds.
64
- */
65
- export declare const SCHED_LATE_FIRE_SLACK_MS: number;
66
- /** The claimed check's id: `sched-settle-<taskRunId>`, then `-<check>` for the later checks. */
67
- export declare const schedSettleJobId: (taskRunId: string, check: number) => string;
68
- /** The unfired check's id: `sched-settle-<taskRunId>-unfired`. */
69
- export declare const schedSettleUnfiredJobId: (taskRunId: string) => string;
70
- /**
71
- * Owes claimed check number `check` of task run `taskRunId` in the transaction in context, due
72
- * `after` milliseconds from now.
73
- */
74
- export declare function oweSchedSettle(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string, check: number, after: number): Promise<boolean>;
75
- /**
76
- * Owes the unfired check of task run `taskRunId` in the transaction that writes the run: for a
77
- * tick of the schedule (`catchUpWindowMinutes` given) when the fire job would itself call the
78
- * tick missed (its tick plus the window plus {@link SCHED_LATE_FIRE_SLACK_MS}, at least
79
- * {@link SCHED_SETTLE_AFTER_MS} on); for a "run now" (null) {@link SCHED_SETTLE_AFTER_MS} on.
80
- */
81
- export declare function oweSchedSettleUnfired(outbox: Pick<OutboxService, "add">, taskRunId: string, orgId: string, scheduledFor: Date, catchUpWindowMinutes: number | null): Promise<boolean>;