@tidegap/core 0.19.0 → 0.21.0
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/README.md +1 -1
- package/dist/agent-snippet.d.ts +30 -0
- package/dist/agent-snippet.js +120 -0
- package/dist/channels/commands.d.ts +137 -0
- package/dist/channels/commands.js +377 -0
- package/dist/channels/delivery-codes.d.ts +9 -0
- package/dist/channels/delivery-codes.js +53 -0
- package/dist/channels/digest.d.ts +102 -0
- package/dist/channels/digest.js +146 -0
- package/dist/channels/format.d.ts +138 -0
- package/dist/channels/format.js +425 -0
- package/dist/channels/index.d.ts +15 -0
- package/dist/channels/index.js +15 -0
- package/dist/channels/kinds/command.d.ts +13 -0
- package/dist/channels/kinds/command.js +65 -0
- package/dist/channels/kinds/discord.d.ts +16 -0
- package/dist/channels/kinds/discord.js +67 -0
- package/dist/channels/kinds/email.d.ts +20 -0
- package/dist/channels/kinds/email.js +162 -0
- package/dist/channels/kinds/index.d.ts +8 -0
- package/dist/channels/kinds/index.js +17 -0
- package/dist/channels/kinds/ntfy.d.ts +25 -0
- package/dist/channels/kinds/ntfy.js +163 -0
- package/dist/channels/kinds/pushover.d.ts +11 -0
- package/dist/channels/kinds/pushover.js +77 -0
- package/dist/channels/kinds/shared.d.ts +25 -0
- package/dist/channels/kinds/shared.js +39 -0
- package/dist/channels/kinds/shortcuts.d.ts +9 -0
- package/dist/channels/kinds/shortcuts.js +45 -0
- package/dist/channels/kinds/slack.d.ts +8 -0
- package/dist/channels/kinds/slack.js +57 -0
- package/dist/channels/kinds/telegram.d.ts +4 -0
- package/dist/channels/kinds/telegram.js +26 -0
- package/dist/channels/kinds/webhook.d.ts +30 -0
- package/dist/channels/kinds/webhook.js +177 -0
- package/dist/channels/payload.d.ts +36 -0
- package/dist/channels/payload.js +61 -0
- package/dist/channels/plan.d.ts +24 -0
- package/dist/channels/plan.js +66 -0
- package/dist/channels/registry.d.ts +191 -0
- package/dist/channels/registry.js +125 -0
- package/dist/channels/telegram.d.ts +110 -0
- package/dist/channels/telegram.js +168 -0
- package/dist/channels/types.d.ts +107 -0
- package/dist/channels/types.js +26 -0
- package/dist/claude-tools.d.ts +29 -0
- package/dist/claude-tools.js +49 -0
- package/dist/data/tables.generated.d.ts +6 -0
- package/dist/data/tables.generated.js +37 -0
- package/dist/energy.d.ts +84 -0
- package/dist/energy.js +138 -0
- package/dist/grid.d.ts +17 -2
- package/dist/grid.js +43 -4
- package/dist/handoff.d.ts +171 -0
- package/dist/handoff.js +482 -0
- package/dist/history.d.ts +17 -0
- package/dist/history.js +14 -1
- package/dist/index.d.ts +49 -15
- package/dist/index.js +36 -8
- package/dist/job-cadence.d.ts +76 -0
- package/dist/job-cadence.js +240 -0
- package/dist/jobs.d.ts +183 -0
- package/dist/jobs.js +917 -0
- package/dist/notify.d.ts +11 -0
- package/dist/notify.js +19 -4
- package/dist/postpone.d.ts +113 -0
- package/dist/postpone.js +185 -0
- package/dist/providers/anthropic.js +14 -8
- package/dist/providers/base.d.ts +145 -2
- package/dist/providers/base.js +41 -1
- package/dist/providers/budget.d.ts +25 -0
- package/dist/providers/budget.js +57 -0
- package/dist/providers/defaults.js +4 -0
- package/dist/providers/defer.d.ts +16 -0
- package/dist/providers/defer.js +31 -0
- package/dist/providers/gemini.d.ts +4 -0
- package/dist/providers/gemini.js +34 -24
- package/dist/providers/http.d.ts +40 -0
- package/dist/providers/http.js +82 -0
- package/dist/providers/index.d.ts +2 -2
- package/dist/providers/index.js +1 -1
- package/dist/providers/ollama.d.ts +4 -0
- package/dist/providers/ollama.js +32 -25
- package/dist/providers/openai.js +11 -9
- package/dist/providers/timeout.d.ts +36 -0
- package/dist/providers/timeout.js +71 -0
- package/dist/quota.d.ts +7 -2
- package/dist/quota.js +12 -27
- package/dist/readiness.d.ts +205 -0
- package/dist/readiness.js +326 -0
- package/dist/redact.d.ts +42 -0
- package/dist/redact.js +110 -0
- package/dist/region-host.d.ts +30 -0
- package/dist/region-host.js +69 -0
- package/dist/region.d.ts +102 -20
- package/dist/region.js +175 -23
- package/dist/run-limits.d.ts +268 -0
- package/dist/run-limits.js +572 -0
- package/dist/run-session.d.ts +36 -0
- package/dist/run-session.js +72 -0
- package/dist/schedule-job.d.ts +118 -0
- package/dist/schedule-job.js +243 -0
- package/dist/scheduler.d.ts +391 -18
- package/dist/scheduler.js +1341 -177
- package/dist/sign.d.ts +8 -1
- package/dist/sign.js +49 -3
- package/dist/storage/sqlite.d.ts +183 -5
- package/dist/storage/sqlite.js +720 -19
- package/dist/task-events.d.ts +117 -0
- package/dist/task-events.js +345 -0
- package/dist/tool-surface.d.ts +31 -2
- package/dist/tool-surface.js +390 -65
- package/dist/types.d.ts +267 -3
- package/dist/types.js +6 -1
- package/dist/zone-time.d.ts +22 -0
- package/dist/zone-time.js +76 -0
- package/package.json +13 -5
- package/dist/aggregator.d.ts.map +0 -1
- package/dist/aggregator.js.map +0 -1
- package/dist/budget.d.ts.map +0 -1
- package/dist/budget.js.map +0 -1
- package/dist/data/tables.generated.d.ts.map +0 -1
- package/dist/data/tables.generated.js.map +0 -1
- package/dist/energy.d.ts.map +0 -1
- package/dist/energy.js.map +0 -1
- package/dist/grid.d.ts.map +0 -1
- package/dist/grid.js.map +0 -1
- package/dist/history.d.ts.map +0 -1
- package/dist/history.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/notify.d.ts.map +0 -1
- package/dist/notify.js.map +0 -1
- package/dist/paths.d.ts.map +0 -1
- package/dist/paths.js.map +0 -1
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/base.d.ts.map +0 -1
- package/dist/providers/base.js.map +0 -1
- package/dist/providers/defaults.d.ts.map +0 -1
- package/dist/providers/defaults.js.map +0 -1
- package/dist/providers/gemini.d.ts.map +0 -1
- package/dist/providers/gemini.js.map +0 -1
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/ollama.d.ts.map +0 -1
- package/dist/providers/ollama.js.map +0 -1
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js.map +0 -1
- package/dist/quota.d.ts.map +0 -1
- package/dist/quota.js.map +0 -1
- package/dist/recommend.d.ts.map +0 -1
- package/dist/recommend.js.map +0 -1
- package/dist/redact.d.ts.map +0 -1
- package/dist/redact.js.map +0 -1
- package/dist/region.d.ts.map +0 -1
- package/dist/region.js.map +0 -1
- package/dist/routing.d.ts.map +0 -1
- package/dist/routing.js.map +0 -1
- package/dist/scheduler.d.ts.map +0 -1
- package/dist/scheduler.js.map +0 -1
- package/dist/select-window.d.ts.map +0 -1
- package/dist/select-window.js.map +0 -1
- package/dist/sign.d.ts.map +0 -1
- package/dist/sign.js.map +0 -1
- package/dist/storage/sqlite.d.ts.map +0 -1
- package/dist/storage/sqlite.js.map +0 -1
- package/dist/tool-surface.d.ts.map +0 -1
- package/dist/tool-surface.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Task events (audit trail): one row per change of a task, in the ledger's
|
|
3
|
+
* `task_events` table, written in the same transaction as the task write.
|
|
4
|
+
* Shared by both cores; the Python core writes `dispatched`, `completed`,
|
|
5
|
+
* `failed`, `attempt_failed` and `cancelled` rows with the same shape.
|
|
6
|
+
*/
|
|
7
|
+
import type { TaskRecord } from "./types.js";
|
|
8
|
+
export declare const TASK_EVENT_KINDS: readonly ["created", "rescheduled", "postponed", "expedited", "cancelled", "dispatched", "completed", "failed", "attempt_failed", "deferred", "replied", "notified", "result_file", "progress"];
|
|
9
|
+
export type TaskEventKind = (typeof TASK_EVENT_KINDS)[number];
|
|
10
|
+
/**
|
|
11
|
+
* Who made the change: `agent:<host>`, `page`, `client:<name>`, `cli`,
|
|
12
|
+
* `tick`, `telegram`, `openclaw` or `system`.
|
|
13
|
+
*/
|
|
14
|
+
export type TaskEventActor = string;
|
|
15
|
+
export interface TaskEvent {
|
|
16
|
+
id: number;
|
|
17
|
+
taskId: string;
|
|
18
|
+
/** ISO-8601. */
|
|
19
|
+
at: string;
|
|
20
|
+
kind: TaskEventKind;
|
|
21
|
+
actor: TaskEventActor;
|
|
22
|
+
/** snake_case keys: `from` / `to` timing, `reason`, `preset`, `provider`, `model`, `error`… */
|
|
23
|
+
details: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
/** An event to write with a task change; `taskId` and `at` come from the write. */
|
|
26
|
+
export interface TaskEventInput {
|
|
27
|
+
kind: TaskEventKind;
|
|
28
|
+
actor?: TaskEventActor;
|
|
29
|
+
details?: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A valid actor string, or `system`. `agent:` and `client:` names lose
|
|
33
|
+
* control characters and are cut to 64 characters. Never throws: an audit
|
|
34
|
+
* write must not block a task action.
|
|
35
|
+
*/
|
|
36
|
+
export declare function normalizeActor(actor: unknown): TaskEventActor;
|
|
37
|
+
export declare function isTaskEventKind(kind: unknown): kind is TaskEventKind;
|
|
38
|
+
/**
|
|
39
|
+
* The details of one event: `from` / `to` timing for moves and run-now,
|
|
40
|
+
* provider and model for created / dispatched / completed / failed /
|
|
41
|
+
* attempt_failed, the first line of the error for failed, then `extra`.
|
|
42
|
+
*/
|
|
43
|
+
export declare function eventDetails(kind: TaskEventKind, prev: TaskRecord<unknown> | undefined, next: TaskRecord<unknown>, extra?: Record<string, unknown>): Record<string, unknown>;
|
|
44
|
+
/** The kind a status change implies when the writer named none (terminal moves only). */
|
|
45
|
+
export declare function impliedEventKind(prevStatus: string, nextStatus: string): TaskEventKind | undefined;
|
|
46
|
+
/** A one-line summary of the details, or "". */
|
|
47
|
+
export declare function eventSummary(e: Pick<TaskEvent, "kind" | "details">): string;
|
|
48
|
+
/**
|
|
49
|
+
* One line per failed attempt the retry policy queued again, since the
|
|
50
|
+
* newest `created` event or manual retry, for the text views: key
|
|
51
|
+
* `attempt_<n>`, text `failed <at> (<class>): <error>; next attempt from
|
|
52
|
+
* <retry_at>`. `events` are oldest first.
|
|
53
|
+
*/
|
|
54
|
+
export declare function attemptLines(events: readonly Pick<TaskEvent, "at" | "kind" | "details">[]): {
|
|
55
|
+
key: string;
|
|
56
|
+
text: string;
|
|
57
|
+
}[];
|
|
58
|
+
/** `<at> <kind> by <actor>[ - <summary>]`, one line. */
|
|
59
|
+
export declare function formatTaskEvent(e: Pick<TaskEvent, "at" | "kind" | "actor" | "details">): string;
|
|
60
|
+
/** The API / JSON shape of one event. */
|
|
61
|
+
export declare function taskEventJson(e: TaskEvent): {
|
|
62
|
+
at: string;
|
|
63
|
+
kind: TaskEventKind;
|
|
64
|
+
actor: string;
|
|
65
|
+
details: Record<string, unknown>;
|
|
66
|
+
};
|
|
67
|
+
/** A run reports no progress in its first 5 seconds: a fast run writes none. */
|
|
68
|
+
export declare const PROGRESS_QUIET_MS = 5000;
|
|
69
|
+
/** At most one progress event per 15 seconds. */
|
|
70
|
+
export declare const PROGRESS_GAP_MS = 15000;
|
|
71
|
+
/** At most 24 progress events per task, so its `created` event stays inside a page of 50. */
|
|
72
|
+
export declare const PROGRESS_MAX_EVENTS = 24;
|
|
73
|
+
/** Longest progress line. */
|
|
74
|
+
export declare const PROGRESS_LINE_MAX_CHARS = 200;
|
|
75
|
+
/**
|
|
76
|
+
* Longest wait of one progress write for another writer's lock, in ms. The
|
|
77
|
+
* tick writes progress on its event loop; past this the line is dropped.
|
|
78
|
+
*/
|
|
79
|
+
export declare const PROGRESS_BUSY_MS = 100;
|
|
80
|
+
/**
|
|
81
|
+
* Untrusted output text without terminal escape sequences, control
|
|
82
|
+
* characters (a tab becomes a space), line separators or bidirectional
|
|
83
|
+
* controls. An adapter cleans a line with this before it scrubs secrets, so
|
|
84
|
+
* a removed character cannot join a secret after the scrub.
|
|
85
|
+
*/
|
|
86
|
+
export declare function cleanProgressText(raw: string): string;
|
|
87
|
+
/**
|
|
88
|
+
* One progress line as it is stored: cleaned by {@link cleanProgressText},
|
|
89
|
+
* trailing space trimmed, cut to {@link PROGRESS_LINE_MAX_CHARS}
|
|
90
|
+
* characters. Undefined for a blank line.
|
|
91
|
+
*/
|
|
92
|
+
export declare function progressLine(raw: string): string | undefined;
|
|
93
|
+
/** The `progress` callback of one run and its end. */
|
|
94
|
+
export interface ProgressRecorder {
|
|
95
|
+
/** A line the run printed. Lines that come while no slot is open replace each other; the newest is written when one opens. */
|
|
96
|
+
onLine(line: string): void;
|
|
97
|
+
/** The run ended: a line still waiting for its slot is dropped, and nothing is written after this. */
|
|
98
|
+
close(): void;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The caps of a run's progress events, for every adapter: nothing in the
|
|
102
|
+
* first {@link PROGRESS_QUIET_MS}, then at most one event per
|
|
103
|
+
* {@link PROGRESS_GAP_MS} and {@link PROGRESS_MAX_EVENTS} per task, each line
|
|
104
|
+
* cleaned by {@link progressLine}. A line that comes before its slot waits
|
|
105
|
+
* for it, and a newer line takes its place, so the event shows the newest
|
|
106
|
+
* line. `write` gets the details of each event (`line`, `seq`); `seq`
|
|
107
|
+
* counts the task's progress events from 1, after the `existing` ones (read
|
|
108
|
+
* once, when the first event is due). The caller scrubs secrets from each
|
|
109
|
+
* line first. Never throws.
|
|
110
|
+
*/
|
|
111
|
+
export declare function createProgressRecorder(write: (details: {
|
|
112
|
+
line: string;
|
|
113
|
+
seq: number;
|
|
114
|
+
}) => void, opts?: {
|
|
115
|
+
existing?: () => number;
|
|
116
|
+
}): ProgressRecorder;
|
|
117
|
+
//# sourceMappingURL=task-events.d.ts.map
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Task events (audit trail): one row per change of a task, in the ledger's
|
|
3
|
+
* `task_events` table, written in the same transaction as the task write.
|
|
4
|
+
* Shared by both cores; the Python core writes `dispatched`, `completed`,
|
|
5
|
+
* `failed`, `attempt_failed` and `cancelled` rows with the same shape.
|
|
6
|
+
*/
|
|
7
|
+
import { includesWebTool } from "./claude-tools.js";
|
|
8
|
+
import { stripControlChars } from "./history.js";
|
|
9
|
+
export const TASK_EVENT_KINDS = [
|
|
10
|
+
"created",
|
|
11
|
+
"rescheduled",
|
|
12
|
+
"postponed",
|
|
13
|
+
"expedited",
|
|
14
|
+
"cancelled",
|
|
15
|
+
"dispatched",
|
|
16
|
+
"completed",
|
|
17
|
+
"failed",
|
|
18
|
+
// A run failed and the retry policy queued another attempt: the task is
|
|
19
|
+
// `scheduled` again. Not terminal: no notice, wake, result file or series
|
|
20
|
+
// roll. Details: `attempt`, `failure_class`, `error` (first line),
|
|
21
|
+
// `retry_at`, and the run's own keys (`steps`, `stopped_by`).
|
|
22
|
+
"attempt_failed",
|
|
23
|
+
"deferred",
|
|
24
|
+
"replied",
|
|
25
|
+
// A Claude Code session was told the task finished (its prompt hook, a
|
|
26
|
+
// `queue wait` that printed the result, or a wake). At most one per finish:
|
|
27
|
+
// details carry the task's `completed_at`.
|
|
28
|
+
"notified",
|
|
29
|
+
// The tick wrote the task's result file, or could not (`written: false`
|
|
30
|
+
// with the error). At most one per task.
|
|
31
|
+
"result_file",
|
|
32
|
+
// A line a long `command` run printed while it ran (`line`, `seq`), within
|
|
33
|
+
// the caps of `createProgressRecorder`.
|
|
34
|
+
"progress",
|
|
35
|
+
];
|
|
36
|
+
const FIXED_ACTORS = new Set(["page", "cli", "tick", "telegram", "openclaw", "system"]);
|
|
37
|
+
const NAME_MAX = 64;
|
|
38
|
+
/**
|
|
39
|
+
* A valid actor string, or `system`. `agent:` and `client:` names lose
|
|
40
|
+
* control characters and are cut to 64 characters. Never throws: an audit
|
|
41
|
+
* write must not block a task action.
|
|
42
|
+
*/
|
|
43
|
+
export function normalizeActor(actor) {
|
|
44
|
+
if (typeof actor !== "string")
|
|
45
|
+
return "system";
|
|
46
|
+
if (FIXED_ACTORS.has(actor))
|
|
47
|
+
return actor;
|
|
48
|
+
const m = /^(agent|client):(.*)$/s.exec(actor);
|
|
49
|
+
if (!m)
|
|
50
|
+
return "system";
|
|
51
|
+
// eslint-disable-next-line no-control-regex
|
|
52
|
+
const name = m[2].replace(/[\u0000-\u001f\u007f]/g, "").trim().slice(0, NAME_MAX);
|
|
53
|
+
return name ? `${m[1]}:${name}` : "system";
|
|
54
|
+
}
|
|
55
|
+
export function isTaskEventKind(kind) {
|
|
56
|
+
return typeof kind === "string" && TASK_EVENT_KINDS.includes(kind);
|
|
57
|
+
}
|
|
58
|
+
function timing(r) {
|
|
59
|
+
return { scheduled_for: r.scheduledFor ?? null, not_before: r.notBefore ?? null, deadline: r.deadline ?? null };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The run facts of a `created` or `dispatched` event. Every `claude-code`
|
|
63
|
+
* row gets its planned mode: `fork` when it can run in a copy of its origin
|
|
64
|
+
* session (a checked origin, which a job occurrence never has, no web
|
|
65
|
+
* tool, and its folder is the session's), else `fresh`; the receipt's `session` records
|
|
66
|
+
* what the run did. A `claude-code` row with handoff fields also gets its
|
|
67
|
+
* profile, tools and folder, and a one-off command its folder. Empty for
|
|
68
|
+
* every other row.
|
|
69
|
+
*/
|
|
70
|
+
function handoffDetails(r) {
|
|
71
|
+
if (!r.bodyJson)
|
|
72
|
+
return {};
|
|
73
|
+
let spec;
|
|
74
|
+
try {
|
|
75
|
+
spec = JSON.parse(r.bodyJson);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return {};
|
|
79
|
+
}
|
|
80
|
+
if (spec?.provider === "command" && spec.command?.origin === "cli")
|
|
81
|
+
return { cwd: spec.command.cwd ?? null };
|
|
82
|
+
if (spec?.provider !== "claude-code")
|
|
83
|
+
return {};
|
|
84
|
+
const handoff = spec.handoff && typeof spec.handoff === "object" ? spec.handoff : undefined;
|
|
85
|
+
const tools = Array.isArray(spec.tools) ? spec.tools : null;
|
|
86
|
+
const cwd = typeof handoff?.cwd === "string" ? handoff.cwd : null;
|
|
87
|
+
const fork = spec.origin?.agent === "claude-code" &&
|
|
88
|
+
!!r.originSessionId &&
|
|
89
|
+
!!r.originCwd &&
|
|
90
|
+
!(tools && includesWebTool(tools)) &&
|
|
91
|
+
(cwd === null || cwd === r.originCwd);
|
|
92
|
+
const planned_mode = fork ? "fork" : "fresh";
|
|
93
|
+
if (!handoff)
|
|
94
|
+
return { planned_mode };
|
|
95
|
+
return { profile: handoff.profile ?? null, tools, cwd, planned_mode };
|
|
96
|
+
}
|
|
97
|
+
function providerModel(bodyJson) {
|
|
98
|
+
if (!bodyJson)
|
|
99
|
+
return {};
|
|
100
|
+
try {
|
|
101
|
+
const spec = JSON.parse(bodyJson);
|
|
102
|
+
return {
|
|
103
|
+
...(typeof spec.provider === "string" ? { provider: spec.provider } : {}),
|
|
104
|
+
...(typeof spec.model === "string" ? { model: spec.model } : {}),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return {};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const MOVE_KINDS = new Set(["rescheduled", "postponed", "expedited"]);
|
|
112
|
+
const SPEC_KINDS = new Set(["created", "dispatched", "completed", "failed", "attempt_failed"]);
|
|
113
|
+
/**
|
|
114
|
+
* The details of one event: `from` / `to` timing for moves and run-now,
|
|
115
|
+
* provider and model for created / dispatched / completed / failed /
|
|
116
|
+
* attempt_failed, the first line of the error for failed, then `extra`.
|
|
117
|
+
*/
|
|
118
|
+
export function eventDetails(kind, prev, next, extra = {}) {
|
|
119
|
+
const out = {};
|
|
120
|
+
if (MOVE_KINDS.has(kind)) {
|
|
121
|
+
if (prev)
|
|
122
|
+
out.from = timing(prev);
|
|
123
|
+
out.to = timing(next);
|
|
124
|
+
}
|
|
125
|
+
if (kind === "created") {
|
|
126
|
+
const t = timing(next);
|
|
127
|
+
if (t.deadline)
|
|
128
|
+
out.deadline = t.deadline;
|
|
129
|
+
if (t.not_before)
|
|
130
|
+
out.not_before = t.not_before;
|
|
131
|
+
}
|
|
132
|
+
if (SPEC_KINDS.has(kind))
|
|
133
|
+
Object.assign(out, providerModel(next.bodyJson));
|
|
134
|
+
if (kind === "created" || kind === "dispatched")
|
|
135
|
+
Object.assign(out, handoffDetails(next));
|
|
136
|
+
if (kind === "failed" && next.error)
|
|
137
|
+
out.error = next.error.split("\n")[0].slice(0, 300);
|
|
138
|
+
return { ...out, ...extra };
|
|
139
|
+
}
|
|
140
|
+
/** The kind a status change implies when the writer named none (terminal moves only). */
|
|
141
|
+
export function impliedEventKind(prevStatus, nextStatus) {
|
|
142
|
+
if (prevStatus === nextStatus)
|
|
143
|
+
return undefined;
|
|
144
|
+
if (nextStatus === "completed" || nextStatus === "failed" || nextStatus === "cancelled")
|
|
145
|
+
return nextStatus;
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
function short(iso) {
|
|
149
|
+
return typeof iso === "string" ? iso : "none";
|
|
150
|
+
}
|
|
151
|
+
/** A one-line summary of the details, or "". */
|
|
152
|
+
export function eventSummary(e) {
|
|
153
|
+
const d = e.details ?? {};
|
|
154
|
+
const from = (d.from ?? {});
|
|
155
|
+
const to = (d.to ?? {});
|
|
156
|
+
const parts = [];
|
|
157
|
+
switch (e.kind) {
|
|
158
|
+
case "expedited":
|
|
159
|
+
parts.push(`not_before was ${short(from.not_before)}`, `scheduled_for ${short(from.scheduled_for)} -> ${short(to.scheduled_for)}`);
|
|
160
|
+
break;
|
|
161
|
+
case "rescheduled":
|
|
162
|
+
case "postponed":
|
|
163
|
+
if (typeof d.preset === "string")
|
|
164
|
+
parts.push(`preset ${d.preset}`);
|
|
165
|
+
parts.push(`scheduled_for ${short(from.scheduled_for)} -> ${short(to.scheduled_for)}`);
|
|
166
|
+
if (from.not_before !== to.not_before)
|
|
167
|
+
parts.push(`not_before ${short(from.not_before)} -> ${short(to.not_before)}`);
|
|
168
|
+
break;
|
|
169
|
+
case "deferred":
|
|
170
|
+
parts.push(`retry at ${short(d.retry_at)}`);
|
|
171
|
+
break;
|
|
172
|
+
case "attempt_failed":
|
|
173
|
+
parts.push(`attempt ${typeof d.attempt === "number" ? d.attempt : "?"} (${short(d.failure_class)})`, `retry at ${short(d.retry_at)}`);
|
|
174
|
+
if (typeof d.error === "string")
|
|
175
|
+
parts.push(d.error);
|
|
176
|
+
break;
|
|
177
|
+
case "replied":
|
|
178
|
+
parts.push(`reply ${short(d.child_task_id)}`);
|
|
179
|
+
break;
|
|
180
|
+
case "notified":
|
|
181
|
+
parts.push(`via ${short(d.via)}`);
|
|
182
|
+
break;
|
|
183
|
+
case "result_file":
|
|
184
|
+
parts.push(d.written === true ? `written ${short(d.path)}` : `not written: ${short(d.error)}`);
|
|
185
|
+
break;
|
|
186
|
+
case "progress":
|
|
187
|
+
if (typeof d.line === "string")
|
|
188
|
+
parts.push(d.line);
|
|
189
|
+
break;
|
|
190
|
+
case "failed":
|
|
191
|
+
if (typeof d.error === "string")
|
|
192
|
+
parts.push(d.error);
|
|
193
|
+
break;
|
|
194
|
+
default:
|
|
195
|
+
if (typeof d.provider === "string")
|
|
196
|
+
parts.push(`${d.provider}${typeof d.model === "string" ? `/${d.model}` : ""}`);
|
|
197
|
+
}
|
|
198
|
+
if (typeof d.reason === "string")
|
|
199
|
+
parts.push(d.reason);
|
|
200
|
+
return parts.join("; ");
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* One line per failed attempt the retry policy queued again, since the
|
|
204
|
+
* newest `created` event or manual retry, for the text views: key
|
|
205
|
+
* `attempt_<n>`, text `failed <at> (<class>): <error>; next attempt from
|
|
206
|
+
* <retry_at>`. `events` are oldest first.
|
|
207
|
+
*/
|
|
208
|
+
export function attemptLines(events) {
|
|
209
|
+
const out = [];
|
|
210
|
+
for (const e of events) {
|
|
211
|
+
if (e.kind === "created" || (e.kind === "rescheduled" && e.details?.reason === "retry"))
|
|
212
|
+
out.length = 0;
|
|
213
|
+
if (e.kind !== "attempt_failed")
|
|
214
|
+
continue;
|
|
215
|
+
const d = e.details ?? {};
|
|
216
|
+
const n = typeof d.attempt === "number" ? d.attempt : out.length + 1;
|
|
217
|
+
const cls = typeof d.failure_class === "string" ? ` (${d.failure_class})` : "";
|
|
218
|
+
const error = typeof d.error === "string" ? `: ${d.error}` : "";
|
|
219
|
+
const next = typeof d.retry_at === "string" ? `; next attempt from ${d.retry_at}` : "";
|
|
220
|
+
out.push({ key: `attempt_${n}`, text: `failed ${e.at}${cls}${error}${next}` });
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
/** `<at> <kind> by <actor>[ - <summary>]`, one line. */
|
|
225
|
+
export function formatTaskEvent(e) {
|
|
226
|
+
const summary = eventSummary(e);
|
|
227
|
+
return `${e.at} ${e.kind} by ${e.actor}${summary ? ` - ${summary}` : ""}`;
|
|
228
|
+
}
|
|
229
|
+
/** The API / JSON shape of one event. */
|
|
230
|
+
export function taskEventJson(e) {
|
|
231
|
+
return { at: e.at, kind: e.kind, actor: e.actor, details: e.details };
|
|
232
|
+
}
|
|
233
|
+
/** A run reports no progress in its first 5 seconds: a fast run writes none. */
|
|
234
|
+
export const PROGRESS_QUIET_MS = 5_000;
|
|
235
|
+
/** At most one progress event per 15 seconds. */
|
|
236
|
+
export const PROGRESS_GAP_MS = 15_000;
|
|
237
|
+
/** At most 24 progress events per task, so its `created` event stays inside a page of 50. */
|
|
238
|
+
export const PROGRESS_MAX_EVENTS = 24;
|
|
239
|
+
/** Longest progress line. */
|
|
240
|
+
export const PROGRESS_LINE_MAX_CHARS = 200;
|
|
241
|
+
/**
|
|
242
|
+
* Longest wait of one progress write for another writer's lock, in ms. The
|
|
243
|
+
* tick writes progress on its event loop; past this the line is dropped.
|
|
244
|
+
*/
|
|
245
|
+
export const PROGRESS_BUSY_MS = 100;
|
|
246
|
+
/** Terminal escape sequences (CSI, OSC and two-character escapes): removed whole, not only their ESC. */
|
|
247
|
+
const TERMINAL_ESCAPES = /\u001b\[[0-?]*[ -/]*[@-~]|\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)?|\u001b[@-Z\\-_]/g;
|
|
248
|
+
/** Line and paragraph separators (become a space) and bidirectional controls (removed). */
|
|
249
|
+
const LINE_SEPARATORS = /[\u2028\u2029]/g;
|
|
250
|
+
const BIDI_CONTROLS = /[\u200e\u200f\u202a-\u202e\u2066-\u2069]/g;
|
|
251
|
+
/** The most of a raw line {@link progressLine} reads, so a huge line costs no more than this. */
|
|
252
|
+
const PROGRESS_SCAN_CHARS = 8_192;
|
|
253
|
+
/**
|
|
254
|
+
* Untrusted output text without terminal escape sequences, control
|
|
255
|
+
* characters (a tab becomes a space), line separators or bidirectional
|
|
256
|
+
* controls. An adapter cleans a line with this before it scrubs secrets, so
|
|
257
|
+
* a removed character cannot join a secret after the scrub.
|
|
258
|
+
*/
|
|
259
|
+
export function cleanProgressText(raw) {
|
|
260
|
+
return stripControlChars(raw.replace(TERMINAL_ESCAPES, "").replace(LINE_SEPARATORS, " ").replace(BIDI_CONTROLS, ""));
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* One progress line as it is stored: cleaned by {@link cleanProgressText},
|
|
264
|
+
* trailing space trimmed, cut to {@link PROGRESS_LINE_MAX_CHARS}
|
|
265
|
+
* characters. Undefined for a blank line.
|
|
266
|
+
*/
|
|
267
|
+
export function progressLine(raw) {
|
|
268
|
+
const clean = cleanProgressText(raw.length > PROGRESS_SCAN_CHARS ? raw.slice(0, PROGRESS_SCAN_CHARS) : raw).trimEnd();
|
|
269
|
+
if (clean.trim() === "")
|
|
270
|
+
return undefined;
|
|
271
|
+
const chars = Array.from(clean);
|
|
272
|
+
return chars.length > PROGRESS_LINE_MAX_CHARS ? chars.slice(0, PROGRESS_LINE_MAX_CHARS).join("") : clean;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* The caps of a run's progress events, for every adapter: nothing in the
|
|
276
|
+
* first {@link PROGRESS_QUIET_MS}, then at most one event per
|
|
277
|
+
* {@link PROGRESS_GAP_MS} and {@link PROGRESS_MAX_EVENTS} per task, each line
|
|
278
|
+
* cleaned by {@link progressLine}. A line that comes before its slot waits
|
|
279
|
+
* for it, and a newer line takes its place, so the event shows the newest
|
|
280
|
+
* line. `write` gets the details of each event (`line`, `seq`); `seq`
|
|
281
|
+
* counts the task's progress events from 1, after the `existing` ones (read
|
|
282
|
+
* once, when the first event is due). The caller scrubs secrets from each
|
|
283
|
+
* line first. Never throws.
|
|
284
|
+
*/
|
|
285
|
+
export function createProgressRecorder(write, opts = {}) {
|
|
286
|
+
const started = Date.now();
|
|
287
|
+
let count;
|
|
288
|
+
let lastAt;
|
|
289
|
+
let pending;
|
|
290
|
+
let timer;
|
|
291
|
+
let closed = false;
|
|
292
|
+
const written = () => {
|
|
293
|
+
if (count === undefined) {
|
|
294
|
+
try {
|
|
295
|
+
count = Math.max(0, Math.floor(opts.existing?.() ?? 0));
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
count = 0;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return count;
|
|
302
|
+
};
|
|
303
|
+
const flush = () => {
|
|
304
|
+
timer = undefined;
|
|
305
|
+
const line = pending;
|
|
306
|
+
pending = undefined;
|
|
307
|
+
if (closed || line === undefined || written() >= PROGRESS_MAX_EVENTS)
|
|
308
|
+
return;
|
|
309
|
+
const seq = written() + 1;
|
|
310
|
+
count = seq;
|
|
311
|
+
lastAt = Date.now();
|
|
312
|
+
try {
|
|
313
|
+
write({ line, seq });
|
|
314
|
+
}
|
|
315
|
+
catch {
|
|
316
|
+
// A busy ledger costs this line (at most PROGRESS_BUSY_MS of waiting), not the run.
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
return {
|
|
320
|
+
onLine(raw) {
|
|
321
|
+
if (closed || (count !== undefined && count >= PROGRESS_MAX_EVENTS))
|
|
322
|
+
return;
|
|
323
|
+
const line = progressLine(raw);
|
|
324
|
+
if (line === undefined)
|
|
325
|
+
return;
|
|
326
|
+
pending = line;
|
|
327
|
+
if (timer !== undefined)
|
|
328
|
+
return;
|
|
329
|
+
const due = Math.max(started + PROGRESS_QUIET_MS, lastAt !== undefined ? lastAt + PROGRESS_GAP_MS : 0);
|
|
330
|
+
const wait = due - Date.now();
|
|
331
|
+
if (wait <= 0)
|
|
332
|
+
return flush();
|
|
333
|
+
timer = setTimeout(flush, wait);
|
|
334
|
+
timer.unref?.();
|
|
335
|
+
},
|
|
336
|
+
close() {
|
|
337
|
+
closed = true;
|
|
338
|
+
pending = undefined;
|
|
339
|
+
if (timer !== undefined)
|
|
340
|
+
clearTimeout(timer);
|
|
341
|
+
timer = undefined;
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
//# sourceMappingURL=task-events.js.map
|
package/dist/tool-surface.d.ts
CHANGED
|
@@ -23,7 +23,9 @@
|
|
|
23
23
|
* the MCP renderer validates strictly (it `.parse()`s incoming arguments),
|
|
24
24
|
* the OpenClaw renderer advertises the looser shapes it always has — so the
|
|
25
25
|
* observable wire contract on each host is preserved while the surface
|
|
26
|
-
* (names, parameter sets, requiredness, descriptions) is unified.
|
|
26
|
+
* (names, parameter sets, requiredness, descriptions) is unified. A tool
|
|
27
|
+
* whose handler differs per host carries a `descriptionPerHost` override;
|
|
28
|
+
* resolve the text with `descriptionForHost`.
|
|
27
29
|
*
|
|
28
30
|
* Host applicability is first-class: `set_delivery` is OpenClaw-only, and a
|
|
29
31
|
* handful of parameters are host-scoped (MCP's `dry_run` / `dispatch` /
|
|
@@ -66,11 +68,31 @@ export interface ToolParam {
|
|
|
66
68
|
itemKind?: "string" | "enum";
|
|
67
69
|
properties?: readonly ToolParam[];
|
|
68
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* MCP tool annotations (the four behavior hints plus a display title).
|
|
73
|
+
* Neutral data: the MCP server advertises them in `tools/list`; OpenClaw has
|
|
74
|
+
* no analogue and ignores them. A description must never contradict them.
|
|
75
|
+
*/
|
|
76
|
+
export interface ToolAnnotations {
|
|
77
|
+
title: string;
|
|
78
|
+
/** Writes no file and no row. */
|
|
79
|
+
readOnlyHint: boolean;
|
|
80
|
+
/** Deletes or irreversibly replaces existing data. */
|
|
81
|
+
destructiveHint: boolean;
|
|
82
|
+
/** A second identical call changes nothing more. */
|
|
83
|
+
idempotentHint: boolean;
|
|
84
|
+
/** The call reaches a provider API, a grid feed or a usage endpoint. */
|
|
85
|
+
openWorldHint: boolean;
|
|
86
|
+
}
|
|
69
87
|
/** One tool: its wire name, canonical description, host applicability, and
|
|
70
88
|
* the ordered parameter descriptors. */
|
|
71
89
|
export interface CanonicalToolDef {
|
|
72
90
|
name: string;
|
|
91
|
+
/** The MCP text; also used on any host without an override. */
|
|
73
92
|
description: string;
|
|
93
|
+
/** Per-host description, for a host whose handler differs from the MCP one. */
|
|
94
|
+
descriptionPerHost?: Partial<Record<ToolHost, string>>;
|
|
95
|
+
annotations?: ToolAnnotations;
|
|
74
96
|
hosts: readonly ToolHost[];
|
|
75
97
|
params: readonly ToolParam[];
|
|
76
98
|
}
|
|
@@ -79,8 +101,14 @@ export declare const TOOL_SURFACE: readonly CanonicalToolDef[];
|
|
|
79
101
|
export declare function getToolDef(name: string): CanonicalToolDef | undefined;
|
|
80
102
|
/** Like {@link getToolDef} but throws when the name is unknown. */
|
|
81
103
|
export declare function getToolDefOrThrow(name: string): CanonicalToolDef;
|
|
82
|
-
/**
|
|
104
|
+
/** A tool's description as one host shows it (its override, else the base). */
|
|
105
|
+
export declare function descriptionForHost(def: CanonicalToolDef, host: ToolHost): string;
|
|
106
|
+
/** The tools a given host registers, in canonical order, each carrying the
|
|
107
|
+
* description resolved for that host. */
|
|
83
108
|
export declare function toolsForHost(host: ToolHost): CanonicalToolDef[];
|
|
109
|
+
/** Tool counts per host; the site derives its "N tools" copy from these. */
|
|
110
|
+
export declare const MCP_TOOL_COUNT: number;
|
|
111
|
+
export declare const OPENCLAW_TOOL_COUNT: number;
|
|
84
112
|
/** Which hosts expose a given parameter (defaults to the tool's hosts). */
|
|
85
113
|
export declare function paramHosts(param: ToolParam, def: CanonicalToolDef): readonly ToolHost[];
|
|
86
114
|
/** Whether a parameter is exposed on a given host. */
|
|
@@ -114,6 +142,7 @@ export interface NeutralParamSnapshot {
|
|
|
114
142
|
export declare function neutralSurfaceForHost(host: ToolHost): Array<{
|
|
115
143
|
name: string;
|
|
116
144
|
description: string;
|
|
145
|
+
annotations?: ToolAnnotations;
|
|
117
146
|
params: NeutralParamSnapshot[];
|
|
118
147
|
}>;
|
|
119
148
|
//# sourceMappingURL=tool-surface.d.ts.map
|