pi-web-ui 0.104.3 → 0.105.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +1 -1
  3. package/README.zh-CN.md +2 -2
  4. package/bin/pi-web-ui.mjs +9 -3
  5. package/dist/server/agent-service-daemon.js +7 -7
  6. package/dist/server/agent-service.js +513 -115
  7. package/dist/server/approval-default-rules.js +15 -0
  8. package/dist/server/client-state.js +42 -2
  9. package/dist/server/dispatch-domain-handlers.js +35 -7
  10. package/dist/server/dsh/dsh-agent-service.js +88 -33
  11. package/dist/server/dsh/dsh-serialize.js +16 -1
  12. package/dist/server/ensure-bash.js +7 -2
  13. package/dist/server/file-transfer-routes.js +114 -1
  14. package/dist/server/index.js +347 -68
  15. package/dist/server/load-tools-tool.js +2 -1
  16. package/dist/server/locales.js +5 -0
  17. package/dist/server/markers/marker.js +28 -0
  18. package/dist/server/mcp-bridge.js +20 -6
  19. package/dist/server/pkg-info.js +69 -0
  20. package/dist/server/plugin-facilities.js +18 -9
  21. package/dist/server/plugin-manifest-validate.js +3 -0
  22. package/dist/server/plugin-updater.js +11 -5
  23. package/dist/server/plugins.js +3 -12
  24. package/dist/server/present-files-tool.js +47 -1
  25. package/dist/server/preset-fields.js +14 -0
  26. package/dist/server/preset-share.js +5 -12
  27. package/dist/server/prompt-composer.js +1 -1
  28. package/dist/server/remote-ssh-service.js +158 -5
  29. package/dist/server/serialize.js +15 -4
  30. package/dist/server/session-parser.js +138 -2
  31. package/dist/server/settings-service.js +30 -5
  32. package/dist/server/terminals.js +7 -1
  33. package/dist/server/text-quote.js +64 -2
  34. package/dist/server/tool-manager.js +10 -0
  35. package/dist/server/tool-prompts.js +1 -0
  36. package/dist/server/update-check.js +98 -24
  37. package/dist/server/workspace-snapshot.js +251 -13
  38. package/dist/server/worktree-service.js +262 -0
  39. package/dist/server/worktree-tool.js +173 -0
  40. package/package.json +3 -1
  41. package/plugin-sdk/index.d.ts +14 -1
  42. package/themes/zhujian-dark.css +16 -4
  43. package/themes/zhujian.css +16 -4
  44. package/themes/zhupi-dark.css +16 -4
  45. package/themes/zhupi.css +16 -4
  46. package/web/dist/assets/{TerminalPanel-VjlOOAig.js → TerminalPanel-B9exeXrl.js} +1 -1
  47. package/web/dist/assets/index-BQC6lADz.css +1 -0
  48. package/web/dist/assets/index-D79IKG33.js +690 -0
  49. package/web/dist/index.html +21 -21
  50. package/web/dist/sw.js +1 -1
  51. package/web/dist/assets/index-5SITmQd_.js +0 -680
  52. package/web/dist/assets/index-D9wTa3O-.css +0 -1
@@ -0,0 +1,173 @@
1
+ /**
2
+ * server/worktree-tool.ts
3
+ *
4
+ * Git 工作树(worktree)管理工具。
5
+ *
6
+ * 支持动作:
7
+ * - list: 列出当前仓库的所有工作树、分支、未提交文件数与领先状态;
8
+ * - create: 创建新的工作树(支持基于已有分支或新分支,可选 switchTo 立即切换工作区);
9
+ * - switch: 切换当前工作区至指定工作树;
10
+ * - remove: 安全移除工作树(含未提交修改保护)。
11
+ */
12
+ import { Type } from "typebox";
13
+ import { resolve } from "node:path";
14
+ import { pick } from "./i18n.js";
15
+ import { createWorktree, listWorktrees, normalizeWirePath, removeWorktree } from "./worktree-service.js";
16
+ export const WORKTREE_TOOL_NAME = "worktree";
17
+ const WorktreeParameters = Type.Object({
18
+ action: Type.Union([Type.Literal("list"), Type.Literal("create"), Type.Literal("switch"), Type.Literal("remove")], {
19
+ description: "Operation to perform: list | create | switch | remove.",
20
+ }),
21
+ path: Type.Optional(Type.String({
22
+ description: "Target directory path for worktree create/switch/remove (relative or absolute).",
23
+ })),
24
+ branch: Type.Optional(Type.String({
25
+ description: "Branch name to check out or bind to the worktree.",
26
+ })),
27
+ newBranch: Type.Optional(Type.Union([Type.Boolean(), Type.String()], {
28
+ description: "Create and switch to a new branch (boolean true or string branch name).",
29
+ })),
30
+ commit: Type.Optional(Type.String({
31
+ description: "Base commit or branch to branch off from.",
32
+ })),
33
+ switchTo: Type.Optional(Type.Boolean({
34
+ description: "create: true to switch the active workspace to the new worktree immediately.",
35
+ })),
36
+ force: Type.Optional(Type.Boolean({
37
+ description: "remove: true to force removal even if dirty or containing unmerged changes.",
38
+ })),
39
+ });
40
+ export function makeWorktreeTool(host, getLang) {
41
+ return {
42
+ name: WORKTREE_TOOL_NAME,
43
+ label: "Manage Git worktrees",
44
+ description: "Inspect, create, switch, or remove Git worktrees for isolated branch or PR development. " +
45
+ "Maintains synchronization between the active workspace and git worktree directories.",
46
+ promptSnippet: "manage Git worktrees for isolated PR and branch work",
47
+ promptGuidelines: [
48
+ "Use worktree(action='create') rather than running git worktree add in bash so the workspace UI stays synchronized",
49
+ "Call worktree(action='switch') or pass switchTo=true to change the active workspace to the target worktree",
50
+ "Always inspect worktree(action='list') before moving to a new PR to avoid leaving unmerged changes",
51
+ ],
52
+ parameters: WorktreeParameters,
53
+ async execute(toolCallId, params) {
54
+ const lang = getLang();
55
+ const cwd = host.getCwd();
56
+ try {
57
+ switch (params.action) {
58
+ case "list": {
59
+ const list = await listWorktrees(cwd);
60
+ if (list.length === 0) {
61
+ return {
62
+ content: [
63
+ {
64
+ type: "text",
65
+ text: pick(lang, "当前目录不属于 Git 仓库或暂未发现任何 Git 工作树。", "Current directory is not a Git repository or no worktrees found.", "worktree.list.empty"),
66
+ },
67
+ ],
68
+ details: { worktrees: [] },
69
+ };
70
+ }
71
+ const lines = [];
72
+ lines.push(pick(lang, `共发现 ${list.length} 个 Git 工作树:`, `Found ${list.length} Git worktree(s):`, "worktree.list.header"));
73
+ for (const item of list) {
74
+ const tags = [];
75
+ if (item.isMain)
76
+ tags.push(pick(lang, "主仓库", "main", "worktree.tag.main"));
77
+ if (item.isCurrent)
78
+ tags.push(pick(lang, "当前工作区", "active", "worktree.tag.current"));
79
+ if (item.isDirty) {
80
+ tags.push(pick(lang, `未提交修改: ${item.uncommittedCount ?? 0} 个文件`, `dirty: ${item.uncommittedCount ?? 0} files`, "worktree.tag.dirty"));
81
+ }
82
+ if (item.aheadCount && item.aheadCount > 0) {
83
+ tags.push(pick(lang, `领先主分支 ${item.aheadCount} 个提交`, `ahead ${item.aheadCount} commit(s)`, "worktree.tag.ahead"));
84
+ }
85
+ const tagStr = tags.length > 0 ? ` [${tags.join(" | ")}]` : "";
86
+ const branchStr = item.branch ? ` (${item.branch})` : "";
87
+ lines.push(`- ${item.path}${branchStr}${tagStr}`);
88
+ }
89
+ return {
90
+ content: [{ type: "text", text: lines.join("\n") }],
91
+ details: { worktrees: list },
92
+ };
93
+ }
94
+ case "create": {
95
+ if (!params.path) {
96
+ throw new Error(pick(lang, "创建工作树必须指定 path 参数", "path parameter is required for creating a worktree", "worktree.create.missing_path"));
97
+ }
98
+ const res = await createWorktree(cwd, {
99
+ path: params.path,
100
+ branch: params.branch,
101
+ newBranch: params.newBranch,
102
+ commit: params.commit,
103
+ }, () => lang);
104
+ if (params.switchTo) {
105
+ await host.setCwd(res.path);
106
+ }
107
+ await host.onWorktreeChanged?.();
108
+ const switchedText = params.switchTo
109
+ ? pick(lang, ",并已切换当前工作区至该目录", ", and switched active workspace to it", "worktree.create.switched")
110
+ : "";
111
+ return {
112
+ content: [
113
+ {
114
+ type: "text",
115
+ text: pick(lang, `✅ 成功创建 Git 工作树:${res.path}${res.branch ? ` (分支: ${res.branch})` : ""}${switchedText}`, `✅ Successfully created Git worktree: ${res.path}${res.branch ? ` (branch: ${res.branch})` : ""}${switchedText}`, "worktree.create.success"),
116
+ },
117
+ ],
118
+ details: { path: res.path, branch: res.branch, switched: Boolean(params.switchTo) },
119
+ };
120
+ }
121
+ case "switch": {
122
+ if (!params.path) {
123
+ throw new Error(pick(lang, "切换工作树必须指定 path 参数", "path parameter is required for switching worktree", "worktree.switch.missing_path"));
124
+ }
125
+ const target = normalizeWirePath(resolve(cwd, params.path));
126
+ await host.setCwd(target);
127
+ await host.onWorktreeChanged?.();
128
+ return {
129
+ content: [
130
+ {
131
+ type: "text",
132
+ text: pick(lang, `🌿 已将当前工作区切换至 Git 工作树:${target}`, `🌿 Switched active workspace to Git worktree: ${target}`, "worktree.switch.success"),
133
+ },
134
+ ],
135
+ details: { path: target },
136
+ };
137
+ }
138
+ case "remove": {
139
+ if (!params.path) {
140
+ throw new Error(pick(lang, "移除工作树必须指定 path 参数", "path parameter is required for removing worktree", "worktree.remove.missing_path"));
141
+ }
142
+ const target = normalizeWirePath(resolve(cwd, params.path));
143
+ await removeWorktree(cwd, target, params.force, () => lang);
144
+ await host.onWorktreeChanged?.();
145
+ return {
146
+ content: [
147
+ {
148
+ type: "text",
149
+ text: pick(lang, `🗑️ 已安全移除 Git 工作树:${target}`, `🗑️ Successfully removed Git worktree: ${target}`, "worktree.remove.success"),
150
+ },
151
+ ],
152
+ details: { path: target, removed: true },
153
+ };
154
+ }
155
+ default:
156
+ throw new Error(`Unknown action: ${params.action}`);
157
+ }
158
+ }
159
+ catch (err) {
160
+ const msg = err.message || String(err);
161
+ return {
162
+ content: [
163
+ {
164
+ type: "text",
165
+ text: pick(lang, `❌ 操作 Git 工作树失败:${msg}`, `❌ Worktree operation failed: ${msg}`, "worktree.op.failed"),
166
+ },
167
+ ],
168
+ details: { error: msg },
169
+ };
170
+ }
171
+ },
172
+ };
173
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-web-ui",
3
- "version": "0.104.3",
3
+ "version": "0.105.0",
4
4
  "description": "Just open your browser to get all your work done — browser cockpit for AI coding agents (chat, code, files, terminal, Git in one tab)",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -81,6 +81,8 @@
81
81
  "build:desktop": "tsc -p desktop/tsconfig.json",
82
82
  "desktop:dev": "npm run build:desktop && electron dist/desktop/main.js",
83
83
  "desktop:dist": "npm run build && npm run build:desktop && electron-builder -c desktop/electron-builder.yml",
84
+ "desktop:portable": "npm run build && npm run build:desktop && node scripts/make-portable.mjs",
85
+ "desktop:portable:full": "npm run build && npm run build:desktop && node scripts/make-portable.mjs --with-git",
84
86
  "format": "prettier --write server/ web/src/ extensions/ scripts/ bin/ desktop/ tests/*.mjs tests/unit/ *.mjs",
85
87
  "format:check": "prettier --check server/ web/src/ extensions/ scripts/ bin/ desktop/ tests/*.mjs tests/unit/ *.mjs",
86
88
  "lint": "oxlint server/ web/src/ extensions/ scripts/ bin/ desktop/",
@@ -5,7 +5,20 @@
5
5
  */
6
6
 
7
7
  export type PluginPermissionFamily =
8
- "fs" | "fs:read" | "fs:write" | "ui" | "tools" | "http" | "chat" | "net" | "dom" | "dom:anchor";
8
+ | "fs"
9
+ | "fs:read"
10
+ | "fs:write"
11
+ | "ui"
12
+ | "tools"
13
+ | "http"
14
+ | "chat"
15
+ | "net"
16
+ | "llm"
17
+ | "dom"
18
+ | "dom:anchor"
19
+ | "notify"
20
+ | "storage"
21
+ | "secrets";
9
22
 
10
23
  export interface WsEntry {
11
24
  name: string;
@@ -187,6 +187,22 @@ body,
187
187
  padding: 2px 0 10px;
188
188
  }
189
189
 
190
+ .msg-user {
191
+ padding-bottom: 2px !important;
192
+ }
193
+
194
+ .msg-user .msg-actions {
195
+ margin-top: 2px !important;
196
+ }
197
+
198
+ .msg-user + .msg-assistant {
199
+ padding-top: 2px !important;
200
+ }
201
+
202
+ .msg-user + .msg-assistant .msg-body > :first-child {
203
+ margin-top: 2px !important;
204
+ }
205
+
190
206
  /* 操作按钮:卡头只有自己的复制键(.chead-copy),消息级按钮(复制三件套 /
191
207
  分叉 / 回滚 / 朗读)统一在底部 .msg-actions 行,流内排在消息气泡/正文下边,
192
208
  桌面端与手机端保持一致。 */
@@ -423,10 +439,6 @@ div.chead[role="button"]:hover,
423
439
  align-items: center;
424
440
  }
425
441
 
426
- .thinking-icon {
427
- color: var(--text-faint) !important;
428
- }
429
-
430
442
  .thinking-body {
431
443
  border-top: 1px solid var(--border-soft) !important;
432
444
  font-style: normal !important;
@@ -190,6 +190,22 @@ body,
190
190
  padding: 2px 0 10px;
191
191
  }
192
192
 
193
+ .msg-user {
194
+ padding-bottom: 2px !important;
195
+ }
196
+
197
+ .msg-user .msg-actions {
198
+ margin-top: 2px !important;
199
+ }
200
+
201
+ .msg-user + .msg-assistant {
202
+ padding-top: 2px !important;
203
+ }
204
+
205
+ .msg-user + .msg-assistant .msg-body > :first-child {
206
+ margin-top: 2px !important;
207
+ }
208
+
193
209
  /* 操作按钮:卡头只有自己的复制键(.chead-copy),消息级按钮(复制三件套 /
194
210
  分叉 / 回滚 / 朗读)统一在底部 .msg-actions 行,流内排在消息气泡/正文下边,
195
211
  桌面端与手机端保持一致。 */
@@ -431,10 +447,6 @@ div.chead[role="button"]:hover,
431
447
  align-items: center;
432
448
  }
433
449
 
434
- .thinking-icon {
435
- color: var(--text-faint) !important;
436
- }
437
-
438
450
  .thinking-body {
439
451
  border-top: 1px solid var(--border-soft) !important;
440
452
  font-style: normal !important;
@@ -187,6 +187,22 @@ body,
187
187
  padding: 2px 0 10px;
188
188
  }
189
189
 
190
+ .msg-user {
191
+ padding-bottom: 2px !important;
192
+ }
193
+
194
+ .msg-user .msg-actions {
195
+ margin-top: 2px !important;
196
+ }
197
+
198
+ .msg-user + .msg-assistant {
199
+ padding-top: 2px !important;
200
+ }
201
+
202
+ .msg-user + .msg-assistant .msg-body > :first-child {
203
+ margin-top: 2px !important;
204
+ }
205
+
190
206
  /* 操作按钮:卡头只有自己的复制键(.chead-copy),消息级按钮(复制三件套 /
191
207
  分叉 / 回滚 / 朗读)统一在底部 .msg-actions 行,流内排在消息气泡/正文下边,
192
208
  桌面端与手机端保持一致。 */
@@ -407,10 +423,6 @@ div.chead[role="button"]:hover,
407
423
  color: var(--text-faint);
408
424
  }
409
425
 
410
- .thinking-icon {
411
- color: var(--text-faint) !important;
412
- }
413
-
414
426
  .thinking-body {
415
427
  border-top: none !important;
416
428
  font-style: normal !important;
package/themes/zhupi.css CHANGED
@@ -191,6 +191,22 @@ body,
191
191
  padding: 2px 0 10px;
192
192
  }
193
193
 
194
+ .msg-user {
195
+ padding-bottom: 2px !important;
196
+ }
197
+
198
+ .msg-user .msg-actions {
199
+ margin-top: 2px !important;
200
+ }
201
+
202
+ .msg-user + .msg-assistant {
203
+ padding-top: 2px !important;
204
+ }
205
+
206
+ .msg-user + .msg-assistant .msg-body > :first-child {
207
+ margin-top: 2px !important;
208
+ }
209
+
194
210
  /* 操作按钮:卡头只有自己的复制键(.chead-copy),消息级按钮(复制三件套 /
195
211
  分叉 / 回滚 / 朗读)统一在底部 .msg-actions 行,流内排在消息气泡/正文下边,
196
212
  桌面端与手机端保持一致。 */
@@ -416,10 +432,6 @@ div.chead[role="button"]:hover,
416
432
  color: var(--text-faint);
417
433
  }
418
434
 
419
- .thinking-icon {
420
- color: var(--text-faint) !important;
421
- }
422
-
423
435
  .thinking-body {
424
436
  border-top: none !important;
425
437
  font-style: normal !important;
@@ -1,3 +1,3 @@
1
- import{r as e}from"./rolldown-runtime-hePW80VL.js";import{i as t,t as n}from"./react-CR5VJ85Q.js";import{n as r,t as i}from"./xterm-Cf5xxe4g.js";import{a,c as o,d as s,f as c,g as l,h as u,i as d,l as f,m as p,o as m,p as h,r as g,s as _,t as v,u as y}from"./index-5SITmQd_.js";var b=e(t(),1);function x(e,t){return Math.abs(t)>10&&Math.abs(t)>Math.abs(e)*1.2}function S(e,t){return e-t}var C=n();function w(e){let t=null;return{clean:e.replace(/\r?\n?\[pi-term-exit:(-?\d+)\]\r?\n?/g,(e,n)=>(t=Number(n),`\r
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{i as t,t as n}from"./react-CR5VJ85Q.js";import{n as r,t as i}from"./xterm-Cf5xxe4g.js";import{a,c as o,d as s,f as c,g as l,h as u,i as d,l as f,m as p,o as m,p as h,r as g,s as _,t as v,u as y}from"./index-D79IKG33.js";var b=e(t(),1);function x(e,t){return Math.abs(t)>10&&Math.abs(t)>Math.abs(e)*1.2}function S(e,t){return e-t}var C=n();function w(e){let t=null;return{clean:e.replace(/\r?\n?\[pi-term-exit:(-?\d+)\]\r?\n?/g,(e,n)=>(t=Number(n),`\r
2
2
  `)).replace(/\r?\n?\x1b\[90m\[(?:进程已退出,退出码 |Process exited with code )-?\d+\]\x1b\[0m\r?\n?/g,`\r
3
3
  `),exitCode:t}}function T({conversationId:e,terminalId:t,command:n,cwd:o,title:s,agentBash:c,active:l,running:u,exitCode:f,register:p}){let h=(0,b.useRef)(null),_=(0,b.useRef)(null),{locale:v}=m(),y=(0,b.useRef)(v);y.current=v;let T=n?JSON.stringify(n):``,E=(0,b.useRef)(u===!1);(0,b.useEffect)(()=>{let u=h.current;if(!u)return;let f=new r({theme:d(),fontFamily:`"SF Mono", "JetBrains Mono", ui-monospace, Menlo, Consolas, monospace`,fontSize:13,cursorBlink:!0,scrollback:8e3}),m=new i;f.loadAddon(m),f.open(u),_.current={term:f,fit:m},l&&f.focus();let v=()=>{f.options.theme=d()};window.addEventListener(g,v),f.attachCustomKeyEventHandler(e=>{if(e.type!==`keydown`)return!0;let t=e.key?.toLowerCase();if((e.ctrlKey||e.metaKey)&&t===`v`)return!1;if(e.ctrlKey&&!e.shiftKey&&!e.altKey&&t===`c`&&f.hasSelection()){let e=f.textarea;return e&&(e.value=f.getSelection(),e.select()),!1}return!0});let b=p(e,t,{write:e=>f.write(w(e).clean),dispose:()=>f.dispose()}),C=()=>{try{m.fit(),a({type:`terminal_resize`,terminalId:t,conversationId:e,cols:f.cols,rows:f.rows})}catch{}},T=requestAnimationFrame(()=>{try{m.fit()}catch{}E.current||a(n?{type:`run_command`,terminalId:t,conversationId:e,command:n,cols:f.cols,rows:f.rows}:{type:`terminal_create`,terminalId:t,title:s,locale:y.current,agentBash:c,conversationId:e,cwd:o,cols:f.cols,rows:f.rows})}),D=f.onData(n=>{a({type:`terminal_input`,terminalId:t,conversationId:e,data:n})}),O=f.element,k=null,A=0,j=0,M=0,N=!1,P=e=>{if(e.touches.length!==1){k=null,N=!1;return}let t=e.touches[0];k=t.identifier,A=t.clientX,j=t.clientY,M=t.clientY,N=!1},F=e=>{if(k===null||e.touches.length!==1)return;let t=e.touches[0];if(t.identifier!==k)return;if(!N){if(!x(t.clientX-A,t.clientY-j))return;N=!0}e.preventDefault();let n=S(M,t.clientY);M=t.clientY,n!==0&&typeof WheelEvent<`u`&&(u.querySelector(`.xterm-viewport`)??O)?.dispatchEvent(new WheelEvent(`wheel`,{bubbles:!0,cancelable:!0,deltaY:n,deltaMode:0,clientX:t.clientX,clientY:t.clientY}))},I=e=>{if(k!==null){if(e.touches.length>0){k=null,N=!1;return}k=null,N=!1}};O?.addEventListener(`touchstart`,P,{passive:!0}),O?.addEventListener(`touchmove`,F,{passive:!1}),O?.addEventListener(`touchend`,I),O?.addEventListener(`touchcancel`,I);let L=null;return typeof ResizeObserver<`u`&&(L=new ResizeObserver(()=>{u.offsetWidth>0&&u.offsetHeight>0&&C()}),L.observe(u)),()=>{cancelAnimationFrame(T),D.dispose(),O?.removeEventListener(`touchstart`,P),O?.removeEventListener(`touchmove`,F),O?.removeEventListener(`touchend`,I),O?.removeEventListener(`touchcancel`,I),window.removeEventListener(g,v),L?.disconnect(),b(),f.dispose(),_.current=null}},[e,t,T,p]),(0,b.useEffect)(()=>{if(!l)return;let n=requestAnimationFrame(()=>{let n=_.current;if(n){try{n.fit.fit(),a({type:`terminal_resize`,terminalId:t,conversationId:e,cols:n.term.cols,rows:n.term.rows})}catch{}n.term.focus()}});return()=>cancelAnimationFrame(n)},[l]);let{t:D}=m(),O=(0,b.useRef)(void 0);return(0,b.useEffect)(()=>{if(u===O.current||(O.current=u,u!==!1))return;let e=_.current;e&&e.term.write(`\r\n\x1b[90m${D(`exitBanner`,{code:f??``})}\x1b[0m\r\n`)},[u,t,f]),(0,C.jsx)(`div`,{ref:h,className:`term-xterm ${l?``:`hidden`}`})}var E={name:``,command:``,cwd:"${pwd}"};function D({chat:e,terminal:t,uiTerminalToolbar:n,onUiAction:r}){let i=_(),[d,m]=(0,b.useState)(null),[g,x]=(0,b.useState)(!1),[S,w]=(0,b.useState)(!1),[D,O]=(0,b.useState)(null),[k,A]=(0,b.useState)(E),[j,M]=(0,b.useState)(null),N=(0,b.useRef)(null),[P,F]=(0,b.useState)(!0),[I,L]=(0,b.useState)(null),[R,z]=(0,b.useState)(``);(0,b.useEffect)(()=>{e.terminals.length===0?m(null):e.terminals.some(e=>e.id===d)||m(e.terminals[e.terminals.length-1].id)},[e.terminals,d]),(0,b.useEffect)(()=>{e.terminalActiveId&&(m(e.terminalActiveId),x(!1))},[e.terminalActiveId]),(0,b.useEffect)(()=>()=>{N.current&&clearTimeout(N.current)},[]);let B=e.terminals.filter(e=>!e.agentBash),V=e.terminals.filter(e=>e.agentBash),H=n=>{if(!e.ready)return;let r=o(),i=e.activeConversationId||e.state?.conversationId||``;t.create({...n,id:r,conversationId:i,cols:n.cols??80,rows:n.rows??24,running:!0,exitCode:null}),m(r),x(!1)},U=()=>H({title:i(`terminalTitle`,{n:B.length+1}),cwd:e.state?.cwd??``}),W=n=>{let r=n.name||n.command,i=e.terminals.find(e=>e.title===r);if(i){t.restart(i.id),m(i.id),a({type:`run_command`,terminalId:i.id,conversationId:i.conversationId,command:n,cols:80,rows:24});return}H({title:r,cwd:e.state?.cwd??``,command:n})},G=n=>{let r=e.terminals.find(e=>e.id===n);if(r&&a({type:`terminal_kill`,terminalId:n,conversationId:r.conversationId}),t.close(n),d===n){let t=e.terminals.filter(e=>e.id!==n);m(t.length>0?t[t.length-1].id:null)}},K=e=>(0,C.jsxs)(`div`,{className:`term-tab ${e.id===d?`active`:``}`,children:[(0,C.jsxs)(`button`,{type:`button`,className:`term-tab-main`,title:`${e.cwd}${e.command?`\n> ${e.command.command}`:``}`,onClick:()=>{I||(m(e.id),x(!1))},children:[(0,C.jsx)(`span`,{className:`term-tab-dot ${e.running?`run`:`exit`}`}),(0,C.jsxs)(`span`,{className:`term-tab-title`,children:[I===e.id?(0,C.jsx)(`input`,{autoFocus:!0,className:`term-tab-rename-input`,value:R,placeholder:e.title,onClick:e=>e.stopPropagation(),onChange:e=>z(e.target.value),onKeyDown:t=>{if(t.stopPropagation(),t.key===`Enter`&&!t.nativeEvent.isComposing){let t=R.trim();t&&a({type:`rename_terminal`,terminalId:e.id,conversationId:e.conversationId,title:t}),L(null)}else t.key===`Escape`&&L(null)},onBlur:()=>L(null)}):e.title,!e.running&&(0,C.jsx)(`span`,{className:`term-tab-exit`,children:i(`exited`,{code:e.exitCode===null?``:` ${e.exitCode}`})})]})]}),(0,C.jsx)(`button`,{type:`button`,className:`term-tab-close term-tab-rename`,title:i(`renameTerminal`),onClick:t=>{t.stopPropagation(),z(e.title),L(e.id)},children:(0,C.jsx)(f,{})}),(0,C.jsx)(`button`,{type:`button`,className:`term-tab-close`,title:i(`closeTerminal`),onClick:()=>G(e.id),children:(0,C.jsx)(l,{})})]},e.id),q=()=>{w(!0),O(null),A(E)},ee=t=>{let n=e.commands[t];n&&(w(!1),O(t),A({name:n.name,command:n.command,cwd:n.cwd??``}))},J=()=>{w(!1),O(null)},Y=()=>{let t=k.name.trim(),n=k.command.trim();if(!t||!n)return;let r={name:t,command:n,cwd:k.cwd.trim()||void 0},i=S?[...e.commands,r]:D===null?e.commands:e.commands.map((e,t)=>t===D?r:e);a({type:`save_commands`,commands:i}),J()},te=t=>{if(j===t){let n=e.commands.filter((e,n)=>n!==t);a({type:`save_commands`,commands:n}),M(null),N.current&&clearTimeout(N.current)}else M(t),N.current&&clearTimeout(N.current),N.current=setTimeout(()=>M(null),2500)},ne=S||D!==null,X=n===void 0?[`host:term-cmd-refresh`,`host:term-cmd-new`,`host:term-tab-new`].map(e=>({id:e,source:`host`})):n.filter(e=>!e.hidden),Z=(e,t)=>{let n=new Set(e);return X.filter(e=>n.has(e.id)||t&&e.source!==`host`)},Q=(e,t)=>{let n=X.find(t=>t.id===e);return n?.labelExplicit?n.label:t},$={"host:term-cmd-refresh":(0,C.jsx)(`button`,{type:`button`,className:`panel-refresh`,title:Q(`host:term-cmd-refresh`,i(`rerun`)),onClick:()=>a({type:`list_commands`}),children:(0,C.jsx)(h,{})}),"host:term-cmd-new":(0,C.jsx)(`button`,{type:`button`,className:`panel-new`,title:Q(`host:term-cmd-new`,i(`newCommand`)),onClick:q,children:(0,C.jsx)(c,{})}),"host:term-tab-new":(0,C.jsx)(`button`,{type:`button`,className:`panel-new`,title:Q(`host:term-tab-new`,i(`newTerminal`)),onClick:U,children:(0,C.jsx)(c,{})})};return(0,C.jsxs)(`div`,{className:`terminal-view`,children:[(0,C.jsxs)(`aside`,{className:`term-side term-commands ${g?`open`:``}`,children:[(0,C.jsxs)(`div`,{className:`panel-header`,children:[(0,C.jsx)(`span`,{className:`panel-title`,children:i(`commands`)}),(0,C.jsx)(`div`,{className:`panel-header-actions`,children:v(Z([`host:term-cmd-refresh`,`host:term-cmd-new`],!1),$,r)})]}),(0,C.jsx)(`div`,{className:`panel-body`,children:ne?(0,C.jsxs)(`div`,{className:`cmd-form`,children:[(0,C.jsx)(`label`,{htmlFor:`cmd-name`,children:i(`name`)}),(0,C.jsx)(`input`,{id:`cmd-name`,className:`cmd-input`,value:k.name,placeholder:i(`exampleName`),autoFocus:!0,onChange:e=>A({...k,name:e.target.value})}),(0,C.jsx)(`label`,{htmlFor:`cmd-command`,children:i(`command`)}),(0,C.jsx)(`input`,{id:`cmd-command`,className:`cmd-input`,value:k.command,placeholder:i(`exampleCommand`),onChange:e=>A({...k,command:e.target.value}),onKeyDown:e=>{e.key===`Enter`&&!e.nativeEvent.isComposing&&Y()}}),(0,C.jsxs)(`label`,{htmlFor:`cmd-cwd`,children:[i(`directory`),` `,(0,C.jsx)(`span`,{className:`cmd-hint`,children:i(`cwdHint`)})]}),(0,C.jsx)(`input`,{id:`cmd-cwd`,className:`cmd-input`,value:k.cwd,placeholder:"${pwd}",onChange:e=>A({...k,cwd:e.target.value}),onKeyDown:e=>{e.key===`Enter`&&!e.nativeEvent.isComposing&&Y()}}),(0,C.jsxs)(`div`,{className:`cmd-form-actions`,children:[(0,C.jsx)(`button`,{type:`button`,className:`btn`,onClick:J,children:i(`cancel`)}),(0,C.jsx)(`button`,{type:`button`,className:`btn primary`,disabled:!k.name.trim()||!k.command.trim(),onClick:Y,children:i(`save`)})]})]}):(0,C.jsxs)(C.Fragment,{children:[e.commands.length===0&&(0,C.jsx)(`div`,{className:`panel-empty`,children:i(`noCommands`)}),e.commands.map((e,t)=>(0,C.jsxs)(`div`,{className:`cmd-item`,children:[(0,C.jsx)(`button`,{type:`button`,className:`cmd-run`,title:i(`clickToRun`),onClick:()=>W(e),children:(0,C.jsx)(s,{})}),(0,C.jsxs)(`button`,{type:`button`,className:`cmd-main`,title:i(`clickToRun`),onClick:()=>W(e),children:[(0,C.jsx)(`span`,{className:`cmd-name`,children:e.name}),(0,C.jsx)(`span`,{className:`cmd-command`,children:e.command}),e.cwd&&(0,C.jsx)(`span`,{className:`cmd-cwd`,children:e.cwd})]}),(0,C.jsx)(`button`,{type:`button`,className:`cmd-act`,title:i(`edit`),onClick:()=>ee(t),children:(0,C.jsx)(f,{})}),(0,C.jsx)(`button`,{type:`button`,className:`cmd-act del ${j===t?`confirm`:``}`,title:i(`delete`),onClick:()=>te(t),children:j===t?i(`confirmQ`):(0,C.jsx)(u,{})})]},t))]})}),(0,C.jsxs)(`div`,{className:`term-tabs-block`,children:[(0,C.jsxs)(`div`,{className:`panel-header`,children:[(0,C.jsx)(`span`,{className:`panel-title`,children:i(`terminal`)}),v(Z([`host:term-tab-new`],!0),$,r)]}),(0,C.jsxs)(`div`,{className:`panel-body`,children:[e.terminals.length===0&&(0,C.jsx)(`div`,{className:`panel-empty`,children:i(`noTerminal`)}),B.map(K),V.length>0&&(0,C.jsxs)(`div`,{className:`term-folder`,children:[(0,C.jsxs)(`button`,{type:`button`,className:`term-folder-header ${P?`open`:``}`,title:i(`aiBashGroup`),onClick:()=>F(e=>!e),children:[(0,C.jsx)(`span`,{className:`term-folder-caret`,children:P?`▾`:`▸`}),(0,C.jsx)(`span`,{className:`term-folder-title`,children:i(`aiBashGroup`)}),(0,C.jsx)(`span`,{className:`term-folder-count`,children:V.length})]}),P&&(0,C.jsx)(`div`,{className:`term-folder-body`,children:V.map(K)})]})]})]})]}),(0,C.jsxs)(`div`,{className:`term-main`,children:[g&&(0,C.jsx)(`div`,{className:`drawer-backdrop`,onClick:()=>x(!1)}),(0,C.jsx)(`button`,{type:`button`,className:`term-side-toggle`,title:i(`commands`),onClick:()=>x(e=>!e),children:(0,C.jsx)(y,{})}),e.terminals.length===0?(0,C.jsxs)(`div`,{className:`term-empty`,children:[(0,C.jsx)(p,{className:`term-empty-icon`}),(0,C.jsx)(`div`,{className:`term-empty-title`,children:i(`builtinTerminal`)}),(0,C.jsx)(`div`,{className:`term-empty-sub`,children:i(`termEmptySub`)})]}):e.terminals.map(e=>(0,C.jsx)(T,{conversationId:e.conversationId,terminalId:e.id,command:e.command,cwd:e.cwd,title:e.title,agentBash:e.agentBash,active:e.id===d,running:e.running,exitCode:e.exitCode,register:t.register},`${e.conversationId}:${e.id}`))]})]})}export{D as TerminalPanel};