-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_commands.dart
More file actions
468 lines (455 loc) · 15.9 KB
/
Copy pathclient_commands.dart
File metadata and controls
468 lines (455 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/// Client commands — slash commands that the app handles locally without
/// sending anything to the agent. They show up in the slash palette
/// alongside agent-provided commands and server commands.
///
/// To add a new client command, add an entry to [clientCommands]. The
/// handler runs in widget context with access to Riverpod via [ref].
library;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:phosphoricons_flutter/phosphoricons_flutter.dart';
import '../../app/theme.dart';
import '../../store/connection.dart';
import '../../store/models.dart';
import '../../store/store.dart';
import '../../transport/protocol.dart';
import '../../status/status_event.dart';
import '../../status/status_providers.dart';
import '../widgets/sheet_header.dart';
import '../widgets/searchable_list_sheet.dart';
import '../session/session_identity.dart';
import '../../app/routes.dart';
typedef ClientCmdHandler =
Future<void> Function(
BuildContext context,
WidgetRef ref, {
required String sessionId,
required String arg,
});
class ClientCommand {
const ClientCommand({
required this.name,
required this.description,
required this.handler,
});
final String name;
final String description;
final ClientCmdHandler handler;
SlashCmd toSlashCmd() =>
SlashCmd(name: name, description: description, source: 'builtin');
}
/// Try to handle [raw] (e.g. `/new`) as a client command. Returns true if a
/// handler matched; false if [raw] should be sent on to the agent.
Future<bool> handleClientCommand(
String raw, {
required BuildContext context,
required WidgetRef ref,
required String sessionId,
}) async {
if (!raw.startsWith('/')) return false;
final firstSpace = raw.indexOf(RegExp(r'\s'));
final name = firstSpace < 0 ? raw.substring(1) : raw.substring(1, firstSpace);
final arg = firstSpace < 0 ? '' : raw.substring(firstSpace + 1).trim();
for (final c in clientCommands) {
if (c.name == name) {
await c.handler(context, ref, sessionId: sessionId, arg: arg);
return true;
}
}
return false;
}
final List<ClientCommand> clientCommands = <ClientCommand>[
ClientCommand(
name: 'new',
description: 'Start a fresh agent in this worktree',
handler: (context, ref, {required sessionId, required arg}) async {
// Resolved before the first await: `ref` throws once its widget is
// unmounted, and the record must survive the thing that reported to it.
final status = ref.status;
final session = ref.read(sessionsProvider).byId(sessionId);
if (session == null) return;
// SPEC-30 decision 18: the new agent runs in THIS pane's worktree, so no
// dialog is needed. A session with no worktree on disk yet cannot answer
// "where does it run?" — spawning bare would silently land the agent in
// the repo's primary checkout, so refuse and say why.
final worktreePath = session.worktreePath;
if (worktreePath == null) {
status.warning(
'This session has no worktree yet — send a message '
'first, or start one from the sidebar.',
source: StatusSources.session,
sessionId: sessionId,
);
return;
}
try {
final newId = await ref
.read(storeControllerProvider.notifier)
.spawnSession(
session.projectId,
worktreePath: worktreePath,
branch: session.branch,
);
if (!context.mounted) return;
context.go(routeForSession(newId));
} catch (e) {
status.failure(
'Could not spawn session',
error: e,
source: StatusSources.session,
sessionId: sessionId,
);
}
},
),
ClientCommand(
name: 'cancel',
description: 'Cancel the current agent turn',
handler: (context, ref, {required sessionId, required arg}) async {
try {
await ref.read(connectionControllerProvider.notifier).request(
MsgType.cmd,
{'kind': 'cancel', 'sessionId': sessionId},
);
} catch (_) {
/* best-effort */
}
},
),
ClientCommand(
name: 'unpair',
description: 'Forget the paired desktop server and return to pairing',
handler: (context, ref, {required sessionId, required arg}) async {
await ref.read(connectionControllerProvider.notifier).unpair();
if (!context.mounted) return;
context.go(kRouteRoot);
},
),
ClientCommand(
name: 'help',
description: 'Show available commands',
handler: (context, ref, {required sessionId, required arg}) async {
final agentCmds = ref.read(commandsProvider(sessionId));
final lines = [
for (final c in clientCommands) '/${c.name} — ${c.description}',
for (final c in agentCmds) '${c.invocation} — ${c.description}',
];
if (!context.mounted) return;
showDialog<void>(
context: context,
builder: (_) => AlertDialog(
title: const Text('Available commands'),
content: SizedBox(
width: 400,
child: SingleChildScrollView(
child: Text(
lines.join('\n'),
style: Theme.of(context).textTheme.bodySmall?.mono,
),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'),
),
],
),
);
},
),
ClientCommand(
name: 'ask',
description:
'Debug: ask the server to ask you a question (round-trip test)',
handler: (context, ref, {required sessionId, required arg}) async {
try {
await ref.read(connectionControllerProvider.notifier).request(
MsgType.cmd,
{'kind': 'debug.ask', 'sessionId': sessionId},
);
} catch (_) {
/* best-effort */
}
},
),
ClientCommand(
name: 'compact',
description: 'Compact the conversation to free up context',
handler: (context, ref, {required sessionId, required arg}) async {
final meta = ref.read(sessionMetaProvider(sessionId));
if (meta == null) {
ref.status.warning(
'Not available for this session',
source: StatusSources.agent,
sessionId: sessionId,
);
return;
}
ref
.read(storeControllerProvider.notifier)
.sendSessionAction(sessionId, 'compact');
ref.status.info(
'Compact requested',
source: StatusSources.agent,
sessionId: sessionId,
);
},
),
ClientCommand(
name: 'thinking',
description: 'Set the agent thinking level',
handler: (context, ref, {required sessionId, required arg}) async {
// Resolved before the first await: `ref` throws once its widget is
// unmounted, and the record must survive the thing that reported to it.
final status = ref.status;
final level = await _pickThinkingLevel(context);
if (level == null || !context.mounted) return;
ref
.read(storeControllerProvider.notifier)
.sendSessionAction(sessionId, 'thinking', args: {'level': level});
status.info(
'Thinking level: $level',
source: StatusSources.agent,
sessionId: sessionId,
);
},
),
ClientCommand(
name: 'model',
description: 'Switch the agent model',
handler: (context, ref, {required sessionId, required arg}) async {
// Resolved before the first await: `ref` throws once its widget is
// unmounted, and the record must survive the thing that reported to it.
final status = ref.status;
final meta = ref.read(sessionMetaProvider(sessionId));
final models = meta?.models ?? const [];
if (models.isEmpty) {
status.warning(
'No models available for this session',
source: StatusSources.agent,
sessionId: sessionId,
);
return;
}
final picked = await _pickModel(context, models, meta?.model);
if (picked == null || !context.mounted) return;
// Re-read after the picker await: the active model may have changed
// (e.g. TUI-side switch) while the sheet was open.
final current = ref.read(sessionMetaProvider(sessionId))?.model;
if (current != null &&
current.provider == picked.provider &&
current.id == picked.id) {
return;
}
ref
.read(storeControllerProvider.notifier)
.sendSessionAction(
sessionId,
'model',
args: {'provider': picked.provider, 'id': picked.id},
);
status.progress(
'Switching to ${picked.name}…',
source: StatusSources.agent,
sessionId: sessionId,
);
},
),
ClientCommand(
name: 'session',
description: 'Show this session’s identity, or /session id to copy its id',
handler: (context, ref, {required sessionId, required arg}) async {
// WHY a CLIENT command and not sent to the agent (D7): pi's own `/session`
// is an agent command, so in makit's composer it would fall through to
// `store.sendMessage` and — mid-turn — land in the server's pending queue,
// executing only after the turn it was meant to help you hand off.
// Intercepting it here answers at 100% of a turn. This handler returning
// (via `handleClientCommand` matching) is the fix for that bug.
//
// Resolved before any await (SPEC-48 D3, enforced by
// `test/status/status_lifetime_test.dart`): `ref` dies with its widget.
final status = ref.status;
// `/session id` copies ONLY the bare agent session id (D6). The panel's
// `Copy all` is the "give me everything" job; this is "give me the id", so
// it must not emit the whole label:value payload.
if (arg == 'id') {
final id = ref.read(sessionIdentityProvider(sessionId)).agentSessionId;
if (id == null) {
// Say why rather than copying an empty string: a draft (or a back end
// with no native session concept) has no id to hand off yet.
status.warning(
'No agent session id yet',
source: StatusSources.session,
sessionId: sessionId,
);
return;
}
// A clipboard write can throw for real (another process holds it on
// Windows; the host denies it). Unreported, the user gets neither the id
// nor a reason — so the write is waited on, and only a write that landed
// is allowed to claim success. Same contract as the panel's `Copy all`.
try {
await Clipboard.setData(ClipboardData(text: id));
} catch (e) {
status.failure(
'Could not copy session id',
error: e,
source: StatusSources.session,
sessionId: sessionId,
);
return;
}
status.info(
'Session id copied',
source: StatusSources.session,
detail: id,
sessionId: sessionId,
);
return;
}
// Bare `/session` opens the panel. Presented as a bottom sheet
// (`desktop: false`) like the other client commands (`/model`,
// `/thinking`): the invocation comes from the composer, where a sheet is
// the established surface. `sessionId` is passed so the open panel watches
// and fills in live (D19).
if (!context.mounted) return;
await showSessionIdentity(
context: context,
desktop: false,
sessionId: sessionId,
);
},
),
ClientCommand(
name: 'name',
description: 'Rename this session (shown in the session list)',
handler: (context, ref, {required sessionId, required arg}) async {
// Resolved before the first await: `ref` throws once its widget is
// unmounted, and the record must survive the thing that reported to it.
final status = ref.status;
final current = ref.read(sessionsProvider).byId(sessionId)?.title ?? '';
final title = arg.isNotEmpty
? arg
: await _promptSessionName(context, initial: current);
if (title == null || title.isEmpty || !context.mounted) return;
ref
.read(storeControllerProvider.notifier)
.sendSessionAction(sessionId, 'name', args: {'name': title});
status.success(
'Renamed to “$title”',
source: StatusSources.session,
sessionId: sessionId,
);
},
),
];
/// Prompt for a session name via a simple text dialog. Resolves with the
/// trimmed name, or null if dismissed/empty.
Future<String?> _promptSessionName(
BuildContext context, {
String initial = '',
}) async {
final controller = TextEditingController(text: initial);
controller.selection = TextSelection(
baseOffset: 0,
extentOffset: controller.text.length,
);
final result = await showDialog<String>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Rename session'),
content: StatefulBuilder(
builder: (ctx, setState) => TextField(
controller: controller,
autofocus: true,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
hintText: 'Session name',
suffixIcon: controller.text.isEmpty
? null
: IconButton(
icon: const Icon(PhosphorIconsLight.x),
tooltip: 'Clear',
onPressed: () {
controller.clear();
setState(() {});
},
),
),
onChanged: (_) => setState(() {}),
onSubmitted: (v) => Navigator.pop(ctx, v.trim()),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(ctx, controller.text.trim()),
child: const Text('Rename'),
),
],
),
);
controller.dispose();
return result;
}
/// pi's thinking levels, low → high. `off` disables reasoning. Shared with the
/// composer's [ThinkingSignal] indicator so the bar count matches the picker.
const thinkingLevels = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh'];
/// Present the thinking-level options in a modal sheet; resolves with the
/// chosen level or null if dismissed.
Future<String?> _pickThinkingLevel(BuildContext context) {
return showModalBottomSheet<String>(
context: context,
showDragHandle: true,
builder: (sheetContext) => SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const SheetHeader(title: 'Thinking level'),
for (final level in thinkingLevels)
ListTile(
title: Text(level),
onTap: () => Navigator.pop(sheetContext, level),
),
],
),
),
);
}
/// Present the selectable models in a modal sheet, marking [current]. Resolves
/// with the chosen model or null if dismissed. The sheet is capped at ~85% of
/// the screen height and offers a search button to filter by name/provider.
Future<ModelInfo?> _pickModel(
BuildContext context,
List<ModelInfo> models,
ModelInfo? current,
) {
bool matches(ModelInfo m, String q) {
final ql = q.toLowerCase();
return m.name.toLowerCase().contains(ql) ||
m.provider.toLowerCase().contains(ql);
}
return showSearchableListSheet<ModelInfo>(
context: context,
title: 'Model',
items: models,
matches: matches,
tileBuilder: (ctx, m) => ListTile(
title: Text(m.name),
subtitle: Text(m.provider),
trailing:
(current != null &&
current.provider == m.provider &&
current.id == m.id)
? const Icon(PhosphorIconsLight.check)
: null,
onTap: () => Navigator.of(ctx).pop(m),
),
);
}