增加调试前端页面

This commit is contained in:
andy committed 2026-09-06 00:25:25 +08:00
1 parent 3080b17d02
commit ee9d2d7cf1
22 files changed
+1442 -59

No files matched your search

+18
View File
@@ -26,6 +26,7 @@ const (
SuperAgentProbeTimeoutEnv = "FIRE_SAFETY_SUPERAGENT_PROBE_TIMEOUT"
ChatEnabledEnv = "FIRE_SAFETY_CHAT_ENABLED"
ChatPageEnabledEnv = "FIRE_SAFETY_CHAT_PAGE_ENABLED"
ChatAuthTokenEnv = "FIRE_SAFETY_CHAT_AUTH_TOKEN"
ChatAllowLegacyShortTokenEnv = "FIRE_SAFETY_CHAT_ALLOW_LEGACY_SHORT_TOKEN"
ChatSubjectIDEnv = "FIRE_SAFETY_CHAT_SUBJECT_ID"
@@ -119,6 +120,7 @@ type SuperAgentConfig struct {
// final end-user authentication.
type ChatConfig struct {
Enabled bool
PageEnabled bool
AuthToken string
AllowLegacyShortToken bool
SubjectID string
@@ -231,6 +233,10 @@ func loadChatConfig() (ChatConfig, error) {
if err != nil {
return ChatConfig{}, err
}
pageEnabled, err := parseBool(ChatPageEnabledEnv, false)
if err != nil {
return ChatConfig{}, err
}
allowLegacyShortToken, err := parseBool(ChatAllowLegacyShortTokenEnv, false)
if err != nil {
return ChatConfig{}, err
@@ -258,6 +264,7 @@ func loadChatConfig() (ChatConfig, error) {
cfg := ChatConfig{
Enabled: enabled,
PageEnabled: pageEnabled,
AuthToken: os.Getenv(ChatAuthTokenEnv),
AllowLegacyShortToken: allowLegacyShortToken,
SubjectID: valueOrDefault(ChatSubjectIDEnv, defaultChatSubjectID),
@@ -492,6 +499,17 @@ func validateMCPDependencies(mcp MCPConfig, postGIS PostGISConfig, superAgent Su
}
func validateChatDependencies(chat ChatConfig, superAgent SuperAgentConfig, mcp MCPConfig) error {
if chat.PageEnabled {
if !chat.Enabled {
return fmt.Errorf("%s must be true when %s is true", ChatEnabledEnv, ChatPageEnabledEnv)
}
if chat.CompatAppID == "" {
return fmt.Errorf("%s is required when %s is true", ChatCompatAppIDEnv, ChatPageEnabledEnv)
}
if len(chat.AllowedOrigins) == 0 {
return fmt.Errorf("%s must contain at least one exact HTTP(S) origin when %s is true", ChatAllowedOriginsEnv, ChatPageEnabledEnv)
}
}
if !chat.Enabled {
return nil
}
+62 -1
View File
@@ -41,6 +41,9 @@ func TestLoadDefaults(t *testing.T) {
if cfg.Chat.Enabled {
t.Fatal("Chat.Enabled = true, want false")
}
if cfg.Chat.PageEnabled {
t.Fatal("Chat.PageEnabled = true, want false")
}
if cfg.Chat.AllowLegacyShortToken {
t.Fatal("Chat.AllowLegacyShortToken = true, want false")
}
@@ -68,6 +71,7 @@ func TestLoadConfiguredChat(t *testing.T) {
t.Setenv(SuperAgentBaseURLEnv, "https://superagent.example.test")
t.Setenv(SuperAgentOpenAPIKeyEnv, "superagent-test-open-api-key")
t.Setenv(ChatEnabledEnv, "true")
t.Setenv(ChatPageEnabledEnv, "true")
t.Setenv(ChatAuthTokenEnv, "0123456789abcdef0123456789abcdef")
t.Setenv(ChatSubjectIDEnv, " local-chat-test-subject ")
t.Setenv(ChatCompatAppIDEnv, " fire-safety-public-app ")
@@ -81,7 +85,7 @@ func TestLoadConfiguredChat(t *testing.T) {
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if !cfg.Chat.Enabled || cfg.Chat.AuthToken == "" || cfg.Chat.SubjectID != "local-chat-test-subject" || cfg.Chat.CompatAppID != "fire-safety-public-app" {
if !cfg.Chat.Enabled || !cfg.Chat.PageEnabled || cfg.Chat.AuthToken == "" || cfg.Chat.SubjectID != "local-chat-test-subject" || cfg.Chat.CompatAppID != "fire-safety-public-app" {
t.Fatalf("unexpected Chat config: %#v", cfg.Chat)
}
if got, want := strings.Join(cfg.Chat.AllowedOrigins, ","), "http://localhost:5173,https://fire.example.test"; got != want {
@@ -213,6 +217,7 @@ func TestLoadRejectsInvalidValues(t *testing.T) {
{name: "probe timeout syntax", key: SuperAgentProbeTimeoutEnv, value: "forever"},
{name: "probe timeout non-positive", key: SuperAgentProbeTimeoutEnv, value: "0s"},
{name: "Chat boolean", key: ChatEnabledEnv, value: "sometimes"},
{name: "Chat page boolean", key: ChatPageEnabledEnv, value: "sometimes"},
{name: "Chat legacy short token boolean", key: ChatAllowLegacyShortTokenEnv, value: "sometimes"},
{name: "Chat body syntax", key: ChatMaxBodyBytesEnv, value: "large"},
{name: "Chat body non-positive", key: ChatMaxBodyBytesEnv, value: "0"},
@@ -256,6 +261,61 @@ func TestLoadRejectsInvalidValues(t *testing.T) {
}
}
func TestLoadRequiresChatPageDependencies(t *testing.T) {
configureEnabledChat := func(t *testing.T) {
t.Helper()
t.Setenv(SuperAgentEnabledEnv, "true")
t.Setenv(SuperAgentBaseURLEnv, "https://superagent.example.test")
t.Setenv(SuperAgentOpenAPIKeyEnv, "superagent-open-api-key")
t.Setenv(ChatEnabledEnv, "true")
t.Setenv(ChatAuthTokenEnv, "0123456789abcdef0123456789abcdef")
}
tests := []struct {
name string
configure func(*testing.T)
wantErrKey string
}{
{
name: "chat disabled",
configure: func(t *testing.T) {
t.Setenv(ChatCompatAppIDEnv, "fire-safety-app")
t.Setenv(ChatAllowedOriginsEnv, "https://fire.example.test")
},
wantErrKey: ChatEnabledEnv,
},
{
name: "missing compatibility app ID",
configure: func(t *testing.T) {
configureEnabledChat(t)
t.Setenv(ChatAllowedOriginsEnv, "https://fire.example.test")
},
wantErrKey: ChatCompatAppIDEnv,
},
{
name: "missing allowed origin",
configure: func(t *testing.T) {
configureEnabledChat(t)
t.Setenv(ChatCompatAppIDEnv, "fire-safety-app")
},
wantErrKey: ChatAllowedOriginsEnv,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clearEnvironment(t)
t.Setenv(ChatPageEnabledEnv, "true")
tt.configure(t)
_, err := Load()
if err == nil || !strings.Contains(err.Error(), tt.wantErrKey) {
t.Fatalf("Load() error = %v, want error mentioning %s", err, tt.wantErrKey)
}
})
}
}
func TestLoadRequiresEnabledChatSettings(t *testing.T) {
tests := []struct {
name string
@@ -521,6 +581,7 @@ func clearEnvironment(t *testing.T) {
SuperAgentProbeSubjectIDEnv,
SuperAgentProbeTimeoutEnv,
ChatEnabledEnv,
ChatPageEnabledEnv,
ChatAuthTokenEnv,
ChatAllowLegacyShortTokenEnv,
ChatSubjectIDEnv,