@pi-chamber/web 0.4.0 → 0.5.1
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/dist/assets/{CommitSection-0zl1puqf.js → CommitSection-MZJqMxUT.js} +1 -1
- package/dist/assets/DiagramEditor-DmDE7OFs.js +1 -0
- package/dist/assets/DiagramView-DjMwKAR1.js +1 -0
- package/dist/assets/{DiffView-BoTz2Zqe.js → DiffView-C3F9al7D.js} +2 -2
- package/dist/assets/{FilesView-CVMNE9Pr.js → FilesView-DzboGeAe.js} +6 -6
- package/dist/assets/{GitView-DnsbCAWl.js → GitView-BfyNIPaR.js} +4 -4
- package/dist/assets/{JsonTreeView-Cn1Mk0Wf.js → JsonTreeView-CGH92Uqo.js} +1 -1
- package/dist/assets/{MarkdownRendererImpl-DJKD8tNq.js → MarkdownRendererImpl-DAV0eoSb.js} +2 -2
- package/dist/assets/{PatchDiff-GdGq3IlM.js → PatchDiff-Dux9rNuF.js} +1 -1
- package/dist/assets/{PierreDiffViewer-CdkAUt-S.js → PierreDiffViewer-9imQKDgh.js} +2 -2
- package/dist/assets/{SessionSwitcherContent-BTzVmIop.js → SessionSwitcherContent-BL7f43TK.js} +1 -1
- package/dist/assets/SessionSwitcherDropdown-D86K7VFR.js +2 -0
- package/dist/assets/SessionUnreadDot-BfH_vRW9.js +1 -0
- package/dist/assets/SettingsView-CRYyy2nz.js +4 -0
- package/dist/assets/SettingsWindow-CAMrbwzB.js +1 -0
- package/dist/assets/{ToolOutputDialog-BHaf293m.js → ToolOutputDialog-D6AeevDV.js} +6 -6
- package/dist/assets/{ToolPartDiffPreview-BTMXNcqG.js → ToolPartDiffPreview-Bkjc2WKO.js} +2 -2
- package/dist/assets/{VirtualizedFileDiff-CRVvv23V.js → VirtualizedFileDiff-B2HHw9NH.js} +4 -4
- package/dist/assets/{WindowsWindowControls-CTtxXnD1.js → WindowsWindowControls-D1V7M-q1.js} +131 -126
- package/dist/assets/collapsible-IV1T6v7c.js +1 -0
- package/dist/assets/{computeVirtualFileMetrics-D1I8YrPV.js → computeVirtualFileMetrics-BxQbLxY-.js} +1 -1
- package/dist/assets/contextFileOpenGuard-xvnEr8Go.js +1 -0
- package/dist/assets/index--5Zr-fSk.js +8 -0
- package/dist/assets/index--zDkVFU6.css +1 -0
- package/dist/assets/{index-DbghctxK.js → index-Jd0py_uf.js} +1 -1
- package/dist/assets/{isDiffPlainText-BK9bF1l5.js → isDiffPlainText-BEflvkV4.js} +1 -1
- package/dist/assets/{isFilePlainText-Cua0RCRz.js → isFilePlainText-Byt9dFxj.js} +1 -1
- package/dist/assets/{main-CoA--JT-.js → main-DLedH1mh.js} +7 -7
- package/dist/assets/main-DOqxJvVh.js +2 -0
- package/dist/assets/markdownCore-U_gf0I8i.js +3 -0
- package/dist/assets/miniChat-BkwDDO9w.js +2 -0
- package/dist/assets/mobile-BrrYaFh4.js +2 -0
- package/dist/assets/renderElectronMiniChatApp-IM1L48Ek.js +2 -0
- package/dist/assets/renderMobileApp-ChddJ_wj.js +14 -0
- package/dist/assets/{runtimeSurface-UriLeO95.js → runtimeSurface-D3p7a-uO.js} +1 -1
- package/dist/assets/{useInlineCommentController-5XANmO1k.js → useInlineCommentController-BASCWgsn.js} +2 -2
- package/dist/assets/usePanelSlide-UOz-oH7E.js +56 -0
- package/dist/assets/usePushVisibilityBeacon-DAaVsdjo.js +10 -0
- package/dist/assets/{useSwitcherItems-Cc6Tx-LO.js → useSwitcherItems-CAx_SuxF.js} +1 -1
- package/dist/assets/{vendor-codemirror-language-data-DxpwplIe.js → vendor-codemirror-language-data-UQYGQ5y2.js} +2 -2
- package/dist/assets/{web-components-g7DKkne_.js → web-components-D08z3xzy.js} +4 -4
- package/dist/index.html +7 -4
- package/dist/mini-chat.html +6 -3
- package/dist/mobile.html +6 -3
- package/package.json +1 -1
- package/server/lib/git/DOCUMENTATION.md +2 -2
- package/server/lib/git/issue-2746-longpaths.test.js +11 -11
- package/server/lib/git/service.js +16 -53
- package/server/lib/git/service.test.js +40 -40
- package/server/lib/pi/extension-protocol.js +62 -0
- package/server/lib/pi/extension-protocol.test.js +34 -0
- package/server/lib/pi/fixtures/pichamber-gui-demo.ts +251 -0
- package/server/lib/pi/routes-extension-events.test.js +152 -0
- package/server/lib/pi/routes.js +278 -5
- package/server/lib/pi/routes.test.js +0 -25
- package/server/lib/pi/session-daemon/DOCUMENTATION.md +6 -3
- package/server/lib/pi/session-daemon/extension-bridge.js +488 -0
- package/server/lib/pi/session-daemon/session-daemon-extensions.e2e.test.js +215 -0
- package/server/lib/pi/session-daemon/session-daemon-extensions.test.js +559 -0
- package/server/lib/pi/session-daemon/session-daemon.js +304 -34
- package/server/lib/pi/session-daemon/session-daemon.test.js +89 -1
- package/server/lib/pi/small-model-generation.js +14 -1
- package/server/lib/pi/small-model-generation.test.js +20 -5
- package/server/lib/server/cors.js +0 -2
- package/server/lib/server/cors.test.js +4 -4
- package/server/lib/stt/providers/openai-compatible-provider.test.js +1 -1
- package/server/lib/workspace/host.js +1 -1
- package/dist/assets/DiagramEditor-CFV_TjE2.js +0 -1
- package/dist/assets/DiagramView-0I4PZUpw.js +0 -1
- package/dist/assets/SessionSwitcherDropdown-DuPmnk3k.js +0 -2
- package/dist/assets/SessionUnreadDot-ogHL0INY.js +0 -1
- package/dist/assets/SettingsView-SHhVrRdh.js +0 -4
- package/dist/assets/SettingsWindow-D_D1qqLF.js +0 -1
- package/dist/assets/collapsible-BO4qEdib.js +0 -1
- package/dist/assets/contextFileOpenGuard-w5xG9Qz2.js +0 -1
- package/dist/assets/index-3zee2IYy.css +0 -1
- package/dist/assets/index-CI-cd-mw.js +0 -8
- package/dist/assets/main-BNJQ6mSF.js +0 -2
- package/dist/assets/markdownCore-BGlLZrb8.js +0 -3
- package/dist/assets/miniChat-DU2jFhXq.js +0 -2
- package/dist/assets/mobile-B3lJ_CJR.js +0 -2
- package/dist/assets/renderElectronMiniChatApp-BfXYv3zl.js +0 -2
- package/dist/assets/renderMobileApp-Cd_F7yMe.js +0 -14
- package/dist/assets/usePanelSlide-CBZw2Cjo.js +0 -56
- package/dist/assets/useWindowTitle-Bf8ZNN_R.js +0 -10
- package/server/lib/pi/legacy-ui-model-defaults.js +0 -57
- package/server/lib/pi/legacy-ui-model-defaults.test.js +0 -65
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
MAX_EXTENSION_APP_HTML_CHARS,
|
|
5
|
+
sanitizeExtensionFormFields,
|
|
6
|
+
validateExtensionFormValues,
|
|
7
|
+
} from '../extension-protocol.js';
|
|
8
|
+
|
|
9
|
+
const MAX_EXTENSION_PANELS_PER_SESSION = 24;
|
|
10
|
+
const MAX_EXTENSION_APPS_PER_SESSION = 8;
|
|
11
|
+
const MAX_EXTENSION_PANEL_ACTIONS = 8;
|
|
12
|
+
const MAX_EXTENSION_EDITOR_TEXT_CHARS = 100_000;
|
|
13
|
+
const MAX_EXTENSION_TITLE_CHARS = 256;
|
|
14
|
+
const PROVIDER_OBSERVER = Symbol('pichamber.extension-provider-observer');
|
|
15
|
+
const LABEL_OBSERVER = Symbol('pichamber.extension-label-observer');
|
|
16
|
+
|
|
17
|
+
const textFromContent = (content) => (
|
|
18
|
+
Array.isArray(content)
|
|
19
|
+
? content.filter((part) => part?.type === 'text' && typeof part.text === 'string').map((part) => part.text).join('')
|
|
20
|
+
: ''
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
/** Owns extension UI bridge state and translation for one daemon instance. */
|
|
24
|
+
export const createExtensionBridge = ({
|
|
25
|
+
publish,
|
|
26
|
+
resolveDirectory,
|
|
27
|
+
redactAttachmentPaths,
|
|
28
|
+
redactAttachmentValues,
|
|
29
|
+
findRuntimeBySessionId,
|
|
30
|
+
getDefaultDirectory,
|
|
31
|
+
getSequence,
|
|
32
|
+
protocolError,
|
|
33
|
+
requestSessionShutdown,
|
|
34
|
+
}) => {
|
|
35
|
+
const extensionStatusesBySession = new Map();
|
|
36
|
+
const extensionWidgetsBySession = new Map();
|
|
37
|
+
const extensionPanelsBySession = new Map();
|
|
38
|
+
const extensionAppsBySession = new Map();
|
|
39
|
+
const extensionTitlesBySession = new Map();
|
|
40
|
+
// --- Extension bridging -------------------------------------------------
|
|
41
|
+
// Pi extensions run inside each session runtime. Their user-interaction
|
|
42
|
+
// surface (dialogs, notifications, statuses, widgets) is translated here
|
|
43
|
+
// into public stream events; blocking dialogs are resolved by the
|
|
44
|
+
// `extensions.respond` daemon command.
|
|
45
|
+
const pendingExtensionDialogs = new Map();
|
|
46
|
+
|
|
47
|
+
const cancelPendingExtensionDialogs = (sessionId, reason = 'aborted') => {
|
|
48
|
+
for (const pending of pendingExtensionDialogs.values()) {
|
|
49
|
+
if (sessionId !== undefined && pending.sessionId !== sessionId) continue;
|
|
50
|
+
pending.settle({}, reason);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const directoryForSession = (sessionId) => findRuntimeBySessionId(sessionId)?.cwd || getDefaultDirectory();
|
|
55
|
+
const publishForSession = (event, payload, sessionId) => publish(event, payload, sessionId, directoryForSession(sessionId));
|
|
56
|
+
|
|
57
|
+
const clearOneExtensionState = (sessionId) => {
|
|
58
|
+
const statuses = extensionStatusesBySession.get(sessionId);
|
|
59
|
+
const widgets = extensionWidgetsBySession.get(sessionId);
|
|
60
|
+
const panels = extensionPanelsBySession.get(sessionId);
|
|
61
|
+
const apps = extensionAppsBySession.get(sessionId);
|
|
62
|
+
if (statuses) for (const key of statuses.keys()) publishForSession('extension.status', { key }, sessionId);
|
|
63
|
+
if (widgets) for (const key of widgets.keys()) publishForSession('extension.widget', { key }, sessionId);
|
|
64
|
+
if (panels) for (const id of panels.keys()) publishForSession('extension.ui', { id, removed: true }, sessionId);
|
|
65
|
+
if (apps) for (const appId of apps.keys()) publishForSession('extension.app', { appId, removed: true }, sessionId);
|
|
66
|
+
if (extensionTitlesBySession.has(sessionId)) publishForSession('extension.title', {}, sessionId);
|
|
67
|
+
extensionStatusesBySession.delete(sessionId);
|
|
68
|
+
extensionWidgetsBySession.delete(sessionId);
|
|
69
|
+
extensionPanelsBySession.delete(sessionId);
|
|
70
|
+
extensionAppsBySession.delete(sessionId);
|
|
71
|
+
extensionTitlesBySession.delete(sessionId);
|
|
72
|
+
cancelPendingExtensionDialogs(sessionId, 'session-closed');
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const clearExtensionState = (sessionId) => {
|
|
76
|
+
if (sessionId) {
|
|
77
|
+
clearOneExtensionState(sessionId);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const sessionIds = new Set([
|
|
81
|
+
...extensionStatusesBySession.keys(),
|
|
82
|
+
...extensionWidgetsBySession.keys(),
|
|
83
|
+
...extensionPanelsBySession.keys(),
|
|
84
|
+
...extensionAppsBySession.keys(),
|
|
85
|
+
...extensionTitlesBySession.keys(),
|
|
86
|
+
]);
|
|
87
|
+
for (const id of sessionIds) clearOneExtensionState(id);
|
|
88
|
+
cancelPendingExtensionDialogs(undefined, 'daemon-stopped');
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const publishCatalogChange = (sessionId, flags) => {
|
|
92
|
+
publishForSession('extension.catalog', flags, sessionId);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const installMutationObservers = (session) => {
|
|
96
|
+
const modelRuntime = session?.modelRuntime;
|
|
97
|
+
if (modelRuntime) {
|
|
98
|
+
const observer = modelRuntime[PROVIDER_OBSERVER];
|
|
99
|
+
if (observer) {
|
|
100
|
+
observer.sessionId = session.sessionId;
|
|
101
|
+
} else {
|
|
102
|
+
const state = { sessionId: session.sessionId };
|
|
103
|
+
Object.defineProperty(modelRuntime, PROVIDER_OBSERVER, { value: state });
|
|
104
|
+
for (const method of ['registerProvider', 'registerNativeProvider', 'unregisterProvider']) {
|
|
105
|
+
if (typeof modelRuntime[method] !== 'function') continue;
|
|
106
|
+
const original = modelRuntime[method].bind(modelRuntime);
|
|
107
|
+
modelRuntime[method] = (...args) => {
|
|
108
|
+
const result = original(...args);
|
|
109
|
+
if (!state.suppress) publishCatalogChange(state.sessionId, { providers: true });
|
|
110
|
+
return result;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const manager = session?.sessionManager;
|
|
117
|
+
if (manager && !manager[LABEL_OBSERVER] && typeof manager.appendLabelChange === 'function') {
|
|
118
|
+
Object.defineProperty(manager, LABEL_OBSERVER, { value: true });
|
|
119
|
+
const original = manager.appendLabelChange.bind(manager);
|
|
120
|
+
manager.appendLabelChange = (...args) => {
|
|
121
|
+
const result = original(...args);
|
|
122
|
+
publishForSession('session.tree.updated', {}, session.sessionId);
|
|
123
|
+
return result;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const createExtensionUIContext = (sessionId) => {
|
|
129
|
+
const dialog = (method, fields, opts, parseResponse) => {
|
|
130
|
+
const requestId = randomUUID();
|
|
131
|
+
return new Promise((resolve) => {
|
|
132
|
+
const settle = (response, reason = 'answered') => {
|
|
133
|
+
if (pendingExtensionDialogs.get(requestId)?.settle !== settle) return;
|
|
134
|
+
clearTimeout(timer);
|
|
135
|
+
signal?.removeEventListener('abort', onAbort);
|
|
136
|
+
pendingExtensionDialogs.delete(requestId);
|
|
137
|
+
publish('extension.dialog.dismiss', { requestId, reason }, sessionId);
|
|
138
|
+
resolve(parseResponse(response));
|
|
139
|
+
};
|
|
140
|
+
const onAbort = () => settle({}, 'aborted');
|
|
141
|
+
const timer = opts?.timeout
|
|
142
|
+
? setTimeout(() => settle({}, 'timeout'), opts.timeout)
|
|
143
|
+
: undefined;
|
|
144
|
+
const signal = opts?.signal;
|
|
145
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
146
|
+
const payload = {
|
|
147
|
+
requestId,
|
|
148
|
+
method,
|
|
149
|
+
...fields,
|
|
150
|
+
...(Number.isFinite(opts?.timeout) ? { timeoutMs: opts.timeout } : {}),
|
|
151
|
+
};
|
|
152
|
+
pendingExtensionDialogs.set(requestId, { sessionId, settle, timer, payload });
|
|
153
|
+
publish('extension.dialog', payload, sessionId);
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
select: (title, options, opts) => dialog(
|
|
159
|
+
'select',
|
|
160
|
+
{ title, options: options.map((option) => String(option)) },
|
|
161
|
+
opts,
|
|
162
|
+
(response) => (typeof response?.value === 'string' ? response.value : undefined),
|
|
163
|
+
),
|
|
164
|
+
confirm: (title, message, opts) => dialog(
|
|
165
|
+
'confirm',
|
|
166
|
+
{ title, message },
|
|
167
|
+
opts,
|
|
168
|
+
(response) => response?.confirmed === true,
|
|
169
|
+
),
|
|
170
|
+
input: (title, placeholder, opts) => dialog(
|
|
171
|
+
'input',
|
|
172
|
+
{ title, ...(typeof placeholder === 'string' ? { placeholder } : {}) },
|
|
173
|
+
opts,
|
|
174
|
+
(response) => (typeof response?.value === 'string' ? response.value : undefined),
|
|
175
|
+
),
|
|
176
|
+
form: (title, fields, opts) => {
|
|
177
|
+
// PiChamber-specific extension of the pi UI bridge (gate with
|
|
178
|
+
// isPiChamber(ctx)): structured multi-input dialogs resolved with a
|
|
179
|
+
// values object keyed by field id.
|
|
180
|
+
const sanitizedFields = sanitizeExtensionFormFields(fields);
|
|
181
|
+
return dialog(
|
|
182
|
+
'form',
|
|
183
|
+
{ title, ...(sanitizedFields.length > 0 ? { fields: sanitizedFields } : {}) },
|
|
184
|
+
opts,
|
|
185
|
+
(response) => (response?.values && typeof response.values === 'object' && !Array.isArray(response.values)
|
|
186
|
+
? response.values
|
|
187
|
+
: undefined),
|
|
188
|
+
);
|
|
189
|
+
},
|
|
190
|
+
editor: (title, prefill) => dialog(
|
|
191
|
+
'editor',
|
|
192
|
+
{ title, ...(typeof prefill === 'string' ? { prefill } : {}) },
|
|
193
|
+
undefined,
|
|
194
|
+
(response) => (typeof response?.value === 'string' ? response.value : undefined),
|
|
195
|
+
),
|
|
196
|
+
notify: (message, level) => {
|
|
197
|
+
publish('extension.notify', {
|
|
198
|
+
message: String(message ?? ''),
|
|
199
|
+
...(level === 'warning' || level === 'error' ? { level } : { level: 'info' }),
|
|
200
|
+
}, sessionId);
|
|
201
|
+
},
|
|
202
|
+
setStatus: (key, text) => {
|
|
203
|
+
if (typeof key !== 'string' || key.length === 0) return;
|
|
204
|
+
// Mirror into server-side normalized state for snapshot / reconnect.
|
|
205
|
+
const statuses = extensionStatusesBySession.get(sessionId) ?? new Map();
|
|
206
|
+
if (typeof text === 'string' && text.length > 0) statuses.set(key, String(text).slice(0, 1000));
|
|
207
|
+
else statuses.delete(key);
|
|
208
|
+
if (statuses.size === 0) extensionStatusesBySession.delete(sessionId);
|
|
209
|
+
else extensionStatusesBySession.set(sessionId, statuses);
|
|
210
|
+
publish('extension.status', {
|
|
211
|
+
key,
|
|
212
|
+
...(typeof text === 'string' && text.length > 0 ? { text: String(text).slice(0, 1000) } : {}),
|
|
213
|
+
}, sessionId);
|
|
214
|
+
},
|
|
215
|
+
setWidget: (key, content, options) => {
|
|
216
|
+
if (typeof key !== 'string' || key.length === 0) return;
|
|
217
|
+
// Only string-array widgets are representable over the wire.
|
|
218
|
+
if (content !== undefined && !Array.isArray(content)) return;
|
|
219
|
+
const widgets = extensionWidgetsBySession.get(sessionId) ?? new Map();
|
|
220
|
+
if (Array.isArray(content) && content.length > 0) {
|
|
221
|
+
const lines = content.map((line) => String(line).slice(0, 2000)).slice(0, 100);
|
|
222
|
+
const placement = options?.placement === 'belowEditor' ? 'belowEditor' : 'aboveEditor';
|
|
223
|
+
widgets.set(key, { lines, placement });
|
|
224
|
+
} else {
|
|
225
|
+
widgets.delete(key);
|
|
226
|
+
}
|
|
227
|
+
if (widgets.size === 0) extensionWidgetsBySession.delete(sessionId);
|
|
228
|
+
else extensionWidgetsBySession.set(sessionId, widgets);
|
|
229
|
+
publish('extension.widget', {
|
|
230
|
+
key,
|
|
231
|
+
...(Array.isArray(content) && content.length > 0 ? { lines: content.map((line) => String(line).slice(0, 2000)).slice(0, 100) } : {}),
|
|
232
|
+
...(options?.placement === 'belowEditor' ? { placement: 'belowEditor' } : Array.isArray(content) && content.length > 0 ? { placement: 'aboveEditor' } : {}),
|
|
233
|
+
}, sessionId);
|
|
234
|
+
},
|
|
235
|
+
// Terminal-only surfaces have no PiChamber equivalent yet.
|
|
236
|
+
onTerminalInput: () => () => {},
|
|
237
|
+
setWorkingMessage: () => {},
|
|
238
|
+
setWorkingVisible: () => {},
|
|
239
|
+
setWorkingIndicator: () => {},
|
|
240
|
+
setHiddenThinkingLabel: () => {},
|
|
241
|
+
setFooter: () => {},
|
|
242
|
+
setHeader: () => {},
|
|
243
|
+
setTitle: (value) => {
|
|
244
|
+
const title = String(value ?? '').replace(/[\u0000-\u001f\u007f]/g, ' ').trim().slice(0, MAX_EXTENSION_TITLE_CHARS);
|
|
245
|
+
if (title) extensionTitlesBySession.set(sessionId, title);
|
|
246
|
+
else extensionTitlesBySession.delete(sessionId);
|
|
247
|
+
publishForSession('extension.title', title ? { title } : {}, sessionId);
|
|
248
|
+
},
|
|
249
|
+
custom: async () => undefined,
|
|
250
|
+
pasteToEditor: (value) => {
|
|
251
|
+
publishForSession('extension.editor', { text: String(value ?? '').slice(0, MAX_EXTENSION_EDITOR_TEXT_CHARS) }, sessionId);
|
|
252
|
+
},
|
|
253
|
+
setEditorText: (value) => {
|
|
254
|
+
publishForSession('extension.editor', { text: String(value ?? '').slice(0, MAX_EXTENSION_EDITOR_TEXT_CHARS) }, sessionId);
|
|
255
|
+
},
|
|
256
|
+
getEditorText: () => '',
|
|
257
|
+
addAutocompleteProvider: () => {},
|
|
258
|
+
setEditorComponent: () => {},
|
|
259
|
+
getEditorComponent: () => undefined,
|
|
260
|
+
getAllThemes: () => [],
|
|
261
|
+
getTheme: () => undefined,
|
|
262
|
+
setTheme: () => ({ success: false, error: 'Theme switching is not supported in PiChamber sessions.' }),
|
|
263
|
+
getToolsExpanded: () => false,
|
|
264
|
+
setToolsExpanded: () => {},
|
|
265
|
+
// Extensions may style status/widget strings with theme helpers; those
|
|
266
|
+
// strings are rendered as plain text in PiChamber, so pass them through.
|
|
267
|
+
get theme() {
|
|
268
|
+
return identityTheme;
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const identityTheme = {
|
|
274
|
+
fg: (_color, text) => text,
|
|
275
|
+
bg: (_color, text) => text,
|
|
276
|
+
bold: (text) => text,
|
|
277
|
+
italic: (text) => text,
|
|
278
|
+
strikethrough: (text) => text,
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const buildExtensionBindings = (session) => {
|
|
282
|
+
installMutationObservers(session);
|
|
283
|
+
return {
|
|
284
|
+
uiContext: createExtensionUIContext(session.sessionId),
|
|
285
|
+
mode: 'rpc',
|
|
286
|
+
commandContextActions: {
|
|
287
|
+
waitForIdle: () => session.waitForIdle(),
|
|
288
|
+
newSession: async (options) => {
|
|
289
|
+
const owner = findRuntimeBySessionId(session.sessionId);
|
|
290
|
+
if (!owner) throw new Error('Session runtime is no longer available.');
|
|
291
|
+
return owner.newSession(options);
|
|
292
|
+
},
|
|
293
|
+
fork: async (entryId, forkOptions) => {
|
|
294
|
+
const owner = findRuntimeBySessionId(session.sessionId);
|
|
295
|
+
if (!owner) throw new Error('Session runtime is no longer available.');
|
|
296
|
+
const result = await owner.fork(entryId, forkOptions);
|
|
297
|
+
return { cancelled: result.cancelled };
|
|
298
|
+
},
|
|
299
|
+
navigateTree: async (targetId, navigateOptions) => {
|
|
300
|
+
const result = await session.navigateTree(targetId, {
|
|
301
|
+
summarize: navigateOptions?.summarize,
|
|
302
|
+
customInstructions: navigateOptions?.customInstructions,
|
|
303
|
+
replaceInstructions: navigateOptions?.replaceInstructions,
|
|
304
|
+
label: navigateOptions?.label,
|
|
305
|
+
});
|
|
306
|
+
return { cancelled: result.cancelled };
|
|
307
|
+
},
|
|
308
|
+
switchSession: async (sessionPath, switchOptions) => {
|
|
309
|
+
const owner = findRuntimeBySessionId(session.sessionId);
|
|
310
|
+
if (!owner) throw new Error('Session runtime is no longer available.');
|
|
311
|
+
return owner.switchSession(sessionPath, switchOptions);
|
|
312
|
+
},
|
|
313
|
+
reload: async () => {
|
|
314
|
+
clearExtensionState(session.sessionId);
|
|
315
|
+
const providerObserver = session.modelRuntime?.[PROVIDER_OBSERVER];
|
|
316
|
+
if (providerObserver) providerObserver.suppress = true;
|
|
317
|
+
try {
|
|
318
|
+
await session.reload();
|
|
319
|
+
} finally {
|
|
320
|
+
if (providerObserver) providerObserver.suppress = false;
|
|
321
|
+
}
|
|
322
|
+
publishCatalogChange(session.sessionId, { providers: true, resources: true, commands: true });
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
shutdownHandler: () => requestSessionShutdown?.(session.sessionId),
|
|
326
|
+
onError: (error) => {
|
|
327
|
+
publish('extension.error', {
|
|
328
|
+
source: typeof error?.extensionPath === 'string' ? error.extensionPath : 'unknown',
|
|
329
|
+
...(typeof error?.event === 'string' ? { event: error.event } : {}),
|
|
330
|
+
message: String(error?.error ?? 'Unknown extension error.'),
|
|
331
|
+
}, session.sessionId);
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
// Resolves a pending extension dialog. Unknown or already-settled request
|
|
338
|
+
// ids resolve to `{ resolved: false }` instead of throwing: a stale client
|
|
339
|
+
// retry must never tear down the shared daemon socket.
|
|
340
|
+
const resolveExtensionDialog = async (payload) => {
|
|
341
|
+
if (!payload || typeof payload.requestId !== 'string' || payload.requestId.length === 0) {
|
|
342
|
+
throw protocolError('INVALID_ARGUMENT', 'The extension dialog response is invalid.');
|
|
343
|
+
}
|
|
344
|
+
const pending = pendingExtensionDialogs.get(payload.requestId);
|
|
345
|
+
if (!pending) {
|
|
346
|
+
return { resolved: false };
|
|
347
|
+
}
|
|
348
|
+
if (payload.directory !== undefined) await resolveDirectory(payload.directory);
|
|
349
|
+
if (payload.cancelled === true) {
|
|
350
|
+
// Dialog closures derive their typed result (undefined/false) from an
|
|
351
|
+
// empty response, which mirrors a timeout or explicit cancellation.
|
|
352
|
+
pending.settle({}, 'cancelled');
|
|
353
|
+
} else if (payload.confirmed === true) {
|
|
354
|
+
pending.settle({ confirmed: true });
|
|
355
|
+
} else if (typeof payload.value === 'string') {
|
|
356
|
+
pending.settle({ value: payload.value });
|
|
357
|
+
} else if (payload.values && typeof payload.values === 'object' && !Array.isArray(payload.values)) {
|
|
358
|
+
const values = {};
|
|
359
|
+
for (const [key, entry] of Object.entries(payload.values)) {
|
|
360
|
+
if (typeof key === 'string' && key.length > 0 && key.length <= 128 && typeof entry === 'string' && entry.length <= 8_000) {
|
|
361
|
+
values[key] = entry;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (pending.payload?.method !== 'form' || !validateExtensionFormValues(pending.payload.fields, values)) {
|
|
365
|
+
throw protocolError('INVALID_ARGUMENT', 'The extension form response is invalid.');
|
|
366
|
+
}
|
|
367
|
+
pending.settle({ values });
|
|
368
|
+
} else {
|
|
369
|
+
pending.settle({});
|
|
370
|
+
}
|
|
371
|
+
return { resolved: true };
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const publishExtensionCustomMessage = (sessionId, message, directory = getDefaultDirectory()) => {
|
|
375
|
+
if (typeof message.customType !== 'string' || message.customType.length === 0) return;
|
|
376
|
+
// Context-only custom messages (display: false) are not user-visible content.
|
|
377
|
+
if (message.display === false) return;
|
|
378
|
+
const text = typeof message.content === 'string'
|
|
379
|
+
? message.content
|
|
380
|
+
: Array.isArray(message.content)
|
|
381
|
+
? textFromContent(message.content)
|
|
382
|
+
: '';
|
|
383
|
+
const timestamp = Number.isFinite(message.timestamp) ? message.timestamp : Date.now();
|
|
384
|
+
publish('extension.message', {
|
|
385
|
+
id: `custom-${sessionId}-${getSequence() + 1}`,
|
|
386
|
+
customType: message.customType,
|
|
387
|
+
text: redactAttachmentPaths(text),
|
|
388
|
+
...(message.details !== undefined ? { details: redactAttachmentValues(message.details) } : {}),
|
|
389
|
+
createdAt: timestamp,
|
|
390
|
+
}, sessionId, directory);
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// Mirrors a declarative `pichamber.ui` descriptor into normalized panel
|
|
394
|
+
// state and publishes an `extension.ui` event. Latest wins per stable id;
|
|
395
|
+
// `removed: true` (or a payload without component/title) unregisters.
|
|
396
|
+
const mirrorExtensionPanel = (sessionId, descriptor, directory) => {
|
|
397
|
+
const id = typeof descriptor.id === 'string' && descriptor.id.length > 0 ? descriptor.id.slice(0, 128) : '';
|
|
398
|
+
if (!id) return;
|
|
399
|
+
const hasBody = typeof descriptor.component === 'string'
|
|
400
|
+
|| typeof descriptor.title === 'string'
|
|
401
|
+
|| Array.isArray(descriptor.actions);
|
|
402
|
+
const removed = descriptor.removed === true || !hasBody;
|
|
403
|
+
const panels = extensionPanelsBySession.get(sessionId) ?? new Map();
|
|
404
|
+
const normalized = removed ? undefined : {
|
|
405
|
+
id,
|
|
406
|
+
...(typeof descriptor.title === 'string' ? { title: descriptor.title.slice(0, 256) } : {}),
|
|
407
|
+
...(typeof descriptor.component === 'string' ? { component: descriptor.component.slice(0, 64) } : {}),
|
|
408
|
+
...(descriptor.props && typeof descriptor.props === 'object' && !Array.isArray(descriptor.props) ? { props: redactAttachmentValues(descriptor.props) } : {}),
|
|
409
|
+
...(Array.isArray(descriptor.actions) ? { actions: descriptor.actions.slice(0, MAX_EXTENSION_PANEL_ACTIONS) } : {}),
|
|
410
|
+
};
|
|
411
|
+
if (removed) {
|
|
412
|
+
panels.delete(id);
|
|
413
|
+
} else {
|
|
414
|
+
panels.set(id, normalized);
|
|
415
|
+
}
|
|
416
|
+
if (panels.size > MAX_EXTENSION_PANELS_PER_SESSION) {
|
|
417
|
+
const oldest = [...panels.keys()].slice(0, panels.size - MAX_EXTENSION_PANELS_PER_SESSION);
|
|
418
|
+
for (const key of oldest) panels.delete(key);
|
|
419
|
+
}
|
|
420
|
+
if (panels.size === 0) extensionPanelsBySession.delete(sessionId);
|
|
421
|
+
else extensionPanelsBySession.set(sessionId, panels);
|
|
422
|
+
publish('extension.ui', removed ? { id, removed: true } : normalized, sessionId, directory);
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
// Mirrors a `pichamber.app` descriptor into normalized app state and
|
|
426
|
+
// publishes an `extension.app` event. HTML is capped; removal unregisters.
|
|
427
|
+
const mirrorExtensionApp = (sessionId, descriptor, directory) => {
|
|
428
|
+
const appId = typeof descriptor.appId === 'string' && descriptor.appId.length > 0 ? descriptor.appId.slice(0, 128) : '';
|
|
429
|
+
if (!appId) return;
|
|
430
|
+
const html = typeof descriptor.html === 'string'
|
|
431
|
+
? (descriptor.html.length > MAX_EXTENSION_APP_HTML_CHARS ? descriptor.html.slice(0, MAX_EXTENSION_APP_HTML_CHARS) : descriptor.html)
|
|
432
|
+
: undefined;
|
|
433
|
+
const removed = descriptor.removed === true || !html || html.length === 0;
|
|
434
|
+
const apps = extensionAppsBySession.get(sessionId) ?? new Map();
|
|
435
|
+
if (removed) {
|
|
436
|
+
apps.delete(appId);
|
|
437
|
+
} else {
|
|
438
|
+
apps.set(appId, {
|
|
439
|
+
appId,
|
|
440
|
+
...(typeof descriptor.title === 'string' ? { title: descriptor.title.slice(0, 256) } : {}),
|
|
441
|
+
html,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
if (apps.size > MAX_EXTENSION_APPS_PER_SESSION) {
|
|
445
|
+
const oldest = [...apps.keys()].slice(0, apps.size - MAX_EXTENSION_APPS_PER_SESSION);
|
|
446
|
+
for (const key of oldest) apps.delete(key);
|
|
447
|
+
}
|
|
448
|
+
if (apps.size === 0) extensionAppsBySession.delete(sessionId);
|
|
449
|
+
else extensionAppsBySession.set(sessionId, apps);
|
|
450
|
+
publish('extension.app', {
|
|
451
|
+
appId,
|
|
452
|
+
...(removed ? { removed: true } : {
|
|
453
|
+
...(typeof descriptor.title === 'string' ? { title: descriptor.title.slice(0, 256) } : {}),
|
|
454
|
+
html,
|
|
455
|
+
}),
|
|
456
|
+
}, sessionId, directory);
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
const getSnapshotState = (sessionId) => {
|
|
460
|
+
if (!sessionId) return {};
|
|
461
|
+
const statuses = extensionStatusesBySession.get(sessionId);
|
|
462
|
+
const widgets = extensionWidgetsBySession.get(sessionId);
|
|
463
|
+
const panels = extensionPanelsBySession.get(sessionId);
|
|
464
|
+
const apps = extensionAppsBySession.get(sessionId);
|
|
465
|
+
const title = extensionTitlesBySession.get(sessionId);
|
|
466
|
+
const dialogs = [...pendingExtensionDialogs.values()]
|
|
467
|
+
.filter((pending) => pending.sessionId === sessionId)
|
|
468
|
+
.map((pending) => pending.payload);
|
|
469
|
+
return {
|
|
470
|
+
...(statuses?.size ? { statuses: [...statuses.entries()].map(([key, text]) => ({ key, text })) } : {}),
|
|
471
|
+
...(widgets?.size ? { widgets: [...widgets.entries()].map(([key, widget]) => ({ key, ...widget })) } : {}),
|
|
472
|
+
...(dialogs.length ? { dialogs } : {}),
|
|
473
|
+
...(panels?.size ? { panels: [...panels.values()] } : {}),
|
|
474
|
+
...(apps?.size ? { apps: [...apps.values()] } : {}),
|
|
475
|
+
...(title ? { title } : {}),
|
|
476
|
+
};
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
return {
|
|
480
|
+
buildExtensionBindings,
|
|
481
|
+
clearExtensionState,
|
|
482
|
+
getSnapshotState,
|
|
483
|
+
mirrorExtensionApp,
|
|
484
|
+
mirrorExtensionPanel,
|
|
485
|
+
publishExtensionCustomMessage,
|
|
486
|
+
resolveExtensionDialog,
|
|
487
|
+
};
|
|
488
|
+
};
|