mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-14 08:16:56 +08:00
fix: setup flow doesn't hijack normal conversation
- Detects when user sends Chinese text, short text, or text with spaces during key-input steps → auto-pauses setup and answers the question - API key masking fixed (no more garbled display) - User can resume setup with '开始配置' anytime
This commit is contained in:
@@ -79,13 +79,38 @@ func setConfig(st *store.Store, uid int64, key, val string) {
|
|||||||
func (a *Agent) handleSetupFlow(userID int64, text string, L string) (string, bool) {
|
func (a *Agent) handleSetupFlow(userID int64, text string, L string) (string, bool) {
|
||||||
state := a.getSetupState(userID)
|
state := a.getSetupState(userID)
|
||||||
|
|
||||||
// Cancel setup
|
|
||||||
lower := strings.ToLower(text)
|
lower := strings.ToLower(text)
|
||||||
|
|
||||||
|
// Cancel setup — explicit or implicit (user asking unrelated questions)
|
||||||
if lower == "cancel" || lower == "取消" || lower == "/cancel" {
|
if lower == "cancel" || lower == "取消" || lower == "/cancel" {
|
||||||
a.clearSetupState(userID)
|
a.clearSetupState(userID)
|
||||||
return a.setupMsg(L, "cancelled"), true
|
return a.setupMsg(L, "cancelled"), true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If in a step that expects a key/secret, check if user is NOT sending a key
|
||||||
|
// Keys are typically long strings without spaces and Chinese characters
|
||||||
|
if state.Step == "await_api_key" || state.Step == "await_api_secret" || state.Step == "await_passphrase" || state.Step == "await_ai_key" {
|
||||||
|
trimmed := strings.TrimSpace(text)
|
||||||
|
hasChinese := false
|
||||||
|
for _, r := range trimmed {
|
||||||
|
if r >= 0x4e00 && r <= 0x9fff {
|
||||||
|
hasChinese = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hasSpaces := strings.Contains(trimmed, " ") && !strings.HasPrefix(trimmed, "sk-")
|
||||||
|
tooShort := len(trimmed) < 8
|
||||||
|
|
||||||
|
if hasChinese || hasSpaces || tooShort {
|
||||||
|
// User is probably asking a question, not providing a key
|
||||||
|
a.clearSetupState(userID)
|
||||||
|
if L == "zh" {
|
||||||
|
return "👌 配置已暂停。我先回答你的问题——\n\n随时发送 *开始配置* 继续配置。", false
|
||||||
|
}
|
||||||
|
return "👌 Setup paused. Let me answer your question first—\n\nSend *setup* anytime to continue.", false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch state.Step {
|
switch state.Step {
|
||||||
case "await_exchange":
|
case "await_exchange":
|
||||||
return a.handleExchangeChoice(userID, text, state, L)
|
return a.handleExchangeChoice(userID, text, state, L)
|
||||||
@@ -220,12 +245,12 @@ func (a *Agent) finishSetup(userID int64, state *SetupState, L string) (string,
|
|||||||
a.clearSetupState(userID)
|
a.clearSetupState(userID)
|
||||||
|
|
||||||
result := ""
|
result := ""
|
||||||
|
maskedKey := maskKey(state.APIKey)
|
||||||
if L == "zh" {
|
if L == "zh" {
|
||||||
result = fmt.Sprintf("🎉 *配置完成!*\n\n"+
|
result = fmt.Sprintf("🎉 *配置完成!*\n\n"+
|
||||||
"• 交易所: %s\n"+
|
"• 交易所: %s\n"+
|
||||||
"• API Key: %s...%s\n",
|
"• API Key: %s\n",
|
||||||
strings.Title(state.Exchange),
|
strings.Title(state.Exchange), maskedKey)
|
||||||
state.APIKey[:4], state.APIKey[len(state.APIKey)-4:])
|
|
||||||
if state.AIModel != "" {
|
if state.AIModel != "" {
|
||||||
result += fmt.Sprintf("• AI 模型: %s\n", state.AIModel)
|
result += fmt.Sprintf("• AI 模型: %s\n", state.AIModel)
|
||||||
}
|
}
|
||||||
@@ -233,9 +258,8 @@ func (a *Agent) finishSetup(userID int64, state *SetupState, L string) (string,
|
|||||||
} else {
|
} else {
|
||||||
result = fmt.Sprintf("🎉 *Setup Complete!*\n\n"+
|
result = fmt.Sprintf("🎉 *Setup Complete!*\n\n"+
|
||||||
"• Exchange: %s\n"+
|
"• Exchange: %s\n"+
|
||||||
"• API Key: %s...%s\n",
|
"• API Key: %s\n",
|
||||||
strings.Title(state.Exchange),
|
strings.Title(state.Exchange), maskedKey)
|
||||||
state.APIKey[:4], state.APIKey[len(state.APIKey)-4:])
|
|
||||||
if state.AIModel != "" {
|
if state.AIModel != "" {
|
||||||
result += fmt.Sprintf("• AI Model: %s\n", state.AIModel)
|
result += fmt.Sprintf("• AI Model: %s\n", state.AIModel)
|
||||||
}
|
}
|
||||||
@@ -334,6 +358,13 @@ func (a *Agent) createTraderFromSetup(state *SetupState) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func maskKey(key string) string {
|
||||||
|
if len(key) <= 8 {
|
||||||
|
return "****"
|
||||||
|
}
|
||||||
|
return key[:4] + "****" + key[len(key)-4:]
|
||||||
|
}
|
||||||
|
|
||||||
func needsPassphrase(exchange string) bool {
|
func needsPassphrase(exchange string) bool {
|
||||||
return exchange == "okx" || exchange == "bitget" || exchange == "kucoin"
|
return exchange == "okx" || exchange == "bitget" || exchange == "kucoin"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user