@dreamlake/dreamlake-cli 0.89.0 → 0.90.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/dist/api/client.d.ts +2 -1
- package/dist/api/client.js +1 -1
- package/dist/api/index.d.ts +100 -68
- package/dist/api/index.js +2 -0
- package/dist/api/note-files.d.ts +57 -30
- package/dist/api/note-files.js +100 -58
- package/dist/api/note-linger.d.ts +83 -0
- package/dist/api/note-linger.js +270 -0
- package/dist/api/note-presence.d.ts +82 -0
- package/dist/api/note-presence.js +72 -0
- package/dist/api/notes.d.ts +57 -32
- package/dist/api/notes.js +30 -2
- package/dist/cli/notes/api-transport.d.ts +5 -0
- package/dist/cli/notes/api-transport.js +20 -0
- package/dist/cli/notes/files-commands.d.ts +2 -25
- package/dist/cli/notes/files-commands.js +16 -22
- package/dist/cli/notes/index.js +5 -8
- package/dist/cli/notes/linger.d.ts +3 -58
- package/dist/cli/notes/linger.js +56 -299
- package/dist/cli/notes/select-command.js +20 -41
- package/dist/cli/version.d.ts +1 -1
- package/dist/cli/version.js +1 -1
- package/package.json +9 -9
package/dist/cli/notes/linger.js
CHANGED
|
@@ -1,79 +1,17 @@
|
|
|
1
|
-
/** Foreground
|
|
2
|
-
import { once } from
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export const LINGER_BATCH_MS = 2000;
|
|
11
|
-
/** Durations are bounded to avoid accidental busy loops or effectively silent sessions. */
|
|
12
|
-
export function lingerDuration(value, option) {
|
|
13
|
-
if (value === undefined)
|
|
14
|
-
return LINGER_BATCH_MS;
|
|
15
|
-
const match = /^(\d+(?:\.\d+)?)(ms|s|m)$/.exec(value);
|
|
16
|
-
const ms = match ? Number(match[1]) * ({ ms: 1, s: 1000, m: 60000 }[match[2]] ?? 0) : NaN;
|
|
17
|
-
if (!Number.isFinite(ms) || ms < 250 || ms > 300000)
|
|
18
|
-
throw new Error(`${option} requires a duration from 250ms to 5m (for example 500ms or 2s)`);
|
|
19
|
-
return ms;
|
|
20
|
-
}
|
|
21
|
-
const HEARTBEAT_MS = 20_000;
|
|
22
|
-
const runtime = {
|
|
23
|
-
now: Date.now,
|
|
24
|
-
wait: async (ms, signal) => { await delay(ms, undefined, { signal }); },
|
|
25
|
-
};
|
|
26
|
-
const quote = (value) => JSON.stringify(value);
|
|
27
|
-
const label = (value) => quote(value).slice(1, -1);
|
|
28
|
-
const participant = (p) => p.user.kind === 'agent'
|
|
29
|
-
? `${label(p.user.name)} (agent)`
|
|
30
|
-
: `@${label(p.user.id)}`;
|
|
1
|
+
/** Foreground presentation for the shared CLI/TypeScript collaboration engine. */
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
import { fail } from '../helpers.js';
|
|
4
|
+
import { ctx, report, resolveNote, seg } from './index.js';
|
|
5
|
+
import { watchNote } from '../../api/note-linger.js';
|
|
6
|
+
import { noteTransport } from './api-transport.js';
|
|
7
|
+
export { LINGER_BATCH_MS, lingerDuration, LingerBatch } from '../../api/note-linger.js';
|
|
8
|
+
const label = (value) => JSON.stringify(value).slice(1, -1);
|
|
9
|
+
const participant = (p) => p.user.kind === 'agent' ? `${label(p.user.name)} (agent)` : `@${label(p.user.id)}`;
|
|
31
10
|
function selectionText(p) {
|
|
32
11
|
const s = p.selection;
|
|
33
|
-
|
|
34
|
-
return '';
|
|
35
|
-
return ` selected ${quote(s.text)}${s.truncated ? ' [truncated]' : ''}`;
|
|
36
|
-
}
|
|
37
|
-
/** Stable activity fingerprints suppress renewed read/typing observations each poll. */
|
|
38
|
-
export class LingerBatch {
|
|
39
|
-
self;
|
|
40
|
-
participants = new Map();
|
|
41
|
-
activity = new Map();
|
|
42
|
-
selections = new Map();
|
|
43
|
-
constructor(initial, self) {
|
|
44
|
-
this.self = self;
|
|
45
|
-
this.diff(initial);
|
|
46
|
-
}
|
|
47
|
-
others(rows) { return rows.filter(p => p.user.id !== this.self); }
|
|
48
|
-
diff(next) {
|
|
49
|
-
const current = new Map(this.others(next.participants).map(p => [p.client, p]));
|
|
50
|
-
const joined = [...current].filter(([id]) => !this.participants.has(id)).map(([, p]) => p);
|
|
51
|
-
const left = [...this.participants].filter(([id]) => !current.has(id)).map(([, p]) => p);
|
|
52
|
-
const selections = [...current.values()].flatMap(p => {
|
|
53
|
-
const fingerprint = JSON.stringify(p.selection ?? null);
|
|
54
|
-
const changed = this.selections.get(p.client) !== fingerprint;
|
|
55
|
-
this.selections.set(p.client, fingerprint);
|
|
56
|
-
return changed && (p.selection || this.participants.has(p.client)) ? [p] : [];
|
|
57
|
-
});
|
|
58
|
-
for (const p of left) {
|
|
59
|
-
this.selections.delete(p.client);
|
|
60
|
-
selections.push({ ...p, selection: null });
|
|
61
|
-
}
|
|
62
|
-
this.participants = current;
|
|
63
|
-
const seen = new Map();
|
|
64
|
-
const activities = next.activities.filter(a => {
|
|
65
|
-
if (`agent:${a.agent.owner}:${a.agent.id}` === this.self)
|
|
66
|
-
return false;
|
|
67
|
-
const key = `${a.agent.owner}:${a.agent.id}:${a.kind}`;
|
|
68
|
-
const fingerprint = JSON.stringify([a.hash, a.scope, a.ranges]);
|
|
69
|
-
seen.set(key, fingerprint);
|
|
70
|
-
return this.activity.get(key) !== fingerprint;
|
|
71
|
-
});
|
|
72
|
-
this.activity = seen;
|
|
73
|
-
return { joined, left, activities, selections };
|
|
74
|
-
}
|
|
12
|
+
return !s || s.status !== 'resolved' || s.start === s.end ? '' : ` selected ${JSON.stringify(s.text)}${s.truncated ? ' [truncated]' : ''}`;
|
|
75
13
|
}
|
|
76
|
-
export async function runNotesLinger(ref, opts, clock
|
|
14
|
+
export async function runNotesLinger(ref, opts, clock) {
|
|
77
15
|
const target = ref ?? opts.note;
|
|
78
16
|
if (!target || (ref && opts.note && ref !== opts.note)) {
|
|
79
17
|
fail('give one note id, slug or name');
|
|
@@ -83,45 +21,19 @@ export async function runNotesLinger(ref, opts, clock = runtime) {
|
|
|
83
21
|
fail('set DREAMLAKE_AGENT_ID to a stable task-session ID');
|
|
84
22
|
return 1;
|
|
85
23
|
}
|
|
86
|
-
if (opts.at !== undefined || opts.toc || opts.tag !== undefined || opts.legacy || opts.view && opts.view !== 'source' || opts.since !== undefined ||
|
|
87
|
-
opts.section !== undefined || opts.startLine !== undefined || opts.endLine !== undefined || opts.numbered) {
|
|
24
|
+
if (opts.at !== undefined || opts.toc || opts.tag !== undefined || opts.legacy || opts.view && opts.view !== 'source' || opts.since !== undefined || opts.section !== undefined || opts.startLine !== undefined || opts.endLine !== undefined || opts.numbered) {
|
|
88
25
|
fail('--linger requires a complete source read; omit --at, --toc, --tag, --legacy, --view html/markdown, --since, section and line options');
|
|
89
26
|
return 1;
|
|
90
27
|
}
|
|
91
|
-
const
|
|
92
|
-
if (!['diff', 'inline-dff'].includes(format)) {
|
|
93
|
-
fail('--format must be diff or inline-dff');
|
|
94
|
-
return 1;
|
|
95
|
-
}
|
|
96
|
-
if (opts.ifMatch !== undefined && (!opts.ifMatch.trim() || opts.ifMatch === '*' || /[\r\n]/.test(opts.ifMatch))) {
|
|
97
|
-
fail('--if-match requires an exact saved revision');
|
|
98
|
-
return 1;
|
|
99
|
-
}
|
|
100
|
-
let debounce, throttle, intent;
|
|
101
|
-
try {
|
|
102
|
-
debounce = lingerDuration(opts.debounce, '--debounce');
|
|
103
|
-
throttle = lingerDuration(opts.throttle, '--throttle');
|
|
104
|
-
intent = presenceIntent(opts.intent);
|
|
105
|
-
}
|
|
106
|
-
catch (error) {
|
|
107
|
-
return report(error);
|
|
108
|
-
}
|
|
109
|
-
const abort = new AbortController();
|
|
110
|
-
const stop = () => abort.abort();
|
|
111
|
-
const writeOutput = async (value) => {
|
|
112
|
-
if (!process.stdout.write(value))
|
|
113
|
-
await once(process.stdout, 'drain', {
|
|
114
|
-
signal: AbortSignal.any([abort.signal, AbortSignal.timeout(30000)]),
|
|
115
|
-
});
|
|
116
|
-
};
|
|
28
|
+
const abort = new AbortController(), stop = () => abort.abort();
|
|
117
29
|
process.on('SIGINT', stop);
|
|
118
30
|
process.on('SIGTERM', stop);
|
|
119
|
-
clock
|
|
120
|
-
if (clock
|
|
31
|
+
clock?.signal?.addEventListener('abort', stop, { once: true });
|
|
32
|
+
if (clock?.signal?.aborted)
|
|
121
33
|
stop();
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
34
|
+
const write = async (value) => { if (!process.stdout.write(value))
|
|
35
|
+
await once(process.stdout, 'drain', { signal: AbortSignal.any([abort.signal, AbortSignal.timeout(30000)]) }); };
|
|
36
|
+
const selections = new Map();
|
|
125
37
|
try {
|
|
126
38
|
const c = await ctx(opts);
|
|
127
39
|
if (!c)
|
|
@@ -129,211 +41,56 @@ export async function runNotesLinger(ref, opts, clock = runtime) {
|
|
|
129
41
|
const id = await resolveNote(c, target);
|
|
130
42
|
if (!id)
|
|
131
43
|
return 1;
|
|
132
|
-
if (abort.signal.aborted)
|
|
133
|
-
return 0;
|
|
134
44
|
const path = `/namespaces/${seg(c.ns)}/notes/${seg(id)}`;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
leave = () => requestJson(c.remote, path + '/presence', {
|
|
140
|
-
method: 'POST', token: c.token, json: { action: 'leave' }, timeoutMs: 5000, redirect: 'error',
|
|
141
|
-
});
|
|
142
|
-
// Intent rides the join only; heartbeats omit the field, which preserves it.
|
|
143
|
-
const joined = await request('/presence', { method: 'POST', json: { action: 'join', ...(intent ? { intent } : {}) } });
|
|
144
|
-
if (typeof joined?.state?.user?.id !== 'string')
|
|
145
|
-
throw new Error('server did not return an agent presence session');
|
|
146
|
-
const self = joined.state.user.id;
|
|
147
|
-
let heartbeat = clock.now();
|
|
148
|
-
let latest, sequence = 0, handled = -1, streamError;
|
|
149
|
-
let wake = new AbortController();
|
|
150
|
-
const wait = async (ms) => {
|
|
151
|
-
wake = new AbortController();
|
|
152
|
-
try {
|
|
153
|
-
await clock.wait(ms, AbortSignal.any([abort.signal, wake.signal]));
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
if (!wake.signal.aborted && !abort.signal.aborted)
|
|
157
|
-
throw error;
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
streaming = (clock.events ?? readNoteEvents)(c.remote.replace(/\/+$/, '') + path + '/events', c.token, id, abort.signal, event => {
|
|
161
|
-
latest = event;
|
|
162
|
-
sequence++;
|
|
163
|
-
wake.abort();
|
|
164
|
-
}).then(() => {
|
|
165
|
-
if (!abort.signal.aborted)
|
|
166
|
-
streamError = new Error('Notes event stream disconnected');
|
|
167
|
-
wake.abort();
|
|
168
|
-
}, error => { streamError = error; wake.abort(); });
|
|
169
|
-
while (!latest && !streamError && !abort.signal.aborted)
|
|
170
|
-
await wait(45000);
|
|
171
|
-
if (abort.signal.aborted)
|
|
172
|
-
return 0;
|
|
173
|
-
if (streamError)
|
|
174
|
-
throw streamError;
|
|
175
|
-
const readBody = async (previous) => {
|
|
176
|
-
const query = new URLSearchParams({ contract: 'v2' });
|
|
177
|
-
if (previous) {
|
|
178
|
-
query.set('since', previous.hash);
|
|
179
|
-
query.set('format', format);
|
|
180
|
-
}
|
|
181
|
-
const body = await request('/body?' + query, { headers: !previous && opts.ifMatch ? { 'If-Match': opts.ifMatch } : {} });
|
|
182
|
-
tokens(body, id);
|
|
183
|
-
if (!previous) {
|
|
184
|
-
if (typeof body.content !== 'string' || 'sha256:' + createHash('sha256').update(body.content).digest('hex') !== body.hash)
|
|
185
|
-
throw new Error('source does not match the returned hash');
|
|
186
|
-
if (opts.ifMatch && body.revision !== opts.ifMatch)
|
|
187
|
-
throw new Error('note changed since the acknowledged revision');
|
|
188
|
-
}
|
|
189
|
-
else if (body.base !== previous.hash || body.format !== format || typeof body.patch !== 'string') {
|
|
190
|
-
throw new Error('invalid incremental Notes v2 response');
|
|
191
|
-
}
|
|
192
|
-
return body;
|
|
193
|
-
};
|
|
194
|
-
const readActivities = async () => {
|
|
195
|
-
const feed = await request('/agent-activity');
|
|
196
|
-
if (!Array.isArray(feed?.activities))
|
|
197
|
-
throw new Error('server does not support activity reads');
|
|
198
|
-
return feed.activities;
|
|
199
|
-
};
|
|
200
|
-
const initialEvent = latest;
|
|
201
|
-
handled = sequence;
|
|
202
|
-
let initial = { body: await readBody(), participants: initialEvent.participants, activities: await readActivities() };
|
|
203
|
-
if (abort.signal.aborted)
|
|
204
|
-
return 0;
|
|
205
|
-
if (streamError)
|
|
206
|
-
throw streamError;
|
|
207
|
-
let previous = initial.body;
|
|
208
|
-
let observedActivities = initial.activities;
|
|
209
|
-
const batches = new LingerBatch(initial, self);
|
|
210
|
-
const printedSelections = new Map(batches.others(initial.participants).map(p => [p.client, selectionText(p)]));
|
|
211
|
-
if (opts.json)
|
|
212
|
-
await writeOutput(JSON.stringify({ type: 'snapshot', observedAt: clock.now(), ...initial.body, selectionHash: initialEvent.hash, participants: batches.others(initial.participants) }) + '\n');
|
|
213
|
-
else {
|
|
214
|
-
await writeOutput(`note: ${id}\nhash: ${previous.hash}\nrevision: ${previous.revision}\n\n${previous.content}\n\nPresent:\n`);
|
|
215
|
-
const others = batches.others(initial.participants);
|
|
216
|
-
await writeOutput(others.length ? others.map(p => '- ' + participant(p) + (p.selection ? selectionText(p) : '')).join('\n') + '\n' : 'No other participants present.\n');
|
|
217
|
-
}
|
|
218
|
-
// Release the full initial source rather than holding it through the entire session.
|
|
219
|
-
previous = { note: previous.note, hash: previous.hash, revision: previous.revision };
|
|
220
|
-
initial = undefined;
|
|
221
|
-
let observedHash = initialEvent.hash;
|
|
222
|
-
let activityVersion = initialEvent.activityVersion;
|
|
223
|
-
let lastEditAt = clock.now(), lastOutputAt = clock.now();
|
|
224
|
-
let dirtyContent = initialEvent.hash !== previous.hash;
|
|
225
|
-
const arrivals = new Map(), departures = new Map();
|
|
226
|
-
const activity = new Map();
|
|
227
|
-
const selections = new Map();
|
|
228
|
-
process.stderr.write(`Listening for live events; edit quiet period ${debounce}ms, output throttle ${throttle}ms. Ctrl-C to leave.\n`);
|
|
229
|
-
while (!abort.signal.aborted) {
|
|
230
|
-
if (streamError)
|
|
231
|
-
throw streamError;
|
|
232
|
-
if (sequence !== handled) {
|
|
233
|
-
handled = sequence;
|
|
234
|
-
const event = latest;
|
|
235
|
-
if (event.hash !== observedHash) {
|
|
236
|
-
observedHash = event.hash;
|
|
237
|
-
lastEditAt = clock.now();
|
|
238
|
-
dirtyContent = true;
|
|
239
|
-
}
|
|
240
|
-
if (event.activityVersion !== activityVersion)
|
|
241
|
-
observedActivities = await readActivities();
|
|
242
|
-
const delta = batches.diff({ body: previous, participants: event.participants, activities: observedActivities });
|
|
243
|
-
activityVersion = event.activityVersion;
|
|
244
|
-
for (const p of delta.joined) {
|
|
245
|
-
if (!departures.delete(p.client))
|
|
246
|
-
arrivals.set(p.client, p);
|
|
247
|
-
}
|
|
248
|
-
for (const p of delta.left) {
|
|
249
|
-
if (!arrivals.delete(p.client))
|
|
250
|
-
departures.set(p.client, p);
|
|
251
|
-
}
|
|
252
|
-
for (const p of delta.selections)
|
|
253
|
-
selections.set(p.client, p);
|
|
254
|
-
for (const a of delta.activities)
|
|
255
|
-
activity.set(`${a.agent.owner}:${a.agent.id}:${a.kind}`, a);
|
|
256
|
-
}
|
|
257
|
-
if (clock.now() - heartbeat >= HEARTBEAT_MS) {
|
|
258
|
-
await request('/presence', { method: 'POST', json: { action: 'heartbeat' } });
|
|
259
|
-
heartbeat = clock.now();
|
|
260
|
-
}
|
|
261
|
-
const now = clock.now();
|
|
262
|
-
const deliverContent = dirtyContent && now - lastEditAt >= debounce;
|
|
263
|
-
if (now - lastOutputAt >= throttle && (deliverContent || arrivals.size || departures.size || activity.size || selections.size)) {
|
|
264
|
-
const nextBody = deliverContent ? await readBody(previous) : undefined;
|
|
265
|
-
if (abort.signal.aborted)
|
|
266
|
-
break;
|
|
267
|
-
if (streamError)
|
|
268
|
-
throw streamError;
|
|
269
|
-
// A source event arriving during the read needs its own quiet period.
|
|
270
|
-
if (sequence !== handled && latest.hash !== observedHash)
|
|
271
|
-
continue;
|
|
272
|
-
const events = { joined: [...arrivals.values()], left: [...departures.values()], activities: [...activity.values()],
|
|
273
|
-
selections: [...selections.values()].map(p => ({ ...p, hash: observedHash })) };
|
|
45
|
+
await watchNote(noteTransport(c), path, id, { signal: abort.signal, format: opts.format, debounce: opts.debounce, throttle: opts.throttle, ifMatch: opts.ifMatch, intent: opts.intent }, async (event) => {
|
|
46
|
+
if (event.type === 'snapshot') {
|
|
47
|
+
for (const p of event.participants)
|
|
48
|
+
selections.set(p.client, selectionText(p));
|
|
274
49
|
if (opts.json)
|
|
275
|
-
await
|
|
50
|
+
await write(JSON.stringify(event) + '\n');
|
|
276
51
|
else {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
output += `+ ${participant(p)} joined\n`;
|
|
280
|
-
for (const p of events.left)
|
|
281
|
-
output += `- ${participant(p)} left\n`;
|
|
282
|
-
for (const a of events.activities)
|
|
283
|
-
output += `* ${label(a.agent.name)} (agent) ${a.kind === 'read' ? 'read' : 'edited'} the note\n`;
|
|
284
|
-
const departed = new Set(events.left.map(p => p.client));
|
|
285
|
-
for (const p of events.selections) {
|
|
286
|
-
const selected = selectionText(p);
|
|
287
|
-
if (!departed.has(p.client) && selected && selected !== printedSelections.get(p.client))
|
|
288
|
-
output += `* ${participant(p)}${selected}\n`;
|
|
289
|
-
printedSelections.set(p.client, selected);
|
|
290
|
-
}
|
|
291
|
-
for (const client of departed)
|
|
292
|
-
printedSelections.delete(client);
|
|
293
|
-
if (nextBody)
|
|
294
|
-
output += `base: ${nextBody.base}\nhash: ${nextBody.hash}\nrevision: ${nextBody.revision}\nformat: ${format}\n\n${nextBody.patch}\n`;
|
|
295
|
-
if (output)
|
|
296
|
-
await writeOutput(`\n[${new Date(clock.now()).toISOString()}]\n${output}`);
|
|
297
|
-
}
|
|
298
|
-
lastOutputAt = clock.now();
|
|
299
|
-
arrivals.clear();
|
|
300
|
-
departures.clear();
|
|
301
|
-
activity.clear();
|
|
302
|
-
selections.clear();
|
|
303
|
-
if (nextBody) {
|
|
304
|
-
previous = { note: nextBody.note, hash: nextBody.hash, revision: nextBody.revision };
|
|
305
|
-
dirtyContent = false;
|
|
52
|
+
await write(`note: ${id}\nhash: ${event.hash}\nrevision: ${event.revision}\n\n${event.content}\n\nPresent:\n`);
|
|
53
|
+
await write(event.participants.length ? event.participants.map(p => '- ' + participant(p) + (p.selection ? selectionText(p) : '')).join('\n') + '\n' : 'No other participants present.\n');
|
|
306
54
|
}
|
|
55
|
+
process.stderr.write('Listening for live events; Ctrl-C to leave.\n');
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (opts.json) {
|
|
59
|
+
await write(JSON.stringify(event) + '\n');
|
|
60
|
+
return;
|
|
307
61
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
62
|
+
let output = '';
|
|
63
|
+
for (const p of event.joined)
|
|
64
|
+
output += `+ ${participant(p)} joined\n`;
|
|
65
|
+
for (const p of event.left)
|
|
66
|
+
output += `- ${participant(p)} left\n`;
|
|
67
|
+
for (const a of event.activities)
|
|
68
|
+
output += `* ${label(a.agent.name)} (agent) ${a.kind === 'read' ? 'read' : 'edited'} the note\n`;
|
|
69
|
+
const departed = new Set(event.left.map(p => p.client));
|
|
70
|
+
for (const p of event.selections) {
|
|
71
|
+
const selected = selectionText(p);
|
|
72
|
+
if (!departed.has(p.client) && selected && selected !== selections.get(p.client))
|
|
73
|
+
output += `* ${participant(p)}${selected}\n`;
|
|
74
|
+
selections.set(p.client, selected);
|
|
75
|
+
}
|
|
76
|
+
for (const client of departed)
|
|
77
|
+
selections.delete(client);
|
|
78
|
+
if (event.content)
|
|
79
|
+
output += `base: ${event.content.base}\nhash: ${event.content.hash}\nrevision: ${event.content.revision}\nformat: ${opts.format ?? 'diff'}\n\n${event.content.patch}\n`;
|
|
80
|
+
if (output)
|
|
81
|
+
await write(`\n[${new Date(event.observedAt).toISOString()}]\n${output}`);
|
|
82
|
+
}, clock ? { ...clock, signal: abort.signal, events: clock.events ? ((url, token, note, signal, receive, request) => clock.events(c.remote.replace(/\/+$/, '') + url, token, note, signal, receive, request)) : undefined } : undefined);
|
|
83
|
+
return 0;
|
|
317
84
|
}
|
|
318
85
|
catch (error) {
|
|
319
86
|
if (!abort.signal.aborted)
|
|
320
|
-
|
|
87
|
+
return report(error);
|
|
88
|
+
return 0;
|
|
321
89
|
}
|
|
322
90
|
finally {
|
|
323
91
|
abort.abort();
|
|
324
|
-
await streaming;
|
|
325
|
-
// Keep signal handlers during bounded cleanup, including repeated Ctrl-C.
|
|
326
|
-
if (leave)
|
|
327
|
-
try {
|
|
328
|
-
await leave();
|
|
329
|
-
}
|
|
330
|
-
catch {
|
|
331
|
-
fail('could not send leave; presence will expire automatically');
|
|
332
|
-
rc ||= 1;
|
|
333
|
-
}
|
|
334
92
|
process.removeListener('SIGINT', stop);
|
|
335
93
|
process.removeListener('SIGTERM', stop);
|
|
336
|
-
clock
|
|
94
|
+
clock?.signal?.removeEventListener('abort', stop);
|
|
337
95
|
}
|
|
338
|
-
return rc;
|
|
339
96
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/** Resolve canonical source text, then publish one hash-bound agent selection. */
|
|
2
2
|
import { ctx, report, resolveNote, seg } from './index.js';
|
|
3
3
|
import { emitJson, fail } from '../helpers.js';
|
|
4
|
-
import { HttpError
|
|
5
|
-
import {
|
|
4
|
+
import { HttpError } from '../client.js';
|
|
5
|
+
import { ApiError } from '../../api/client.js';
|
|
6
|
+
import { notePresenceApi } from '../../api/note-presence.js';
|
|
7
|
+
import { noteTransport } from './api-transport.js';
|
|
6
8
|
import { EXIT_NO_MATCH } from './edit-commands.js';
|
|
7
9
|
export async function runNotesTextSelect(css, opts) {
|
|
8
10
|
if (css !== undefined || !opts.text || !opts.note || opts.section === '') {
|
|
@@ -28,46 +30,10 @@ export async function runNotesTextSelect(css, opts) {
|
|
|
28
30
|
const id = await resolveNote(c, opts.note);
|
|
29
31
|
if (!id)
|
|
30
32
|
return 1;
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
// when the match is ambiguous. Keep authentication, suppress observation only.
|
|
34
|
-
const snapshot = await requestJson(c.remote, opts.section !== undefined ? `${path}/sections/${seg(opts.section)}` : `${path}/body?contract=v2`, { token: c.token, headers: { 'X-DreamLake-Agent-Id': '' } });
|
|
35
|
-
const source = opts.section !== undefined ? snapshot.text : snapshot.content;
|
|
36
|
-
const offset = opts.section !== undefined ? snapshot.range?.start : 0;
|
|
37
|
-
if (typeof source !== 'string' || !/^sha256:[a-f0-9]{64}$/.test(snapshot.hash ?? '') ||
|
|
38
|
-
!Number.isSafeInteger(offset) || offset < 0 ||
|
|
39
|
-
(opts.section !== undefined && snapshot.range?.end !== offset + Array.from(source).length)) {
|
|
40
|
-
throw new Error('server did not provide canonical source/hash and code-point range; section selection requires the updated Notes server');
|
|
41
|
-
}
|
|
42
|
-
if (opts.hash !== undefined && opts.hash !== snapshot.hash) {
|
|
43
|
-
fail('stale_range: source hash changed; read the section again before selecting');
|
|
44
|
-
return 1;
|
|
45
|
-
}
|
|
46
|
-
const matches = find(source, opts.text);
|
|
47
|
-
const requested = opts.occurrence === undefined ? 1 : Number(opts.occurrence);
|
|
48
|
-
const nth = requested < 0 ? matches.length + requested + 1 : requested;
|
|
49
|
-
if (!matches.length || nth < 1 || nth > matches.length) {
|
|
50
|
-
fail(`${matches.length} matches in ${opts.section === undefined ? 'note' : `section ${opts.section}`}; requested occurrence ${requested}. No selection published.`);
|
|
51
|
-
return EXIT_NO_MATCH;
|
|
52
|
-
}
|
|
53
|
-
if (matches.length > 1 && opts.occurrence === undefined) {
|
|
54
|
-
fail(`${matches.length} matches; use --section or --occurrence 1..${matches.length} (or -o -1 for the last). No selection published.`);
|
|
55
|
-
return EXIT_NO_MATCH;
|
|
56
|
-
}
|
|
57
|
-
const match = matches[nth - 1];
|
|
58
|
-
const range = { start: offset + match.ind[0], end: offset + match.ind[1] };
|
|
59
|
-
const result = await requestJson(c.remote, `${path}/presence`, {
|
|
60
|
-
method: 'POST', token: c.token, json: { action: 'seek', hash: snapshot.hash, ranges: [range] },
|
|
33
|
+
const receipt = await notePresenceApi(noteTransport(c), `/namespaces/${seg(c.ns)}/notes`).selectText(id, {
|
|
34
|
+
text: opts.text, section: opts.section, occurrence: opts.occurrence === undefined ? undefined : Number(opts.occurrence), hash: opts.hash,
|
|
61
35
|
});
|
|
62
|
-
const
|
|
63
|
-
if (activity?.kind !== 'seeking' || activity.hash !== snapshot.hash || activity.ranges?.length !== 1 ||
|
|
64
|
-
activity.ranges[0].start !== range.start || activity.ranges[0].end !== range.end ||
|
|
65
|
-
!Number.isFinite(activity.expiresAt) || !Number.isFinite(result.state?.expiresAt)) {
|
|
66
|
-
throw new Error('selection publication unconfirmed: server returned an unexpected presence receipt');
|
|
67
|
-
}
|
|
68
|
-
const scope = opts.section === undefined ? { kind: 'note' } : { kind: 'section', anchor: opts.section };
|
|
69
|
-
const receipt = { note: id, hash: snapshot.hash, text: match.text, scope, range, occurrence: nth, matches: matches.length,
|
|
70
|
-
published: true, selectionExpiresAt: activity.expiresAt, presenceExpiresAt: result.state.expiresAt };
|
|
36
|
+
const range = receipt.range, scope = receipt.scope, nth = receipt.occurrence, matches = { length: receipt.matches };
|
|
71
37
|
if (opts.json)
|
|
72
38
|
emitJson(receipt);
|
|
73
39
|
else
|
|
@@ -75,6 +41,19 @@ export async function runNotesTextSelect(css, opts) {
|
|
|
75
41
|
return 0;
|
|
76
42
|
}
|
|
77
43
|
catch (error) {
|
|
44
|
+
if (error instanceof ApiError && ['note_selection_no_match', 'note_selection_ambiguous'].includes(error.code)) {
|
|
45
|
+
const body = error.body;
|
|
46
|
+
fail(error.code === 'note_selection_ambiguous' ? `${body.matches} matches; use --section or --occurrence 1..${body.matches} (or -o -1 for the last). No selection published.` : `${body.matches} matches in ${opts.section === undefined ? 'note' : `section ${opts.section}`}; requested occurrence ${body.occurrence}. No selection published.`);
|
|
47
|
+
return EXIT_NO_MATCH;
|
|
48
|
+
}
|
|
49
|
+
if (error instanceof ApiError && error.code === 'stale_range') {
|
|
50
|
+
fail('stale_range: source hash changed; read the section again before selecting');
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
if (error instanceof ApiError && error.code === 'note_selection_unconfirmed') {
|
|
54
|
+
fail('selection publication unconfirmed: server returned an unexpected presence receipt');
|
|
55
|
+
return 1;
|
|
56
|
+
}
|
|
78
57
|
if (error instanceof HttpError && error.status === 409 && error.body.includes('stale_range')) {
|
|
79
58
|
fail('stale_range: note changed before publication; read the section again and retry. No selection published.');
|
|
80
59
|
return 1;
|
package/dist/cli/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.90.0";
|
package/dist/cli/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dreamlake/dreamlake-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.90.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/dreamlake-ai/dreamlake-cli.git"
|
|
@@ -35,13 +35,13 @@
|
|
|
35
35
|
"node": ">=18"
|
|
36
36
|
},
|
|
37
37
|
"optionalDependencies": {
|
|
38
|
-
"@dreamlake/dreamlake-cli-darwin-arm64": "0.
|
|
39
|
-
"@dreamlake/dreamlake-cli-darwin-x64": "0.
|
|
40
|
-
"@dreamlake/dreamlake-cli-linux-x64": "0.
|
|
41
|
-
"@dreamlake/dreamlake-cli-linux-arm64": "0.
|
|
42
|
-
"@dreamlake/dreamlake-cli-linux-x64-musl": "0.
|
|
43
|
-
"@dreamlake/dreamlake-cli-linux-arm64-musl": "0.
|
|
44
|
-
"@dreamlake/dreamlake-cli-win32-x64": "0.
|
|
45
|
-
"@dreamlake/dreamlake-cli-win32-arm64": "0.
|
|
38
|
+
"@dreamlake/dreamlake-cli-darwin-arm64": "0.90.0",
|
|
39
|
+
"@dreamlake/dreamlake-cli-darwin-x64": "0.90.0",
|
|
40
|
+
"@dreamlake/dreamlake-cli-linux-x64": "0.90.0",
|
|
41
|
+
"@dreamlake/dreamlake-cli-linux-arm64": "0.90.0",
|
|
42
|
+
"@dreamlake/dreamlake-cli-linux-x64-musl": "0.90.0",
|
|
43
|
+
"@dreamlake/dreamlake-cli-linux-arm64-musl": "0.90.0",
|
|
44
|
+
"@dreamlake/dreamlake-cli-win32-x64": "0.90.0",
|
|
45
|
+
"@dreamlake/dreamlake-cli-win32-arm64": "0.90.0"
|
|
46
46
|
}
|
|
47
47
|
}
|