@oyasmi/pipiclaw 0.7.3 → 0.7.5
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/dist/agent/channel-runner.js +14 -0
- package/dist/agent/commands.d.ts +1 -1
- package/dist/agent/commands.js +6 -0
- package/dist/memory/task-digest.d.ts +21 -0
- package/dist/memory/task-digest.js +66 -0
- package/dist/runtime/bootstrap.js +13 -1
- package/dist/runtime/dingtalk.d.ts +1 -0
- package/dist/runtime/dingtalk.js +6 -2
- package/dist/runtime/event-commands.d.ts +5 -0
- package/dist/runtime/event-commands.js +2 -2
- package/dist/runtime/task-commands.d.ts +6 -0
- package/dist/runtime/task-commands.js +133 -0
- package/dist/settings.d.ts +7 -0
- package/dist/settings.js +13 -0
- package/dist/shared/task-ledger.d.ts +53 -0
- package/dist/shared/task-ledger.js +149 -0
- package/dist/subagents/tool.d.ts +1 -0
- package/dist/subagents/tool.js +1 -0
- package/dist/tools/bash.d.ts +6 -0
- package/dist/tools/bash.js +6 -1
- package/dist/tools/command-optimizer.d.ts +6 -0
- package/dist/tools/command-optimizer.js +73 -0
- package/dist/tools/config.d.ts +9 -0
- package/dist/tools/config.js +21 -0
- package/dist/tools/event-manage.d.ts +35 -0
- package/dist/tools/event-manage.js +238 -0
- package/dist/tools/index.js +2 -0
- package/dist/tools/registry.d.ts +2 -0
- package/dist/tools/registry.js +31 -0
- package/dist/tools/task-manage.d.ts +43 -0
- package/dist/tools/task-manage.js +226 -0
- package/dist/tui/app.js +2 -0
- package/dist/tui/commands.d.ts +1 -0
- package/dist/tui/commands.js +3 -0
- package/dist/tui/turn-controller.d.ts +1 -0
- package/dist/tui/turn-controller.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { mkdir, readdir, readFile, rename, unlink } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
import { parseScheduledEventContent } from "../runtime/events.js";
|
|
6
|
+
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
7
|
+
import { normalizeTaskId, parseTaskFrontmatter, readActiveTasks, taskBody } from "../shared/task-ledger.js";
|
|
8
|
+
const SETTABLE_STATUSES = ["open", "in-progress", "awaiting-user", "blocked"];
|
|
9
|
+
const taskManageSchema = Type.Object({
|
|
10
|
+
label: Type.String({ description: "Brief description of the ledger change (shown to the user)" }),
|
|
11
|
+
action: Type.Union([Type.Literal("set"), Type.Literal("done"), Type.Literal("list")], {
|
|
12
|
+
description: '"set" updates a task\'s status/wake/recurrence; "done" closes it out (archive one-shot tasks, clean up residual events); "list" returns active tasks.',
|
|
13
|
+
}),
|
|
14
|
+
id: Type.Optional(Type.String({ description: "Task id (filename without .md). Required for set/done." })),
|
|
15
|
+
status: Type.Optional(Type.Union(SETTABLE_STATUSES.map((status) => Type.Literal(status)), { description: "New status for set. To close a task use action done, not status." })),
|
|
16
|
+
wake: Type.Optional(Type.String({
|
|
17
|
+
description: "ISO8601 earliest-recheck time for set; empty string clears it. Keep in sync with any .checkin event.",
|
|
18
|
+
})),
|
|
19
|
+
recurrence: Type.Optional(Type.String({ description: "Annotation only (e.g. 每周一); empty string clears it." })),
|
|
20
|
+
});
|
|
21
|
+
function parseAction(action) {
|
|
22
|
+
if (action === "set" || action === "done" || action === "list") {
|
|
23
|
+
return action;
|
|
24
|
+
}
|
|
25
|
+
throw new Error('Unsupported task action. Use "set", "done", or "list".');
|
|
26
|
+
}
|
|
27
|
+
function toWorkspacePath(options, hostPath) {
|
|
28
|
+
if (hostPath.startsWith(options.workspaceDir)) {
|
|
29
|
+
return `${options.workspacePath}${hostPath.slice(options.workspaceDir.length)}`;
|
|
30
|
+
}
|
|
31
|
+
return hostPath;
|
|
32
|
+
}
|
|
33
|
+
function tasksDir(options) {
|
|
34
|
+
return join(options.channelDir, "tasks");
|
|
35
|
+
}
|
|
36
|
+
function eventsDir(options) {
|
|
37
|
+
return join(options.workspaceDir, "events");
|
|
38
|
+
}
|
|
39
|
+
function renderTaskFile(fields, body) {
|
|
40
|
+
const lines = ["---", `status: ${fields.status}`];
|
|
41
|
+
if (fields.wake)
|
|
42
|
+
lines.push(`wake: ${fields.wake}`);
|
|
43
|
+
if (fields.recurrence)
|
|
44
|
+
lines.push(`recurrence: ${fields.recurrence}`);
|
|
45
|
+
lines.push("---");
|
|
46
|
+
return `${lines.join("\n")}\n${body}`;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Read a task file and split it into its three-field frontmatter and verbatim body.
|
|
50
|
+
* Fail-closed: an unreadable frontmatter block is rejected (fix it with edit first)
|
|
51
|
+
* rather than guessed at, so task_manage never silently mangles a body.
|
|
52
|
+
*/
|
|
53
|
+
async function readTaskDocument(taskPath, id) {
|
|
54
|
+
if (!existsSync(taskPath)) {
|
|
55
|
+
throw new Error(`Task "${id}" does not exist; create it with write first.`);
|
|
56
|
+
}
|
|
57
|
+
const content = await readFile(taskPath, "utf-8");
|
|
58
|
+
const frontmatter = parseTaskFrontmatter(content);
|
|
59
|
+
if (!frontmatter.readable) {
|
|
60
|
+
throw new Error(`Task "${id}" has no readable frontmatter; fix it with edit before using task_manage.`);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
fields: {
|
|
64
|
+
status: frontmatter.status ?? "open",
|
|
65
|
+
wake: frontmatter.wake,
|
|
66
|
+
recurrence: frontmatter.recurrence,
|
|
67
|
+
},
|
|
68
|
+
body: taskBody(content),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** Apply a `set` request's optional fields onto the existing frontmatter. */
|
|
72
|
+
function applySet(fields, request) {
|
|
73
|
+
const next = { ...fields };
|
|
74
|
+
if (request.status !== undefined) {
|
|
75
|
+
if (!SETTABLE_STATUSES.includes(request.status)) {
|
|
76
|
+
throw new Error(`Invalid status "${request.status}". Use one of ${SETTABLE_STATUSES.join(", ")}, or action "done".`);
|
|
77
|
+
}
|
|
78
|
+
next.status = request.status;
|
|
79
|
+
}
|
|
80
|
+
if (request.wake !== undefined) {
|
|
81
|
+
const trimmed = request.wake.trim();
|
|
82
|
+
if (trimmed === "") {
|
|
83
|
+
next.wake = undefined;
|
|
84
|
+
}
|
|
85
|
+
else if (!Number.isFinite(new Date(trimmed).getTime())) {
|
|
86
|
+
throw new Error(`wake "${request.wake}" is not a valid ISO8601 date.`);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
next.wake = trimmed;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (request.recurrence !== undefined) {
|
|
93
|
+
const trimmed = request.recurrence.trim();
|
|
94
|
+
next.recurrence = trimmed === "" ? undefined : trimmed;
|
|
95
|
+
}
|
|
96
|
+
return next;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* On close-out, delete residual one-shot/immediate events named `task.<channelId>.<id>.*`
|
|
100
|
+
* and report whether a periodic (schedule) event survives. Unparseable events with the
|
|
101
|
+
* prefix are left untouched (fail-safe: don't blind-delete something we can't classify).
|
|
102
|
+
*/
|
|
103
|
+
async function cleanupTaskEvents(options, id) {
|
|
104
|
+
const dir = eventsDir(options);
|
|
105
|
+
if (!existsSync(dir))
|
|
106
|
+
return { deleted: [], hasPeriodic: false };
|
|
107
|
+
const prefix = `task.${options.channelId}.${id}.`;
|
|
108
|
+
const deleted = [];
|
|
109
|
+
let hasPeriodic = false;
|
|
110
|
+
for (const filename of await readdir(dir)) {
|
|
111
|
+
if (!filename.endsWith(".json") || !filename.startsWith(prefix))
|
|
112
|
+
continue;
|
|
113
|
+
const eventPath = join(dir, filename);
|
|
114
|
+
let type;
|
|
115
|
+
try {
|
|
116
|
+
type = parseScheduledEventContent(await readFile(eventPath, "utf-8"), filename).type;
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
continue; // can't classify → leave it for /events to handle
|
|
120
|
+
}
|
|
121
|
+
if (type === "periodic") {
|
|
122
|
+
hasPeriodic = true; // the cadence lives on for the next cycle
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
await unlink(eventPath);
|
|
126
|
+
deleted.push(filename.slice(0, -".json".length));
|
|
127
|
+
}
|
|
128
|
+
return { deleted, hasPeriodic };
|
|
129
|
+
}
|
|
130
|
+
async function setTask(options, request) {
|
|
131
|
+
if (!request.id)
|
|
132
|
+
throw new Error('action "set" requires an id.');
|
|
133
|
+
const id = normalizeTaskId(request.id);
|
|
134
|
+
const taskPath = join(tasksDir(options), `${id}.md`);
|
|
135
|
+
const { fields, body } = await readTaskDocument(taskPath, id);
|
|
136
|
+
const nextFields = applySet(fields, request);
|
|
137
|
+
await writeFileAtomically(taskPath, renderTaskFile(nextFields, body));
|
|
138
|
+
return {
|
|
139
|
+
action: "set",
|
|
140
|
+
id,
|
|
141
|
+
path: toWorkspacePath(options, taskPath),
|
|
142
|
+
status: nextFields.status,
|
|
143
|
+
notice: `已更新任务 \`${id}\`(status: ${nextFields.status}${nextFields.wake ? `, wake: ${nextFields.wake}` : ""})。`,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
async function doneTask(options, request) {
|
|
147
|
+
if (!request.id)
|
|
148
|
+
throw new Error('action "done" requires an id.');
|
|
149
|
+
const id = normalizeTaskId(request.id);
|
|
150
|
+
const dir = tasksDir(options);
|
|
151
|
+
const taskPath = join(dir, `${id}.md`);
|
|
152
|
+
const { fields, body } = await readTaskDocument(taskPath, id);
|
|
153
|
+
await writeFileAtomically(taskPath, renderTaskFile({ ...fields, status: "done" }, body));
|
|
154
|
+
const { deleted, hasPeriodic } = await cleanupTaskEvents(options, id);
|
|
155
|
+
// Periodic task → sleeps in place until its schedule fires next; one-shot → archive.
|
|
156
|
+
let archived = false;
|
|
157
|
+
let finalPath = taskPath;
|
|
158
|
+
if (!hasPeriodic) {
|
|
159
|
+
const archiveDir = join(dir, "archive");
|
|
160
|
+
await mkdir(archiveDir, { recursive: true });
|
|
161
|
+
finalPath = join(archiveDir, `${id}.md`);
|
|
162
|
+
await rename(taskPath, finalPath);
|
|
163
|
+
archived = true;
|
|
164
|
+
}
|
|
165
|
+
const cleanup = deleted.length > 0 ? `,清理事件 ${deleted.join(", ")}` : "";
|
|
166
|
+
const disposition = archived ? "已归档" : "周期任务留原地待下次唤醒";
|
|
167
|
+
return {
|
|
168
|
+
action: "done",
|
|
169
|
+
id,
|
|
170
|
+
path: toWorkspacePath(options, finalPath),
|
|
171
|
+
status: "done",
|
|
172
|
+
archived,
|
|
173
|
+
deletedEvents: deleted,
|
|
174
|
+
notice: `任务 \`${id}\` 已完成(${disposition}${cleanup})。`,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async function listTasks(options) {
|
|
178
|
+
const entries = await readActiveTasks(tasksDir(options));
|
|
179
|
+
return {
|
|
180
|
+
action: "list",
|
|
181
|
+
tasks: entries.map((entry) => ({
|
|
182
|
+
id: entry.id,
|
|
183
|
+
title: entry.title,
|
|
184
|
+
status: entry.frontmatter.readable ? (entry.frontmatter.status ?? "open") : "unreadable",
|
|
185
|
+
wake: entry.frontmatter.wake,
|
|
186
|
+
actionable: entry.actionable,
|
|
187
|
+
})),
|
|
188
|
+
notice: `台账共有 ${entries.length} 个 active 任务。`,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
export async function manageTask(options, request) {
|
|
192
|
+
switch (request.action) {
|
|
193
|
+
case "set":
|
|
194
|
+
return setTask(options, request);
|
|
195
|
+
case "done":
|
|
196
|
+
return doneTask(options, request);
|
|
197
|
+
case "list":
|
|
198
|
+
return listTasks(options);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
export function createTaskManageTool(options) {
|
|
202
|
+
return {
|
|
203
|
+
name: "task_manage",
|
|
204
|
+
label: "task_manage",
|
|
205
|
+
description: "Manage this channel's task ledger frontmatter and lifecycle: set status/wake/recurrence, close a task out " +
|
|
206
|
+
"(archiving one-shot tasks and cleaning up its residual events), or list active tasks. Write the task body " +
|
|
207
|
+
"(goal, DoD, manual, cycle log) with write/edit; this tool owns only the frontmatter and close-out.",
|
|
208
|
+
parameters: taskManageSchema,
|
|
209
|
+
execute: async (_toolCallId, args) => {
|
|
210
|
+
const result = await manageTask(options, {
|
|
211
|
+
action: parseAction(args.action),
|
|
212
|
+
id: args.id,
|
|
213
|
+
status: args.status,
|
|
214
|
+
wake: args.wake,
|
|
215
|
+
recurrence: args.recurrence,
|
|
216
|
+
});
|
|
217
|
+
return {
|
|
218
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
219
|
+
details: {
|
|
220
|
+
kind: "task_manage",
|
|
221
|
+
...result,
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
package/dist/tui/app.js
CHANGED
|
@@ -17,6 +17,7 @@ import { ensureChannelDir } from "../runtime/channel-paths.js";
|
|
|
17
17
|
import { finalDeliveryOf, progressStyleOf } from "../runtime/dingtalk.js";
|
|
18
18
|
import { handleEventsCommand } from "../runtime/event-commands.js";
|
|
19
19
|
import { ChannelStore } from "../runtime/store.js";
|
|
20
|
+
import { handleTasksCommand } from "../runtime/task-commands.js";
|
|
20
21
|
import { getUsageLedger } from "../usage/ledger.js";
|
|
21
22
|
import { parseUsageMode, renderUsageReport } from "../usage/render.js";
|
|
22
23
|
import { TUI_SLASH_COMMANDS } from "./commands.js";
|
|
@@ -88,6 +89,7 @@ export async function runTuiApp(options) {
|
|
|
88
89
|
renderHelp: () => renderBuiltInHelp(),
|
|
89
90
|
renderUsage: (args) => renderUsageReport(getUsageLedger(), channelId, parseUsageMode(args), new Date()),
|
|
90
91
|
runEvents: (args) => handleEventsCommand({ args, workspaceDir: paths.workspaceDir, historyPath: paths.eventHistoryPath }),
|
|
92
|
+
runTasks: (args) => handleTasksCommand({ args, channelDir }),
|
|
91
93
|
statusInfo: { version: readCliVersion(), sandbox, startedAt: Date.now() },
|
|
92
94
|
});
|
|
93
95
|
try {
|
package/dist/tui/commands.d.ts
CHANGED
|
@@ -37,6 +37,7 @@ export interface DispatchDeps {
|
|
|
37
37
|
renderStatus(): string;
|
|
38
38
|
renderUsage(args: string): string;
|
|
39
39
|
runEvents(args: string): Promise<string>;
|
|
40
|
+
runTasks(args: string): Promise<string>;
|
|
40
41
|
}
|
|
41
42
|
export declare function dispatch(input: string, deps: DispatchDeps): Promise<DispatchOutcome>;
|
|
42
43
|
/** Slash commands offered in editor autocomplete: transport + session + TUI-only. */
|
package/dist/tui/commands.js
CHANGED
|
@@ -35,6 +35,8 @@ export async function dispatch(input, deps) {
|
|
|
35
35
|
return { kind: "reply", text: deps.renderUsage(command.args) };
|
|
36
36
|
case "events":
|
|
37
37
|
return { kind: "reply", text: await deps.runEvents(command.args) };
|
|
38
|
+
case "tasks":
|
|
39
|
+
return { kind: "reply", text: await deps.runTasks(command.args) };
|
|
38
40
|
case "stop":
|
|
39
41
|
return { kind: "stop" };
|
|
40
42
|
case "steer":
|
|
@@ -56,6 +58,7 @@ export const TUI_SLASH_COMMANDS = [
|
|
|
56
58
|
{ name: "status", description: "Show model, context and run state" },
|
|
57
59
|
{ name: "usage", description: "Show LLM cost for this channel", argumentHint: "[7d|month]" },
|
|
58
60
|
{ name: "events", description: "Manage scheduled events", argumentHint: "<list|show|delete|history>" },
|
|
61
|
+
{ name: "tasks", description: "View the task ledger (read-only)", argumentHint: "[show <id>|archive]" },
|
|
59
62
|
{ name: "model", description: "Show or switch the model", argumentHint: "[provider/model]" },
|
|
60
63
|
{ name: "new", description: "Start a new session" },
|
|
61
64
|
{ name: "compact", description: "Compact the session context", argumentHint: "[instructions]" },
|
|
@@ -22,6 +22,7 @@ export interface TurnControllerDeps {
|
|
|
22
22
|
renderHelp: () => string;
|
|
23
23
|
renderUsage: (args: string) => string;
|
|
24
24
|
runEvents: (args: string) => Promise<string>;
|
|
25
|
+
runTasks: (args: string) => Promise<string>;
|
|
25
26
|
/** Static bits the status renderer needs alongside the live run state. */
|
|
26
27
|
statusInfo: {
|
|
27
28
|
version: string;
|
|
@@ -35,6 +35,7 @@ export class TurnController {
|
|
|
35
35
|
renderHelp: deps.renderHelp,
|
|
36
36
|
renderUsage: deps.renderUsage,
|
|
37
37
|
runEvents: deps.runEvents,
|
|
38
|
+
runTasks: deps.runTasks,
|
|
38
39
|
renderStatus: () => renderStatus({
|
|
39
40
|
state: { running: this.running, currentTaskText: this.currentTaskText, runner: this.deps.runner },
|
|
40
41
|
version: deps.statusInfo.version,
|
package/package.json
CHANGED