@ian-pascoe/pi-todo 0.3.0 → 0.4.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 +11 -7
- package/package.json +1 -1
- package/src/pi-todo-extension.ts +34 -9
- package/src/todo-list.ts +112 -28
package/README.md
CHANGED
|
@@ -16,13 +16,16 @@ pi -e ./src/index.ts
|
|
|
16
16
|
|
|
17
17
|
The `todo` tool supports five actions:
|
|
18
18
|
|
|
19
|
-
| Action | Input
|
|
20
|
-
| -------- |
|
|
21
|
-
| `list` | —
|
|
22
|
-
| `add` | `title`, optional `description` and `status`
|
|
23
|
-
| `
|
|
24
|
-
| `
|
|
25
|
-
| `
|
|
19
|
+
| Action | Input | Result |
|
|
20
|
+
| -------- | ----------------------------------------------------- | --------------------------------- |
|
|
21
|
+
| `list` | — | Lists every Task |
|
|
22
|
+
| `add` | `title`, optional `description` and `status` | Adds a Task |
|
|
23
|
+
| `add` | `tasks: [{ title, description? }]`, optional `status` | Adds several Tasks at once |
|
|
24
|
+
| `update` | `id`, plus fields to change | Updates a Task |
|
|
25
|
+
| `remove` | `id` | Removes one Task |
|
|
26
|
+
| `clear` | — | Removes every Task and resets IDs |
|
|
27
|
+
|
|
28
|
+
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.
|
|
26
29
|
|
|
27
30
|
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:
|
|
28
31
|
|
|
@@ -30,6 +33,7 @@ A Pi `codemode` script receives the result as an object (the tool declares an `o
|
|
|
30
33
|
| ---------------- | ---------------------------------------------------------------------------- |
|
|
31
34
|
| `list` | `{ action: "list", tasks }`, each Task `{ id, title, description?, status }` |
|
|
32
35
|
| `add` / `update` | `{ action, task }` with the resulting Task |
|
|
36
|
+
| batch `add` | `{ action: "add", tasks }` with the created Tasks in ID order |
|
|
33
37
|
| `remove` | `{ action: "remove", id }` |
|
|
34
38
|
| `clear` | `{ action: "clear", cleared }` with the number of Tasks removed |
|
|
35
39
|
|
package/package.json
CHANGED
package/src/pi-todo-extension.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { projectTodoContext, todoStateFromEntry } from "./todo-context.js";
|
|
|
6
6
|
import {
|
|
7
7
|
applyTodoAction,
|
|
8
8
|
createEmptyTodoState,
|
|
9
|
+
formatTaskCount,
|
|
9
10
|
TODO_ACTIONS,
|
|
10
11
|
TODO_STATUSES,
|
|
11
12
|
TodoToolOutputSchema,
|
|
@@ -44,13 +45,34 @@ const TodoParameters = Type.Object({
|
|
|
44
45
|
description: "Task ID for update or remove",
|
|
45
46
|
}),
|
|
46
47
|
),
|
|
47
|
-
title: Type.Optional(
|
|
48
|
+
title: Type.Optional(
|
|
49
|
+
Type.String({ description: "Task title for add or update; not allowed with tasks" }),
|
|
50
|
+
),
|
|
48
51
|
description: Type.Optional(
|
|
49
52
|
Type.Union([Type.String(), Type.Null()], {
|
|
50
|
-
description:
|
|
53
|
+
description:
|
|
54
|
+
"Optional Task description; null removes it during update; not allowed with tasks",
|
|
51
55
|
}),
|
|
52
56
|
),
|
|
53
|
-
|
|
57
|
+
tasks: Type.Optional(
|
|
58
|
+
Type.Array(
|
|
59
|
+
Type.Object(
|
|
60
|
+
{
|
|
61
|
+
title: Type.String({ description: "Task title" }),
|
|
62
|
+
description: Type.Optional(Type.String({ description: "Optional Task description" })),
|
|
63
|
+
},
|
|
64
|
+
{ additionalProperties: false },
|
|
65
|
+
),
|
|
66
|
+
{
|
|
67
|
+
minItems: 1,
|
|
68
|
+
description:
|
|
69
|
+
"Tasks to add in one call, all or none, with sequential IDs; use instead of title and description",
|
|
70
|
+
},
|
|
71
|
+
),
|
|
72
|
+
),
|
|
73
|
+
status: Type.Optional(
|
|
74
|
+
StringEnum(TODO_STATUSES, { description: "Task status; for add, applies to every new Task" }),
|
|
75
|
+
),
|
|
54
76
|
});
|
|
55
77
|
|
|
56
78
|
function restoreTodoState(context: ExtensionContext): TodoStateSnapshot {
|
|
@@ -192,6 +214,10 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
192
214
|
let text = theme.fg("toolTitle", theme.bold("todo")) + theme.fg("muted", ` ${params.action}`);
|
|
193
215
|
if (params.id !== undefined) text += theme.fg("accent", ` #${params.id}`);
|
|
194
216
|
if (params.action === "add" && params.title) text += theme.fg("dim", ` "${params.title}"`);
|
|
217
|
+
if (params.action === "add" && params.tasks) {
|
|
218
|
+
const count = params.tasks.length;
|
|
219
|
+
text += theme.fg("dim", ` ${formatTaskCount(count)}`);
|
|
220
|
+
}
|
|
195
221
|
return new Text(text, 0, 0);
|
|
196
222
|
},
|
|
197
223
|
renderResult: (result, { expanded }, theme) => {
|
|
@@ -199,13 +225,12 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
199
225
|
if (!result.details) {
|
|
200
226
|
return new Text(theme.fg("error", text?.text ?? "Todo operation failed"), 0, 0);
|
|
201
227
|
}
|
|
202
|
-
if (result.details
|
|
203
|
-
const tasks = result.details
|
|
228
|
+
if ("tasks" in result.details) {
|
|
229
|
+
const { action, tasks } = result.details;
|
|
204
230
|
if (tasks.length === 0) return new Text(theme.fg("dim", "Todo List is empty"), 0, 0);
|
|
205
231
|
const visibleTasks = expanded ? tasks : tasks.slice(0, TODO_COLLAPSED_TASK_LIMIT);
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
];
|
|
232
|
+
const heading = `${action === "add" ? "Added " : ""}${formatTaskCount(tasks.length)}:`;
|
|
233
|
+
const lines = [theme.fg("muted", heading)];
|
|
209
234
|
for (const task of visibleTasks) {
|
|
210
235
|
lines.push(renderTodoTaskLine(task, theme));
|
|
211
236
|
if (expanded && task.description) {
|
|
@@ -244,7 +269,7 @@ export default function piTodoExtension(pi: ExtensionAPI): void {
|
|
|
244
269
|
}
|
|
245
270
|
const confirmed = await context.ui.confirm(
|
|
246
271
|
"Clear Todo List",
|
|
247
|
-
`Remove all ${state.tasks.length}
|
|
272
|
+
`Remove all ${formatTaskCount(state.tasks.length)}?`,
|
|
248
273
|
);
|
|
249
274
|
if (!confirmed) return;
|
|
250
275
|
const result = runTodoAction({ action: "clear" }, context);
|
package/src/todo-list.ts
CHANGED
|
@@ -32,21 +32,30 @@ export type TodoStateSnapshot = {
|
|
|
32
32
|
readonly tasks: readonly TodoTask[];
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
/** One Task to create within a batch `add`. */
|
|
36
|
+
export type TodoTaskDraft = {
|
|
37
|
+
readonly title: string;
|
|
38
|
+
readonly description?: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
35
41
|
/** Boundary input shared by the Todo tool's five operations. */
|
|
36
42
|
export type TodoActionInput = {
|
|
37
43
|
readonly action: TodoAction;
|
|
38
44
|
readonly id?: number;
|
|
39
45
|
readonly title?: string;
|
|
40
46
|
readonly description?: string | null;
|
|
47
|
+
readonly tasks?: readonly TodoTaskDraft[];
|
|
41
48
|
readonly status?: TodoStatus;
|
|
42
49
|
};
|
|
43
50
|
|
|
44
51
|
/**
|
|
45
|
-
* Render details and the tool's structured result. The action
|
|
46
|
-
* Task a mutation added or updated, the ID it removed,
|
|
52
|
+
* 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 it removed,
|
|
54
|
+
* and the count it cleared. A batch `add` returns the Tasks it created as `tasks`; a single `add`
|
|
55
|
+
* returns `task`.
|
|
47
56
|
*/
|
|
48
57
|
export type TodoToolDetails =
|
|
49
|
-
| { readonly action: "list"; readonly tasks: readonly TodoTask[] }
|
|
58
|
+
| { readonly action: "list" | "add"; readonly tasks: readonly TodoTask[] }
|
|
50
59
|
| { readonly action: "add" | "update"; readonly task: TodoTask }
|
|
51
60
|
| { readonly action: "remove"; readonly id: TodoTaskId }
|
|
52
61
|
| { readonly action: "clear"; readonly cleared: number };
|
|
@@ -64,14 +73,16 @@ const TodoTaskRecord = Type.Object({
|
|
|
64
73
|
|
|
65
74
|
/**
|
|
66
75
|
* JSON Schema of the `todo` tool's `structuredContent`, which codemode scripts receive instead of
|
|
67
|
-
* the model-facing text. Flat so Pi's one-line script declaration stays compact; `action`
|
|
68
|
-
* which other field is present.
|
|
76
|
+
* 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 present.
|
|
69
78
|
*/
|
|
70
79
|
export const TodoToolOutputSchema = Type.Object(
|
|
71
80
|
{
|
|
72
81
|
action: StringEnum(TODO_ACTIONS, { description: "The operation that ran" }),
|
|
73
82
|
tasks: Type.Optional(
|
|
74
|
-
Type.Array(TodoTaskRecord, {
|
|
83
|
+
Type.Array(TodoTaskRecord, {
|
|
84
|
+
description: "list: every Task in ID order; add with tasks: the Tasks created, in ID order",
|
|
85
|
+
}),
|
|
75
86
|
),
|
|
76
87
|
task: Type.Optional(TodoTaskRecord),
|
|
77
88
|
id: Type.Optional(
|
|
@@ -157,6 +168,96 @@ function todoOperationFailure(action: TodoAction, message: string): TodoActionRe
|
|
|
157
168
|
return { ok: false, error: new TodoOperationError(action, message) };
|
|
158
169
|
}
|
|
159
170
|
|
|
171
|
+
function newTodoTask(
|
|
172
|
+
id: TodoTaskId,
|
|
173
|
+
title: string,
|
|
174
|
+
description: string | undefined,
|
|
175
|
+
status: TodoStatus,
|
|
176
|
+
): TodoTask {
|
|
177
|
+
return description ? { id, title, description, status } : { id, title, status };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Counts Tasks in model-facing text: "1 Task", "3 Tasks". */
|
|
181
|
+
export function formatTaskCount(count: number): string {
|
|
182
|
+
return `${count} ${count === 1 ? "Task" : "Tasks"}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
type ParsedTodoDraft = { readonly title: string; readonly description: string | undefined };
|
|
186
|
+
|
|
187
|
+
/** Trims and validates one Task to add; `field` prefixes the field name in the failure message. */
|
|
188
|
+
function parseTodoDraft(
|
|
189
|
+
title: string | undefined,
|
|
190
|
+
description: string | null | undefined,
|
|
191
|
+
field: string,
|
|
192
|
+
): ParsedTodoDraft | TodoActionResult {
|
|
193
|
+
const trimmedTitle = title?.trim();
|
|
194
|
+
if (!trimmedTitle) {
|
|
195
|
+
return todoOperationFailure("add", `Todo add failed: ${field}title must not be empty`);
|
|
196
|
+
}
|
|
197
|
+
const trimmedDescription = description?.trim();
|
|
198
|
+
if (description !== undefined && description !== null && !trimmedDescription) {
|
|
199
|
+
return todoOperationFailure("add", `Todo add failed: ${field}description must not be empty`);
|
|
200
|
+
}
|
|
201
|
+
return { title: trimmedTitle, description: trimmedDescription };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isTodoActionResult(value: ParsedTodoDraft | TodoActionResult): value is TodoActionResult {
|
|
205
|
+
return "ok" in value;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function addTask(state: TodoStateSnapshot, input: TodoActionInput): TodoActionResult {
|
|
209
|
+
const draft = parseTodoDraft(input.title, input.description, "");
|
|
210
|
+
if (isTodoActionResult(draft)) return draft;
|
|
211
|
+
const id = parseTodoTaskId(state.nextId);
|
|
212
|
+
if (id === undefined || state.nextId === Number.MAX_SAFE_INTEGER) {
|
|
213
|
+
return todoOperationFailure("add", "Todo add failed: Task ID limit reached");
|
|
214
|
+
}
|
|
215
|
+
const task = newTodoTask(id, draft.title, draft.description, input.status ?? "pending");
|
|
216
|
+
return {
|
|
217
|
+
ok: true,
|
|
218
|
+
state: { nextId: state.nextId + 1, tasks: [...state.tasks, task] },
|
|
219
|
+
message: `Added Task #${task.id}`,
|
|
220
|
+
details: { action: "add", task },
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Validates every draft before creating any Task, so a batch is all-or-nothing. */
|
|
225
|
+
function addTasks(
|
|
226
|
+
state: TodoStateSnapshot,
|
|
227
|
+
input: TodoActionInput & { readonly tasks: readonly TodoTaskDraft[] },
|
|
228
|
+
): TodoActionResult {
|
|
229
|
+
// A null description is "absent" for add, as it is for a single Task.
|
|
230
|
+
if (input.title !== undefined || (input.description ?? undefined) !== undefined) {
|
|
231
|
+
return todoOperationFailure("add", "Todo add failed: provide either title or tasks, not both");
|
|
232
|
+
}
|
|
233
|
+
if (input.tasks.length === 0) {
|
|
234
|
+
return todoOperationFailure("add", "Todo add failed: tasks must not be empty");
|
|
235
|
+
}
|
|
236
|
+
const drafts: ParsedTodoDraft[] = [];
|
|
237
|
+
for (const [index, task] of input.tasks.entries()) {
|
|
238
|
+
const draft = parseTodoDraft(task.title, task.description, `tasks[${index}].`);
|
|
239
|
+
if (isTodoActionResult(draft)) return draft;
|
|
240
|
+
drafts.push(draft);
|
|
241
|
+
}
|
|
242
|
+
// The last Task's successor ID must stay a safe integer so the saved state restores.
|
|
243
|
+
const exceedsIdLimit = state.nextId + drafts.length > Number.MAX_SAFE_INTEGER;
|
|
244
|
+
const status = input.status ?? "pending";
|
|
245
|
+
const added: TodoTask[] = [];
|
|
246
|
+
for (const [index, { title, description }] of drafts.entries()) {
|
|
247
|
+
const id = parseTodoTaskId(state.nextId + index);
|
|
248
|
+
if (id === undefined || exceedsIdLimit) {
|
|
249
|
+
return todoOperationFailure("add", "Todo add failed: Task ID limit reached");
|
|
250
|
+
}
|
|
251
|
+
added.push(newTodoTask(id, title, description, status));
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
ok: true,
|
|
255
|
+
state: { nextId: state.nextId + added.length, tasks: [...state.tasks, ...added] },
|
|
256
|
+
message: `Added ${formatTaskCount(added.length)}\n${formatTodoList(added)}`,
|
|
257
|
+
details: { action: "add", tasks: added },
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
160
261
|
/** Applies one validated-by-schema tool request without performing session or UI effects. */
|
|
161
262
|
export function applyTodoAction(
|
|
162
263
|
state: TodoStateSnapshot,
|
|
@@ -171,27 +272,10 @@ export function applyTodoAction(
|
|
|
171
272
|
details: { action: "list", tasks: state.tasks },
|
|
172
273
|
};
|
|
173
274
|
|
|
174
|
-
case "add":
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (input.description !== undefined && input.description !== null && !description) {
|
|
179
|
-
return todoOperationFailure("add", "Todo add failed: description must not be empty");
|
|
180
|
-
}
|
|
181
|
-
const id = parseTodoTaskId(state.nextId);
|
|
182
|
-
if (id === undefined || state.nextId === Number.MAX_SAFE_INTEGER) {
|
|
183
|
-
return todoOperationFailure("add", "Todo add failed: Task ID limit reached");
|
|
184
|
-
}
|
|
185
|
-
const task: TodoTask = description
|
|
186
|
-
? { id, title, description, status: input.status ?? "pending" }
|
|
187
|
-
: { id, title, status: input.status ?? "pending" };
|
|
188
|
-
return {
|
|
189
|
-
ok: true,
|
|
190
|
-
state: { nextId: state.nextId + 1, tasks: [...state.tasks, task] },
|
|
191
|
-
message: `Added Task #${task.id}`,
|
|
192
|
-
details: { action: "add", task },
|
|
193
|
-
};
|
|
194
|
-
}
|
|
275
|
+
case "add":
|
|
276
|
+
return input.tasks === undefined
|
|
277
|
+
? addTask(state, input)
|
|
278
|
+
: addTasks(state, { ...input, tasks: input.tasks });
|
|
195
279
|
|
|
196
280
|
case "update":
|
|
197
281
|
case "remove": {
|
|
@@ -264,7 +348,7 @@ export function applyTodoAction(
|
|
|
264
348
|
return {
|
|
265
349
|
ok: true,
|
|
266
350
|
state: count > 0 || state.nextId !== 1 ? createEmptyTodoState() : state,
|
|
267
|
-
message: `Cleared ${count}
|
|
351
|
+
message: `Cleared ${formatTaskCount(count)}`,
|
|
268
352
|
details: { action: "clear", cleared: count },
|
|
269
353
|
};
|
|
270
354
|
}
|