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
|
@@ -1,348 +1,428 @@
|
|
|
1
|
-
/** @jsxImportSource @opentui/solid */
|
|
2
|
-
import { createMemo, For } from "solid-js";
|
|
3
|
-
import type { InputRenderable } from "@opentui/core";
|
|
4
|
-
import { THINKING_TEXT } from "../constants";
|
|
5
|
-
import type { OverlayState } from "../types";
|
|
6
|
-
|
|
7
|
-
const MAX_VISIBLE_MESSAGES = 20;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
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
|
-
return
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
const
|
|
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
|
-
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
import { createMemo, For } from "solid-js";
|
|
3
|
+
import type { InputRenderable } from "@opentui/core";
|
|
4
|
+
import { THINKING_TEXT } from "../constants";
|
|
5
|
+
import type { OverlayState } from "../types";
|
|
6
|
+
|
|
7
|
+
const MAX_VISIBLE_MESSAGES = 20;
|
|
8
|
+
|
|
9
|
+
type InlinePart =
|
|
10
|
+
| { type: "text" | "code" | "bold" | "italic"; text: string }
|
|
11
|
+
| { type: "link"; text: string; url: string };
|
|
12
|
+
|
|
13
|
+
function renderInlineMarkdown(text: string) {
|
|
14
|
+
const parts: InlinePart[] = [];
|
|
15
|
+
let remaining = text;
|
|
16
|
+
|
|
17
|
+
while (remaining.length > 0) {
|
|
18
|
+
if (remaining.startsWith("`")) {
|
|
19
|
+
const end = remaining.indexOf("`", 1);
|
|
20
|
+
if (end === -1) {
|
|
21
|
+
parts.push({ type: "text", text: remaining });
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
parts.push({ type: "code", text: remaining.slice(1, end) });
|
|
25
|
+
remaining = remaining.slice(end + 1);
|
|
26
|
+
} else if (remaining.startsWith("**")) {
|
|
27
|
+
const end = remaining.indexOf("**", 2);
|
|
28
|
+
if (end === -1) {
|
|
29
|
+
parts.push({ type: "text", text: remaining });
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
parts.push({ type: "bold", text: remaining.slice(2, end) });
|
|
33
|
+
remaining = remaining.slice(end + 2);
|
|
34
|
+
} else if (remaining.startsWith("*") && !remaining.startsWith("**")) {
|
|
35
|
+
const end = remaining.indexOf("*", 1);
|
|
36
|
+
if (end === -1) {
|
|
37
|
+
parts.push({ type: "text", text: remaining });
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
parts.push({ type: "italic", text: remaining.slice(1, end) });
|
|
41
|
+
remaining = remaining.slice(end + 1);
|
|
42
|
+
} else if (remaining.startsWith("[") && remaining.includes("](")) {
|
|
43
|
+
const closeBracket = remaining.indexOf("](");
|
|
44
|
+
const closeParen = remaining.indexOf(")", closeBracket);
|
|
45
|
+
if (closeParen === -1) {
|
|
46
|
+
parts.push({ type: "text", text: remaining });
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
const linkText = remaining.slice(1, closeBracket);
|
|
50
|
+
const linkUrl = remaining.slice(closeBracket + 2, closeParen);
|
|
51
|
+
parts.push({ type: "link", text: linkText, url: linkUrl });
|
|
52
|
+
remaining = remaining.slice(closeParen + 1);
|
|
53
|
+
} else {
|
|
54
|
+
const nextSpecial = searchSpecialChar(remaining);
|
|
55
|
+
if (nextSpecial === -1) {
|
|
56
|
+
parts.push({ type: "text", text: remaining });
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
if (nextSpecial > 0) {
|
|
60
|
+
parts.push({ type: "text", text: remaining.slice(0, nextSpecial) });
|
|
61
|
+
}
|
|
62
|
+
remaining = remaining.slice(nextSpecial);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return parts;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function searchSpecialChar(text: string): number {
|
|
70
|
+
for (let i = 0; i < text.length; i++) {
|
|
71
|
+
const c = text[i];
|
|
72
|
+
if (c === "`" || c === "*" || (c === "[" && text.includes("](", i))) {
|
|
73
|
+
return i;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return -1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function formatKeybind(kb: string | false): string | false {
|
|
80
|
+
if (kb === false) return false;
|
|
81
|
+
return kb
|
|
82
|
+
.split(/[+]/)
|
|
83
|
+
.map(p => p.charAt(0).toUpperCase() + p.slice(1))
|
|
84
|
+
.join("+");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function SideChat(props: OverlayState & { width: number; transcriptHeight: number; tokenLimit: number }) {
|
|
88
|
+
const theme = props.api.theme.current;
|
|
89
|
+
let input: InputRenderable | undefined;
|
|
90
|
+
let inputValue = "";
|
|
91
|
+
|
|
92
|
+
const panelWidth = props.width;
|
|
93
|
+
const contentWidth = props.width - 4;
|
|
94
|
+
|
|
95
|
+
const msgs = createMemo(() => {
|
|
96
|
+
const messages = props.state.entries
|
|
97
|
+
.map((entry) => {
|
|
98
|
+
const textParts: string[] = [];
|
|
99
|
+
const reasoning: Array<{ id: string; text: string }> = [];
|
|
100
|
+
|
|
101
|
+
for (const p of entry.parts) {
|
|
102
|
+
if (p.type === "text") {
|
|
103
|
+
if (p.text.trim()) textParts.push(p.text.trim());
|
|
104
|
+
} else if (p.type === "reasoning") {
|
|
105
|
+
if (p.text) reasoning.push({ id: p.id, text: p.text });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (textParts.length === 0 && reasoning.length === 0) return null;
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
id: entry.info.id,
|
|
113
|
+
role: entry.info.role as "user" | "assistant",
|
|
114
|
+
text: textParts.join("\n"),
|
|
115
|
+
reasoning,
|
|
116
|
+
};
|
|
117
|
+
})
|
|
118
|
+
.filter((m): m is NonNullable<typeof m> => m !== null)
|
|
119
|
+
.slice(-MAX_VISIBLE_MESSAGES);
|
|
120
|
+
|
|
121
|
+
if (props.state.loading && props.streamingAnswer) {
|
|
122
|
+
const streaming = props.streamingAnswer.trim();
|
|
123
|
+
const last = messages[messages.length - 1];
|
|
124
|
+
const lastText = last?.role === "assistant" ? last.text : "";
|
|
125
|
+
if (streaming && streaming !== lastText) {
|
|
126
|
+
messages.push({
|
|
127
|
+
id: "__streaming__",
|
|
128
|
+
role: "assistant",
|
|
129
|
+
text: streaming,
|
|
130
|
+
reasoning: [],
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return messages;
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const ctxLabel = createMemo(() => {
|
|
139
|
+
const n = props.state.tokenCount ?? 0;
|
|
140
|
+
if (n <= 0) return "";
|
|
141
|
+
const current = n >= 1000 ? (n / 1000).toFixed(1) + "k" : String(n);
|
|
142
|
+
const limit = props.tokenLimit >= 1000 ? (props.tokenLimit / 1000).toFixed(0) + "k" : String(props.tokenLimit);
|
|
143
|
+
return current + "/" + limit + " ctx";
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const shortModelName = createMemo(() => {
|
|
147
|
+
const name = props.modelName;
|
|
148
|
+
const parts = name.split("/");
|
|
149
|
+
return parts.length >= 2 ? parts[parts.length - 1] : name;
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const renderThinking = (r: { id: string; text: string }) => {
|
|
153
|
+
if (!props.thinkCollapsed) {
|
|
154
|
+
return (
|
|
155
|
+
<box flexDirection="column">
|
|
156
|
+
<text fg={theme.textMuted}>
|
|
157
|
+
{"▼ thinking:"}
|
|
158
|
+
</text>
|
|
159
|
+
<text fg={theme.textMuted}>{r.text}</text>
|
|
160
|
+
</box>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const label = props.thinkConfig.showSummary
|
|
165
|
+
? "▶ thinking: " + r.text.slice(0, 60).replace(/\n/g, " ") + (r.text.length > 60 ? "..." : "")
|
|
166
|
+
: "▶ thinking (" + r.text.length + " chars)";
|
|
167
|
+
|
|
168
|
+
return <text fg={theme.textMuted}>{label}</text>;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<box
|
|
173
|
+
position="absolute"
|
|
174
|
+
bottom={0}
|
|
175
|
+
right={0}
|
|
176
|
+
onMouseDown={() => input?.focus()}
|
|
177
|
+
>
|
|
178
|
+
<box
|
|
179
|
+
width={panelWidth}
|
|
180
|
+
flexDirection="column"
|
|
181
|
+
border={true}
|
|
182
|
+
borderColor={theme.borderActive}
|
|
183
|
+
backgroundColor={theme.backgroundPanel}
|
|
184
|
+
>
|
|
185
|
+
<box
|
|
186
|
+
flexDirection="row"
|
|
187
|
+
justifyContent="space-between"
|
|
188
|
+
alignItems="center"
|
|
189
|
+
paddingTop={1}
|
|
190
|
+
paddingLeft={1}
|
|
191
|
+
paddingRight={1}
|
|
192
|
+
>
|
|
193
|
+
<box flexDirection="row" gap={1} alignItems="center">
|
|
194
|
+
<box paddingLeft={1} paddingRight={1} backgroundColor={theme.accent}>
|
|
195
|
+
<text fg={theme.background}>
|
|
196
|
+
<b>{"OpenCode-SideChat"}</b>
|
|
197
|
+
</text>
|
|
198
|
+
</box>
|
|
199
|
+
|
|
200
|
+
</box>
|
|
201
|
+
<box flexDirection="row" gap={1} alignItems="center">
|
|
202
|
+
<text fg={theme.textMuted}>{shortModelName()}</text>
|
|
203
|
+
{ctxLabel() ? (
|
|
204
|
+
<text fg={theme.textMuted}>{ctxLabel()}</text>
|
|
205
|
+
) : (
|
|
206
|
+
<text>{""}</text>
|
|
207
|
+
)}
|
|
208
|
+
</box>
|
|
209
|
+
</box>
|
|
210
|
+
|
|
211
|
+
<box paddingLeft={1} paddingRight={1}>
|
|
212
|
+
<scrollbox
|
|
213
|
+
scrollY={true}
|
|
214
|
+
stickyScroll={true}
|
|
215
|
+
stickyStart="bottom"
|
|
216
|
+
height={props.transcriptHeight}
|
|
217
|
+
width={contentWidth}
|
|
218
|
+
>
|
|
219
|
+
<box flexDirection="column" gap={1} paddingTop={1} paddingBottom={1} width={contentWidth - 2}>
|
|
220
|
+
{msgs().length > 0 ? (
|
|
221
|
+
<For each={msgs()}>
|
|
222
|
+
{(msg) => (
|
|
223
|
+
<box flexDirection="column" gap={0}>
|
|
224
|
+
<text fg={msg.role === "assistant" ? theme.secondary : theme.text}>
|
|
225
|
+
<b>{msg.role === "assistant" ? "Agent:" : "You:"}</b>
|
|
226
|
+
</text>
|
|
227
|
+
{msg.reasoning.map((r) => renderThinking(r))}
|
|
228
|
+
{msg.text ? (
|
|
229
|
+
<box flexDirection="column">
|
|
230
|
+
<RenderMarkdown text={msg.text} theme={theme} />
|
|
231
|
+
</box>
|
|
232
|
+
) : (
|
|
233
|
+
<text>{""}</text>
|
|
234
|
+
)}
|
|
235
|
+
</box>
|
|
236
|
+
)}
|
|
237
|
+
</For>
|
|
238
|
+
) : props.state.loading ? (
|
|
239
|
+
<text fg={theme.textMuted}>{THINKING_TEXT}</text>
|
|
240
|
+
) : (
|
|
241
|
+
<text>{""}</text>
|
|
242
|
+
)}
|
|
243
|
+
{props.state.error ? (
|
|
244
|
+
<text fg={theme.error}>{"Error: " + String(props.state.error)}</text>
|
|
245
|
+
) : (
|
|
246
|
+
<text>{""}</text>
|
|
247
|
+
)}
|
|
248
|
+
</box>
|
|
249
|
+
</scrollbox>
|
|
250
|
+
</box>
|
|
251
|
+
|
|
252
|
+
<box paddingLeft={1} paddingRight={1}>
|
|
253
|
+
<input
|
|
254
|
+
ref={(node) => { input = node; props.onInput?.(node); }}
|
|
255
|
+
width={contentWidth}
|
|
256
|
+
placeholder={props.state.loading ? "..." : ">"}
|
|
257
|
+
textColor={theme.text}
|
|
258
|
+
placeholderColor={theme.textMuted}
|
|
259
|
+
backgroundColor={theme.backgroundElement}
|
|
260
|
+
focusedTextColor={theme.text}
|
|
261
|
+
cursorColor={theme.primary}
|
|
262
|
+
focusedBackgroundColor={theme.backgroundElement}
|
|
263
|
+
onInput={(value) => {
|
|
264
|
+
inputValue = value;
|
|
265
|
+
}}
|
|
266
|
+
onSubmit={() => {
|
|
267
|
+
const submitted = (input?.value ?? inputValue).trim();
|
|
268
|
+
if (!submitted || props.state.loading) return;
|
|
269
|
+
if (!props.onSubmit(submitted)) return;
|
|
270
|
+
inputValue = "";
|
|
271
|
+
if (input) input.value = "";
|
|
272
|
+
}}
|
|
273
|
+
/>
|
|
274
|
+
</box>
|
|
275
|
+
|
|
276
|
+
<box
|
|
277
|
+
flexDirection="row"
|
|
278
|
+
gap={1}
|
|
279
|
+
paddingTop={0}
|
|
280
|
+
paddingBottom={1}
|
|
281
|
+
paddingLeft={1}
|
|
282
|
+
paddingRight={1}
|
|
283
|
+
alignItems="center"
|
|
284
|
+
>
|
|
285
|
+
{formatKeybind(props.clearKeybind) && (
|
|
286
|
+
<box flexDirection="row" gap={1} alignItems="center">
|
|
287
|
+
<text fg={theme.secondary}><b>{formatKeybind(props.clearKeybind)}</b></text>
|
|
288
|
+
<text fg={theme.primary}>{"Clear"}</text>
|
|
289
|
+
<text fg={theme.textMuted}>{"·"}</text>
|
|
290
|
+
</box>
|
|
291
|
+
)}
|
|
292
|
+
{formatKeybind(props.thinkToggleKeybind) && (
|
|
293
|
+
<box flexDirection="row" gap={1} alignItems="center">
|
|
294
|
+
<text fg={theme.secondary}><b>{formatKeybind(props.thinkToggleKeybind)}</b></text>
|
|
295
|
+
<text fg={theme.primary}>{"Thinking"}</text>
|
|
296
|
+
<text fg={theme.textMuted}>{props.thinkCollapsed ? "" : "(on)"}</text>
|
|
297
|
+
<text fg={theme.textMuted}>{"·"}</text>
|
|
298
|
+
</box>
|
|
299
|
+
)}
|
|
300
|
+
<text fg={theme.secondary}><b>{"Tab"}</b></text>
|
|
301
|
+
<text fg={theme.primary}>{"Model"}</text>
|
|
302
|
+
</box>
|
|
303
|
+
</box>
|
|
304
|
+
</box>
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
type RenderBlock =
|
|
309
|
+
| { type: "line"; style?: "heading" | "blockquote" | "listitem" | "hr"; parts: InlinePart[] }
|
|
310
|
+
| { type: "codeblock"; codeText: string };
|
|
311
|
+
|
|
312
|
+
function RenderMarkdown(props: { text: string; theme: import("@opencode-ai/plugin/tui").TuiThemeCurrent }) {
|
|
313
|
+
const t = props.theme;
|
|
314
|
+
|
|
315
|
+
const lines = createMemo((): RenderBlock[] => {
|
|
316
|
+
const text = props.text;
|
|
317
|
+
const result: RenderBlock[] = [];
|
|
318
|
+
|
|
319
|
+
let inCodeBlock = false;
|
|
320
|
+
let codeBuffer = "";
|
|
321
|
+
|
|
322
|
+
for (const rawLine of text.split("\n")) {
|
|
323
|
+
if (rawLine.startsWith("```")) {
|
|
324
|
+
if (inCodeBlock) {
|
|
325
|
+
result.push({ type: "codeblock", codeText: codeBuffer });
|
|
326
|
+
codeBuffer = "";
|
|
327
|
+
}
|
|
328
|
+
inCodeBlock = !inCodeBlock;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (inCodeBlock) {
|
|
332
|
+
codeBuffer += (codeBuffer ? "\n" : "") + rawLine;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Detect block-level styles
|
|
337
|
+
let line = rawLine;
|
|
338
|
+
let style: "heading" | "blockquote" | "listitem" | "hr" | undefined;
|
|
339
|
+
|
|
340
|
+
if (/^#{1,6}\s/.test(line)) {
|
|
341
|
+
style = "heading";
|
|
342
|
+
line = line.replace(/^#{1,6}\s+/, "");
|
|
343
|
+
} else if (line.startsWith("> ")) {
|
|
344
|
+
style = "blockquote";
|
|
345
|
+
line = line.slice(2);
|
|
346
|
+
} else if (/^[-*]\s/.test(line)) {
|
|
347
|
+
style = "listitem";
|
|
348
|
+
line = line.replace(/^[-*]\s/, "");
|
|
349
|
+
} else if (/^\d+\.\s/.test(line)) {
|
|
350
|
+
style = "listitem";
|
|
351
|
+
// Keep number prefix (1., 2. etc.) visible
|
|
352
|
+
} else if (/^[-*=_]{3,}$/.test(line)) {
|
|
353
|
+
style = "hr";
|
|
354
|
+
line = "";
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
result.push({ type: "line", style, parts: renderInlineMarkdown(line) });
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (inCodeBlock && codeBuffer) {
|
|
361
|
+
result.push({ type: "codeblock", codeText: codeBuffer });
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return result;
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
return (
|
|
368
|
+
<For each={lines()}>
|
|
369
|
+
{(block) => {
|
|
370
|
+
if (block.type === "codeblock") {
|
|
371
|
+
return (
|
|
372
|
+
<box
|
|
373
|
+
backgroundColor={t.backgroundElement}
|
|
374
|
+
paddingLeft={1}
|
|
375
|
+
paddingRight={1}
|
|
376
|
+
>
|
|
377
|
+
<text fg={t.markdownCodeBlock}>{block.codeText}</text>
|
|
378
|
+
</box>
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const s = block.style;
|
|
383
|
+
let textColor: import("@opentui/core").RGBA;
|
|
384
|
+
if (s === "heading") {
|
|
385
|
+
textColor = t.markdownHeading;
|
|
386
|
+
} else if (s === "blockquote") {
|
|
387
|
+
textColor = t.markdownBlockQuote;
|
|
388
|
+
} else if (s === "listitem") {
|
|
389
|
+
textColor = t.markdownListItem;
|
|
390
|
+
} else if (s === "hr") {
|
|
391
|
+
textColor = t.markdownHorizontalRule;
|
|
392
|
+
} else {
|
|
393
|
+
textColor = t.markdownText;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (s === "hr") {
|
|
397
|
+
return <text fg={t.markdownHorizontalRule}>{"―".repeat(8)}</text>;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Render inline parts with appropriate colors
|
|
401
|
+
return (
|
|
402
|
+
<box flexDirection="row" flexWrap="wrap" gap={0}>
|
|
403
|
+
<For each={block.parts}>
|
|
404
|
+
{(part) => {
|
|
405
|
+
switch (part.type) {
|
|
406
|
+
case "bold":
|
|
407
|
+
return <text fg={t.markdownStrong}><b>{part.text}</b></text>;
|
|
408
|
+
case "italic":
|
|
409
|
+
return <text fg={t.markdownEmph}><i>{part.text}</i></text>;
|
|
410
|
+
case "code":
|
|
411
|
+
return <text fg={t.markdownCode}>{part.text}</text>;
|
|
412
|
+
case "link":
|
|
413
|
+
return (
|
|
414
|
+
<text fg={t.markdownLinkText}>
|
|
415
|
+
{part.text}<text fg={t.markdownLink}>{"(" + part.url + ")"}</text>
|
|
416
|
+
</text>
|
|
417
|
+
);
|
|
418
|
+
default:
|
|
419
|
+
return <text fg={textColor}>{part.text}</text>;
|
|
420
|
+
}
|
|
421
|
+
}}
|
|
422
|
+
</For>
|
|
423
|
+
</box>
|
|
424
|
+
);
|
|
425
|
+
}}
|
|
426
|
+
</For>
|
|
427
|
+
);
|
|
428
|
+
}
|