@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.
- package/docs/commands.md +104 -70
- package/docs/dependencies.md +153 -23
- package/docs/index.md +54 -10
- package/docs/install-steps.md +20 -17
- package/docs/jobs.md +42 -17
- package/docs/mod.md +37 -30
- package/docs/options.md +2 -2
- package/docs/session.md +39 -29
- package/docs/testing.md +84 -23
- package/docs/ui.md +128 -58
- package/installer.d.ts +2 -2
- package/jobs/context.js +1 -1
- package/jobs/program.d.ts +2 -3
- package/jobs/program.d.ts.map +1 -1
- package/jobs/program.js +72 -23
- package/mod.d.ts +23 -8
- package/mod.d.ts.map +1 -1
- package/mod.js +3 -3
- package/package.json +1 -1
- package/records.d.ts +5 -3
- package/records.d.ts.map +1 -1
- package/records.js +18 -9
- package/register.d.ts.map +1 -1
- package/register.js +7 -15
- package/rpc.d.ts +7 -0
- package/rpc.d.ts.map +1 -0
- package/rpc.js +59 -0
- package/runtime/bound.d.ts +1 -0
- package/runtime/bound.d.ts.map +1 -1
- package/runtime/bound.js +10 -0
- package/runtime/bun.d.ts +20 -0
- package/runtime/bun.d.ts.map +1 -0
- package/runtime/bun.js +20 -0
- package/runtime/claude.d.ts +1 -0
- package/runtime/claude.d.ts.map +1 -1
- package/runtime/conversations.d.ts +9 -2
- package/runtime/conversations.d.ts.map +1 -1
- package/runtime/conversations.js +244 -185
- package/runtime/deadline.d.ts +4 -3
- package/runtime/deadline.d.ts.map +1 -1
- package/runtime/deadline.js +10 -4
- package/runtime/engine.d.ts +4 -4
- package/runtime/engine.d.ts.map +1 -1
- package/runtime/installer.d.ts +3 -3
- package/runtime/installer.d.ts.map +1 -1
- package/runtime/installer.js +10 -9
- package/runtime/interfaces.d.ts +108 -42
- package/runtime/interfaces.d.ts.map +1 -1
- package/runtime/interfaces.js +639 -143
- package/runtime/lifecycle.d.ts +3 -5
- package/runtime/lifecycle.d.ts.map +1 -1
- package/runtime/lifecycle.js +74 -63
- package/runtime/options.d.ts.map +1 -1
- package/runtime/options.js +2 -6
- package/runtime/parts.d.ts +64 -1
- package/runtime/parts.d.ts.map +1 -1
- package/runtime/parts.js +311 -4
- package/runtime/programs.d.ts +1 -0
- package/runtime/programs.d.ts.map +1 -1
- package/runtime/programs.js +5 -3
- package/runtime/rpc.d.ts +2 -0
- package/runtime/rpc.d.ts.map +1 -1
- package/runtime/rpc.js +30 -5
- package/runtime/sessions.d.ts +3 -3
- package/runtime/sessions.d.ts.map +1 -1
- package/runtime/sessions.js +38 -73
- package/runtime/toaster.js +1 -1
- package/runtime/ui.d.ts +19 -8
- package/runtime/ui.d.ts.map +1 -1
- package/runtime/ui.js +51 -47
- package/runtime/web.d.ts +0 -73
- package/runtime/web.d.ts.map +1 -1
- package/runtime/web.js +9 -5
- package/runtime/widgets.d.ts +4 -2
- package/runtime/widgets.d.ts.map +1 -1
- package/runtime/widgets.js +17 -16
- package/session/conversation.d.ts +6 -2
- package/session/conversation.d.ts.map +1 -1
- package/session/session.d.ts +22 -6
- package/session/session.d.ts.map +1 -1
- package/session/session.js +1 -0
- package/src/installer.ts +2 -2
- package/src/jobs/context.ts +1 -1
- package/src/jobs/program.ts +63 -23
- package/src/mod.ts +22 -11
- package/src/records.ts +21 -9
- package/src/register.ts +7 -15
- package/src/rpc.ts +67 -0
- package/src/runtime/bound.ts +10 -0
- package/src/runtime/bun.ts +40 -0
- package/src/runtime/claude.ts +1 -0
- package/src/runtime/conversations.ts +247 -182
- package/src/runtime/deadline.ts +13 -6
- package/src/runtime/engine.ts +4 -4
- package/src/runtime/installer.ts +13 -12
- package/src/runtime/interfaces.ts +753 -172
- package/src/runtime/lifecycle.ts +93 -67
- package/src/runtime/options.ts +2 -5
- package/src/runtime/parts.ts +364 -5
- package/src/runtime/programs.ts +6 -3
- package/src/runtime/rpc.ts +32 -5
- package/src/runtime/sessions.ts +39 -73
- package/src/runtime/toaster.ts +1 -1
- package/src/runtime/ui.ts +67 -55
- package/src/runtime/web.ts +10 -39
- package/src/runtime/widgets.ts +20 -18
- package/src/session/conversation.ts +7 -2
- package/src/session/session.ts +14 -6
- package/src/testing/fake-claude.ts +10 -4
- package/src/testing/fake-serve.ts +106 -6
- package/src/testing/fake-sessions.ts +6 -5
- package/src/testing.ts +149 -67
- package/src/ui/areas.ts +2 -2
- package/src/ui/elements.ts +21 -16
- package/src/ui/panes.ts +14 -7
- package/testing/fake-claude.d.ts +2 -0
- package/testing/fake-claude.d.ts.map +1 -1
- package/testing/fake-claude.js +10 -4
- package/testing/fake-serve.d.ts +32 -2
- package/testing/fake-serve.d.ts.map +1 -1
- package/testing/fake-serve.js +81 -6
- package/testing/fake-sessions.d.ts.map +1 -1
- package/testing/fake-sessions.js +6 -5
- package/testing.d.ts +21 -4
- package/testing.d.ts.map +1 -1
- package/testing.js +131 -55
- package/ui/areas.d.ts +3 -1
- package/ui/areas.d.ts.map +1 -1
- package/ui/elements.d.ts +18 -9
- package/ui/elements.d.ts.map +1 -1
- package/ui/elements.js +9 -8
- package/ui/panes.d.ts +10 -6
- package/ui/panes.d.ts.map +1 -1
- package/ui/panes.js +5 -3
- package/runtime/pages.d.ts +0 -34
- package/runtime/pages.d.ts.map +0 -1
- package/runtime/pages.js +0 -157
- package/src/runtime/pages.ts +0 -198
package/runtime/pages.d.ts
DELETED
|
@@ -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
|
package/runtime/pages.d.ts.map
DELETED
|
@@ -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
|
-
}
|
package/src/runtime/pages.ts
DELETED
|
@@ -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
|
-
}
|