opencode-sidechat 1.0.0 → 1.1.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/README.md +12 -3
- package/package.json +1 -1
- package/src/components/SideChat.tsx +428 -348
- package/src/config.ts +198 -189
- package/src/constants.ts +47 -45
- package/src/index.tsx +414 -379
- package/src/session.ts +260 -257
- package/src/types.ts +57 -54
package/src/index.tsx
CHANGED
|
@@ -1,379 +1,414 @@
|
|
|
1
|
-
/** @jsxImportSource @opentui/solid */
|
|
2
|
-
import type { TuiPlugin, TuiPluginModule } from "@opencode-ai/plugin/tui";
|
|
3
|
-
import { createSignal, Show } from "solid-js";
|
|
4
|
-
import { loadConfig } from "./config";
|
|
5
|
-
import { SideChat } from "./components/SideChat";
|
|
6
|
-
import {
|
|
7
|
-
CMD_TOGGLE_FOCUS,
|
|
8
|
-
CMD_CLEAR,
|
|
9
|
-
CMD_CHANGE_MODEL,
|
|
10
|
-
CMD_TOGGLE_THINK,
|
|
11
|
-
PLUGIN_ID,
|
|
12
|
-
} from "./constants";
|
|
13
|
-
import {
|
|
14
|
-
getAvailableToolIDs,
|
|
15
|
-
resolveAllowedTools,
|
|
16
|
-
buildToolSelection,
|
|
17
|
-
buildPermissionRules,
|
|
18
|
-
buildSideSystemPrompt,
|
|
19
|
-
resolveModel,
|
|
20
|
-
formatPreference,
|
|
21
|
-
openModelPicker,
|
|
22
|
-
getErrorMessage,
|
|
23
|
-
} from "./session";
|
|
24
|
-
import type { SideDialogState, ModelPreference } from "./types";
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const tui: TuiPlugin = async (api, _options) => {
|
|
29
|
-
const config = loadConfig();
|
|
30
|
-
const keybind = config.keybind;
|
|
31
|
-
const clearKeybind = config.clearKeybind;
|
|
32
|
-
const thinkToggleKeybind = config.thinkToggleKeybind;
|
|
33
|
-
|
|
34
|
-
const [state, setState] = createSignal<SideDialogState>({
|
|
35
|
-
entries: [],
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const [tempSessionID, setTempSessionID] = createSignal<string | undefined>(undefined);
|
|
43
|
-
const [selectedModel, setSelectedModel] = createSignal<ModelPreference>(undefined);
|
|
44
|
-
const [visible, setVisible] = createSignal(false);
|
|
45
|
-
const [thinkCollapsed, setThinkCollapsed] = createSignal(config.think.defaultState === "collapsed");
|
|
46
|
-
|
|
47
|
-
let overlayInput: { focus: () => void } | undefined;
|
|
48
|
-
let
|
|
49
|
-
let
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
:
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
:
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
:
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
import type { TuiPlugin, TuiPluginModule } from "@opencode-ai/plugin/tui";
|
|
3
|
+
import { createSignal, Show, ErrorBoundary } from "solid-js";
|
|
4
|
+
import { loadConfig } from "./config";
|
|
5
|
+
import { SideChat } from "./components/SideChat";
|
|
6
|
+
import {
|
|
7
|
+
CMD_TOGGLE_FOCUS,
|
|
8
|
+
CMD_CLEAR,
|
|
9
|
+
CMD_CHANGE_MODEL,
|
|
10
|
+
CMD_TOGGLE_THINK,
|
|
11
|
+
PLUGIN_ID,
|
|
12
|
+
} from "./constants";
|
|
13
|
+
import {
|
|
14
|
+
getAvailableToolIDs,
|
|
15
|
+
resolveAllowedTools,
|
|
16
|
+
buildToolSelection,
|
|
17
|
+
buildPermissionRules,
|
|
18
|
+
buildSideSystemPrompt,
|
|
19
|
+
resolveModel,
|
|
20
|
+
formatPreference,
|
|
21
|
+
openModelPicker,
|
|
22
|
+
getErrorMessage,
|
|
23
|
+
} from "./session";
|
|
24
|
+
import type { SideDialogState, ModelPreference } from "./types";
|
|
25
|
+
|
|
26
|
+
const PROMPT_TIMEOUT_MS = 120_000;
|
|
27
|
+
|
|
28
|
+
const tui: TuiPlugin = async (api, _options) => {
|
|
29
|
+
const config = loadConfig();
|
|
30
|
+
const keybind = config.keybind;
|
|
31
|
+
const clearKeybind = config.clearKeybind;
|
|
32
|
+
const thinkToggleKeybind = config.thinkToggleKeybind;
|
|
33
|
+
|
|
34
|
+
const [state, setState] = createSignal<SideDialogState>({
|
|
35
|
+
entries: [],
|
|
36
|
+
loading: false,
|
|
37
|
+
error: undefined,
|
|
38
|
+
tokenCount: 0,
|
|
39
|
+
});
|
|
40
|
+
const [streamingAnswer, setStreamingAnswer] = createSignal("", { equals: false });
|
|
41
|
+
|
|
42
|
+
const [tempSessionID, setTempSessionID] = createSignal<string | undefined>(undefined);
|
|
43
|
+
const [selectedModel, setSelectedModel] = createSignal<ModelPreference>(undefined);
|
|
44
|
+
const [visible, setVisible] = createSignal(false);
|
|
45
|
+
const [thinkCollapsed, setThinkCollapsed] = createSignal(config.think.defaultState === "collapsed");
|
|
46
|
+
|
|
47
|
+
let overlayInput: { focus: () => void } | undefined;
|
|
48
|
+
let previousFocus: import("@opentui/core").Renderable | null = null;
|
|
49
|
+
let unsubscribers: Array<() => void> = [];
|
|
50
|
+
let sessionInitPromise: Promise<string | undefined> | undefined;
|
|
51
|
+
let promptTimeout: ReturnType<typeof setTimeout> | undefined;
|
|
52
|
+
let cachedToolIDs: string[] | undefined;
|
|
53
|
+
let cachedPromptResult:
|
|
54
|
+
| { system: string; tools: Record<string, boolean>; permission: any[] }
|
|
55
|
+
| undefined;
|
|
56
|
+
|
|
57
|
+
const getModelName = () =>
|
|
58
|
+
formatPreference(
|
|
59
|
+
selectedModel() ?? resolveModel(config.model, state().entries, api).model,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const clearListeners = () => {
|
|
63
|
+
while (unsubscribers.length > 0) {
|
|
64
|
+
try { unsubscribers.pop()?.(); } catch (err) { console.error("[SideChat] listener cleanup:", err); }
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const refreshSession = () => {
|
|
69
|
+
const sid = tempSessionID();
|
|
70
|
+
if (!sid) return;
|
|
71
|
+
try {
|
|
72
|
+
const messages = api.state.session.messages(sid);
|
|
73
|
+
const entries: SideDialogState["entries"] = [];
|
|
74
|
+
let tokenCount = 0;
|
|
75
|
+
for (const info of messages) {
|
|
76
|
+
entries.push({ info, parts: [...(api.state.part(info.id) ?? [])] });
|
|
77
|
+
if ("tokens" in info && info.tokens) {
|
|
78
|
+
tokenCount += (info.tokens.input ?? 0) + (info.tokens.output ?? 0);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const MAX_STORED_ENTRIES = 100;
|
|
82
|
+
setState((s) => ({ ...s, entries: entries.slice(-MAX_STORED_ENTRIES), tokenCount }));
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.error("[SideChat] refreshSession failed:", err);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const buildSystemPrompt = async () => {
|
|
89
|
+
if (!cachedToolIDs) {
|
|
90
|
+
cachedToolIDs = await getAvailableToolIDs(api);
|
|
91
|
+
}
|
|
92
|
+
const toolIDs = cachedToolIDs;
|
|
93
|
+
const resolvedTools = resolveAllowedTools(config.allowedTools, toolIDs);
|
|
94
|
+
const result = {
|
|
95
|
+
system: buildSideSystemPrompt(config.systemPrompt, resolvedTools),
|
|
96
|
+
toolIDs,
|
|
97
|
+
resolvedTools,
|
|
98
|
+
tools: buildToolSelection(toolIDs, resolvedTools),
|
|
99
|
+
permission: buildPermissionRules(toolIDs, resolvedTools),
|
|
100
|
+
};
|
|
101
|
+
cachedPromptResult = { system: result.system, tools: result.tools, permission: result.permission };
|
|
102
|
+
return result;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const initSession = async (): Promise<string | undefined> => {
|
|
106
|
+
clearListeners();
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
const { permission } = await buildSystemPrompt();
|
|
110
|
+
|
|
111
|
+
const created = await api.client.session.create(
|
|
112
|
+
{
|
|
113
|
+
title: "side chat",
|
|
114
|
+
directory: api.state.path.directory,
|
|
115
|
+
permission,
|
|
116
|
+
},
|
|
117
|
+
{ throwOnError: true },
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const sid = created.data.id;
|
|
121
|
+
setTempSessionID(sid);
|
|
122
|
+
|
|
123
|
+
unsubscribers.push(
|
|
124
|
+
api.event.on("session.idle", (event) => {
|
|
125
|
+
if (event.properties.sessionID !== sid) return;
|
|
126
|
+
if (promptTimeout) { clearTimeout(promptTimeout); promptTimeout = undefined; }
|
|
127
|
+
refreshSession();
|
|
128
|
+
setState((s) => ({
|
|
129
|
+
...s,
|
|
130
|
+
loading: false,
|
|
131
|
+
}));
|
|
132
|
+
setStreamingAnswer("");
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
unsubscribers.push(
|
|
137
|
+
api.event.on("message.updated", (event) => {
|
|
138
|
+
if (event.properties.sessionID !== sid) return;
|
|
139
|
+
refreshSession();
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
unsubscribers.push(
|
|
144
|
+
api.event.on("message.part.delta", (event) => {
|
|
145
|
+
if (
|
|
146
|
+
event.properties.sessionID !== sid ||
|
|
147
|
+
event.properties.field !== "text" ||
|
|
148
|
+
!state().loading
|
|
149
|
+
) return;
|
|
150
|
+
setStreamingAnswer((prev) => prev + event.properties.delta);
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
unsubscribers.push(
|
|
155
|
+
api.event.on("message.part.updated", (event) => {
|
|
156
|
+
if (event.properties.sessionID !== sid) return;
|
|
157
|
+
refreshSession();
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
unsubscribers.push(
|
|
162
|
+
api.event.on("session.error", (event) => {
|
|
163
|
+
if (event.properties.sessionID !== sid) return;
|
|
164
|
+
if (promptTimeout) { clearTimeout(promptTimeout); promptTimeout = undefined; }
|
|
165
|
+
setState((s) => ({
|
|
166
|
+
...s,
|
|
167
|
+
error: getErrorMessage(event.properties.error),
|
|
168
|
+
loading: false,
|
|
169
|
+
}));
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
setState((s) => ({ ...s, error: undefined }));
|
|
174
|
+
return sid;
|
|
175
|
+
} catch (cause) {
|
|
176
|
+
const msg = getErrorMessage(cause);
|
|
177
|
+
setState((s) => ({ ...s, error: msg }));
|
|
178
|
+
sessionInitPromise = undefined;
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const ensureSession = (): Promise<string | undefined> => {
|
|
184
|
+
if (tempSessionID()) return Promise.resolve(tempSessionID());
|
|
185
|
+
if (!sessionInitPromise) sessionInitPromise = initSession();
|
|
186
|
+
return sessionInitPromise;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const destroySession = async () => {
|
|
190
|
+
const sid = tempSessionID();
|
|
191
|
+
if (!sid) return;
|
|
192
|
+
setTempSessionID(undefined);
|
|
193
|
+
clearListeners();
|
|
194
|
+
try {
|
|
195
|
+
await api.client.session.abort(
|
|
196
|
+
{ sessionID: sid },
|
|
197
|
+
{ throwOnError: true },
|
|
198
|
+
);
|
|
199
|
+
} catch (err) { console.error("[SideChat] session abort:", err); }
|
|
200
|
+
try {
|
|
201
|
+
await api.client.session.delete(
|
|
202
|
+
{ sessionID: sid },
|
|
203
|
+
{ throwOnError: true },
|
|
204
|
+
);
|
|
205
|
+
} catch (err) { console.error("[SideChat] session delete:", err); }
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const handleSubmit = (text: string): boolean => {
|
|
209
|
+
if (state().loading) return false;
|
|
210
|
+
|
|
211
|
+
setState((s) => ({
|
|
212
|
+
...s,
|
|
213
|
+
error: undefined,
|
|
214
|
+
loading: true,
|
|
215
|
+
}));
|
|
216
|
+
setStreamingAnswer("");
|
|
217
|
+
|
|
218
|
+
if (promptTimeout) clearTimeout(promptTimeout);
|
|
219
|
+
promptTimeout = setTimeout(() => {
|
|
220
|
+
setState((s) => s.loading ? { ...s, loading: false, error: "Request timed out." } : s);
|
|
221
|
+
}, PROMPT_TIMEOUT_MS);
|
|
222
|
+
|
|
223
|
+
void ensureSession().then((sid) => {
|
|
224
|
+
if (!sid) {
|
|
225
|
+
setState((s) => ({
|
|
226
|
+
...s,
|
|
227
|
+
error: "Failed to create session.",
|
|
228
|
+
loading: false,
|
|
229
|
+
}));
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
void (async () => {
|
|
234
|
+
try {
|
|
235
|
+
const { system, tools } = cachedPromptResult ?? await buildSystemPrompt();
|
|
236
|
+
const resolved =
|
|
237
|
+
selectedModel() ??
|
|
238
|
+
resolveModel(config.model, state().entries, api).model;
|
|
239
|
+
|
|
240
|
+
await api.client.session.promptAsync(
|
|
241
|
+
{
|
|
242
|
+
sessionID: sid,
|
|
243
|
+
system,
|
|
244
|
+
tools,
|
|
245
|
+
parts: [{ type: "text", text }],
|
|
246
|
+
...(resolved?.model ? { model: resolved.model } : {}),
|
|
247
|
+
...(resolved?.variant ? { variant: resolved.variant } : {}),
|
|
248
|
+
},
|
|
249
|
+
{ throwOnError: true },
|
|
250
|
+
);
|
|
251
|
+
} catch (cause) {
|
|
252
|
+
setState((s) => ({
|
|
253
|
+
...s,
|
|
254
|
+
error: getErrorMessage(cause),
|
|
255
|
+
loading: false,
|
|
256
|
+
}));
|
|
257
|
+
}
|
|
258
|
+
})();
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
return true;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const handleClear = async () => {
|
|
265
|
+
await destroySession();
|
|
266
|
+
setState({
|
|
267
|
+
entries: [],
|
|
268
|
+
loading: false,
|
|
269
|
+
error: undefined,
|
|
270
|
+
tokenCount: 0,
|
|
271
|
+
});
|
|
272
|
+
setStreamingAnswer("");
|
|
273
|
+
sessionInitPromise = undefined;
|
|
274
|
+
setThinkCollapsed(config.think.defaultState === "collapsed");
|
|
275
|
+
await ensureSession();
|
|
276
|
+
if (visible()) {
|
|
277
|
+
setTimeout(() => overlayInput?.focus(), 0);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const handleToggle = () => {
|
|
282
|
+
const currentRoute = api.route.current;
|
|
283
|
+
if (currentRoute.name !== "session" && !visible()) return;
|
|
284
|
+
const wasVisible = visible();
|
|
285
|
+
if (!wasVisible) {
|
|
286
|
+
previousFocus = api.renderer.currentFocusedRenderable;
|
|
287
|
+
}
|
|
288
|
+
setVisible(!wasVisible);
|
|
289
|
+
if (wasVisible && previousFocus) {
|
|
290
|
+
const restore = previousFocus;
|
|
291
|
+
previousFocus = null;
|
|
292
|
+
setTimeout(() => {
|
|
293
|
+
try { restore.focus(); } catch {}
|
|
294
|
+
}, 50);
|
|
295
|
+
} else if (!wasVisible) {
|
|
296
|
+
setTimeout(() => overlayInput?.focus(), 50);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const handleToggleThink = () => {
|
|
301
|
+
setThinkCollapsed((prev) => !prev);
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const handleChangeModel = () => {
|
|
305
|
+
const currentRoute = api.route.current;
|
|
306
|
+
if (currentRoute.name !== "session") return;
|
|
307
|
+
openModelPicker(api, config, selectedModel(), (model) => {
|
|
308
|
+
setSelectedModel(model);
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
api.lifecycle.onDispose(() => {
|
|
313
|
+
clearListeners();
|
|
314
|
+
void destroySession();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
api.slots.register({
|
|
318
|
+
slots: {
|
|
319
|
+
app: () => (
|
|
320
|
+
<Show when={visible()}>
|
|
321
|
+
<ErrorBoundary fallback={(err) => <text>{String(err)}</text>}>
|
|
322
|
+
<SideChat
|
|
323
|
+
api={api}
|
|
324
|
+
modelName={getModelName()}
|
|
325
|
+
state={state()}
|
|
326
|
+
streamingAnswer={streamingAnswer()}
|
|
327
|
+
width={config.width}
|
|
328
|
+
transcriptHeight={config.transcriptHeight}
|
|
329
|
+
tokenLimit={config.tokenLimit}
|
|
330
|
+
thinkCollapsed={thinkCollapsed()}
|
|
331
|
+
thinkConfig={config.think}
|
|
332
|
+
keybind={config.keybind}
|
|
333
|
+
clearKeybind={config.clearKeybind}
|
|
334
|
+
thinkToggleKeybind={config.thinkToggleKeybind}
|
|
335
|
+
onInput={(node) => { overlayInput = node; }}
|
|
336
|
+
onChangeModel={handleChangeModel}
|
|
337
|
+
onSubmit={handleSubmit}
|
|
338
|
+
/>
|
|
339
|
+
</ErrorBoundary>
|
|
340
|
+
</Show>
|
|
341
|
+
),
|
|
342
|
+
},
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
api.keymap.registerLayer({
|
|
346
|
+
commands: [
|
|
347
|
+
{
|
|
348
|
+
namespace: "palette",
|
|
349
|
+
name: CMD_TOGGLE_FOCUS,
|
|
350
|
+
title: "side",
|
|
351
|
+
desc: "Open/side chat overlay",
|
|
352
|
+
category: "Plugin",
|
|
353
|
+
slashName: "side",
|
|
354
|
+
enabled: () => api.route.current.name === "session" || visible(),
|
|
355
|
+
run: () => handleToggle(),
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
namespace: "palette",
|
|
359
|
+
name: CMD_CLEAR,
|
|
360
|
+
title: "side clear",
|
|
361
|
+
desc: "Clear the side chat conversation",
|
|
362
|
+
category: "Plugin",
|
|
363
|
+
slashName: "side-clear",
|
|
364
|
+
enabled: () => api.route.current.name === "session",
|
|
365
|
+
run: () => void handleClear(),
|
|
366
|
+
},
|
|
367
|
+
{
|
|
368
|
+
namespace: "palette",
|
|
369
|
+
name: CMD_CHANGE_MODEL,
|
|
370
|
+
title: "side model",
|
|
371
|
+
desc: "Change the side chat model",
|
|
372
|
+
category: "Plugin",
|
|
373
|
+
slashName: "side-model",
|
|
374
|
+
enabled: () => api.route.current.name === "session",
|
|
375
|
+
run: () => handleChangeModel(),
|
|
376
|
+
},
|
|
377
|
+
],
|
|
378
|
+
bindings: [
|
|
379
|
+
...(keybind !== false
|
|
380
|
+
? [{
|
|
381
|
+
key: keybind,
|
|
382
|
+
cmd: CMD_TOGGLE_FOCUS,
|
|
383
|
+
desc: "Toggle side chat",
|
|
384
|
+
}]
|
|
385
|
+
: []),
|
|
386
|
+
],
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
api.keymap.registerLayer({
|
|
390
|
+
priority: 1000,
|
|
391
|
+
enabled: () => visible(),
|
|
392
|
+
commands: [
|
|
393
|
+
{ name: CMD_CLEAR, run: () => void handleClear() },
|
|
394
|
+
{ name: CMD_CHANGE_MODEL, run: () => handleChangeModel() },
|
|
395
|
+
{ name: CMD_TOGGLE_THINK, run: () => handleToggleThink() },
|
|
396
|
+
],
|
|
397
|
+
bindings: [
|
|
398
|
+
...(clearKeybind !== false
|
|
399
|
+
? [{ key: clearKeybind, cmd: CMD_CLEAR }]
|
|
400
|
+
: []),
|
|
401
|
+
...(thinkToggleKeybind !== false
|
|
402
|
+
? [{ key: thinkToggleKeybind, cmd: CMD_TOGGLE_THINK }]
|
|
403
|
+
: []),
|
|
404
|
+
{ key: "tab", cmd: CMD_CHANGE_MODEL },
|
|
405
|
+
],
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
const plugin: TuiPluginModule & { id: string } = {
|
|
410
|
+
id: PLUGIN_ID,
|
|
411
|
+
tui,
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
export default plugin;
|