@ftrdotdev/pi-tracker 0.2.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/LICENSE +21 -0
- package/README.md +164 -0
- package/package.json +71 -0
- package/src/domain.test.ts +157 -0
- package/src/domain.ts +62 -0
- package/src/index.ts +455 -0
- package/src/persistence.test.ts +192 -0
- package/src/persistence.ts +47 -0
- package/src/store.test.ts +809 -0
- package/src/store.ts +517 -0
- package/src/tool-metadata.test.ts +213 -0
- package/src/tool-metadata.ts +302 -0
- package/src/ui.test.ts +204 -0
- package/src/ui.ts +468 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Felix Nordén
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# pi-tracker
|
|
2
|
+
|
|
3
|
+
pi-tracker is an extension for Pi. It manages todolists in your session.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Create and delete todolists.
|
|
8
|
+
- Add, update, and remove items in a todolist.
|
|
9
|
+
- Mark an item as complete or incomplete.
|
|
10
|
+
- Change the task text of an item.
|
|
11
|
+
- Persist todolists with the session.
|
|
12
|
+
- Show the active todolist in a widget above the editor.
|
|
13
|
+
|
|
14
|
+
## How it works
|
|
15
|
+
|
|
16
|
+
pi-tracker stores its state in an Effect service. It writes a snapshot to the
|
|
17
|
+
session after every change. It restores the state when the session resumes.
|
|
18
|
+
|
|
19
|
+
The snapshot does not enter the LLM context.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### The tracker tool
|
|
24
|
+
|
|
25
|
+
Ask Pi to manage your todolists. Pi calls the `tracker` tool. The tool
|
|
26
|
+
supports these actions:
|
|
27
|
+
|
|
28
|
+
| Action | Purpose | Parameters |
|
|
29
|
+
| ------------- | ----------------------------------------- | ---------------------------------------------- |
|
|
30
|
+
| `list` | Show all lists and items | — |
|
|
31
|
+
| `create_list` | Create a list (becomes active by default) | `name`, `initial_items?`, `activate?` |
|
|
32
|
+
| `delete_list` | Delete a list | `list_id` |
|
|
33
|
+
| `set_active` | Set or clear the active list | `list_id` (optional) |
|
|
34
|
+
| `add_item` | Add one or more items | `list_id`, `text` (string or array of strings) |
|
|
35
|
+
| `update_item` | Update one or more items | `item_id`/`text?`/`done?` or `items` |
|
|
36
|
+
| `remove_item` | Remove an item | `item_id` |
|
|
37
|
+
|
|
38
|
+
`create_list` accepts `initial_items` (an array of item texts) to create the
|
|
39
|
+
list with its first items in one call — list and items are created atomically.
|
|
40
|
+
`add_item` accepts a single `text` or an array of texts — several items are
|
|
41
|
+
added in one call.
|
|
42
|
+
|
|
43
|
+
Item ids are `listName:index` — the 1-based position of the item in its
|
|
44
|
+
list, as shown by the `list` action (e.g. `Work:2`). They are not stored on
|
|
45
|
+
items: an id is just the list name plus the array position, so ids are
|
|
46
|
+
unique across lists because list names are unique, and ids shift when items
|
|
47
|
+
are removed (`Work:3` becomes `Work:2`) — re-list before referencing items
|
|
48
|
+
after a removal. `update_item` accepts the scalar form (`item_id` with
|
|
49
|
+
optional `text`/`done`) or a batched `items` array
|
|
50
|
+
(`[{item_id, text?, done?}, ...]`); batches may span lists, since each
|
|
51
|
+
`item_id` names its own list. Creating a list makes it the active list (the
|
|
52
|
+
widget switches to it); pass `activate: false` to keep the current active
|
|
53
|
+
list.
|
|
54
|
+
|
|
55
|
+
The tool validates every call and returns an error that names exactly what to
|
|
56
|
+
fix: each action accepts only its own parameters, required fields are
|
|
57
|
+
enforced, and the two `update_item` forms never mix. Read the error and retry
|
|
58
|
+
with corrected parameters — not-found errors also list the available ids.
|
|
59
|
+
`update_item` also appends a reminder when one call marks two or more items
|
|
60
|
+
done at once: the working rhythm is to mark each item done in the same turn
|
|
61
|
+
it completes, never batch the marking at the end.
|
|
62
|
+
|
|
63
|
+
Example prompt:
|
|
64
|
+
|
|
65
|
+
> Create a list called "Work" and add "write plan" to it.
|
|
66
|
+
|
|
67
|
+
Call `set_active` without `list_id` to deselect. The widget hides when no
|
|
68
|
+
list is active.
|
|
69
|
+
|
|
70
|
+
### Working through a list
|
|
71
|
+
|
|
72
|
+
The intended rhythm: break multi-step work into items up front (one item per
|
|
73
|
+
deliverable), work through them one at a time, and mark each done as it
|
|
74
|
+
completes. The list — shown in the widget — is the live working state; the
|
|
75
|
+
agent should read it with `list` before starting and after finishing, and
|
|
76
|
+
update item text with `update_item` when scope changes.
|
|
77
|
+
|
|
78
|
+
Failed calls are recoverable: the tool's errors say what to fix, and
|
|
79
|
+
not-found errors name the available ids. The agent corrects the call and
|
|
80
|
+
retries in the same turn instead of repeating the same failing call.
|
|
81
|
+
|
|
82
|
+
### The /tracker command
|
|
83
|
+
|
|
84
|
+
Open `/tracker` to manage lists interactively.
|
|
85
|
+
|
|
86
|
+
While the cursor moves over the lists, the items pane below previews the
|
|
87
|
+
focused list. `enter` commits it as the active list and opens the items
|
|
88
|
+
pane for editing.
|
|
89
|
+
|
|
90
|
+
| Key | Action |
|
|
91
|
+
| ------- | -------------------------------------- |
|
|
92
|
+
| `tab` | Switch between the lists and the items |
|
|
93
|
+
| `↑` `↓` | Move the cursor |
|
|
94
|
+
| `enter` | Select a list and open its items |
|
|
95
|
+
| `space` | Toggle the active list |
|
|
96
|
+
| `n` | Create a list |
|
|
97
|
+
| `d` | Delete a list |
|
|
98
|
+
| `a` | Add an item |
|
|
99
|
+
| `x` | Toggle an item complete or incomplete |
|
|
100
|
+
| `e` | Edit the item text |
|
|
101
|
+
| `r` | Remove an item |
|
|
102
|
+
| `esc` | Close the view |
|
|
103
|
+
|
|
104
|
+
### The widget
|
|
105
|
+
|
|
106
|
+
The widget shows the active list above the editor. It appears when a list is
|
|
107
|
+
active. It hides when no list is active.
|
|
108
|
+
|
|
109
|
+
The widget has a rounded border. The border uses the theme's `border` color.
|
|
110
|
+
|
|
111
|
+
## Persistence
|
|
112
|
+
|
|
113
|
+
The state lives in the session file. Pi writes a snapshot after every change.
|
|
114
|
+
The state restores on resume, fork, and tree navigation.
|
|
115
|
+
|
|
116
|
+
## Installation
|
|
117
|
+
|
|
118
|
+
Install from npm:
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
pi install npm:@ftrdotdev/pi-tracker
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
From git or a local checkout:
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
pi install git:github.com/felixnorden/pi-mono
|
|
128
|
+
pi install ./path/to/pi-mono/packages/tracker
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
To try the package without installing it, use `-e` (temporary, current run
|
|
132
|
+
only):
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
pi -e npm:@ftrdotdev/pi-tracker
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Registration lives in `package.json` under the `pi` field:
|
|
139
|
+
|
|
140
|
+
```json
|
|
141
|
+
"pi": {
|
|
142
|
+
"extensions": ["./src/index.ts"]
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Development
|
|
147
|
+
|
|
148
|
+
| Command | Purpose |
|
|
149
|
+
| ------------------- | -------------------------------------------- |
|
|
150
|
+
| `bun test` | Run the test suite (vitest + @effect/vitest) |
|
|
151
|
+
| `bun test:watch` | Run the test suite in watch mode |
|
|
152
|
+
| `bunx tsc --noEmit` | Type check |
|
|
153
|
+
| `bun lint` | Lint with oxlint |
|
|
154
|
+
|
|
155
|
+
## Project structure
|
|
156
|
+
|
|
157
|
+
| File | Purpose |
|
|
158
|
+
| -------------------- | ------------------------------------------------------------ |
|
|
159
|
+
| `src/domain.ts` | Schema domain model (`TodoItem`, `TodoList`, `TrackerState`) |
|
|
160
|
+
| `src/store.ts` | `TrackerStore` service with `Effect.Ref` state |
|
|
161
|
+
| `src/persistence.ts` | `TrackerPersistence` service (save and restore snapshots) |
|
|
162
|
+
| `src/ui.ts` | Widget pane and interactive `/tracker` component |
|
|
163
|
+
| `src/index.ts` | Pi bridge: tool, command, session hooks, widget refresh |
|
|
164
|
+
| `src/*.test.ts` | Test suites |
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ftrdotdev/pi-tracker",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Session-persistent todolist extension for Pi",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"effect",
|
|
7
|
+
"extension",
|
|
8
|
+
"pi",
|
|
9
|
+
"pi-coding-agent",
|
|
10
|
+
"pi-package",
|
|
11
|
+
"task",
|
|
12
|
+
"todo",
|
|
13
|
+
"todolist",
|
|
14
|
+
"tracker"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/felixnorden/pi-mono#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/felixnorden/pi-mono/issues"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Felix Nordén <felixnorden@gmail.com>",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/felixnorden/pi-mono.git",
|
|
25
|
+
"directory": "packages/tracker"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"src",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"prepare": "bun effect-tsgo patch --typescript --oxlint",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"test:watch": "vitest",
|
|
40
|
+
"lint": "oxlint && oxlint --type-aware -A all -D typescript/unbound-method",
|
|
41
|
+
"lint:fix": "oxlint --fix",
|
|
42
|
+
"fmt": "oxfmt",
|
|
43
|
+
"typecheck": "tsc --noEmit"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@effect/platform-node": "4.0.0-rc.108",
|
|
47
|
+
"@ftrdotdev/pi-tui": "^0.2.0",
|
|
48
|
+
"effect": "4.0.0-rc.108"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@effect/tsgo": "0.36.4",
|
|
52
|
+
"@effect/vitest": "4.0.0-rc.108",
|
|
53
|
+
"@types/bun": "latest",
|
|
54
|
+
"oxfmt": "^0.63.0",
|
|
55
|
+
"oxlint": "1.77.0",
|
|
56
|
+
"oxlint-tsgolint": "7.0.2001",
|
|
57
|
+
"typescript": "7.0.2",
|
|
58
|
+
"vitest": "^4.1.10"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"@earendil-works/pi-ai": "*",
|
|
62
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
63
|
+
"@earendil-works/pi-tui": "*",
|
|
64
|
+
"typebox": "*"
|
|
65
|
+
},
|
|
66
|
+
"pi": {
|
|
67
|
+
"extensions": [
|
|
68
|
+
"./src/index.ts"
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { Effect, Option, Result } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
decodeStateEffect,
|
|
5
|
+
emptyState,
|
|
6
|
+
encodeState,
|
|
7
|
+
TodoItem,
|
|
8
|
+
TodoList,
|
|
9
|
+
TrackerState,
|
|
10
|
+
} from "./domain.ts";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Round-trip helpers compare *encoded* forms (plain JSON) instead of decoded
|
|
14
|
+
* values, because `Schema.Class` decode produces class instances while the
|
|
15
|
+
* fixtures are plain object literals — encoding both sides normalizes them.
|
|
16
|
+
*/
|
|
17
|
+
const roundTripsTo = (state: TrackerState, snapshot: unknown) =>
|
|
18
|
+
assert.deepStrictEqual(encodeState(decodeOrThrow(snapshot)), encodeState(state));
|
|
19
|
+
|
|
20
|
+
const decodeOrThrow = (snapshot: unknown): TrackerState =>
|
|
21
|
+
Effect.runSync(decodeStateEffect(snapshot));
|
|
22
|
+
|
|
23
|
+
/** A realistic two-list state used by several tests. */
|
|
24
|
+
const sampleState = (): TrackerState =>
|
|
25
|
+
new TrackerState({
|
|
26
|
+
lists: [
|
|
27
|
+
new TodoList({
|
|
28
|
+
id: 1,
|
|
29
|
+
name: "Work",
|
|
30
|
+
items: [
|
|
31
|
+
new TodoItem({ text: "write plan", done: true }),
|
|
32
|
+
new TodoItem({ text: "implement tracker", done: false }),
|
|
33
|
+
],
|
|
34
|
+
}),
|
|
35
|
+
new TodoList({
|
|
36
|
+
id: 2,
|
|
37
|
+
name: "Home",
|
|
38
|
+
items: [new TodoItem({ text: "water plants", done: false })],
|
|
39
|
+
}),
|
|
40
|
+
],
|
|
41
|
+
activeListId: 1,
|
|
42
|
+
nextListId: 3,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("emptyState produces the canonical empty snapshot", () => {
|
|
46
|
+
assert.deepStrictEqual(encodeState(emptyState()), {
|
|
47
|
+
lists: [],
|
|
48
|
+
activeListId: null,
|
|
49
|
+
nextListId: 1,
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("empty state round-trips through encode/decode", () => {
|
|
54
|
+
roundTripsTo(emptyState(), encodeState(emptyState()));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("a realistic state round-trips, preserving order", () => {
|
|
58
|
+
const state = sampleState();
|
|
59
|
+
roundTripsTo(state, encodeState(state));
|
|
60
|
+
const decoded = decodeOrThrow(encodeState(state));
|
|
61
|
+
assert.deepStrictEqual(decoded.lists, state.lists);
|
|
62
|
+
assert.strictEqual(decoded.activeListId, 1);
|
|
63
|
+
assert.strictEqual(decoded.nextListId, 3);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("decoded values are real class instances", () => {
|
|
67
|
+
const decoded = decodeOrThrow(encodeState(sampleState()));
|
|
68
|
+
const firstItem = decoded.lists[0]?.items[0];
|
|
69
|
+
assert(firstItem instanceof TodoItem);
|
|
70
|
+
assert(decoded.lists[0] instanceof TodoList);
|
|
71
|
+
assert(decoded instanceof TrackerState);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("constructors validate their input", () => {
|
|
75
|
+
// Invalid types / missing fields throw at construction time. (The invalid
|
|
76
|
+
// inputs are cast because the constructor signature is already typed.)
|
|
77
|
+
assert.throws(() => new TodoItem({ text: 42, done: false } as never));
|
|
78
|
+
assert.throws(() => new TodoItem({ text: "x" } as never));
|
|
79
|
+
assert.throws(() => new TrackerState({} as never));
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("encoded snapshots are JSON-serializable", () => {
|
|
83
|
+
const state = sampleState();
|
|
84
|
+
const encoded = encodeState(state);
|
|
85
|
+
assert.deepStrictEqual(JSON.parse(JSON.stringify(encoded)), encoded);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("activeListId: null round-trips as null (not undefined)", () => {
|
|
89
|
+
const decoded = decodeOrThrow(encodeState(emptyState()));
|
|
90
|
+
assert(decoded.activeListId === null);
|
|
91
|
+
assert("activeListId" in encodeState(decoded));
|
|
92
|
+
assert.strictEqual(encodeState(decoded).activeListId, null);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("unknown extra keys are ignored (forward compatibility)", () => {
|
|
96
|
+
const state = sampleState();
|
|
97
|
+
const encoded = encodeState(state);
|
|
98
|
+
const snapshot = { ...encoded, futureKey: { anything: 1 } };
|
|
99
|
+
// An extra key inside a valid list object is tolerated too.
|
|
100
|
+
const listWithExtra = { ...encoded.lists[0]!, extra: true };
|
|
101
|
+
const nested = { ...encoded, lists: [listWithExtra, ...encoded.lists.slice(1)] };
|
|
102
|
+
roundTripsTo(state, snapshot);
|
|
103
|
+
roundTripsTo(state, nested);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("rejects a snapshot with the wrong field type", () => {
|
|
107
|
+
const result = Effect.runSync(Effect.result(decodeStateEffect({ lists: 42 })));
|
|
108
|
+
assert(Result.isFailure(result));
|
|
109
|
+
const failure = Option.getOrThrow(Result.getFailure(result));
|
|
110
|
+
assert.strictEqual(failure._tag, "SchemaError");
|
|
111
|
+
assert.match(failure.message, /lists/i);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("rejects a snapshot with an invalid item field", () => {
|
|
115
|
+
const bad = {
|
|
116
|
+
lists: [{ id: 1, name: "Work", items: [{ text: "x", done: "yes" }] }],
|
|
117
|
+
activeListId: null,
|
|
118
|
+
nextListId: 2,
|
|
119
|
+
};
|
|
120
|
+
const result = Effect.runSync(Effect.result(decodeStateEffect(bad)));
|
|
121
|
+
assert(Result.isFailure(result));
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("ignores legacy item id fields (ids are positions, not stored)", () => {
|
|
125
|
+
// Snapshots from both previous formats (numeric ids and listName:index
|
|
126
|
+
// strings) decode cleanly; the stored id is ignored.
|
|
127
|
+
const legacy = {
|
|
128
|
+
lists: [{ id: 1, name: "Work", items: [{ id: "Work:1", text: "x", done: false }] }],
|
|
129
|
+
activeListId: null,
|
|
130
|
+
nextListId: 2,
|
|
131
|
+
nextItemId: 9,
|
|
132
|
+
};
|
|
133
|
+
const decoded = decodeOrThrow(legacy);
|
|
134
|
+
assert.deepStrictEqual(decoded.lists[0]?.items, [new TodoItem({ text: "x", done: false })]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("enforces Int list ids (fractional ids are rejected)", () => {
|
|
138
|
+
const bad = {
|
|
139
|
+
lists: [{ id: 1.5, name: "Work", items: [] }],
|
|
140
|
+
activeListId: null,
|
|
141
|
+
nextListId: 2,
|
|
142
|
+
};
|
|
143
|
+
const result = Effect.runSync(Effect.result(decodeStateEffect(bad)));
|
|
144
|
+
assert(Result.isFailure(result));
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("rejects a snapshot missing required fields", () => {
|
|
148
|
+
const result = Effect.runSync(Effect.result(decodeStateEffect({ lists: [] })));
|
|
149
|
+
assert(Result.isFailure(result));
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("rejects non-object input", () => {
|
|
153
|
+
for (const bad of ["not json", 42, null, undefined, [1, 2]]) {
|
|
154
|
+
const result = Effect.runSync(Effect.result(decodeStateEffect(bad)));
|
|
155
|
+
assert(Result.isFailure(result), `expected ${JSON.stringify(bad)} to fail`);
|
|
156
|
+
}
|
|
157
|
+
});
|
package/src/domain.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Domain model for the tracker extension.
|
|
5
|
+
*
|
|
6
|
+
* Schema classes double as their own TypeScript types (no separate
|
|
7
|
+
* interfaces). The on-disk / session snapshot is the full `TrackerState`,
|
|
8
|
+
* so decode does not need to apply defaults: a snapshot is either complete
|
|
9
|
+
* and valid, or rejected with a `SchemaError`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export class TodoItem extends Schema.Class<TodoItem>("tracker/TodoItem")({
|
|
13
|
+
text: Schema.String,
|
|
14
|
+
done: Schema.Boolean,
|
|
15
|
+
}) {}
|
|
16
|
+
|
|
17
|
+
export class TodoList extends Schema.Class<TodoList>("tracker/TodoList")({
|
|
18
|
+
id: Schema.Int,
|
|
19
|
+
name: Schema.String,
|
|
20
|
+
items: Schema.Array(TodoItem),
|
|
21
|
+
}) {}
|
|
22
|
+
|
|
23
|
+
export class TrackerState extends Schema.Class<TrackerState>("tracker/TrackerState")({
|
|
24
|
+
lists: Schema.Array(TodoList),
|
|
25
|
+
// null = no active list (avoids Option in the serialized snapshot)
|
|
26
|
+
activeListId: Schema.NullOr(Schema.Int),
|
|
27
|
+
// Strictly-increasing counter ⇒ stable list ids, never reused after deletes.
|
|
28
|
+
nextListId: Schema.Int,
|
|
29
|
+
}) {}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Fresh state: no lists, list counter starting at 1.
|
|
33
|
+
*
|
|
34
|
+
* Item ids are not stored: `listName:index` is a reference syntax where
|
|
35
|
+
* `index` is the 1-based position in the list's `items` array. State is
|
|
36
|
+
* always carried as real class instances: `Schema.Class` encode requires
|
|
37
|
+
* instances (its Declaration checks the class marker), and the constructors
|
|
38
|
+
* validate their input (`new TodoItem({ text: 42, ... })` throws).
|
|
39
|
+
*/
|
|
40
|
+
export const emptyState = (): TrackerState =>
|
|
41
|
+
new TrackerState({ lists: [], activeListId: null, nextListId: 1 });
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Encode a `TrackerState` into its plain JSON shape (the session snapshot).
|
|
45
|
+
* Guarantees the snapshot is JSON-serializable and schema-valid.
|
|
46
|
+
*/
|
|
47
|
+
export const encodeState = Schema.encodeSync(TrackerState);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Encode a `TrackerState` into its plain JSON shape, as an Effect.
|
|
51
|
+
* Fails with `SchemaError` in the error channel instead of throwing.
|
|
52
|
+
*/
|
|
53
|
+
export const encodeStateEffect = Schema.encodeEffect(TrackerState);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Decode an untrusted snapshot (e.g. `data` from a session custom entry)
|
|
57
|
+
* into a validated `TrackerState`. Fails with `SchemaError` on malformed
|
|
58
|
+
* input; unknown extra keys are ignored for forward compatibility.
|
|
59
|
+
*/
|
|
60
|
+
export const decodeStateEffect = Schema.decodeUnknownEffect(TrackerState, {
|
|
61
|
+
onExcessProperty: "ignore",
|
|
62
|
+
});
|