feat: validate and activate AI table bindings

This commit is contained in:
姚章浩
2026-08-13 13:14:06 +08:00
parent a7432aed49
commit 0fc64ba77d
11 changed files with 362 additions and 39 deletions
+53 -18
View File
@@ -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 {