@wherever-dev/client 0.5.3 → 0.6.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/src/types.ts CHANGED
@@ -64,6 +64,14 @@ export interface ChatMessage {
64
64
  // e.g. one loaded from server history). Confirmed on the server's user echo
65
65
  // (message_end role:user) or when it reappears in loaded history.
66
66
  delivery?: 'sending' | 'failed';
67
+ // True on a user message this client RESTORED from the server's steer-queue
68
+ // snapshot rather than from history or its own send (see queue_update). Such a
69
+ // message is queued in the agent but not yet in the session file, so when pi
70
+ // finally injects it the server's user echo must RECONCILE onto this bubble
71
+ // (clearing the flag) instead of appending a duplicate. Content-matching the
72
+ // last user message is not enough: with several messages queued they are
73
+ // delivered oldest-first, so the echoed one is usually not the last bubble.
74
+ restoredFromQueue?: boolean;
67
75
  }
68
76
 
69
77
  export interface SessionNotice {
@@ -93,6 +101,101 @@ export interface FolderConflictInfo {
93
101
  continued: boolean;
94
102
  }
95
103
 
104
+ // State for the MISSING-FOLDER lock. Set when the server reports that this
105
+ // session's working folder does not exist on this machine (the `folderMissing`
106
+ // flag on session_created, and the `folder_missing` frame that names the path).
107
+ // The transcript still paints -- reading never needed the folder -- but the
108
+ // server built no live agent and refuses every send.
109
+ //
110
+ // Unlike a folder conflict this is HARD: there is no "Continue anyway". It is
111
+ // cured by restoring the folder and RELOADING the session (whether a session has
112
+ // a live agent is a load-time decision). `cwd` is the absolute missing path, so
113
+ // the UI can name exactly what to restore where the composer would be.
114
+ export interface FolderMissingInfo {
115
+ cwd: string;
116
+ }
117
+
118
+ // --- RESTORE (materialising a missing working folder) ----------------------
119
+ //
120
+ // Vocabulary, pinned (see CONTEXT.md): RESTORE here means making a missing
121
+ // working folder exist again -- cloning the repository back, or creating the
122
+ // folder. It is NOT the unrelated in-codebase sense of re-materialising queued
123
+ // steers and drafts after a reload (`restoredFromQueue` above). Every frame and
124
+ // field in this family is namespaced `restore*`.
125
+ //
126
+ // These mirror the server's restore-job snapshot (server/src/restore-jobs.ts)
127
+ // field for field, duplicated here rather than imported so this package keeps no
128
+ // server dependency (the same posture as ContextUsage).
129
+
130
+ // One honest progress reading. `percent` is null exactly when git reports no
131
+ // percentage for the phase, and `indeterminate` says so explicitly rather than
132
+ // leaving the UI to invent a 0. `scope` is the repository itself
133
+ // ('repository') or a submodule's path relative to the target: submodules are
134
+ // counted SEPARATELY, never folded into one fake global percentage.
135
+ export interface RestoreProgressInfo {
136
+ phase: string;
137
+ scope: string;
138
+ percent: number | null;
139
+ indeterminate: boolean;
140
+ /** The git line this was parsed from, for a detail line under the bar. */
141
+ text: string;
142
+ at: number;
143
+ }
144
+
145
+ // A failed restore, explained. `cause` is the server's mapping onto a named,
146
+ // actionable problem ('no-key' | 'unknown-host' | 'not-found' | 'network' |
147
+ // 'unknown'); `message` is the human sentence for it, and `stderr` is git's raw
148
+ // output, kept UNDERNEATH the mapping and never replaced by it.
149
+ export interface RestoreFailureInfo {
150
+ cause: string;
151
+ message: string;
152
+ stderr: string;
153
+ }
154
+
155
+ // A restore job as the server reports it. Keyed by `targetPath` on the server,
156
+ // so `id` is what tells a retry apart from the job it replaced.
157
+ export interface RestoreJobInfo {
158
+ id: number;
159
+ kind: 'clone' | 'create';
160
+ targetPath: string;
161
+ /** The url the job is ACTUALLY cloning (clone kind only). */
162
+ url?: string;
163
+ gitInit?: boolean;
164
+ state: 'running' | 'done' | 'failed' | 'cancelled';
165
+ progress: RestoreProgressInfo | null;
166
+ failure?: RestoreFailureInfo;
167
+ startedAt: number;
168
+ endedAt?: number;
169
+ }
170
+
171
+ // What the restore panel needs to render, for ONE folder. Created as soon as a
172
+ // folder-missing session is loaded (with `job` already set when the server is
173
+ // mid-clone for that path, so a reconnecting phone repaints a running job), and
174
+ // updated by the server's restore frames.
175
+ export interface RestoreInfo {
176
+ /** The absolute folder being restored. Frames for any other path are ignored. */
177
+ targetPath: string;
178
+ /** The server's job for that path, or null while none has been started. */
179
+ job: RestoreJobInfo | null;
180
+ // True when THIS client's last start JOINED a job that was already running.
181
+ // With `requestedUrl` it is what lets the panel say "a clone of <job.url> is
182
+ // already running" instead of pretending the edited url was accepted.
183
+ joined: boolean;
184
+ /** The url this client last asked for, whether or not it was the one used. */
185
+ requestedUrl?: string;
186
+ // A request REFUSED before anything was spawned (an unsafe target, a
187
+ // non-empty folder, a url outside the allowlist). No job exists and no
188
+ // completion is coming, so the panel shows this and stays in its offer state.
189
+ rejection: {reason: string; message: string} | null;
190
+ // True when this restore is the first step of a SESSION CREATE (the
191
+ // new-session clone), not the restore of a loaded session's missing folder.
192
+ // Same job, same frames, same progress display -- what differs is the ENDING:
193
+ // there is no session to reload here (it does not exist until the clone
194
+ // lands), so the server continues into creation itself and the client hands
195
+ // the screen back to the create instead of offering a Reload.
196
+ forSessionCreate?: boolean;
197
+ }
198
+
96
199
  export interface WhereverState {
97
200
  connected: boolean;
98
201
  connecting: boolean;
@@ -120,7 +223,22 @@ export interface WhereverState {
120
223
  isStreaming: boolean;
121
224
  messages: ChatMessage[];
122
225
  clientId: string | null;
226
+ // Version of the server we are connected to (the `wherever-dev` package
227
+ // version), as reported in its `connected` message. null until connected, or
228
+ // when talking to a server old enough not to report it.
229
+ serverVersion: string | null;
123
230
  folderConflict: FolderConflictInfo | null;
231
+ // The active session's working folder is gone from this machine (see
232
+ // FolderMissingInfo). Non-null implies `readOnly`, and no `session_ready` is
233
+ // coming: the composer is replaced by a notice naming the path. Cleared with
234
+ // the session it belongs to.
235
+ folderMissing: FolderMissingInfo | null;
236
+ // The restore of the active session's missing folder: the server's job for
237
+ // that path (running, finished or not yet started) plus this client's own last
238
+ // request. Non-null exactly while a folder-missing session is open, so the
239
+ // panel that replaces the composer renders from here. Cleared with the session
240
+ // it belongs to.
241
+ restore: RestoreInfo | null;
124
242
  isInterrupted: boolean;
125
243
  // A dismissible, non-fatal notice about the active session (e.g. a CLI bridge
126
244
  // took over while this session was mid-turn here, discarding the in-flight
@@ -0,0 +1,111 @@
1
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
+ import {get} from 'sveltore';
3
+ import {WhereverClient} from '../src/client.js';
4
+ import type {WhereverState} from '../src/types.js';
5
+ import {makeWSFactory} from './harness.js';
6
+
7
+ // The per-turn CONVERSATION-MODE SIGNAL on the wire: a spoken conversation is
8
+ // state that lives in the web app's knobs registry, and a dictated message is
9
+ // byte-identical to a typed one, so the agent could never tell the mode was on
10
+ // and never called `say`. The client now stamps an OPTIONAL `conversationMode`
11
+ // boolean on the EXISTING `message` payload (no new message type, no new chat
12
+ // role); when off the field is absent, so the frame is exactly what it was before
13
+ // and an older server ignores it.
14
+
15
+ function installLocalStorage() {
16
+ const store = new Map<string, string>();
17
+ (globalThis as any).localStorage = {
18
+ getItem: (k: string) => (store.has(k) ? store.get(k)! : null),
19
+ setItem: (k: string, v: string) => void store.set(k, v),
20
+ removeItem: (k: string) => void store.delete(k),
21
+ clear: () => store.clear(),
22
+ };
23
+ return store;
24
+ }
25
+
26
+ function createAndLoad(ws: ReturnType<typeof makeWSFactory>, client: WhereverClient) {
27
+ client.joinSession('/tmp/project/session.jsonl');
28
+ ws.last().receive({
29
+ type: 'session_created',
30
+ sessionId: 'sid-1',
31
+ sessionFile: '/tmp/project/session.jsonl',
32
+ cwd: '/tmp/project',
33
+ model: 'fake:model',
34
+ isStreaming: false,
35
+ });
36
+ ws.last().receive({
37
+ type: 'message_history',
38
+ sessionId: 'sid-1',
39
+ messages: [],
40
+ totalCount: 0,
41
+ offset: 0,
42
+ });
43
+ }
44
+
45
+ describe('conversation-mode flag on the message payload', () => {
46
+ let ws: ReturnType<typeof makeWSFactory>;
47
+ let client: WhereverClient;
48
+ const state = () => get<WhereverState>(client.stateStore);
49
+
50
+ beforeEach(() => {
51
+ vi.useFakeTimers();
52
+ installLocalStorage();
53
+ (globalThis as any).localStorage.clear();
54
+ ws = makeWSFactory();
55
+ client = new WhereverClient({
56
+ host: 'localhost',
57
+ port: 1234,
58
+ secure: false,
59
+ WebSocketCtor: ws.ctor,
60
+ });
61
+ client.connect();
62
+ ws.last().open();
63
+ createAndLoad(ws, client);
64
+ });
65
+
66
+ afterEach(() => {
67
+ client.disconnect(true);
68
+ vi.useRealTimers();
69
+ });
70
+
71
+ it('omits the field entirely for a normal send (no options)', () => {
72
+ expect(client.sendMessage('plain send')).toBe(true);
73
+ const frame = ws.last().lastSentOfType('message');
74
+ expect(frame.message).toBe('plain send');
75
+ expect('conversationMode' in frame).toBe(false);
76
+ });
77
+
78
+ it('omits the field when the caller passes conversationMode: false', () => {
79
+ client.sendMessage('mode off', {conversationMode: false});
80
+ const frame = ws.last().lastSentOfType('message');
81
+ expect('conversationMode' in frame).toBe(false);
82
+ });
83
+
84
+ it('stamps conversationMode: true when the caller asks for it', () => {
85
+ client.sendMessage('speak this back', {conversationMode: true});
86
+ const frame = ws.last().lastSentOfType('message');
87
+ expect(frame.type).toBe('message');
88
+ expect(frame.conversationMode).toBe(true);
89
+ // The user text is untouched: the signal is a field, never a preamble.
90
+ expect(frame.message).toBe('speak this back');
91
+ });
92
+
93
+ it('keeps the local echo free of the flag (nothing user-visible changes)', () => {
94
+ client.sendMessage('echo check', {conversationMode: true});
95
+ const echo = state().messages.find((m) => m.content === 'echo check')!;
96
+ expect(echo.content).toBe('echo check');
97
+ expect((echo as unknown as Record<string, unknown>).conversationMode).toBeUndefined();
98
+ });
99
+
100
+ it('re-stamps the flag on resend, from the caller (a resend is a fresh turn)', () => {
101
+ client.sendMessage('retry me', {conversationMode: true});
102
+ const id = state().messages.find((m) => m.content === 'retry me')!.id;
103
+ // The user flipped the mode OFF before hitting Retry: no flag this time.
104
+ expect(client.resendMessage(id)).toBe(true);
105
+ expect('conversationMode' in ws.last().lastSentOfType('message')).toBe(false);
106
+
107
+ // And with the mode back on, the resend carries it again.
108
+ expect(client.resendMessage(id, {conversationMode: true})).toBe(true);
109
+ expect(ws.last().lastSentOfType('message').conversationMode).toBe(true);
110
+ });
111
+ });
@@ -0,0 +1,145 @@
1
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
+ import {get} from 'sveltore';
3
+ import {WhereverClient} from '../src/client.js';
4
+ import {makeWSFactory} from './harness.js';
5
+
6
+ // A client attached read-only because of a FOLDER CONFLICT can only escape in
7
+ // two ways: click "Continue anyway" (which lives on the conflict banner), or
8
+ // have the conflict resolve on its own. Both hinge on the live `folder_conflict`
9
+ // update, which now carries the server's authoritative `readOnly`:
10
+ // - while the conflict is active, the banner must stay up (it holds the only
11
+ // escape hatch); dropping it strands the user read-only with no affordance;
12
+ // - once it resolves, read-only must be RELEASED, since the banner that could
13
+ // have lifted it is gone.
14
+
15
+ describe('folder-conflict read-only lifecycle', () => {
16
+ let ws: ReturnType<typeof makeWSFactory>;
17
+ let client: WhereverClient;
18
+
19
+ function attachAsConflictObserver() {
20
+ client.createSession('/tmp/project');
21
+ ws.last().receive({
22
+ type: 'session_created',
23
+ sessionId: 'sid-1',
24
+ sessionFile: '/tmp/project/session.jsonl',
25
+ cwd: '/tmp/project',
26
+ model: 'fake:model',
27
+ readOnly: true,
28
+ folderConflict: true,
29
+ });
30
+ }
31
+
32
+ beforeEach(() => {
33
+ vi.useFakeTimers();
34
+ ws = makeWSFactory();
35
+ client = new WhereverClient({
36
+ host: 'localhost',
37
+ port: 1234,
38
+ secure: false,
39
+ WebSocketCtor: ws.ctor,
40
+ });
41
+ client.connect();
42
+ ws.last().open();
43
+ });
44
+
45
+ afterEach(() => {
46
+ client.disconnect(true);
47
+ vi.useRealTimers();
48
+ });
49
+
50
+ it('keeps the banner (and its escape hatch) while the conflict is active', () => {
51
+ attachAsConflictObserver();
52
+ expect(get(client.stateStore).readOnly).toBe(true);
53
+ expect(get(client.stateStore).folderConflict).toEqual({
54
+ cwd: '/tmp/project',
55
+ active: true,
56
+ continued: false,
57
+ });
58
+
59
+ ws.last().receive({
60
+ type: 'folder_conflict',
61
+ cwd: '/tmp/project',
62
+ active: true,
63
+ readOnly: true,
64
+ });
65
+
66
+ const s = get(client.stateStore);
67
+ expect(s.folderConflict?.active).toBe(true);
68
+ // Not continued: the "Continue anyway" button must still be offered.
69
+ expect(s.folderConflict?.continued).toBe(false);
70
+ expect(s.readOnly).toBe(true);
71
+ });
72
+
73
+ it('releases read-only when the conflict resolves', () => {
74
+ attachAsConflictObserver();
75
+
76
+ ws.last().receive({
77
+ type: 'folder_conflict',
78
+ cwd: '/tmp/project',
79
+ active: false,
80
+ readOnly: false,
81
+ });
82
+
83
+ const s = get(client.stateStore);
84
+ expect(s.folderConflict).toBeNull();
85
+ expect(s.readOnly).toBe(false);
86
+ });
87
+
88
+ it('keeps a hard read-only session locked when the server says so', () => {
89
+ attachAsConflictObserver();
90
+
91
+ // A sessions.readOnly folder: the conflict is gone but the server still
92
+ // reports read-only, which is not a dismissible warning.
93
+ ws.last().receive({
94
+ type: 'folder_conflict',
95
+ cwd: '/tmp/project',
96
+ active: false,
97
+ readOnly: true,
98
+ });
99
+
100
+ const s = get(client.stateStore);
101
+ expect(s.folderConflict).toBeNull();
102
+ expect(s.readOnly).toBe(true);
103
+ });
104
+
105
+ it('converges when a pre-continue broadcast arrives after the optimistic continue', () => {
106
+ attachAsConflictObserver();
107
+ client.continueFolderConflict();
108
+ expect(get(client.stateStore).readOnly).toBe(false);
109
+
110
+ // In flight before the server saw the continue: it still carries the old
111
+ // verdict and re-locks the composer, while `continued` has already removed
112
+ // the banner's button. Only the server's answer to the continue settles it,
113
+ // and same-socket ordering guarantees that answer lands last.
114
+ ws.last().receive({
115
+ type: 'folder_conflict',
116
+ cwd: '/tmp/project',
117
+ active: true,
118
+ readOnly: true,
119
+ });
120
+ expect(get(client.stateStore).readOnly).toBe(true);
121
+
122
+ ws.last().receive({
123
+ type: 'folder_conflict',
124
+ cwd: '/tmp/project',
125
+ active: true,
126
+ readOnly: false,
127
+ });
128
+
129
+ const s = get(client.stateStore);
130
+ expect(s.readOnly).toBe(false);
131
+ expect(s.folderConflict).toEqual({
132
+ cwd: '/tmp/project',
133
+ active: true,
134
+ continued: true,
135
+ });
136
+ });
137
+
138
+ it('leaves read-only untouched when the server omits it (older server)', () => {
139
+ attachAsConflictObserver();
140
+
141
+ ws.last().receive({type: 'folder_conflict', cwd: '/tmp/project', active: false});
142
+
143
+ expect(get(client.stateStore).readOnly).toBe(true);
144
+ });
145
+ });
@@ -0,0 +1,114 @@
1
+ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
2
+ import {get} from 'sveltore';
3
+ import {WhereverClient} from '../src/client.js';
4
+ import {makeWSFactory} from './harness.js';
5
+
6
+ // FOLDER MISSING is the third read-only reason: the session's working folder
7
+ // does not exist on this machine, so the server built no live agent and refuses
8
+ // every send. It is HARD -- unlike a folder conflict there is no "Continue
9
+ // anyway" -- and it is cured by restoring the folder and RELOADING, never by a
10
+ // client-side dismiss. The client mirrors it so the UI can replace the composer
11
+ // with a notice naming the absolute path.
12
+
13
+ describe('missing-folder read-only state', () => {
14
+ let ws: ReturnType<typeof makeWSFactory>;
15
+ let client: WhereverClient;
16
+
17
+ function loadMissingFolderSession() {
18
+ client.joinSession('/tmp/gone/session.jsonl');
19
+ ws.last().receive({
20
+ type: 'session_created',
21
+ sessionId: 'sid-1',
22
+ sessionFile: '/tmp/gone/session.jsonl',
23
+ cwd: '/tmp/gone',
24
+ model: 'fake:model',
25
+ readOnly: true,
26
+ folderMissing: true,
27
+ pending: true,
28
+ });
29
+ ws.last().receive({
30
+ type: 'folder_missing',
31
+ sessionId: 'sid-1',
32
+ cwd: '/tmp/gone',
33
+ });
34
+ }
35
+
36
+ beforeEach(() => {
37
+ vi.useFakeTimers();
38
+ ws = makeWSFactory();
39
+ client = new WhereverClient({
40
+ host: 'localhost',
41
+ port: 1234,
42
+ secure: false,
43
+ WebSocketCtor: ws.ctor,
44
+ });
45
+ client.connect();
46
+ ws.last().open();
47
+ });
48
+
49
+ afterEach(() => {
50
+ client.disconnect(true);
51
+ vi.useRealTimers();
52
+ });
53
+
54
+ it('locks the session and records the absolute missing path', () => {
55
+ loadMissingFolderSession();
56
+
57
+ const s = get(client.stateStore);
58
+ expect(s.readOnly).toBe(true);
59
+ expect(s.folderMissing).toEqual({cwd: '/tmp/gone'});
60
+ // No agent is coming (the server built none), so the composer stays gated.
61
+ expect(s.agentPending).toBe(true);
62
+ });
63
+
64
+ it('is not lifted by "Continue anyway"', () => {
65
+ loadMissingFolderSession();
66
+ // A folder conflict is raised on top (or a stale UI still shows its
67
+ // button): continuing answers the CONFLICT, never the missing folder.
68
+ ws.last().receive({
69
+ type: 'folder_conflict',
70
+ cwd: '/tmp/gone',
71
+ active: true,
72
+ readOnly: true,
73
+ });
74
+ client.continueFolderConflict();
75
+
76
+ const s = get(client.stateStore);
77
+ expect(s.readOnly).toBe(true);
78
+ expect(s.folderMissing).toEqual({cwd: '/tmp/gone'});
79
+ });
80
+
81
+ it('stays locked when a conflict update reports the server verdict', () => {
82
+ loadMissingFolderSession();
83
+
84
+ ws.last().receive({
85
+ type: 'folder_conflict',
86
+ cwd: '/tmp/gone',
87
+ active: false,
88
+ readOnly: true,
89
+ });
90
+
91
+ expect(get(client.stateStore).readOnly).toBe(true);
92
+ expect(get(client.stateStore).folderMissing).toEqual({cwd: '/tmp/gone'});
93
+ });
94
+
95
+ it('clears with the session it belonged to', () => {
96
+ loadMissingFolderSession();
97
+
98
+ client.switchSession('/tmp/present/session.jsonl');
99
+ expect(get(client.stateStore).folderMissing).toBeNull();
100
+
101
+ ws.last().receive({
102
+ type: 'session_created',
103
+ sessionId: 'sid-2',
104
+ sessionFile: '/tmp/present/session.jsonl',
105
+ cwd: '/tmp/present',
106
+ model: 'fake:model',
107
+ pending: true,
108
+ });
109
+
110
+ const s = get(client.stateStore);
111
+ expect(s.folderMissing).toBeNull();
112
+ expect(s.readOnly).toBe(false);
113
+ });
114
+ });