@rungent/react 0.0.0 → 0.7.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/LICENSE +18 -0
- package/README.md +44 -1
- package/dist/client-mUY72akp.js +1192 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +1 -0
- package/dist/index-BuUWmx0G.d.ts +560 -0
- package/dist/index.d.ts +382 -0
- package/dist/index.js +3832 -0
- package/dist/styles.css +1885 -0
- package/package.json +80 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,3832 @@
|
|
|
1
|
+
import { a as submitFormAnswers, c as attachmentsOf, d as reduceThread, f as RungentRequestError, h as checkAnswers, i as submitForm, l as initialThreadState, n as answerInput, o as appendUserMessage, r as cancelAll, s as attachmentPart, t as answerApproval } from "./client-mUY72akp.js";
|
|
2
|
+
import { createContext, memo, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import ReactMarkdown from "react-markdown";
|
|
5
|
+
import remarkGfm from "remark-gfm";
|
|
6
|
+
// src/dock-state.ts
|
|
7
|
+
/**
|
|
8
|
+
* The dock's state, kept outside React so that it survives the dock being mounted again (a host
|
|
9
|
+
* that re-renders or remounts its layout on navigation) and its first paint is already final:
|
|
10
|
+
* a mounted dock reads it synchronously instead of restoring it in an effect.
|
|
11
|
+
*/
|
|
12
|
+
/** The panel's width: its default, and the range the user can drag it in. */
|
|
13
|
+
const DOCK_WIDTH = {
|
|
14
|
+
initial: 420,
|
|
15
|
+
min: 360,
|
|
16
|
+
max: 640
|
|
17
|
+
};
|
|
18
|
+
const DEFAULTS = {
|
|
19
|
+
open: false,
|
|
20
|
+
threadId: null,
|
|
21
|
+
width: DOCK_WIDTH.initial,
|
|
22
|
+
expanded: false,
|
|
23
|
+
view: "thread",
|
|
24
|
+
openings: 0
|
|
25
|
+
};
|
|
26
|
+
const entries = /* @__PURE__ */ new Map();
|
|
27
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
28
|
+
function parse$1(raw, transient) {
|
|
29
|
+
try {
|
|
30
|
+
const value = JSON.parse(raw ?? "{}");
|
|
31
|
+
if (typeof value !== "object" || value === null) return {
|
|
32
|
+
...DEFAULTS,
|
|
33
|
+
...transient
|
|
34
|
+
};
|
|
35
|
+
const { open, threadId, width, expanded } = value;
|
|
36
|
+
return {
|
|
37
|
+
open: typeof open === "boolean" ? open : DEFAULTS.open,
|
|
38
|
+
threadId: typeof threadId === "string" || threadId === null ? threadId : DEFAULTS.threadId,
|
|
39
|
+
width: typeof width === "number" && Number.isFinite(width) ? width : DEFAULTS.width,
|
|
40
|
+
expanded: typeof expanded === "boolean" ? expanded : DEFAULTS.expanded,
|
|
41
|
+
...transient
|
|
42
|
+
};
|
|
43
|
+
} catch {
|
|
44
|
+
return {
|
|
45
|
+
...DEFAULTS,
|
|
46
|
+
...transient
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function stored(key) {
|
|
51
|
+
try {
|
|
52
|
+
return window.localStorage.getItem(key);
|
|
53
|
+
} catch {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** The dock's state under `key`: what storage holds, read again only when it changed. */
|
|
58
|
+
function readDock(key) {
|
|
59
|
+
const entry = entries.get(key);
|
|
60
|
+
const raw = stored(key);
|
|
61
|
+
if (entry && (raw === void 0 || raw === entry.raw)) return entry.state;
|
|
62
|
+
const { view, openings } = entry?.state ?? DEFAULTS;
|
|
63
|
+
const state = parse$1(raw ?? null, {
|
|
64
|
+
view,
|
|
65
|
+
openings
|
|
66
|
+
});
|
|
67
|
+
entries.set(key, {
|
|
68
|
+
raw: raw ?? null,
|
|
69
|
+
state
|
|
70
|
+
});
|
|
71
|
+
return state;
|
|
72
|
+
}
|
|
73
|
+
/** Changes the dock's state under `key`, stores it and tells every dock showing it. */
|
|
74
|
+
function writeDock(key, change) {
|
|
75
|
+
const current = readDock(key);
|
|
76
|
+
if (Object.entries(change).every(([name, value]) => current[name] === value)) return;
|
|
77
|
+
const opened = change.open === true && !current.open;
|
|
78
|
+
const state = {
|
|
79
|
+
...current,
|
|
80
|
+
...change,
|
|
81
|
+
openings: current.openings + (opened ? 1 : 0)
|
|
82
|
+
};
|
|
83
|
+
const { view: _, openings: __, ...persisted } = state;
|
|
84
|
+
const raw = JSON.stringify(persisted);
|
|
85
|
+
try {
|
|
86
|
+
window.localStorage.setItem(key, raw);
|
|
87
|
+
} catch {}
|
|
88
|
+
entries.set(key, {
|
|
89
|
+
raw,
|
|
90
|
+
state
|
|
91
|
+
});
|
|
92
|
+
for (const listener of listeners.get(key) ?? []) listener();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The dock's state under `key` and a function to change it. Rendered on the server, and while
|
|
96
|
+
* hydrating, it is the defaults; on the client the first paint already shows the stored state.
|
|
97
|
+
*/
|
|
98
|
+
function useDockState(key) {
|
|
99
|
+
const subscribe = useCallback((listener) => {
|
|
100
|
+
const set = listeners.get(key) ?? /* @__PURE__ */ new Set();
|
|
101
|
+
listeners.set(key, set);
|
|
102
|
+
set.add(listener);
|
|
103
|
+
return () => {
|
|
104
|
+
set.delete(listener);
|
|
105
|
+
};
|
|
106
|
+
}, [key]);
|
|
107
|
+
return [useSyncExternalStore(subscribe, () => readDock(key), () => DEFAULTS), useCallback((change) => writeDock(key, change), [key])];
|
|
108
|
+
}
|
|
109
|
+
// src/strings.ts
|
|
110
|
+
const en = {
|
|
111
|
+
assistant: "{name}",
|
|
112
|
+
openAssistant: "Open {name}",
|
|
113
|
+
close: "Close",
|
|
114
|
+
history: "History",
|
|
115
|
+
newChat: "New chat",
|
|
116
|
+
noThreads: "No conversations yet.",
|
|
117
|
+
untitled: "Untitled",
|
|
118
|
+
deleteThread: "Delete",
|
|
119
|
+
loadMore: "Load more",
|
|
120
|
+
welcomeTitle: "What can I do for you?",
|
|
121
|
+
welcome: "Ask about anything in this product. I can look things up and act for you.",
|
|
122
|
+
suggestions: "Suggestions",
|
|
123
|
+
expand: "Expand",
|
|
124
|
+
collapse: "Back to the side panel",
|
|
125
|
+
resize: "Resize the panel",
|
|
126
|
+
jumpToLatest: "Jump to the latest",
|
|
127
|
+
contextTitle: "Context",
|
|
128
|
+
contextDetail: "{used} of {budget} tokens",
|
|
129
|
+
contextOver: "Over by {percent}%",
|
|
130
|
+
contextEstimated: "Estimated before the model answered.",
|
|
131
|
+
formCancel: "Cancel",
|
|
132
|
+
formIssues: "Fix these and submit again:",
|
|
133
|
+
composerPlaceholder: "Message {name}…",
|
|
134
|
+
composerLabel: "Message",
|
|
135
|
+
send: "Send",
|
|
136
|
+
stop: "Stop",
|
|
137
|
+
think: "Think",
|
|
138
|
+
thinkOffHint: "Let the model think before it answers: slower, for harder questions",
|
|
139
|
+
thinkOnHint: "The model thinks before it answers. Turn off for faster answers",
|
|
140
|
+
thinking: "Thinking…",
|
|
141
|
+
retrying: "Model busy, retrying ({attempt}/{max})…",
|
|
142
|
+
cancelled: "Stopped.",
|
|
143
|
+
contextUsed: "{percent}% of context used",
|
|
144
|
+
toolAwaitingApproval: "Awaiting approval",
|
|
145
|
+
toolAwaitingInput: "Waiting for the form",
|
|
146
|
+
toolFailed: "Did not succeed",
|
|
147
|
+
toolNotExecuted: "Not run",
|
|
148
|
+
toolDeduplicated: "Already done",
|
|
149
|
+
traceWorking: "Working…",
|
|
150
|
+
traceWriting: "Writing the answer…",
|
|
151
|
+
traceThought: "Thought",
|
|
152
|
+
traceSearching: "Searching for “{query}”…",
|
|
153
|
+
traceSearched: "Searched for “{query}”",
|
|
154
|
+
traceSearchedPlain: "Searched",
|
|
155
|
+
traceResult: "1 result",
|
|
156
|
+
traceResults: "{count} results",
|
|
157
|
+
traceItem: "1 item",
|
|
158
|
+
traceItems: "{count} items",
|
|
159
|
+
traceAsked: "Asked you to choose",
|
|
160
|
+
traceAwaitingAnswer: "Waiting for your answer",
|
|
161
|
+
traceWorked: "Worked",
|
|
162
|
+
traceSeparator: " · ",
|
|
163
|
+
traceMore: "+{count} more",
|
|
164
|
+
traceDetails: "Details",
|
|
165
|
+
traceArguments: "Arguments",
|
|
166
|
+
traceResultJson: "Result",
|
|
167
|
+
sources: "Sources",
|
|
168
|
+
"error.model_unavailable": "The model is unavailable right now. Try again later.",
|
|
169
|
+
"error.max_steps": "This took too many steps, so I stopped. Try a narrower request.",
|
|
170
|
+
"error.open_interrupts": "Answer or dismiss the open request first.",
|
|
171
|
+
"error.history_conflict": "This conversation changed elsewhere. Reload it.",
|
|
172
|
+
"error.invalid_interaction": "That answer was not accepted.",
|
|
173
|
+
"error.internal": "Something went wrong.",
|
|
174
|
+
"error.request": "Could not reach {name}.",
|
|
175
|
+
approvalNeeded: "Needs your approval",
|
|
176
|
+
approve: "Approve",
|
|
177
|
+
reject: "Reject",
|
|
178
|
+
expired: "This request expired. Ask again to retry.",
|
|
179
|
+
expiresAt: "Expires {time}",
|
|
180
|
+
target: "Target",
|
|
181
|
+
risk: "Risk",
|
|
182
|
+
"risk.low": "Low",
|
|
183
|
+
"risk.medium": "Medium",
|
|
184
|
+
"risk.high": "High",
|
|
185
|
+
cost: "Cost",
|
|
186
|
+
changes: "Changes",
|
|
187
|
+
arguments: "Details",
|
|
188
|
+
"effect.read": "Read",
|
|
189
|
+
"effect.create": "Create",
|
|
190
|
+
"effect.update": "Update",
|
|
191
|
+
"effect.delete": "Delete",
|
|
192
|
+
"effect.external": "External",
|
|
193
|
+
submit: "Submit",
|
|
194
|
+
required: "Required",
|
|
195
|
+
optional: "optional",
|
|
196
|
+
recommended: "Recommended",
|
|
197
|
+
other: "Other",
|
|
198
|
+
otherPlaceholder: "Your own answer",
|
|
199
|
+
"issue.required": "This is required.",
|
|
200
|
+
"issue.one": "Choose only one.",
|
|
201
|
+
taskRunning: "Waiting for {op} to finish",
|
|
202
|
+
cancelTask: "Cancel",
|
|
203
|
+
searchPlaceholder: "Search or ask {name}…",
|
|
204
|
+
searchLabel: "Search",
|
|
205
|
+
searching: "Searching…",
|
|
206
|
+
noResults: "No results.",
|
|
207
|
+
failedSources: "Some sources did not answer: {sources}",
|
|
208
|
+
askAssistant: "Ask {name}",
|
|
209
|
+
loading: "Loading…",
|
|
210
|
+
loadFailed: "Could not load this page.",
|
|
211
|
+
models: "Models",
|
|
212
|
+
modelsHint: "Models are tried in order. A model that is rate-limited or out of credit rests, and the next one answers.",
|
|
213
|
+
provider: "Provider",
|
|
214
|
+
model: "Model",
|
|
215
|
+
baseURL: "Base URL",
|
|
216
|
+
apiKey: "API key",
|
|
217
|
+
apiKeySaved: "Saved — leave empty to keep",
|
|
218
|
+
vision: "Images",
|
|
219
|
+
thinkingOn: "Thinking on",
|
|
220
|
+
thinkingOff: "Thinking off",
|
|
221
|
+
thinkingHint: "Provider options that turn the model’s reasoning on and off, as JSON. Users see a Think switch when a model has both; it is off unless they turn it on. Leave both empty for a model that cannot think.",
|
|
222
|
+
thinkingInvalid: "Thinking on and off must both be JSON objects of provider options, or both empty.",
|
|
223
|
+
moveUp: "Move up",
|
|
224
|
+
moveDown: "Move down",
|
|
225
|
+
remove: "Remove",
|
|
226
|
+
addModel: "Add model",
|
|
227
|
+
save: "Save",
|
|
228
|
+
saved: "Saved.",
|
|
229
|
+
saveFailed: "Could not save: {status}",
|
|
230
|
+
temperature: "Temperature",
|
|
231
|
+
maxOutputTokens: "Max output tokens",
|
|
232
|
+
configuredInCode: "Configured in code; change it there.",
|
|
233
|
+
saveReplacesCode: "Saving replaces the models configured in code with this list.",
|
|
234
|
+
resetDefaults: "Reset to defaults",
|
|
235
|
+
resetConfirm: "Replace every model with the defaults? Stored API keys and unsaved changes are lost.",
|
|
236
|
+
resetDone: "Reset to defaults.",
|
|
237
|
+
cancel: "Cancel",
|
|
238
|
+
attach: "Attach files",
|
|
239
|
+
attachments: "Attachments",
|
|
240
|
+
dropFiles: "Drop files to attach",
|
|
241
|
+
uploading: "Uploading {file}…",
|
|
242
|
+
uploadFailed: "Could not upload {file}.",
|
|
243
|
+
removeAttachment: "Remove {file}",
|
|
244
|
+
openAttachment: "Open {file}",
|
|
245
|
+
image: "Image",
|
|
246
|
+
file: "File",
|
|
247
|
+
rateAnswer: "Rate this answer",
|
|
248
|
+
goodAnswer: "Good answer",
|
|
249
|
+
badAnswer: "Bad answer",
|
|
250
|
+
feedbackComment: "What went wrong? (optional)",
|
|
251
|
+
feedbackSend: "Send",
|
|
252
|
+
feedbackSkip: "Skip",
|
|
253
|
+
feedbackThanks: "Thanks for the feedback.",
|
|
254
|
+
feedbackFailed: "Could not send feedback."
|
|
255
|
+
};
|
|
256
|
+
const zhCN = {
|
|
257
|
+
assistant: "{name}",
|
|
258
|
+
openAssistant: "打开 {name}",
|
|
259
|
+
close: "关闭",
|
|
260
|
+
history: "历史",
|
|
261
|
+
newChat: "新对话",
|
|
262
|
+
noThreads: "还没有对话。",
|
|
263
|
+
untitled: "未命名",
|
|
264
|
+
deleteThread: "删除",
|
|
265
|
+
loadMore: "加载更多",
|
|
266
|
+
welcomeTitle: "有什么可以帮你?",
|
|
267
|
+
welcome: "问问这个产品里的任何事。我可以帮你查找,也可以替你操作。",
|
|
268
|
+
suggestions: "试试这样问",
|
|
269
|
+
expand: "展开",
|
|
270
|
+
collapse: "回到侧边栏",
|
|
271
|
+
resize: "调整面板宽度",
|
|
272
|
+
jumpToLatest: "回到最新",
|
|
273
|
+
contextTitle: "上下文",
|
|
274
|
+
contextDetail: "{used} / {budget} tokens",
|
|
275
|
+
contextOver: "超出 {percent}%",
|
|
276
|
+
contextEstimated: "模型回答前的估算值。",
|
|
277
|
+
formCancel: "取消",
|
|
278
|
+
formIssues: "请修改以下内容后重新提交:",
|
|
279
|
+
composerPlaceholder: "给 {name} 发消息…",
|
|
280
|
+
composerLabel: "消息",
|
|
281
|
+
send: "发送",
|
|
282
|
+
stop: "停止",
|
|
283
|
+
think: "深度思考",
|
|
284
|
+
thinkOffHint: "让模型先思考再回答:更慢,适合较难的问题",
|
|
285
|
+
thinkOnHint: "模型会先思考再回答。关闭可更快得到回答",
|
|
286
|
+
thinking: "思考中…",
|
|
287
|
+
retrying: "模型繁忙,重试中({attempt}/{max})…",
|
|
288
|
+
cancelled: "已停止。",
|
|
289
|
+
contextUsed: "已用上下文 {percent}%",
|
|
290
|
+
toolAwaitingApproval: "等待确认",
|
|
291
|
+
toolAwaitingInput: "等待填写表单",
|
|
292
|
+
toolFailed: "未成功",
|
|
293
|
+
toolNotExecuted: "未执行",
|
|
294
|
+
toolDeduplicated: "已完成过",
|
|
295
|
+
traceWorking: "处理中…",
|
|
296
|
+
traceWriting: "正在撰写回答…",
|
|
297
|
+
traceThought: "已思考",
|
|
298
|
+
traceSearching: "正在搜索“{query}”…",
|
|
299
|
+
traceSearched: "搜索了“{query}”",
|
|
300
|
+
traceSearchedPlain: "已搜索",
|
|
301
|
+
traceResult: "1 条结果",
|
|
302
|
+
traceResults: "{count} 条结果",
|
|
303
|
+
traceItem: "1 项",
|
|
304
|
+
traceItems: "{count} 项",
|
|
305
|
+
traceAsked: "请你选择",
|
|
306
|
+
traceAwaitingAnswer: "等待你的回答",
|
|
307
|
+
traceWorked: "已处理",
|
|
308
|
+
traceSeparator: " · ",
|
|
309
|
+
traceMore: "等 {count} 项",
|
|
310
|
+
traceDetails: "详情",
|
|
311
|
+
traceArguments: "参数",
|
|
312
|
+
traceResultJson: "结果",
|
|
313
|
+
sources: "来源",
|
|
314
|
+
"error.model_unavailable": "模型暂时不可用,请稍后再试。",
|
|
315
|
+
"error.max_steps": "步骤太多,已停止。请把请求说得更具体些。",
|
|
316
|
+
"error.open_interrupts": "请先回答或放弃待处理的请求。",
|
|
317
|
+
"error.history_conflict": "对话已在别处更改,请重新加载。",
|
|
318
|
+
"error.invalid_interaction": "该回答未被接受。",
|
|
319
|
+
"error.internal": "出错了。",
|
|
320
|
+
"error.request": "无法连接 {name}。",
|
|
321
|
+
approvalNeeded: "需要你确认",
|
|
322
|
+
approve: "确认",
|
|
323
|
+
reject: "拒绝",
|
|
324
|
+
expired: "该请求已过期,请重新发起。",
|
|
325
|
+
expiresAt: "{time} 过期",
|
|
326
|
+
target: "对象",
|
|
327
|
+
risk: "风险",
|
|
328
|
+
"risk.low": "低",
|
|
329
|
+
"risk.medium": "中",
|
|
330
|
+
"risk.high": "高",
|
|
331
|
+
cost: "费用",
|
|
332
|
+
changes: "变更",
|
|
333
|
+
arguments: "详情",
|
|
334
|
+
"effect.read": "读取",
|
|
335
|
+
"effect.create": "创建",
|
|
336
|
+
"effect.update": "修改",
|
|
337
|
+
"effect.delete": "删除",
|
|
338
|
+
"effect.external": "外部",
|
|
339
|
+
submit: "提交",
|
|
340
|
+
required: "必填",
|
|
341
|
+
optional: "选填",
|
|
342
|
+
recommended: "推荐",
|
|
343
|
+
other: "其他",
|
|
344
|
+
otherPlaceholder: "自己填写",
|
|
345
|
+
"issue.required": "此项必填。",
|
|
346
|
+
"issue.one": "只能选一项。",
|
|
347
|
+
taskRunning: "等待 {op} 完成",
|
|
348
|
+
cancelTask: "取消",
|
|
349
|
+
searchPlaceholder: "搜索或问 {name}…",
|
|
350
|
+
searchLabel: "搜索",
|
|
351
|
+
searching: "搜索中…",
|
|
352
|
+
noResults: "没有结果。",
|
|
353
|
+
failedSources: "部分来源未响应:{sources}",
|
|
354
|
+
askAssistant: "问 {name}",
|
|
355
|
+
loading: "加载中…",
|
|
356
|
+
loadFailed: "无法加载此页面。",
|
|
357
|
+
models: "模型",
|
|
358
|
+
modelsHint: "按顺序使用模型。被限流或余额不足的模型会暂停,由下一个接替。",
|
|
359
|
+
provider: "提供方",
|
|
360
|
+
model: "模型",
|
|
361
|
+
baseURL: "Base URL",
|
|
362
|
+
apiKey: "API 密钥",
|
|
363
|
+
apiKeySaved: "已保存,留空则不变",
|
|
364
|
+
vision: "图片",
|
|
365
|
+
thinkingOn: "开启思考",
|
|
366
|
+
thinkingOff: "关闭思考",
|
|
367
|
+
thinkingHint: "开启和关闭模型推理的服务商参数,JSON 格式。两项都填写后,用户会看到“深度思考”开关,默认关闭。不能思考的模型两项都留空。",
|
|
368
|
+
thinkingInvalid: "开启思考和关闭思考须同为服务商参数的 JSON 对象,或同为空。",
|
|
369
|
+
moveUp: "上移",
|
|
370
|
+
moveDown: "下移",
|
|
371
|
+
remove: "移除",
|
|
372
|
+
addModel: "添加模型",
|
|
373
|
+
save: "保存",
|
|
374
|
+
saved: "已保存。",
|
|
375
|
+
saveFailed: "保存失败:{status}",
|
|
376
|
+
temperature: "温度",
|
|
377
|
+
maxOutputTokens: "最大输出 token",
|
|
378
|
+
configuredInCode: "在代码中配置,请在代码里修改。",
|
|
379
|
+
saveReplacesCode: "保存后,此列表将取代代码中配置的模型。",
|
|
380
|
+
resetDefaults: "恢复默认",
|
|
381
|
+
resetConfirm: "用默认设置替换所有模型?已保存的 API 密钥和未保存的更改都会丢失。",
|
|
382
|
+
resetDone: "已恢复默认。",
|
|
383
|
+
cancel: "取消",
|
|
384
|
+
attach: "添加附件",
|
|
385
|
+
attachments: "附件",
|
|
386
|
+
dropFiles: "拖放文件以添加附件",
|
|
387
|
+
uploading: "正在上传 {file}…",
|
|
388
|
+
uploadFailed: "无法上传 {file}。",
|
|
389
|
+
removeAttachment: "移除 {file}",
|
|
390
|
+
openAttachment: "打开 {file}",
|
|
391
|
+
image: "图片",
|
|
392
|
+
file: "文件",
|
|
393
|
+
rateAnswer: "评价这个回答",
|
|
394
|
+
goodAnswer: "回答有帮助",
|
|
395
|
+
badAnswer: "回答没帮助",
|
|
396
|
+
feedbackComment: "哪里不对?(选填)",
|
|
397
|
+
feedbackSend: "发送",
|
|
398
|
+
feedbackSkip: "跳过",
|
|
399
|
+
feedbackThanks: "感谢反馈。",
|
|
400
|
+
feedbackFailed: "反馈发送失败。"
|
|
401
|
+
};
|
|
402
|
+
/** The built-in string tables, by locale. */
|
|
403
|
+
const locales = {
|
|
404
|
+
en,
|
|
405
|
+
"zh-CN": zhCN
|
|
406
|
+
};
|
|
407
|
+
const fill = (text, vars) => text.replace(/\{(\w+)\}/g, (match, name) => String(vars[name] ?? match));
|
|
408
|
+
/**
|
|
409
|
+
* Looks strings up in `overrides`, then the locale's table (`zh-CN`, else `en`), then `en`,
|
|
410
|
+
* then gives the key itself. `{name}` is the assistant's name: the `assistant` string, which
|
|
411
|
+
* defaults to `name`.
|
|
412
|
+
*/
|
|
413
|
+
function translator(locale, overrides, name) {
|
|
414
|
+
const table = locale === "zh-CN" ? zhCN : en;
|
|
415
|
+
const english = en;
|
|
416
|
+
const raw = (key) => overrides[key] ?? table[key] ?? english[key];
|
|
417
|
+
const assistant = fill(raw("assistant") ?? name, { name });
|
|
418
|
+
const find = (key, vars) => {
|
|
419
|
+
const text = raw(key);
|
|
420
|
+
return text === void 0 ? void 0 : fill(text, {
|
|
421
|
+
...vars,
|
|
422
|
+
name: assistant
|
|
423
|
+
});
|
|
424
|
+
};
|
|
425
|
+
return Object.assign((key, vars) => find(key, vars) ?? key, { find });
|
|
426
|
+
}
|
|
427
|
+
// src/context.tsx
|
|
428
|
+
const RungentContext = createContext(null);
|
|
429
|
+
/** The same-origin path of `href`, or null when it leaves the page's origin. */
|
|
430
|
+
function samePath(href) {
|
|
431
|
+
const url = new URL(href, window.location.href);
|
|
432
|
+
if (url.origin !== window.location.origin) return null;
|
|
433
|
+
return `${url.pathname}${url.search}${url.hash}`;
|
|
434
|
+
}
|
|
435
|
+
function useToggle() {
|
|
436
|
+
const [isOpen, store] = useState(false);
|
|
437
|
+
const control = useRef(null);
|
|
438
|
+
const set = useCallback((open) => (control.current ?? store)(open), []);
|
|
439
|
+
return useMemo(() => ({
|
|
440
|
+
isOpen,
|
|
441
|
+
set,
|
|
442
|
+
store,
|
|
443
|
+
control
|
|
444
|
+
}), [isOpen, set]);
|
|
445
|
+
}
|
|
446
|
+
/** The dock's open state, in the store that outlives the dock's mounts. */
|
|
447
|
+
function useDockToggle(key) {
|
|
448
|
+
const [{ open: isOpen }, write] = useDockState(key);
|
|
449
|
+
const store = useCallback((open) => write({ open }), [write]);
|
|
450
|
+
const control = useRef(null);
|
|
451
|
+
const set = useCallback((open) => (control.current ?? store)(open), [store]);
|
|
452
|
+
return useMemo(() => ({
|
|
453
|
+
isOpen,
|
|
454
|
+
set,
|
|
455
|
+
store,
|
|
456
|
+
control
|
|
457
|
+
}), [
|
|
458
|
+
isOpen,
|
|
459
|
+
set,
|
|
460
|
+
store
|
|
461
|
+
]);
|
|
462
|
+
}
|
|
463
|
+
/** A function that always calls the latest `value`, so a host's inline callbacks change nothing. */
|
|
464
|
+
function useLatest(value) {
|
|
465
|
+
const ref = useRef(value);
|
|
466
|
+
ref.current = value;
|
|
467
|
+
const stable = useCallback((...args) => ref.current(...args), []);
|
|
468
|
+
return value ? stable : void 0;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* A component's open state: `open` when controlled, else the provider's. Every change goes
|
|
472
|
+
* through `onOpenChange`.
|
|
473
|
+
*/
|
|
474
|
+
function useControlledOpen(toggle, open, onOpenChange) {
|
|
475
|
+
const controlled = open !== void 0;
|
|
476
|
+
const onChange = useRef(onOpenChange);
|
|
477
|
+
onChange.current = onOpenChange;
|
|
478
|
+
const { store, control } = toggle;
|
|
479
|
+
const set = useCallback((next) => {
|
|
480
|
+
if (!controlled) store(next);
|
|
481
|
+
onChange.current?.(next);
|
|
482
|
+
}, [controlled, store]);
|
|
483
|
+
useEffect(() => {
|
|
484
|
+
control.current = set;
|
|
485
|
+
return () => {
|
|
486
|
+
if (control.current === set) control.current = null;
|
|
487
|
+
};
|
|
488
|
+
}, [control, set]);
|
|
489
|
+
useEffect(() => {
|
|
490
|
+
if (open !== void 0) store(open);
|
|
491
|
+
}, [open, store]);
|
|
492
|
+
return [open ?? toggle.isOpen, set];
|
|
493
|
+
}
|
|
494
|
+
const DEFAULT_NAME = "Assistant";
|
|
495
|
+
function readFlag(key) {
|
|
496
|
+
try {
|
|
497
|
+
return window.localStorage.getItem(key) === "on";
|
|
498
|
+
} catch {
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/** The user's thinking switch, kept in `localStorage` under `key`; off until they turn it on. */
|
|
503
|
+
function useThinking(key, available) {
|
|
504
|
+
const [on, store] = useState(() => typeof window === "undefined" ? false : readFlag(key));
|
|
505
|
+
const [storedKey, setStoredKey] = useState(key);
|
|
506
|
+
if (storedKey !== key) {
|
|
507
|
+
setStoredKey(key);
|
|
508
|
+
store(readFlag(key));
|
|
509
|
+
}
|
|
510
|
+
const set = useCallback((next) => {
|
|
511
|
+
store(next);
|
|
512
|
+
try {
|
|
513
|
+
if (next) window.localStorage.setItem(key, "on");
|
|
514
|
+
else window.localStorage.removeItem(key);
|
|
515
|
+
} catch {}
|
|
516
|
+
}, [key]);
|
|
517
|
+
return useMemo(() => ({
|
|
518
|
+
available,
|
|
519
|
+
on,
|
|
520
|
+
set
|
|
521
|
+
}), [
|
|
522
|
+
available,
|
|
523
|
+
on,
|
|
524
|
+
set
|
|
525
|
+
]);
|
|
526
|
+
}
|
|
527
|
+
const NO_FORMS = {};
|
|
528
|
+
function RungentProvider({ client, locale = "en", strings, upload, context, onOpen, theme = "system", forms = NO_FORMS, suggestions, children }) {
|
|
529
|
+
const instance = client.baseUrl || (typeof window === "undefined" ? "" : window.location.origin);
|
|
530
|
+
const dockKey = `rungent:dock:${instance}`;
|
|
531
|
+
const dock = useDockToggle(dockKey);
|
|
532
|
+
const palette = useToggle();
|
|
533
|
+
const paletteSeed = useRef("");
|
|
534
|
+
const [ask, setAsk] = useState(null);
|
|
535
|
+
const askTaken = useRef(0);
|
|
536
|
+
const getContext = useLatest(context);
|
|
537
|
+
const getSuggestions = useLatest(suggestions);
|
|
538
|
+
const onOpenLatest = useLatest(onOpen);
|
|
539
|
+
const uploadLatest = useLatest(upload);
|
|
540
|
+
const [name, setName] = useState(DEFAULT_NAME);
|
|
541
|
+
const [canThink, setCanThink] = useState(false);
|
|
542
|
+
const thinking = useThinking(`rungent:thinking:${instance}`, canThink);
|
|
543
|
+
useEffect(() => {
|
|
544
|
+
let live = true;
|
|
545
|
+
client.info().then((info) => {
|
|
546
|
+
if (!live) return;
|
|
547
|
+
setName(info.name);
|
|
548
|
+
setCanThink(info.thinking === true);
|
|
549
|
+
}, () => {});
|
|
550
|
+
return () => {
|
|
551
|
+
live = false;
|
|
552
|
+
};
|
|
553
|
+
}, [client]);
|
|
554
|
+
const navigate = useCallback((href) => {
|
|
555
|
+
const path = samePath(href);
|
|
556
|
+
if (path === null) window.open(href, "_blank", "noopener,noreferrer");
|
|
557
|
+
else if (onOpenLatest) onOpenLatest(path);
|
|
558
|
+
else window.location.assign(path);
|
|
559
|
+
}, [onOpenLatest]);
|
|
560
|
+
const setDock = dock.set;
|
|
561
|
+
const askAssistant = useCallback((query) => {
|
|
562
|
+
setAsk((current) => ({
|
|
563
|
+
id: (current?.id ?? 0) + 1,
|
|
564
|
+
query
|
|
565
|
+
}));
|
|
566
|
+
setDock(true);
|
|
567
|
+
}, [setDock]);
|
|
568
|
+
const t = useMemo(() => translator(locale, strings ?? {}, name), [
|
|
569
|
+
locale,
|
|
570
|
+
strings,
|
|
571
|
+
name
|
|
572
|
+
]);
|
|
573
|
+
const value = useMemo(() => ({
|
|
574
|
+
client,
|
|
575
|
+
locale,
|
|
576
|
+
t,
|
|
577
|
+
theme,
|
|
578
|
+
upload: uploadLatest,
|
|
579
|
+
context: getContext,
|
|
580
|
+
forms,
|
|
581
|
+
suggestions: getSuggestions,
|
|
582
|
+
navigate,
|
|
583
|
+
dock,
|
|
584
|
+
dockKey,
|
|
585
|
+
palette,
|
|
586
|
+
paletteSeed,
|
|
587
|
+
ask,
|
|
588
|
+
askTaken,
|
|
589
|
+
askAssistant,
|
|
590
|
+
thinking
|
|
591
|
+
}), [
|
|
592
|
+
client,
|
|
593
|
+
locale,
|
|
594
|
+
t,
|
|
595
|
+
theme,
|
|
596
|
+
uploadLatest,
|
|
597
|
+
getContext,
|
|
598
|
+
forms,
|
|
599
|
+
getSuggestions,
|
|
600
|
+
navigate,
|
|
601
|
+
dock,
|
|
602
|
+
dockKey,
|
|
603
|
+
palette,
|
|
604
|
+
ask,
|
|
605
|
+
askAssistant,
|
|
606
|
+
thinking
|
|
607
|
+
]);
|
|
608
|
+
return /* @__PURE__ */ jsx(RungentContext, {
|
|
609
|
+
value,
|
|
610
|
+
children
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
function useRungent() {
|
|
614
|
+
const value = useContext(RungentContext);
|
|
615
|
+
if (!value) throw new Error("rungent components must be inside <RungentProvider>");
|
|
616
|
+
return value;
|
|
617
|
+
}
|
|
618
|
+
/** Controls the dock from the host page, e.g. an "Ask the assistant" button. */
|
|
619
|
+
function useDock() {
|
|
620
|
+
const { dock, askAssistant } = useRungent();
|
|
621
|
+
return {
|
|
622
|
+
open: dock.isOpen,
|
|
623
|
+
setOpen: dock.set,
|
|
624
|
+
ask: askAssistant
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
/** Controls the search palette from the host page, e.g. a search button in the header. */
|
|
628
|
+
function useSearchPalette() {
|
|
629
|
+
const { palette, paletteSeed } = useRungent();
|
|
630
|
+
const { isOpen, set } = palette;
|
|
631
|
+
return useMemo(() => ({
|
|
632
|
+
isOpen,
|
|
633
|
+
open: (query = "") => {
|
|
634
|
+
paletteSeed.current = query;
|
|
635
|
+
set(true);
|
|
636
|
+
},
|
|
637
|
+
close: () => set(false)
|
|
638
|
+
}), [
|
|
639
|
+
isOpen,
|
|
640
|
+
set,
|
|
641
|
+
paletteSeed
|
|
642
|
+
]);
|
|
643
|
+
}
|
|
644
|
+
/** Attributes for a component's outermost element, which carries the theme variables. */
|
|
645
|
+
function useRoot(className) {
|
|
646
|
+
const { theme, locale } = useRungent();
|
|
647
|
+
return {
|
|
648
|
+
className: `rungent-root ${className}`,
|
|
649
|
+
"data-rungent-theme": theme,
|
|
650
|
+
lang: locale
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
// src/approval.tsx
|
|
654
|
+
function useExpired(expiresAt) {
|
|
655
|
+
const deadline = expiresAt ? Date.parse(expiresAt) : NaN;
|
|
656
|
+
const [expired, setExpired] = useState(() => deadline <= Date.now());
|
|
657
|
+
useEffect(() => {
|
|
658
|
+
if (Number.isNaN(deadline)) return;
|
|
659
|
+
const left = deadline - Date.now();
|
|
660
|
+
if (left <= 0) {
|
|
661
|
+
setExpired(true);
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
const timer = setTimeout(() => setExpired(true), Math.min(left, 2 ** 31 - 1));
|
|
665
|
+
return () => clearTimeout(timer);
|
|
666
|
+
}, [deadline]);
|
|
667
|
+
return expired;
|
|
668
|
+
}
|
|
669
|
+
function display(value) {
|
|
670
|
+
if (value === null || value === void 0 || value === "") return "—";
|
|
671
|
+
if (typeof value === "string") return value;
|
|
672
|
+
return JSON.stringify(value);
|
|
673
|
+
}
|
|
674
|
+
/** Asks the user to approve one write, showing exactly what will run. */
|
|
675
|
+
function ApprovalCard({ interrupt, onAnswer }) {
|
|
676
|
+
const { t, locale } = useRungent();
|
|
677
|
+
const root = useRoot("rungent-card rungent-approval");
|
|
678
|
+
const { impact, arguments: args } = interrupt.meta;
|
|
679
|
+
const expired = useExpired(interrupt.interrupt.expiresAt);
|
|
680
|
+
const [answered, setAnswered] = useState(false);
|
|
681
|
+
const titleId = useId();
|
|
682
|
+
const answer = (approved) => {
|
|
683
|
+
setAnswered(true);
|
|
684
|
+
onAnswer(approved);
|
|
685
|
+
};
|
|
686
|
+
const expiresAt = interrupt.interrupt.expiresAt;
|
|
687
|
+
return /* @__PURE__ */ jsxs("section", {
|
|
688
|
+
...root,
|
|
689
|
+
"data-risk": impact.risk ?? void 0,
|
|
690
|
+
"data-effect": impact.effect,
|
|
691
|
+
"aria-labelledby": titleId,
|
|
692
|
+
children: [
|
|
693
|
+
/* @__PURE__ */ jsx("p", {
|
|
694
|
+
className: "rungent-card-eyebrow",
|
|
695
|
+
children: t("approvalNeeded")
|
|
696
|
+
}),
|
|
697
|
+
/* @__PURE__ */ jsx("h3", {
|
|
698
|
+
id: titleId,
|
|
699
|
+
className: "rungent-card-title",
|
|
700
|
+
children: impact.title
|
|
701
|
+
}),
|
|
702
|
+
/* @__PURE__ */ jsxs("div", {
|
|
703
|
+
className: "rungent-badges",
|
|
704
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
705
|
+
className: "rungent-badge",
|
|
706
|
+
"data-effect": impact.effect,
|
|
707
|
+
children: t(`effect.${impact.effect}`)
|
|
708
|
+
}), impact.risk ? /* @__PURE__ */ jsxs("span", {
|
|
709
|
+
className: "rungent-badge",
|
|
710
|
+
"data-risk": impact.risk,
|
|
711
|
+
children: [
|
|
712
|
+
t("risk"),
|
|
713
|
+
": ",
|
|
714
|
+
t(`risk.${impact.risk}`)
|
|
715
|
+
]
|
|
716
|
+
}) : null]
|
|
717
|
+
}),
|
|
718
|
+
/* @__PURE__ */ jsxs("dl", {
|
|
719
|
+
className: "rungent-facts",
|
|
720
|
+
children: [impact.target ? /* @__PURE__ */ jsxs("div", {
|
|
721
|
+
className: "rungent-fact",
|
|
722
|
+
children: [/* @__PURE__ */ jsx("dt", { children: t("target") }), /* @__PURE__ */ jsx("dd", { children: impact.target })]
|
|
723
|
+
}) : null, impact.costHint ? /* @__PURE__ */ jsxs("div", {
|
|
724
|
+
className: "rungent-fact",
|
|
725
|
+
children: [/* @__PURE__ */ jsx("dt", { children: t("cost") }), /* @__PURE__ */ jsx("dd", { children: impact.costHint })]
|
|
726
|
+
}) : null]
|
|
727
|
+
}),
|
|
728
|
+
impact.changes.length > 0 ? /* @__PURE__ */ jsx("ul", {
|
|
729
|
+
className: "rungent-changes",
|
|
730
|
+
"aria-label": t("changes"),
|
|
731
|
+
children: impact.changes.map((change) => /* @__PURE__ */ jsx("li", { children: change }, change))
|
|
732
|
+
}) : null,
|
|
733
|
+
Object.keys(args).length > 0 ? /* @__PURE__ */ jsxs("details", {
|
|
734
|
+
className: "rungent-arguments",
|
|
735
|
+
children: [/* @__PURE__ */ jsx("summary", { children: t("arguments") }), /* @__PURE__ */ jsx("dl", {
|
|
736
|
+
className: "rungent-facts rungent-mono",
|
|
737
|
+
children: Object.entries(args).map(([key, value]) => /* @__PURE__ */ jsxs("div", {
|
|
738
|
+
className: "rungent-fact",
|
|
739
|
+
children: [/* @__PURE__ */ jsx("dt", { children: key }), /* @__PURE__ */ jsx("dd", { children: display(value) })]
|
|
740
|
+
}, key))
|
|
741
|
+
})]
|
|
742
|
+
}) : null,
|
|
743
|
+
expired ? /* @__PURE__ */ jsx("p", {
|
|
744
|
+
className: "rungent-note",
|
|
745
|
+
role: "status",
|
|
746
|
+
children: t("expired")
|
|
747
|
+
}) : /* @__PURE__ */ jsxs("div", {
|
|
748
|
+
className: "rungent-actions",
|
|
749
|
+
children: [
|
|
750
|
+
expiresAt ? /* @__PURE__ */ jsx("span", {
|
|
751
|
+
className: "rungent-muted rungent-small",
|
|
752
|
+
children: t("expiresAt", { time: new Date(expiresAt).toLocaleTimeString(locale, {
|
|
753
|
+
hour: "2-digit",
|
|
754
|
+
minute: "2-digit"
|
|
755
|
+
}) })
|
|
756
|
+
}) : null,
|
|
757
|
+
/* @__PURE__ */ jsx("button", {
|
|
758
|
+
type: "button",
|
|
759
|
+
className: "rungent-button",
|
|
760
|
+
disabled: answered,
|
|
761
|
+
onClick: () => answer(false),
|
|
762
|
+
children: t("reject")
|
|
763
|
+
}),
|
|
764
|
+
/* @__PURE__ */ jsx("button", {
|
|
765
|
+
type: "button",
|
|
766
|
+
className: "rungent-button rungent-primary",
|
|
767
|
+
"data-danger": impact.risk === "high" || impact.effect === "delete" || void 0,
|
|
768
|
+
disabled: answered,
|
|
769
|
+
onClick: () => answer(true),
|
|
770
|
+
children: t("approve")
|
|
771
|
+
})
|
|
772
|
+
]
|
|
773
|
+
})
|
|
774
|
+
]
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
// src/hooks.ts
|
|
778
|
+
function threadReducer(state, action) {
|
|
779
|
+
if (action.type === "load") return action.state;
|
|
780
|
+
if (action.type === "user") return appendUserMessage(state, action.item);
|
|
781
|
+
if (action.type === "interrupts") return {
|
|
782
|
+
...state,
|
|
783
|
+
interrupts: action.interrupts
|
|
784
|
+
};
|
|
785
|
+
return reduceThread(state, action.event);
|
|
786
|
+
}
|
|
787
|
+
function textOf(message) {
|
|
788
|
+
if (typeof message === "string") return message;
|
|
789
|
+
return message.map((part) => part.type === "text" ? part.text : "").join("");
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* The latest state of the threads shown on this page, so that a thread mounted again (a dock a
|
|
793
|
+
* host mounted again on navigation) shows at once as it was and follows on from there.
|
|
794
|
+
*/
|
|
795
|
+
const shown = /* @__PURE__ */ new WeakMap();
|
|
796
|
+
const SHOWN_LIMIT = 8;
|
|
797
|
+
function remember(client, id, state) {
|
|
798
|
+
const threads = shown.get(client) ?? /* @__PURE__ */ new Map();
|
|
799
|
+
shown.set(client, threads);
|
|
800
|
+
threads.delete(id);
|
|
801
|
+
threads.set(id, state);
|
|
802
|
+
if (threads.size > SHOWN_LIMIT) threads.delete(threads.keys().next().value);
|
|
803
|
+
}
|
|
804
|
+
const known = (client, id) => (id ? shown.get(client)?.get(id) : void 0) ?? initialThreadState;
|
|
805
|
+
/** Sends a run and detaches once it started; its events arrive through `follow`. */
|
|
806
|
+
async function start(run) {
|
|
807
|
+
const events = run[Symbol.asyncIterator]();
|
|
808
|
+
await events.next();
|
|
809
|
+
await events.return?.();
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* A thread's live state: it replays the thread's events and follows them, and opens the pages
|
|
813
|
+
* (`rungent.ui.open`) that runs started here ask for.
|
|
814
|
+
*/
|
|
815
|
+
function useThread(threadId) {
|
|
816
|
+
const { client, navigate, context, thinking } = useRungent();
|
|
817
|
+
const think = thinking.available && thinking.on;
|
|
818
|
+
const [createdId, setCreatedId] = useState(null);
|
|
819
|
+
const [shownProp, setShownProp] = useState(threadId);
|
|
820
|
+
if (shownProp !== threadId) {
|
|
821
|
+
setShownProp(threadId);
|
|
822
|
+
setCreatedId(null);
|
|
823
|
+
}
|
|
824
|
+
const id = threadId ?? createdId;
|
|
825
|
+
const [state, dispatch] = useReducer(threadReducer, id, (id) => known(client, id));
|
|
826
|
+
const [error, setError] = useState(null);
|
|
827
|
+
const created = useRef(null);
|
|
828
|
+
/** The thread `state` is of. */
|
|
829
|
+
const stateOf = useRef(id);
|
|
830
|
+
/** Opens with a seq above this were asked by a run started here and not yet shown. */
|
|
831
|
+
const openAfter = useRef(Number.POSITIVE_INFINITY);
|
|
832
|
+
const stateRef = useRef(state);
|
|
833
|
+
stateRef.current = state;
|
|
834
|
+
useEffect(() => {
|
|
835
|
+
let from = stateRef.current;
|
|
836
|
+
if (id !== stateOf.current && id !== created.current) {
|
|
837
|
+
from = known(client, id);
|
|
838
|
+
dispatch({
|
|
839
|
+
type: "load",
|
|
840
|
+
state: from
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
stateOf.current = id;
|
|
844
|
+
setError(null);
|
|
845
|
+
if (!id) return;
|
|
846
|
+
const controller = new AbortController();
|
|
847
|
+
(async () => {
|
|
848
|
+
const events = client.follow(id, {
|
|
849
|
+
after: from.lastSeq,
|
|
850
|
+
signal: controller.signal
|
|
851
|
+
});
|
|
852
|
+
for await (const event of events) dispatch({
|
|
853
|
+
type: "event",
|
|
854
|
+
event
|
|
855
|
+
});
|
|
856
|
+
})().catch((reason) => {
|
|
857
|
+
if (!controller.signal.aborted) setError(reason);
|
|
858
|
+
});
|
|
859
|
+
return () => controller.abort();
|
|
860
|
+
}, [client, id]);
|
|
861
|
+
useEffect(() => {
|
|
862
|
+
if (id) remember(client, id, state);
|
|
863
|
+
}, [
|
|
864
|
+
client,
|
|
865
|
+
id,
|
|
866
|
+
state
|
|
867
|
+
]);
|
|
868
|
+
useEffect(() => {
|
|
869
|
+
for (const open of state.opens) if (open.seq > openAfter.current) {
|
|
870
|
+
openAfter.current = open.seq;
|
|
871
|
+
navigate(open.path);
|
|
872
|
+
}
|
|
873
|
+
}, [state.opens, navigate]);
|
|
874
|
+
return {
|
|
875
|
+
threadId: id,
|
|
876
|
+
state,
|
|
877
|
+
error,
|
|
878
|
+
send: useCallback(async (message) => {
|
|
879
|
+
setError(null);
|
|
880
|
+
try {
|
|
881
|
+
let target = id;
|
|
882
|
+
if (!target) {
|
|
883
|
+
target = (await client.createThread()).id;
|
|
884
|
+
created.current = target;
|
|
885
|
+
setCreatedId(target);
|
|
886
|
+
}
|
|
887
|
+
const open = stateRef.current.interrupts;
|
|
888
|
+
const run = client.run(target, {
|
|
889
|
+
message,
|
|
890
|
+
context: context?.() ?? [],
|
|
891
|
+
...open.length > 0 ? { resume: cancelAll(open) } : {},
|
|
892
|
+
...think ? { thinking: true } : {}
|
|
893
|
+
});
|
|
894
|
+
if (run.messageId) dispatch({
|
|
895
|
+
type: "user",
|
|
896
|
+
item: {
|
|
897
|
+
kind: "user",
|
|
898
|
+
id: run.messageId,
|
|
899
|
+
text: textOf(message),
|
|
900
|
+
attachments: attachmentsOf(message)
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
openAfter.current = stateRef.current.lastSeq;
|
|
904
|
+
await start(run);
|
|
905
|
+
} catch (reason) {
|
|
906
|
+
setError(reason);
|
|
907
|
+
}
|
|
908
|
+
}, [
|
|
909
|
+
client,
|
|
910
|
+
context,
|
|
911
|
+
id,
|
|
912
|
+
think
|
|
913
|
+
]),
|
|
914
|
+
resume: useCallback(async (entries) => {
|
|
915
|
+
if (!id) return;
|
|
916
|
+
setError(null);
|
|
917
|
+
openAfter.current = stateRef.current.lastSeq;
|
|
918
|
+
const open = stateRef.current.interrupts;
|
|
919
|
+
const answered = new Set(entries.map((entry) => entry.interruptId));
|
|
920
|
+
dispatch({
|
|
921
|
+
type: "interrupts",
|
|
922
|
+
interrupts: open.filter((item) => !answered.has(item.id))
|
|
923
|
+
});
|
|
924
|
+
try {
|
|
925
|
+
await start(client.run(id, {
|
|
926
|
+
resume: entries,
|
|
927
|
+
context: context?.() ?? [],
|
|
928
|
+
...think ? { thinking: true } : {}
|
|
929
|
+
}));
|
|
930
|
+
} catch (reason) {
|
|
931
|
+
dispatch({
|
|
932
|
+
type: "interrupts",
|
|
933
|
+
interrupts: open
|
|
934
|
+
});
|
|
935
|
+
setError(reason);
|
|
936
|
+
}
|
|
937
|
+
}, [
|
|
938
|
+
client,
|
|
939
|
+
context,
|
|
940
|
+
id,
|
|
941
|
+
think
|
|
942
|
+
]),
|
|
943
|
+
cancel: useCallback(async () => {
|
|
944
|
+
const runId = stateRef.current.runId;
|
|
945
|
+
if (runId) await client.cancel(runId).catch(setError);
|
|
946
|
+
}, [client])
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
/** The user's threads, newest first. */
|
|
950
|
+
function useThreads() {
|
|
951
|
+
const { client } = useRungent();
|
|
952
|
+
const [threads, setThreads] = useState([]);
|
|
953
|
+
const [next, setNext] = useState();
|
|
954
|
+
const [loading, setLoading] = useState(true);
|
|
955
|
+
const [error, setError] = useState(null);
|
|
956
|
+
const [version, setVersion] = useState(0);
|
|
957
|
+
const load = useCallback(async (cursor, append) => {
|
|
958
|
+
setLoading(true);
|
|
959
|
+
try {
|
|
960
|
+
const page = await client.listThreads(cursor);
|
|
961
|
+
setThreads((current) => append ? [...current, ...page.items] : page.items);
|
|
962
|
+
setNext(page.next);
|
|
963
|
+
setError(null);
|
|
964
|
+
} catch (reason) {
|
|
965
|
+
setError(reason);
|
|
966
|
+
} finally {
|
|
967
|
+
setLoading(false);
|
|
968
|
+
}
|
|
969
|
+
}, [client]);
|
|
970
|
+
useEffect(() => {
|
|
971
|
+
load(void 0, false);
|
|
972
|
+
}, [load, version]);
|
|
973
|
+
return {
|
|
974
|
+
threads,
|
|
975
|
+
loading,
|
|
976
|
+
error,
|
|
977
|
+
hasMore: next !== void 0,
|
|
978
|
+
reload: useCallback(() => setVersion((v) => v + 1), []),
|
|
979
|
+
loadMore: useCallback(() => {
|
|
980
|
+
if (next !== void 0) load(next, true);
|
|
981
|
+
}, [load, next]),
|
|
982
|
+
remove: useCallback(async (threadId) => {
|
|
983
|
+
await client.deleteThread(threadId);
|
|
984
|
+
setThreads((current) => current.filter((thread) => thread.id !== threadId));
|
|
985
|
+
}, [client])
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
const SEARCH_DEBOUNCE_MS = 150;
|
|
989
|
+
/** Federated search, debounced; a newer query aborts the older request. */
|
|
990
|
+
function useSearch(query) {
|
|
991
|
+
const { client } = useRungent();
|
|
992
|
+
const [result, setResult] = useState(null);
|
|
993
|
+
const [loading, setLoading] = useState(false);
|
|
994
|
+
const [error, setError] = useState(null);
|
|
995
|
+
useEffect(() => {
|
|
996
|
+
const q = query.trim();
|
|
997
|
+
setResult(null);
|
|
998
|
+
setError(null);
|
|
999
|
+
if (!q) {
|
|
1000
|
+
setLoading(false);
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
setLoading(true);
|
|
1004
|
+
const controller = new AbortController();
|
|
1005
|
+
const timer = setTimeout(() => {
|
|
1006
|
+
client.search(q, { signal: controller.signal }).then(setResult, (reason) => {
|
|
1007
|
+
if (!controller.signal.aborted) setError(reason);
|
|
1008
|
+
}).finally(() => {
|
|
1009
|
+
if (!controller.signal.aborted) setLoading(false);
|
|
1010
|
+
});
|
|
1011
|
+
}, SEARCH_DEBOUNCE_MS);
|
|
1012
|
+
return () => {
|
|
1013
|
+
clearTimeout(timer);
|
|
1014
|
+
controller.abort();
|
|
1015
|
+
};
|
|
1016
|
+
}, [client, query]);
|
|
1017
|
+
return {
|
|
1018
|
+
result,
|
|
1019
|
+
loading,
|
|
1020
|
+
error
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
/** The operations visible to this user. */
|
|
1024
|
+
function useCatalog() {
|
|
1025
|
+
const { client } = useRungent();
|
|
1026
|
+
const [operations, setOperations] = useState([]);
|
|
1027
|
+
const [loading, setLoading] = useState(true);
|
|
1028
|
+
const [error, setError] = useState(null);
|
|
1029
|
+
useEffect(() => {
|
|
1030
|
+
let live = true;
|
|
1031
|
+
client.catalog().then((list) => live && setOperations(list), (reason) => live && setError(reason)).finally(() => live && setLoading(false));
|
|
1032
|
+
return () => {
|
|
1033
|
+
live = false;
|
|
1034
|
+
};
|
|
1035
|
+
}, [client]);
|
|
1036
|
+
return {
|
|
1037
|
+
operations,
|
|
1038
|
+
loading,
|
|
1039
|
+
error
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
// src/scrollbar.ts
|
|
1043
|
+
/**
|
|
1044
|
+
* rungent's scrollbars: thin, on a transparent track, and hidden until the user scrolls (or points
|
|
1045
|
+
* at the area). The look is CSS (`.rungent-scroll` in styles.css); this shows the thumb while the
|
|
1046
|
+
* user scrolls, and leaves it hidden while content scrolls by itself (an answer streaming).
|
|
1047
|
+
*/
|
|
1048
|
+
/** The class that gives a scroll container rungent's scrollbar. */
|
|
1049
|
+
const SCROLL_CLASS = "rungent-scroll";
|
|
1050
|
+
/** Set on the container while the user scrolls it; styles.css shows the thumb then. */
|
|
1051
|
+
const SCROLLING_ATTRIBUTE = "data-rungent-scrolling";
|
|
1052
|
+
/** The thumb hides this long after the last scroll. */
|
|
1053
|
+
const SCROLL_IDLE_MS = 900;
|
|
1054
|
+
/** A scroll this soon after the user's wheel, touch, pointer or key counts as theirs. */
|
|
1055
|
+
const INPUT_WINDOW_MS = 1e3;
|
|
1056
|
+
const INPUTS = [
|
|
1057
|
+
"wheel",
|
|
1058
|
+
"touchmove",
|
|
1059
|
+
"pointerdown",
|
|
1060
|
+
"keydown"
|
|
1061
|
+
];
|
|
1062
|
+
/**
|
|
1063
|
+
* Gives `element` rungent's scrollbar and shows its thumb while the user scrolls it. Works on any
|
|
1064
|
+
* element, React or not; returns a function that stops watching.
|
|
1065
|
+
*/
|
|
1066
|
+
function watchScrollbar(element) {
|
|
1067
|
+
element.classList.add(SCROLL_CLASS);
|
|
1068
|
+
let input = Number.NEGATIVE_INFINITY;
|
|
1069
|
+
let timer;
|
|
1070
|
+
const show = () => {
|
|
1071
|
+
element.setAttribute(SCROLLING_ATTRIBUTE, "");
|
|
1072
|
+
clearTimeout(timer);
|
|
1073
|
+
timer = setTimeout(() => element.removeAttribute(SCROLLING_ATTRIBUTE), SCROLL_IDLE_MS);
|
|
1074
|
+
};
|
|
1075
|
+
const onInput = () => {
|
|
1076
|
+
input = Date.now();
|
|
1077
|
+
};
|
|
1078
|
+
const onScroll = () => {
|
|
1079
|
+
if (Date.now() - input <= INPUT_WINDOW_MS) show();
|
|
1080
|
+
};
|
|
1081
|
+
for (const type of INPUTS) element.addEventListener(type, onInput, { passive: true });
|
|
1082
|
+
element.addEventListener("scroll", onScroll, { passive: true });
|
|
1083
|
+
return () => {
|
|
1084
|
+
for (const type of INPUTS) element.removeEventListener(type, onInput);
|
|
1085
|
+
element.removeEventListener("scroll", onScroll);
|
|
1086
|
+
clearTimeout(timer);
|
|
1087
|
+
element.removeAttribute(SCROLLING_ATTRIBUTE);
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* `watchScrollbar` for the element in `ref`, also when it mounts later or changes (a list shown
|
|
1092
|
+
* only while open). Put `SCROLL_CLASS` in its `className` as well, so the first paint already
|
|
1093
|
+
* has the thin, hidden scrollbar.
|
|
1094
|
+
*/
|
|
1095
|
+
function useScrollbar(ref) {
|
|
1096
|
+
const watched = useRef(null);
|
|
1097
|
+
useEffect(() => {
|
|
1098
|
+
const element = ref.current;
|
|
1099
|
+
if (watched.current?.element === element) return;
|
|
1100
|
+
watched.current?.stop();
|
|
1101
|
+
watched.current = element ? {
|
|
1102
|
+
element,
|
|
1103
|
+
stop: watchScrollbar(element)
|
|
1104
|
+
} : null;
|
|
1105
|
+
});
|
|
1106
|
+
useEffect(() => () => {
|
|
1107
|
+
watched.current?.stop();
|
|
1108
|
+
watched.current = null;
|
|
1109
|
+
}, []);
|
|
1110
|
+
}
|
|
1111
|
+
/** …and at most this share of the conversation's height. */
|
|
1112
|
+
const AUTOSIZE_MAX_SHARE = .4;
|
|
1113
|
+
/**
|
|
1114
|
+
* The height (border box) a textarea takes for content `scrollHeight` high (padding included,
|
|
1115
|
+
* borders not), capped at `max`, and whether its content then scrolls.
|
|
1116
|
+
*/
|
|
1117
|
+
function autosizeHeight(scrollHeight, borders, max) {
|
|
1118
|
+
const height = Math.ceil(scrollHeight + borders);
|
|
1119
|
+
return height > max ? {
|
|
1120
|
+
height: Math.floor(max),
|
|
1121
|
+
scrolls: true
|
|
1122
|
+
} : {
|
|
1123
|
+
height,
|
|
1124
|
+
scrolls: false
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
const px = (value) => Number.parseFloat(value) || 0;
|
|
1128
|
+
/**
|
|
1129
|
+
* Fits `element` to its text: at most `AUTOSIZE_MAX_LINES` lines and `AUTOSIZE_MAX_SHARE` of
|
|
1130
|
+
* `room` pixels, never less than one line. Measured and set in one go, so nothing paints between.
|
|
1131
|
+
*/
|
|
1132
|
+
function fitTextarea(element, room) {
|
|
1133
|
+
const style = getComputedStyle(element);
|
|
1134
|
+
const borders = px(style.borderTopWidth) + px(style.borderBottomWidth);
|
|
1135
|
+
const padding = px(style.paddingTop) + px(style.paddingBottom);
|
|
1136
|
+
const line = px(style.lineHeight) || (px(style.fontSize) || 14) * 1.5;
|
|
1137
|
+
const oneLine = line + padding + borders;
|
|
1138
|
+
const max = Math.max(oneLine, Math.min(10 * line + padding + borders, room * AUTOSIZE_MAX_SHARE));
|
|
1139
|
+
const top = element.scrollTop;
|
|
1140
|
+
element.style.overflowY = "hidden";
|
|
1141
|
+
element.style.height = "auto";
|
|
1142
|
+
const { height, scrolls } = autosizeHeight(element.scrollHeight, borders, max);
|
|
1143
|
+
element.style.height = `${height}px`;
|
|
1144
|
+
element.style.overflowY = scrolls ? "auto" : "hidden";
|
|
1145
|
+
element.scrollTop = top;
|
|
1146
|
+
}
|
|
1147
|
+
/**
|
|
1148
|
+
* Keeps the textarea in `ref` fitted to `value`, and to its width when that changes (the panel
|
|
1149
|
+
* resized). `room` gives the height it may take a share of.
|
|
1150
|
+
*/
|
|
1151
|
+
function useAutosize(ref, value, room) {
|
|
1152
|
+
const fit = useCallback(() => {
|
|
1153
|
+
if (ref.current) fitTextarea(ref.current, room());
|
|
1154
|
+
}, [ref, room]);
|
|
1155
|
+
useLayoutEffect(fit, [fit, value]);
|
|
1156
|
+
useEffect(() => {
|
|
1157
|
+
const element = ref.current;
|
|
1158
|
+
if (!element || typeof ResizeObserver === "undefined") return;
|
|
1159
|
+
let width = element.clientWidth;
|
|
1160
|
+
const observer = new ResizeObserver(() => {
|
|
1161
|
+
if (element.clientWidth === width) return;
|
|
1162
|
+
width = element.clientWidth;
|
|
1163
|
+
fit();
|
|
1164
|
+
});
|
|
1165
|
+
observer.observe(element);
|
|
1166
|
+
return () => observer.disconnect();
|
|
1167
|
+
}, [ref, fit]);
|
|
1168
|
+
}
|
|
1169
|
+
// src/form.tsx
|
|
1170
|
+
function at(values, path) {
|
|
1171
|
+
let current = values;
|
|
1172
|
+
for (const segment of path.split(".")) {
|
|
1173
|
+
if (typeof current !== "object" || current === null) return void 0;
|
|
1174
|
+
current = current[segment];
|
|
1175
|
+
}
|
|
1176
|
+
return current;
|
|
1177
|
+
}
|
|
1178
|
+
function initial(question, given) {
|
|
1179
|
+
const scalar = (value) => typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? String(value) : void 0;
|
|
1180
|
+
if (question.kind === "text") return { text: scalar(given) ?? question.default ?? "" };
|
|
1181
|
+
const fromGiven = Array.isArray(given) ? given.flatMap((value) => scalar(value) ?? []) : scalar(given) !== void 0 ? [scalar(given)] : void 0;
|
|
1182
|
+
const fallback = question.default === void 0 ? [] : typeof question.default === "string" ? [question.default] : question.default;
|
|
1183
|
+
const known = new Set(question.options.map((option) => option.id));
|
|
1184
|
+
return {
|
|
1185
|
+
selected: (fromGiven ?? fallback).filter((id) => known.has(id)),
|
|
1186
|
+
custom: question.allowCustom ? (fromGiven ?? []).filter((id) => !known.has(id)).join(", ") : ""
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
/** Answers as `checkAnswers` expects them; untouched optional questions are left out. */
|
|
1190
|
+
function collect(form, values) {
|
|
1191
|
+
const answers = {};
|
|
1192
|
+
for (const question of form.questions) {
|
|
1193
|
+
const value = values[question.id];
|
|
1194
|
+
if (!value) continue;
|
|
1195
|
+
if ("text" in value) {
|
|
1196
|
+
if (value.text.trim() || question.required) answers[question.id] = value.text;
|
|
1197
|
+
} else {
|
|
1198
|
+
const custom = value.custom.trim();
|
|
1199
|
+
if (value.selected.length > 0 || custom || question.required) answers[question.id] = {
|
|
1200
|
+
selected: value.selected,
|
|
1201
|
+
...custom ? { custom } : {}
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
return answers;
|
|
1206
|
+
}
|
|
1207
|
+
const ISSUE_KEYS = {
|
|
1208
|
+
required: "issue.required",
|
|
1209
|
+
"only one answer is allowed": "issue.one"
|
|
1210
|
+
};
|
|
1211
|
+
/** A form the agent or an operation asks the user to fill in. */
|
|
1212
|
+
function RungentForm({ form, values: given, issues: refused = [], onSubmit, onCancel }) {
|
|
1213
|
+
const { t } = useRungent();
|
|
1214
|
+
const root = useRoot("rungent-card rungent-form");
|
|
1215
|
+
const baseId = useId();
|
|
1216
|
+
const formRef = useRef(null);
|
|
1217
|
+
const [values, setValues] = useState(() => Object.fromEntries(form.questions.map((question) => [question.id, initial(question, at(given, question.id))])));
|
|
1218
|
+
const ids = new Set(form.questions.map((question) => question.id));
|
|
1219
|
+
const [issues, setIssues] = useState(() => Object.fromEntries(refused.filter((issue) => ids.has(issue.path)).map((issue) => [issue.path, ISSUE_KEYS[issue.message] ? t(ISSUE_KEYS[issue.message]) : issue.message])));
|
|
1220
|
+
const general = refused.filter((issue) => !ids.has(issue.path));
|
|
1221
|
+
const [submitted, setSubmitted] = useState(false);
|
|
1222
|
+
const set = (id, value) => {
|
|
1223
|
+
setValues((current) => ({
|
|
1224
|
+
...current,
|
|
1225
|
+
[id]: value
|
|
1226
|
+
}));
|
|
1227
|
+
setIssues(({ [id]: _, ...rest }) => rest);
|
|
1228
|
+
};
|
|
1229
|
+
const submit = (event) => {
|
|
1230
|
+
event.preventDefault();
|
|
1231
|
+
const answers = collect(form, values);
|
|
1232
|
+
const found = checkAnswers(form, answers);
|
|
1233
|
+
if (found.length > 0) {
|
|
1234
|
+
const messages = {};
|
|
1235
|
+
for (const issue of found) {
|
|
1236
|
+
const key = ISSUE_KEYS[issue.message];
|
|
1237
|
+
messages[issue.question] ??= key ? t(key) : issue.message;
|
|
1238
|
+
}
|
|
1239
|
+
setIssues(messages);
|
|
1240
|
+
const first = form.questions.find((question) => messages[question.id]);
|
|
1241
|
+
formRef.current?.querySelector(`[data-question="${first?.id}"] input`)?.focus();
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
setSubmitted(true);
|
|
1245
|
+
onSubmit(answers);
|
|
1246
|
+
};
|
|
1247
|
+
return /* @__PURE__ */ jsxs("form", {
|
|
1248
|
+
...root,
|
|
1249
|
+
ref: formRef,
|
|
1250
|
+
onSubmit: submit,
|
|
1251
|
+
noValidate: true,
|
|
1252
|
+
"aria-labelledby": `${baseId}-title`,
|
|
1253
|
+
children: [
|
|
1254
|
+
/* @__PURE__ */ jsx("h3", {
|
|
1255
|
+
id: `${baseId}-title`,
|
|
1256
|
+
className: "rungent-card-title",
|
|
1257
|
+
children: form.prompt
|
|
1258
|
+
}),
|
|
1259
|
+
general.length > 0 ? /* @__PURE__ */ jsxs("div", {
|
|
1260
|
+
className: "rungent-issue",
|
|
1261
|
+
role: "alert",
|
|
1262
|
+
children: [t("formIssues"), /* @__PURE__ */ jsx("ul", { children: general.map((issue) => /* @__PURE__ */ jsxs("li", { children: [issue.path ? `${issue.path}: ` : "", issue.message] }, `${issue.path}:${issue.message}`)) })]
|
|
1263
|
+
}) : null,
|
|
1264
|
+
form.questions.map((question) => {
|
|
1265
|
+
const id = `${baseId}-${question.id}`;
|
|
1266
|
+
const value = values[question.id] ?? initial(question, void 0);
|
|
1267
|
+
const issue = issues[question.id];
|
|
1268
|
+
const describedBy = issue ? `${id}-issue` : void 0;
|
|
1269
|
+
const label = /* @__PURE__ */ jsxs(Fragment, { children: [question.prompt, question.required ? /* @__PURE__ */ jsxs("span", {
|
|
1270
|
+
className: "rungent-required",
|
|
1271
|
+
"aria-hidden": "true",
|
|
1272
|
+
children: [" ", "*"]
|
|
1273
|
+
}) : /* @__PURE__ */ jsxs("span", {
|
|
1274
|
+
className: "rungent-muted",
|
|
1275
|
+
children: [
|
|
1276
|
+
" (",
|
|
1277
|
+
t("optional"),
|
|
1278
|
+
")"
|
|
1279
|
+
]
|
|
1280
|
+
})] });
|
|
1281
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1282
|
+
className: "rungent-question",
|
|
1283
|
+
"data-question": question.id,
|
|
1284
|
+
children: [question.kind === "text" || !("selected" in value) ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("label", {
|
|
1285
|
+
htmlFor: id,
|
|
1286
|
+
className: "rungent-label",
|
|
1287
|
+
children: label
|
|
1288
|
+
}), /* @__PURE__ */ jsx("input", {
|
|
1289
|
+
id,
|
|
1290
|
+
className: "rungent-input",
|
|
1291
|
+
value: "text" in value ? value.text : "",
|
|
1292
|
+
placeholder: question.kind === "text" ? question.placeholder : void 0,
|
|
1293
|
+
required: question.required,
|
|
1294
|
+
"aria-invalid": issue ? true : void 0,
|
|
1295
|
+
"aria-describedby": describedBy,
|
|
1296
|
+
onChange: (event) => set(question.id, { text: event.target.value })
|
|
1297
|
+
})] }) : /* @__PURE__ */ jsxs("fieldset", {
|
|
1298
|
+
className: "rungent-choices",
|
|
1299
|
+
"aria-describedby": describedBy,
|
|
1300
|
+
"aria-invalid": issue ? true : void 0,
|
|
1301
|
+
children: [
|
|
1302
|
+
/* @__PURE__ */ jsx("legend", {
|
|
1303
|
+
className: "rungent-label",
|
|
1304
|
+
children: label
|
|
1305
|
+
}),
|
|
1306
|
+
question.options.map((option) => {
|
|
1307
|
+
const checked = value.selected.includes(option.id);
|
|
1308
|
+
return /* @__PURE__ */ jsxs("label", {
|
|
1309
|
+
className: "rungent-choice",
|
|
1310
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
1311
|
+
type: question.multiple ? "checkbox" : "radio",
|
|
1312
|
+
name: id,
|
|
1313
|
+
checked,
|
|
1314
|
+
onChange: () => set(question.id, {
|
|
1315
|
+
custom: question.multiple ? value.custom : "",
|
|
1316
|
+
selected: question.multiple ? checked ? value.selected.filter((selected) => selected !== option.id) : [...value.selected, option.id] : [option.id]
|
|
1317
|
+
})
|
|
1318
|
+
}), /* @__PURE__ */ jsxs("span", { children: [
|
|
1319
|
+
option.label,
|
|
1320
|
+
option.recommended ? /* @__PURE__ */ jsx("span", {
|
|
1321
|
+
className: "rungent-badge rungent-recommended",
|
|
1322
|
+
children: t("recommended")
|
|
1323
|
+
}) : null,
|
|
1324
|
+
option.description ? /* @__PURE__ */ jsx("span", {
|
|
1325
|
+
className: "rungent-choice-description",
|
|
1326
|
+
children: option.description
|
|
1327
|
+
}) : null
|
|
1328
|
+
] })]
|
|
1329
|
+
}, option.id);
|
|
1330
|
+
}),
|
|
1331
|
+
question.allowCustom ? /* @__PURE__ */ jsx("input", {
|
|
1332
|
+
className: "rungent-input",
|
|
1333
|
+
"aria-label": `${question.prompt}: ${t("other")}`,
|
|
1334
|
+
placeholder: t("otherPlaceholder"),
|
|
1335
|
+
value: value.custom,
|
|
1336
|
+
onChange: (event) => set(question.id, {
|
|
1337
|
+
custom: event.target.value,
|
|
1338
|
+
selected: question.multiple || !event.target.value ? value.selected : []
|
|
1339
|
+
})
|
|
1340
|
+
}) : null
|
|
1341
|
+
]
|
|
1342
|
+
}), issue ? /* @__PURE__ */ jsx("p", {
|
|
1343
|
+
id: `${id}-issue`,
|
|
1344
|
+
className: "rungent-issue",
|
|
1345
|
+
role: "alert",
|
|
1346
|
+
children: issue
|
|
1347
|
+
}) : null]
|
|
1348
|
+
}, question.id);
|
|
1349
|
+
}),
|
|
1350
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1351
|
+
className: "rungent-actions",
|
|
1352
|
+
children: [onCancel ? /* @__PURE__ */ jsx("button", {
|
|
1353
|
+
type: "button",
|
|
1354
|
+
className: "rungent-button rungent-ghost",
|
|
1355
|
+
onClick: onCancel,
|
|
1356
|
+
disabled: submitted,
|
|
1357
|
+
children: t("formCancel")
|
|
1358
|
+
}) : null, /* @__PURE__ */ jsx("button", {
|
|
1359
|
+
type: "submit",
|
|
1360
|
+
className: "rungent-button rungent-primary",
|
|
1361
|
+
disabled: submitted,
|
|
1362
|
+
children: t("submit")
|
|
1363
|
+
})]
|
|
1364
|
+
})
|
|
1365
|
+
]
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
1368
|
+
// src/stream.ts
|
|
1369
|
+
/** Streaming text: a steady reveal of what arrived, and Markdown that is safe to show half-done. */
|
|
1370
|
+
/** At least this many characters a frame, so a slow stream still moves. */
|
|
1371
|
+
const MIN_PER_FRAME = 2;
|
|
1372
|
+
/** A backlog is caught up over about this many frames, so a burst does not lag behind. */
|
|
1373
|
+
const CATCH_UP_FRAMES = 24;
|
|
1374
|
+
function reducedMotion() {
|
|
1375
|
+
return typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* The part of `text` to show: while it streams, characters appear at a steady pace instead of in
|
|
1379
|
+
* the bursts the network delivers; they keep appearing after the stream ends until all are shown.
|
|
1380
|
+
* Text that was complete when it mounted (history) shows at once.
|
|
1381
|
+
*/
|
|
1382
|
+
function useSmoothText(text, streaming) {
|
|
1383
|
+
const [shown, setShown] = useState(() => streaming ? 0 : text.length);
|
|
1384
|
+
const target = useRef(text.length);
|
|
1385
|
+
target.current = text.length;
|
|
1386
|
+
const animating = shown < text.length;
|
|
1387
|
+
useEffect(() => {
|
|
1388
|
+
if (!animating) return;
|
|
1389
|
+
if (reducedMotion()) {
|
|
1390
|
+
setShown(target.current);
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
let frame = requestAnimationFrame(function step() {
|
|
1394
|
+
setShown((current) => {
|
|
1395
|
+
const backlog = target.current - current;
|
|
1396
|
+
if (backlog <= 0) return current;
|
|
1397
|
+
return current + Math.max(MIN_PER_FRAME, Math.ceil(backlog / CATCH_UP_FRAMES));
|
|
1398
|
+
});
|
|
1399
|
+
frame = requestAnimationFrame(step);
|
|
1400
|
+
});
|
|
1401
|
+
return () => cancelAnimationFrame(frame);
|
|
1402
|
+
}, [animating]);
|
|
1403
|
+
const end = Math.min(shown, text.length);
|
|
1404
|
+
const code = text.charCodeAt(end - 1);
|
|
1405
|
+
return text.slice(0, code >= 55296 && code <= 56319 ? end - 1 : end);
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Hides a link, image or citation whose Markdown has not fully arrived (`[label](/pa`), so the
|
|
1409
|
+
* reader never sees raw brackets turn into a link.
|
|
1410
|
+
*/
|
|
1411
|
+
function settlePartialMarkdown(text) {
|
|
1412
|
+
const open = text.lastIndexOf("[");
|
|
1413
|
+
if (open < 0) return text;
|
|
1414
|
+
const tail = text.slice(open);
|
|
1415
|
+
if (/^\[[^\]\n]*$/.test(tail) || /^\[[^\]\n]*\]\([^)\s]*$/.test(tail)) return text.slice(0, open > 0 && text[open - 1] === "!" ? open - 1 : open);
|
|
1416
|
+
return text;
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Splits Markdown into top-level blocks at blank lines outside code fences, so that while text
|
|
1420
|
+
* streams only the last block renders again. A blank line followed by an indented line continues
|
|
1421
|
+
* the block (a list item's next paragraph).
|
|
1422
|
+
*/
|
|
1423
|
+
function markdownBlocks(text) {
|
|
1424
|
+
const blocks = [];
|
|
1425
|
+
let current = [];
|
|
1426
|
+
let blank = false;
|
|
1427
|
+
let fence = null;
|
|
1428
|
+
for (const line of text.split("\n")) {
|
|
1429
|
+
if (fence === null && line.trim() === "") {
|
|
1430
|
+
blank = current.length > 0;
|
|
1431
|
+
continue;
|
|
1432
|
+
}
|
|
1433
|
+
if (blank) {
|
|
1434
|
+
if (/^[ \t]/.test(line)) current.push("");
|
|
1435
|
+
else {
|
|
1436
|
+
blocks.push(current.join("\n"));
|
|
1437
|
+
current = [];
|
|
1438
|
+
}
|
|
1439
|
+
blank = false;
|
|
1440
|
+
}
|
|
1441
|
+
const marker = /^\s*(`{3,}|~{3,})/.exec(line)?.[1];
|
|
1442
|
+
if (marker) {
|
|
1443
|
+
if (fence === null) fence = marker;
|
|
1444
|
+
else if (marker.startsWith(fence)) fence = null;
|
|
1445
|
+
}
|
|
1446
|
+
current.push(line);
|
|
1447
|
+
}
|
|
1448
|
+
if (current.length > 0) blocks.push(current.join("\n"));
|
|
1449
|
+
return blocks;
|
|
1450
|
+
}
|
|
1451
|
+
// src/markdown.tsx
|
|
1452
|
+
const CITE = "#rungent-cite-";
|
|
1453
|
+
/**
|
|
1454
|
+
* Turns `[n]` markers with a citation into links the renderer shows as citations; other markers
|
|
1455
|
+
* and code spans are left alone.
|
|
1456
|
+
*/
|
|
1457
|
+
function linkCitations(text, cited) {
|
|
1458
|
+
return text.split(/(`+[^`]*`+)/).map((part, index) => index % 2 === 1 ? part : part.replace(/\[(\d{1,3})\](?!\()/g, (match, n, offset) => {
|
|
1459
|
+
return !((offset === 0 || part[offset - 1] === "\n") && part[offset + match.length] === ":") && cited.has(Number(n)) ? `[${n}](${CITE}${n})` : match;
|
|
1460
|
+
})).join("");
|
|
1461
|
+
}
|
|
1462
|
+
const Block = memo(function Block({ text, components }) {
|
|
1463
|
+
return /* @__PURE__ */ jsx(ReactMarkdown, {
|
|
1464
|
+
remarkPlugins: [remarkGfm],
|
|
1465
|
+
components,
|
|
1466
|
+
children: text
|
|
1467
|
+
});
|
|
1468
|
+
});
|
|
1469
|
+
/** Link and image renderers: citations, same-origin links through `navigate`, relative URLs. */
|
|
1470
|
+
function renderers(cited, navigate, baseUrl) {
|
|
1471
|
+
const resolve = (href) => baseUrl ? new URL(href, baseUrl).href : href;
|
|
1472
|
+
const follow = (event, href) => {
|
|
1473
|
+
if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
1474
|
+
if (href.startsWith("#")) return;
|
|
1475
|
+
event.preventDefault();
|
|
1476
|
+
navigate(href);
|
|
1477
|
+
};
|
|
1478
|
+
return {
|
|
1479
|
+
a({ href = "", children }) {
|
|
1480
|
+
if (href.startsWith(CITE)) {
|
|
1481
|
+
const n = Number(href.slice(14));
|
|
1482
|
+
const hit = cited.get(n)?.hit;
|
|
1483
|
+
if (!hit) return /* @__PURE__ */ jsx(Fragment, { children });
|
|
1484
|
+
return /* @__PURE__ */ jsx("a", {
|
|
1485
|
+
className: "rungent-cite",
|
|
1486
|
+
href: hit.url,
|
|
1487
|
+
title: hit.title,
|
|
1488
|
+
"aria-label": `${n}: ${hit.title}`,
|
|
1489
|
+
onClick: (event) => follow(event, hit.url),
|
|
1490
|
+
children: n
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
const target = resolve(href);
|
|
1494
|
+
const external = samePath(target) === null;
|
|
1495
|
+
return /* @__PURE__ */ jsx("a", {
|
|
1496
|
+
href: target,
|
|
1497
|
+
...external ? {
|
|
1498
|
+
target: "_blank",
|
|
1499
|
+
rel: "noopener noreferrer"
|
|
1500
|
+
} : {},
|
|
1501
|
+
onClick: (event) => follow(event, target),
|
|
1502
|
+
children
|
|
1503
|
+
});
|
|
1504
|
+
},
|
|
1505
|
+
img({ src, alt }) {
|
|
1506
|
+
return typeof src === "string" ? /* @__PURE__ */ jsx("img", {
|
|
1507
|
+
src: resolve(src),
|
|
1508
|
+
alt: alt ?? ""
|
|
1509
|
+
}) : null;
|
|
1510
|
+
},
|
|
1511
|
+
pre({ children }) {
|
|
1512
|
+
return /* @__PURE__ */ jsx("pre", {
|
|
1513
|
+
className: SCROLL_CLASS,
|
|
1514
|
+
children
|
|
1515
|
+
});
|
|
1516
|
+
},
|
|
1517
|
+
table({ children }) {
|
|
1518
|
+
return /* @__PURE__ */ jsx("table", {
|
|
1519
|
+
className: SCROLL_CLASS,
|
|
1520
|
+
children
|
|
1521
|
+
});
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
}
|
|
1525
|
+
const NONE = [];
|
|
1526
|
+
/** Safe Markdown: raw HTML is not rendered, and same-origin links route through `onOpen`. */
|
|
1527
|
+
function Markdown({ text, citations = NONE, baseUrl, streaming }) {
|
|
1528
|
+
const { navigate } = useRungent();
|
|
1529
|
+
const cited = useMemo(() => new Map(citations.map((citation) => [citation.n, citation])), [citations]);
|
|
1530
|
+
const components = useMemo(() => renderers(cited, navigate, baseUrl), [
|
|
1531
|
+
cited,
|
|
1532
|
+
navigate,
|
|
1533
|
+
baseUrl
|
|
1534
|
+
]);
|
|
1535
|
+
return /* @__PURE__ */ jsx("div", {
|
|
1536
|
+
className: "rungent-markdown",
|
|
1537
|
+
"data-streaming": streaming || void 0,
|
|
1538
|
+
children: markdownBlocks(linkCitations(text, cited)).map((block, index) => /* @__PURE__ */ jsx(Block, {
|
|
1539
|
+
text: block,
|
|
1540
|
+
components
|
|
1541
|
+
}, index))
|
|
1542
|
+
});
|
|
1543
|
+
}
|
|
1544
|
+
// src/ring.tsx
|
|
1545
|
+
const SIZE = 18;
|
|
1546
|
+
const RADIUS = 15.5 / 2;
|
|
1547
|
+
const CIRCUMFERENCE = 2 * Math.PI * RADIUS;
|
|
1548
|
+
function compact(tokens, locale) {
|
|
1549
|
+
return new Intl.NumberFormat(locale, {
|
|
1550
|
+
notation: "compact",
|
|
1551
|
+
maximumFractionDigits: 1
|
|
1552
|
+
}).format(tokens);
|
|
1553
|
+
}
|
|
1554
|
+
/** How full the conversation's context is: a ring that turns amber at 70% and red at 90%. */
|
|
1555
|
+
function ContextRing({ usage }) {
|
|
1556
|
+
const { t, locale } = useRungent();
|
|
1557
|
+
const [open, setOpen] = useState(false);
|
|
1558
|
+
const tipId = useId();
|
|
1559
|
+
const percent = Math.round(usage.usedTokens / usage.budgetTokens * 100);
|
|
1560
|
+
const level = percent >= 90 ? "high" : percent >= 70 ? "medium" : "low";
|
|
1561
|
+
const filled = Math.min(percent, 100) / 100;
|
|
1562
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
1563
|
+
className: "rungent-ring-wrap",
|
|
1564
|
+
onMouseEnter: () => setOpen(true),
|
|
1565
|
+
onMouseLeave: () => setOpen(false),
|
|
1566
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
1567
|
+
type: "button",
|
|
1568
|
+
className: "rungent-ring",
|
|
1569
|
+
"data-level": level,
|
|
1570
|
+
"aria-label": `${t("contextTitle")}: ${t("contextUsed", { percent })}`,
|
|
1571
|
+
"aria-describedby": open ? tipId : void 0,
|
|
1572
|
+
onFocus: () => setOpen(true),
|
|
1573
|
+
onBlur: () => setOpen(false),
|
|
1574
|
+
children: /* @__PURE__ */ jsxs("svg", {
|
|
1575
|
+
width: SIZE,
|
|
1576
|
+
height: SIZE,
|
|
1577
|
+
viewBox: `0 0 ${SIZE} ${SIZE}`,
|
|
1578
|
+
"aria-hidden": "true",
|
|
1579
|
+
children: [/* @__PURE__ */ jsx("circle", {
|
|
1580
|
+
className: "rungent-ring-track",
|
|
1581
|
+
cx: SIZE / 2,
|
|
1582
|
+
cy: SIZE / 2,
|
|
1583
|
+
r: RADIUS
|
|
1584
|
+
}), /* @__PURE__ */ jsx("circle", {
|
|
1585
|
+
className: "rungent-ring-value",
|
|
1586
|
+
cx: SIZE / 2,
|
|
1587
|
+
cy: SIZE / 2,
|
|
1588
|
+
r: RADIUS,
|
|
1589
|
+
strokeDasharray: CIRCUMFERENCE,
|
|
1590
|
+
strokeDashoffset: CIRCUMFERENCE * (1 - filled)
|
|
1591
|
+
})]
|
|
1592
|
+
})
|
|
1593
|
+
}), open ? /* @__PURE__ */ jsxs("span", {
|
|
1594
|
+
id: tipId,
|
|
1595
|
+
role: "tooltip",
|
|
1596
|
+
className: "rungent-ring-tip",
|
|
1597
|
+
children: [
|
|
1598
|
+
/* @__PURE__ */ jsx("strong", { children: percent > 100 ? t("contextOver", { percent: percent - 100 }) : t("contextUsed", { percent }) }),
|
|
1599
|
+
/* @__PURE__ */ jsx("span", { children: t("contextDetail", {
|
|
1600
|
+
used: compact(usage.usedTokens, locale),
|
|
1601
|
+
budget: compact(usage.budgetTokens, locale)
|
|
1602
|
+
}) }),
|
|
1603
|
+
usage.source === "estimated" ? /* @__PURE__ */ jsx("span", {
|
|
1604
|
+
className: "rungent-muted",
|
|
1605
|
+
children: t("contextEstimated")
|
|
1606
|
+
}) : null
|
|
1607
|
+
]
|
|
1608
|
+
}) : null]
|
|
1609
|
+
});
|
|
1610
|
+
}
|
|
1611
|
+
// src/trace.ts
|
|
1612
|
+
const SEARCH_TOOL = "search";
|
|
1613
|
+
const INPUT_TOOL = "request_input";
|
|
1614
|
+
/**
|
|
1615
|
+
* Groups items into the user's messages, answers and traces: the steps between two of them.
|
|
1616
|
+
* While `running`, a run that has shown nothing yet (or ended a step with text and works on)
|
|
1617
|
+
* gets an empty trace at the end, under the key its first step keeps.
|
|
1618
|
+
*/
|
|
1619
|
+
function threadBlocks(items, running) {
|
|
1620
|
+
const blocks = [];
|
|
1621
|
+
let turn = "start";
|
|
1622
|
+
let traces = 0;
|
|
1623
|
+
for (const item of items) {
|
|
1624
|
+
const last = blocks.at(-1);
|
|
1625
|
+
if (item.kind === "user") {
|
|
1626
|
+
turn = item.id;
|
|
1627
|
+
traces = 0;
|
|
1628
|
+
blocks.push({
|
|
1629
|
+
kind: "user",
|
|
1630
|
+
key: item.id,
|
|
1631
|
+
item
|
|
1632
|
+
});
|
|
1633
|
+
} else if (item.kind === "assistant") blocks.push({
|
|
1634
|
+
kind: "answer",
|
|
1635
|
+
key: item.id,
|
|
1636
|
+
item
|
|
1637
|
+
});
|
|
1638
|
+
else if (last?.kind === "trace") last.steps.push(item);
|
|
1639
|
+
else blocks.push({
|
|
1640
|
+
kind: "trace",
|
|
1641
|
+
key: `trace:${turn}:${traces++}`,
|
|
1642
|
+
steps: [item]
|
|
1643
|
+
});
|
|
1644
|
+
}
|
|
1645
|
+
const last = blocks.at(-1);
|
|
1646
|
+
if (running && last?.kind !== "trace" && !(last?.kind === "answer" && last.item.streaming)) blocks.push({
|
|
1647
|
+
kind: "trace",
|
|
1648
|
+
key: `trace:${turn}:${traces}`,
|
|
1649
|
+
steps: []
|
|
1650
|
+
});
|
|
1651
|
+
return blocks;
|
|
1652
|
+
}
|
|
1653
|
+
const isSearch = (tool) => (tool.meta?.op ?? tool.name) === SEARCH_TOOL;
|
|
1654
|
+
const isInput = (tool) => (tool.meta?.op ?? tool.name) === INPUT_TOOL;
|
|
1655
|
+
function parse(json) {
|
|
1656
|
+
if (json === null) return void 0;
|
|
1657
|
+
try {
|
|
1658
|
+
return JSON.parse(json);
|
|
1659
|
+
} catch {
|
|
1660
|
+
return;
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1664
|
+
/** The hits a search step found, from its result. */
|
|
1665
|
+
function searchHits(tool) {
|
|
1666
|
+
const result = parse(tool.result);
|
|
1667
|
+
if (!isRecord(result) || !Array.isArray(result.hits)) return null;
|
|
1668
|
+
return result.hits.flatMap((hit) => isRecord(hit) && typeof hit.title === "string" ? [{
|
|
1669
|
+
title: hit.title,
|
|
1670
|
+
...typeof hit.url === "string" ? { url: hit.url } : {}
|
|
1671
|
+
}] : []);
|
|
1672
|
+
}
|
|
1673
|
+
const LIST_KEYS = [
|
|
1674
|
+
"items",
|
|
1675
|
+
"data",
|
|
1676
|
+
"results",
|
|
1677
|
+
"records",
|
|
1678
|
+
"list",
|
|
1679
|
+
"hits"
|
|
1680
|
+
];
|
|
1681
|
+
/** How many things an operation's result lists: an array, or the one list in an object. */
|
|
1682
|
+
function resultCount(json) {
|
|
1683
|
+
const result = parse(json);
|
|
1684
|
+
if (Array.isArray(result)) return result.length;
|
|
1685
|
+
if (!isRecord(result)) return null;
|
|
1686
|
+
for (const key of LIST_KEYS) {
|
|
1687
|
+
const value = result[key];
|
|
1688
|
+
if (Array.isArray(value)) return value.length;
|
|
1689
|
+
}
|
|
1690
|
+
const lists = Object.values(result).filter(Array.isArray);
|
|
1691
|
+
return lists.length === 1 ? lists[0].length : null;
|
|
1692
|
+
}
|
|
1693
|
+
const ARG_LIMIT = 3;
|
|
1694
|
+
const VALUE_LIMIT = 32;
|
|
1695
|
+
/** The call's plain arguments as `key=value`, the first few, long values cut. */
|
|
1696
|
+
function keyArguments(args) {
|
|
1697
|
+
if (!args) return [];
|
|
1698
|
+
return Object.entries(args).filter((entry) => [
|
|
1699
|
+
"string",
|
|
1700
|
+
"number",
|
|
1701
|
+
"boolean"
|
|
1702
|
+
].includes(typeof entry[1]) && entry[1] !== "").slice(0, ARG_LIMIT).map(([key, value]) => {
|
|
1703
|
+
const text = String(value).replace(/\s+/g, " ");
|
|
1704
|
+
return `${key}=${text.length > VALUE_LIMIT ? `${text.slice(0, 31)}…` : text}`;
|
|
1705
|
+
});
|
|
1706
|
+
}
|
|
1707
|
+
/** The search text of a search step. */
|
|
1708
|
+
function searchQuery(tool) {
|
|
1709
|
+
const q = tool.arguments?.q;
|
|
1710
|
+
return typeof q === "string" ? q.trim() : "";
|
|
1711
|
+
}
|
|
1712
|
+
/** A tool's name for people: its operation's title, else its name made readable. */
|
|
1713
|
+
function toolTitle(tool) {
|
|
1714
|
+
if (tool.meta?.title) return tool.meta.title;
|
|
1715
|
+
const words = tool.name.replace(/[_-]+/g, " ").trim();
|
|
1716
|
+
return words ? words[0]?.toUpperCase() + words.slice(1) : tool.id;
|
|
1717
|
+
}
|
|
1718
|
+
/** What a step did, in a line: `Searched for "q"`, `List servers`, `Thinking…`. */
|
|
1719
|
+
function stepLabel(step, t) {
|
|
1720
|
+
if (step.kind === "progress") return step.message;
|
|
1721
|
+
if (step.kind === "reasoning") return step.streaming ? t("thinking") : t("traceThought");
|
|
1722
|
+
if (isSearch(step)) {
|
|
1723
|
+
const query = searchQuery(step);
|
|
1724
|
+
if (step.status === "running") return query ? t("traceSearching", { query }) : t("searching");
|
|
1725
|
+
return query ? t("traceSearched", { query }) : t("traceSearchedPlain");
|
|
1726
|
+
}
|
|
1727
|
+
if (isInput(step)) return t("traceAsked");
|
|
1728
|
+
return toolTitle(step);
|
|
1729
|
+
}
|
|
1730
|
+
/** What a step found, in short: `5 results`, `zone=bj-1`, `12 items`. */
|
|
1731
|
+
function stepFacts(step, t) {
|
|
1732
|
+
if (step.kind !== "tool" || isInput(step)) return [];
|
|
1733
|
+
if (isSearch(step)) {
|
|
1734
|
+
const hits = step.status === "succeeded" ? searchHits(step) : null;
|
|
1735
|
+
if (!hits) return [];
|
|
1736
|
+
return [hits.length === 1 ? t("traceResult") : t("traceResults", { count: hits.length })];
|
|
1737
|
+
}
|
|
1738
|
+
const facts = keyArguments(step.arguments);
|
|
1739
|
+
const count = step.status === "succeeded" ? resultCount(step.result) : null;
|
|
1740
|
+
if (count !== null) facts.push(count === 1 ? t("traceItem") : t("traceItems", { count }));
|
|
1741
|
+
if (step.deduplicated) facts.push(t("toolDeduplicated"));
|
|
1742
|
+
return facts;
|
|
1743
|
+
}
|
|
1744
|
+
const SUMMARY_LIMIT = 3;
|
|
1745
|
+
/**
|
|
1746
|
+
* What a finished trace did, in one line without its time: `Searched · List servers`. Repeats
|
|
1747
|
+
* fold together; past three it says how many more.
|
|
1748
|
+
*/
|
|
1749
|
+
function summarize(steps, t) {
|
|
1750
|
+
const names = [];
|
|
1751
|
+
for (const step of steps) {
|
|
1752
|
+
if (step.kind === "progress") continue;
|
|
1753
|
+
const name = step.kind === "reasoning" ? t("traceThought") : isSearch(step) ? t("traceSearchedPlain") : isInput(step) ? t("traceAsked") : toolTitle(step);
|
|
1754
|
+
if (!names.includes(name)) names.push(name);
|
|
1755
|
+
}
|
|
1756
|
+
const acts = names.length > 1 ? names.filter((name) => name !== t("traceThought")) : names;
|
|
1757
|
+
if (acts.length === 0) return t("traceWorked");
|
|
1758
|
+
const shown = acts.slice(0, SUMMARY_LIMIT);
|
|
1759
|
+
const line = shown.join(t("traceSeparator"));
|
|
1760
|
+
const more = acts.length - shown.length;
|
|
1761
|
+
return more > 0 ? `${line} ${t("traceMore", { count: more })}` : line;
|
|
1762
|
+
}
|
|
1763
|
+
/** The span of a trace in milliseconds by the server's clock, until `endAt` when later. */
|
|
1764
|
+
function traceDuration(steps, endAt) {
|
|
1765
|
+
const starts = steps.map((step) => step.startedAt).filter((at) => at !== null);
|
|
1766
|
+
const ends = steps.map((step) => step.kind === "progress" ? step.startedAt : step.endedAt).concat(endAt).filter((at) => at !== null);
|
|
1767
|
+
if (starts.length === 0 || ends.length === 0) return null;
|
|
1768
|
+
const span = Math.max(...ends) - Math.min(...starts);
|
|
1769
|
+
return span >= 0 ? span : null;
|
|
1770
|
+
}
|
|
1771
|
+
/** A step's time in milliseconds by the server's clock, once it ended. */
|
|
1772
|
+
function stepDuration(step) {
|
|
1773
|
+
if (step.kind === "progress" || step.startedAt === null || step.endedAt === null) return null;
|
|
1774
|
+
return Math.max(0, step.endedAt - step.startedAt);
|
|
1775
|
+
}
|
|
1776
|
+
/** `0.4s`, `12s`, `1m 5s`, in the locale's units. */
|
|
1777
|
+
function formatDuration(ms, locale) {
|
|
1778
|
+
const unit = (value, name, digits = 0) => new Intl.NumberFormat(locale, {
|
|
1779
|
+
style: "unit",
|
|
1780
|
+
unit: name,
|
|
1781
|
+
unitDisplay: "narrow",
|
|
1782
|
+
maximumFractionDigits: digits
|
|
1783
|
+
}).format(value);
|
|
1784
|
+
if (ms < 1e4) return unit(Math.max(ms, 100) / 1e3, "second", 1);
|
|
1785
|
+
const seconds = Math.round(ms / 1e3);
|
|
1786
|
+
if (seconds < 60) return unit(seconds, "second");
|
|
1787
|
+
return `${unit(Math.floor(seconds / 60), "minute")} ${unit(seconds % 60, "second")}`;
|
|
1788
|
+
}
|
|
1789
|
+
// src/trace-view.tsx
|
|
1790
|
+
/** A run's steps, live while it works and folded into one line once it is done. */
|
|
1791
|
+
/**
|
|
1792
|
+
* When this page first saw each step or trace, by id: the server's timestamps say how long a
|
|
1793
|
+
* finished step took, but a live one counts on this clock, which may differ from the server's.
|
|
1794
|
+
* Kept outside React, so a dock that mounts again keeps counting.
|
|
1795
|
+
*/
|
|
1796
|
+
const firstSeen = /* @__PURE__ */ new Map();
|
|
1797
|
+
const seen = (id) => {
|
|
1798
|
+
const at = firstSeen.get(id);
|
|
1799
|
+
if (at !== void 0) return at;
|
|
1800
|
+
const now = Date.now();
|
|
1801
|
+
firstSeen.set(id, now);
|
|
1802
|
+
return now;
|
|
1803
|
+
};
|
|
1804
|
+
/** The time now, again every second while `ticking`. */
|
|
1805
|
+
function useNow(ticking) {
|
|
1806
|
+
const [now, setNow] = useState(Date.now);
|
|
1807
|
+
useEffect(() => {
|
|
1808
|
+
if (!ticking) return;
|
|
1809
|
+
setNow(Date.now());
|
|
1810
|
+
const timer = setInterval(() => setNow(Date.now()), 1e3);
|
|
1811
|
+
return () => clearInterval(timer);
|
|
1812
|
+
}, [ticking]);
|
|
1813
|
+
return now;
|
|
1814
|
+
}
|
|
1815
|
+
function Chevron() {
|
|
1816
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
1817
|
+
viewBox: "0 0 24 24",
|
|
1818
|
+
width: "14",
|
|
1819
|
+
height: "14",
|
|
1820
|
+
"aria-hidden": "true",
|
|
1821
|
+
className: "rungent-icon",
|
|
1822
|
+
children: /* @__PURE__ */ jsx("path", { d: "m9 6 6 6-6 6" })
|
|
1823
|
+
});
|
|
1824
|
+
}
|
|
1825
|
+
function StepMark({ mark }) {
|
|
1826
|
+
return mark === "running" ? /* @__PURE__ */ jsx("span", {
|
|
1827
|
+
className: "rungent-spinner rungent-step-mark",
|
|
1828
|
+
"aria-hidden": "true"
|
|
1829
|
+
}) : /* @__PURE__ */ jsx("span", {
|
|
1830
|
+
className: "rungent-step-mark rungent-status",
|
|
1831
|
+
"data-status": mark,
|
|
1832
|
+
"aria-hidden": "true"
|
|
1833
|
+
});
|
|
1834
|
+
}
|
|
1835
|
+
function Json({ label, value }) {
|
|
1836
|
+
const ref = useRef(null);
|
|
1837
|
+
useScrollbar(ref);
|
|
1838
|
+
let text;
|
|
1839
|
+
try {
|
|
1840
|
+
text = JSON.stringify(typeof value === "string" ? JSON.parse(value) : value, null, 2);
|
|
1841
|
+
} catch {
|
|
1842
|
+
text = String(value);
|
|
1843
|
+
}
|
|
1844
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("div", {
|
|
1845
|
+
className: "rungent-step-json-label",
|
|
1846
|
+
children: label
|
|
1847
|
+
}), /* @__PURE__ */ jsx("pre", {
|
|
1848
|
+
ref,
|
|
1849
|
+
className: `rungent-step-json ${SCROLL_CLASS}`,
|
|
1850
|
+
children: text
|
|
1851
|
+
})] });
|
|
1852
|
+
}
|
|
1853
|
+
/** Reasoning as it streams: a few lines high, following its end while it grows. */
|
|
1854
|
+
function Reasoning({ text, streaming }) {
|
|
1855
|
+
const ref = useRef(null);
|
|
1856
|
+
useScrollbar(ref);
|
|
1857
|
+
useLayoutEffect(() => {
|
|
1858
|
+
const element = ref.current;
|
|
1859
|
+
if (element && streaming && text) element.scrollTop = element.scrollHeight;
|
|
1860
|
+
}, [text, streaming]);
|
|
1861
|
+
return /* @__PURE__ */ jsx("div", {
|
|
1862
|
+
ref,
|
|
1863
|
+
className: `rungent-step-reasoning ${SCROLL_CLASS}`,
|
|
1864
|
+
children: text
|
|
1865
|
+
});
|
|
1866
|
+
}
|
|
1867
|
+
function Hits({ tool }) {
|
|
1868
|
+
const { navigate } = useRungent();
|
|
1869
|
+
const hits = searchHits(tool) ?? [];
|
|
1870
|
+
if (hits.length === 0) return null;
|
|
1871
|
+
const follow = (event, url) => {
|
|
1872
|
+
if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
1873
|
+
event.preventDefault();
|
|
1874
|
+
navigate(url);
|
|
1875
|
+
};
|
|
1876
|
+
return /* @__PURE__ */ jsx("ol", {
|
|
1877
|
+
className: "rungent-step-hits",
|
|
1878
|
+
children: hits.map((hit, index) => /* @__PURE__ */ jsx("li", { children: hit.url ? /* @__PURE__ */ jsx("a", {
|
|
1879
|
+
href: hit.url,
|
|
1880
|
+
onClick: (event) => follow(event, hit.url),
|
|
1881
|
+
children: hit.title
|
|
1882
|
+
}) : hit.title }, index))
|
|
1883
|
+
});
|
|
1884
|
+
}
|
|
1885
|
+
/** What a step shows when opened: hits, reasoning, the error, and its JSON on request. */
|
|
1886
|
+
function stepBody(step, t) {
|
|
1887
|
+
if (step.kind === "reasoning") return step.text ? /* @__PURE__ */ jsx(Reasoning, {
|
|
1888
|
+
text: step.text,
|
|
1889
|
+
streaming: step.streaming
|
|
1890
|
+
}) : null;
|
|
1891
|
+
if (step.kind !== "tool") return null;
|
|
1892
|
+
const hits = isSearch(step) && step.status === "succeeded" ? /* @__PURE__ */ jsx(Hits, { tool: step }) : null;
|
|
1893
|
+
const json = step.arguments || step.result ? /* @__PURE__ */ jsxs("details", {
|
|
1894
|
+
className: "rungent-step-details",
|
|
1895
|
+
children: [
|
|
1896
|
+
/* @__PURE__ */ jsx("summary", { children: t("traceDetails") }),
|
|
1897
|
+
step.arguments ? /* @__PURE__ */ jsx(Json, {
|
|
1898
|
+
label: t("traceArguments"),
|
|
1899
|
+
value: step.arguments
|
|
1900
|
+
}) : null,
|
|
1901
|
+
step.result ? /* @__PURE__ */ jsx(Json, {
|
|
1902
|
+
label: t("traceResultJson"),
|
|
1903
|
+
value: step.result
|
|
1904
|
+
}) : null
|
|
1905
|
+
]
|
|
1906
|
+
}) : null;
|
|
1907
|
+
return hits || json ? /* @__PURE__ */ jsxs(Fragment, { children: [hits, json] }) : null;
|
|
1908
|
+
}
|
|
1909
|
+
function Step({ step, active, live, awaiting, now }) {
|
|
1910
|
+
const { t, locale } = useRungent();
|
|
1911
|
+
const unfinished = step.kind === "tool" && step.status === "running" || step.kind === "reasoning" && step.streaming;
|
|
1912
|
+
const running = unfinished && active && awaiting === null;
|
|
1913
|
+
const [open, setOpen] = useState(null);
|
|
1914
|
+
const expanded = open ?? (step.kind === "reasoning" && step.streaming);
|
|
1915
|
+
const bodyId = useId();
|
|
1916
|
+
const startedHere = seen(step.id);
|
|
1917
|
+
const body = stepBody(step, t);
|
|
1918
|
+
const failed = step.kind === "tool" && step.status === "failed";
|
|
1919
|
+
const mark = running ? "running" : awaiting ? "waiting" : failed ? "failed" : step.kind === "progress" || unfinished ? "note" : "succeeded";
|
|
1920
|
+
const duration = running ? now - startedHere : stepDuration(step);
|
|
1921
|
+
const facts = stepFacts(step, t);
|
|
1922
|
+
if (awaiting) facts.push(awaiting === "approval" ? t("toolAwaitingApproval") : awaiting === "form" ? t("toolAwaitingInput") : t("traceAwaitingAnswer"));
|
|
1923
|
+
const row = /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1924
|
+
/* @__PURE__ */ jsx(StepMark, { mark }),
|
|
1925
|
+
/* @__PURE__ */ jsxs("span", {
|
|
1926
|
+
className: "rungent-step-text",
|
|
1927
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
1928
|
+
className: "rungent-step-label",
|
|
1929
|
+
children: stepLabel(step, t)
|
|
1930
|
+
}), facts.map((fact) => /* @__PURE__ */ jsx("span", {
|
|
1931
|
+
className: "rungent-step-fact",
|
|
1932
|
+
children: fact
|
|
1933
|
+
}, fact))]
|
|
1934
|
+
}),
|
|
1935
|
+
duration !== null && (running || duration >= 100) && step.kind !== "progress" ? /* @__PURE__ */ jsx("span", {
|
|
1936
|
+
className: "rungent-step-time",
|
|
1937
|
+
children: formatDuration(duration, locale)
|
|
1938
|
+
}) : null,
|
|
1939
|
+
body ? /* @__PURE__ */ jsx(Chevron, {}) : null
|
|
1940
|
+
] });
|
|
1941
|
+
return /* @__PURE__ */ jsxs("li", {
|
|
1942
|
+
className: "rungent-step",
|
|
1943
|
+
"data-live": live || void 0,
|
|
1944
|
+
"data-open": body && expanded || void 0,
|
|
1945
|
+
children: [
|
|
1946
|
+
body ? /* @__PURE__ */ jsx("button", {
|
|
1947
|
+
type: "button",
|
|
1948
|
+
className: "rungent-step-row",
|
|
1949
|
+
"aria-expanded": expanded,
|
|
1950
|
+
"aria-controls": bodyId,
|
|
1951
|
+
onClick: () => setOpen(!expanded),
|
|
1952
|
+
children: row
|
|
1953
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
1954
|
+
className: "rungent-step-row",
|
|
1955
|
+
children: row
|
|
1956
|
+
}),
|
|
1957
|
+
failed && step.kind === "tool" ? /* @__PURE__ */ jsx("p", {
|
|
1958
|
+
className: "rungent-step-error",
|
|
1959
|
+
children: step.error?.code === "not_executed" ? t("toolNotExecuted") : step.error?.message || t("toolFailed")
|
|
1960
|
+
}) : null,
|
|
1961
|
+
body && expanded ? /* @__PURE__ */ jsx("div", {
|
|
1962
|
+
id: bodyId,
|
|
1963
|
+
className: "rungent-step-body",
|
|
1964
|
+
children: body
|
|
1965
|
+
}) : null
|
|
1966
|
+
]
|
|
1967
|
+
});
|
|
1968
|
+
}
|
|
1969
|
+
/**
|
|
1970
|
+
* The steps of a run: each with what it did, its time, and what it found on request. Live, the
|
|
1971
|
+
* header says what the run does now and the steps are open; done, it folds into one line
|
|
1972
|
+
* (`Searched, list servers · 14s`) that opens again on a click.
|
|
1973
|
+
*/
|
|
1974
|
+
function Trace({ traceKey, steps, live, writing, endAt, retry, awaiting }) {
|
|
1975
|
+
const { t, locale } = useRungent();
|
|
1976
|
+
const [open, setOpen] = useState(null);
|
|
1977
|
+
const waiting = steps.some((step) => step.kind === "tool" && awaiting(step.id) !== null);
|
|
1978
|
+
const expanded = open ?? (live && !writing || waiting);
|
|
1979
|
+
const startedHere = seen(traceKey);
|
|
1980
|
+
const now = useNow(live);
|
|
1981
|
+
const listId = useId();
|
|
1982
|
+
const last = steps.at(-1);
|
|
1983
|
+
const lastRunning = last && (last.kind === "tool" && last.status === "running" || last.kind === "reasoning" && last.streaming);
|
|
1984
|
+
const thinking = live && !writing && !lastRunning;
|
|
1985
|
+
const failed = steps.some((step) => step.kind === "tool" && step.status === "failed");
|
|
1986
|
+
let title;
|
|
1987
|
+
if (live) title = retry ? t("retrying", {
|
|
1988
|
+
attempt: retry.attempt,
|
|
1989
|
+
max: retry.maxAttempts
|
|
1990
|
+
}) : writing ? t("traceWriting") : expanded && steps.length > 0 ? t("traceWorking") : thinking || !last ? t("thinking") : stepLabel(last, t);
|
|
1991
|
+
else title = summarize(steps, t);
|
|
1992
|
+
const duration = live ? now - startedHere : traceDuration(steps, endAt);
|
|
1993
|
+
return /* @__PURE__ */ jsxs("section", {
|
|
1994
|
+
className: "rungent-trace",
|
|
1995
|
+
"data-live": live || void 0,
|
|
1996
|
+
"aria-label": title,
|
|
1997
|
+
children: [/* @__PURE__ */ jsxs("button", {
|
|
1998
|
+
type: "button",
|
|
1999
|
+
className: "rungent-trace-head",
|
|
2000
|
+
"aria-expanded": expanded,
|
|
2001
|
+
"aria-controls": listId,
|
|
2002
|
+
onClick: () => setOpen(!expanded),
|
|
2003
|
+
disabled: steps.length === 0,
|
|
2004
|
+
children: [
|
|
2005
|
+
/* @__PURE__ */ jsx(StepMark, { mark: live ? "running" : waiting ? "waiting" : failed ? "failed" : "succeeded" }),
|
|
2006
|
+
/* @__PURE__ */ jsx("span", {
|
|
2007
|
+
className: "rungent-trace-title",
|
|
2008
|
+
children: title
|
|
2009
|
+
}),
|
|
2010
|
+
duration !== null && (live ? duration >= 2e3 : true) ? /* @__PURE__ */ jsx("span", {
|
|
2011
|
+
className: "rungent-step-time",
|
|
2012
|
+
children: formatDuration(duration, locale)
|
|
2013
|
+
}) : null,
|
|
2014
|
+
steps.length > 0 ? /* @__PURE__ */ jsx(Chevron, {}) : null
|
|
2015
|
+
]
|
|
2016
|
+
}), expanded && steps.length > 0 ? /* @__PURE__ */ jsxs("ol", {
|
|
2017
|
+
id: listId,
|
|
2018
|
+
className: "rungent-steps",
|
|
2019
|
+
children: [steps.map((step) => /* @__PURE__ */ jsx(Step, {
|
|
2020
|
+
step,
|
|
2021
|
+
active: live,
|
|
2022
|
+
live: live && step === last && !thinking,
|
|
2023
|
+
awaiting: step.kind === "tool" ? awaiting(step.id) : null,
|
|
2024
|
+
now
|
|
2025
|
+
}, step.id)), thinking ? /* @__PURE__ */ jsx("li", {
|
|
2026
|
+
className: "rungent-step",
|
|
2027
|
+
"data-live": "",
|
|
2028
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
2029
|
+
className: "rungent-step-row",
|
|
2030
|
+
children: [/* @__PURE__ */ jsx(StepMark, { mark: "running" }), /* @__PURE__ */ jsx("span", {
|
|
2031
|
+
className: "rungent-step-text",
|
|
2032
|
+
children: /* @__PURE__ */ jsx("span", {
|
|
2033
|
+
className: "rungent-step-label",
|
|
2034
|
+
children: t("thinking")
|
|
2035
|
+
})
|
|
2036
|
+
})]
|
|
2037
|
+
})
|
|
2038
|
+
}) : null]
|
|
2039
|
+
}) : null]
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
// src/thread.tsx
|
|
2043
|
+
/**
|
|
2044
|
+
* An operation's form: the host's own component for the operation when it registered one, else
|
|
2045
|
+
* the generic form the host's `role: form` operation described. Both start from the arguments.
|
|
2046
|
+
*/
|
|
2047
|
+
function HostForm({ open, submit, submitAnswers, cancel }) {
|
|
2048
|
+
const { forms, t } = useRungent();
|
|
2049
|
+
const root = useRoot("rungent-card rungent-host-form");
|
|
2050
|
+
const { op, form, arguments: args, issues = [] } = open.meta;
|
|
2051
|
+
const Own = forms[op];
|
|
2052
|
+
if (!Own) return /* @__PURE__ */ jsx(RungentForm, {
|
|
2053
|
+
form,
|
|
2054
|
+
values: args,
|
|
2055
|
+
issues,
|
|
2056
|
+
onSubmit: submitAnswers,
|
|
2057
|
+
onCancel: cancel
|
|
2058
|
+
});
|
|
2059
|
+
return /* @__PURE__ */ jsxs("section", {
|
|
2060
|
+
...root,
|
|
2061
|
+
"aria-label": form.prompt,
|
|
2062
|
+
"data-op": op,
|
|
2063
|
+
children: [
|
|
2064
|
+
/* @__PURE__ */ jsx("h3", {
|
|
2065
|
+
className: "rungent-card-title",
|
|
2066
|
+
children: form.prompt
|
|
2067
|
+
}),
|
|
2068
|
+
issues.length > 0 ? /* @__PURE__ */ jsxs("div", {
|
|
2069
|
+
className: "rungent-issue",
|
|
2070
|
+
role: "alert",
|
|
2071
|
+
children: [t("formIssues"), /* @__PURE__ */ jsx("ul", { children: issues.map((issue) => /* @__PURE__ */ jsxs("li", { children: [issue.path ? `${issue.path}: ` : "", issue.message] }, `${issue.path}:${issue.message}`)) })]
|
|
2072
|
+
}) : null,
|
|
2073
|
+
/* @__PURE__ */ jsx(Own, {
|
|
2074
|
+
op,
|
|
2075
|
+
arguments: args,
|
|
2076
|
+
issues,
|
|
2077
|
+
submit,
|
|
2078
|
+
cancel
|
|
2079
|
+
})
|
|
2080
|
+
]
|
|
2081
|
+
});
|
|
2082
|
+
}
|
|
2083
|
+
/** Answers an interrupt, then moves focus to the composer: the card it came from goes away. */
|
|
2084
|
+
function Interrupt({ open, thread, focusComposer }) {
|
|
2085
|
+
const answer = (entries) => {
|
|
2086
|
+
focusComposer();
|
|
2087
|
+
thread.resume(entries);
|
|
2088
|
+
};
|
|
2089
|
+
const { t } = useRungent();
|
|
2090
|
+
switch (open.kind) {
|
|
2091
|
+
case "approval": return /* @__PURE__ */ jsx(ApprovalCard, {
|
|
2092
|
+
interrupt: open,
|
|
2093
|
+
onAnswer: (approved) => answer([answerApproval(open, approved)])
|
|
2094
|
+
});
|
|
2095
|
+
case "input": return /* @__PURE__ */ jsx(RungentForm, {
|
|
2096
|
+
form: open.form,
|
|
2097
|
+
onSubmit: (answers) => answer([answerInput(open, answers)])
|
|
2098
|
+
});
|
|
2099
|
+
case "form": return /* @__PURE__ */ jsx(HostForm, {
|
|
2100
|
+
open,
|
|
2101
|
+
submit: (args) => answer([submitForm(open, args)]),
|
|
2102
|
+
submitAnswers: (answers) => answer([submitFormAnswers(open, answers)]),
|
|
2103
|
+
cancel: () => answer(cancelAll([open]))
|
|
2104
|
+
});
|
|
2105
|
+
case "task": return /* @__PURE__ */ jsxs("div", {
|
|
2106
|
+
className: "rungent-card rungent-task",
|
|
2107
|
+
role: "status",
|
|
2108
|
+
children: [
|
|
2109
|
+
/* @__PURE__ */ jsx("span", {
|
|
2110
|
+
className: "rungent-spinner",
|
|
2111
|
+
"aria-hidden": "true"
|
|
2112
|
+
}),
|
|
2113
|
+
/* @__PURE__ */ jsx("span", { children: open.progress?.message ?? t("taskRunning", { op: open.op }) }),
|
|
2114
|
+
/* @__PURE__ */ jsx("button", {
|
|
2115
|
+
type: "button",
|
|
2116
|
+
className: "rungent-button",
|
|
2117
|
+
onClick: () => answer(cancelAll([open])),
|
|
2118
|
+
children: t("cancelTask")
|
|
2119
|
+
})
|
|
2120
|
+
]
|
|
2121
|
+
});
|
|
2122
|
+
default: return null;
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
function errorKey(code) {
|
|
2126
|
+
switch (code) {
|
|
2127
|
+
case "model_unavailable":
|
|
2128
|
+
case "max_steps":
|
|
2129
|
+
case "open_interrupts":
|
|
2130
|
+
case "history_conflict":
|
|
2131
|
+
case "invalid_interaction": return `error.${code}`;
|
|
2132
|
+
default: return "error.internal";
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
let nextAttachment = 0;
|
|
2136
|
+
function useAttachments() {
|
|
2137
|
+
const { upload } = useRungent();
|
|
2138
|
+
const [pending, setPending] = useState([]);
|
|
2139
|
+
const settle = (key, change) => setPending((current) => current.map((item) => item.key === key ? {
|
|
2140
|
+
...item,
|
|
2141
|
+
...change
|
|
2142
|
+
} : item));
|
|
2143
|
+
const add = (files) => {
|
|
2144
|
+
if (!upload) return;
|
|
2145
|
+
for (const file of files) {
|
|
2146
|
+
const key = nextAttachment++;
|
|
2147
|
+
setPending((current) => [...current, {
|
|
2148
|
+
key,
|
|
2149
|
+
name: file.name,
|
|
2150
|
+
image: file.type.startsWith("image/"),
|
|
2151
|
+
status: "uploading"
|
|
2152
|
+
}]);
|
|
2153
|
+
upload(file).then((uploaded) => settle(key, {
|
|
2154
|
+
status: "done",
|
|
2155
|
+
file: uploaded
|
|
2156
|
+
}), () => settle(key, { status: "failed" }));
|
|
2157
|
+
}
|
|
2158
|
+
};
|
|
2159
|
+
return {
|
|
2160
|
+
pending,
|
|
2161
|
+
add,
|
|
2162
|
+
remove: (key) => setPending((current) => current.filter((item) => item.key !== key)),
|
|
2163
|
+
clear: () => setPending([])
|
|
2164
|
+
};
|
|
2165
|
+
}
|
|
2166
|
+
function PaperclipIcon() {
|
|
2167
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
2168
|
+
viewBox: "0 0 24 24",
|
|
2169
|
+
width: "18",
|
|
2170
|
+
height: "18",
|
|
2171
|
+
"aria-hidden": "true",
|
|
2172
|
+
className: "rungent-icon",
|
|
2173
|
+
children: /* @__PURE__ */ jsx("path", { d: "m21.4 11.1-8.6 8.6a5.7 5.7 0 0 1-8-8l8.5-8.6a3.8 3.8 0 0 1 5.4 5.4l-8.6 8.6a1.9 1.9 0 0 1-2.7-2.7l7.9-7.9" })
|
|
2174
|
+
});
|
|
2175
|
+
}
|
|
2176
|
+
function FileIcon() {
|
|
2177
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
2178
|
+
viewBox: "0 0 24 24",
|
|
2179
|
+
width: "16",
|
|
2180
|
+
height: "16",
|
|
2181
|
+
"aria-hidden": "true",
|
|
2182
|
+
className: "rungent-icon",
|
|
2183
|
+
children: [/* @__PURE__ */ jsx("path", { d: "M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" }), /* @__PURE__ */ jsx("path", { d: "M14 2v6h6" })]
|
|
2184
|
+
});
|
|
2185
|
+
}
|
|
2186
|
+
function ThumbIcon({ down }) {
|
|
2187
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
2188
|
+
viewBox: "0 0 24 24",
|
|
2189
|
+
width: "15",
|
|
2190
|
+
height: "15",
|
|
2191
|
+
"aria-hidden": "true",
|
|
2192
|
+
className: "rungent-icon",
|
|
2193
|
+
style: down ? { transform: "scaleY(-1)" } : void 0,
|
|
2194
|
+
children: [/* @__PURE__ */ jsx("path", { d: "M7 10v12" }), /* @__PURE__ */ jsx("path", { d: "M15 5.9 14 10h5.8a2 2 0 0 1 1.9 2.6l-2.3 8a2 2 0 0 1-1.9 1.4H4a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2h2.8a2 2 0 0 0 1.8-1.1L12 2a3.1 3.1 0 0 1 3 3.9Z" })]
|
|
2195
|
+
});
|
|
2196
|
+
}
|
|
2197
|
+
function PendingList({ attachments }) {
|
|
2198
|
+
const { t } = useRungent();
|
|
2199
|
+
const { pending, remove } = attachments;
|
|
2200
|
+
if (pending.length === 0) return null;
|
|
2201
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("ul", {
|
|
2202
|
+
className: "rungent-attachments",
|
|
2203
|
+
"aria-label": t("attachments"),
|
|
2204
|
+
children: pending.map((item) => /* @__PURE__ */ jsxs("li", {
|
|
2205
|
+
className: item.image ? "rungent-attachment rungent-thumb" : "rungent-attachment rungent-chip",
|
|
2206
|
+
"data-status": item.status,
|
|
2207
|
+
"aria-busy": item.status === "uploading" || void 0,
|
|
2208
|
+
children: [
|
|
2209
|
+
item.status === "uploading" ? /* @__PURE__ */ jsx("span", {
|
|
2210
|
+
className: "rungent-spinner",
|
|
2211
|
+
role: "img",
|
|
2212
|
+
"aria-label": t("uploading", { file: item.name })
|
|
2213
|
+
}) : item.image && item.file ? /* @__PURE__ */ jsx("img", {
|
|
2214
|
+
className: "rungent-thumb-img",
|
|
2215
|
+
src: item.file.url,
|
|
2216
|
+
alt: item.name
|
|
2217
|
+
}) : /* @__PURE__ */ jsx(FileIcon, {}),
|
|
2218
|
+
item.image ? null : /* @__PURE__ */ jsx("span", {
|
|
2219
|
+
className: "rungent-attachment-name",
|
|
2220
|
+
children: item.name
|
|
2221
|
+
}),
|
|
2222
|
+
/* @__PURE__ */ jsx("button", {
|
|
2223
|
+
type: "button",
|
|
2224
|
+
className: "rungent-attachment-remove",
|
|
2225
|
+
"aria-label": t("removeAttachment", { file: item.name }),
|
|
2226
|
+
onClick: () => remove(item.key),
|
|
2227
|
+
children: "×"
|
|
2228
|
+
})
|
|
2229
|
+
]
|
|
2230
|
+
}, item.key))
|
|
2231
|
+
}), pending.filter((item) => item.status === "failed").map((item) => /* @__PURE__ */ jsx("p", {
|
|
2232
|
+
className: "rungent-error rungent-small rungent-upload-error",
|
|
2233
|
+
role: "alert",
|
|
2234
|
+
children: t("uploadFailed", { file: item.name })
|
|
2235
|
+
}, item.key))] });
|
|
2236
|
+
}
|
|
2237
|
+
function ThinkIcon() {
|
|
2238
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
2239
|
+
viewBox: "0 0 24 24",
|
|
2240
|
+
width: "16",
|
|
2241
|
+
height: "16",
|
|
2242
|
+
"aria-hidden": "true",
|
|
2243
|
+
className: "rungent-icon",
|
|
2244
|
+
children: [
|
|
2245
|
+
/* @__PURE__ */ jsx("path", { d: "M9 18h6" }),
|
|
2246
|
+
/* @__PURE__ */ jsx("path", { d: "M10 22h4" }),
|
|
2247
|
+
/* @__PURE__ */ jsx("path", { d: "M15.1 14c.2-1 .7-1.7 1.4-2.5A5.5 5.5 0 0 0 12 2.5a5.5 5.5 0 0 0-4.5 9c.7.8 1.2 1.5 1.4 2.5" })
|
|
2248
|
+
]
|
|
2249
|
+
});
|
|
2250
|
+
}
|
|
2251
|
+
/** The user's thinking switch: off by default, remembered per browser, sent with each run. */
|
|
2252
|
+
function ThinkToggle() {
|
|
2253
|
+
const { t, thinking } = useRungent();
|
|
2254
|
+
if (!thinking.available) return null;
|
|
2255
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
2256
|
+
type: "button",
|
|
2257
|
+
className: "rungent-think",
|
|
2258
|
+
"aria-pressed": thinking.on,
|
|
2259
|
+
title: t(thinking.on ? "thinkOnHint" : "thinkOffHint"),
|
|
2260
|
+
onClick: () => thinking.set(!thinking.on),
|
|
2261
|
+
children: [/* @__PURE__ */ jsx(ThinkIcon, {}), /* @__PURE__ */ jsx("span", { children: t("think") })]
|
|
2262
|
+
});
|
|
2263
|
+
}
|
|
2264
|
+
function SendIcon() {
|
|
2265
|
+
return /* @__PURE__ */ jsxs("svg", {
|
|
2266
|
+
viewBox: "0 0 24 24",
|
|
2267
|
+
width: "16",
|
|
2268
|
+
height: "16",
|
|
2269
|
+
"aria-hidden": "true",
|
|
2270
|
+
className: "rungent-icon",
|
|
2271
|
+
children: [/* @__PURE__ */ jsx("path", { d: "M12 19V5" }), /* @__PURE__ */ jsx("path", { d: "m5 12 7-7 7 7" })]
|
|
2272
|
+
});
|
|
2273
|
+
}
|
|
2274
|
+
function StopIcon() {
|
|
2275
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
2276
|
+
viewBox: "0 0 24 24",
|
|
2277
|
+
width: "14",
|
|
2278
|
+
height: "14",
|
|
2279
|
+
"aria-hidden": "true",
|
|
2280
|
+
children: /* @__PURE__ */ jsx("rect", {
|
|
2281
|
+
x: "5",
|
|
2282
|
+
y: "5",
|
|
2283
|
+
width: "14",
|
|
2284
|
+
height: "14",
|
|
2285
|
+
rx: "2",
|
|
2286
|
+
fill: "currentColor"
|
|
2287
|
+
})
|
|
2288
|
+
});
|
|
2289
|
+
}
|
|
2290
|
+
/**
|
|
2291
|
+
* The message box: one card with the text, which grows with it and then scrolls, and a row
|
|
2292
|
+
* under it with attachments, the thinking switch and the context ring on the left and send or
|
|
2293
|
+
* stop on the right.
|
|
2294
|
+
*/
|
|
2295
|
+
function Composer({ thread, attachments }) {
|
|
2296
|
+
const { t, upload } = useRungent();
|
|
2297
|
+
const [text, setText] = useState("");
|
|
2298
|
+
const input = useRef(null);
|
|
2299
|
+
const running = thread.state.status === "running";
|
|
2300
|
+
const ready = attachments.pending.flatMap((item) => item.status === "done" && item.file ? [item.file] : []);
|
|
2301
|
+
const uploading = attachments.pending.some((item) => item.status === "uploading");
|
|
2302
|
+
const canSend = !running && !uploading && (text.trim() !== "" || ready.length > 0);
|
|
2303
|
+
useAutosize(input, text, useCallback(() => input.current?.closest(".rungent-thread")?.clientHeight || window.innerHeight, []));
|
|
2304
|
+
useScrollbar(input);
|
|
2305
|
+
const wasRunning = useRef(running);
|
|
2306
|
+
useEffect(() => {
|
|
2307
|
+
if (wasRunning.current && !running) {
|
|
2308
|
+
const active = document.activeElement;
|
|
2309
|
+
const thread = input.current?.closest(".rungent-thread");
|
|
2310
|
+
if (!active || active === document.body || thread?.contains(active)) input.current?.focus();
|
|
2311
|
+
}
|
|
2312
|
+
wasRunning.current = running;
|
|
2313
|
+
}, [running]);
|
|
2314
|
+
const send = () => {
|
|
2315
|
+
if (!canSend) return;
|
|
2316
|
+
const message = text.trim();
|
|
2317
|
+
setText("");
|
|
2318
|
+
attachments.clear();
|
|
2319
|
+
thread.send(ready.length === 0 ? message : [...message ? [{
|
|
2320
|
+
type: "text",
|
|
2321
|
+
text: message
|
|
2322
|
+
}] : [], ...ready.map((file) => attachmentPart(file))]);
|
|
2323
|
+
};
|
|
2324
|
+
const onKeyDown = (event) => {
|
|
2325
|
+
const composing = event.nativeEvent.isComposing || event.keyCode === 229;
|
|
2326
|
+
if (event.key === "Enter" && !event.shiftKey && !composing) {
|
|
2327
|
+
event.preventDefault();
|
|
2328
|
+
send();
|
|
2329
|
+
}
|
|
2330
|
+
};
|
|
2331
|
+
const onPaste = (event) => {
|
|
2332
|
+
const files = [...event.clipboardData.files];
|
|
2333
|
+
if (!upload || files.length === 0) return;
|
|
2334
|
+
event.preventDefault();
|
|
2335
|
+
attachments.add(files);
|
|
2336
|
+
};
|
|
2337
|
+
const context = thread.state.context;
|
|
2338
|
+
const started = thread.state.items.length > 0 || running;
|
|
2339
|
+
return /* @__PURE__ */ jsx("form", {
|
|
2340
|
+
className: "rungent-composer",
|
|
2341
|
+
onSubmit: (event) => {
|
|
2342
|
+
event.preventDefault();
|
|
2343
|
+
send();
|
|
2344
|
+
},
|
|
2345
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
2346
|
+
className: "rungent-composer-box",
|
|
2347
|
+
children: [
|
|
2348
|
+
/* @__PURE__ */ jsx(PendingList, { attachments }),
|
|
2349
|
+
/* @__PURE__ */ jsx("textarea", {
|
|
2350
|
+
ref: input,
|
|
2351
|
+
className: `rungent-composer-input ${SCROLL_CLASS}`,
|
|
2352
|
+
rows: 1,
|
|
2353
|
+
"aria-label": t("composerLabel"),
|
|
2354
|
+
placeholder: t("composerPlaceholder"),
|
|
2355
|
+
value: text,
|
|
2356
|
+
onChange: (event) => setText(event.target.value),
|
|
2357
|
+
onKeyDown,
|
|
2358
|
+
onPaste,
|
|
2359
|
+
"data-rungent-autofocus": ""
|
|
2360
|
+
}),
|
|
2361
|
+
/* @__PURE__ */ jsxs("div", {
|
|
2362
|
+
className: "rungent-composer-bar",
|
|
2363
|
+
children: [
|
|
2364
|
+
upload ? /* @__PURE__ */ jsxs("label", {
|
|
2365
|
+
className: "rungent-icon-button rungent-attach",
|
|
2366
|
+
title: t("attach"),
|
|
2367
|
+
children: [
|
|
2368
|
+
/* @__PURE__ */ jsx("input", {
|
|
2369
|
+
type: "file",
|
|
2370
|
+
multiple: true,
|
|
2371
|
+
className: "rungent-visually-hidden",
|
|
2372
|
+
onChange: (event) => {
|
|
2373
|
+
attachments.add([...event.target.files ?? []]);
|
|
2374
|
+
event.target.value = "";
|
|
2375
|
+
}
|
|
2376
|
+
}),
|
|
2377
|
+
/* @__PURE__ */ jsx(PaperclipIcon, {}),
|
|
2378
|
+
/* @__PURE__ */ jsx("span", {
|
|
2379
|
+
className: "rungent-visually-hidden",
|
|
2380
|
+
children: t("attach")
|
|
2381
|
+
})
|
|
2382
|
+
]
|
|
2383
|
+
}) : null,
|
|
2384
|
+
/* @__PURE__ */ jsx(ThinkToggle, {}),
|
|
2385
|
+
context && started ? /* @__PURE__ */ jsx(ContextRing, { usage: context }) : null,
|
|
2386
|
+
/* @__PURE__ */ jsx("span", { className: "rungent-composer-spacer" }),
|
|
2387
|
+
running ? /* @__PURE__ */ jsx("button", {
|
|
2388
|
+
type: "button",
|
|
2389
|
+
className: "rungent-send",
|
|
2390
|
+
"data-stop": "",
|
|
2391
|
+
"aria-label": t("stop"),
|
|
2392
|
+
title: t("stop"),
|
|
2393
|
+
onClick: () => void thread.cancel(),
|
|
2394
|
+
children: /* @__PURE__ */ jsx(StopIcon, {})
|
|
2395
|
+
}) : /* @__PURE__ */ jsx("button", {
|
|
2396
|
+
type: "submit",
|
|
2397
|
+
className: "rungent-send",
|
|
2398
|
+
"aria-label": t("send"),
|
|
2399
|
+
title: t("send"),
|
|
2400
|
+
disabled: !canSend,
|
|
2401
|
+
children: /* @__PURE__ */ jsx(SendIcon, {})
|
|
2402
|
+
})
|
|
2403
|
+
]
|
|
2404
|
+
})
|
|
2405
|
+
]
|
|
2406
|
+
})
|
|
2407
|
+
});
|
|
2408
|
+
}
|
|
2409
|
+
function UserMessage({ item }) {
|
|
2410
|
+
const { t } = useRungent();
|
|
2411
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2412
|
+
className: "rungent-user-turn",
|
|
2413
|
+
children: [item.attachments.length > 0 ? /* @__PURE__ */ jsx("ul", {
|
|
2414
|
+
className: "rungent-attachments rungent-sent",
|
|
2415
|
+
"aria-label": t("attachments"),
|
|
2416
|
+
children: item.attachments.map((attachment, index) => {
|
|
2417
|
+
const name = attachment.name ?? t(attachment.kind === "image" ? "image" : "file");
|
|
2418
|
+
return /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx("a", {
|
|
2419
|
+
className: attachment.kind === "image" ? "rungent-attachment rungent-thumb" : "rungent-attachment rungent-chip",
|
|
2420
|
+
href: attachment.url,
|
|
2421
|
+
target: "_blank",
|
|
2422
|
+
rel: "noopener noreferrer",
|
|
2423
|
+
"aria-label": t("openAttachment", { file: name }),
|
|
2424
|
+
children: attachment.kind === "image" ? /* @__PURE__ */ jsx("img", {
|
|
2425
|
+
className: "rungent-thumb-img",
|
|
2426
|
+
src: attachment.url,
|
|
2427
|
+
alt: ""
|
|
2428
|
+
}) : /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(FileIcon, {}), /* @__PURE__ */ jsx("span", {
|
|
2429
|
+
className: "rungent-attachment-name",
|
|
2430
|
+
children: name
|
|
2431
|
+
})] })
|
|
2432
|
+
}) }, index);
|
|
2433
|
+
})
|
|
2434
|
+
}) : null, item.text ? /* @__PURE__ */ jsx("div", {
|
|
2435
|
+
className: "rungent-message rungent-user",
|
|
2436
|
+
children: item.text
|
|
2437
|
+
}) : null]
|
|
2438
|
+
});
|
|
2439
|
+
}
|
|
2440
|
+
function Rating({ threadId, messageId }) {
|
|
2441
|
+
const { client, t } = useRungent();
|
|
2442
|
+
const [rating, setRating] = useState(null);
|
|
2443
|
+
const [asking, setAsking] = useState(false);
|
|
2444
|
+
const [comment, setComment] = useState("");
|
|
2445
|
+
const [status, setStatus] = useState("idle");
|
|
2446
|
+
const send = (feedback) => client.setFeedback(threadId, messageId, feedback).then(() => setStatus("sent"), () => setStatus("failed"));
|
|
2447
|
+
const rate = (next) => {
|
|
2448
|
+
setRating(next);
|
|
2449
|
+
setAsking(next === "down");
|
|
2450
|
+
setComment("");
|
|
2451
|
+
setStatus("idle");
|
|
2452
|
+
send({ rating: next });
|
|
2453
|
+
};
|
|
2454
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2455
|
+
className: "rungent-feedback",
|
|
2456
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
2457
|
+
className: "rungent-feedback-buttons",
|
|
2458
|
+
role: "group",
|
|
2459
|
+
"aria-label": t("rateAnswer"),
|
|
2460
|
+
children: [
|
|
2461
|
+
/* @__PURE__ */ jsx("button", {
|
|
2462
|
+
type: "button",
|
|
2463
|
+
className: "rungent-icon-button rungent-feedback-button",
|
|
2464
|
+
"aria-pressed": rating === "up",
|
|
2465
|
+
"aria-label": t("goodAnswer"),
|
|
2466
|
+
title: t("goodAnswer"),
|
|
2467
|
+
onClick: () => rate("up"),
|
|
2468
|
+
children: /* @__PURE__ */ jsx(ThumbIcon, {})
|
|
2469
|
+
}),
|
|
2470
|
+
/* @__PURE__ */ jsx("button", {
|
|
2471
|
+
type: "button",
|
|
2472
|
+
className: "rungent-icon-button rungent-feedback-button",
|
|
2473
|
+
"aria-pressed": rating === "down",
|
|
2474
|
+
"aria-label": t("badAnswer"),
|
|
2475
|
+
title: t("badAnswer"),
|
|
2476
|
+
onClick: () => rate("down"),
|
|
2477
|
+
children: /* @__PURE__ */ jsx(ThumbIcon, { down: true })
|
|
2478
|
+
}),
|
|
2479
|
+
/* @__PURE__ */ jsx("span", {
|
|
2480
|
+
className: "rungent-muted rungent-small",
|
|
2481
|
+
role: "status",
|
|
2482
|
+
children: status === "failed" ? t("feedbackFailed") : status === "sent" && !asking ? t("feedbackThanks") : null
|
|
2483
|
+
})
|
|
2484
|
+
]
|
|
2485
|
+
}), asking ? /* @__PURE__ */ jsxs("form", {
|
|
2486
|
+
className: "rungent-feedback-form",
|
|
2487
|
+
onSubmit: (event) => {
|
|
2488
|
+
event.preventDefault();
|
|
2489
|
+
setAsking(false);
|
|
2490
|
+
const text = comment.trim();
|
|
2491
|
+
if (text) send({
|
|
2492
|
+
rating: "down",
|
|
2493
|
+
comment: text
|
|
2494
|
+
});
|
|
2495
|
+
},
|
|
2496
|
+
children: [
|
|
2497
|
+
/* @__PURE__ */ jsx("input", {
|
|
2498
|
+
className: "rungent-input",
|
|
2499
|
+
"aria-label": t("feedbackComment"),
|
|
2500
|
+
placeholder: t("feedbackComment"),
|
|
2501
|
+
value: comment,
|
|
2502
|
+
onChange: (event) => setComment(event.target.value)
|
|
2503
|
+
}),
|
|
2504
|
+
/* @__PURE__ */ jsx("button", {
|
|
2505
|
+
type: "button",
|
|
2506
|
+
className: "rungent-button rungent-ghost",
|
|
2507
|
+
onClick: () => setAsking(false),
|
|
2508
|
+
children: t("feedbackSkip")
|
|
2509
|
+
}),
|
|
2510
|
+
/* @__PURE__ */ jsx("button", {
|
|
2511
|
+
type: "submit",
|
|
2512
|
+
className: "rungent-button",
|
|
2513
|
+
disabled: !comment.trim(),
|
|
2514
|
+
children: t("feedbackSend")
|
|
2515
|
+
})
|
|
2516
|
+
]
|
|
2517
|
+
}) : null]
|
|
2518
|
+
});
|
|
2519
|
+
}
|
|
2520
|
+
/** The last answer of each finished turn, which the user can rate. */
|
|
2521
|
+
function rateable(items, running) {
|
|
2522
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2523
|
+
let last = null;
|
|
2524
|
+
for (const item of items) if (item.kind === "user") {
|
|
2525
|
+
if (last) ids.add(last);
|
|
2526
|
+
last = null;
|
|
2527
|
+
} else if (item.kind === "assistant" && !item.streaming && item.text.trim()) last = item.id;
|
|
2528
|
+
if (last && !running) ids.add(last);
|
|
2529
|
+
return ids;
|
|
2530
|
+
}
|
|
2531
|
+
function hasFiles(event) {
|
|
2532
|
+
return [...event.dataTransfer.types].includes("Files");
|
|
2533
|
+
}
|
|
2534
|
+
/** Within this many pixels of the end, the reader is "at the latest" and new content follows. */
|
|
2535
|
+
const PIN_SLACK = 48;
|
|
2536
|
+
/** Where the reader left each thread that is not at its end, so it shows there again. */
|
|
2537
|
+
const scrolled = /* @__PURE__ */ new Map();
|
|
2538
|
+
/**
|
|
2539
|
+
* Keeps a scroll container at its end while its content grows, unless the reader scrolled up to
|
|
2540
|
+
* read; `pin` brings it back to the end and keeps it there. A thread (`key`) shown again, say by
|
|
2541
|
+
* a dock mounted again, opens where the reader left it.
|
|
2542
|
+
*/
|
|
2543
|
+
function useStickToBottom(scroller, key) {
|
|
2544
|
+
const pinned = useRef(true);
|
|
2545
|
+
const [atEnd, setAtEnd] = useState(true);
|
|
2546
|
+
const keyRef = useRef(key);
|
|
2547
|
+
keyRef.current = key;
|
|
2548
|
+
const toEnd = useCallback(() => {
|
|
2549
|
+
const element = scroller.current;
|
|
2550
|
+
if (element) element.scrollTop = element.scrollHeight;
|
|
2551
|
+
}, [scroller]);
|
|
2552
|
+
useLayoutEffect(() => {
|
|
2553
|
+
const element = scroller.current;
|
|
2554
|
+
const top = key === null ? void 0 : scrolled.get(key);
|
|
2555
|
+
if (!element || top === void 0) return;
|
|
2556
|
+
element.scrollTop = top;
|
|
2557
|
+
pinned.current = false;
|
|
2558
|
+
setAtEnd(false);
|
|
2559
|
+
}, [key]);
|
|
2560
|
+
useEffect(() => {
|
|
2561
|
+
const element = scroller.current;
|
|
2562
|
+
if (!element) return;
|
|
2563
|
+
const onScroll = () => {
|
|
2564
|
+
const near = element.scrollHeight - element.scrollTop - element.clientHeight <= PIN_SLACK;
|
|
2565
|
+
pinned.current = near;
|
|
2566
|
+
setAtEnd(near);
|
|
2567
|
+
const key = keyRef.current;
|
|
2568
|
+
if (key !== null) {
|
|
2569
|
+
if (near) scrolled.delete(key);
|
|
2570
|
+
else scrolled.set(key, element.scrollTop);
|
|
2571
|
+
}
|
|
2572
|
+
};
|
|
2573
|
+
element.addEventListener("scroll", onScroll, { passive: true });
|
|
2574
|
+
const observer = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(() => {
|
|
2575
|
+
if (pinned.current) toEnd();
|
|
2576
|
+
});
|
|
2577
|
+
observer?.observe(element);
|
|
2578
|
+
for (const child of element.children) observer?.observe(child);
|
|
2579
|
+
const children = new MutationObserver(() => {
|
|
2580
|
+
observer?.disconnect();
|
|
2581
|
+
observer?.observe(element);
|
|
2582
|
+
for (const child of element.children) observer?.observe(child);
|
|
2583
|
+
if (pinned.current) toEnd();
|
|
2584
|
+
});
|
|
2585
|
+
children.observe(element, {
|
|
2586
|
+
childList: true,
|
|
2587
|
+
subtree: true,
|
|
2588
|
+
characterData: true
|
|
2589
|
+
});
|
|
2590
|
+
return () => {
|
|
2591
|
+
element.removeEventListener("scroll", onScroll);
|
|
2592
|
+
observer?.disconnect();
|
|
2593
|
+
children.disconnect();
|
|
2594
|
+
};
|
|
2595
|
+
}, [scroller, toEnd]);
|
|
2596
|
+
return {
|
|
2597
|
+
atEnd,
|
|
2598
|
+
pin: useCallback(() => {
|
|
2599
|
+
pinned.current = true;
|
|
2600
|
+
setAtEnd(true);
|
|
2601
|
+
toEnd();
|
|
2602
|
+
}, [toEnd])
|
|
2603
|
+
};
|
|
2604
|
+
}
|
|
2605
|
+
/** An empty conversation: what rungent can do, and questions to start from. */
|
|
2606
|
+
function Welcome({ ask }) {
|
|
2607
|
+
const { t, suggestions } = useRungent();
|
|
2608
|
+
const questions = suggestions?.() ?? [];
|
|
2609
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2610
|
+
className: "rungent-welcome",
|
|
2611
|
+
children: [
|
|
2612
|
+
/* @__PURE__ */ jsx("span", {
|
|
2613
|
+
className: "rungent-welcome-mark",
|
|
2614
|
+
"aria-hidden": "true",
|
|
2615
|
+
children: "✦"
|
|
2616
|
+
}),
|
|
2617
|
+
/* @__PURE__ */ jsx("h3", {
|
|
2618
|
+
className: "rungent-welcome-title",
|
|
2619
|
+
children: t("welcomeTitle")
|
|
2620
|
+
}),
|
|
2621
|
+
/* @__PURE__ */ jsx("p", {
|
|
2622
|
+
className: "rungent-muted",
|
|
2623
|
+
children: t("welcome")
|
|
2624
|
+
}),
|
|
2625
|
+
questions.length > 0 ? /* @__PURE__ */ jsx("ul", {
|
|
2626
|
+
className: "rungent-suggestions",
|
|
2627
|
+
"aria-label": t("suggestions"),
|
|
2628
|
+
children: questions.map((question) => /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx("button", {
|
|
2629
|
+
type: "button",
|
|
2630
|
+
className: "rungent-suggestion",
|
|
2631
|
+
onClick: () => ask(question),
|
|
2632
|
+
children: question
|
|
2633
|
+
}) }, question))
|
|
2634
|
+
}) : null
|
|
2635
|
+
]
|
|
2636
|
+
});
|
|
2637
|
+
}
|
|
2638
|
+
const NO_CITATIONS = [];
|
|
2639
|
+
/** An answer that started this long before it mounted is replayed history. */
|
|
2640
|
+
const HISTORY_MS = 5e3;
|
|
2641
|
+
/** An answer: text revealed at a steady pace while it streams, with a caret at its end. */
|
|
2642
|
+
function AssistantMessage({ item, citations, children }) {
|
|
2643
|
+
const [history] = useState(() => item.startedAt !== null && item.startedAt < Date.now() - HISTORY_MS);
|
|
2644
|
+
const smooth = useSmoothText(item.text, item.streaming && !history);
|
|
2645
|
+
const shown = history ? item.text : smooth;
|
|
2646
|
+
const typing = item.streaming || shown.length < item.text.length;
|
|
2647
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2648
|
+
className: "rungent-message rungent-assistant",
|
|
2649
|
+
"aria-busy": typing || void 0,
|
|
2650
|
+
children: [/* @__PURE__ */ jsx(Markdown, {
|
|
2651
|
+
text: typing ? settlePartialMarkdown(shown) : item.text,
|
|
2652
|
+
citations: citations ?? NO_CITATIONS,
|
|
2653
|
+
streaming: typing
|
|
2654
|
+
}), typing ? null : children]
|
|
2655
|
+
});
|
|
2656
|
+
}
|
|
2657
|
+
/** One conversation: messages, tool activity, open requests and the composer. */
|
|
2658
|
+
function RungentThread({ threadId, onThreadCreated, initialMessage }) {
|
|
2659
|
+
const { t, upload } = useRungent();
|
|
2660
|
+
const root = useRoot("rungent-thread");
|
|
2661
|
+
const thread = useThread(threadId);
|
|
2662
|
+
const { state } = thread;
|
|
2663
|
+
const attachments = useAttachments();
|
|
2664
|
+
const [dragging, setDragging] = useState(false);
|
|
2665
|
+
const scroller = useRef(null);
|
|
2666
|
+
const rootRef = useRef(null);
|
|
2667
|
+
const { atEnd, pin } = useStickToBottom(scroller, thread.threadId);
|
|
2668
|
+
useScrollbar(scroller);
|
|
2669
|
+
const focusComposer = () => rootRef.current?.querySelector("[data-rungent-autofocus]")?.focus();
|
|
2670
|
+
const sentInitial = useRef(false);
|
|
2671
|
+
useEffect(() => {
|
|
2672
|
+
if (initialMessage && !sentInitial.current) {
|
|
2673
|
+
sentInitial.current = true;
|
|
2674
|
+
thread.send(initialMessage);
|
|
2675
|
+
}
|
|
2676
|
+
}, [initialMessage, thread.send]);
|
|
2677
|
+
useEffect(() => {
|
|
2678
|
+
if (threadId === null && thread.threadId) onThreadCreated?.(thread.threadId);
|
|
2679
|
+
}, [
|
|
2680
|
+
thread.threadId,
|
|
2681
|
+
threadId,
|
|
2682
|
+
onThreadCreated
|
|
2683
|
+
]);
|
|
2684
|
+
const userTurns = state.items.filter((item) => item.kind === "user").length;
|
|
2685
|
+
const turnsSeen = useRef(userTurns);
|
|
2686
|
+
useEffect(() => {
|
|
2687
|
+
if (userTurns > turnsSeen.current) pin();
|
|
2688
|
+
turnsSeen.current = userTurns;
|
|
2689
|
+
}, [userTurns, pin]);
|
|
2690
|
+
const running = state.status === "running";
|
|
2691
|
+
const rated = rateable(state.items, running);
|
|
2692
|
+
const blocks = threadBlocks(state.items, running);
|
|
2693
|
+
const lastTrace = blocks.findLastIndex((block) => block.kind === "trace");
|
|
2694
|
+
const liveTrace = lastTrace > blocks.findLastIndex((block) => block.kind === "user") ? lastTrace : -1;
|
|
2695
|
+
const awaiting = (toolCallId) => {
|
|
2696
|
+
const open = state.interrupts.find((open) => open.interrupt.toolCallId === toolCallId);
|
|
2697
|
+
return open?.kind === "approval" || open?.kind === "form" || open?.kind === "input" ? open.kind : null;
|
|
2698
|
+
};
|
|
2699
|
+
const drop = upload ? {
|
|
2700
|
+
onDragOver: (event) => {
|
|
2701
|
+
if (!hasFiles(event)) return;
|
|
2702
|
+
event.preventDefault();
|
|
2703
|
+
setDragging(true);
|
|
2704
|
+
},
|
|
2705
|
+
onDragLeave: (event) => {
|
|
2706
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setDragging(false);
|
|
2707
|
+
},
|
|
2708
|
+
onDrop: (event) => {
|
|
2709
|
+
if (!hasFiles(event)) return;
|
|
2710
|
+
event.preventDefault();
|
|
2711
|
+
setDragging(false);
|
|
2712
|
+
attachments.add([...event.dataTransfer.files]);
|
|
2713
|
+
}
|
|
2714
|
+
} : {};
|
|
2715
|
+
const empty = state.items.length === 0 && !running;
|
|
2716
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
2717
|
+
...root,
|
|
2718
|
+
ref: rootRef,
|
|
2719
|
+
"data-dragging": dragging || void 0,
|
|
2720
|
+
...drop,
|
|
2721
|
+
children: [
|
|
2722
|
+
dragging ? /* @__PURE__ */ jsx("div", {
|
|
2723
|
+
className: "rungent-drop",
|
|
2724
|
+
"aria-hidden": "true",
|
|
2725
|
+
children: t("dropFiles")
|
|
2726
|
+
}) : null,
|
|
2727
|
+
/* @__PURE__ */ jsxs("div", {
|
|
2728
|
+
className: `rungent-messages ${SCROLL_CLASS}`,
|
|
2729
|
+
"data-empty": empty || void 0,
|
|
2730
|
+
ref: scroller,
|
|
2731
|
+
role: "log",
|
|
2732
|
+
"aria-live": "polite",
|
|
2733
|
+
children: [
|
|
2734
|
+
empty ? /* @__PURE__ */ jsx(Welcome, { ask: (question) => void thread.send(question) }) : null,
|
|
2735
|
+
blocks.map((block, index) => {
|
|
2736
|
+
if (block.kind === "user") return /* @__PURE__ */ jsx(UserMessage, { item: block.item }, block.key);
|
|
2737
|
+
if (block.kind === "trace") {
|
|
2738
|
+
const live = running && index === liveTrace;
|
|
2739
|
+
const next = blocks[index + 1];
|
|
2740
|
+
const answer = next?.kind === "answer" ? next.item : null;
|
|
2741
|
+
return /* @__PURE__ */ jsx(Trace, {
|
|
2742
|
+
traceKey: block.key,
|
|
2743
|
+
steps: block.steps,
|
|
2744
|
+
live,
|
|
2745
|
+
writing: live && answer?.streaming === true,
|
|
2746
|
+
endAt: answer?.endedAt ?? null,
|
|
2747
|
+
retry: state.retry,
|
|
2748
|
+
awaiting
|
|
2749
|
+
}, block.key);
|
|
2750
|
+
}
|
|
2751
|
+
const { item } = block;
|
|
2752
|
+
return /* @__PURE__ */ jsx(AssistantMessage, {
|
|
2753
|
+
item,
|
|
2754
|
+
citations: state.citations[item.id],
|
|
2755
|
+
children: thread.threadId && rated.has(item.id) ? /* @__PURE__ */ jsx(Rating, {
|
|
2756
|
+
threadId: thread.threadId,
|
|
2757
|
+
messageId: item.id
|
|
2758
|
+
}) : null
|
|
2759
|
+
}, item.id);
|
|
2760
|
+
}),
|
|
2761
|
+
state.interrupts.map((open) => /* @__PURE__ */ jsx(Interrupt, {
|
|
2762
|
+
open,
|
|
2763
|
+
thread,
|
|
2764
|
+
focusComposer
|
|
2765
|
+
}, open.id)),
|
|
2766
|
+
state.status === "cancelled" ? /* @__PURE__ */ jsx("p", {
|
|
2767
|
+
className: "rungent-note",
|
|
2768
|
+
children: t("cancelled")
|
|
2769
|
+
}) : null,
|
|
2770
|
+
state.status === "failed" && state.error ? /* @__PURE__ */ jsx("p", {
|
|
2771
|
+
className: "rungent-error",
|
|
2772
|
+
role: "alert",
|
|
2773
|
+
children: t(errorKey(state.error.code))
|
|
2774
|
+
}) : null,
|
|
2775
|
+
thread.error ? /* @__PURE__ */ jsx("p", {
|
|
2776
|
+
className: "rungent-error",
|
|
2777
|
+
role: "alert",
|
|
2778
|
+
children: t("error.request")
|
|
2779
|
+
}) : null
|
|
2780
|
+
]
|
|
2781
|
+
}),
|
|
2782
|
+
/* @__PURE__ */ jsxs("div", {
|
|
2783
|
+
className: "rungent-composer-wrap",
|
|
2784
|
+
children: [atEnd ? null : /* @__PURE__ */ jsx("button", {
|
|
2785
|
+
type: "button",
|
|
2786
|
+
className: "rungent-jump",
|
|
2787
|
+
onClick: pin,
|
|
2788
|
+
children: t("jumpToLatest")
|
|
2789
|
+
}), /* @__PURE__ */ jsx(Composer, {
|
|
2790
|
+
thread,
|
|
2791
|
+
attachments
|
|
2792
|
+
})]
|
|
2793
|
+
})
|
|
2794
|
+
]
|
|
2795
|
+
});
|
|
2796
|
+
}
|
|
2797
|
+
// src/dock.tsx
|
|
2798
|
+
function History({ current, onPick }) {
|
|
2799
|
+
const { t, locale } = useRungent();
|
|
2800
|
+
const { threads, loading, hasMore, loadMore, remove } = useThreads();
|
|
2801
|
+
const list = useRef(null);
|
|
2802
|
+
useScrollbar(list);
|
|
2803
|
+
return /* @__PURE__ */ jsxs("nav", {
|
|
2804
|
+
ref: list,
|
|
2805
|
+
className: `rungent-history ${SCROLL_CLASS}`,
|
|
2806
|
+
"aria-label": t("history"),
|
|
2807
|
+
children: [
|
|
2808
|
+
threads.length === 0 && !loading ? /* @__PURE__ */ jsx("p", {
|
|
2809
|
+
className: "rungent-muted",
|
|
2810
|
+
children: t("noThreads")
|
|
2811
|
+
}) : null,
|
|
2812
|
+
/* @__PURE__ */ jsx("ul", {
|
|
2813
|
+
className: "rungent-history-list",
|
|
2814
|
+
children: threads.map((thread) => /* @__PURE__ */ jsxs("li", {
|
|
2815
|
+
className: "rungent-history-row",
|
|
2816
|
+
children: [/* @__PURE__ */ jsxs("button", {
|
|
2817
|
+
type: "button",
|
|
2818
|
+
className: "rungent-history-item",
|
|
2819
|
+
"aria-current": thread.id === current || void 0,
|
|
2820
|
+
onClick: () => onPick(thread.id),
|
|
2821
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
2822
|
+
className: "rungent-history-title",
|
|
2823
|
+
children: thread.title ?? t("untitled")
|
|
2824
|
+
}), /* @__PURE__ */ jsx("time", {
|
|
2825
|
+
className: "rungent-muted rungent-small",
|
|
2826
|
+
dateTime: thread.createdAt,
|
|
2827
|
+
children: new Date(thread.createdAt).toLocaleDateString(locale, {
|
|
2828
|
+
month: "short",
|
|
2829
|
+
day: "numeric"
|
|
2830
|
+
})
|
|
2831
|
+
})]
|
|
2832
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
2833
|
+
type: "button",
|
|
2834
|
+
className: "rungent-icon-button",
|
|
2835
|
+
"aria-label": `${t("deleteThread")}: ${thread.title ?? t("untitled")}`,
|
|
2836
|
+
onClick: () => void remove(thread.id),
|
|
2837
|
+
children: "×"
|
|
2838
|
+
})]
|
|
2839
|
+
}, thread.id))
|
|
2840
|
+
}),
|
|
2841
|
+
loading ? /* @__PURE__ */ jsx("p", {
|
|
2842
|
+
className: "rungent-muted",
|
|
2843
|
+
children: t("loading")
|
|
2844
|
+
}) : null,
|
|
2845
|
+
hasMore && !loading ? /* @__PURE__ */ jsx("button", {
|
|
2846
|
+
type: "button",
|
|
2847
|
+
className: "rungent-button",
|
|
2848
|
+
onClick: loadMore,
|
|
2849
|
+
children: t("loadMore")
|
|
2850
|
+
}) : null
|
|
2851
|
+
]
|
|
2852
|
+
});
|
|
2853
|
+
}
|
|
2854
|
+
/** The page keeps at least this much room beside the panel. */
|
|
2855
|
+
const PAGE_MIN = 320;
|
|
2856
|
+
/** Arrow keys move the panel's edge by this much. */
|
|
2857
|
+
const KEY_STEP = 16;
|
|
2858
|
+
function clampWidth(width, room) {
|
|
2859
|
+
const max = Math.max(DOCK_WIDTH.min, Math.min(DOCK_WIDTH.max, room - PAGE_MIN));
|
|
2860
|
+
return Math.round(Math.min(Math.max(width, DOCK_WIDTH.min), max));
|
|
2861
|
+
}
|
|
2862
|
+
function Icon({ path }) {
|
|
2863
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
2864
|
+
viewBox: "0 0 24 24",
|
|
2865
|
+
width: "16",
|
|
2866
|
+
height: "16",
|
|
2867
|
+
"aria-hidden": "true",
|
|
2868
|
+
className: "rungent-icon",
|
|
2869
|
+
children: /* @__PURE__ */ jsx("path", { d: path })
|
|
2870
|
+
});
|
|
2871
|
+
}
|
|
2872
|
+
const ICONS = {
|
|
2873
|
+
history: "M3 12a9 9 0 1 0 3-6.7L3 8M3 3v5h5M12 7v5l3 2",
|
|
2874
|
+
newChat: "M12 5v14M5 12h14",
|
|
2875
|
+
expand: "M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7",
|
|
2876
|
+
collapse: "M4 14h6v6M20 10h-6V4M14 10l7-7M3 21l7-7",
|
|
2877
|
+
close: "M18 6 6 18M6 6l12 12"
|
|
2878
|
+
};
|
|
2879
|
+
/** The panel's left edge: drag, arrow keys, Home/End, or double-click to reset. */
|
|
2880
|
+
function ResizeHandle({ width, room, onResize, onCommit }) {
|
|
2881
|
+
const { t } = useRungent();
|
|
2882
|
+
const drag = useRef(null);
|
|
2883
|
+
const onPointerDown = (event) => {
|
|
2884
|
+
if (event.button !== 0) return;
|
|
2885
|
+
event.preventDefault();
|
|
2886
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
2887
|
+
drag.current = {
|
|
2888
|
+
x: event.clientX,
|
|
2889
|
+
width
|
|
2890
|
+
};
|
|
2891
|
+
};
|
|
2892
|
+
const onPointerMove = (event) => {
|
|
2893
|
+
if (!drag.current) return;
|
|
2894
|
+
onResize(clampWidth(drag.current.width + drag.current.x - event.clientX, room()));
|
|
2895
|
+
};
|
|
2896
|
+
const onPointerUp = (event) => {
|
|
2897
|
+
if (!drag.current) return;
|
|
2898
|
+
drag.current = null;
|
|
2899
|
+
onCommit(clampWidth(width, room()));
|
|
2900
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
2901
|
+
};
|
|
2902
|
+
const onKeyDown = (event) => {
|
|
2903
|
+
const next = event.key === "ArrowLeft" ? width + KEY_STEP : event.key === "ArrowRight" ? width - KEY_STEP : event.key === "Home" ? DOCK_WIDTH.max : event.key === "End" ? DOCK_WIDTH.min : null;
|
|
2904
|
+
if (next === null) return;
|
|
2905
|
+
event.preventDefault();
|
|
2906
|
+
onCommit(clampWidth(next, room()));
|
|
2907
|
+
};
|
|
2908
|
+
return /* @__PURE__ */ jsx("div", {
|
|
2909
|
+
className: "rungent-resize",
|
|
2910
|
+
role: "separator",
|
|
2911
|
+
"aria-orientation": "vertical",
|
|
2912
|
+
"aria-label": t("resize"),
|
|
2913
|
+
"aria-valuemin": DOCK_WIDTH.min,
|
|
2914
|
+
"aria-valuemax": DOCK_WIDTH.max,
|
|
2915
|
+
"aria-valuenow": width,
|
|
2916
|
+
tabIndex: 0,
|
|
2917
|
+
onPointerDown,
|
|
2918
|
+
onPointerMove,
|
|
2919
|
+
onPointerUp,
|
|
2920
|
+
onPointerCancel: onPointerUp,
|
|
2921
|
+
onKeyDown,
|
|
2922
|
+
onDoubleClick: () => onCommit(clampWidth(DOCK_WIDTH.initial, room()))
|
|
2923
|
+
});
|
|
2924
|
+
}
|
|
2925
|
+
/**
|
|
2926
|
+
* The assistant beside the product's page. Open, the panel takes its room from the page instead
|
|
2927
|
+
* of covering it, so what rungent opens or changes stays in view; its width can be dragged and is
|
|
2928
|
+
* remembered. Expanded, the conversation fills the area with the history beside it. On narrow
|
|
2929
|
+
* screens it fills the screen. ⌘J / Ctrl+J toggles it, Escape closes it (or leaves expanded).
|
|
2930
|
+
*
|
|
2931
|
+
* Mount it once, with `RungentProvider`, in the app's root layout: outside per-route layouts
|
|
2932
|
+
* (Next.js `app/layout.tsx`, a React Router root route), so that navigating never mounts it
|
|
2933
|
+
* again. Whether it is open, its width, thread and view live in a store that outlives the dock
|
|
2934
|
+
* (and `localStorage`), and the first paint shows them, so even a dock mounted again stays put:
|
|
2935
|
+
* it animates only when the user opens it.
|
|
2936
|
+
*/
|
|
2937
|
+
function RungentDock({ children, open: openProp, onOpenChange, threadId: threadProp, onThreadChange, onLayoutChange, launcher = true }) {
|
|
2938
|
+
const { t, dock, dockKey, ask, askTaken } = useRungent();
|
|
2939
|
+
const root = useRoot("rungent-dock");
|
|
2940
|
+
const launcherRoot = useRoot("rungent-launcher-root");
|
|
2941
|
+
const [dockOpen, setDockOpen] = useControlledOpen(dock, openProp, onOpenChange);
|
|
2942
|
+
const [saved, write] = useDockState(dockKey);
|
|
2943
|
+
const threadId = threadProp === void 0 ? saved.threadId : threadProp;
|
|
2944
|
+
const onThread = useRef(onThreadChange);
|
|
2945
|
+
onThread.current = onThreadChange;
|
|
2946
|
+
const setThreadId = useCallback((id) => {
|
|
2947
|
+
if (threadProp === void 0) write({ threadId: id });
|
|
2948
|
+
onThread.current?.(id);
|
|
2949
|
+
}, [threadProp, write]);
|
|
2950
|
+
const { view, expanded } = saved;
|
|
2951
|
+
const setView = (next) => write({ view: next });
|
|
2952
|
+
const setExpanded = (next) => write({ expanded: next });
|
|
2953
|
+
const [session, setSession] = useState(0);
|
|
2954
|
+
const [pendingAsk, setPendingAsk] = useState(null);
|
|
2955
|
+
const shell = useRef(null);
|
|
2956
|
+
const panel = useRef(null);
|
|
2957
|
+
const returnFocus = useRef(null);
|
|
2958
|
+
const titleId = useId();
|
|
2959
|
+
const wraps = children !== void 0;
|
|
2960
|
+
const [shellWidth, setShellWidth] = useState(() => typeof window === "undefined" ? DOCK_WIDTH.initial + PAGE_MIN : window.innerWidth);
|
|
2961
|
+
const room = useCallback(() => (wraps ? shell.current?.clientWidth : 0) || window.innerWidth, [wraps]);
|
|
2962
|
+
useLayoutEffect(() => {
|
|
2963
|
+
setShellWidth(room());
|
|
2964
|
+
const element = shell.current;
|
|
2965
|
+
if (!element || typeof ResizeObserver === "undefined") return;
|
|
2966
|
+
const observer = new ResizeObserver(() => setShellWidth(room()));
|
|
2967
|
+
observer.observe(element);
|
|
2968
|
+
return () => observer.disconnect();
|
|
2969
|
+
}, [room]);
|
|
2970
|
+
const [dragWidth, setDragWidth] = useState(null);
|
|
2971
|
+
const width = clampWidth(dragWidth ?? saved.width, shellWidth);
|
|
2972
|
+
const commitWidth = (next) => {
|
|
2973
|
+
setDragWidth(null);
|
|
2974
|
+
write({ width: next });
|
|
2975
|
+
};
|
|
2976
|
+
const layout = !dockOpen ? "closed" : expanded ? "expanded" : "panel";
|
|
2977
|
+
useLayoutEffect(() => {
|
|
2978
|
+
if (wraps || layout !== "panel" || window.innerWidth <= 640) return;
|
|
2979
|
+
const root = document.documentElement;
|
|
2980
|
+
const previous = root.style.paddingRight;
|
|
2981
|
+
root.style.paddingRight = `${width}px`;
|
|
2982
|
+
return () => {
|
|
2983
|
+
root.style.paddingRight = previous;
|
|
2984
|
+
};
|
|
2985
|
+
}, [
|
|
2986
|
+
wraps,
|
|
2987
|
+
layout,
|
|
2988
|
+
width
|
|
2989
|
+
]);
|
|
2990
|
+
const onLayout = useRef(onLayoutChange);
|
|
2991
|
+
onLayout.current = onLayoutChange;
|
|
2992
|
+
useEffect(() => onLayout.current?.(layout), [layout]);
|
|
2993
|
+
const openRef = useRef(dockOpen);
|
|
2994
|
+
openRef.current = dockOpen;
|
|
2995
|
+
useEffect(() => {
|
|
2996
|
+
const onKey = (event) => {
|
|
2997
|
+
if (event.key.toLowerCase() === "j" && (event.metaKey || event.ctrlKey) && !event.altKey) {
|
|
2998
|
+
event.preventDefault();
|
|
2999
|
+
setDockOpen(!openRef.current);
|
|
3000
|
+
}
|
|
3001
|
+
};
|
|
3002
|
+
window.addEventListener("keydown", onKey);
|
|
3003
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
3004
|
+
}, [setDockOpen]);
|
|
3005
|
+
useEffect(() => {
|
|
3006
|
+
if (!ask || ask.id <= askTaken.current) return;
|
|
3007
|
+
askTaken.current = ask.id;
|
|
3008
|
+
setPendingAsk(ask.query);
|
|
3009
|
+
setThreadId(null);
|
|
3010
|
+
setView("thread");
|
|
3011
|
+
setSession((n) => n + 1);
|
|
3012
|
+
}, [ask]);
|
|
3013
|
+
const [entering, setEntering] = useState(false);
|
|
3014
|
+
const openings = useRef(saved.openings);
|
|
3015
|
+
const wasOpen = useRef(dockOpen);
|
|
3016
|
+
useLayoutEffect(() => {
|
|
3017
|
+
const explicit = saved.openings !== openings.current || openProp !== void 0;
|
|
3018
|
+
openings.current = saved.openings;
|
|
3019
|
+
if (dockOpen && !wasOpen.current && explicit) {
|
|
3020
|
+
setEntering(true);
|
|
3021
|
+
if (document.activeElement instanceof HTMLElement && !panel.current?.contains(document.activeElement)) returnFocus.current = document.activeElement;
|
|
3022
|
+
panel.current?.querySelector("[data-rungent-autofocus]")?.focus();
|
|
3023
|
+
} else if (!dockOpen && wasOpen.current) {
|
|
3024
|
+
setEntering(false);
|
|
3025
|
+
if (returnFocus.current) {
|
|
3026
|
+
returnFocus.current.focus();
|
|
3027
|
+
returnFocus.current = null;
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
3030
|
+
wasOpen.current = dockOpen;
|
|
3031
|
+
}, [
|
|
3032
|
+
dockOpen,
|
|
3033
|
+
saved.openings,
|
|
3034
|
+
openProp
|
|
3035
|
+
]);
|
|
3036
|
+
const newChat = () => {
|
|
3037
|
+
setPendingAsk(null);
|
|
3038
|
+
setThreadId(null);
|
|
3039
|
+
setView("thread");
|
|
3040
|
+
setSession((n) => n + 1);
|
|
3041
|
+
};
|
|
3042
|
+
const pick = (id) => {
|
|
3043
|
+
setPendingAsk(null);
|
|
3044
|
+
setThreadId(id);
|
|
3045
|
+
setView("thread");
|
|
3046
|
+
};
|
|
3047
|
+
const conversation = /* @__PURE__ */ jsx(RungentThread, {
|
|
3048
|
+
threadId,
|
|
3049
|
+
onThreadCreated: (id) => {
|
|
3050
|
+
setPendingAsk(null);
|
|
3051
|
+
setThreadId(id);
|
|
3052
|
+
},
|
|
3053
|
+
...pendingAsk ? { initialMessage: pendingAsk } : {}
|
|
3054
|
+
}, session);
|
|
3055
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
3056
|
+
ref: shell,
|
|
3057
|
+
className: "rungent-shell",
|
|
3058
|
+
"data-layout": layout,
|
|
3059
|
+
"data-fixed": wraps ? void 0 : "",
|
|
3060
|
+
children: [wraps ? /* @__PURE__ */ jsx("div", {
|
|
3061
|
+
className: "rungent-shell-page",
|
|
3062
|
+
inert: layout === "expanded",
|
|
3063
|
+
children
|
|
3064
|
+
}) : null, dockOpen ? /* @__PURE__ */ jsxs("aside", {
|
|
3065
|
+
...root,
|
|
3066
|
+
ref: panel,
|
|
3067
|
+
"aria-labelledby": titleId,
|
|
3068
|
+
"data-entering": entering || void 0,
|
|
3069
|
+
onAnimationEnd: (event) => {
|
|
3070
|
+
if (event.target === event.currentTarget) setEntering(false);
|
|
3071
|
+
},
|
|
3072
|
+
style: { "--rungent-dock-width": `${width}px` },
|
|
3073
|
+
onKeyDown: (event) => {
|
|
3074
|
+
if (event.key !== "Escape" || event.defaultPrevented) return;
|
|
3075
|
+
event.preventDefault();
|
|
3076
|
+
if (expanded) setExpanded(false);
|
|
3077
|
+
else setDockOpen(false);
|
|
3078
|
+
},
|
|
3079
|
+
children: [
|
|
3080
|
+
expanded ? null : /* @__PURE__ */ jsx(ResizeHandle, {
|
|
3081
|
+
width,
|
|
3082
|
+
room,
|
|
3083
|
+
onResize: setDragWidth,
|
|
3084
|
+
onCommit: commitWidth
|
|
3085
|
+
}),
|
|
3086
|
+
/* @__PURE__ */ jsxs("header", {
|
|
3087
|
+
className: "rungent-dock-header",
|
|
3088
|
+
children: [
|
|
3089
|
+
/* @__PURE__ */ jsx("h2", {
|
|
3090
|
+
id: titleId,
|
|
3091
|
+
className: "rungent-dock-title",
|
|
3092
|
+
children: t("assistant")
|
|
3093
|
+
}),
|
|
3094
|
+
expanded ? null : /* @__PURE__ */ jsx("button", {
|
|
3095
|
+
type: "button",
|
|
3096
|
+
className: "rungent-icon-button",
|
|
3097
|
+
"aria-label": t("history"),
|
|
3098
|
+
title: t("history"),
|
|
3099
|
+
"aria-pressed": view === "history",
|
|
3100
|
+
onClick: () => setView(view === "history" ? "thread" : "history"),
|
|
3101
|
+
children: /* @__PURE__ */ jsx(Icon, { path: ICONS.history })
|
|
3102
|
+
}),
|
|
3103
|
+
/* @__PURE__ */ jsx("button", {
|
|
3104
|
+
type: "button",
|
|
3105
|
+
className: "rungent-icon-button",
|
|
3106
|
+
"aria-label": t("newChat"),
|
|
3107
|
+
title: t("newChat"),
|
|
3108
|
+
onClick: newChat,
|
|
3109
|
+
children: /* @__PURE__ */ jsx(Icon, { path: ICONS.newChat })
|
|
3110
|
+
}),
|
|
3111
|
+
/* @__PURE__ */ jsx("button", {
|
|
3112
|
+
type: "button",
|
|
3113
|
+
className: "rungent-icon-button rungent-expand",
|
|
3114
|
+
"aria-label": expanded ? t("collapse") : t("expand"),
|
|
3115
|
+
title: expanded ? t("collapse") : t("expand"),
|
|
3116
|
+
"aria-pressed": expanded,
|
|
3117
|
+
onClick: () => setExpanded(!expanded),
|
|
3118
|
+
children: /* @__PURE__ */ jsx(Icon, { path: expanded ? ICONS.collapse : ICONS.expand })
|
|
3119
|
+
}),
|
|
3120
|
+
/* @__PURE__ */ jsx("button", {
|
|
3121
|
+
type: "button",
|
|
3122
|
+
className: "rungent-icon-button",
|
|
3123
|
+
"aria-label": t("close"),
|
|
3124
|
+
title: t("close"),
|
|
3125
|
+
onClick: () => setDockOpen(false),
|
|
3126
|
+
children: /* @__PURE__ */ jsx(Icon, { path: ICONS.close })
|
|
3127
|
+
})
|
|
3128
|
+
]
|
|
3129
|
+
}),
|
|
3130
|
+
/* @__PURE__ */ jsxs("div", {
|
|
3131
|
+
className: "rungent-dock-body",
|
|
3132
|
+
"data-expanded": expanded || void 0,
|
|
3133
|
+
children: [expanded || view === "history" ? /* @__PURE__ */ jsx(History, {
|
|
3134
|
+
current: threadId,
|
|
3135
|
+
onPick: pick
|
|
3136
|
+
}) : null, expanded || view === "thread" ? /* @__PURE__ */ jsx("div", {
|
|
3137
|
+
className: "rungent-dock-main",
|
|
3138
|
+
children: conversation
|
|
3139
|
+
}) : null]
|
|
3140
|
+
})
|
|
3141
|
+
]
|
|
3142
|
+
}) : launcher ? /* @__PURE__ */ jsx("div", {
|
|
3143
|
+
...launcherRoot,
|
|
3144
|
+
children: /* @__PURE__ */ jsx("button", {
|
|
3145
|
+
type: "button",
|
|
3146
|
+
className: "rungent-launcher",
|
|
3147
|
+
"aria-label": t("openAssistant"),
|
|
3148
|
+
"aria-keyshortcuts": "Meta+J Control+J",
|
|
3149
|
+
onClick: () => setDockOpen(true),
|
|
3150
|
+
children: /* @__PURE__ */ jsx("span", {
|
|
3151
|
+
"aria-hidden": "true",
|
|
3152
|
+
children: "✦"
|
|
3153
|
+
})
|
|
3154
|
+
})
|
|
3155
|
+
}) : null]
|
|
3156
|
+
});
|
|
3157
|
+
}
|
|
3158
|
+
// src/help.tsx
|
|
3159
|
+
/** Fetches and renders one Markdown help page. */
|
|
3160
|
+
function HelpViewer({ url }) {
|
|
3161
|
+
const { t } = useRungent();
|
|
3162
|
+
const root = useRoot("rungent-help");
|
|
3163
|
+
const [page, setPage] = useState();
|
|
3164
|
+
useEffect(() => {
|
|
3165
|
+
const controller = new AbortController();
|
|
3166
|
+
fetch(url, {
|
|
3167
|
+
signal: controller.signal,
|
|
3168
|
+
headers: { Accept: "text/markdown, text/plain" }
|
|
3169
|
+
}).then(async (response) => {
|
|
3170
|
+
if (!response.ok) throw new Error(String(response.status));
|
|
3171
|
+
setPage({
|
|
3172
|
+
url,
|
|
3173
|
+
text: await response.text()
|
|
3174
|
+
});
|
|
3175
|
+
}).catch(() => {
|
|
3176
|
+
if (!controller.signal.aborted) setPage({
|
|
3177
|
+
url,
|
|
3178
|
+
failed: true
|
|
3179
|
+
});
|
|
3180
|
+
});
|
|
3181
|
+
return () => controller.abort();
|
|
3182
|
+
}, [url]);
|
|
3183
|
+
const absolute = new URL(url, window.location.href).href;
|
|
3184
|
+
return /* @__PURE__ */ jsx("article", {
|
|
3185
|
+
...root,
|
|
3186
|
+
"aria-busy": page?.url !== url || void 0,
|
|
3187
|
+
children: page?.url !== url ? /* @__PURE__ */ jsx("p", {
|
|
3188
|
+
className: "rungent-muted",
|
|
3189
|
+
children: t("loading")
|
|
3190
|
+
}) : "failed" in page ? /* @__PURE__ */ jsx("p", {
|
|
3191
|
+
className: "rungent-error",
|
|
3192
|
+
role: "alert",
|
|
3193
|
+
children: t("loadFailed")
|
|
3194
|
+
}) : /* @__PURE__ */ jsx(Markdown, {
|
|
3195
|
+
text: page.text,
|
|
3196
|
+
baseUrl: absolute
|
|
3197
|
+
})
|
|
3198
|
+
});
|
|
3199
|
+
}
|
|
3200
|
+
// src/palette.tsx
|
|
3201
|
+
/** Renders an excerpt with its `«…»` matches highlighted. */
|
|
3202
|
+
function Excerpt({ text }) {
|
|
3203
|
+
const parts = text.split(/«([^»]*)»/).map((part, index) => index % 2 === 1 ? /* @__PURE__ */ jsx("mark", { children: part }, index) : part);
|
|
3204
|
+
return /* @__PURE__ */ jsx("span", {
|
|
3205
|
+
className: "rungent-hit-excerpt",
|
|
3206
|
+
children: parts
|
|
3207
|
+
});
|
|
3208
|
+
}
|
|
3209
|
+
/** `work_item`, `workItem` or `work-item` as `Work item`. */
|
|
3210
|
+
function humanize(type) {
|
|
3211
|
+
const words = type.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_\-\s]+/g, " ").trim().toLowerCase();
|
|
3212
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
3213
|
+
}
|
|
3214
|
+
function group(hits) {
|
|
3215
|
+
const groups = /* @__PURE__ */ new Map();
|
|
3216
|
+
for (const hit of hits) groups.set(hit.type, [...groups.get(hit.type) ?? [], hit]);
|
|
3217
|
+
return [...groups];
|
|
3218
|
+
}
|
|
3219
|
+
/**
|
|
3220
|
+
* ⌘K / Ctrl+K search over every source, grouped by type. Arrow keys move, Enter opens, and the
|
|
3221
|
+
* last row hands the query to the assistant.
|
|
3222
|
+
*/
|
|
3223
|
+
function SearchPalette({ open: openProp, onOpenChange, onAsk } = {}) {
|
|
3224
|
+
const { t, navigate, askAssistant, palette, paletteSeed } = useRungent();
|
|
3225
|
+
const root = useRoot("rungent-palette-root");
|
|
3226
|
+
const [open, setOpen] = useControlledOpen(palette, openProp, onOpenChange);
|
|
3227
|
+
const openRef = useRef(open);
|
|
3228
|
+
openRef.current = open;
|
|
3229
|
+
const [query, setQuery] = useState("");
|
|
3230
|
+
const [active, setActive] = useState(0);
|
|
3231
|
+
const { result, loading } = useSearch(open ? query : "");
|
|
3232
|
+
const input = useRef(null);
|
|
3233
|
+
const options = useRef(null);
|
|
3234
|
+
useScrollbar(options);
|
|
3235
|
+
const returnFocus = useRef(null);
|
|
3236
|
+
const listId = useId();
|
|
3237
|
+
useEffect(() => {
|
|
3238
|
+
const onKey = (event) => {
|
|
3239
|
+
if (event.key.toLowerCase() === "k" && (event.metaKey || event.ctrlKey) && !event.altKey) {
|
|
3240
|
+
event.preventDefault();
|
|
3241
|
+
setOpen(!openRef.current);
|
|
3242
|
+
}
|
|
3243
|
+
};
|
|
3244
|
+
window.addEventListener("keydown", onKey);
|
|
3245
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
3246
|
+
}, [setOpen]);
|
|
3247
|
+
useEffect(() => {
|
|
3248
|
+
if (open) {
|
|
3249
|
+
returnFocus.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
3250
|
+
setQuery(paletteSeed.current);
|
|
3251
|
+
paletteSeed.current = "";
|
|
3252
|
+
input.current?.focus();
|
|
3253
|
+
} else {
|
|
3254
|
+
setQuery("");
|
|
3255
|
+
returnFocus.current?.focus();
|
|
3256
|
+
returnFocus.current = null;
|
|
3257
|
+
}
|
|
3258
|
+
}, [open]);
|
|
3259
|
+
useEffect(() => setActive(0), [result]);
|
|
3260
|
+
if (!open) return null;
|
|
3261
|
+
const groups = group(result?.hits ?? []);
|
|
3262
|
+
const rows = [...groups.flatMap(([, hits]) => hits.map((hit) => ({
|
|
3263
|
+
kind: "hit",
|
|
3264
|
+
hit
|
|
3265
|
+
}))), ...query.trim() ? [{ kind: "ask" }] : []];
|
|
3266
|
+
const typeLabel = (type) => t.find(`type.${type}`) ?? humanize(type);
|
|
3267
|
+
const optionId = (index) => `${listId}-${index}`;
|
|
3268
|
+
const choose = (row) => {
|
|
3269
|
+
if (!row) return;
|
|
3270
|
+
setOpen(false);
|
|
3271
|
+
if (row.kind === "ask") (onAsk ?? askAssistant)(query.trim());
|
|
3272
|
+
else navigate(row.hit.url);
|
|
3273
|
+
};
|
|
3274
|
+
const onKeyDown = (event) => {
|
|
3275
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
3276
|
+
event.preventDefault();
|
|
3277
|
+
if (rows.length === 0) return;
|
|
3278
|
+
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
3279
|
+
setActive((current) => (current + step + rows.length) % rows.length);
|
|
3280
|
+
} else if (event.key === "Enter" && !event.nativeEvent.isComposing) {
|
|
3281
|
+
event.preventDefault();
|
|
3282
|
+
choose(rows[active]);
|
|
3283
|
+
} else if (event.key === "Escape") {
|
|
3284
|
+
event.preventDefault();
|
|
3285
|
+
setOpen(false);
|
|
3286
|
+
}
|
|
3287
|
+
};
|
|
3288
|
+
let index = -1;
|
|
3289
|
+
const option = (row, content) => {
|
|
3290
|
+
index += 1;
|
|
3291
|
+
const at = index;
|
|
3292
|
+
return /* @__PURE__ */ jsx("div", {
|
|
3293
|
+
id: optionId(at),
|
|
3294
|
+
role: "option",
|
|
3295
|
+
tabIndex: -1,
|
|
3296
|
+
"aria-selected": at === active,
|
|
3297
|
+
className: "rungent-option",
|
|
3298
|
+
onMouseMove: () => setActive(at),
|
|
3299
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
3300
|
+
onClick: () => choose(row),
|
|
3301
|
+
children: content
|
|
3302
|
+
}, row.kind === "ask" ? "ask" : `${row.hit.type}:${row.hit.id}`);
|
|
3303
|
+
};
|
|
3304
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
3305
|
+
...root,
|
|
3306
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
3307
|
+
className: "rungent-overlay",
|
|
3308
|
+
onMouseDown: () => setOpen(false)
|
|
3309
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
3310
|
+
className: "rungent-palette",
|
|
3311
|
+
role: "dialog",
|
|
3312
|
+
"aria-modal": "true",
|
|
3313
|
+
"aria-label": t("searchLabel"),
|
|
3314
|
+
children: [
|
|
3315
|
+
/* @__PURE__ */ jsx("input", {
|
|
3316
|
+
ref: input,
|
|
3317
|
+
className: "rungent-palette-input",
|
|
3318
|
+
role: "combobox",
|
|
3319
|
+
"aria-expanded": rows.length > 0,
|
|
3320
|
+
"aria-controls": listId,
|
|
3321
|
+
"aria-activedescendant": rows.length > 0 ? optionId(active) : void 0,
|
|
3322
|
+
"aria-autocomplete": "list",
|
|
3323
|
+
"aria-label": t("searchLabel"),
|
|
3324
|
+
placeholder: t("searchPlaceholder"),
|
|
3325
|
+
value: query,
|
|
3326
|
+
onChange: (event) => setQuery(event.target.value),
|
|
3327
|
+
onKeyDown
|
|
3328
|
+
}),
|
|
3329
|
+
/* @__PURE__ */ jsxs("div", {
|
|
3330
|
+
ref: options,
|
|
3331
|
+
id: listId,
|
|
3332
|
+
role: "listbox",
|
|
3333
|
+
className: `rungent-options ${SCROLL_CLASS}`,
|
|
3334
|
+
"aria-label": t("searchLabel"),
|
|
3335
|
+
children: [groups.map(([type, hits]) => /* @__PURE__ */ jsxs("div", {
|
|
3336
|
+
role: "group",
|
|
3337
|
+
"aria-label": typeLabel(type),
|
|
3338
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
3339
|
+
className: "rungent-group",
|
|
3340
|
+
"aria-hidden": "true",
|
|
3341
|
+
children: typeLabel(type)
|
|
3342
|
+
}), hits.map((hit) => option({
|
|
3343
|
+
kind: "hit",
|
|
3344
|
+
hit
|
|
3345
|
+
}, /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("span", {
|
|
3346
|
+
className: "rungent-hit-title",
|
|
3347
|
+
children: hit.title
|
|
3348
|
+
}), hit.excerpt ? /* @__PURE__ */ jsx(Excerpt, { text: hit.excerpt }) : null] })))]
|
|
3349
|
+
}, type)), query.trim() ? option({ kind: "ask" }, /* @__PURE__ */ jsxs("span", {
|
|
3350
|
+
className: "rungent-ask",
|
|
3351
|
+
children: [
|
|
3352
|
+
/* @__PURE__ */ jsx("span", {
|
|
3353
|
+
"aria-hidden": "true",
|
|
3354
|
+
children: "✦ "
|
|
3355
|
+
}),
|
|
3356
|
+
t("askAssistant"),
|
|
3357
|
+
": ",
|
|
3358
|
+
/* @__PURE__ */ jsx("q", { children: query.trim() })
|
|
3359
|
+
]
|
|
3360
|
+
})) : null]
|
|
3361
|
+
}),
|
|
3362
|
+
/* @__PURE__ */ jsxs("div", {
|
|
3363
|
+
className: "rungent-palette-status",
|
|
3364
|
+
role: "status",
|
|
3365
|
+
children: [
|
|
3366
|
+
loading ? t("searching") : null,
|
|
3367
|
+
!loading && result && result.hits.length === 0 ? t("noResults") : null,
|
|
3368
|
+
result && result.failed.length > 0 ? /* @__PURE__ */ jsxs("span", {
|
|
3369
|
+
className: "rungent-muted",
|
|
3370
|
+
children: [" ", t("failedSources", { sources: result.failed.map((failure) => failure.source).join(", ") })]
|
|
3371
|
+
}) : null
|
|
3372
|
+
]
|
|
3373
|
+
})
|
|
3374
|
+
]
|
|
3375
|
+
})]
|
|
3376
|
+
});
|
|
3377
|
+
}
|
|
3378
|
+
// src/settings.tsx
|
|
3379
|
+
const PROVIDERS = [
|
|
3380
|
+
"openai-compatible",
|
|
3381
|
+
"openai",
|
|
3382
|
+
"anthropic"
|
|
3383
|
+
];
|
|
3384
|
+
/** What each provider's thinking options usually are, shown as placeholders. */
|
|
3385
|
+
const THINKING_EXAMPLES = {
|
|
3386
|
+
"openai-compatible": {
|
|
3387
|
+
on: "{\"openai-compatible\": {\"enable_thinking\": true}}",
|
|
3388
|
+
off: "{\"openai-compatible\": {\"enable_thinking\": false}}"
|
|
3389
|
+
},
|
|
3390
|
+
openai: {
|
|
3391
|
+
on: "{\"openai\": {\"reasoningEffort\": \"medium\"}}",
|
|
3392
|
+
off: "{\"openai\": {\"reasoningEffort\": \"none\"}}"
|
|
3393
|
+
},
|
|
3394
|
+
anthropic: {
|
|
3395
|
+
on: "{\"anthropic\": {\"thinking\": {\"type\": \"enabled\", \"budgetTokens\": 4000}}}",
|
|
3396
|
+
off: "{\"anthropic\": {\"thinking\": {\"type\": \"disabled\"}}}"
|
|
3397
|
+
}
|
|
3398
|
+
};
|
|
3399
|
+
let nextKey = 0;
|
|
3400
|
+
const row = (endpoint) => ({
|
|
3401
|
+
key: nextKey++,
|
|
3402
|
+
endpoint,
|
|
3403
|
+
apiKey: "",
|
|
3404
|
+
thinkingOn: endpoint.thinking ? JSON.stringify(endpoint.thinking.on) : "",
|
|
3405
|
+
thinkingOff: endpoint.thinking ? JSON.stringify(endpoint.thinking.off) : ""
|
|
3406
|
+
});
|
|
3407
|
+
/** Provider options typed as JSON: an object of objects, or undefined when it is not one. */
|
|
3408
|
+
function providerOptions(text) {
|
|
3409
|
+
try {
|
|
3410
|
+
const value = JSON.parse(text);
|
|
3411
|
+
const isObject = (item) => typeof item === "object" && item !== null && !Array.isArray(item);
|
|
3412
|
+
return isObject(value) && Object.values(value).every(isObject) ? value : void 0;
|
|
3413
|
+
} catch {
|
|
3414
|
+
return;
|
|
3415
|
+
}
|
|
3416
|
+
}
|
|
3417
|
+
/** The row's thinking options; null when they are not both empty or both provider options. */
|
|
3418
|
+
function thinkingOf(item) {
|
|
3419
|
+
if (!item.thinkingOn.trim() && !item.thinkingOff.trim()) return void 0;
|
|
3420
|
+
const on = providerOptions(item.thinkingOn);
|
|
3421
|
+
const off = providerOptions(item.thinkingOff);
|
|
3422
|
+
return on && off ? {
|
|
3423
|
+
on,
|
|
3424
|
+
off
|
|
3425
|
+
} : null;
|
|
3426
|
+
}
|
|
3427
|
+
/** The endpoint as PUT takes it; models configured in code (provider `custom`) stay out. */
|
|
3428
|
+
function update(item) {
|
|
3429
|
+
const { hasApiKey: _, thinking: __, provider, ...fields } = item.endpoint;
|
|
3430
|
+
if (provider === "custom") return [];
|
|
3431
|
+
const thinking = thinkingOf(item);
|
|
3432
|
+
return [{
|
|
3433
|
+
...fields,
|
|
3434
|
+
provider,
|
|
3435
|
+
...item.apiKey ? { apiKey: item.apiKey } : {},
|
|
3436
|
+
...thinking ? { thinking } : {}
|
|
3437
|
+
}];
|
|
3438
|
+
}
|
|
3439
|
+
/** An id no other row has, `model-N`. */
|
|
3440
|
+
function freshId(rows) {
|
|
3441
|
+
const taken = new Set(rows.map((item) => item.endpoint.id));
|
|
3442
|
+
let n = rows.length + 1;
|
|
3443
|
+
while (taken.has(`model-${n}`)) n += 1;
|
|
3444
|
+
return `model-${n}`;
|
|
3445
|
+
}
|
|
3446
|
+
/** Sets or, when empty, removes an optional numeric field. */
|
|
3447
|
+
function numberField(endpoint, key, value) {
|
|
3448
|
+
const { [key]: _, ...rest } = endpoint;
|
|
3449
|
+
const number = Number(value);
|
|
3450
|
+
return value === "" || Number.isNaN(number) ? rest : {
|
|
3451
|
+
...rest,
|
|
3452
|
+
[key]: number
|
|
3453
|
+
};
|
|
3454
|
+
}
|
|
3455
|
+
/** The admin form for the model chain. Keys are write-only: it shows whether one is stored. */
|
|
3456
|
+
function ModelSettings() {
|
|
3457
|
+
const { client, t } = useRungent();
|
|
3458
|
+
const root = useRoot("rungent-settings");
|
|
3459
|
+
const baseId = useId();
|
|
3460
|
+
const [rows, setRows] = useState(null);
|
|
3461
|
+
const [status, setStatus] = useState("idle");
|
|
3462
|
+
const [confirming, setConfirming] = useState(false);
|
|
3463
|
+
useEffect(() => {
|
|
3464
|
+
let live = true;
|
|
3465
|
+
client.getSettings().then((settings) => live && setRows(settings.models.chain.map(row)), (error) => live && setStatus({ failed: error instanceof RungentRequestError ? String(error.status) : "—" }));
|
|
3466
|
+
return () => {
|
|
3467
|
+
live = false;
|
|
3468
|
+
};
|
|
3469
|
+
}, [client]);
|
|
3470
|
+
const edit = (index, change) => {
|
|
3471
|
+
setStatus("idle");
|
|
3472
|
+
setRows((current) => (current ?? []).map((item, at) => at === index ? change(item) : item));
|
|
3473
|
+
};
|
|
3474
|
+
const field = (index, fields) => edit(index, (item) => ({
|
|
3475
|
+
...item,
|
|
3476
|
+
endpoint: {
|
|
3477
|
+
...item.endpoint,
|
|
3478
|
+
...fields
|
|
3479
|
+
}
|
|
3480
|
+
}));
|
|
3481
|
+
const move = (index, by) => {
|
|
3482
|
+
setStatus("idle");
|
|
3483
|
+
setRows((current) => {
|
|
3484
|
+
const next = [...current ?? []];
|
|
3485
|
+
const [item] = next.splice(index, 1);
|
|
3486
|
+
if (item) next.splice(index + by, 0, item);
|
|
3487
|
+
return next;
|
|
3488
|
+
});
|
|
3489
|
+
};
|
|
3490
|
+
const save = async (event) => {
|
|
3491
|
+
event.preventDefault();
|
|
3492
|
+
if (!rows) return;
|
|
3493
|
+
if (rows.some((item) => item.endpoint.provider !== "custom" && thinkingOf(item) === null)) {
|
|
3494
|
+
setStatus("invalidThinking");
|
|
3495
|
+
return;
|
|
3496
|
+
}
|
|
3497
|
+
setStatus("saving");
|
|
3498
|
+
try {
|
|
3499
|
+
const saved = await client.putSettings({ models: { chain: rows.flatMap(update) } });
|
|
3500
|
+
setRows(saved.models.chain.map(row));
|
|
3501
|
+
setStatus("saved");
|
|
3502
|
+
} catch (error) {
|
|
3503
|
+
setStatus({ failed: error instanceof RungentRequestError ? String(error.status) : "—" });
|
|
3504
|
+
}
|
|
3505
|
+
};
|
|
3506
|
+
const reset = async () => {
|
|
3507
|
+
setConfirming(false);
|
|
3508
|
+
setStatus("saving");
|
|
3509
|
+
try {
|
|
3510
|
+
const defaults = await client.resetSettings();
|
|
3511
|
+
setRows(defaults.models.chain.map(row));
|
|
3512
|
+
setStatus("reset");
|
|
3513
|
+
} catch (error) {
|
|
3514
|
+
setStatus({ failed: error instanceof RungentRequestError ? String(error.status) : "—" });
|
|
3515
|
+
}
|
|
3516
|
+
};
|
|
3517
|
+
const fromCode = (rows ?? []).some((item) => item.endpoint.provider === "custom");
|
|
3518
|
+
return /* @__PURE__ */ jsxs("form", {
|
|
3519
|
+
...root,
|
|
3520
|
+
onSubmit: save,
|
|
3521
|
+
"aria-labelledby": `${baseId}-title`,
|
|
3522
|
+
children: [
|
|
3523
|
+
/* @__PURE__ */ jsx("h2", {
|
|
3524
|
+
id: `${baseId}-title`,
|
|
3525
|
+
className: "rungent-settings-title",
|
|
3526
|
+
children: t("models")
|
|
3527
|
+
}),
|
|
3528
|
+
/* @__PURE__ */ jsx("p", {
|
|
3529
|
+
className: "rungent-muted rungent-settings-hint",
|
|
3530
|
+
children: t("modelsHint")
|
|
3531
|
+
}),
|
|
3532
|
+
rows === null && status === "idle" ? /* @__PURE__ */ jsx("p", {
|
|
3533
|
+
className: "rungent-muted",
|
|
3534
|
+
children: t("loading")
|
|
3535
|
+
}) : null,
|
|
3536
|
+
/* @__PURE__ */ jsx("ol", {
|
|
3537
|
+
className: "rungent-chain",
|
|
3538
|
+
children: (rows ?? []).map((item, index) => {
|
|
3539
|
+
const id = `${baseId}-${item.key}`;
|
|
3540
|
+
const { endpoint } = item;
|
|
3541
|
+
if (endpoint.provider === "custom") return /* @__PURE__ */ jsxs("li", {
|
|
3542
|
+
className: "rungent-card rungent-endpoint",
|
|
3543
|
+
"data-provider": "custom",
|
|
3544
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
3545
|
+
className: "rungent-endpoint-head",
|
|
3546
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
3547
|
+
className: "rungent-endpoint-index",
|
|
3548
|
+
children: index + 1
|
|
3549
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
3550
|
+
className: "rungent-endpoint-name",
|
|
3551
|
+
children: endpoint.model || endpoint.id
|
|
3552
|
+
})]
|
|
3553
|
+
}), /* @__PURE__ */ jsx("p", {
|
|
3554
|
+
className: "rungent-muted rungent-small rungent-settings-hint",
|
|
3555
|
+
children: t("configuredInCode")
|
|
3556
|
+
})]
|
|
3557
|
+
}, item.key);
|
|
3558
|
+
const example = THINKING_EXAMPLES[endpoint.provider];
|
|
3559
|
+
const thinkingInvalid = status === "invalidThinking" && thinkingOf(item) === null;
|
|
3560
|
+
return /* @__PURE__ */ jsxs("li", {
|
|
3561
|
+
className: "rungent-card rungent-endpoint",
|
|
3562
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
3563
|
+
className: "rungent-endpoint-head",
|
|
3564
|
+
children: [
|
|
3565
|
+
/* @__PURE__ */ jsx("span", {
|
|
3566
|
+
className: "rungent-endpoint-index",
|
|
3567
|
+
children: index + 1
|
|
3568
|
+
}),
|
|
3569
|
+
/* @__PURE__ */ jsx("span", {
|
|
3570
|
+
className: "rungent-endpoint-name",
|
|
3571
|
+
children: endpoint.model || "—"
|
|
3572
|
+
}),
|
|
3573
|
+
/* @__PURE__ */ jsx("button", {
|
|
3574
|
+
type: "button",
|
|
3575
|
+
className: "rungent-icon-button",
|
|
3576
|
+
"aria-label": `${t("moveUp")}: ${endpoint.model}`,
|
|
3577
|
+
disabled: index === 0,
|
|
3578
|
+
onClick: () => move(index, -1),
|
|
3579
|
+
children: "↑"
|
|
3580
|
+
}),
|
|
3581
|
+
/* @__PURE__ */ jsx("button", {
|
|
3582
|
+
type: "button",
|
|
3583
|
+
className: "rungent-icon-button",
|
|
3584
|
+
"aria-label": `${t("moveDown")}: ${endpoint.model}`,
|
|
3585
|
+
disabled: index === (rows?.length ?? 0) - 1,
|
|
3586
|
+
onClick: () => move(index, 1),
|
|
3587
|
+
children: "↓"
|
|
3588
|
+
}),
|
|
3589
|
+
/* @__PURE__ */ jsx("button", {
|
|
3590
|
+
type: "button",
|
|
3591
|
+
className: "rungent-icon-button",
|
|
3592
|
+
"aria-label": `${t("remove")}: ${endpoint.model}`,
|
|
3593
|
+
onClick: () => {
|
|
3594
|
+
setStatus("idle");
|
|
3595
|
+
setRows((current) => (current ?? []).filter((_, at) => at !== index));
|
|
3596
|
+
},
|
|
3597
|
+
children: "×"
|
|
3598
|
+
})
|
|
3599
|
+
]
|
|
3600
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
3601
|
+
className: "rungent-fields",
|
|
3602
|
+
children: [
|
|
3603
|
+
/* @__PURE__ */ jsx("label", {
|
|
3604
|
+
htmlFor: `${id}-provider`,
|
|
3605
|
+
className: "rungent-label",
|
|
3606
|
+
children: t("provider")
|
|
3607
|
+
}),
|
|
3608
|
+
/* @__PURE__ */ jsx("select", {
|
|
3609
|
+
id: `${id}-provider`,
|
|
3610
|
+
className: "rungent-input",
|
|
3611
|
+
value: endpoint.provider,
|
|
3612
|
+
onChange: (event) => field(index, { provider: event.target.value }),
|
|
3613
|
+
children: PROVIDERS.map((provider) => /* @__PURE__ */ jsx("option", {
|
|
3614
|
+
value: provider,
|
|
3615
|
+
children: provider
|
|
3616
|
+
}, provider))
|
|
3617
|
+
}),
|
|
3618
|
+
/* @__PURE__ */ jsx("label", {
|
|
3619
|
+
htmlFor: `${id}-model`,
|
|
3620
|
+
className: "rungent-label",
|
|
3621
|
+
children: t("model")
|
|
3622
|
+
}),
|
|
3623
|
+
/* @__PURE__ */ jsx("input", {
|
|
3624
|
+
id: `${id}-model`,
|
|
3625
|
+
className: "rungent-input",
|
|
3626
|
+
required: true,
|
|
3627
|
+
value: endpoint.model,
|
|
3628
|
+
onChange: (event) => field(index, { model: event.target.value })
|
|
3629
|
+
}),
|
|
3630
|
+
/* @__PURE__ */ jsx("label", {
|
|
3631
|
+
htmlFor: `${id}-base`,
|
|
3632
|
+
className: "rungent-label",
|
|
3633
|
+
children: t("baseURL")
|
|
3634
|
+
}),
|
|
3635
|
+
/* @__PURE__ */ jsx("input", {
|
|
3636
|
+
id: `${id}-base`,
|
|
3637
|
+
className: "rungent-input",
|
|
3638
|
+
type: "url",
|
|
3639
|
+
value: endpoint.baseURL ?? "",
|
|
3640
|
+
onChange: (event) => {
|
|
3641
|
+
const value = event.target.value;
|
|
3642
|
+
edit(index, (other) => {
|
|
3643
|
+
const { baseURL: _, ...rest } = other.endpoint;
|
|
3644
|
+
return {
|
|
3645
|
+
...other,
|
|
3646
|
+
endpoint: value ? {
|
|
3647
|
+
...rest,
|
|
3648
|
+
baseURL: value
|
|
3649
|
+
} : rest
|
|
3650
|
+
};
|
|
3651
|
+
});
|
|
3652
|
+
}
|
|
3653
|
+
}),
|
|
3654
|
+
/* @__PURE__ */ jsx("label", {
|
|
3655
|
+
htmlFor: `${id}-key`,
|
|
3656
|
+
className: "rungent-label",
|
|
3657
|
+
children: t("apiKey")
|
|
3658
|
+
}),
|
|
3659
|
+
/* @__PURE__ */ jsx("input", {
|
|
3660
|
+
id: `${id}-key`,
|
|
3661
|
+
className: "rungent-input",
|
|
3662
|
+
type: "password",
|
|
3663
|
+
autoComplete: "off",
|
|
3664
|
+
placeholder: endpoint.hasApiKey ? t("apiKeySaved") : "",
|
|
3665
|
+
value: item.apiKey,
|
|
3666
|
+
onChange: (event) => edit(index, (other) => ({
|
|
3667
|
+
...other,
|
|
3668
|
+
apiKey: event.target.value
|
|
3669
|
+
}))
|
|
3670
|
+
}),
|
|
3671
|
+
/* @__PURE__ */ jsx("label", {
|
|
3672
|
+
htmlFor: `${id}-temperature`,
|
|
3673
|
+
className: "rungent-label",
|
|
3674
|
+
children: t("temperature")
|
|
3675
|
+
}),
|
|
3676
|
+
/* @__PURE__ */ jsx("input", {
|
|
3677
|
+
id: `${id}-temperature`,
|
|
3678
|
+
className: "rungent-input",
|
|
3679
|
+
type: "number",
|
|
3680
|
+
min: 0,
|
|
3681
|
+
max: 2,
|
|
3682
|
+
step: .1,
|
|
3683
|
+
value: endpoint.temperature ?? "",
|
|
3684
|
+
onChange: (event) => {
|
|
3685
|
+
const value = event.target.value;
|
|
3686
|
+
edit(index, (other) => ({
|
|
3687
|
+
...other,
|
|
3688
|
+
endpoint: numberField(other.endpoint, "temperature", value)
|
|
3689
|
+
}));
|
|
3690
|
+
}
|
|
3691
|
+
}),
|
|
3692
|
+
/* @__PURE__ */ jsx("label", {
|
|
3693
|
+
htmlFor: `${id}-max-tokens`,
|
|
3694
|
+
className: "rungent-label",
|
|
3695
|
+
children: t("maxOutputTokens")
|
|
3696
|
+
}),
|
|
3697
|
+
/* @__PURE__ */ jsx("input", {
|
|
3698
|
+
id: `${id}-max-tokens`,
|
|
3699
|
+
className: "rungent-input",
|
|
3700
|
+
type: "number",
|
|
3701
|
+
min: 1,
|
|
3702
|
+
step: 1,
|
|
3703
|
+
value: endpoint.maxOutputTokens ?? "",
|
|
3704
|
+
onChange: (event) => {
|
|
3705
|
+
const value = event.target.value;
|
|
3706
|
+
edit(index, (other) => ({
|
|
3707
|
+
...other,
|
|
3708
|
+
endpoint: numberField(other.endpoint, "maxOutputTokens", value)
|
|
3709
|
+
}));
|
|
3710
|
+
}
|
|
3711
|
+
}),
|
|
3712
|
+
/* @__PURE__ */ jsx("span", {}),
|
|
3713
|
+
/* @__PURE__ */ jsxs("label", {
|
|
3714
|
+
className: "rungent-choice",
|
|
3715
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
3716
|
+
type: "checkbox",
|
|
3717
|
+
checked: endpoint.vision ?? false,
|
|
3718
|
+
onChange: (event) => field(index, { vision: event.target.checked })
|
|
3719
|
+
}), /* @__PURE__ */ jsx("span", { children: t("vision") })]
|
|
3720
|
+
}),
|
|
3721
|
+
/* @__PURE__ */ jsx("label", {
|
|
3722
|
+
htmlFor: `${id}-thinking-on`,
|
|
3723
|
+
className: "rungent-label",
|
|
3724
|
+
children: t("thinkingOn")
|
|
3725
|
+
}),
|
|
3726
|
+
/* @__PURE__ */ jsx("textarea", {
|
|
3727
|
+
id: `${id}-thinking-on`,
|
|
3728
|
+
className: "rungent-input rungent-json",
|
|
3729
|
+
rows: 1,
|
|
3730
|
+
spellCheck: false,
|
|
3731
|
+
placeholder: example.on,
|
|
3732
|
+
"aria-invalid": thinkingInvalid || void 0,
|
|
3733
|
+
"aria-describedby": `${id}-thinking-hint`,
|
|
3734
|
+
value: item.thinkingOn,
|
|
3735
|
+
onChange: (event) => edit(index, (other) => ({
|
|
3736
|
+
...other,
|
|
3737
|
+
thinkingOn: event.target.value
|
|
3738
|
+
}))
|
|
3739
|
+
}),
|
|
3740
|
+
/* @__PURE__ */ jsx("label", {
|
|
3741
|
+
htmlFor: `${id}-thinking-off`,
|
|
3742
|
+
className: "rungent-label",
|
|
3743
|
+
children: t("thinkingOff")
|
|
3744
|
+
}),
|
|
3745
|
+
/* @__PURE__ */ jsx("textarea", {
|
|
3746
|
+
id: `${id}-thinking-off`,
|
|
3747
|
+
className: "rungent-input rungent-json",
|
|
3748
|
+
rows: 1,
|
|
3749
|
+
spellCheck: false,
|
|
3750
|
+
placeholder: example.off,
|
|
3751
|
+
"aria-invalid": thinkingInvalid || void 0,
|
|
3752
|
+
"aria-describedby": `${id}-thinking-hint`,
|
|
3753
|
+
value: item.thinkingOff,
|
|
3754
|
+
onChange: (event) => edit(index, (other) => ({
|
|
3755
|
+
...other,
|
|
3756
|
+
thinkingOff: event.target.value
|
|
3757
|
+
}))
|
|
3758
|
+
}),
|
|
3759
|
+
/* @__PURE__ */ jsx("span", {}),
|
|
3760
|
+
/* @__PURE__ */ jsx("p", {
|
|
3761
|
+
id: `${id}-thinking-hint`,
|
|
3762
|
+
className: "rungent-muted rungent-small",
|
|
3763
|
+
children: t("thinkingHint")
|
|
3764
|
+
})
|
|
3765
|
+
]
|
|
3766
|
+
})]
|
|
3767
|
+
}, item.key);
|
|
3768
|
+
})
|
|
3769
|
+
}),
|
|
3770
|
+
fromCode ? /* @__PURE__ */ jsx("p", {
|
|
3771
|
+
className: "rungent-muted rungent-small rungent-settings-note",
|
|
3772
|
+
children: t("saveReplacesCode")
|
|
3773
|
+
}) : null,
|
|
3774
|
+
confirming ? /* @__PURE__ */ jsxs("div", {
|
|
3775
|
+
className: "rungent-card rungent-confirm",
|
|
3776
|
+
role: "alertdialog",
|
|
3777
|
+
"aria-label": t("resetDefaults"),
|
|
3778
|
+
children: [/* @__PURE__ */ jsx("p", { children: t("resetConfirm") }), /* @__PURE__ */ jsxs("div", {
|
|
3779
|
+
className: "rungent-actions",
|
|
3780
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
3781
|
+
type: "button",
|
|
3782
|
+
className: "rungent-button",
|
|
3783
|
+
onClick: () => setConfirming(false),
|
|
3784
|
+
children: t("cancel")
|
|
3785
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
3786
|
+
type: "button",
|
|
3787
|
+
className: "rungent-button rungent-primary",
|
|
3788
|
+
"data-danger": "",
|
|
3789
|
+
onClick: () => void reset(),
|
|
3790
|
+
children: t("resetDefaults")
|
|
3791
|
+
})]
|
|
3792
|
+
})]
|
|
3793
|
+
}) : null,
|
|
3794
|
+
/* @__PURE__ */ jsxs("div", {
|
|
3795
|
+
className: "rungent-actions",
|
|
3796
|
+
children: [
|
|
3797
|
+
/* @__PURE__ */ jsx("span", {
|
|
3798
|
+
className: "rungent-small",
|
|
3799
|
+
role: "status",
|
|
3800
|
+
children: status === "saved" ? t("saved") : status === "reset" ? t("resetDone") : status === "invalidThinking" ? t("thinkingInvalid") : typeof status === "object" ? t("saveFailed", { status: status.failed }) : null
|
|
3801
|
+
}),
|
|
3802
|
+
/* @__PURE__ */ jsx("button", {
|
|
3803
|
+
type: "button",
|
|
3804
|
+
className: "rungent-button rungent-ghost",
|
|
3805
|
+
disabled: rows === null || status === "saving" || confirming,
|
|
3806
|
+
onClick: () => setConfirming(true),
|
|
3807
|
+
children: t("resetDefaults")
|
|
3808
|
+
}),
|
|
3809
|
+
/* @__PURE__ */ jsx("button", {
|
|
3810
|
+
type: "button",
|
|
3811
|
+
className: "rungent-button",
|
|
3812
|
+
disabled: rows === null,
|
|
3813
|
+
onClick: () => setRows((current) => [...current ?? [], row({
|
|
3814
|
+
id: freshId(current ?? []),
|
|
3815
|
+
provider: "openai-compatible",
|
|
3816
|
+
model: "",
|
|
3817
|
+
hasApiKey: false
|
|
3818
|
+
})]),
|
|
3819
|
+
children: t("addModel")
|
|
3820
|
+
}),
|
|
3821
|
+
/* @__PURE__ */ jsx("button", {
|
|
3822
|
+
type: "submit",
|
|
3823
|
+
className: "rungent-button rungent-primary",
|
|
3824
|
+
disabled: rows === null || status === "saving",
|
|
3825
|
+
children: t("save")
|
|
3826
|
+
})
|
|
3827
|
+
]
|
|
3828
|
+
})
|
|
3829
|
+
]
|
|
3830
|
+
});
|
|
3831
|
+
}
|
|
3832
|
+
export { ApprovalCard, DOCK_WIDTH, HelpViewer, Markdown, ModelSettings, RungentDock, RungentForm, RungentProvider, RungentThread, SCROLLING_ATTRIBUTE, SCROLL_CLASS, SearchPalette, locales, useCatalog, useDock, useScrollbar, useSearch, useSearchPalette, useThread, useThreads, watchScrollbar };
|