forked from yhy0/ChYing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_runtime.go
More file actions
308 lines (276 loc) · 8.95 KB
/
Copy pathproject_runtime.go
File metadata and controls
308 lines (276 loc) · 8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/yhy0/ChYing/conf/file"
"github.com/yhy0/ChYing/pkg/db"
"github.com/yhy0/ChYing/pkg/desktop"
"github.com/yhy0/logging"
)
const desktopWindowName = "承影"
func (a *App) ensureDesktopRuntime() {
a.desktopRuntimeOnce.Do(func() {
now := time.Now().UTC()
a.desktopRequests = make(chan []string, 16)
a.runtimeState = desktop.RuntimeState{
Version: 1,
Status: desktop.StatusSelecting,
PID: os.Getpid(),
Message: "等待选择项目",
StartedAt: now,
UpdatedAt: now,
}
})
}
// startDesktopLaunchController starts the single serialized project-open queue.
// It is called once Wails has started so window and event operations are safe.
func (a *App) startDesktopLaunchController(initialArgs []string) {
a.ensureDesktopRuntime()
a.desktopControllerOnce.Do(func() {
a.publishDesktopState(a.currentDesktopState())
go func() {
for args := range a.desktopRequests {
a.processDesktopLaunchArgs(args)
}
}()
})
if len(initialArgs) > 0 {
a.handleDesktopLaunchArgs(initialArgs)
}
}
// handleDesktopLaunchArgs is safe to call from Wails' second-instance callback,
// including during application construction before the main window is ready.
func (a *App) handleDesktopLaunchArgs(args []string) {
a.ensureDesktopRuntime()
request := append([]string(nil), args...)
a.desktopRequests <- request
}
func (a *App) processDesktopLaunchArgs(args []string) {
projectName, err := desktop.ProjectFromArgs(args)
if err != nil {
_ = a.failDesktopOpen("", fmt.Errorf("启动参数无效: %w", err))
a.focusMainWindow()
return
}
if projectName == "" {
a.focusMainWindow()
return
}
result := a.OpenExistingProject(projectName)
if result.Error != "" {
logging.Logger.Warnf("Agent 打开项目失败: %s", result.Error)
}
}
// OpenExistingProject is the single project-opening entry point shared by the
// desktop UI and agent launch requests.
func (a *App) OpenExistingProject(projectName string) Result {
a.ensureDesktopRuntime()
a.projectOpenMu.Lock()
defer a.projectOpenMu.Unlock()
name, err := desktop.NormalizeProjectName(projectName)
if err != nil {
return a.failDesktopOpen(projectName, err)
}
state := a.currentDesktopState()
if state.Status == desktop.StatusReady {
if state.Project != name {
return Result{Error: fmt.Sprintf("ChYing 已打开项目 %q;为避免污染数据,不会静默切换到 %q,请先退出桌面应用", state.Project, name)}
}
state.UpdatedAt = time.Now().UTC()
state.Message = "项目已就绪"
a.setAndPublishDesktopState(state)
a.focusMainWindow()
return Result{Data: state}
}
if state.Status == desktop.StatusFailed || db.CurrentProjectName != "" {
current := state.Project
if current == "" {
current = db.CurrentProjectName
}
return Result{Error: fmt.Sprintf("ChYing 当前项目 %q 的初始化状态不可复用,请退出桌面应用后重试", current)}
}
if err := validateExistingProject(name); err != nil {
return a.failDesktopOpen(name, err)
}
a.updateDesktopState(desktop.StatusOpening, name, 5, "正在打开项目", "")
steps := []struct {
progress int
message string
run func() Result
}{
{15, "正在准备项目", func() Result { return a.StartInitialization("Open existing project", name) }},
{25, "正在初始化基础组件", a.StepBasicInitialization},
{35, "正在加载配置", a.StepConfigurationLoad},
{50, "正在连接项目数据库", func() Result { return a.StepDatabaseConnection(name) }},
{65, "正在检查数据库结构", a.StepSchemaValidation},
{80, "正在启动代理服务", a.StepProxyServerStart},
{90, "正在加载项目数据", func() Result { return a.StepProjectDataLoad("Open existing project", name) }},
{100, "正在启动 MCP 服务", a.StepInitializationComplete},
}
for _, step := range steps {
a.updateDesktopState(desktop.StatusOpening, name, step.progress, step.message, "")
if err := resultError(step.run()); err != nil {
return a.failDesktopOpen(name, err)
}
}
state = a.currentDesktopState()
if state.Status != desktop.StatusReady || state.Project != name || state.MCPURL == "" {
message := state.Error
if message == "" {
message = "MCP 服务未能进入就绪状态"
}
return a.failDesktopOpen(name, fmt.Errorf("%s", message))
}
a.focusMainWindow()
return Result{Data: state}
}
// GetDesktopRuntimeState exposes the same readiness contract used by chyingctl.
func (a *App) GetDesktopRuntimeState() Result {
a.ensureDesktopRuntime()
return Result{Data: a.currentDesktopState()}
}
func resultError(result Result) error {
if result.Error != "" {
return fmt.Errorf("%s", result.Error)
}
if progress, ok := result.Data.(*InitProgress); ok && !progress.Success {
if progress.Error != "" {
return fmt.Errorf("%s", progress.Error)
}
return fmt.Errorf("%s", progress.Message)
}
if result.Data == nil {
return fmt.Errorf("初始化步骤没有返回结果")
}
return nil
}
func validateExistingProject(projectName string) error {
return validateExistingProjectAt(filepath.Join(file.ChyingDir, "db"), projectName)
}
func validateExistingProjectAt(root string, projectName string) error {
projectDir := filepath.Join(root, projectName)
rel, err := filepath.Rel(root, projectDir)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return fmt.Errorf("项目路径不合法")
}
dirInfo, err := os.Lstat(projectDir)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("项目 %q 不存在", projectName)
}
return fmt.Errorf("检查项目目录失败: %w", err)
}
if dirInfo.Mode()&os.ModeSymlink != 0 || !dirInfo.IsDir() {
return fmt.Errorf("项目目录必须是真实目录,不能是符号链接")
}
databasePath := filepath.Join(projectDir, projectName+".db")
databaseInfo, err := os.Lstat(databasePath)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("项目数据库不存在: %s", databasePath)
}
return fmt.Errorf("检查项目数据库失败: %w", err)
}
if databaseInfo.Mode()&os.ModeSymlink != 0 || !databaseInfo.Mode().IsRegular() {
return fmt.Errorf("项目数据库必须是普通文件,不能是符号链接")
}
return nil
}
func (a *App) failDesktopOpen(projectName string, err error) Result {
message := err.Error()
if a.currentDesktopState().Status == desktop.StatusReady {
return Result{Error: message}
}
a.publishDesktopFailure(projectName, message)
return Result{Error: message}
}
func (a *App) publishDesktopFailure(projectName string, message string) {
a.updateDesktopState(desktop.StatusFailed, projectName, 0, "项目打开失败", message)
}
func (a *App) markDesktopProjectReady(projectName string, mcpURL string) {
a.ensureDesktopRuntime()
a.runtimeMu.Lock()
state := a.runtimeState
state.Status = desktop.StatusReady
state.Project = projectName
state.MCPURL = mcpURL
state.PID = os.Getpid()
state.Progress = 100
state.Message = "项目已就绪"
state.Error = ""
state.UpdatedAt = time.Now().UTC()
a.runtimeState = state
a.runtimeMu.Unlock()
a.publishDesktopState(state)
if wailsApp != nil {
wailsApp.Event.Emit("DesktopProjectOpened", state)
}
}
func (a *App) markDesktopProjectFailed(projectName string, message string) {
a.publishDesktopFailure(projectName, message)
}
func (a *App) markDesktopStopped() {
a.ensureDesktopRuntime()
state := a.currentDesktopState()
state.Status = desktop.StatusStopped
state.Progress = 0
state.Message = "ChYing 已退出"
state.MCPURL = ""
state.UpdatedAt = time.Now().UTC()
a.setAndPublishDesktopState(state)
}
func (a *App) updateDesktopState(status string, projectName string, progress int, message string, errorMessage string) {
a.ensureDesktopRuntime()
state := a.currentDesktopState()
state.Version = 1
state.Status = status
state.Project = projectName
state.PID = os.Getpid()
state.Progress = progress
state.Message = message
state.Error = errorMessage
state.UpdatedAt = time.Now().UTC()
if status != desktop.StatusReady {
state.MCPURL = ""
}
a.setAndPublishDesktopState(state)
}
func (a *App) setAndPublishDesktopState(state desktop.RuntimeState) {
a.ensureDesktopRuntime()
a.runtimeMu.Lock()
a.runtimeState = state
a.runtimeMu.Unlock()
a.publishDesktopState(state)
}
func (a *App) currentDesktopState() desktop.RuntimeState {
a.ensureDesktopRuntime()
a.runtimeMu.RLock()
defer a.runtimeMu.RUnlock()
return a.runtimeState
}
func (a *App) publishDesktopState(state desktop.RuntimeState) {
if err := desktop.WriteRuntimeState(desktop.RuntimeFilePath(), state); err != nil {
logging.Logger.Warnf("写入桌面运行状态失败: %v", err)
}
if wailsApp != nil {
wailsApp.Event.Emit("DesktopProjectOpenProgress", state)
if state.Status == desktop.StatusFailed {
wailsApp.Event.Emit("DesktopProjectOpenFailed", state)
}
}
}
func (a *App) focusMainWindow() {
if wailsApp == nil {
return
}
window, ok := wailsApp.Window.GetByName(desktopWindowName)
if !ok || window == nil {
return
}
window.Show()
window.Restore()
window.Focus()
}