This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (119 loc) · 3.19 KB
/
main.go
File metadata and controls
136 lines (119 loc) · 3.19 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
package main
import (
"os"
"path/filepath"
"strings"
"github.com/gdamore/tcell/v2"
"llmShortTermMemory/model"
)
func init() {
model.ApiKey = os.Getenv("OPENAI_API_KEY")
if model.ApiKey == "" {
data, err := os.ReadFile("OPENAI_API_KEY")
if err == nil {
model.ApiKey = strings.TrimSpace(string(data))
} else {
execPath, _ := os.Executable()
execDir := filepath.Dir(execPath)
configPath := filepath.Join(execDir, "OPENAI_API_KEY")
data, err := os.ReadFile(configPath)
if err == nil {
model.ApiKey = strings.TrimSpace(string(data))
}
}
}
data, err := os.ReadFile("INSTRUCTION_CONVERSATION")
if err == nil {
model.InstructionConversation = strings.TrimSpace(string(data))
} else {
execPath, _ := os.Executable()
execDir := filepath.Dir(execPath)
instructionPath := filepath.Join(execDir, "INSTRUCTION_CONVERSATION")
data, err := os.ReadFile(instructionPath)
if err == nil {
model.InstructionConversation = strings.TrimSpace(string(data))
} else {
model.InstructionConversation = ""
}
}
data, err = os.ReadFile("INSTRUCTION_SUMMARY")
if err == nil {
model.InstructionSummary = strings.TrimSpace(string(data))
} else {
execPath, _ := os.Executable()
execDir := filepath.Dir(execPath)
instructionPath := filepath.Join(execDir, "INSTRUCTION_SUMMARY")
data, err := os.ReadFile(instructionPath)
if err == nil {
model.InstructionSummary = strings.TrimSpace(string(data))
} else {
model.InstructionSummary = ""
}
}
}
func main() {
var appState *model.Frame
// 檢查命令列參數
useOldUI := false
for _, arg := range os.Args[1:] {
if arg == "--old" {
useOldUI = true
break
}
}
if useOldUI {
appState = model.CreateOldUI()
} else {
appState = model.CreateUI()
}
appState.Input.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
text := appState.Input.GetText()
if event.Key() == tcell.KeyTab {
return nil // 阻止 Tab 鍵在輸入框中觸發
}
if event.Key() == tcell.KeyEnter && len(text) > 2 && (strings.HasSuffix(text, "$$") || strings.HasSuffix(text, "$$")) {
text = strings.TrimSuffix(text, "$$")
text = strings.TrimSuffix(text, "$$")
appState.Input.SetText("", true)
if useOldUI {
appState.OldAPIHandler(text)
} else {
appState.APIHandler(text)
}
return nil
}
return event
})
// 設置全局按鍵處理
appState.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlC:
appState.App.Stop()
case tcell.KeyTab:
// Tab 切換焦點
currentFocus := appState.App.GetFocus()
if useOldUI {
// 舊版 UI 只有 Input 和 Conversation
if currentFocus == appState.Input {
appState.App.SetFocus(appState.Conversation)
} else {
appState.App.SetFocus(appState.Input)
}
} else {
// 新版 UI 有 Input、Conversation 和 Summary
if currentFocus == appState.Input {
appState.App.SetFocus(appState.Conversation)
} else if currentFocus == appState.Conversation {
appState.App.SetFocus(appState.Summary)
} else {
appState.App.SetFocus(appState.Input)
}
}
}
return event
})
// 運行應用
if err := appState.App.Run(); err != nil {
panic(err)
}
}