@oyasmi/pipiclaw 0.6.9 → 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 +33 -6
- package/dist/agent/commands.d.ts +1 -1
- package/dist/agent/commands.js +7 -0
- package/dist/agent/index.d.ts +0 -1
- package/dist/agent/index.js +0 -1
- package/dist/agent/progress-formatter.js +1 -1
- package/dist/agent/runner-factory.d.ts +0 -1
- package/dist/agent/runner-factory.js +0 -3
- package/dist/log.d.ts +0 -2
- package/dist/log.js +0 -8
- package/dist/memory/lifecycle.d.ts +3 -0
- package/dist/memory/lifecycle.js +26 -4
- package/dist/memory/maintenance-state.d.ts +0 -1
- package/dist/memory/maintenance-state.js +0 -6
- package/dist/paths.d.ts +3 -0
- package/dist/paths.js +3 -0
- package/dist/runtime/bootstrap.d.ts +2 -1
- package/dist/runtime/bootstrap.js +17 -3
- 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 +38 -3
- package/dist/runtime/events.js +266 -65
- package/dist/web/config.d.ts +1 -2
- package/dist/web/config.js +0 -3
- package/package.json +4 -2
|
@@ -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
|
@@ -28,27 +28,62 @@ export interface PeriodicEvent {
|
|
|
28
28
|
preAction?: EventAction;
|
|
29
29
|
}
|
|
30
30
|
export type ScheduledEvent = ImmediateEvent | OneShotEvent | PeriodicEvent;
|
|
31
|
+
export type EventHistoryAction = "loaded" | "scheduled" | "triggered" | "skipped" | "enqueued" | "queue_full" | "deleted" | "invalid" | "pre_action_started" | "pre_action_passed" | "pre_action_blocked" | "pre_action_failed" | "cancelled";
|
|
32
|
+
export type EventHistoryResult = "ok" | "error" | "skipped";
|
|
33
|
+
export interface EventHistoryRecord {
|
|
34
|
+
ts: string;
|
|
35
|
+
eventName: string;
|
|
36
|
+
eventPath: string;
|
|
37
|
+
eventType: ScheduledEvent["type"] | "unknown";
|
|
38
|
+
channelId?: string;
|
|
39
|
+
action: EventHistoryAction;
|
|
40
|
+
result: EventHistoryResult;
|
|
41
|
+
reason?: string;
|
|
42
|
+
schedule?: string;
|
|
43
|
+
timezone?: string;
|
|
44
|
+
at?: string;
|
|
45
|
+
nextRunAt?: string;
|
|
46
|
+
textPreview?: string;
|
|
47
|
+
preAction?: {
|
|
48
|
+
type: "bash";
|
|
49
|
+
command: string;
|
|
50
|
+
timeoutMs: number;
|
|
51
|
+
exitCode?: number | null;
|
|
52
|
+
durationMs?: number;
|
|
53
|
+
};
|
|
54
|
+
queue?: {
|
|
55
|
+
accepted: boolean;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export interface EventsWatcherOptions {
|
|
59
|
+
historyPath?: string;
|
|
60
|
+
}
|
|
61
|
+
export declare function formatLocalTimestamp(date?: Date): string;
|
|
62
|
+
export declare function parseScheduledEventContent(content: string, filename: string): ScheduledEvent;
|
|
31
63
|
export declare class EventsWatcher {
|
|
32
64
|
private eventsDir;
|
|
33
65
|
private bot;
|
|
34
66
|
private executor;
|
|
35
67
|
private commandGuardConfig?;
|
|
68
|
+
private options;
|
|
36
69
|
private timers;
|
|
37
70
|
private crons;
|
|
38
71
|
private debounceTimers;
|
|
39
72
|
private startTime;
|
|
40
73
|
private watcher;
|
|
41
74
|
private knownFiles;
|
|
42
|
-
constructor(eventsDir: string, bot: DingTalkBot, executor: Executor, commandGuardConfig?: SecurityConfig["commandGuard"] | undefined);
|
|
75
|
+
constructor(eventsDir: string, bot: DingTalkBot, executor: Executor, commandGuardConfig?: SecurityConfig["commandGuard"] | undefined, options?: EventsWatcherOptions);
|
|
43
76
|
start(): void;
|
|
44
77
|
stop(): void;
|
|
78
|
+
private ensureHistoryFile;
|
|
79
|
+
private appendHistory;
|
|
80
|
+
private appendEventHistory;
|
|
45
81
|
private debounce;
|
|
46
82
|
private scanExisting;
|
|
47
83
|
private handleFileChange;
|
|
48
84
|
private handleDelete;
|
|
49
85
|
private cancelScheduled;
|
|
50
86
|
private handleFile;
|
|
51
|
-
private parsePreAction;
|
|
52
87
|
private parseEvent;
|
|
53
88
|
private handleImmediate;
|
|
54
89
|
private handleOneShot;
|
|
@@ -64,4 +99,4 @@ export declare class EventsWatcher {
|
|
|
64
99
|
/**
|
|
65
100
|
* Create and start an events watcher.
|
|
66
101
|
*/
|
|
67
|
-
export declare function createEventsWatcher(workspaceDir: string, bot: DingTalkBot, executor: Executor, commandGuardConfig?: SecurityConfig["commandGuard"]): EventsWatcher;
|
|
102
|
+
export declare function createEventsWatcher(workspaceDir: string, bot: DingTalkBot, executor: Executor, commandGuardConfig?: SecurityConfig["commandGuard"], historyPath?: string): EventsWatcher;
|