@principal-ai/principal-view-core 0.28.6 → 0.28.7
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/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +4 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +11 -2
- package/dist/node.js.map +1 -1
- package/dist/storage/topic-types.d.ts +189 -0
- package/dist/storage/topic-types.d.ts.map +1 -0
- package/dist/storage/topic-types.js +59 -0
- package/dist/storage/topic-types.js.map +1 -0
- package/dist/storage/topicStore.d.ts +130 -0
- package/dist/storage/topicStore.d.ts.map +1 -0
- package/dist/storage/topicStore.js +389 -0
- package/dist/storage/topicStore.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +13 -0
- package/src/node.ts +24 -0
- package/src/storage/topic-types.ts +220 -0
- package/src/storage/topicStore.test.ts +156 -0
- package/src/storage/topicStore.ts +481 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical topic contract — the shapes every consumer of `~/.principal/topics`
|
|
3
|
+
* agrees on. Browser-safe (no `node:*`); the file-per-topic store that reads and
|
|
4
|
+
* writes these lives in `./node` (see `topicStore.ts`).
|
|
5
|
+
*
|
|
6
|
+
* A **topic** is a curated bundle of trails on one subject. Two distinct named
|
|
7
|
+
* shapes, split by id-namespace / ownership, with an explicit translation
|
|
8
|
+
* between them — NOT one shared shape. They share most field names, but `id`
|
|
9
|
+
* and `trailIds` live in different namespaces (local vs server), so passing one
|
|
10
|
+
* where the other is expected corrupts the trail foreign-keys.
|
|
11
|
+
*
|
|
12
|
+
* - {@link DraftTopic} — locally-authored, what desktop persists + edits.
|
|
13
|
+
* - {@link PublishedTopic} — server-authoritative, what the server returns and
|
|
14
|
+
* mobile reads.
|
|
15
|
+
* - {@link publishedFromDraft} — the Draft -> Published projection (one-way;
|
|
16
|
+
* there is no reverse hydration).
|
|
17
|
+
*
|
|
18
|
+
* The shapes mirror the over-the-wire form used by the sharing API (web-ade) so
|
|
19
|
+
* a published topic and a locally stored one stay interchangeable on read. All
|
|
20
|
+
* timestamps are ISO 8601 strings because this contract crosses the desktop/web
|
|
21
|
+
* boundary.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Status of a topic — a small structured axis plus optional human nuance.
|
|
26
|
+
*
|
|
27
|
+
* `state` is the machine-meaningful signal (drives badge color, sorting, and
|
|
28
|
+
* filtering). `label` is free-form text shown in place of the default per-state
|
|
29
|
+
* label. `waitingOn` describes an external blocker and is meaningful when
|
|
30
|
+
* `state` is `waiting`.
|
|
31
|
+
*
|
|
32
|
+
* The structured shape is deliberate: it lets automations check and resolve a
|
|
33
|
+
* blocker without parsing prose — e.g. flip `waiting` -> `paused` once `until`
|
|
34
|
+
* passes, or once a referenced PR merges.
|
|
35
|
+
*/
|
|
36
|
+
export interface TopicStatus {
|
|
37
|
+
/**
|
|
38
|
+
* Structured lifecycle axis, ordered by a feature's "aliveness" from nascent
|
|
39
|
+
* to retired. Readers treat an absent OR unrecognized `state` as
|
|
40
|
+
* `new-thought`, so legacy topics and renamed values need no migration.
|
|
41
|
+
*/
|
|
42
|
+
state:
|
|
43
|
+
| 'new-thought'
|
|
44
|
+
| 'working'
|
|
45
|
+
| 'paused'
|
|
46
|
+
| 'waiting'
|
|
47
|
+
| 'done-for-now'
|
|
48
|
+
| 'deprecated'
|
|
49
|
+
| 'abandoned';
|
|
50
|
+
/** Free-form text shown in place of the default label for the state. */
|
|
51
|
+
label?: string;
|
|
52
|
+
/** External blocker description. Meaningful when `state` is `waiting`. */
|
|
53
|
+
waitingOn?: {
|
|
54
|
+
/** Human description of the blocker, e.g. "design sign-off". */
|
|
55
|
+
note?: string;
|
|
56
|
+
/** ISO 8601 — when the hold is expected to lift. */
|
|
57
|
+
until?: string;
|
|
58
|
+
/** Structured pointer to the thing being waited on, for automations. */
|
|
59
|
+
ref?: {
|
|
60
|
+
kind: 'url' | 'pr' | 'issue' | 'topic' | 'trail';
|
|
61
|
+
/** The address/id of the referenced thing. */
|
|
62
|
+
value: string;
|
|
63
|
+
/** Optional human label for display. */
|
|
64
|
+
title?: string;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* An image attached to a topic — primarily a screenshot dragged into the
|
|
71
|
+
* description. Bytes live on the topic (referenced from the description via an
|
|
72
|
+
* `asset://<id>` markdown link), not inlined into the description string. A
|
|
73
|
+
* render-time resolver swaps `asset://<id>` for `url` (preferred) or a data-URL
|
|
74
|
+
* built from `data`, keeping a local (data-only) and a published (url-bearing)
|
|
75
|
+
* topic interchangeable on read.
|
|
76
|
+
*
|
|
77
|
+
* Local-only on a {@link DraftTopic}: assets are uploaded/resolved at publish
|
|
78
|
+
* time, so a {@link PublishedTopic} carries the resolved `url`, not raw `data`.
|
|
79
|
+
*/
|
|
80
|
+
export interface TopicAsset {
|
|
81
|
+
/** Content hash — free dedup and the stable `asset://` target. */
|
|
82
|
+
id: string;
|
|
83
|
+
/** MIME type, e.g. "image/png". */
|
|
84
|
+
mime: string;
|
|
85
|
+
/** Base64-encoded bytes. Present locally and on publish. */
|
|
86
|
+
data?: string;
|
|
87
|
+
/** Resolvable URL, when the bytes have been offloaded (e.g. to S3). */
|
|
88
|
+
url?: string;
|
|
89
|
+
/** Markdown alt text. */
|
|
90
|
+
alt?: string;
|
|
91
|
+
/** Origin metadata, for future re-capture / open-live affordances. */
|
|
92
|
+
source?: { storyId?: string; storybookUrl?: string };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* GitHub identity of a topic's creator. Populated when a signed-in user
|
|
97
|
+
* authors a topic; left undefined for local-only topics created before
|
|
98
|
+
* sign-in. The server requires this on publish.
|
|
99
|
+
*/
|
|
100
|
+
export interface TopicCreator {
|
|
101
|
+
githubId: number;
|
|
102
|
+
githubLogin: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Locally-authored topic — the shape desktop persists to
|
|
107
|
+
* `~/.principal/topics/<id>.json` and edits.
|
|
108
|
+
*
|
|
109
|
+
* Local id namespace. `description` / `createdBy` are optional (a Draft may be
|
|
110
|
+
* authored before sign-in). Carries local-only {@link TopicAsset}s
|
|
111
|
+
* (`asset://<id>` screenshots, raw `data`). Has NO `visibility` — that's a
|
|
112
|
+
* published concept.
|
|
113
|
+
*
|
|
114
|
+
* A Draft keeps existing after publish (it can be in a "published state"); its
|
|
115
|
+
* link to the server identity lives in the desktop's `topics-sync.json`
|
|
116
|
+
* sidecar, not in this shape.
|
|
117
|
+
*/
|
|
118
|
+
export interface DraftTopic {
|
|
119
|
+
/** Unique identifier in the LOCAL id namespace. */
|
|
120
|
+
id: string;
|
|
121
|
+
/** Display title. */
|
|
122
|
+
title: string;
|
|
123
|
+
/** Optional markdown body. */
|
|
124
|
+
description?: string;
|
|
125
|
+
/** Ordered list of LOCAL trail ids — foreign keys into the local trail store. */
|
|
126
|
+
trailIds: string[];
|
|
127
|
+
/** ISO 8601. */
|
|
128
|
+
createdAt: string;
|
|
129
|
+
/** ISO 8601. */
|
|
130
|
+
updatedAt: string;
|
|
131
|
+
/** Creator identity; undefined for topics authored before sign-in. */
|
|
132
|
+
createdBy?: TopicCreator;
|
|
133
|
+
/** Optional workflow status. Absent reads as `new-thought`. */
|
|
134
|
+
status?: TopicStatus;
|
|
135
|
+
/** Local image attachments, referenced from the description via `asset://<id>`. */
|
|
136
|
+
assets?: TopicAsset[];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Server-authoritative topic — the shape the server returns and mobile reads.
|
|
141
|
+
*
|
|
142
|
+
* Remote id namespace. `description` / `createdBy` are required (publish is the
|
|
143
|
+
* validation gate). Carries `visibility`; assets have been resolved to `url`s.
|
|
144
|
+
*
|
|
145
|
+
* CAVEAT: "Published" means "has a server identity", NOT "public" — a
|
|
146
|
+
* PublishedTopic can be `visibility: 'private'`.
|
|
147
|
+
*/
|
|
148
|
+
export interface PublishedTopic {
|
|
149
|
+
/** Unique identifier in the REMOTE (server) id namespace. */
|
|
150
|
+
id: string;
|
|
151
|
+
/** Display title. */
|
|
152
|
+
title: string;
|
|
153
|
+
/** Markdown body — required on the server. */
|
|
154
|
+
description: string;
|
|
155
|
+
/** Ordered list of REMOTE trail ids. */
|
|
156
|
+
trailIds: string[];
|
|
157
|
+
/** ISO 8601. */
|
|
158
|
+
createdAt: string;
|
|
159
|
+
/** ISO 8601. */
|
|
160
|
+
updatedAt: string;
|
|
161
|
+
/** Creator identity — required on the server. */
|
|
162
|
+
createdBy: TopicCreator;
|
|
163
|
+
/** Optional workflow status, travels with the topic when published. */
|
|
164
|
+
status?: TopicStatus;
|
|
165
|
+
/** Server visibility. "private" is still a PublishedTopic. */
|
|
166
|
+
visibility: 'private' | 'public';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Inputs the {@link DraftTopic} cannot supply on its own — the namespace
|
|
171
|
+
* crossing and the fields publish must enforce. The caller resolves these
|
|
172
|
+
* before projecting:
|
|
173
|
+
* - `trailIds`: LOCAL trail ids remapped to REMOTE ids (web-ade references
|
|
174
|
+
* trails by their server id; see desktop's `resolveRemoteTrailIds`).
|
|
175
|
+
* - `createdBy`: required on the server; supplied here if the draft lacks it.
|
|
176
|
+
* - `visibility`: a published concept the draft never carries.
|
|
177
|
+
*/
|
|
178
|
+
export interface PublishProjection {
|
|
179
|
+
trailIds: string[];
|
|
180
|
+
createdBy: TopicCreator;
|
|
181
|
+
visibility: 'private' | 'public';
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Project a {@link DraftTopic} into a {@link PublishedTopic} — the one-way
|
|
186
|
+
* Draft -> Published translation. It does NOT perform any I/O (no id minting,
|
|
187
|
+
* no asset upload, no network): the caller resolves the namespace-crossing and
|
|
188
|
+
* required fields into {@link PublishProjection} first (remapping trail ids,
|
|
189
|
+
* uploading assets, choosing visibility), and this enforces the shape.
|
|
190
|
+
*
|
|
191
|
+
* One-way by design: there is no reverse `PublishedTopic -> DraftTopic`
|
|
192
|
+
* hydration. A published topic's edits write through to the server; the local
|
|
193
|
+
* Draft is reconciled from the server's response field-by-field, never by
|
|
194
|
+
* rebuilding a Draft from a Published shape.
|
|
195
|
+
*
|
|
196
|
+
* Throws if `description` is empty — publish is also the validation gate, and
|
|
197
|
+
* the server rejects a description-less topic.
|
|
198
|
+
*/
|
|
199
|
+
export function publishedFromDraft(
|
|
200
|
+
draft: DraftTopic,
|
|
201
|
+
projection: PublishProjection,
|
|
202
|
+
): PublishedTopic {
|
|
203
|
+
const description = (draft.description ?? '').trim();
|
|
204
|
+
if (description.length === 0) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`Topic ${draft.id} has no description; a description is required to publish.`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
id: draft.id,
|
|
211
|
+
title: draft.title,
|
|
212
|
+
description,
|
|
213
|
+
trailIds: projection.trailIds,
|
|
214
|
+
createdAt: draft.createdAt,
|
|
215
|
+
updatedAt: draft.updatedAt,
|
|
216
|
+
createdBy: projection.createdBy,
|
|
217
|
+
...(draft.status !== undefined ? { status: draft.status } : {}),
|
|
218
|
+
visibility: projection.visibility,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TopicStore — file-per-topic CRUD, index rebuild, and legacy-blob migration.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
6
|
+
import { mkdtempSync, rmSync, writeFileSync, existsSync, readFileSync } from 'fs';
|
|
7
|
+
import { tmpdir } from 'os';
|
|
8
|
+
import { join } from 'path';
|
|
9
|
+
import { TopicStore } from './topicStore';
|
|
10
|
+
import type { DraftTopic } from './topic-types';
|
|
11
|
+
|
|
12
|
+
let dir: string;
|
|
13
|
+
let topicsDir: string;
|
|
14
|
+
let legacyBlob: string;
|
|
15
|
+
|
|
16
|
+
const makeStore = () => new TopicStore(topicsDir, legacyBlob);
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
dir = mkdtempSync(join(tmpdir(), 'topicstore-'));
|
|
20
|
+
topicsDir = join(dir, 'topics');
|
|
21
|
+
legacyBlob = join(dir, 'topics.json');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
rmSync(dir, { recursive: true, force: true });
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('CRUD', () => {
|
|
29
|
+
test('create generates an id, persists a file, and reads back', async () => {
|
|
30
|
+
const store = makeStore();
|
|
31
|
+
const created = await store.createTopic({ title: 'Auth flow', trailIds: [] });
|
|
32
|
+
expect(created.id).toMatch(/^topic-/);
|
|
33
|
+
expect(existsSync(join(topicsDir, `${created.id}.json`))).toBe(true);
|
|
34
|
+
|
|
35
|
+
const got = await store.getTopic(created.id);
|
|
36
|
+
expect(got?.title).toBe('Auth flow');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('create honors an explicit id and rejects duplicates', async () => {
|
|
40
|
+
const store = makeStore();
|
|
41
|
+
await store.createTopic({ id: 'topic-x', title: 'A', trailIds: [] });
|
|
42
|
+
await expect(
|
|
43
|
+
store.createTopic({ id: 'topic-x', title: 'B', trailIds: [] }),
|
|
44
|
+
).rejects.toThrow(/already exists/);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('update changes fields but never id/createdAt/trailIds', async () => {
|
|
48
|
+
const store = makeStore();
|
|
49
|
+
const t = await store.createTopic({ id: 'topic-u', title: 'A', trailIds: ['t1'] });
|
|
50
|
+
const updated = await store.updateTopic('topic-u', { title: 'B', description: 'hi' });
|
|
51
|
+
expect(updated.title).toBe('B');
|
|
52
|
+
expect(updated.description).toBe('hi');
|
|
53
|
+
expect(updated.createdAt).toBe(t.createdAt);
|
|
54
|
+
expect(updated.trailIds).toEqual(['t1']);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('trail membership add/remove/reorder', async () => {
|
|
58
|
+
const store = makeStore();
|
|
59
|
+
await store.createTopic({ id: 'topic-m', title: 'M', trailIds: [] });
|
|
60
|
+
await store.addTrailToTopic('topic-m', 'a');
|
|
61
|
+
await store.addTrailToTopic('topic-m', 'b');
|
|
62
|
+
await store.addTrailToTopic('topic-m', 'a'); // dup no-op
|
|
63
|
+
expect((await store.getTopic('topic-m'))?.trailIds).toEqual(['a', 'b']);
|
|
64
|
+
|
|
65
|
+
await store.reorderTopicTrails('topic-m', ['b', 'a']);
|
|
66
|
+
expect((await store.getTopic('topic-m'))?.trailIds).toEqual(['b', 'a']);
|
|
67
|
+
|
|
68
|
+
await expect(
|
|
69
|
+
store.reorderTopicTrails('topic-m', ['b', 'c']),
|
|
70
|
+
).rejects.toThrow(/permutation/);
|
|
71
|
+
|
|
72
|
+
await store.removeTrailFromTopic('topic-m', 'b');
|
|
73
|
+
expect((await store.getTopic('topic-m'))?.trailIds).toEqual(['a']);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('delete removes the file and the index entry', async () => {
|
|
77
|
+
const store = makeStore();
|
|
78
|
+
await store.createTopic({ id: 'topic-d', title: 'D', trailIds: [] });
|
|
79
|
+
expect(await store.deleteTopic('topic-d')).toBe(true);
|
|
80
|
+
expect(existsSync(join(topicsDir, 'topic-d.json'))).toBe(false);
|
|
81
|
+
expect(await store.getTopic('topic-d')).toBeNull();
|
|
82
|
+
expect(await store.deleteTopic('topic-d')).toBe(false);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('no eviction cap — many topics all survive', async () => {
|
|
86
|
+
const store = makeStore();
|
|
87
|
+
for (let i = 0; i < 120; i++) {
|
|
88
|
+
await store.createTopic({ id: `topic-${i}`, title: `T${i}`, trailIds: [] });
|
|
89
|
+
}
|
|
90
|
+
expect((await store.getTopics()).length).toBe(120);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('index rebuild', () => {
|
|
95
|
+
test('a corrupt _index.json is rebuilt by scanning topic files', async () => {
|
|
96
|
+
const store = makeStore();
|
|
97
|
+
await store.createTopic({ id: 'topic-r', title: 'R', trailIds: ['z'] });
|
|
98
|
+
|
|
99
|
+
// Corrupt the manifest and force a fresh store to rebuild from disk.
|
|
100
|
+
writeFileSync(join(topicsDir, '_index.json'), 'not json', 'utf8');
|
|
101
|
+
const fresh = makeStore();
|
|
102
|
+
const list = await fresh.list();
|
|
103
|
+
expect(list.map((e) => e.id)).toContain('topic-r');
|
|
104
|
+
expect(list.find((e) => e.id === 'topic-r')?.trailCount).toBe(1);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('migration', () => {
|
|
109
|
+
const seedLegacy = (topics: Partial<DraftTopic>[]) => {
|
|
110
|
+
writeFileSync(
|
|
111
|
+
legacyBlob,
|
|
112
|
+
JSON.stringify({ version: '1.0.0', topics }, null, 2),
|
|
113
|
+
'utf8',
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
test('fans the blob out to file-per-topic and backs up the blob', async () => {
|
|
118
|
+
seedLegacy([
|
|
119
|
+
{ id: 'topic-1', title: 'One', trailIds: ['a'], createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-02T00:00:00.000Z' },
|
|
120
|
+
{ id: 'topic-2', title: 'Two', trailIds: [] },
|
|
121
|
+
]);
|
|
122
|
+
const store = makeStore();
|
|
123
|
+
const result = await store.migrateFromLegacyBlob();
|
|
124
|
+
|
|
125
|
+
expect(result.migrated).toBe(2);
|
|
126
|
+
expect(result.noLegacyBlob).toBe(false);
|
|
127
|
+
expect(result.backupPath).toBe(`${legacyBlob}.bak`);
|
|
128
|
+
expect(existsSync(legacyBlob)).toBe(false);
|
|
129
|
+
expect(existsSync(`${legacyBlob}.bak`)).toBe(true);
|
|
130
|
+
|
|
131
|
+
const one = await store.getTopic('topic-1');
|
|
132
|
+
expect(one?.title).toBe('One');
|
|
133
|
+
expect(one?.createdAt).toBe('2026-01-01T00:00:00.000Z');
|
|
134
|
+
expect(existsSync(join(topicsDir, 'topic-1.json'))).toBe(true);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('skips legacy entries with no id', async () => {
|
|
138
|
+
seedLegacy([{ title: 'no id' }, { id: 'topic-ok', title: 'ok', trailIds: [] }]);
|
|
139
|
+
const result = await makeStore().migrateFromLegacyBlob();
|
|
140
|
+
expect(result.migrated).toBe(1);
|
|
141
|
+
expect(result.skipped).toBe(1);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('reports noLegacyBlob when there is nothing to migrate', async () => {
|
|
145
|
+
const result = await makeStore().migrateFromLegacyBlob();
|
|
146
|
+
expect(result.noLegacyBlob).toBe(true);
|
|
147
|
+
expect(result.migrated).toBe(0);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test('migrated files are greppable pretty-printed JSON', async () => {
|
|
151
|
+
seedLegacy([{ id: 'topic-g', title: 'Grep me', trailIds: [] }]);
|
|
152
|
+
await makeStore().migrateFromLegacyBlob();
|
|
153
|
+
const raw = readFileSync(join(topicsDir, 'topic-g.json'), 'utf8');
|
|
154
|
+
expect(raw).toContain('\n "title": "Grep me"');
|
|
155
|
+
});
|
|
156
|
+
});
|