@ftrdotdev/pi-tracker 0.7.0 → 0.9.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.
@@ -3,9 +3,19 @@
3
3
  * contract, and the prompt-facing strings. Everything here is free of runtime
4
4
  * state; behavior (execute and rendering) lives in index.ts.
5
5
  */
6
- import { StringEnum } from "@earendil-works/pi-ai";
7
- import { Type } from "typebox";
8
- import type { Static } from "typebox";
6
+ import {
7
+ arr,
8
+ bool,
9
+ lit,
10
+ num,
11
+ obj,
12
+ oneOf,
13
+ opt,
14
+ str,
15
+ union,
16
+ type Schema,
17
+ type Static,
18
+ } from "@earendil-works/pi-schema";
9
19
  import { dependentsOf, formatItemRef, parseItemRef, unsatisfiedDeps } from "../core/deps.ts";
10
20
  import type { TodoItem, TodoList, TrackerState } from "../core/domain.ts";
11
21
  import type { EncodedState } from "../core/persistence.ts";
@@ -23,6 +33,15 @@ export const TOOL_ACTIONS = [
23
33
 
24
34
  export type TrackerToolAction = (typeof TOOL_ACTIONS)[number];
25
35
 
36
+ /**
37
+ * The action enum keeps an explicit `type: "string"`. Some providers reject an
38
+ * enum without a type, and TypeBox's `StringEnum` existed for that reason.
39
+ */
40
+ const actionSchema: Schema<TrackerToolAction> = {
41
+ type: "string",
42
+ ...oneOf(TOOL_ACTIONS, { description: "Tracker operation to perform" }),
43
+ };
44
+
26
45
  export const TRACKER_TOOL_NAME = "tracker";
27
46
  export const TRACKER_TOOL_LABEL = "Tracker";
28
47
 
@@ -30,96 +49,72 @@ export const TRACKER_TOOL_LABEL = "Tracker";
30
49
  * How an item is declared when it is created: bare text, or an object that
31
50
  * also carries the same-list items it must wait for.
32
51
  */
33
- const depsSchema = Type.Array(
34
- Type.String({ description: 'A dependency as listName:id, e.g. "Work:1".' }),
35
- { description: "Same-list items that must be done before this one." },
36
- );
52
+ const depsSchema = arr(str({ description: 'A dependency as listName:id, e.g. "Work:1".' }), {
53
+ description: "Same-list items that must be done before this one.",
54
+ });
37
55
 
38
56
  /** The item-title cap. Enforced only at the tool input, never persisted. */
39
57
  export const TITLE_LIMIT = 200;
40
58
 
41
- const titleField = Type.String({
59
+ const titleField = str({
42
60
  description:
43
61
  "Item title: one short action line (max 200 characters). Move longer detail into 'description'.",
44
62
  });
45
63
 
46
- const descriptionField = Type.String({
64
+ const descriptionField = str({
47
65
  description:
48
66
  "Optional: the longer explanation of the item. Put the essential detail in the first paragraph.",
49
67
  });
50
68
 
51
69
  /** A declared reference: a path (optionally narrowed to a symbol or span) or a decision topic. */
52
- const referenceSchema = Type.Union([
53
- Type.Object(
54
- {
55
- kind: Type.Literal("path"),
56
- path: Type.String({ description: "The file path this item must read next." }),
57
- symbol: Type.Optional(Type.String({ description: "A symbol inside the path." })),
58
- span: Type.Optional(Type.String({ description: 'A line span, e.g. "12-14" or "12".' })),
59
- },
60
- { additionalProperties: false },
61
- ),
62
- Type.Object(
63
- {
64
- kind: Type.Literal("decision"),
65
- topic: Type.String({ description: "The decision the item depends on." }),
66
- },
67
- { additionalProperties: false },
68
- ),
70
+ const referenceSchema = union([
71
+ obj({
72
+ kind: lit("path"),
73
+ path: str({ description: "The file path this item must read next." }),
74
+ symbol: opt(str({ description: "A symbol inside the path." })),
75
+ span: opt(str({ description: 'A line span, e.g. "12-14" or "12".' })),
76
+ }),
77
+ obj({
78
+ kind: lit("decision"),
79
+ topic: str({ description: "The decision the item depends on." }),
80
+ }),
69
81
  ]);
70
82
 
71
- const refsSchema = Type.Array(referenceSchema, {
83
+ const refsSchema = arr(referenceSchema, {
72
84
  description:
73
85
  "Optional: the files, symbols, regions, or decisions this item must read after the completed item finishes.",
74
86
  });
75
87
 
76
- const producesSchema = Type.Array(
77
- Type.Object(
78
- { path: Type.String({ description: "A document the item will write." }) },
79
- { additionalProperties: false },
80
- ),
81
- { description: "Optional: every document this item will write." },
82
- );
88
+ const producesSchema = arr(obj({ path: str({ description: "A document the item will write." }) }), {
89
+ description: "Optional: every document this item will write.",
90
+ });
83
91
 
84
92
  /** One new item, as the strict item object both add paths accept. */
85
- const ItemObjectSchema = Type.Object(
86
- {
87
- title: titleField,
88
- description: Type.Optional(descriptionField),
89
- refs: Type.Optional(refsSchema),
90
- produces: Type.Optional(producesSchema),
91
- deps: Type.Optional(depsSchema),
92
- },
93
- // Item objects are strict: a typo inside one fails here.
94
- { additionalProperties: false },
95
- );
93
+ const ItemObjectSchema = obj({
94
+ title: titleField,
95
+ description: opt(descriptionField),
96
+ refs: opt(refsSchema),
97
+ produces: opt(producesSchema),
98
+ deps: opt(depsSchema),
99
+ });
96
100
 
97
101
  /** One `update_items` patch: the item id plus the fields to replace. */
98
- const UpdatePatchSchema = Type.Object(
99
- {
100
- item_id: Type.String({
102
+ const UpdatePatchSchema = obj({
103
+ item_id: str({
104
+ description: 'The id of the item within list_id, as shown by the list action (e.g. "Work:2").',
105
+ }),
106
+ title: opt(str({ description: "Replacement title for the item." })),
107
+ description: opt(str({ description: "Replacement description for the item." })),
108
+ refs: opt(refsSchema),
109
+ produces: opt(producesSchema),
110
+ done: opt(bool({ description: "true marks the item done, false reopens it." })),
111
+ deps: opt(
112
+ arr(str({ description: 'A dependency as listName:id, e.g. "Work:1".' }), {
101
113
  description:
102
- 'The id of the item within list_id, as shown by the list action (e.g. "Work:2").',
114
+ "Replacement dependency set for this item. Pass [] to clear. Must not form a cycle.",
103
115
  }),
104
- title: Type.Optional(Type.String({ description: "Replacement title for the item." })),
105
- description: Type.Optional(
106
- Type.String({ description: "Replacement description for the item." }),
107
- ),
108
- refs: Type.Optional(refsSchema),
109
- produces: Type.Optional(producesSchema),
110
- done: Type.Optional(
111
- Type.Boolean({ description: "true marks the item done, false reopens it." }),
112
- ),
113
- deps: Type.Optional(
114
- Type.Array(Type.String({ description: 'A dependency as listName:id, e.g. "Work:1".' }), {
115
- description:
116
- "Replacement dependency set for this item. Pass [] to clear. Must not form a cycle.",
117
- }),
118
- ),
119
- },
120
- // Patch objects are strict: a typo inside an item update fails here.
121
- { additionalProperties: false },
122
- );
116
+ ),
117
+ });
123
118
 
124
119
  /** One item to add, as the tool accepts it. */
125
120
  export type TrackerItemSpec = Static<typeof ItemObjectSchema>;
@@ -135,61 +130,63 @@ export type TrackerUpdatePatch = Static<typeof UpdatePatchSchema>;
135
130
  * receives a precise, actionable error message instead of a generic schema
136
131
  * rejection.
137
132
  */
138
- export const TrackerToolParams = Type.Object({
139
- action: StringEnum(TOOL_ACTIONS, { description: "Tracker operation to perform" }),
140
- list_id: Type.Optional(
141
- Type.Number({
142
- description:
143
- "List id, as shown by the list action. Required for delete_list, add_items, and for update_items' items={...} batch; optional for set_active (omit to deselect).",
144
- }),
145
- ),
146
- item_id: Type.Optional(
147
- Type.String({
148
- description:
149
- 'Item id, as shown by the list action: listName:id (e.g. "Work:2"). Ids are permanent: removing an item does not change the other ids. Required for update_items (scalar form) and remove_item.',
150
- }),
151
- ),
152
- name: Type.Optional(
153
- Type.String({ description: "Name for the new list. Required for create_list." }),
154
- ),
155
- initial_items: Type.Optional(
156
- Type.Array(ItemObjectSchema, {
157
- description:
158
- "For create_list: initial items, each {title, description?, refs?, produces?, deps?}, added when the list is created.",
159
- minItems: 1,
160
- }),
161
- ),
162
- activate: Type.Optional(
163
- Type.Boolean({
164
- description:
165
- "For create_list: switch the active list to the new list (defaults to true; pass false to keep the current active list).",
166
- }),
167
- ),
168
- title: Type.Optional(
169
- Type.String({
170
- description: "For update_items: the replacement title for the item (max 200 characters).",
171
- }),
172
- ),
173
- description: Type.Optional(descriptionField),
174
- refs: Type.Optional(refsSchema),
175
- produces: Type.Optional(producesSchema),
176
- done: Type.Optional(
177
- Type.Boolean({ description: "For update_items: true marks the item done, false reopens it." }),
178
- ),
179
- deps: Type.Optional(
180
- Type.Array(Type.String({ description: 'A dependency as listName:id, e.g. "Work:1".' }), {
181
- description:
182
- "For update_items: the replacement dependency set (same-list items that must be done first). Pass [] to clear. Must not form a cycle.",
183
- }),
184
- ),
185
- items: Type.Optional(
186
- Type.Array(Type.Union([ItemObjectSchema, UpdatePatchSchema]), {
187
- description:
188
- "For add_items: the item objects to add, each {title, description?, refs?, produces?, deps?}. For update_items: the item patches, each with an item_id.",
189
- minItems: 1,
190
- }),
191
- ),
192
- });
133
+ export const TrackerToolParams = obj(
134
+ {
135
+ action: actionSchema,
136
+ list_id: opt(
137
+ num({
138
+ description:
139
+ "List id, as shown by the list action. Required for delete_list, add_items, and for update_items' items={...} batch; optional for set_active (omit to deselect).",
140
+ }),
141
+ ),
142
+ item_id: opt(
143
+ str({
144
+ description:
145
+ 'Item id, as shown by the list action: listName:id (e.g. "Work:2"). Ids are permanent: removing an item does not change the other ids. Required for update_items (scalar form) and remove_item.',
146
+ }),
147
+ ),
148
+ name: opt(str({ description: "Name for the new list. Required for create_list." })),
149
+ initial_items: opt(
150
+ arr(ItemObjectSchema, {
151
+ description:
152
+ "For create_list: initial items, each {title, description?, refs?, produces?, deps?}, added when the list is created.",
153
+ minItems: 1,
154
+ }),
155
+ ),
156
+ activate: opt(
157
+ bool({
158
+ description:
159
+ "For create_list: switch the active list to the new list (defaults to true; pass false to keep the current active list).",
160
+ }),
161
+ ),
162
+ title: opt(
163
+ str({
164
+ description: "For update_items: the replacement title for the item (max 200 characters).",
165
+ }),
166
+ ),
167
+ description: opt(descriptionField),
168
+ refs: opt(refsSchema),
169
+ produces: opt(producesSchema),
170
+ done: opt(
171
+ bool({ description: "For update_items: true marks the item done, false reopens it." }),
172
+ ),
173
+ deps: opt(
174
+ arr(str({ description: 'A dependency as listName:id, e.g. "Work:1".' }), {
175
+ description:
176
+ "For update_items: the replacement dependency set (same-list items that must be done first). Pass [] to clear. Must not form a cycle.",
177
+ }),
178
+ ),
179
+ items: opt(
180
+ arr(union([ItemObjectSchema, UpdatePatchSchema]), {
181
+ description:
182
+ "For add_items: the item objects to add, each {title, description?, refs?, produces?, deps?}. For update_items: the item patches, each with an item_id.",
183
+ minItems: 1,
184
+ }),
185
+ ),
186
+ },
187
+ // Unknown top-level fields are tolerated and reported by `validateTrackerCall`.
188
+ { open: true },
189
+ );
193
190
 
194
191
  export type TrackerToolParams = Static<typeof TrackerToolParams>;
195
192
 
@@ -484,15 +481,15 @@ export function validateTrackerCall(args: unknown): TrackerCallValidation {
484
481
  /** Structured result payload attached to tool calls, consumed by the renderers. */
485
482
  export interface TrackerToolDetails {
486
483
  action: TrackerToolAction;
487
- error?: string;
488
- listId?: number;
484
+ error?: string | undefined;
485
+ listId?: number | undefined;
489
486
  /** `listName:id`, as shown by the list action. */
490
- itemId?: string;
491
- list?: TodoList;
487
+ itemId?: string | undefined;
488
+ list?: TodoList | undefined;
492
489
  /** Items affected by an add_items/update_items call, in creation/patch order. */
493
- items?: TodoItem[];
490
+ items?: TodoItem[] | undefined;
494
491
  /** Full state snapshot, only for the `list` action (for rendering). */
495
- snapshot?: EncodedState;
492
+ snapshot?: EncodedState | undefined;
496
493
  }
497
494
 
498
495
  /**
@@ -586,7 +583,7 @@ export const doneMarkReminder = (
586
583
  patches: readonly { readonly done?: boolean }[],
587
584
  openRemaining?: number,
588
585
  ): string | null => {
589
- const doneCount = patches.filter((patch) => patch.done === true).length;
586
+ const doneCount = patches.filter((patch) => patch.done ?? false).length;
590
587
  // Only the terminal batch — two or more done marks with nothing left open —
591
588
  // is flagged. Batches that finish several items while other work remains are
592
589
  // normal mid-turn progress and stay quiet. When the post-call open count is