@oyasmi/pipiclaw 0.7.6 → 0.8.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.
Files changed (48) hide show
  1. package/README.md +13 -0
  2. package/dist/agent/channel-runner.d.ts +3 -0
  3. package/dist/agent/channel-runner.js +14 -4
  4. package/dist/agent/commands.js +8 -2
  5. package/dist/agent/prompt-builder.js +46 -0
  6. package/dist/agent/session-events.js +5 -0
  7. package/dist/agent/types.d.ts +2 -0
  8. package/dist/memory/recall.js +6 -0
  9. package/dist/memory/task-digest.js +17 -0
  10. package/dist/models/api-keys.d.ts +5 -1
  11. package/dist/models/api-keys.js +10 -4
  12. package/dist/runtime/bootstrap.d.ts +5 -0
  13. package/dist/runtime/bootstrap.js +59 -2
  14. package/dist/runtime/dingtalk.d.ts +2 -0
  15. package/dist/runtime/dingtalk.js +13 -1
  16. package/dist/runtime/durable-dispatch.d.ts +28 -0
  17. package/dist/runtime/durable-dispatch.js +144 -0
  18. package/dist/runtime/events.d.ts +4 -2
  19. package/dist/runtime/events.js +12 -8
  20. package/dist/runtime/task-commands.d.ts +9 -0
  21. package/dist/runtime/task-commands.js +429 -2
  22. package/dist/runtime/task-driver.d.ts +32 -0
  23. package/dist/runtime/task-driver.js +270 -0
  24. package/dist/settings.d.ts +12 -0
  25. package/dist/settings.js +30 -0
  26. package/dist/shared/atomic-file.js +23 -3
  27. package/dist/shared/task-events.d.ts +32 -0
  28. package/dist/shared/task-events.js +29 -0
  29. package/dist/shared/task-ledger.d.ts +53 -0
  30. package/dist/shared/task-ledger.js +246 -8
  31. package/dist/subagents/tool.d.ts +13 -0
  32. package/dist/subagents/tool.js +229 -15
  33. package/dist/tasks/artifact-subject.d.ts +6 -0
  34. package/dist/tasks/artifact-subject.js +29 -0
  35. package/dist/tasks/control.d.ts +83 -0
  36. package/dist/tasks/control.js +292 -0
  37. package/dist/tasks/store.d.ts +32 -0
  38. package/dist/tasks/store.js +142 -0
  39. package/dist/tasks/verification.d.ts +23 -0
  40. package/dist/tasks/verification.js +68 -0
  41. package/dist/tools/registry.d.ts +1 -1
  42. package/dist/tools/registry.js +6 -4
  43. package/dist/tools/task-manage.d.ts +51 -3
  44. package/dist/tools/task-manage.js +491 -37
  45. package/dist/tools/web-fetch.js +3 -0
  46. package/dist/tui/app.js +1 -1
  47. package/dist/tui/commands.js +1 -1
  48. package/package.json +1 -1
@@ -0,0 +1,144 @@
1
+ import { createHash } from "node:crypto";
2
+ import { readdir, readFile, unlink } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { writeFileAtomically } from "../shared/atomic-file.js";
5
+ import { createSerialQueue } from "../shared/serial-queue.js";
6
+ const DEFAULT_LEASE_MS = 15 * 60_000;
7
+ const DEFAULT_INTERVAL_MS = 30_000;
8
+ function recordPath(stateDir, id) {
9
+ return join(stateDir, `${id}.json`);
10
+ }
11
+ function dispatchId(event) {
12
+ return createHash("sha256")
13
+ .update(JSON.stringify([event.channelId, event.user, event.ts, event.text, event.conversationId]))
14
+ .digest("hex");
15
+ }
16
+ function isRecord(value) {
17
+ return typeof value === "object" && value !== null;
18
+ }
19
+ function parseRecord(raw) {
20
+ try {
21
+ const value = JSON.parse(raw);
22
+ if (!isRecord(value) ||
23
+ value.version !== 1 ||
24
+ typeof value.id !== "string" ||
25
+ typeof value.createdAt !== "string" ||
26
+ (value.status !== "pending" && value.status !== "queued" && value.status !== "running") ||
27
+ !isRecord(value.event) ||
28
+ typeof value.event.channelId !== "string" ||
29
+ typeof value.event.text !== "string" ||
30
+ typeof value.deliveries !== "number") {
31
+ return undefined;
32
+ }
33
+ return value;
34
+ }
35
+ catch {
36
+ return undefined;
37
+ }
38
+ }
39
+ /**
40
+ * Tiny file-backed outbox for synthetic work. It intentionally provides
41
+ * at-least-once delivery: a crash after enqueue may replay a task/event, while
42
+ * a crash can no longer silently discard it merely because it left an in-memory
43
+ * channel queue.
44
+ */
45
+ export class DurableDispatchService {
46
+ constructor(options) {
47
+ this.options = options;
48
+ this.queue = createSerialQueue();
49
+ this.timer = null;
50
+ this.leaseMs = options.leaseMs ?? DEFAULT_LEASE_MS;
51
+ }
52
+ start() {
53
+ if (this.timer)
54
+ return;
55
+ void this.drainOnce();
56
+ this.timer = setInterval(() => void this.drainOnce(), this.options.intervalMs ?? DEFAULT_INTERVAL_MS);
57
+ this.timer.unref?.();
58
+ }
59
+ stop() {
60
+ if (!this.timer)
61
+ return;
62
+ clearInterval(this.timer);
63
+ this.timer = null;
64
+ }
65
+ async dispatch(event) {
66
+ const id = event.dispatchId ?? dispatchId(event);
67
+ await this.queue.run(id, async () => {
68
+ const existing = await this.read(id);
69
+ if (existing)
70
+ return;
71
+ const record = {
72
+ version: 1,
73
+ id,
74
+ createdAt: new Date().toISOString(),
75
+ status: "pending",
76
+ event: { ...event, dispatchId: id },
77
+ deliveries: 0,
78
+ };
79
+ await this.write(record);
80
+ });
81
+ await this.drainOnce();
82
+ return true;
83
+ }
84
+ async markStarted(id) {
85
+ if (!id)
86
+ return;
87
+ await this.queue.run(id, async () => {
88
+ const record = await this.read(id);
89
+ if (!record)
90
+ return;
91
+ record.status = "running";
92
+ record.leaseExpiresAt = new Date(Date.now() + this.leaseMs).toISOString();
93
+ await this.write(record);
94
+ });
95
+ }
96
+ async markCompleted(id) {
97
+ if (!id)
98
+ return;
99
+ await this.queue.run(id, async () => {
100
+ await unlink(recordPath(this.options.stateDir, id)).catch(() => undefined);
101
+ });
102
+ }
103
+ async drainOnce(now = Date.now()) {
104
+ let filenames;
105
+ try {
106
+ filenames = (await readdir(this.options.stateDir)).filter((name) => name.endsWith(".json")).sort();
107
+ }
108
+ catch {
109
+ return;
110
+ }
111
+ for (const filename of filenames) {
112
+ const id = filename.slice(0, -".json".length);
113
+ await this.queue.run(id, async () => {
114
+ const record = await this.read(id);
115
+ if (!record)
116
+ return;
117
+ const leaseMs = record.leaseExpiresAt ? new Date(record.leaseExpiresAt).getTime() : undefined;
118
+ if ((record.status === "queued" || record.status === "running") && leaseMs && leaseMs > now)
119
+ return;
120
+ record.status = "queued";
121
+ record.deliveries++;
122
+ record.leaseExpiresAt = new Date(now + this.leaseMs).toISOString();
123
+ await this.write(record);
124
+ const accepted = this.options.bot.enqueueEvent({ ...record.event, dispatchId: record.id });
125
+ if (accepted)
126
+ return;
127
+ record.status = "pending";
128
+ record.leaseExpiresAt = undefined;
129
+ await this.write(record);
130
+ });
131
+ }
132
+ }
133
+ async read(id) {
134
+ try {
135
+ return parseRecord(await readFile(recordPath(this.options.stateDir, id), "utf-8"));
136
+ }
137
+ catch {
138
+ return undefined;
139
+ }
140
+ }
141
+ async write(record) {
142
+ await writeFileAtomically(recordPath(this.options.stateDir, record.id), `${JSON.stringify(record)}\n`);
143
+ }
144
+ }
@@ -1,6 +1,6 @@
1
1
  import type { Executor } from "../sandbox.js";
2
2
  import type { SecurityConfig } from "../security/types.js";
3
- import type { DingTalkBot } from "./dingtalk.js";
3
+ import type { DingTalkBot, DingTalkEvent } from "./dingtalk.js";
4
4
  export interface EventAction {
5
5
  type: "bash";
6
6
  command: string;
@@ -57,6 +57,8 @@ export interface EventHistoryRecord {
57
57
  }
58
58
  export interface EventsWatcherOptions {
59
59
  historyPath?: string;
60
+ /** Persist synthetic work before it enters the in-memory channel queue. */
61
+ dispatch?: (event: DingTalkEvent) => boolean | Promise<boolean>;
60
62
  }
61
63
  export declare function formatLocalTimestamp(date?: Date): string;
62
64
  export declare function parseScheduledEventContent(content: string, filename: string): ScheduledEvent;
@@ -99,4 +101,4 @@ export declare class EventsWatcher {
99
101
  /**
100
102
  * Create and start an events watcher.
101
103
  */
102
- export declare function createEventsWatcher(workspaceDir: string, bot: DingTalkBot, executor: Executor, commandGuardConfig?: SecurityConfig["commandGuard"], historyPath?: string): EventsWatcher;
104
+ export declare function createEventsWatcher(workspaceDir: string, bot: DingTalkBot, executor: Executor, commandGuardConfig?: SecurityConfig["commandGuard"], historyPath?: string, dispatch?: EventsWatcherOptions["dispatch"]): EventsWatcher;
@@ -319,7 +319,7 @@ export class EventsWatcher {
319
319
  this.handleImmediate(filename, event);
320
320
  break;
321
321
  case "one-shot":
322
- this.handleOneShot(filename, event);
322
+ await this.handleOneShot(filename, event);
323
323
  break;
324
324
  case "periodic":
325
325
  this.handlePeriodic(filename, event);
@@ -347,7 +347,7 @@ export class EventsWatcher {
347
347
  this.appendEventHistory(filename, event, "triggered", "ok");
348
348
  await this.execute(filename, event);
349
349
  }
350
- handleOneShot(filename, event) {
350
+ async handleOneShot(filename, event) {
351
351
  const atTime = new Date(event.at).getTime();
352
352
  const now = Date.now();
353
353
  if (!Number.isFinite(atTime)) {
@@ -357,9 +357,11 @@ export class EventsWatcher {
357
357
  return;
358
358
  }
359
359
  if (atTime <= now) {
360
- log.logInfo(`One-shot event in the past, deleting: ${filename}`);
361
- this.appendEventHistory(filename, event, "skipped", "skipped", { reason: "one-shot event is in the past" });
362
- this.deleteFile(filename);
360
+ // A persisted dispatch turns a missed one-shot into a safe fire-once-now
361
+ // recovery instead of silently discarding work after a process restart.
362
+ log.logInfo(`Recovering past one-shot event now: ${filename}`);
363
+ this.appendEventHistory(filename, event, "triggered", "ok", { reason: "one-shot recovered after due time" });
364
+ await this.execute(filename, event);
363
365
  return;
364
366
  }
365
367
  const delay = atTime - now;
@@ -481,7 +483,9 @@ export class EventsWatcher {
481
483
  conversationId: "",
482
484
  conversationType: "1",
483
485
  };
484
- const enqueued = this.bot.enqueueEvent(syntheticEvent);
486
+ const enqueued = this.options.dispatch
487
+ ? await this.options.dispatch(syntheticEvent)
488
+ : this.bot.enqueueEvent(syntheticEvent);
485
489
  if (enqueued) {
486
490
  this.appendEventHistory(filename, event, "enqueued", "ok", { queue: { accepted: true } });
487
491
  if (deleteAfter) {
@@ -581,7 +585,7 @@ export class EventsWatcher {
581
585
  /**
582
586
  * Create and start an events watcher.
583
587
  */
584
- export function createEventsWatcher(workspaceDir, bot, executor, commandGuardConfig, historyPath) {
588
+ export function createEventsWatcher(workspaceDir, bot, executor, commandGuardConfig, historyPath, dispatch) {
585
589
  const eventsDir = join(workspaceDir, "events");
586
- return new EventsWatcher(eventsDir, bot, executor, commandGuardConfig, { historyPath });
590
+ return new EventsWatcher(eventsDir, bot, executor, commandGuardConfig, { historyPath, dispatch });
587
591
  }
@@ -2,5 +2,14 @@ export interface HandleTasksCommandOptions {
2
2
  args: string;
3
3
  /** The channel directory; tasks live in `<channelDir>/tasks/`. */
4
4
  channelDir: string;
5
+ /** Workspace directory; required for `/tasks doctor` because events are workspace-scoped. */
6
+ workspaceDir?: string;
7
+ channelId?: string;
8
+ /** Direct command issuer; used to create an auditable external-action approval. */
9
+ approver?: string;
10
+ /** Optional immediate task wake, available in the long-lived DingTalk runtime. */
11
+ dispatchTask?: (id: string) => Promise<boolean>;
5
12
  }
13
+ export declare function pauseTask(options: HandleTasksCommandOptions, idInput: string): Promise<string>;
14
+ export declare function resumeTask(options: HandleTasksCommandOptions, idInput: string): Promise<string>;
6
15
  export declare function handleTasksCommand(options: HandleTasksCommandOptions): Promise<string>;