@cmodjs/core 6.3.0 → 8.0.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 (138) hide show
  1. package/docs/commands.md +123 -65
  2. package/docs/dependencies.md +189 -4
  3. package/docs/index.md +56 -12
  4. package/docs/install-steps.md +20 -17
  5. package/docs/jobs.md +43 -16
  6. package/docs/mod.md +43 -27
  7. package/docs/options.md +2 -2
  8. package/docs/session.md +58 -32
  9. package/docs/testing.md +88 -24
  10. package/docs/ui.md +128 -58
  11. package/installer.d.ts +2 -2
  12. package/jobs/context.js +1 -1
  13. package/jobs/program.d.ts +4 -1
  14. package/jobs/program.d.ts.map +1 -1
  15. package/jobs/program.js +79 -20
  16. package/mod.d.ts +59 -9
  17. package/mod.d.ts.map +1 -1
  18. package/mod.js +5 -3
  19. package/package.json +1 -1
  20. package/records.d.ts +15 -2
  21. package/records.d.ts.map +1 -1
  22. package/records.js +24 -10
  23. package/register.d.ts.map +1 -1
  24. package/register.js +7 -4
  25. package/rpc.d.ts +7 -0
  26. package/rpc.d.ts.map +1 -0
  27. package/rpc.js +59 -0
  28. package/runtime/bound.d.ts +1 -0
  29. package/runtime/bound.d.ts.map +1 -1
  30. package/runtime/bound.js +10 -0
  31. package/runtime/bun.d.ts +20 -0
  32. package/runtime/bun.d.ts.map +1 -0
  33. package/runtime/bun.js +20 -0
  34. package/runtime/claude.d.ts +1 -0
  35. package/runtime/claude.d.ts.map +1 -1
  36. package/runtime/conversations.d.ts +12 -1
  37. package/runtime/conversations.d.ts.map +1 -1
  38. package/runtime/conversations.js +472 -362
  39. package/runtime/deadline.d.ts +4 -3
  40. package/runtime/deadline.d.ts.map +1 -1
  41. package/runtime/deadline.js +10 -4
  42. package/runtime/engine.d.ts +5 -4
  43. package/runtime/engine.d.ts.map +1 -1
  44. package/runtime/installer.d.ts +3 -3
  45. package/runtime/installer.d.ts.map +1 -1
  46. package/runtime/installer.js +10 -9
  47. package/runtime/interfaces.d.ts +262 -0
  48. package/runtime/interfaces.d.ts.map +1 -0
  49. package/runtime/interfaces.js +935 -0
  50. package/runtime/lifecycle.d.ts +3 -3
  51. package/runtime/lifecycle.d.ts.map +1 -1
  52. package/runtime/lifecycle.js +83 -52
  53. package/runtime/options.d.ts.map +1 -1
  54. package/runtime/options.js +2 -6
  55. package/runtime/parts.d.ts +64 -1
  56. package/runtime/parts.d.ts.map +1 -1
  57. package/runtime/parts.js +311 -4
  58. package/runtime/programs.d.ts +1 -0
  59. package/runtime/programs.d.ts.map +1 -1
  60. package/runtime/programs.js +5 -3
  61. package/runtime/rpc.d.ts +111 -0
  62. package/runtime/rpc.d.ts.map +1 -0
  63. package/runtime/rpc.js +387 -0
  64. package/runtime/sessions.d.ts +6 -4
  65. package/runtime/sessions.d.ts.map +1 -1
  66. package/runtime/sessions.js +59 -80
  67. package/runtime/toaster.js +1 -1
  68. package/runtime/ui.d.ts +23 -10
  69. package/runtime/ui.d.ts.map +1 -1
  70. package/runtime/ui.js +63 -52
  71. package/runtime/web.d.ts +0 -73
  72. package/runtime/web.d.ts.map +1 -1
  73. package/runtime/web.js +9 -5
  74. package/runtime/widgets.d.ts +4 -2
  75. package/runtime/widgets.d.ts.map +1 -1
  76. package/runtime/widgets.js +17 -16
  77. package/session/conversation.d.ts +7 -1
  78. package/session/conversation.d.ts.map +1 -1
  79. package/session/session.d.ts +31 -7
  80. package/session/session.d.ts.map +1 -1
  81. package/session/session.js +1 -0
  82. package/src/installer.ts +2 -2
  83. package/src/jobs/context.ts +1 -1
  84. package/src/jobs/program.ts +76 -21
  85. package/src/mod.ts +51 -10
  86. package/src/records.ts +33 -9
  87. package/src/register.ts +7 -4
  88. package/src/rpc.ts +67 -0
  89. package/src/runtime/bound.ts +10 -0
  90. package/src/runtime/bun.ts +40 -0
  91. package/src/runtime/claude.ts +1 -0
  92. package/src/runtime/conversations.ts +470 -352
  93. package/src/runtime/deadline.ts +13 -6
  94. package/src/runtime/engine.ts +5 -4
  95. package/src/runtime/installer.ts +13 -12
  96. package/src/runtime/interfaces.ts +1161 -0
  97. package/src/runtime/lifecycle.ts +102 -59
  98. package/src/runtime/options.ts +2 -5
  99. package/src/runtime/parts.ts +364 -5
  100. package/src/runtime/programs.ts +6 -3
  101. package/src/runtime/rpc.ts +457 -0
  102. package/src/runtime/sessions.ts +63 -83
  103. package/src/runtime/toaster.ts +1 -1
  104. package/src/runtime/ui.ts +82 -61
  105. package/src/runtime/web.ts +10 -39
  106. package/src/runtime/widgets.ts +20 -18
  107. package/src/session/conversation.ts +7 -1
  108. package/src/session/session.ts +22 -7
  109. package/src/testing/fake-claude.ts +38 -7
  110. package/src/testing/fake-serve.ts +144 -0
  111. package/src/testing/fake-sessions.ts +21 -6
  112. package/src/testing.ts +156 -68
  113. package/src/ui/areas.ts +2 -2
  114. package/src/ui/elements.ts +21 -16
  115. package/src/ui/panes.ts +14 -7
  116. package/testing/fake-claude.d.ts +6 -0
  117. package/testing/fake-claude.d.ts.map +1 -1
  118. package/testing/fake-claude.js +34 -7
  119. package/testing/fake-serve.d.ts +39 -0
  120. package/testing/fake-serve.d.ts.map +1 -0
  121. package/testing/fake-serve.js +110 -0
  122. package/testing/fake-sessions.d.ts.map +1 -1
  123. package/testing/fake-sessions.js +22 -6
  124. package/testing.d.ts +21 -4
  125. package/testing.d.ts.map +1 -1
  126. package/testing.js +138 -56
  127. package/ui/areas.d.ts +3 -1
  128. package/ui/areas.d.ts.map +1 -1
  129. package/ui/elements.d.ts +18 -9
  130. package/ui/elements.d.ts.map +1 -1
  131. package/ui/elements.js +9 -8
  132. package/ui/panes.d.ts +10 -6
  133. package/ui/panes.d.ts.map +1 -1
  134. package/ui/panes.js +5 -3
  135. package/runtime/pages.d.ts +0 -32
  136. package/runtime/pages.d.ts.map +0 -1
  137. package/runtime/pages.js +0 -142
  138. package/src/runtime/pages.ts +0 -183
package/runtime/parts.js CHANGED
@@ -1,7 +1,11 @@
1
- import { elementOf } from '../ui/elements.js';
2
- import { messageOf } from '../utils/text.js';
3
- import { isStillInstalling } from './dependencies.js';
4
- import { partMethods } from './ui.js';
1
+ import { isObject } from '../records.js';
2
+ import { Dashboard, TurnEnd } from '../ui/areas.js';
3
+ import { elementData, elementOf, Text } from '../ui/elements.js';
4
+ import { listed, messageOf } from '../utils/text.js';
5
+ import { cmodPluginName, isNotInstalled, isStillInstalling } from './dependencies.js';
6
+ import { styleInterface } from './interfaces.js';
7
+ import { partMethods, stalePress } from './ui.js';
8
+ import { areaComponents, widgetsOf } from './widgets.js';
5
9
  const keptDrawings = 200;
6
10
  export function drawnParts({ claude, report, press }) {
7
11
  const drawn = new Map();
@@ -63,3 +67,306 @@ export function drawnParts({ claude, report, press }) {
63
67
  },
64
68
  };
65
69
  }
70
+ const askedWaitMs = 1000;
71
+ const keptUiDraws = 2;
72
+ export function providedBy(listing) {
73
+ if (!isObject(listing))
74
+ return [];
75
+ const named = Array.isArray(listing['provides']) ? listing['provides'].filter((each) => typeof each === 'string') : [];
76
+ return listing['stylesMarkdown'] === true && !named.includes(styleInterface) ? [...named, styleInterface] : named;
77
+ }
78
+ const listedIn = (input, field) => (isObject(input) && Array.isArray(input[field]) ? input[field].filter(isObject) : []);
79
+ const isNamed = (each) => typeof each['id'] === 'string' && typeof each['title'] === 'string';
80
+ export function viewsOf(input) {
81
+ return listedIn(input, 'pages').flatMap((each) => (isNamed(each) && (each['place'] === 'sidebar' || each['place'] === 'settings') ? [{ id: each.id, title: each.title, place: each['place'] }] : []));
82
+ }
83
+ export function pagesOf(input) {
84
+ return listedIn(input, 'pages').flatMap((each) => (isNamed(each) && each['place'] === undefined ? [{ id: each.id, title: each.title }] : []));
85
+ }
86
+ function partsOf(mod, listing) {
87
+ return { mod, widgets: widgetsOf(listing), views: viewsOf(listing), pages: pagesOf(listing) };
88
+ }
89
+ export function serveParts({ claude, router, report, tellRefusedPress, widgets, parts, tellStylers, tellUi, }) {
90
+ const declared = new Map();
91
+ const provided = new Map();
92
+ const providersOf = (name) => [...(provided.get(name) ?? [])];
93
+ const shownBy = new Map();
94
+ let inApps = new Map();
95
+ const tellShown = (mod) => {
96
+ const shown = { ...(shownBy.get(mod) ?? { open: [], shown: null, isFocused: false }), inApps: inApps.get(mod) ?? [] };
97
+ void claude.cmod.call({ to: mod, method: partMethods.shown, input: plain(shown) }).catch((error) => claude.ui.warn(`pages:shown:${mod}`, `cmod: ${mod} did not hear which of its views and pages show: ${messageOf(error)}`));
98
+ };
99
+ const asking = new Map();
100
+ const pressing = new Set();
101
+ let waiting = [];
102
+ let inputDepth = 0;
103
+ let applyTo = async () => undefined;
104
+ let openIn = async () => undefined;
105
+ let started = () => undefined;
106
+ const run = (mod, requests) => applyTo(mod, requests).catch((error) => report(`pages:${mod}`, `Could not show the view or page ${mod} asked for`, error));
107
+ const takeWaiting = async () => {
108
+ const taken = waiting;
109
+ waiting = [];
110
+ for (const { mod, requests } of taken)
111
+ await run(mod, requests);
112
+ };
113
+ const settle = async (earlier) => {
114
+ const own = () => [...pressing].filter((pending) => !earlier.has(pending));
115
+ for (let pending = own(); pending.length > 0; pending = own())
116
+ await Promise.allSettled(pending);
117
+ await takeWaiting();
118
+ };
119
+ const wrapped = async (next) => {
120
+ inputDepth += 1;
121
+ const earlier = new Set(pressing);
122
+ try {
123
+ const answer = await next();
124
+ await settle(earlier);
125
+ return answer;
126
+ }
127
+ finally {
128
+ inputDepth -= 1;
129
+ }
130
+ };
131
+ router.add('command.run', (e, next) => wrapped(async () => (waiting.length > 0 ? {} : next(e))));
132
+ router.add('ui.press', (e, next) => wrapped(() => next(e)));
133
+ const declare = (mod, listing) => {
134
+ const before = declared.get(mod);
135
+ const after = partsOf(mod, listing);
136
+ declared.set(mod, after);
137
+ for (const part of ['widgets', 'views', 'pages'])
138
+ if (JSON.stringify(before?.[part] ?? []) !== JSON.stringify(after[part]))
139
+ tellUi('Change', { part });
140
+ };
141
+ const ask = (mod) => {
142
+ const pending = asking.get(mod) ??
143
+ claude.cmod
144
+ .call({ to: mod, method: partMethods.list, input: null })
145
+ .then((listing) => declare(mod, listing), (error) => {
146
+ if (!isNotInstalled(mod, error) && !isStillInstalling(mod, error))
147
+ claude.ui.warn(`pages:list:${mod}`, `cmod: ${mod} did not list its views and pages: ${messageOf(error)}`);
148
+ })
149
+ .finally(() => asking.delete(mod));
150
+ asking.set(mod, pending);
151
+ return pending;
152
+ };
153
+ const provide = (mod, names) => {
154
+ const wasStyler = provided.get(styleInterface)?.has(mod) === true;
155
+ for (const [name, mods] of provided)
156
+ if (!names.includes(name))
157
+ mods.delete(mod);
158
+ for (const name of names)
159
+ provided.set(name, (provided.get(name) ?? new Set()).add(mod));
160
+ if (wasStyler || names.includes(styleInterface))
161
+ restyle(mod);
162
+ };
163
+ const restyle = (mod) => {
164
+ parts.forget(mod);
165
+ const listed = providersOf(styleInterface);
166
+ tellStylers(listed);
167
+ for (const told of declared.keys()) {
168
+ if (told === cmodPluginName)
169
+ continue;
170
+ void claude.cmod.call({ to: told, method: partMethods.stylers, input: listed }).catch((error) => {
171
+ if (!isStillInstalling(told, error))
172
+ claude.ui.warn(`pages:stylers:${told}`, `cmod: ${told} did not hear which mods style markdown: ${messageOf(error)}`);
173
+ });
174
+ }
175
+ };
176
+ const idsOf = (mod) => {
177
+ const known = declared.get(mod);
178
+ return [...(known?.widgets ?? []), ...(known?.views ?? []), ...(known?.pages ?? [])].map(({ id }) => id);
179
+ };
180
+ const isShownByApps = (mod, id) => {
181
+ const known = declared.get(mod);
182
+ return known?.views.some((each) => each.id === id && each.place === 'sidebar') === true || known?.pages.some((each) => each.id === id) === true;
183
+ };
184
+ const tellRequested = (mod, requests) => {
185
+ for (const request of requests) {
186
+ if ('open' in request && isShownByApps(mod, request.open))
187
+ tellUi('Open', { mod, id: request.open });
188
+ if ('close' in request && isShownByApps(mod, request.close))
189
+ tellUi('Close', { mod, id: request.close });
190
+ }
191
+ };
192
+ const update = (mod, { parts: listing, redraw, requests, isAsked, isStart }) => {
193
+ if (isStart === true)
194
+ started(mod);
195
+ if (listing !== undefined)
196
+ declare(mod, listing);
197
+ if (listing !== undefined)
198
+ provide(mod, providedBy(listing));
199
+ const isRedrawn = redraw === true || requests?.some((request) => 'open' in request) === true;
200
+ if (listing !== undefined || isRedrawn)
201
+ widgets.update(mod, { ...(listing === undefined ? {} : { parts: listing }), ...(isRedrawn ? { redraw: true } : {}) });
202
+ if (redraw === true)
203
+ for (const id of idsOf(mod))
204
+ tellUi('Redraw', { mod, id });
205
+ if (requests === undefined || requests.length === 0)
206
+ return false;
207
+ tellRequested(mod, requests);
208
+ if (isAsked !== true)
209
+ return (void run(mod, requests), false);
210
+ waiting.push({ mod, requests });
211
+ if (inputDepth > 0)
212
+ return true;
213
+ claude.clock.after(askedWaitMs, () => void takeWaiting());
214
+ return false;
215
+ };
216
+ const press = (mod, input) => {
217
+ const pending = claude.cmod
218
+ .call({ to: mod, method: partMethods.press, input })
219
+ .then((answer) => void update(mod, (isObject(answer) ? answer : {})), (error) => {
220
+ parts.forget(mod);
221
+ claude.ui.invalidate('ui.render');
222
+ tellRefusedPress(mod, error);
223
+ })
224
+ .finally(() => pressing.delete(pending));
225
+ pressing.add(pending);
226
+ if (inputDepth === 0)
227
+ void pending.then(takeWaiting);
228
+ return pending;
229
+ };
230
+ const lastDrawn = new Map();
231
+ const plain = (data) => JSON.parse(JSON.stringify(data));
232
+ const nameOf = (mod, id) => `The ${id} ${declared.get(mod)?.pages.some((each) => each.id === id) === true ? 'page' : 'view'} of ${mod}`;
233
+ const element = (mod, id, params, columns, isFocused, surface) => {
234
+ if (!declared.has(mod))
235
+ void ask(mod);
236
+ const given = JSON.stringify(params ?? null);
237
+ const cached = `${id}\u0000${given}\u0000${columns}\u0000${isFocused}\u0000${surface}`;
238
+ const name = nameOf(mod, id);
239
+ const keyPrefix = `${mod}/${id}:`;
240
+ void parts.fill(mod, cached, { page: id, params: plain(params ?? null), columns, isFocused, surface }, name)?.then(() => claude.ui.invalidate('ui.render'));
241
+ const failure = parts.failure(mod, cached);
242
+ const drawnAt = (key) => parts.element(mod, key, keyPrefix, name, `page:${mod}/${id}`);
243
+ const last = lastDrawn.get(keyPrefix);
244
+ const drawn = drawnAt(cached);
245
+ if (drawn !== undefined)
246
+ lastDrawn.set(keyPrefix, { params: given, cached });
247
+ return drawn ?? (last?.params === given ? drawnAt(last.cached) : undefined) ?? (failure === undefined ? undefined : Text({ color: 'error', children: `${name} could not draw: ${failure}` }));
248
+ };
249
+ const pressable = new Map();
250
+ const uiDraws = new Map();
251
+ let lastHandler = 0;
252
+ const numbered = (part, component, drawn) => {
253
+ const numbers = [];
254
+ const tree = elementData(drawn, (run) => {
255
+ lastHandler += 1;
256
+ pressable.set(lastHandler, { ...part, component, run });
257
+ numbers.push(lastHandler);
258
+ return lastHandler;
259
+ });
260
+ const key = `${part.mod}/${part.id}`;
261
+ const earlier = uiDraws.get(key) ?? [];
262
+ const kept = [...earlier, numbers].slice(-keptUiDraws);
263
+ for (const draw of earlier)
264
+ if (!kept.includes(draw))
265
+ for (const handler of draw)
266
+ pressable.delete(handler);
267
+ uiDraws.set(key, kept);
268
+ return tree;
269
+ };
270
+ const viewOrPage = ({ mod, id }) => {
271
+ const known = declared.get(mod);
272
+ return known?.views.find((each) => each.id === id) ?? known?.pages.find((each) => each.id === id);
273
+ };
274
+ let closedTurn;
275
+ const drawnPart = async (part, columns, surface, shownIn) => {
276
+ const { mod, id } = part;
277
+ if (viewOrPage(part) !== undefined) {
278
+ const params = shownBy.get(mod)?.open.find((each) => each.id === id)?.params ?? null;
279
+ const cached = `${id}\u0000${JSON.stringify(params)}\u0000${columns}\u0000true\u0000${surface}`;
280
+ const name = nameOf(mod, id);
281
+ await parts.fill(mod, cached, { page: id, params: plain(params), columns, isFocused: true, surface }, name);
282
+ return { cached, name, keyPrefix: `${mod}/${id}:`, component: 'Pane' };
283
+ }
284
+ const widget = declared.get(mod)?.widgets.find((each) => each.id === id);
285
+ if (widget === undefined)
286
+ throw new Error(`${mod} has no widget, view, or page ${id} in this session.`);
287
+ const name = `The ${widget.title} widget of ${mod}`;
288
+ if (shownIn !== undefined && !widget.areas.includes(shownIn))
289
+ throw new Error(`${name} draws in ${listed(widget.areas)}, not ${shownIn}.`);
290
+ const area = shownIn ?? (widget.areas.includes(Dashboard) ? Dashboard : (widget.areas[0] ?? Dashboard));
291
+ const turn = area === TurnEnd ? closedTurn : undefined;
292
+ if (area === TurnEnd && turn === undefined)
293
+ return undefined;
294
+ const key = `${mod}/${id}`;
295
+ const cached = `${key}\u0000${area}\u0000${columns}\u0000${surface}\u0000${turn?.prompt.id ?? ''}`;
296
+ await parts.fill(mod, cached, { widget: id, area, columns, surface, ...(turn === undefined ? {} : { turn }) }, name);
297
+ return { cached, name, keyPrefix: `${key}:`, component: areaComponents[area] };
298
+ };
299
+ const ui = {
300
+ widgets: () => [...declared.values()].flatMap(({ mod, widgets: listed }) => listed.map(({ id, areas }) => ({ mod, id, areas }))),
301
+ views: () => [...declared.values()].flatMap(({ mod, views }) => views.flatMap(({ id, title, place }) => (place === 'sidebar' ? [{ mod, id, title }] : []))),
302
+ pages: () => [...declared.values()].flatMap(({ mod, pages }) => pages.map(({ id, title }) => ({ mod, id, title }))),
303
+ async draw(_session, part, { columns, surface, area }) {
304
+ const drawing = await drawnPart(part, columns, surface, area);
305
+ if (drawing === undefined)
306
+ return null;
307
+ const { cached, name, keyPrefix, component } = drawing;
308
+ const failure = parts.failure(part.mod, cached);
309
+ if (failure !== undefined)
310
+ throw new Error(`${name} could not draw: ${failure}`);
311
+ const drawn = parts.element(part.mod, cached, keyPrefix, name, `ui:${part.mod}/${part.id}`);
312
+ return drawn === undefined ? null : numbered(part, component, drawn);
313
+ },
314
+ async press(_session, handler, { surface, key, value, kind, link }) {
315
+ const known = pressable.get(handler);
316
+ if (known === undefined)
317
+ throw new Error(`cmod: ${stalePress}`);
318
+ const pressed = { plugin: known.mod, element: key ?? '', component: known.component, requestId: known.id, surface, ...(kind === undefined ? {} : { kind }), ...(value === undefined ? {} : { value }), ...(link === undefined ? {} : { link }) };
319
+ await known.run(...(link !== undefined ? [link, pressed] : value !== undefined ? [value, pressed] : [pressed]));
320
+ return null;
321
+ },
322
+ async open(_session, part) {
323
+ if (viewOrPage(part) === undefined)
324
+ throw new Error(`${part.mod} has no view or page ${part.id} in this session.`);
325
+ await openIn(part.mod, part.id, true);
326
+ return null;
327
+ },
328
+ async close(_session, part) {
329
+ await openIn(part.mod, part.id, false);
330
+ return null;
331
+ },
332
+ shown(_session, listed) {
333
+ const next = new Map();
334
+ for (const { mod, id } of listed)
335
+ next.set(mod, [...(next.get(mod) ?? []), id]);
336
+ const changed = [...new Set([...inApps.keys(), ...next.keys()])].filter((mod) => JSON.stringify(inApps.get(mod) ?? []) !== JSON.stringify(next.get(mod) ?? []));
337
+ inApps = next;
338
+ for (const mod of changed)
339
+ tellShown(mod);
340
+ return null;
341
+ },
342
+ };
343
+ return {
344
+ declared: () => [...declared.values()],
345
+ providers: providersOf,
346
+ stylers: () => providersOf(styleInterface),
347
+ element,
348
+ press,
349
+ update,
350
+ tell(mod, shown) {
351
+ shownBy.set(mod, shown);
352
+ tellShown(mod);
353
+ },
354
+ apply(listener) {
355
+ applyTo = listener;
356
+ },
357
+ onOpen(listener) {
358
+ openIn = listener;
359
+ },
360
+ onStart(listener) {
361
+ started = listener;
362
+ },
363
+ turnClosed(turn) {
364
+ closedTurn = turn;
365
+ for (const { mod, widgets: listed } of declared.values())
366
+ for (const { id, areas } of listed)
367
+ if (areas.includes(TurnEnd))
368
+ tellUi('Redraw', { mod, id });
369
+ },
370
+ ui,
371
+ };
372
+ }
@@ -13,6 +13,7 @@ export declare function readLines(stream: HookStream<ProcessSpawnChunk, ProcessS
13
13
  }>;
14
14
  export declare function spawnCmod(claude: Claude, args: readonly string[], session: string, env?: Readonly<Record<string, string>>): HookStream<ProcessSpawnChunk, ProcessSpawnResult>;
15
15
  export declare function isHeld(claude: Claude, lock: string, staleMs: number): Promise<boolean>;
16
+ export declare function cmodStore(claude: Claude): Promise<string>;
16
17
  export declare function newestCmodVersion(claude: Claude): Promise<string | undefined>;
17
18
  export declare function runCmod(claude: Claude, args: readonly string[], session: string): Promise<string>;
18
19
  export declare function locatingPrograms(claude: Claude): Programs;
@@ -1 +1 @@
1
- {"version":3,"file":"programs.d.ts","sourceRoot":"","sources":["../src/runtime/programs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAIpF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,MAAM,MAAM,QAAQ,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;CAAE,CAAA;AAEpH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAG3D;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAI7E;AAED,wBAAsB,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC,iBAAiB,EAAE,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC,CAgB9K;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM,GAAG,UAAU,CAAC,iBAAiB,EAAE,kBAAkB,CAAC,CAEjL;AAED,wBAAsB,MAAM,CAAC,MA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CAAC,CAgB9K;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM,GAAG,UAAU,CAAC,iBAAiB,EAAE,kBAAkB,CAAC,CAEjL;AAED,wBAAsB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAK5F;AAED,wBAAsB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAE/D;AAED,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CASnF;AAED,wBAAsB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAKvG;AAuBD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,CAqCzD"}
@@ -39,8 +39,11 @@ export async function isHeld(claude, lock, staleMs) {
39
39
  const touched = await Promise.all(holders.map(async ({ name }) => (await claude.fs.stat(`${lock}/${name}`).catch(ifMissing(undefined)))?.mtimeMs ?? 0));
40
40
  return touched.some((at) => now - at < staleMs);
41
41
  }
42
+ export async function cmodStore(claude) {
43
+ return storeFolder({ HOME: await claude.env.home(), XDG_DATA_HOME: await claude.env.dataHome() });
44
+ }
42
45
  export async function newestCmodVersion(claude) {
43
- const store = storeFolder({ HOME: await claude.env.home(), XDG_DATA_HOME: await claude.env.dataHome() });
46
+ const store = await cmodStore(claude);
44
47
  return readCmodVersion({
45
48
  list: async (folder) => (await claude.fs.list(folder).catch(ifMissing([]))).map((entry) => entry.name),
46
49
  exists: async (path) => (await claude.fs.stat(path).catch(ifMissing(undefined))) !== undefined,
@@ -107,8 +110,7 @@ export function locatingPrograms(claude) {
107
110
  downloading = current;
108
111
  },
109
112
  async refresh() {
110
- const [home, dataHome] = await Promise.all([claude.env.home(), claude.env.dataHome()]);
111
- const programs = `${storeFolder({ HOME: home, XDG_DATA_HOME: dataHome })}/programs`;
113
+ const programs = `${await cmodStore(claude)}/programs`;
112
114
  names = new Set((await claude.fs.list(programs).catch(ifMissing([]))).map((entry) => entry.name));
113
115
  folder = programs;
114
116
  },
@@ -0,0 +1,111 @@
1
+ export type RpcId = number | string;
2
+ export type RpcErrorData = {
3
+ readonly code: number;
4
+ readonly message: string;
5
+ readonly data?: unknown;
6
+ };
7
+ export type RpcMessage = {
8
+ readonly jsonrpc: '2.0';
9
+ readonly id: RpcId;
10
+ readonly method: string;
11
+ readonly params?: unknown;
12
+ } | {
13
+ readonly jsonrpc: '2.0';
14
+ readonly method: string;
15
+ readonly params?: unknown;
16
+ } | {
17
+ readonly jsonrpc: '2.0';
18
+ readonly id: RpcId;
19
+ readonly result: unknown;
20
+ } | {
21
+ readonly jsonrpc: '2.0';
22
+ readonly id: RpcId;
23
+ readonly error: RpcErrorData;
24
+ };
25
+ export declare const rpcCodes: {
26
+ readonly methodNotFound: -32601;
27
+ readonly invalidParams: -32602;
28
+ readonly internal: -32603;
29
+ readonly closed: -32099;
30
+ readonly notHeld: -32098;
31
+ readonly cancelled: -32800;
32
+ };
33
+ export declare class RpcError extends Error {
34
+ readonly code: number;
35
+ readonly data: unknown;
36
+ constructor(code: number, message: string, data?: unknown);
37
+ }
38
+ export type RpcHandler = (method: string, params: unknown, signal: AbortSignal) => unknown;
39
+ export type Peer = {
40
+ request<Result>(method: string, params?: unknown, signal?: AbortSignal): Promise<Result>;
41
+ notify(method: string, params?: unknown): void;
42
+ receive(message: unknown): void;
43
+ close(reason: Error): void;
44
+ readonly signal: AbortSignal;
45
+ };
46
+ export declare function createPeer({ send, handle }: {
47
+ readonly send: (message: RpcMessage) => void;
48
+ readonly handle: RpcHandler;
49
+ }): Peer;
50
+ export declare function closedError(reason: unknown): RpcError;
51
+ export declare const rpcPath = "/rpc";
52
+ export type RpcConnection = Peer & {
53
+ readonly id: string;
54
+ };
55
+ export type RpcHttpRequest = {
56
+ readonly method: string;
57
+ readonly path: string;
58
+ readonly signal: AbortSignal;
59
+ json(): Promise<unknown>;
60
+ };
61
+ export type RpcHttpAnswer = {
62
+ readonly status: number;
63
+ readonly json?: unknown;
64
+ };
65
+ export type RpcRoutes = {
66
+ route(request: RpcHttpRequest): Promise<RpcHttpAnswer> | undefined;
67
+ channel(send: (message: RpcMessage) => void): RpcConnection;
68
+ connections(): RpcConnection[];
69
+ close(): void;
70
+ };
71
+ export declare function rpcRoutes({ handle, onClose, now, pollMs, liveMs, }: {
72
+ readonly handle: (connection: RpcConnection) => RpcHandler;
73
+ readonly onClose?: (connection: RpcConnection) => void;
74
+ readonly now: () => number;
75
+ readonly pollMs?: number;
76
+ readonly liveMs?: number;
77
+ }): RpcRoutes;
78
+ export declare class RpcUnreached extends Error {
79
+ }
80
+ export declare function isConnectionGone(error: unknown): boolean;
81
+ export type HttpCall = (path: string, init: {
82
+ readonly method: 'GET' | 'POST' | 'DELETE';
83
+ readonly body?: string;
84
+ }) => Promise<{
85
+ readonly status: number;
86
+ readonly text: string;
87
+ }>;
88
+ export type HttpFetch = (url: string, init: {
89
+ readonly method: string;
90
+ readonly body?: string;
91
+ readonly headers?: Readonly<Record<string, string>>;
92
+ readonly socketPath?: string;
93
+ }) => Promise<{
94
+ readonly status: number;
95
+ readonly text: string;
96
+ }>;
97
+ export declare function addressCall(fetch: HttpFetch, address: string): HttpCall;
98
+ export type RpcClient = Pick<Peer, 'request' | 'notify'> & {
99
+ readonly isConnected: boolean;
100
+ close(): Promise<void>;
101
+ };
102
+ export declare function connectRpc({ call, handle, onConnect, onError, wait, retryMs, keepsConnecting, }: {
103
+ readonly call: HttpCall;
104
+ readonly handle: RpcHandler;
105
+ readonly onConnect?: (client: RpcClient) => unknown;
106
+ readonly onError?: (error: unknown) => void;
107
+ readonly wait: (ms: number) => Promise<void>;
108
+ readonly retryMs?: number;
109
+ readonly keepsConnecting?: boolean;
110
+ }): RpcClient;
111
+ //# sourceMappingURL=rpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../src/runtime/rpc.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG,MAAM,GAAG,MAAM,CAAA;AAEnC,MAAM,MAAM,YAAY,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAA;CAAE,CAAA;AAEvG,MAAM,MAAM,UAAU,GAClB;IAAE,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,GACnG;IAAE,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,GAC/E;IAAE,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GACzE;IAAE,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAA;CAAE,CAAA;AAEjF,eAAO,MAAM,QAAQ;aACnB,cAAc,EAAE,CAAC,KAAK;aACtB,aAAa,EAAE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