@oyasmi/pipiclaw 0.6.10-beta.1 → 0.6.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/README.md +5 -1
- package/dist/agent/commands.d.ts +1 -1
- package/dist/agent/commands.js +7 -0
- package/dist/runtime/bootstrap.js +13 -0
- package/dist/runtime/dingtalk.d.ts +1 -0
- package/dist/runtime/dingtalk.js +6 -2
- package/dist/runtime/event-commands.d.ts +6 -0
- package/dist/runtime/event-commands.js +206 -0
- package/dist/runtime/events.d.ts +1 -1
- package/dist/runtime/events.js +73 -55
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ npm package: [`@oyasmi/pipiclaw`](https://www.npmjs.com/package/@oyasmi/pipiclaw
|
|
|
18
18
|
- 原生支持钉钉 Stream Mode,不需要消息中转服务,不需要公网IP
|
|
19
19
|
- 支持 AI Card 过程展示,思考、工具执行和状态更新可以持续流式呈现
|
|
20
20
|
- 支持 `/help`、`/new`、`/compact`、`/session`、`/model`
|
|
21
|
-
- 忙碌时可继续接收 `/steer`、`/followup`、`/stop`
|
|
21
|
+
- 忙碌时可继续接收 `/steer`、`/followup`、`/stop`、`/events`
|
|
22
22
|
- 按工作区 / 会话通道(workspace / channel)分层管理 `SOUL.md`、`AGENTS.md`、`SESSION.md`、`MEMORY.md`、`HISTORY.md`
|
|
23
23
|
- 支持预定义子代理(sub-agent)和临时内联子代理(inline sub-agent)
|
|
24
24
|
- 支持立即、单次、周期三类事件调度
|
|
@@ -463,6 +463,10 @@ Pipiclaw 有两层命令。
|
|
|
463
463
|
| `/stop` | 停止当前正在执行的任务 |
|
|
464
464
|
| `/steer <message>` | 在当前任务继续执行时追加新的引导信息 |
|
|
465
465
|
| `/followup <message>` | 把新的请求排队,等当前任务结束后再执行 |
|
|
466
|
+
| `/events list` | 列出 `workspace/events/` 中的事件文件摘要 |
|
|
467
|
+
| `/events show <name>` | 查看指定事件文件的完整 JSON |
|
|
468
|
+
| `/events delete <name>` | 删除指定事件文件 |
|
|
469
|
+
| `/events history [name]` | 查看事件调度历史,可按事件名过滤 |
|
|
466
470
|
|
|
467
471
|
忙碌时,普通消息默认等价于 `/steer <message>`。
|
|
468
472
|
|
package/dist/agent/commands.d.ts
CHANGED
package/dist/agent/commands.js
CHANGED
|
@@ -18,6 +18,12 @@ These are handled directly by the DingTalk transport/runtime layer.
|
|
|
18
18
|
- \`/followup <message>\`
|
|
19
19
|
Queue another request to run after the current task completes
|
|
20
20
|
Example: \`/followup After that, draft a short executive summary\`
|
|
21
|
+
- \`/events <list|show|delete|history>\`
|
|
22
|
+
Manage scheduled event files and inspect event history
|
|
23
|
+
Example: \`/events list\`
|
|
24
|
+
Example: \`/events show weekly-review\`
|
|
25
|
+
Example: \`/events delete weekly-review\`
|
|
26
|
+
Example: \`/events history weekly-review\`
|
|
21
27
|
|
|
22
28
|
While a task is running, plain messages use the configured busy-message default. The default is \`steer\`; set \`busyMessageDefault\` in channel.json to \`followUp\` or \`followup\` to queue plain messages after the current task.
|
|
23
29
|
|
|
@@ -55,6 +61,7 @@ export function parseBuiltInCommand(text) {
|
|
|
55
61
|
case "steer":
|
|
56
62
|
case "followup":
|
|
57
63
|
case "stop":
|
|
64
|
+
case "events":
|
|
58
65
|
return { name: rawName, args, rawText };
|
|
59
66
|
default:
|
|
60
67
|
return null;
|
|
@@ -15,6 +15,7 @@ import { loadToolsConfigWithDiagnostics } from "../tools/config.js";
|
|
|
15
15
|
import { ensureChannelDir } from "./channel-paths.js";
|
|
16
16
|
import { createDingTalkContext } from "./delivery.js";
|
|
17
17
|
import { DingTalkBot, isBusyMessageDefaultConfig, isResponseModeConfig, normalizeBusyMessageDefault, normalizeResponseMode, } from "./dingtalk.js";
|
|
18
|
+
import { handleEventsCommand as runEventsCommand } from "./event-commands.js";
|
|
18
19
|
import { createEventsWatcher } from "./events.js";
|
|
19
20
|
import { ChannelStore } from "./store.js";
|
|
20
21
|
const DEFAULT_SOUL = `# SOUL.md
|
|
@@ -404,6 +405,14 @@ export function createRuntimeContext(options) {
|
|
|
404
405
|
log.logInfo(`[${channelId}] Stop requested`);
|
|
405
406
|
}
|
|
406
407
|
},
|
|
408
|
+
async handleEventsCommand(event, bot, args) {
|
|
409
|
+
const response = await runEventsCommand({
|
|
410
|
+
args,
|
|
411
|
+
workspaceDir: options.paths.workspaceDir,
|
|
412
|
+
historyPath: options.paths.eventHistoryPath,
|
|
413
|
+
});
|
|
414
|
+
await bot.sendPlain(event.channelId, response);
|
|
415
|
+
},
|
|
407
416
|
async handleBusyMessage(event, bot, mode, queueText) {
|
|
408
417
|
if (shuttingDown) {
|
|
409
418
|
return { kind: "handled" };
|
|
@@ -476,6 +485,10 @@ export function createRuntimeContext(options) {
|
|
|
476
485
|
const builtInCommand = parseBuiltInCommand(event.text);
|
|
477
486
|
if (builtInCommand) {
|
|
478
487
|
log.logInfo(`[${event.channelId}] Executing command: ${builtInCommand.rawText}`);
|
|
488
|
+
if (builtInCommand.name === "events") {
|
|
489
|
+
await handler.handleEventsCommand(event, bot, builtInCommand.args);
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
479
492
|
await state.runner.handleBuiltinCommand(ctx, builtInCommand);
|
|
480
493
|
return;
|
|
481
494
|
}
|
|
@@ -64,6 +64,7 @@ export interface DingTalkHandler {
|
|
|
64
64
|
isRunning(channelId: string): boolean;
|
|
65
65
|
handleEvent(event: DingTalkEvent, bot: DingTalkBot, isEvent?: boolean): Promise<void>;
|
|
66
66
|
handleStop(channelId: string, bot: DingTalkBot): Promise<void>;
|
|
67
|
+
handleEventsCommand(event: DingTalkEvent, bot: DingTalkBot, args: string): Promise<void>;
|
|
67
68
|
handleBusyMessage(event: DingTalkEvent, bot: DingTalkBot, mode: BusyMessageMode, queueText: string): Promise<BusyMessageResult>;
|
|
68
69
|
}
|
|
69
70
|
export declare class DingTalkBot {
|
package/dist/runtime/dingtalk.js
CHANGED
|
@@ -979,12 +979,16 @@ export class DingTalkBot {
|
|
|
979
979
|
}
|
|
980
980
|
return;
|
|
981
981
|
}
|
|
982
|
+
if (builtInCommand?.name === "events") {
|
|
983
|
+
await this.handler.handleEventsCommand(event, this, builtInCommand.args);
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
982
986
|
if (builtInCommand) {
|
|
983
|
-
await this.sendPlain(channelId, `A task is already running. Use \`/stop\`, \`/steer <message>\`, or \`/
|
|
987
|
+
await this.sendPlain(channelId, `A task is already running. Use \`/stop\`, \`/steer <message>\`, \`/followup <message>\`, or \`/events <action>\`. Plain messages default to ${this.busyMessageDefault}.`);
|
|
984
988
|
return;
|
|
985
989
|
}
|
|
986
990
|
if (isSlashCommand) {
|
|
987
|
-
await this.sendPlain(channelId, "A task is already running. Only `/stop`, `/steer <message>`, and `/
|
|
991
|
+
await this.sendPlain(channelId, "A task is already running. Only `/stop`, `/steer <message>`, `/followup <message>`, and `/events <action>` are available while streaming.");
|
|
988
992
|
return;
|
|
989
993
|
}
|
|
990
994
|
const result = await this.handler.handleBusyMessage(event, this, this.busyMessageDefault, content);
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { existsSync } from "fs";
|
|
2
|
+
import { readdir, readFile, unlink } from "fs/promises";
|
|
3
|
+
import { join, resolve, sep } from "path";
|
|
4
|
+
import { parseScheduledEventContent } from "./events.js";
|
|
5
|
+
const EVENT_NAME_PATTERN = /^[A-Za-z0-9._-]+$/;
|
|
6
|
+
const DEFAULT_HISTORY_LIMIT = 20;
|
|
7
|
+
const TEXT_PREVIEW_MAX_CHARS = 100;
|
|
8
|
+
function usage() {
|
|
9
|
+
return `# Events
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
|
|
13
|
+
- \`/events list\`
|
|
14
|
+
- \`/events show <name>\`
|
|
15
|
+
- \`/events delete <name>\`
|
|
16
|
+
- \`/events history [name]\``;
|
|
17
|
+
}
|
|
18
|
+
function parseEventsCommand(args) {
|
|
19
|
+
const parts = args.trim().split(/\s+/).filter(Boolean);
|
|
20
|
+
const action = parts[0];
|
|
21
|
+
const name = parts[1];
|
|
22
|
+
if (!action || action === "list") {
|
|
23
|
+
if (parts.length > 1)
|
|
24
|
+
throw new Error("Usage: /events list");
|
|
25
|
+
return { action: "list" };
|
|
26
|
+
}
|
|
27
|
+
if (action === "show") {
|
|
28
|
+
if (!name || parts.length > 2)
|
|
29
|
+
throw new Error("Usage: /events show <name>");
|
|
30
|
+
return { action, name };
|
|
31
|
+
}
|
|
32
|
+
if (action === "delete") {
|
|
33
|
+
if (!name || parts.length > 2)
|
|
34
|
+
throw new Error("Usage: /events delete <name>");
|
|
35
|
+
return { action, name };
|
|
36
|
+
}
|
|
37
|
+
if (action === "history") {
|
|
38
|
+
if (parts.length > 2)
|
|
39
|
+
throw new Error("Usage: /events history [name]");
|
|
40
|
+
return name ? { action, name } : { action };
|
|
41
|
+
}
|
|
42
|
+
throw new Error(`Unknown /events action: ${action}`);
|
|
43
|
+
}
|
|
44
|
+
function eventsDir(workspaceDir) {
|
|
45
|
+
return join(workspaceDir, "events");
|
|
46
|
+
}
|
|
47
|
+
function normalizeEventName(name) {
|
|
48
|
+
const trimmed = name.trim();
|
|
49
|
+
const normalized = trimmed.endsWith(".json") ? trimmed.slice(0, -".json".length) : trimmed;
|
|
50
|
+
if (!normalized || normalized === "." || normalized === ".." || !EVENT_NAME_PATTERN.test(normalized)) {
|
|
51
|
+
throw new Error(`Invalid event name: ${name}`);
|
|
52
|
+
}
|
|
53
|
+
return normalized;
|
|
54
|
+
}
|
|
55
|
+
function resolveEventPath(workspaceDir, name) {
|
|
56
|
+
const eventName = normalizeEventName(name);
|
|
57
|
+
const dir = resolve(eventsDir(workspaceDir));
|
|
58
|
+
const eventPath = resolve(dir, `${eventName}.json`);
|
|
59
|
+
if (eventPath !== join(dir, `${eventName}.json`) || !eventPath.startsWith(`${dir}${sep}`)) {
|
|
60
|
+
throw new Error(`Invalid event name: ${name}`);
|
|
61
|
+
}
|
|
62
|
+
return { eventName, eventPath };
|
|
63
|
+
}
|
|
64
|
+
function eventNameFromFilename(filename) {
|
|
65
|
+
return filename.endsWith(".json") ? filename.slice(0, -".json".length) : filename;
|
|
66
|
+
}
|
|
67
|
+
function clipText(text, maxChars = TEXT_PREVIEW_MAX_CHARS) {
|
|
68
|
+
const normalized = text.replace(/\s+/g, " ").trim();
|
|
69
|
+
return normalized.length > maxChars ? `${normalized.slice(0, maxChars - 1)}...` : normalized;
|
|
70
|
+
}
|
|
71
|
+
function formatEventSummary(name, event) {
|
|
72
|
+
const lines = [`- ${name}`, ` type: ${event.type}`, ` channelId: ${event.channelId}`];
|
|
73
|
+
if (event.type === "one-shot") {
|
|
74
|
+
lines.push(` at: ${event.at}`);
|
|
75
|
+
}
|
|
76
|
+
if (event.type === "periodic") {
|
|
77
|
+
lines.push(` schedule: ${event.schedule}`, ` timezone: ${event.timezone}`);
|
|
78
|
+
}
|
|
79
|
+
lines.push(` text: ${clipText(event.text)}`);
|
|
80
|
+
return lines.join("\n");
|
|
81
|
+
}
|
|
82
|
+
async function listEvents(workspaceDir) {
|
|
83
|
+
const dir = eventsDir(workspaceDir);
|
|
84
|
+
if (!existsSync(dir)) {
|
|
85
|
+
return "# Events\n\nNo events directory found.";
|
|
86
|
+
}
|
|
87
|
+
const filenames = (await readdir(dir)).filter((filename) => filename.endsWith(".json")).sort();
|
|
88
|
+
if (filenames.length === 0) {
|
|
89
|
+
return "# Events\n\nNo event files found.";
|
|
90
|
+
}
|
|
91
|
+
const blocks = [];
|
|
92
|
+
for (const filename of filenames) {
|
|
93
|
+
const eventPath = join(dir, filename);
|
|
94
|
+
const name = eventNameFromFilename(filename);
|
|
95
|
+
try {
|
|
96
|
+
const event = parseScheduledEventContent(await readFile(eventPath, "utf-8"), filename);
|
|
97
|
+
blocks.push(formatEventSummary(name, event));
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
101
|
+
blocks.push(`- ${name}\n invalid: ${message}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return `# Events\n\n${blocks.join("\n\n")}`;
|
|
105
|
+
}
|
|
106
|
+
async function showEvent(workspaceDir, name) {
|
|
107
|
+
const { eventName, eventPath } = resolveEventPath(workspaceDir, name);
|
|
108
|
+
if (!existsSync(eventPath)) {
|
|
109
|
+
return `Event not found: ${eventName}`;
|
|
110
|
+
}
|
|
111
|
+
const raw = await readFile(eventPath, "utf-8");
|
|
112
|
+
const parsed = JSON.parse(raw);
|
|
113
|
+
return `# Event: ${eventName}
|
|
114
|
+
|
|
115
|
+
\`\`\`json
|
|
116
|
+
${JSON.stringify(parsed, null, 2)}
|
|
117
|
+
\`\`\``;
|
|
118
|
+
}
|
|
119
|
+
async function deleteEvent(workspaceDir, name) {
|
|
120
|
+
const { eventName, eventPath } = resolveEventPath(workspaceDir, name);
|
|
121
|
+
if (!existsSync(eventPath)) {
|
|
122
|
+
return `Event not found: ${eventName}`;
|
|
123
|
+
}
|
|
124
|
+
await unlink(eventPath);
|
|
125
|
+
return `Deleted event: ${eventName}`;
|
|
126
|
+
}
|
|
127
|
+
function parseHistoryLine(line) {
|
|
128
|
+
try {
|
|
129
|
+
const parsed = JSON.parse(line);
|
|
130
|
+
if (typeof parsed.ts !== "string" || typeof parsed.eventName !== "string") {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
return parsed;
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function formatHistoryRecord(record) {
|
|
140
|
+
const details = [];
|
|
141
|
+
if (record.channelId)
|
|
142
|
+
details.push(` channelId: ${record.channelId}`);
|
|
143
|
+
if (record.schedule)
|
|
144
|
+
details.push(` schedule: ${record.schedule}`);
|
|
145
|
+
if (record.timezone)
|
|
146
|
+
details.push(` timezone: ${record.timezone}`);
|
|
147
|
+
if (record.at)
|
|
148
|
+
details.push(` at: ${record.at}`);
|
|
149
|
+
if (record.nextRunAt)
|
|
150
|
+
details.push(` nextRunAt: ${record.nextRunAt}`);
|
|
151
|
+
if (record.reason)
|
|
152
|
+
details.push(` reason: ${record.reason}`);
|
|
153
|
+
if (record.textPreview)
|
|
154
|
+
details.push(` text: ${clipText(record.textPreview)}`);
|
|
155
|
+
const header = `- ${record.ts} ${record.eventName} ${record.action} ${record.result}`;
|
|
156
|
+
return details.length > 0 ? `${header}\n${details.join("\n")}` : header;
|
|
157
|
+
}
|
|
158
|
+
async function showHistory(historyPath, name) {
|
|
159
|
+
const eventName = name ? normalizeEventName(name) : undefined;
|
|
160
|
+
if (!existsSync(historyPath)) {
|
|
161
|
+
return "# Event History\n\nNo event history found.";
|
|
162
|
+
}
|
|
163
|
+
const raw = await readFile(historyPath, "utf-8");
|
|
164
|
+
const records = raw
|
|
165
|
+
.split("\n")
|
|
166
|
+
.map((line) => line.trim())
|
|
167
|
+
.filter(Boolean)
|
|
168
|
+
.map(parseHistoryLine)
|
|
169
|
+
.filter((record) => !!record)
|
|
170
|
+
.filter((record) => !eventName || record.eventName === eventName)
|
|
171
|
+
.slice(-DEFAULT_HISTORY_LIMIT)
|
|
172
|
+
.reverse();
|
|
173
|
+
if (records.length === 0) {
|
|
174
|
+
return eventName
|
|
175
|
+
? `# Event History\n\nNo history found for event: ${eventName}`
|
|
176
|
+
: "# Event History\n\nNo history found.";
|
|
177
|
+
}
|
|
178
|
+
const suffix = eventName ? `: ${eventName}` : "";
|
|
179
|
+
return `# Event History${suffix}\n\n${records.map(formatHistoryRecord).join("\n\n")}`;
|
|
180
|
+
}
|
|
181
|
+
export async function handleEventsCommand(options) {
|
|
182
|
+
let command;
|
|
183
|
+
try {
|
|
184
|
+
command = parseEventsCommand(options.args);
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
188
|
+
return `${message}\n\n${usage()}`;
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
switch (command.action) {
|
|
192
|
+
case "list":
|
|
193
|
+
return await listEvents(options.workspaceDir);
|
|
194
|
+
case "show":
|
|
195
|
+
return await showEvent(options.workspaceDir, command.name);
|
|
196
|
+
case "delete":
|
|
197
|
+
return await deleteEvent(options.workspaceDir, command.name);
|
|
198
|
+
case "history":
|
|
199
|
+
return await showHistory(options.historyPath, command.name);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
204
|
+
return `Could not ${command.action} events: ${message}`;
|
|
205
|
+
}
|
|
206
|
+
}
|
package/dist/runtime/events.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ export interface EventsWatcherOptions {
|
|
|
59
59
|
historyPath?: string;
|
|
60
60
|
}
|
|
61
61
|
export declare function formatLocalTimestamp(date?: Date): string;
|
|
62
|
+
export declare function parseScheduledEventContent(content: string, filename: string): ScheduledEvent;
|
|
62
63
|
export declare class EventsWatcher {
|
|
63
64
|
private eventsDir;
|
|
64
65
|
private bot;
|
|
@@ -83,7 +84,6 @@ export declare class EventsWatcher {
|
|
|
83
84
|
private handleDelete;
|
|
84
85
|
private cancelScheduled;
|
|
85
86
|
private handleFile;
|
|
86
|
-
private parsePreAction;
|
|
87
87
|
private parseEvent;
|
|
88
88
|
private handleImmediate;
|
|
89
89
|
private handleOneShot;
|
package/dist/runtime/events.js
CHANGED
|
@@ -36,6 +36,78 @@ function truncateTextPreview(text) {
|
|
|
36
36
|
? `${normalized.slice(0, TEXT_PREVIEW_MAX_CHARS - 1)}…`
|
|
37
37
|
: normalized;
|
|
38
38
|
}
|
|
39
|
+
function isRecord(value) {
|
|
40
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
41
|
+
}
|
|
42
|
+
function readRequiredString(data, field, filename) {
|
|
43
|
+
const value = data[field];
|
|
44
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
45
|
+
throw new Error(`Missing required fields (type, channelId, text) in ${filename}`);
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
function parsePreAction(data, filename) {
|
|
50
|
+
if (!data.preAction)
|
|
51
|
+
return undefined;
|
|
52
|
+
if (!isRecord(data.preAction)) {
|
|
53
|
+
throw new Error(`Invalid 'preAction' field in ${filename}, expected an object`);
|
|
54
|
+
}
|
|
55
|
+
const action = data.preAction;
|
|
56
|
+
if (action.type !== "bash") {
|
|
57
|
+
throw new Error(`Unsupported preAction type '${String(action.type)}' in ${filename}, only 'bash' is supported`);
|
|
58
|
+
}
|
|
59
|
+
if (typeof action.command !== "string" || action.command.trim().length === 0) {
|
|
60
|
+
throw new Error(`Missing or empty 'preAction.command' in ${filename}`);
|
|
61
|
+
}
|
|
62
|
+
if (action.timeout !== undefined) {
|
|
63
|
+
if (typeof action.timeout !== "number" || !Number.isFinite(action.timeout) || action.timeout <= 0) {
|
|
64
|
+
throw new Error(`Invalid 'preAction.timeout' in ${filename}, expected a positive millisecond value`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
type: "bash",
|
|
69
|
+
command: action.command,
|
|
70
|
+
...(typeof action.timeout === "number" ? { timeout: action.timeout } : {}),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export function parseScheduledEventContent(content, filename) {
|
|
74
|
+
const data = JSON.parse(content);
|
|
75
|
+
if (!isRecord(data)) {
|
|
76
|
+
throw new Error(`Missing required fields (type, channelId, text) in ${filename}`);
|
|
77
|
+
}
|
|
78
|
+
const type = readRequiredString(data, "type", filename);
|
|
79
|
+
const channelId = readRequiredString(data, "channelId", filename);
|
|
80
|
+
const text = readRequiredString(data, "text", filename);
|
|
81
|
+
const preAction = parsePreAction(data, filename);
|
|
82
|
+
switch (type) {
|
|
83
|
+
case "immediate":
|
|
84
|
+
return { type, channelId, text, ...(preAction ? { preAction } : {}) };
|
|
85
|
+
case "one-shot": {
|
|
86
|
+
if (typeof data.at !== "string" || data.at.trim().length === 0) {
|
|
87
|
+
throw new Error(`Missing 'at' field for one-shot event in ${filename}`);
|
|
88
|
+
}
|
|
89
|
+
return { type, channelId, text, at: data.at, ...(preAction ? { preAction } : {}) };
|
|
90
|
+
}
|
|
91
|
+
case "periodic": {
|
|
92
|
+
if (typeof data.schedule !== "string" || data.schedule.trim().length === 0) {
|
|
93
|
+
throw new Error(`Missing 'schedule' field for periodic event in ${filename}`);
|
|
94
|
+
}
|
|
95
|
+
if (typeof data.timezone !== "string" || data.timezone.trim().length === 0) {
|
|
96
|
+
throw new Error(`Missing 'timezone' field for periodic event in ${filename}`);
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
type,
|
|
100
|
+
channelId,
|
|
101
|
+
text,
|
|
102
|
+
schedule: data.schedule,
|
|
103
|
+
timezone: data.timezone,
|
|
104
|
+
...(preAction ? { preAction } : {}),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
default:
|
|
108
|
+
throw new Error(`Unknown event type '${type}' in ${filename}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
39
111
|
class EventPreActionError extends Error {
|
|
40
112
|
constructor(kind, message, options = {}) {
|
|
41
113
|
super(message);
|
|
@@ -256,62 +328,8 @@ export class EventsWatcher {
|
|
|
256
328
|
break;
|
|
257
329
|
}
|
|
258
330
|
}
|
|
259
|
-
parsePreAction(data, filename) {
|
|
260
|
-
if (!data.preAction)
|
|
261
|
-
return undefined;
|
|
262
|
-
if (typeof data.preAction !== "object" || data.preAction === null) {
|
|
263
|
-
throw new Error(`Invalid 'preAction' field in ${filename}, expected an object`);
|
|
264
|
-
}
|
|
265
|
-
const action = data.preAction;
|
|
266
|
-
if (action.type !== "bash") {
|
|
267
|
-
throw new Error(`Unsupported preAction type '${String(action.type)}' in ${filename}, only 'bash' is supported`);
|
|
268
|
-
}
|
|
269
|
-
if (typeof action.command !== "string" || action.command.trim().length === 0) {
|
|
270
|
-
throw new Error(`Missing or empty 'preAction.command' in ${filename}`);
|
|
271
|
-
}
|
|
272
|
-
if (action.timeout !== undefined) {
|
|
273
|
-
if (typeof action.timeout !== "number" || !Number.isFinite(action.timeout) || action.timeout <= 0) {
|
|
274
|
-
throw new Error(`Invalid 'preAction.timeout' in ${filename}, expected a positive millisecond value`);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return {
|
|
278
|
-
type: "bash",
|
|
279
|
-
command: action.command,
|
|
280
|
-
...(typeof action.timeout === "number" ? { timeout: action.timeout } : {}),
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
331
|
parseEvent(content, filename) {
|
|
284
|
-
|
|
285
|
-
if (!data.type || !data.channelId || !data.text) {
|
|
286
|
-
throw new Error(`Missing required fields (type, channelId, text) in ${filename}`);
|
|
287
|
-
}
|
|
288
|
-
const preAction = this.parsePreAction(data, filename);
|
|
289
|
-
switch (data.type) {
|
|
290
|
-
case "immediate":
|
|
291
|
-
return { type: "immediate", channelId: data.channelId, text: data.text, preAction };
|
|
292
|
-
case "one-shot":
|
|
293
|
-
if (!data.at) {
|
|
294
|
-
throw new Error(`Missing 'at' field for one-shot event in ${filename}`);
|
|
295
|
-
}
|
|
296
|
-
return { type: "one-shot", channelId: data.channelId, text: data.text, at: data.at, preAction };
|
|
297
|
-
case "periodic":
|
|
298
|
-
if (!data.schedule) {
|
|
299
|
-
throw new Error(`Missing 'schedule' field for periodic event in ${filename}`);
|
|
300
|
-
}
|
|
301
|
-
if (!data.timezone) {
|
|
302
|
-
throw new Error(`Missing 'timezone' field for periodic event in ${filename}`);
|
|
303
|
-
}
|
|
304
|
-
return {
|
|
305
|
-
type: "periodic",
|
|
306
|
-
channelId: data.channelId,
|
|
307
|
-
text: data.text,
|
|
308
|
-
schedule: data.schedule,
|
|
309
|
-
timezone: data.timezone,
|
|
310
|
-
preAction,
|
|
311
|
-
};
|
|
312
|
-
default:
|
|
313
|
-
throw new Error(`Unknown event type '${data.type}' in ${filename}`);
|
|
314
|
-
}
|
|
332
|
+
return parseScheduledEventContent(content, filename);
|
|
315
333
|
}
|
|
316
334
|
async handleImmediate(filename, event) {
|
|
317
335
|
const filePath = join(this.eventsDir, filename);
|
package/package.json
CHANGED