@springbrand/agent-runtime 0.3.0-alpha.1 → 0.3.0-alpha.10
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/package.json +6 -6
- package/src/adapter/cloudflare/agent.ts +157 -14
- package/src/adapter/cloudflare/define-agent.ts +12 -7
- package/src/adapter/cloudflare/index.ts +18 -0
- package/src/adapter/cloudflare/scheduling/schedule-rule.ts +322 -0
- package/src/adapter/cloudflare/scheduling/zoned-cron.ts +11 -6
- package/src/adapter/cloudflare/schema.ts +15 -0
- package/src/adapter/cloudflare/subagent/definition.ts +4 -4
- package/src/adapter/cloudflare/subagent/runner.ts +1 -1
- package/src/adapter/cloudflare/subagent/tools.ts +2 -2
- package/src/adapter/cloudflare/universal-agent/preparation.ts +3 -3
- package/src/adapter/cloudflare/universal-agent/tools.ts +4 -6
- package/src/adapter/cloudflare/workspace/version-control.ts +1 -1
- package/src/config/agent-context.ts +1 -1
- package/src/config/assembler.ts +29 -31
- package/src/config/assembly-snapshot.ts +1 -1
- package/src/config/definition.ts +1 -1
- package/src/config/harness-runtime.ts +9 -1
- package/src/config/model-guidance.ts +21 -0
- package/src/config/pinning.ts +22 -3
- package/src/config/ports.ts +27 -7
- package/src/config/public-contracts.ts +2 -2
- package/src/context/assembly.ts +1 -1
- package/src/context/extension/assembly.ts +2 -2
- package/src/context/memory/budget/gate.ts +19 -3
- package/src/conversation/harness-stream.ts +2 -1
- package/src/conversation/message/projection.ts +38 -21
- package/src/conversation/state.ts +23 -2
- package/src/harness/session-harness.ts +86 -5
- package/src/index.ts +7 -7
- package/src/lib/artifacts.ts +18 -0
- package/src/lib/execution-level.ts +13 -0
- package/src/lib/prompt.ts +7 -6
- package/src/lib/token-estimate.ts +8 -0
- package/src/models/index.ts +1 -0
- package/src/session/cloudflare/migration.ts +70 -6
- package/src/skills/springbrand-worker-website/index.ts +1 -1
- package/src/submission/admission.ts +12 -1
- package/src/submission/contracts.ts +42 -2
- package/src/submission/delivery.ts +1 -1
- package/src/submission/finalization.ts +1 -1
- package/src/submission/repository.ts +64 -4
- package/src/submission/unattended.ts +40 -0
- package/src/telemetry/coordinator.ts +1 -1
- package/src/telemetry/subagent-lifecycle.ts +1 -1
- package/src/telemetry/turn-events.ts +1 -1
- package/src/tools/README.md +44 -0
- package/src/tools/{ai-adapter.ts → adapters/ai-to-pi.ts} +6 -9
- package/src/tools/{declared.ts → adapters/declared.ts} +6 -2
- package/src/tools/{nested-tools.ts → adapters/pi-to-ai.ts} +2 -2
- package/src/tools/assistant/ask-user.ts +317 -0
- package/src/tools/assistant/candidate.ts +15 -0
- package/src/tools/assistant/followups.ts +34 -0
- package/src/tools/assistant/index.ts +19 -0
- package/src/tools/assistant/memory.ts +73 -0
- package/src/tools/assistant/plan.ts +98 -0
- package/src/tools/{time.ts → assistant/time.ts} +1 -1
- package/src/tools/{core-host.ts → codemode/adapter.ts} +50 -49
- package/src/tools/{core.ts → codemode/index.ts} +36 -92
- package/src/tools/codemode/workspace-connector.ts +198 -0
- package/src/tools/extension/index.ts +42 -0
- package/src/tools/index.ts +28 -26
- package/src/tools/{gateway.ts → mcp/gateway.ts} +19 -19
- package/src/tools/{mcp.ts → mcp/index.ts} +5 -5
- package/src/tools/{approval → runtime/approval}/repository.ts +13 -12
- package/src/tools/{compiler.ts → runtime/compiler.ts} +84 -147
- package/src/tools/runtime/contracts.ts +117 -0
- package/src/tools/{decision-gate.ts → runtime/decision-gate.ts} +147 -37
- package/src/tools/{human-decisions.ts → runtime/human-decisions.ts} +4 -4
- package/src/tools/{interaction → runtime/interaction}/repository.ts +1 -7
- package/src/tools/runtime/result.ts +22 -0
- package/src/tools/runtime/unattended-error.ts +4 -0
- package/src/tools/sandbox/index.ts +228 -0
- package/src/tools/schedule/index.ts +301 -0
- package/src/tools/skill/contracts.ts +20 -0
- package/src/tools/skill/index.ts +99 -0
- package/src/tools/skill/resources.ts +322 -0
- package/src/tools/skill/scripts.ts +168 -0
- package/src/tools/{subagent-authority.ts → subagent/authority.ts} +31 -31
- package/src/tools/{subagent.ts → subagent/index.ts} +34 -37
- package/src/tools/{subagent-run-repository.ts → subagent/repository.ts} +1 -1
- package/src/tools/{subagent-runtime.ts → subagent/runtime.ts} +1 -1
- package/src/tools/{temporary-agent → subagent/temporary}/approvals.ts +4 -7
- package/src/tools/{temporary-agent → subagent/temporary}/core.ts +1 -1
- package/src/tools/{temporary-agent → subagent/temporary}/runner.ts +1 -1
- package/src/tools/{temporary-agent → subagent/temporary}/workspace.ts +1 -4
- package/src/tools/{web-fetch.ts → web/fetch.ts} +4 -13
- package/src/tools/web/index.ts +2 -0
- package/src/tools/web/search/api.ts +1320 -0
- package/src/tools/web/search/index.ts +2 -0
- package/src/tools/{web-search/web-search.ts → web/search/search.ts} +5 -7
- package/src/tools/{workspace-sandbox.ts → workspace/index.ts} +12 -272
- package/src/tools/{workspace-revision.ts → workspace/revision.ts} +3 -3
- package/src/tools/base.ts +0 -534
- package/src/tools/schedule.ts +0 -261
- package/src/tools/skill.ts +0 -565
- package/src/tools/web-search/api.ts +0 -1318
- package/src/tools/web-search/index.ts +0 -2
- package/src/tools/workspace-state-connector.ts +0 -146
- package/src/tools/{approval → runtime/approval}/index.ts +0 -0
- package/src/tools/{interaction → runtime/interaction}/index.ts +0 -0
- package/src/tools/{temporary-agent → subagent/temporary}/coordinator.ts +0 -0
- package/src/tools/{subagent-types → subagent/types}/contract.ts +0 -0
- package/src/tools/{subagent-types → subagent/types}/extract/index.ts +0 -0
- package/src/tools/{subagent-types → subagent/types}/fanout/index.ts +0 -0
- package/src/tools/{subagent-types → subagent/types}/registry.ts +1 -1
- /package/src/tools/{workspace-versioning.ts → workspace/versioning.ts} +0 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
LocalScheduleRule,
|
|
3
|
+
ScheduleSpec,
|
|
4
|
+
} from "../../../submission/contracts";
|
|
5
|
+
import { isValidTimeZone, wallTimeInstants, zoneOffsetMs } from "./zoned-cron";
|
|
6
|
+
|
|
7
|
+
const DAY_MS = 86_400_000;
|
|
8
|
+
const HOUR_MS = 3_600_000;
|
|
9
|
+
const MINUTE_MS = 60_000;
|
|
10
|
+
/** 扫描上限:每日/每周规则一定在 7 天内出现,这里只兜底防止坏输入空转。 */
|
|
11
|
+
const MAX_SCAN_DAYS = 366 * 5;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 宿主允许在计划时点之后多久仍可启动该轮次。宿主可覆盖,
|
|
15
|
+
* 通用模块只把它当作可参数化的迟到窗口,不承载套餐或任务策略。
|
|
16
|
+
*/
|
|
17
|
+
export const DEFAULT_LATENESS_WINDOW_MS = 5 * MINUTE_MS;
|
|
18
|
+
|
|
19
|
+
export type ScheduleSkipReason = "dst_missing_local_time";
|
|
20
|
+
|
|
21
|
+
/** 一个真实可执行的本地时间轮次。 */
|
|
22
|
+
export interface ScheduledOccurrence {
|
|
23
|
+
kind: "scheduled";
|
|
24
|
+
/** 跨重算、重启都稳定的轮次身份,供宿主做幂等键。 */
|
|
25
|
+
occurrenceId: string;
|
|
26
|
+
/** 计划执行的 UTC 瞬时。 */
|
|
27
|
+
scheduledAt: number;
|
|
28
|
+
/** 任务时区下的本地日期 `YYYY-MM-DD`。 */
|
|
29
|
+
localDate: string;
|
|
30
|
+
/** 任务时区下的本地钟点 `HH:mm`。 */
|
|
31
|
+
localTime: string;
|
|
32
|
+
/** 含边界的最迟可启动瞬时 = `scheduledAt + latenessWindowMs`。 */
|
|
33
|
+
latestStartAt: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 因夏令时缺失钟点而被跳过的轮次;没有可执行瞬时,只留身份与原因。 */
|
|
37
|
+
export interface SkippedOccurrence {
|
|
38
|
+
kind: "skipped";
|
|
39
|
+
occurrenceId: string;
|
|
40
|
+
localDate: string;
|
|
41
|
+
localTime: string;
|
|
42
|
+
reason: ScheduleSkipReason;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type ScheduleOccurrence = ScheduledOccurrence | SkippedOccurrence;
|
|
46
|
+
|
|
47
|
+
/** 一条按任务时区解释的当地时间规则及其生效窗口。 */
|
|
48
|
+
export interface ScheduleRuleSpec {
|
|
49
|
+
rule: LocalScheduleRule;
|
|
50
|
+
/** IANA 时区名,如 `Asia/Shanghai`;绝不退化成固定 UTC offset。 */
|
|
51
|
+
timeZone: string;
|
|
52
|
+
/** 含当天的最早生效本地日期 `YYYY-MM-DD`。 */
|
|
53
|
+
startDate?: string;
|
|
54
|
+
/** 含当天的最后生效本地日期 `YYYY-MM-DD`。 */
|
|
55
|
+
endDate?: string;
|
|
56
|
+
/** 覆盖默认迟到窗口。 */
|
|
57
|
+
latenessWindowMs?: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** `ScheduleSpec.trigger` 里 `local` 变体的形状。 */
|
|
61
|
+
export type LocalScheduleTrigger = Extract<
|
|
62
|
+
ScheduleSpec["trigger"],
|
|
63
|
+
{ kind: "local" }
|
|
64
|
+
>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 把合同的 `local` 触发条件投影成时间规则计算规格。
|
|
68
|
+
*
|
|
69
|
+
* 宿主与路由共用同一映射,避免各自拼一份 `startDate`/`endDate`,改合同字段时漏改。
|
|
70
|
+
*/
|
|
71
|
+
export function toScheduleRuleSpec(
|
|
72
|
+
trigger: LocalScheduleTrigger,
|
|
73
|
+
timeZone: string,
|
|
74
|
+
): ScheduleRuleSpec {
|
|
75
|
+
return {
|
|
76
|
+
rule: trigger.rule,
|
|
77
|
+
timeZone,
|
|
78
|
+
...(trigger.startDate ? { startDate: trigger.startDate } : {}),
|
|
79
|
+
...(trigger.endDate ? { endDate: trigger.endDate } : {}),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export type ScheduleRuleErrorCode =
|
|
84
|
+
| "invalid_rule"
|
|
85
|
+
| "invalid_time_zone"
|
|
86
|
+
| "invalid_date"
|
|
87
|
+
| "invalid_time"
|
|
88
|
+
| "empty_weekdays"
|
|
89
|
+
| "invalid_weekday"
|
|
90
|
+
| "invalid_window"
|
|
91
|
+
| "invalid_limit"
|
|
92
|
+
| "no_future_occurrence";
|
|
93
|
+
|
|
94
|
+
export interface ScheduleRuleError {
|
|
95
|
+
code: ScheduleRuleErrorCode;
|
|
96
|
+
message: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type ScheduleOccurrenceResolution =
|
|
100
|
+
| { ok: true; occurrences: ScheduleOccurrence[] }
|
|
101
|
+
| { ok: false; error: ScheduleRuleError };
|
|
102
|
+
|
|
103
|
+
interface LocalClock {
|
|
104
|
+
hour: number;
|
|
105
|
+
minute: number;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 校验规则形状与时区,不判断是否存在未来时点。 */
|
|
109
|
+
export function validateScheduleRuleSpec(
|
|
110
|
+
spec: ScheduleRuleSpec,
|
|
111
|
+
): ScheduleRuleError | null {
|
|
112
|
+
if (typeof spec.timeZone !== "string" || !isValidTimeZone(spec.timeZone)) {
|
|
113
|
+
return { code: "invalid_time_zone", message: `未知时区:${spec.timeZone}` };
|
|
114
|
+
}
|
|
115
|
+
const rule = spec.rule as unknown;
|
|
116
|
+
if (!isRecord(rule) ||
|
|
117
|
+
(rule.kind !== "once" && rule.kind !== "daily" && rule.kind !== "weekly")) {
|
|
118
|
+
return { code: "invalid_rule", message: "不支持的当地时间规则" };
|
|
119
|
+
}
|
|
120
|
+
const time = typeof rule.time === "string" ? parseTime(rule.time) : null;
|
|
121
|
+
if (!time) {
|
|
122
|
+
return { code: "invalid_time", message: `非法钟点:${String(rule.time ?? "")}` };
|
|
123
|
+
}
|
|
124
|
+
if (rule.kind === "once" &&
|
|
125
|
+
(typeof rule.date !== "string" || parseDate(rule.date) === null)) {
|
|
126
|
+
return { code: "invalid_date", message: `非法日期:${String(rule.date ?? "")}` };
|
|
127
|
+
}
|
|
128
|
+
if (rule.kind === "weekly") {
|
|
129
|
+
if (!Array.isArray(rule.weekdays) || rule.weekdays.length === 0) {
|
|
130
|
+
if (Array.isArray(rule.weekdays)) {
|
|
131
|
+
return { code: "empty_weekdays", message: "每周规则至少需要一个星期" };
|
|
132
|
+
}
|
|
133
|
+
return { code: "invalid_weekday", message: "星期必须是数字数组" };
|
|
134
|
+
}
|
|
135
|
+
if (
|
|
136
|
+
rule.weekdays.some(
|
|
137
|
+
(weekday) => !Number.isInteger(weekday) || weekday < 0 || weekday > 6,
|
|
138
|
+
)
|
|
139
|
+
) {
|
|
140
|
+
return {
|
|
141
|
+
code: "invalid_weekday",
|
|
142
|
+
message: `非法星期:${rule.weekdays.join(",")}`,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const start = optionalDate(spec.startDate);
|
|
147
|
+
if (spec.startDate !== undefined && start === null) {
|
|
148
|
+
return { code: "invalid_date", message: `非法开始日期:${spec.startDate}` };
|
|
149
|
+
}
|
|
150
|
+
const end = optionalDate(spec.endDate);
|
|
151
|
+
if (spec.endDate !== undefined && end === null) {
|
|
152
|
+
return { code: "invalid_date", message: `非法结束日期:${spec.endDate}` };
|
|
153
|
+
}
|
|
154
|
+
if (start !== null && end !== null && end < start) {
|
|
155
|
+
return { code: "invalid_window", message: "结束日期不能早于开始日期" };
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 从 `after`(UTC 瞬时,不含)起枚举轮次,含被跳过的缺失钟点。
|
|
162
|
+
*
|
|
163
|
+
* 预览、下一轮计算和恢复枚举都走这里,保证三者语义一致;输出为空时返回可识别的
|
|
164
|
+
* `no_future_occurrence`,而不是伪造一个执行时间。
|
|
165
|
+
*/
|
|
166
|
+
export function resolveScheduleOccurrences(
|
|
167
|
+
spec: ScheduleRuleSpec,
|
|
168
|
+
options: { after: number; limit: number },
|
|
169
|
+
): ScheduleOccurrenceResolution {
|
|
170
|
+
const error = validateScheduleRuleSpec(spec);
|
|
171
|
+
if (error) return { ok: false, error };
|
|
172
|
+
if (!Number.isSafeInteger(options.limit) || options.limit <= 0) {
|
|
173
|
+
return {
|
|
174
|
+
ok: false,
|
|
175
|
+
error: { code: "invalid_limit", message: "轮次数量必须是正整数" },
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const occurrences = enumerate(spec, options.after, options.limit);
|
|
179
|
+
if (occurrences.length === 0) {
|
|
180
|
+
return {
|
|
181
|
+
ok: false,
|
|
182
|
+
error: {
|
|
183
|
+
code: "no_future_occurrence",
|
|
184
|
+
message: "该规则在给定时间之后没有有效执行时点",
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return { ok: true, occurrences };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 下一个可执行瞬时;缺失钟点被跳过,不伪造、不顺延补跑。 */
|
|
192
|
+
export function nextScheduledOccurrence(
|
|
193
|
+
spec: ScheduleRuleSpec,
|
|
194
|
+
after: number,
|
|
195
|
+
): ScheduledOccurrence | null {
|
|
196
|
+
if (validateScheduleRuleSpec(spec) !== null) return null;
|
|
197
|
+
for (let limit = 1; limit <= 512; limit *= 2) {
|
|
198
|
+
const occurrences = enumerate(spec, after, limit);
|
|
199
|
+
const scheduled = occurrences.find(
|
|
200
|
+
(occurrence): occurrence is ScheduledOccurrence =>
|
|
201
|
+
occurrence.kind === "scheduled",
|
|
202
|
+
);
|
|
203
|
+
if (scheduled) return scheduled;
|
|
204
|
+
if (occurrences.length < limit) return null;
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** 含边界判断某个已计划轮次是否仍在迟到窗口内。 */
|
|
210
|
+
export function isWithinLatenessWindow(
|
|
211
|
+
occurrence: ScheduledOccurrence,
|
|
212
|
+
now: number,
|
|
213
|
+
): boolean {
|
|
214
|
+
return now >= occurrence.scheduledAt && now <= occurrence.latestStartAt;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function enumerate(
|
|
218
|
+
spec: ScheduleRuleSpec,
|
|
219
|
+
after: number,
|
|
220
|
+
limit: number,
|
|
221
|
+
): ScheduleOccurrence[] {
|
|
222
|
+
const time = parseTime(spec.rule.time);
|
|
223
|
+
if (!time) return [];
|
|
224
|
+
const windowMs = spec.latenessWindowMs ?? DEFAULT_LATENESS_WINDOW_MS;
|
|
225
|
+
const wallAfter = after + zoneOffsetMs(after, spec.timeZone);
|
|
226
|
+
const dayOfAfter = Math.floor(wallAfter / DAY_MS) * DAY_MS;
|
|
227
|
+
const startDay = optionalDate(spec.startDate) ?? dayOfAfter;
|
|
228
|
+
const endDay = optionalDate(spec.endDate);
|
|
229
|
+
const occurrences: ScheduleOccurrence[] = [];
|
|
230
|
+
|
|
231
|
+
const onceDay = spec.rule.kind === "once" ? parseDate(spec.rule.date) : null;
|
|
232
|
+
// 一次性规则直接跳到目标日期,不能让重复规则的扫描保险上限拒绝远期任务。
|
|
233
|
+
let day = Math.max(startDay, dayOfAfter, onceDay ?? Number.NEGATIVE_INFINITY);
|
|
234
|
+
const maxScanDays = onceDay === null ? MAX_SCAN_DAYS : 0;
|
|
235
|
+
for (let scanned = 0; scanned <= maxScanDays; scanned += 1, day += DAY_MS) {
|
|
236
|
+
if (endDay !== null && day > endDay) break;
|
|
237
|
+
if (!matchesRule(spec.rule, day)) continue;
|
|
238
|
+
|
|
239
|
+
const wall = day + time.hour * HOUR_MS + time.minute * MINUTE_MS;
|
|
240
|
+
const localDate = formatDate(day);
|
|
241
|
+
const localTime = formatTime(time);
|
|
242
|
+
const occurrenceId = `${localDate}T${localTime}`;
|
|
243
|
+
const instants = wallTimeInstants(wall, spec.timeZone);
|
|
244
|
+
if (instants.length === 0) {
|
|
245
|
+
// 拨快抹掉的钟点:没有可执行瞬时,只在游标之后才作为跳过轮次报告。
|
|
246
|
+
if (wall <= wallAfter) continue;
|
|
247
|
+
occurrences.push({
|
|
248
|
+
kind: "skipped",
|
|
249
|
+
occurrenceId,
|
|
250
|
+
localDate,
|
|
251
|
+
localTime,
|
|
252
|
+
reason: "dst_missing_local_time",
|
|
253
|
+
});
|
|
254
|
+
} else {
|
|
255
|
+
const scheduledAt = instants[0];
|
|
256
|
+
if (scheduledAt <= after) continue;
|
|
257
|
+
occurrences.push({
|
|
258
|
+
kind: "scheduled",
|
|
259
|
+
occurrenceId,
|
|
260
|
+
scheduledAt,
|
|
261
|
+
localDate,
|
|
262
|
+
localTime,
|
|
263
|
+
latestStartAt: scheduledAt + windowMs,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (occurrences.length >= limit) break;
|
|
267
|
+
}
|
|
268
|
+
return occurrences;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function matchesRule(rule: LocalScheduleRule, day: number): boolean {
|
|
272
|
+
switch (rule.kind) {
|
|
273
|
+
case "once":
|
|
274
|
+
return day === parseDate(rule.date);
|
|
275
|
+
case "daily":
|
|
276
|
+
return true;
|
|
277
|
+
case "weekly":
|
|
278
|
+
return rule.weekdays.includes(new Date(day).getUTCDay());
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function parseTime(value: string): LocalClock | null {
|
|
283
|
+
const match = /^([01]\d|2[0-3]):([0-5]\d)$/.exec(value);
|
|
284
|
+
if (!match) return null;
|
|
285
|
+
return { hour: Number(match[1]), minute: Number(match[2]) };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function formatTime({ hour, minute }: LocalClock): string {
|
|
289
|
+
return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** `YYYY-MM-DD` → UTC 零点毫秒;非法日历日期返回 null。 */
|
|
293
|
+
function parseDate(value: string): number | null {
|
|
294
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
|
|
295
|
+
if (!match) return null;
|
|
296
|
+
const year = Number(match[1]);
|
|
297
|
+
const month = Number(match[2]);
|
|
298
|
+
const day = Number(match[3]);
|
|
299
|
+
const ms = Date.UTC(year, month - 1, day);
|
|
300
|
+
const date = new Date(ms);
|
|
301
|
+
if (
|
|
302
|
+
date.getUTCFullYear() !== year ||
|
|
303
|
+
date.getUTCMonth() !== month - 1 ||
|
|
304
|
+
date.getUTCDate() !== day
|
|
305
|
+
) {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
return ms;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function optionalDate(value: string | undefined): number | null {
|
|
312
|
+
if (value === undefined) return null;
|
|
313
|
+
return parseDate(value);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function formatDate(day: number): string {
|
|
317
|
+
return new Date(day).toISOString().slice(0, 10);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
321
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
322
|
+
}
|
|
@@ -55,7 +55,8 @@ export function nextCronOccurrence(
|
|
|
55
55
|
for (const second of parsed.seconds) {
|
|
56
56
|
const wall = day + hour * 3_600_000 + minute * 60_000 + second * 1_000;
|
|
57
57
|
if (wall <= wallAfter) continue;
|
|
58
|
-
const resolved =
|
|
58
|
+
const resolved = wallTimeInstants(wall, timeZone)
|
|
59
|
+
.find((instant) => instant > after);
|
|
59
60
|
if (resolved !== undefined) return resolved;
|
|
60
61
|
}
|
|
61
62
|
}
|
|
@@ -73,18 +74,22 @@ export function isValidTimeZone(timeZone: string): boolean {
|
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
|
|
77
|
+
/**
|
|
78
|
+
* 所有本地墙上时间恰好等于 `wallUtc` 的 UTC 瞬时,升序。
|
|
79
|
+
*
|
|
80
|
+
* `wallUtc` 是"把本地钟点当作 UTC"的伪瞬时(见 {@link zoneOffsetMs})。
|
|
81
|
+
* 返回空数组 = 该钟点在当地被夏令时拨快抹掉;返回两个 = 拨慢导致同一钟点出现两次。
|
|
82
|
+
* 这是 `nextCronOccurrence` 与本地规则计算共用的唯一钟点解析边界。
|
|
83
|
+
*/
|
|
84
|
+
export function wallTimeInstants(wallUtc: number, timeZone: string): number[] {
|
|
77
85
|
const offsets = new Set([
|
|
78
|
-
zoneOffsetMs(after, timeZone),
|
|
79
86
|
zoneOffsetMs(wallUtc - 2 * 86_400_000, timeZone),
|
|
80
87
|
zoneOffsetMs(wallUtc, timeZone),
|
|
81
88
|
zoneOffsetMs(wallUtc + 2 * 86_400_000, timeZone),
|
|
82
89
|
]);
|
|
83
90
|
return [...offsets]
|
|
84
91
|
.map((offset) => wallUtc - offset)
|
|
85
|
-
.filter((instant) =>
|
|
86
|
-
instant > after && instant + zoneOffsetMs(instant, timeZone) === wallUtc
|
|
87
|
-
)
|
|
92
|
+
.filter((instant) => instant + zoneOffsetMs(instant, timeZone) === wallUtc)
|
|
88
93
|
.sort((left, right) => left - right);
|
|
89
94
|
}
|
|
90
95
|
|
|
@@ -51,6 +51,18 @@ export function initializeSchema(sql: SqlTaggedTemplate): void {
|
|
|
51
51
|
if (!submissionColumns.has("regenerate_message_id")) {
|
|
52
52
|
sql`ALTER TABLE pi_submissions ADD COLUMN regenerate_message_id TEXT`;
|
|
53
53
|
}
|
|
54
|
+
if (!submissionColumns.has("origin")) {
|
|
55
|
+
sql`ALTER TABLE pi_submissions ADD COLUMN origin TEXT NOT NULL DEFAULT 'user'`;
|
|
56
|
+
}
|
|
57
|
+
if (!submissionColumns.has("scheduled_max_execution_level")) {
|
|
58
|
+
sql`ALTER TABLE pi_submissions ADD COLUMN scheduled_max_execution_level TEXT`;
|
|
59
|
+
}
|
|
60
|
+
if (!submissionColumns.has("unattended_execution_grant_json")) {
|
|
61
|
+
sql`ALTER TABLE pi_submissions ADD COLUMN unattended_execution_grant_json TEXT`;
|
|
62
|
+
}
|
|
63
|
+
if (!submissionColumns.has("unattended_failure")) {
|
|
64
|
+
sql`ALTER TABLE pi_submissions ADD COLUMN unattended_failure TEXT`;
|
|
65
|
+
}
|
|
54
66
|
sql`CREATE UNIQUE INDEX IF NOT EXISTS pi_submissions_one_running
|
|
55
67
|
ON pi_submissions(status)
|
|
56
68
|
WHERE status = 'running'`;
|
|
@@ -156,6 +168,9 @@ export function initializeSchema(sql: SqlTaggedTemplate): void {
|
|
|
156
168
|
.map((column) => column.name),
|
|
157
169
|
);
|
|
158
170
|
let columns = approvalColumns();
|
|
171
|
+
if (!columns.has("confirmation_json")) {
|
|
172
|
+
sql`ALTER TABLE pi_approvals ADD COLUMN confirmation_json TEXT`;
|
|
173
|
+
}
|
|
159
174
|
if (!columns.has("execution_level")) {
|
|
160
175
|
// 旧 Session 没有持久化当时的授权档位,safe 失败关闭;只影响旧 pending 回执展示,不会静默放行。
|
|
161
176
|
sql`ALTER TABLE pi_approvals
|
|
@@ -11,8 +11,8 @@ import type { HarnessEvent } from "@earendil-works/pi-agent-core";
|
|
|
11
11
|
import type { UIMessageChunk } from "ai";
|
|
12
12
|
import {
|
|
13
13
|
AGENT_TYPES,
|
|
14
|
-
} from "../../../tools/subagent
|
|
15
|
-
import type { AgentType } from "../../../tools/subagent
|
|
14
|
+
} from "../../../tools/subagent/types/registry";
|
|
15
|
+
import type { AgentType } from "../../../tools/subagent/types/contract";
|
|
16
16
|
import type {
|
|
17
17
|
RuntimeProviderPort,
|
|
18
18
|
WorkspacePort,
|
|
@@ -30,8 +30,8 @@ import {
|
|
|
30
30
|
decodeSubagentCompletion,
|
|
31
31
|
emptySubagentUsage,
|
|
32
32
|
encodeSubagentCompletion,
|
|
33
|
-
} from "../../../tools/subagent
|
|
34
|
-
import type { RuntimeSubagentUsageMetrics } from "../../../tools/subagent
|
|
33
|
+
} from "../../../tools/subagent/runtime";
|
|
34
|
+
import type { RuntimeSubagentUsageMetrics } from "../../../tools/subagent/authority";
|
|
35
35
|
import { createCloudflareSubAgentTools } from "./tools";
|
|
36
36
|
|
|
37
37
|
type RunStatus =
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
import type { Api, AssistantMessage, Model, Models } from "@earendil-works/pi-ai";
|
|
13
13
|
import { z } from "zod";
|
|
14
14
|
import { assembleSubagentPrompt } from "../../../lib/prompt";
|
|
15
|
-
import type { AgentType } from "../../../tools/subagent
|
|
15
|
+
import type { AgentType } from "../../../tools/subagent/types/contract";
|
|
16
16
|
import { structuredOutputStream } from "../../../models";
|
|
17
17
|
import {
|
|
18
18
|
openSessionHarness,
|
|
@@ -15,13 +15,13 @@ import type {
|
|
|
15
15
|
} from "@earendil-works/pi-agent-core";
|
|
16
16
|
import { Type } from "@earendil-works/pi-ai";
|
|
17
17
|
import type { WorkspacePort } from "../../../config/ports";
|
|
18
|
-
import type { AgentType } from "../../../tools/subagent
|
|
18
|
+
import type { AgentType } from "../../../tools/subagent/types/contract";
|
|
19
19
|
import {
|
|
20
20
|
compileHarnessTools,
|
|
21
21
|
type CompileHarnessToolsOptions,
|
|
22
22
|
type PiToolCandidate,
|
|
23
23
|
} from "../../../tools";
|
|
24
|
-
import { workspacePiToolCandidates } from "../../../tools/workspace
|
|
24
|
+
import { workspacePiToolCandidates } from "../../../tools/workspace";
|
|
25
25
|
|
|
26
26
|
const READONLY_WORKSPACE_TOOLS = new Set([
|
|
27
27
|
"read",
|
|
@@ -9,16 +9,16 @@ import type {
|
|
|
9
9
|
RuntimeSubagentPort,
|
|
10
10
|
WorkspacePort,
|
|
11
11
|
} from "../../../config/ports";
|
|
12
|
-
import { decodeSubagentCompletion } from "../../../tools/subagent
|
|
12
|
+
import { decodeSubagentCompletion } from "../../../tools/subagent/runtime";
|
|
13
13
|
import type { RuntimeDegradation } from "../../../config/degradation";
|
|
14
14
|
import type { RuntimeMemoryProfile } from "../../../config/profile";
|
|
15
|
-
import { AGENT_TYPES } from "../../../tools/subagent
|
|
15
|
+
import { AGENT_TYPES } from "../../../tools/subagent/types/registry";
|
|
16
16
|
import type { RuntimeAgentConfigContext } from "../../../config/agent-context";
|
|
17
17
|
import {
|
|
18
18
|
createBrowserExecutionFactory,
|
|
19
19
|
createWorkspaceCodeExecutionFactory,
|
|
20
20
|
type RuntimeBrowserBinding,
|
|
21
|
-
} from "../../../tools/
|
|
21
|
+
} from "../../../tools/codemode/adapter";
|
|
22
22
|
import {
|
|
23
23
|
createCloudflareSandboxAdapter,
|
|
24
24
|
type SandboxAdmission,
|
|
@@ -7,15 +7,13 @@ import type {
|
|
|
7
7
|
} from "../../../config/ports";
|
|
8
8
|
import type { RuntimeDegradation } from "../../../config/degradation";
|
|
9
9
|
import type { RuntimeMemoryProfile } from "../../../config/profile";
|
|
10
|
-
import type { WorkspaceRevisionRestorePort } from "../../../tools/workspace
|
|
10
|
+
import type { WorkspaceRevisionRestorePort } from "../../../tools/workspace/versioning";
|
|
11
11
|
import { schedulePiToolCandidates } from "../../../tools/schedule";
|
|
12
|
-
import { workspaceRevisionPiToolCandidate } from "../../../tools/workspace
|
|
13
|
-
import {
|
|
14
|
-
sandboxPiToolCandidates,
|
|
15
|
-
} from "../../../tools/workspace-sandbox";
|
|
12
|
+
import { workspaceRevisionPiToolCandidate } from "../../../tools/workspace/revision";
|
|
13
|
+
import { sandboxPiToolCandidates } from "../../../tools/sandbox";
|
|
16
14
|
import type { RuntimeCodeExecutionFactory } from "../../../config/ports";
|
|
17
15
|
import type { ToolAssemblyResult } from "../../../config/definition";
|
|
18
|
-
import type { PiToolCandidate } from "../../../tools/
|
|
16
|
+
import type { PiToolCandidate } from "../../../tools/runtime/contracts";
|
|
19
17
|
|
|
20
18
|
export function assembleUniversalAgentTools(options: {
|
|
21
19
|
hostTools?: readonly PiToolCandidate[];
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
type WorkspaceRevision,
|
|
6
6
|
type WorkspaceRevisionManifest,
|
|
7
7
|
type WorkspaceVersionPort,
|
|
8
|
-
} from "../../../tools/workspace
|
|
8
|
+
} from "../../../tools/workspace/versioning";
|
|
9
9
|
import { createWorkspaceGitFs } from "./git-fs";
|
|
10
10
|
|
|
11
11
|
const PAGE_SIZE = 256;
|
package/src/config/assembler.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { schedulePiToolCandidates } from "../tools/schedule";
|
|
1
2
|
import type { SkillSource } from "agents/skills";
|
|
2
3
|
import type {
|
|
3
4
|
RuntimeGatewayPort,
|
|
@@ -36,10 +37,7 @@ import {
|
|
|
36
37
|
type PiRuntimeAssembly,
|
|
37
38
|
} from "./assembly-snapshot";
|
|
38
39
|
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
39
|
-
import type {
|
|
40
|
-
PiToolCandidate,
|
|
41
|
-
ToolExposureMode,
|
|
42
|
-
} from "../tools/compiler";
|
|
40
|
+
import type { PiToolCandidate, ToolExposureMode } from "../tools/runtime/contracts";
|
|
43
41
|
import type {
|
|
44
42
|
AssembleRuntimeSnapshotInput,
|
|
45
43
|
RuntimeSettings,
|
|
@@ -50,17 +48,11 @@ import type {
|
|
|
50
48
|
RuntimeToolSurfacePolicy,
|
|
51
49
|
} from "./definition";
|
|
52
50
|
import type { RuntimeCodeExecutionFactory } from "./ports";
|
|
53
|
-
import {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
} from "../tools/base";
|
|
57
|
-
import {
|
|
58
|
-
BROWSER_EXECUTE_TOOL_NAME,
|
|
59
|
-
browserExecutionPiToolCandidate,
|
|
60
|
-
codeExecutionPiToolCandidate,
|
|
61
|
-
} from "../tools/core";
|
|
51
|
+
import { basePiToolCandidates } from "../tools/assistant";
|
|
52
|
+
import { memoryPiToolCandidate } from "../tools/assistant/memory";
|
|
53
|
+
import { BROWSER_EXECUTE_TOOL_NAME, browserExecutionPiToolCandidate, codeExecutionPiToolCandidate } from "../tools/codemode";
|
|
62
54
|
import { skillPiToolCandidates } from "../tools/skill";
|
|
63
|
-
import { createWebSearch, WEB_SEARCH_MODEL } from "../tools/web
|
|
55
|
+
import { createWebSearch, WEB_SEARCH_MODEL } from "../tools/web/search";
|
|
64
56
|
import { subagentPiToolCandidates } from "../tools/subagent";
|
|
65
57
|
import { resolvePiModel } from "../models";
|
|
66
58
|
import {
|
|
@@ -264,6 +256,16 @@ async function createToolSurface(
|
|
|
264
256
|
if (!EXECUTION_LEVELS.includes(candidate.requiredExecutionLevel)) {
|
|
265
257
|
throw new Error(`SpringBrand Tool ${name} execution level is invalid`);
|
|
266
258
|
}
|
|
259
|
+
if (candidate.unattendedExecution) {
|
|
260
|
+
requiredName(
|
|
261
|
+
candidate.unattendedExecution.capability,
|
|
262
|
+
`SpringBrand Tool ${name} unattended capability`,
|
|
263
|
+
);
|
|
264
|
+
requiredName(
|
|
265
|
+
candidate.unattendedExecution.operation,
|
|
266
|
+
`SpringBrand Tool ${name} unattended operation`,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
267
269
|
if (tools.has(name)) {
|
|
268
270
|
throw new Error(`Duplicate Runtime SpringBrand Tool: ${name}`);
|
|
269
271
|
}
|
|
@@ -294,17 +296,12 @@ async function createToolSurface(
|
|
|
294
296
|
typeof candidate.tool.execute === "function",
|
|
295
297
|
);
|
|
296
298
|
const codeExecutionCandidates = Object.freeze([...mergeable]);
|
|
297
|
-
const codeExecutionToolNames = new Set(
|
|
298
|
-
codeExecutionCandidates.map(({ tool }) => tool.name),
|
|
299
|
-
);
|
|
300
299
|
const codeExecution = input.codeExecution.create(
|
|
301
300
|
codeExecutionCandidates,
|
|
302
301
|
);
|
|
303
|
-
const
|
|
304
|
-
({ tool }) => !codeExecutionToolNames.has(tool.name),
|
|
305
|
-
);
|
|
302
|
+
const directToolNames = new Set(directVisible.map(({ tool }) => tool.name));
|
|
306
303
|
const routeList = (deferred: boolean) => {
|
|
307
|
-
const matches =
|
|
304
|
+
const matches = directVisible.filter((candidate) =>
|
|
308
305
|
(defersTool?.(candidate.tool.name) === true) === deferred
|
|
309
306
|
);
|
|
310
307
|
return matches.length > 0
|
|
@@ -316,8 +313,9 @@ async function createToolSurface(
|
|
|
316
313
|
};
|
|
317
314
|
const routeGuidance = [
|
|
318
315
|
"## Tool routing",
|
|
319
|
-
"
|
|
316
|
+
"For top-level deferred Tools, use Provider Tool Search with their exact name, then call them directly.",
|
|
320
317
|
"Top-level immediate Tools are already available outside execute; call them directly.",
|
|
318
|
+
"Only Code Mode Tools listed below are callable inside execute through tools.<name>. Tools marked execute-only are not registered as top-level Tools.",
|
|
321
319
|
"",
|
|
322
320
|
"## Top-level deferred Tools",
|
|
323
321
|
routeList(true),
|
|
@@ -325,21 +323,20 @@ async function createToolSurface(
|
|
|
325
323
|
"## Top-level immediate Tools",
|
|
326
324
|
routeList(false),
|
|
327
325
|
"",
|
|
326
|
+
"## Code Mode Tools",
|
|
327
|
+
codeExecutionCandidates.length > 0
|
|
328
|
+
? codeExecutionCandidates.map(({ tool }) =>
|
|
329
|
+
`- \`tools.${tool.name}\` — ${directToolNames.has(tool.name) ? "also available as a top-level Tool" : "execute-only"}`
|
|
330
|
+
).join("\n")
|
|
331
|
+
: "- None.",
|
|
332
|
+
"",
|
|
328
333
|
].join("\n");
|
|
329
|
-
const materializeGuidance = codeExecutionCandidates.some(
|
|
330
|
-
({ tool }) => tool.name === "materialize_skill_resource",
|
|
331
|
-
)
|
|
332
|
-
? "Skill resource copying is available only inside execute through " +
|
|
333
|
-
"`tools.materialize_skill_resource({ name, path, destination })`; " +
|
|
334
|
-
"no Direct Tool is registered for it. " +
|
|
335
|
-
"Copy multiple resources sequentially in one execute so durable replay preserves call order.\n\n"
|
|
336
|
-
: "";
|
|
337
334
|
return Object.freeze({
|
|
338
335
|
candidates: Object.freeze([
|
|
339
336
|
classify(codeExecutionPiToolCandidate({
|
|
340
337
|
...codeExecution,
|
|
341
338
|
description:
|
|
342
|
-
routeGuidance + "\n" +
|
|
339
|
+
routeGuidance + "\n" + codeExecution.description,
|
|
343
340
|
})),
|
|
344
341
|
...directVisible.map(classify),
|
|
345
342
|
]),
|
|
@@ -632,6 +629,7 @@ class RuntimeBuilder {
|
|
|
632
629
|
: {}),
|
|
633
630
|
hostTools: [
|
|
634
631
|
...this.hostTools,
|
|
632
|
+
...(this.schedule ? schedulePiToolCandidates(this.schedule) : []),
|
|
635
633
|
...subagentPiToolCandidates(
|
|
636
634
|
this.subagents,
|
|
637
635
|
[...this.enabledSubagents],
|
|
@@ -18,7 +18,7 @@ import type {
|
|
|
18
18
|
import type { RuntimeProviderPort } from "./ports";
|
|
19
19
|
import { assembleSystemPrompt } from "../lib/prompt";
|
|
20
20
|
import { resolvePiModel } from "../models";
|
|
21
|
-
import type { PiToolCandidate } from "../tools/
|
|
21
|
+
import type { PiToolCandidate } from "../tools/runtime/contracts";
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* 收集完动态 Extension/MCP 候选后,一次性生成最终 Tool Surface。
|
package/src/config/definition.ts
CHANGED
|
@@ -17,7 +17,7 @@ import type {
|
|
|
17
17
|
} from "./profile";
|
|
18
18
|
import type { RuntimeExtensionConfig } from "../context/extension/contracts";
|
|
19
19
|
import type { RuntimeDegradation } from "./degradation";
|
|
20
|
-
import type { PiToolCandidate } from "../tools/
|
|
20
|
+
import type { PiToolCandidate } from "../tools/runtime/contracts";
|
|
21
21
|
import type {
|
|
22
22
|
RuntimeAgentContext,
|
|
23
23
|
RuntimeAgentPlanningContext,
|