@cmodjs/core 7.0.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 +104 -70
  2. package/docs/dependencies.md +153 -23
  3. package/docs/index.md +54 -10
  4. package/docs/install-steps.md +20 -17
  5. package/docs/jobs.md +42 -17
  6. package/docs/mod.md +37 -30
  7. package/docs/options.md +2 -2
  8. package/docs/session.md +39 -29
  9. package/docs/testing.md +84 -23
  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 +2 -3
  14. package/jobs/program.d.ts.map +1 -1
  15. package/jobs/program.js +72 -23
  16. package/mod.d.ts +23 -8
  17. package/mod.d.ts.map +1 -1
  18. package/mod.js +3 -3
  19. package/package.json +1 -1
  20. package/records.d.ts +5 -3
  21. package/records.d.ts.map +1 -1
  22. package/records.js +18 -9
  23. package/register.d.ts.map +1 -1
  24. package/register.js +7 -15
  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 +9 -2
  37. package/runtime/conversations.d.ts.map +1 -1
  38. package/runtime/conversations.js +244 -185
  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 +4 -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 +108 -42
  48. package/runtime/interfaces.d.ts.map +1 -1
  49. package/runtime/interfaces.js +639 -143
  50. package/runtime/lifecycle.d.ts +3 -5
  51. package/runtime/lifecycle.d.ts.map +1 -1
  52. package/runtime/lifecycle.js +74 -63
  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 +2 -0
  62. package/runtime/rpc.d.ts.map +1 -1
  63. package/runtime/rpc.js +30 -5
  64. package/runtime/sessions.d.ts +3 -3
  65. package/runtime/sessions.d.ts.map +1 -1
  66. package/runtime/sessions.js +38 -73
  67. package/runtime/toaster.js +1 -1
  68. package/runtime/ui.d.ts +19 -8
  69. package/runtime/ui.d.ts.map +1 -1
  70. package/runtime/ui.js +51 -47
  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 +6 -2
  78. package/session/conversation.d.ts.map +1 -1
  79. package/session/session.d.ts +22 -6
  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 +63 -23
  85. package/src/mod.ts +22 -11
  86. package/src/records.ts +21 -9
  87. package/src/register.ts +7 -15
  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 +247 -182
  93. package/src/runtime/deadline.ts +13 -6
  94. package/src/runtime/engine.ts +4 -4
  95. package/src/runtime/installer.ts +13 -12
  96. package/src/runtime/interfaces.ts +753 -172
  97. package/src/runtime/lifecycle.ts +93 -67
  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 +32 -5
  102. package/src/runtime/sessions.ts +39 -73
  103. package/src/runtime/toaster.ts +1 -1
  104. package/src/runtime/ui.ts +67 -55
  105. package/src/runtime/web.ts +10 -39
  106. package/src/runtime/widgets.ts +20 -18
  107. package/src/session/conversation.ts +7 -2
  108. package/src/session/session.ts +14 -6
  109. package/src/testing/fake-claude.ts +10 -4
  110. package/src/testing/fake-serve.ts +106 -6
  111. package/src/testing/fake-sessions.ts +6 -5
  112. package/src/testing.ts +149 -67
  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 +2 -0
  117. package/testing/fake-claude.d.ts.map +1 -1
  118. package/testing/fake-claude.js +10 -4
  119. package/testing/fake-serve.d.ts +32 -2
  120. package/testing/fake-serve.d.ts.map +1 -1
  121. package/testing/fake-serve.js +81 -6
  122. package/testing/fake-sessions.d.ts.map +1 -1
  123. package/testing/fake-sessions.js +6 -5
  124. package/testing.d.ts +21 -4
  125. package/testing.d.ts.map +1 -1
  126. package/testing.js +131 -55
  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 -34
  136. package/runtime/pages.d.ts.map +0 -1
  137. package/runtime/pages.js +0 -157
  138. package/src/runtime/pages.ts +0 -198
@@ -1,34 +0,0 @@
1
- import type { RenderElement } from 'claude-code';
2
- import type { Claude } from './claude.js';
3
- import type { DrawnParts } from './parts.js';
4
- import type { Router } from './router.js';
5
- import { type PageDeclaration, type PageRequest, type PartPress, type PartsUpdate, type ShownPages } from './ui.js';
6
- import type { Widgets } from './widgets.js';
7
- export type ModPages = {
8
- readonly mod: string;
9
- readonly pages: readonly PageDeclaration[];
10
- };
11
- export type PageApply = (mod: string, requests: readonly PageRequest[]) => Promise<void>;
12
- export type Pages = {
13
- declared(): readonly ModPages[];
14
- providers(name: string): readonly string[];
15
- stylers(): readonly string[];
16
- element(mod: string, page: string, params: unknown, columns: number, isFocused: boolean): RenderElement | undefined;
17
- press(mod: string, press: PartPress): void;
18
- update(mod: string, update: PartsUpdate): boolean;
19
- tell(mod: string, shown: ShownPages): void;
20
- apply(listener: PageApply): void;
21
- onStart(listener: (mod: string) => void): void;
22
- };
23
- export declare function providedBy(listing: unknown): readonly string[];
24
- export declare function pagesOf(input: unknown): PageDeclaration[];
25
- export declare function servePages({ claude, router, report, tellRefusedPress, widgets, parts, tellStylers, }: {
26
- readonly claude: Claude;
27
- readonly router: Router;
28
- readonly report: (id: string, message: string, error: unknown) => void;
29
- readonly tellRefusedPress: (mod: string, error: unknown) => void;
30
- readonly widgets: Pick<Widgets, 'update'>;
31
- readonly parts: DrawnParts;
32
- readonly tellStylers: (stylers: readonly string[]) => void;
33
- }): Pages;
34
- //# sourceMappingURL=pages.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pages.d.ts","sourceRoot":"","sources":["../src/runtime/pages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAIhD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAe,KAAK,eAAe,EAAE,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,SAAS,CAAA;AAChI,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAI3C,MAAM,MAAM,QAAQ,GAAG;IAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,eAAe,EAAE,CAAA;CAAE,CAAA;AAE3F,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,WAAW,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;AAExF,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,IAAI,SAAS,QAAQ,EAAE,CAAA;IAC/B,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAA;IAC1C,OAAO,IAAI,SAAS,MAAM,EAAE,CAAA;IAC5B,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,GAAG,aAAa,GAAG,SAAS,CAAA;IACnH,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,CAAA;IAC1C,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAA;IACjD,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,CAAA;IAC1C,KAAK,CAAC,QAAQ,EAAE,SAAS,GAAG,IAAI,CAAA;IAChC,OAAO,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAAA;CAC/C,CAAA;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,MAAM,EAAE,CAI9D;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,EAAE,CAGzD;AAED,wBAAgB,UAAU,CAAC,EACzB,MAAM,EACN,MAAM,EACN,MAAM,EACN,gBAAgB,EAChB,OAAO,EACP,KAAK,EACL,WAAW,GACZ,EAAE;IACD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;IACtE,QAAQ,CAAC,gBAAgB,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;IAChE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;IACzC,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAA;IAC1B,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,KAAK,IAAI,CAAA;CAC3D,GAAG,KAAK,CA4IR"}
package/runtime/pages.js DELETED
@@ -1,157 +0,0 @@
1
- import { isObject } from '../records.js';
2
- import { Text } from '../ui/elements.js';
3
- import { messageOf } from '../utils/text.js';
4
- import { cmodPluginName, isNotInstalled, isStillInstalling } from './dependencies.js';
5
- import { styleInterface } from './interfaces.js';
6
- import { partMethods } from './ui.js';
7
- const askedWaitMs = 1000;
8
- export function providedBy(listing) {
9
- if (!isObject(listing))
10
- return [];
11
- const named = Array.isArray(listing['provides']) ? listing['provides'].filter((each) => typeof each === 'string') : [];
12
- return listing['stylesMarkdown'] === true && !named.includes(styleInterface) ? [...named, styleInterface] : named;
13
- }
14
- export function pagesOf(input) {
15
- const listed = isObject(input) && Array.isArray(input['pages']) ? input['pages'] : [];
16
- return listed.filter(isObject).flatMap((each) => (typeof each['id'] === 'string' && typeof each['title'] === 'string' && (each['place'] === 'sidebar' || each['place'] === 'settings') ? [{ id: each['id'], title: each['title'], place: each['place'] }] : []));
17
- }
18
- export function servePages({ claude, router, report, tellRefusedPress, widgets, parts, tellStylers, }) {
19
- const declared = new Map();
20
- const provided = new Map();
21
- const providersOf = (name) => [...(provided.get(name) ?? [])];
22
- const asking = new Map();
23
- const pressing = new Set();
24
- let waiting = [];
25
- let inputDepth = 0;
26
- let applyTo = async () => undefined;
27
- let started = () => undefined;
28
- const run = (mod, requests) => applyTo(mod, requests).catch((error) => report(`pages:${mod}`, `Could not show the page ${mod} asked for`, error));
29
- const takeWaiting = async () => {
30
- const taken = waiting;
31
- waiting = [];
32
- for (const { mod, requests } of taken)
33
- await run(mod, requests);
34
- };
35
- const settle = async (earlier) => {
36
- const own = () => [...pressing].filter((pending) => !earlier.has(pending));
37
- for (let pending = own(); pending.length > 0; pending = own())
38
- await Promise.allSettled(pending);
39
- await takeWaiting();
40
- };
41
- const wrapped = async (next) => {
42
- inputDepth += 1;
43
- const earlier = new Set(pressing);
44
- try {
45
- const answer = await next();
46
- await settle(earlier);
47
- return answer;
48
- }
49
- finally {
50
- inputDepth -= 1;
51
- }
52
- };
53
- router.add('command.run', (e, next) => wrapped(async () => (waiting.length > 0 ? {} : next(e))));
54
- router.add('ui.press', (e, next) => wrapped(() => next(e)));
55
- const ask = (mod) => {
56
- const pending = asking.get(mod) ??
57
- claude.cmod
58
- .call({ to: mod, method: partMethods.list, input: null })
59
- .then((parts) => void declared.set(mod, pagesOf(parts)), (error) => {
60
- if (!isNotInstalled(mod, error) && !isStillInstalling(mod, error))
61
- claude.ui.warn(`pages:list:${mod}`, `cmod: ${mod} did not list its pages: ${messageOf(error)}`);
62
- })
63
- .finally(() => asking.delete(mod));
64
- asking.set(mod, pending);
65
- return pending;
66
- };
67
- const provide = (mod, names) => {
68
- const wasStyler = provided.get(styleInterface)?.has(mod) === true;
69
- for (const [name, mods] of provided)
70
- if (!names.includes(name))
71
- mods.delete(mod);
72
- for (const name of names)
73
- provided.set(name, (provided.get(name) ?? new Set()).add(mod));
74
- if (wasStyler || names.includes(styleInterface))
75
- restyle(mod);
76
- };
77
- const restyle = (mod) => {
78
- parts.forget(mod);
79
- const listed = providersOf(styleInterface);
80
- tellStylers(listed);
81
- for (const told of declared.keys()) {
82
- if (told === cmodPluginName)
83
- continue;
84
- void claude.cmod.call({ to: told, method: partMethods.stylers, input: listed }).catch((error) => {
85
- if (!isStillInstalling(told, error))
86
- claude.ui.warn(`pages:stylers:${told}`, `cmod: ${told} did not hear which mods style markdown: ${messageOf(error)}`);
87
- });
88
- }
89
- };
90
- const update = (mod, { parts: listing, redraw, requests, isAsked, isStart }) => {
91
- if (isStart === true)
92
- started(mod);
93
- if (listing !== undefined)
94
- declared.set(mod, pagesOf(listing));
95
- if (listing !== undefined)
96
- provide(mod, providedBy(listing));
97
- const isRedrawn = redraw === true || requests?.some((request) => 'open' in request) === true;
98
- if (listing !== undefined || isRedrawn)
99
- widgets.update(mod, { ...(listing === undefined ? {} : { parts: listing }), ...(isRedrawn ? { redraw: true } : {}) });
100
- if (requests === undefined || requests.length === 0)
101
- return false;
102
- if (isAsked !== true)
103
- return (void run(mod, requests), false);
104
- waiting.push({ mod, requests });
105
- if (inputDepth > 0)
106
- return true;
107
- claude.clock.after(askedWaitMs, () => void takeWaiting());
108
- return false;
109
- };
110
- const press = (mod, input) => {
111
- const pending = claude.cmod
112
- .call({ to: mod, method: partMethods.press, input })
113
- .then((answer) => void update(mod, (isObject(answer) ? answer : {})), (error) => {
114
- parts.forget(mod);
115
- claude.ui.invalidate('ui.render');
116
- tellRefusedPress(mod, error);
117
- })
118
- .finally(() => pressing.delete(pending));
119
- pressing.add(pending);
120
- if (inputDepth === 0)
121
- void pending.then(takeWaiting);
122
- };
123
- const lastDrawn = new Map();
124
- const plain = (data) => JSON.parse(JSON.stringify(data));
125
- return {
126
- declared: () => [...declared].map(([mod, pages]) => ({ mod, pages })),
127
- providers: providersOf,
128
- stylers: () => providersOf(styleInterface),
129
- element(mod, page, params, columns, isFocused) {
130
- if (!declared.has(mod))
131
- void ask(mod);
132
- const given = JSON.stringify(params ?? null);
133
- const cached = `${page}\u0000${given}\u0000${columns}\u0000${isFocused}`;
134
- const name = `The ${page} page of ${mod}`;
135
- const keyPrefix = `${mod}/${page}:`;
136
- void parts.fill(mod, cached, { page, params: plain(params ?? null), columns, isFocused }, name)?.then(() => claude.ui.invalidate('ui.render'));
137
- const failure = parts.failure(mod, cached);
138
- const drawnAt = (key) => parts.element(mod, key, keyPrefix, name, `page:${mod}/${page}`);
139
- const last = lastDrawn.get(keyPrefix);
140
- const drawn = drawnAt(cached);
141
- if (drawn !== undefined)
142
- lastDrawn.set(keyPrefix, { params: given, cached });
143
- return drawn ?? (last?.params === given ? drawnAt(last.cached) : undefined) ?? (failure === undefined ? undefined : Text({ color: 'error', children: `${name} could not draw: ${failure}` }));
144
- },
145
- press,
146
- update,
147
- tell(mod, shown) {
148
- 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 pages show: ${messageOf(error)}`));
149
- },
150
- apply(listener) {
151
- applyTo = listener;
152
- },
153
- onStart(listener) {
154
- started = listener;
155
- },
156
- };
157
- }
@@ -1,198 +0,0 @@
1
- import type { RenderElement } from 'claude-code'
2
- import { isObject } from '../records.js'
3
- import { Text } from '../ui/elements.js'
4
- import { messageOf } from '../utils/text.js'
5
- import type { Claude } from './claude.js'
6
- import { cmodPluginName, isNotInstalled, isStillInstalling } from './dependencies.js'
7
- import type { DrawnParts } from './parts.js'
8
- import { styleInterface } from './interfaces.js'
9
- import type { Router } from './router.js'
10
- import { partMethods, type PageDeclaration, type PageRequest, type PartPress, type PartsUpdate, type ShownPages } from './ui.js'
11
- import type { Widgets } from './widgets.js'
12
-
13
- const askedWaitMs = 1000
14
-
15
- export type ModPages = { readonly mod: string; readonly pages: readonly PageDeclaration[] }
16
-
17
- export type PageApply = (mod: string, requests: readonly PageRequest[]) => Promise<void>
18
-
19
- export type Pages = {
20
- declared(): readonly ModPages[]
21
- providers(name: string): readonly string[]
22
- stylers(): readonly string[]
23
- element(mod: string, page: string, params: unknown, columns: number, isFocused: boolean): RenderElement | undefined
24
- press(mod: string, press: PartPress): void
25
- update(mod: string, update: PartsUpdate): boolean
26
- tell(mod: string, shown: ShownPages): void
27
- apply(listener: PageApply): void
28
- onStart(listener: (mod: string) => void): void
29
- }
30
-
31
- export function providedBy(listing: unknown): readonly string[] {
32
- if (!isObject(listing)) return []
33
- const named = Array.isArray(listing['provides']) ? listing['provides'].filter((each): each is string => typeof each === 'string') : []
34
- return listing['stylesMarkdown'] === true && !named.includes(styleInterface) ? [...named, styleInterface] : named
35
- }
36
-
37
- export function pagesOf(input: unknown): PageDeclaration[] {
38
- const listed = isObject(input) && Array.isArray(input['pages']) ? (input['pages'] as unknown[]) : []
39
- return listed.filter(isObject).flatMap((each) => (typeof each['id'] === 'string' && typeof each['title'] === 'string' && (each['place'] === 'sidebar' || each['place'] === 'settings') ? [{ id: each['id'], title: each['title'], place: each['place'] }] : []))
40
- }
41
-
42
- export function servePages({
43
- claude,
44
- router,
45
- report,
46
- tellRefusedPress,
47
- widgets,
48
- parts,
49
- tellStylers,
50
- }: {
51
- readonly claude: Claude
52
- readonly router: Router
53
- readonly report: (id: string, message: string, error: unknown) => void
54
- readonly tellRefusedPress: (mod: string, error: unknown) => void
55
- readonly widgets: Pick<Widgets, 'update'>
56
- readonly parts: DrawnParts
57
- readonly tellStylers: (stylers: readonly string[]) => void
58
- }): Pages {
59
- const declared = new Map<string, readonly PageDeclaration[]>()
60
- const provided = new Map<string, Set<string>>()
61
- const providersOf = (name: string) => [...(provided.get(name) ?? [])]
62
- const asking = new Map<string, Promise<void>>()
63
- const pressing = new Set<Promise<void>>()
64
- let waiting: { readonly mod: string; readonly requests: readonly PageRequest[] }[] = []
65
- let inputDepth = 0
66
- let applyTo: PageApply = async () => undefined
67
- let started: (mod: string) => void = () => undefined
68
-
69
- const run = (mod: string, requests: readonly PageRequest[]) =>
70
- applyTo(mod, requests).catch((error: unknown) => report(`pages:${mod}`, `Could not show the page ${mod} asked for`, error))
71
-
72
- const takeWaiting = async () => {
73
- const taken = waiting
74
- waiting = []
75
- for (const { mod, requests } of taken) await run(mod, requests)
76
- }
77
-
78
- const settle = async (earlier: ReadonlySet<Promise<void>>) => {
79
- const own = () => [...pressing].filter((pending) => !earlier.has(pending))
80
- for (let pending = own(); pending.length > 0; pending = own()) await Promise.allSettled(pending)
81
- await takeWaiting()
82
- }
83
-
84
- const wrapped = async <Answer>(next: () => Promise<Answer>): Promise<Answer> => {
85
- inputDepth += 1
86
- const earlier = new Set(pressing)
87
- try {
88
- const answer = await next()
89
- await settle(earlier)
90
- return answer
91
- } finally {
92
- inputDepth -= 1
93
- }
94
- }
95
-
96
- router.add('command.run', (e, next) => wrapped(async () => (waiting.length > 0 ? {} : next(e))))
97
- router.add('ui.press', (e, next) => wrapped(() => next(e)))
98
-
99
- const ask = (mod: string) => {
100
- const pending =
101
- asking.get(mod) ??
102
- claude.cmod
103
- .call({ to: mod, method: partMethods.list, input: null })
104
- .then(
105
- (parts) => void declared.set(mod, pagesOf(parts)),
106
- (error: unknown) => {
107
- if (!isNotInstalled(mod, error) && !isStillInstalling(mod, error)) claude.ui.warn(`pages:list:${mod}`, `cmod: ${mod} did not list its pages: ${messageOf(error)}`)
108
- },
109
- )
110
- .finally(() => asking.delete(mod))
111
- asking.set(mod, pending)
112
- return pending
113
- }
114
-
115
- const provide = (mod: string, names: readonly string[]) => {
116
- const wasStyler = provided.get(styleInterface)?.has(mod) === true
117
- for (const [name, mods] of provided) if (!names.includes(name)) mods.delete(mod)
118
- for (const name of names) provided.set(name, (provided.get(name) ?? new Set<string>()).add(mod))
119
- if (wasStyler || names.includes(styleInterface)) restyle(mod)
120
- }
121
-
122
- const restyle = (mod: string) => {
123
- parts.forget(mod)
124
- const listed = providersOf(styleInterface)
125
- tellStylers(listed)
126
- for (const told of declared.keys()) {
127
- if (told === cmodPluginName) continue
128
- void claude.cmod.call({ to: told, method: partMethods.stylers, input: listed }).catch((error: unknown) => {
129
- if (!isStillInstalling(told, error)) claude.ui.warn(`pages:stylers:${told}`, `cmod: ${told} did not hear which mods style markdown: ${messageOf(error)}`)
130
- })
131
- }
132
- }
133
-
134
- const update = (mod: string, { parts: listing, redraw, requests, isAsked, isStart }: PartsUpdate) => {
135
- if (isStart === true) started(mod)
136
- if (listing !== undefined) declared.set(mod, pagesOf(listing))
137
- if (listing !== undefined) provide(mod, providedBy(listing))
138
- const isRedrawn = redraw === true || requests?.some((request) => 'open' in request) === true
139
- if (listing !== undefined || isRedrawn) widgets.update(mod, { ...(listing === undefined ? {} : { parts: listing }), ...(isRedrawn ? { redraw: true } : {}) })
140
- if (requests === undefined || requests.length === 0) return false
141
- if (isAsked !== true) return (void run(mod, requests), false)
142
- waiting.push({ mod, requests })
143
- if (inputDepth > 0) return true
144
- claude.clock.after(askedWaitMs, () => void takeWaiting())
145
- return false
146
- }
147
-
148
- const press = (mod: string, input: PartPress) => {
149
- const pending = claude.cmod
150
- .call({ to: mod, method: partMethods.press, input })
151
- .then(
152
- (answer) => void update(mod, (isObject(answer) ? answer : {}) as PartsUpdate),
153
- (error: unknown) => {
154
- parts.forget(mod)
155
- claude.ui.invalidate('ui.render')
156
- tellRefusedPress(mod, error)
157
- },
158
- )
159
- .finally(() => pressing.delete(pending))
160
- pressing.add(pending)
161
- if (inputDepth === 0) void pending.then(takeWaiting)
162
- }
163
-
164
- const lastDrawn = new Map<string, { readonly params: string; readonly cached: string }>()
165
-
166
- const plain = <Data,>(data: Data): Data => JSON.parse(JSON.stringify(data)) as Data
167
-
168
- return {
169
- declared: () => [...declared].map(([mod, pages]) => ({ mod, pages })),
170
- providers: providersOf,
171
- stylers: () => providersOf(styleInterface),
172
- element(mod, page, params, columns, isFocused) {
173
- if (!declared.has(mod)) void ask(mod)
174
- const given = JSON.stringify(params ?? null)
175
- const cached = `${page}\u0000${given}\u0000${columns}\u0000${isFocused}`
176
- const name = `The ${page} page of ${mod}`
177
- const keyPrefix = `${mod}/${page}:`
178
- void parts.fill(mod, cached, { page, params: plain(params ?? null), columns, isFocused }, name)?.then(() => claude.ui.invalidate('ui.render'))
179
- const failure = parts.failure(mod, cached)
180
- const drawnAt = (key: string) => parts.element(mod, key, keyPrefix, name, `page:${mod}/${page}`)
181
- const last = lastDrawn.get(keyPrefix)
182
- const drawn = drawnAt(cached)
183
- if (drawn !== undefined) lastDrawn.set(keyPrefix, { params: given, cached })
184
- return drawn ?? (last?.params === given ? drawnAt(last.cached) : undefined) ?? (failure === undefined ? undefined : Text({ color: 'error', children: `${name} could not draw: ${failure}` }))
185
- },
186
- press,
187
- update,
188
- tell(mod, shown) {
189
- void claude.cmod.call({ to: mod, method: partMethods.shown, input: plain(shown) }).catch((error: unknown) => claude.ui.warn(`pages:shown:${mod}`, `cmod: ${mod} did not hear which of its pages show: ${messageOf(error)}`))
190
- },
191
- apply(listener) {
192
- applyTo = listener
193
- },
194
- onStart(listener) {
195
- started = listener
196
- },
197
- }
198
- }