mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-12 15:26:55 +08:00
change v1
This commit is contained in:
27
agent/web.go
27
agent/web.go
@@ -13,6 +13,14 @@ import (
|
||||
)
|
||||
|
||||
type storeUserIDContextKey struct{}
|
||||
type sessionPolicyContextKey struct{}
|
||||
|
||||
type SessionPolicy struct {
|
||||
Authenticated bool
|
||||
IsAdmin bool
|
||||
CanExecuteTrade bool
|
||||
CanViewSensitiveSecrets bool
|
||||
}
|
||||
|
||||
// WithStoreUserID annotates an HTTP request context with the authenticated store user ID.
|
||||
func WithStoreUserID(ctx context.Context, storeUserID string) context.Context {
|
||||
@@ -26,6 +34,17 @@ func storeUserIDFromContext(ctx context.Context) string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
func WithSessionPolicy(ctx context.Context, policy SessionPolicy) context.Context {
|
||||
return context.WithValue(ctx, sessionPolicyContextKey{}, policy)
|
||||
}
|
||||
|
||||
func sessionPolicyFromContext(ctx context.Context) SessionPolicy {
|
||||
if v, ok := ctx.Value(sessionPolicyContextKey{}).(SessionPolicy); ok {
|
||||
return v
|
||||
}
|
||||
return SessionPolicy{}
|
||||
}
|
||||
|
||||
// validSymbolRe matches only alphanumeric trading symbols (e.g. BTCUSDT, ETH-USD).
|
||||
var validSymbolRe = regexp.MustCompile(`^[A-Za-z0-9\-_]{1,20}$`)
|
||||
|
||||
@@ -80,7 +99,7 @@ func (w *WebHandler) HandleChat(rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if req.UserID == 0 {
|
||||
req.UserID = SessionUserIDFromKey(req.UserKey)
|
||||
req.UserID = SessionUserIDFromKey(storeUserIDFromContext(r.Context()))
|
||||
}
|
||||
msg := req.Message
|
||||
if req.Lang != "" {
|
||||
@@ -93,7 +112,7 @@ func (w *WebHandler) HandleChat(rw http.ResponseWriter, r *http.Request) {
|
||||
resp, err := w.agent.HandleMessageForStoreUser(ctx, storeUserIDFromContext(r.Context()), req.UserID, msg)
|
||||
if err != nil {
|
||||
w.logger.Error("agent HandleMessage failed", "error", err, "user_id", req.UserID)
|
||||
writeJSON(rw, 500, map[string]string{"error": "Failed to process message. Please try again."})
|
||||
writeJSON(rw, 500, map[string]string{"error": "I ran into a problem while handling that message. Please try again."})
|
||||
return
|
||||
}
|
||||
writeJSON(rw, 200, map[string]string{"response": resp})
|
||||
@@ -122,7 +141,7 @@ func (w *WebHandler) HandleChatStream(rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if req.UserID == 0 {
|
||||
req.UserID = SessionUserIDFromKey(req.UserKey)
|
||||
req.UserID = SessionUserIDFromKey(storeUserIDFromContext(r.Context()))
|
||||
}
|
||||
msg := req.Message
|
||||
if req.Lang != "" {
|
||||
@@ -150,7 +169,7 @@ func (w *WebHandler) HandleChatStream(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil {
|
||||
w.logger.Error("agent HandleMessageStream failed", "error", err, "user_id", req.UserID)
|
||||
writeSSE(rw, flusher, "error", "Failed to process message. Please try again.")
|
||||
writeSSE(rw, flusher, "error", "I ran into a problem while handling that message. Please try again.")
|
||||
return
|
||||
}
|
||||
// Send final done event with complete response
|
||||
|
||||
Reference in New Issue
Block a user