@softov/ahpc 0.2.0 → 0.4.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.
Files changed (73) hide show
  1. package/README.md +37 -6
  2. package/dist/src/ahp/fake.js +61 -14
  3. package/dist/src/ahp/live.d.ts +7 -0
  4. package/dist/src/ahp/live.js +156 -43
  5. package/dist/src/ahp/publish.js +13 -0
  6. package/dist/src/ahp/types.d.ts +20 -0
  7. package/dist/src/app.js +34 -7
  8. package/dist/src/blocks.d.ts +4 -74
  9. package/dist/src/blocks.js +10 -48
  10. package/dist/src/cli/main.d.ts +1 -1
  11. package/dist/src/cli/main.js +109 -6
  12. package/dist/src/connect.d.ts +2 -0
  13. package/dist/src/connect.js +1 -0
  14. package/dist/src/control.d.ts +6 -0
  15. package/dist/src/control.js +140 -10
  16. package/dist/src/flags.js +4 -1
  17. package/dist/src/links.d.ts +54 -0
  18. package/dist/src/links.js +120 -0
  19. package/dist/src/main.js +14 -2
  20. package/dist/src/mcp/http.d.ts +10 -0
  21. package/dist/src/mcp/http.js +110 -3
  22. package/dist/src/mcp/serve.d.ts +47 -9
  23. package/dist/src/mcp/serve.js +65 -14
  24. package/dist/src/mcp/stdio.d.ts +1 -0
  25. package/dist/src/mcp/stdio.js +7 -1
  26. package/dist/src/mcp/tools.d.ts +37 -2
  27. package/dist/src/mcp/tools.js +439 -2
  28. package/dist/src/resources.d.ts +13 -0
  29. package/dist/src/resources.js +46 -0
  30. package/dist/src/screens.js +95 -50
  31. package/dist/src/state.d.ts +42 -0
  32. package/dist/src/state.js +80 -1
  33. package/dist/src/tui.d.ts +3 -1
  34. package/dist/src/tui.js +29 -4
  35. package/dist/src/version.d.ts +2 -0
  36. package/dist/src/version.js +38 -0
  37. package/dist/src/view/creature.d.ts +0 -12
  38. package/dist/src/view/creature.js +0 -20
  39. package/dist/src/view/wire.d.ts +36 -0
  40. package/dist/src/view/wire.js +196 -0
  41. package/dist/src/wait.d.ts +11 -0
  42. package/dist/src/wait.js +15 -2
  43. package/dist/src/wire.d.ts +70 -0
  44. package/dist/src/wire.js +194 -0
  45. package/dist/src/wiretui.d.ts +22 -0
  46. package/dist/src/wiretui.js +69 -0
  47. package/package.json +6 -5
  48. package/dist/src/diff.d.ts +0 -44
  49. package/dist/src/diff.js +0 -111
  50. package/dist/src/view/bubble.d.ts +0 -75
  51. package/dist/src/view/bubble.js +0 -86
  52. package/dist/src/view/composer.d.ts +0 -64
  53. package/dist/src/view/composer.js +0 -192
  54. package/dist/src/view/controls.d.ts +0 -44
  55. package/dist/src/view/controls.js +0 -49
  56. package/dist/src/view/details.d.ts +0 -65
  57. package/dist/src/view/details.js +0 -65
  58. package/dist/src/view/filediff.d.ts +0 -29
  59. package/dist/src/view/filediff.js +0 -24
  60. package/dist/src/view/hitl.d.ts +0 -43
  61. package/dist/src/view/hitl.js +0 -171
  62. package/dist/src/view/icons.d.ts +0 -13
  63. package/dist/src/view/icons.js +0 -71
  64. package/dist/src/view/picker.d.ts +0 -42
  65. package/dist/src/view/picker.js +0 -71
  66. package/dist/src/view/sessionhead.d.ts +0 -41
  67. package/dist/src/view/sessionhead.js +0 -60
  68. package/dist/src/view/sessions.d.ts +0 -34
  69. package/dist/src/view/sessions.js +0 -61
  70. package/dist/src/view/toolcall.d.ts +0 -27
  71. package/dist/src/view/toolcall.js +0 -48
  72. package/dist/src/view/transcript.d.ts +0 -50
  73. package/dist/src/view/transcript.js +0 -60
@@ -20,6 +20,8 @@
20
20
  */
21
21
  import { SessionFlag } from '../ahp/types.js';
22
22
  import { spoken, turn as runTurn, until } from '../wait.js';
23
+ /** Every group there is, for `--mcp-tools` to name in its help. */
24
+ export const GROUPS = ['resources', 'terminals', 'automations', 'changes'];
23
25
  const text = (value) => (typeof value === 'string' ? value : '');
24
26
  const uriOf = (input) => {
25
27
  const found = text(input.session);
@@ -42,6 +44,19 @@ const said = (one) => ({
42
44
  : null))
43
45
  .filter((one_) => one_ !== null),
44
46
  });
47
+ /** The URI a resource tool was given, which is a `file://` on the host rather than a local path. */
48
+ const pathOf = (input, key = 'path') => {
49
+ const found = text(input[key]);
50
+ if (found === '')
51
+ throw new Error(`No ${key} was given. Resource tools take a file:// URI on the host, not a path on this machine.`);
52
+ return found;
53
+ };
54
+ /** A host half this connection may not have, or the reason it does not. */
55
+ function has(part, what) {
56
+ if (part === undefined)
57
+ throw new Error(`This host serves no ${what}. A host is given one, and this one was not.`);
58
+ return part;
59
+ }
45
60
  export const TOOLS = [
46
61
  {
47
62
  name: 'list_sessions',
@@ -191,11 +206,23 @@ export const TOOLS = [
191
206
  required: ['session', 'text'],
192
207
  additionalProperties: false,
193
208
  },
194
- run: async (host, input) => {
209
+ run: async (host, input, report) => {
195
210
  const model = text(input.model);
196
211
  const answer = await runTurn(host, uriOf(input), text(input.text), {
197
212
  ...(model === '' ? {} : { model: { id: model } }),
198
213
  ...(typeof input.timeoutSeconds === 'number' ? { timeoutSeconds: input.timeoutSeconds } : {}),
214
+ /*
215
+ * What a caller watching this is told while it waits.
216
+ *
217
+ * The tool a session stopped on, not the reply as it is typed: MCP's
218
+ * progress carries a human-readable line and has no shape for partial
219
+ * result content, so the text still arrives whole at the end. What
220
+ * this fixes is an agent that looked frozen for a minute.
221
+ */
222
+ ...(report === undefined ? {} : {
223
+ onStep: (call) => report(call.name),
224
+ onWaiting: (call) => report(`waiting on ${call.name}`),
225
+ }),
199
226
  });
200
227
  // Not an error: the turn is still running and the session is still
201
228
  // there, which is a different thing to tell a caller than a failure.
@@ -323,6 +350,416 @@ export const TOOLS = [
323
350
  return { answered: true };
324
351
  },
325
352
  },
353
+ /*
354
+ * The files the host serves, which is the `resources` group.
355
+ *
356
+ * Every one of these takes a `file://` URI on the *host*, not a path here -
357
+ * the host may be on another machine, and a tool that quietly resolved a
358
+ * relative path against this process's directory would be wrong in a way
359
+ * nobody notices until it writes somewhere.
360
+ */
361
+ {
362
+ name: 'list_directory',
363
+ group: 'resources',
364
+ title: 'List a directory',
365
+ description: 'What is in a directory the host serves. Takes a file:// URI on the host.',
366
+ readOnly: true,
367
+ input: {
368
+ type: 'object',
369
+ properties: { path: { type: 'string', description: 'A file:// URI of a directory on the host.' } },
370
+ required: ['path'],
371
+ additionalProperties: false,
372
+ },
373
+ run: async (host, input) => has(host.resourceList, 'filesystem')(pathOf(input)),
374
+ },
375
+ {
376
+ name: 'read_file',
377
+ group: 'resources',
378
+ title: 'Read a file',
379
+ description: 'The contents of a file the host serves. Text comes back as text; anything the host sends as bytes comes back base64 with the encoding said.',
380
+ readOnly: true,
381
+ input: {
382
+ type: 'object',
383
+ properties: { path: { type: 'string', description: 'A file:// URI on the host.' } },
384
+ required: ['path'],
385
+ additionalProperties: false,
386
+ },
387
+ run: async (host, input) => has(host.resourceRead, 'filesystem')(pathOf(input)),
388
+ },
389
+ {
390
+ name: 'write_file',
391
+ group: 'resources',
392
+ title: 'Write a file',
393
+ description: 'Write a file on the host, refusing if it changed since it was read. Pass force to write over whatever is there now. A host that has not granted write access to that directory refuses this, and only a person at the host can grant it.',
394
+ readOnly: false,
395
+ input: {
396
+ type: 'object',
397
+ properties: {
398
+ path: { type: 'string', description: 'A file:// URI on the host.' },
399
+ content: { type: 'string', description: 'The whole new contents. This replaces the file.' },
400
+ createOnly: { type: 'boolean', description: 'Refuse if the file already exists.' },
401
+ force: { type: 'boolean', description: 'Write even if the file changed since it was last read.' },
402
+ },
403
+ required: ['path', 'content'],
404
+ additionalProperties: false,
405
+ },
406
+ run: async (host, input) => {
407
+ const write = has(host.resourceWrite, 'writable filesystem');
408
+ const uri = pathOf(input);
409
+ /*
410
+ * The etag the file has now, unless told not to.
411
+ *
412
+ * The same guard `ahpc resource write` has, and it matters more here: a
413
+ * model reads a file, thinks about it, and writes it back, and the whole
414
+ * of that is a read-modify-write with a person editing in between. A
415
+ * write with no `ifMatch` lands on whatever is there and loses their edit.
416
+ */
417
+ let ifMatch;
418
+ if (input.force !== true && host.resourceResolve) {
419
+ try {
420
+ ifMatch = (await host.resourceResolve(uri)).etag;
421
+ }
422
+ catch { /* not there yet, so there is nothing to have changed */ }
423
+ }
424
+ await write(uri, text(input.content), {
425
+ ...(input.createOnly === true ? { createOnly: true } : {}),
426
+ ...(ifMatch === undefined ? {} : { ifMatch }),
427
+ });
428
+ return { written: uri };
429
+ },
430
+ },
431
+ {
432
+ name: 'make_directory',
433
+ group: 'resources',
434
+ title: 'Make a directory',
435
+ description: 'Create a directory on the host.',
436
+ readOnly: false,
437
+ input: {
438
+ type: 'object',
439
+ properties: { path: { type: 'string' } },
440
+ required: ['path'],
441
+ additionalProperties: false,
442
+ },
443
+ run: async (host, input) => {
444
+ await has(host.resourceMkdir, 'writable filesystem')(pathOf(input));
445
+ return { made: pathOf(input) };
446
+ },
447
+ },
448
+ {
449
+ name: 'delete_path',
450
+ group: 'resources',
451
+ title: 'Delete a file or directory',
452
+ description: 'Remove something on the host. A directory needs recursive.',
453
+ readOnly: false,
454
+ input: {
455
+ type: 'object',
456
+ properties: {
457
+ path: { type: 'string' },
458
+ recursive: { type: 'boolean', description: 'Needed to remove a directory that is not empty.' },
459
+ },
460
+ required: ['path'],
461
+ additionalProperties: false,
462
+ },
463
+ run: async (host, input) => {
464
+ await has(host.resourceDelete, 'writable filesystem')(pathOf(input), {
465
+ ...(input.recursive === true ? { recursive: true } : {}),
466
+ });
467
+ return { deleted: pathOf(input) };
468
+ },
469
+ },
470
+ {
471
+ name: 'move_path',
472
+ group: 'resources',
473
+ title: 'Move or rename',
474
+ description: 'Move something on the host, which is also how it is renamed.',
475
+ readOnly: false,
476
+ input: {
477
+ type: 'object',
478
+ properties: {
479
+ from: { type: 'string' },
480
+ to: { type: 'string' },
481
+ failIfExists: { type: 'boolean', description: 'Refuse rather than write over something already at the destination.' },
482
+ },
483
+ required: ['from', 'to'],
484
+ additionalProperties: false,
485
+ },
486
+ run: async (host, input) => {
487
+ await has(host.resourceMove, 'writable filesystem')(pathOf(input, 'from'), pathOf(input, 'to'), {
488
+ ...(input.failIfExists === true ? { failIfExists: true } : {}),
489
+ });
490
+ return { moved: pathOf(input, 'to') };
491
+ },
492
+ },
493
+ {
494
+ name: 'copy_path',
495
+ group: 'resources',
496
+ title: 'Copy',
497
+ description: 'Copy something on the host.',
498
+ readOnly: false,
499
+ input: {
500
+ type: 'object',
501
+ properties: {
502
+ from: { type: 'string' },
503
+ to: { type: 'string' },
504
+ failIfExists: { type: 'boolean', description: 'Refuse rather than write over something already at the destination.' },
505
+ },
506
+ required: ['from', 'to'],
507
+ additionalProperties: false,
508
+ },
509
+ run: async (host, input) => {
510
+ await has(host.resourceCopy, 'writable filesystem')(pathOf(input, 'from'), pathOf(input, 'to'), {
511
+ ...(input.failIfExists === true ? { failIfExists: true } : {}),
512
+ });
513
+ return { copied: pathOf(input, 'to') };
514
+ },
515
+ },
516
+ /* The host's terminals, which is the `terminals` group. */
517
+ {
518
+ name: 'list_terminals',
519
+ group: 'terminals',
520
+ title: 'List terminals',
521
+ description: 'The terminals the host is running, with the URI each other terminal tool takes.',
522
+ readOnly: true,
523
+ input: { type: 'object', properties: {}, additionalProperties: false },
524
+ run: async (host) => (await host.terminals()).map((row) => ({
525
+ terminal: row.resource,
526
+ title: row.title,
527
+ ...(row.exitCode === undefined ? {} : { exitCode: row.exitCode }),
528
+ })),
529
+ },
530
+ {
531
+ name: 'new_terminal',
532
+ group: 'terminals',
533
+ title: 'Open a terminal',
534
+ description: 'Start a terminal on the host and return its URI.',
535
+ readOnly: false,
536
+ input: {
537
+ type: 'object',
538
+ properties: {
539
+ workingDirectory: { type: 'string', description: 'An absolute path on the host.' },
540
+ name: { type: 'string' },
541
+ },
542
+ additionalProperties: false,
543
+ },
544
+ run: async (host, input) => ({
545
+ terminal: await host.createTerminal({
546
+ ...(text(input.workingDirectory) === '' ? {} : { cwd: text(input.workingDirectory) }),
547
+ ...(text(input.name) === '' ? {} : { name: text(input.name) }),
548
+ }),
549
+ }),
550
+ },
551
+ {
552
+ name: 'send_to_terminal',
553
+ group: 'terminals',
554
+ title: 'Type into a terminal',
555
+ description: 'Send a line to a terminal. A newline is added unless newline is false, because a shell runs lines rather than strings.',
556
+ readOnly: false,
557
+ input: {
558
+ type: 'object',
559
+ properties: {
560
+ terminal: { type: 'string', description: 'A terminal URI from list_terminals or new_terminal.' },
561
+ text: { type: 'string' },
562
+ newline: { type: 'boolean', description: 'Whether to end it with a newline. True unless said otherwise.' },
563
+ },
564
+ required: ['terminal', 'text'],
565
+ additionalProperties: false,
566
+ },
567
+ run: async (host, input) => {
568
+ host.writeTerminal(pathOf(input, 'terminal'), `${text(input.text)}${input.newline === false ? '' : '\n'}`);
569
+ return { sent: true };
570
+ },
571
+ },
572
+ {
573
+ name: 'read_terminal',
574
+ group: 'terminals',
575
+ title: 'Read a terminal',
576
+ description: 'What a terminal has written so far. With waitSeconds it keeps reading until the process exits or that long passes, which is how a command that was just sent is waited on.',
577
+ readOnly: true,
578
+ input: {
579
+ type: 'object',
580
+ properties: {
581
+ terminal: { type: 'string' },
582
+ waitSeconds: { type: 'number', description: 'Wait this long for the process to exit before answering. Zero, the default, answers with what is there now.' },
583
+ },
584
+ required: ['terminal'],
585
+ additionalProperties: false,
586
+ },
587
+ run: async (host, input, report) => {
588
+ const uri = pathOf(input, 'terminal');
589
+ const seconds = typeof input.waitSeconds === 'number' && input.waitSeconds > 0 ? input.waitSeconds : 0;
590
+ return new Promise((done) => {
591
+ let last;
592
+ const stop = () => {
593
+ clearTimeout(timer);
594
+ handle.close();
595
+ done({
596
+ terminal: uri,
597
+ title: last?.title ?? '',
598
+ output: last?.output ?? '',
599
+ ...(last?.exitCode === undefined ? { running: true } : { exitCode: last.exitCode }),
600
+ });
601
+ };
602
+ const timer = setTimeout(stop, Math.max(0, seconds) * 1000);
603
+ timer.unref?.();
604
+ const handle = host.watchTerminal(uri, (state) => {
605
+ last = state;
606
+ report?.(state.exitCode === undefined ? `${state.output.length} bytes` : `exited ${state.exitCode}`);
607
+ // The first state carries the whole buffer, so a caller that is not
608
+ // waiting has its answer as soon as one arrives.
609
+ if (seconds === 0 || state.exitCode !== undefined)
610
+ stop();
611
+ });
612
+ });
613
+ },
614
+ },
615
+ {
616
+ name: 'dispose_terminal',
617
+ group: 'terminals',
618
+ title: 'Close a terminal',
619
+ description: 'Close a terminal on the host.',
620
+ readOnly: false,
621
+ input: {
622
+ type: 'object',
623
+ properties: { terminal: { type: 'string' } },
624
+ required: ['terminal'],
625
+ additionalProperties: false,
626
+ },
627
+ run: async (host, input) => {
628
+ await host.disposeTerminal(pathOf(input, 'terminal'));
629
+ return { disposed: true };
630
+ },
631
+ },
632
+ /* Scheduled work, which is the `automations` group. */
633
+ {
634
+ name: 'list_automations',
635
+ group: 'automations',
636
+ title: 'List automations',
637
+ description: 'What the host runs on a schedule, whether each is on, and when it next fires.',
638
+ readOnly: true,
639
+ input: { type: 'object', properties: {}, additionalProperties: false },
640
+ run: async (host) => (await has(host.automations, 'automations')()).map((one) => ({
641
+ automation: one.resource,
642
+ title: one.title,
643
+ enabled: one.enabled,
644
+ ...(one.schedule === undefined ? {} : { schedule: one.schedule.expression, timeZone: one.schedule.timeZone }),
645
+ ...(one.nextRunAt === undefined ? {} : { nextRunAt: one.nextRunAt }),
646
+ operations: one.operations,
647
+ lastRuns: one.runs.slice(0, 5),
648
+ })),
649
+ },
650
+ {
651
+ name: 'run_automation',
652
+ group: 'automations',
653
+ title: 'Run an automation',
654
+ description: 'Fire an automation now, without waiting for its schedule. Answers once the host has taken it, not once it has finished.',
655
+ readOnly: false,
656
+ input: {
657
+ type: 'object',
658
+ properties: { automation: { type: 'string', description: 'An automation URI from list_automations.' } },
659
+ required: ['automation'],
660
+ additionalProperties: false,
661
+ },
662
+ run: async (host, input) => {
663
+ await has(host.runAutomation, 'automations')(pathOf(input, 'automation'));
664
+ return { started: true };
665
+ },
666
+ },
667
+ {
668
+ name: 'set_automation_enabled',
669
+ group: 'automations',
670
+ title: 'Turn an automation on or off',
671
+ description: 'Stop an automation firing, or start it again. The definition stays either way.',
672
+ readOnly: false,
673
+ input: {
674
+ type: 'object',
675
+ properties: { automation: { type: 'string' }, enabled: { type: 'boolean' } },
676
+ required: ['automation', 'enabled'],
677
+ additionalProperties: false,
678
+ },
679
+ run: async (host, input) => {
680
+ await has(host.setAutomationEnabled, 'automations')(pathOf(input, 'automation'), input.enabled === true);
681
+ return { enabled: input.enabled === true };
682
+ },
683
+ },
684
+ {
685
+ name: 'remove_automation',
686
+ group: 'automations',
687
+ title: 'Remove an automation',
688
+ description: 'Delete an automation from the host. Use set_automation_enabled to stop one without losing it.',
689
+ readOnly: false,
690
+ input: {
691
+ type: 'object',
692
+ properties: { automation: { type: 'string' } },
693
+ required: ['automation'],
694
+ additionalProperties: false,
695
+ },
696
+ run: async (host, input) => {
697
+ await has(host.removeAutomation, 'automations')(pathOf(input, 'automation'));
698
+ return { removed: true };
699
+ },
700
+ },
701
+ /* What a session changed, which is the `changes` group. */
702
+ {
703
+ name: 'list_changesets',
704
+ group: 'changes',
705
+ title: 'List changesets',
706
+ description: 'The changesets a session offers - what the conversation changed, what one turn changed, what the working tree has. Each is a URI show_changes takes.',
707
+ readOnly: true,
708
+ input: {
709
+ type: 'object',
710
+ properties: { session: { type: 'string' } },
711
+ required: ['session'],
712
+ additionalProperties: false,
713
+ },
714
+ run: async (host, input) => (await has(host.changesets, 'changesets')(uriOf(input))).map((scope) => ({
715
+ changeset: scope.uriTemplate,
716
+ label: scope.label,
717
+ ...(scope.description === undefined ? {} : { description: scope.description }),
718
+ // What is still to be filled in. A template with these left in it is not
719
+ // a URI yet, and saying so is better than the host refusing it later.
720
+ variables: scope.variables,
721
+ })),
722
+ },
723
+ {
724
+ name: 'show_changes',
725
+ group: 'changes',
726
+ title: 'Show a changeset',
727
+ description: 'The files in a changeset and how much each changed. The contents are not here: a changeset of two hundred files is a list worth having and megabytes that are not. Read one with read_file.',
728
+ readOnly: true,
729
+ input: {
730
+ type: 'object',
731
+ properties: {
732
+ session: { type: 'string' },
733
+ changeset: { type: 'string', description: 'A changeset URI from list_changesets. The session\'s own, if omitted.' },
734
+ },
735
+ required: ['session'],
736
+ additionalProperties: false,
737
+ },
738
+ run: async (host, input) => {
739
+ const target = text(input.changeset);
740
+ const found = await host.changes(uriOf(input), target === '' ? undefined : target);
741
+ return {
742
+ status: found.status,
743
+ files: found.files.map((file) => ({
744
+ uri: file.uri,
745
+ added: file.diff.added,
746
+ removed: file.diff.removed,
747
+ ...(file.before === undefined ? { created: true } : {}),
748
+ ...(file.after === undefined ? { deleted: true } : {}),
749
+ })),
750
+ operations: (found.operations ?? []).map((op) => op.id),
751
+ };
752
+ },
753
+ },
326
754
  ];
327
- /** One tool by the name a caller used, or nothing. */
755
+ /**
756
+ * One tool by the name a caller used, or nothing.
757
+ *
758
+ * Over the whole table, including groups nobody turned on - whether a tool
759
+ * exists and whether this server serves it are different questions, and
760
+ * `served` answers the second. Telling somebody the tool is in a group they
761
+ * did not ask for is a better answer than telling them it does not exist.
762
+ */
328
763
  export const named = (name) => TOOLS.find((one) => one.name === name);
764
+ /** The tools a server started with these groups serves. */
765
+ export const served = (groups = []) => TOOLS.filter((one) => one.group === undefined || groups.includes(one.group));
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The host's filesystem, as a textui resource provider.
3
+ *
4
+ * textui's file picker, its search and its viewers read the resource
5
+ * registry and never `node:fs` - which is the reason they can be pointed at
6
+ * a filesystem on another machine. This is that pointing: `file:` on this
7
+ * client is the host's disk, reached through the protocol's `resource*`
8
+ * requests, so a folder picked in a dialog is a folder the host can start a
9
+ * session in. Read-only here; what writes goes through `ahpc resource`.
10
+ */
11
+ import type { ResourceProvider } from '@textui/core';
12
+ import type { HostConnection } from './ahp/connection.js';
13
+ export declare function hostResources(host: HostConnection): ResourceProvider;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The host's filesystem, as a textui resource provider.
3
+ *
4
+ * textui's file picker, its search and its viewers read the resource
5
+ * registry and never `node:fs` - which is the reason they can be pointed at
6
+ * a filesystem on another machine. This is that pointing: `file:` on this
7
+ * client is the host's disk, reached through the protocol's `resource*`
8
+ * requests, so a folder picked in a dialog is a folder the host can start a
9
+ * session in. Read-only here; what writes goes through `ahpc resource`.
10
+ */
11
+ const nameOf = (uri) => decodeURIComponent(uri.replace(/\/+$/, '').split('/').pop() ?? '');
12
+ export function hostResources(host) {
13
+ const resource = (uri, kind, size) => ({
14
+ uri,
15
+ kind: kind === 'directory' ? 'directory' : 'file',
16
+ metadata: { name: nameOf(uri), ...(size === undefined ? {} : { size }), readonly: true },
17
+ capabilities: kind === 'directory' ? ['list'] : ['read'],
18
+ });
19
+ return {
20
+ scheme: 'file',
21
+ // A host without the family answers -32601, which is the same answer as
22
+ // "nothing there" to something asking whether it can list a folder.
23
+ stat: async (uri) => {
24
+ if (!host.resourceResolve)
25
+ return null;
26
+ try {
27
+ const found = await host.resourceResolve(uri);
28
+ return resource(found.uri, found.type, found.size);
29
+ }
30
+ catch {
31
+ return null;
32
+ }
33
+ },
34
+ list: async (uri) => {
35
+ if (!host.resourceList)
36
+ return [];
37
+ return (await host.resourceList(uri)).map((entry) => resource(entry.uri, entry.kind, entry.size));
38
+ },
39
+ read: async (uri) => {
40
+ if (!host.resourceRead)
41
+ throw new Error('This host does not serve files.');
42
+ const found = await host.resourceRead(uri);
43
+ return found.encoding === 'base64' ? Uint8Array.from(Buffer.from(found.data, 'base64')) : found.data;
44
+ },
45
+ };
46
+ }