dsh-gufa-code 1.3.218 → 1.3.234
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -2
- package/client.js +334 -10
- package/host/open-with.js +322 -0
- package/index.js +1 -1
- package/package.json +1 -1
- package/src/client/00-core.js +15 -0
- package/src/client/90-app.js +141 -9
- package/src/client/99-main.js +177 -0
package/README.md
CHANGED
|
@@ -45,7 +45,9 @@ dsh plugin add dsh-gufa-code
|
|
|
45
45
|
|
|
46
46
|
### 入口与工作区
|
|
47
47
|
|
|
48
|
+
- **系统「打开方式」(macOS,安装插件自动注册)**:插件首次加载会在 `~/Applications` 生成并注册一个「古法编程」打开器(Host 监听打开请求并通过 SSE 推送,不做文件轮询);之后右键 `.md` / `.php` / `.html` / `.txt` / 常见代码文件 →「打开方式」会出现「古法编程」,点击自动唤起 DSH、进入经典古法 IDE,并把工作区设为文件所在目录、打开该文件。系统默认打开方式仍需你在 Finder 里手动确认一次;Windows/Linux 版本后续补。
|
|
48
49
|
- 工作区行的 **「…」菜单** 里有「古法编程」(第二项),点它进入 IDE;
|
|
50
|
+
- **经典界面聊天右上角工具区多了「古法编程」文本入口**(在头像菜单左侧的空位,和「智能体团队」同款 link 样式):点击直接进入 IDE,并打开当前聊天所在的工作区,不用先打开工作区菜单;
|
|
49
51
|
- 从哪个工作区点进来,就打开哪个工作区的古法编程;
|
|
50
52
|
- IDE 左上角可切换工作区 / 新建工作区;退出只有一个入口:标题栏右上角 **「返回经典模式」**。
|
|
51
53
|
|
|
@@ -85,7 +87,21 @@ dsh plugin add dsh-gufa-code
|
|
|
85
87
|
|
|
86
88
|
### 左侧面板
|
|
87
89
|
|
|
88
|
-
-
|
|
90
|
+
- **经典模式入口**
|
|
91
|
+
- 聊天右上角工具区、头像菜单左侧新增一个带品牌色边框的「古法编程」文本入口(只加边框,不做额外底纹/阴影),点击进入当前聊天工作区的 IDE;不再需要从工作区「…」菜单里找入口。
|
|
92
|
+
|
|
93
|
+
**系统文件打开方式(macOS)**
|
|
94
|
+
- 记住最后一次打开的工作区:重启 DSH 自动恢复 IDE 时,入口同步不再把项目改回当前会话 cwd,而是保留 boot 恢复的上一次工作区;新增 `project-apply` / `entry-sync keep-saved` 日志。
|
|
95
|
+
- pending 路径统一 `realpath` 规范化,避免 symlink/大小写差异导致相对路径算错;打开文件失败不再静默 ack,会保留请求重试并记录 `open-with open-failed rel=...`;日志补充完整 pending 路径。
|
|
96
|
+
- 修复日志定位的最后一环:boot effect 因 `sessionCwd` 依赖变化被 cleanup 取消,`finally` 里的完成信号被跳过,open-with 永远等到 `boot=false` 超时;改为无条件发出 `boot-stage done`。同时用 `G.openWithActive` 阻止「入口工作区同步」把 open-with 打开的目标工作区改回当前会话 cwd。
|
|
97
|
+
- 把 boot 完成信号改成 React 提交后的状态(`bootStage: idle → done` 的 effect),不再在 async finally 里直接写全局标记;App 挂载时先清掉旧标记。open-with 只认「boot 已提交 + mode 可见 + overlay 已挂载」,彻底消除旧项目晚提交覆盖新工作区的窗口。
|
|
98
|
+
- 修复日志定位到的交替失败:App 组件在经典模式下已挂载时,open-with 误判为「IDE 已就绪」而没有真正进入 IDE,文件被打开在隐藏的 IDE 里。现在必须以 `mode=true + overlay 挂载 + ideBootDone` 三重条件判定就绪,否则先 `enterGufa()` 再等待。
|
|
99
|
+
- 修复冷启动真正根因:App 初始 boot 会先恢复上次项目,覆盖 open-with 刚打开的工作区,导致随后按相对路径打开文件报「路径不存在」。现在 boot 优先使用 pending 打开请求的目录,且 open-with 等 boot 完成后再打开文件;新增 `open-with boot/apply` 日志。
|
|
100
|
+
- 改为 Host 用 `fs.watch` 监听 pending 文件 + SSE `open.watch` 事件推送;客户端不再定时轮询文件,打开请求即时到达;宿主/网络断开时才做传输层重连。
|
|
101
|
+
- 修复 DSH 完全退出后冷启动打开文件的竞态:pending 请求不再被第一个轮询窗口直接消费,改为客户端处理成功后 ack 删除;未就绪的窗口不消费请求,失败会自动重试,不再出现「一次成功一次失败」的交替。
|
|
102
|
+
- 插件自动在用户目录生成并注册一个轻量「古法编程」打开器,注册常见编程/文本/HTML/Markdown 扩展名;右键「打开方式」即可选择;打开文件自动进经典 IDE、工作区为文件所在目录。默认关联需用户确认一次。
|
|
103
|
+
|
|
104
|
+
**资源管理器**:懒加载文件树、Git 状态颜色、错误/问题点、目录改动标记;
|
|
89
105
|
- **源代码管理**:
|
|
90
106
|
- **多仓库**:自动列出工作区根目录 + 一级子目录里的所有 Git 仓库(同一面板逐个切换),每个仓库独立暂存/提交/推送/拉取/切换分支/diff/Git Graph;子目录仓库摘要每 5 秒自动刷新;
|
|
91
107
|
- **分支**:本地分支 / 远程分支分组展示;点远端分支自动创建同名本地跟踪分支并切换;打开分支弹窗时后台 fetch 一次,刚推到远端的分支也能看到;
|
|
@@ -144,7 +160,7 @@ dsh plugin add dsh-gufa-code
|
|
|
144
160
|
|
|
145
161
|
## 更新日志
|
|
146
162
|
|
|
147
|
-
### 1.3.
|
|
163
|
+
### 1.3.234(最新)
|
|
148
164
|
|
|
149
165
|
**资源管理器**
|
|
150
166
|
- 点击空白处选中工作区根目录;之后新建/粘贴默认落到根目录;
|
package/client.js
CHANGED
|
@@ -16,10 +16,20 @@ const GUFA_NS = 'gufa-code'
|
|
|
16
16
|
const G = {}
|
|
17
17
|
G.NS = GUFA_NS
|
|
18
18
|
/** 客户端构建标记(排查页面是否加载了新代码时看标题栏) */
|
|
19
|
-
G.BUILD = '1.3.
|
|
19
|
+
G.BUILD = '1.3.234'
|
|
20
20
|
G.PANEL_ID = 'gufa-code'
|
|
21
21
|
/** 从哪个工作区点进来的(工作区菜单/节点按钮写入,App 启动时消费)。 */
|
|
22
22
|
G.pendingEntryWorkspace = null
|
|
23
|
+
/** 经典模式悬浮入口:优先打开当前聊天所在工作区(而不是上次保存的项目)。 */
|
|
24
|
+
G.pendingSessionEntry = false
|
|
25
|
+
/** 由 App 暴露给文件打开器使用的编辑器入口(打开指定绝对路径文件)。 */
|
|
26
|
+
G.ideOpenPaths = null
|
|
27
|
+
/** 系统「打开方式」请求的目标条目:App boot 时优先打开该工作区,避免先恢复旧项目再被覆盖。 */
|
|
28
|
+
G.pendingOpenEntries = null
|
|
29
|
+
/** 正在处理系统「打开方式」请求:期间入口工作区同步不得把项目改回当前会话 cwd。 */
|
|
30
|
+
G.openWithActive = false
|
|
31
|
+
/** App 初始 boot(恢复项目/会话)是否完成;open-with 必须等它结束再切目标工作区。 */
|
|
32
|
+
G.ideBootDone = false
|
|
23
33
|
G.ctx = null
|
|
24
34
|
G.h = h
|
|
25
35
|
G.React = React
|
|
@@ -2020,6 +2030,11 @@ html[data-gufa-ide] .liang-skin-backdrop{display:none!important}
|
|
|
2020
2030
|
background:transparent;color:var(--dsw-alias-label-primary);font:inherit;font-size:12.5px;cursor:pointer;text-align:left}
|
|
2021
2031
|
.gufa-more-entry:hover{background:var(--dsw-alias-interactive-bg-hover, var(--gufa-hover))}
|
|
2022
2032
|
.gufa-more-entry-label{flex:1 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
2033
|
+
/* 经典模式聊天右上角工具区的「古法编程」入口:只加一个显眼边框,其余保持原生 link 样式 */
|
|
2034
|
+
.gufa-header-entry{min-height:28px;flex:none;display:inline-flex;align-items:center;padding:3px 9px;border:1.5px solid var(--gufa-accent, #b13a2b);border-radius:7px;background:transparent;color:var(--dsw-alias-label-secondary, var(--gufa-text-dim));font:500 12px/1 -apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;cursor:pointer;white-space:nowrap;transition:background .15s ease,color .15s ease}
|
|
2035
|
+
.gufa-header-entry:hover{color:var(--dsw-alias-label-primary, var(--gufa-text));background:var(--dsw-alias-interactive-bg-hover, var(--gufa-hover))}
|
|
2036
|
+
.gufa-header-entry:focus-visible{outline:2px solid var(--dsw-focus-ring-color, var(--gufa-accent));outline-offset:2px}
|
|
2037
|
+
.gufa-header-entry-label{white-space:nowrap}
|
|
2023
2038
|
.gufa-menu{position:absolute;z-index:2147483000;min-width:150px;padding:5px;border-radius:9px;border:1px solid var(--gufa-border-strong);
|
|
2024
2039
|
background:var(--gufa-solid-overlay-bg);box-shadow:0 12px 30px color-mix(in srgb, #000 28%, transparent)}
|
|
2025
2040
|
.gufa-menu-item{display:flex;align-items:center;gap:8px;width:100%;height:28px;padding:0 9px;border:0;border-radius:6px;
|
|
@@ -13605,6 +13620,20 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13605
13620
|
const [workspaceSessions, setWorkspaceSessions] = useState({ status: 'idle', sessions: [], groups: [], sessionIds: [], archivedSessionIds: [] })
|
|
13606
13621
|
const [workspaceIndex, setWorkspaceIndex] = useState([])
|
|
13607
13622
|
const [recentCurrentIds, setRecentCurrentIds] = useState([])
|
|
13623
|
+
const [bootStage, setBootStage] = useState('idle')
|
|
13624
|
+
useLayoutEffect(() => {
|
|
13625
|
+
/* App 挂载是新实例:先清掉上一实例的 boot 完成标记,避免 open-with 看到陈旧 true */
|
|
13626
|
+
G.ideBootDone = false
|
|
13627
|
+
}, [])
|
|
13628
|
+
useEffect(() => {
|
|
13629
|
+
if (bootStage === 'done') {
|
|
13630
|
+
G.ideBootDone = true
|
|
13631
|
+
void G.api('dev.log', { message: 'boot-stage done' }).catch(() => {})
|
|
13632
|
+
} else if (bootStage === 'loading') {
|
|
13633
|
+
G.ideBootDone = false
|
|
13634
|
+
}
|
|
13635
|
+
return () => { if (bootStage === 'done') G.ideBootDone = false }
|
|
13636
|
+
}, [bootStage])
|
|
13608
13637
|
/* 会话运行状态(与经典侧边栏同源:uiSession.sessionStatus,含 running/pendingInteraction/completionUnread) */
|
|
13609
13638
|
const [sessionStatuses, setSessionStatuses] = useState(() => new Map())
|
|
13610
13639
|
const [sessionStatusReady, setSessionStatusReady] = useState(false)
|
|
@@ -13654,6 +13683,14 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13654
13683
|
G.projectRoot = typeof value?.root === 'string' ? value.root : null
|
|
13655
13684
|
setProject(value)
|
|
13656
13685
|
writeJson(GUFA_LS.project, value?.root ?? null)
|
|
13686
|
+
try {
|
|
13687
|
+
if ((window.__gufaProjectApplyLogs ?? 20) > 0) {
|
|
13688
|
+
window.__gufaProjectApplyLogs = (window.__gufaProjectApplyLogs ?? 20) - 1
|
|
13689
|
+
void G.api('dev.log', { message: `project-apply root=${String(value?.root ?? '')}` }).catch(() => {})
|
|
13690
|
+
}
|
|
13691
|
+
} catch {
|
|
13692
|
+
/* ignore */
|
|
13693
|
+
}
|
|
13657
13694
|
setTabs([])
|
|
13658
13695
|
setActivePath(null)
|
|
13659
13696
|
setSelection(null)
|
|
@@ -13678,8 +13715,31 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13678
13715
|
if (bootedRef.current) return
|
|
13679
13716
|
bootedRef.current = true
|
|
13680
13717
|
let cancelled = false
|
|
13718
|
+
setBootStage('idle')
|
|
13681
13719
|
void (async () => {
|
|
13682
13720
|
/* 从工作区菜单点进来的:跳过「恢复上次项目」,直接按入口工作区打开 */
|
|
13721
|
+
/* 系统「打开方式」:优先直接打开文件所在目录,绝不先恢复上次项目,避免随后覆盖 */
|
|
13722
|
+
const pendingOpenEntries = Array.isArray(G.pendingOpenEntries) ? G.pendingOpenEntries : null
|
|
13723
|
+
if (pendingOpenEntries !== null && pendingOpenEntries.length > 0) {
|
|
13724
|
+
const firstOpen = pendingOpenEntries.find((entry) => entry !== null && typeof entry === 'object' && typeof entry.path === 'string' && entry.path !== '')
|
|
13725
|
+
if (firstOpen !== undefined) {
|
|
13726
|
+
const openPath = String(firstOpen.path)
|
|
13727
|
+
const normalizedOpen = openPath.replace(/\\/g, '/')
|
|
13728
|
+
const openDir = firstOpen.kind === 'dir'
|
|
13729
|
+
? openPath
|
|
13730
|
+
: (normalizedOpen.lastIndexOf('/') > 0 ? openPath.slice(0, normalizedOpen.lastIndexOf('/')) : openPath)
|
|
13731
|
+
try {
|
|
13732
|
+
const value = await G.api('project.open', { path: openDir })
|
|
13733
|
+
if (!cancelled) applyProject(value)
|
|
13734
|
+
void G.api('dev.log', { message: `open-with boot workspace=${value.root}` }).catch(() => {})
|
|
13735
|
+
return
|
|
13736
|
+
} catch (error) {
|
|
13737
|
+
if (!cancelled) setBootError(G.errorText(error))
|
|
13738
|
+
}
|
|
13739
|
+
}
|
|
13740
|
+
}
|
|
13741
|
+
const preferSessionWorkspace = G.pendingSessionEntry === true
|
|
13742
|
+
if (preferSessionWorkspace) G.pendingSessionEntry = false
|
|
13683
13743
|
const pendingEntry = G.pendingEntryWorkspace
|
|
13684
13744
|
if (pendingEntry !== null && pendingEntry !== undefined && typeof pendingEntry.key === 'string') {
|
|
13685
13745
|
try {
|
|
@@ -13695,6 +13755,15 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13695
13755
|
/* 回落到恢复上次项目 */
|
|
13696
13756
|
}
|
|
13697
13757
|
}
|
|
13758
|
+
if (preferSessionWorkspace && typeof sessionCwd === 'string' && sessionCwd !== '') {
|
|
13759
|
+
try {
|
|
13760
|
+
const value = await G.api('project.open', { path: sessionCwd })
|
|
13761
|
+
if (!cancelled) applyProject(value)
|
|
13762
|
+
} catch (error) {
|
|
13763
|
+
if (!cancelled) setBootError(G.errorText(error))
|
|
13764
|
+
}
|
|
13765
|
+
return
|
|
13766
|
+
}
|
|
13698
13767
|
const saved = floatState !== null && floatState !== undefined && typeof floatState.projectRoot === 'string' && floatState.projectRoot !== ''
|
|
13699
13768
|
? floatState.projectRoot
|
|
13700
13769
|
: readJson(GUFA_LS.project)
|
|
@@ -13718,7 +13787,10 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13718
13787
|
if (!cancelled) setBootError(G.errorText(error))
|
|
13719
13788
|
}
|
|
13720
13789
|
}
|
|
13721
|
-
})()
|
|
13790
|
+
})().catch(() => {}).finally(() => {
|
|
13791
|
+
/* 依赖变化触发的 cleanup 不代表组件卸载;这里必须无条件发出 boot 完成信号 */
|
|
13792
|
+
setBootStage('done')
|
|
13793
|
+
})
|
|
13722
13794
|
return () => {
|
|
13723
13795
|
cancelled = true
|
|
13724
13796
|
}
|
|
@@ -13871,7 +13943,7 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13871
13943
|
/* ignore */
|
|
13872
13944
|
}
|
|
13873
13945
|
setActivePath(existing.path)
|
|
13874
|
-
return
|
|
13946
|
+
return true
|
|
13875
13947
|
}
|
|
13876
13948
|
try {
|
|
13877
13949
|
const doc = await G.api('fs.read', { root: project.root, path })
|
|
@@ -13902,8 +13974,10 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13902
13974
|
))
|
|
13903
13975
|
}
|
|
13904
13976
|
setActivePath(gufaNormalizeProjectPath(doc.path ?? path))
|
|
13977
|
+
return true
|
|
13905
13978
|
} catch (error) {
|
|
13906
13979
|
setBanner(G.errorText(error))
|
|
13980
|
+
return false
|
|
13907
13981
|
}
|
|
13908
13982
|
}, [project, tabs])
|
|
13909
13983
|
|
|
@@ -13915,9 +13989,10 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13915
13989
|
|
|
13916
13990
|
/* 从搜索/定位结果打开文件并跳到指定行列(等 tab 切换完成后再触发编辑器 reveal) */
|
|
13917
13991
|
const openFileAt = useCallback(async (rawPath, line, column) => {
|
|
13918
|
-
if (typeof rawPath !== 'string' || rawPath === '') return
|
|
13992
|
+
if (typeof rawPath !== 'string' || rawPath === '') return false
|
|
13919
13993
|
const path = gufaNormalizeProjectPath(rawPath)
|
|
13920
|
-
await openFile(path)
|
|
13994
|
+
const opened = await openFile(path)
|
|
13995
|
+
if (opened === false) return false
|
|
13921
13996
|
const targetLine = Number.isFinite(Number(line)) ? Math.max(1, Math.trunc(Number(line))) : 1
|
|
13922
13997
|
const targetColumn = Number.isFinite(Number(column)) ? Math.max(1, Math.trunc(Number(column))) : 1
|
|
13923
13998
|
const target = { path, line: targetLine, column: targetColumn }
|
|
@@ -13933,6 +14008,75 @@ G.GufaApp = function GufaApp(props) {
|
|
|
13933
14008
|
pendingOpenAtRef.current = target
|
|
13934
14009
|
}, [openFile])
|
|
13935
14010
|
|
|
14011
|
+
/* 系统文件「打开方式」:暴露入口给 99-main 的 pending 轮询(打开指定绝对路径文件)。 */
|
|
14012
|
+
const projectRef = useRef(project)
|
|
14013
|
+
projectRef.current = project
|
|
14014
|
+
const applyProjectRef = useRef(applyProject)
|
|
14015
|
+
applyProjectRef.current = applyProject
|
|
14016
|
+
const openFileAtRef = useRef(openFileAt)
|
|
14017
|
+
openFileAtRef.current = openFileAt
|
|
14018
|
+
const openFileRef = useRef(openFile)
|
|
14019
|
+
openFileRef.current = openFile
|
|
14020
|
+
useEffect(() => {
|
|
14021
|
+
const sleep = (ms) => new Promise((resolve) => { window.setTimeout(resolve, ms) })
|
|
14022
|
+
const normalize = (value) => String(value ?? '').replace(/\\/g, '/').replace(/\/+$/, '')
|
|
14023
|
+
const dirOf = (value) => {
|
|
14024
|
+
const normalized = String(value ?? '').replace(/\\/g, '/')
|
|
14025
|
+
const index = normalized.lastIndexOf('/')
|
|
14026
|
+
return index > 0 ? String(value).slice(0, value.length - (normalized.length - index)) : value
|
|
14027
|
+
}
|
|
14028
|
+
const handler = async (entries) => {
|
|
14029
|
+
const list = Array.isArray(entries)
|
|
14030
|
+
? entries.filter((entry) => entry !== null && typeof entry === 'object' && typeof entry.path === 'string' && entry.path !== '')
|
|
14031
|
+
: []
|
|
14032
|
+
if (list.length === 0) return false
|
|
14033
|
+
const first = list[0]
|
|
14034
|
+
const targetDir = first.kind === 'dir' ? first.path : dirOf(first.path)
|
|
14035
|
+
const targetRoot = normalize(targetDir)
|
|
14036
|
+
if (normalize(projectRef.current?.root) !== targetRoot) {
|
|
14037
|
+
try {
|
|
14038
|
+
const value = await G.api('project.open', { path: targetDir })
|
|
14039
|
+
applyProjectRef.current(value)
|
|
14040
|
+
/* 等 React 把项目状态提交;如果 boot 恢复旧项目还在覆盖,继续等目标根稳定 */
|
|
14041
|
+
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
14042
|
+
await sleep(150)
|
|
14043
|
+
if (normalize(projectRef.current?.root) === targetRoot) break
|
|
14044
|
+
}
|
|
14045
|
+
if (normalize(projectRef.current?.root) !== targetRoot) {
|
|
14046
|
+
console.error('[gufa-code] 目标工作区未稳定', targetRoot, projectRef.current?.root)
|
|
14047
|
+
return false
|
|
14048
|
+
}
|
|
14049
|
+
} catch (error) {
|
|
14050
|
+
console.error('[gufa-code] 打开文件所在工作区失败', error)
|
|
14051
|
+
return false
|
|
14052
|
+
}
|
|
14053
|
+
}
|
|
14054
|
+
const root = normalize(projectRef.current?.root ?? targetRoot)
|
|
14055
|
+
void G.api('dev.log', { message: `open-with apply dir=${targetDir} root=${root}` }).catch(() => {})
|
|
14056
|
+
const files = list.filter((entry) => entry.kind !== 'dir')
|
|
14057
|
+
for (let index = 0; index < files.length; index += 1) {
|
|
14058
|
+
const absolute = normalize(files[index].path)
|
|
14059
|
+
const rel = absolute.startsWith(`${root}/`) ? absolute.slice(root.length + 1) : absolute.split('/').pop()
|
|
14060
|
+
if (typeof rel !== 'string' || rel === '' || rel.includes('..')) continue
|
|
14061
|
+
void G.api('dev.log', { message: `open-with open rel=${rel}` }).catch(() => {})
|
|
14062
|
+
const opened = index === 0
|
|
14063
|
+
? await openFileAtRef.current(rel, 1, 1)
|
|
14064
|
+
: await openFileRef.current(rel)
|
|
14065
|
+
if (opened === false) {
|
|
14066
|
+
void G.api('dev.log', { message: `open-with open-failed rel=${rel}` }).catch(() => {})
|
|
14067
|
+
return false
|
|
14068
|
+
}
|
|
14069
|
+
}
|
|
14070
|
+
G.pendingOpenEntries = null
|
|
14071
|
+
return true
|
|
14072
|
+
}
|
|
14073
|
+
G.ideOpenPaths = handler
|
|
14074
|
+
return () => {
|
|
14075
|
+
if (G.ideOpenPaths === handler) G.ideOpenPaths = null
|
|
14076
|
+
}
|
|
14077
|
+
}, [])
|
|
14078
|
+
|
|
14079
|
+
|
|
13936
14080
|
/* 未打开文件跳转:activePath 切到目标文件后(子组件编辑器 effect 已先行)再发 reveal */
|
|
13937
14081
|
useEffect(() => {
|
|
13938
14082
|
const pending = pendingOpenAtRef.current
|
|
@@ -15081,6 +15225,11 @@ G.GufaApp = function GufaApp(props) {
|
|
|
15081
15225
|
const entryWorkspaceSyncedRef = useRef(false)
|
|
15082
15226
|
useEffect(() => {
|
|
15083
15227
|
if (entryWorkspaceSyncedRef.current) return
|
|
15228
|
+
/* 系统「打开方式」正在处理:工作区由 open-with 决定,入口同步不得改回会话 cwd */
|
|
15229
|
+
if (G.openWithActive === true) {
|
|
15230
|
+
entryWorkspaceSyncedRef.current = true
|
|
15231
|
+
return
|
|
15232
|
+
}
|
|
15084
15233
|
/* 实例窗口:保持空工作区,不跟随主窗口/DSH 当前会话自动打开项目 */
|
|
15085
15234
|
if (G.instanceBoot === true) {
|
|
15086
15235
|
entryWorkspaceSyncedRef.current = true
|
|
@@ -15091,14 +15240,12 @@ G.GufaApp = function GufaApp(props) {
|
|
|
15091
15240
|
entryWorkspaceSyncedRef.current = true
|
|
15092
15241
|
return
|
|
15093
15242
|
}
|
|
15094
|
-
if (project === null && (G.pendingEntryWorkspace === null || G.pendingEntryWorkspace === undefined)) return
|
|
15095
15243
|
const pending = G.pendingEntryWorkspace
|
|
15096
15244
|
if (pending === null || pending === undefined || typeof pending.key !== 'string') {
|
|
15097
|
-
|
|
15245
|
+
/* 没有显式入口(例如重启 DSH 自动恢复 IDE):保留 boot 已恢复的「上次工作区」,
|
|
15246
|
+
不要改回当前会话 cwd —— 否则 open-with 打开的工作区永远不会被记住。 */
|
|
15098
15247
|
entryWorkspaceSyncedRef.current = true
|
|
15099
|
-
|
|
15100
|
-
void openProjectPath(sessionCwd).catch((error) => setBanner(G.errorText(error)))
|
|
15101
|
-
}
|
|
15248
|
+
void G.api('dev.log', { message: `entry-sync keep-saved project=${project !== null && typeof project.root === 'string' ? project.root : 'null'} sessionCwd=${typeof sessionCwd === 'string' ? sessionCwd : ''}` }).catch(() => {})
|
|
15102
15249
|
return
|
|
15103
15250
|
}
|
|
15104
15251
|
entryWorkspaceSyncedRef.current = true
|
|
@@ -17241,6 +17388,162 @@ G.installWorkspaceMenuEntry = function installWorkspaceMenuEntry(ctx) {
|
|
|
17241
17388
|
}
|
|
17242
17389
|
}
|
|
17243
17390
|
|
|
17391
|
+
/** 经典模式聊天标题行的文本入口:和「智能体团队」同款 link 样式,但文案直接叫「古法编程」。 */
|
|
17392
|
+
G.GufaHeaderEntry = function GufaHeaderEntry() {
|
|
17393
|
+
const ideVisible = G.useMode()
|
|
17394
|
+
if (ideVisible === true) return null
|
|
17395
|
+
return h(
|
|
17396
|
+
'button',
|
|
17397
|
+
{
|
|
17398
|
+
type: 'button',
|
|
17399
|
+
className: 'gufa-header-entry',
|
|
17400
|
+
title: '进入古法编程(当前聊天的工作区)',
|
|
17401
|
+
'aria-label': '古法编程',
|
|
17402
|
+
onClick: (event) => {
|
|
17403
|
+
try {
|
|
17404
|
+
event.preventDefault()
|
|
17405
|
+
event.stopPropagation()
|
|
17406
|
+
} catch {
|
|
17407
|
+
/* ignore */
|
|
17408
|
+
}
|
|
17409
|
+
try {
|
|
17410
|
+
G.pendingSessionEntry = true
|
|
17411
|
+
G.enterGufa()
|
|
17412
|
+
} catch (error) {
|
|
17413
|
+
console.error('[gufa-code] 进入古法编程失败', error)
|
|
17414
|
+
}
|
|
17415
|
+
},
|
|
17416
|
+
},
|
|
17417
|
+
h('span', { className: 'gufa-header-entry-label' }, '古法编程'),
|
|
17418
|
+
)
|
|
17419
|
+
}
|
|
17420
|
+
|
|
17421
|
+
/** 文件关联打开:订阅 Host 的 open.watch 事件(不再轮询文件),处理成功后 ack。 */
|
|
17422
|
+
G.installOpenWithHandler = function installOpenWithHandler(ctx) {
|
|
17423
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') return () => {}
|
|
17424
|
+
let skip = false
|
|
17425
|
+
try {
|
|
17426
|
+
skip = typeof G.isFloatMode === 'function' && G.isFloatMode() === true
|
|
17427
|
+
if (!skip && /[?&]gufa-instance=1/.test(String(location.search || ''))) skip = true
|
|
17428
|
+
if (!skip && /welcome|update/i.test(String(location.href || ''))) skip = true
|
|
17429
|
+
} catch {
|
|
17430
|
+
skip = false
|
|
17431
|
+
}
|
|
17432
|
+
if (skip) return () => {}
|
|
17433
|
+
let stopped = false
|
|
17434
|
+
let busy = false
|
|
17435
|
+
let lastToken = ''
|
|
17436
|
+
let abortStream = null
|
|
17437
|
+
let reconnectTimer = 0
|
|
17438
|
+
let retryTimer = 0
|
|
17439
|
+
let logs = 20
|
|
17440
|
+
const log = (message) => {
|
|
17441
|
+
if (logs <= 0) return
|
|
17442
|
+
logs -= 1
|
|
17443
|
+
try { void G.api('dev.log', { message }).catch(() => {}) } catch { /* ignore */ }
|
|
17444
|
+
}
|
|
17445
|
+
const sleep = (ms) => new Promise((resolve) => { window.setTimeout(resolve, ms) })
|
|
17446
|
+
const ideReady = () => {
|
|
17447
|
+
try {
|
|
17448
|
+
return G.mode.get() === true
|
|
17449
|
+
&& G.ideBootDone === true
|
|
17450
|
+
&& typeof G.ideOpenPaths === 'function'
|
|
17451
|
+
&& document.querySelector('.gufa-overlay-root') !== null
|
|
17452
|
+
} catch {
|
|
17453
|
+
return false
|
|
17454
|
+
}
|
|
17455
|
+
}
|
|
17456
|
+
const waitIdeReady = async (maxAttempts = 100) => {
|
|
17457
|
+
for (let attempt = 0; attempt < maxAttempts && stopped !== true; attempt += 1) {
|
|
17458
|
+
if (ideReady()) return true
|
|
17459
|
+
await sleep(200)
|
|
17460
|
+
}
|
|
17461
|
+
return false
|
|
17462
|
+
}
|
|
17463
|
+
const openEntries = async (entries) => {
|
|
17464
|
+
if (!ideReady()) {
|
|
17465
|
+
log(`open-with enter ide mode=${G.mode.get() === true} hook=${typeof G.ideOpenPaths === 'function'} boot=${G.ideBootDone === true}`)
|
|
17466
|
+
try {
|
|
17467
|
+
G.pendingOpenEntries = entries
|
|
17468
|
+
G.pendingSessionEntry = false
|
|
17469
|
+
G.enterGufa()
|
|
17470
|
+
} catch {
|
|
17471
|
+
return false
|
|
17472
|
+
}
|
|
17473
|
+
/* 必须等 IDE 覆盖层真正挂载 + App 初始 boot 完成,否则旧项目会覆盖刚打开的工作区 */
|
|
17474
|
+
if (!(await waitIdeReady())) return false
|
|
17475
|
+
}
|
|
17476
|
+
const result = await G.ideOpenPaths(entries)
|
|
17477
|
+
return result !== false
|
|
17478
|
+
}
|
|
17479
|
+
const handlePending = async (event) => {
|
|
17480
|
+
if (stopped || busy) return
|
|
17481
|
+
const entries = event !== null && event !== undefined && Array.isArray(event.entries) ? event.entries : []
|
|
17482
|
+
const token = event !== null && event !== undefined && typeof event.token === 'string' ? event.token : ''
|
|
17483
|
+
if (entries.length === 0 || token === '' || token === lastToken) return
|
|
17484
|
+
G.openWithActive = true
|
|
17485
|
+
busy = true
|
|
17486
|
+
log(`open-with pending token=${token} entries=${entries.length} paths=${entries.map((entry) => entry.path).join('|')}`)
|
|
17487
|
+
const ok = await openEntries(entries)
|
|
17488
|
+
busy = false
|
|
17489
|
+
if (stopped) return
|
|
17490
|
+
if (ok) {
|
|
17491
|
+
lastToken = token
|
|
17492
|
+
await G.api('open.ack', { token }).catch(() => {})
|
|
17493
|
+
G.openWithActive = false
|
|
17494
|
+
log(`open-with ack token=${token}`)
|
|
17495
|
+
return
|
|
17496
|
+
}
|
|
17497
|
+
log(`open-with retry token=${token}`)
|
|
17498
|
+
if (retryTimer !== 0) window.clearTimeout(retryTimer)
|
|
17499
|
+
retryTimer = window.setTimeout(() => {
|
|
17500
|
+
retryTimer = 0
|
|
17501
|
+
void handlePending(event)
|
|
17502
|
+
}, 3000)
|
|
17503
|
+
}
|
|
17504
|
+
const scheduleReconnect = (delay = 1500) => {
|
|
17505
|
+
if (stopped) return
|
|
17506
|
+
if (reconnectTimer !== 0) return
|
|
17507
|
+
reconnectTimer = window.setTimeout(() => {
|
|
17508
|
+
reconnectTimer = 0
|
|
17509
|
+
connect()
|
|
17510
|
+
}, delay)
|
|
17511
|
+
}
|
|
17512
|
+
const connect = () => {
|
|
17513
|
+
if (stopped) return
|
|
17514
|
+
try {
|
|
17515
|
+
abortStream = G.stream('open.watch', {}, (event) => {
|
|
17516
|
+
if (event === null || event === undefined) return
|
|
17517
|
+
if (event.type === 'pending') void handlePending(event)
|
|
17518
|
+
else if (event.type === 'error') scheduleReconnect()
|
|
17519
|
+
}, () => scheduleReconnect())
|
|
17520
|
+
log('open-with stream connected')
|
|
17521
|
+
} catch {
|
|
17522
|
+
scheduleReconnect(3000)
|
|
17523
|
+
}
|
|
17524
|
+
}
|
|
17525
|
+
const start = () => {
|
|
17526
|
+
if (stopped) return
|
|
17527
|
+
void G.api('open.ensure', {}).catch(() => {})
|
|
17528
|
+
connect()
|
|
17529
|
+
}
|
|
17530
|
+
try {
|
|
17531
|
+
if (typeof ctx?.inject === 'function') ctx.inject(['layout'], () => start())
|
|
17532
|
+
else start()
|
|
17533
|
+
} catch {
|
|
17534
|
+
start()
|
|
17535
|
+
}
|
|
17536
|
+
return () => {
|
|
17537
|
+
stopped = true
|
|
17538
|
+
if (reconnectTimer !== 0) window.clearTimeout(reconnectTimer)
|
|
17539
|
+
if (retryTimer !== 0) window.clearTimeout(retryTimer)
|
|
17540
|
+
reconnectTimer = 0
|
|
17541
|
+
retryTimer = 0
|
|
17542
|
+
try { if (typeof abortStream === 'function') abortStream() } catch { /* ignore */ }
|
|
17543
|
+
abortStream = null
|
|
17544
|
+
}
|
|
17545
|
+
}
|
|
17546
|
+
|
|
17244
17547
|
/** 「更多」菜单里的入口行:点击进入古法编程(从当前工作区进入)。 */
|
|
17245
17548
|
G.MoreMenuEntry = function MoreMenuEntry(props) {
|
|
17246
17549
|
return h(
|
|
@@ -17831,6 +18134,14 @@ const GUFA_PLUGIN = {
|
|
|
17831
18134
|
}
|
|
17832
18135
|
}, 2500)
|
|
17833
18136
|
|
|
18137
|
+
// 系统文件「打开方式」:自动安装打开器并轮询 pending 文件请求。
|
|
18138
|
+
try {
|
|
18139
|
+
const disposeOpenWith = G.installOpenWithHandler(ctx)
|
|
18140
|
+
if (typeof disposeOpenWith === 'function') ctx.effect(() => disposeOpenWith, 'gufa-code: open-with handler')
|
|
18141
|
+
} catch (error) {
|
|
18142
|
+
console.error('[gufa-code] 文件关联打开器安装失败', error)
|
|
18143
|
+
}
|
|
18144
|
+
|
|
17834
18145
|
// 入口:工作区行的「...」菜单里注入「古法编程」一行。
|
|
17835
18146
|
try {
|
|
17836
18147
|
const disposeMenuEntry = G.installWorkspaceMenuEntry(ctx)
|
|
@@ -17839,6 +18150,19 @@ const GUFA_PLUGIN = {
|
|
|
17839
18150
|
console.error('[gufa-code] 安装工作区菜单入口失败', error)
|
|
17840
18151
|
}
|
|
17841
18152
|
|
|
18153
|
+
// 经典模式聊天右上角工具区:在头像菜单左侧的空位放「古法编程」文本入口。
|
|
18154
|
+
try {
|
|
18155
|
+
ctx.inject(['slots'], (scope) => {
|
|
18156
|
+
scope.slots.inject('conversation.session.header.utilities', () => scope.slots.register({
|
|
18157
|
+
name: 'conversation.session.header.utilities',
|
|
18158
|
+
id: 'gufa-code-entry',
|
|
18159
|
+
order: -100,
|
|
18160
|
+
}, G.GufaHeaderEntry))
|
|
18161
|
+
})
|
|
18162
|
+
} catch (error) {
|
|
18163
|
+
console.error('[gufa-code] 聊天头部入口注册失败', error)
|
|
18164
|
+
}
|
|
18165
|
+
|
|
17842
18166
|
// 面板注册:入口注入到工作区行的「...」菜单里(DSH 未提供该菜单的 slot)。
|
|
17843
18167
|
ctx.inject(['slots'], (scope) => {
|
|
17844
18168
|
// main key 仅作安全网:若侧边栏把主面板切到 gufa-code(点击拦截失效时),
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 系统文件关联("古法编程 Open")。
|
|
3
|
+
*
|
|
4
|
+
* 目标:安装插件后,用户在 Finder/资源管理器里右键文件 →「打开方式」能看到「古法编程」;
|
|
5
|
+
* 选中后唤起 DSH,插件读到 pending 请求 → 自动进入 IDE(工作区 = 文件所在目录)并打开文件。
|
|
6
|
+
*
|
|
7
|
+
* macOS 实现:在 ~/Applications 生成一个极薄的 .app 包装(AppleScript applet),
|
|
8
|
+
* 注册 LaunchServices 文档类型;不修改 DSH 本体、不需要管理员权限。
|
|
9
|
+
* Windows/Linux 的实现留待后续补充(当前 route 返回 supported:false)。
|
|
10
|
+
*/
|
|
11
|
+
import { execFile } from 'node:child_process'
|
|
12
|
+
import { access, cp, mkdir, readFile, realpath, readdir, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
13
|
+
import { constants, readFileSync, watch } from 'node:fs'
|
|
14
|
+
import { homedir, tmpdir } from 'node:os'
|
|
15
|
+
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
16
|
+
import { fileURLToPath } from 'node:url'
|
|
17
|
+
import { promisify } from 'node:util'
|
|
18
|
+
import { ApiError, badRequest } from './context.js'
|
|
19
|
+
import { debugLog } from './dev.js'
|
|
20
|
+
|
|
21
|
+
const run = promisify(execFile)
|
|
22
|
+
const PLUGIN_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
23
|
+
const PLUGIN_VERSION = (() => {
|
|
24
|
+
try {
|
|
25
|
+
return String(JSON.parse(readFileSync(join(PLUGIN_ROOT, 'package.json'), 'utf8')).version ?? '0')
|
|
26
|
+
} catch {
|
|
27
|
+
return '0'
|
|
28
|
+
}
|
|
29
|
+
})()
|
|
30
|
+
const PENDING_DIR = join(homedir(), '.dsh')
|
|
31
|
+
const PENDING_FILE = join(PENDING_DIR, 'gufa-open-pending.txt')
|
|
32
|
+
const OPENER_ID = 'com.gufa-code.opener'
|
|
33
|
+
const OPENER_NAME = '古法编程'
|
|
34
|
+
const OPENER_APP = join(homedir(), 'Applications', 'GufaCodeOpen.app')
|
|
35
|
+
|
|
36
|
+
/** 注册到「打开方式」的文件扩展名:常见的编程/文本/HTML/Markdown。 */
|
|
37
|
+
export const OPEN_WITH_EXTENSIONS = [
|
|
38
|
+
'md', 'markdown', 'mdx', 'txt', 'text', 'log', 'rst', 'adoc',
|
|
39
|
+
'json', 'jsonc', 'json5', 'yml', 'yaml', 'toml', 'ini', 'conf', 'env',
|
|
40
|
+
'js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'mts', 'cts', 'vue', 'svelte', 'astro',
|
|
41
|
+
'html', 'htm', 'xhtml', 'css', 'scss', 'sass', 'less',
|
|
42
|
+
'py', 'pyw', 'rb', 'php', 'phtml', 'go', 'rs', 'java', 'kt', 'kts', 'scala', 'swift', 'm', 'mm',
|
|
43
|
+
'c', 'h', 'cc', 'cpp', 'cxx', 'hpp', 'hxx', 'cs', 'fs', 'vb',
|
|
44
|
+
'sh', 'bash', 'zsh', 'fish', 'ps1', 'bat', 'cmd',
|
|
45
|
+
'sql', 'graphql', 'gql', 'proto', 'tf', 'hcl', 'lua', 'pl', 'pm', 'r', 'dart', 'ex', 'exs',
|
|
46
|
+
'xml', 'svg', 'csv', 'tsv', 'gitignore', 'editorconfig',
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
let installPromise = null
|
|
50
|
+
|
|
51
|
+
async function exists(path) {
|
|
52
|
+
try {
|
|
53
|
+
await access(path, constants.F_OK)
|
|
54
|
+
return true
|
|
55
|
+
} catch {
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function plistJson(path) {
|
|
61
|
+
const { stdout } = await run('plutil', ['-convert', 'json', '-o', '-', path], { timeout: 10000, maxBuffer: 8 * 1024 * 1024 })
|
|
62
|
+
return JSON.parse(stdout)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function writePlist(path, value) {
|
|
66
|
+
const jsonPath = `${path}.json`
|
|
67
|
+
await writeFile(jsonPath, JSON.stringify(value), 'utf8')
|
|
68
|
+
await run('plutil', ['-convert', 'xml1', jsonPath], { timeout: 10000 })
|
|
69
|
+
await rename(jsonPath, path)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 生成 AppleScript applet:接收 Finder 的打开事件,把路径写入 pending 并唤起 DSH。 */
|
|
73
|
+
function openerScript() {
|
|
74
|
+
return [
|
|
75
|
+
'on open theFiles',
|
|
76
|
+
' set homePath to POSIX path of (path to home folder)',
|
|
77
|
+
' set dirPath to homePath & ".dsh"',
|
|
78
|
+
' set outPath to dirPath & "/gufa-open-pending.txt"',
|
|
79
|
+
' try',
|
|
80
|
+
' do shell script "mkdir -p " & quoted form of dirPath',
|
|
81
|
+
' set fh to open for access (POSIX file outPath) with write permission',
|
|
82
|
+
' set eof fh to 0',
|
|
83
|
+
' repeat with f in theFiles',
|
|
84
|
+
' write ((POSIX path of f) & linefeed) to fh as «class utf8»',
|
|
85
|
+
' end repeat',
|
|
86
|
+
' close access fh',
|
|
87
|
+
' end try',
|
|
88
|
+
' try',
|
|
89
|
+
' do shell script "open -b com.deepseek.dsh \'dsh://open\'"',
|
|
90
|
+
' on error',
|
|
91
|
+
' try',
|
|
92
|
+
' do shell script "open -a \'DeepSeek Harness\' \'dsh://open\'"',
|
|
93
|
+
' end try',
|
|
94
|
+
' end try',
|
|
95
|
+
'end open',
|
|
96
|
+
].join('\r')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function buildOpenerApp() {
|
|
100
|
+
const tmpScript = join(tmpdir(), `gufa-code-open-${process.pid}-${Date.now()}.applescript`)
|
|
101
|
+
const tmpApp = join(tmpdir(), `GufaCodeOpen-${process.pid}-${Date.now()}.app`)
|
|
102
|
+
try {
|
|
103
|
+
await writeFile(tmpScript, openerScript(), 'utf8')
|
|
104
|
+
await rm(tmpApp, { recursive: true, force: true })
|
|
105
|
+
await run('osacompile', ['-o', tmpApp, tmpScript], { timeout: 30000, maxBuffer: 8 * 1024 * 1024 })
|
|
106
|
+
const infoPath = join(tmpApp, 'Contents', 'Info.plist')
|
|
107
|
+
const info = await plistJson(infoPath)
|
|
108
|
+
info.CFBundleIdentifier = OPENER_ID
|
|
109
|
+
info.CFBundleName = OPENER_NAME
|
|
110
|
+
info.CFBundleDisplayName = OPENER_NAME
|
|
111
|
+
info.CFBundleShortVersionString = PLUGIN_VERSION
|
|
112
|
+
info.CFBundleVersion = PLUGIN_VERSION
|
|
113
|
+
info.LSUIElement = true
|
|
114
|
+
info.NSHumanReadableCopyright = '古法编程 · AI 辅助(dsh-gufa-code)'
|
|
115
|
+
info.CFBundleDocumentTypes = [{
|
|
116
|
+
CFBundleTypeName: '古法编程文档',
|
|
117
|
+
CFBundleTypeRole: 'Editor',
|
|
118
|
+
LSHandlerRank: 'Alternate',
|
|
119
|
+
CFBundleTypeExtensions: OPEN_WITH_EXTENSIONS,
|
|
120
|
+
}]
|
|
121
|
+
await writePlist(infoPath, info)
|
|
122
|
+
try {
|
|
123
|
+
await run('codesign', ['--force', '--deep', '-s', '-', tmpApp], { timeout: 30000 })
|
|
124
|
+
} catch {
|
|
125
|
+
/* ad-hoc 签名失败不阻塞 */
|
|
126
|
+
}
|
|
127
|
+
await mkdir(dirname(OPENER_APP), { recursive: true })
|
|
128
|
+
await rm(OPENER_APP, { recursive: true, force: true })
|
|
129
|
+
await cp(tmpApp, OPENER_APP, { recursive: true })
|
|
130
|
+
} finally {
|
|
131
|
+
await rm(tmpScript, { force: true }).catch(() => {})
|
|
132
|
+
await rm(tmpApp, { recursive: true, force: true }).catch(() => {})
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function lsregister(path, unregister = false) {
|
|
137
|
+
const candidates = [
|
|
138
|
+
'/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister',
|
|
139
|
+
'/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister',
|
|
140
|
+
]
|
|
141
|
+
let binary = ''
|
|
142
|
+
for (const candidate of candidates) {
|
|
143
|
+
if (await exists(candidate)) {
|
|
144
|
+
binary = candidate
|
|
145
|
+
break
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (binary === '') return false
|
|
149
|
+
await run(binary, unregister ? ['-u', path] : ['-f', path], { timeout: 20000, maxBuffer: 8 * 1024 * 1024 }).catch(() => undefined)
|
|
150
|
+
return true
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 确保 macOS 打开器已安装/更新(幂等)。 */
|
|
154
|
+
async function ensureMacOpener() {
|
|
155
|
+
if (process.platform !== 'darwin') return { supported: false, installed: false, reason: '目前仅支持 macOS' }
|
|
156
|
+
if (installPromise !== null) return installPromise
|
|
157
|
+
installPromise = (async () => {
|
|
158
|
+
let installed = false
|
|
159
|
+
let currentVersion = ''
|
|
160
|
+
if (await exists(join(OPENER_APP, 'Contents', 'Info.plist'))) {
|
|
161
|
+
installed = true
|
|
162
|
+
try {
|
|
163
|
+
currentVersion = String((await plistJson(join(OPENER_APP, 'Contents', 'Info.plist'))).CFBundleVersion ?? '')
|
|
164
|
+
} catch {
|
|
165
|
+
currentVersion = ''
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (installed !== true || currentVersion !== PLUGIN_VERSION) {
|
|
169
|
+
await buildOpenerApp()
|
|
170
|
+
installed = true
|
|
171
|
+
}
|
|
172
|
+
await lsregister(OPENER_APP)
|
|
173
|
+
return { supported: true, installed: true, appPath: OPENER_APP, version: PLUGIN_VERSION, extensions: OPEN_WITH_EXTENSIONS.length }
|
|
174
|
+
})().finally(() => {
|
|
175
|
+
installPromise = null
|
|
176
|
+
})
|
|
177
|
+
return installPromise
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** 读取 pending 打开请求;不删除,等客户端处理成功后 open.ack 再删(避免启动竞态丢请求)。 */
|
|
181
|
+
async function readPending() {
|
|
182
|
+
if (!(await exists(PENDING_FILE))) return { token: '', entries: [] }
|
|
183
|
+
const info = await stat(PENDING_FILE).catch(() => null)
|
|
184
|
+
if (info === null) return { token: '', entries: [] }
|
|
185
|
+
/* 超过 2 分钟的旧请求直接过期,避免反复打开很久以前误触的文件 */
|
|
186
|
+
if (Date.now() - info.mtimeMs > 120000) {
|
|
187
|
+
await rm(PENDING_FILE, { force: true }).catch(() => {})
|
|
188
|
+
return { token: '', entries: [] }
|
|
189
|
+
}
|
|
190
|
+
const token = `${Math.round(info.mtimeMs)}:${info.size}`
|
|
191
|
+
let text = ''
|
|
192
|
+
try {
|
|
193
|
+
text = await readFile(PENDING_FILE, 'utf8')
|
|
194
|
+
} catch {
|
|
195
|
+
text = ''
|
|
196
|
+
}
|
|
197
|
+
const paths = text.split(/\r?\n/).map((line) => line.trim()).filter((line) => line !== '' && isAbsolute(line))
|
|
198
|
+
const entries = []
|
|
199
|
+
for (const path of paths.slice(0, 20)) {
|
|
200
|
+
/* 打开器拿到的路径可能含 symlink/大小写差异;项目根是 realpath,
|
|
201
|
+
这里统一 canonical 化,客户端才能算出正确的相对路径。 */
|
|
202
|
+
const canonical = await realpath(path).catch(() => path)
|
|
203
|
+
const detail = await stat(canonical).catch(() => null)
|
|
204
|
+
if (detail === null) continue
|
|
205
|
+
entries.push({ path: canonical, kind: detail.isDirectory() ? 'dir' : 'file' })
|
|
206
|
+
}
|
|
207
|
+
return { token, entries }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** 客户端处理成功后确认该请求,Host 才删除 pending 文件。 */
|
|
211
|
+
async function ackPending(token) {
|
|
212
|
+
const info = await stat(PENDING_FILE).catch(() => null)
|
|
213
|
+
if (info === null) return { ok: false, reason: 'missing' }
|
|
214
|
+
const current = `${Math.round(info.mtimeMs)}:${info.size}`
|
|
215
|
+
if (typeof token === 'string' && token !== '' && token !== current) return { ok: false, reason: 'stale' }
|
|
216
|
+
await rm(PENDING_FILE, { force: true }).catch(() => {})
|
|
217
|
+
return { ok: true }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function register(router) {
|
|
221
|
+
router.method('open.pending', async () => {
|
|
222
|
+
if (process.platform !== 'darwin') return { supported: false, token: '', entries: [] }
|
|
223
|
+
const pending = await readPending()
|
|
224
|
+
if (pending.entries.length > 0) {
|
|
225
|
+
try { debugLog(`open-pending entries=${pending.entries.length} token=${pending.token} paths=${pending.entries.map((entry) => entry.path).join('|')}`) } catch { /* ignore */ }
|
|
226
|
+
}
|
|
227
|
+
return { supported: true, ...pending }
|
|
228
|
+
})
|
|
229
|
+
router.method('open.ack', async (body) => {
|
|
230
|
+
if (process.platform !== 'darwin') return { ok: false }
|
|
231
|
+
const result = await ackPending(typeof body?.token === 'string' ? body.token : '')
|
|
232
|
+
try { debugLog(`open-ack token=${String(body?.token ?? '')} ok=${result.ok === true} reason=${result.reason ?? ''}`) } catch { /* ignore */ }
|
|
233
|
+
return result
|
|
234
|
+
})
|
|
235
|
+
/* 事件推送:客户端连上后先发当前 pending,再 fs.watch 监听;不再由客户端轮询文件。 */
|
|
236
|
+
router.stream('open.watch', async (body, context = {}) => {
|
|
237
|
+
const send = typeof context.send === 'function' ? context.send : () => {}
|
|
238
|
+
const signal = context.signal
|
|
239
|
+
if (process.platform !== 'darwin') {
|
|
240
|
+
try { send({ type: 'unsupported' }) } catch { /* ignore */ }
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
await mkdir(PENDING_DIR, { recursive: true }).catch(() => {})
|
|
244
|
+
let stopped = false
|
|
245
|
+
let watcher = null
|
|
246
|
+
let debounce = null
|
|
247
|
+
let finish = null
|
|
248
|
+
const emit = async () => {
|
|
249
|
+
if (stopped) return
|
|
250
|
+
const pending = await readPending().catch(() => ({ token: '', entries: [] }))
|
|
251
|
+
if (stopped || pending.entries.length === 0) return
|
|
252
|
+
try {
|
|
253
|
+
send({ type: 'pending', token: pending.token, entries: pending.entries })
|
|
254
|
+
debugLog(`open-watch emit entries=${pending.entries.length} token=${pending.token} paths=${pending.entries.map((entry) => entry.path).join('|')}`)
|
|
255
|
+
} catch {
|
|
256
|
+
/* 客户端已断开 */
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const stop = () => {
|
|
260
|
+
stopped = true
|
|
261
|
+
if (debounce !== null) {
|
|
262
|
+
clearTimeout(debounce)
|
|
263
|
+
debounce = null
|
|
264
|
+
}
|
|
265
|
+
if (watcher !== null) {
|
|
266
|
+
try { watcher.close() } catch { /* ignore */ }
|
|
267
|
+
watcher = null
|
|
268
|
+
}
|
|
269
|
+
if (signal !== undefined && signal !== null && typeof signal.removeEventListener === 'function') {
|
|
270
|
+
try { signal.removeEventListener('abort', stop) } catch { /* ignore */ }
|
|
271
|
+
}
|
|
272
|
+
if (typeof finish === 'function') finish()
|
|
273
|
+
}
|
|
274
|
+
if (signal !== undefined && signal !== null) {
|
|
275
|
+
if (signal.aborted === true) return
|
|
276
|
+
signal.addEventListener('abort', stop, { once: true })
|
|
277
|
+
}
|
|
278
|
+
await emit()
|
|
279
|
+
try {
|
|
280
|
+
watcher = watch(PENDING_DIR, { persistent: true }, (eventType, filename) => {
|
|
281
|
+
if (filename !== null && filename !== undefined && basename(String(filename)) !== basename(PENDING_FILE)) return
|
|
282
|
+
if (debounce !== null) clearTimeout(debounce)
|
|
283
|
+
debounce = setTimeout(() => {
|
|
284
|
+
debounce = null
|
|
285
|
+
void emit()
|
|
286
|
+
}, 80)
|
|
287
|
+
})
|
|
288
|
+
watcher.on?.('error', () => stop())
|
|
289
|
+
} catch (error) {
|
|
290
|
+
debugLog(`open-watch error=${String(error instanceof Error ? error.message : error)}`)
|
|
291
|
+
}
|
|
292
|
+
await new Promise((resolve) => {
|
|
293
|
+
finish = resolve
|
|
294
|
+
if (stopped) {
|
|
295
|
+
finish()
|
|
296
|
+
return
|
|
297
|
+
}
|
|
298
|
+
if (signal !== undefined && signal !== null && signal.aborted === true) stop()
|
|
299
|
+
})
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
router.method('open.ensure', async () => {
|
|
303
|
+
if (process.platform !== 'darwin') return { supported: false, installed: false, reason: '目前仅支持 macOS' }
|
|
304
|
+
return ensureMacOpener()
|
|
305
|
+
})
|
|
306
|
+
router.method('open.status', async () => {
|
|
307
|
+
if (process.platform !== 'darwin') return { supported: false, installed: false }
|
|
308
|
+
return {
|
|
309
|
+
supported: true,
|
|
310
|
+
installed: await exists(OPENER_APP),
|
|
311
|
+
appPath: OPENER_APP,
|
|
312
|
+
pendingPath: PENDING_FILE,
|
|
313
|
+
extensions: OPEN_WITH_EXTENSIONS.length,
|
|
314
|
+
}
|
|
315
|
+
})
|
|
316
|
+
router.method('open.uninstall', async () => {
|
|
317
|
+
if (process.platform !== 'darwin') return { ok: false, reason: '目前仅支持 macOS' }
|
|
318
|
+
await lsregister(OPENER_APP, true)
|
|
319
|
+
await rm(OPENER_APP, { recursive: true, force: true })
|
|
320
|
+
return { ok: true }
|
|
321
|
+
})
|
|
322
|
+
}
|
package/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import { fileURLToPath } from 'node:url'
|
|
|
13
13
|
import { createRouter, ProjectRegistry } from './host/context.js'
|
|
14
14
|
|
|
15
15
|
const HOST_DIR = join(dirname(fileURLToPath(import.meta.url)), 'host')
|
|
16
|
-
const MODULE_FILES = ['assets.js', 'dev.js', 'project.js', 'fs.js', 'git.js', 'ssh.js', 'terminal.js', 'chat.js']
|
|
16
|
+
const MODULE_FILES = ['assets.js', 'dev.js', 'project.js', 'fs.js', 'git.js', 'ssh.js', 'terminal.js', 'chat.js', 'open-with.js']
|
|
17
17
|
|
|
18
18
|
function existingModuleFiles() {
|
|
19
19
|
return MODULE_FILES.filter((name) => {
|
package/package.json
CHANGED
package/src/client/00-core.js
CHANGED
|
@@ -12,6 +12,16 @@ G.BUILD = 'dev'
|
|
|
12
12
|
G.PANEL_ID = 'gufa-code'
|
|
13
13
|
/** 从哪个工作区点进来的(工作区菜单/节点按钮写入,App 启动时消费)。 */
|
|
14
14
|
G.pendingEntryWorkspace = null
|
|
15
|
+
/** 经典模式悬浮入口:优先打开当前聊天所在工作区(而不是上次保存的项目)。 */
|
|
16
|
+
G.pendingSessionEntry = false
|
|
17
|
+
/** 由 App 暴露给文件打开器使用的编辑器入口(打开指定绝对路径文件)。 */
|
|
18
|
+
G.ideOpenPaths = null
|
|
19
|
+
/** 系统「打开方式」请求的目标条目:App boot 时优先打开该工作区,避免先恢复旧项目再被覆盖。 */
|
|
20
|
+
G.pendingOpenEntries = null
|
|
21
|
+
/** 正在处理系统「打开方式」请求:期间入口工作区同步不得把项目改回当前会话 cwd。 */
|
|
22
|
+
G.openWithActive = false
|
|
23
|
+
/** App 初始 boot(恢复项目/会话)是否完成;open-with 必须等它结束再切目标工作区。 */
|
|
24
|
+
G.ideBootDone = false
|
|
15
25
|
G.ctx = null
|
|
16
26
|
G.h = h
|
|
17
27
|
G.React = React
|
|
@@ -2012,6 +2022,11 @@ html[data-gufa-ide] .liang-skin-backdrop{display:none!important}
|
|
|
2012
2022
|
background:transparent;color:var(--dsw-alias-label-primary);font:inherit;font-size:12.5px;cursor:pointer;text-align:left}
|
|
2013
2023
|
.gufa-more-entry:hover{background:var(--dsw-alias-interactive-bg-hover, var(--gufa-hover))}
|
|
2014
2024
|
.gufa-more-entry-label{flex:1 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
2025
|
+
/* 经典模式聊天右上角工具区的「古法编程」入口:只加一个显眼边框,其余保持原生 link 样式 */
|
|
2026
|
+
.gufa-header-entry{min-height:28px;flex:none;display:inline-flex;align-items:center;padding:3px 9px;border:1.5px solid var(--gufa-accent, #b13a2b);border-radius:7px;background:transparent;color:var(--dsw-alias-label-secondary, var(--gufa-text-dim));font:500 12px/1 -apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;cursor:pointer;white-space:nowrap;transition:background .15s ease,color .15s ease}
|
|
2027
|
+
.gufa-header-entry:hover{color:var(--dsw-alias-label-primary, var(--gufa-text));background:var(--dsw-alias-interactive-bg-hover, var(--gufa-hover))}
|
|
2028
|
+
.gufa-header-entry:focus-visible{outline:2px solid var(--dsw-focus-ring-color, var(--gufa-accent));outline-offset:2px}
|
|
2029
|
+
.gufa-header-entry-label{white-space:nowrap}
|
|
2015
2030
|
.gufa-menu{position:absolute;z-index:2147483000;min-width:150px;padding:5px;border-radius:9px;border:1px solid var(--gufa-border-strong);
|
|
2016
2031
|
background:var(--gufa-solid-overlay-bg);box-shadow:0 12px 30px color-mix(in srgb, #000 28%, transparent)}
|
|
2017
2032
|
.gufa-menu-item{display:flex;align-items:center;gap:8px;width:100%;height:28px;padding:0 9px;border:0;border-radius:6px;
|
package/src/client/90-app.js
CHANGED
|
@@ -381,6 +381,20 @@ G.GufaApp = function GufaApp(props) {
|
|
|
381
381
|
const [workspaceSessions, setWorkspaceSessions] = useState({ status: 'idle', sessions: [], groups: [], sessionIds: [], archivedSessionIds: [] })
|
|
382
382
|
const [workspaceIndex, setWorkspaceIndex] = useState([])
|
|
383
383
|
const [recentCurrentIds, setRecentCurrentIds] = useState([])
|
|
384
|
+
const [bootStage, setBootStage] = useState('idle')
|
|
385
|
+
useLayoutEffect(() => {
|
|
386
|
+
/* App 挂载是新实例:先清掉上一实例的 boot 完成标记,避免 open-with 看到陈旧 true */
|
|
387
|
+
G.ideBootDone = false
|
|
388
|
+
}, [])
|
|
389
|
+
useEffect(() => {
|
|
390
|
+
if (bootStage === 'done') {
|
|
391
|
+
G.ideBootDone = true
|
|
392
|
+
void G.api('dev.log', { message: 'boot-stage done' }).catch(() => {})
|
|
393
|
+
} else if (bootStage === 'loading') {
|
|
394
|
+
G.ideBootDone = false
|
|
395
|
+
}
|
|
396
|
+
return () => { if (bootStage === 'done') G.ideBootDone = false }
|
|
397
|
+
}, [bootStage])
|
|
384
398
|
/* 会话运行状态(与经典侧边栏同源:uiSession.sessionStatus,含 running/pendingInteraction/completionUnread) */
|
|
385
399
|
const [sessionStatuses, setSessionStatuses] = useState(() => new Map())
|
|
386
400
|
const [sessionStatusReady, setSessionStatusReady] = useState(false)
|
|
@@ -430,6 +444,14 @@ G.GufaApp = function GufaApp(props) {
|
|
|
430
444
|
G.projectRoot = typeof value?.root === 'string' ? value.root : null
|
|
431
445
|
setProject(value)
|
|
432
446
|
writeJson(GUFA_LS.project, value?.root ?? null)
|
|
447
|
+
try {
|
|
448
|
+
if ((window.__gufaProjectApplyLogs ?? 20) > 0) {
|
|
449
|
+
window.__gufaProjectApplyLogs = (window.__gufaProjectApplyLogs ?? 20) - 1
|
|
450
|
+
void G.api('dev.log', { message: `project-apply root=${String(value?.root ?? '')}` }).catch(() => {})
|
|
451
|
+
}
|
|
452
|
+
} catch {
|
|
453
|
+
/* ignore */
|
|
454
|
+
}
|
|
433
455
|
setTabs([])
|
|
434
456
|
setActivePath(null)
|
|
435
457
|
setSelection(null)
|
|
@@ -454,8 +476,31 @@ G.GufaApp = function GufaApp(props) {
|
|
|
454
476
|
if (bootedRef.current) return
|
|
455
477
|
bootedRef.current = true
|
|
456
478
|
let cancelled = false
|
|
479
|
+
setBootStage('idle')
|
|
457
480
|
void (async () => {
|
|
458
481
|
/* 从工作区菜单点进来的:跳过「恢复上次项目」,直接按入口工作区打开 */
|
|
482
|
+
/* 系统「打开方式」:优先直接打开文件所在目录,绝不先恢复上次项目,避免随后覆盖 */
|
|
483
|
+
const pendingOpenEntries = Array.isArray(G.pendingOpenEntries) ? G.pendingOpenEntries : null
|
|
484
|
+
if (pendingOpenEntries !== null && pendingOpenEntries.length > 0) {
|
|
485
|
+
const firstOpen = pendingOpenEntries.find((entry) => entry !== null && typeof entry === 'object' && typeof entry.path === 'string' && entry.path !== '')
|
|
486
|
+
if (firstOpen !== undefined) {
|
|
487
|
+
const openPath = String(firstOpen.path)
|
|
488
|
+
const normalizedOpen = openPath.replace(/\\/g, '/')
|
|
489
|
+
const openDir = firstOpen.kind === 'dir'
|
|
490
|
+
? openPath
|
|
491
|
+
: (normalizedOpen.lastIndexOf('/') > 0 ? openPath.slice(0, normalizedOpen.lastIndexOf('/')) : openPath)
|
|
492
|
+
try {
|
|
493
|
+
const value = await G.api('project.open', { path: openDir })
|
|
494
|
+
if (!cancelled) applyProject(value)
|
|
495
|
+
void G.api('dev.log', { message: `open-with boot workspace=${value.root}` }).catch(() => {})
|
|
496
|
+
return
|
|
497
|
+
} catch (error) {
|
|
498
|
+
if (!cancelled) setBootError(G.errorText(error))
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
const preferSessionWorkspace = G.pendingSessionEntry === true
|
|
503
|
+
if (preferSessionWorkspace) G.pendingSessionEntry = false
|
|
459
504
|
const pendingEntry = G.pendingEntryWorkspace
|
|
460
505
|
if (pendingEntry !== null && pendingEntry !== undefined && typeof pendingEntry.key === 'string') {
|
|
461
506
|
try {
|
|
@@ -471,6 +516,15 @@ G.GufaApp = function GufaApp(props) {
|
|
|
471
516
|
/* 回落到恢复上次项目 */
|
|
472
517
|
}
|
|
473
518
|
}
|
|
519
|
+
if (preferSessionWorkspace && typeof sessionCwd === 'string' && sessionCwd !== '') {
|
|
520
|
+
try {
|
|
521
|
+
const value = await G.api('project.open', { path: sessionCwd })
|
|
522
|
+
if (!cancelled) applyProject(value)
|
|
523
|
+
} catch (error) {
|
|
524
|
+
if (!cancelled) setBootError(G.errorText(error))
|
|
525
|
+
}
|
|
526
|
+
return
|
|
527
|
+
}
|
|
474
528
|
const saved = floatState !== null && floatState !== undefined && typeof floatState.projectRoot === 'string' && floatState.projectRoot !== ''
|
|
475
529
|
? floatState.projectRoot
|
|
476
530
|
: readJson(GUFA_LS.project)
|
|
@@ -494,7 +548,10 @@ G.GufaApp = function GufaApp(props) {
|
|
|
494
548
|
if (!cancelled) setBootError(G.errorText(error))
|
|
495
549
|
}
|
|
496
550
|
}
|
|
497
|
-
})()
|
|
551
|
+
})().catch(() => {}).finally(() => {
|
|
552
|
+
/* 依赖变化触发的 cleanup 不代表组件卸载;这里必须无条件发出 boot 完成信号 */
|
|
553
|
+
setBootStage('done')
|
|
554
|
+
})
|
|
498
555
|
return () => {
|
|
499
556
|
cancelled = true
|
|
500
557
|
}
|
|
@@ -647,7 +704,7 @@ G.GufaApp = function GufaApp(props) {
|
|
|
647
704
|
/* ignore */
|
|
648
705
|
}
|
|
649
706
|
setActivePath(existing.path)
|
|
650
|
-
return
|
|
707
|
+
return true
|
|
651
708
|
}
|
|
652
709
|
try {
|
|
653
710
|
const doc = await G.api('fs.read', { root: project.root, path })
|
|
@@ -678,8 +735,10 @@ G.GufaApp = function GufaApp(props) {
|
|
|
678
735
|
))
|
|
679
736
|
}
|
|
680
737
|
setActivePath(gufaNormalizeProjectPath(doc.path ?? path))
|
|
738
|
+
return true
|
|
681
739
|
} catch (error) {
|
|
682
740
|
setBanner(G.errorText(error))
|
|
741
|
+
return false
|
|
683
742
|
}
|
|
684
743
|
}, [project, tabs])
|
|
685
744
|
|
|
@@ -691,9 +750,10 @@ G.GufaApp = function GufaApp(props) {
|
|
|
691
750
|
|
|
692
751
|
/* 从搜索/定位结果打开文件并跳到指定行列(等 tab 切换完成后再触发编辑器 reveal) */
|
|
693
752
|
const openFileAt = useCallback(async (rawPath, line, column) => {
|
|
694
|
-
if (typeof rawPath !== 'string' || rawPath === '') return
|
|
753
|
+
if (typeof rawPath !== 'string' || rawPath === '') return false
|
|
695
754
|
const path = gufaNormalizeProjectPath(rawPath)
|
|
696
|
-
await openFile(path)
|
|
755
|
+
const opened = await openFile(path)
|
|
756
|
+
if (opened === false) return false
|
|
697
757
|
const targetLine = Number.isFinite(Number(line)) ? Math.max(1, Math.trunc(Number(line))) : 1
|
|
698
758
|
const targetColumn = Number.isFinite(Number(column)) ? Math.max(1, Math.trunc(Number(column))) : 1
|
|
699
759
|
const target = { path, line: targetLine, column: targetColumn }
|
|
@@ -709,6 +769,75 @@ G.GufaApp = function GufaApp(props) {
|
|
|
709
769
|
pendingOpenAtRef.current = target
|
|
710
770
|
}, [openFile])
|
|
711
771
|
|
|
772
|
+
/* 系统文件「打开方式」:暴露入口给 99-main 的 pending 轮询(打开指定绝对路径文件)。 */
|
|
773
|
+
const projectRef = useRef(project)
|
|
774
|
+
projectRef.current = project
|
|
775
|
+
const applyProjectRef = useRef(applyProject)
|
|
776
|
+
applyProjectRef.current = applyProject
|
|
777
|
+
const openFileAtRef = useRef(openFileAt)
|
|
778
|
+
openFileAtRef.current = openFileAt
|
|
779
|
+
const openFileRef = useRef(openFile)
|
|
780
|
+
openFileRef.current = openFile
|
|
781
|
+
useEffect(() => {
|
|
782
|
+
const sleep = (ms) => new Promise((resolve) => { window.setTimeout(resolve, ms) })
|
|
783
|
+
const normalize = (value) => String(value ?? '').replace(/\\/g, '/').replace(/\/+$/, '')
|
|
784
|
+
const dirOf = (value) => {
|
|
785
|
+
const normalized = String(value ?? '').replace(/\\/g, '/')
|
|
786
|
+
const index = normalized.lastIndexOf('/')
|
|
787
|
+
return index > 0 ? String(value).slice(0, value.length - (normalized.length - index)) : value
|
|
788
|
+
}
|
|
789
|
+
const handler = async (entries) => {
|
|
790
|
+
const list = Array.isArray(entries)
|
|
791
|
+
? entries.filter((entry) => entry !== null && typeof entry === 'object' && typeof entry.path === 'string' && entry.path !== '')
|
|
792
|
+
: []
|
|
793
|
+
if (list.length === 0) return false
|
|
794
|
+
const first = list[0]
|
|
795
|
+
const targetDir = first.kind === 'dir' ? first.path : dirOf(first.path)
|
|
796
|
+
const targetRoot = normalize(targetDir)
|
|
797
|
+
if (normalize(projectRef.current?.root) !== targetRoot) {
|
|
798
|
+
try {
|
|
799
|
+
const value = await G.api('project.open', { path: targetDir })
|
|
800
|
+
applyProjectRef.current(value)
|
|
801
|
+
/* 等 React 把项目状态提交;如果 boot 恢复旧项目还在覆盖,继续等目标根稳定 */
|
|
802
|
+
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
803
|
+
await sleep(150)
|
|
804
|
+
if (normalize(projectRef.current?.root) === targetRoot) break
|
|
805
|
+
}
|
|
806
|
+
if (normalize(projectRef.current?.root) !== targetRoot) {
|
|
807
|
+
console.error('[gufa-code] 目标工作区未稳定', targetRoot, projectRef.current?.root)
|
|
808
|
+
return false
|
|
809
|
+
}
|
|
810
|
+
} catch (error) {
|
|
811
|
+
console.error('[gufa-code] 打开文件所在工作区失败', error)
|
|
812
|
+
return false
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
const root = normalize(projectRef.current?.root ?? targetRoot)
|
|
816
|
+
void G.api('dev.log', { message: `open-with apply dir=${targetDir} root=${root}` }).catch(() => {})
|
|
817
|
+
const files = list.filter((entry) => entry.kind !== 'dir')
|
|
818
|
+
for (let index = 0; index < files.length; index += 1) {
|
|
819
|
+
const absolute = normalize(files[index].path)
|
|
820
|
+
const rel = absolute.startsWith(`${root}/`) ? absolute.slice(root.length + 1) : absolute.split('/').pop()
|
|
821
|
+
if (typeof rel !== 'string' || rel === '' || rel.includes('..')) continue
|
|
822
|
+
void G.api('dev.log', { message: `open-with open rel=${rel}` }).catch(() => {})
|
|
823
|
+
const opened = index === 0
|
|
824
|
+
? await openFileAtRef.current(rel, 1, 1)
|
|
825
|
+
: await openFileRef.current(rel)
|
|
826
|
+
if (opened === false) {
|
|
827
|
+
void G.api('dev.log', { message: `open-with open-failed rel=${rel}` }).catch(() => {})
|
|
828
|
+
return false
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
G.pendingOpenEntries = null
|
|
832
|
+
return true
|
|
833
|
+
}
|
|
834
|
+
G.ideOpenPaths = handler
|
|
835
|
+
return () => {
|
|
836
|
+
if (G.ideOpenPaths === handler) G.ideOpenPaths = null
|
|
837
|
+
}
|
|
838
|
+
}, [])
|
|
839
|
+
|
|
840
|
+
|
|
712
841
|
/* 未打开文件跳转:activePath 切到目标文件后(子组件编辑器 effect 已先行)再发 reveal */
|
|
713
842
|
useEffect(() => {
|
|
714
843
|
const pending = pendingOpenAtRef.current
|
|
@@ -1857,6 +1986,11 @@ G.GufaApp = function GufaApp(props) {
|
|
|
1857
1986
|
const entryWorkspaceSyncedRef = useRef(false)
|
|
1858
1987
|
useEffect(() => {
|
|
1859
1988
|
if (entryWorkspaceSyncedRef.current) return
|
|
1989
|
+
/* 系统「打开方式」正在处理:工作区由 open-with 决定,入口同步不得改回会话 cwd */
|
|
1990
|
+
if (G.openWithActive === true) {
|
|
1991
|
+
entryWorkspaceSyncedRef.current = true
|
|
1992
|
+
return
|
|
1993
|
+
}
|
|
1860
1994
|
/* 实例窗口:保持空工作区,不跟随主窗口/DSH 当前会话自动打开项目 */
|
|
1861
1995
|
if (G.instanceBoot === true) {
|
|
1862
1996
|
entryWorkspaceSyncedRef.current = true
|
|
@@ -1867,14 +2001,12 @@ G.GufaApp = function GufaApp(props) {
|
|
|
1867
2001
|
entryWorkspaceSyncedRef.current = true
|
|
1868
2002
|
return
|
|
1869
2003
|
}
|
|
1870
|
-
if (project === null && (G.pendingEntryWorkspace === null || G.pendingEntryWorkspace === undefined)) return
|
|
1871
2004
|
const pending = G.pendingEntryWorkspace
|
|
1872
2005
|
if (pending === null || pending === undefined || typeof pending.key !== 'string') {
|
|
1873
|
-
|
|
2006
|
+
/* 没有显式入口(例如重启 DSH 自动恢复 IDE):保留 boot 已恢复的「上次工作区」,
|
|
2007
|
+
不要改回当前会话 cwd —— 否则 open-with 打开的工作区永远不会被记住。 */
|
|
1874
2008
|
entryWorkspaceSyncedRef.current = true
|
|
1875
|
-
|
|
1876
|
-
void openProjectPath(sessionCwd).catch((error) => setBanner(G.errorText(error)))
|
|
1877
|
-
}
|
|
2009
|
+
void G.api('dev.log', { message: `entry-sync keep-saved project=${project !== null && typeof project.root === 'string' ? project.root : 'null'} sessionCwd=${typeof sessionCwd === 'string' ? sessionCwd : ''}` }).catch(() => {})
|
|
1878
2010
|
return
|
|
1879
2011
|
}
|
|
1880
2012
|
entryWorkspaceSyncedRef.current = true
|
package/src/client/99-main.js
CHANGED
|
@@ -191,6 +191,162 @@ G.installWorkspaceMenuEntry = function installWorkspaceMenuEntry(ctx) {
|
|
|
191
191
|
}
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
+
/** 经典模式聊天标题行的文本入口:和「智能体团队」同款 link 样式,但文案直接叫「古法编程」。 */
|
|
195
|
+
G.GufaHeaderEntry = function GufaHeaderEntry() {
|
|
196
|
+
const ideVisible = G.useMode()
|
|
197
|
+
if (ideVisible === true) return null
|
|
198
|
+
return h(
|
|
199
|
+
'button',
|
|
200
|
+
{
|
|
201
|
+
type: 'button',
|
|
202
|
+
className: 'gufa-header-entry',
|
|
203
|
+
title: '进入古法编程(当前聊天的工作区)',
|
|
204
|
+
'aria-label': '古法编程',
|
|
205
|
+
onClick: (event) => {
|
|
206
|
+
try {
|
|
207
|
+
event.preventDefault()
|
|
208
|
+
event.stopPropagation()
|
|
209
|
+
} catch {
|
|
210
|
+
/* ignore */
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
G.pendingSessionEntry = true
|
|
214
|
+
G.enterGufa()
|
|
215
|
+
} catch (error) {
|
|
216
|
+
console.error('[gufa-code] 进入古法编程失败', error)
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
h('span', { className: 'gufa-header-entry-label' }, '古法编程'),
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** 文件关联打开:订阅 Host 的 open.watch 事件(不再轮询文件),处理成功后 ack。 */
|
|
225
|
+
G.installOpenWithHandler = function installOpenWithHandler(ctx) {
|
|
226
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') return () => {}
|
|
227
|
+
let skip = false
|
|
228
|
+
try {
|
|
229
|
+
skip = typeof G.isFloatMode === 'function' && G.isFloatMode() === true
|
|
230
|
+
if (!skip && /[?&]gufa-instance=1/.test(String(location.search || ''))) skip = true
|
|
231
|
+
if (!skip && /welcome|update/i.test(String(location.href || ''))) skip = true
|
|
232
|
+
} catch {
|
|
233
|
+
skip = false
|
|
234
|
+
}
|
|
235
|
+
if (skip) return () => {}
|
|
236
|
+
let stopped = false
|
|
237
|
+
let busy = false
|
|
238
|
+
let lastToken = ''
|
|
239
|
+
let abortStream = null
|
|
240
|
+
let reconnectTimer = 0
|
|
241
|
+
let retryTimer = 0
|
|
242
|
+
let logs = 20
|
|
243
|
+
const log = (message) => {
|
|
244
|
+
if (logs <= 0) return
|
|
245
|
+
logs -= 1
|
|
246
|
+
try { void G.api('dev.log', { message }).catch(() => {}) } catch { /* ignore */ }
|
|
247
|
+
}
|
|
248
|
+
const sleep = (ms) => new Promise((resolve) => { window.setTimeout(resolve, ms) })
|
|
249
|
+
const ideReady = () => {
|
|
250
|
+
try {
|
|
251
|
+
return G.mode.get() === true
|
|
252
|
+
&& G.ideBootDone === true
|
|
253
|
+
&& typeof G.ideOpenPaths === 'function'
|
|
254
|
+
&& document.querySelector('.gufa-overlay-root') !== null
|
|
255
|
+
} catch {
|
|
256
|
+
return false
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const waitIdeReady = async (maxAttempts = 100) => {
|
|
260
|
+
for (let attempt = 0; attempt < maxAttempts && stopped !== true; attempt += 1) {
|
|
261
|
+
if (ideReady()) return true
|
|
262
|
+
await sleep(200)
|
|
263
|
+
}
|
|
264
|
+
return false
|
|
265
|
+
}
|
|
266
|
+
const openEntries = async (entries) => {
|
|
267
|
+
if (!ideReady()) {
|
|
268
|
+
log(`open-with enter ide mode=${G.mode.get() === true} hook=${typeof G.ideOpenPaths === 'function'} boot=${G.ideBootDone === true}`)
|
|
269
|
+
try {
|
|
270
|
+
G.pendingOpenEntries = entries
|
|
271
|
+
G.pendingSessionEntry = false
|
|
272
|
+
G.enterGufa()
|
|
273
|
+
} catch {
|
|
274
|
+
return false
|
|
275
|
+
}
|
|
276
|
+
/* 必须等 IDE 覆盖层真正挂载 + App 初始 boot 完成,否则旧项目会覆盖刚打开的工作区 */
|
|
277
|
+
if (!(await waitIdeReady())) return false
|
|
278
|
+
}
|
|
279
|
+
const result = await G.ideOpenPaths(entries)
|
|
280
|
+
return result !== false
|
|
281
|
+
}
|
|
282
|
+
const handlePending = async (event) => {
|
|
283
|
+
if (stopped || busy) return
|
|
284
|
+
const entries = event !== null && event !== undefined && Array.isArray(event.entries) ? event.entries : []
|
|
285
|
+
const token = event !== null && event !== undefined && typeof event.token === 'string' ? event.token : ''
|
|
286
|
+
if (entries.length === 0 || token === '' || token === lastToken) return
|
|
287
|
+
G.openWithActive = true
|
|
288
|
+
busy = true
|
|
289
|
+
log(`open-with pending token=${token} entries=${entries.length} paths=${entries.map((entry) => entry.path).join('|')}`)
|
|
290
|
+
const ok = await openEntries(entries)
|
|
291
|
+
busy = false
|
|
292
|
+
if (stopped) return
|
|
293
|
+
if (ok) {
|
|
294
|
+
lastToken = token
|
|
295
|
+
await G.api('open.ack', { token }).catch(() => {})
|
|
296
|
+
G.openWithActive = false
|
|
297
|
+
log(`open-with ack token=${token}`)
|
|
298
|
+
return
|
|
299
|
+
}
|
|
300
|
+
log(`open-with retry token=${token}`)
|
|
301
|
+
if (retryTimer !== 0) window.clearTimeout(retryTimer)
|
|
302
|
+
retryTimer = window.setTimeout(() => {
|
|
303
|
+
retryTimer = 0
|
|
304
|
+
void handlePending(event)
|
|
305
|
+
}, 3000)
|
|
306
|
+
}
|
|
307
|
+
const scheduleReconnect = (delay = 1500) => {
|
|
308
|
+
if (stopped) return
|
|
309
|
+
if (reconnectTimer !== 0) return
|
|
310
|
+
reconnectTimer = window.setTimeout(() => {
|
|
311
|
+
reconnectTimer = 0
|
|
312
|
+
connect()
|
|
313
|
+
}, delay)
|
|
314
|
+
}
|
|
315
|
+
const connect = () => {
|
|
316
|
+
if (stopped) return
|
|
317
|
+
try {
|
|
318
|
+
abortStream = G.stream('open.watch', {}, (event) => {
|
|
319
|
+
if (event === null || event === undefined) return
|
|
320
|
+
if (event.type === 'pending') void handlePending(event)
|
|
321
|
+
else if (event.type === 'error') scheduleReconnect()
|
|
322
|
+
}, () => scheduleReconnect())
|
|
323
|
+
log('open-with stream connected')
|
|
324
|
+
} catch {
|
|
325
|
+
scheduleReconnect(3000)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const start = () => {
|
|
329
|
+
if (stopped) return
|
|
330
|
+
void G.api('open.ensure', {}).catch(() => {})
|
|
331
|
+
connect()
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
if (typeof ctx?.inject === 'function') ctx.inject(['layout'], () => start())
|
|
335
|
+
else start()
|
|
336
|
+
} catch {
|
|
337
|
+
start()
|
|
338
|
+
}
|
|
339
|
+
return () => {
|
|
340
|
+
stopped = true
|
|
341
|
+
if (reconnectTimer !== 0) window.clearTimeout(reconnectTimer)
|
|
342
|
+
if (retryTimer !== 0) window.clearTimeout(retryTimer)
|
|
343
|
+
reconnectTimer = 0
|
|
344
|
+
retryTimer = 0
|
|
345
|
+
try { if (typeof abortStream === 'function') abortStream() } catch { /* ignore */ }
|
|
346
|
+
abortStream = null
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
194
350
|
/** 「更多」菜单里的入口行:点击进入古法编程(从当前工作区进入)。 */
|
|
195
351
|
G.MoreMenuEntry = function MoreMenuEntry(props) {
|
|
196
352
|
return h(
|
|
@@ -781,6 +937,14 @@ const GUFA_PLUGIN = {
|
|
|
781
937
|
}
|
|
782
938
|
}, 2500)
|
|
783
939
|
|
|
940
|
+
// 系统文件「打开方式」:自动安装打开器并轮询 pending 文件请求。
|
|
941
|
+
try {
|
|
942
|
+
const disposeOpenWith = G.installOpenWithHandler(ctx)
|
|
943
|
+
if (typeof disposeOpenWith === 'function') ctx.effect(() => disposeOpenWith, 'gufa-code: open-with handler')
|
|
944
|
+
} catch (error) {
|
|
945
|
+
console.error('[gufa-code] 文件关联打开器安装失败', error)
|
|
946
|
+
}
|
|
947
|
+
|
|
784
948
|
// 入口:工作区行的「...」菜单里注入「古法编程」一行。
|
|
785
949
|
try {
|
|
786
950
|
const disposeMenuEntry = G.installWorkspaceMenuEntry(ctx)
|
|
@@ -789,6 +953,19 @@ const GUFA_PLUGIN = {
|
|
|
789
953
|
console.error('[gufa-code] 安装工作区菜单入口失败', error)
|
|
790
954
|
}
|
|
791
955
|
|
|
956
|
+
// 经典模式聊天右上角工具区:在头像菜单左侧的空位放「古法编程」文本入口。
|
|
957
|
+
try {
|
|
958
|
+
ctx.inject(['slots'], (scope) => {
|
|
959
|
+
scope.slots.inject('conversation.session.header.utilities', () => scope.slots.register({
|
|
960
|
+
name: 'conversation.session.header.utilities',
|
|
961
|
+
id: 'gufa-code-entry',
|
|
962
|
+
order: -100,
|
|
963
|
+
}, G.GufaHeaderEntry))
|
|
964
|
+
})
|
|
965
|
+
} catch (error) {
|
|
966
|
+
console.error('[gufa-code] 聊天头部入口注册失败', error)
|
|
967
|
+
}
|
|
968
|
+
|
|
792
969
|
// 面板注册:入口注入到工作区行的「...」菜单里(DSH 未提供该菜单的 slot)。
|
|
793
970
|
ctx.inject(['slots'], (scope) => {
|
|
794
971
|
// main key 仅作安全网:若侧边栏把主面板切到 gufa-code(点击拦截失效时),
|