@ian-pascoe/pi-todo 0.4.0 → 0.5.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 +4 -0
- package/package.json +1 -1
- package/src/pi-todo-extension.ts +39 -5
- package/src/todo-list.ts +128 -36
package/README.md
CHANGED
|
@@ -22,11 +22,14 @@ The `todo` tool supports five actions:
|
|
|
22
22
|
| `add` | `title`, optional `description` and `status` | Adds a Task |
|
|
23
23
|
| `add` | `tasks: [{ title, description? }]`, optional `status` | Adds several Tasks at once |
|
|
24
24
|
| `update` | `id`, plus fields to change | Updates a Task |
|
|
25
|
+
| `update` | `updates: [{ id, status?, title?, description? }]` | Updates several Tasks at once |
|
|
25
26
|
| `remove` | `id` | Removes one Task |
|
|
26
27
|
| `clear` | — | Removes every Task and resets IDs |
|
|
27
28
|
|
|
28
29
|
A batch `add` creates every Task in `tasks` atomically, with sequential IDs, and the result lists them. It writes one `pi-todo-state` session entry, so the tool group that made it projects a single Todo List Snapshot. If any title or description is empty, or the Task IDs would run out, it creates no Task. `tasks` must hold at least one entry and replaces `title`/`description`: a request that gives both `tasks` and `title` or a non-null `description` is rejected rather than guessed at. Per-Task fields other than `title` and `description` are rejected too. A top-level `status` applies to every new Task.
|
|
29
30
|
|
|
31
|
+
A batch `update` changes every Task in `updates` atomically and the result lists them in request order. It writes one `pi-todo-state` session entry, so the tool group that made it projects a single Todo List Snapshot. If any ID is unknown or repeated, any title or description is empty, or any entry provides no title, description, or status, it changes no Task. `updates` must hold at least one entry and replaces `id`, `title`, `description`, and `status`: a request that gives both `updates` and any of those (a `null` description counts as absent) is rejected rather than guessed at. Per-Task fields other than `id`, `status`, `title`, and `description` are rejected too.
|
|
32
|
+
|
|
30
33
|
A Pi `codemode` script receives the result as an object (the tool declares an `outputSchema`) instead of the text (its field names are already single words, so they match Pi's snake_case convention); the model still reads the same text, and failures still throw:
|
|
31
34
|
|
|
32
35
|
| Action | Script value |
|
|
@@ -34,6 +37,7 @@ A Pi `codemode` script receives the result as an object (the tool declares an `o
|
|
|
34
37
|
| `list` | `{ action: "list", tasks }`, each Task `{ id, title, description?, status }` |
|
|
35
38
|
| `add` / `update` | `{ action, task }` with the resulting Task |
|
|
36
39
|
| batch `add` | `{ action: "add", tasks }` with the created Tasks in ID order |
|
|
40
|
+
| batch `update` | `{ action: "update", tasks }` with the changed Tasks in request order |
|
|
37
41
|
| `remove` | `{ action: "remove", id }` |
|
|
38
42
|
| `clear` | `{ action: "clear", cleared }` with the number of Tasks removed |
|
|
39
43
|
|
package/package.json
CHANGED
package/src/pi-todo-extension.ts
CHANGED
|
@@ -42,16 +42,18 @@ const TodoParameters = Type.Object({
|
|
|
42
42
|
Type.Integer({
|
|
43
43
|
minimum: 1,
|
|
44
44
|
maximum: Number.MAX_SAFE_INTEGER,
|
|
45
|
-
description: "Task ID for update or remove",
|
|
45
|
+
description: "Task ID for update or remove; not allowed with updates",
|
|
46
46
|
}),
|
|
47
47
|
),
|
|
48
48
|
title: Type.Optional(
|
|
49
|
-
Type.String({
|
|
49
|
+
Type.String({
|
|
50
|
+
description: "Task title for add or update; not allowed with tasks or updates",
|
|
51
|
+
}),
|
|
50
52
|
),
|
|
51
53
|
description: Type.Optional(
|
|
52
54
|
Type.Union([Type.String(), Type.Null()], {
|
|
53
55
|
description:
|
|
54
|
-
"Optional Task description; null removes it during update; not allowed with tasks",
|
|
56
|
+
"Optional Task description; null removes it during update; not allowed with tasks or updates",
|
|
55
57
|
}),
|
|
56
58
|
),
|
|
57
59
|
tasks: Type.Optional(
|
|
@@ -70,8 +72,36 @@ const TodoParameters = Type.Object({
|
|
|
70
72
|
},
|
|
71
73
|
),
|
|
72
74
|
),
|
|
75
|
+
updates: Type.Optional(
|
|
76
|
+
Type.Array(
|
|
77
|
+
Type.Object(
|
|
78
|
+
{
|
|
79
|
+
id: Type.Integer({
|
|
80
|
+
minimum: 1,
|
|
81
|
+
maximum: Number.MAX_SAFE_INTEGER,
|
|
82
|
+
description: "Task ID",
|
|
83
|
+
}),
|
|
84
|
+
status: Type.Optional(StringEnum(TODO_STATUSES, { description: "New Task status" })),
|
|
85
|
+
title: Type.Optional(Type.String({ description: "New Task title" })),
|
|
86
|
+
description: Type.Optional(
|
|
87
|
+
Type.Union([Type.String(), Type.Null()], {
|
|
88
|
+
description: "New Task description; null removes it",
|
|
89
|
+
}),
|
|
90
|
+
),
|
|
91
|
+
},
|
|
92
|
+
{ additionalProperties: false },
|
|
93
|
+
),
|
|
94
|
+
{
|
|
95
|
+
minItems: 1,
|
|
96
|
+
description:
|
|
97
|
+
"Task changes to apply in one call, all or none, one per Task ID; use instead of id, title, description, and status",
|
|
98
|
+
},
|
|
99
|
+
),
|
|
100
|
+
),
|
|
73
101
|
status: Type.Optional(
|
|
74
|
-
StringEnum(TODO_STATUSES, {
|
|
102
|
+
StringEnum(TODO_STATUSES, {
|
|
103
|
+
description: "Task status; for add, applies to every new Task; not allowed with updates",
|
|
104
|
+
}),
|
|
75
105
|
),
|
|
76
106
|
});
|
|
77
107
|
|
|
@@ -218,6 +248,9 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
218
248
|
const count = params.tasks.length;
|
|
219
249
|
text += theme.fg("dim", ` ${formatTaskCount(count)}`);
|
|
220
250
|
}
|
|
251
|
+
if (params.action === "update" && params.updates) {
|
|
252
|
+
text += theme.fg("dim", ` ${formatTaskCount(params.updates.length)}`);
|
|
253
|
+
}
|
|
221
254
|
return new Text(text, 0, 0);
|
|
222
255
|
},
|
|
223
256
|
renderResult: (result, { expanded }, theme) => {
|
|
@@ -229,7 +262,8 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
229
262
|
const { action, tasks } = result.details;
|
|
230
263
|
if (tasks.length === 0) return new Text(theme.fg("dim", "Todo List is empty"), 0, 0);
|
|
231
264
|
const visibleTasks = expanded ? tasks : tasks.slice(0, TODO_COLLAPSED_TASK_LIMIT);
|
|
232
|
-
const
|
|
265
|
+
const verb = action === "add" ? "Added " : action === "update" ? "Updated " : "";
|
|
266
|
+
const heading = `${verb}${formatTaskCount(tasks.length)}:`;
|
|
233
267
|
const lines = [theme.fg("muted", heading)];
|
|
234
268
|
for (const task of visibleTasks) {
|
|
235
269
|
lines.push(renderTodoTaskLine(task, theme));
|
package/src/todo-list.ts
CHANGED
|
@@ -38,6 +38,14 @@ export type TodoTaskDraft = {
|
|
|
38
38
|
readonly description?: string;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
/** One Task change within a batch `update`. */
|
|
42
|
+
export type TodoTaskChange = {
|
|
43
|
+
readonly id: number;
|
|
44
|
+
readonly title?: string;
|
|
45
|
+
readonly description?: string | null;
|
|
46
|
+
readonly status?: TodoStatus;
|
|
47
|
+
};
|
|
48
|
+
|
|
41
49
|
/** Boundary input shared by the Todo tool's five operations. */
|
|
42
50
|
export type TodoActionInput = {
|
|
43
51
|
readonly action: TodoAction;
|
|
@@ -45,17 +53,18 @@ export type TodoActionInput = {
|
|
|
45
53
|
readonly title?: string;
|
|
46
54
|
readonly description?: string | null;
|
|
47
55
|
readonly tasks?: readonly TodoTaskDraft[];
|
|
56
|
+
readonly updates?: readonly TodoTaskChange[];
|
|
48
57
|
readonly status?: TodoStatus;
|
|
49
58
|
};
|
|
50
59
|
|
|
51
60
|
/**
|
|
52
61
|
* Render details and the tool's structured result. The action, plus `tasks` versus `task` for
|
|
53
|
-
* `add`, discriminates the list from the Task(s) a mutation added or updated, the ID
|
|
54
|
-
* and the count it cleared. A batch `add` returns the Tasks it created as `tasks`; a
|
|
55
|
-
* returns `task`.
|
|
62
|
+
* `add` and `update`, discriminates the list from the Task(s) a mutation added or updated, the ID
|
|
63
|
+
* it removed, and the count it cleared. A batch `add` returns the Tasks it created as `tasks`; a
|
|
64
|
+
* single `add` returns `task`; likewise a batch `update` returns the Tasks it changed as `tasks`.
|
|
56
65
|
*/
|
|
57
66
|
export type TodoToolDetails =
|
|
58
|
-
| { readonly action: "list" | "add"; readonly tasks: readonly TodoTask[] }
|
|
67
|
+
| { readonly action: "list" | "add" | "update"; readonly tasks: readonly TodoTask[] }
|
|
59
68
|
| { readonly action: "add" | "update"; readonly task: TodoTask }
|
|
60
69
|
| { readonly action: "remove"; readonly id: TodoTaskId }
|
|
61
70
|
| { readonly action: "clear"; readonly cleared: number };
|
|
@@ -74,14 +83,16 @@ const TodoTaskRecord = Type.Object({
|
|
|
74
83
|
/**
|
|
75
84
|
* JSON Schema of the `todo` tool's `structuredContent`, which codemode scripts receive instead of
|
|
76
85
|
* the model-facing text. Flat so Pi's one-line script declaration stays compact; `action` (and, for
|
|
77
|
-
* `add`, whether the request carried `tasks`) says which other field is
|
|
86
|
+
* `add` and `update`, whether the request carried `tasks` or `updates`) says which other field is
|
|
87
|
+
* present.
|
|
78
88
|
*/
|
|
79
89
|
export const TodoToolOutputSchema = Type.Object(
|
|
80
90
|
{
|
|
81
91
|
action: StringEnum(TODO_ACTIONS, { description: "The operation that ran" }),
|
|
82
92
|
tasks: Type.Optional(
|
|
83
93
|
Type.Array(TodoTaskRecord, {
|
|
84
|
-
description:
|
|
94
|
+
description:
|
|
95
|
+
"list: every Task in ID order; add with tasks: the Tasks created, in ID order; update with updates: the Tasks changed, in request order",
|
|
85
96
|
}),
|
|
86
97
|
),
|
|
87
98
|
task: Type.Optional(TodoTaskRecord),
|
|
@@ -201,7 +212,9 @@ function parseTodoDraft(
|
|
|
201
212
|
return { title: trimmedTitle, description: trimmedDescription };
|
|
202
213
|
}
|
|
203
214
|
|
|
204
|
-
function isTodoActionResult
|
|
215
|
+
function isTodoActionResult<T extends object>(
|
|
216
|
+
value: T | TodoActionResult,
|
|
217
|
+
): value is TodoActionResult {
|
|
205
218
|
return "ok" in value;
|
|
206
219
|
}
|
|
207
220
|
|
|
@@ -258,6 +271,107 @@ function addTasks(
|
|
|
258
271
|
};
|
|
259
272
|
}
|
|
260
273
|
|
|
274
|
+
/** Replaces each changed Task in place, keeping Task order and the next ID. */
|
|
275
|
+
function replaceTasks(state: TodoStateSnapshot, changed: readonly TodoTask[]): TodoStateSnapshot {
|
|
276
|
+
const byId = new Map(changed.map((task) => [task.id, task]));
|
|
277
|
+
return {
|
|
278
|
+
nextId: state.nextId,
|
|
279
|
+
tasks: state.tasks.map((candidate) => byId.get(candidate.id) ?? candidate),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Applies one change to a Task; `path` (e.g. `updates[1]`) locates a batch entry in failures. */
|
|
284
|
+
function changeTask(
|
|
285
|
+
task: TodoTask,
|
|
286
|
+
change: Pick<TodoTaskChange, "title" | "description" | "status">,
|
|
287
|
+
path: string | undefined,
|
|
288
|
+
): TodoTask | TodoActionResult {
|
|
289
|
+
if (
|
|
290
|
+
change.title === undefined &&
|
|
291
|
+
change.description === undefined &&
|
|
292
|
+
change.status === undefined
|
|
293
|
+
) {
|
|
294
|
+
return todoOperationFailure(
|
|
295
|
+
"update",
|
|
296
|
+
path === undefined
|
|
297
|
+
? "Todo update failed: provide a title, description, or status"
|
|
298
|
+
: `Todo update failed: ${path} must provide a title, description, or status`,
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
const title = change.title === undefined ? task.title : change.title.trim();
|
|
302
|
+
if (!title) {
|
|
303
|
+
return todoOperationFailure(
|
|
304
|
+
"update",
|
|
305
|
+
`Todo update failed: ${path ? `${path}.` : ""}title must not be empty`,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
const description =
|
|
309
|
+
change.description === undefined ? task.description : change.description?.trim();
|
|
310
|
+
if (change.description !== undefined && change.description !== null && !description) {
|
|
311
|
+
return todoOperationFailure(
|
|
312
|
+
"update",
|
|
313
|
+
`Todo update failed: ${path ? `${path}.` : ""}description must not be empty`,
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
return newTodoTask(task.id, title, description, change.status ?? task.status);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Validates every change before applying any, so a batch is all-or-nothing. */
|
|
320
|
+
function updateTasks(
|
|
321
|
+
state: TodoStateSnapshot,
|
|
322
|
+
input: TodoActionInput & { readonly updates: readonly TodoTaskChange[] },
|
|
323
|
+
): TodoActionResult {
|
|
324
|
+
// Models often send null for omitted fields; Pi strips those except where null is valid, so a
|
|
325
|
+
// null description here is treated as absent, as for a batch add.
|
|
326
|
+
if (
|
|
327
|
+
input.id !== undefined ||
|
|
328
|
+
input.title !== undefined ||
|
|
329
|
+
(input.description ?? undefined) !== undefined ||
|
|
330
|
+
input.status !== undefined
|
|
331
|
+
) {
|
|
332
|
+
return todoOperationFailure(
|
|
333
|
+
"update",
|
|
334
|
+
"Todo update failed: provide either id and fields or updates, not both",
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
if (input.updates.length === 0) {
|
|
338
|
+
return todoOperationFailure("update", "Todo update failed: updates must not be empty");
|
|
339
|
+
}
|
|
340
|
+
const seen = new Map<number, number>();
|
|
341
|
+
const changed: TodoTask[] = [];
|
|
342
|
+
for (const [index, change] of input.updates.entries()) {
|
|
343
|
+
const path = `updates[${index}]`;
|
|
344
|
+
const id = parseTodoTaskId(change.id);
|
|
345
|
+
if (id === undefined) {
|
|
346
|
+
return todoOperationFailure(
|
|
347
|
+
"update",
|
|
348
|
+
`Todo update failed: ${path}.id must be a positive safe integer`,
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
const first = seen.get(id);
|
|
352
|
+
if (first !== undefined) {
|
|
353
|
+
return todoOperationFailure(
|
|
354
|
+
"update",
|
|
355
|
+
`Todo update failed: ${path}.id #${id} duplicates updates[${first}].id`,
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
seen.set(id, index);
|
|
359
|
+
const task = state.tasks.find((candidate) => candidate.id === id);
|
|
360
|
+
if (!task) {
|
|
361
|
+
return todoOperationFailure("update", `Todo update failed: ${path}.id #${id} was not found`);
|
|
362
|
+
}
|
|
363
|
+
const result = changeTask(task, change, path);
|
|
364
|
+
if (isTodoActionResult(result)) return result;
|
|
365
|
+
changed.push(result);
|
|
366
|
+
}
|
|
367
|
+
return {
|
|
368
|
+
ok: true,
|
|
369
|
+
state: replaceTasks(state, changed),
|
|
370
|
+
message: `Updated ${formatTaskCount(changed.length)}\n${formatTodoList(changed)}`,
|
|
371
|
+
details: { action: "update", tasks: changed },
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
261
375
|
/** Applies one validated-by-schema tool request without performing session or UI effects. */
|
|
262
376
|
export function applyTodoAction(
|
|
263
377
|
state: TodoStateSnapshot,
|
|
@@ -280,6 +394,9 @@ export function applyTodoAction(
|
|
|
280
394
|
case "update":
|
|
281
395
|
case "remove": {
|
|
282
396
|
const action = input.action;
|
|
397
|
+
if (action === "update" && input.updates !== undefined) {
|
|
398
|
+
return updateTasks(state, { ...input, updates: input.updates });
|
|
399
|
+
}
|
|
283
400
|
if (input.id === undefined) {
|
|
284
401
|
return todoOperationFailure(action, `Todo ${action} failed: id is required`);
|
|
285
402
|
}
|
|
@@ -308,38 +425,13 @@ export function applyTodoAction(
|
|
|
308
425
|
details: { action: "remove", id: task.id },
|
|
309
426
|
};
|
|
310
427
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
input.description === undefined &&
|
|
314
|
-
input.status === undefined
|
|
315
|
-
) {
|
|
316
|
-
return todoOperationFailure(
|
|
317
|
-
"update",
|
|
318
|
-
"Todo update failed: provide a title, description, or status",
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
const title = input.title === undefined ? task.title : input.title.trim();
|
|
322
|
-
if (!title) {
|
|
323
|
-
return todoOperationFailure("update", "Todo update failed: title must not be empty");
|
|
324
|
-
}
|
|
325
|
-
const description =
|
|
326
|
-
input.description === undefined ? task.description : input.description?.trim();
|
|
327
|
-
if (input.description !== undefined && input.description !== null && !description) {
|
|
328
|
-
return todoOperationFailure("update", "Todo update failed: description must not be empty");
|
|
329
|
-
}
|
|
330
|
-
const updatedTask: TodoTask = description
|
|
331
|
-
? { id: task.id, title, description, status: input.status ?? task.status }
|
|
332
|
-
: { id: task.id, title, status: input.status ?? task.status };
|
|
428
|
+
const changed = changeTask(task, input, undefined);
|
|
429
|
+
if (isTodoActionResult(changed)) return changed;
|
|
333
430
|
return {
|
|
334
431
|
ok: true,
|
|
335
|
-
state:
|
|
336
|
-
nextId: state.nextId,
|
|
337
|
-
tasks: state.tasks.map((candidate) =>
|
|
338
|
-
candidate.id === updatedTask.id ? updatedTask : candidate,
|
|
339
|
-
),
|
|
340
|
-
},
|
|
432
|
+
state: replaceTasks(state, [changed]),
|
|
341
433
|
message: `Updated Task #${task.id}`,
|
|
342
|
-
details: { action: "update", task:
|
|
434
|
+
details: { action: "update", task: changed },
|
|
343
435
|
};
|
|
344
436
|
}
|
|
345
437
|
|