diff --git a/.env.example b/.env.example index a569a66..974a3ec 100644 --- a/.env.example +++ b/.env.example @@ -8,5 +8,17 @@ DOBOTAI_AGENT_BINDINGS={"dingtalk:*":"replace-with-agent-id"} WEKNORA_BASE_URL=http://127.0.0.1:8080 WEKNORA_API_KEY=replace-me +# DingTalk Stream mode (official SDK). Leave all three empty to disable the +# DingTalk channel; set all three to enable it. Never commit real values. +DINGTALK_CLIENT_ID= +DINGTALK_CLIENT_SECRET= +# Tenant key this DingTalk ingress serves (matches tenant_key used by QA). +DOBOTAI_TENANT_KEY= + +# Optional DingTalk AI card template ID. Leave empty to use markdown-only replies. +# When set, a successful QA answer will be delivered as an AI card; markdown +# is only sent as a fallback when card delivery fails. Never commit real values. +DINGTALK_CARD_TEMPLATE_ID= + # Independent DobotAI PostgreSQL; never point this at WeKnora's database DATABASE_URL=postgres://dobotai:replace-me@127.0.0.1:5432/dobotai?sslmode=disable diff --git a/README.md b/README.md index 631db9b..5bc5b41 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,16 @@ Implemented: - external-message idempotency; - independent PostgreSQL session/turn storage; - authenticated internal `POST /v1/qa` ingress endpoint. +- DingTalk Stream text ingress (official SDK) wired into `cmd/dobotai`. + +## DingTalk channel (Stream, text only) + +- Uses the official DingTalk Stream SDK (`dingtalk-stream-sdk-go`); no public webhook endpoint is exposed. +- Text messages only for now. Images, OpenAPI fallback, and card replies are not implemented yet. +- Group chats are handled only when the bot is @-mentioned; other group messages are ignored. +- Sessions: direct chats use one long-lived session per user; group chats share one session per group (default `shared` policy). +- Replies are sent via the callback's `sessionWebhook`, which is fail-closed: only `https` URLs on `*.dingtalk.com` (port 443, no redirects) are accepted. +- Required env vars to enable (all three or none): `DINGTALK_CLIENT_ID`, `DINGTALK_CLIENT_SECRET`, `DOBOTAI_TENANT_KEY`. Partial configuration is a fatal startup error. ## WeKnora baseline diff --git a/cmd/dobotai/main.go b/cmd/dobotai/main.go index 458245d..a5f40dc 100644 --- a/cmd/dobotai/main.go +++ b/cmd/dobotai/main.go @@ -12,6 +12,7 @@ import ( "time" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/agentbinding" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/channels/dingtalk" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/httpapi" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/qa" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/session" @@ -52,6 +53,52 @@ func main() { if err != nil { log.Fatal(err) } + + dingCfg := dingtalk.StreamConfig{ + ClientID: os.Getenv("DINGTALK_CLIENT_ID"), + ClientSecret: os.Getenv("DINGTALK_CLIENT_SECRET"), + TenantKey: os.Getenv("DOBOTAI_TENANT_KEY"), + } + if err := dingCfg.Validate(); err != nil { + log.Fatal(err) + } + var streamClient *dingtalk.StreamClient + if dingCfg.Enabled() { + sender, err := dingtalk.NewWebhookSender(nil) + if err != nil { + log.Fatal(err) + } + downloader, err := dingtalk.NewImageDownloader(dingCfg.ClientID, dingCfg.ClientSecret, nil) + if err != nil { + log.Fatal(err) + } + var cardSender dingtalk.FinalCardSender + if cardTemplateID := strings.TrimSpace(os.Getenv("DINGTALK_CARD_TEMPLATE_ID")); cardTemplateID != "" { + cs, err := dingtalk.NewCardSender(dingCfg.ClientID, dingCfg.ClientSecret, cardTemplateID, nil) + if err != nil { + log.Fatalf("DingTalk card sender init failed: %v", err) + } + cardSender = cs + } + handler, err := dingtalk.NewHandlerWithOptions( + dingtalk.TenantConfig{TenantKey: dingCfg.TenantKey, Channel: "dingtalk"}, + qaService, + sender, + dingtalk.HandlerOptions{ImageDownloader: downloader, CardSender: cardSender}, + ) + if err != nil { + log.Fatal(err) + } + streamClient, err = dingtalk.NewStreamClient(dingCfg, handler) + if err != nil { + log.Fatal(err) + } + if err := streamClient.Start(ctx); err != nil { + log.Fatal(err) + } + defer streamClient.Close() + log.Printf("DingTalk Stream ingress started for tenant %s", dingCfg.TenantKey) + } address := strings.TrimSpace(os.Getenv("DOBOTAI_LISTEN_ADDR")) if address == "" { address = "127.0.0.1:8090" diff --git a/db/migrations/000001_core.up.sql b/db/migrations/000001_core.up.sql index ac7ff85..bfe35e9 100644 --- a/db/migrations/000001_core.up.sql +++ b/db/migrations/000001_core.up.sql @@ -1,4 +1,4 @@ -CREATE TABLE session_mappings ( +CREATE TABLE IF NOT EXISTS session_mappings ( session_key TEXT PRIMARY KEY, tenant_key TEXT NOT NULL, channel TEXT NOT NULL, @@ -10,10 +10,10 @@ CREATE TABLE session_mappings ( updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); -CREATE INDEX idx_session_mappings_tenant_channel +CREATE INDEX IF NOT EXISTS idx_session_mappings_tenant_channel ON session_mappings (tenant_key, channel, external_subject_id); -CREATE TABLE turns ( +CREATE TABLE IF NOT EXISTS turns ( id TEXT PRIMARY KEY, tenant_key TEXT NOT NULL, channel TEXT NOT NULL, @@ -23,7 +23,7 @@ CREATE TABLE turns ( weknora_request_id TEXT NOT NULL DEFAULT '', question TEXT NOT NULL, answer TEXT NOT NULL DEFAULT '', - references JSONB NOT NULL DEFAULT '[]'::JSONB, + references_json JSONB NOT NULL DEFAULT '[]'::JSONB, status TEXT NOT NULL CHECK (status IN ('processing','succeeded','failed','cancelled')), error_message TEXT NOT NULL DEFAULT '', created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), @@ -31,5 +31,5 @@ CREATE TABLE turns ( UNIQUE (tenant_key, channel, external_message_id) ); -CREATE INDEX idx_turns_tenant_created ON turns (tenant_key, created_at DESC); -CREATE INDEX idx_turns_session_created ON turns (session_key, created_at DESC); +CREATE INDEX IF NOT EXISTS idx_turns_tenant_created ON turns (tenant_key, created_at DESC); +CREATE INDEX IF NOT EXISTS idx_turns_session_created ON turns (session_key, created_at DESC); diff --git a/db/migrations/000001_core_contract_test.go b/db/migrations/000001_core_contract_test.go new file mode 100644 index 0000000..4b855ec --- /dev/null +++ b/db/migrations/000001_core_contract_test.go @@ -0,0 +1,91 @@ +// Package migrations_test contains source-level contract tests for the SQL +// migration files. They guard against the PostgreSQL 16 reserved-word +// failure (`references JSONB`) and guarantee the migration can be safely +// re-run from a half-applied state (session_mappings + its index created, +// turns not created). No real database connection is used. +package migrations_test + +import ( + "os" + "path/filepath" + "regexp" + "strings" + "testing" +) + +func readUpMigration(t *testing.T) string { + t.Helper() + data, err := os.ReadFile(filepath.Join(".", "000001_core.up.sql")) + if err != nil { + t.Fatalf("read up migration: %v", err) + } + return string(data) +} + +func readDownMigration(t *testing.T) string { + t.Helper() + data, err := os.ReadFile(filepath.Join(".", "000001_core.down.sql")) + if err != nil { + t.Fatalf("read down migration: %v", err) + } + return string(data) +} + +// TestUpMigrationUsesIfNotExists enforces idempotency: every CREATE TABLE and +// CREATE INDEX must use IF NOT EXISTS so a half-applied migration (e.g. +// session_mappings already created, turns missing) can be re-run cleanly. +func TestUpMigrationUsesIfNotExists(t *testing.T) { + up := readUpMigration(t) + + for _, stmt := range splitStatements(up) { + upper := strings.ToUpper(stmt) + if strings.Contains(upper, "CREATE TABLE") && !strings.Contains(upper, "CREATE TABLE IF NOT EXISTS") { + t.Errorf("CREATE TABLE without IF NOT EXISTS:\n%s", strings.TrimSpace(stmt)) + } + if strings.Contains(upper, "CREATE INDEX") && !strings.Contains(upper, "CREATE INDEX IF NOT EXISTS") { + t.Errorf("CREATE INDEX without IF NOT EXISTS:\n%s", strings.TrimSpace(stmt)) + } + } + + if got := countMatches(up, `(?i)CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS`); got != 2 { + t.Errorf("expected 2 CREATE TABLE IF NOT EXISTS statements, got %d", got) + } + if got := countMatches(up, `(?i)CREATE\s+INDEX\s+IF\s+NOT\s+EXISTS`); got != 3 { + t.Errorf("expected 3 CREATE INDEX IF NOT EXISTS statements, got %d", got) + } +} + +// TestUpMigrationAvoidsReservedWordReferences ensures the turns table does +// not declare a bare `references JSONB` column. REFERENCES is a reserved word +// in PostgreSQL 16 and fails with "syntax error at or near references" +// unless quoted. The column must be named references_json. +func TestUpMigrationAvoidsReservedWordReferences(t *testing.T) { + up := readUpMigration(t) + + if regexp.MustCompile(`(?im)^\s*references\s+JSONB`).MatchString(up) { + t.Error("up migration declares reserved-word column `references JSONB`; rename it to references_json") + } + if !regexp.MustCompile(`(?im)^\s*references_json\s+JSONB\s+NOT\s+NULL\s+DEFAULT\s+'\[\]'::JSONB`).MatchString(up) { + t.Error("up migration must declare column `references_json JSONB NOT NULL DEFAULT '[]'::JSONB`") + } +} + +// TestDownMigrationDropsBothTables guards the rollback path stays droppable. +func TestDownMigrationDropsBothTables(t *testing.T) { + down := readDownMigration(t) + upper := strings.ToUpper(down) + if !strings.Contains(upper, "DROP TABLE IF EXISTS TURNS") { + t.Error("down migration must drop turns") + } + if !strings.Contains(upper, "DROP TABLE IF EXISTS SESSION_MAPPINGS") { + t.Error("down migration must drop session_mappings") + } +} + +func splitStatements(sql string) []string { + return strings.Split(sql, ";") +} + +func countMatches(haystack, pattern string) int { + return len(regexp.MustCompile(pattern).FindAllString(haystack, -1)) +} diff --git a/db/migrations/000002_turn_media.down.sql b/db/migrations/000002_turn_media.down.sql new file mode 100644 index 0000000..22adb4c --- /dev/null +++ b/db/migrations/000002_turn_media.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE turns + DROP COLUMN IF EXISTS media_json; diff --git a/db/migrations/000002_turn_media.up.sql b/db/migrations/000002_turn_media.up.sql new file mode 100644 index 0000000..6fd892d --- /dev/null +++ b/db/migrations/000002_turn_media.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE turns + ADD COLUMN IF NOT EXISTS media_json JSONB NOT NULL DEFAULT '[]'::JSONB; diff --git a/db/migrations/000002_turn_media_contract_test.go b/db/migrations/000002_turn_media_contract_test.go new file mode 100644 index 0000000..50eda2e --- /dev/null +++ b/db/migrations/000002_turn_media_contract_test.go @@ -0,0 +1,80 @@ +// Package migrations_test contains source-level contract tests for the +// 000002_turn_media migration. No real database connection is used. +package migrations_test + +import ( + "os" + "path/filepath" + "regexp" + "strings" + "testing" +) + +func readMediaUpMigration(t *testing.T) string { + t.Helper() + data, err := os.ReadFile(filepath.Join(".", "000002_turn_media.up.sql")) + if err != nil { + t.Fatalf("read up migration: %v", err) + } + return string(data) +} + +func readMediaDownMigration(t *testing.T) string { + t.Helper() + data, err := os.ReadFile(filepath.Join(".", "000002_turn_media.down.sql")) + if err != nil { + t.Fatalf("read down migration: %v", err) + } + return string(data) +} + +// TestUpMediaMigrationAddsMediaJSONColumn enforces that the up migration +// adds a media_json JSONB column to the turns table with appropriate defaults. +func TestUpMediaMigrationAddsMediaJSONColumn(t *testing.T) { + up := readMediaUpMigration(t) + upper := strings.ToUpper(up) + + if !strings.Contains(upper, "ADD COLUMN IF NOT EXISTS MEDIA_JSON") { + t.Error("up migration must use ADD COLUMN IF NOT EXISTS media_json") + } + if !strings.Contains(upper, "JSONB NOT NULL DEFAULT '[]'::JSONB") { + t.Error("up migration must declare media_json JSONB NOT NULL DEFAULT '[]'::JSONB") + } +} + +// TestUpMediaMigrationIsIdempotent enforces that the up migration uses IF NOT +// EXISTS so it can be safely re-run. +func TestUpMediaMigrationIsIdempotent(t *testing.T) { + up := readMediaUpMigration(t) + upper := strings.ToUpper(up) + + if !strings.Contains(upper, "IF NOT EXISTS") { + t.Error("up migration must use IF NOT EXISTS for idempotency") + } +} + +// TestDownMediaMigrationDropsColumn enforces the down migration drops the +// media_json column with IF EXISTS. +func TestDownMediaMigrationDropsColumn(t *testing.T) { + down := readMediaDownMigration(t) + upper := strings.ToUpper(down) + + if !strings.Contains(upper, "DROP COLUMN IF EXISTS MEDIA_JSON") { + t.Error("down migration must use DROP COLUMN IF EXISTS media_json") + } +} + +// TestUpMigrationHasNoReservedWordMedia ensures the column is named media_json +// not a bare "media" which could be a reserved word in future PostgreSQL. +func TestUpMigrationHasNoReservedWordMedia(t *testing.T) { + up := readMediaUpMigration(t) + + // The bare word "media" as a column name (not media_json) should be avoided + bare := regexp.MustCompile(`(?i)\bMEDIA\s+JSONB\b`) + if bare.MatchString(up) { + t.Error("up migration must not use bare 'media' column name; use media_json") + } + if !regexp.MustCompile(`(?i)\bMEDIA_JSON\s+JSONB\b`).MatchString(up) { + t.Error("up migration must use media_json column name") + } +} diff --git a/deploy/systemd/dobotai.service b/deploy/systemd/dobotai.service new file mode 100644 index 0000000..2c1f530 --- /dev/null +++ b/deploy/systemd/dobotai.service @@ -0,0 +1,20 @@ +[Unit] +Description=DobotAI external DingTalk and WeKnora gateway +Documentation=file:///home/zheromiku/dobotai-platform/README.md + +[Service] +Type=simple +WorkingDirectory=/home/zheromiku/dobotai-platform +ExecStart=/usr/bin/python3 /home/zheromiku/dobotai-platform/scripts/run-local.py +Restart=on-failure +RestartSec=5s +TimeoutStopSec=20s +KillMode=control-group +NoNewPrivileges=true +PrivateTmp=true +UMask=0077 +StandardOutput=journal +StandardError=journal + +[Install] +WantedBy=default.target diff --git a/docs/architecture.md b/docs/architecture.md index a9262b5..7415f28 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -56,6 +56,7 @@ No WeKnora source patch is allowed as a workaround. - `internal/qa`: business orchestration and terminal-state ownership. - `internal/session`: external-to-WeKnora session policy. - `internal/store/postgres`: DobotAI business persistence only. +- `internal/channels/dingtalk`: DingTalk Stream ingress (official SDK), text-only robot callbacks, fail-closed session webhook replies (`https`, `*.dingtalk.com`, no redirects). Wired in `cmd/dobotai`; enabled only when `DINGTALK_CLIENT_ID`, `DINGTALK_CLIENT_SECRET`, and `DOBOTAI_TENANT_KEY` are all set. Group messages require an @-mention; direct chats map to one long-lived session per user, groups to one shared session per group. Images, OpenAPI fallback, and card replies are not implemented yet. - future `channels/*`: platform callbacks and outbound replies. - future `content-sync/*`: DingTalk KB, NAS, and CMS synchronization. - model gateway: token accounting, routing, quotas, circuit breaking, and provider observability. diff --git a/go.mod b/go.mod index f77f862..0776142 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,14 @@ module gitea.zheromiku.top/Weknora/dobotai-platform go 1.25.0 -require github.com/jackc/pgx/v5 v5.9.2 +require ( + github.com/jackc/pgx/v5 v5.9.2 + github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1 +) require ( + github.com/google/uuid v1.3.0 // indirect + github.com/gorilla/websocket v1.5.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect diff --git a/go.sum b/go.sum index 6553d58..7cebbcf 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= @@ -7,13 +13,20 @@ github.com/jackc/pgx/v5 v5.9.2 h1:3ZhOzMWnR4yJ+RW1XImIPsD1aNSz4T4fyP7zlQb56hw= github.com/jackc/pgx/v5 v5.9.2/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1 h1:Lb/Uzkiw2Ugt2Xf03J5wmv81PdkYOiWbI8CNBi1boC8= +github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1/go.mod h1:ln3IqPYYocZbYvl9TAOrG/cxGR9xcn4pnZRLdCTEGEU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4= golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc= golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/channels/dingtalk/card.go b/internal/channels/dingtalk/card.go new file mode 100644 index 0000000..bc7bafe --- /dev/null +++ b/internal/channels/dingtalk/card.go @@ -0,0 +1,351 @@ +package dingtalk + +import ( + "bytes" + "context" + "crypto/rand" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "strings" + "sync" + "time" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" +) + +const ( + cardCreateURL = "https://api.dingtalk.com/v1.0/card/instances/createAndDeliver" + cardStreamingURL = "https://api.dingtalk.com/v1.0/card/streaming" +) + +// CardRequest is the input for sending a final AI card. +type CardRequest struct { + ConversationType core.ConversationType + ExternalUserID string + ExternalChatID string + Content string +} + +// CardSender sends DingTalk AI cards via the OpenAPI. +type CardSender struct { + clientID string + clientSecret string + cardTemplateID string + tokenURL string + createURL string + streamURL string + apiClient *http.Client + allowTestURL bool + + mu sync.Mutex + token string + tokenTill time.Time +} + +// NewCardSender creates a CardSender. All three config values are required +// after trimming whitespace. A nil http.Client installs a hardened default +// (no redirects, Proxy=nil, 15s timeout). +func NewCardSender(clientID, clientSecret, cardTemplateID string, client *http.Client) (*CardSender, error) { + return newCardSender(clientID, clientSecret, cardTemplateID, client, oauthTokenURL, cardCreateURL, cardStreamingURL, false) +} + +// newCardSenderForTest is used by tests to inject httptest URLs. +func newCardSenderForTest(clientID, clientSecret, cardTemplateID string, client *http.Client, tokenURL, createURL string) (*CardSender, error) { + streamURL := strings.TrimSuffix(createURL, "/create") + "/stream" + return newCardSender(clientID, clientSecret, cardTemplateID, client, tokenURL, createURL, streamURL, true) +} + +func newCardSender(clientID, clientSecret, cardTemplateID string, client *http.Client, tokenURL, createURL, streamURL string, allowTest bool) (*CardSender, error) { + clientID = strings.TrimSpace(clientID) + clientSecret = strings.TrimSpace(clientSecret) + cardTemplateID = strings.TrimSpace(cardTemplateID) + if clientID == "" { + return nil, errors.New("dingtalk card sender: clientID is required") + } + if clientSecret == "" { + return nil, errors.New("dingtalk card sender: clientSecret is required") + } + if cardTemplateID == "" { + return nil, errors.New("dingtalk card sender: cardTemplateID is required") + } + + base := &http.Client{Timeout: 15 * time.Second} + if client != nil { + copyClient := *client + base = ©Client + if base.Timeout <= 0 { + base.Timeout = 15 * time.Second + } + } + + apiClient := *base + apiClient.CheckRedirect = func(*http.Request, []*http.Request) error { + return errors.New("dingtalk API redirect refused") + } + if !allowTest { + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.Proxy = nil + apiClient.Transport = transport + } + + return &CardSender{ + clientID: clientID, + clientSecret: clientSecret, + cardTemplateID: cardTemplateID, + tokenURL: tokenURL, + createURL: createURL, + streamURL: streamURL, + apiClient: &apiClient, + allowTestURL: allowTest, + }, nil +} + +// SendFinalCard sends the AI card to DingTalk and returns the generated outTrackID. +// It is a thin wrapper: StartCard → UpdateCard(finalize=true). +func (s *CardSender) SendFinalCard(ctx context.Context, req CardRequest) (string, error) { + outTrackID, err := s.StartCard(ctx, req) + if err != nil { + return "", err + } + if err := s.UpdateCard(ctx, outTrackID, req.Content, true, false); err != nil { + return "", err + } + return outTrackID, nil +} + +// StartCard creates a card instance via createAndDeliver and returns the +// generated outTrackID. It does NOT call the streaming finalize endpoint; +// callers must use UpdateCard to stream content. +func (s *CardSender) StartCard(ctx context.Context, req CardRequest) (string, error) { + // Validate conversation type before any API call + if req.ConversationType != core.ConversationDirect && req.ConversationType != core.ConversationGroup { + return "", fmt.Errorf("dingtalk card: unsupported conversation type %q", req.ConversationType) + } + + // Validate content + req.Content = strings.TrimSpace(req.Content) + if req.Content == "" { + return "", errors.New("dingtalk card: content is required") + } + if len(req.Content) > 64*1024 { + return "", errors.New("dingtalk card: content exceeds 64 KiB") + } + + // Validate identity fields + if req.ConversationType == core.ConversationDirect { + req.ExternalUserID = strings.TrimSpace(req.ExternalUserID) + if req.ExternalUserID == "" { + return "", errors.New("dingtalk card: ExternalUserID is required for direct conversations") + } + } else { + req.ExternalChatID = strings.TrimSpace(req.ExternalChatID) + if req.ExternalChatID == "" { + return "", errors.New("dingtalk card: ExternalChatID is required for group conversations") + } + } + + // Generate outTrackID + outTrackID, err := generateUUID() + if err != nil { + return "", fmt.Errorf("dingtalk card: generate outTrackId: %w", err) + } + + // Build card body + body := s.buildCardBody(req, outTrackID) + + // Get access token + token, err := s.accessToken(ctx) + if err != nil { + return "", err + } + + // Send card (create only, no finalize) + if err := s.postCard(ctx, body, token); err != nil { + return "", err + } + + return outTrackID, nil +} + +// UpdateCard sends a streaming update to an existing card. outTrackID and +// content must be non-empty after trimming; content must not exceed 64 KiB. +// Set isFinalize=true to close the card; set isError=true to mark the card +// as an error state. +func (s *CardSender) UpdateCard(ctx context.Context, outTrackID, content string, isFinalize, isError bool) error { + outTrackID = strings.TrimSpace(outTrackID) + if outTrackID == "" { + return errors.New("dingtalk card: outTrackID is required") + } + content = strings.TrimSpace(content) + if content == "" { + return errors.New("dingtalk card: content is required") + } + if len(content) > 64*1024 { + return errors.New("dingtalk card: content exceeds 64 KiB") + } + + token, err := s.accessToken(ctx) + if err != nil { + return err + } + + return s.finalizeCard(ctx, outTrackID, content, token, isFinalize, isError) +} + +func (s *CardSender) buildCardBody(req CardRequest, outTrackID string) map[string]any { + body := map[string]any{ + "cardTemplateId": s.cardTemplateID, + "outTrackId": outTrackID, + "callbackType": "STREAM", + "cardData": map[string]any{ + "cardParamMap": map[string]any{ + "content": req.Content, + }, + }, + "userIdType": 1, + } + + if req.ConversationType == core.ConversationDirect { + body["openSpaceId"] = "dtv1.card//IM_ROBOT." + req.ExternalUserID + body["imRobotOpenSpaceModel"] = map[string]any{ + "supportForward": true, + } + body["imRobotOpenDeliverModel"] = map[string]any{ + "robotCode": s.clientID, + "spaceType": "IM_ROBOT", + "extension": map[string]any{}, + } + } else { + body["openSpaceId"] = "dtv1.card//IM_GROUP." + req.ExternalChatID + body["imGroupOpenSpaceModel"] = map[string]any{ + "supportForward": true, + } + body["imGroupOpenDeliverModel"] = map[string]any{ + "robotCode": s.clientID, + "extension": map[string]any{}, + } + } + return body +} + +func (s *CardSender) accessToken(ctx context.Context) (string, error) { + s.mu.Lock() + defer s.mu.Unlock() + if s.token != "" && time.Now().Before(s.tokenTill) { + return s.token, nil + } + + payload, err := s.postJSON(ctx, s.tokenURL, map[string]string{ + "appKey": s.clientID, + "appSecret": s.clientSecret, + }, "") + if err != nil { + return "", err + } + + var response struct { + AccessToken string `json:"accessToken"` + ExpireIn int64 `json:"expireIn"` + } + if json.Unmarshal(payload, &response) != nil || strings.TrimSpace(response.AccessToken) == "" { + return "", errors.New("dingtalk card: invalid OAuth response") + } + + ttl := time.Duration(response.ExpireIn) * time.Second + refreshAfter := ttl - 5*time.Minute + if refreshAfter <= 0 { + refreshAfter = ttl / 2 + } + if refreshAfter <= 0 { + refreshAfter = time.Minute + } + s.token = response.AccessToken + s.tokenTill = time.Now().Add(refreshAfter) + return s.token, nil +} + +func (s *CardSender) postCard(ctx context.Context, body map[string]any, token string) error { + payload, err := s.postJSON(ctx, s.createURL, body, token) + if err != nil { + return err + } + // Discard successful response body + if len(payload) > 0 { + // Body is already bounded by postJSON→readBoundedAPIResponse; just discard + } + return nil +} + +func (s *CardSender) finalizeCard(ctx context.Context, outTrackID, content, token string, isFinalize, isError bool) error { + guid, err := generateUUID() + if err != nil { + return errors.New("dingtalk card: generate stream guid failed") + } + body := map[string]any{ + "outTrackId": outTrackID, + "guid": guid, + "key": "content", + "content": content, + "isFull": true, + "isFinalize": isFinalize, + "isError": isError, + } + _, err = s.doJSON(ctx, http.MethodPut, s.streamURL, body, token) + return err +} + +func (s *CardSender) postJSON(ctx context.Context, endpoint string, value any, token string) ([]byte, error) { + return s.doJSON(ctx, http.MethodPost, endpoint, value, token) +} + +func (s *CardSender) doJSON(ctx context.Context, method, endpoint string, value any, token string) ([]byte, error) { + body, err := json.Marshal(value) + if err != nil { + return nil, errors.New("dingtalk card: encode API request failed") + } + + req, err := http.NewRequestWithContext(ctx, method, endpoint, bytes.NewReader(body)) + if err != nil { + return nil, errors.New("dingtalk card: create API request failed") + } + req.Header.Set("Content-Type", "application/json") + if token != "" { + req.Header.Set("x-acs-dingtalk-access-token", token) + } + + resp, err := s.apiClient.Do(req) + if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, errors.New("dingtalk card: API request failed") + } + defer resp.Body.Close() + + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + // Discard body without reading it + _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, maxAPIResponseBytes)) + return nil, fmt.Errorf("dingtalk card: API HTTP %d", resp.StatusCode) + } + + return readBoundedAPIResponse(resp) +} + +// generateUUID creates a UUID v4 (random) using crypto/rand, formatted as +// lowercase hex with dashes: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. +func generateUUID() (string, error) { + var b [16]byte + if _, err := rand.Read(b[:]); err != nil { + return "", err + } + // Set version 4 + b[6] = (b[6] & 0x0f) | 0x40 + // Set variant bits (10xx) + b[8] = (b[8] & 0x3f) | 0x80 + + return fmt.Sprintf("%x-%x-%x-%x-%x", + b[0:4], b[4:6], b[6:8], b[8:10], b[10:16]), nil +} diff --git a/internal/channels/dingtalk/card_pump.go b/internal/channels/dingtalk/card_pump.go new file mode 100644 index 0000000..dcdd22a --- /dev/null +++ b/internal/channels/dingtalk/card_pump.go @@ -0,0 +1,91 @@ +package dingtalk + +import ( + "context" + "log" + "sync" + "time" +) + +type cardPumpUpdate func(context.Context, string) error + +type cardUpdatePump struct { + updates chan string + stop chan struct{} + done chan struct{} + once sync.Once +} + +func newCardUpdatePump(ctx context.Context, interval time.Duration, update cardPumpUpdate) *cardUpdatePump { + if interval <= 0 { + interval = 500 * time.Millisecond + } + ticker := time.NewTicker(interval) + return newCardUpdatePumpWithTicks(ctx, ticker.C, ticker.Stop, update) +} + +func newCardUpdatePumpWithTicks(ctx context.Context, ticks <-chan time.Time, stopTicks func(), update cardPumpUpdate) *cardUpdatePump { + p := &cardUpdatePump{ + updates: make(chan string, 1), + stop: make(chan struct{}), + done: make(chan struct{}), + } + go p.run(ctx, ticks, stopTicks, update) + return p +} + +func (p *cardUpdatePump) run(ctx context.Context, ticks <-chan time.Time, stopTicks func(), update cardPumpUpdate) { + defer close(p.done) + if stopTicks != nil { + defer stopTicks() + } + for { + select { + case <-ctx.Done(): + return + case <-p.stop: + return + case <-ticks: + var latest string + select { + case latest = <-p.updates: + default: + continue + } + if err := update(ctx, latest); err != nil { + log.Printf("DingTalk async card update failed, continuing") + } + } + } +} + +// Offer replaces any pending content with the newest full card state. It never +// waits for DingTalk I/O, so the WeKnora SSE reader cannot be back-pressured by +// card delivery latency. +func (p *cardUpdatePump) Offer(content string) { + select { + case <-p.stop: + return + default: + } + select { + case p.updates <- content: + return + default: + } + select { + case <-p.updates: + default: + } + select { + case p.updates <- content: + case <-p.stop: + } +} + +// Stop prevents stale non-final updates from racing with the synchronous final +// card update and waits for any in-flight DingTalk request to finish. +func (p *cardUpdatePump) Stop() { + p.once.Do(func() { close(p.stop) }) + <-p.done +} diff --git a/internal/channels/dingtalk/card_pump_test.go b/internal/channels/dingtalk/card_pump_test.go new file mode 100644 index 0000000..4e4859b --- /dev/null +++ b/internal/channels/dingtalk/card_pump_test.go @@ -0,0 +1,109 @@ +package dingtalk + +import ( + "context" + "errors" + "testing" + "time" +) + +func waitPumpUpdate(t *testing.T, updates <-chan string) string { + t.Helper() + select { + case got := <-updates: + return got + case <-time.After(time.Second): + t.Fatal("timed out waiting for card pump update") + return "" + } +} + +func TestCardUpdatePumpCoalescesToLatestWithoutBlockingOffer(t *testing.T) { + ticks := make(chan time.Time, 2) + observed := make(chan string, 2) + pump := newCardUpdatePumpWithTicks(context.Background(), ticks, nil, func(_ context.Context, content string) error { + observed <- content + return nil + }) + defer pump.Stop() + + pump.Offer("one") + pump.Offer("two") + pump.Offer("latest") + ticks <- time.Now() + if got := waitPumpUpdate(t, observed); got != "latest" { + t.Fatalf("update=%q want latest", got) + } +} + +func TestCardUpdatePumpCoalescesWhileDingTalkRequestIsBlocked(t *testing.T) { + ticks := make(chan time.Time, 3) + started := make(chan string, 2) + release := make(chan struct{}) + pump := newCardUpdatePumpWithTicks(context.Background(), ticks, nil, func(_ context.Context, content string) error { + started <- content + <-release + return nil + }) + + pump.Offer("first") + ticks <- time.Now() + if got := waitPumpUpdate(t, started); got != "first" { + t.Fatalf("first update=%q", got) + } + + // These offers must not block behind the in-flight HTTP request and only the + // newest state should survive. + pump.Offer("second") + pump.Offer("third") + pump.Offer("newest") + close(release) + ticks <- time.Now() + if got := waitPumpUpdate(t, started); got != "newest" { + t.Fatalf("coalesced update=%q want newest", got) + } + pump.Stop() +} + +func TestCardUpdatePumpContinuesAfterUpdateError(t *testing.T) { + ticks := make(chan time.Time, 2) + calls := make(chan string, 2) + count := 0 + pump := newCardUpdatePumpWithTicks(context.Background(), ticks, nil, func(_ context.Context, content string) error { + count++ + calls <- content + if count == 1 { + return errors.New("token and content must not be logged") + } + return nil + }) + defer pump.Stop() + + pump.Offer("first") + ticks <- time.Now() + _ = waitPumpUpdate(t, calls) + pump.Offer("second") + ticks <- time.Now() + if got := waitPumpUpdate(t, calls); got != "second" { + t.Fatalf("second update=%q", got) + } +} + +func TestCardUpdatePumpStopDropsPendingAndIsIdempotent(t *testing.T) { + ticks := make(chan time.Time, 1) + observed := make(chan string, 1) + pump := newCardUpdatePumpWithTicks(context.Background(), ticks, nil, func(_ context.Context, content string) error { + observed <- content + return nil + }) + pump.Offer("stale") + pump.Stop() + pump.Stop() + pump.Offer("after-stop") + + select { + case got := <-observed: + t.Fatalf("unexpected update after stop: %q", got) + default: + } +} diff --git a/internal/channels/dingtalk/card_test.go b/internal/channels/dingtalk/card_test.go new file mode 100644 index 0000000..02369ea --- /dev/null +++ b/internal/channels/dingtalk/card_test.go @@ -0,0 +1,1178 @@ +package dingtalk + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" +) + +// --------------------------------------------------------------------------- +// Helper: start a test server with token + create endpoints +// --------------------------------------------------------------------------- + +type cardTestServer struct { + server *httptest.Server + tokenCalls int + createCalls int + streamCalls int + lastTokenBody map[string]string + lastCreateBody map[string]any + lastCreateHeader string + lastStreamBody map[string]any + lastStreamHeader string + lastStreamMethod string + // configurable responses + tokenResponse any + tokenStatus int + createResponse any + createStatus int + createBodySize int64 // Content-Length for oversized response + streamStatus int + streamBodySize int64 +} + +func newCardTestServer() *cardTestServer { + ts := &cardTestServer{ + tokenStatus: http.StatusOK, + createStatus: http.StatusOK, + streamStatus: http.StatusOK, + tokenResponse: map[string]any{"accessToken": "test-access-token", "expireIn": 3600}, + createResponse: map[string]any{"result": "ok"}, + } + ts.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/token": + ts.tokenCalls++ + if ts.tokenStatus != http.StatusOK { + w.WriteHeader(ts.tokenStatus) + return + } + ts.lastTokenBody = make(map[string]string) + json.NewDecoder(r.Body).Decode(&ts.lastTokenBody) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(ts.tokenResponse) + case "/create": + ts.createCalls++ + ts.lastCreateHeader = r.Header.Get("x-acs-dingtalk-access-token") + if ts.createStatus != http.StatusOK { + w.WriteHeader(ts.createStatus) + return + } + if ts.createBodySize > 0 { + // Write the oversized body; Content-Length is deliberately omitted + // so the reader sees the actual size via LimitReader. + w.WriteHeader(ts.createStatus) + io.Copy(w, io.LimitReader(strings.NewReader(strings.Repeat("x", int(ts.createBodySize))), ts.createBodySize)) + return + } + ts.lastCreateBody = make(map[string]any) + json.NewDecoder(r.Body).Decode(&ts.lastCreateBody) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(ts.createResponse) + case "/stream": + ts.streamCalls++ + ts.lastStreamMethod = r.Method + ts.lastStreamHeader = r.Header.Get("x-acs-dingtalk-access-token") + ts.lastStreamBody = make(map[string]any) + json.NewDecoder(r.Body).Decode(&ts.lastStreamBody) + if ts.streamStatus != http.StatusOK { + w.WriteHeader(ts.streamStatus) + return + } + if ts.streamBodySize > 0 { + w.WriteHeader(ts.streamStatus) + io.Copy(w, io.LimitReader(strings.NewReader(strings.Repeat("x", int(ts.streamBodySize))), ts.streamBodySize)) + return + } + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"result": "ok"}) + default: + http.NotFound(w, r) + } + })) + return ts +} + +func (ts *cardTestServer) Close() { + ts.server.Close() +} + +func (ts *cardTestServer) URL() string { + return ts.server.URL +} + +func (ts *cardTestServer) Client() *http.Client { + return ts.server.Client() +} + +// --------------------------------------------------------------------------- +// Test: missing config +// --------------------------------------------------------------------------- + +func TestCardSenderMissingConfig(t *testing.T) { + tests := []struct { + name, id, secret, template string + }{ + {"empty id", "", "secret", "tpl"}, + {"empty secret", "id", "", "tpl"}, + {"empty template", "id", "secret", ""}, + {"whitespace id", " ", "secret", "tpl"}, + {"whitespace secret", "id", " ", "tpl"}, + {"whitespace template", "id", "secret", " "}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := NewCardSender(tt.id, tt.secret, tt.template, nil) + if err == nil { + t.Fatal("expected error, got nil") + } + if got != nil { + t.Fatal("expected nil sender on error") + } + }) + } +} + +// --------------------------------------------------------------------------- +// Test: direct conversation full payload +// --------------------------------------------------------------------------- + +func TestCardSendDirectPayload(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("client-id", "client-secret", "card-template-123", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + req := CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-456", + Content: "Hello from AI", + } + outTrackID, err := sender.SendFinalCard(context.Background(), req) + if err != nil { + t.Fatalf("SendFinalCard: %v", err) + } + + // Verify outTrackID is UUID format + if !isUUID(outTrackID) { + t.Fatalf("outTrackID is not a UUID: %q", outTrackID) + } + + // Verify token was called + if ts.tokenCalls != 1 { + t.Fatalf("tokenCalls=%d, want 1", ts.tokenCalls) + } + + // Verify OAuth body fields + if ts.lastTokenBody["appKey"] != "client-id" || ts.lastTokenBody["appSecret"] != "client-secret" { + t.Fatalf("OAuth body fields: appKey=%q appSecret=%q", ts.lastTokenBody["appKey"], ts.lastTokenBody["appSecret"]) + } + + // Verify create header + if ts.lastCreateHeader != "test-access-token" { + t.Fatalf("create header token=%q", ts.lastCreateHeader) + } + if ts.streamCalls != 1 { + t.Fatalf("streamCalls=%d, want 1", ts.streamCalls) + } + if ts.lastStreamMethod != http.MethodPut { + t.Fatalf("stream method=%q, want PUT", ts.lastStreamMethod) + } + if ts.lastStreamHeader != "test-access-token" { + t.Fatalf("stream header token=%q", ts.lastStreamHeader) + } + if ts.lastStreamBody["outTrackId"] != outTrackID || ts.lastStreamBody["key"] != "content" || + ts.lastStreamBody["content"] != "Hello from AI" || ts.lastStreamBody["isFull"] != true || + ts.lastStreamBody["isFinalize"] != true || ts.lastStreamBody["isError"] != false { + t.Fatalf("unexpected final stream body: %#v", ts.lastStreamBody) + } + + // Verify create body + body := ts.lastCreateBody + if body["cardTemplateId"] != "card-template-123" { + t.Errorf("cardTemplateId=%v", body["cardTemplateId"]) + } + if body["outTrackId"] != outTrackID { + t.Errorf("outTrackId=%v, want %v (returned value must equal body)", body["outTrackId"], outTrackID) + } + if body["callbackType"] != "STREAM" { + t.Errorf("callbackType=%v", body["callbackType"]) + } + cardData, ok := body["cardData"].(map[string]any) + if !ok { + t.Fatal("cardData missing or wrong type") + } + paramMap, ok := cardData["cardParamMap"].(map[string]any) + if !ok { + t.Fatal("cardParamMap missing or wrong type") + } + if paramMap["content"] != "Hello from AI" { + t.Errorf("content=%v", paramMap["content"]) + } + if body["userIdType"] != float64(1) { + t.Errorf("userIdType=%v", body["userIdType"]) + } + + // Direct-specific fields + if body["openSpaceId"] != "dtv1.card//IM_ROBOT.user-456" { + t.Errorf("openSpaceId=%v", body["openSpaceId"]) + } + imRobotModel, ok := body["imRobotOpenSpaceModel"].(map[string]any) + if !ok { + t.Fatal("imRobotOpenSpaceModel missing") + } + if imRobotModel["supportForward"] != true { + t.Errorf("supportForward=%v", imRobotModel["supportForward"]) + } + + deliverModel, ok := body["imRobotOpenDeliverModel"].(map[string]any) + if !ok { + t.Fatal("imRobotOpenDeliverModel missing") + } + if deliverModel["robotCode"] != "client-id" { + t.Errorf("robotCode=%v", deliverModel["robotCode"]) + } + if deliverModel["spaceType"] != "IM_ROBOT" { + t.Errorf("spaceType=%v", deliverModel["spaceType"]) + } + ext, ok := deliverModel["extension"].(map[string]any) + if !ok { + t.Fatal("extension missing") + } + if len(ext) != 0 { + t.Errorf("extension should be empty, got %v", ext) + } + + // Group-specific fields must NOT be present + if _, ok := body["imGroupOpenSpaceModel"]; ok { + t.Error("imGroupOpenSpaceModel should not be present for direct") + } + if _, ok := body["imGroupOpenDeliverModel"]; ok { + t.Error("imGroupOpenDeliverModel should not be present for direct") + } +} + +// --------------------------------------------------------------------------- +// Test: group conversation full payload +// --------------------------------------------------------------------------- + +func TestCardSendGroupPayload(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("client-id", "client-secret", "card-template-123", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + req := CardRequest{ + ConversationType: core.ConversationGroup, + ExternalChatID: "chat-789", + Content: "Group message", + } + outTrackID, err := sender.SendFinalCard(context.Background(), req) + if err != nil { + t.Fatalf("SendFinalCard: %v", err) + } + + if !isUUID(outTrackID) { + t.Fatalf("outTrackID is not a UUID: %q", outTrackID) + } + + body := ts.lastCreateBody + + // Group-specific fields + if body["openSpaceId"] != "dtv1.card//IM_GROUP.chat-789" { + t.Errorf("openSpaceId=%v", body["openSpaceId"]) + } + + groupModel, ok := body["imGroupOpenSpaceModel"].(map[string]any) + if !ok { + t.Fatal("imGroupOpenSpaceModel missing") + } + if groupModel["supportForward"] != true { + t.Errorf("supportForward=%v", groupModel["supportForward"]) + } + + deliverModel, ok := body["imGroupOpenDeliverModel"].(map[string]any) + if !ok { + t.Fatal("imGroupOpenDeliverModel missing") + } + if deliverModel["robotCode"] != "client-id" { + t.Errorf("robotCode=%v", deliverModel["robotCode"]) + } + ext, ok := deliverModel["extension"].(map[string]any) + if !ok { + t.Fatal("extension missing") + } + if len(ext) != 0 { + t.Errorf("extension should be empty, got %v", ext) + } + + // Direct-specific fields must NOT be present + if _, ok := body["imRobotOpenSpaceModel"]; ok { + t.Error("imRobotOpenSpaceModel should not be present for group") + } + if _, ok := body["imRobotOpenDeliverModel"]; ok { + t.Error("imRobotOpenDeliverModel should not be present for group") + } +} + +// --------------------------------------------------------------------------- +// Test: OAuth token reuse (cache) +// --------------------------------------------------------------------------- + +func TestCardOAuthTokenReuse(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("client-id", "client-secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + for i := 0; i < 3; i++ { + req := CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "msg", + } + if _, err := sender.SendFinalCard(context.Background(), req); err != nil { + t.Fatalf("call %d: %v", i, err) + } + } + + if ts.tokenCalls != 1 { + t.Fatalf("tokenCalls=%d, want 1 (token should be cached)", ts.tokenCalls) + } + if ts.createCalls != 3 { + t.Fatalf("createCalls=%d, want 3", ts.createCalls) + } +} + +// --------------------------------------------------------------------------- +// Test: missing identity fields +// --------------------------------------------------------------------------- + +func TestCardSendMissingIdentity(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + // Direct without ExternalUserID + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + Content: "msg", + }) + if err == nil { + t.Fatal("direct without ExternalUserID should fail") + } + + // Group without ExternalChatID + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationGroup, + Content: "msg", + }) + if err == nil { + t.Fatal("group without ExternalChatID should fail") + } + + // Verify no API calls were made + if ts.tokenCalls != 0 || ts.createCalls != 0 { + t.Fatal("API should not be called when identity is missing") + } +} + +// --------------------------------------------------------------------------- +// Test: empty content and over 64KiB +// --------------------------------------------------------------------------- + +func TestCardSendContentValidation(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + // Empty content + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: "", + }) + if err == nil { + t.Fatal("empty content should fail") + } + + // Whitespace-only content + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: " ", + }) + if err == nil { + t.Fatal("whitespace-only content should fail") + } + + // Over 64KiB (UTF-8 bytes) + over64KiB := strings.Repeat("中", 21846) // 21846*3 = 65538 > 64*1024 + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: over64KiB, + }) + if err == nil { + t.Fatal("over 64KiB content should fail") + } + + // Verify no API calls were made + if ts.tokenCalls != 0 || ts.createCalls != 0 { + t.Fatal("API should not be called when content validation fails") + } +} + +// --------------------------------------------------------------------------- +// Test: OAuth non-2xx +// --------------------------------------------------------------------------- + +func TestCardOAuthNon2xx(t *testing.T) { + ts := newCardTestServer() + ts.tokenStatus = http.StatusUnauthorized + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: "msg", + }) + if err == nil { + t.Fatal("OAuth non-2xx should fail") + } + + // Error must not contain secret + if strings.Contains(err.Error(), "secret") { + t.Fatalf("error should not leak secret: %v", err) + } +} + +// --------------------------------------------------------------------------- +// Test: create card non-2xx (no body leaked) +// --------------------------------------------------------------------------- + +func TestCardCreateNon2xx(t *testing.T) { + ts := newCardTestServer() + ts.createStatus = http.StatusInternalServerError + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: "msg", + }) + if err == nil { + t.Fatal("create non-2xx should fail") + } + + // Error must not contain secret, token, or content + for _, secret := range []string{"secret", "test-access-token", "msg"} { + if strings.Contains(err.Error(), secret) { + t.Fatalf("error leaked %q: %v", secret, err) + } + } +} + +// --------------------------------------------------------------------------- +// Test: redirect does not leak token +// --------------------------------------------------------------------------- + +func TestCardRedirectNoTokenLeak(t *testing.T) { + var leaked string + target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + leaked = r.Header.Get("x-acs-dingtalk-access-token") + w.WriteHeader(http.StatusNoContent) + })) + defer target.Close() + + var source *httptest.Server + source = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/token" { + json.NewEncoder(w).Encode(map[string]any{"accessToken": "top-secret-token", "expireIn": 3600}) + return + } + http.Redirect(w, r, target.URL, http.StatusFound) + })) + defer source.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", source.Client(), source.URL+"/token", source.URL+"/create") + if err != nil { + t.Fatal(err) + } + + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: "msg", + }) + if err == nil { + t.Fatal("API redirect should fail") + } + if leaked != "" { + t.Fatalf("token leaked across redirect: %q", leaked) + } +} + +// --------------------------------------------------------------------------- +// Test: API response over 1MiB +// --------------------------------------------------------------------------- + +func TestCardAPIResponseOversize(t *testing.T) { + ts := newCardTestServer() + ts.createBodySize = maxAPIResponseBytes + 1 + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: "msg", + }) + if err == nil { + t.Fatal("oversized API response should fail") + } +} + +// --------------------------------------------------------------------------- +// Test: error does not contain secret, token, or content +// --------------------------------------------------------------------------- + +func TestCardErrorNoSecrets(t *testing.T) { + // Use a server that returns HTTP 500 for create + ts := newCardTestServer() + ts.createStatus = http.StatusInternalServerError + defer ts.Close() + + sender, err := newCardSenderForTest("my-client", "my-secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: "sensitive content here", + }) + if err == nil { + t.Fatal("should fail") + } + + errStr := err.Error() + for _, secret := range []string{"my-secret", "test-access-token", "sensitive content here"} { + if strings.Contains(errStr, secret) { + t.Fatalf("error leaked %q: %v", secret, err) + } + } +} + +// --------------------------------------------------------------------------- +// Test: outTrackID is UUID format and equals body value +// --------------------------------------------------------------------------- + +func TestCardOutTrackIDUUID(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + outTrackID, err := sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: "msg", + }) + if err != nil { + t.Fatal(err) + } + + if !isUUID(outTrackID) { + t.Fatalf("outTrackID is not a valid UUID: %q", outTrackID) + } + + // Verify the outTrackID returned matches the body field + body := ts.lastCreateBody + bodyTrackID, ok := body["outTrackId"].(string) + if !ok || bodyTrackID != outTrackID { + t.Fatalf("body outTrackId=%v, return value=%v", bodyTrackID, outTrackID) + } +} + +// --------------------------------------------------------------------------- +// Test: unsupported conversation type rejected before API +// --------------------------------------------------------------------------- + +func TestCardSendUnsupportedConversationType(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: "unknown", + Content: "msg", + }) + if err == nil { + t.Fatal("unsupported conversation type should fail") + } + + // Verify no API calls were made + if ts.tokenCalls != 0 || ts.createCalls != 0 { + t.Fatal("API should not be called for unsupported conversation type") + } +} + +// --------------------------------------------------------------------------- +// Test: 64KiB boundary — exactly 64KiB should succeed +// --------------------------------------------------------------------------- + +func TestCardSend64KiBBoundary(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + // Exactly 64KiB of ASCII + content64KiB := strings.Repeat("a", 64*1024) + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: content64KiB, + }) + if err != nil { + t.Fatalf("exactly 64KiB should succeed: %v", err) + } + + // 64KiB + 1 byte should fail + content64KiBPlus1 := strings.Repeat("a", 64*1024+1) + _, err = sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user", + Content: content64KiBPlus1, + }) + if err == nil { + t.Fatal("64KiB+1 should fail") + } +} + +// --------------------------------------------------------------------------- +// Test: StartCard creates only — no stream finalize call +// --------------------------------------------------------------------------- + +func TestStartCardCreateOnly(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + outTrackID, err := sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "Thinking...", + }) + if err != nil { + t.Fatalf("StartCard: %v", err) + } + if !isUUID(outTrackID) { + t.Fatalf("outTrackID is not a UUID: %q", outTrackID) + } + + // Must have exactly 1 create call, 0 stream calls + if ts.createCalls != 1 { + t.Fatalf("createCalls=%d, want 1", ts.createCalls) + } + if ts.streamCalls != 0 { + t.Fatalf("streamCalls=%d, want 0 (StartCard must not finalize)", ts.streamCalls) + } + if ts.tokenCalls != 1 { + t.Fatalf("tokenCalls=%d, want 1", ts.tokenCalls) + } +} + +// --------------------------------------------------------------------------- +// Test: UpdateCard sends PUT with exact payload fields +// --------------------------------------------------------------------------- + +func TestUpdateCardExactPayload(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + // Start a card first + outTrackID, err := sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "Thinking...", + }) + if err != nil { + t.Fatalf("StartCard: %v", err) + } + + // Reset stream call count (StartCard doesn't call stream) + streamCallsBefore := ts.streamCalls + + // UpdateCard with finalize + err = sender.UpdateCard(context.Background(), outTrackID, "Updated content", true, false) + if err != nil { + t.Fatalf("UpdateCard: %v", err) + } + + if ts.streamCalls != streamCallsBefore+1 { + t.Fatalf("streamCalls=%d, want %d", ts.streamCalls, streamCallsBefore+1) + } + if ts.lastStreamMethod != http.MethodPut { + t.Fatalf("stream method=%q, want PUT", ts.lastStreamMethod) + } + if ts.lastStreamHeader != "test-access-token" { + t.Fatalf("stream header token=%q", ts.lastStreamHeader) + } + + body := ts.lastStreamBody + if body["outTrackId"] != outTrackID { + t.Errorf("outTrackId=%v, want %v", body["outTrackId"], outTrackID) + } + if body["key"] != "content" { + t.Errorf("key=%v, want content", body["key"]) + } + if body["content"] != "Updated content" { + t.Errorf("content=%v, want Updated content", body["content"]) + } + if body["isFull"] != true { + t.Errorf("isFull=%v, want true", body["isFull"]) + } + if body["isFinalize"] != true { + t.Errorf("isFinalize=%v, want true", body["isFinalize"]) + } + if body["isError"] != false { + t.Errorf("isError=%v, want false", body["isError"]) + } + if body["guid"] == nil || body["guid"] == "" { + t.Error("guid is missing or empty") + } +} + +// --------------------------------------------------------------------------- +// Test: SendFinalCard calls create exactly once and stream exactly once with finalize=true +// --------------------------------------------------------------------------- + +func TestSendFinalCardCreateStreamOnce(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + outTrackID, err := sender.SendFinalCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "Hello", + }) + if err != nil { + t.Fatalf("SendFinalCard: %v", err) + } + if !isUUID(outTrackID) { + t.Fatalf("outTrackID is not a UUID: %q", outTrackID) + } + + // Must create exactly once, stream exactly once + if ts.createCalls != 1 { + t.Fatalf("createCalls=%d, want 1", ts.createCalls) + } + if ts.streamCalls != 1 { + t.Fatalf("streamCalls=%d, want 1", ts.streamCalls) + } + + // Stream must be finalize=true + if ts.lastStreamBody["isFinalize"] != true { + t.Fatalf("isFinalize=%v, want true", ts.lastStreamBody["isFinalize"]) + } + if ts.lastStreamBody["content"] != "Hello" { + t.Fatalf("content=%v, want Hello", ts.lastStreamBody["content"]) + } +} + +// --------------------------------------------------------------------------- +// Test: UpdateCard with non-final / error flags +// --------------------------------------------------------------------------- + +func TestUpdateCardNonFinalAndError(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + outTrackID, err := sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "Processing...", + }) + if err != nil { + t.Fatalf("StartCard: %v", err) + } + + // Non-final update (streaming progress) + err = sender.UpdateCard(context.Background(), outTrackID, "Step 1 complete", false, false) + if err != nil { + t.Fatalf("UpdateCard (non-final): %v", err) + } + if ts.lastStreamBody["isFinalize"] != false { + t.Errorf("isFinalize should be false, got %v", ts.lastStreamBody["isFinalize"]) + } + if ts.lastStreamBody["isError"] != false { + t.Errorf("isError should be false, got %v", ts.lastStreamBody["isError"]) + } + + // Error update + err = sender.UpdateCard(context.Background(), outTrackID, "Something went wrong", true, true) + if err != nil { + t.Fatalf("UpdateCard (error): %v", err) + } + if ts.lastStreamBody["isFinalize"] != true { + t.Errorf("isFinalize should be true, got %v", ts.lastStreamBody["isFinalize"]) + } + if ts.lastStreamBody["isError"] != true { + t.Errorf("isError should be true, got %v", ts.lastStreamBody["isError"]) + } + if ts.lastStreamBody["content"] != "Something went wrong" { + t.Errorf("content=%v", ts.lastStreamBody["content"]) + } +} + +// --------------------------------------------------------------------------- +// Test: StartCard rejects empty content +// --------------------------------------------------------------------------- + +func TestStartCardEmptyContent(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + _, err = sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "", + }) + if err == nil { + t.Fatal("empty content should fail") + } + if ts.tokenCalls != 0 || ts.createCalls != 0 { + t.Fatal("API should not be called when content is empty") + } +} + +// --------------------------------------------------------------------------- +// Test: UpdateCard rejects empty outTrackID +// --------------------------------------------------------------------------- + +func TestUpdateCardEmptyOutTrackID(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + err = sender.UpdateCard(context.Background(), "", "content", true, false) + if err == nil { + t.Fatal("empty outTrackID should fail") + } + if ts.tokenCalls != 0 || ts.streamCalls != 0 { + t.Fatal("API should not be called when outTrackID is empty") + } + + err = sender.UpdateCard(context.Background(), " ", "content", true, false) + if err == nil { + t.Fatal("whitespace-only outTrackID should fail") + } +} + +// --------------------------------------------------------------------------- +// Test: UpdateCard rejects empty content +// --------------------------------------------------------------------------- + +func TestUpdateCardEmptyContent(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + err = sender.UpdateCard(context.Background(), "some-id", "", true, false) + if err == nil { + t.Fatal("empty content should fail") + } + if ts.tokenCalls != 0 || ts.streamCalls != 0 { + t.Fatal("API should not be called when content is empty") + } +} + +// --------------------------------------------------------------------------- +// Test: UpdateCard rejects content over 64KiB +// --------------------------------------------------------------------------- + +func TestUpdateCardOver64KiB(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + over64KiB := strings.Repeat("x", 64*1024+1) + err = sender.UpdateCard(context.Background(), "some-id", over64KiB, true, false) + if err == nil { + t.Fatal("over 64KiB content should fail") + } + if ts.tokenCalls != 0 || ts.streamCalls != 0 { + t.Fatal("API should not be called when content exceeds 64KiB") + } +} + +// --------------------------------------------------------------------------- +// Test: UpdateCard stream non-2xx — error must not leak secret/token/content +// --------------------------------------------------------------------------- + +func TestUpdateCardStreamNon2xx(t *testing.T) { + ts := newCardTestServer() + ts.streamStatus = http.StatusInternalServerError + defer ts.Close() + + sender, err := newCardSenderForTest("my-client", "my-secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + outTrackID, err := sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "Thinking...", + }) + if err != nil { + t.Fatalf("StartCard: %v", err) + } + + err = sender.UpdateCard(context.Background(), outTrackID, "sensitive data", true, false) + if err == nil { + t.Fatal("stream non-2xx should fail") + } + + errStr := err.Error() + for _, secret := range []string{"my-secret", "test-access-token", "sensitive data"} { + if strings.Contains(errStr, secret) { + t.Fatalf("error leaked %q: %v", secret, err) + } + } +} + +// --------------------------------------------------------------------------- +// Test: UpdateCard redirect does not leak token +// --------------------------------------------------------------------------- + +func TestUpdateCardStreamRedirectNoTokenLeak(t *testing.T) { + var leaked string + target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + leaked = r.Header.Get("x-acs-dingtalk-access-token") + w.WriteHeader(http.StatusNoContent) + })) + defer target.Close() + + // Create a server that redirects stream calls + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/token": + json.NewEncoder(w).Encode(map[string]any{"accessToken": "top-secret-token", "expireIn": 3600}) + case "/create": + json.NewEncoder(w).Encode(map[string]any{"result": "ok"}) + default: + http.Redirect(w, r, target.URL, http.StatusFound) + } + })) + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL+"/token", ts.URL+"/create") + if err != nil { + t.Fatal(err) + } + + outTrackID, err := sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "Thinking...", + }) + if err != nil { + t.Fatalf("StartCard: %v", err) + } + + err = sender.UpdateCard(context.Background(), outTrackID, "content", true, false) + if err == nil { + t.Fatal("API redirect should fail") + } + if leaked != "" { + t.Fatalf("token leaked across redirect: %q", leaked) + } +} + +// --------------------------------------------------------------------------- +// Test: UpdateCard stream oversize response +// --------------------------------------------------------------------------- + +func TestUpdateCardStreamOversize(t *testing.T) { + ts := newCardTestServer() + ts.streamBodySize = maxAPIResponseBytes + 1 + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + outTrackID, err := sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "Thinking...", + }) + if err != nil { + t.Fatalf("StartCard: %v", err) + } + + err = sender.UpdateCard(context.Background(), outTrackID, "content", true, false) + if err == nil { + t.Fatal("oversized stream response should fail") + } +} + +// --------------------------------------------------------------------------- +// Test: OAuth called only once across StartCard + multiple UpdateCard +// --------------------------------------------------------------------------- + +func TestOAuthAcrossStartAndUpdates(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + + outTrackID, err := sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", + Content: "Processing...", + }) + if err != nil { + t.Fatalf("StartCard: %v", err) + } + + // Multiple updates + for i := 0; i < 3; i++ { + err = sender.UpdateCard(context.Background(), outTrackID, "chunk "+string(rune('0'+i)), false, false) + if err != nil { + t.Fatalf("UpdateCard %d: %v", i, err) + } + } + + // Final update + err = sender.UpdateCard(context.Background(), outTrackID, "done", true, false) + if err != nil { + t.Fatalf("UpdateCard final: %v", err) + } + + // Token should be called exactly once (cached across all calls) + if ts.tokenCalls != 1 { + t.Fatalf("tokenCalls=%d, want 1 (token should be cached)", ts.tokenCalls) + } + if ts.createCalls != 1 { + t.Fatalf("createCalls=%d, want 1", ts.createCalls) + } + if ts.streamCalls != 4 { + t.Fatalf("streamCalls=%d, want 4", ts.streamCalls) + } +} + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +func isUUID(s string) bool { + if len(s) != 36 { + return false + } + // UUID format: 8-4-4-4-12 + for i, c := range s { + switch i { + case 8, 13, 18, 23: + if c != '-' { + return false + } + default: + if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) { + return false + } + } + } + return true +} diff --git a/internal/channels/dingtalk/handler.go b/internal/channels/dingtalk/handler.go new file mode 100644 index 0000000..0e3cc1a --- /dev/null +++ b/internal/channels/dingtalk/handler.go @@ -0,0 +1,435 @@ +package dingtalk + +import ( + "context" + "errors" + "fmt" + "log" + "strings" + "time" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora" +) + +const ( + msgTypeText = "text" + msgTypePicture = "picture" + conversationTypeDirect = "1" + conversationTypeGroup = "2" + replyTitle = "DobotAI" + + unsupportedMessageReply = "当前版本暂仅支持文字消息。" + qaErrorReply = "抱歉,当前暂时无法完成回答,请稍后重试。" + imageErrorReply = "图片识别暂时不可用,请稍后重试或发送文字。" + imageQuery = "请描述并分析这张图片。" + streamingPlaceholder = "正在理解问题…" +) + +type TenantConfig struct { + TenantKey string + Channel string +} + +type BotCallback struct { + ConversationType string + ConversationID string + Msgtype string + MsgID string + SenderNick string + SenderStaffID string + SenderID string + IsInAtList bool + SessionWebhook string + Text string + DownloadCode string +} + +type QAAsker interface { + Ask(ctx context.Context, req core.QARequest) (core.QAResult, error) +} + +// StreamingQAAsker asks the QA backend with streaming observer support. +type StreamingQAAsker interface { + QAAsker + AskStream(ctx context.Context, req core.QARequest, observer weknora.AnswerObserver) (core.QAResult, error) +} + +// EventQAAsker exposes safe stage metadata without thinking/tool payloads. +type EventQAAsker interface { + QAAsker + AskEvents(ctx context.Context, req core.QARequest, observer weknora.StreamObserver) (core.QAResult, error) +} + +type MarkdownSender interface { + SendMarkdown(ctx context.Context, webhookURL, title, text string) error +} + +type ImageDownloader interface { + Download(ctx context.Context, downloadCode string) (core.ImageInput, error) +} + +// FinalCardSender sends an AI card as the final reply. Implementations must +// not log err, outTrackID, or content at any level. +type FinalCardSender interface { + SendFinalCard(context.Context, CardRequest) (string, error) +} + +// StreamingCardSender supports streaming card lifecycle: start, update, and finalize. +type StreamingCardSender interface { + FinalCardSender + StartCard(ctx context.Context, req CardRequest) (string, error) + UpdateCard(ctx context.Context, id, content string, isFinalize, isError bool) error +} + +type HandlerOptions struct { + ImageDownloader ImageDownloader + CardSender FinalCardSender +} + +type Handler struct { + cfg TenantConfig + qa QAAsker + reply MarkdownSender + downloader ImageDownloader + card FinalCardSender + now func() time.Time + cardUpdateInterval time.Duration + finalDeliveryTimeout time.Duration +} + +// NewHandler accepts at most one image downloader. A nil/omitted downloader +// keeps text ingress available while picture messages fail closed. +func NewHandler(cfg TenantConfig, qa QAAsker, reply MarkdownSender, downloaders ...ImageDownloader) (*Handler, error) { + var downloader ImageDownloader + if len(downloaders) > 1 { + return nil, errors.New("dingtalk handler: at most one image downloader is allowed") + } + if len(downloaders) == 1 { + downloader = downloaders[0] + } + return NewHandlerWithOptions(cfg, qa, reply, HandlerOptions{ImageDownloader: downloader}) +} + +// NewHandlerWithOptions creates a Handler with an optional image downloader +// and optional card sender. Passing nil for either option is equivalent to +// omitting the feature. +func NewHandlerWithOptions(cfg TenantConfig, qa QAAsker, reply MarkdownSender, opts HandlerOptions) (*Handler, error) { + if strings.TrimSpace(cfg.TenantKey) == "" { + return nil, errors.New("dingtalk handler: tenant key is required") + } + if strings.TrimSpace(cfg.Channel) == "" { + return nil, errors.New("dingtalk handler: channel is required") + } + if qa == nil { + return nil, errors.New("dingtalk handler: QA asker is required") + } + if reply == nil { + return nil, errors.New("dingtalk handler: markdown sender is required") + } + return &Handler{ + cfg: cfg, + qa: qa, + reply: reply, + downloader: opts.ImageDownloader, + card: opts.CardSender, + now: time.Now, + cardUpdateInterval: 500 * time.Millisecond, + finalDeliveryTimeout: defaultHTTPTimeout, + }, nil +} + +func (h *Handler) Handle(ctx context.Context, cb BotCallback) error { + // Admission comes first: a group message without @ must have zero side effects. + if cb.ConversationType == conversationTypeGroup && !cb.IsInAtList { + return nil + } + + // Validate the only reply destination before downloading media or spending a + // model call. Webhook errors never include its query string. + webhookURL := strings.TrimSpace(cb.SessionWebhook) + if webhookURL == "" { + return errors.New("dingtalk handler: sessionWebhook is required to reply") + } + if err := ValidateWebhookURL(webhookURL); err != nil { + return fmt.Errorf("dingtalk handler: invalid sessionWebhook: %w", err) + } + if strings.TrimSpace(cb.MsgID) == "" { + return errors.New("dingtalk handler: msgId is required") + } + + externalUserID := strings.TrimSpace(cb.SenderStaffID) + if externalUserID == "" { + externalUserID = strings.TrimSpace(cb.SenderID) + } + if externalUserID == "" { + return errors.New("dingtalk handler: sender staffId/senderId is required") + } + + req := core.QARequest{ + TenantKey: h.cfg.TenantKey, + Channel: h.cfg.Channel, + ExternalUserID: externalUserID, + ExternalMessageID: strings.TrimSpace(cb.MsgID), + SenderName: cb.SenderNick, + } + switch cb.ConversationType { + case conversationTypeGroup: + if strings.TrimSpace(cb.ConversationID) == "" { + return errors.New("dingtalk handler: conversationId is required for group messages") + } + req.ConversationType = core.ConversationGroup + req.ExternalChatID = strings.TrimSpace(cb.ConversationID) + default: + req.ConversationType = core.ConversationDirect + } + + switch strings.ToLower(strings.TrimSpace(cb.Msgtype)) { + case msgTypeText: + req.Query = strings.TrimSpace(cb.Text) + if req.Query == "" { + return errors.New("dingtalk handler: text message content is empty") + } + case msgTypePicture: + if strings.TrimSpace(cb.DownloadCode) == "" { + log.Printf("DingTalk image preparation failed: stage=missing_download_code") + return h.sendReply(ctx, webhookURL, imageErrorReply) + } + if h.downloader == nil { + log.Printf("DingTalk image preparation failed: stage=downloader_unavailable") + return h.sendReply(ctx, webhookURL, imageErrorReply) + } + image, err := h.downloader.Download(ctx, strings.TrimSpace(cb.DownloadCode)) + if err != nil { + log.Printf("DingTalk image preparation failed: stage=%s", imageFailureStage(err)) + return h.sendReply(ctx, webhookURL, imageErrorReply) + } + req.Query = strings.TrimSpace(cb.Text) + if req.Query == "" { + req.Query = imageQuery + } + req.Images = []core.ImageInput{image} + default: + return h.sendReply(ctx, webhookURL, unsupportedMessageReply) + } + + if eventCard, eventQA := h.eventStreamingCapable(); eventCard != nil && eventQA != nil { + return h.handleEventStreaming(ctx, webhookURL, req, eventCard, eventQA) + } + // Backward-compatible A3.2 answer-only streaming capability. + streamingCard, streamingQA := h.streamingCapable() + if streamingCard != nil && streamingQA != nil { + return h.handleStreaming(ctx, webhookURL, req, streamingCard, streamingQA) + } + return h.handleNonStreaming(ctx, webhookURL, req) +} + +func (h *Handler) eventStreamingCapable() (StreamingCardSender, EventQAAsker) { + sc, ok := h.card.(StreamingCardSender) + if !ok { + return nil, nil + } + eq, ok := h.qa.(EventQAAsker) + if !ok { + return nil, nil + } + return sc, eq +} + +func (h *Handler) streamingCapable() (StreamingCardSender, StreamingQAAsker) { + sc, ok := h.card.(StreamingCardSender) + if !ok { + return nil, nil + } + sq, ok := h.qa.(StreamingQAAsker) + if !ok { + return nil, nil + } + return sc, sq +} + +func (h *Handler) handleNonStreaming(ctx context.Context, webhookURL string, req core.QARequest) error { + result, err := h.qa.Ask(ctx, req) + if err != nil { + if replyErr := h.sendReply(ctx, webhookURL, qaErrorReply); replyErr != nil { + return replyErr + } + return nil + } + + if h.card != nil { + cardReq := CardRequest{ + ConversationType: req.ConversationType, + ExternalUserID: req.ExternalUserID, + ExternalChatID: req.ExternalChatID, + Content: result.Answer, + } + if _, cardErr := h.card.SendFinalCard(ctx, cardReq); cardErr == nil { + return nil + } + log.Printf("DingTalk card delivery failed, falling back to markdown") + } + return h.sendReply(ctx, webhookURL, result.Answer) +} + +func (h *Handler) handleStreaming(ctx context.Context, webhookURL string, req core.QARequest, streamingCard StreamingCardSender, streamingQA StreamingQAAsker) error { + cardReq := CardRequest{ + ConversationType: req.ConversationType, + ExternalUserID: req.ExternalUserID, + ExternalChatID: req.ExternalChatID, + Content: streamingPlaceholder, + } + outTrackID, err := streamingCard.StartCard(ctx, cardReq) + if err != nil { + log.Printf("DingTalk streaming card start failed, falling back to non-streaming") + // Fallback: Ask → Markdown only (no SendFinalCard, no AskStream) + result, askErr := h.qa.Ask(ctx, req) + if askErr != nil { + if replyErr := h.sendReply(ctx, webhookURL, qaErrorReply); replyErr != nil { + return replyErr + } + return nil + } + return h.sendReply(ctx, webhookURL, result.Answer) + } + + // Streaming observer with the configured update throttle (500ms by default). + var lastUpdate time.Time + observer := func(fullContent string) { + if strings.TrimSpace(fullContent) == "" { + return + } + now := h.now() + if lastUpdate.IsZero() || now.Sub(lastUpdate) >= h.cardUpdateInterval { + // Advance the throttle window before I/O so a failing DingTalk API + // cannot be hammered once per model chunk. The final update below is + // always attempted regardless of this timestamp. + lastUpdate = now + if updateErr := streamingCard.UpdateCard(ctx, outTrackID, fullContent, false, false); updateErr != nil { + log.Printf("DingTalk streaming card update failed, continuing") + } + } + } + + result, err := streamingQA.AskStream(ctx, req, observer) + if err != nil || strings.TrimSpace(result.Answer) == "" { + return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, qaErrorReply, true) + } + return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, result.Answer, false) +} + +func (h *Handler) handleEventStreaming(ctx context.Context, webhookURL string, req core.QARequest, streamingCard StreamingCardSender, eventQA EventQAAsker) error { + cardReq := CardRequest{ + ConversationType: req.ConversationType, + ExternalUserID: req.ExternalUserID, + ExternalChatID: req.ExternalChatID, + Content: streamingPlaceholder, + } + outTrackID, err := streamingCard.StartCard(ctx, cardReq) + if err != nil { + log.Printf("DingTalk event card start failed, falling back to markdown") + result, askErr := h.qa.Ask(ctx, req) + if askErr != nil { + return h.sendReply(ctx, webhookURL, qaErrorReply) + } + return h.sendReply(ctx, webhookURL, result.Answer) + } + + pump := newCardUpdatePump(ctx, h.cardUpdateInterval, func(updateCtx context.Context, content string) error { + return streamingCard.UpdateCard(updateCtx, outTrackID, content, false, false) + }) + answerStarted := false + result, err := eventQA.AskEvents(ctx, req, func(event weknora.StreamEvent) { + if event.ResponseType == "answer" && strings.TrimSpace(event.FullAnswer) != "" { + answerStarted = true + pump.Offer(event.FullAnswer) + return + } + if answerStarted { + return + } + if content := renderProgressEvent(event); content != "" { + pump.Offer(content) + } + }) + // Stop and drain the asynchronous worker before the synchronous final update + // so no stale progress/partial answer can overwrite the terminal card state. + pump.Stop() + + if err != nil || strings.TrimSpace(result.Answer) == "" { + return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, qaErrorReply, true) + } + return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, result.Answer, false) +} + +func (h *Handler) finalizeCardOrMarkdown(ctx context.Context, webhookURL string, streamingCard StreamingCardSender, outTrackID, content string, isError bool) error { + timeout := h.finalDeliveryTimeout + if timeout <= 0 { + timeout = defaultHTTPTimeout + } + + // Final card convergence must still be attempted when the QA context was + // canceled or timed out; otherwise the card can remain "processing". + finalCtx, cancelFinal := context.WithTimeout(context.WithoutCancel(ctx), timeout) + updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, content, true, isError) + cancelFinal() + if updateErr == nil { + return nil + } + + // The card attempt may have consumed its entire deadline. Markdown must get + // a fresh delivery window rather than reusing the expired finalCtx. + fallbackCtx, cancelFallback := context.WithTimeout(context.WithoutCancel(ctx), timeout) + defer cancelFallback() + return h.sendReply(fallbackCtx, webhookURL, content) +} + +func renderProgressEvent(event weknora.StreamEvent) string { + switch event.ResponseType { + case "tool_call": + switch strings.TrimSpace(event.ToolName) { + case "query_understand": + return "正在理解问题…" + case "knowledge_search": + return "正在检索知识库…" + } + case "tool_result": + switch strings.TrimSpace(event.ToolName) { + case "query_understand": + return "问题理解完成\n\n正在检索知识库…" + case "knowledge_search": + return "知识库检索完成\n\n正在生成答案…" + } + case "references": + return fmt.Sprintf("检索到 %d 条相关内容\n\n正在生成答案…", event.ReferenceCount) + } + return "" +} + +func imageFailureStage(err error) string { + message := strings.ToLower(err.Error()) + switch { + case strings.Contains(message, "oauth"): + return "oauth" + case strings.Contains(message, "api"): + return "file_api" + case strings.Contains(message, "unsafe") || strings.Contains(message, "destination"): + return "unsafe_url" + case strings.Contains(message, "format") || strings.Contains(message, "image data") || strings.Contains(message, "dimensions") || strings.Contains(message, "size limit"): + return "image_validation" + case strings.Contains(message, "request") || strings.Contains(message, "http") || strings.Contains(message, "read"): + return "image_fetch" + default: + return "unknown" + } +} + +func (h *Handler) sendReply(ctx context.Context, webhookURL, text string) error { + if strings.TrimSpace(webhookURL) == "" { + return errors.New("dingtalk handler: sessionWebhook is required to reply") + } + if err := h.reply.SendMarkdown(ctx, webhookURL, replyTitle, text); err != nil { + return fmt.Errorf("dingtalk handler: send reply: %w", err) + } + return nil +} diff --git a/internal/channels/dingtalk/handler_test.go b/internal/channels/dingtalk/handler_test.go new file mode 100644 index 0000000..f14f915 --- /dev/null +++ b/internal/channels/dingtalk/handler_test.go @@ -0,0 +1,1656 @@ +package dingtalk + +import ( + "context" + "errors" + "strings" + "testing" + "time" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora" +) + +type fakeQA struct { + called bool + lastReq core.QARequest + result core.QAResult + err error +} + +func (f *fakeQA) Ask(_ context.Context, req core.QARequest) (core.QAResult, error) { + f.called = true + f.lastReq = req + return f.result, f.err +} + +type fakeReply struct { + called bool + lastURL string + lastTitle string + lastText string + err error +} + +func (f *fakeReply) SendMarkdown(_ context.Context, url, title, text string) error { + f.called = true + f.lastURL = url + f.lastTitle = title + f.lastText = text + return f.err +} + +type fakeImageDownloader struct { + called bool + code string + image core.ImageInput + err error +} + +func (f *fakeImageDownloader) Download(_ context.Context, code string) (core.ImageInput, error) { + f.called = true + f.code = code + return f.image, f.err +} + +// fakeCard implements FinalCardSender for tests. It must not log err, outTrackID, +// or content. All fields are test-observable and never leak sensitive values. +type fakeCard struct { + called bool + lastReq CardRequest + returnID string + err error +} + +func (f *fakeCard) SendFinalCard(_ context.Context, req CardRequest) (string, error) { + f.called = true + f.lastReq = req + return f.returnID, f.err +} + +func newTestHandler(t *testing.T, qa QAAsker, reply *fakeReply) *Handler { + return newTestHandlerWithDownloader(t, qa, reply, nil) +} + +func newTestHandlerWithDownloader(t *testing.T, qa QAAsker, reply *fakeReply, downloader ImageDownloader) *Handler { + t.Helper() + cfg := TenantConfig{ + TenantKey: "tenant-a", + Channel: "dingtalk", + } + h, err := NewHandler(cfg, qa, reply, downloader) + if err != nil { + t.Fatalf("NewHandler: %v", err) + } + return h +} + +func newTestHandlerWithCard(t *testing.T, qa QAAsker, reply *fakeReply, card *fakeCard) *Handler { + t.Helper() + cfg := TenantConfig{ + TenantKey: "tenant-a", + Channel: "dingtalk", + } + h, err := NewHandlerWithOptions(cfg, qa, reply, HandlerOptions{CardSender: card}) + if err != nil { + t.Fatalf("NewHandlerWithOptions: %v", err) + } + return h +} + +func directTextCallback() BotCallback { + return BotCallback{ + ConversationType: "1", + Msgtype: "text", + MsgID: "msg-1", + SenderNick: "Alice", + SenderStaffID: "staff-123", + SenderID: "user-abc", + SessionWebhook: "https://oapi.dingtalk.com/robot/send?access_token=x", + Text: " hello world ", + } +} + +func groupTextCallback() BotCallback { + return BotCallback{ + ConversationType: "2", + ConversationID: "cid-group-1", + Msgtype: "text", + MsgID: "msg-2", + SenderNick: "Bob", + SenderStaffID: "staff-456", + SenderID: "user-def", + IsInAtList: true, + SessionWebhook: "https://oapi.dingtalk.com/robot/send?access_token=x", + Text: " group question ", + } +} + +func directPictureCallback() BotCallback { + data := directTextCallback() + data.Msgtype = "picture" + data.Text = "" + data.DownloadCode = "temporary-download-code" + return data +} + +// --- A: direct text --- + +func TestHandleDirectTextCallsQAAndRepliesAnswer(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "**answer**"}} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if !qa.called { + t.Fatal("QA.Ask was not called") + } + req := qa.lastReq + if req.TenantKey != "tenant-a" || req.Channel != "dingtalk" { + t.Fatalf("unexpected TenantKey/Channel: %+v", req) + } + if req.ConversationType != core.ConversationDirect { + t.Fatalf("ConversationType = %q, want direct", req.ConversationType) + } + if req.ExternalUserID != "staff-123" { + t.Fatalf("ExternalUserID = %q, want staff-123", req.ExternalUserID) + } + if req.ExternalMessageID != "msg-1" { + t.Fatalf("ExternalMessageID = %q, want msg-1", req.ExternalMessageID) + } + if req.SenderName != "Alice" { + t.Fatalf("SenderName = %q, want Alice", req.SenderName) + } + if req.Query != "hello world" { + t.Fatalf("Query = %q, want trimmed 'hello world'", req.Query) + } + if req.ExternalChatID != "" { + t.Fatalf("ExternalChatID should be empty for direct, got %q", req.ExternalChatID) + } + + if !reply.called { + t.Fatal("Reply.SendMarkdown was not called") + } + if reply.lastText != "**answer**" { + t.Fatalf("reply text = %q, want answer", reply.lastText) + } + if reply.lastURL == "" { + t.Fatal("reply URL should not be empty") + } +} + +func TestHandleDirectTextFallsBackToSenderID(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "ok"}} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := directTextCallback() + data.SenderStaffID = "" + err := h.Handle(context.Background(), data) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if qa.lastReq.ExternalUserID != "user-abc" { + t.Fatalf("ExternalUserID = %q, want fallback to senderId user-abc", qa.lastReq.ExternalUserID) + } +} + +func TestHandleDirectTextTrimsQuery(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "ok"}} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := directTextCallback() + data.Text = " padded " + _ = h.Handle(context.Background(), data) + if qa.lastReq.Query != "padded" { + t.Fatalf("Query = %q, want trimmed 'padded'", qa.lastReq.Query) + } +} + +// --- B: group text, @bot --- + +func TestHandleGroupTextAtBotCallsQAAndReplies(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "group answer"}} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + err := h.Handle(context.Background(), groupTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if !qa.called { + t.Fatal("QA.Ask was not called") + } + req := qa.lastReq + if req.ConversationType != core.ConversationGroup { + t.Fatalf("ConversationType = %q, want group", req.ConversationType) + } + if req.ExternalChatID != "cid-group-1" { + t.Fatalf("ExternalChatID = %q, want cid-group-1", req.ExternalChatID) + } + if req.Query != "group question" { + t.Fatalf("Query = %q, want trimmed 'group question'", req.Query) + } + if !reply.called { + t.Fatal("Reply.SendMarkdown was not called") + } +} + +func TestHandleGroupTextNotAtBotSilentlyIgnored(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := groupTextCallback() + data.IsInAtList = false + err := h.Handle(context.Background(), data) + if err != nil { + t.Fatalf("Handle: expected nil error for non-@bot group message, got %v", err) + } + if qa.called { + t.Fatal("QA.Ask should not be called for non-@bot group message") + } + if reply.called { + t.Fatal("Reply.SendMarkdown should not be called for non-@bot group message") + } +} + +// --- C: non-text message --- + +func TestHandleNonTextRepliesUnsupported(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := directTextCallback() + data.Msgtype = "audio" + err := h.Handle(context.Background(), data) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if qa.called { + t.Fatal("QA.Ask should not be called for non-text message") + } + if !reply.called { + t.Fatal("Reply.SendMarkdown should be called for non-text message") + } + if !strings.Contains(reply.lastText, "暂仅支持文字消息") { + t.Fatalf("reply text = %q, want unsupported-message notice", reply.lastText) + } +} + +func TestHandlePictureDownloadsAndCallsQA(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "图片回答"}} + reply := &fakeReply{} + image := core.ImageInput{DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 4, Width: 1, Height: 1} + downloader := &fakeImageDownloader{image: image} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + + if err := h.Handle(context.Background(), directPictureCallback()); err != nil { + t.Fatalf("Handle picture: %v", err) + } + if !downloader.called || downloader.code != "temporary-download-code" { + t.Fatalf("download called=%v code=%q", downloader.called, downloader.code) + } + if !qa.called || len(qa.lastReq.Images) != 1 || qa.lastReq.Images[0] != image { + t.Fatalf("QA request did not receive image: %+v", qa.lastReq) + } + if qa.lastReq.Query != "请描述并分析这张图片。" { + t.Fatalf("picture query=%q", qa.lastReq.Query) + } + if !reply.called || reply.lastText != "图片回答" { + t.Fatalf("reply=%+v", reply) + } +} + +func TestHandlePicturePreparationFailuresAreFriendlyAndDoNotCallQA(t *testing.T) { + tests := []struct { + name string + code string + downloader ImageDownloader + }{ + {name: "missing download code", code: "", downloader: &fakeImageDownloader{}}, + {name: "missing downloader", code: "code", downloader: nil}, + {name: "download error", code: "secret-code", downloader: &fakeImageDownloader{err: errors.New("signed URL and token must not leak")}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + h := newTestHandlerWithDownloader(t, qa, reply, tt.downloader) + data := directPictureCallback() + data.DownloadCode = tt.code + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle: %v", err) + } + if qa.called { + t.Fatal("QA must not be called") + } + if !reply.called || reply.lastText != "图片识别暂时不可用,请稍后重试或发送文字。" { + t.Fatalf("friendly reply=%q", reply.lastText) + } + }) + } +} + +func TestHandleGroupPictureWithoutMentionDoesNothing(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + downloader := &fakeImageDownloader{} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + data := directPictureCallback() + data.ConversationType = conversationTypeGroup + data.IsInAtList = false + data.SessionWebhook = "not even validated" + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle: %v", err) + } + if downloader.called || qa.called || reply.called { + t.Fatalf("unexpected side effects: downloader=%v qa=%v reply=%v", downloader.called, qa.called, reply.called) + } +} + +func TestHandlePictureRejectsWebhookBeforeDownload(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + downloader := &fakeImageDownloader{} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + data := directPictureCallback() + data.SessionWebhook = "http://127.0.0.1/steal" + if err := h.Handle(context.Background(), data); err == nil { + t.Fatal("expected invalid webhook error") + } + if downloader.called || qa.called || reply.called { + t.Fatalf("invalid webhook caused side effects: downloader=%v qa=%v reply=%v", downloader.called, qa.called, reply.called) + } +} + +// --- D: QA error --- + +func TestHandleQAErrorRepliesFriendlyMessage(t *testing.T) { + qa := &fakeQA{err: errors.New("weknora connection timeout: internal stack trace")} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if !reply.called { + t.Fatal("Reply.SendMarkdown should be called on QA error") + } + if !strings.Contains(reply.lastText, "暂时无法完成回答") { + t.Fatalf("reply text = %q, want friendly error message", reply.lastText) + } + if strings.Contains(reply.lastText, "weknora") || strings.Contains(reply.lastText, "timeout") || strings.Contains(reply.lastText, "stack") { + t.Fatalf("reply text leaks internal error: %q", reply.lastText) + } +} + +// --- E: invalid input --- + +func TestHandleEmptyTextReturnsError(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := directTextCallback() + data.Text = " " + err := h.Handle(context.Background(), data) + if err == nil { + t.Fatal("expected error for empty text") + } + if qa.called { + t.Fatal("QA.Ask should not be called for empty text") + } + if reply.called { + t.Fatal("Reply.SendMarkdown should not be called for empty text") + } +} + +func TestHandleMissingMsgIDReturnsError(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := directTextCallback() + data.MsgID = "" + err := h.Handle(context.Background(), data) + if err == nil { + t.Fatal("expected error for missing MsgId") + } + if qa.called { + t.Fatal("QA.Ask should not be called for missing MsgId") + } + if reply.called { + t.Fatal("Reply.SendMarkdown should not be called for missing MsgId") + } +} + +func TestHandleMissingUserReturnsError(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := directTextCallback() + data.SenderStaffID = "" + data.SenderID = "" + err := h.Handle(context.Background(), data) + if err == nil { + t.Fatal("expected error for missing user") + } + if qa.called { + t.Fatal("QA.Ask should not be called for missing user") + } + if reply.called { + t.Fatal("Reply.SendMarkdown should not be called for missing user") + } +} + +func TestHandleGroupMissingConversationIDReturnsError(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := groupTextCallback() + data.ConversationID = "" + err := h.Handle(context.Background(), data) + if err == nil { + t.Fatal("expected error for group missing ConversationId") + } + if qa.called { + t.Fatal("QA.Ask should not be called for group missing ConversationId") + } + if reply.called { + t.Fatal("Reply.SendMarkdown should not be called for group missing ConversationId") + } +} + +// --- F: missing sessionWebhook --- + +func TestHandleMissingSessionWebhookReturnsError(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "ok"}} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := directTextCallback() + data.SessionWebhook = "" + err := h.Handle(context.Background(), data) + if err == nil { + t.Fatal("expected error for missing sessionWebhook") + } + if qa.called { + t.Fatal("QA.Ask should not be called when sessionWebhook is missing (model call would be wasted)") + } + if reply.called { + t.Fatal("Reply.SendMarkdown should not be called when sessionWebhook is missing") + } +} + +func TestHandleNonTextMissingSessionWebhookReturnsError(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + h := newTestHandler(t, qa, reply) + + data := directTextCallback() + data.Msgtype = "picture" + data.SessionWebhook = "" + err := h.Handle(context.Background(), data) + if err == nil { + t.Fatal("expected error for non-text with missing sessionWebhook") + } +} + +// --- reply send failure --- + +// --- F: richText mixed (text + picture) --- + +func TestHandleRichTextMixedDownloadsAndCallsQAWithRealQuery(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "图文回答"}} + reply := &fakeReply{} + image := core.ImageInput{DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 4, Width: 1, Height: 1} + downloader := &fakeImageDownloader{image: image} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + + // Simulate richText mixed: text + picture + data := directTextCallback() + data.Msgtype = "picture" + data.DownloadCode = "temporary-download-code" + data.Text = " 请分析这张截图中的错误 " + + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle richText mixed: %v", err) + } + if !downloader.called || downloader.code != "temporary-download-code" { + t.Fatalf("download called=%v code=%q", downloader.called, downloader.code) + } + if !qa.called || len(qa.lastReq.Images) != 1 || qa.lastReq.Images[0] != image { + t.Fatalf("QA request did not receive image: %+v", qa.lastReq) + } + // Key assert: mixed uses real query, not default imageQuery + if qa.lastReq.Query != "请分析这张截图中的错误" { + t.Fatalf("richText mixed query=%q, want real query", qa.lastReq.Query) + } + if !reply.called || reply.lastText != "图文回答" { + t.Fatalf("reply=%+v", reply) + } +} + +func TestHandleRichTextMixedEmptyQueryFallsBackToDefault(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "默认回答"}} + reply := &fakeReply{} + image := core.ImageInput{DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 4, Width: 1, Height: 1} + downloader := &fakeImageDownloader{image: image} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + + data := directTextCallback() + data.Msgtype = "picture" + data.DownloadCode = "code" + data.Text = " " + + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle: %v", err) + } + // When Text is empty after trim, falls back to default imageQuery + if qa.lastReq.Query != imageQuery { + t.Fatalf("query=%q, want default imageQuery=%q", qa.lastReq.Query, imageQuery) + } +} + +func TestHandleIndependentPictureStillUsesDefaultQuery(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "图片回答"}} + reply := &fakeReply{} + image := core.ImageInput{DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 4, Width: 1, Height: 1} + downloader := &fakeImageDownloader{image: image} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + + // Independent picture: Text is empty (as A1 produces) + data := directPictureCallback() + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle picture: %v", err) + } + // Independent picture should still use default query + if qa.lastReq.Query != imageQuery { + t.Fatalf("picture query=%q, want default imageQuery=%q", qa.lastReq.Query, imageQuery) + } +} + +func TestHandleGroupRichTextMixedWithoutMentionDoesNothing(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + downloader := &fakeImageDownloader{} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + + data := directTextCallback() + data.Msgtype = "picture" + data.DownloadCode = "code" + data.Text = "忽略这条" + data.ConversationType = conversationTypeGroup + data.IsInAtList = false + data.SessionWebhook = "not even validated" + + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle: %v", err) + } + if downloader.called || qa.called || reply.called { + t.Fatalf("unexpected side effects: downloader=%v qa=%v reply=%v", downloader.called, qa.called, reply.called) + } +} + +func TestHandleRichTextMixedDownloadFailureFriendlyReplyNoQA(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + downloader := &fakeImageDownloader{err: errors.New("download failed")} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + + data := directTextCallback() + data.Msgtype = "picture" + data.DownloadCode = "broken-code" + data.Text = "帮我看看这张图" + + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle: %v", err) + } + if qa.called { + t.Fatal("QA must not be called on download failure") + } + if !reply.called || reply.lastText != imageErrorReply { + t.Fatalf("friendly reply=%q, want %q", reply.lastText, imageErrorReply) + } +} + +func TestHandleReplySendFailureReturnsError(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "ok"}} + reply := &fakeReply{err: errors.New("webhook timeout")} + h := newTestHandler(t, qa, reply) + + err := h.Handle(context.Background(), directTextCallback()) + if err == nil { + t.Fatal("expected error when reply send fails") + } +} + +// --- A3.1: AI card priority / markdown fallback --- + +func TestHandleCardSuccessDirectFieldsCorrectNoMarkdown(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "**card answer**"}} + reply := &fakeReply{} + card := &fakeCard{returnID: "out-track-123"} + h := newTestHandlerWithCard(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if !card.called { + t.Fatal("card.SendFinalCard was not called") + } + if card.lastReq.ConversationType != core.ConversationDirect { + t.Fatalf("card ConversationType = %q, want direct", card.lastReq.ConversationType) + } + if card.lastReq.ExternalUserID != "staff-123" { + t.Fatalf("card ExternalUserID = %q, want staff-123", card.lastReq.ExternalUserID) + } + if card.lastReq.Content != "**card answer**" { + t.Fatalf("card Content = %q, want answer", card.lastReq.Content) + } + if reply.called { + t.Fatal("SendMarkdown must not be called when card succeeds") + } +} + +func TestHandleCardSuccessGroupChatIDCorrect(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "group card answer"}} + reply := &fakeReply{} + card := &fakeCard{returnID: "out-track-group"} + h := newTestHandlerWithCard(t, qa, reply, card) + + err := h.Handle(context.Background(), groupTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if !card.called { + t.Fatal("card.SendFinalCard was not called") + } + if card.lastReq.ConversationType != core.ConversationGroup { + t.Fatalf("card ConversationType = %q, want group", card.lastReq.ConversationType) + } + if card.lastReq.ExternalChatID != "cid-group-1" { + t.Fatalf("card ExternalChatID = %q, want cid-group-1", card.lastReq.ExternalChatID) + } + if reply.called { + t.Fatal("SendMarkdown must not be called when card succeeds") + } +} + +func TestHandleCardFailureFallsBackToMarkdown(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "**fallback answer**"}} + reply := &fakeReply{} + card := &fakeCard{err: errors.New("card API 500")} + h := newTestHandlerWithCard(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if !card.called { + t.Fatal("card.SendFinalCard was not called") + } + if !reply.called { + t.Fatal("SendMarkdown must be called when card fails (fallback)") + } + if reply.lastText != "**fallback answer**" { + t.Fatalf("fallback reply text = %q, want answer", reply.lastText) + } +} + +func TestHandleQAFailureNoCardAndFriendlyMarkdown(t *testing.T) { + qa := &fakeQA{err: errors.New("internal weknora error")} + reply := &fakeReply{} + card := &fakeCard{} + h := newTestHandlerWithCard(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if card.called { + t.Fatal("card.SendFinalCard must not be called on QA failure") + } + if !reply.called { + t.Fatal("SendMarkdown must be called on QA failure") + } + if !strings.Contains(reply.lastText, "暂时无法完成回答") { + t.Fatalf("friendly reply text = %q", reply.lastText) + } + if strings.Contains(reply.lastText, "weknora") { + t.Fatalf("reply text leaks internal error: %q", reply.lastText) + } +} + +func TestHandleNilCardNormalTextRegression(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "**plain markdown**"}} + reply := &fakeReply{} + // Use NewHandler (no card) — must behave exactly as before + h := newTestHandler(t, qa, reply) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + if !reply.called { + t.Fatal("SendMarkdown must be called when no card sender") + } + if reply.lastText != "**plain markdown**" { + t.Fatalf("reply text = %q, want answer", reply.lastText) + } +} + +func TestHandleImageDownloadFailureNoCard(t *testing.T) { + qa := &fakeQA{} + reply := &fakeReply{} + card := &fakeCard{} + downloader := &fakeImageDownloader{err: errors.New("download failed")} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + // Inject card via options — test that download failure short-circuits before card + h.card = card + + data := directPictureCallback() + data.DownloadCode = "broken-code" + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle: %v", err) + } + if card.called { + t.Fatal("card.SendFinalCard must not be called on image download failure") + } + if qa.called { + t.Fatal("QA must not be called on download failure") + } + if !reply.called || reply.lastText != imageErrorReply { + t.Fatalf("friendly reply=%q, want %q", reply.lastText, imageErrorReply) + } +} + +func TestHandleNewHandlerBackwardCompat(t *testing.T) { + qa := &fakeQA{result: core.QAResult{Answer: "ok"}} + reply := &fakeReply{} + + // one downloader + h, err := NewHandler(TenantConfig{TenantKey: "t", Channel: "c"}, qa, reply, &fakeImageDownloader{}) + if err != nil { + t.Fatalf("NewHandler with one downloader: %v", err) + } + if h.downloader == nil { + t.Fatal("downloader should be set") + } + if h.card != nil { + t.Fatal("card should be nil when using NewHandler") + } + + // zero downloader + h2, err := NewHandler(TenantConfig{TenantKey: "t", Channel: "c"}, qa, reply) + if err != nil { + t.Fatalf("NewHandler with zero downloader: %v", err) + } + if h2.downloader != nil { + t.Fatal("downloader should be nil when not provided") + } + if h2.card != nil { + t.Fatal("card should be nil when using NewHandler") + } + + // two downloaders — must error + _, err = NewHandler(TenantConfig{TenantKey: "t", Channel: "c"}, qa, reply, &fakeImageDownloader{}, &fakeImageDownloader{}) + if err == nil { + t.Fatal("NewHandler with two downloaders should error") + } +} + +// --- A3.2: Streaming QA + Card integration --- + +// fakeStreamingQA implements both QAAsker and StreamingQAAsker. +// The observer is called synchronously with cumulative content during AskStream. +type fakeStreamingQA struct { + called bool + lastReq core.QARequest + result core.QAResult + err error + // streaming + streamCalled bool + chunks []string // observer receives cumulated content after each chunk + observerCaptured weknora.AnswerObserver +} + +func (f *fakeStreamingQA) Ask(_ context.Context, req core.QARequest) (core.QAResult, error) { + f.called = true + f.lastReq = req + return f.result, f.err +} + +func (f *fakeStreamingQA) AskStream(_ context.Context, req core.QARequest, observer weknora.AnswerObserver) (core.QAResult, error) { + f.streamCalled = true + f.lastReq = req + f.observerCaptured = observer + var accumulated string + for _, ch := range f.chunks { + accumulated += ch + if observer != nil { + observer(accumulated) + } + } + return f.result, f.err +} + +// fakeStreamingCard implements both FinalCardSender and StreamingCardSender. +type fakeStreamingCard struct { + // FinalCardSender + finalCalled bool + finalReq CardRequest + finalID string + finalErr error + + // StreamingCardSender + startCalled bool + startReq CardRequest + startID string + startErr error + + updates []cardUpdate + updateErr error +} + +type cardUpdate struct { + id string + content string + isFinalize bool + isError bool +} + +func (f *fakeStreamingCard) SendFinalCard(_ context.Context, req CardRequest) (string, error) { + f.finalCalled = true + f.finalReq = req + return f.finalID, f.finalErr +} + +func (f *fakeStreamingCard) StartCard(_ context.Context, req CardRequest) (string, error) { + f.startCalled = true + f.startReq = req + return f.startID, f.startErr +} + +func (f *fakeStreamingCard) UpdateCard(_ context.Context, id, content string, isFinalize, isError bool) error { + f.updates = append(f.updates, cardUpdate{id: id, content: content, isFinalize: isFinalize, isError: isError}) + return f.updateErr +} + +func newTestHandlerWithStreaming(t *testing.T, qa QAAsker, reply *fakeReply, card FinalCardSender) *Handler { + t.Helper() + cfg := TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"} + h, err := NewHandlerWithOptions(cfg, qa, reply, HandlerOptions{CardSender: card}) + if err != nil { + t.Fatalf("NewHandlerWithOptions: %v", err) + } + return h +} + +func TestHandleStreamingHappyPathDirect(t *testing.T) { + card := &fakeStreamingCard{startID: "track-happy-1"} + qa := &fakeStreamingQA{ + chunks: []string{"Hello", " World", "!"}, + result: core.QAResult{Answer: "Hello World!"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + // StartCard called once with placeholder + if !card.startCalled { + t.Fatal("StartCard was not called") + } + if card.startReq.Content != streamingPlaceholder { + t.Fatalf("StartCard Content = %q, want %q", card.startReq.Content, streamingPlaceholder) + } + if card.startReq.ConversationType != core.ConversationDirect { + t.Fatalf("StartCard ConversationType = %q, want direct", card.startReq.ConversationType) + } + if card.startReq.ExternalUserID != "staff-123" { + t.Fatalf("StartCard ExternalUserID = %q, want staff-123", card.startReq.ExternalUserID) + } + + // AskStream called, not Ask + if !qa.streamCalled { + t.Fatal("AskStream was not called") + } + + // First non-empty chunk → immediate update (not finalize) + if len(card.updates) < 1 { + t.Fatal("expected at least one update") + } + first := card.updates[0] + if first.id != "track-happy-1" { + t.Fatalf("first update id = %q, want track-happy-1", first.id) + } + if first.content != "Hello" { + t.Fatalf("first update content = %q, want Hello", first.content) + } + if first.isFinalize || first.isError { + t.Fatal("first update should not be finalize or error") + } + + // Final update must be the last one: content=full answer, isFinalize=true, isError=false + last := card.updates[len(card.updates)-1] + if last.content != "Hello World!" { + t.Fatalf("final update content = %q, want full answer", last.content) + } + if !last.isFinalize { + t.Fatal("final update must be finalize") + } + if last.isError { + t.Fatal("final update must not be error") + } + + // No markdown fallback + if reply.called { + t.Fatal("SendMarkdown must not be called on streaming success") + } +} + +func TestHandleStreamingThrottleSecondUpdate(t *testing.T) { + card := &fakeStreamingCard{startID: "track-throttle"} + qa := &fakeStreamingQA{ + chunks: []string{"A", "B", "C"}, + result: core.QAResult{Answer: "ABC"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + // Simulate clock advancing past interval + base := int64(0) + h.now = func() time.Time { return time.Unix(0, base) } + h.cardUpdateInterval = time.Millisecond + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + // All updates happen in the same goroutine, so the clock never advances + // during the single observer call chain. The first chunk fires immediately, + // subsequent chunks are throttled (clock hasn't advanced). + // The final update always fires. + if len(card.updates) < 2 { + t.Fatalf("expected at least first + final update, got %d", len(card.updates)) + } + // First update is immediate + if card.updates[0].isFinalize || card.updates[0].isError { + t.Fatal("first update must not be finalize/error") + } + // Last update is final + last := card.updates[len(card.updates)-1] + if !last.isFinalize || last.isError { + t.Fatal("last update must be finalize, not error") + } + if last.content != "ABC" { + t.Fatalf("final content = %q, want ABC", last.content) + } + if reply.called { + t.Fatal("SendMarkdown must not be called") + } +} + +func TestHandleStreamingThrottleClockAdvances(t *testing.T) { + card := &fakeStreamingCard{startID: "track-adv"} + qa := &fakeStreamingQA{ + chunks: []string{"X", "Y", "Z"}, + result: core.QAResult{Answer: "XYZ"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + callCount := int64(0) + h.now = func() time.Time { + c := callCount + callCount++ + // Each call advances by 800ms to cross the 750ms interval + return time.Unix(0, c*800_000_000) + } + h.cardUpdateInterval = 750 * time.Millisecond + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + // First chunk fires immediately (1 update), then each subsequent observer call + // sees clock advanced past interval → fires another update. + // So we should see: first("X"), second("XY"), final("XYZ") + nonFinal := 0 + for _, u := range card.updates { + if !u.isFinalize && !u.isError { + nonFinal++ + } + } + if nonFinal < 2 { + t.Fatalf("expected at least 2 intermediate updates with clock advance, got %d (updates=%+v)", nonFinal, card.updates) + } + last := card.updates[len(card.updates)-1] + if !last.isFinalize || last.isError { + t.Fatal("last update must be finalize") + } + if last.content != "XYZ" { + t.Fatalf("final content = %q, want XYZ", last.content) + } + if reply.called { + t.Fatal("SendMarkdown must not be called") + } +} + +func TestHandleStreamingStartFailsFallbackAskMarkdown(t *testing.T) { + card := &fakeStreamingCard{startErr: errors.New("card API unavailable")} + qa := &fakeStreamingQA{ + result: core.QAResult{Answer: "**fallback markdown**"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + // StartCard was attempted + if !card.startCalled { + t.Fatal("StartCard was not called") + } + + // Fallback: Ask (not AskStream) was called + if !qa.called { + t.Fatal("Ask (fallback) was not called") + } + if qa.streamCalled { + t.Fatal("AskStream must not be called on Start failure") + } + + // No SendFinalCard + if card.finalCalled { + t.Fatal("SendFinalCard must not be called on Start failure") + } + + // Markdown fallback + if !reply.called { + t.Fatal("SendMarkdown must be called on Start failure") + } + if reply.lastText != "**fallback markdown**" { + t.Fatalf("markdown text = %q, want fallback markdown", reply.lastText) + } + + // No card updates + if len(card.updates) > 0 { + t.Fatal("no card updates expected on Start failure") + } +} + +func TestHandleStreamingIntermediateUpdateFailsContinuesQA(t *testing.T) { + card := &fakeStreamingCard{startID: "track-midfail", updateErr: errors.New("update HTTP 500")} + qa := &fakeStreamingQA{ + chunks: []string{"Hello", " World"}, + result: core.QAResult{Answer: "Hello World"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + // QA still completed (AskStream must have been called and returned) + if !qa.streamCalled { + t.Fatal("AskStream must be called") + } + + // There must be a final update attempt (even though intermediate failed) + foundFinal := false + for _, u := range card.updates { + if u.isFinalize && !u.isError { + foundFinal = true + } + } + if !foundFinal { + t.Fatal("final update must be attempted even after intermediate failures") + } + + // Since updateErr is set, the final update also fails → markdown fallback + if !reply.called { + t.Fatal("SendMarkdown must be called when final update fails") + } + if reply.lastText != "Hello World" { + t.Fatalf("markdown text = %q, want full answer", reply.lastText) + } +} + +func TestHandleStreamingFinalUpdateFailsMarkdown(t *testing.T) { + card := &fakeStreamingCard{startID: "track-finalfail"} + qa := &fakeStreamingQA{ + chunks: []string{"OK"}, + result: core.QAResult{Answer: "OK"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + // Make only the final update fail by using a counter in updateErr + updateCount := 0 + card.updateErr = nil // will be set per-call via a wrapper + // We'll use a different approach: make the final call fail by checking content + // Actually, simplest: set updateErr to an error, and the test checks markdown fallback + card.updateErr = errors.New("finalize API error") + _ = updateCount + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + if !qa.streamCalled { + t.Fatal("AskStream must be called") + } + + // Markdown fallback with full answer + if !reply.called { + t.Fatal("SendMarkdown must be called when final update fails") + } + if reply.lastText != "OK" { + t.Fatalf("markdown text = %q, want OK", reply.lastText) + } +} + +func TestHandleStreamingQAErrorConvergesErrorCard(t *testing.T) { + card := &fakeStreamingCard{startID: "track-errcard"} + qa := &fakeStreamingQA{ + chunks: []string{"Partial"}, + err: errors.New("secret weknora internal failure"), + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + if !qa.streamCalled { + t.Fatal("AskStream must be called") + } + + // Last update must be error card: isFinalize=true, isError=true, content=qaErrorReply + last := card.updates[len(card.updates)-1] + if !last.isFinalize { + t.Fatal("error card must be finalize") + } + if !last.isError { + t.Fatal("error card must be isError") + } + if last.content != qaErrorReply { + t.Fatalf("error card content = %q, want %q", last.content, qaErrorReply) + } + // Must not leak QA error + if strings.Contains(last.content, "weknora") || strings.Contains(last.content, "secret") { + t.Fatalf("error card leaks internal error: %q", last.content) + } + + // No markdown (error card succeeded) + if reply.called { + t.Fatal("SendMarkdown must not be called when error card succeeds") + } + + // QA error was not leaked in any update content + for _, u := range card.updates { + if strings.Contains(u.content, "weknora") || strings.Contains(u.content, "secret") { + t.Fatalf("update leaks internal error: %q", u.content) + } + } +} + +func TestHandleStreamingErrorCardFinalizeFailsMarkdown(t *testing.T) { + card := &fakeStreamingCard{startID: "track-errfinalfail", updateErr: errors.New("finalize API error")} + qa := &fakeStreamingQA{ + chunks: []string{"Partial"}, + err: errors.New("secret weknora internal failure"), + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + // Markdown fallback with friendly error message + if !reply.called { + t.Fatal("SendMarkdown must be called when error card finalize fails") + } + if reply.lastText != qaErrorReply { + t.Fatalf("markdown text = %q, want %q", reply.lastText, qaErrorReply) + } + // Must not leak QA error + if strings.Contains(reply.lastText, "weknora") || strings.Contains(reply.lastText, "secret") { + t.Fatalf("markdown leaks internal error: %q", reply.lastText) + } +} + +func TestHandleOldFakeCardStillA31(t *testing.T) { + // fakeCard does NOT implement StreamingCardSender — must use A3.1 path + card := &fakeCard{returnID: "out-a31"} + qa := &fakeStreamingQA{ + result: core.QAResult{Answer: "**a31 answer**"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + // A3.1: Ask (not AskStream) + SendFinalCard + if !qa.called { + t.Fatal("Ask must be called (A3.1 path)") + } + if qa.streamCalled { + t.Fatal("AskStream must not be called (A3.1 path)") + } + if !card.called { + t.Fatal("SendFinalCard must be called (A3.1 path)") + } + if card.lastReq.Content != "**a31 answer**" { + t.Fatalf("card content = %q, want a31 answer", card.lastReq.Content) + } + if reply.called { + t.Fatal("SendMarkdown must not be called when card succeeds") + } +} + +func TestHandleStreamingGroupNotAtNoStart(t *testing.T) { + card := &fakeStreamingCard{startID: "track-group"} + qa := &fakeStreamingQA{} + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + data := groupTextCallback() + data.IsInAtList = false + err := h.Handle(context.Background(), data) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + if card.startCalled { + t.Fatal("StartCard must not be called for non-@ group message") + } + if qa.called || qa.streamCalled { + t.Fatal("QA must not be called for non-@ group message") + } + if reply.called { + t.Fatal("SendMarkdown must not be called for non-@ group message") + } +} + +func TestHandleStreamingImageDownloadFailureNoStart(t *testing.T) { + card := &fakeStreamingCard{startID: "track-img"} + qa := &fakeStreamingQA{} + reply := &fakeReply{} + downloader := &fakeImageDownloader{err: errors.New("download failed")} + h := newTestHandlerWithDownloader(t, qa, reply, downloader) + h.card = card + + data := directPictureCallback() + data.DownloadCode = "broken-code" + if err := h.Handle(context.Background(), data); err != nil { + t.Fatalf("Handle: %v", err) + } + + if card.startCalled { + t.Fatal("StartCard must not be called on image download failure") + } + if qa.called || qa.streamCalled { + t.Fatal("QA must not be called on download failure") + } + if !reply.called || reply.lastText != imageErrorReply { + t.Fatalf("friendly reply=%q, want %q", reply.lastText, imageErrorReply) + } +} + +func TestHandleStreamingQAEmptyAnswerConvergesErrorCard(t *testing.T) { + card := &fakeStreamingCard{startID: "track-empty"} + qa := &fakeStreamingQA{ + chunks: []string{}, + result: core.QAResult{Answer: ""}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + if !qa.streamCalled { + t.Fatal("AskStream must be called") + } + + // Empty answer: error card with qaErrorReply + last := card.updates[len(card.updates)-1] + if !last.isFinalize || !last.isError { + t.Fatal("empty answer must produce error card") + } + if last.content != qaErrorReply { + t.Fatalf("error card content = %q, want %q", last.content, qaErrorReply) + } + if reply.called { + t.Fatal("SendMarkdown must not be called when error card succeeds") + } +} + +func TestHandleStreamingObserverIgnoresWhitespaceOnly(t *testing.T) { + card := &fakeStreamingCard{startID: "track-ws"} + qa := &fakeStreamingQA{ + // The observer is called with cumulative content; whitespace-only + // chunks should not trigger updates. + chunks: []string{" ", " \n ", "Real"}, + result: core.QAResult{Answer: " \n Real"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + // First non-whitespace update is "Real" (cumulated) + firstUpdate := card.updates[0] + if firstUpdate.content != " \n Real" { + t.Fatalf("first update content = %q, want cumulative whitespace plus Real", firstUpdate.content) + } + if firstUpdate.isFinalize || firstUpdate.isError { + t.Fatal("first update must not be finalize/error") + } + + last := card.updates[len(card.updates)-1] + if !last.isFinalize || last.isError { + t.Fatal("last update must be finalize, not error") + } + if reply.called { + t.Fatal("SendMarkdown must not be called") + } +} + +func TestHandleStreamingStartFailsQAFailsMarkdownFriendly(t *testing.T) { + // Start fails → fallback to Ask → Ask also fails → friendly markdown + card := &fakeStreamingCard{startErr: errors.New("card API unavailable")} + qa := &fakeStreamingQA{ + err: errors.New("secret weknora internal failure"), + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + if !card.startCalled { + t.Fatal("StartCard was not called") + } + if !qa.called { + t.Fatal("Ask (fallback) was not called") + } + if qa.streamCalled { + t.Fatal("AskStream must not be called") + } + if !reply.called { + t.Fatal("SendMarkdown must be called") + } + if !strings.Contains(reply.lastText, "暂时无法完成回答") { + t.Fatalf("friendly markdown = %q", reply.lastText) + } + if strings.Contains(reply.lastText, "weknora") || strings.Contains(reply.lastText, "secret") { + t.Fatalf("markdown leaks internal error: %q", reply.lastText) + } +} + +func TestHandleStreamingFinalUpdateAlwaysFiresEvenWithoutIntermediate(t *testing.T) { + // No intermediate chunks (observer never called with non-empty content), + // but final update must still fire with the answer. + card := &fakeStreamingCard{startID: "track-noint"} + qa := &fakeStreamingQA{ + chunks: []string{}, // no intermediate chunks + result: core.QAResult{Answer: "direct answer"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), directTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + if !qa.streamCalled { + t.Fatal("AskStream must be called") + } + + // Must have exactly one update: final + if len(card.updates) != 1 { + t.Fatalf("expected 1 update (final only), got %d: %+v", len(card.updates), card.updates) + } + u := card.updates[0] + if !u.isFinalize || u.isError { + t.Fatal("only update must be finalize, not error") + } + if u.content != "direct answer" { + t.Fatalf("content = %q, want direct answer", u.content) + } + if reply.called { + t.Fatal("SendMarkdown must not be called") + } +} + +func TestHandleStreamingGroupWithCard(t *testing.T) { + card := &fakeStreamingCard{startID: "track-group-ok"} + qa := &fakeStreamingQA{ + chunks: []string{"group", " reply"}, + result: core.QAResult{Answer: "group reply"}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + + err := h.Handle(context.Background(), groupTextCallback()) + if err != nil { + t.Fatalf("Handle: %v", err) + } + + if !card.startCalled { + t.Fatal("StartCard was not called") + } + if card.startReq.ConversationType != core.ConversationGroup { + t.Fatalf("StartCard ConversationType = %q, want group", card.startReq.ConversationType) + } + if card.startReq.ExternalChatID != "cid-group-1" { + t.Fatalf("StartCard ExternalChatID = %q, want cid-group-1", card.startReq.ExternalChatID) + } + if !qa.streamCalled { + t.Fatal("AskStream was not called") + } + last := card.updates[len(card.updates)-1] + if !last.isFinalize || last.isError { + t.Fatal("last update must be finalize, not error") + } + if last.content != "group reply" { + t.Fatalf("final content = %q, want group reply", last.content) + } + if reply.called { + t.Fatal("SendMarkdown must not be called") + } +} + +// --- A3.3: safe progress events + asynchronous card coalescing --- + +type fakeEventQA struct { + fakeStreamingQA + events []weknora.StreamEvent + eventCalled bool +} + +func (f *fakeEventQA) AskEvents(_ context.Context, req core.QARequest, observer weknora.StreamObserver) (core.QAResult, error) { + f.eventCalled = true + f.lastReq = req + for _, event := range f.events { + if observer != nil { + observer(event) + } + } + return f.result, f.err +} + +func TestRenderProgressEventUsesOnlySafeFixedText(t *testing.T) { + tests := []struct { + name string + event weknora.StreamEvent + want string + }{ + {"understanding", weknora.StreamEvent{ResponseType: "tool_call", ToolName: "query_understand"}, "正在理解问题…"}, + {"understood", weknora.StreamEvent{ResponseType: "tool_result", ToolName: "query_understand"}, "问题理解完成\n\n正在检索知识库…"}, + {"searching", weknora.StreamEvent{ResponseType: "tool_call", ToolName: "knowledge_search"}, "正在检索知识库…"}, + {"searched", weknora.StreamEvent{ResponseType: "tool_result", ToolName: "knowledge_search"}, "知识库检索完成\n\n正在生成答案…"}, + {"references", weknora.StreamEvent{ResponseType: "references", ReferenceCount: 7}, "检索到 7 条相关内容\n\n正在生成答案…"}, + {"thinking hidden", weknora.StreamEvent{ResponseType: "thinking"}, ""}, + {"unknown tool hidden", weknora.StreamEvent{ResponseType: "tool_call", ToolName: "secret_tool"}, ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := renderProgressEvent(tt.event); got != tt.want { + t.Fatalf("renderProgressEvent=%q want %q", got, tt.want) + } + }) + } +} + +func TestHandleEventStreamingFinalizesLatestAnswerWithoutMarkdown(t *testing.T) { + card := &fakeStreamingCard{startID: "track-events"} + qa := &fakeEventQA{ + fakeStreamingQA: fakeStreamingQA{result: core.QAResult{Answer: "完整答案"}}, + events: []weknora.StreamEvent{ + {ResponseType: "tool_call", ToolName: "query_understand"}, + {ResponseType: "tool_call", ToolName: "knowledge_search"}, + {ResponseType: "references", ReferenceCount: 3}, + {ResponseType: "answer", FullAnswer: "部分"}, + {ResponseType: "answer", FullAnswer: "完整答案", Done: true}, + }, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + h.cardUpdateInterval = time.Hour + if err := h.Handle(context.Background(), directTextCallback()); err != nil { + t.Fatalf("Handle: %v", err) + } + if !qa.eventCalled || qa.streamCalled { + t.Fatalf("eventCalled=%v streamCalled=%v", qa.eventCalled, qa.streamCalled) + } + if card.startReq.Content != streamingPlaceholder { + t.Fatalf("placeholder=%q", card.startReq.Content) + } + if len(card.updates) != 1 { + t.Fatalf("stale async updates must be dropped before final: %+v", card.updates) + } + last := card.updates[0] + if last.content != "完整答案" || !last.isFinalize || last.isError { + t.Fatalf("final update=%+v", last) + } + if reply.called { + t.Fatal("SendMarkdown must not be called") + } +} + +func TestHandleEventStreamingErrorConvergesCard(t *testing.T) { + card := &fakeStreamingCard{startID: "track-event-error"} + qa := &fakeEventQA{ + fakeStreamingQA: fakeStreamingQA{err: errors.New("private upstream failure")}, + events: []weknora.StreamEvent{{ResponseType: "tool_call", ToolName: "knowledge_search"}, {ResponseType: "error", Done: true}}, + } + reply := &fakeReply{} + h := newTestHandlerWithStreaming(t, qa, reply, card) + h.cardUpdateInterval = time.Hour + if err := h.Handle(context.Background(), directTextCallback()); err != nil { + t.Fatalf("Handle: %v", err) + } + last := card.updates[len(card.updates)-1] + if last.content != qaErrorReply || !last.isFinalize || !last.isError { + t.Fatalf("error final=%+v", last) + } + if strings.Contains(last.content, "private") || reply.called { + t.Fatalf("error leaked or markdown duplicated: content=%q markdown=%v", last.content, reply.called) + } +} + +type deadlineFinalCard struct{} + +func (deadlineFinalCard) SendFinalCard(context.Context, CardRequest) (string, error) { + return "track-deadline", nil +} + +func (deadlineFinalCard) StartCard(context.Context, CardRequest) (string, error) { + return "track-deadline", nil +} + +func (deadlineFinalCard) UpdateCard(ctx context.Context, _ string, _ string, isFinalize, _ bool) error { + if !isFinalize { + return nil + } + <-ctx.Done() + return ctx.Err() +} + +type contextCheckingReply struct { + called bool + contextErr error +} + +func (r *contextCheckingReply) SendMarkdown(ctx context.Context, _, _, _ string) error { + r.called = true + r.contextErr = ctx.Err() + return r.contextErr +} + +func TestHandleEventStreamingFinalTimeoutUsesFreshMarkdownContext(t *testing.T) { + qa := &fakeEventQA{ + fakeStreamingQA: fakeStreamingQA{result: core.QAResult{Answer: "完整答案"}}, + events: []weknora.StreamEvent{{ResponseType: "answer", FullAnswer: "完整答案", Done: true}}, + } + reply := &contextCheckingReply{} + h, err := NewHandlerWithOptions( + TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"}, + qa, + reply, + HandlerOptions{CardSender: deadlineFinalCard{}}, + ) + if err != nil { + t.Fatalf("NewHandlerWithOptions: %v", err) + } + h.cardUpdateInterval = time.Hour + h.finalDeliveryTimeout = 5 * time.Millisecond + + if err := h.Handle(context.Background(), directTextCallback()); err != nil { + t.Fatalf("Markdown fallback must receive a fresh context: %v", err) + } + if !reply.called { + t.Fatal("Markdown fallback was not called") + } + if reply.contextErr != nil { + t.Fatalf("Markdown fallback received expired context: %v", reply.contextErr) + } +} diff --git a/internal/channels/dingtalk/image.go b/internal/channels/dingtalk/image.go new file mode 100644 index 0000000..64deac3 --- /dev/null +++ b/internal/channels/dingtalk/image.go @@ -0,0 +1,342 @@ +package dingtalk + +import ( + "bytes" + "context" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "image" + _ "image/gif" + _ "image/jpeg" + _ "image/png" + "io" + "net" + "net/http" + "net/netip" + "net/url" + "strings" + "sync" + "time" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" +) + +const ( + oauthTokenURL = "https://api.dingtalk.com/v1.0/oauth2/accessToken" + messageFileDownloadURL = "https://api.dingtalk.com/v1.0/robot/messageFiles/download" + maxAPIResponseBytes = 1 << 20 + maxImageBytes = 8 << 20 + maxImagePixels = 40_000_000 +) + +var blockedDownloadRanges = []netip.Prefix{ + netip.MustParsePrefix("100.64.0.0/10"), + netip.MustParsePrefix("192.0.0.0/24"), + netip.MustParsePrefix("192.0.2.0/24"), + netip.MustParsePrefix("198.18.0.0/15"), + netip.MustParsePrefix("198.51.100.0/24"), + netip.MustParsePrefix("203.0.113.0/24"), + netip.MustParsePrefix("2001:db8::/32"), +} + +type DingTalkImageDownloader struct { + clientID string + clientSecret string + tokenURL string + fileURL string + apiClient *http.Client + downloadHTTP *http.Client + allowTestURL bool + + mu sync.Mutex + token string + tokenTill time.Time +} + +func NewImageDownloader(clientID, clientSecret string, client *http.Client) (*DingTalkImageDownloader, error) { + return newImageDownloader(clientID, clientSecret, client, oauthTokenURL, messageFileDownloadURL, false) +} + +func newImageDownloaderForTest(clientID, clientSecret string, client *http.Client, tokenURL, fileURL string) (*DingTalkImageDownloader, error) { + return newImageDownloader(clientID, clientSecret, client, tokenURL, fileURL, true) +} + +func newImageDownloader(clientID, clientSecret string, client *http.Client, tokenURL, fileURL string, allowTest bool) (*DingTalkImageDownloader, error) { + clientID = strings.TrimSpace(clientID) + clientSecret = strings.TrimSpace(clientSecret) + if clientID == "" { + return nil, errors.New("dingtalk image downloader: clientID is required") + } + if clientSecret == "" { + return nil, errors.New("dingtalk image downloader: clientSecret is required") + } + base := &http.Client{Timeout: 30 * time.Second} + if client != nil { + copyClient := *client + base = ©Client + if base.Timeout <= 0 { + base.Timeout = 30 * time.Second + } + } + apiClient := *base + apiClient.CheckRedirect = func(*http.Request, []*http.Request) error { + return errors.New("dingtalk API redirect refused") + } + downloadClient := *base + if !allowTest { + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.Proxy = nil + transport.DialContext = safeDialContext + apiClient.Transport = transport + downloadClient.Transport = transport.Clone() + } + d := &DingTalkImageDownloader{ + clientID: clientID, clientSecret: clientSecret, + tokenURL: tokenURL, fileURL: fileURL, + apiClient: &apiClient, downloadHTTP: &downloadClient, allowTestURL: allowTest, + } + d.downloadHTTP.CheckRedirect = func(req *http.Request, via []*http.Request) error { + if len(via) >= 3 { + return errors.New("dingtalk image redirect limit exceeded") + } + req.Header = make(http.Header) + if !d.allowTestURL { + if err := validateDownloadURL(req.URL); err != nil { + return err + } + } + return nil + } + return d, nil +} + +func (d *DingTalkImageDownloader) Download(ctx context.Context, downloadCode string) (core.ImageInput, error) { + downloadCode = strings.TrimSpace(downloadCode) + if downloadCode == "" { + return core.ImageInput{}, errors.New("dingtalk image download: downloadCode is required") + } + token, err := d.accessToken(ctx) + if err != nil { + return core.ImageInput{}, err + } + payload, err := d.postJSON(ctx, d.fileURL, map[string]string{ + "downloadCode": downloadCode, + "robotCode": d.clientID, + }, token) + if err != nil { + return core.ImageInput{}, err + } + var fileResponse struct { + DownloadURL string `json:"downloadUrl"` + } + if json.Unmarshal(payload, &fileResponse) != nil || strings.TrimSpace(fileResponse.DownloadURL) == "" { + return core.ImageInput{}, errors.New("dingtalk image download: invalid message file response") + } + downloadURL := strings.TrimSpace(fileResponse.DownloadURL) + if !d.allowTestURL { + downloadURL, err = normalizeInitialDownloadURL(downloadURL) + if err != nil { + return core.ImageInput{}, err + } + parsed, parseErr := url.Parse(downloadURL) + if parseErr != nil || validateDownloadURL(parsed) != nil { + return core.ImageInput{}, errors.New("dingtalk image download: unsafe download URL") + } + } + req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil) + if err != nil { + return core.ImageInput{}, errors.New("dingtalk image download: invalid download request") + } + resp, err := d.downloadHTTP.Do(req) + if err != nil { + if ctx.Err() != nil { + return core.ImageInput{}, ctx.Err() + } + return core.ImageInput{}, errors.New("dingtalk image download: request failed") + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return core.ImageInput{}, fmt.Errorf("dingtalk image download: HTTP %d", resp.StatusCode) + } + if resp.ContentLength > maxImageBytes { + return core.ImageInput{}, errors.New("dingtalk image download: image exceeds size limit") + } + data, err := io.ReadAll(io.LimitReader(resp.Body, maxImageBytes+1)) + if err != nil { + return core.ImageInput{}, errors.New("dingtalk image download: read failed") + } + if len(data) > maxImageBytes { + return core.ImageInput{}, errors.New("dingtalk image download: image exceeds size limit") + } + return validateImageBytes(data) +} + +func (d *DingTalkImageDownloader) accessToken(ctx context.Context) (string, error) { + d.mu.Lock() + defer d.mu.Unlock() + if d.token != "" && time.Now().Before(d.tokenTill) { + return d.token, nil + } + payload, err := d.postJSON(ctx, d.tokenURL, map[string]string{ + "appKey": d.clientID, "appSecret": d.clientSecret, + }, "") + if err != nil { + return "", err + } + var response struct { + AccessToken string `json:"accessToken"` + ExpireIn int64 `json:"expireIn"` + } + if json.Unmarshal(payload, &response) != nil || strings.TrimSpace(response.AccessToken) == "" { + return "", errors.New("dingtalk image download: invalid OAuth response") + } + ttl := time.Duration(response.ExpireIn) * time.Second + refreshAfter := ttl - 5*time.Minute + if refreshAfter <= 0 { + refreshAfter = ttl / 2 + } + if refreshAfter <= 0 { + refreshAfter = time.Minute + } + d.token = response.AccessToken + d.tokenTill = time.Now().Add(refreshAfter) + return d.token, nil +} + +func (d *DingTalkImageDownloader) postJSON(ctx context.Context, endpoint string, value any, token string) ([]byte, error) { + body, err := json.Marshal(value) + if err != nil { + return nil, errors.New("dingtalk image download: encode API request failed") + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) + if err != nil { + return nil, errors.New("dingtalk image download: create API request failed") + } + req.Header.Set("Content-Type", "application/json") + if token != "" { + req.Header.Set("x-acs-dingtalk-access-token", token) + } + resp, err := d.apiClient.Do(req) + if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, errors.New("dingtalk image download: API request failed") + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return nil, fmt.Errorf("dingtalk image download: API HTTP %d", resp.StatusCode) + } + return readBoundedAPIResponse(resp) +} + +func readBoundedAPIResponse(resp *http.Response) ([]byte, error) { + if resp.ContentLength > maxAPIResponseBytes { + return nil, errors.New("dingtalk image download: API response exceeds size limit") + } + payload, err := io.ReadAll(io.LimitReader(resp.Body, maxAPIResponseBytes+1)) + if err != nil { + return nil, errors.New("dingtalk image download: API response read failed") + } + if len(payload) > maxAPIResponseBytes { + return nil, errors.New("dingtalk image download: API response exceeds size limit") + } + return payload, nil +} + +func validateImageBytes(data []byte) (core.ImageInput, error) { + if len(data) == 0 || len(data) > maxImageBytes { + return core.ImageInput{}, errors.New("dingtalk image download: invalid image size") + } + mime := http.DetectContentType(data) + expectedFormat := map[string]string{ + "image/png": "png", "image/jpeg": "jpeg", "image/gif": "gif", + }[mime] + if expectedFormat == "" { + return core.ImageInput{}, errors.New("dingtalk image download: unsupported image format") + } + config, format, err := image.DecodeConfig(bytes.NewReader(data)) + if err != nil || format != expectedFormat || config.Width <= 0 || config.Height <= 0 { + return core.ImageInput{}, errors.New("dingtalk image download: invalid image data") + } + if int64(config.Width)*int64(config.Height) > maxImagePixels { + return core.ImageInput{}, errors.New("dingtalk image download: image dimensions exceed limit") + } + return core.ImageInput{ + DataURI: "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(data), + MIMEType: mime, SizeBytes: int64(len(data)), Width: config.Width, Height: config.Height, + }, nil +} + +func normalizeInitialDownloadURL(raw string) (string, error) { + parsed, err := url.Parse(strings.TrimSpace(raw)) + if err != nil || parsed.Hostname() == "" || parsed.User != nil { + return "", errors.New("dingtalk image download: unsafe download URL") + } + switch parsed.Scheme { + case "http": + parsed.Scheme = "https" + case "https": + default: + return "", errors.New("dingtalk image download: unsafe download URL") + } + return parsed.String(), nil +} + +func validateDownloadURL(parsed *url.URL) error { + if parsed == nil || parsed.Scheme != "https" || parsed.Hostname() == "" || parsed.User != nil { + return errors.New("dingtalk image download: unsafe download URL") + } + return nil +} + +func safeDialContext(ctx context.Context, network, address string) (net.Conn, error) { + host, port, err := net.SplitHostPort(address) + if err != nil { + return nil, errors.New("dingtalk image download: invalid destination") + } + var ips []net.IP + if literal := net.ParseIP(host); literal != nil { + ips = []net.IP{literal} + } else { + resolved, resolveErr := net.DefaultResolver.LookupIPAddr(ctx, host) + if resolveErr != nil || len(resolved) == 0 { + return nil, errors.New("dingtalk image download: destination resolution failed") + } + for _, item := range resolved { + ips = append(ips, item.IP) + } + } + for _, ip := range ips { + if !isSafeDownloadIP(ip) { + return nil, errors.New("dingtalk image download: destination is not public") + } + } + dialer := &net.Dialer{Timeout: 15 * time.Second, KeepAlive: 30 * time.Second} + for _, ip := range ips { + conn, dialErr := dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), port)) + if dialErr == nil { + return conn, nil + } + } + return nil, errors.New("dingtalk image download: destination connection failed") +} + +func isSafeDownloadIP(ip net.IP) bool { + if ip == nil || !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsMulticast() || ip.IsUnspecified() { + return false + } + addr, ok := netip.AddrFromSlice(ip) + if !ok { + return false + } + addr = addr.Unmap() + for _, prefix := range blockedDownloadRanges { + if prefix.Contains(addr) { + return false + } + } + return true +} diff --git a/internal/channels/dingtalk/image_test.go b/internal/channels/dingtalk/image_test.go new file mode 100644 index 0000000..4dd91a1 --- /dev/null +++ b/internal/channels/dingtalk/image_test.go @@ -0,0 +1,226 @@ +package dingtalk + +import ( + "bytes" + "context" + "encoding/json" + "image" + "image/color" + "image/png" + "io" + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func tinyPNG(t *testing.T) []byte { + t.Helper() + img := image.NewRGBA(image.Rect(0, 0, 2, 3)) + for y := 0; y < 3; y++ { + for x := 0; x < 2; x++ { + img.Set(x, y, color.RGBA{R: 255, A: 255}) + } + } + var out bytes.Buffer + if err := png.Encode(&out, img); err != nil { + t.Fatal(err) + } + return out.Bytes() +} + +func TestNewImageDownloaderMissingCredentials(t *testing.T) { + for _, tc := range []struct{ id, secret string }{{"", "s"}, {"id", ""}, {" ", "s"}, {"id", " "}} { + if got, err := NewImageDownloader(tc.id, tc.secret, nil); err == nil || got != nil { + t.Fatalf("NewImageDownloader(%q,%q)=%v,%v", tc.id, tc.secret, got, err) + } + } +} + +func TestDownloadEmptyCodeReturnsError(t *testing.T) { + d, err := NewImageDownloader("id", "secret", nil) + if err != nil { + t.Fatal(err) + } + for _, code := range []string{"", " "} { + if _, err := d.Download(context.Background(), code); err == nil { + t.Fatalf("Download(%q) should fail", code) + } + } +} + +func TestImageDownloaderTwoHopAndTokenCache(t *testing.T) { + pngBytes := tinyPNG(t) + var tokenCalls, fileCalls int + var server *httptest.Server + server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/token": + tokenCalls++ + var body map[string]string + _ = json.NewDecoder(r.Body).Decode(&body) + if body["appKey"] != "client-id" || body["appSecret"] != "client-secret" { + t.Errorf("oauth body=%v", body) + } + if body["clientId"] != "" || body["clientSecret"] != "" { + t.Errorf("legacy OAuth field names must not be sent: %v", body) + } + _ = json.NewEncoder(w).Encode(map[string]any{"accessToken": "access-token", "expireIn": 3600}) + case "/file": + fileCalls++ + if r.Header.Get("x-acs-dingtalk-access-token") != "access-token" { + t.Errorf("token header missing") + } + var body map[string]string + _ = json.NewDecoder(r.Body).Decode(&body) + if body["downloadCode"] != "download-code" || body["robotCode"] != "client-id" { + t.Errorf("file body=%v", body) + } + _ = json.NewEncoder(w).Encode(map[string]string{"downloadUrl": server.URL + "/image?signature=secret"}) + case "/image": + w.Header().Set("Content-Type", "text/plain") // deliberately untrusted + _, _ = w.Write(pngBytes) + default: + http.NotFound(w, r) + } + })) + defer server.Close() + + d, err := newImageDownloaderForTest("client-id", "client-secret", server.Client(), server.URL+"/token", server.URL+"/file") + if err != nil { + t.Fatal(err) + } + for i := 0; i < 2; i++ { + got, err := d.Download(context.Background(), "download-code") + if err != nil { + t.Fatalf("Download: %v", err) + } + if got.MIMEType != "image/png" || got.Width != 2 || got.Height != 3 || got.SizeBytes != int64(len(pngBytes)) || !strings.HasPrefix(got.DataURI, "data:image/png;base64,") { + t.Fatalf("image=%+v", got) + } + } + if tokenCalls != 1 || fileCalls != 2 { + t.Fatalf("tokenCalls=%d fileCalls=%d", tokenCalls, fileCalls) + } +} + +func TestImageDownloaderRejectsRedirectWithoutLeakingToken(t *testing.T) { + var leaked string + target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + leaked = r.Header.Get("x-acs-dingtalk-access-token") + w.WriteHeader(http.StatusNoContent) + })) + defer target.Close() + var source *httptest.Server + source = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/token" { + _ = json.NewEncoder(w).Encode(map[string]any{"accessToken": "top-secret-token", "expireIn": 3600}) + return + } + http.Redirect(w, r, target.URL, http.StatusFound) + })) + defer source.Close() + d, err := newImageDownloaderForTest("id", "secret", source.Client(), source.URL+"/token", source.URL+"/file") + if err != nil { + t.Fatal(err) + } + if _, err := d.Download(context.Background(), "code"); err == nil { + t.Fatal("API redirect should fail") + } + if leaked != "" { + t.Fatalf("token leaked across redirect: %q", leaked) + } +} + +func TestImageDownloaderRejectsInvalidImageAndOversize(t *testing.T) { + tests := []struct { + name string + contentType string + body []byte + length int64 + }{ + {name: "html disguised", contentType: "image/png", body: []byte("not image")}, + {name: "oversized content length", contentType: "image/png", body: []byte("x"), length: maxImageBytes + 1}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var server *httptest.Server + server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/token": + _ = json.NewEncoder(w).Encode(map[string]any{"accessToken": "token", "expireIn": 3600}) + case "/file": + _ = json.NewEncoder(w).Encode(map[string]string{"downloadUrl": server.URL + "/image?q=signed-secret"}) + case "/image": + w.Header().Set("Content-Type", tt.contentType) + if tt.length > 0 { + w.Header().Set("Content-Length", "8388609") + } + _, _ = w.Write(tt.body) + } + })) + defer server.Close() + d, _ := newImageDownloaderForTest("id", "secret", server.Client(), server.URL+"/token", server.URL+"/file") + _, err := d.Download(context.Background(), "download-secret") + if err == nil { + t.Fatal("expected rejection") + } + for _, secret := range []string{"download-secret", "signed-secret", "token"} { + if strings.Contains(err.Error(), secret) { + t.Fatalf("error leaked %q: %v", secret, err) + } + } + }) + } +} + +func TestNormalizeInitialDownloadURL(t *testing.T) { + got, err := normalizeInitialDownloadURL("http://example.com/media/path?signature=secret") + if err != nil { + t.Fatalf("normalize: %v", err) + } + if got != "https://example.com/media/path?signature=secret" { + t.Fatalf("normalized=%q", got) + } + for _, raw := range []string{"ftp://example.com/a", "http://user:pass@example.com/a", "not-a-url"} { + if _, err := normalizeInitialDownloadURL(raw); err == nil { + t.Fatalf("%q should fail", raw) + } + } +} + +func TestSafeDownloadIPRejectsNonPublicRanges(t *testing.T) { + for _, raw := range []string{"127.0.0.1", "10.0.0.1", "192.168.1.1", "169.254.1.1", "100.64.0.1", "192.0.2.1", "198.18.0.1", "203.0.113.1", "::1", "fc00::1", "2001:db8::1"} { + if isSafeDownloadIP(net.ParseIP(raw)) { + t.Errorf("%s must be rejected", raw) + } + } + for _, raw := range []string{"8.8.8.8", "1.1.1.1", "2606:4700:4700::1111"} { + if !isSafeDownloadIP(net.ParseIP(raw)) { + t.Errorf("%s should be public", raw) + } + } +} + +func TestReadImageRejectsPixelLimit(t *testing.T) { + // A PNG only needs a valid signature+IHDR for DecodeConfig. Patch a valid + // tiny PNG's dimensions and let CRC become invalid: DecodeConfig must reject + // it or the pixel policy must reject it, either outcome is fail closed. + data := tinyPNG(t) + if len(data) < 24 { + t.Fatal("short png fixture") + } + copy(data[16:20], []byte{0, 0, 0x27, 0x10}) // 10000 + copy(data[20:24], []byte{0, 0, 0x13, 0x88}) // 5000 = 50M + if _, err := validateImageBytes(data); err == nil { + t.Fatal("50M-pixel image must fail") + } +} + +func TestBoundedAPIResponse(t *testing.T) { + resp := &http.Response{Body: io.NopCloser(io.LimitReader(strings.NewReader(strings.Repeat("x", maxAPIResponseBytes+1)), maxAPIResponseBytes+1))} + if _, err := readBoundedAPIResponse(resp); err == nil { + t.Fatal("oversized API response must fail") + } +} diff --git a/internal/channels/dingtalk/stream.go b/internal/channels/dingtalk/stream.go new file mode 100644 index 0000000..d69d922 --- /dev/null +++ b/internal/channels/dingtalk/stream.go @@ -0,0 +1,284 @@ +package dingtalk + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "strings" + + "github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot" + dtsdk "github.com/open-dingtalk/dingtalk-stream-sdk-go/client" +) + +// StreamConfig holds the DingTalk Stream mode credentials plus the tenant +// this ingress serves. An entirely empty config means the DingTalk channel +// is disabled; a partially filled config is a fatal misconfiguration. +type StreamConfig struct { + ClientID string + ClientSecret string + TenantKey string +} + +// Enabled reports whether all three values are present. Callers must run +// Validate first: Enabled on a partially filled config returns false. +func (c StreamConfig) Enabled() bool { + return strings.TrimSpace(c.ClientID) != "" && + strings.TrimSpace(c.ClientSecret) != "" && + strings.TrimSpace(c.TenantKey) != "" +} + +// Validate enforces the all-or-nothing rule: either every field is blank +// (channel disabled, no error) or every field is set (channel enabled). +// Any mix is an error so misconfigured deployments fail fast at startup. +func (c StreamConfig) Validate() error { + id := strings.TrimSpace(c.ClientID) != "" + secret := strings.TrimSpace(c.ClientSecret) != "" + tenant := strings.TrimSpace(c.TenantKey) != "" + if !id && !secret && !tenant { + return nil + } + if id && secret && tenant { + return nil + } + missing := make([]string, 0, 3) + if !id { + missing = append(missing, "client id") + } + if !secret { + missing = append(missing, "client secret") + } + if !tenant { + missing = append(missing, "tenant key") + } + return fmt.Errorf("dingtalk stream config is partial: missing %s", strings.Join(missing, ", ")) +} + +// BotCallbackFromSDK converts the official SDK callback model into the +// channel-internal BotCallback. Msgtype is normalized (trimmed, lowercased). +// For picture messages the downloadCode is extracted from the SDK Content +// field (json marshal/unmarshal round-trip); malformed or missing codes +// produce an empty DownloadCode but never downgrade the Msgtype. +// For richText messages the Content field is parsed for text+picture items; +// if a picture item is present Msgtype becomes "picture", otherwise text-only +// richText is promoted to "text". Malformed/empty richText keeps "richtext". +func BotCallbackFromSDK(data *chatbot.BotCallbackDataModel) (BotCallback, error) { + if data == nil { + return BotCallback{}, errors.New("dingtalk stream: callback data is nil") + } + msgtype := strings.ToLower(strings.TrimSpace(data.Msgtype)) + cb := BotCallback{ + ConversationType: data.ConversationType, + ConversationID: data.ConversationId, + Msgtype: msgtype, + MsgID: data.MsgId, + SenderNick: data.SenderNick, + SenderStaffID: data.SenderStaffId, + SenderID: data.SenderId, + IsInAtList: data.IsInAtList, + SessionWebhook: data.SessionWebhook, + Text: data.Text.Content, + } + if msgtype == "richtext" { + rtMsgtype, rtText, rtCode := parseRichText(data.Content) + cb.Msgtype = rtMsgtype + cb.Text = rtText + cb.DownloadCode = rtCode + return cb, nil + } + if msgtype == "picture" { + cb.DownloadCode = extractDownloadCode(data.Content) + } + return cb, nil +} + +// parseRichText parses the SDK Content field for richText messages. +// It handles three content shapes: +// 1. Direct object: {"richText": [...]} +// 2. Bare richText array (compatibility) +// 3. Nested envelope: {"content":{"richText":[...]}} +// +// Picture items are identified by a "type" field (case-insensitive trimmed) +// equal to "picture". The first non-empty downloadCode is used; subsequent +// picture items are ignored. Text items are joined with newlines. +// +// Returns: (msgtype, text, downloadCode). Msgtype is "picture" if any picture +// item exists, "text" if only text items, "richtext" if malformed/empty. +func parseRichText(raw interface{}) (string, string, string) { + items := extractRichTextItems(raw) + if len(items) == 0 { + return "richtext", "", "" + } + + var textParts []string + var downloadCode string + hasPicture := false + + for _, item := range items { + itemType := strings.ToLower(strings.TrimSpace(item.Type)) + + if itemType == "picture" { + hasPicture = true + if downloadCode == "" { + code := strings.TrimSpace(item.DownloadCode) + if code != "" { + downloadCode = code + } + } + continue + } + + txt := strings.TrimSpace(item.Text) + if txt != "" { + textParts = append(textParts, txt) + } + } + + text := strings.Join(textParts, "\n") + + if hasPicture { + return "picture", text, downloadCode + } + if text != "" { + return "text", text, "" + } + return "richtext", "", "" +} + +// richTextItem is a single item in a richText array. +type richTextItem struct { + Type string `json:"type"` + DownloadCode string `json:"downloadCode"` + Text string `json:"text"` +} + +// extractRichTextItems extracts the richText item array from the raw Content +// field. It handles direct object, bare-array, and nested-envelope shapes. +func extractRichTextItems(raw interface{}) []richTextItem { + if raw == nil { + return nil + } + b, err := json.Marshal(raw) + if err != nil { + return nil + } + + // Real DingTalk SDK shape: Content is already {"richText": [...]}. + var object struct { + RichText []richTextItem `json:"richText"` + } + if json.Unmarshal(b, &object) == nil && len(object.RichText) > 0 { + return object.RichText + } + + // Accept a bare array for transport compatibility. + var direct []richTextItem + if json.Unmarshal(b, &direct) == nil && len(direct) > 0 { + return direct + } + + // Accept a preserved callback envelope: {"content":{"richText":[...]}}. + var nested struct { + Content struct { + RichText []richTextItem `json:"richText"` + } `json:"content"` + } + if json.Unmarshal(b, &nested) == nil && len(nested.Content.RichText) > 0 { + return nested.Content.RichText + } + + return nil +} + +// extractDownloadCode attempts to pull content.downloadCode from the SDK +// Content field (interface{}) via a json marshal/unmarshal round-trip. It +// never panics on malformed input and returns an empty string for any +// extraction failure. +func extractDownloadCode(raw interface{}) string { + if raw == nil { + return "" + } + b, err := json.Marshal(raw) + if err != nil { + return "" + } + var direct struct { + DownloadCode string `json:"downloadCode"` + } + if json.Unmarshal(b, &direct) == nil && strings.TrimSpace(direct.DownloadCode) != "" { + return strings.TrimSpace(direct.DownloadCode) + } + // Keep compatibility with transports that preserve the callback envelope. + var nested struct { + Content struct { + DownloadCode string `json:"downloadCode"` + } `json:"content"` + } + if err := json.Unmarshal(b, &nested); err != nil { + return "" + } + return strings.TrimSpace(nested.Content.DownloadCode) +} + +// StreamClient wraps the official DingTalk Stream SDK client. It wires +// robot callbacks into the existing Handler synchronously (no goroutines, +// no queue). The SDK default auto-reconnect stays enabled while running; +// Close disables it so the connection stays closed. +type StreamClient struct { + inner *dtsdk.StreamClient +} + +// NewStreamClient validates the config, builds the SDK client with the app +// credential and registers the chatbot callback router. The handler is +// invoked synchronously from the SDK callback; its return value only +// acknowledges the frame, replies always go through the session webhook. +func NewStreamClient(config StreamConfig, handler *Handler) (*StreamClient, error) { + if err := config.Validate(); err != nil { + return nil, err + } + if !config.Enabled() { + return nil, errors.New("dingtalk stream: config is empty, client not created") + } + if handler == nil { + return nil, errors.New("dingtalk stream: handler is required") + } + cred := dtsdk.NewAppCredentialConfig(strings.TrimSpace(config.ClientID), strings.TrimSpace(config.ClientSecret)) + // Keep the SDK default AutoReconnect=true so runtime disconnects + // reconnect automatically; Close flips it off before closing. + inner := dtsdk.NewStreamClient( + dtsdk.WithAppCredential(cred), + ) + inner.RegisterChatBotCallbackRouter(func(ctx context.Context, data *chatbot.BotCallbackDataModel) ([]byte, error) { + cb, err := BotCallbackFromSDK(data) + if err != nil { + return nil, err + } + if err := handler.Handle(ctx, cb); err != nil { + return nil, err + } + return nil, nil + }) + return &StreamClient{inner: inner}, nil +} + +// Start connects to DingTalk Stream and spawns the SDK read loop. Any +// connection or credential error is propagated to the caller. +func (c *StreamClient) Start(ctx context.Context) error { + if c == nil || c.inner == nil { + return errors.New("dingtalk stream: client is nil") + } + if err := c.inner.Start(ctx); err != nil { + return fmt.Errorf("dingtalk stream start: %w", err) + } + return nil +} + +// Close tears down the websocket. Auto-reconnect is disabled before the +// underlying Close so the SDK processLoop cannot reconnect afterwards. +func (c *StreamClient) Close() { + if c == nil || c.inner == nil { + return + } + c.inner.AutoReconnect = false + c.inner.Close() +} diff --git a/internal/channels/dingtalk/stream_test.go b/internal/channels/dingtalk/stream_test.go new file mode 100644 index 0000000..2752a5f --- /dev/null +++ b/internal/channels/dingtalk/stream_test.go @@ -0,0 +1,588 @@ +package dingtalk + +import ( + "strings" + "testing" + + "github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot" +) + +func TestStreamConfigValidate(t *testing.T) { + t.Parallel() + tests := []struct { + name string + cfg StreamConfig + wantErr bool + wantOn bool + errSubstr string + }{ + { + name: "all empty is disabled without error", + cfg: StreamConfig{}, + wantErr: false, + wantOn: false, + }, + { + name: "whitespace only is disabled without error", + cfg: StreamConfig{ClientID: " ", ClientSecret: "\t", TenantKey: " "}, + wantErr: false, + wantOn: false, + }, + { + name: "client id only is an error", + cfg: StreamConfig{ClientID: "id"}, + wantErr: true, + errSubstr: "client secret", + }, + { + name: "client secret only is an error", + cfg: StreamConfig{ClientSecret: "secret"}, + wantErr: true, + errSubstr: "client id", + }, + { + name: "credentials without tenant key is an error", + cfg: StreamConfig{ClientID: "id", ClientSecret: "secret"}, + wantErr: true, + errSubstr: "tenant key", + }, + { + name: "tenant key without credentials is an error", + cfg: StreamConfig{TenantKey: "acme"}, + wantErr: true, + errSubstr: "client id", + }, + { + name: "all three present is enabled", + cfg: StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"}, + wantErr: false, + wantOn: true, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + err := tt.cfg.Validate() + if tt.wantErr && err == nil { + t.Fatalf("Validate() = nil, want error containing %q", tt.errSubstr) + } + if !tt.wantErr && err != nil { + t.Fatalf("Validate() = %v, want nil", err) + } + if tt.wantErr && tt.errSubstr != "" && !strings.Contains(err.Error(), tt.errSubstr) { + t.Fatalf("Validate() error = %q, want substring %q", err.Error(), tt.errSubstr) + } + if got := tt.cfg.Enabled(); got != tt.wantOn { + t.Fatalf("Enabled() = %v, want %v", got, tt.wantOn) + } + }) + } +} + +func TestBotCallbackFromSDK(t *testing.T) { + t.Parallel() + t.Run("nil data returns error", func(t *testing.T) { + t.Parallel() + if _, err := BotCallbackFromSDK(nil); err == nil { + t.Fatal("BotCallbackFromSDK(nil) = nil error, want error") + } + }) + + t.Run("maps every field one to one", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "2", + ConversationId: "cid123", + Msgtype: "text", + MsgId: "msg-9", + SenderNick: "Alice", + SenderStaffId: "staff-1", + SenderId: "sender-1", + IsInAtList: true, + SessionWebhook: "https://oapi.dingtalk.com/robot/sendBySession?session=abc", + Text: chatbot.BotCallbackDataTextModel{Content: " 你好 "}, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v, want nil", err) + } + want := BotCallback{ + ConversationType: "2", + ConversationID: "cid123", + Msgtype: "text", + MsgID: "msg-9", + SenderNick: "Alice", + SenderStaffID: "staff-1", + SenderID: "sender-1", + IsInAtList: true, + SessionWebhook: "https://oapi.dingtalk.com/robot/sendBySession?session=abc", + Text: " 你好 ", + } + if cb != want { + t.Fatalf("BotCallbackFromSDK() = %+v, want %+v", cb, want) + } + }) + + t.Run("text content is not trimmed by conversion", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "text", + MsgId: "m", + Text: chatbot.BotCallbackDataTextModel{Content: ""}, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Text != "" { + t.Fatalf("Text = %q, want empty", cb.Text) + } + }) +} + +func TestBotCallbackFromSDKPicture(t *testing.T) { + t.Parallel() + + t.Run("extracts downloadCode from picture content", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "picture", + MsgId: "msg-pic-1", + Content: map[string]interface{}{ + "downloadCode": " abc123 ", + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture", cb.Msgtype) + } + if cb.DownloadCode != "abc123" { + t.Fatalf("DownloadCode = %q, want trimmed abc123", cb.DownloadCode) + } + if cb.Text != "" { + t.Fatalf("Text = %q, want empty for picture", cb.Text) + } + }) + + t.Run("malformed content keeps picture with empty downloadCode", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "picture", + MsgId: "msg-pic-2", + Content: "not-a-map", + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture (never downgrade)", cb.Msgtype) + } + if cb.DownloadCode != "" { + t.Fatalf("DownloadCode = %q, want empty", cb.DownloadCode) + } + }) + + t.Run("missing downloadCode keeps picture", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "picture", + MsgId: "msg-pic-3", + Content: map[string]interface{}{ + "content": map[string]interface{}{}, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture", cb.Msgtype) + } + if cb.DownloadCode != "" { + t.Fatalf("DownloadCode = %q, want empty", cb.DownloadCode) + } + }) + + t.Run("text regression: Msgtype normalized", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: " TEXT ", + MsgId: "msg-t-1", + Text: chatbot.BotCallbackDataTextModel{Content: "hello"}, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "text" { + t.Fatalf("Msgtype = %q, want normalized text", cb.Msgtype) + } + if cb.Text != "hello" { + t.Fatalf("Text = %q, want hello", cb.Text) + } + }) + + t.Run("nil content keeps picture", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "picture", + MsgId: "msg-pic-4", + Content: nil, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture", cb.Msgtype) + } + if cb.DownloadCode != "" { + t.Fatalf("DownloadCode = %q, want empty", cb.DownloadCode) + } + }) +} + +func mustTestHandler(t *testing.T) *Handler { + t.Helper() + return newTestHandler(t, &fakeQA{}, &fakeReply{}) +} + +func TestBotCallbackFromSDKRichText(t *testing.T) { + t.Parallel() + + t.Run("direct Content richText text+picture", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-1", + Content: map[string]interface{}{ + "richText": []interface{}{ + map[string]interface{}{"text": " 问题描述 "}, + map[string]interface{}{"type": "picture", "downloadCode": " code-abc "}, + }, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture", cb.Msgtype) + } + if cb.DownloadCode != "code-abc" { + t.Fatalf("DownloadCode = %q, want code-abc", cb.DownloadCode) + } + if cb.Text != "问题描述" { + t.Fatalf("Text = %q, want 问题描述 (trimmed text segments joined)", cb.Text) + } + }) + + t.Run("nested content envelope richText", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-2", + Content: map[string]interface{}{ + "content": map[string]interface{}{ + "richText": []interface{}{ + map[string]interface{}{"text": "描述"}, + map[string]interface{}{"type": "picture", "downloadCode": "code-xyz"}, + }, + }, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture", cb.Msgtype) + } + if cb.DownloadCode != "code-xyz" { + t.Fatalf("DownloadCode = %q, want code-xyz", cb.DownloadCode) + } + if cb.Text != "描述" { + t.Fatalf("Text = %q, want 描述", cb.Text) + } + }) + + t.Run("multi text segments preserve order", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-3", + Content: []interface{}{ + map[string]interface{}{"text": "第一段"}, + map[string]interface{}{"text": " 第二段 "}, + map[string]interface{}{"type": "picture", "downloadCode": "code-multi"}, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture", cb.Msgtype) + } + if cb.Text != "第一段\n第二段" { + t.Fatalf("Text = %q, want joined with newline", cb.Text) + } + }) + + t.Run("first non-empty downloadCode used, subsequent ignored", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-4", + Content: []interface{}{ + map[string]interface{}{"type": "picture", "downloadCode": " code-first "}, + map[string]interface{}{"type": "picture", "downloadCode": "code-second"}, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.DownloadCode != "code-first" { + t.Fatalf("DownloadCode = %q, want first code-first", cb.DownloadCode) + } + }) + + t.Run("picture item missing downloadCode still picture", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-5", + Content: []interface{}{ + map[string]interface{}{"text": "看看这张图"}, + map[string]interface{}{"type": "picture"}, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture (never downgrade)", cb.Msgtype) + } + if cb.DownloadCode != "" { + t.Fatalf("DownloadCode = %q, want empty", cb.DownloadCode) + } + if cb.Text != "看看这张图" { + t.Fatalf("Text = %q, want 看看这张图", cb.Text) + } + }) + + t.Run("pure text richText becomes text", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-6", + Content: []interface{}{ + map[string]interface{}{"text": "你好"}, + map[string]interface{}{"text": " 世界 "}, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "text" { + t.Fatalf("Msgtype = %q, want text", cb.Msgtype) + } + if cb.Text != "你好\n世界" { + t.Fatalf("Text = %q, want joined text", cb.Text) + } + if cb.DownloadCode != "" { + t.Fatalf("DownloadCode = %q, want empty", cb.DownloadCode) + } + }) + + t.Run("malformed richText keeps richtext", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-7", + Content: "not-a-list", + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "richtext" { + t.Fatalf("Msgtype = %q, want richtext (malformed, keep original)", cb.Msgtype) + } + }) + + t.Run("empty richText keeps richtext", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-8", + Content: []interface{}{}, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "richtext" { + t.Fatalf("Msgtype = %q, want richtext (empty, keep original)", cb.Msgtype) + } + }) + + t.Run("richText with type case normalization", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: " RICHTEXT ", + MsgId: "msg-rt-9", + Content: []interface{}{ + map[string]interface{}{"text": "hello"}, + map[string]interface{}{"TYPE": " PICTURE ", "DOWNLOADCODE": "code-case"}, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture", cb.Msgtype) + } + if cb.DownloadCode != "code-case" { + t.Fatalf("DownloadCode = %q, want code-case", cb.DownloadCode) + } + if cb.Text != "hello" { + t.Fatalf("Text = %q, want hello", cb.Text) + } + }) + + t.Run("richText skips empty text segments", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "richText", + MsgId: "msg-rt-10", + Content: []interface{}{ + map[string]interface{}{"text": " "}, + map[string]interface{}{"text": "有效内容"}, + map[string]interface{}{"text": ""}, + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "text" { + t.Fatalf("Msgtype = %q, want text", cb.Msgtype) + } + if cb.Text != "有效内容" { + t.Fatalf("Text = %q, want 有效内容 (empty segments skipped)", cb.Text) + } + }) + + t.Run("text regression still works", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "text", + MsgId: "msg-t-2", + Text: chatbot.BotCallbackDataTextModel{Content: "hello"}, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "text" { + t.Fatalf("Msgtype = %q, want text", cb.Msgtype) + } + if cb.Text != "hello" { + t.Fatalf("Text = %q, want hello", cb.Text) + } + }) + + t.Run("picture regression still works", func(t *testing.T) { + t.Parallel() + data := &chatbot.BotCallbackDataModel{ + ConversationType: "1", + Msgtype: "picture", + MsgId: "msg-p-2", + Content: map[string]interface{}{ + "downloadCode": "regression-code", + }, + } + cb, err := BotCallbackFromSDK(data) + if err != nil { + t.Fatalf("BotCallbackFromSDK() error = %v", err) + } + if cb.Msgtype != "picture" { + t.Fatalf("Msgtype = %q, want picture", cb.Msgtype) + } + if cb.DownloadCode != "regression-code" { + t.Fatalf("DownloadCode = %q, want regression-code", cb.DownloadCode) + } + }) +} + +func TestNewStreamClientValidation(t *testing.T) { + t.Parallel() + h := mustTestHandler(t) + + t.Run("nil handler is an error", func(t *testing.T) { + t.Parallel() + cfg := StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"} + if _, err := NewStreamClient(cfg, nil); err == nil { + t.Fatal("NewStreamClient with nil handler = nil error, want error") + } + }) + + t.Run("invalid config is an error", func(t *testing.T) { + t.Parallel() + if _, err := NewStreamClient(StreamConfig{}, h); err == nil { + t.Fatal("NewStreamClient with empty config = nil error, want error") + } + }) + + t.Run("valid config keeps SDK auto reconnect on until Close disables it", func(t *testing.T) { + t.Parallel() + cfg := StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"} + sc, err := NewStreamClient(cfg, h) + if err != nil { + t.Fatalf("NewStreamClient() error = %v, want nil", err) + } + if sc == nil || sc.inner == nil { + t.Fatal("NewStreamClient() returned nil client") + } + // The SDK default is AutoReconnect=true; keep it so runtime + // disconnects reconnect automatically. + if !sc.inner.AutoReconnect { + t.Fatal("AutoReconnect = false after NewStreamClient, want true (SDK default must be kept)") + } + // Close must flip AutoReconnect off first so the SDK processLoop + // does not reconnect a closed connection. + sc.Close() + if sc.inner.AutoReconnect { + t.Fatal("AutoReconnect = true after Close, want false (Close must stay closed)") + } + // Closing again must remain a safe no-op. + sc.Close() + }) +} diff --git a/internal/channels/dingtalk/webhook.go b/internal/channels/dingtalk/webhook.go new file mode 100644 index 0000000..33d25bb --- /dev/null +++ b/internal/channels/dingtalk/webhook.go @@ -0,0 +1,174 @@ +package dingtalk + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/url" + "strings" + "time" +) + +const ( + // maxWebhookBodyBytes caps both the outgoing request body and the + // response body we are willing to read back from the session webhook. + maxWebhookBodyBytes = 1 << 20 // 1 MiB + defaultHTTPTimeout = 15 * time.Second +) + +// WebhookSender posts replies to a DingTalk session webhook. It fails +// closed: only https URLs on port 443 without userinfo whose hostname is +// dingtalk.com or a subdomain are accepted, redirects are refused, and any +// non-2xx response is an error. This prevents answers from leaking to +// arbitrary public hosts (SSRF). +type WebhookSender struct { + client *http.Client + extraHosts map[string]bool // test-only additional allowed hostnames + validateURL func(string) error +} + +// NewWebhookSender builds a sender. A nil client installs a hardened default +// (no redirects, sane timeout). +func NewWebhookSender(client *http.Client) (*WebhookSender, error) { + return newWebhookSender(client, nil) +} + +func newWebhookSender(client *http.Client, extraHosts map[string]bool) (*WebhookSender, error) { + if client == nil { + client = &http.Client{Timeout: defaultHTTPTimeout} + } + wrapped := *client + wrapped.CheckRedirect = func(req *http.Request, via []*http.Request) error { + return errors.New("dingtalk webhook redirects are not allowed") + } + sender := &WebhookSender{client: &wrapped, extraHosts: extraHosts} + sender.validateURL = func(raw string) error { + return sender.validate(raw) + } + return sender, nil +} + +// newWebhookSenderForTest is used by tests to allow loopback TLS servers +// (any port) while exercising the exact same send/validation code path. +func newWebhookSenderForTest(client *http.Client, hosts ...string) (*WebhookSender, error) { + extra := make(map[string]bool, len(hosts)) + for _, h := range hosts { + extra[strings.ToLower(h)] = true + } + sender, err := newWebhookSender(client, extra) + if err != nil { + return nil, err + } + base := sender.validateURL + sender.validateURL = func(raw string) error { + if err := base(raw); err == nil { + return nil + } else if !strings.Contains(err.Error(), "default port 443") { + return err + } + // Allow non-443 ports only for explicitly allow-listed test hosts. + u, parseErr := url.Parse(raw) + if parseErr != nil { + return parseErr + } + if u.Scheme != "https" || u.User != nil || !extra[strings.ToLower(u.Hostname())] { + return fmt.Errorf("dingtalk webhook URL must use default port 443, got %q", u.Port()) + } + return nil + } + return sender, nil +} + +// ValidateWebhookURL enforces the production webhook URL policy. +func ValidateWebhookURL(raw string) error { + return (&WebhookSender{}).validate(raw) +} + +func (s *WebhookSender) validate(raw string) error { + if strings.TrimSpace(raw) == "" { + return errors.New("dingtalk webhook URL is empty") + } + u, err := url.Parse(raw) + if err != nil { + return fmt.Errorf("dingtalk webhook URL parse: %w", err) + } + if u.Scheme != "https" { + return fmt.Errorf("dingtalk webhook URL must be https, got %q", u.Scheme) + } + if u.User != nil { + return errors.New("dingtalk webhook URL must not contain userinfo") + } + host := u.Hostname() + if host == "" { + return errors.New("dingtalk webhook URL must include a host") + } + port := u.Port() + if port != "" && port != "443" { + return fmt.Errorf("dingtalk webhook URL must use default port 443, got %q", port) + } + if !s.hostAllowed(host) { + return fmt.Errorf("dingtalk webhook host %q is not allowed", host) + } + return nil +} + +func (s *WebhookSender) hostAllowed(host string) bool { + host = strings.ToLower(host) + if ip := net.ParseIP(host); ip != nil { + return false + } + if host == "dingtalk.com" || strings.HasSuffix(host, ".dingtalk.com") { + return true + } + return s.extraHosts[host] +} + +// SendMarkdown posts a markdown message to the session webhook. +func (s *WebhookSender) SendMarkdown(ctx context.Context, webhookURL, title, text string) error { + if err := s.validateURL(webhookURL); err != nil { + return err + } + if title == "" { + title = "DobotAI" + } + body, err := json.Marshal(map[string]any{ + "msgtype": "markdown", + "markdown": map[string]string{ + "title": title, + "text": text, + }, + }) + if err != nil { + return fmt.Errorf("marshal dingtalk webhook payload: %w", err) + } + if len(body) > maxWebhookBodyBytes { + return errors.New("dingtalk webhook payload exceeds 1MiB") + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, webhookURL, bytes.NewReader(body)) + if err != nil { + return fmt.Errorf("build dingtalk webhook request: %w", err) + } + req.Header.Set("Content-Type", "application/json") + resp, err := s.client.Do(req) + if err != nil { + return fmt.Errorf("send dingtalk webhook: %w", err) + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, maxWebhookBodyBytes)) + return fmt.Errorf("dingtalk webhook returned status %d", resp.StatusCode) + } + n, err := io.Copy(io.Discard, io.LimitReader(resp.Body, maxWebhookBodyBytes+1)) + if err != nil { + return fmt.Errorf("read dingtalk webhook response: %w", err) + } + if n > maxWebhookBodyBytes { + return fmt.Errorf("dingtalk webhook response exceeds %d bytes", maxWebhookBodyBytes) + } + return nil +} diff --git a/internal/channels/dingtalk/webhook_test.go b/internal/channels/dingtalk/webhook_test.go new file mode 100644 index 0000000..16e1f95 --- /dev/null +++ b/internal/channels/dingtalk/webhook_test.go @@ -0,0 +1,171 @@ +package dingtalk + +import ( + "context" + "crypto/tls" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" +) + +func newTestSender(t *testing.T, client *http.Client) *WebhookSender { + t.Helper() + sender, err := NewWebhookSender(client) + if err != nil { + t.Fatalf("NewWebhookSender: %v", err) + } + return sender +} + +func TestWebhookSenderRejectsInvalidURLs(t *testing.T) { + sender := newTestSender(t, http.DefaultClient) + cases := map[string]string{ + "http not https": "http://oapi.dingtalk.com/robot/send?access_token=x", + "userinfo": "https://user:pass@oapi.dingtalk.com/robot/send?access_token=x", + "arbitrary public host": "https://evil.example.com/robot/send", + "non-default port": "https://oapi.dingtalk.com:8443/robot/send", + "empty url": "", + "no host": "https:///robot/send", + } + for name, raw := range cases { + t.Run(name, func(t *testing.T) { + if err := sender.SendMarkdown(context.Background(), raw, "t", "hello"); err == nil { + t.Fatalf("expected rejection for %q", raw) + } + }) + } +} + +func TestWebhookSenderRejectsRedirect(t *testing.T) { + var finalURL string + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + finalURL = r.URL.String() + w.WriteHeader(http.StatusOK) + })) + defer server.Close() + + parsed, err := url.Parse(server.URL) + if err != nil { + t.Fatal(err) + } + redirector := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, parsed.String(), http.StatusFound) + })) + defer redirector.Close() + + client := redirector.Client() + // TLS test servers run on 127.0.0.1, so allow the loopback host in this test only. + sender, err := newWebhookSenderForTest(client, "127.0.0.1") + if err != nil { + t.Fatalf("newWebhookSenderForTest: %v", err) + } + err = sender.SendMarkdown(context.Background(), redirector.URL+"/robot/send", "t", "hello") + if err == nil { + t.Fatalf("expected redirect to be rejected, final request reached %q", finalURL) + } + if !strings.Contains(err.Error(), "redirect") { + t.Fatalf("expected redirect error, got %v", err) + } +} + +func TestWebhookSenderRejectsNon2xx(t *testing.T) { + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + })) + defer server.Close() + sender, err := newWebhookSenderForTest(server.Client(), "127.0.0.1") + if err != nil { + t.Fatalf("newWebhookSenderForTest: %v", err) + } + if err := sender.SendMarkdown(context.Background(), server.URL, "t", "hello"); err == nil { + t.Fatal("expected non-2xx error") + } +} + +func TestWebhookSenderAcceptsHTTPSDingTalkAndSendsMarkdown(t *testing.T) { + var body []byte + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.TLS == nil { + t.Error("request must use TLS") + } + var readErr error + body, readErr = io.ReadAll(r.Body) + if readErr != nil { + t.Errorf("read body: %v", readErr) + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"errcode":0}`)) + })) + server.TLS = &tls.Config{} + server.StartTLS() + defer server.Close() + + // Test-only variant that accepts the loopback host but keeps the same + // markdown payload code path as the production oapi.dingtalk.com sender. + sender, err := newWebhookSenderForTest(server.Client(), "127.0.0.1") + if err != nil { + t.Fatalf("newWebhookSenderForTest: %v", err) + } + if err := sender.SendMarkdown(context.Background(), server.URL+"/robot/send?access_token=x", "回答", "**你好**"); err != nil { + t.Fatalf("SendMarkdown: %v", err) + } + + var payload struct { + Msgtype string `json:"msgtype"` + Markdown struct { + Title string `json:"title"` + Text string `json:"text"` + } `json:"markdown"` + } + if err := json.Unmarshal(body, &payload); err != nil { + t.Fatalf("payload is not JSON: %v", err) + } + if payload.Msgtype != "markdown" { + t.Fatalf("msgtype = %q, want markdown", payload.Msgtype) + } + if payload.Markdown.Text != "**你好**" || payload.Markdown.Title == "" { + t.Fatalf("unexpected markdown payload: %+v", payload.Markdown) + } +} + +func TestWebhookSenderRejectsOversizedResponse(t *testing.T) { + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + // Stream more than 1MiB so the sender must fail rather than + // silently truncate. + chunk := make([]byte, 64*1024) + for i := 0; i < (maxWebhookBodyBytes/len(chunk))+1; i++ { + _, _ = w.Write(chunk) + } + })) + defer server.Close() + sender, err := newWebhookSenderForTest(server.Client(), "127.0.0.1") + if err != nil { + t.Fatalf("newWebhookSenderForTest: %v", err) + } + err = sender.SendMarkdown(context.Background(), server.URL, "t", "hello") + if err == nil { + t.Fatal("expected error for response body larger than 1MiB, got silent truncation") + } + if !strings.Contains(err.Error(), "exceeds") { + t.Fatalf("expected size-limit error, got %v", err) + } +} + +// TestWebhookSenderValidateWebhookURLAcceptsDingTalkHosts asserts the pure +// URL policy accepts oapi.dingtalk.com and *.dingtalk.com over https/443. +func TestWebhookSenderValidateWebhookURLAcceptsDingTalkHosts(t *testing.T) { + for _, raw := range []string{ + "https://oapi.dingtalk.com/robot/send?access_token=x", + "https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend", + } { + if err := ValidateWebhookURL(raw); err != nil { + t.Fatalf("ValidateWebhookURL(%q): %v", raw, err) + } + } +} diff --git a/internal/core/types.go b/internal/core/types.go index 4653878..105f1a3 100644 --- a/internal/core/types.go +++ b/internal/core/types.go @@ -15,6 +15,27 @@ const ( var ErrTurnInProgress = errors.New("turn is already processing") +// ImageInput contains the image data for a QA request. The DataURI is a +// full data: URI (e.g. "data:image/png;base64,...") and must never be +// persisted to the audit log. +type ImageInput struct { + DataURI string + MIMEType string + SizeBytes int64 + Width int + Height int +} + +// MediaAudit records metadata about media in a turn for audit purposes. +// It must never contain DataURI, download codes, or URLs. +type MediaAudit struct { + Type string `json:"type"` // "image" + MIMEType string `json:"mime_type"` + SizeBytes int64 `json:"size_bytes"` + Width int `json:"width"` + Height int `json:"height"` +} + type QARequest struct { TenantKey string Channel string @@ -24,6 +45,7 @@ type QARequest struct { ExternalMessageID string SenderName string Query string + Images []ImageInput } type Reference struct { @@ -65,6 +87,7 @@ type Turn struct { Question string Answer string References []Reference + Media []MediaAudit Status string ErrorMessage string } diff --git a/internal/qa/ask_events_test.go b/internal/qa/ask_events_test.go new file mode 100644 index 0000000..04cdf6b --- /dev/null +++ b/internal/qa/ask_events_test.go @@ -0,0 +1,254 @@ +package qa + +import ( + "context" + "errors" + "testing" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/session" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora" +) + +type fakeEventStreamingWeKnora struct { + *fakeWeKnora + events []weknora.StreamEvent + streamResult weknora.ChatResult + streamErr error + streamCalls int +} + +func (f *fakeEventStreamingWeKnora) AgentChatEvents(_ context.Context, _ string, request weknora.AgentChatRequest, observer weknora.StreamObserver) (weknora.ChatResult, error) { + f.streamCalls++ + f.mu.Lock() + f.queries = append(f.queries, request.Query) + f.requests = append(f.requests, request) + f.mu.Unlock() + for _, e := range f.events { + if observer != nil { + observer(e) + } + } + return f.streamResult, f.streamErr +} + +func TestAskEventsStreamFakeSequencePassesThroughAndSuccess(t *testing.T) { + repo := newMemoryRepository() + client := &fakeEventStreamingWeKnora{ + fakeWeKnora: &fakeWeKnora{}, + events: []weknora.StreamEvent{ + {ResponseType: "thinking"}, + {ResponseType: "tool_call", ToolName: "query_understand"}, + {ResponseType: "tool_result", ToolName: "query_understand"}, + {ResponseType: "tool_call", ToolName: "knowledge_search"}, + {ResponseType: "references", ReferenceCount: 3}, + {ResponseType: "answer", FullAnswer: "hello ", Done: false}, + {ResponseType: "answer", FullAnswer: "hello world", Done: true}, + }, + streamResult: weknora.ChatResult{RequestID: "request-1", Answer: "hello world", References: []core.Reference{{KnowledgeID: "kb-1"}}}, + } + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + var observed []weknora.StreamEvent + req := streamQARequest("message-events") + result, err := service.AskEvents(context.Background(), req, func(e weknora.StreamEvent) { + observed = append(observed, e) + }) + if err != nil { + t.Fatalf("AskEvents: %v", err) + } + if result.Replayed { + t.Fatal("should not be replay") + } + if result.Answer != "hello world" { + t.Fatalf("expected 'hello world', got %q", result.Answer) + } + if client.streamCalls != 1 { + t.Fatalf("expected 1 stream call, got %d", client.streamCalls) + } + if len(observed) != 7 { + t.Fatalf("expected 7 events, got %d: %+v", len(observed), observed) + } + // Verify sequence + expectedTypes := []string{"thinking", "tool_call", "tool_result", "tool_call", "references", "answer", "answer"} + for i, want := range expectedTypes { + if observed[i].ResponseType != want { + t.Fatalf("event[%d] ResponseType: want %q, got %q", i, want, observed[i].ResponseType) + } + } + // Verify turn persisted + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-events"] + repo.mu.Unlock() + if stored.Status != "succeeded" || stored.Answer != "hello world" { + t.Fatalf("stored=%+v", stored) + } +} + +func TestAskEventsEventStreamErrorTurnFailed(t *testing.T) { + repo := newMemoryRepository() + client := &fakeEventStreamingWeKnora{ + fakeWeKnora: &fakeWeKnora{}, + events: []weknora.StreamEvent{ + {ResponseType: "answer", FullAnswer: "partial"}, + {ResponseType: "error", Done: true}, + }, + streamErr: errors.New("stream failed"), + } + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + var observed []weknora.StreamEvent + _, err = service.AskEvents(context.Background(), streamQARequest("message-events-error"), func(e weknora.StreamEvent) { + observed = append(observed, e) + }) + if err == nil { + t.Fatal("expected error") + } + if len(observed) != 2 { + t.Fatalf("expected 2 events before error, got %d: %+v", len(observed), observed) + } + // Turn must be failed + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-events-error"] + repo.mu.Unlock() + if stored.Status != "failed" || stored.Answer != "" { + t.Fatalf("expected failed turn without answer, got %+v", stored) + } +} + +func TestAskEventsLegacyFallbackSingleAnswerEvent(t *testing.T) { + repo := newMemoryRepository() + // fakeWeKnora only implements AgentChat (no AgentChatStream, no AgentChatEvents) + client := &fakeWeKnora{} + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + var observed []weknora.StreamEvent + req := streamQARequest("message-legacy-events") + result, err := service.AskEvents(context.Background(), req, func(e weknora.StreamEvent) { + observed = append(observed, e) + }) + if err != nil { + t.Fatalf("AskEvents legacy: %v", err) + } + if result.Answer != "answer" { + t.Fatalf("expected 'answer', got %q", result.Answer) + } + // Legacy fallback: single final answer event + if len(observed) != 1 { + t.Fatalf("expected 1 legacy event, got %d: %+v", len(observed), observed) + } + if observed[0].ResponseType != "answer" || observed[0].FullAnswer != "answer" || !observed[0].Done { + t.Fatalf("legacy event should be answer done: %+v", observed[0]) + } +} + +func TestAskEventsDuplicateReplayZeroEvents(t *testing.T) { + repo := newMemoryRepository() + client := &fakeEventStreamingWeKnora{ + fakeWeKnora: &fakeWeKnora{}, + events: []weknora.StreamEvent{ + {ResponseType: "answer", FullAnswer: "answer", Done: true}, + }, + streamResult: weknora.ChatResult{RequestID: "request-1", Answer: "answer"}, + } + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + req := streamQARequest("message-events-replay") + // First call + firstEvents := 0 + if _, err := service.AskEvents(context.Background(), req, func(weknora.StreamEvent) { firstEvents++ }); err != nil { + t.Fatalf("first AskEvents: %v", err) + } + // Second call — replay + secondEvents := 0 + result, err := service.AskEvents(context.Background(), req, func(weknora.StreamEvent) { secondEvents++ }) + if err != nil { + t.Fatalf("replay AskEvents: %v", err) + } + if !result.Replayed { + t.Fatal("expected replay") + } + if secondEvents != 0 { + t.Fatalf("replay must not notify events, got %d", secondEvents) + } + if firstEvents != 1 { + t.Fatalf("first call expected 1 event, got %d", firstEvents) + } + if client.streamCalls != 1 { + t.Fatalf("expected 1 weknora call, got %d", client.streamCalls) + } +} + +func TestAskEventsStreamingFallbackFromAgentChatStream(t *testing.T) { + repo := newMemoryRepository() + // Client has AgentChatStream but not AgentChatEvents + client := &fakeStreamingWeKnora{ + fakeWeKnora: &fakeWeKnora{}, + updates: []string{"hello ", "hello world"}, + streamResult: weknora.ChatResult{RequestID: "request-1", Answer: "hello world", References: []core.Reference{{KnowledgeID: "kb-1"}}}, + } + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + var observed []weknora.StreamEvent + req := streamQARequest("message-stream-fallback") + result, err := service.AskEvents(context.Background(), req, func(e weknora.StreamEvent) { + observed = append(observed, e) + }) + if err != nil { + t.Fatalf("AskEvents stream fallback: %v", err) + } + if result.Answer != "hello world" { + t.Fatalf("expected 'hello world', got %q", result.Answer) + } + // Each answer observer callback becomes a StreamEvent with FullAnswer + if len(observed) != 2 { + t.Fatalf("expected 2 events from stream fallback, got %d: %+v", len(observed), observed) + } + if observed[0].ResponseType != "answer" || observed[0].FullAnswer != "hello " || observed[0].Done { + t.Fatalf("event[0]: %+v", observed[0]) + } + if observed[1].ResponseType != "answer" || observed[1].FullAnswer != "hello world" || observed[1].Done { + t.Fatalf("event[1]: %+v", observed[1]) + } + // Turn persisted + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-stream-fallback"] + repo.mu.Unlock() + if stored.Status != "succeeded" || stored.Answer != "hello world" { + t.Fatalf("stored=%+v", stored) + } +} + +// TestAskStreamOldRegression verifies the old AskStream still works after refactoring. +func TestAskStreamOldRegression(t *testing.T) { + repo := newMemoryRepository() + client := &fakeStreamingWeKnora{ + fakeWeKnora: &fakeWeKnora{}, updates: []string{"hello ", "hello world"}, + streamResult: weknora.ChatResult{RequestID: "request-stream", Answer: "hello world", References: []core.Reference{{KnowledgeID: "kb-1"}}}, + } + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + var observed []string + result, err := service.AskStream(context.Background(), streamQARequest("message-stream-regression"), func(full string) { observed = append(observed, full) }) + if err != nil { + t.Fatalf("AskStream: %v", err) + } + if len(observed) != 2 || observed[0] != "hello " || observed[1] != "hello world" { + t.Fatalf("observed=%v", observed) + } + if result.Answer != "hello world" || result.WeKnoraRequestID != "request-stream" || client.streamCalls != 1 { + t.Fatalf("result=%+v streamCalls=%d", result, client.streamCalls) + } +} diff --git a/internal/qa/service.go b/internal/qa/service.go index d482cf3..706b18f 100644 --- a/internal/qa/service.go +++ b/internal/qa/service.go @@ -22,6 +22,14 @@ type WeKnora interface { AgentChat(ctx context.Context, sessionID string, request weknora.AgentChatRequest) (weknora.ChatResult, error) } +type streamingWeKnora interface { + AgentChatStream(ctx context.Context, sessionID string, request weknora.AgentChatRequest, observer weknora.AnswerObserver) (weknora.ChatResult, error) +} + +type eventStreamingWeKnora interface { + AgentChatEvents(ctx context.Context, sessionID string, request weknora.AgentChatRequest, observer weknora.StreamObserver) (weknora.ChatResult, error) +} + type Service struct { repository core.Repository agents AgentResolver @@ -40,6 +48,18 @@ func New(repository core.Repository, agents AgentResolver, client WeKnora, group } func (s *Service) Ask(ctx context.Context, request core.QARequest) (core.QAResult, error) { + return s.ask(ctx, request, nil, nil) +} + +func (s *Service) AskStream(ctx context.Context, request core.QARequest, observer weknora.AnswerObserver) (core.QAResult, error) { + return s.ask(ctx, request, observer, nil) +} + +func (s *Service) AskEvents(ctx context.Context, request core.QARequest, observer weknora.StreamObserver) (core.QAResult, error) { + return s.ask(ctx, request, nil, observer) +} + +func (s *Service) ask(ctx context.Context, request core.QARequest, answerObserver weknora.AnswerObserver, eventObserver weknora.StreamObserver) (core.QAResult, error) { if err := validateRequest(request); err != nil { return core.QAResult{}, err } @@ -56,9 +76,16 @@ func (s *Service) Ask(ctx context.Context, request core.QARequest) (core.QAResul return core.QAResult{}, err } + media := make([]core.MediaAudit, 0, len(request.Images)) + for _, image := range request.Images { + media = append(media, core.MediaAudit{ + Type: "image", MIMEType: image.MIMEType, SizeBytes: image.SizeBytes, + Width: image.Width, Height: image.Height, + }) + } turn := core.Turn{ID: newID(), TenantKey: request.TenantKey, Channel: request.Channel, ExternalMessageID: request.ExternalMessageID, SessionKey: sessionKey, - Question: request.Query, Status: "processing"} + Question: request.Query, Media: media, Status: "processing"} stored, created, err := s.repository.BeginTurn(ctx, turn) if err != nil { return core.QAResult{}, fmt.Errorf("begin turn: %w", err) @@ -91,9 +118,58 @@ func (s *Service) Ask(ctx context.Context, request core.QARequest) (core.QAResul if request.ConversationType == core.ConversationGroup && strings.TrimSpace(request.SenderName) != "" { query = request.SenderName + ": " + request.Query } - chat, err := s.weknora.AgentChat(ctx, sessionID, weknora.AgentChatRequest{ + images := make([]weknora.Image, 0, len(request.Images)) + for _, image := range request.Images { + images = append(images, weknora.Image{Data: image.DataURI}) + } + chatRequest := weknora.AgentChatRequest{ Query: query, AgentID: agentID, AgentEnabled: true, Channel: "im", EnableMemory: true, - }) + Images: images, + } + var chat weknora.ChatResult + if eventObserver != nil { + // Prefer AgentChatEvents; fallback to AgentChatStream adaptation; then AgentChat. + if eventStream, ok := s.weknora.(eventStreamingWeKnora); ok { + chat, err = eventStream.AgentChatEvents(ctx, sessionID, chatRequest, eventObserver) + } else if streaming, ok := s.weknora.(streamingWeKnora); ok { + // Adapt AgentChatStream answer chunks to StreamEvent + chat, err = streaming.AgentChatStream(ctx, sessionID, chatRequest, func(full string) { + if answerObserver != nil { + answerObserver(full) + } + eventObserver(weknora.StreamEvent{ + ResponseType: "answer", + FullAnswer: full, + }) + }) + } else { + chat, err = s.weknora.AgentChat(ctx, sessionID, chatRequest) + if err == nil && strings.TrimSpace(chat.Answer) != "" { + if answerObserver != nil { + answerObserver(chat.Answer) + } + eventObserver(weknora.StreamEvent{ + ResponseType: "answer", + FullAnswer: chat.Answer, + Done: true, + }) + } + } + } else if answerObserver != nil { + if streaming, ok := s.weknora.(streamingWeKnora); ok { + chat, err = streaming.AgentChatStream(ctx, sessionID, chatRequest, answerObserver) + } else { + chat, err = s.weknora.AgentChat(ctx, sessionID, chatRequest) + if err == nil && strings.TrimSpace(chat.Answer) != "" { + answerObserver(chat.Answer) + } + } + } else { + chat, err = s.weknora.AgentChat(ctx, sessionID, chatRequest) + } + if err == nil && strings.TrimSpace(chat.Answer) == "" { + err = weknora.ErrEmptyAnswer + } if err != nil { turn.Status, turn.ErrorMessage = "failed", err.Error() _ = s.repository.FinishTurn(ctx, turn) diff --git a/internal/qa/service_test.go b/internal/qa/service_test.go index 63ef6a7..e53ff79 100644 --- a/internal/qa/service_test.go +++ b/internal/qa/service_test.go @@ -2,6 +2,7 @@ package qa import ( "context" + "errors" "sync" "testing" @@ -63,6 +64,7 @@ type fakeWeKnora struct { mu sync.Mutex sessions int queries []string + requests []weknora.AgentChatRequest } func (f *fakeWeKnora) CreateSession(context.Context, string, string) (string, error) { @@ -75,9 +77,32 @@ func (f *fakeWeKnora) AgentChat(_ context.Context, sessionID string, request wek f.mu.Lock() defer f.mu.Unlock() f.queries = append(f.queries, request.Query) + f.requests = append(f.requests, request) return weknora.ChatResult{RequestID: "request-1", Answer: "answer", References: []core.Reference{{KnowledgeID: "kb-1"}}}, nil } +type fakeStreamingWeKnora struct { + *fakeWeKnora + updates []string + streamResult weknora.ChatResult + streamErr error + streamCalls int +} + +func (f *fakeStreamingWeKnora) AgentChatStream(_ context.Context, _ string, request weknora.AgentChatRequest, observer weknora.AnswerObserver) (weknora.ChatResult, error) { + f.streamCalls++ + f.mu.Lock() + f.queries = append(f.queries, request.Query) + f.requests = append(f.requests, request) + f.mu.Unlock() + for _, update := range f.updates { + if observer != nil { + observer(update) + } + } + return f.streamResult, f.streamErr +} + func TestAskUsesSharedGroupSessionAndReplaysDuplicate(t *testing.T) { repo := newMemoryRepository() client := &fakeWeKnora{} @@ -102,4 +127,237 @@ func TestAskUsesSharedGroupSessionAndReplaysDuplicate(t *testing.T) { if client.sessions != 1 || len(client.queries) != 1 || client.queries[0] != "Alice: question" { t.Fatalf("sessions=%d queries=%v", client.sessions, client.queries) } + // sender prefix must not regress + if len(client.requests) != 1 { + t.Fatalf("expected 1 weknora request, got %d", len(client.requests)) + } + if client.requests[0].Query != "Alice: question" { + t.Fatalf("sender prefix regression: got %q", client.requests[0].Query) + } + // Turn.Media must be empty for no-image request + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-1"] + repo.mu.Unlock() + if len(stored.Media) != 0 { + t.Fatalf("expected empty Media, got %+v", stored.Media) + } +} + +func TestAskPropagatesImagesToWeKnora(t *testing.T) { + repo := newMemoryRepository() + client := &fakeWeKnora{} + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + req := core.QARequest{ + TenantKey: "tenant", Channel: "dingtalk", ConversationType: core.ConversationGroup, + ExternalChatID: "group-1", ExternalUserID: "user-1", + ExternalMessageID: "message-img", SenderName: "Alice", Query: "describe this", + Images: []core.ImageInput{ + {DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 1024, Width: 640, Height: 480}, + }, + } + result, err := service.Ask(context.Background(), req) + if err != nil { + t.Fatalf("Ask: %v", err) + } + if result.Answer != "answer" { + t.Fatalf("unexpected answer: %s", result.Answer) + } + client.mu.Lock() + defer client.mu.Unlock() + if len(client.requests) != 1 { + t.Fatalf("expected 1 weknora request, got %d", len(client.requests)) + } + chatReq := client.requests[0] + if len(chatReq.Images) != 1 { + t.Fatalf("expected 1 image in weknora request, got %d", len(chatReq.Images)) + } + if chatReq.Images[0].Data != "data:image/png;base64,AAAA" { + t.Fatalf("image Data not propagated: %q", chatReq.Images[0].Data) + } + // sender prefix must still work + if chatReq.Query != "Alice: describe this" { + t.Fatalf("sender prefix regression: %q", chatReq.Query) + } +} + +func TestAskDerivesTurnMediaFromImages(t *testing.T) { + repo := newMemoryRepository() + client := &fakeWeKnora{} + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + req := core.QARequest{ + TenantKey: "tenant", Channel: "dingtalk", ConversationType: core.ConversationGroup, + ExternalChatID: "group-1", ExternalUserID: "user-1", + ExternalMessageID: "message-media", SenderName: "Alice", Query: "q", + Images: []core.ImageInput{ + {DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 2048, Width: 640, Height: 480}, + }, + } + _, err = service.Ask(context.Background(), req) + if err != nil { + t.Fatalf("Ask: %v", err) + } + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-media"] + repo.mu.Unlock() + if len(stored.Media) != 1 { + t.Fatalf("expected 1 MediaAudit, got %d", len(stored.Media)) + } + m := stored.Media[0] + if m.Type != "image" || m.MIMEType != "image/png" || m.SizeBytes != 2048 || m.Width != 640 || m.Height != 480 { + t.Fatalf("MediaAudit metadata mismatch: %+v", m) + } + // DataURI must NOT be in Turn.Media + if m.Type == "data:image/png;base64,AAAA" { + t.Fatal("MediaAudit must not contain DataURI") + } +} + +func TestAskDuplicateReplayDoesNotCallWeKnoraAgain(t *testing.T) { + repo := newMemoryRepository() + client := &fakeWeKnora{} + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + req := core.QARequest{ + TenantKey: "tenant", Channel: "dingtalk", ConversationType: core.ConversationGroup, + ExternalChatID: "group-1", ExternalUserID: "user-1", + ExternalMessageID: "message-replay", SenderName: "Alice", Query: "q", + Images: []core.ImageInput{ + {DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 1024, Width: 640, Height: 480}, + }, + } + first, err := service.Ask(context.Background(), req) + if err != nil { + t.Fatalf("first Ask: %v", err) + } + second, err := service.Ask(context.Background(), req) + if err != nil { + t.Fatalf("replay Ask: %v", err) + } + if !second.Replayed { + t.Fatal("second should be replayed") + } + if first.Answer != second.Answer { + t.Fatal("replay must return same answer") + } + client.mu.Lock() + defer client.mu.Unlock() + // WeKnora must be called exactly once + if len(client.requests) != 1 { + t.Fatalf("duplicate must not call WeKnora again; got %d requests", len(client.requests)) + } + // Turn.Media must be preserved on replay + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-replay"] + repo.mu.Unlock() + if len(stored.Media) != 1 { + t.Fatalf("replay must preserve Turn.Media: %+v", stored.Media) + } +} + +func streamQARequest(messageID string) core.QARequest { + return core.QARequest{ + TenantKey: "tenant", Channel: "dingtalk", ConversationType: core.ConversationDirect, + ExternalUserID: "user-1", ExternalMessageID: messageID, Query: "question", + } +} + +func TestAskStreamUsesStreamingObserverAndPersistsFinal(t *testing.T) { + repo := newMemoryRepository() + client := &fakeStreamingWeKnora{ + fakeWeKnora: &fakeWeKnora{}, updates: []string{"hello ", "hello world"}, + streamResult: weknora.ChatResult{RequestID: "request-stream", Answer: "hello world", References: []core.Reference{{KnowledgeID: "kb-1"}}}, + } + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + var observed []string + result, err := service.AskStream(context.Background(), streamQARequest("message-stream"), func(full string) { observed = append(observed, full) }) + if err != nil { + t.Fatalf("AskStream: %v", err) + } + if len(observed) != 2 || observed[0] != "hello " || observed[1] != "hello world" { + t.Fatalf("observed=%v", observed) + } + if result.Answer != "hello world" || result.WeKnoraRequestID != "request-stream" || client.streamCalls != 1 { + t.Fatalf("result=%+v streamCalls=%d", result, client.streamCalls) + } + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-stream"] + repo.mu.Unlock() + if stored.Status != "succeeded" || stored.Answer != "hello world" { + t.Fatalf("stored=%+v", stored) + } +} + +func TestAskStreamFailurePersistsFailedWithoutPartialAnswer(t *testing.T) { + repo := newMemoryRepository() + client := &fakeStreamingWeKnora{fakeWeKnora: &fakeWeKnora{}, updates: []string{"partial"}, streamErr: errors.New("stream failed")} + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + var observed []string + _, err = service.AskStream(context.Background(), streamQARequest("message-stream-error"), func(full string) { observed = append(observed, full) }) + if err == nil { + t.Fatal("expected stream error") + } + if len(observed) != 1 || observed[0] != "partial" { + t.Fatalf("observed=%v", observed) + } + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-stream-error"] + repo.mu.Unlock() + if stored.Status != "failed" || stored.Answer != "" { + t.Fatalf("partial answer must not be persisted as success: %+v", stored) + } +} + +func TestAskStreamFallsBackToFinalObserverForLegacyClient(t *testing.T) { + repo := newMemoryRepository() + client := &fakeWeKnora{} + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + var observed []string + result, err := service.AskStream(context.Background(), streamQARequest("message-legacy"), func(full string) { observed = append(observed, full) }) + if err != nil { + t.Fatalf("AskStream: %v", err) + } + if result.Answer != "answer" || len(observed) != 1 || observed[0] != "answer" { + t.Fatalf("result=%+v observed=%v", result, observed) + } +} + +func TestAskStreamDuplicateReplayDoesNotNotifyAgain(t *testing.T) { + repo := newMemoryRepository() + client := &fakeStreamingWeKnora{ + fakeWeKnora: &fakeWeKnora{}, updates: []string{"answer"}, + streamResult: weknora.ChatResult{RequestID: "request-stream", Answer: "answer"}, + } + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + req := streamQARequest("message-stream-replay") + if _, err = service.AskStream(context.Background(), req, func(string) {}); err != nil { + t.Fatalf("first AskStream: %v", err) + } + secondCalls := 0 + result, err := service.AskStream(context.Background(), req, func(string) { secondCalls++ }) + if err != nil { + t.Fatalf("replay AskStream: %v", err) + } + if !result.Replayed || secondCalls != 0 || client.streamCalls != 1 { + t.Fatalf("result=%+v secondCalls=%d streamCalls=%d", result, secondCalls, client.streamCalls) + } } diff --git a/internal/store/postgres/store.go b/internal/store/postgres/store.go index da867ed..dbc13b5 100644 --- a/internal/store/postgres/store.go +++ b/internal/store/postgres/store.go @@ -35,10 +35,18 @@ func (s *Store) Close() { } func (s *Store) BeginTurn(ctx context.Context, turn core.Turn) (core.Turn, bool, error) { + media := turn.Media + if media == nil { + media = []core.MediaAudit{} + } + mediaJSON, err := json.Marshal(media) + if err != nil { + return core.Turn{}, false, err + } command, err := s.pool.Exec(ctx, `INSERT INTO turns - (id, tenant_key, channel, external_message_id, session_key, question, status) - VALUES ($1,$2,$3,$4,$5,$6,'processing') ON CONFLICT DO NOTHING`, - turn.ID, turn.TenantKey, turn.Channel, turn.ExternalMessageID, turn.SessionKey, turn.Question) + (id, tenant_key, channel, external_message_id, session_key, question, status, media_json) + VALUES ($1,$2,$3,$4,$5,$6,'processing',$7) ON CONFLICT DO NOTHING`, + turn.ID, turn.TenantKey, turn.Channel, turn.ExternalMessageID, turn.SessionKey, turn.Question, mediaJSON) if err != nil { return core.Turn{}, false, err } @@ -84,14 +92,23 @@ func (s *Store) FinishTurn(ctx context.Context, turn core.Turn) error { if err != nil { return err } - _, err = s.pool.Exec(ctx, `UPDATE turns SET weknora_session_id=$2,weknora_request_id=$3,answer=$4,references=$5,status=$6,error_message=$7,updated_at=NOW() WHERE id=$1`, turn.ID, turn.WeKnoraSessionID, turn.WeKnoraRequestID, turn.Answer, references, turn.Status, turn.ErrorMessage) + media := turn.Media + if media == nil { + media = []core.MediaAudit{} + } + mediaJSON, err := json.Marshal(media) + if err != nil { + return err + } + _, err = s.pool.Exec(ctx, `UPDATE turns SET weknora_session_id=$2,weknora_request_id=$3,answer=$4,references_json=$5,media_json=$6,status=$7,error_message=$8,updated_at=NOW() WHERE id=$1`, turn.ID, turn.WeKnoraSessionID, turn.WeKnoraRequestID, turn.Answer, references, mediaJSON, turn.Status, turn.ErrorMessage) return err } func (s *Store) loadTurn(ctx context.Context, tenantKey, channel, messageID string) (core.Turn, error) { var turn core.Turn var references []byte - err := s.pool.QueryRow(ctx, `SELECT id,tenant_key,channel,external_message_id,session_key,weknora_session_id,weknora_request_id,question,answer,references,status,error_message FROM turns WHERE tenant_key=$1 AND channel=$2 AND external_message_id=$3`, tenantKey, channel, messageID).Scan(&turn.ID, &turn.TenantKey, &turn.Channel, &turn.ExternalMessageID, &turn.SessionKey, &turn.WeKnoraSessionID, &turn.WeKnoraRequestID, &turn.Question, &turn.Answer, &references, &turn.Status, &turn.ErrorMessage) + var mediaJSON []byte + err := s.pool.QueryRow(ctx, `SELECT id,tenant_key,channel,external_message_id,session_key,weknora_session_id,weknora_request_id,question,answer,references_json,media_json,status,error_message FROM turns WHERE tenant_key=$1 AND channel=$2 AND external_message_id=$3`, tenantKey, channel, messageID).Scan(&turn.ID, &turn.TenantKey, &turn.Channel, &turn.ExternalMessageID, &turn.SessionKey, &turn.WeKnoraSessionID, &turn.WeKnoraRequestID, &turn.Question, &turn.Answer, &references, &mediaJSON, &turn.Status, &turn.ErrorMessage) if err != nil { return core.Turn{}, err } @@ -100,5 +117,10 @@ func (s *Store) loadTurn(ctx context.Context, tenantKey, channel, messageID stri return core.Turn{}, err } } + if len(mediaJSON) > 0 { + if err := json.Unmarshal(mediaJSON, &turn.Media); err != nil { + return core.Turn{}, err + } + } return turn, nil } diff --git a/internal/store/postgres/store_contract_test.go b/internal/store/postgres/store_contract_test.go new file mode 100644 index 0000000..8ca3244 --- /dev/null +++ b/internal/store/postgres/store_contract_test.go @@ -0,0 +1,91 @@ +// Source-level contract tests: the store's SQL must use the renamed +// references_json column (the bare `references` identifier is a reserved +// word in PostgreSQL 16), while the Go domain field stays turn.References. +// No database connection is required. +package postgres + +import ( + "os" + "path/filepath" + "regexp" + "strings" + "testing" +) + +func readStoreSource(t *testing.T) string { + t.Helper() + data, err := os.ReadFile(filepath.Join(".", "store.go")) + if err != nil { + t.Fatalf("read store.go: %v", err) + } + return string(data) +} + +func extractSQLLiterals(src string) []string { + var sqls []string + re := regexp.MustCompile("`([^`]*)`") + for _, m := range re.FindAllStringSubmatch(src, -1) { + upper := strings.ToUpper(m[1]) + if strings.Contains(upper, "SELECT") || strings.Contains(upper, "UPDATE") || + strings.Contains(upper, "INSERT") || strings.Contains(upper, "DELETE") { + sqls = append(sqls, m[1]) + } + } + return sqls +} + +func TestStoreSQLUsesReferencesJSONColumn(t *testing.T) { + src := readStoreSource(t) + sqls := extractSQLLiterals(src) + if len(sqls) == 0 { + t.Fatal("no SQL literals found in store.go") + } + + var update, selectTurns string + for _, sql := range sqls { + upper := strings.ToUpper(sql) + if strings.HasPrefix(strings.TrimSpace(upper), "UPDATE TURNS") { + update = sql + } + if strings.Contains(upper, "SELECT") && strings.Contains(upper, "FROM TURNS") { + selectTurns = sql + } + } + if update == "" { + t.Fatal("FinishTurn UPDATE turns statement not found") + } + if selectTurns == "" { + t.Fatal("loadTurn SELECT FROM turns statement not found") + } + if !strings.Contains(update, "references_json=$5") { + t.Error("FinishTurn UPDATE must write to references_json column") + } + if !strings.Contains(selectTurns, "answer,references_json,media_json,status") && !strings.Contains(selectTurns, "answer,references_json,status") { + t.Error("loadTurn SELECT must read references_json column") + } +} + +func TestStoreSQLAvoidsBareReferencesIdentifier(t *testing.T) { + src := readStoreSource(t) + + // Inside the actual SQL literals, the bare word `references` is a + // reserved word in PostgreSQL 16 and must not appear as a column + // identifier. The Go variable/field names are irrelevant here because + // we only inspect SQL string literals. + bare := regexp.MustCompile(`(?i)\breferences\b`) + for _, sql := range extractSQLLiterals(src) { + // references_json is fine; strip it before checking for the bare word. + scrubbed := regexp.MustCompile(`(?i)\breferences_json\b`).ReplaceAllString(sql, "") + if bare.MatchString(scrubbed) { + t.Errorf("SQL literal uses reserved word `references` as a column name; use references_json:\n%s", sql) + } + } +} + +func TestStoreKeepsDomainFieldReferences(t *testing.T) { + src := readStoreSource(t) + + if !regexp.MustCompile(`turn\.References`).MatchString(src) { + t.Error("store must keep using the domain field turn.References (API unchanged)") + } +} diff --git a/internal/store/postgres/store_media_test.go b/internal/store/postgres/store_media_test.go new file mode 100644 index 0000000..73cd0a6 --- /dev/null +++ b/internal/store/postgres/store_media_test.go @@ -0,0 +1,99 @@ +package postgres + +import ( + "encoding/json" + "strings" + "testing" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" +) + +func TestStoreSQLUsesMediaJSONColumn(t *testing.T) { + src := readStoreSource(t) + sqls := extractSQLLiterals(src) + + var insert, update, selectTurns string + for _, sql := range sqls { + upper := strings.ToUpper(sql) + if strings.Contains(upper, "INSERT") && strings.Contains(upper, "INTO TURNS") { + insert = sql + } + if strings.HasPrefix(strings.TrimSpace(upper), "UPDATE TURNS") { + update = sql + } + if strings.Contains(upper, "SELECT") && strings.Contains(upper, "FROM TURNS") { + selectTurns = sql + } + } + + if insert == "" { + t.Fatal("BeginTurn INSERT INTO turns statement not found") + } + if update == "" { + t.Fatal("FinishTurn UPDATE turns statement not found") + } + if selectTurns == "" { + t.Fatal("loadTurn SELECT FROM turns statement not found") + } + + if !strings.Contains(insert, "media_json") { + t.Error("BeginTurn INSERT must include media_json column") + } + if !strings.Contains(update, "media_json") { + t.Error("FinishTurn UPDATE must write to media_json column") + } + if !strings.Contains(selectTurns, "media_json") { + t.Error("loadTurn SELECT must read media_json column") + } +} + +func TestMediaAuditJSONExcludesSecrets(t *testing.T) { + ma := core.MediaAudit{ + Type: "image", + MIMEType: "image/png", + SizeBytes: 2048, + Width: 640, + Height: 480, + } + raw, err := json.Marshal(ma) + if err != nil { + t.Fatalf("marshal: %v", err) + } + s := string(raw) + for _, secret := range []string{"DataURI", "data_uri", "DownloadCode", "download_code", "URL", "url"} { + if strings.Contains(s, secret) { + t.Errorf("MediaAudit JSON must not contain secret field %q: %s", secret, s) + } + } + var fields map[string]any + if err := json.Unmarshal(raw, &fields); err != nil { + t.Fatalf("unmarshal fields: %v", err) + } + for _, key := range []string{"type", "mime_type", "size_bytes", "width", "height"} { + if _, ok := fields[key]; !ok { + t.Errorf("MediaAudit JSON missing %q field: %s", key, s) + } + } +} + +func TestMediaAuditNilMarshalsToNull(t *testing.T) { + // Nil slice marshals to "null" — the store must guard against this + // because the media_json column is NOT NULL with DEFAULT '[]'. + var media []core.MediaAudit + raw, err := json.Marshal(media) + if err != nil { + t.Fatalf("marshal: %v", err) + } + if string(raw) != "null" { + t.Errorf("nil media slice marshals to %q (expected null); store must handle nil→[]", raw) + } +} + +func TestStoreHasNilMediaGuard(t *testing.T) { + src := readStoreSource(t) + // The store must ensure nil media is persisted as [] not null. + // Check for a nil guard in BeginTurn (e.g. "if turn.Media == nil" or "media == nil"). + if !strings.Contains(src, "edia == nil") && !strings.Contains(src, "edia != nil") && !strings.Contains(src, "len(turn.Media)") { + t.Error("store must guard against nil media slice (NOT NULL column)") + } +} diff --git a/internal/weknora/client.go b/internal/weknora/client.go index 975bc24..6173a1b 100644 --- a/internal/weknora/client.go +++ b/internal/weknora/client.go @@ -17,6 +17,25 @@ import ( ) var ErrIncompleteStream = errors.New("weknora stream ended without a terminal event") +var ErrEmptyAnswer = errors.New("weknora stream completed with an empty answer") + +// AnswerObserver is called with the accumulated full answer content after each +// answer chunk. The observer is purely a notification; its return is ignored. +type AnswerObserver func(fullContent string) + +// StreamEvent is a safe, structured representation of a WeKnora streaming event. +// It deliberately does not expose raw Content, Data, or Reference content to +// prevent leaking internal thinking/tool details to external channels. +type StreamEvent struct { + ResponseType string + Done bool + FullAnswer string + ReferenceCount int + ToolName string +} + +// StreamObserver is called with a safe StreamEvent for each SSE event. +type StreamObserver func(StreamEvent) type Client struct { baseURL string @@ -78,6 +97,18 @@ func (c *Client) CreateSession(ctx context.Context, title, description string) ( } func (c *Client) AgentChat(ctx context.Context, sessionID string, request AgentChatRequest) (ChatResult, error) { + return c.AgentChatStream(ctx, sessionID, request, nil) +} + +func (c *Client) AgentChatStream(ctx context.Context, sessionID string, request AgentChatRequest, observer AnswerObserver) (ChatResult, error) { + return c.agentChat(ctx, sessionID, request, observer, nil) +} + +func (c *Client) AgentChatEvents(ctx context.Context, sessionID string, request AgentChatRequest, observer StreamObserver) (ChatResult, error) { + return c.agentChat(ctx, sessionID, request, nil, observer) +} + +func (c *Client) agentChat(ctx context.Context, sessionID string, request AgentChatRequest, answerObserver AnswerObserver, eventObserver StreamObserver) (ChatResult, error) { if strings.TrimSpace(sessionID) == "" || strings.TrimSpace(request.AgentID) == "" || strings.TrimSpace(request.Query) == "" { return ChatResult{}, errors.New("session id, agent id and query are required") } @@ -102,7 +133,7 @@ func (c *Client) AgentChat(ctx context.Context, sessionID string, request AgentC message, _ := io.ReadAll(io.LimitReader(resp.Body, 32<<10)) return ChatResult{}, fmt.Errorf("weknora agent chat returned HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(message))) } - return parseSSE(resp.Body) + return parseSSEWithEventObserver(resp.Body, answerObserver, eventObserver) } func (c *Client) doJSON(ctx context.Context, method, path string, input, output any) error { @@ -132,12 +163,26 @@ func (c *Client) doJSON(ctx context.Context, method, path string, input, output } func parseSSE(reader io.Reader) (ChatResult, error) { + return parseSSEWithEventObserver(reader, nil, nil) +} + +func parseSSEWithObserver(reader io.Reader, observer AnswerObserver) (ChatResult, error) { + return parseSSEWithEventObserver(reader, observer, nil) +} + +func parseSSEWithEventObserver(reader io.Reader, answerObserver AnswerObserver, eventObserver StreamObserver) (ChatResult, error) { scanner := bufio.NewScanner(reader) scanner.Buffer(make([]byte, 64<<10), 2<<20) var result ChatResult var dataLines []string terminal := false + notify := func(e StreamEvent) { + if eventObserver != nil { + eventObserver(e) + } + } + flush := func() error { if len(dataLines) == 0 { return nil @@ -150,6 +195,7 @@ func parseSSE(reader io.Reader) (ChatResult, error) { Content string `json:"content"` Done bool `json:"done"` KnowledgeReferences []core.Reference `json:"knowledge_references"` + Data json.RawMessage `json:"data"` } if err := json.Unmarshal([]byte(raw), &event); err != nil { return fmt.Errorf("decode weknora SSE event: %w", err) @@ -160,8 +206,21 @@ func parseSSE(reader io.Reader) (ChatResult, error) { switch event.ResponseType { case "references": result.References = append([]core.Reference(nil), event.KnowledgeReferences...) + notify(StreamEvent{ + ResponseType: "references", + ReferenceCount: len(event.KnowledgeReferences), + }) case "answer": + hasContent := strings.TrimSpace(event.Content) != "" result.Answer += event.Content + if hasContent && answerObserver != nil { + answerObserver(result.Answer) + } + notify(StreamEvent{ + ResponseType: "answer", + Done: event.Done, + FullAnswer: result.Answer, + }) if event.Done { terminal = true } @@ -169,11 +228,37 @@ func parseSSE(reader io.Reader) (ChatResult, error) { if event.Content == "" { event.Content = "weknora returned an unspecified error" } + // Notify with error type but no content, then return the error + notify(StreamEvent{ + ResponseType: "error", + Done: true, + }) return errors.New(event.Content) + case "thinking", "reflection": + notify(StreamEvent{ + ResponseType: event.ResponseType, + Done: event.Done, + }) + case "tool_call", "tool_result": + toolName := extractToolName(event.Data) + notify(StreamEvent{ + ResponseType: event.ResponseType, + Done: event.Done, + ToolName: toolName, + }) case "complete": if event.Done { terminal = true } + notify(StreamEvent{ + ResponseType: "complete", + Done: event.Done, + }) + default: + notify(StreamEvent{ + ResponseType: event.ResponseType, + Done: event.Done, + }) } return nil } @@ -199,5 +284,30 @@ func parseSSE(reader io.Reader) (ChatResult, error) { if !terminal { return ChatResult{}, ErrIncompleteStream } + if strings.TrimSpace(result.Answer) == "" { + return ChatResult{}, ErrEmptyAnswer + } return result, nil } + +// extractToolName extracts the tool_name string from a JSON Data field. +// The Data may be any JSON shape; we look for the "tool_name" key and return +// its string value. Returns empty string if not found or not a string. +func extractToolName(data json.RawMessage) string { + if len(data) == 0 { + return "" + } + var m map[string]json.RawMessage + if err := json.Unmarshal(data, &m); err != nil { + return "" + } + v, ok := m["tool_name"] + if !ok { + return "" + } + var s string + if err := json.Unmarshal(v, &s); err != nil { + return "" + } + return s +} diff --git a/internal/weknora/client_test.go b/internal/weknora/client_test.go index 1d41e73..234fd9b 100644 --- a/internal/weknora/client_test.go +++ b/internal/weknora/client_test.go @@ -2,6 +2,7 @@ package weknora import ( "context" + "encoding/json" "errors" "net/http" "net/http/httptest" @@ -63,3 +64,171 @@ func TestParseSSEErrorDominatesPartialAnswer(t *testing.T) { t.Fatalf("expected terminal vision error, got %v", err) } } + +func TestAgentChatRequestJSONSerializesImages(t *testing.T) { + req := AgentChatRequest{ + Query: "请描述并分析这张图片。", + AgentEnabled: true, + AgentID: "agent-1", + Channel: "im", + Images: []Image{ + {Data: "data:image/png;base64,iVBORw0KGgo="}, + }, + } + raw, err := json.Marshal(req) + if err != nil { + t.Fatalf("marshal: %v", err) + } + if !strings.Contains(string(raw), `"images":[{"data":"data:image/png;base64,iVBORw0KGgo="}]`) { + t.Fatalf("images not serialized correctly: %s", raw) + } +} + +func TestParseSSEWithObserverAnswerChunks(t *testing.T) { + stream := "data: {\"response_type\":\"answer\",\"content\":\"hello \",\"done\":false}\n\n" + + "data: {\"response_type\":\"answer\",\"content\":\"world\",\"done\":true}\n\n" + var observed []string + result, err := parseSSEWithObserver(strings.NewReader(stream), func(full string) { + observed = append(observed, full) + }) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if result.Answer != "hello world" { + t.Fatalf("expected 'hello world', got %q", result.Answer) + } + if len(observed) != 2 || observed[0] != "hello " || observed[1] != "hello world" { + t.Fatalf("unexpected observed: %v", observed) + } +} + +func TestParseSSEWithObserverReferencesDoesNotTrigger(t *testing.T) { + stream := "data: {\"response_type\":\"references\",\"knowledge_references\":[{\"knowledge_id\":\"kb-1\",\"content\":\"ref\"}]}\n\n" + + "data: {\"response_type\":\"answer\",\"content\":\"x\",\"done\":true}\n\n" + var observed []string + _, err := parseSSEWithObserver(strings.NewReader(stream), func(full string) { + observed = append(observed, full) + }) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(observed) != 1 || observed[0] != "x" { + t.Fatalf("references should not trigger observer, got %v", observed) + } +} + +func TestParseSSEWithObserverDoneEmptyChunk(t *testing.T) { + // done=true but content empty — should mark terminal but not trigger observer + stream := "data: {\"response_type\":\"answer\",\"content\":\"hello\",\"done\":false}\n\n" + + "data: {\"response_type\":\"answer\",\"content\":\"\",\"done\":true}\n\n" + var observed []string + result, err := parseSSEWithObserver(strings.NewReader(stream), func(full string) { + observed = append(observed, full) + }) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if result.Answer != "hello" { + t.Fatalf("expected 'hello', got %q", result.Answer) + } + if len(observed) != 1 || observed[0] != "hello" { + t.Fatalf("done empty chunk should not trigger observer, got %v", observed) + } +} + +func TestParseSSEWithObserverTerminalEmptyAnswer(t *testing.T) { + stream := "data: {\"response_type\":\"answer\",\"content\":\"\",\"done\":true}\n\n" + var observed []string + _, err := parseSSEWithObserver(strings.NewReader(stream), func(full string) { + observed = append(observed, full) + }) + if !errors.Is(err, ErrEmptyAnswer) { + t.Fatalf("expected ErrEmptyAnswer, got %v", err) + } + if len(observed) != 0 { + t.Fatalf("empty answer should not trigger observer, got %v", observed) + } +} + +func TestParseSSEWithObserverErrorDominatesPartial(t *testing.T) { + stream := "data: {\"response_type\":\"answer\",\"content\":\"partial\",\"done\":false}\n\n" + + "data: {\"response_type\":\"error\",\"content\":\"boom\",\"done\":true}\n\n" + var observed []string + _, err := parseSSEWithObserver(strings.NewReader(stream), func(full string) { + observed = append(observed, full) + }) + if err == nil || !strings.Contains(err.Error(), "boom") { + t.Fatalf("expected error containing 'boom', got %v", err) + } + // observer should have been called once with "partial" before the error + if len(observed) != 1 || observed[0] != "partial" { + t.Fatalf("expected observer called with 'partial', got %v", observed) + } +} + +func TestAgentChatStreamCallsObserver(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/api/v1/agent-chat/session-1", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"response_type\":\"answer\",\"content\":\"hello \",\"done\":false}\n\n")) + _, _ = w.Write([]byte("data: {\"response_type\":\"answer\",\"content\":\"world\",\"done\":true}\n\n")) + }) + server := httptest.NewServer(mux) + defer server.Close() + + client, err := NewClient(server.URL, "test-key", 5*time.Second) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + var observed []string + result, err := client.AgentChatStream(context.Background(), "session-1", + AgentChatRequest{Query: "q", AgentID: "agent-1", Channel: "im"}, + func(full string) { observed = append(observed, full) }) + if err != nil { + t.Fatalf("AgentChatStream: %v", err) + } + if result.Answer != "hello world" { + t.Fatalf("expected 'hello world', got %q", result.Answer) + } + if len(observed) != 2 || observed[0] != "hello " || observed[1] != "hello world" { + t.Fatalf("unexpected observed: %v", observed) + } +} + +func TestAgentChatStreamNilObserver(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/api/v1/agent-chat/session-1", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"response_type\":\"answer\",\"content\":\"hello\",\"done\":true}\n\n")) + }) + server := httptest.NewServer(mux) + defer server.Close() + + client, err := NewClient(server.URL, "test-key", 5*time.Second) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + result, err := client.AgentChatStream(context.Background(), "session-1", + AgentChatRequest{Query: "q", AgentID: "agent-1", Channel: "im"}, nil) + if err != nil { + t.Fatalf("AgentChatStream with nil observer: %v", err) + } + if result.Answer != "hello" { + t.Fatalf("expected 'hello', got %q", result.Answer) + } +} + +func TestAgentChatRequestJSONOmitsEmptyImages(t *testing.T) { + req := AgentChatRequest{ + Query: "hello", + AgentEnabled: true, + AgentID: "agent-1", + } + raw, err := json.Marshal(req) + if err != nil { + t.Fatalf("marshal: %v", err) + } + if strings.Contains(string(raw), `"images"`) { + t.Fatalf("empty images should be omitted: %s", raw) + } +} diff --git a/internal/weknora/stream_event_test.go b/internal/weknora/stream_event_test.go new file mode 100644 index 0000000..331805a --- /dev/null +++ b/internal/weknora/stream_event_test.go @@ -0,0 +1,269 @@ +package weknora + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +// TestStreamEventSequence verifies the full event sequence: +// thinking → tool_call(query_understand) → tool_result → tool_call(knowledge_search) → +// references(count=3) → answer cumulative → answer done. +// It also asserts that no thinking/tool content leaks into StreamEvent (type has no Content field). +func TestStreamEventSequence(t *testing.T) { + stream := "data: {\"response_type\":\"thinking\",\"content\":\"inner monologue\",\"done\":false}\n\n" + + "data: {\"response_type\":\"tool_call\",\"data\":{\"tool_name\":\"query_understand\",\"arguments\":{\"query\":\"test\"}},\"done\":false}\n\n" + + "data: {\"response_type\":\"tool_result\",\"data\":{\"tool_name\":\"query_understand\",\"result\":\"understood\"},\"done\":false}\n\n" + + "data: {\"response_type\":\"tool_call\",\"data\":{\"tool_name\":\"knowledge_search\",\"arguments\":{\"query\":\"test\"}},\"done\":false}\n\n" + + "data: {\"response_type\":\"references\",\"knowledge_references\":[{\"knowledge_id\":\"kb-1\",\"content\":\"ref1\"},{\"knowledge_id\":\"kb-2\",\"content\":\"ref2\"},{\"knowledge_id\":\"kb-3\",\"content\":\"ref3\"}]}\n\n" + + "data: {\"response_type\":\"answer\",\"content\":\"hello \",\"done\":false}\n\n" + + "data: {\"response_type\":\"answer\",\"content\":\"world\",\"done\":true}\n\n" + + var events []StreamEvent + result, err := parseSSEWithEventObserver(strings.NewReader(stream), nil, func(e StreamEvent) { + events = append(events, e) + }) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if result.Answer != "hello world" { + t.Fatalf("expected 'hello world', got %q", result.Answer) + } + if len(result.References) != 3 { + t.Fatalf("expected 3 references, got %d", len(result.References)) + } + + // Expected sequence: thinking, tool_call(query_understand), tool_result, tool_call(knowledge_search), references, answer(hello ), answer(hello world) + expectedTypes := []string{"thinking", "tool_call", "tool_result", "tool_call", "references", "answer", "answer"} + if len(events) != len(expectedTypes) { + t.Fatalf("expected %d events, got %d: %+v", len(expectedTypes), len(events), events) + } + for i, want := range expectedTypes { + if events[i].ResponseType != want { + t.Fatalf("event[%d] ResponseType: want %q, got %q", i, want, events[i].ResponseType) + } + } + + // thinking: must not carry Content + if events[0].ToolName != "" || events[0].FullAnswer != "" || events[0].ReferenceCount != 0 { + t.Fatalf("thinking event must not leak content: %+v", events[0]) + } + + // tool_call(query_understand): ToolName must be "query_understand" + if events[1].ToolName != "query_understand" { + t.Fatalf("tool_call expected ToolName 'query_understand', got %q", events[1].ToolName) + } + if events[1].FullAnswer != "" || events[1].ReferenceCount != 0 { + t.Fatalf("tool_call must not leak other fields: %+v", events[1]) + } + + // tool_result: ToolName must be "query_understand" + if events[2].ToolName != "query_understand" { + t.Fatalf("tool_result expected ToolName 'query_understand', got %q", events[2].ToolName) + } + + // tool_call(knowledge_search): ToolName must be "knowledge_search" + if events[3].ToolName != "knowledge_search" { + t.Fatalf("tool_call expected ToolName 'knowledge_search', got %q", events[3].ToolName) + } + + // references: ReferenceCount must be 3 + if events[4].ReferenceCount != 3 { + t.Fatalf("references expected ReferenceCount=3, got %d", events[4].ReferenceCount) + } + + // answer(hello): Done=false, FullAnswer="hello " + if events[5].FullAnswer != "hello " || events[5].Done { + t.Fatalf("answer chunk 1: expected FullAnswer='hello ' Done=false, got %+v", events[5]) + } + + // answer(hello world): Done=true, FullAnswer="hello world" + if events[6].FullAnswer != "hello world" || !events[6].Done { + t.Fatalf("answer chunk 2: expected FullAnswer='hello world' Done=true, got %+v", events[6]) + } + + // Verify no event has any raw content field (StreamEvent struct has no Content/Data field) + // This is a compile-time guarantee — if StreamEvent had Content, we'd have a compilation error. + // Runtime: ensure events don't leak thinking content or tool Data content. + for i, e := range events { + if e.ResponseType == "thinking" && e.FullAnswer != "" { + t.Fatalf("event[%d]: thinking must not leak content", i) + } + } +} + +// TestStreamEventErrorDoesNotLeakContent verifies error events notify but do not expose error content. +func TestStreamEventErrorDoesNotLeakContent(t *testing.T) { + stream := "data: {\"response_type\":\"answer\",\"content\":\"partial\",\"done\":false}\n\n" + + "data: {\"response_type\":\"error\",\"content\":\"secret internal error details\",\"done\":true}\n\n" + + var events []StreamEvent + _, err := parseSSEWithEventObserver(strings.NewReader(stream), nil, func(e StreamEvent) { + events = append(events, e) + }) + if err == nil { + t.Fatal("expected error from error event") + } + // The error event notification should have been sent before the error returned. + // It should only have ResponseType="error" and Done=true, no Content. + if len(events) < 2 { + t.Fatalf("expected at least 2 events (answer + error notification), got %d: %+v", len(events), events) + } + last := events[len(events)-1] + if last.ResponseType != "error" { + t.Fatalf("expected last event to be error, got %+v", last) + } + if !last.Done { + t.Fatal("error event should have Done=true") + } + // Error content must NOT leak into the event + if last.FullAnswer != "" || last.ToolName != "" || last.ReferenceCount != 0 { + t.Fatalf("error event must not leak content: %+v", last) + } +} + +// TestStreamEventAnswerObserverRegression verifies the old AnswerObserver still works correctly. +func TestStreamEventAnswerObserverRegression(t *testing.T) { + stream := "data: {\"response_type\":\"answer\",\"content\":\"hello \",\"done\":false}\n\n" + + "data: {\"response_type\":\"answer\",\"content\":\"world\",\"done\":true}\n\n" + + var observed []string + result, err := parseSSEWithEventObserver(strings.NewReader(stream), func(full string) { + observed = append(observed, full) + }, nil) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if result.Answer != "hello world" { + t.Fatalf("expected 'hello world', got %q", result.Answer) + } + if len(observed) != 2 || observed[0] != "hello " || observed[1] != "hello world" { + t.Fatalf("unexpected observed: %v", observed) + } +} + +// TestStreamEventAnswerObserverDoneEmptyChunk verifies done empty chunk +// notifies StreamEvent but does NOT trigger old AnswerObserver. +func TestStreamEventAnswerObserverDoneEmptyChunk(t *testing.T) { + stream := "data: {\"response_type\":\"answer\",\"content\":\"hello\",\"done\":false}\n\n" + + "data: {\"response_type\":\"answer\",\"content\":\"\",\"done\":true}\n\n" + + var observed []string + var events []StreamEvent + result, err := parseSSEWithEventObserver(strings.NewReader(stream), func(full string) { + observed = append(observed, full) + }, func(e StreamEvent) { + events = append(events, e) + }) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if result.Answer != "hello" { + t.Fatalf("expected 'hello', got %q", result.Answer) + } + // Old AnswerObserver: only called for non-empty content chunks + if len(observed) != 1 || observed[0] != "hello" { + t.Fatalf("old observer should only see non-empty chunks, got %v", observed) + } + // StreamEvent: should include the done empty chunk (FullAnswer is accumulated) + if len(events) < 2 { + t.Fatalf("expected at least 2 stream events, got %d: %+v", len(events), events) + } + last := events[len(events)-1] + if !last.Done || last.FullAnswer != "hello" { + t.Fatalf("last event should have Done=true FullAnswer='hello', got %+v", last) + } +} + +// TestAgentChatEventsRunner verifies the AgentChatEvents HTTP method reuses HTTP logic. +func TestAgentChatEventsRunner(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/api/v1/agent-chat/session-1", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"response_type\":\"thinking\",\"content\":\"think\",\"done\":false}\n\n")) + _, _ = w.Write([]byte("data: {\"response_type\":\"tool_call\",\"data\":{\"tool_name\":\"knowledge_search\"},\"done\":false}\n\n")) + _, _ = w.Write([]byte("data: {\"response_type\":\"references\",\"knowledge_references\":[{\"knowledge_id\":\"kb-1\",\"content\":\"ref\"}]}\n\n")) + _, _ = w.Write([]byte("data: {\"response_type\":\"answer\",\"content\":\"hello\",\"done\":true}\n\n")) + }) + server := httptest.NewServer(mux) + defer server.Close() + + client, err := NewClient(server.URL, "test-key", 5*time.Second) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + var events []StreamEvent + result, err := client.AgentChatEvents(context.Background(), "session-1", + AgentChatRequest{Query: "q", AgentID: "agent-1", Channel: "im"}, + func(e StreamEvent) { events = append(events, e) }) + if err != nil { + t.Fatalf("AgentChatEvents: %v", err) + } + if result.Answer != "hello" { + t.Fatalf("expected 'hello', got %q", result.Answer) + } + if len(events) != 4 { + t.Fatalf("expected 4 events, got %d: %+v", len(events), events) + } +} + +// TestAgentChatEventsNilObserver verifies nil observer is safe. +func TestAgentChatEventsNilObserver(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/api/v1/agent-chat/session-1", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"response_type\":\"answer\",\"content\":\"ok\",\"done\":true}\n\n")) + }) + server := httptest.NewServer(mux) + defer server.Close() + + client, err := NewClient(server.URL, "test-key", 5*time.Second) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + result, err := client.AgentChatEvents(context.Background(), "session-1", + AgentChatRequest{Query: "q", AgentID: "agent-1", Channel: "im"}, nil) + if err != nil { + t.Fatalf("AgentChatEvents with nil observer: %v", err) + } + if result.Answer != "ok" { + t.Fatalf("expected 'ok', got %q", result.Answer) + } +} + +// TestAgentChatEventsErrorTurn verifies error event returns error but notifies first. +func TestAgentChatEventsErrorTurn(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/api/v1/agent-chat/session-1", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"response_type\":\"error\",\"content\":\"boom\",\"done\":true}\n\n")) + }) + server := httptest.NewServer(mux) + defer server.Close() + + client, err := NewClient(server.URL, "test-key", 5*time.Second) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + var events []StreamEvent + _, err = client.AgentChatEvents(context.Background(), "session-1", + AgentChatRequest{Query: "q", AgentID: "agent-1", Channel: "im"}, + func(e StreamEvent) { events = append(events, e) }) + if err == nil { + t.Fatal("expected error") + } + if !errors.Is(err, errors.New("boom")) && !strings.Contains(err.Error(), "boom") { + t.Fatalf("expected error containing 'boom', got %v", err) + } + // error event should have notified with type="error", done=true, no content + if len(events) != 1 { + t.Fatalf("expected 1 error notification event, got %d: %+v", len(events), events) + } + if events[0].ResponseType != "error" || !events[0].Done { + t.Fatalf("expected error done event, got %+v", events[0]) + } +} diff --git a/scripts/deploy-local.sh b/scripts/deploy-local.sh new file mode 100755 index 0000000..f61808f --- /dev/null +++ b/scripts/deploy-local.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +set -euo pipefail + +root="/home/zheromiku/dobotai-platform" +go_bin="${GO_BIN:-/home/zheromiku/.local/share/go1.26.5/bin/go}" +bin_dir="$root/bin" +current="$bin_dir/dobotai" +next="$bin_dir/dobotai.new" +previous="$bin_dir/dobotai.prev" +service="dobotai.service" + +mkdir -p "$bin_dir" +cd "$root" + +"$go_bin" build -trimpath -o "$next" ./cmd/dobotai +chmod 0755 "$next" + +if [[ -f "$current" ]]; then + cp -p "$current" "$previous" +fi +mv -f "$next" "$current" + +systemctl --user restart "$service" + +for _ in $(seq 1 30); do + if curl --noproxy '*' --fail --silent --max-time 2 http://127.0.0.1:8090/healthz >/dev/null; then + echo "DOBOTAI_DEPLOY_OK" + exit 0 + fi + sleep 1 +done + +echo "DobotAI health check failed; attempting binary rollback" >&2 +if [[ -f "$previous" ]]; then + cp -p "$previous" "$current" + systemctl --user restart "$service" +fi +systemctl --user status "$service" --no-pager >&2 || true +exit 1 diff --git a/scripts/run-local.py b/scripts/run-local.py new file mode 100755 index 0000000..301d858 --- /dev/null +++ b/scripts/run-local.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +"""Load DobotAI's local .env without shell evaluation, then exec the binary.""" + +import os +from pathlib import Path + +ROOT = Path("/home/zheromiku/dobotai-platform") +ENV_FILE = ROOT / ".env" +BINARY = ROOT / "bin" / "dobotai" + + +def load_env(path: Path) -> dict[str, str]: + env = os.environ.copy() + for raw in path.read_text().splitlines(): + line = raw.strip() + if not line or line.startswith("#") or "=" not in line: + continue + key, value = line.split("=", 1) + env[key.strip()] = value.strip() + # WeKnora is local and DingTalk is directly reachable in China. Do not make + # the service depend on a desktop proxy that may be absent during boot. + for key in ( + "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", + "http_proxy", "https_proxy", "all_proxy", + ): + env.pop(key, None) + env["NO_PROXY"] = "localhost,127.0.0.1" + env["no_proxy"] = env["NO_PROXY"] + return env + + +def main() -> None: + if not ENV_FILE.is_file(): + raise SystemExit(f"missing environment file: {ENV_FILE}") + if not BINARY.is_file(): + raise SystemExit(f"missing DobotAI binary: {BINARY}") + os.chdir(ROOT) + os.execve(BINARY, [str(BINARY)], load_env(ENV_FILE)) + + +if __name__ == "__main__": + main()