diff --git a/cmd/dobotai/faq_workflow.go b/cmd/dobotai/faq_workflow.go index 25d2f41..c718ab6 100644 --- a/cmd/dobotai/faq_workflow.go +++ b/cmd/dobotai/faq_workflow.go @@ -13,10 +13,11 @@ import ( ) type faqConfig struct { - BaseID string - SheetID string - ViewID string - OperatorID string + BaseID string + SheetID string + ViewID string + OperatorID string + FieldMapping map[string]string } func readFAQConfig() (faqConfig, error) { @@ -46,7 +47,7 @@ func buildFAQDispatcher(repo faqtable.ExportRepository, clientID, clientSecret s } client, err := questiontable.NewClient(questiontable.Config{ ClientID: clientID, Secret: clientSecret, BaseID: cfg.BaseID, - SheetID: cfg.SheetID, OperatorID: cfg.OperatorID, + SheetID: cfg.SheetID, OperatorID: cfg.OperatorID, FieldMapping: cfg.FieldMapping, }, nil) if err != nil { return nil, err diff --git a/cmd/dobotai/main.go b/cmd/dobotai/main.go index 13a3040..5caa43c 100644 --- a/cmd/dobotai/main.go +++ b/cmd/dobotai/main.go @@ -107,10 +107,17 @@ func main() { if err := dingCfg.Validate(); err != nil { log.Fatal(err) } + activeBindings, err := settingsRuntime.repository.ActiveBindings(storeCtx, dingCfg.TenantKey) + if err != nil { + log.Fatal(err) + } faqCfg, err := readFAQConfig() if err != nil { log.Fatal(err) } + if binding, ok := activeBindings["faq_table"]; ok { + faqCfg = faqConfig{BaseID: binding.BaseID, SheetID: binding.SheetID, ViewID: binding.ViewID, OperatorID: binding.OperatorID, FieldMapping: binding.FieldMapping} + } var faqWorkflowDone chan struct{} var faqRecordLinks *faqtable.RecordLinkBuilder if faqCfg.BaseID != "" { @@ -134,11 +141,15 @@ func main() { log.Printf("DingTalk FAQ table dispatcher started") } + supportConversationID := os.Getenv("DINGTALK_SUPPORT_GROUP_OPEN_CONVERSATION_ID") + if binding, ok := activeBindings["support_group"]; ok { + supportConversationID = binding.ConversationID + } supportDispatcher, err := buildSupportDispatcher( store, dingCfg.ClientID, dingCfg.ClientSecret, - os.Getenv("DINGTALK_SUPPORT_GROUP_OPEN_CONVERSATION_ID"), + supportConversationID, faqRecordLinks, ) if err != nil { @@ -153,6 +164,9 @@ func main() { if err != nil { log.Fatal(err) } + if binding, ok := activeBindings["question_table"]; ok { + qtCfg = questionTableConfig{BaseID: binding.BaseID, SheetID: binding.SheetID, OperatorID: binding.OperatorID, FieldMapping: binding.FieldMapping} + } var questionTableDone chan struct{} if qtCfg.BaseID != "" { qtRepo, err := postgres.NewQuestionTableExportRepository(store) diff --git a/cmd/dobotai/question_table_sync.go b/cmd/dobotai/question_table_sync.go index 6950765..fffa7fe 100644 --- a/cmd/dobotai/question_table_sync.go +++ b/cmd/dobotai/question_table_sync.go @@ -19,9 +19,10 @@ type questionTablePollerRunner interface { // questionTableConfig holds the optional Notable question-table env values. type questionTableConfig struct { - BaseID string - SheetID string - OperatorID string + BaseID string + SheetID string + OperatorID string + FieldMapping map[string]string } // readQuestionTableConfig reads the three dedicated env vars and returns @@ -61,11 +62,12 @@ func buildQuestionTableDispatcher(repo questiontable.ExportRepository, clientID, } client, err := questiontable.NewClient(questiontable.Config{ - ClientID: clientID, - Secret: clientSecret, - BaseID: cfg.BaseID, - SheetID: cfg.SheetID, - OperatorID: cfg.OperatorID, + ClientID: clientID, + Secret: clientSecret, + BaseID: cfg.BaseID, + SheetID: cfg.SheetID, + OperatorID: cfg.OperatorID, + FieldMapping: cfg.FieldMapping, }, nil) if err != nil { return nil, err diff --git a/cmd/dobotai/settings_binding_validator.go b/cmd/dobotai/settings_binding_validator.go new file mode 100644 index 0000000..eba4ad8 --- /dev/null +++ b/cmd/dobotai/settings_binding_validator.go @@ -0,0 +1,50 @@ +package main + +import ( + "context" + "errors" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/questiontable" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/settings" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/store/postgres" +) + +type validatedSettingsRepository struct { + *postgres.SettingsRepository + clientID string + secret string +} + +func (r *validatedSettingsRepository) SaveBinding(ctx context.Context, binding settings.ResourceBinding) error { + switch binding.Kind { + case "question_table", "faq_table": + client, err := questiontable.NewClient(questiontable.Config{ + ClientID: r.clientID, Secret: r.secret, BaseID: binding.BaseID, + SheetID: binding.SheetID, OperatorID: binding.OperatorID, + }, nil) + if err != nil { + return errors.New("table_binding_invalid_config") + } + fields, err := client.ReadFields(ctx) + if err != nil { + return err + } + required := questiontable.QuestionSchema() + if binding.Kind == "faq_table" { + required = questiontable.FAQSchema() + } + mapping, err := questiontable.MapRequiredFields(fields, required) + if err != nil { + return err + } + binding.FieldMapping = mapping + binding.ValidationCode = "schema_ok" + binding.Activate = true + case "support_group": + binding.ValidationCode = "pending_test_message" + binding.Activate = false + default: + return errors.New("unsupported_binding_kind") + } + return r.SettingsRepository.SaveBinding(ctx, binding) +} diff --git a/cmd/dobotai/settings_wiring.go b/cmd/dobotai/settings_wiring.go index a26b035..d83fe40 100644 --- a/cmd/dobotai/settings_wiring.go +++ b/cmd/dobotai/settings_wiring.go @@ -83,5 +83,6 @@ func buildSettingsRuntime(ctx context.Context, store *postgres.Store, tenantKey if err != nil { return nil, err } - return &settingsRuntime{repository: repo, controller: settings.NewController(repo), admin: control, clientID: clientID, secret: secret}, nil + validatedRepo := &validatedSettingsRepository{SettingsRepository: repo, clientID: clientID, secret: secret} + return &settingsRuntime{repository: repo, controller: settings.NewController(validatedRepo), admin: control, clientID: clientID, secret: secret}, nil } diff --git a/internal/questiontable/client.go b/internal/questiontable/client.go index 4900eba..47613f6 100644 --- a/internal/questiontable/client.go +++ b/internal/questiontable/client.go @@ -19,11 +19,12 @@ const defaultBase = "https://api.dingtalk.com" // Config holds the required parameters for the DingTalk question-table client. type Config struct { - ClientID string - Secret string - BaseID string - SheetID string - OperatorID string + ClientID string + Secret string + BaseID string + SheetID string + OperatorID string + FieldMapping map[string]string // TokenURL overrides the token endpoint. Empty → default. TokenURL string @@ -54,11 +55,12 @@ var defaultTransport = func() *http.Transport { // Client is a stdlib-only DingTalk HTTP client that inserts records into a // Notable v1 base. It owns a mutex-protected in-memory access-token cache. type Client struct { - clientID string - secret string - baseID string - sheetID string - operatorID string + clientID string + secret string + baseID string + sheetID string + operatorID string + fieldMapping map[string]string tokenURL string baseURL string @@ -113,14 +115,15 @@ func NewClient(cfg Config, httpClient *http.Client) (*Client, error) { } return &Client{ - clientID: clientID, - secret: secret, - baseID: baseID, - sheetID: sheetID, - operatorID: operatorID, - tokenURL: tokenURL, - baseURL: baseURL, - http: httpClient, + clientID: clientID, + secret: secret, + baseID: baseID, + sheetID: sheetID, + operatorID: operatorID, + fieldMapping: cloneFieldMapping(cfg.FieldMapping), + tokenURL: tokenURL, + baseURL: baseURL, + http: httpClient, }, nil } @@ -161,6 +164,7 @@ func (c *Client) InsertRecord(ctx context.Context, row Row) (string, error) { // InsertFields sends one already-typed field map to DingTalk Notable v1 and // returns the record ID. It is shared by other reviewed Notable table schemas. func (c *Client) InsertFields(ctx context.Context, fields map[string]interface{}) (string, error) { + fields = c.remapFields(fields) token, err := c.getAccessToken(ctx) if err != nil { return "", fmt.Errorf("questiontable token: %w", err) @@ -213,6 +217,37 @@ func (c *Client) InsertFields(ctx context.Context, fields map[string]interface{} return ir.Value[0].ID, nil } +func cloneFieldMapping(in map[string]string) map[string]string { + out := make(map[string]string, len(in)) + for key, value := range in { + out[key] = value + } + return out +} + +var providerFieldLogical = map[string]string{ + "ZaMZF6j": "chinese_question", "SCug9My": "asked_time", "S2XA1uZ": "asker", + "bETcL3r": "original_question", "Y4cpmxU": "answer", "JgY2f1z": "trigger_person", + "DNIV3mQ": "language", "eBc6a6b": "department", "fPNLeVW": "region", + "01ZM8y7": "standard_question", "fj3JQ1C": "original_question", "07YYagS": "language", "Eu7gkKu": "asker", +} + +func (c *Client) remapFields(in map[string]interface{}) map[string]interface{} { + if len(c.fieldMapping) == 0 { + return in + } + out := make(map[string]interface{}, len(in)) + for providerID, value := range in { + logical := providerFieldLogical[providerID] + if mapped := c.fieldMapping[logical]; logical != "" && mapped != "" { + out[mapped] = value + } else { + out[providerID] = value + } + } + return out +} + // safeDoErr wraps an http.Do error without exposing the raw URL or credentials. // If the context is already done, returns a wrapped ctx.Err() so errors.Is works. func safeDoErr(ctx context.Context, err error) error { diff --git a/internal/questiontable/remap_test.go b/internal/questiontable/remap_test.go new file mode 100644 index 0000000..8d1ea81 --- /dev/null +++ b/internal/questiontable/remap_test.go @@ -0,0 +1,14 @@ +package questiontable + +import "testing" + +func TestRemapFieldsUsesValidatedLogicalMapping(t *testing.T) { + client := &Client{fieldMapping: map[string]string{"answer": "new-answer", "original_question": "new-question"}} + got := client.remapFields(map[string]interface{}{FieldAnswer: "A", FieldOriginalQuestion: "Q"}) + if got["new-answer"] != "A" || got["new-question"] != "Q" { + t.Fatalf("got=%#v", got) + } + if _, ok := got[FieldAnswer]; ok { + t.Fatal("legacy field id must not remain when mapping exists") + } +} diff --git a/internal/questiontable/schema.go b/internal/questiontable/schema.go new file mode 100644 index 0000000..e213cbb --- /dev/null +++ b/internal/questiontable/schema.go @@ -0,0 +1,108 @@ +package questiontable + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "strings" +) + +type Field struct { + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` +} + +type RequiredField struct { + Logical string + Name string + Type string +} + +func QuestionSchema() []RequiredField { + return []RequiredField{ + {"chinese_question", "中文问题", "text"}, + {"asked_time", "提问时间", "date"}, + {"asker", "提问人", "user"}, + {"original_question", "原始问题", "text"}, + {"answer", "AI答案", "text"}, + {"trigger_person", "流程触发人员信息", "text"}, + {"language", "语言", "text"}, + {"department", "部门", "text"}, + {"region", "区域", "text"}, + } +} + +func FAQSchema() []RequiredField { + return []RequiredField{ + {"standard_question", "标准问题(必填)", "text"}, + {"original_question", "原始问题", "text"}, + {"language", "语言", "text"}, + {"asker", "提问人", "user"}, + } +} + +func MapRequiredFields(fields []Field, required []RequiredField) (map[string]string, error) { + out := make(map[string]string, len(required)) + for _, want := range required { + nameMatches := make([]Field, 0, 1) + for _, got := range fields { + if strings.TrimSpace(got.Name) == want.Name { + nameMatches = append(nameMatches, got) + } + } + if len(nameMatches) == 0 { + return nil, errors.New("missing_field") + } + if len(nameMatches) > 1 { + return nil, errors.New("duplicate_field") + } + if strings.TrimSpace(nameMatches[0].Type) != want.Type { + return nil, errors.New("field_type_mismatch") + } + if strings.TrimSpace(nameMatches[0].ID) == "" { + return nil, errors.New("missing_field_id") + } + out[want.Logical] = strings.TrimSpace(nameMatches[0].ID) + } + return out, nil +} + +func (c *Client) ReadFields(ctx context.Context) ([]Field, error) { + token, err := c.getAccessToken(ctx) + if err != nil { + return nil, errors.New("schema_token_failed") + } + endpoint := fmt.Sprintf("%s/v1.0/notable/bases/%s/sheets/%s/fields?operatorId=%s", c.baseURL, url.PathEscape(c.baseID), url.PathEscape(c.sheetID), url.QueryEscape(c.operatorID)) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) + if err != nil { + return nil, errors.New("schema_request_failed") + } + req.Header.Set("x-acs-dingtalk-access-token", token) + resp, err := c.http.Do(req) + if err != nil { + return nil, errors.New("schema_network_failed") + } + defer resp.Body.Close() + raw, err := io.ReadAll(io.LimitReader(resp.Body, 256<<10)) + if err != nil { + return nil, errors.New("schema_read_failed") + } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return nil, fmt.Errorf("schema_http_%d", resp.StatusCode) + } + var payload struct { + Value []Field `json:"value"` + } + if json.Unmarshal(raw, &payload) != nil { + return nil, errors.New("schema_decode_failed") + } + if len(payload.Value) == 0 { + return nil, errors.New("schema_empty") + } + return payload.Value, nil +} diff --git a/internal/questiontable/schema_test.go b/internal/questiontable/schema_test.go new file mode 100644 index 0000000..cf47f2e --- /dev/null +++ b/internal/questiontable/schema_test.go @@ -0,0 +1,77 @@ +package questiontable + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestReadFieldsUsesOperatorAndMapsQuestionSchema(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/token", func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(`{"accessToken":"token","expireIn":7200}`)) + }) + mux.HandleFunc("/v1.0/notable/bases/base/sheets/sheet/fields", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("operatorId") != "union" { + t.Fatalf("operator=%q", r.URL.Query().Get("operatorId")) + } + _, _ = w.Write([]byte(`{"value":[ + {"id":"q","name":"中文问题","type":"text"},{"id":"time","name":"提问时间","type":"date"}, + {"id":"asker","name":"提问人","type":"user"},{"id":"orig","name":"原始问题","type":"text"}, + {"id":"answer","name":"AI答案","type":"text"},{"id":"trigger","name":"流程触发人员信息","type":"text"}, + {"id":"lang","name":"语言","type":"text"},{"id":"dept","name":"部门","type":"text"},{"id":"region","name":"区域","type":"text"}]}`)) + }) + server := httptest.NewServer(mux) + defer server.Close() + client, err := NewClient(Config{ClientID: "id", Secret: "secret", BaseID: "base", SheetID: "sheet", OperatorID: "union", TokenURL: server.URL + "/token", BaseURL: server.URL}, server.Client()) + if err != nil { + t.Fatal(err) + } + fields, err := client.ReadFields(context.Background()) + if err != nil { + t.Fatal(err) + } + mapping, err := MapRequiredFields(fields, QuestionSchema()) + if err != nil { + t.Fatal(err) + } + if mapping["answer"] != "answer" || len(mapping) != 9 { + t.Fatalf("mapping=%v", mapping) + } +} + +func TestMapRequiredFieldsRejectsMissingDuplicateAndWrongType(t *testing.T) { + required := []RequiredField{{Logical: "question", Name: "问题", Type: "text"}} + cases := []struct { + name string + fields []Field + code string + }{ + {"missing", nil, "missing_field"}, + {"duplicate", []Field{{ID: "1", Name: "问题", Type: "text"}, {ID: "2", Name: "问题", Type: "text"}}, "duplicate_field"}, + {"wrong type", []Field{{ID: "1", Name: "问题", Type: "user"}}, "field_type_mismatch"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, err := MapRequiredFields(tc.fields, required) + if err == nil || !strings.Contains(err.Error(), tc.code) { + t.Fatalf("err=%v", err) + } + }) + } +} + +func TestFAQSchemaUsesFourCurrentInsertFields(t *testing.T) { + required := FAQSchema() + if len(required) != 4 { + t.Fatalf("len=%d", len(required)) + } + want := map[string]string{"standard_question": "text", "original_question": "text", "language": "text", "asker": "user"} + for _, field := range required { + if want[field.Logical] != field.Type { + t.Fatalf("field=%+v", field) + } + } +} diff --git a/internal/settings/controller.go b/internal/settings/controller.go index cfb8e80..4a89259 100644 --- a/internal/settings/controller.go +++ b/internal/settings/controller.go @@ -12,6 +12,9 @@ type ResourceBinding struct { Kind, TenantKey, ConversationID, ConversationTitle string BaseID, SheetID, ViewID, OperatorID string CreatedByStaffID string + FieldMapping map[string]string + ValidationCode string + Activate bool } type BootstrapCandidate struct { TenantKey, Code, StaffID, UnionID, DisplayName, Department string diff --git a/internal/store/postgres/settings.go b/internal/store/postgres/settings.go index 73d74ed..956e5f2 100644 --- a/internal/store/postgres/settings.go +++ b/internal/store/postgres/settings.go @@ -62,7 +62,7 @@ func (r *SettingsRepository) DeleteSettingSession(ctx context.Context, tenant, s return err } func (r *SettingsRepository) SaveBinding(ctx context.Context, b settings.ResourceBinding) error { - payload, err := json.Marshal(map[string]string{"conversation_id": b.ConversationID, "conversation_title": b.ConversationTitle, "base_id": b.BaseID, "sheet_id": b.SheetID, "view_id": b.ViewID, "operator_id": b.OperatorID}) + payload, err := json.Marshal(map[string]interface{}{"conversation_id": b.ConversationID, "conversation_title": b.ConversationTitle, "base_id": b.BaseID, "sheet_id": b.SheetID, "view_id": b.ViewID, "operator_id": b.OperatorID, "field_mapping": b.FieldMapping}) if err != nil { return err } @@ -75,10 +75,20 @@ func (r *SettingsRepository) SaveBinding(ctx context.Context, b settings.Resourc if err = tx.QueryRow(ctx, `SELECT COALESCE(MAX(revision),0)+1 FROM system_resource_bindings WHERE tenant_key=$1 AND kind=$2`, b.TenantKey, b.Kind).Scan(&rev); err != nil { return err } - if _, err = tx.Exec(ctx, `UPDATE system_resource_bindings SET state='retired' WHERE tenant_key=$1 AND kind=$2 AND state='active'`, b.TenantKey, b.Kind); err != nil { - return err + state := "candidate" + var validatedAt, activatedAt interface{} + if b.Activate { + state = "active" + validatedAt, activatedAt = time.Now(), time.Now() + if _, err = tx.Exec(ctx, `UPDATE system_resource_bindings SET state='retired' WHERE tenant_key=$1 AND kind=$2 AND state='active'`, b.TenantKey, b.Kind); err != nil { + return err + } } - _, err = tx.Exec(ctx, `INSERT INTO system_resource_bindings(id,tenant_key,kind,revision,state,public_config,validation_code,validated_at,activated_at,created_by_staff_id) VALUES($1,$2,$3,$4,'active',$5,'pending_external_probe',NOW(),NOW(),$6)`, newSettingsID(), b.TenantKey, b.Kind, rev, payload, b.CreatedByStaffID) + validationCode := strings.TrimSpace(b.ValidationCode) + if validationCode == "" { + validationCode = "pending_external_probe" + } + _, err = tx.Exec(ctx, `INSERT INTO system_resource_bindings(id,tenant_key,kind,revision,state,public_config,validation_code,validated_at,activated_at,created_by_staff_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, newSettingsID(), b.TenantKey, b.Kind, rev, state, payload, validationCode, validatedAt, activatedAt, b.CreatedByStaffID) if err != nil { return err } @@ -208,11 +218,19 @@ func (r *SettingsRepository) ActiveBindings(ctx context.Context, tenant string) if err = rows.Scan(&kind, &raw); err != nil { return nil, err } - var v map[string]string + var v struct { + ConversationID string `json:"conversation_id"` + ConversationTitle string `json:"conversation_title"` + BaseID string `json:"base_id"` + SheetID string `json:"sheet_id"` + ViewID string `json:"view_id"` + OperatorID string `json:"operator_id"` + FieldMapping map[string]string `json:"field_mapping"` + } if json.Unmarshal(raw, &v) != nil { continue } - out[kind] = settings.ResourceBinding{Kind: kind, TenantKey: tenant, ConversationID: v["conversation_id"], ConversationTitle: v["conversation_title"], BaseID: v["base_id"], SheetID: v["sheet_id"], ViewID: v["view_id"], OperatorID: v["operator_id"]} + out[kind] = settings.ResourceBinding{Kind: kind, TenantKey: tenant, ConversationID: v.ConversationID, ConversationTitle: v.ConversationTitle, BaseID: v.BaseID, SheetID: v.SheetID, ViewID: v.ViewID, OperatorID: v.OperatorID, FieldMapping: v.FieldMapping, Activate: true} } return out, rows.Err() }