diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ddd886e5..c67b924ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,27 @@ Platform releases use `vX.Y.Z` tags and publish `ghcr.io/tencent/loopforge`. LoopForge Local packages use `local-vX.Y.Z` tags and are published by manually running the `release-local` workflow. +## Unreleased — enterprise IM channels (Feishu and DingTalk chats, proactive notifications, data retention) + +- Feishu and DingTalk notifications of waiting or failed nodes are action cards when the Bot has chat enabled: approve, retry, rework or pass directly in the chat, with the same checks as WeCom cards; the result comes back as a private message. Feishu needs the `card.action.trigger` callback on the long connection; DingTalk needs an interactive card template ID in the Bot settings (empty keeps text notifications). +- Feishu and DingTalk Bots answer in group chats when @-mentioned (Feishu needs the “read @-bot messages in groups” permission); other group messages are ignored. Each group has its own shared context and every message runs as the sender's bound account. +- Feishu write confirmations are a card with “确认执行 / 确认并启动 SOP / 取消” buttons instead of a bare link; only the requester can use them and the result arrives as a private message. The Bot form labels the model field “回答模型” and keeps “默认” selectable. +- Requirements created from chat show the SOP they will be bound to, and the confirmation page lets the user choose another SOP of the project and start it right after creation. +- **Chat Bot data now has a retention period.** An hourly cleanup removes the text of finished conversation turns, notification deliveries and web-confirmation drafts, and the native CLI logs of Bot turns, 90 days after they finished, and deletes the finished records after 180 days. This also applies to existing WeCom conversation and notification records on the first start after upgrading. Records still waiting for someone (in progress, ready to send, delivery unknown) are kept. Set `AGENTFLOW_BOT_BODY_RETENTION_DAYS` and `AGENTFLOW_BOT_RECORD_RETENTION_DAYS` (0 keeps forever) before enabling the Bot workers if you need longer. Records whose text was removed show “正文已按保留期清理” and cannot be sent again. See `docs/im-channels.md` sections 9 and 10. +- Feishu and DingTalk Bots send SOP notifications (completed, failed, waiting) as private messages to one member who bound an account in a private chat with the Bot; notification-only configurations (chat disabled) work too. Saving the recipient first sends a trial message and only a confirmed delivery is accepted; unbinding clears it. Feishu unknown outcomes are retried with the same uuid for 55 minutes, DingTalk ones stop at 送达待核对, where managers can confirm receipt or send again. Switching the recipient with queued notifications asks whether to move or cancel them. See `docs/im-channels.md` section 8. +- A DingTalk reply whose session webhook expired can be sent to the original sender as a proactive private message from the record page, after that member passed a trial send. +- WeCom Bots gain the 网页确认 write mode: writes come back as a summary and a confirmation link, the bound user confirms on the platform page, and a draft runs at most once. Feishu and DingTalk Bots can use the same mode (they cannot write automatically), so requirements can be created and SOPs dispatched or started from a private chat after a confirmation on the platform page. +- DingTalk enterprise internal applications can be added as Bots over Stream mode: private text chats, account binding and read-only queries, replies through each message's session webhook. The administrator enters the enterprise CorpId; the first matching message confirms it automatically, another enterprise stops processing as a conflict. The Stream connection is implemented by the platform (only data types of `open-dingtalk/dingtalk-stream-sdk-go` v0.9.1 are used) because the SDK's client can crash the process after a heartbeat timeout. Session webhooks are stored encrypted and cleared when used or expired; replies whose webhook expired stay on the record page as 会话回复授权已过期. See `docs/im-channels.md` section 4A. + +- Feishu self-built applications can be added as Bots: private text chats, account binding and read-only project queries over the official long connection (`larksuite/oapi-sdk-go` v3.13.0). The first message from a new application records its enterprise; a Bot manager confirms it before any request is processed, and messages from other enterprises are dropped. See `docs/im-channels.md`. +- **New WeCom Bots default to read-only.** A Bot created from now on answers queries but does not write; writes are refused with a pointer to the platform page. Existing WeCom Bots keep automatic execution. Managers choose the mode under 执行权限. +- **WeCom fallback send changed.** A reply whose stream fails is sent again as a proactive message only when the stream provably did not go out (stream unavailable, request never written, or rejected by WeCom). A stream that may have been delivered but had no receipt is no longer sent twice: the record shows 送达待核对, and a manager chooses 确认已收到 or 再次发送 (a new delivery that may duplicate) on the record page. +- **Disabling a Bot keeps generated replies.** Queued and running turns are cancelled, replies already generated wait and are sent after the Bot is enabled again, and a send in progress becomes 送达待核对. Deleting a Bot cancels unsent work, moves in-flight sends to 待核对, and revokes the Bot's chat bindings. +- Bot secrets are encrypted with AES-256-GCM using `AGENTFLOW_CREDENTIAL_KEYS`. Compose installations get a generated key in `.env` (added to existing `.env` files on the next `./start.sh`), and `compose.yaml` now passes `AGENTFLOW_CREDENTIAL_KEYS`, `AGENTFLOW_PUBLIC_URL` and `AGENTFLOW_BOT_WORKERS_ENABLED` to the platform container. Back up `.env` together with the database. Existing plaintext secrets stay readable; `agentflow -credential-action status|reencrypt` reports and migrates them. Without a key, saving a new secret returns `credential_key_missing`. +- **Bot chats accept a plain natural-language answer.** Before, a Bot turn could only end through `submit_assistant_reply`, which refuses replies without drafts, so plain query answers ran the model three times and failed with 助手未提交回复. Now plain text from a successful run is the reply; protocol JSON, progress-only text, empty output and failed runs still go through the terminal retry. Writes still need drafts or the tool gateway. Applies to WeCom and Feishu Bots, not to the web assistant or SOP nodes. +- Receiving leases, send windows and retries are judged on the database clock with an epoch per owner, so a replica that lost its lease can no longer overwrite a reply or send it twice. Notification deliveries gain the state `send_unknown`. +- Upgrading needs a maintenance window: stop every old replica, start the new version once with `AGENTFLOW_BOT_WORKERS_ENABLED=false`, run `migrations/036_enterprise_im_channels.sql` and `migrations/037_bot_binding_ownership.sql`, run `-credential-action reencrypt`, then restart with workers enabled. Bots not backfilled by 036 do not receive messages. + ## 0.4.0 — scheduled task history, an MR node in delivery SOPs and agent CLIs in conversation workspaces - Scheduled tasks keep an execution history with a session mode, keep conversations per job and gain row actions (#958). AI jobs run with the key of the user who saved them and fall back to the shared key (#966). diff --git a/cmd/agentflow/credentials.go b/cmd/agentflow/credentials.go new file mode 100644 index 000000000..039d8a599 --- /dev/null +++ b/cmd/agentflow/credentials.go @@ -0,0 +1,39 @@ +package main + +import ( + "agentflow/internal/repo" + "errors" + "fmt" + "io" +) + +// runCredentialAction implements -credential-action. status counts plaintext +// and sealed Bot credentials per key id; reencrypt seals plaintext and +// rewraps old key ids with the current key, one row at a time. Only counts +// and key ids are printed, never secret values. reencrypt is re-entrant. +func runCredentialAction(s *repo.Store, action string, out io.Writer) error { + var report repo.CredentialReport + var err error + switch action { + case "status": + report, err = s.BotCredentialStatus() + case "reencrypt": + report, err = s.ReencryptBotCredentials() + default: + return errors.New("-credential-action must be status or reencrypt") + } + if err != nil && !errors.Is(err, repo.ErrCredentialKeyMissing) { + return err + } + fmt.Fprintf(out, "bot credentials %s: %s\n", action, report) + for _, failure := range report.Failed { + fmt.Fprintf(out, "failed %s\n", failure) + } + if err != nil { + return fmt.Errorf("AGENTFLOW_CREDENTIAL_KEYS is required for reencrypt: %w", err) + } + if len(report.Failed) > 0 { + return fmt.Errorf("%d credential(s) failed", len(report.Failed)) + } + return nil +} diff --git a/cmd/agentflow/main.go b/cmd/agentflow/main.go index 88f6f0a31..860043211 100644 --- a/cmd/agentflow/main.go +++ b/cmd/agentflow/main.go @@ -17,6 +17,7 @@ import ( "time" "agentflow/internal/config" + "agentflow/internal/credcrypt" "agentflow/internal/engine" "agentflow/internal/httpapi" "agentflow/internal/managedcli" @@ -95,6 +96,7 @@ func main() { cliProvider := flag.String("cli-provider", "", "provider for CLI rollback: codex, codebuddy, or claude-code") cliTimeout := flag.Duration("cli-timeout", 80*time.Minute, "total managed CLI operation timeout") browserImage := flag.Bool("browser-image", false, "build or pull the agent browser sidecar image, print its name and exit") + credentialAction := flag.String("credential-action", "", "Bot credential administration: status or reencrypt; exits without starting services") flag.Parse() loadDotEnv() if *browserImage { @@ -107,7 +109,7 @@ func main() { fmt.Println(image) return } - if os.Getenv("AGENTFLOW_SESSION_SECRET") == "" && *cliAction == "" { + if os.Getenv("AGENTFLOW_SESSION_SECRET") == "" && *cliAction == "" && *credentialAction == "" { log.Fatal("AGENTFLOW_SESSION_SECRET is required; generate with openssl rand -hex 32") } if os.Getenv("AGENTFLOW_AUTH_REQUIRED") == "" { @@ -137,6 +139,22 @@ func main() { if err != nil { log.Fatal(err) } + keyring, err := credcrypt.FromEnv() + if err != nil { + log.Fatal(err) + } + s.SetCredentialKeyring(keyring) + if *credentialAction != "" { + if err := runCredentialAction(s, *credentialAction, os.Stdout); err != nil { + log.Fatal(err) + } + return + } + if keyring == nil { + log.Printf("%s is not set: stored plaintext Bot secrets stay readable, saving a new secret returns credential_key_missing", credcrypt.EnvKeys) + } else { + log.Printf("bot credential keyring current_kid=%s", keyring.CurrentKID()) + } log.Printf("storage driver=%v database=%v role=%s", s.StorageInfo()["driver"], s.StorageInfo()["database"], role) e := engine.New(s, runtime.NewManagerWithLimit(root, *workerConcurrency)) runtime.PrebuildIsolatedImage() diff --git a/compose.yaml b/compose.yaml index 25e5535c9..9cc4c90ea 100644 --- a/compose.yaml +++ b/compose.yaml @@ -33,6 +33,14 @@ services: AGENTFLOW_SECURE_COOKIES: ${AGENTFLOW_SECURE_COOKIES:-false} AGENTFLOW_GITHUB_CLIENT_ID: ${AGENTFLOW_GITHUB_CLIENT_ID:-} AGENTFLOW_GIT_SSH_KNOWN_HOSTS: ${AGENTFLOW_GIT_SSH_KNOWN_HOSTS:-} + # Chat Bots: keys sealing Bot secrets (scripts/configure.sh adds one), + # the address users open from chat links, the maintenance switch and + # the retention of chat text and records in days (0 keeps forever). + AGENTFLOW_CREDENTIAL_KEYS: ${AGENTFLOW_CREDENTIAL_KEYS:-} + AGENTFLOW_PUBLIC_URL: ${AGENTFLOW_PUBLIC_URL:-${LOOPFORGE_PUBLIC_URL:-}} + AGENTFLOW_BOT_WORKERS_ENABLED: ${AGENTFLOW_BOT_WORKERS_ENABLED:-true} + AGENTFLOW_BOT_BODY_RETENTION_DAYS: ${AGENTFLOW_BOT_BODY_RETENTION_DAYS:-90} + AGENTFLOW_BOT_RECORD_RETENTION_DAYS: ${AGENTFLOW_BOT_RECORD_RETENTION_DAYS:-180} AGENTFLOW_ASSISTANT_SESSION_DIR: /data/assistant AGENTFLOW_CONVERSATION_DIR: ${LOOPFORGE_DATA_DIR}/conversations AGENTFLOW_CLARIFY_SESSION_DIR: /data/clarify diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yaml index fad8b3b10..1ac092b10 100644 --- a/docs/api/openapi.yaml +++ b/docs/api/openapi.yaml @@ -3123,6 +3123,69 @@ paths: schema: {type: array, items: {type: object}} "403": description: Project membership required + post: + summary: Create a notification Bot (WeCom, Feishu or DingTalk) + description: | + `channel` is `wecom` (default for clients that omit it), `feishu` or + `dingtalk`. New Bots default to `writeApproval=readonly`; Feishu and + DingTalk accept only `readonly`. Feishu and DingTalk credentials are + verified before anything is stored. DingTalk requires `tenantKey` + (the enterprise CorpId): the first private message from that + enterprise locks it, another enterprise moves the Bot to + `tenantState=conflict`. Secrets are sealed with + AGENTFLOW_CREDENTIAL_KEYS. + operationId: createNotificationBot + requestBody: + required: true + content: + application/json: + schema: {$ref: "#/components/schemas/NotificationBotInput"} + responses: + "200": + description: Stored Bot, without credentials + "400": + description: Invalid input, `credential_invalid`, `tenant_required` or `immutable_field` + "409": + description: "`receive_slot_taken` (application already receives in another configuration) or `channel_not_ready`" + "503": + description: "`credential_key_missing` or `bot_maintenance`" + /api/notification-bots/{id}: + put: + summary: Update a notification Bot + description: | + Channel, project and (for Feishu) the App ID are immutable. Send the + `configVersion` read from the Bot; a concurrent change returns + `409 config_changed`. + operationId: updateNotificationBot + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + requestBody: + required: true + content: + application/json: + schema: {$ref: "#/components/schemas/NotificationBotInput"} + responses: + "200": + description: Stored Bot + "400": + description: Invalid input or `immutable_field` + "409": + description: "`config_changed`, `receive_slot_taken` or `channel_not_ready`" + "503": + description: "`credential_key_missing` or `bot_maintenance`" + delete: + summary: Delete a notification Bot + description: | + Cancels unsent work, moves in-flight sends to `reply_unknown` / + `send_unknown`, and revokes the configuration's chat bindings. + operationId: deleteNotificationBot + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + responses: + "200": + description: Deleted + "503": + description: "`bot_maintenance`" /api/notification-bots/{id}/interactions: get: summary: Inspect Bot connection and recent chat interactions @@ -3134,12 +3197,247 @@ paths: schema: {type: string} responses: "200": - description: Connection status, recent conversations and messages; credentials excluded + description: Connection status, recent conversations and messages; credentials excluded. Messages whose text was removed by the retention cleanup carry `bodyPurgedAt` and empty `content`/`reply`; they cannot be sent again. DingTalk messages carry `pushResendAvailable`. "403": description: Bot management permission required + /api/notification-bots/{id}/tenant: + get: + summary: Read the enterprise confirmation state of a Feishu Bot + description: | + Returns `tenantState` (`awaiting_confirmation`, `locked`, `conflict`), + the candidate observed on the first trusted event, `configVersion`, + and the platform's enterprise query (`queried`) or `queryError`. + operationId: getNotificationBotTenant + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + responses: + "200": + description: Confirmation state + "409": + description: "`channel_not_ready` (WeCom has no enterprise confirmation)" + /api/notification-bots/{id}/tenant/confirm: + post: + summary: Lock the candidate enterprise scope, or restart discovery + operationId: confirmNotificationBotTenant + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [configVersion] + properties: + tenantKey: {type: string} + configVersion: {type: integer} + rediscover: {type: boolean} + responses: + "200": + description: Stored Bot with `tenantState=locked` (or `awaiting_confirmation` after rediscover) + "403": + description: Same-origin browser request and Bot management permission required + "409": + description: "`config_changed` or `tenant_candidate_mismatch`" + /api/bot-interactions/pending: + get: + summary: List a project's chat replies awaiting manual review + description: Includes records of deleted Bots for project managers. + operationId: listPendingBotInteractions + parameters: + - {name: projectId, in: query, required: true, schema: {type: string}} + responses: + "200": + description: Interactions in `reply_unknown` or `reply_failed` + "403": + description: Bot creator or project maintainer required + /api/bot-interactions/{interactionId}/confirm: + post: + summary: Mark a reply_unknown reply as received + operationId: confirmBotInteraction + parameters: + - {name: interactionId, in: path, required: true, schema: {type: string}} + requestBody: + required: true + content: + application/json: + schema: {$ref: "#/components/schemas/BotManualAction"} + responses: + "200": + description: Interaction now `replied` with `receiptSource=manual`; a repeated actionKey returns the same result + "409": + description: "`state_conflict`" + /api/bot-interactions/{interactionId}/resend: + post: + summary: Send a saved reply again as a new delivery generation + description: Accepts `reply_unknown` or `reply_failed`; the recipient may receive a duplicate. + operationId: resendBotInteraction + parameters: + - {name: interactionId, in: path, required: true, schema: {type: string}} + requestBody: + required: true + content: + application/json: + schema: {$ref: "#/components/schemas/BotManualAction"} + responses: + "200": + description: Interaction back in `ready` with `deliveryGeneration` + 1; a repeated actionKey returns the same generation + "409": + description: "`bot_deleted`, `route_expired` or `state_conflict`" + /api/bot-interactions/{interactionId}/push-resend: + post: + summary: Send an expired DingTalk reply as a proactive private message + description: Accepts `reply_failed` with `terminalReason=route_expired` on a DingTalk Bot. The original sender must still be bound to the same platform user, keep project permission and have passed a trial proactive send. The saved reply is sent again as a new generation with a new key. + operationId: pushResendBotInteraction + parameters: + - {name: interactionId, in: path, required: true, schema: {type: string}} + requestBody: + required: true + content: + application/json: + schema: {$ref: "#/components/schemas/BotManualAction"} + responses: + "200": + description: Interaction back in `ready` with `sendRoute=push` and `deliveryGeneration` + 1; a repeated actionKey returns the same result + "409": + description: "`push_target_unverified`, `bot_deleted` or `state_conflict`" + /api/notification-bots/{id}/targets/verify: + post: + summary: Send a trial proactive message to a bound member (Feishu, DingTalk) + description: Records `targetVerifiedAt` on the member's binding when the platform confirms delivery. Does not change the notification recipient. + operationId: verifyBotPushTarget + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [targetId] + properties: + targetId: {type: string, description: Sender ID of the bound member (Feishu open_id, DingTalk staffId)} + responses: + "200": + description: Verified target + "409": + description: "`push_target_unverified` or `push_target_unavailable`" + "502": + description: "`push_trial_failed`; `outcome` is `rejected`, `not_delivered` or `unknown`" + /api/notification-bots/{id}/sessions: + get: + summary: List selectable notification targets + description: WeCom lists chats from wecom-cli. Feishu and DingTalk list members bound in a private chat with this Bot (after the enterprise is confirmed); `verifiedAt` marks a successful trial send. + operationId: listNotificationBotSessions + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + responses: + "200": + description: "`{sessions, total}`" + "409": + description: "`push_target_unavailable` (enterprise not confirmed)" + /api/notification-bots/{id}/test: + post: + summary: Send a test notification to the saved target + description: For Feishu and DingTalk this repeats the trial send and refreshes the target verification. + operationId: testNotificationBot + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + responses: + "200": + description: Test sent + "409": + description: "`push_target_unavailable`" + "502": + description: "`push_trial_failed`" + /api/notification-deliveries: + get: + summary: List notification deliveries of a project + description: Content is never returned. `contentSource=purged` marks deliveries whose rendered text was removed by the retention cleanup; they cannot be retried. + operationId: listNotificationDeliveries + parameters: + - {name: projectId, in: query, required: true, schema: {type: string}} + responses: + "200": + description: Deliveries, newest first + /api/notification-deliveries/{deliveryId}/retry: + post: + summary: Send a failed, unauthorized, unavailable-target or send_unknown delivery again + description: Starts a new delivery generation with a new send key. Deliveries with invalid or purged content are refused. + operationId: retryNotificationDelivery + parameters: + - {name: deliveryId, in: path, required: true, schema: {type: string}} + - {name: projectId, in: query, required: true, schema: {type: string}} + responses: + "200": + description: Delivery back in `pending` + "409": + description: The delivery cannot be retried + /api/notification-deliveries/{deliveryId}/confirm: + post: + summary: Mark a send_unknown notification as received + operationId: confirmNotificationDelivery + parameters: + - {name: deliveryId, in: path, required: true, schema: {type: string}} + - {name: projectId, in: query, required: true, schema: {type: string}} + responses: + "200": + description: "`{\"ok\": true}`; delivery now `sent` with `receiptSource=manual`" + "409": + description: Only `send_unknown` deliveries can be confirmed + /api/bot-drafts/{id}: + get: + summary: Read a Bot write draft awaiting confirmation + description: Only the platform user bound to the chat sender can read it; other users get 404. Status is `pending`, `executing`, `executed`, `failed`, `needs_review`, `expired` or `cancelled`. Pending drafts list `newIssues` (index, title, workflowId) and the selectable `workflows` of the project. + operationId: getBotDraft + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + responses: + "200": + description: Draft summary, operations and results + "404": + description: Unknown draft or another user's draft + /api/bot-drafts/{id}/confirm: + post: + summary: Confirm and execute a Bot write draft + description: Re-checks the Bot (enabled, chat on, confirm mode), the sender binding, project permission, expiry and the frozen parameters, then moves the draft from `pending` to `executing` once. Any other state returns the current draft without executing. `choices` set the SOP of new requirements (only SOPs listed in the draft's `workflows`) and whether to start it after creation; results report `已启动` or `未启动`. + operationId: confirmBotDraft + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + requestBody: + required: false + content: + application/json: + schema: + type: object + properties: + choices: + type: array + items: + type: object + properties: + index: {type: integer, description: Position of the draft in the operation list} + workflowId: {type: string} + start: {type: boolean} + responses: + "200": + description: Draft after the confirmation (or its current state) + "403": + description: Cross-origin request + "409": + description: "`draft_invalidated`; the draft was cancelled and `draft` carries the reason" + /api/bot-drafts/{id}/cancel: + post: + summary: Cancel a pending WeCom Bot write draft + operationId: cancelBotDraft + parameters: + - {name: id, in: path, required: true, schema: {type: string}} + responses: + "200": + description: Draft in its current state /api/bot-bindings: get: - summary: List the logged-in user's WeCom identity bindings + summary: List the logged-in user's chat identity bindings (WeCom, Feishu) operationId: listBotUserBindings responses: "200": @@ -3148,7 +3446,7 @@ paths: description: Interactive browser login required /api/bot-bindings/{id}: delete: - summary: Revoke the logged-in user's WeCom identity binding + summary: Revoke the logged-in user's chat identity binding operationId: revokeBotUserBinding parameters: - name: id @@ -3166,7 +3464,7 @@ paths: description: Binding does not belong to the current user /api/bot-bindings/claim: post: - summary: Nominate the logged-in account; original WeCom sender must confirm + summary: Nominate the logged-in account; the original chat sender must confirm operationId: claimBotUserBinding requestBody: required: true @@ -3179,7 +3477,7 @@ paths: token: {type: string, minLength: 64, maxLength: 64} responses: "200": - description: Login name, status and browser-only confirmation command + description: Login name, status, browser-only confirmation command, and channel, Bot, project and masked sender of the link "400": description: Expired, invalid or already claimed challenge "401": @@ -3979,6 +4277,28 @@ components: items: $ref: "#/components/schemas/ScheduleJob" schemas: + NotificationBotInput: + type: object + required: [name, botId] + properties: + projectId: {type: string} + name: {type: string, maxLength: 80} + channel: {type: string, enum: [wecom, feishu, dingtalk]} + botId: {type: string, description: "WeCom Bot ID, Feishu App ID or DingTalk Client ID", maxLength: 191} + secret: {type: string, description: "Empty keeps the stored secret", maxLength: 512} + enabled: {type: boolean} + interactionEnabled: {type: boolean} + model: {type: string} + writeApproval: {type: string, enum: [readonly, auto], description: "auto is WeCom only"} + targetId: {type: string, description: "WeCom notification target"} + configVersion: {type: integer, description: "Expected version for updates"} + tenantKey: {type: string, maxLength: 191, description: "DingTalk enterprise CorpId; required on create, changeable until the enterprise is locked"} + BotManualAction: + type: object + required: [actionKey] + properties: + actionKey: {type: string, maxLength: 64, description: "Client-generated key; a repeated key returns the same result"} + reason: {type: string, maxLength: 60} OpenObject: type: object additionalProperties: true diff --git a/docs/deployment.md b/docs/deployment.md index 0d1c79995..e9a8eefe3 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -108,6 +108,20 @@ Repositories automatically use the current user's key for their SSH host and por SSH handles clone, fetch and push. A conversation delivery returns the branch and commit SHA. You can optionally add a repository web URL to open the host and create a PR/MR manually. Issue import and PR/MR automation need a hosting API credential. To automate those actions, add a hosting-specific MCP in **Settings → Assets → MCP**, give it its own scoped token, and attach it to the relevant agent or workflow. [CNB does not support Git SSH](https://docs.cnb.cool/en/guide/git-access.html); it uses HTTPS with a CNB access token, which is outside this integration. +## Chat Bots (WeCom, Feishu, DingTalk) + +Chat Bots are configured per project on the Bot page. The deployment provides: + +| Variable | Default | Meaning | +| --- | --- | --- | +| `AGENTFLOW_CREDENTIAL_KEYS` | generated by `scripts/configure.sh` | `kid:base64(32 bytes)[,…]`; the first key seals new Bot secrets. Every replica needs the same value. Back it up with the database: without it saved secrets cannot be decrypted. | +| `AGENTFLOW_PUBLIC_URL` | `LOOPFORGE_PUBLIC_URL` | Address chat users open for account binding, run pages and WeCom write confirmation. Must be reachable from their browsers. | +| `AGENTFLOW_BOT_WORKERS_ENABLED` | `true` | `false` during an upgrade window: no receiving connections, no notification delivery, Bot configuration writes return `503 bot_maintenance`. | +| `AGENTFLOW_BOT_BODY_RETENTION_DAYS` | `90` | Days after a finished conversation turn, notification or write draft until its text and the native CLI log of the turn are removed. `0` keeps them. | +| `AGENTFLOW_BOT_RECORD_RETENTION_DAYS` | `180` | Days until the finished records themselves are deleted. `0` keeps them; a value below the body retention is raised to it. | + +The retention cleanup runs hourly in each process that runs the Bot workers and never touches records still waiting for someone (in progress, ready to send or delivery unknown). Native CLI logs of Bot turns live in the CLI home under `AGENTFLOW_ASSISTANT_SESSION_DIR`; with several API replicas that directory must be shared, otherwise a replica that did not run a turn records its log as missing. Channel setup, upgrade and rollback steps are in the [enterprise IM manual](im-channels.md) (Chinese). + ## Accounts The first account created in the browser becomes the platform administrator. After that, anyone who can reach the login page may sign up as a regular user while `AGENTFLOW_ALLOW_SIGNUP` is `true` (the default). Registered users can create requirements and run SOPs on your workers, so set `AGENTFLOW_ALLOW_SIGNUP=false` for any installation reachable from untrusted networks and create accounts explicitly instead. diff --git a/docs/im-channels.md b/docs/im-channels.md new file mode 100644 index 000000000..9e50dc753 --- /dev/null +++ b/docs/im-channels.md @@ -0,0 +1,346 @@ +# 企业 IM 渠道配置与联调手册 + +本手册对应 `docs/proposals/enterprise-im.md`,记录各渠道的配置步骤、所需权限、上线顺序和真实环境验证方法。当前可配置的渠道:**企业微信、飞书(单聊、群聊 @ Bot)、钉钉(单聊、群聊 @ Bot)**;三个渠道都支持 SOP 主动通知,写操作通过“网页确认”在平台确认后执行(企业微信另可选自动执行)。数据保留见第 9 节,升级与回退见第 2 节和第 10 节,最终测试记录见 [testing/im-final-test-record.md](testing/im-final-test-record.md)。 + +## 1. 平台侧前置条件 + +| 配置 | 说明 | +| --- | --- | +| `AGENTFLOW_PUBLIC_URL` | 用户从 IM 打开绑定页时使用的公开地址,必须是 `http(s)://host` | +| `AGENTFLOW_CREDENTIAL_KEYS` | `kid:base64(32字节)[,kid:base64(32字节)]`,第一把为当前写入密钥。所有副本配置相同的值。未配置时,已有的旧明文 Secret 仍可读取,保存新 Secret 返回 `503 credential_key_missing` | +| `AGENTFLOW_BOT_WORKERS_ENABLED` | 默认 `true`。维护窗口设为 `false`:对话接收、通知发送和数据保留清理都不启动,Bot 配置写接口返回 `503 bot_maintenance` | +| `AGENTFLOW_BOT_BODY_RETENTION_DAYS` | 默认 `90`。已结束的对话、通知、网页确认草稿的正文,以及 Bot 轮次的原生 CLI 日志,在结束这么多天后清除。`0` 表示不清除 | +| `AGENTFLOW_BOT_RECORD_RETENTION_DAYS` | 默认 `180`。已结束的记录本身在这么多天后删除。`0` 表示不删除;小于正文保留期时按正文保留期处理 | + +**Compose 部署**(`./start.sh`、`./start.sh dev`、`scripts/install.sh`):`scripts/configure.sh` 会在 `.env` 中生成 `AGENTFLOW_CREDENTIAL_KEYS`,旧的 `.env` 缺少这一项时也会自动补上;`compose.yaml` 会把它、`AGENTFLOW_PUBLIC_URL`(未设置时使用 `LOOPFORGE_PUBLIC_URL`)和 `AGENTFLOW_BOT_WORKERS_ENABLED` 传给平台容器。**`.env` 里的密钥要和数据库备份一起保管**,密钥丢失后已保存的 Secret 无法解密,只能重新填写。确认生效:平台启动日志中有 `bot credential keyring current_kid=...`;如果日志显示 `AGENTFLOW_CREDENTIAL_KEYS is not set`,说明容器没有拿到这个变量。 + +生成一把密钥: + +```bash +echo "k$(date +%Y%m):$(openssl rand -base64 32)" +``` + +凭据运维子命令(不启动服务,只输出计数和 kid,不输出 Secret): + +```bash +agentflow -credential-action status +agentflow -credential-action reencrypt # 可重复执行 +``` + +轮换密钥:把新密钥放在列表第一位,保留旧密钥,重启所有副本后执行 `reencrypt`;`status` 显示旧 kid 计数为 0 后,再从列表中移除旧密钥。 + +## 2. 升级维护窗口 + +1. 备份数据库和当前二进制。 +2. **停止全部旧版本副本。** 旧版本不认识 `AGENTFLOW_BOT_WORKERS_ENABLED`,不能依赖它暂停。 +3. 以 `AGENTFLOW_BOT_WORKERS_ENABLED=false` 启动一个新版本实例。启动时的增量 DDL 会补齐新列、索引和 `bot_native_sessions` 表。 +4. 执行 `migrations/036_enterprise_im_channels.sql`: + - 第 1 段检查所有新列 `present = 1`; + - 第 2 段列出会共用接收占位的重复启用配置,先逐一处理(停用或删除重复项); + - 第 3、4 段回填物理列和交互的项目快照,可重复执行; + - 第 5 段记录各渠道配置数和未回填数,`not_backfilled` 必须为 0。 +5. 执行 `migrations/037_bot_binding_ownership.sql`:第 1、2 段列出多配置共用和孤立的企微绑定,第 3 段回填唯一归属,第 4 段列出历史 Bot 原生会话(`legacy_unmapped`,只供核查,不删除)。 +6. 配置 `AGENTFLOW_CREDENTIAL_KEYS`,执行 `agentflow -credential-action reencrypt`,再用 `status` 确认 `plaintext=0`。 +7. **确认数据保留设置。** 开启 worker 后的第一轮清理会立刻处理升级前的历史数据:默认清除 90 天前已结束记录的正文,删除 180 天前已结束的记录,包括企业微信已有的对话记录和发送记录。需要保留更久,先在 `.env` 设置第 9 节的两个变量(或设为 `0` 暂不清理),再进行下一步。数据库备份是唯一的恢复途径。 +8. 以 `AGENTFLOW_BOT_WORKERS_ENABLED=true` 重启全部新版本副本。先验证企微,再依次启用飞书、钉钉和主动通知。启动日志出现 `bot retention: …` 一行,列出本轮清理的数量;没有可清理的数据时不输出。 + +未执行 036 的配置(`config_version = 0`)不会建立接收连接,启动日志会提示数量。 + +阶段 C 之后的版本(G、H 等)没有新的手动脚本:启动时的增量 DDL 补齐 `bot_pending_drafts` 表和新增列。从任一预发版本(阶段 B 到 G)升级到最终版本,只需停旧副本、启动新版本,并按第 7 步确认保留设置。 + +**数据库版本**:036/037 已在 MySQL 8.4.13(默认 `sql_mode` 含 `ONLY_FULL_GROUP_BY`)和 MariaDB 10.11 上各连续执行两次,结果一致。标识比较统一用 `BINARY`,避免 `information_schema` 与业务表排序规则不同时报 1267。 + +**回退**:完整的回退矩阵见第 10 节。执行第 6 步(`reencrypt`)之后,库里的 Secret 是 `lf1:` 密文,旧版本会把密文当作 Secret 使用,企微连接无法鉴权。旧版本仍能启动并读出配置,但它不认识租约 epoch,会与新版本按两套协议争抢连接。因此回退必须:停止全部新版本副本 → 恢复第 1 步的备份 → 启动旧版本。维护窗口结束后新产生的对话和配置需要按停机时间段人工核对。预发演练结果见 [R1 验收记录](testing/im-r1-acceptance.md#3-预发升级与回退演练)。 + +## 3. 企业微信 + +配置方式与升级前相同。变化: + +- 新建的企微 Bot 默认“只读查询”;已有 Bot 保持“自动执行”。可在第①步“执行权限”中切换。 +- 阶段 G 新增“网页确认”:需要写平台数据时,Bot 不直接执行,而是回复操作摘要和确认链接(`/bot-approve.html#draft=…`,30 分钟内有效),见第 8.3 节。 +- 流式回复失败后的补发,只在确认未发出(流式不可用、请求未写出、平台明确拒绝)时进行一次;已写出但未收到回执的,记录为“送达待核对”,在记录页“确认已收到”或“再次发送”。 + +## 4. 飞书(企业自建应用) + +### 4.1 开放平台应用配置 + +1. 在飞书开放平台创建**企业自建应用**,记下 App ID(`cli_` 开头)和 App Secret。 +2. 添加应用能力:**机器人**。 +3. 事件与回调 → 订阅方式选择**使用长连接接收事件**,添加事件 `im.message.receive_v1`(接收消息)。要在通知卡片上直接审批,在“回调配置”中同样选择长连接,并添加回调 `card.action.trigger`(卡片回传交互),见第 8.5 节。 +4. 权限管理中开通下表权限,创建版本并发布,可用范围设为试点成员。 + +| 权限(以开放平台权限列表的显示名称为准) | 用途 | 是否必需 | +| --- | --- | --- | +| 读取用户发给机器人的单聊消息 | 接收私聊消息事件 | 必需 | +| 以应用的身份发消息 | 原消息回复(Message Reply);阶段 G 的主动通知(向绑定成员的 open_id 发单聊消息)也用这项权限 | 必需 | +| 获取企业信息 | 企业确认页展示企业名称和 tenant_key,与事件中的候选值对照 | 可选 | +| 获取群组中用户@机器人消息 | 群聊中 @ Bot 提问(2026-11-20 起) | 用群聊时必需 | + +**不要开通“获取群组中所有消息”。** 平台只处理 @ 了机器人的群消息(按事件里 `mentions` 的 `mentioned_type=bot` 判断,旧事件没有这个字段时有任何 @ 即处理),开通后机器人会收到大量无关消息。 + +**最小权限即前两项。** “获取企业信息”只影响确认页的辅助展示:未开通时,确认页显示查询失败原因,企业确认仍按“首个可信事件发现 → 管理员确认”完成。权限的精确名称和 scope 标识以应用后台为准,联调时在本表补记实际值。 + +### 4.2 平台配置 + +1. 项目 → 机器人 → 添加 Bot,渠道选择“飞书”,填写名称、App ID、App Secret,开启“启用此 Bot”和“启用对话”。保存时平台会用 App ID / Secret 获取 tenant_access_token 校验凭据,失败则不保存。 +2. 进入第②步“企业确认”。此时状态为“企业待确认”。 +3. 让一名试点成员在飞书里给机器人发一条单聊消息。机器人回复“机器人正在完成企业确认,请稍后重发消息。”,确认页出现候选企业标识(并显示平台查询结果是否一致)。 +4. Bot 管理员点击“确认企业”。状态变为“企业已确认”,之后只处理该企业的消息。确认不会断开连接。 +5. 如果出现“企业冲突”(收到了两个不同企业的消息),核实应用发布范围后点击“重新发现”。 + +飞书 Bot 的“执行权限”可选“只读查询”(默认)或“网页确认”。只读时,写请求会提示到平台页面操作,模型的写草稿不会执行,外部 MCP 写调用和工作区工具被拒绝。网页确认时,创建需求、派发或启动 SOP 等写操作先生成确认卡片(飞书)或确认链接(钉钉),发送者本人确认后才执行,见第 8.3 节。飞书、钉钉不提供“自动执行”。 + +**回答模型**:第①步“回答模型(CodeBuddy)”决定 Bot 回答对话和生成完成汇报用的模型,新建时即可选择,默认是平台默认模型,之后可随时更换。 + +**群聊**:把机器人拉进群后,成员 @ 机器人提问即可,不 @ 的群消息不处理(连接状态里计入“忽略的群消息”)。每个群一个会话上下文,群成员共享历史;每条消息按发言人自己绑定的平台账号和项目权限执行,没有绑定的成员会收到绑定链接(只有本人发回确认码才能完成绑定)。写操作的确认卡片发在群里,但只有发起人本人能确认,结果以单聊发给他。 + +### 4.3 用户绑定 + +1. 未绑定成员发消息后收到绑定链接(15 分钟有效)。有效链接存在时,再发消息会提示查看上一条链接;主动发送“绑定账号”可换新链接,每分钟最多一次,10 分钟最多三次。企业微信以 Markdown 链接展示;飞书和钉钉为纯文本消息,链接单独占一行,避免聊天客户端把后面的括号或文字识别进链接。 +2. 打开链接并登录平台,页面显示渠道、机器人、项目和脱敏后的飞书发送者,确认后得到 `/bind <确认码>`。 +3. 在**同一个飞书机器人的单聊**中发送该指令。绑定按机器人配置隔离:同一个人在另一个飞书机器人里需要重新绑定。 +4. 绑定成功后重新发送问题。发送 `/unbind` 或在个人配置中解除绑定。 + +### 4.4 送达与重试 + +- 回复使用 Message Reply,并把持久化的发送 key 作为 `uuid`。飞书对相同 `uuid` 一小时内去重。 +- 平台未返回结果时,在首次发送后 55 分钟内用同一 key 自动重试(5 秒、30 秒、2 分钟,之后每 2 分钟),超过窗口转为“送达待核对”。 +- 明确未发出(连接失败、限流、令牌失效)最多自动重试 3 次,耗尽后为“回复失败”。 +- 平台明确拒绝(例如原消息已撤回)直接为“回复失败”。 + +### 4.5 入站确认与故障恢复 + +- 收到事件后,平台在 2.5 秒内完成入库并回执。数据库慢或不可用时,到时直接回执失败(500),由飞书重投;重投的同一条消息按业务消息 ID 去重,只产生一条交互、只运行一次模型。 +- 长连接断开后,接收服务自动重连;已在执行的回复走开放平台 HTTP 接口,不受长连接影响。 +- 接收进程崩溃后,其他副本在 45 秒连接租约到期后接管:执行中的任务回复“上一轮处理已中断……”,不重跑模型;已生成未发送的回复只发送一次;发送中的回复用同一 `uuid` 重试,用户只收到一条。 +- 以上场景的多进程故障测试:`internal/botinteraction/feishu_fault_mysql_test.go`,需要一个专用空库: + +```bash +AGENTFLOW_BOT_FAULT_TEST_MYSQL_DSN='user:pass@tcp(127.0.0.1:3306)/bot_fault?parseTime=true' \ + go test -count=1 -run '^TestFeishuFaultMySQL$' -v -timeout 15m ./internal/botinteraction +``` + +## 4A. 钉钉(企业内部应用) + +### 4A.1 开发者后台配置 + +1. 在钉钉开发者后台创建**企业内部应用**,记下 Client ID(原 AppKey)和 Client Secret(原 AppSecret)。 +2. 添加应用能力:**机器人**。消息接收模式选择 **Stream 模式**(不需要公网回调地址)。 +3. 发布应用,可用范围设为试点成员。 +4. 记下企业的 **CorpId**(`ding` 开头,开发者后台或管理后台的企业信息里可以看到)。 + +原会话回复使用每条消息自带的 sessionWebhook,不需要额外的接口权限。阶段 G 的主动通知和过期回复补发调用 `POST /v1.0/robot/oToMessages/batchSend`(机器人批量发送单聊消息,`robotCode` 用 Client ID),需要在权限管理中开通“企业内机器人发送消息”相关权限。权限和字段的精确名称以开发者后台为准,联调时补记实际值;未开通时,试发会显示钉钉返回的错误码,接收人不会被保存。 + +### 4A.2 平台配置 + +1. 项目 → 通道管理 → 添加通道,渠道选“钉钉”,填写名称、Client ID、Client Secret 和**企业 CorpId**,开启“启用此 Bot”和“启用对话”。保存时平台用 Client ID / Secret 获取 accessToken 校验凭据,失败则不保存。 +2. 第②步“企业确认”显示“企业待确认”。与飞书不同,**钉钉不需要手动点确认**:首条单聊消息的企业(`chatbotCorpId`)与填写的 CorpId 一致时自动确认,这条消息也会正常处理。确认人记为填写 CorpId 的管理员。 +3. 不一致时状态变为“企业冲突”,之后的消息只回复“正在完成企业确认”的提示,不处理业务。核实后在第①步改正 CorpId 并保存,或点“重新发现”。企业确认后 CorpId 不能再改,更换企业请新建配置。 + +绑定流程和执行权限(只读查询或网页确认)与飞书相同(第 4.2、4.3 节)。发送者身份使用 `senderStaffId`;事件缺少它时丢弃并记录 `missing_staff_id`。发送者企业与机器人企业不一致的消息丢弃(`sender_corp_mismatch`)。 + +### 4A.3 连接与送达 + +- **连接**:平台自行实现 Stream 连接层,只用官方 SDK 的帧和事件数据结构(原因见 `scripts/im-sdk-probe/README.md`)。每 60 秒发一次 WebSocket ping,70 秒内没有任何数据就断开重连;收到服务端 `disconnect` 立即重连;重连由接收服务按 1 秒起的退避执行,每次重新读取凭据。 +- **入站确认**:消息入库后回执 200;数据库慢或不可用时,2.5 秒内回执 500,由钉钉重投,重投按 `msgId` 去重。 +- **会话回复**:sessionWebhook 只接受 `https://oapi.dingtalk.com/robot/sendBySession`,禁止重定向;以密文保存,绑定到这条交互,回复成功或过期后清除。 +- **过期**:发送时离 `sessionWebhookExpiredTime` 不足 10 秒,记为“回复失败 · 会话回复授权已过期”,回复内容保留在记录页,不能在原会话再次发送。 +- **重试**:明确未发出(连接失败、限流 130101、HTTP 429)最多重试 3 次。钉钉会话回复没有幂等键,结果未知(超时、5xx、系统繁忙 -1)直接记为“送达待核对”,不自动重发,由管理员在记录页确认或再次发送。 + +多进程故障测试 `internal/botinteraction/dingtalk_fault_mysql_test.go`(专用空库): + +```bash +AGENTFLOW_BOT_FAULT_TEST_MYSQL_DSN='user:pass@tcp(127.0.0.1:3306)/bot_fault_dt?parseTime=true' \ + go test -count=1 -run '^TestDingTalkFaultMySQL$' -v -timeout 15m ./internal/botinteraction +``` + +### 4A.4 R2 真实环境验收清单(钉钉私聊闭环) + +逐项结果见 [R2 验收记录](testing/im-r2-acceptance.md)。 + +## 5. 记录页人工处理 + +| 状态 | 可用操作 | +| --- | --- | +| 送达待核对(`reply_unknown`) | 确认已收到;再次发送(新 key,对方可能收到重复消息) | +| 回复失败(`reply_failed`) | 再次发送;会话回复授权过期的不能在原会话再次发送。钉钉另有“改用主动单聊发送”,见第 8.2 节 | +| 机器人已删除 | 可确认已收到,再次发送返回 `bot_deleted` | +| 通知送达待核对(`send_unknown`) | 发送记录中“确认已收到”(记为人工确认)或“再次发送”(新的一代和新 key) | + +停用 Bot 时,已生成未发送的回复暂存,重新启用后发送;正在执行的任务取消;发送中的记录转为“送达待核对”。 + +## 6. R1 真实环境验收清单(飞书私聊闭环) + +逐项实际结果、证据层级和固定验收提交见 [R1 验收记录](testing/im-r1-acceptance.md)。下表是步骤和通过标准;状态列以验收记录为准。 + +每项记录:测试企业(脱敏)、配置 ID、configVersion、消息 ID、交互 ID、平台回执、实际结果、北京时间。状态填“通过 / 失败 / 未运行 / 阻塞”。 + +| 编号 | 场景 | 步骤 | 通过标准 | 状态 | +| --- | --- | --- | --- | --- | +| R1-01 | 凭据校验 | 填错 App Secret 保存;再填正确值 | 错误时提示 `credential_invalid` 且未保存;正确后状态“企业待确认” | 未运行 | +| R1-02 | 企业确认 | 按 4.2 第 3–4 步 | 确认前只收到固定提示、不调用模型;候选值与“获取企业信息”一致(如已开通);确认后连接不断开 | 未运行 | +| R1-03 | 两阶段绑定 | 成员 A 打开链接,成员 B 在自己的单聊发送 A 的确认码;再由 A 发送 | B 的确认失败;A 绑定成功,页面显示渠道、机器人、项目、脱敏发送者 | 未运行 | +| R1-04 | 只读查询 | 普通项目成员询问项目状态、SOP 进度、本人待办 | 原消息回复,内容按该成员权限;记录页 `replied`,有平台消息 ID | 未运行 | +| R1-05 | 写请求 | 让机器人“创建一个需求” | 提示到平台页面操作,平台无新增数据 | 未运行 | +| R1-06 | 重复投递 | 处理期间让入库延迟超过 3 秒(或观察平台重投) | 同一消息只有一条交互,模型只运行一次 | 未运行 | +| R1-07 | 重启恢复 | 模型运行中重启实例;回复已生成未发送时重启 | 运行中的记录显示“执行已中断”;已生成的回复只发送一次 | 未运行 | +| R1-08 | 撤权与解绑 | 移除成员项目权限后再提问;`/unbind` 后再提问 | 下一次提问即不再执行 | 未运行 | +| R1-09 | 非文本与群聊 | 发送图片;把机器人拉进群并 @ | 图片收到“只支持文字”提示;群消息无回复,连接计数增加(R1 时的行为;2026-11-20 起群里 @ Bot 会回复,不 @ 的仍不回复,见第 4.2 节) | 未运行 | +| R1-10 | 跨企业 | 另一企业的用户(如应用对外可用)发消息 | 无回复,日志 `tenant_mismatch` | 未运行 | +| R1-11 | 送达未知 | 在回复请求发出后断开平台出口网络 | 恢复后用同一 uuid 重试,用户只收到一条;超过窗口为“送达待核对” | 未运行 | +| R1-12 | 企微回归 | 旧企微 Bot:私聊、群聊、流式进度、卡片、主动通知 | 与升级前一致;流式失败的补发行为符合第 3 节 | 未运行 | +| R1-13 | 升级迁移 | 在预发库按第 2 节执行 | 036/037 两次执行结果一致;`plaintext=0`;企微 Bot 正常接收 | 未运行 | + +## 7. 行为变更:Bot 直接采用自然语言回答 + +**变更**:Bot 对话(企业微信和飞书,只读和自动执行两种模式)在模型正常结束、没有调用终态工具时,直接采用模型输出的自然语言作为回复。 + +**原因**:终态工具 `submit_assistant_reply` 只接受写草稿,不带草稿的提交会被拒绝。升级前,Bot 回答纯查询类问题时会重跑终态步骤两次,最后以“助手未提交回复”失败。用户收到“本次处理未完成”,模型白跑三次。在未改动的基线代码上复现:同一输入运行 3 次模型后失败。 + +**采用条件**(全部满足才采用): + +| 条件 | 不满足时 | +| --- | --- | +| 本次 CLI 运行成功退出 | 按原规则重跑终态步骤或失败 | +| 输出是纯文本,不含助手协议 JSON | 重跑终态步骤(协议 JSON 代表写草稿尝试,即使解析时草稿被过滤也不采用) | +| 不是“我先查一下”这类只有进度的文本(与网页助手同一判定) | 重跑终态步骤 | +| 输出非空 | 重跑终态步骤,三次仍无结果则失败 | + +**不变的部分**:写操作仍然只能通过草稿或平台工具网关执行,只读 Bot 的写草稿不执行;回复里的个人凭据照常脱敏;重跑终态步骤时不会重新查询。 + +**影响范围**:只影响 Bot 对话入口 `generateBotReply`,网页助手、SOP 节点和通知汇报都不受影响。新老企微 Bot 和飞书 Bot 都适用。 + +**剩余风险**:模型输出了含义空泛的文字(例如“结果已准备好”),这段文字会原样发给用户,不再重试。这和网页助手的行为一致。 + +**测试**:`internal/httpapi/bot_natural_reply_test.go` 覆盖 9 种情况(只读和自动执行各自的首轮采用、只有进度、空输出、带草稿的协议 JSON、不带草稿的协议 JSON、CLI 失败、三次都无结果、凭据脱敏),另有基线对照,结果见 [R1 验收记录](testing/im-r1-acceptance.md#4-行为变更测试自然语言回答)。 + +## 8. 阶段 G:主动通知、过期回复补发与网页确认 + +### 8.1 飞书、钉钉的 SOP 通知 + +SOP 的完成(`succeeded`)、失败(`failed`)和待处理(`waiting`)通知,以 Bot 单聊消息发给一名**通知接收人**。只启用通知、关闭“启用对话”的配置也能发送。 + +1. 接收人必须先在该 Bot 的单聊里完成账号绑定(第 4.3 节)。 +2. 企业确认后,配置页第③步“通知接收人”点“刷新成员列表”,列出本 Bot 已绑定的成员。 +3. 选中成员后点“试发并保存”:平台先向他发一条试发消息,**平台确认送达后**才保存,并在该成员的绑定上记录 `target_verified_at`。试发失败或结果未知时不保存,页面显示原因。 +4. 接收人解绑或重新绑定时,验证随之清除;之后的通知记为“会话不可用”,需要重新试发。每次发送前都会重新读取绑定和项目权限。 +5. 切换接收人时,如果原接收人还有待发通知,页面会让管理员选择:改发到新接收人,或取消这些通知。两种选择都写在发送记录里。 + +送达规则: + +| 渠道 | 接口 | 幂等 | 结果未知时 | +| --- | --- | --- | --- | +| 飞书 | `POST /open-apis/im/v1/messages?receive_id_type=open_id`,发送 key 作为 `uuid` | 同一 `uuid` 一小时内去重 | 55 分钟内用同一 key 自动重试 | +| 钉钉 | `POST /v1.0/robot/oToMessages/batchSend` | 无 | 记为“送达待核对”,不自动重发 | + +发送前先把发送 key、代数和数据库时间写入投递记录,再调用接口;进程在调用中途退出时,下一次领取能知道“可能已发出”,按上表处理。消息正文是纯文本:企微用的 Markdown 标题、加粗和链接会转成文字,链接单独一行。钉钉返回的 `invalidStaffIdList` 记为“会话不可用”,`flowControlledStaffIdList` 和 429 稍后重试。 + +更换 Secret 时,App ID / Client ID 不能修改,平台先用新 Secret 校验同一应用,校验通过后接收人的验证继续有效。 + +### 8.2 钉钉过期回复改用主动单聊 + +会话 Webhook 过期导致的回复失败(“回复失败 · 会话回复授权已过期”),在对话记录里可以改用主动单聊,把已保存的回复发给原发送者: + +- 原发送者已经通过主动发送试发时,显示“改用主动单聊发送”;否则显示“试发给原发送者”,试发成功后再改用。试发不改变通知接收人。 +- 服务端在事务内重新校验:回复仍是 `reply_failed / route_expired`、Bot 启用且开启对话、原发送者的绑定仍属于同一平台账号并通过了试发;HTTP 层另外复核项目权限。校验不通过返回 `409 push_target_unverified`。 +- 通过后作为新的一代、使用新 key 发送,记录操作人、原因和“主动单聊”路由。钉钉没有幂等,结果未知时同样停在“送达待核对”。 + +### 8.3 网页确认(企业微信、飞书、钉钉) + +“执行权限”选“网页确认”后: + +1. 查询直接回答。需要写平台数据时,模型只提交草稿;平台冻结草稿(随机 ID、参数哈希、30 分钟有效),回复操作摘要和“在平台确认这些操作”链接。 +2. 只有聊天发送者绑定的平台账号能打开链接。确认时重新检查:Bot 仍启用对话并处于网页确认模式、聊天账号的绑定未变、该账号仍有项目执行权限、草稿未过期、参数未被修改。任一项不满足,草稿作废并说明原因。 +3. 确认在一条加锁语句里把草稿从“等待确认”改为“执行中”,重复点击或在另一个标签页确认,只返回当前状态,不会再执行。执行沿用网页助手的预检:有依赖警告的草稿不执行。 +4. 执行中进程退出的草稿,10 分钟后显示“执行中断,待核对”,平台不会自动重新执行,请在平台核对结果。 +5. 解绑、删除 Bot、关闭对话或改为其他执行权限时,等待确认的草稿全部作废。 + +**飞书的确认卡片。** 飞书 Bot 回复写操作时直接发一张卡片:上方是 Bot 的说明,下面列出操作清单(含新需求的 SOP),按钮为“确认执行”“确认并启动 SOP”(有新需求时出现)“取消”和“在平台确认(可更换 SOP)”。点按钮与在确认页确认走同一套检查和状态迁移;只有发起人本人能用,其他人点击会提示“不是你发起的”;执行结果以单聊消息发给点击者。卡片回调需要第 4.1 节的 `card.action.trigger`。钉钉仍是文字说明加确认链接。 + +业务写入接口本身没有幂等键;“只执行一次”由状态机保证,不依赖写入接口去重。飞书、钉钉同样可以选“网页确认”(2026-11-20 起):机制与企业微信相同,确认链接在纯文本消息中单独占一行;不提供“自动执行”。 + +**新需求的 SOP。** 草稿里每个新建需求后面写明将绑定的 SOP:用户在聊天里点名了 SOP 时,模型查到后写进草稿;没点名时按项目默认 SOP(与在平台新建需求的规则相同)。确认页为每个新需求提供 SOP 下拉框,与平台新建需求表单一致:第一项是“项目默认 SOP(名称)”,其后是本项目 SOP、共享 SOP 和通用模板(“通用 · ”开头,已归档的不列出),可以更换,并可勾选“创建后立即启动这个 SOP”。模型点名了 SOP 时默认选中该 SOP,改回“项目默认”后需求按项目默认绑定。只接受页面列出的 SOP;选了其他项目的 SOP 时整份草稿不执行。聊天摘要里未点名的写成“SOP:项目默认 · 名称”。启动结果写在执行结果里(“已启动:SOP 名称(运行编号)”或“未启动:原因”),需求已创建但启动失败时,可在平台手动启动。 + +### 8.4 升级 + +阶段 G 没有需要手动执行的迁移脚本。新版本启动时的增量 DDL 会补齐 `notification_deliveries` 的发送 key、代数、开始时间、平台消息 ID 和人工确认列,`bot_user_bindings.target_verified_at`、`bot_interactions.send_route`,并新建 `bot_pending_drafts`(utf8mb4)。回退到阶段 F 的版本时,这些列和表保留不用;回退前请先处理“执行中”的网页确认草稿。 + +逐项真实环境验收见 [阶段 G 验收记录](testing/im-stage-g-acceptance.md)。 + +### 8.5 通知卡片:在飞书、钉钉里直接审批 + +Bot **启用对话**时,节点**待人工确认**和**执行失败**的通知以带按钮的卡片发给通知接收人,按钮与企业微信卡片相同:通过、重试、返工、手动通过、继续、不重试(按节点当前状态出现)。完成类通知仍是文字。只开通知、不开对话的 Bot 收不到点击,继续发文字通知。 + +点击后: + +1. 平台在回调时限内记录这次点击并回应(飞书弹出提示,钉钉在卡片状态栏显示“已收到”),然后按企业微信卡片的同一套规则处理:点击者必须已绑定平台账号并有项目权限,节点仍停在通知时的状态和轮次,按钮对应的操作仍然可用。 +2. 只有通知接收人本人的点击会被执行;转发或他人点击记为“已过期或已处理”。 +3. 处理结果以单聊消息发给点击者,例如“「人工确认」操作已完成:确认通过,继续流程”。再次点击同一张卡片会提示已处理。 + +**飞书**:不需要额外配置,按第 4.1 节添加 `card.action.trigger` 回调(长连接)即可。卡片使用消息卡片 JSON 1.0,按钮值包含操作和投递编号。 + +**钉钉**:需要在开发者后台 → 卡片平台建一个互动卡片模板,发布后把模板 ID 填到 Bot 配置第①步的“互动卡片模板 ID”。不填时继续发文字通知。另需开通“互动卡片实例写权限”。模板约定: + +| 变量 | 类型 | 内容 | +| --- | --- | --- | +| `title` | 文本 | 如“待人工确认:登录优化” | +| `content` | Markdown | 原因、待处理事项、需求、节点、SOP、返工目标 | +| `detail_url` | 文本 | 运行详情页地址,可用于“查看详情”跳转按钮 | +| `status` | 文本 | 初始为空;点击后平台回写“已收到,处理结果会以消息发给你” | +| `show_approve`、`show_retry`、`show_reject`、`show_pass`、`show_resume`、`show_decline` | 布尔 | 该按钮当前是否可用,用于按钮的显示条件 | +| `text_approve` 等 | 文本 | 平台建议的按钮文字,可选使用 | + +六个按钮的点击事件设为**回传请求**,回传参数 `{"action": "approve"}`(其余按钮分别为 `retry`、`reject`、`pass`、`resume`、`decline`)。平台以 `callbackType=STREAM` 创建卡片,点击经现有 Stream 连接回传(主题 `/v1.0/card/instances/callback`),投放场域为机器人单聊(`IM_ROBOT`,`robotCode` 用 Client ID)。卡片实例 ID 为“投递编号.发送 key 前 12 位”,再次发送会生成新卡片。模板变量名和权限名称以卡片平台、开发者后台为准,联调时补记实际值。 + +## 9. 数据保留(阶段 H) + +### 9.1 规则 + +每个运行 Bot worker 的进程每小时清理一次,每批 1000 条,每类每轮最多 10 批,时间从记录**实际结束**算起: + +| 数据 | 正文保留期(默认 90 天)到期后 | 记录保留期(默认 180 天)到期后 | +| --- | --- | --- | +| 对话交互(`bot_interactions`),状态为已回复、回复失败、已取消 | 清空消息、引用、回复和会话回复路由,记录 `body_purged_at`;记录页显示“正文已按保留期清理”,不能再次发送 | 删除 | +| 通知发送记录(`notification_deliveries`),状态为已发送、失败、已取消、已过期、会话不可用 | 清空渲染后的正文和 AI 汇报请求,`content_source=purged`,不能重试 | 删除 | +| 网页确认草稿(`bot_pending_drafts`),状态为已执行、失败、已过期、已取消 | 清空草稿、摘要和执行结果 | 删除 | +| Bot 轮次的原生 CLI 日志 | 按映射删除日志文件,见 9.2 | 日志已删除或确认不存在的映射被删除 | + +**不清理的数据**:排队中、执行中、待发送、发送中、送达待核对(`reply_unknown`、`send_unknown`)、待授权、网页确认的“执行中”和“执行中断,待核对”。这些记录保留到有人处理为止。等待确认的草稿过期后先改为“已过期”,再按上表处理。账号绑定、Bot 配置、会话映射(`bot_conversations`)和连接状态不在清理范围内。 + +“实际结束”的取法:对话交互和草稿用最后更新时间;通知发送记录没有结束时间列,要求创建时间、最后一次尝试时间和发送时间都早于期限,最近重试过的记录不会被提前清理。清理正文时不改动更新时间,所以记录保留期仍从结束时算起。 + +### 9.2 原生 CLI 日志 + +Bot 轮次在用户共用的 CLI HOME(`AGENTFLOW_ASSISTANT_SESSION_DIR/<用户 ID>/home`)中运行,同一目录里还有其他来源的日志。清理只处理同时满足以下条件的文件: + +- 对应的 `bot_native_sessions` 映射已结束(完成、失败、中断),且结束时间早于正文保留期; +- 文件名等于映射记录的请求 session ID 或 CLI 报告的 session ID:CodeBuddy 在 `.codebuddy/projects`、`.codebuddy/sessions` 下,Claude Code 在 `.claude/projects` 下,Codex 按线程 ID 匹配 `.codex/sessions/**/rollout-*-<线程 ID>.jsonl`,并从 `codex-threads.json` 删除对应条目; +- 位于该映射所属用户自己的 CLI HOME 内。不跟随符号链接;日志目录经过符号链接指向 HOME 之外时整棵跳过; +- 同一个 session 仍被其他未结束或未到期的映射使用时,本轮跳过。 + +每条映射记录结果:`removed`(附删除的相对路径)、`missing`(没找到文件)、`error`(下一轮重试)、`unsupported`(该 CLI 的日志位置未登记,例如 OpenCode、Kimi Code,保留供人工核查)。查看统计: + +```sql +SELECT engine, cleanup_status, COUNT(*) FROM bot_native_sessions GROUP BY engine, cleanup_status; +SELECT id, user_id, requested_session_id, native_session_id, cleanup_detail FROM bot_native_sessions WHERE cleanup_status IN ('error', 'unsupported'); +``` + +多个 API 副本部署时,`AGENTFLOW_ASSISTANT_SESSION_DIR` 需要是共享存储;否则没有执行该轮的副本找不到文件,会记为 `missing`,日志留在原副本上。状态为 `started` 的映射(进程在运行中退出)保留追踪状态,不自动清理。 + +**升级前的历史日志**没有映射,清理永远不会触碰它们。`migrations/037_bot_binding_ownership.sql` 第 4 段在升级时列出 `legacy_unmapped` 交互清单,请在开启 worker 之前导出保存:记录保留期到期后这些交互会被删除,清单就无法再生成。按清单人工核查后再决定是否删除对应日志。 + +### 9.3 调整与暂停 + +- 改保留期:修改 `.env` 中的两个变量后重启。缩短保留期会在下一轮立即生效;延长保留期不能恢复已清理的数据。 +- 暂停清理:两个变量都设为 `0`,或者维护期间设置 `AGENTFLOW_BOT_WORKERS_ENABLED=false`(同时停止收发)。 +- 清理结果:日志 `bot retention: interaction bodies=… interactions=… notification bodies=… notifications=… drafts expired=… draft bodies=… drafts=… native mappings=… native logs handled=…`;失败时输出 `bot retention: cleanup failed`,下一小时重试。 + +## 10. 回退 + +| 回退目标 | 做法 | 数据影响 | +| --- | --- | --- | +| 最终版本 → 阶段 B 到 G 的预发版本 | 停止全部新版本副本,启动目标版本。新增列和表保留不用 | 停止数据保留清理。以下按各阶段代码推断:回到阶段 F 及更早:排队中的飞书、钉钉主动通知会被取消(“该渠道的主动通知尚未开放”),网页确认链接失效(`/bot-approve.html` 不存在),等待确认的草稿不再执行。回到阶段 E 及更早:钉钉配置不再建立连接 | +| 最终版本 → 未接入 IM 的版本,未执行凭据重新加密 | 停止全部新版本副本,启动旧版本 | 旧版本能读出配置并使用明文 Secret;但它不认识租约 epoch,必须先停全部新副本。新增列和表保留不用。旧版本不认识渠道字段,飞书、钉钉配置会被当作企微配置读取(按代码推断,未演练),回退前先停用这些配置 | +| 最终版本 → 未接入 IM 的版本,已执行凭据重新加密 | **只能**停止全部新版本副本 → 恢复升级前的备份 → 启动旧版本 | 维护窗口之后新产生的对话、配置和通知需要按停机时间段人工核对;已被数据保留清理删除的记录随备份恢复 | + +演练结果:[R1 验收记录第 3 节](testing/im-r1-acceptance.md#3-预发升级与回退演练)(阶段 B,MySQL 8.4.13)和 [最终测试记录第 3 节](testing/im-final-test-record.md#3-最终版本升级与回退演练)(最终版本,含数据保留首轮清理)。 diff --git a/docs/proposals/enterprise-im.md b/docs/proposals/enterprise-im.md new file mode 100644 index 000000000..94933270d --- /dev/null +++ b/docs/proposals/enterprise-im.md @@ -0,0 +1,833 @@ +# LoopForge 企业 IM 接入实施方案(单分支开发) + +版本为第 3 版,日期为 2026-11-18。状态为实施中:阶段 A–E(R1 飞书私聊闭环)的代码与自动化测试已完成,R1 已于 2026-11-18 验收通过(真实环境通过 R1-01/02/05,其余经验收负责人豁免)。阶段 F(R2 钉钉私聊闭环)已于 2026-11-19 验收通过,见 `docs/testing/im-r2-acceptance.md`。阶段 G 已于 2026-11-20 验收通过,见 `docs/testing/im-stage-g-acceptance.md`;实现与本文的差异见第 9.3 节。阶段 H(数据保留、安装配置、迁移与回退手册)已完成,最终测试记录见 `docs/testing/im-final-test-record.md`;实现与本文的差异见第 10.1 节。验收提交、预发演练、故障测试和逐项结果见 `docs/testing/im-r1-acceptance.md`。第 3 版补齐数据库时间、配置物理列、锁顺序、重试终态、停用语义、状态枚举、企业确认和维护窗口的实现约定,并按 `origin/main@e89cda9` 重新核对源码和迁移编号。本文是 `feature/enterprise-im` 分支的执行清单。 + +目标是在 LoopForge 已有的企微 Bot、账号绑定、交互队列和通知服务之上,扩展飞书和钉钉。首轮先完成三个渠道的账号绑定、私聊查询和可靠回复,再接入项目助手与 SOP 主动通知。 + +**基线。** 开发分支 `feature/enterprise-im` 于 2026-11-18 从 `origin/main@e89cda9804643d3631e77652d1bca47a079adb69` 建立。第 2 版依据的是 `69e4bf0`,两者之间共 20 个提交,其中第 2.1 节列出的 Bot、通知、身份、助手入口文件都没有改动。迁移目录的最大编号已从 033 变为 035,本方案的新迁移脚本改用 036、037。 + +## 1. 交付范围与默认行为 + +| 范围 | 本分支交付 | +| --- | --- | +| 企微兼容 | 已有 Bot、绑定、会话、通知与卡片继续可用;补齐发送状态和连接易主处理 | +| 飞书 | 企业自建应用,使用官方 Go SDK 长连接;支持私聊文本、账号绑定、只读项目查询、回复原消息 | +| 钉钉 | 企业内部应用,接入 Stream 协议(SDK 使用范围见第 8.3 节);支持私聊文本、账号绑定、只读项目查询、通过会话 Webhook 回复 | +| 助手 | 使用绑定用户的个人模型凭据和项目权限;新增渠道首轮只执行只读任务 | +| 主动通知 | 向已验证的目标发送 `succeeded`、`failed`、`waiting` 文本通知,复用现有 SOP 通知任务 | +| 管理与运维 | Bot 配置、凭据轮换、连接状态、交互与投递记录、人工核对或补发、数据保留 | +| 后续扩展 | 飞书、钉钉的群聊、交互卡片、附件和写操作,按渠道单独验收后开放 | + +首轮产品行为采用以下默认值。这些是本方案的设计选择,实施评审时可以调整。 + +- **写操作默认只读。** 新建 Bot(**包括新建的企微 Bot**)默认 `writeApproval=readonly`。企微历史记录缺少该字段时,继续映射现有的自动执行策略。新渠道收到写请求时,引导用户到项目页面操作。新建企微 Bot 从“自动执行”改为“只读”属于管理员能感知到的变化,写入上线说明和 `CHANGELOG.md`,见第 12 节。 +- **配置归属与执行权限。** 一个 Bot 配置固定归属一个项目。绑定用户的执行权限来自其平台账号和当前项目。即使 Bot 由平台管理员创建,普通成员发来的消息仍按该成员的权限执行。 +- **聊天范围。** 飞书、钉钉首轮只处理私聊文本,群消息计数后直接结束处理。企微已有的群聊和卡片按原协议继续工作。 +- **接收配置唯一。** 同一 LoopForge 部署中,一个企业应用只能启用一个接收配置。不同环境需要配置各自独立的机器人应用。租约在同一部署的数据库范围内协调。 +- **未绑定用户。** 未绑定的用户先收到网页绑定链接,在网页上确认现有 LoopForge 账号,再由原 IM 发送者提交 `/bind `。绑定成功后,用户需要重新发送业务请求。 +- **回复形式。** 企微保留现有的流式进度。飞书、钉钉首轮只返回最终文本;处理时间较长时,另发一条接单提示,提示有独立的记录和投递 key。 +- **网页登录。** 网页绑定使用 LoopForge 现有的登录能力。企业 SSO 可以作为已有登录入口使用。 + +本分支按第 11 节的 IM-01 至 IM-12 验收。三个渠道的真实联调分别记录证据。 + +## 2. 现有代码与改动落点 + +### 2.1 已核对的实现 + +| 现有位置 | 当前行为 | 本次改动 | +| --- | --- | --- | +| `internal/domain/notification.go` | `NotificationBot` 保存外部 Bot ID、项目、启停、模型和通知目标 | 增加渠道、企业范围、配置版本和写策略 | +| `internal/botinteraction/service.go` | 直接维护企微连接、入队、模型处理与回复 | 阶段 A 已改为通过渠道接口编排;阶段 C 增加租约版本与发送恢复 | +| `internal/botinteraction/socket.go` | 企微发送和回执等待;原先错误以字符串返回 | 阶段 A 已改为记录是否开始写出、回执状态,转换成明确的发送结果 | +| `internal/botinteraction/progress.go`、`cards.go` | 企微流式进度与交互卡片 | 阶段 A 已包装为企微适配器的可选能力 | +| `internal/notification/service.go`、`provider.go` | 用官方 `wecom-cli` 查询会话和发送通知,复用现有通知队列 | 按渠道分流;飞书、钉钉新增独立于接收连接的主动发送 | +| `internal/repo/notifications.go` | `NotificationsRepo` 以 JSON payload 保存配置,保存时逐行扫描以防重复启用;投递已有 `lease_token` | 补物理列、配置版本、投递 key 和生命周期事务 | +| `internal/repo/bot_credentials.go` | `UsersRepo` 按**配置 ID** 把传入的 secret 原样写入名为 `ciphertext` 的列 | 引入带版本的凭据加密和密钥轮换 | +| `internal/repo/bot_identities.go` | 两阶段绑定、nonce 摘要、15 分钟有效期、解绑 | 增加配置归属、渠道显示、原子限频和撤销联动 | +| `internal/repo/bot_interactions.go` | 队列、会话、45 秒连接租约、10 秒续约、结果保存 | 增加 epoch、条件更新、发送阶段和旧 owner 保护 | +| `internal/httpapi/bot_interactions.go` | `botExecutionUser` 校验绑定与项目权限,`generateBotReply` 接入现有助手 | 按渠道取身份范围,执行前复核配置,传入只读策略 | +| `internal/httpapi/bot_identities.go` | 已登录网页确认,原发送者回传确认码 | 扩展元数据,按配置校验,多渠道文案 | +| `web/app/bots.js`、`web/bot-bind.js` | Bot 管理、发送记录、绑定确认页面 | 沿用原页面,增加渠道、状态、确认信息和补发操作 | +| `docs/api/openapi.yaml` | 已有 Bot、交互和绑定 API | 同步接口字段、错误码和新增操作 | + +以下事实已对照 `origin/main@e89cda9` 逐条核对。 [交互 Repo](../../internal/repo/bot_interactions.go) · [凭据 Repo](../../internal/repo/bot_credentials.go) · [Bot 对话入口](../../internal/httpapi/bot_interactions.go) + +| 核对项 | 源码证据与修订落点 | +| --- | --- | +| 结果写回 | `SaveBotReply` 只按交互 ID 和 `running` 状态更新,不检查 `RowsAffected`。阶段 C 增加归属条件,并处理更新 0 行的冲突 | +| 发送完成 | `MarkBotReplySent` 直接把 `ready` 改为 `replied`。阶段 C 增加 `sending` 状态和明确的结果迁移 | +| 凭据存储 | `putBotCredential` 原样写入传入的 secret,`BotCredential` 读取时也原样返回;行主键是配置 ID。阶段 B 增加加密封装 | +| 企微备用发送 | `service.process` 在 `ws.stream` 失败后直接调用 `ws.send`。阶段 A 保留这一行为;阶段 C 改为按送达证据分类,用户能感知到的变化由 IM-08 单独验收 | +| CLI 原生会话 | `generateBotReply` 每轮生成 `cliSessionID`,调用 `runAssistantCLI` 时丢弃了返回的 session ID。阶段 B 建表,并接入写入点 | +| 通知卡片 ID | 用户消息的交互 ID 由 `BotMessageID(bot.BotID, msgid)` 派生,即外部 ID;而通知卡片和详情交互由 `BotMessageID(bot.ID, "notification:"…)` 派生,即配置 ID。两者都保持不变;`IdentityScope()` 只用于平台消息的去重 | +| 防重复启用 | `SaveNotificationBot` 先用 `lockBotDefinition` 在 `bot_connections` 中按外部 BotID 建行并加锁,再扫描全部配置的 payload。阶段 B 改为 `receive_slot` 唯一列;企微的连接行键保持不变 | +| 通知状态 | `notification_deliveries.status` 现有取值:`pending`、`sending`、`sent`、`failed`、`cancelled`、`expired`、`awaiting_authorization`、`target_unavailable`。企微的 `waiting`/`failed` 卡片通知进入交互队列后即记为 `sent`,实际送达状态在对应的交互记录里 | +| 停用处理 | 通知 worker 领取任务时,若发现 Bot 已停用,把任务记为 `cancelled`。对话 worker 由 `reconcile` 停止连接,但交互记录的状态不变 | +| 迁移编号 | `origin/main@e89cda9` 已有 `034_schedule_job_executions.sql`、`035_codebuddy_model_catalog.sql`。本方案使用 036、037;合并前如果目标分支又新增了迁移,再顺延,并在 PR 中说明 | + +### 2.2 结构约定 + +遵守根目录 `AGENTS.md` 的约定: + +- **Repo 归属。** 通知归 `NotificationsRepo`,凭据和绑定归 `UsersRepo`,交互与连接归 `BotInteractionsRepo`,草稿和原生会话映射归 `AssistantRepo`。 +- **方法与协作。** 新增的领域方法写在对应 Repo 上,跨领域通过现有的显式注入协作。共享的 `deps` 只保留 gorm 句柄。 +- **handler。** 按领域归属。 +- **前端。** 沿用 `design.md` 中 Bot 管理的布局、状态徽标和现有设计令牌。 + +[仓库约定](../../AGENTS.md) · [前端约定](../../design.md) + +新增或调整的文件如下。“已新增”表示阶段 A 已提交。 + +```text +internal/botinteraction/ + channel.go 渠道接口与事件、回复、发送结果类型(阶段 A 已新增) + channel_wecom.go 包装现有 socket / progress / cards(阶段 A 已新增) + channel_feishu.go 飞书适配器(阶段 E 已新增) + channel_dingtalk.go 钉钉适配器(阶段 F) + inbound.go 公共入站校验、分类和持久化(阶段 A 已新增) + delivery.go 回复状态机和恢复(阶段 C 已新增) + retention.go 数据保留(阶段 H) + service.go 编排连接、领取和处理 +internal/credcrypt/ + keyring.go AES-GCM 凭据加解密(阶段 B 已新增) +internal/repo/ + dbtime.go 数据库时间方言函数(阶段 B 已新增) + notifications.go Bot 配置与通知投递 + bot_credentials.go 凭据存储 + bot_identities.go 身份绑定 + bot_interactions.go 连接、交互和会话 + bot_native_sessions.go Bot 与原生 CLI 会话映射,归 AssistantRepo(阶段 B 已新增) +internal/httpapi/ + notifications.go Bot 管理 + bot_identities.go 绑定确认 + bot_interactions.go 助手入口 + bot_interaction_records.go 对话记录和人工操作 +web/app/bots.js +web/bot-bind.js +migrations/ + 036_enterprise_im_channels.sql + 037_bot_binding_ownership.sql +scripts/im-sdk-probe/ SDK 固定版本探针,独立 Go 模块(阶段 A 已新增) +docs/ + im-channels.md 配置与联调手册 + api/im.md IM 管理和使用接口 + api/openapi.yaml +``` + +## 3. 单分支开发方式 + +全部阶段都在 `feature/enterprise-im` 分支上完成,最终合成一个 PR。每个阶段保留 1–3 个可以独立审阅的提交。开发目录使用独立的 worktree。 + +```bash +git fetch origin main +git worktree add -b feature/enterprise-im ../LoopForge-im origin/main +cd ../LoopForge-im +git rev-parse HEAD # e89cda9804643d3631e77652d1bca47a079adb69 +``` + +`AGENTFLOW_*` 环境变量和 `cmd/agentflow` 名称沿用项目现有约定。 + +| 阶段 | 主要提交主题 | 阶段完成后的可验证结果 | +| --- | --- | --- | +| A | `refactor(im): define channel contracts and wrap wecom` | 企微通过渠道接口工作,存量回归通过;模拟渠道覆盖接收、回执和断线;SDK 固定版本完成探针验证 | +| B | `feat(im): scope bot configs and encrypt credentials` | 渠道配置、物理列、数据库时间函数、身份范围、加密、CLI 会话映射写入、手动迁移可用 | +| C | `fix(im): fence workers and persist delivery outcomes` | 换主保护、发送恢复、停用和删除语义、人工补发和记录页可用 | +| D | `feat(im): bind channel identities and enforce read-only policy` | 两阶段绑定与只读业务链路可用 | +| E | `feat(im): connect feishu private chats` | 飞书真实私聊完成企业确认、绑定、查询和回复 | +| F | `feat(im): connect dingtalk stream chats` | 钉钉真实私聊通过同一套验收,并完成回复过期验证 | +| G | `feat(im): deliver sop notifications across channels` | 三个渠道发出主动通知;企微可选网页确认写操作;钉钉过期回复可改用主动单聊补发 | +| H | `docs(im): record rollout and acceptance evidence` | 保留策略、部署说明、真实验收和最终 PR 齐全 | + +阶段检查只使用已经实现的能力。尚未接入的渠道在 UI 中显示为开发中,API 返回明确的 `channel_not_ready`。飞书、钉钉的业务消息走同一个入口,所以阶段 D 的只读约束必须在阶段 E 之前完成。 + +## 4. 渠道接口与运行规则 + +### 4.1 接收、回复与主动发送 + +渠道适配器放在 `internal/botinteraction` 包内,这样企微适配器可以直接包装已有的未导出 socket 方法。公共编排只处理领域事件和发送结果。下面的签名已在阶段 A 落地(`channel.go`)。 + +```go +type Channel interface { + Kind() string + Verify(context.Context, domain.NotificationBot, Credential) (Verification, error) + Connect(context.Context, domain.NotificationBot, Credential, InboundSink) (Conn, error) +} + +type Conn interface { + Done() <-chan struct{} + Reply(context.Context, OutboundMessage) SendResult + Close() error +} + +// 返回 Accepted=true 表示本次事件新建了交互;重复事件返回 Accepted=false 且 err 为 nil。 +// 返回 err 时适配器按平台协议回失败确认,让平台重投。 +type InboundSink func(context.Context, InboundEvent) (InboundAck, error) + +// 主动通知通过应用 API 发送,实例只需持有通知投递租约。 +type PushSender interface { + Push(context.Context, domain.NotificationBot, Credential, OutboundMessage) SendResult +} + +type SendOutcome string + +const ( + Delivered SendOutcome = "delivered" + NotDelivered SendOutcome = "not_delivered" + Rejected SendOutcome = "rejected" + Unknown SendOutcome = "unknown" +) + +type SendResult struct { + Outcome SendOutcome + Code string + Retryable bool + PlatformMessageID string + SafeDetail string +} +``` + +第 2 版写的 `InboundSink` 只返回 `error`,第 3 版改为同时返回 `InboundAck`。原因是企微要在“新建交互成功”之后才发出“已收到,正在处理…”的接单流式消息,重复事件不能再发一次。 + +`InboundEvent` 包含以下内容: + +- **消息字段:** 业务消息 ID、平台事件 ID、发送者、会话、聊天类型、消息类型、正文和引用; +- **可信事件字段:** 适配器从可信事件中读取的应用标识、企业范围和回复路由; +- **企微专用字段:** 卡片动作、投递 ID、卡片 task ID 和请求 ID。 + +服务端以当前连接对应的 Bot 配置决定项目、模型和权限,不信任事件中自带的项目信息。 + +`OutboundMessage` 包含: + +- 内部交互或投递 ID; +- 冻结的目标; +- 正文; +- 是否为最终回复; +- 持久化的 `send_key`; +- 回复路由。 + +日志和公共 DTO 只使用脱敏后的信息。 + +企微的流式进度和卡片通过可选接口提供(`progressConn`、`cardConn`),飞书、钉钉首轮只用普通文本回复。 + +`PushSender` 由渠道对象实现,与 `Conn` 相互独立。阶段 A 中,企微的 `Push` 包装注入的 `wecom-cli` 发送函数;通知服务仍按原路径直接调用 `Provider`,到阶段 G 再切换到 `PushSender`。 + +企微的凭据校验复用 `CLIProvider` 的签名鉴权:阶段 A 新增 `CLIProvider.Verify`,由 `notification.New` 把它显式注入企微渠道。校验只调用鉴权接口,不建立短暂的验证连接,因此不会挤掉正在运行的接收连接。 + +### 4.2 身份和连接范围 + +JSON 中的 `botId` 继续作为外部应用标识。新增渠道字段 `channel`,取值为 `wecom`、`feishu`、`dingtalk`。 + +| 标识 | 历史企微 | 飞书、钉钉 | +| --- | --- | --- | +| 配置 ID | `NotificationBot.ID` | `NotificationBot.ID` | +| 外部 ID | 原 `BotID` | App ID / Client ID | +| 身份范围 `IdentityScope()` | 原外部 Bot ID | 内部配置 ID | +| 平台消息去重 | `BotMessageID(bot.IdentityScope(), messageID)` | `BotMessageID(bot.IdentityScope(), messageID)` | +| 内部卡片交互 ID | 保持 `BotMessageID(bot.ID, "notification:"…)` | 首轮不生成卡片 | +| 会话 ID | 保留现有派生规则(内部配置 ID、聊天类型、Chat ID) | 使用相同派生函数 | +| 接收占位 `ReceiveSlot()` | 对渠道和外部 ID 做稳定派生 | 对渠道和外部 ID 做稳定派生 | +| 连接租约 `LeaseKey()` | 原外部 Bot ID | 接收占位值 | + +`ReceiveSlot` 采用 `channel + ":" + hex(sha256(externalID))`,长度小于现有列的 191 字符上限。只有在 `enabled && interactionEnabled` 时才写入这个唯一列,其余情况写 SQL `NULL`。 + +**创建后不可改的字段:** 配置 ID、渠道、外部应用 ID 和项目。企业范围按第 8.2、8.3 节先确认,进入 `locked` 状态后同样固定。 + +- 修改名称、模型、启停,或更换同一应用的 Secret,使用更新接口。 +- 切换项目或应用,需要新建配置并重新绑定。 +- 旧客户端提交时缺少 `channel`,一律按企微处理;新客户端必须明确提交渠道。 + +绑定的读取、创建挑战、确认、解绑、平台消息去重和助手执行,统一调用 `IdentityScope()`。企微的旧绑定继续按原键访问。 + +`bot_connections.bot_id` 存的是 `LeaseKey()`: + +- 企微行存原外部 Bot ID; +- 飞书和钉钉行存 `ReceiveSlot()`,即 `channel:hex(sha256(externalID))`,例如钉钉为 `dingtalk:`。 + +连接的领取、续约、释放、状态查询,以及第 7.1 节中按连接行加锁的参数,都统一传 `bot.LeaseKey()`。而 `bot_interactions.bot_id` 继续存内部配置 ID。这两列分别表示连接租约和配置归属,不能混用。 + +### 4.3 配置、凭据与重连 + +**保存流程。** Secret 的校验放在数据库事务之外完成。保存请求携带预期的 `configVersion`,事务内按第 7.1 节的锁顺序先锁配置行,比较版本,再原子更新配置和密文;版本冲突返回 `409 config_changed`。 + +**两个版本号。** 两者都是 `notification_bots` 上的物理列,payload 中同步保存一份用于展示: + +| 版本号 | 递增时机 | 不递增的情况 | +| --- | --- | --- | +| `config_version` | 每次成功写配置:名称、模型、启停、写策略、企业锁定、目标变更 | 记录 `tenantCandidate` 等候选字段 | +| `credential_version` | 只在 Secret 变更时 | — | + +**重连只看连接相关字段。** 接收服务比较以下字段的指纹:渠道、外部应用 ID、`credential_version`、`enabled`、`interaction_enabled`。指纹变化才关闭旧连接并重连。现有 `reconcile` 用 `UpdatedAt` 判断是否重连,阶段 B 改为比较这个指纹。 + +因此,企业锁定、改名称和模型只会增加 `config_version`,不会触发重连;从下一条事件或下一轮执行开始按新值校验。出站发送每次都重新读取配置和当前凭据。 + +停用和删除的状态处理见第 7.2 节。删除 Bot 的事务还会撤销该配置的挑战和绑定。对历史企微按外部 ID 关联的绑定,先核对是否还有其他保留的配置使用同一身份范围。历史记录中的 Secret 和会话 Webhook,在取消或进入终态后的清理中移除。 + +## 5. 数据模型、凭据和迁移 + +### 5.1 增量字段 + +| 实体或表 | 拟新增字段与约束 | +| --- | --- | +| `notification_bots` | 物理列:`channel`、`external_app_id`、`tenant_key`、可空唯一列 `receive_slot`、`enabled`、`interaction_enabled`、`config_version BIGINT NOT NULL DEFAULT 0`、`credential_version BIGINT NOT NULL DEFAULT 0`。payload 同步保存渠道、企业名称、`tenantState`、`tenantCandidate`(含候选事件 ID、接收时间、当时的凭据版本)、确认人和确认时间、`writeApproval`。物理列与 payload 在同一事务写入;归属判定只读物理列 | +| `user_bot_credentials` | 保留现有列,密文改用带版本的封装格式 | +| `bot_connections` | `epoch BIGINT`、`lease_until_us BIGINT`、换主次数、最后事件时间、鉴权失败次数、忽略的群消息次数;原有 owner 和 `lease_until` 列保留,仅用于展示 | +| `bot_interactions` | `project_id`(记录页授权快照)、`lease_owner`、`lease_epoch`、`config_version`、`platform_event_id`、`platform_message_id`、`msg_type`、`content_length`、`reply_route` 密文、`route_expires_at_us`、`send_key`、`delivery_generation`、`send_started_at_us`、`send_attempts`、`undelivered_retries`、`next_send_at_us`、`send_lease_until_us`、`resent_by`、`confirmed_by`、`confirmed_at`、`receipt_source`、`terminal_reason` | +| `bot_conversations` | 当前渠道所需的发送目标元数据、`target_verified_at`、提示限频的窗口和计数。授权型 URL 另存为密文 | +| `bot_user_bindings` | `config_id`,原身份主键保留。新渠道必填,历史企微通过核查回填 | +| `bot_binding_challenges` | `config_id`、`channel`、`sender_mask`;原 nonce/code 哈希和有效期保留 | +| `notification_deliveries` | `send_key`、`delivery_generation`、`send_started_at_us`、`send_lease_until_us`、`platform_message_id`、发送结果分类、`confirmed_by`、`confirmed_at`、`receipt_source`;继续使用现有 `lease_token` | +| `bot_pending_drafts` | 阶段 G 的企微网页确认草稿:用户、配置、项目、操作摘要、冻结参数、有效期、状态、执行 key。方法归 `AssistantRepo` | +| `bot_native_sessions` | 阶段 B 建表并写入:配置、项目、平台用户、会话、交互、engine、请求的 session ID、原生 session ID、服务端存储范围、相对日志路径、生命周期和清理状态。方法归 `AssistantRepo` | + +索引: + +- `idx_bot_queue` 覆盖配置、状态和 `next_send_at_us`;待处理状态查询与记录页各建所需的索引。 +- 正文使用 utf8mb4。 +- 标识的相等比较使用稳定的派生键或二进制比较,避免大小写折叠。 + +#### 交互状态枚举(`bot_interactions.status`) + +| 状态 | 含义 | 是否终态 | +| --- | --- | --- | +| `queued` | 已入队,等待执行 | 否 | +| `running` | 正在由持有租约的 worker 执行模型或卡片动作 | 否 | +| `ready` | 回复正文已保存,等待发送(包括 Bot 停用期间暂存的回复) | 否 | +| `sending` | 已领取发送,网络调用可能在进行中 | 否 | +| `replied` | 平台确认送达,或管理员人工确认已收到(`receipt_source=manual`) | 是 | +| `reply_failed` | 平台明确拒绝、明确未送达且重试已耗尽,或回复路由已过期 | 是,可人工再次发送 | +| `reply_unknown` | 无法确认是否送达,等待人工核对 | 是,可人工确认或再次发送 | +| `cancelled` | 未执行就被取消,或执行中因停用、删除被取消 | 是 | + +现有的 `ExpireBotCard` 把过期卡片记为 `replied` 并写入原因,这一行为保持不变。 + +#### 通知状态枚举(`notification_deliveries.status`) + +| 状态 | 含义 | 是否终态 | +| --- | --- | --- | +| `pending` | 等待发送或等待重试 | 否 | +| `sending` | 已领取发送 | 否 | +| `sent` | 平台确认送达、人工确认已收到,或企微卡片已交给交互队列 | 是 | +| `failed` | 平台明确拒绝,或重试已耗尽 | 是,可重试 | +| `send_unknown` | 无法确认是否送达,等待人工核对(新增) | 是,可人工确认或再次发送 | +| `awaiting_authorization` | 渠道授权过期 | 否,授权恢复后重试 | +| `target_unavailable` | 目标不可用 | 是,可重试 | +| `cancelled` | Bot 停用或删除、来源已删除、项目不匹配 | 是 | +| `expired` | 超过 24 小时,或等待节点已推进 | 是 | + +通知不使用交互的 `reply_*` 状态名。前端徽标和查询按两张表各自的枚举处理。 + +### 5.2 数据库时间 + +所有租约、发送窗口、重试时间和路由有效期,**写入和判断都使用数据库时间**,Go 进程的墙钟只用来决定何时在本地提前取消。 + +- **存储格式。** 这些字段以 epoch 微秒存为 `BIGINT`,列名带 `_us` 后缀,避免 MySQL `DATETIME` 与 SQLite 文本时间在格式和时区上的比较差异。 +- **方言函数。** `internal/repo/dbtime.go` 提供 `dbNowUS(db)`,返回当前方言的 SQL 表达式: + - MySQL:`CAST(UNIX_TIMESTAMP(NOW(6)) * 1000000 AS SIGNED)`,与会话时区无关; + - SQLite:`CAST(ROUND((julianday('now') - 2440587.5) * 86400000000.0) AS INTEGER)`。 +- **写入与判断都在 SQL 内。** 写入时用 `SET lease_until_us = + 45000000`,判断时用 `lease_until_us > `。Go 端不传入当前时间值。 +- **测试。** 阶段 B 为该函数单独写测试,覆盖 SQLite 下的写入、比较、跨秒边界和 `OpenTest` 时间注入;MySQL 测试覆盖相同的断言。可控时钟测试通过替换 `dbNowUS` 表达式为固定值实现。 +- **展示列。** 原有的 `DATETIME` 列(如 `lease_until`)保留,在同一语句中一并写入,仅用于展示和兼容,不参与判断。 + +### 5.3 凭据加密 + +`internal/credcrypt` 使用标准库的 AES-256-GCM,每次加密生成随机 nonce。 + +**密钥配置。** 通过 `AGENTFLOW_CREDENTIAL_KEYS` 注入,格式为 `kid:base64(32字节)[,kid:base64(32字节)]`,第一把是当前写入密钥。 + +**密文格式。** 封装为 `lf1::`。凭据内容序列化为 `{"secret":"..."}`,AAD 编码配置 ID 与渠道。会话 Webhook 的 AAD 编码配置、交互 ID 和字段用途。 + +**读写规则:** + +- 历史明文仍可读取,管理页标记为“待迁移”。 +- 带 `lf1:` 前缀的数据必须按密文解析,解密失败直接返回错误。 +- 新写入需要有效密钥;密钥缺失时返回 `503 credential_key_missing`。 + +**迁移命令。** 增加运维子命令 `-credential-action status|reencrypt`,通过显式指令逐条迁移旧明文或旧 kid 密文,记录成功、跳过和失败的数量。命令可以重复执行。密钥解析和 Repo 注入使用现有的装配入口,日志只记录 kid 和计数。 + +### 5.4 手动迁移顺序 + +遵守 `AGENTS.md` 中“增量 DDL 与手工数据迁移”的约定。生产 MySQL 是验收数据库,SQLite `OpenTest` 用于服务测试。 + +1. **备份与只读检查。** 备份数据库和原版本二进制。先跑只读检查,列出重复启用的企微配置、孤立绑定和无法判断归属的绑定。 +2. **停旧副本,以维护模式启动新版本。** 先停止**全部**旧版本副本。`AGENTFLOW_BOT_WORKERS_ENABLED` 是阶段 B 新增的开关,旧版本二进制不认识它,所以不能指望旧版本靠这个开关暂停 worker。然后以 `AGENTFLOW_BOT_WORKERS_ENABLED=false` 启动一个新版本实例。开关关闭时: + - 对话和通知两个后台入口都不启动; + - Bot 配置的写接口返回 `503 bot_maintenance`; + - 启动时的增量 DDL 补齐缺失的表、列和索引,`receive_slot` 初始为 `NULL`。 +3. **运行 `036` 回填段。** 填写渠道、外部应用 ID、接收占位、物理启停列和版本列。缺少渠道的旧值统一识别为企微。回填前先处理重复占位;脚本失败时事务回滚。 +4. **运行 `037` 归属查询。** 明确匹配的,回填绑定和挑战的 `config_id`。多个配置共用旧企微身份的记录,保留清单,按真实启用的配置确认归属。 +5. **重新加密凭据。** 所有副本配置相同的 Keyring,执行显式的 `reencrypt`,复核计数。 +6. **恢复服务。** 以 `AGENTFLOW_BOT_WORKERS_ENABLED=true` 重启全部新版本副本。先验证企微,再依次启用飞书和钉钉。 + +**脚本补充说明:** + +- `036` 同时补齐 `bot_native_sessions` 的基线 DDL 和交互的项目归属快照。 +- `037` 对历史原生会话单独输出“可证实映射”和“待核查”两份清单;映射证据不足的记录标记为待核查。 +- 脚本先确认所需的列存在再引用;DDL 与回填分段执行,出错即停止。 +- MySQL 版本兼容性通过目标测试镜像验证,按 schema 检查生成明确的手工 DDL。 +- 迁移前后记录表行数、绑定归属计数和冲突数。 + +**升级与回退。** 升级完成后,所有运行中的副本使用同一套新租约协议。凭据加密完成后,回退的二进制必须具备相同的解密能力;如果要退回更旧的版本,使用升级前的备份,并按停机窗口核对期间新增的业务数据。 + +## 6. 入站、绑定与业务权限 + +### 6.1 公共入站流程 + +处理链路:`SDK 事件 → 校验当前配置与应用/企业 → 分类 → 事务入队 → 平台确认 → worker 执行`。阶段 A 已把“由事件生成交互并入队”提取到 `inbound.go` 的 `Service.accept`;企微、模拟渠道以及后续的飞书、钉钉适配器都经过这个入口。 + +**确认时限。** 飞书 handler 只做校验和有界的数据库事务,要留在官方约 3 秒的确认窗口之内,因此入队操作设置短超时。 + +**确认结果。** 入库成功和重复事件都正常确认。数据库写入失败时,handler 返回错误;飞书固定版本 SDK 会据此回复 500,平台随后重投(见第 8.2 节)。 [飞书长连接](https://open.feishu.cn/document/ukTMukTMukTM/uYDNxYjL2QTM24iN0EjN/event-subscription-configure-) · [飞书事件重投](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/event-subscription-guide/event-subscriptions) + +**去重。** 业务去重键是 `IdentityScope + 平台业务消息 ID`,平台事件 ID 另存,便于排查。平台重投时即使事件 ID 不同,同一业务消息也只执行一轮。接单提示同样通过持久化记录发送,与最终回复使用不同的 key。 + +**输入限制:** + +- 新渠道文本上限暂定 8000 个 Unicode 字符。 +- 非文本和超长输入只记录类型与长度,并按限频提示用户;正文在进入助手之前完成分类。 +- 企微沿用已有的输入与历史限额。 +- 同一个 Bot 的同一会话串行处理。 + +### 6.2 两阶段绑定 + +1. 未绑定的私聊发送者收到 `/bot-bind.html#request=`。服务端只保存 nonce 的哈希,有效期 15 分钟。 +2. 网页通过现有的 `/api/auth/current` 登录,显示渠道、Bot 名称、项目、脱敏后的发送者,以及将要绑定的账号。 +3. 用户确认后,页面发起同源 POST `/api/bot-bindings/claim`。服务器保存短期确认码的摘要,页面显示 `/bind `。 +4. 用户在原 IM 中发送确认码。适配器从原 Bot 的可信事件中读取发送者;公共处理校验配置范围、原发送者、有效期和确认码摘要,在同一事务中写入绑定并消费挑战。 +5. 绑定成功后提示用户重新发送业务请求。挑战、确认码和绑定指令在进入模型和构造历史之前剔除。 + +**解绑与复核。** 沿用现有的 `/unbind` 命令和个人配置中的解绑入口。状态 API 和确认 API 都会复核:配置存在、Bot 已启用、当前账号有效、有项目访问权限。绑定页只接受同源写请求,授权依赖网页登录会话。 + +**限频:** + +- 引导消息默认 10 分钟最多发 3 次;用户主动获取新链接每分钟最多 1 次;计数使用原子更新。 +- 已有有效挑战时,提示用户查看上一条链接。 +- 旧链接只存了哈希,无法重新生成;用户主动获取新链接时,作废旧挑战并创建新挑战。 + +### 6.3 执行业务和只读约束 + +`botExecutionUser` 每轮都读取当前配置和绑定,核对:用户存在、Bot 状态、项目成员权限、配置版本。每次工具调用仍经过平台原有的权限检查。绑定有效性和工具授权在执行边界再验证一次,以覆盖执行过程中被解绑或撤权的情况。 + +阶段 D 在 `assistantExecMode` 中加入明确的只读标记,并贯穿工具目录、工具网关和终态草稿处理: + +- **平台工具。** 只读工具继续按项目权限开放;写工具在工具目录和执行网关两处按同一策略拒绝。 +- **外部 MCP 和原生工具。** 按真实能力分类,只开放已确认为只读的能力。CLI 命令、文件写入和其他具有写入能力的工具,在只读模式下一律拒绝。 +- **写草稿。** 模型产生写草稿时,回复“请在平台页面确认操作”。新增渠道首轮只通过只读助手完成状态查询。 +- **模型凭据。** 使用绑定用户自己的模型凭据;用户未配置密钥时提示其配置,不回退到 Bot 创建者的身份。 +- **提示词边界。** 模型输入保留角色层级和用户原始消息,提示词中的输入标记用于区分材料来源。写权限和目标校验都在服务端实施,不依赖提示词。 + +用三类真实项目查询验证上述策略:项目状态、SOP 进度、本人待办。首批真实验收使用普通项目成员账号。 + +## 7. 连接租约与发送状态机 + +### 7.1 锁顺序、连接与交互归属 + +**锁顺序固定为:配置 → 连接 → 交互/投递。** 同一类表有多行时,按主键升序加锁。所有带归属条件的写操作(保存结果、开始发送、写回发送结果、写卡片 task ID、切换会话、恢复、停用、删除、人工补发、人工确认)都在一个事务内按以下步骤执行: + +1. `SELECT … FROM notification_bots WHERE id = ? FOR UPDATE`:读物理列 `enabled`、`interaction_enabled`、`config_version`。 +2. `SELECT … FROM bot_connections WHERE bot_id = FOR UPDATE`:检查 `owner`、`epoch`,以及 `lease_until_us > `。删除、停用和人工操作没有连接 owner,只加锁,不比较 owner。 +3. 按条件更新交互或投递行,检查 `RowsAffected`。 + +```sql +-- 第 3 步示例(MySQL);第 1、2 步已在同一事务内持锁 +UPDATE bot_interactions +SET status = ?, updated_at = ? +WHERE id = ? + AND status = ? + AND lease_owner = ? + AND lease_epoch = ?; +``` + +第 1、2 步任一条件不满足,或第 3 步更新了 0 行,就返回 `ErrLeaseLost` 或明确的状态冲突,执行循环随即停止本地工作。SQLite 没有行锁,以数据库级写锁串行化;`r.locking(tx)` 在 SQLite 上不加子句,测试中语义相同。阶段 C 的评审清单逐个核对每条写路径的加锁顺序;MySQL 测试用两个并发事务复现“删除”与“写回结果”的交错,确认不会死锁。 + +**连接租约。** 继续采用 45 秒 TTL、10 秒续约,写入和判断按第 5.2 节使用数据库时间。 + +- owner 变更或租约过期后重新领取时,原子递增 `epoch`;同一个有效 owner 续约时保持 epoch 不变。 +- 领取交互时保存 `lease_owner`、`lease_epoch` 和 `config_version`。连接相关的 Repo 方法统一以 `bot.LeaseKey()` 为键;领取交互时的配置过滤条件使用 `bot.ID`。 + +**续约失败与恢复。** 接收副本续约失败时,关闭连接,并取消模型和发送的上下文。恢复时: + +- 旧 epoch 下的 `running` 记录,写入现有提示“执行已中断,请核查结果”; +- `ready` 记录继续投递已保存的结果。 + +### 7.2 回复发送、重试和终态 + +```text +queued → running → ready → sending → replied + └──中断──→ ready(中断说明) + +sending → 明确未送达,未达重试上限 → ready(next_send_at_us,保留正文和 key) +sending → 明确未送达,已达重试上限 → reply_failed(undelivered_exhausted) +sending → 平台明确拒绝 → reply_failed +sending → 送达未知(飞书,窗口内) → ready(同一 key,按窗口退避) +sending → 送达未知(飞书窗口已到期 / 企微 / 钉钉) → reply_unknown +ready → 回复路由过期 → reply_failed(route_expired) +reply_unknown / reply_failed → 人工再次发送 → ready(generation + 1,新 key) +reply_unknown → 人工确认已收到 → replied(receipt_source=manual) + +queued → 停用 / 删除 → cancelled(bot_disabled / bot_deleted) +running → 停用 / 删除 → cancelled,并向本地任务发送取消信号;执行记录保留 +ready → 停用 → 保持 ready(暂存,停用期间不领取) +ready → 删除 → cancelled(bot_deleted,正文保留在记录页) +sending → 停用 / 删除 → reply_unknown(bot_disabled_during_send / bot_deleted_during_send) +``` + +**两类自动重试分开处理:** + +| 结果 | 判断依据 | 处理 | +| --- | --- | --- | +| `Delivered` | 平台确认该回复成功 | 记为 `replied`,保存回执。该状态只表示平台确认已发送 | +| `NotDelivered` | 有证据表明请求未提交,或平台明确表示未发送且可重试 | **按次数重试**:首次失败后最多自动重试 3 次,间隔 5 秒、30 秒、2 分钟,`undelivered_retries` 单独计数;耗尽后转 `reply_failed`(通知转 `failed`)。所有渠道一致,重试沿用同一个 key | +| `Rejected` | 平台明确拒绝 | 直接转 `reply_failed`,显示安全错误码。已核对过的限流码或可刷新的鉴权错误,按 `NotDelivered` 处理 | +| `Unknown` | 请求可能已写出:读回执超时、网络中断,或进程在发送阶段退出 | **飞书**:按固定 key 和时间窗口恢复(第 7.3 节),不计入 `undelivered_retries`;窗口到期转 `reply_unknown`。**企微、钉钉**:直接转 `reply_unknown`(通知转 `send_unknown`) | + +发送步骤: + +1. 领取 `ready` 记录,复核绑定、项目、当前配置和回复路由。复核以当前配置为准,要求应用身份、企业范围和启用状态有效,不要求与生成回复时的 `config_version` 相等。这样停用期间暂存的回复,重新启用后仍可发送。 +2. 按第 7.1 节的锁顺序原子写入:`sending`、当前 generation 的首次 `send_started_at_us`(只在为空时写)、发送次数、短发送租约 `send_lease_until_us`。`send_key` 在首次生成回复时就已持久化。 +3. 事务提交后再调用 SDK,网络操作不在事务内。单次发送超时 10 秒;发送前要求连接租约至少还剩 15 秒。 +4. 按平台回执和请求证据分类,带条件写回结果和平台消息 ID。 + +**送达证据。** `WriteJSON` 报错时请求可能已部分写出;HTTP 5xx 也可能出现在平台已经处理请求之后。因此分类依据实际的请求证据,不看错误类型。现有企微“流式失败就主动再发一次”的路径,阶段 C 起只在确认未送达时才启用备用发送。 + +**换主时的发送窗口。** `sending` 状态的交互被新 owner 接管时,保留它原有的发送占用窗口。新 owner 要等原发送租约到期、再加上预留的时钟偏差之后,才执行平台幂等恢复或转为待核对。外部 IM 接口无法检查数据库 epoch,所以靠发送窗口、固定 key 和未知状态三者共同防止重复发送。 + +**停用与删除。** 停用和删除事务都按锁顺序执行: + +- **迟到写回。** 成功写回先提交的,交互保留 `replied`、通知保留 `sent`;停用或删除先提交的,`sending` 记录转为 `reply_unknown` 或 `send_unknown`。旧 worker 迟到的写回会因状态、租约或配置校验失败而停止,迟到的回执写入独立审计。 +- **删除后的待核对记录。** 仍可人工核对、确认已收到;人工再次发送返回 `409 bot_deleted`。删除事务清空回复路由、发送租约和旧的写回凭据,保留正文、generation、key 和已有回执。对话记录保存项目归属快照,项目管理员可以从项目记录页继续查看。 +- **停用期间暂存的回复。** 记录页显示“停用期间暂存”。重新启用后,回复路由仍有效的正常发送,已过期的转 `reply_failed`(`route_expired`)。 +- **停用对通知的影响。** 沿用现有行为:领取时发现停用,记为 `cancelled`;处于 `sending` 的转为 `send_unknown`。 + +### 7.3 固定 key 与人工处理 + +**固定 key。** 首次生成回复时,持久化一个随机 UUID 或等价的不透明 key。自动重试从数据库读取同一个 key、正文和目标。 + +**飞书恢复窗口。** 飞书的 `uuid` 参数提供一小时的请求去重窗口,本方案以当前 generation 的“首次 `send_started_at_us` + 55 分钟”作为恢复上限: + +- **时钟。** 首次时间值由数据库时间写入,恢复和重试资格也用同一个数据库时钟计算。自动重试、连接易主、进程重启都不重置这个值。 +- **每次发送前检查。** 每次调用 SDK 前都重新检查窗口,并要求单次请求的超时也落在上限之内。 +- **退避。** 窗口内依次按 5 秒、30 秒、2 分钟退避,之后最多每 2 分钟一次;到截止时刻转 `reply_unknown`。 + +[飞书固定版本 SDK 的回复去重字段](https://github.com/larksuite/oapi-sdk-go/blob/v3.13.0/service/im/v1/model.go) + +**SDK 自身的重试。** 适配器显式设置 SDK 和 HTTP 客户端的重试策略,确保底层重试沿用同一个 key。没有平台幂等保护的发送,由公共状态机决定是否重试,并关闭 SDK 的自动重发选项。 + +**记录页操作。** 企微和钉钉在缺少已验证的平台幂等能力时,`Unknown` 一律进入 `reply_unknown`。记录页分别提供“确认已收到”和“再次发送”两个操作,“再次发送”会明确提示可能重复。 + +**人工再次发送:** + +- 只接受 `reply_unknown` 或 `reply_failed` 状态的记录。 +- 在事务内校验:操作权限、Bot 和绑定有效、目标和路由仍可用。 +- 校验通过后:状态改为 `ready`,`delivery_generation += 1`,生成新 key,重置发送次数、`undelivered_retries`、`next_send_at_us`,并把当前 generation 的 `send_started_at_us` 置空(首次进入 `sending` 时由数据库填写)。 +- 留下操作人和原因,只投递已保存的结果,之后由正常的发送 worker 领取。 +- 请求的 action key 去重覆盖状态更新,重复点击返回同一个 generation。 + +**人工确认已收到:** + +- 交互从 `reply_unknown` 改为 `replied`,通知从 `send_unknown` 改为 `sent`。 +- 写入 `confirmed_by`、数据库时间 `confirmed_at` 和 `receipt_source=manual`,界面显示“人工确认”。 +- 相关审计字段在阶段 C 一并加入交互和通知模型。 + +**冲突返回。** Bot 已删除时,人工再次发送返回 `409 bot_deleted`;路由已过期时返回 `409 route_expired`。路由过期的钉钉回复,到阶段 G 后可改用主动单聊补发,见第 9.2 节。 + +## 8. 三个渠道的实现细节 + +### 8.1 企业微信 + +**接收。** 阶段 A 起由 `channel_wecom.go` 包装 `dial`、`socket.request`、`stream` 和卡片更新。`socket.request` 的错误现在带有发送阶段,分类规则如下: + +| 情况 | 分类 | +| --- | --- | +| 写出前失败 | `NotDelivered` | +| 写出失败,或已写出后回执超时、连接关闭、上下文取消 | `Unknown` | +| 平台返回非 0 错误码 | `Rejected` | + +阶段 A 只把分类结果映射回原有行为,用户可感知的行为不变: + +- `Delivered` 记为已发送; +- `Rejected` 记为发送失败; +- 其他情况断开重连,之后重发 `ready` 记录; +- 流式失败后仍然备用发送一次。 + +**主动通知。** 继续沿用官方 CLI 的会话发现、目标校验和授权状态。 + +**兼容回归固定以下内容:** + +- 历史外部 Bot ID、绑定 ID、消息 ID、租约 key 的派生结果; +- 私聊、群聊、群内 @、新会话命令与流式进度; +- `waiting`、`failed` 通知卡片的服务端 task ID 和操作者授权检查; +- 主动通知的目标发现、授权过期处理和最终文本; +- 同 key 的平台回执、连接中断、发送结果未知和人工补发。 + +### 8.2 飞书 + +**依赖版本。** 官方 `github.com/larksuite/oapi-sdk-go/v3@v3.13.0`。阶段 A 已在 Go 1.26.11 下用探针模块验证,结论如下: + +- `ws.Client.Start(ctx)` 会阻塞,context 取消后返回 `context canceled`; +- 提供 `Close` 和 `CloseAndWait`; +- 事件 handler 返回错误时,SDK 回复 500,平台据此重投; +- `ReplyMessageReqBody.Uuid`、`CreateMessageReqBody.Uuid`、`tenant/v2` 的 `Query` 均可编译; +- govulncheck v1.9.0 未发现漏洞。 + +依赖在阶段 E 首次实际使用时写入主模块的 `go.mod`、`go.sum` 和 `THIRD_PARTY_NOTICES.md`,避免 `go mod tidy` 移除尚未使用的依赖。 [飞书 Go SDK](https://github.com/larksuite/oapi-sdk-go) + +| 环节 | 实施约定 | +| --- | --- | +| 应用 | 企业自建应用,开启机器人、单聊接收和发送权限,发布给试点成员 | +| Verify | 用 App ID / Secret 获取 `tenant_access_token`,验证凭据。企业归属进入 `awaiting_confirmation`,按下文流程锁定 | +| 企业信息辅助 | 用应用令牌调用 `GET /open-apis/tenant/v2/tenant/query`(官方“获取企业信息”接口,自建应用可用,频率上限 1000 次/分钟)。固定版本 SDK 的 `Tenant` 模型包含 `tenant_key`、企业名称和 display_id。查询结果只作为确认页的辅助信息,与事件候选值并排展示;接口失败(如 `1184001` 无权限)不阻断流程。所需权限以开放平台应用后台的显示为准 | +| Connect | 官方长连接客户端加消息事件 dispatcher,设置 `WithAutoReconnect(false)`,由公共连接循环负责重连。`Start` 返回时关闭 `Conn.Done()`,`Close` 调用 `CloseAndWait` | +| 事件 | `header.app_id`、`header.tenant_key`、`event_id`、`message_id`、`sender.open_id`、`chat_id`、`chat_type`、`message_type`、文本 JSON | +| 原会话回复 | Message Reply 接口,传原 `message_id`、文本和已持久化的 `send_key`(作为 `uuid`) | +| 主动通知 | Message Create 接口,使用当前 Bot 已验证的目标,带独立的投递 key | +| 鉴权与限流 | 从 SDK 返回中提取平台错误码,建立明确的可重试名单;遇到未知错误码时保守地停止自动重发,并显示可诊断的原因 | + +**企业确认流程(`tenantState`)。** 飞书和钉钉共用这套状态:`awaiting_confirmation` → `locked`,异常时为 `conflict`。 + +1. 新配置通过 Verify 后保存 `tenantState=awaiting_confirmation`。接收配置照常占用唯一的 ReceiveSlot,连接可以接收用于发现企业的事件。管理页显示“等待企业确认”。 +2. 适配器从当前凭据认证的 SDK 连接中,读取 App ID 匹配、`tenant_key` 非空的首个消息事件,保存 `tenantCandidate`、事件 ID、接收时间和当时的凭据版本。**写候选字段不增加 `config_version`。** 该事件到此结束: + - 不创建绑定或业务交互; + - 通过一条持久化提示记录回复发送者:“机器人正在完成企业确认,请稍后重发消息。” + - 出现不同的候选值时,状态转为 `conflict`,显示冲突诊断,等待管理员重新发起发现。 +3. Bot 管理员在同源页面确认候选企业:POST `/api/notification-bots/:id/tenant/confirm`,携带候选值和 `configVersion`。事务检查候选来源、当前凭据版本、管理权限,然后写入固定的 `tenant_key`、确认人、数据库确认时间,状态变为 `locked`,`config_version` 加一。因为第 2 步没有改版本号,管理员页面拿到的版本仍然有效;锁定也不触发重连(见第 4.3 节)。 +4. 锁定后,提示测试用户重新发送业务消息。之后接收的每个事件都必须与固定值匹配。轮换同一应用的凭据时保留锁定范围,并继续检查事件归属。要更换应用或已锁定的企业范围,需新建配置。 +5. 阶段 E 的适配器测试、HTTP 测试和真实测试覆盖:发现前、候选已采集、确认并发、凭据轮换、锁定后跨企业消息被拒绝。已取到令牌但尚未锁定的 Bot,在 UI 和 API 中持续显示“企业待确认”。 + +飞书事件样例需覆盖:企业或应用不匹配、同一消息不同投递 ID、应用范围内的 `open_id`、短事务失败、SDK 关闭。接收方使用 `open_id` 时保留其应用范围。 [飞书身份范围](https://open.feishu.cn/document/platform-overveiw/basic-concepts/user-identity-introduction/introduction) + +### 8.3 钉钉 + +**SDK 只用一部分。** 依赖为官方 `github.com/open-dingtalk/dingtalk-stream-sdk-go@v0.9.1`,这是当前最新的稳定版,另有 `v0.9.2-beta.1`。阶段 A 的探针验证发现以下问题,因此**阶段 F 不使用 SDK 的 `StreamClient` 连接循环和 `ChatbotReplier`**: + +| 问题 | 证据 | 影响 | +| --- | --- | --- | +| 心跳超时后再收到一帧,未恢复的 panic 会使进程退出 | 探针中服务端不回 pong,10 秒后推送一帧文本,子进程以 `send on closed channel` 退出(exit 2)。原因是 `processLoop` 退出时关闭了 `readChan`/`closeChan`,而读协程和 pong 超时协程仍会向它们写入 | 宿主进程整体崩溃,所有项目的服务中断 | +| `Start` 建连不接受 context | 内部调用 `dialer.Dial`,而不是 `DialContext` | 停止时可能卡住 | +| 自动重连使用 `context.Background()` 无限循环 | `reconnect` 实现 | 调用 `Close` 后仍可能重新连上,形成僵尸连接 | +| `ChatbotReplier` 使用默认重定向策略、固定 5 秒超时,200 响应未关闭 body | `ReplyMessage` 实现 | 违反“禁止重定向”约定,并泄漏连接 | + +阶段 F 只采用该 SDK 中经过验证、无副作用的部分: + +- 用 `payload.ConnectionEndpointRequest/Response` 获取接入点(实施时改为自建请求:SDK 的 `GetConnectionEndpoint` 挂在 `StreamClient` 上,使用默认传输和重定向策略,与本节的 HTTP 约定不符); +- 用 `payload` 包解析 DataFrame、构造确认帧; +- 用 `chatbot.BotCallbackDataModel` 解析事件。 + +WebSocket 读写循环、心跳和重连由适配器基于主模块已有的 `gorilla/websocket v1.5.3` 实现,与企微连接的生命周期一致。 + +会话 Webhook 和主动发送使用自建的 HTTP 客户端:禁止重定向,设置明确超时,限制响应读取长度。如果上游修复了上述问题并发布稳定版,再评估是否改回 SDK 循环。 + +**凭据校验。** 该 SDK 不提供 access token 接口,Verify 直接调用官方 `POST https://api.dingtalk.com/v1.0/oauth2/accessToken`。 [钉钉 Stream Go SDK](https://github.com/open-dingtalk/dingtalk-stream-sdk-go) + +| 环节 | 实施约定 | +| --- | --- | +| Verify | 用官方 accessToken 接口校验 Client ID / Secret | +| 企业确认 | 复用第 8.2 节的 `tenantState`。管理员必须手填企业 CorpId,作为预期值保存,状态为 `awaiting_confirmation`。首个可信事件的企业字段与预期值一致时,自动转为 `locked`,记录事件 ID 和数据库时间,确认人记为填写 CorpId 的管理员,`config_version` 加一;不一致时转为 `conflict`,不处理业务,由管理员修正后重新发现。等待确认期间的消息回复与飞书相同的提示 | +| Connect | 自建 Stream 读写循环,注册机器人回调 topic;事件入库成功后回复成功确认帧,入库失败时回复错误帧 | +| 发送者 | 优先读取经过验证的 `senderStaffId`,与当前企业范围组合;字段缺失时拒绝业务执行并记录诊断 | +| 事件 | `msgId`、`conversationId`、`conversationType`、`chatbotCorpId`、`senderCorpId`、`msgtype`、`text.content` | +| 应用归属 | 绑定当前应用的连接。平台原始事件中带有 `robotCode` 时,解析并核对;SDK 模型未暴露的字段,用原始 payload 的解析测试覆盖 | +| 会话回复 | 保存 `sessionWebhook` 的密文和 `sessionWebhookExpiredTime`,后者按毫秒解释,转成 `route_expires_at_us`;发送前留出超时的安全余量 | +| 到期 | 保存业务结果,原因记为“会话回复授权已过期”,状态为 `reply_failed`(`route_expired`),可在记录页查看结果 | +| 主动通知 | 使用官方机器人单聊主动发送接口,发给已验证的本企业员工目标,不依赖 sessionWebhook | + +`v0.9.1` 的公开事件模型没有定义 `robotCode`,因此应用归属校验基于实际的连接上下文和原始事件契约实现。`chatbotCorpId` 的格式与配置中 CorpId 的对应关系,在真实试点验证后记录到渠道手册。 [钉钉固定版本事件模型](https://github.com/open-dingtalk/dingtalk-stream-sdk-go/blob/v0.9.1/chatbot/model.go) + +会话 Webhook 只接受可信 Stream 事件给出的平台 HTTPS 地址:校验官方允许的主机和路径,禁止重定向,固定请求方法和内容。用户输入和模型结果都不能提供该地址。发送进入终态后,按数据保留策略清理授权 URL。 + +## 9. 助手与主动通知 + +### 9.1 助手接入 + +新增渠道首轮通过 `generateBotReply` 进入现有助手,复用 `openAssistantToolSession`、`botReplyMessages` 和平台工具网关。渠道适配器只负责传输,项目和用户权限仍由服务端确定。 + +**写策略:** + +- 企微旧 Bot 的 `auto` 策略通过兼容分支读取。 +- 新建 Bot 一律采用 `readonly`。 +- 阶段 G 为企微新增可选的 `confirm` 策略:写草稿保存后,在 IM 中返回操作摘要和 `/bot-approve.html#draft=` 链接。 + +**网页确认。** 要求登录,并检查绑定用户、Bot 配置、项目权限、草稿有效期和冻结参数。 + +- 草稿使用随机 ID。确认请求(POST)原子地把状态从 `pending` 改为 `executing`,并把草稿的执行 key 传给实际的业务写入。 +- 执行中断的草稿进入待核对;用户重复点击时,返回当前的执行状态。 +- 解绑、停用、删除 Bot 或撤销权限,都会使 `pending` 状态的草稿失效。 +- 草稿执行复用现有的 `resolveAssistantDrafts` 和预检逻辑。网页确认相关的改动放在 `AssistantRepo` 和 `assistantHandlers`。 +- 飞书、钉钉的 `confirm` 模式,在后续按同一契约验收后开放。 + +### 9.2 SOP 主动通知 + +`notification.Service` 保留现有的投递任务、报告生成和 `lease_token`。飞书、钉钉通过 `PushSender` 发送,在只启用通知、未启用对话的配置下也能工作。 + +1. 在用户已绑定的可信私聊中记录目标元数据;管理页从本 Bot 已知的会话中选择目标。 +2. 服务端试发成功后写入 `target_verified_at`,并绑定当时的配置版本和实际平台目标。 +3. SOP 用原有的状态事件生成通知,覆盖现有枚举 `succeeded`、`failed`、`waiting` 的全部取值。 +4. 发送前重新读取 Bot 和目标,保留投递记录中冻结的项目、目标和 key,按第 7 节的规则分类结果。通知的未知送达结果使用 `send_unknown`。 +5. 处理节点等待通知时,复用 `waitingNodeStale` 和原有的轮次校验;节点已经推进的,取消过时通知。 + +**目标维护:** + +- Bot 更换 Secret 后,重新验证原应用身份,目标验证状态随轮换结果一并更新。 +- 切换目标时,旧的待发任务要么显式取消,要么由人工选择迁移,操作都记录下来。 + +**钉钉的额外限制:** + +- 钉钉的通知配置,只有在独立主动发送接口真实验证成功后才能启用。 +- 管理页分别展示接收连接健康、会话回复能力和主动发送权限。 + +**钉钉过期回复改用主动单聊补发(阶段 G)。** 前提是该 Bot 的主动单聊发送已通过真实验证,即 `target_verified_at` 有效。满足前提时,对于状态为 `reply_failed` 且原因是 `route_expired` 的钉钉私聊回复,管理员可以在记录页**显式选择**“改用主动单聊发送”。服务端在事务内重新校验以下几项: + +- 操作者对该 Bot 的管理权限; +- 原发送者的绑定仍然有效,并且对项目仍有权限; +- 目标是本 Bot 已验证的、属于原发送者的单聊目标,且属于同一企业范围。 + +校验通过后,按人工再次发送的规则处理:`delivery_generation += 1`,生成新的发送 key;回复路由切换为主动发送,只投递已保存的结果;记录操作人、原因和路由切换。没有已验证目标时,该按钮不显示,接口返回 `409 push_target_unverified`。 + +### 9.3 阶段 G 实现说明(与上文的差异) + +- **目标验证记录在绑定上。** 第 5.1 节把 `target_verified_at` 放在 `bot_conversations`。实现放在 `bot_user_bindings`:飞书、钉钉的通知目标就是在本 Bot 单聊中绑定的成员(飞书 open_id、钉钉 staffId),解绑或重新绑定会清除验证,比按会话记录更直接。配置上的 `target` 只保存所选成员,展示时从绑定读取验证状态。 +- **验证不绑定凭据版本。** 应用标识创建后不可修改,更换 Secret 时先校验同一应用,因此验证在轮换后继续有效,不随凭据版本失效。 +- **业务写入没有幂等键。** 第 9.1 节要求把执行 key 传给业务写入;平台写入接口目前没有幂等参数。实现中执行 key 只记录在草稿上,“只执行一次”由 `pending → executing` 的加锁状态迁移保证,执行中断的草稿转为待核对、不再执行。 +- **切换接收人的处理只覆盖飞书、钉钉。** 企微切换接收会话的行为保持不变,以免改变已有配置的使用方式。 + +## 10. 阶段任务清单 + +| 阶段 | 具体改动 | 必须通过的验证 | +| --- | --- | --- | +| A | 定义渠道契约,包装企微,接入模拟渠道,提取公共入站入口,分类发送结果;用探针模块验证 SDK 固定版本 | 既有企微用例全部通过;模拟渠道能控制接收、回执和断线;探针记录 SDK 生命周期、确认行为和漏洞扫描结果 | +| B | 领域字段和标识方法、物理列(含启停和版本)、`dbNowUS` 方言函数、凭据加密、生命周期事务、CLI 会话映射表及启动前写入、维护开关、手动 SQL、配置 UI | 历史键值逐字相等;并发保存时只启用一个接收配置;SQLite 和 MySQL 数据库时间比较测试通过;新 Bot 的原生 CLI 运行可追溯;密钥轮换和迁移重入通过 | +| C | epoch、锁顺序和归属条件、发送 key、两段式发送、短发送租约、两类重试、停用和删除语义、状态枚举、人工操作 | 旧 owner 失效后无法保存结果;`sending` 恢复遵守窗口;未知状态不会误记为成功;重试耗尽转失败;停用保留 `ready`;删除与写回并发时不死锁 | +| D | 两阶段绑定元数据与限频、项目权限复核、只读工具策略 | 换人点击链接无法完成原发送者的绑定;撤权生效;写工具与原生写入被拒绝 | +| E | 飞书 Verify / Connect / Reply、企业确认(含企业信息辅助查询)、Bot 配置表单、官方事件 fixture;写入依赖 | 锁定前只采集候选;锁定后在真实租户完成绑定、状态查询、同消息去重和重启恢复 | +| F | 钉钉自建 Stream 循环、企业确认(手填 CorpId)、会话路由密文与过期;写入依赖 | 真实企业通过与飞书相同的用例;过期回复可在记录页核对结果;连接异常不导致进程退出 | +| G | 独立主动发送、三类 SOP 通知、企微 confirm、身份撤销联动、钉钉过期回复改用主动单聊补发 | 通知专用 Bot 发送成功;过时的等待通知被取消;重复确认只执行一次;改用主动发送重新校验并使用新 key | +| H | 分批清理、安装配置、迁移与回退手册、最终测试记录 | 三个渠道的实际验收结果可追溯;CI、API 和前端文档与最终实现一致 | + +**数据保留。** 暂定正文保留 90 天、终态元数据保留 180 天,可通过配置调整。每小时分批清理一次,每批 1000 条,按终态白名单和实际完成时间筛选。`running`、`ready`、`sending` 以及待核对的记录,保留到人工处置或明确的业务过期为止。 + +**CLI 会话日志:阶段 B 建映射表并持续写入,阶段 H 实现清理。** 同一个 Bot 逻辑会话可能对应多个消息轮次和多个原生 session,因此映射以“交互 + 请求 session”为单位保存,支持一对多。 + +- **启动前写入。** 在 `generateBotReply` 生成 `cliSessionID` 之后、调用 `runAssistantCLI` 启动进程之前,把 `config_id`、`project_id`、`user_id`、`conversation_id`、`bot_session_id`、`interaction_id`、`engine`、`requested_session_id` 原子写入 `bot_native_sessions`。写入失败时,本轮不启动 CLI,并在交互记录中写明追踪错误。 +- **补全实际 session。** 通过已有的原生事件解析逻辑和 `runAssistantCLI` 的返回值,更新实际的 session ID;进入终态时补充完成时间。 + - 重试时恢复了同一个原生 session 的,复用原映射;实际新建了 session 的,新增一条映射。 + - 原生 session 尚未确认、或进程中断的映射,保留其追踪状态。 +- **清理范围。** 保存服务端确定的存储范围和相对日志路径,按当前用户实际的 CLI HOME 解析,并验证归属。清理只处理同时满足以下条件的 Bot 原生日志:已终结、超过保留期、能由映射明确定位。逐文件记录清理结果和错误。 +- **历史日志。** 升级前产生的原生日志,只为有可信记录、能精确关联的 session 生成历史映射,其余列入 `legacy_unmapped` 清单,等待人工核查。共享 HOME 下网页助手和其他来源的日志,按各自的归属处理。 + +阶段 B 的验证覆盖:启动前映射落库、映射写入失败、实际 session 与请求 ID 不同、同一 Bot 的多轮会话、崩溃记录。阶段 H 在临时目录构造混合来源的日志,验证只删除映射指向的到期 Bot 文件。 + +### 10.1 阶段 H 实现说明(与上文的差异) + +- **清理范围包含企微已有数据。** 保留规则作用于 `bot_interactions`、`notification_deliveries` 和 `bot_pending_drafts` 的全部终态记录,不区分渠道,升级后首轮清理就会处理历史企微记录。手册第 2 节第 7 步要求先确认保留设置。 +- **通知记录的完成时间。** `notification_deliveries` 没有完成时间列,用“创建时间、最后一次尝试时间、发送时间都早于期限”判断,最近重试过的记录不会被提前清理。 +- **每轮批次上限。** 每类每轮最多 10 批(每批 1000 条),避免升级后积压数据导致首轮长时间占用数据库。 +- **飞书、钉钉开放网页确认。** 第 9.1 节原定飞书、钉钉只读;2026-11-20 按验收负责人要求,飞书、钉钉也可以选“网页确认”,以便在单聊中创建需求、派发或启动 SOP。机制与企微相同,确认链接在纯文本中单独一行;“自动执行”仍只对企微开放。 +- **原生日志按文件名匹配。** 映射没有在运行时记录日志路径;清理时按请求和报告的 session ID 在该用户的 CLI HOME 中查找(CodeBuddy、Claude Code、Codex),删除后把相对路径写回 `log_path`。其他 CLI 记为 `unsupported`,留给人工核查。 + +## 11. 测试与验收 + +### 11.1 自动化 + +| 层级 | 覆盖 | +| --- | --- | +| 适配器 | 官方 fixture、消息字段、应用或企业不匹配时拒绝、非文本与超长输入、回复 key、发送阶段分类、WebSocket/Stream 生命周期;钉钉自建循环在心跳超时、断线和关闭时不 panic | +| Repo | 原企微主键不变、跨 Bot 同名 ID、一次性绑定、多副本抢占、epoch、更新 0 行、删除事务、唯一索引冲突、`dbNowUS` 时间比较、锁顺序 | +| 服务 | 同一消息重复投递;发送前、发送中、发送后崩溃;生成结果只保存一次;`sending` 恢复;删除与迟到回执的竞态;停用后 `ready` 暂存与恢复;明确未送达重试耗尽;人工补发回到 `ready`;数据库时钟下的飞书窗口边界;钉钉路由到期;通知专用配置 | +| HTTP | Bot 越权访问、跨项目使用交互 ID、凭据校验失败、配置 CAS、企业确认 CAS、同源绑定、人工补发的权限与幂等、草稿确认 | +| 前端 | 渠道切换、Secret 状态、连接与发送能力显示、企业待确认与冲突、绑定确认、补发按钮、出错后保留输入 | +| 真实 CLI | 只读模式下的平台工具、外部 MCP、原生命令与文件写入约束;Bot 原生 session 映射与混合来源日志的清理;使用现有网关的契约测试 | + +MySQL 测试验证并发更新和唯一索引,SQLite 测试覆盖公共业务。租约竞态测试使用可控的数据库时钟和发送屏障,复现以下时序:旧进程在 `sending` 之后暂停,新进程接管,旧进程再恢复。 + +开发使用仓库当前的 Go 版本:`go.mod` 要求 `1.26.11`。先跑改动涉及的包和相关前端测试,再按贡献要求和 `platform` CI 完成全量检查。 [贡献要求](../../CONTRIBUTING.zh-CN.md) · [平台 CI](../../.github/workflows/platform.yml) + +```bash +# 在 IM 开发目录执行 +go test -p=2 ./internal/botinteraction ./internal/notification ./internal/repo ./internal/httpapi -count=1 -timeout=600s +go vet ./... +npm --prefix webtest test +python3 scripts/check-public.py + +# 构建嵌入的前端,再跑全量 +bash scripts/build-chatbot.sh +go test -p=2 ./... -count=1 -timeout=600s + +# 一次性本机 MySQL 测试库 +make test-mysql + +# SDK 探针(独立模块) +(cd scripts/im-sdk-probe && go test -count=1 ./... && go run golang.org/x/vuln/cmd/govulncheck@v1.9.0 ./...) + +# Linux CI 执行仓库提供的扫描与覆盖率流程 +bash scripts/scan-secrets.sh +``` + +`scan-secrets.sh` 目前下载 Linux x64 二进制,在对应的 CI 环境中运行。覆盖率和真实浏览器测试沿用 `platform.yml` 的分组与门禁。依赖变更时,更新第三方来源声明,并执行 CI 的漏洞检查。 + +### 11.2 真实验收清单 + +| 编号 | 场景 | 通过标准 | +| --- | --- | --- | +| IM-01 | 新建和轮换 Bot | 渠道字段、凭据校验、企业确认状态、连接与发送能力都可见;失败时保留原配置 | +| IM-02 | 两阶段绑定 | 平台登录和原 IM 发送者都确认后才生效;确认码过期或跨 Bot 使用被拒绝 | +| IM-03 | 原会话查询 | 普通成员查询当前项目状态,在原会话中收到以正确身份执行的回复 | +| IM-04 | 重复入站 | 同一业务消息重复推送只产生一条交互,模型和业务只启动一轮 | +| IM-05 | 重启与易主 | `queued` 恢复执行;`ready` 只发送已保存的结果;`running` 中断后可核查;旧 owner 无法覆盖结果 | +| IM-06 | 发送结果未知 | 平台实际已接收而本地超时时,状态正确,并按渠道的幂等能力处理 | +| IM-07 | 只读与撤权 | 写工具被阻止;解绑和项目权限撤销对下一次工具执行生效 | +| IM-08 | 企微回归 | 旧机器人、旧绑定、群聊、流式进度、卡片与主动通知全部通过;备用发送的变化按下方专项验收 | +| IM-09 | 钉钉回复到期 | 授权过期后记录原因,已生成的结果可查看,重发按钮按路由有效期处理;阶段 G 后可显式改用主动单聊补发 | +| IM-10 | 主动通知 | 三个渠道都收到 `succeeded`、`failed`、`waiting` 通知,过时的等待通知被取消 | +| IM-11 | 人工补发 | `reply_unknown`、`reply_failed` 原子变回 `ready`,generation 加一且 key 更新;沿用已保存的业务结果,记录操作者,相同 action key 去重 | +| IM-12 | 数据升级和回退 | 旧记录可读、密钥可轮换、迁移可重入;维护窗口按“先停旧副本、再以关闭 worker 的配置启动新版本”执行;阶段 B 起新增的 CLI 会话都有映射;历史未映射清单保留;按版本与数据能力执行回退 | + +**IM-08 备用发送专项**,单独保存证据: + +1. 传输前已确认未发送,或平台明确拒绝且该错误允许走备用路径时:验证主动发送最多补发一次,并记录原路径和备用路径的结果。 +2. 流式回复已被平台接收、但本地回执超时时:验证记录进入 `reply_unknown`,平台发送调用只计一次,页面明确提示“送达结果待核对”。 +3. 在上一场景中模拟用户实际未收到:验证管理员能从记录页查看已保存的回复,并可确认或人工再次发送。再次发送使用新的 generation,并提示可能重复。 +4. 发送中删除 Bot:验证 `sending → reply_unknown`,旧 worker 迟到的结果无法覆盖;项目管理员可以核对,补发操作返回 `bot_deleted`。 +5. 发送中停用 Bot:验证 `sending → reply_unknown`,`ready` 状态的记录保留为暂存,重新启用后路由有效的正常发送。 + +**飞书窗口专项**:用可控的数据库时钟模拟以下情况——首次发送后 54 分 59 秒、55 分钟、实例重启、两个副本的本地墙钟存在偏差。检查首次发送时间不被重置;已达上限,或剩余时间不足一次请求的超时时,转为待核对。 + +每个平台保存一组测试企业、测试项目和普通成员的脱敏证据,记录配置版本、消息 ID、交互 ID、平台回执、实际结果和测试时间。报告使用“通过 / 失败 / 未运行 / 阻塞”四种状态。 + +## 12. 上线执行与交接 + +**上线说明与 `CHANGELOG.md` 需写明三处用户可感知的变化:** + +1. **企微备用发送。** 原行为是流式失败后主动再发一次;新行为是只对确认未送达的错误执行备用发送,送达结果未知的显示“待核对”。用户可能需要到记录页查看已保存的回复,再选择人工补发。发布说明附 IM-08 专项结果、记录页入口和客服处理步骤。 +2. **新建企微 Bot 默认只读。** 新建的企微 Bot 默认 `writeApproval=readonly`,不再自动执行写操作;已有企微 Bot 保持原有策略。需要写操作的项目,由管理员在 Bot 配置中显式选择(阶段 G 起可选 `confirm`)。 +3. **停用 Bot 的处理。** 停用时取消正在执行的任务;已生成的回复暂存,重新启用后发送;发送中的记录进入待核对。 + +**试点顺序。** 依次启用:企微兼容版本 → 一个项目的飞书 → 一个项目的钉钉 → 主动通知。迁移按第 5.4 节执行,所有副本同步配置密钥和新租约协议。公开地址沿用 `AGENTFLOW_PUBLIC_URL`,确保用户能从 IM 打开绑定页和运行页。 + +```dotenv +AGENTFLOW_PUBLIC_URL=https://loopforge.example.com +AGENTFLOW_CREDENTIAL_KEYS=: +AGENTFLOW_BOT_WORKERS_ENABLED=true +AGENTFLOW_BOT_MESSAGE_RETENTION_DAYS=90 +AGENTFLOW_BOT_METADATA_RETENTION_DAYS=180 +``` + +**运维与可观测:** + +- 机器人的 App ID、Secret、企业范围由管理页面保存。 +- 日志按项目、渠道、内部 Bot ID、交互或投递 ID 关联,记录安全错误码、发送阶段和平台回执。 +- 建立以下可观测指标:连接异常、最老的 `queued` 消息、`ready` 积压、发送结果未知、凭据失败。 + +**交接物:** 本方案、`docs/im-channels.md`、API 文档和 OpenAPI、手动迁移脚本、配置样例、第三方依赖说明、SDK 探针、测试结果和真实渠道的验收记录。最终 PR 按 IM-01 至 IM-12 逐项列出状态,并保留各阶段提交供审阅。 + +**待核实的外部条件:** + +| 条件 | 核实动作 | 对应阶段 | 状态 | +| --- | --- | --- | --- | +| SDK 版本和关闭/确认接口 | 用固定 tag 编译探针,核对源码和实际行为 | A | 已完成。飞书可用;钉钉连接循环不可用,改为只用部分 SDK(第 8.3 节);复现步骤和调用栈见 `scripts/im-sdk-probe/README.md` | +| 飞书企业信息接口 | 核对官方文档和固定版本 SDK 的模型 | A | 已完成。接口与字段已确认,所需权限在应用后台核实 | +| 飞书企业范围与权限 | 准备一个自建应用,完成可信事件发现、管理员确认 tenant key,再做单聊收发 | E | 代码完成;最小权限清单见 `docs/im-channels.md` 4.1,真实租户待验 | +| 钉钉企业标识和机器人字段 | 保存脱敏的原始事件,确认 CorpId、应用和发送者之间的对应关系 | F | 待准备 | +| 钉钉主动发送权限 | 用实际的机器人 API 完成一次本企业单聊通知 | G | 待准备 | +| 企微历史绑定归属 | 跑只读 SQL 清单,核对重复 Bot、孤立绑定和现有通知配置 | B、上线前 | 待执行 | +| 目标基线变化 | 合并前对照目标分支复核本文列出的文件和迁移编号 | 每阶段 | 已核对 `e89cda9` | + +本方案以三渠道接入为目标,飞书先行试点,新增渠道首轮只读。各阶段的实现和真实租户的验收结果,由本分支后续提交补齐。 diff --git a/docs/testing/im-final-test-record.md b/docs/testing/im-final-test-record.md new file mode 100644 index 000000000..75229b390 --- /dev/null +++ b/docs/testing/im-final-test-record.md @@ -0,0 +1,122 @@ +# 企业 IM 接入最终测试记录(阶段 A–H) + +对应方案 `docs/proposals/enterprise-im.md` 第 10、11 节,操作手册 `docs/im-channels.md`。状态取值:通过、失败、未运行、阻塞;验收负责人整体确认、没有逐项回报的,单独标注。时间均为北京时间。 + +## 1. 版本与阶段 + +| 项 | 值 | +| --- | --- | +| 分支 | `feature/enterprise-im`,基线 `origin/main@e89cda9`,2026-11-20 合入 `origin/main@6f606cf` | +| 最终代码提交 | `6df93df`(合并提交;此后只有文档提交) | +| 渠道 | 企业微信(对话、通知、网页确认)、飞书(私聊只读、通知)、钉钉(私聊只读、通知) | + +| 阶段 | 主要提交 | 内容 | 验收 | +| --- | --- | --- | --- | +| A | `e8f24e3`、`edd060a`、`386a7b1`、`addcaa1` | 渠道契约、包装企微、SDK 探针(独立模块) | 自动化 | +| B–D | `7fc24c1` | 配置作用域、物理列、凭据加密、租约 epoch、两段式发送、两阶段绑定、只读 Bot、迁移 036/037 | 自动化;预发升级演练(R1 记录第 3 节) | +| E(R1) | `5d52656`、`57c5450`、`b533691`、`e148fe1` | 飞书私聊闭环 | 2026-11-18 验收通过,[R1 记录](im-r1-acceptance.md) | +| F(R2) | `f9c94e0`、`64e1619` | 钉钉 Stream 私聊闭环 | 2026-11-19 验收通过,[R2 记录](im-r2-acceptance.md) | +| G | `32e8eea`、`03f9541`、`74bbc34`、`f86881a` | 飞书、钉钉主动通知;钉钉过期回复改用主动单聊;企微网页确认 | 2026-11-20 验收通过(未逐项回报),[阶段 G 记录](im-stage-g-acceptance.md) | +| H | `859e0bf`、`96efdf8`、`6df93df` | 数据保留与原生日志清理、安装配置、合入主干、迁移与回退手册 | 本记录 | + +## 2. 最终代码的自动化结果(`6df93df`,2026-11-20) + +| 范围 | 环境 | 结果 | +| --- | --- | --- | +| `go build ./...`、`go vet ./...` | Go 1.26.11 | 通过 | +| `gofmt -l internal cmd` | — | 本分支改动的文件全部通过;列出的 `internal/domain/workflow.go`、`internal/runtime/workspace_preparation_test.go` 在 `origin/main` 上同样未格式化,本分支未改动 | +| `go test -p=2 ./...`(先执行 `scripts/build-chatbot.sh`) | 本机 SQLite | 除下面两项外全部通过 | +| ↳ `cmd/agentflow` `TestSDNotifySendsReadyAndMainPID` | 本机 | 失败,环境相关:本机临时目录路径过长,unix 套接字绑定报 `invalid argument`;在 `origin/main@6f606cf` 上同样失败 | +| ↳ `internal/runtime` 7 个 Docker 用例 | 本机 | 失败,本机没有 Docker;在 `origin/main@6f606cf` 上同样失败 | +| `internal/repo` 全部 MySQL 用例 | MySQL 8.4.13 | 除 `TestMySQLReadsObserveExternalChangesWithoutReload` 外全部通过;该用例在基线上同样失败 | +| 本分支的 MySQL 用例:租约竞争、阶段 G 语句、数据保留 | MySQL 8.4.13;MariaDB 10.11 | 通过 | +| 飞书故障测试 F1–F6、钉钉故障测试 D1–D5 | MySQL 8.4.13 | 通过(123 秒、65 秒) | +| 数据保留:保留期边界、只清终态、正文清理不改更新时间、清理后不能重发或重试、过期草稿、分批 | SQLite;MySQL 8.4.13;MariaDB | 通过 | +| 原生日志清理:混合来源目录只删映射指向的到期文件;其他用户、网页助手、未到期、符号链接指向 HOME 外的文件都保留;Codex 按线程 ID 匹配并清理线程索引;未登记的 CLI 记为 `unsupported`;共享 session 跳过 | 本机临时目录 | 通过 | +| 前端 `webtest`(`npm run coverage`) | Node 22.23.5 | 222 个通过,行覆盖率 87.89%(门槛 85%) | +| `scripts/test-install.py` | 本机 | 50 个通过 | +| `scripts/check-public.py`、`scripts/collect-go-notices.py` | 本机 | 通过(95 个模块) | +| `govulncheck ./...` | 本机 | 代码不受影响(依赖中 1 个漏洞,未被调用) | +| SDK 探针 `scripts/im-sdk-probe`:测试与 `govulncheck` | 本机 | 通过;无漏洞 | +| OpenAPI | 本机 | YAML 可解析;`server.go` 中所有 `/api/notification*`、`/api/bot*` 路由都有条目 | +| Go 覆盖率门禁、真实浏览器用例、`scan-secrets.sh`、Playwright e2e | — | 未运行:依赖 CI 的 Linux 环境和浏览器,由 PR 的 `platform` 与 `e2e` 工作流执行 | + +## 3. 最终版本升级与回退演练 + +环境:本机 MySQL 8.4.13。旧版本为 `origin/main@6f606cf` 构建的二进制,新版本为 `6df93df` 构建的二进制。时间 2026-11-20 01:07–01:12。 + +**升级前数据**(旧版本 API 写入,另用 SQL 补入历史行):4 个企微配置(含共用 Bot ID 和一次被拒的重复启用);4 条绑定(正常、共用、孤立、未完成);6 条交互:200 天前和 100 天前已回复各 1 条、近期已回复、回复失败、待发、执行中各 1 条;4 条通知发送记录:200 天前已发送、200 天前仍待发、100 天前失败、近期已发送;用户 CLI HOME 里一个升级前的原生日志文件。备份 `mysqldump --single-transaction`,84 KB。 + +| 步骤(手册第 2 节) | 实际结果 | +| --- | --- | +| 3. 新版本以 `AGENTFLOW_BOT_WORKERS_ENABLED=false` 启动 | worker 关闭;连接 `offline`;保存配置返回 `503 bot_maintenance`。**数据保留没有运行**:6 条交互、4 条通知原样保留,日志无 `bot retention` | +| 4. 036,执行两次 | 15 个新列 `present=1`;`wecom` 配置 4 个、接收占位 2 个、未回填 0;交互缺项目 0。两次输出相同。200 天前交互的 `updated_at` 未被改动 | +| 5. 037,执行两次 | 共用和孤立绑定各列出 1 条;唯一归属 2 条已回填;`legacy_unmapped` 列出 4 条。两次输出相同 | +| 6. 凭据 | `plaintext=4` → `reencrypt` 改写 4 条 → 再次执行跳过 4 条 → `plaintext=0 kid_h1=4`;库中无明文 | +| 8. 开启 worker 重启(默认 90/180 天) | 日志 `bot retention: interaction bodies=2 interactions=1 notification bodies=2 notifications=1 …`。200 天前的已回复交互和已发送通知被删除;100 天前的交互正文清空、`body_purged_at` 已记录、`updated_at` 仍为原值;100 天前失败的通知正文清空、`content_source=purged`,重试返回 409;近期记录、待发、执行中的交互保持原样;200 天前仍待发的通知未被删除(由通知 worker 按原规则处理为过期);升级前的原生日志文件仍在 | +| 新增结构 | `bot_interactions.body_purged_at`、`send_route`,`notification_deliveries.send_key`、`receipt_source`,`bot_native_sessions.cleanup_detail`、`cleaned_at`,`bot_user_bindings.target_verified_at` 已由启动时 DDL 补齐;`bot_pending_drafts` 为 `utf8mb4_unicode_ci` | +| 回退 A:旧版本直接运行在已迁移的库上 | 能启动、读出配置并占用连接,但 Secret 已是密文,旧版本无法使用。**不支持** | +| 回退 B:恢复备份后启动旧版本 | 4 张表行数与升级前一致(交互 6 条,含被保留清理删除的记录),Secret 为明文,库中没有新列;旧版本读出 4 个 Bot 并占用连接 | + +本机没有企微真实回调,企微连接停在“重连中”,与 R1 演练一致。 + +## 4. 真实验收清单的追溯(方案第 11.2 节) + +| 编号 | 企业微信 | 飞书 | 钉钉 | 证据 | +| --- | --- | --- | --- | --- | +| IM-01 新建和轮换 Bot | 回归项,未单独执行 | 通过(R1-01、R1-02) | 通过(R2-01、R2-02) | R1、R2 记录;轮换未在真实环境执行,有自动化 | +| IM-02 两阶段绑定 | 回归项,未单独执行 | 单人路径通过(R1-05 前置),跨人未运行(豁免) | 单人路径通过(R2-04) | `64e1619` 修复了纯文本渠道的链接 | +| IM-03 原会话查询 | 回归项,未单独执行 | 通过(R1-05 得到自然语言回复) | 通过(R2-02 首条消息得到回复) | 三类查询未逐项报告 | +| IM-04 重复入站 | 未运行 | 未运行(豁免),F1、F2 通过 | 未运行(豁免),D1、D2 通过 | 故障测试 | +| IM-05 重启与易主 | 演练第 8 步:执行中改为“已中断”,待发保留 | 未运行(豁免),F4、F5 通过 | 未运行(豁免),D3、D4 通过 | 故障测试、本记录第 3 节 | +| IM-06 发送结果未知 | 未运行 | 未运行(豁免),F5、F6 通过 | 未运行(豁免) | 自动化 | +| IM-07 只读与撤权 | 回归项 | 写请求被拒通过(R1-05);撤权未运行(豁免) | 未运行(豁免) | 只读策略与撤权测试 | +| IM-08 企微回归与备用发送 | 未运行(豁免,R1-12) | — | — | 单测;预发中旧 Bot 可读、可解密、可占用连接 | +| IM-09 钉钉回复到期 | — | — | G-07 确认通过(未逐项回报) | D5、阶段 G 服务层测试 | +| IM-10 主动通知 | G-10 回归确认通过(未逐项回报) | G-01、G-03 确认通过(未逐项回报) | G-02、G-03 确认通过(未逐项回报) | 阶段 G 记录 | +| IM-11 人工补发 | 未运行 | 未运行 | G-07 确认通过(未逐项回报) | 自动化 | +| IM-12 数据升级和回退 | 通过(演练环境:R1 第 3 节、本记录第 3 节) | 同左 | 同左 | 团队预发库按 R1 时的手册执行过一次;最终版本仅在演练环境执行 | + +**数据保留(阶段 H)没有真实环境验收项**:默认 90 天才开始清理,预发环境里没有这么久的数据。建议在预发库上临时设置 `AGENTFLOW_BOT_BODY_RETENTION_DAYS=1`、`AGENTFLOW_BOT_RECORD_RETENTION_DAYS=2` 观察一轮,再恢复默认值。 + +## 5. 飞书、钉钉网页确认(2026-11-20 追加) + +按验收负责人要求,飞书、钉钉的“执行权限”增加“网页确认”,用于在单聊中创建需求、派发或启动 SOP。“自动执行”仍只对企业微信开放。 + +自动化:保存配置时飞书、钉钉接受“只读”和“网页确认”,拒绝“自动执行”;飞书、钉钉各跑一遍网页确认:确认前不执行、回复为纯文本且链接单独一行、本人确认后只创建一次、重复确认不再执行;配置页只出现两个选项。 + +| 编号 | 场景 | 步骤与通过标准 | 状态 | +| --- | --- | --- | --- | +| W-01 | 飞书创建需求 | 执行权限选“网页确认”,单聊要求创建一个需求:收到操作摘要和单独一行的链接;本人打开、登录、确认后需求创建一次;再点确认只显示结果 | 未运行 | +| W-02 | 钉钉创建需求 | 同 W-01 | 未运行 | +| W-03 | 启动 SOP | 在飞书或钉钉要求把需求派发到某个 SOP 或启动运行:确认后运行出现在平台上,且只有一次 | 未运行 | +| W-04 | 作废 | 另一个平台账号打开链接看不到内容;发送者 `/unbind` 后原链接显示已作废 | 未运行 | +| W-05 | 选择 SOP | 在单聊中说“建一个需求:xxx”(不指定 SOP):摘要里写明“项目默认 · 名称”;确认页下拉框第一项为“项目默认 SOP(名称)”且默认选中,还列出通用模板;确认页换成另一个 SOP 并勾选立即启动,确认后需求绑定所选 SOP,运行出现在平台上 | 未运行 | +| W-06 | 点名 SOP | 在单聊中说“建一个需求:xxx,用 <某 SOP>”:摘要里的 SOP 就是点名的那个 | 未运行 | +| W-07 | 飞书确认卡片 | 飞书单聊要求建需求:收到确认卡片(不再只是链接);点“确认并启动 SOP”后收到“操作已执行…已启动”消息,平台上需求和运行各一个;再点一次只提示已执行 | 未运行 | +| W-08 | 卡片取消与他人点击 | 另一个已绑定成员点同一张卡片:提示不是你发起的;发起人点“取消”后再点确认只提示已取消 | 未运行 | +| G-11 | 群聊 @ Bot | 飞书、钉钉各把机器人拉进群:不 @ 的消息无回复;@ 机器人提问得到回复,答案按发言人的权限;未绑定成员 @ 时收到绑定链接 | 未运行 | +| M-01 | 新建时选回答模型 | 新建飞书 Bot,第①步“回答模型”点开选择一个模型并保存:列表里显示该模型;对话回复使用该模型;可再改回“默认” | 未运行 | + +## 5A. 飞书、钉钉通知卡片(2026-11-20 追加) + +按验收负责人要求,待人工确认和执行失败的通知在飞书、钉钉中以带按钮的卡片发送,点击直接审批(手册第 8.5 节)。 + +自动化:飞书卡片为 interactive 消息、按钮值带操作和投递编号、用户文本不按 Markdown 渲染;钉钉未填模板时发文字、填模板后调用 `createAndDeliver`(STREAM 回调、IM_ROBOT 场域、按钮开关变量);两渠道的点击回调映射为卡片操作,未知操作被丢弃;点击经通用入站路径执行操作并以单聊回复,非接收人的点击进入其自己的会话(随后按过期处理);飞书点击端到端通过等待节点,第二次点击提示已处理;只开通知的 Bot 不发卡片。 + +| 编号 | 场景 | 步骤与通过标准 | 状态 | +| --- | --- | --- | --- | +| C-01 | 飞书卡片审批 | 开发者后台添加 `card.action.trigger` 长连接回调;运行一个带人工确认节点的 SOP:接收人收到卡片,点“通过”后弹出提示、收到“操作已完成”消息,平台上节点通过 | 未运行 | +| C-02 | 钉钉卡片审批 | 按手册建模板并填写模板 ID,同 C-01 | 未运行 | +| C-03 | 重复点击 | 同一张卡片再点一次:提示已过期或已处理,节点不再变化 | 未运行 | +| C-04 | 失败节点 | 脚本节点失败:卡片出现“重试”“手动通过”,点击后结果正确 | 未运行 | +| C-05 | 未填模板 | 钉钉清空模板 ID:通知恢复为文字 | 未运行 | + +## 6. 遗留事项 + +1. **钉钉主动单聊权限**:权限的确切名称、`robotCode` 用 Client ID 是否可用,真实环境未回报(阶段 G 记录第 3 节)。 +2. **豁免项**:R1、R2 的豁免项和阶段 G 的整体确认,没有逐项真实证据,按上表保留“未运行”或“确认通过(未逐项回报)”。 +3. **多副本部署**:原生日志清理要求 `AGENTFLOW_ASSISTANT_SESSION_DIR` 共享,否则其他副本记为 `missing`(手册第 9.2 节)。 +4. **未登记日志位置的 CLI**:OpenCode、Kimi Code 等引擎的 Bot 日志记为 `unsupported`,需要人工清理。 +5. **CI 项**:Go 覆盖率门禁、真实浏览器用例、密钥扫描和 e2e 由 PR 的 CI 执行,结果以 CI 为准。 +6. **提交说明中的个人登录名**:提交 `57adbf2` 的说明含有员工登录名,文件已在 `c9ea35e` 中改正;改写提交说明需要强推,未执行。 diff --git a/docs/testing/im-r1-acceptance.md b/docs/testing/im-r1-acceptance.md new file mode 100644 index 000000000..a31250890 --- /dev/null +++ b/docs/testing/im-r1-acceptance.md @@ -0,0 +1,134 @@ +# R1 验收记录:飞书私聊闭环 + +对应方案 `docs/proposals/enterprise-im.md` 的阶段 A–E,以及操作手册 `docs/im-channels.md`。状态取值:通过、失败、未运行、阻塞。时间均为北京时间。 + +## 1. 固定验收提交 + +| 项 | 值 | +| --- | --- | +| 分支 | `feature/enterprise-im` | +| 验收提交 | `57c54503c0f25658a043128b00b8cdce1fd82db5` | +| 基线 | `origin/main@e89cda9` | +| 构建 | `bash scripts/build-chatbot.sh && go build -o bin/agentflow ./cmd/agentflow`(Go 1.26.11,Node 22) | + +该提交之后只追加文档提交,可以用 `git diff 57c5450 HEAD --stat -- . ':!docs'` 核对,输出应为空。若验收中需要修复代码,会产生新的验收提交,并在本表追加一行。 + +## 2. 自动化结果(2026-11-18) + +| 范围 | 环境 | 结果 | +| --- | --- | --- | +| 全量 `go test ./...` | 本机 | 28 个包通过。`cmd/agentflow`(Unix 套接字路径过长)和 `internal/runtime`(没有 Docker)在未改动的基线上同样失败 | +| `go vet`、`check-public.py`、Go 依赖许可证清单 | 本机 | 通过 | +| `internal/repo` MySQL 集成用例 | MySQL 8.4.13 | 361 个通过。`TestMySQLReadsObserveExternalChangesWithoutReload` 失败,基线上同样失败(分析统计相关,与 IM 无关) | +| 需要独立库的 MySQL 用例(6 组) | MySQL 8.4.13 | 41 个通过 | +| 036/037 迁移用例 | MySQL 8.4.13、MariaDB 10.11 | 通过。8.4 首次运行时发现 037 违反 `ONLY_FULL_GROUP_BY`,已在验收提交中修复 | +| 飞书多进程故障测试 F1–F6 | MySQL 8.4.13 | 通过,见第 5 节 | +| 前端 `webtest` | Node 22.23.5,正式构建产物 | 208 个通过,覆盖率 87.58%(门槛 85%)。验收提交未改动前端 | +| 依赖漏洞扫描 | govulncheck v1.9.0 | 新增依赖无漏洞;`golang.org/x/crypto` 一条为基线已有,且调用不到 | + +## 3. 预发升级与回退演练 + +环境:本机沙箱中的 MySQL 8.4.13(官方 Debian 12 包,默认 `sql_mode`)。旧版本是 `origin/main@e89cda9` 构建的二进制,新版本是验收提交构建的二进制。时间 2026-11-18 15:28–15:33。 + +**升级前数据**:全部由旧版本 API 写入。 + +- 4 个企微配置: + - 对话 A、对话 C; + - 通知 B1、B2,共用同一个 Bot ID; + - 另有一次重复启用被旧版本拒绝(409)。 +- 4 条绑定: + - 正常 1 条; + - 共用 Bot ID 1 条; + - 孤立 1 条; + - 未完成 1 条。 +- 4 条交互:已回复、回复失败、已生成待发、执行中各 1 条。 +- 备份:`mysqldump --single-transaction`,大小 81 KB。 + +| 步骤(手册第 2 节) | 实际结果 | +| --- | --- | +| 3. 新版本以 `AGENTFLOW_BOT_WORKERS_ENABLED=false` 启动 | 日志提示 worker 已关闭;两条连接为 `offline`,未被占用;保存配置返回 `503 bot_maintenance`;读取配置正常 | +| 4. 036,执行两次 | 15 个新列都是 `present=1`。`wecom` 配置 4 个、接收占位 2 个、未回填 0 个;交互缺项目 0 条。两次输出完全相同 | +| 5. 037,执行两次 | 共用 Bot ID 和孤立绑定各列出 1 条,留待人工处理;唯一归属的 2 条已回填 `config_id`;`legacy_unmapped` 列出 2 条。两次输出完全相同 | +| 6. `-credential-action` | 先 `status` 显示 `plaintext=4`,`reencrypt` 改写 4 条;再次 `reencrypt` 跳过 4 条;最后 `status` 显示 `plaintext=0 kid_pre1=4`。库中前缀均为 `lf1:pre1:`,查不到明文 | +| 7. 开启 worker 重启 | 两条接收连接被占用,`epoch=1`;凭据可以解密;执行中的交互改为“上一轮处理已中断……”;已生成待发的回复保持原样;4 条交互都有了项目快照 | +| 升级后接口 | 旧配置读出时显示 `credentialFormat=encrypted`,写策略按旧版自动执行处理;旧 `configVersion` 返回 `409 config_changed`;新建企微 Bot 默认 `readonly`,Secret 已加密;同一应用重复启用返回 `409 receive_slot_taken`;钉钉返回 `409 channel_not_ready`;飞书用虚构 App ID 向**真实飞书开放平台**校验,返回 `400 credential_invalid`(错误码 10003),配置未保存;待核对列表可按项目查询 | +| 回退 A:旧版本直接在已迁移的库上运行 | 能启动,能读出配置,也会占用连接;但 Secret 已是密文,旧版本无法使用。**不支持这种回退** | +| 回退 B:恢复备份后启动旧版本 | 4 张表行数与升级前一致,Secret 为明文,库中没有新列;旧版本正常读出 4 个 Bot 并占用连接 | + +本机没有企微和飞书的真实回调,所以企微连接一直停在“重连中”。这是预期结果,企微的真实收发要在 R1-12 中验证。 + +## 4. 行为变更测试:自然语言回答 + +基线对照:在 `origin/main` 上,模型对查询给出纯自然语言回答、不调用终态工具时,会运行 3 次模型,最后返回“助手未提交回复”。 + +`internal/httpapi/bot_natural_reply_test.go` 的 9 种情况全部通过: + +| 情况 | 模型运行次数 | 结果 | +| --- | --- | --- | +| 自然语言回答(旧企微,自动执行) | 1 | 采用 | +| 自然语言回答(只读) | 1 | 采用 | +| 首轮只有进度(“我先查询一下”),第二轮给出回答 | 2 | 采用第二轮的回答 | +| 首轮空输出,第二轮给出回答 | 2 | 采用第二轮的回答 | +| 带草稿的协议 JSON | 3 | 不采用,失败 | +| 不带草稿的协议 JSON,第二轮给出回答 | 2 | 不采用 JSON,采用第二轮的回答 | +| CLI 退出失败(输出了回答) | 3 | 不采用,失败 | +| 三次都没有输出 | 3 | 失败 | +| 回答里带个人凭据 | 1 | 采用,凭据替换为 `[redacted]` | + +这组测试发现并修复了一处问题:协议 JSON 中的草稿在解析时被过滤后,剩下的“将创建需求”曾被当作回复发出。现在只采用纯文本。 + +## 5. 故障测试(模拟飞书平台 + 真实 MySQL 8.4 + 真实接收进程) + +`internal/botinteraction/feishu_fault_mysql_test.go`。父进程模拟飞书开放平台,包括长连接、protobuf 帧、Message Reply 和一小时 `uuid` 去重,同时模拟模型。接收端是运行生产代码的子进程,用 SIGKILL 杀掉来模拟崩溃。修复后共运行 2 次(第 2 次在验收提交上),结果相同。 + +| 编号 | 场景 | 交互状态 | 模型运行 | 回复请求 | 用户收到 | 其他 | +| --- | --- | --- | --- | --- | --- | --- | +| F1 | 模型运行期间,同一消息以 3 个不同事件 ID 投递 | replied | 1 | 1 | 1 | 3 次投递都回执 200 | +| F2 | 数据库写入被锁 4 秒(`FLUSH TABLES WITH READ LOCK`),超过 3 秒回执窗口 | replied | 1 | 1 | 1 | 2.501 秒回执 500;重投回执 200。修复前此场景收不到任何回执,测试失败 | +| F3 | 模型运行期间长连接断开 | replied | 1 | 1 | 1 | 自动重连,之后的新消息正常处理 | +| F4 | 模型运行期间接收进程崩溃 | replied | 1 | 1 | 1 | 46 秒后另一个进程接管,`epoch=2`;发出“已中断”说明,没有重跑模型 | +| F5 | 回复请求已送达,响应还没回来时接收进程崩溃 | replied | 1 | 2(同一个 `uuid`) | 1 | 新进程用原 key 重试,由平台去重 | +| F6 | 回复响应丢失(12 秒无响应) | replied | 1 | 2(同一个 `uuid`) | 1 | 5 秒后用原 key 重试 | + +最后检查:7 条交互全部是 `replied`,没有残留的 `sending` 或 `reply_unknown`。 + +## 6. R1 真实环境验收(逐项) + +真实环境指真实飞书租户和试点账号。真实环境部署:用户预发实例,`feature/enterprise-im@e148fe1`,2026-11-18 17:55 前后完成。下表的“状态”按真实环境的通过标准填写,“已有证据”是演练或模拟环境中得到的结果,不能代替真实验收。 + +| 编号 | 状态 | 已有证据 | 还需要 | +| --- | --- | --- | --- | +| R1-01 凭据校验 | 通过 | 错误路径:真实飞书开放平台返回 10003 → `credential_invalid`,未保存(第 3 节)。正确路径:2026-11-18 用户部署 `e148fe1`(Go 代码同 `57c5450`)后,用真实应用保存成功,管理页健康状态为“消息权限可用”(用户截图) | — | +| R1-02 企业确认 | 通过 | 2026-11-18 管理页显示企业已确认、连接正常(用户截图);确认前的固定提示和确认后不断线由用户核对后确认“验证没问题” | — | +| R1-03 两阶段绑定 | 未运行(验收豁免) | 单人绑定已走通:试点成员绑定后得到了模型回复(R1-05)。跨人确认被拒在测试中通过,真实环境未报告 | 成员 B 在自己的单聊发送 A 的确认码,结果应为失败 | +| R1-04 只读查询 | 未运行(验收豁免) | 已绑定成员得到了正常的自然语言回复(R1-05),模型凭据可用;状态、SOP 进度、待办三类查询未报告结果 | 三类查询的回复截图或交互记录 | +| R1-05 写请求 | 通过 | 2026-11-18 请求“创建标题为 R1 写测试的需求”,机器人答复只读模式不能创建、请到平台页面新建;用户确认平台未新增该需求。同时在真实环境验证了“直接采用自然语言回答”(第 4 节) | — | +| R1-06 重复投递 | 未运行(验收豁免) | F1、F2 通过 | 真实平台重投 | +| R1-07 重启恢复 | 未运行(验收豁免) | F4、F5,以及预发第 7 步通过 | 预发实例 | +| R1-08 撤权与解绑 | 未运行(验收豁免) | 测试:撤权和解绑在下一轮生效 | 试点成员 | +| R1-09 非文本与群聊 | 未运行(验收豁免) | 服务层测试:图片收到提示;群聊计数后忽略 | 飞书应用 | +| R1-10 跨企业 | 未运行(验收豁免) | 服务层测试:其他企业的事件丢弃,记为 `tenant_mismatch` | 第二个企业,或改用测试企业的发布范围 | +| R1-11 送达未知 | 未运行(验收豁免) | F5、F6 通过;55 分钟窗口边界有单测 | 预发实例的出口网络控制 | +| R1-12 企微回归 | 未运行(验收豁免) | 单测通过;预发中旧 Bot 能读出、凭据能解密、连接能占用 | 一个真实的旧企微 Bot | +| R1-13 升级迁移 | 通过(演练环境) | 第 3 节:036/037 两次执行一致,`plaintext=0`,回退 B 通过 | 团队预发库按手册第 2 节再执行一次 | + +### 验收结论 + +2026-11-18 21:48,验收负责人确认 **R1 验收通过**,部署版本 `feature/enterprise-im@e148fe1`(Go 代码与验收提交 `57c5450` 相同)。 + +- 真实环境通过:R1-01、R1-02、R1-05。 +- 演练环境通过:R1-13。 +- 其余各项未在真实环境单独执行,由验收负责人豁免;对应能力的证据是上表的自动化测试和第 5 节故障测试。 +- 后续若在试点中补测这些项,在本表追加结果,不改变本结论。 + +## 7. 真实验收的前置条件 + +1. **飞书应用**:企业自建应用,开机器人能力,长连接订阅 `im.message.receive_v1`,开通 `docs/im-channels.md` 4.1 节的权限,发布给试点成员。 +2. **预发实例**: + - 部署验收提交 `57c5450`; + - 能访问 `open.feishu.cn`(长连接是出站连接,不需要入站端口); + - `AGENTFLOW_PUBLIC_URL` 要能从试点成员的浏览器打开,绑定页依赖它; + - 配置 `AGENTFLOW_CREDENTIAL_KEYS`; + - 使用 MySQL 8.4。 +3. **试点账号**:两名普通项目成员,各自配置个人模型凭据。 +4. **企微回归**:一个已在使用的企微 Bot。 diff --git a/docs/testing/im-r2-acceptance.md b/docs/testing/im-r2-acceptance.md new file mode 100644 index 000000000..e765cc94f --- /dev/null +++ b/docs/testing/im-r2-acceptance.md @@ -0,0 +1,83 @@ +# R2 验收记录:钉钉私聊闭环 + +对应方案 `docs/proposals/enterprise-im.md` 的阶段 F,以及操作手册 `docs/im-channels.md` 第 4A 节。状态取值:通过、失败、未运行、阻塞。时间均为北京时间。 + +## 1. 固定验收提交 + +| 项 | 值 | +| --- | --- | +| 分支 | `feature/enterprise-im` | +| 验收提交 | `f9c94e07b87c4de8aaaa19e421cd8fd30f62b86e` | +| 构建 | `bash scripts/build-chatbot.sh && go build -o bin/agentflow ./cmd/agentflow`(Go 1.26.11,Node 22);Compose 部署按原方式 | +| 升级 | 从 R1 版本升级时没有新的数据库迁移:新增的 `tenantExpected` 保存在配置 payload 中,会话回复地址使用已有的 `reply_route` 列,直接替换版本即可 | + +该提交之后只追加文档提交。若验收中需要修复代码,会产生新的验收提交,并在本表追加一行。 + +| 修复验收提交 | 原因 | +| --- | --- | +| `64e1619` | 真实钉钉中绑定链接打不开:飞书、钉钉是纯文本消息,Markdown 链接的右括号被识别进 URL。改为链接单独一行,绑定页容错提取令牌 | + +## 2. 自动化结果(2026-11-18) + +| 范围 | 环境 | 结果 | +| --- | --- | --- | +| 钉钉适配器单元测试:事件映射、凭据校验、ping/回执/入库超时、`disconnect`、心跳超时与迟到帧、关闭期间的帧、取消、回复分类、禁止重定向 | 本机,`-race` 连跑 3 轮 | 通过 | +| 服务层闭环:企业自动确认、跨企业丢弃、路由加密与回复后清除、结果未知不自动重发、会话回复过期 | 本机 | 通过 | +| Repo:预期企业锁定、冲突保持、更正 CorpId、锁定后不可改、路由密文绑定交互 | 本机 | 通过 | +| HTTP:缺少 CorpId、凭据校验失败不保存、禁止自动执行、锁定后改 CorpId 被拒 | 本机 | 通过 | +| 全量 `go test ./...` | 本机 | 只剩与基线相同的环境性失败(`cmd/agentflow` 的 Unix 套接字路径、`internal/runtime` 的 Docker) | +| `internal/repo` MySQL 集成用例 | MySQL 8.4.13 | 362 个通过;`TestMySQLReadsObserveExternalChangesWithoutReload` 在基线上同样失败 | +| 飞书故障测试 F1–F6 | MySQL 8.4.13 | 通过,路由清除改动没有影响飞书 | +| 钉钉故障测试 D1–D5 | MySQL 8.4.13 | 连续 2 次通过,见第 3 节 | +| 前端 `webtest` | Node 22.23.5 | 209 个通过,覆盖率 87.61%(门槛 85%) | +| `go vet`、`check-public.py`、依赖许可证清单(92 个模块)、govulncheck | 本机 | 通过;新增的钉钉 SDK 没有漏洞 | + +## 3. 故障测试(模拟钉钉网关 + 真实 MySQL 8.4 + 真实接收进程) + +`internal/botinteraction/dingtalk_fault_mysql_test.go`。父进程模拟钉钉的接入点、Stream 帧和会话 Webhook,同时模拟模型。接收端是运行生产代码的子进程,用 SIGKILL 模拟崩溃。 + +| 编号 | 场景 | 结果 | 模型运行 | Webhook 调用 | +| --- | --- | --- | --- | --- | +| D1 | 模型运行期间,同一消息以 3 个帧 ID 投递;这也是第一条消息 | replied;3 次回执都是 200;企业自动确认,`config_version` 加一 | 1 | 1 | +| D2 | 数据库写入被锁 4 秒 | 2.507 秒回执 500;重投后正常处理 | 1 | 1 | +| D3 | 模型运行期间网关停止回 pong,3 秒后在旧连接上推迟到帧(SDK 会崩溃的场景) | 接收进程没有退出,自动重连;回复正常发出,重连后新消息也正常处理 | 1 | 1 | +| D4 | 回复请求已发出、响应没回来时接收进程崩溃 | 新进程接管后记为 `reply_unknown`,不自动重发;人工再次发送后为第 2 代并发出 | 1 | 2(第二次是人工补发) | +| D5 | 回复生成前,会话 Webhook 剩余有效期已不足 10 秒 | `reply_failed / route_expired`,回复内容保留,路由已清除,没有调用 Webhook | 1 | 0 | + +## 4. R2 真实环境验收(逐项) + +真实环境指真实钉钉企业和试点账号。2026-11-19 验收负责人在预发实例部署 `64e1619` 后完成了下列通过项;其余项未在真实环境单独执行。“已有证据”是自动化或模拟环境的结果,不能代替真实验收。 + +| 编号 | 场景 | 步骤与通过标准 | 状态 | 已有证据 | +| --- | --- | --- | --- | --- | +| R2-01 | 凭据校验 | 填错 Client Secret 保存,提示 `credential_invalid` 且不保存;填对后保存成功,状态为“企业待确认” | 通过(正确路径) | HTTP 测试 | +| R2-02 | 企业自动确认 | 填写正确 CorpId,试点成员发第一条单聊消息:企业自动确认,**这条消息正常得到回复**,连接不中断 | 通过 | D1、服务层测试 | +| R2-03 | 企业冲突 | 故意填错 CorpId 后发消息:状态为“企业冲突”,只收到“正在完成企业确认”的提示;改正 CorpId 后再发消息,恢复正常 | 未运行 | Repo 测试 | +| R2-04 | 两阶段绑定 | 与 R1-03 相同:成员 B 发送 A 的确认码失败,A 自己发送成功 | 通过(单人路径,`64e1619` 之后) | 绑定测试(与飞书共用) | +| R2-05 | 只读查询 | 查询项目状态、SOP 进度、本人待办,都在原会话收到回复 | 未运行 | 只读策略测试 | +| R2-06 | 写请求 | 要求创建需求:提示到平台页面操作,平台没有新增数据 | 未运行 | 只读策略测试,R1-05 已在飞书真实环境通过 | +| R2-07 | 重复投递 | 测试库 `FLUSH TABLES WITH READ LOCK` 4 秒期间发消息:只收到一条回复,只产生一条交互 | 未运行 | D1、D2 | +| R2-08 | 重启恢复 | 模型处理期间重启容器:收到一条“上一轮处理已中断”的回复;Stream 自动重连 | 未运行 | D3、D4 | +| R2-09 | 会话回复过期 | 记录真实的 `sessionWebhookExpiredTime` 长度;过期后结果在记录页显示“会话回复授权已过期”,不能再次发送 | 未运行 | D5 | +| R2-10 | 非文本与群聊 | 发图片收到“只支持文字”的提示;群里 @ 机器人没有回复,连接上的群消息计数增加 | 未运行 | 服务层测试 | +| R2-11 | 撤权与解绑 | 与 R1-08 相同 | 未运行 | 撤权测试 | +| R2-12 | 回归 | 飞书和企微 Bot 在同一版本上正常收发 | 未运行 | F1–F6,全量回归 | + +### 验收结论 + +2026-11-19 20:06,验收负责人确认 R2 没有问题,继续阶段 G,部署版本 `feature/enterprise-im@64e1619`。 + +- 真实环境通过:R2-01(正确路径)、R2-02、R2-04(单人绑定)。 +- 其余各项未在真实环境单独执行,由验收负责人豁免;证据见上表和第 3 节故障测试。试点中补测的结果追加在本表,不改变本结论。 +- 真实环境事实:首条单聊消息自动确认了企业,说明 `chatbotCorpId` 与后台 CorpId 一致;回复正常送达,说明 sessionWebhook 在允许列表内(`oapi.dingtalk.com/robot/sendBySession`);`robotCode` 是否等于 Client ID 未记录,阶段 G 的主动单聊按 Client ID 发送,试发失败时会显示钉钉的错误码。 + +**真实环境中需要记录的事实**: +- `chatbotCorpId` 的格式是否与后台显示的 CorpId 完全一致,决定 R2-02 能否自动确认; +- 原始事件里有没有 `robotCode`,以及它是否等于 Client ID; +- sessionWebhook 的域名和路径。如果不是 `oapi.dingtalk.com/robot/sendBySession`,消息会被记为 `untrusted_webhook` 丢弃,需要回报给我调整允许列表。 + +## 5. 真实验收的前置条件 + +1. **钉钉企业内部应用**:开通机器人能力,消息接收模式选 Stream,发布给试点成员。提供 Client ID、Client Secret 和企业 CorpId,配置方法见手册第 4A.1 节。 +2. **部署**:部署验收提交 `f9c94e0` 或之后的文档提交;机器要能访问 `api.dingtalk.com` 和 `oapi.dingtalk.com`。Stream 是向外发起的连接,不需要开放入站端口。 +3. **试点账号**:两名普通项目成员,各自配置个人模型凭据。 diff --git a/docs/testing/im-stage-g-acceptance.md b/docs/testing/im-stage-g-acceptance.md new file mode 100644 index 000000000..a65e22cb6 --- /dev/null +++ b/docs/testing/im-stage-g-acceptance.md @@ -0,0 +1,59 @@ +# 阶段 G 验收记录:主动通知、钉钉过期回复补发、企微网页确认 + +对应方案 `docs/proposals/enterprise-im.md` 第 9 节和第 10 节阶段 G,操作手册 `docs/im-channels.md` 第 8 节。状态取值:通过、失败、未运行、阻塞。时间均为北京时间。 + +## 1. 固定验收提交 + +| 项 | 值 | +| --- | --- | +| 分支 | `feature/enterprise-im` | +| 代码提交 | `32e8eea`(主动通知)、`03f9541`(钉钉过期回复补发)、`74bbc34`(企微网页确认)、`f86881a`(确认页嵌入二进制) | +| 验收提交 | `f86881a`,之后只追加文档提交 | +| 升级 | 没有手动迁移脚本;启动时的增量 DDL 补列并新建 `bot_pending_drafts`,见手册第 8.4 节 | + +## 2. 自动化结果(2026-11-19) + +| 范围 | 环境 | 结果 | +| --- | --- | --- | +| 飞书主动发送:open_id、`uuid` 透传、拒绝与缺少目标 | 本机,`-race` | 通过 | +| 钉钉主动发送:token 缓存与轮换后重取、401 清缓存、`invalidStaffIdList`、`flowControlledStaffIdList`、429、5xx、无法识别的响应、凭据被拒 | 本机,`-race` | 通过 | +| 通知服务:试发验证(未知结果不算验证)、三类 SOP 通知在只启用通知的配置下送达、飞书未知结果用同一 key 重试、钉钉未知结果停在 `send_unknown`、中断后续领(钉钉不重发、飞书窗口内重发)、未送达不保留开始时间、解绑后停止发送并清除验证、过时的等待通知不发送、人工确认与再次发送(新一代、新 key)、切换接收人时迁移或取消 | 本机,`-race` | 通过 | +| HTTP:未绑定成员不能选、试发未知不保存、同一接收人重存不再试发、待发通知需要显式选择、确认收到、测试按钮重新试发 | 本机 | 通过 | +| 钉钉过期回复改用主动单聊:未验证与不同账号被拒、新一代与新 key、走 PushSender 而不是 Webhook、重复点击不重发;HTTP 层的可用标记与 409 | 本机,`-race` | 通过 | +| 企微网页确认:确认前不执行、他人不可查看或确认、跨源请求被拒、确认只执行一次、重复点击返回当前状态、取消、撤权与解绑与切换模式后作废、钉钉不能选网页确认 | 本机,`-race` | 通过 | +| 草稿状态机:8 个并发确认只有 1 个成功、执行中断转待核对且不能再执行、过期、参数被改后拒绝 | 本机 SQLite;MariaDB | 通过 | +| 阶段 G 语句集成测试 `TestMySQLStageGPushAndDrafts` | MariaDB(本机) | 通过 | +| `internal/repo` 全部 MySQL 用例 | MariaDB(本机) | 新增用例通过;`TestMySQLReadsObserveExternalChangesWithoutReload` 与基线相同地失败;`TestMySQLAssistantParticipantProjectionReadOnly` 使用 `CAST(? AS JSON)`,MariaDB 不支持,与本阶段改动无关 | +| MySQL 8.4 复跑 | MySQL 8.4.13(2026-11-20 补跑) | `TestMySQLStageGPushAndDrafts`、F1–F6、D1–D5 通过;`internal/repo` 全部 MySQL 用例只有基线同样失败的 `TestMySQLReadsObserveExternalChangesWithoutReload`。最初记为“未运行”是误判:本机的 MySQL 8.4 并未被清理,见[最终测试记录](im-final-test-record.md) | +| `internal/httpapi`、`internal/notification`、`internal/botinteraction`、`internal/repo`、`internal/domain` | 本机 | 通过 | +| 前端 `webtest` | Node 22.23.5 | 213 个通过,覆盖率 87.63%(门槛 85%) | +| `go vet ./...`、`gofmt`、`check-public.py` | 本机 | 通过 | + +## 3. 真实环境验收(逐项) + +真实环境指真实飞书租户、钉钉企业和企微 Bot。“已有证据”是自动化结果,不能代替真实验收。 + +### 验收结论 + +2026-11-20 00:24,验收负责人在预发实例确认阶段 G 验收没有问题,继续阶段 H。部署版本 `feature/enterprise-im@f86881a`(之后只有文档提交)。 + +验收负责人没有逐项回报各场景的实际结果,下表状态记为“确认通过(未逐项回报)”,表示验收负责人整体确认,不代表每一项都单独留有证据。试点中补充的逐项结果(例如钉钉主动单聊所需权限的确切名称、`robotCode` 用 Client ID 是否可用)追加在本表,不改变本结论。 + +| 编号 | 场景 | 步骤与通过标准 | 状态 | 已有证据 | +| --- | --- | --- | --- | --- | +| G-01 | 飞书试发 | 已绑定成员 → 第③步刷新 → 试发并保存:成员收到试发消息,页面显示“试发成功” | 确认通过(未逐项回报) | 通知服务、HTTP 测试 | +| G-02 | 钉钉试发 | 同 G-01。记录:是否需要额外开通权限、权限名称、`robotCode` 用 Client ID 是否可用 | 确认通过(未逐项回报) | 钉钉主动发送单测 | +| G-03 | 三类 SOP 通知 | 关闭“启用对话”,运行一个绑定了该 Bot 的 SOP:等待、失败、完成三类通知都以单聊送达,发送记录为“已发送” | 确认通过(未逐项回报) | 通知服务测试 | +| G-04 | 过时的等待通知 | 等待通知排队期间在平台处理该节点:通知不发送,记录为“已过期” | 确认通过(未逐项回报) | 通知服务测试 | +| G-05 | 解绑联动 | 接收人发送 `/unbind` 后再触发通知:记录为“会话不可用”,第③步显示需要重新试发 | 确认通过(未逐项回报) | 通知服务测试 | +| G-06 | 切换接收人 | 有待发通知时切换接收人:出现改发或取消的选择,结果写入发送记录 | 确认通过(未逐项回报) | HTTP、前端测试 | +| G-07 | 钉钉过期回复补发 | 等会话 Webhook 过期后,在对话记录点“改用主动单聊发送”:原发送者收到已保存的回复,记录显示已回复 | 确认通过(未逐项回报) | 服务层、HTTP 测试 | +| G-08 | 企微网页确认 | “执行权限”选“网页确认”,在企微要求创建需求:收到摘要和链接;本人打开确认后需求只创建一次;再次点击只显示结果 | 确认通过(未逐项回报) | HTTP、页面测试 | +| G-09 | 网页确认作废 | 另一个账号打开链接看不到内容;解绑后原链接显示已作废 | 确认通过(未逐项回报) | HTTP 测试 | +| G-10 | 回归 | 企微已有 Bot 的接收会话、测试通知、卡片照常;飞书、钉钉对话照常 | 确认通过(未逐项回报) | 全量回归 | + +## 4. 真实验收的前置条件 + +1. 部署 `f86881a` 或之后的文档提交,`AGENTFLOW_PUBLIC_URL` 可从试点成员浏览器访问(网页确认链接依赖它)。 +2. 飞书应用已开通“以应用的身份发消息”;钉钉应用开通机器人发送单聊消息的权限(见手册第 4A.1 节)。 +3. 至少一名已在飞书、钉钉 Bot 单聊中完成绑定的成员;一个企微 Bot 和一名已绑定的成员。 diff --git a/e2e/api/30-runs.test.mjs b/e2e/api/30-runs.test.mjs index 78c58c9e0..8fb67e4d8 100644 --- a/e2e/api/30-runs.test.mjs +++ b/e2e/api/30-runs.test.mjs @@ -46,6 +46,11 @@ test('approval gate: reject reworks upstream in the same workspace, approve comp await get(`/api/runs/${id}/workspace`); }); + await test.test('run control lists the approval actions while waiting', async () => { + const control = await get(`/api/runs/${id}/control`); + assert.match(JSON.stringify(control), /runs_approve/); + }); + await test.test('wrong node cannot be approved', async () => { await api('POST', `/api/runs/${id}/approve`, { nodeId: 'prep' }, { expect: [400, 409, 500] }); }); diff --git a/e2e/api/50-platform.test.mjs b/e2e/api/50-platform.test.mjs index 407a49cae..77df9a298 100644 --- a/e2e/api/50-platform.test.mjs +++ b/e2e/api/50-platform.test.mjs @@ -97,6 +97,9 @@ test('schedule job: a manual trigger dispatches todo requirements of the project const saved = await put('/api/schedule-jobs', { ...job, name: 'E2E 定时任务(改)' }); assert.equal(saved.name, 'E2E 定时任务(改)'); assert.ok(list(await get('/api/schedule-jobs')).some(j => j.id === id)); + // The enable switch keeps the e2e job disabled. + assert.equal((await post(`/api/schedule-jobs/${id}/enabled`, { enabled: false })).enabled, false); + await api('POST', `/api/schedule-jobs/${id}/enabled`, {}, { expect: 400 }); await post(`/api/schedule-jobs/${id}/run`, {}); const runs = await waitFor('scheduled dispatch', async () => { @@ -105,6 +108,7 @@ test('schedule job: a manual trigger dispatches todo requirements of the project }, { timeoutMs: 120_000 }); trackRun(runs[0].id); await waitRun(runs[0].id, r => r.status === 'succeeded', { label: 'scheduled run succeeded' }); + assert.ok(Array.isArray(list(await get(`/api/schedule-job-executions?projectId=${P}&jobId=${id}`)))); const after = list(await get('/api/schedule-jobs')).find(j => j.id === id); assert.ok(['success', 'waiting', 'running'].includes(after.lastStatus), `job status ${after.lastStatus}: ${after.lastError || ''}`); }); diff --git a/e2e/api/62-integrations.test.mjs b/e2e/api/62-integrations.test.mjs index d28afaedc..ff4304d0e 100644 --- a/e2e/api/62-integrations.test.mjs +++ b/e2e/api/62-integrations.test.mjs @@ -51,6 +51,9 @@ test('notification bots: create, update, secret stays write-only, delete', async const interactions = await get(`/api/notification-bots/${bot.id}/interactions`, { expect: [200] }); assert.ok(interactions); + // Chat replies waiting for a manager's review: none without traffic. + assert.ok(Array.isArray(list(await get(`/api/bot-interactions/pending?projectId=${P}`)))); + await api('GET', '/api/bot-interactions/pending?projectId=e2e-missing-project', undefined, { expect: [403, 404] }); await del(`/api/notification-bots/${bot.id}`, { expect: [200, 204] }); assert.ok(!list(await get(`/api/notification-bots?projectId=${P}`)).some(b => b.id === bot.id)); diff --git a/e2e/api/69-assistant-sessions.test.mjs b/e2e/api/69-assistant-sessions.test.mjs index 354883fee..723647960 100644 --- a/e2e/api/69-assistant-sessions.test.mjs +++ b/e2e/api/69-assistant-sessions.test.mjs @@ -109,6 +109,16 @@ test('archive flags the conversation and can be undone', async () => { test('generation endpoints answer for an idle conversation', async () => { await api('GET', `${S}/${session.id}/generation`, undefined, { expect: 404 }); await api('POST', `${S}/${session.id}/generation/cancel`, {}, { expect: [404, 409] }); + // No native CLI approvals are pending without a generation. + assert.deepEqual(list(await get(`${S}/${session.id}/native-approvals`)), []); +}); + +test('runtime mode: platform is accepted, workspace needs an open workspace, unknown is refused', async () => { + const saved = await put(`${S}/${session.id}/mode`, { mode: 'platform' }); + assert.equal(saved.id, session.id); + await api('PUT', `${S}/${session.id}/mode`, { mode: 'workspace' }, { expect: [409] }); + await api('PUT', `${S}/${session.id}/mode`, { mode: 'ludicrous' }, { expect: [409] }); + session = await get(`${S}/${session.id}`); }); test('project conversations: non-members read only, members share full access', { skip: tokenBSkip }, async () => { diff --git a/e2e/api/90-route-sweep.test.mjs b/e2e/api/90-route-sweep.test.mjs index 17dd5089c..94bf5c6f2 100644 --- a/e2e/api/90-route-sweep.test.mjs +++ b/e2e/api/90-route-sweep.test.mjs @@ -83,6 +83,14 @@ test('unknown bots, rooms and spaces answer 404', async () => { for (const sub of ['decisions', 'flows', 'sops']) await status('POST', `/api/spaces/${MISSING}/${sub}`, {}, [404]); }); +test('read-only status endpoints answer for a signed-in user', async () => { + const feedback = await get('/api/page-feedback?limit=1'); + assert.equal(typeof feedback.total, 'number'); + const install = await get('/api/assistant/cli-install?engine=codex'); + assert.ok(['installed', 'missing', 'installing', 'failed'].includes(install.status), install.status); + await status('GET', '/api/assistant/cli-install?engine=not-a-cli', undefined, [400]); +}); + test('OTLP ingest accepts an empty export', async () => { await status('POST', '/api/otel/v1/traces', {}, [200, 202]); }); diff --git a/e2e/local.sh b/e2e/local.sh index 2fa5660a6..7054a5110 100755 --- a/e2e/local.sh +++ b/e2e/local.sh @@ -73,7 +73,10 @@ echo "== platform on $BASE" HOST_IP=$(hostname -I 2>/dev/null | awk '{print $1}') # AGENTFLOW_LOCAL_LOGIN_USER enables the loopback-only acceptance login, so the # session-only endpoints (personal API tokens, logout) can be exercised. +# Chat Bot secrets are sealed with AGENTFLOW_CREDENTIAL_KEYS; without a key +# saving a Bot answers 503 credential_key_missing. AGENTFLOW_SESSION_SECRET=$(head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n') \ +AGENTFLOW_CREDENTIAL_KEYS="e2e:$(head -c 32 /dev/urandom | base64 | tr -d '\n')" \ AGENTFLOW_AUTH_REQUIRED=true \ AGENTFLOW_LOCAL_LOGIN_USER=e2e-local \ AGENTFLOW_RUNNER_BINARY="$WORK/agentflow-runner" \ diff --git a/go.mod b/go.mod index 2704c933f..e725c0373 100644 --- a/go.mod +++ b/go.mod @@ -10,9 +10,10 @@ require ( github.com/goccy/go-yaml v1.19.2 github.com/gorilla/websocket v1.5.3 github.com/klauspost/compress v1.20.3 + github.com/larksuite/oapi-sdk-go/v3 v3.13.0 + github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1 github.com/zalando/go-keyring v0.2.8 golang.org/x/crypto v0.58.0 - golang.org/x/net v0.62.0 golang.org/x/sys v0.49.0 gorm.io/driver/mysql v1.6.0 gorm.io/gorm v1.31.2 @@ -34,6 +35,7 @@ require ( github.com/go-playground/validator/v10 v10.30.1 // indirect github.com/goccy/go-json v0.10.5 // indirect github.com/godbus/dbus/v5 v5.2.2 // indirect + github.com/gogo/protobuf v1.3.2 // indirect github.com/google/uuid v1.3.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect @@ -51,6 +53,7 @@ require ( github.com/ugorji/go/codec v1.3.1 // indirect go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect golang.org/x/arch v0.22.0 // indirect + golang.org/x/net v0.62.0 // indirect golang.org/x/text v0.43.0 // indirect google.golang.org/protobuf v1.36.10 // indirect modernc.org/libc v1.22.5 // indirect diff --git a/go.sum b/go.sum index 6cc183153..2bb6460a3 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ= github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= @@ -58,10 +60,14 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.20.3 h1:zWNoFqq/lSuYQRim3dUXlXeA0IqwuZGicdG5ZTfY5j4= github.com/klauspost/compress v1.20.3/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/larksuite/oapi-sdk-go/v3 v3.13.0 h1:v+x3V8a16HVXdcUEwj9LVwe+ku/K9WUy7Cp4WjQCVHo= +github.com/larksuite/oapi-sdk-go/v3 v3.13.0/go.mod h1:F1nwLfYBSKvD8mS/OFw90Gr+V4RIugPBR44m/ThA5jg= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -73,6 +79,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +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/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -100,6 +108,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/zalando/go-keyring v0.2.8 h1:6sD/Ucpl7jNq10rM2pgqTs0sZ9V3qMrqfIIy5YPccHs= github.com/zalando/go-keyring v0.2.8/go.mod h1:tsMo+VpRq5NGyKfxoBVjCuMrG47yj8cmakZDO5QGii0= go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= @@ -108,17 +118,42 @@ go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY= golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM= golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/botinteraction/botinteraction_cases_test.go b/internal/botinteraction/botinteraction_cases_test.go index eb7f8d220..a565ad81f 100644 --- a/internal/botinteraction/botinteraction_cases_test.go +++ b/internal/botinteraction/botinteraction_cases_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "strings" + "sync/atomic" "testing" "time" @@ -184,15 +185,16 @@ func TestConnectionReconnectsAfterLocalClose(t *testing.T) { } connected := make(chan struct{}, 3) var upgrader websocket.Upgrader - attempts := 0 + // Handlers run on separate server goroutines; count attempts atomically. + var attempts atomic.Int64 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ws, err := upgrader.Upgrade(w, r, nil) if err != nil { return } - attempts++ + attempt := attempts.Add(1) connected <- struct{}{} - if attempts%2 == 0 { + if attempt%2 == 0 { var frame Frame if ws.ReadJSON(&frame) == nil { _ = ws.WriteJSON(map[string]any{"headers": frame.Headers, "errcode": 400}) @@ -470,6 +472,16 @@ func TestNotificationCardTaskBranches(t *testing.T) { "from": map[string]string{"userid": "internal-user"}, "msgtype": "event", "event": map[string]string{"eventtype": "template_card_event", "event_key": "approve", "task_id": singleTask}, }) + // The inbound path reads the current configuration, so the Bot exists. + bot.BotID, bot.OwnerID, bot.Enabled, bot.InteractionEnabled = "external-task", 1, true, true + if err := st.SaveNotificationBot(bot, "s"); err != nil { + t.Fatal(err) + } + body, _ = json.Marshal(map[string]any{ + "msgid": "single-action", "aibotid": bot.BotID, "chattype": "single", + "from": map[string]string{"userid": "internal-user"}, "msgtype": "event", + "event": map[string]string{"eventtype": "template_card_event", "event_key": "approve", "task_id": singleTask}, + }) svc.receive(context.Background(), bot, Frame{Cmd: "aibot_event_callback", Body: body}, nil) if action, err := st.BotInteraction(repo.BotMessageID(bot.BotID, "single-action")); err != nil || action.Action != "approve" { t.Fatalf("single action = %#v, %v", action, err) diff --git a/internal/botinteraction/cards.go b/internal/botinteraction/cards.go index 09e093df4..5c80d656b 100644 --- a/internal/botinteraction/cards.go +++ b/internal/botinteraction/cards.go @@ -13,6 +13,10 @@ import ( var ErrCardExpired = errors.New("该节点已不再等待本次操作") +// ErrCardDeliveryUnknown: the card may have reached the chat but the platform +// did not confirm it; the notification needs manual review, not a resend. +var ErrCardDeliveryUnknown = errors.New("企业微信卡片送达结果待核对") + func ActionsForDelivery(store *repo.Store, item domain.NotificationDelivery) []map[string]any { run, ok := store.RunState(item.RunID) if !ok { @@ -97,8 +101,13 @@ func (s *Service) QueueNotificationCard(ctx context.Context, bot domain.Notifica if err != nil { return err } - if record.Status == "reply_failed" { + switch record.Status { + case domain.BotReplyFailed: return errors.New("企业微信卡片发送失败,请检查会话和 Bot 授权") + case domain.BotReplyUnknown: + return ErrCardDeliveryUnknown + case domain.BotCancelled: + return errors.New("机器人已停用或删除,卡片未发送") } if record.Status == "replied" { if record.LastError != "" { @@ -145,37 +154,7 @@ func actionLabel(action string) string { // Card text fields are plain text. Keep the reason visible above context rows, // instead of embedding a long Markdown notification in the clipped subtitle. func notificationCard(run domain.Run, item domain.NotificationDelivery, actions []map[string]any, content string) map[string]any { - reason, required := "", "" - node := run.Nodes[item.NodeID] - if node != nil { - if node.GateResult != nil { - reason = node.GateResult.Summary - if strings.TrimSpace(reason) == "" { - reason = node.GateResult.Reason - } - required = node.GateResult.ActionRequired - } - if strings.TrimSpace(reason) == "" { - reason = node.Error - } - } - if strings.TrimSpace(reason) == "" { - for _, line := range strings.Split(content, "\n") { - line = strings.TrimSpace(line) - if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, "需求:") || strings.HasPrefix(line, "SOP:") || strings.HasPrefix(line, "节点:") || strings.HasPrefix(line, "查看详情:") { - continue - } - reason = line - break - } - } - if strings.TrimSpace(reason) == "" { - if item.EventType == "waiting" { - reason = "流程在此设置了人工确认,请确认后继续。" - } else { - reason = "节点执行失败,请查看运行详情。" - } - } + reason, required := cardReason(run, item, content) body := "原因:" + cardText(reason, 76) if required != "" { body += "\n待处理:" + cardText(required, 27) @@ -208,6 +187,43 @@ func notificationCard(run domain.Run, item domain.NotificationDelivery, actions } return card } + +// cardReason is the short explanation shown on action cards: the gate +// summary, the node error or the first content line. +func cardReason(run domain.Run, item domain.NotificationDelivery, content string) (string, string) { + reason, required := "", "" + node := run.Nodes[item.NodeID] + if node != nil { + if node.GateResult != nil { + reason = node.GateResult.Summary + if strings.TrimSpace(reason) == "" { + reason = node.GateResult.Reason + } + required = node.GateResult.ActionRequired + } + if strings.TrimSpace(reason) == "" { + reason = node.Error + } + } + if strings.TrimSpace(reason) == "" { + for _, line := range strings.Split(content, "\n") { + line = strings.TrimSpace(line) + if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, "需求:") || strings.HasPrefix(line, "SOP:") || strings.HasPrefix(line, "节点:") || strings.HasPrefix(line, "查看详情:") { + continue + } + reason = line + break + } + } + if strings.TrimSpace(reason) == "" { + if item.EventType == "waiting" { + reason = "流程在此设置了人工确认,请确认后继续。" + } else { + reason = "节点执行失败,请查看运行详情。" + } + } + return reason, required +} func notificationCardIssue(content string) string { for _, line := range strings.Split(content, "\n") { if issue, ok := strings.CutPrefix(strings.TrimSpace(line), "需求:"); ok && strings.TrimSpace(issue) != "" { diff --git a/internal/botinteraction/cards_test.go b/internal/botinteraction/cards_test.go index b313ed7d6..32f74d797 100644 --- a/internal/botinteraction/cards_test.go +++ b/internal/botinteraction/cards_test.go @@ -81,7 +81,11 @@ func TestPrivateCardCallbackResolvesExactTaskToFrozenRecipient(t *testing.T) { t.Fatal(err) } defer store.Close() - bot := domain.NotificationBot{ID: "bot", BotID: "external", ProjectID: "project"} + // The inbound path reads the current configuration, so the Bot exists. + bot := domain.NotificationBot{ID: "bot", BotID: "external", ProjectID: "project", OwnerID: 1, Enabled: true, InteractionEnabled: true} + if err = store.SaveNotificationBot(bot, "s"); err != nil { + t.Fatal(err) + } delivery := domain.NotificationDelivery{ID: "delivery", BotID: bot.ID, ProjectID: bot.ProjectID, TargetID: "openid-recipient", TargetType: "single"} if err = store.DB.Create(&delivery).Error; err != nil { t.Fatal(err) diff --git a/internal/botinteraction/channel.go b/internal/botinteraction/channel.go new file mode 100644 index 000000000..4cf96ae92 --- /dev/null +++ b/internal/botinteraction/channel.go @@ -0,0 +1,162 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "context" + "errors" + "time" +) + +// Channel kinds. Stage A only connects WeCom; Feishu and DingTalk adapters are +// added in later stages and report ErrChannelNotReady until then. +const ( + ChannelWeCom = "wecom" + ChannelFeishu = "feishu" + ChannelDingTalk = "dingtalk" +) + +// ErrChannelNotReady is returned for a channel kind without a registered or +// fully configured adapter. HTTP handlers map it to `channel_not_ready`. +var ErrChannelNotReady = errors.New("channel_not_ready") + +// Credential is the decrypted secret material for one Bot configuration. +type Credential struct { + Secret string +} + +// Verification is what an adapter learns while checking credentials. Tenant +// fields are informational; tenant locking happens in the confirmation flow. +type Verification struct { + TenantKey string + TenantName string +} + +// ReplyRoute holds the channel-specific data needed to answer in the original +// conversation. It is persisted encrypted once later stages store it. +type ReplyRoute struct { + RequestID string // WeCom callback req_id used by stream replies + MessageID string // Feishu original message_id for Message Reply + Webhook string // DingTalk sessionWebhook (authorization-bearing) + ExpiresAt time.Time // zero when the route does not expire +} + +// InboundEvent is a channel-neutral message or card event. Adapters fill it +// only from trusted, authenticated connection data; the service decides the +// project, model and permissions from the Bot configuration of the connection. +type InboundEvent struct { + Channel string + AppID string // external application identity carried by the event + TenantKey string // enterprise scope carried by the event, when available + EventID string // platform delivery/event id, kept for diagnostics + MessageID string // platform business message id, used for de-duplication + + SenderID string + ChatID string + ChatType string // "single" or "group" + MsgType string + Text string + Quote string + // Mentioned is set for group messages that @ the Bot (Feishu, + // DingTalk); other group messages are ignored. + Mentioned bool + + // WeCom card callbacks. + Action string + DeliveryID string + CardTaskID string + + Route ReplyRoute +} + +// InboundAck reports the persistence outcome of one inbound event. +// Accepted is true only when this event created a new interaction; duplicates +// and ignored events return Accepted=false with a nil error. +type InboundAck struct { + Accepted bool + InteractionID string + Ignored string // reason when the event was dropped as invalid +} + +// InboundSink persists an event. A non-nil error means the event was not +// stored and the adapter must negative-acknowledge so the platform redelivers. +type InboundSink func(context.Context, InboundEvent) (InboundAck, error) + +// OutboundMessage is one reply or push. Text is final user-visible content. +type OutboundMessage struct { + InteractionID string // interaction or delivery id + Action string // WeCom card action of the source interaction, if any + ChatID string + ChatType string + Text string + SendKey string // persisted idempotency key (stage C) + Route ReplyRoute + // Card, when set on a Feishu or DingTalk push, is sent as an action + // card; Text stays the plain-text fallback. + Card *PushCard +} + +// SendOutcome classifies a send by delivery evidence, not by error type. +type SendOutcome string + +const ( + // Delivered: the platform confirmed this message. + Delivered SendOutcome = "delivered" + // NotDelivered: evidence shows the request was never submitted, or the + // platform explicitly reported it as not sent and retryable. + NotDelivered SendOutcome = "not_delivered" + // Rejected: the platform explicitly refused the message. + Rejected SendOutcome = "rejected" + // Unknown: the request may have been written; delivery cannot be proven. + Unknown SendOutcome = "unknown" +) + +type SendResult struct { + Outcome SendOutcome + Code string + Retryable bool + PlatformMessageID string + SafeDetail string // user/admin visible, never contains secrets +} + +func (r SendResult) err() error { + if r.SafeDetail != "" { + return errors.New(r.SafeDetail) + } + return errors.New("回复发送失败:" + string(r.Outcome)) +} + +// Channel is one IM platform adapter. +type Channel interface { + Kind() string + // Verify checks credentials without opening a receiving connection, so it + // never displaces the connection currently held by a receiver. + Verify(context.Context, domain.NotificationBot, Credential) (Verification, error) + // Connect opens a receiving connection. Inbound events are passed to sink. + Connect(context.Context, domain.NotificationBot, Credential, InboundSink) (Conn, error) +} + +// Conn is one live receiving connection. +type Conn interface { + // Done is closed when the connection is no longer usable. + Done() <-chan struct{} + Reply(context.Context, OutboundMessage) SendResult + Close() error +} + +// PushSender sends proactive notifications through application APIs. It is +// implemented by the channel object and does not require a receiving Conn. +type PushSender interface { + Push(context.Context, domain.NotificationBot, Credential, OutboundMessage) SendResult +} + +// progressConn is an optional Conn capability: in-place progress updates for +// a reply that is still being generated (WeCom stream replies). +type progressConn interface { + progressTurn(context.Context, domain.BotInteraction) (context.Context, func()) +} + +// cardConn is an optional Conn capability: interactive notification cards. +type cardConn interface { + sendCard(ctx context.Context, chatID string, card map[string]any) SendResult + updateActionCard(ctx context.Context, item domain.BotInteraction, result string, finish bool) error +} diff --git a/internal/botinteraction/channel_dingtalk.go b/internal/botinteraction/channel_dingtalk.go new file mode 100644 index 000000000..3db3838eb --- /dev/null +++ b/internal/botinteraction/channel_dingtalk.go @@ -0,0 +1,556 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net" + "net/http" + "net/url" + "strconv" + "strings" + "sync" + "time" + + "github.com/gorilla/websocket" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" +) + +// dingtalkChannel connects a DingTalk enterprise internal application over +// the Stream protocol and replies through the session webhook of each +// message. Only the data types of open-dingtalk/dingtalk-stream-sdk-go +// v0.9.1 are used (payload frames, chatbot callback model): its StreamClient +// can crash the host process after a pong timeout, cannot be cancelled while +// dialing and keeps reconnecting after Close (scripts/im-sdk-probe/README.md). +// This adapter owns the connection instead: +// +// - one reader goroutine; writes are serialized; nothing ever sends on a +// channel that another goroutine closes; +// - WebSocket pings every keepAlive, and the read deadline closes a +// connection that stops answering; +// - dialing and every HTTP call honor the context and have timeouts; +// - no reconnect inside the adapter: the service reconnects with backoff +// and re-reads the credential, as for the other channels; +// - a bot callback is acknowledged only after persistence, bounded by +// ackTimeout; storage errors answer 500 so DingTalk redelivers. +type dingtalkChannel struct { + // api is the open API origin; tests point it at a local server. + api string + http *http.Client + // allowInsecureStream accepts ws:// endpoints (tests only). + allowInsecureStream bool + keepAlive time.Duration + pongWait time.Duration + ackTimeout time.Duration + handshakeTimeout time.Duration + maxInFlight int + // webhookAllowed decides whether a session webhook from an event may be + // used. Production accepts only the official HTTPS endpoint. + webhookAllowed func(*url.URL) bool +} + +const ( + dingtalkDefaultAPI = "https://api.dingtalk.com" + dingtalkUserAgent = "loopforge-dingtalk-stream/1" + // dingtalkMaxResponse bounds every response body read from DingTalk. + dingtalkMaxResponse = 64 << 10 +) + +func newDingTalkChannel() *dingtalkChannel { + return &dingtalkChannel{ + api: dingtalkDefaultAPI, + http: &http.Client{Timeout: sendTimeout, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}, + keepAlive: 60 * time.Second, + pongWait: 10 * time.Second, + ackTimeout: 2500 * time.Millisecond, + handshakeTimeout: 10 * time.Second, + maxInFlight: 8, + webhookAllowed: officialDingTalkWebhook, + } +} + +func (d *dingtalkChannel) Kind() string { return ChannelDingTalk } + +// officialDingTalkWebhook accepts only the official session reply endpoint. +func officialDingTalkWebhook(u *url.URL) bool { + return u != nil && u.Scheme == "https" && u.Host == "oapi.dingtalk.com" && u.Path == "/robot/sendBySession" && u.User == nil && u.Fragment == "" +} + +// postJSON sends one JSON request without following redirects and reads at +// most dingtalkMaxResponse bytes of the answer. +func (d *dingtalkChannel) postJSON(ctx context.Context, target string, body any) (int, []byte, error) { + raw, err := json.Marshal(body) + if err != nil { + return 0, nil, err + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, target, bytes.NewReader(raw)) + if err != nil { + return 0, nil, err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", dingtalkUserAgent) + resp, err := d.http.Do(req) + if err != nil { + return 0, nil, err + } + defer resp.Body.Close() + data, err := io.ReadAll(io.LimitReader(resp.Body, dingtalkMaxResponse)) + return resp.StatusCode, data, err +} + +// Verify obtains an application access token, which proves the Client ID and +// Client Secret. The enterprise is confirmed from trusted events against the +// CorpId the administrator entered. +func (d *dingtalkChannel) Verify(ctx context.Context, bot domain.NotificationBot, cred Credential) (Verification, error) { + status, data, err := d.postJSON(ctx, d.api+"/v1.0/oauth2/accessToken", map[string]string{"appKey": bot.BotID, "appSecret": cred.Secret}) + if err != nil { + return Verification{}, errors.New("钉钉开放平台暂不可用,请稍后重试") + } + var out struct { + AccessToken string `json:"accessToken"` + Code string `json:"code"` + } + _ = json.Unmarshal(data, &out) + if status != http.StatusOK || out.AccessToken == "" { + code := out.Code + if code == "" { + code = strconv.Itoa(status) + } + return Verification{}, fmt.Errorf("钉钉拒绝了 Client ID 或 Client Secret(%s)", truncate(code, 64)) + } + return Verification{}, nil +} + +// openConnection registers the subscriptions and returns the Stream endpoint +// and one-time ticket. +func (d *dingtalkChannel) openConnection(ctx context.Context, bot domain.NotificationBot, cred Credential) (*payload.ConnectionEndpointResponse, error) { + reqBody := payload.ConnectionEndpointRequest{ + ClientId: bot.BotID, + ClientSecret: cred.Secret, + UserAgent: dingtalkUserAgent, + Subscriptions: []*payload.SubscriptionModel{ + {Type: "SYSTEM", Topic: "ping"}, + {Type: "SYSTEM", Topic: "disconnect"}, + {Type: "CALLBACK", Topic: payload.BotMessageCallbackTopic}, + {Type: "CALLBACK", Topic: payload.CardInstanceCallbackTopic}, + }, + } + status, data, err := d.postJSON(ctx, d.api+"/v1.0/gateway/connections/open", reqBody) + if err != nil { + return nil, errors.New("钉钉 Stream 接入点暂不可用") + } + if status != http.StatusOK { + return nil, fmt.Errorf("钉钉拒绝建立 Stream 连接(HTTP %d)", status) + } + endpoint := &payload.ConnectionEndpointResponse{} + if err := json.Unmarshal(data, endpoint); err != nil || endpoint.Valid() != nil { + return nil, errors.New("钉钉 Stream 接入点响应无效") + } + u, err := url.Parse(endpoint.Endpoint) + if err != nil || (u.Scheme != "wss" && !(d.allowInsecureStream && u.Scheme == "ws")) || u.Host == "" { + return nil, errors.New("钉钉 Stream 接入点地址无效") + } + return endpoint, nil +} + +// Connect opens the Stream connection and returns once it is established. +func (d *dingtalkChannel) Connect(ctx context.Context, bot domain.NotificationBot, cred Credential, sink InboundSink) (Conn, error) { + endpoint, err := d.openConnection(ctx, bot, cred) + if err != nil { + return nil, err + } + u, _ := url.Parse(endpoint.Endpoint) + q := u.Query() + q.Set("ticket", endpoint.Ticket) + u.RawQuery = q.Encode() + dialer := &websocket.Dialer{HandshakeTimeout: d.handshakeTimeout, Proxy: http.ProxyFromEnvironment} + dialCtx, cancelDial := context.WithTimeout(ctx, d.handshakeTimeout) + ws, resp, err := dialer.DialContext(dialCtx, u.String(), nil) + cancelDial() + if resp != nil && resp.Body != nil { + resp.Body.Close() + } + if err != nil { + return nil, errors.New("钉钉 Stream 连接建立失败") + } + runCtx, cancel := context.WithCancel(ctx) + c := &dingtalkConn{channel: d, bot: bot, ws: ws, sink: sink, ctx: runCtx, cancel: cancel, done: make(chan struct{}), slots: make(chan struct{}, max(1, d.maxInFlight))} + ws.SetReadLimit(1 << 20) + c.extendRead() + ws.SetPongHandler(func(string) error { c.extendRead(); return nil }) + go c.keepalive() + go c.readLoop() + return c, nil +} + +type dingtalkConn struct { + channel *dingtalkChannel + bot domain.NotificationBot + ws *websocket.Conn + sink InboundSink + ctx context.Context + cancel context.CancelFunc + done chan struct{} + + writeMu sync.Mutex + handlers sync.WaitGroup + slots chan struct{} + stopOnce sync.Once +} + +func (c *dingtalkConn) Done() <-chan struct{} { return c.done } + +// Close stops the connection and waits for the reader and in-flight +// acknowledgements to finish. +func (c *dingtalkConn) Close() error { + c.stop() + select { + case <-c.done: + case <-time.After(5 * time.Second): + } + return nil +} + +// stop cancels the context and closes the socket, which ends the reader. +func (c *dingtalkConn) stop() { + c.stopOnce.Do(func() { + c.cancel() + _ = c.ws.Close() + }) +} + +func (c *dingtalkConn) extendRead() { + _ = c.ws.SetReadDeadline(time.Now().Add(c.channel.keepAlive + c.channel.pongWait)) +} + +// keepalive pings the server and closes the socket when the context ends. +func (c *dingtalkConn) keepalive() { + ticker := time.NewTicker(c.channel.keepAlive) + defer ticker.Stop() + for { + select { + case <-c.ctx.Done(): + c.stop() + return + case <-ticker.C: + if err := c.ws.WriteControl(websocket.PingMessage, nil, time.Now().Add(5*time.Second)); err != nil { + c.stop() + return + } + } + } +} + +// readLoop is the only reader. When it ends the connection is stopped, the +// handlers drain (their writes fail on the closed socket) and Done closes. +func (c *dingtalkConn) readLoop() { + defer func() { + c.stop() + c.handlers.Wait() + close(c.done) + }() + for { + kind, data, err := c.ws.ReadMessage() + if err != nil { + return + } + c.extendRead() + if kind != websocket.TextMessage { + continue + } + if !c.handleFrame(data) { + return + } + } +} + +// handleFrame routes one data frame; false ends the connection. +func (c *dingtalkConn) handleFrame(data []byte) bool { + frame, err := payload.DecodeDataFrame(data) + if err != nil || frame == nil || frame.Headers == nil { + log.Printf("dingtalk stream frame ignored: undecodable") + return true + } + switch { + case frame.Type == "SYSTEM" && frame.GetTopic() == "ping": + pong := payload.NewDataFrameAckPong(frame.GetMessageId()) + pong.Data = frame.Data + return c.write(pong) == nil + case frame.Type == "SYSTEM" && frame.GetTopic() == "disconnect": + // The server is about to close this connection; reconnect now. + return false + case frame.Type == "CALLBACK" && (frame.GetTopic() == payload.BotMessageCallbackTopic || frame.GetTopic() == payload.CardInstanceCallbackTopic): + select { + case c.slots <- struct{}{}: + case <-c.ctx.Done(): + return false + } + c.handlers.Add(1) + go func() { + defer func() { <-c.slots; c.handlers.Done() }() + if frame.GetTopic() == payload.CardInstanceCallbackTopic { + c.write(c.handleCardCallback(frame)) + return + } + c.write(c.handleCallback(frame)) + }() + return true + default: + resp := payload.NewDataFrameResponse(payload.DataFrameResponseStatusCodeKHandlerNotFound) + resp.SetHeader(payload.DataFrameHeaderKMessageId, frame.GetMessageId()) + resp.SetHeader(payload.DataFrameHeaderKContentType, payload.DataFrameContentTypeKJson) + return c.write(resp) == nil + } +} + +// handleCallback persists a bot message and builds its acknowledgement. +func (c *dingtalkConn) handleCallback(frame *payload.DataFrame) *payload.DataFrameResponse { + resp := payload.NewSuccessDataFrameResponse() + ev, reason := dingtalkEvent(c.bot, frame.Data, frame.GetMessageId(), c.channel.webhookAllowed) + if reason != "" { + log.Printf("dingtalk inbound ignored reason=%s", reason) + } else if err := c.acknowledge(ev); err != nil { + resp = payload.NewErrorDataFrameResponse(errors.New("persist_failed")) + } + resp.SetHeader(payload.DataFrameHeaderKMessageId, frame.GetMessageId()) + resp.SetHeader(payload.DataFrameHeaderKContentType, payload.DataFrameContentTypeKJson) + return resp +} + +// handleCardCallback persists an action card click. DingTalk expects an +// answer within two seconds; the card status line is updated in place and +// the result follows as a private message. +func (c *dingtalkConn) handleCardCallback(frame *payload.DataFrame) *payload.DataFrameResponse { + status := "已收到,处理结果会以消息发给你" + ev, reason := dingtalkCardEvent(c.bot, frame.Data, frame.GetMessageId()) + if reason != "" { + log.Printf("dingtalk card callback ignored reason=%s", reason) + status = "这张卡片已失效,请到平台查看运行状态" + } else if err := c.acknowledge(ev); err != nil { + status = "暂时无法处理,请稍后再点一次" + } + resp := payload.NewSuccessDataFrameResponse() + _ = resp.SetJson(map[string]any{"response": map[string]any{ + "cardData": map[string]any{"cardParamMap": map[string]string{"status": status}}, + "cardUpdateOptions": map[string]bool{"updateCardDataByKey": true}, + }}) + resp.SetHeader(payload.DataFrameHeaderKMessageId, frame.GetMessageId()) + resp.SetHeader(payload.DataFrameHeaderKContentType, payload.DataFrameContentTypeKJson) + return resp +} + +// dingtalkCardEvent maps a card callback to an action event. The template +// buttons pass {"action": ""} as callback parameters; the instance ID +// names the delivery. +func dingtalkCardEvent(bot domain.NotificationBot, raw, frameID string) (InboundEvent, string) { + var req struct { + Type string `json:"type"` + OutTrackID string `json:"outTrackId"` + CorpID string `json:"corpId"` + UserID string `json:"userId"` + Content string `json:"content"` + } + if json.Unmarshal([]byte(raw), &req) != nil { + return InboundEvent{}, "undecodable" + } + var content struct { + CardPrivateData struct { + ActionIDs []string `json:"actionIds"` + Params map[string]any `json:"params"` + } `json:"cardPrivateData"` + } + _ = json.Unmarshal([]byte(req.Content), &content) + action, _ := content.CardPrivateData.Params["action"].(string) + if action == "" && len(content.CardPrivateData.ActionIDs) > 0 { + action = content.CardPrivateData.ActionIDs[0] + } + action = strings.TrimSpace(action) + if !pushCardKey(action) { + return InboundEvent{}, "unknown_action" + } + if req.OutTrackID == "" || req.UserID == "" || frameID == "" { + return InboundEvent{}, "missing_identity" + } + return InboundEvent{ + Channel: ChannelDingTalk, + AppID: bot.BotID, // callbacks arrive only on this application's connection + TenantKey: req.CorpID, + EventID: frameID, + MessageID: "card:" + frameID, + SenderID: req.UserID, + ChatID: req.UserID, + ChatType: "single", + MsgType: "card_action", + Action: action, + DeliveryID: dingtalkCardDelivery(req.OutTrackID), + }, "" +} + +// acknowledge runs the sink with the ack budget. Repository calls take no +// context, so a slow database answers with a failure at the deadline; the +// redelivered message is de-duplicated by its msgId. +func (c *dingtalkConn) acknowledge(ev InboundEvent) error { + ackCtx, cancel := context.WithTimeout(c.ctx, c.channel.ackTimeout) + defer cancel() + done := make(chan error, 1) + go func() { + _, err := c.sink(ackCtx, ev) + done <- err + }() + select { + case err := <-done: + return err + case <-ackCtx.Done(): + return errDingTalkAckTimeout + } +} + +var errDingTalkAckTimeout = errors.New("钉钉事件入库超时,等待平台重投") + +func (c *dingtalkConn) write(resp *payload.DataFrameResponse) error { + c.writeMu.Lock() + defer c.writeMu.Unlock() + _ = c.ws.SetWriteDeadline(time.Now().Add(5 * time.Second)) + return c.ws.WriteJSON(resp) +} + +// dingtalkCallback is the bot callback payload. robotCode is not part of the +// SDK model; when present it must name this application. +type dingtalkCallback struct { + chatbot.BotCallbackDataModel + RobotCode string `json:"robotCode"` +} + +// dingtalkEvent maps a bot callback to the channel-neutral event. A non-empty +// reason means the callback is acknowledged and dropped. +func dingtalkEvent(bot domain.NotificationBot, raw, frameID string, webhookAllowed func(*url.URL) bool) (InboundEvent, string) { + var m dingtalkCallback + if err := json.Unmarshal([]byte(raw), &m); err != nil { + return InboundEvent{}, "undecodable" + } + if m.SenderStaffId == "" { + // Without the verified staff ID there is no stable identity to bind. + return InboundEvent{}, "missing_staff_id" + } + if m.SenderCorpId != "" && m.ChatbotCorpId != "" && m.SenderCorpId != m.ChatbotCorpId { + return InboundEvent{}, "sender_corp_mismatch" + } + appID := bot.BotID + if m.RobotCode != "" { + appID = m.RobotCode + } + ev := InboundEvent{ + Channel: ChannelDingTalk, + AppID: appID, + TenantKey: m.ChatbotCorpId, + EventID: frameID, + MessageID: m.MsgId, + SenderID: m.SenderStaffId, + ChatID: m.ConversationId, + MsgType: m.Msgtype, + } + switch m.ConversationType { + case "1": + ev.ChatType = "single" + case "2": + // DingTalk delivers group messages to the robot only when it is @; + // isInAtList confirms it when present. + ev.ChatType = "group" + ev.Mentioned = m.IsInAtList || !strings.Contains(raw, `"isInAtList"`) + default: + ev.ChatType = m.ConversationType + } + if m.Msgtype == "text" { + ev.Text = strings.TrimSpace(m.Text.Content) + } + if m.SessionWebhook != "" { + u, err := url.Parse(m.SessionWebhook) + if err != nil || webhookAllowed == nil || !webhookAllowed(u) { + return InboundEvent{}, "untrusted_webhook" + } + ev.Route.Webhook = m.SessionWebhook + if m.SessionWebhookExpiredTime > 0 { + // sessionWebhookExpiredTime is epoch milliseconds. + ev.Route.ExpiresAt = time.UnixMilli(m.SessionWebhookExpiredTime).UTC() + } + } + return ev, "" +} + +// Reply answers in the original conversation through its session webhook. +func (c *dingtalkConn) Reply(ctx context.Context, msg OutboundMessage) SendResult { + return c.channel.replyWebhook(ctx, msg) +} + +func (d *dingtalkChannel) replyWebhook(ctx context.Context, msg OutboundMessage) SendResult { + if msg.Route.Webhook == "" { + return SendResult{Outcome: Rejected, Code: "route_missing", SafeDetail: "缺少钉钉会话回复地址,无法在原会话回复"} + } + u, err := url.Parse(msg.Route.Webhook) + if err != nil || d.webhookAllowed == nil || !d.webhookAllowed(u) { + return SendResult{Outcome: Rejected, Code: "route_untrusted", SafeDetail: "钉钉会话回复地址无效"} + } + if !msg.Route.ExpiresAt.IsZero() && !time.Now().Add(sendTimeout).Before(msg.Route.ExpiresAt) { + return SendResult{Outcome: Rejected, Code: "route_expired", SafeDetail: "会话回复授权已过期"} + } + status, data, err := d.postJSON(ctx, u.String(), map[string]any{"msgtype": "text", "text": map[string]string{"content": msg.Text}}) + if err != nil { + return classifyDingTalkTransport(err) + } + return classifyDingTalkResponse(status, data) +} + +// classifyDingTalkTransport: a failed dial or DNS lookup sent nothing; any +// other transport error may follow a delivered request. DingTalk session +// replies have no idempotency key, so unknown stops at reply_unknown. +func classifyDingTalkTransport(err error) SendResult { + var op *net.OpError + var dns *net.DNSError + if (errors.As(err, &op) && op.Op == "dial") || errors.As(err, &dns) { + return SendResult{Outcome: NotDelivered, Code: "dial_failed", Retryable: true, SafeDetail: "无法连接钉钉开放平台"} + } + return SendResult{Outcome: Unknown, Code: "transport", SafeDetail: "钉钉回复请求未返回结果"} +} + +// dingtalkRetryable lists errcodes meaning "not sent, try again". +var dingtalkRetryable = map[int]bool{ + 130101: true, // sending too fast +} + +func classifyDingTalkResponse(status int, data []byte) SendResult { + switch { + case status == http.StatusTooManyRequests: + return SendResult{Outcome: NotDelivered, Code: "429", Retryable: true, SafeDetail: "钉钉限流,稍后重试"} + case status >= 500: + return SendResult{Outcome: Unknown, Code: strconv.Itoa(status), SafeDetail: "钉钉返回服务端错误,送达结果未知"} + case status != http.StatusOK: + return SendResult{Outcome: Rejected, Code: strconv.Itoa(status), SafeDetail: "钉钉拒绝了这次回复(HTTP " + strconv.Itoa(status) + ")"} + } + var out struct { + ErrCode *int `json:"errcode"` + ErrMsg string `json:"errmsg"` + } + if json.Unmarshal(data, &out) != nil || out.ErrCode == nil { + return SendResult{Outcome: Unknown, Code: "unparsed", SafeDetail: "钉钉回复响应无法识别,送达结果未知"} + } + code := *out.ErrCode + if code == 0 { + return SendResult{Outcome: Delivered} + } + detail := "钉钉拒绝了这次回复(错误码 " + strconv.Itoa(code) + ")" + if dingtalkRetryable[code] { + return SendResult{Outcome: NotDelivered, Code: strconv.Itoa(code), Retryable: true, SafeDetail: detail} + } + if code == -1 { + // "System busy" does not say whether the message went out. + return SendResult{Outcome: Unknown, Code: "-1", SafeDetail: "钉钉系统繁忙,送达结果未知"} + } + return SendResult{Outcome: Rejected, Code: strconv.Itoa(code), SafeDetail: detail} +} diff --git a/internal/botinteraction/channel_dingtalk_test.go b/internal/botinteraction/channel_dingtalk_test.go new file mode 100644 index 000000000..01bba9a26 --- /dev/null +++ b/internal/botinteraction/channel_dingtalk_test.go @@ -0,0 +1,456 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/gorilla/websocket" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" +) + +const dingtalkWebhook = "https://oapi.dingtalk.com/robot/sendBySession?session=s1" + +func dingtalkCallbackJSON(msgID, convType, msgType, text string, extra map[string]any) string { + m := map[string]any{ + "conversationId": "cid-1", "chatbotCorpId": "ding-corp", "senderCorpId": "ding-corp", + "msgId": msgID, "senderStaffId": "staff-1", "senderId": "$:LWCP_v1:$x", "conversationType": convType, + "sessionWebhook": dingtalkWebhook, "sessionWebhookExpiredTime": int64(1_900_000_000_000), "msgtype": msgType, + } + if msgType == "text" { + m["text"] = map[string]string{"content": text} + } + for k, v := range extra { + if v == nil { + delete(m, k) + } else { + m[k] = v + } + } + raw, _ := json.Marshal(m) + return string(raw) +} + +func TestDingTalkEventMapping(t *testing.T) { + bot := domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelDingTalk, BotID: "ding-app"} + ev, reason := dingtalkEvent(bot, dingtalkCallbackJSON("msg-1", "1", "text", " 项目状态 ", nil), "frame-1", officialDingTalkWebhook) + if reason != "" || ev.AppID != "ding-app" || ev.TenantKey != "ding-corp" || ev.MessageID != "msg-1" || ev.EventID != "frame-1" || + ev.SenderID != "staff-1" || ev.ChatID != "cid-1" || ev.ChatType != "single" || ev.MsgType != "text" || ev.Text != "项目状态" || + ev.Route.Webhook != dingtalkWebhook || !ev.Route.ExpiresAt.Equal(time.UnixMilli(1_900_000_000_000).UTC()) { + t.Fatalf("event = %+v %s", ev, reason) + } + if ev, _ := dingtalkEvent(bot, dingtalkCallbackJSON("m", "2", "text", "@bot hi", nil), "f", officialDingTalkWebhook); ev.ChatType != "group" || !ev.Mentioned { + t.Fatalf("group = %+v", ev) + } + if ev, _ := dingtalkEvent(bot, dingtalkCallbackJSON("m", "2", "text", "hi", map[string]any{"isInAtList": false}), "f", officialDingTalkWebhook); ev.Mentioned { + t.Fatalf("group without @ = %+v", ev) + } + if ev, _ := dingtalkEvent(bot, dingtalkCallbackJSON("m", "2", "text", "hi", map[string]any{"isInAtList": true}), "f", officialDingTalkWebhook); !ev.Mentioned { + t.Fatalf("group with @ = %+v", ev) + } + if ev, _ := dingtalkEvent(bot, dingtalkCallbackJSON("m", "1", "picture", "", nil), "f", officialDingTalkWebhook); ev.MsgType != "picture" || ev.Text != "" { + t.Fatalf("picture = %+v", ev) + } + // robotCode, when present, must name this application (checked by the + // common inbound path as app_mismatch). + if ev, _ := dingtalkEvent(bot, dingtalkCallbackJSON("m", "1", "text", "x", map[string]any{"robotCode": "other-app"}), "f", officialDingTalkWebhook); ev.AppID != "other-app" { + t.Fatalf("robotCode = %+v", ev) + } + for name, extra := range map[string]map[string]any{ + "missing_staff_id": {"senderStaffId": nil}, + "sender_corp_mismatch": {"senderCorpId": "other-corp"}, + "untrusted_webhook": {"sessionWebhook": "https://evil.example/robot/sendBySession?session=s"}, + } { + if _, reason := dingtalkEvent(bot, dingtalkCallbackJSON("m", "1", "text", "x", extra), "f", officialDingTalkWebhook); reason != name { + t.Errorf("%s: reason = %q", name, reason) + } + } + if _, reason := dingtalkEvent(bot, "{", "f", officialDingTalkWebhook); reason != "undecodable" { + t.Fatal(reason) + } + for _, raw := range []string{"http://oapi.dingtalk.com/robot/sendBySession?session=s", "https://oapi.dingtalk.com.evil/robot/sendBySession", "https://oapi.dingtalk.com/robot/send?access_token=x", "https://u:p@oapi.dingtalk.com/robot/sendBySession"} { + u, _ := url.Parse(raw) + if officialDingTalkWebhook(u) { + t.Errorf("accepted %s", raw) + } + } +} + +// fakeDingTalk plays the open API and the Stream gateway. +type fakeDingTalk struct { + t *testing.T + srv *httptest.Server + conns chan *websocket.Conn + acks chan payload.DataFrameResponse + closed chan struct{} + endpointOK atomic.Bool + noPong atomic.Bool + mu sync.Mutex + writeMu sync.Mutex + webhook func(w http.ResponseWriter, r *http.Request) + requests []map[string]any +} + +func newFakeDingTalk(t *testing.T) *fakeDingTalk { + f := &fakeDingTalk{t: t, conns: make(chan *websocket.Conn, 4), acks: make(chan payload.DataFrameResponse, 64), closed: make(chan struct{}, 4)} + f.endpointOK.Store(true) + mux := http.NewServeMux() + mux.HandleFunc("/v1.0/oauth2/accessToken", func(w http.ResponseWriter, r *http.Request) { + var in map[string]string + json.NewDecoder(r.Body).Decode(&in) + if in["appKey"] != "ding-app" || in["appSecret"] != "secret" { + w.WriteHeader(400) + json.NewEncoder(w).Encode(map[string]string{"code": "invalidClientIdOrSecret", "message": "x"}) + return + } + json.NewEncoder(w).Encode(map[string]any{"accessToken": "tok", "expireIn": 7200}) + }) + mux.HandleFunc("/v1.0/gateway/connections/open", func(w http.ResponseWriter, r *http.Request) { + var in payload.ConnectionEndpointRequest + json.NewDecoder(r.Body).Decode(&in) + topics := []string{} + for _, s := range in.Subscriptions { + topics = append(topics, s.Type+":"+s.Topic) + } + if !f.endpointOK.Load() || in.ClientId != "ding-app" || in.ClientSecret != "secret" || !strings.Contains(strings.Join(topics, ","), "CALLBACK:"+payload.BotMessageCallbackTopic) { + w.WriteHeader(401) + return + } + json.NewEncoder(w).Encode(payload.ConnectionEndpointResponse{Endpoint: "ws" + strings.TrimPrefix(f.srv.URL, "http") + "/stream", Ticket: "ticket-1"}) + }) + var upgrader websocket.Upgrader + mux.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("ticket") != "ticket-1" { + w.WriteHeader(403) + return + } + c, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + if f.noPong.Load() { + c.SetPingHandler(func(string) error { return nil }) + } + f.conns <- c + for { + _, raw, err := c.ReadMessage() + if err != nil { + f.closed <- struct{}{} + return + } + var resp payload.DataFrameResponse + if json.Unmarshal(raw, &resp) == nil { + f.acks <- resp + } + } + }) + mux.HandleFunc("/robot/sendBySession", func(w http.ResponseWriter, r *http.Request) { + var body map[string]any + json.NewDecoder(r.Body).Decode(&body) + f.mu.Lock() + f.requests = append(f.requests, body) + h := f.webhook + f.mu.Unlock() + if h != nil { + h(w, r) + return + } + json.NewEncoder(w).Encode(map[string]any{"errcode": 0, "errmsg": "ok"}) + }) + f.srv = httptest.NewServer(mux) + t.Cleanup(f.srv.Close) + return f +} + +func (f *fakeDingTalk) channel() *dingtalkChannel { + ch := newDingTalkChannel() + ch.api = f.srv.URL + ch.allowInsecureStream = true + ch.keepAlive = 200 * time.Millisecond + ch.pongWait = 300 * time.Millisecond + ch.ackTimeout = 300 * time.Millisecond + host := strings.TrimPrefix(f.srv.URL, "http://") + ch.webhookAllowed = func(u *url.URL) bool { return u.Host == host && u.Path == "/robot/sendBySession" } + return ch +} + +func (f *fakeDingTalk) webhookURL() string { return f.srv.URL + "/robot/sendBySession?session=s1" } + +func (f *fakeDingTalk) send(c *websocket.Conn, typ, topic, id, data string) { + f.t.Helper() + frame := payload.DataFrame{SpecVersion: "1.0", Type: typ, Time: time.Now().UnixMilli(), Headers: payload.DataFrameHeader{"topic": topic, "messageId": id, "contentType": "application/json"}, Data: data} + f.writeMu.Lock() + defer f.writeMu.Unlock() + if err := c.WriteMessage(websocket.TextMessage, frame.Encode()); err != nil { + f.t.Fatal(err) + } +} + +func (f *fakeDingTalk) ack(t *testing.T) payload.DataFrameResponse { + t.Helper() + select { + case a := <-f.acks: + return a + case <-time.After(5 * time.Second): + t.Fatal("no ack") + return payload.DataFrameResponse{} + } +} + +func waitClosed(t *testing.T, done <-chan struct{}, within time.Duration, what string) { + t.Helper() + select { + case <-done: + case <-time.After(within): + t.Fatalf("%s: connection not closed within %s", what, within) + } +} + +var dingtalkBot = domain.NotificationBot{ID: "cfg-dt", Channel: domain.BotChannelDingTalk, BotID: "ding-app"} + +func TestDingTalkVerify(t *testing.T) { + f := newFakeDingTalk(t) + ch := f.channel() + if _, err := ch.Verify(context.Background(), dingtalkBot, Credential{Secret: "secret"}); err != nil { + t.Fatal(err) + } + if _, err := ch.Verify(context.Background(), dingtalkBot, Credential{Secret: "bad"}); err == nil || !strings.Contains(err.Error(), "invalidClientIdOrSecret") { + t.Fatalf("bad secret = %v", err) + } +} + +func TestDingTalkStreamPingCallbackAndAck(t *testing.T) { + f := newFakeDingTalk(t) + var mu sync.Mutex + var got []InboundEvent + mode := "ok" + sink := func(ctx context.Context, ev InboundEvent) (InboundAck, error) { + mu.Lock() + m := mode + got = append(got, ev) + mu.Unlock() + switch m { + case "fail": + return InboundAck{}, errors.New("database unavailable") + case "block": + time.Sleep(2 * time.Second) + } + return InboundAck{Accepted: true}, nil + } + ch := f.channel() + ch.webhookAllowed = officialDingTalkWebhook // fixtures carry the official address + conn, err := ch.Connect(context.Background(), dingtalkBot, Credential{Secret: "secret"}, sink) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + server := <-f.conns + + f.send(server, "SYSTEM", "ping", "ping-1", `{"opaque":"42"}`) + if a := f.ack(t); a.Code != 200 || a.Headers["messageId"] != "ping-1" || a.Data != `{"opaque":"42"}` { + t.Fatalf("pong = %+v", a) + } + f.send(server, "CALLBACK", payload.BotMessageCallbackTopic, "frame-1", dingtalkCallbackJSON("msg-1", "1", "text", "你好", nil)) + if a := f.ack(t); a.Code != 200 || a.Headers["messageId"] != "frame-1" { + t.Fatalf("stored ack = %+v", a) + } + mu.Lock() + mode = "fail" + mu.Unlock() + f.send(server, "CALLBACK", payload.BotMessageCallbackTopic, "frame-2", dingtalkCallbackJSON("msg-2", "1", "text", "再来", nil)) + if a := f.ack(t); a.Code != 500 || a.Headers["messageId"] != "frame-2" { + t.Fatalf("failed ack = %+v", a) + } + mu.Lock() + mode = "block" + mu.Unlock() + start := time.Now() + f.send(server, "CALLBACK", payload.BotMessageCallbackTopic, "frame-3", dingtalkCallbackJSON("msg-3", "1", "text", "慢", nil)) + if a := f.ack(t); a.Code != 500 || time.Since(start) > time.Second { + t.Fatalf("blocked sink ack = %+v after %s", a, time.Since(start)) + } + // Dropped callbacks (no staff id) and unknown topics are still answered. + f.send(server, "CALLBACK", payload.BotMessageCallbackTopic, "frame-4", dingtalkCallbackJSON("msg-4", "1", "text", "x", map[string]any{"senderStaffId": nil})) + if a := f.ack(t); a.Code != 200 { + t.Fatalf("dropped ack = %+v", a) + } + f.send(server, "EVENT", "chat_update", "frame-5", "{}") + if a := f.ack(t); a.Code != 404 || a.Headers["messageId"] != "frame-5" { + t.Fatalf("unknown topic = %+v", a) + } + mu.Lock() + if len(got) != 3 || got[0].MessageID != "msg-1" || got[0].Text != "你好" || got[0].Route.Webhook != dingtalkWebhook { + t.Fatalf("events = %+v", got) + } + mu.Unlock() +} + +func TestDingTalkStreamLifecycle(t *testing.T) { + sink := func(ctx context.Context, ev InboundEvent) (InboundAck, error) { return InboundAck{Accepted: true}, nil } + t.Run("server disconnect frame", func(t *testing.T) { + f := newFakeDingTalk(t) + conn, err := f.channel().Connect(context.Background(), dingtalkBot, Credential{Secret: "secret"}, sink) + if err != nil { + t.Fatal(err) + } + server := <-f.conns + f.send(server, "SYSTEM", "disconnect", "d-1", "{}") + waitClosed(t, conn.Done(), 2*time.Second, "disconnect") + }) + t.Run("server stops answering pings", func(t *testing.T) { + f := newFakeDingTalk(t) + f.noPong.Store(true) + conn, err := f.channel().Connect(context.Background(), dingtalkBot, Credential{Secret: "secret"}, sink) + if err != nil { + t.Fatal(err) + } + server := <-f.conns + waitClosed(t, conn.Done(), 2*time.Second, "pong timeout") + // The defect of the SDK loop: a late frame after the timeout. It must + // be harmless here (the process would crash with the SDK). + f.writeMu.Lock() + _ = server.WriteMessage(websocket.TextMessage, []byte(`{"type":"SYSTEM","headers":{"topic":"ping","messageId":"late"}}`)) + f.writeMu.Unlock() + time.Sleep(200 * time.Millisecond) + }) + t.Run("pongs keep the connection alive", func(t *testing.T) { + f := newFakeDingTalk(t) + conn, err := f.channel().Connect(context.Background(), dingtalkBot, Credential{Secret: "secret"}, sink) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + select { + case <-conn.Done(): + t.Fatal("healthy connection closed") + case <-time.After(1500 * time.Millisecond): + } + }) + t.Run("context cancel", func(t *testing.T) { + f := newFakeDingTalk(t) + ctx, cancel := context.WithCancel(context.Background()) + conn, err := f.channel().Connect(ctx, dingtalkBot, Credential{Secret: "secret"}, sink) + if err != nil { + t.Fatal(err) + } + cancel() + waitClosed(t, conn.Done(), 2*time.Second, "cancel") + select { + case <-f.closed: + case <-time.After(2 * time.Second): + t.Fatal("server side not closed") + } + }) + t.Run("close with a blocked handler and frames in flight", func(t *testing.T) { + f := newFakeDingTalk(t) + release := make(chan struct{}) + defer close(release) + blocked := func(ctx context.Context, ev InboundEvent) (InboundAck, error) { <-release; return InboundAck{}, nil } + conn, err := f.channel().Connect(context.Background(), dingtalkBot, Credential{Secret: "secret"}, blocked) + if err != nil { + t.Fatal(err) + } + server := <-f.conns + for i := 0; i < 20; i++ { + f.send(server, "CALLBACK", payload.BotMessageCallbackTopic, fmt.Sprintf("f-%d", i), dingtalkCallbackJSON(fmt.Sprintf("m-%d", i), "1", "text", "x", nil)) + } + start := time.Now() + conn.Close() + waitClosed(t, conn.Done(), time.Second, "close") + if time.Since(start) > 2*time.Second { + t.Fatalf("Close took %s", time.Since(start)) + } + // Frames after close: nothing panics. + f.writeMu.Lock() + _ = server.WriteMessage(websocket.TextMessage, []byte(`{}`)) + f.writeMu.Unlock() + }) + t.Run("rejected endpoint and bad scheme", func(t *testing.T) { + f := newFakeDingTalk(t) + f.endpointOK.Store(false) + if _, err := f.channel().Connect(context.Background(), dingtalkBot, Credential{Secret: "secret"}, sink); err == nil || !strings.Contains(err.Error(), "401") { + t.Fatalf("rejected endpoint = %v", err) + } + f.endpointOK.Store(true) + ch := f.channel() + ch.allowInsecureStream = false + if _, err := ch.Connect(context.Background(), dingtalkBot, Credential{Secret: "secret"}, sink); err == nil || !strings.Contains(err.Error(), "地址无效") { + t.Fatalf("ws:// in production mode = %v", err) + } + }) +} + +func TestDingTalkReplyClassification(t *testing.T) { + f := newFakeDingTalk(t) + ch := f.channel() + msg := OutboundMessage{InteractionID: "i", Text: "答复", Route: ReplyRoute{Webhook: f.webhookURL(), ExpiresAt: time.Now().Add(time.Hour)}} + if got := ch.replyWebhook(context.Background(), msg); got.Outcome != Delivered { + t.Fatalf("reply = %+v", got) + } + f.mu.Lock() + body := f.requests[0] + f.mu.Unlock() + if body["msgtype"] != "text" || body["text"].(map[string]any)["content"] != "答复" { + t.Fatalf("body = %v", body) + } + cases := []struct { + name string + status int + body string + want SendOutcome + }{ + {"rate limited errcode", 200, `{"errcode":130101,"errmsg":"send too fast"}`, NotDelivered}, + {"busy", 200, `{"errcode":-1,"errmsg":"busy"}`, Unknown}, + {"other errcode", 200, `{"errcode":300001,"errmsg":"session expired"}`, Rejected}, + {"unparsed 200", 200, ``, Unknown}, + {"server error", 502, ``, Unknown}, + {"too many requests", 429, ``, NotDelivered}, + {"forbidden", 403, ``, Rejected}, + {"redirect is not followed", 302, ``, Rejected}, + } + for _, tc := range cases { + f.mu.Lock() + f.webhook = func(w http.ResponseWriter, r *http.Request) { + if tc.status == 302 { + w.Header().Set("Location", "https://evil.example/") + } + w.WriteHeader(tc.status) + fmt.Fprint(w, tc.body) + } + f.mu.Unlock() + if got := ch.replyWebhook(context.Background(), msg); got.Outcome != tc.want || got.SafeDetail == "" { + t.Errorf("%s = %+v", tc.name, got) + } + } + for name, m := range map[string]OutboundMessage{ + "route_missing": {Text: "x"}, + "route_untrusted": {Text: "x", Route: ReplyRoute{Webhook: "https://evil.example/robot/sendBySession"}}, + "route_expired": {Text: "x", Route: ReplyRoute{Webhook: f.webhookURL(), ExpiresAt: time.Now().Add(5 * time.Second)}}, + } { + if got := ch.replyWebhook(context.Background(), m); got.Outcome != Rejected || got.Code != name { + t.Errorf("%s = %+v", name, got) + } + } + // Transport: refused connection sent nothing; a reset after the request + // may have been delivered. + dead := httptest.NewServer(http.NotFoundHandler()) + deadURL := dead.URL + dead.Close() + ch.webhookAllowed = func(*url.URL) bool { return true } + if got := ch.replyWebhook(context.Background(), OutboundMessage{Text: "x", Route: ReplyRoute{Webhook: deadURL + "/robot/sendBySession"}}); got.Outcome != NotDelivered { + t.Fatalf("refused = %+v", got) + } + if got := classifyDingTalkTransport(errors.New("read: connection reset by peer")); got.Outcome != Unknown { + t.Fatalf("reset = %+v", got) + } +} diff --git a/internal/botinteraction/channel_feishu.go b/internal/botinteraction/channel_feishu.go new file mode 100644 index 000000000..254a8c714 --- /dev/null +++ b/internal/botinteraction/channel_feishu.go @@ -0,0 +1,370 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "strconv" + "strings" + "sync" + "time" + + lark "github.com/larksuite/oapi-sdk-go/v3" + larkcore "github.com/larksuite/oapi-sdk-go/v3/core" + "github.com/larksuite/oapi-sdk-go/v3/event/dispatcher" + "github.com/larksuite/oapi-sdk-go/v3/event/dispatcher/callback" + larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" + larkws "github.com/larksuite/oapi-sdk-go/v3/ws" +) + +// feishuChannel connects a Feishu self-built application through the +// official long-connection client (larksuite/oapi-sdk-go v3.13.0) and +// replies with Message Reply. Only private text chats are handled; the +// common inbound path drops group events and classifies other types. +// +// Delivery: Feishu de-duplicates replies that carry the same uuid for one +// hour, so the persisted send key is passed as uuid and unknown outcomes are +// retried with that key (sendPolicy marks Feishu idempotent). The SDK itself +// retries a request only after a dial failure or an invalid tenant token, +// both with the identical body and therefore the same uuid. +type feishuChannel struct { + // base is the open platform origin; tests point it at a local server. + base string + http *http.Client + // ackTimeout bounds persistence inside the event callback so the SDK + // acknowledges within Feishu's three-second window. + ackTimeout time.Duration + readyWait time.Duration +} + +func newFeishuChannel() *feishuChannel { + return &feishuChannel{ + base: lark.FeishuBaseUrl, + http: &http.Client{Timeout: sendTimeout, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}, + ackTimeout: 2500 * time.Millisecond, + readyWait: 15 * time.Second, + } +} + +func (f *feishuChannel) Kind() string { return ChannelFeishu } + +func (f *feishuChannel) client(bot domain.NotificationBot, cred Credential) *lark.Client { + return lark.NewClient(bot.BotID, cred.Secret, + lark.WithOpenBaseUrl(f.base), + lark.WithHttpClient(f.http), + lark.WithReqTimeout(sendTimeout), + lark.WithEnableTokenCache(true), + lark.WithLogLevel(larkcore.LogLevelError), + ) +} + +// Verify obtains a tenant access token, which proves the App ID and Secret. +// The enterprise query is informational: its failure (for example a missing +// permission) does not fail verification. The tenant key itself is locked +// only through the trusted-event confirmation flow. +func (f *feishuChannel) Verify(ctx context.Context, bot domain.NotificationBot, cred Credential) (Verification, error) { + cli := f.client(bot, cred) + resp, err := cli.GetTenantAccessTokenBySelfBuiltApp(ctx, &larkcore.SelfBuiltTenantAccessTokenReq{AppID: bot.BotID, AppSecret: cred.Secret}) + if err != nil { + return Verification{}, errors.New("飞书开放平台暂不可用,请稍后重试") + } + if resp.Code != 0 || resp.TenantAccessToken == "" { + return Verification{}, fmt.Errorf("飞书拒绝了 App ID 或 App Secret(错误码 %d)", resp.Code) + } + out := Verification{} + if q, err := cli.Tenant.V2.Tenant.Query(ctx); err == nil && q.Success() && q.Data != nil && q.Data.Tenant != nil { + if q.Data.Tenant.TenantKey != nil { + out.TenantKey = *q.Data.Tenant.TenantKey + } + if q.Data.Tenant.Name != nil { + out.TenantName = *q.Data.Tenant.Name + } + } + return out, nil +} + +// Connect starts the long connection and returns once the SDK reports it is +// ready. The SDK's own reconnect loop is disabled: the service reconnects +// with backoff and re-reads the credential each time. +func (f *feishuChannel) Connect(ctx context.Context, bot domain.NotificationBot, cred Credential, sink InboundSink) (Conn, error) { + conn := &feishuConn{channel: f, bot: bot, api: f.client(bot, cred), done: make(chan struct{})} + handler := dispatcher.NewEventDispatcher("", "").OnP2MessageReceiveV1(func(evCtx context.Context, ev *larkim.P2MessageReceiveV1) error { + inbound, ok := feishuEvent(ev) + if !ok { + return nil + } + return f.acknowledge(ctx, inbound, sink) + }).OnP2CardActionTrigger(func(evCtx context.Context, ev *callback.CardActionTriggerEvent) (*callback.CardActionTriggerResponse, error) { + inbound, ok := feishuCardEvent(ev) + if !ok { + return feishuToast("error", "这张卡片已失效,请到平台查看运行状态"), nil + } + if err := f.acknowledge(ctx, inbound, sink); err != nil { + return feishuToast("error", "暂时无法处理,请稍后再点一次"), nil + } + return feishuToast("info", "已收到,处理结果会以消息发给你"), nil + }) + ready := make(chan struct{}) + var readyOnce sync.Once + conn.ws = larkws.NewClient(bot.BotID, cred.Secret, + larkws.WithEventHandler(handler), + larkws.WithAutoReconnect(false), + larkws.WithDomain(f.base), + larkws.WithHttpClient(f.http), + larkws.WithLogLevel(larkcore.LogLevelError), + larkws.WithOnReady(func() { readyOnce.Do(func() { close(ready) }) }), + ) + runCtx, cancel := context.WithCancel(ctx) + conn.cancel = cancel + go func() { + defer close(conn.done) + conn.err = conn.ws.Start(runCtx) + }() + timer := time.NewTimer(f.readyWait) + defer timer.Stop() + select { + case <-ready: + return conn, nil + case <-conn.done: + cancel() + return nil, errors.New("飞书长连接建立失败") + case <-timer.C: + conn.Close() + return nil, errors.New("飞书长连接建立超时") + case <-ctx.Done(): + conn.Close() + return nil, ctx.Err() + } +} + +// errFeishuAckTimeout: persistence did not finish inside the ack budget. +var errFeishuAckTimeout = errors.New("飞书事件入库超时,等待平台重投") + +// acknowledge persists an event within Feishu's three-second ack window. A +// storage error is returned so the SDK answers 500 and Feishu redelivers; +// duplicates and ignored events are acknowledged. The repository calls do not +// take a context, so the sink runs in its own goroutine and a slow database +// answers with a failure at the deadline instead of blocking past the window. +// The write may still complete afterwards; the redelivered event is then a +// duplicate of the same business message and is acknowledged without a second +// interaction. +func (f *feishuChannel) acknowledge(ctx context.Context, inbound InboundEvent, sink InboundSink) error { + ackCtx, cancel := context.WithTimeout(ctx, f.ackTimeout) + defer cancel() + done := make(chan error, 1) + go func() { + _, err := sink(ackCtx, inbound) + done <- err + }() + select { + case err := <-done: + return err + case <-ackCtx.Done(): + return errFeishuAckTimeout + } +} + +// feishuEvent maps a receive event to the channel-neutral event. Text +// content is JSON ({"text": ...}); other message types keep their type and +// an empty text for classification. +func feishuEvent(ev *larkim.P2MessageReceiveV1) (InboundEvent, bool) { + if ev == nil || ev.EventV2Base == nil || ev.EventV2Base.Header == nil || ev.Event == nil || ev.Event.Message == nil || ev.Event.Sender == nil || ev.Event.Sender.SenderId == nil { + return InboundEvent{}, false + } + msg := ev.Event.Message + out := InboundEvent{ + Channel: ChannelFeishu, + AppID: ev.EventV2Base.Header.AppID, + TenantKey: ev.EventV2Base.Header.TenantKey, + EventID: ev.EventV2Base.Header.EventID, + MessageID: deref(msg.MessageId), + SenderID: deref(ev.Event.Sender.SenderId.OpenId), + ChatID: deref(msg.ChatId), + MsgType: deref(msg.MessageType), + } + switch deref(msg.ChatType) { + case "p2p": + out.ChatType = "single" + case "group": + out.ChatType = "group" + default: + out.ChatType = deref(msg.ChatType) + } + if out.MsgType == "text" { + var content struct { + Text string `json:"text"` + } + if json.Unmarshal([]byte(deref(msg.Content)), &content) == nil { + out.Text = content.Text + } + } + // Group messages count only when they @ a bot (without the "all group + // messages" permission Feishu delivers nothing else). The @ placeholders + // (@_user_1 …) are removed from the text. + mentionedBot := false + for _, m := range msg.Mentions { + if m == nil { + continue + } + if deref(m.MentionedType) == "bot" { + mentionedBot = true + } + if key := deref(m.Key); key != "" { + out.Text = strings.ReplaceAll(out.Text, key, "") + } + } + out.Text = strings.TrimSpace(out.Text) + if out.ChatType == "group" { + out.Mentioned = mentionedBot || (len(msg.Mentions) > 0 && !feishuMentionTypesKnown(msg.Mentions)) + } + out.Route.MessageID = out.MessageID + return out, true +} + +// feishuMentionTypesKnown reports whether the event carries mentioned_type +// (older events do not); without it any mention counts as an @ of the Bot. +func feishuMentionTypesKnown(mentions []*larkim.MentionEvent) bool { + for _, m := range mentions { + if m != nil && m.MentionedType != nil { + return true + } + } + return false +} + +// feishuCardEvent maps a card button click (card.action.trigger) to an +// action event. The button value carries the action and the delivery; the +// clicker is identified by open_id, like private-chat senders. +func feishuCardEvent(ev *callback.CardActionTriggerEvent) (InboundEvent, bool) { + if ev == nil || ev.EventV2Base == nil || ev.EventV2Base.Header == nil || ev.Event == nil || ev.Event.Action == nil || ev.Event.Operator == nil { + return InboundEvent{}, false + } + action, _ := ev.Event.Action.Value["af_action"].(string) + delivery, _ := ev.Event.Action.Value["af_delivery"].(string) + if DraftAction(action) { + // Draft confirmation cards carry the draft ID instead. + delivery, _ = ev.Event.Action.Value["af_draft"].(string) + } + if !pushCardKey(action) || delivery == "" || ev.Event.Operator.OpenID == "" || ev.EventV2Base.Header.EventID == "" { + return InboundEvent{}, false + } + out := InboundEvent{ + Channel: ChannelFeishu, + AppID: ev.EventV2Base.Header.AppID, + TenantKey: ev.EventV2Base.Header.TenantKey, + EventID: ev.EventV2Base.Header.EventID, + MessageID: "card:" + ev.EventV2Base.Header.EventID, + SenderID: ev.Event.Operator.OpenID, + ChatID: ev.Event.Operator.OpenID, + ChatType: "single", + MsgType: "card_action", + Action: action, + DeliveryID: delivery, + } + if out.TenantKey == "" && ev.Event.Operator.TenantKey != nil { + out.TenantKey = *ev.Event.Operator.TenantKey + } + return out, true +} + +func feishuToast(kind, text string) *callback.CardActionTriggerResponse { + return &callback.CardActionTriggerResponse{Toast: &callback.Toast{Type: kind, Content: text}} +} + +func deref(s *string) string { + if s == nil { + return "" + } + return *s +} + +type feishuConn struct { + channel *feishuChannel + bot domain.NotificationBot + api *lark.Client + ws *larkws.Client + cancel context.CancelFunc + done chan struct{} + err error + closeOnce sync.Once +} + +func (c *feishuConn) Done() <-chan struct{} { return c.done } + +// Close stops the client and waits for its workers to finish. +func (c *feishuConn) Close() error { + c.closeOnce.Do(func() { + c.cancel() + waitCtx, stop := context.WithTimeout(context.Background(), 5*time.Second) + defer stop() + _ = c.ws.CloseAndWait(waitCtx) + }) + return nil +} + +// Reply answers the original message with the persisted key as uuid. +func (c *feishuConn) Reply(ctx context.Context, msg OutboundMessage) SendResult { + if msg.Route.MessageID == "" { + return SendResult{Outcome: Rejected, Code: "route_missing", SafeDetail: "缺少飞书原消息标识,无法在原会话回复"} + } + msgType, content := "text", []byte(nil) + if msg.Card != nil { + // Write confirmations go out as a card with buttons. + msgType, content = "interactive", feishuCardJSON(msg.Card) + } else { + content, _ = json.Marshal(map[string]string{"text": msg.Text}) + } + body := larkim.NewReplyMessageReqBodyBuilder().MsgType(msgType).Content(string(content)) + if msg.SendKey != "" { + body = body.Uuid(msg.SendKey) + } + req := larkim.NewReplyMessageReqBuilder().MessageId(msg.Route.MessageID).Body(body.Build()).Build() + resp, err := c.api.Im.V1.Message.Reply(ctx, req) + if err != nil { + return classifyFeishuTransport(err) + } + if !resp.Success() { + return classifyFeishuCode(resp.Code) + } + out := SendResult{Outcome: Delivered} + if resp.Data != nil { + out.PlatformMessageID = deref(resp.Data.MessageId) + } + return out +} + +// classifyFeishuTransport: a dial failure means nothing was sent (the SDK +// already retried once); any other transport error may follow a delivered +// request and is unknown — safe to retry with the same uuid. +func classifyFeishuTransport(err error) SendResult { + var dial *larkcore.DialFailedError + if errors.As(err, &dial) { + return SendResult{Outcome: NotDelivered, Code: "dial_failed", Retryable: true, SafeDetail: "无法连接飞书开放平台"} + } + return SendResult{Outcome: Unknown, Code: "transport", SafeDetail: "飞书回复请求未返回结果"} +} + +// feishuRetryable lists platform codes that mean "not sent, try again": +// request rate limits and an invalid/expired tenant access token (the next +// attempt fetches a new token). Unknown codes stop automatic retries. +var feishuRetryable = map[int]bool{ + 99991400: true, // request frequency limit + 99991661: true, // missing access token + 99991663: true, // invalid tenant access token + 99991664: true, // invalid app access token + 99991668: true, // invalid user access token +} + +func classifyFeishuCode(code int) SendResult { + detail := "飞书拒绝了这次回复(错误码 " + strconv.Itoa(code) + ")" + if feishuRetryable[code] { + return SendResult{Outcome: NotDelivered, Code: strconv.Itoa(code), Retryable: true, SafeDetail: detail} + } + if strings.HasPrefix(strconv.Itoa(code), "5") && code < 600 { + return SendResult{Outcome: Unknown, Code: strconv.Itoa(code), SafeDetail: detail} + } + return SendResult{Outcome: Rejected, Code: strconv.Itoa(code), SafeDetail: detail} +} diff --git a/internal/botinteraction/channel_feishu_test.go b/internal/botinteraction/channel_feishu_test.go new file mode 100644 index 000000000..c07ea90f8 --- /dev/null +++ b/internal/botinteraction/channel_feishu_test.go @@ -0,0 +1,352 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" + + larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" + larkws "github.com/larksuite/oapi-sdk-go/v3/ws" + + "github.com/gorilla/websocket" +) + +// feishuEventJSON is an im.message.receive_v1 event in the official shape. +func feishuEventJSON(eventID, appID, tenant, messageID, chatType, msgType, content string) string { + b, _ := json.Marshal(map[string]any{ + "schema": "2.0", + "header": map[string]any{"event_id": eventID, "event_type": "im.message.receive_v1", "app_id": appID, "tenant_key": tenant, "create_time": "1794970000000", "token": ""}, + "event": map[string]any{ + "sender": map[string]any{"sender_id": map[string]any{"open_id": "ou_sender", "union_id": "on_x", "user_id": "u_x"}, "sender_type": "user", "tenant_key": tenant}, + "message": map[string]any{"message_id": messageID, "chat_id": "oc_chat", "chat_type": chatType, "message_type": msgType, "content": content, "create_time": "1794970000000"}, + }, + }) + return string(b) +} + +func TestFeishuEventMapping(t *testing.T) { + var ev larkim.P2MessageReceiveV1 + if err := json.Unmarshal([]byte(feishuEventJSON("evt-1", "cli_app", "tenant-1", "om_1", "p2p", "text", `{"text":"项目状态"}`)), &ev); err != nil { + t.Fatal(err) + } + got, ok := feishuEvent(&ev) + if !ok || got.Channel != ChannelFeishu || got.AppID != "cli_app" || got.TenantKey != "tenant-1" || got.EventID != "evt-1" || got.MessageID != "om_1" || got.SenderID != "ou_sender" || got.ChatType != "single" || got.MsgType != "text" || got.Text != "项目状态" || got.Route.MessageID != "om_1" { + t.Fatalf("event = %+v", got) + } + json.Unmarshal([]byte(feishuEventJSON("evt-2", "cli_app", "tenant-1", "om_2", "group", "image", `{"image_key":"img"}`)), &ev) + got, _ = feishuEvent(&ev) + if got.ChatType != "group" || got.MsgType != "image" || got.Text != "" { + t.Fatalf("group image = %+v", got) + } + if _, ok := feishuEvent(&larkim.P2MessageReceiveV1{}); ok { + t.Fatal("empty event accepted") + } +} + +// fakeFeishuOpenAPI serves the token, tenant and reply endpoints. +type fakeFeishuOpenAPI struct { + mu sync.Mutex + tokenCode int + replyCode int + replyHTTP int + uuids []string + texts []string +} + +func (f *fakeFeishuOpenAPI) handler(t *testing.T) http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("/open-apis/auth/v3/tenant_access_token/internal", func(w http.ResponseWriter, r *http.Request) { + f.mu.Lock() + code := f.tokenCode + f.mu.Unlock() + w.Header().Set("Content-Type", "application/json") + if code != 0 { + json.NewEncoder(w).Encode(map[string]any{"code": code, "msg": "invalid app secret"}) + return + } + json.NewEncoder(w).Encode(map[string]any{"code": 0, "msg": "ok", "tenant_access_token": "t-token", "expire": 7200}) + }) + mux.HandleFunc("/open-apis/tenant/v2/tenant/query", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"tenant": map[string]any{"name": "测试企业", "tenant_key": "tenant-1"}}}) + }) + mux.HandleFunc("/open-apis/im/v1/messages/", func(w http.ResponseWriter, r *http.Request) { + if !strings.HasSuffix(r.URL.Path, "/reply") || r.Header.Get("Authorization") != "Bearer t-token" { + t.Errorf("unexpected reply request %s %s", r.URL.Path, r.Header.Get("Authorization")) + } + var body struct { + MsgType string `json:"msg_type"` + Content string `json:"content"` + UUID string `json:"uuid"` + } + json.NewDecoder(r.Body).Decode(&body) + var content struct { + Text string `json:"text"` + } + json.Unmarshal([]byte(body.Content), &content) + f.mu.Lock() + f.uuids = append(f.uuids, body.UUID) + f.texts = append(f.texts, content.Text) + code, status := f.replyCode, f.replyHTTP + f.mu.Unlock() + w.Header().Set("Content-Type", "application/json") + if status != 0 { + w.WriteHeader(status) + } + if code != 0 { + json.NewEncoder(w).Encode(map[string]any{"code": code, "msg": "fail"}) + return + } + json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"message_id": "om_reply"}}) + }) + return mux +} + +func TestFeishuVerifyAndReplyClassification(t *testing.T) { + api := &fakeFeishuOpenAPI{} + srv := httptest.NewServer(api.handler(t)) + defer srv.Close() + ch := newFeishuChannel() + ch.base = srv.URL + bot := domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelFeishu, BotID: "cli_app"} + verified, err := ch.Verify(context.Background(), bot, Credential{Secret: "s"}) + if err != nil || verified.TenantKey != "tenant-1" || verified.TenantName != "测试企业" { + t.Fatalf("verify = %+v %v", verified, err) + } + api.tokenCode = 10014 + if _, err := ch.Verify(context.Background(), bot, Credential{Secret: "bad"}); err == nil || !strings.Contains(err.Error(), "10014") { + t.Fatalf("bad secret = %v", err) + } + api.tokenCode = 0 + + conn := &feishuConn{channel: ch, bot: bot, api: ch.client(bot, Credential{Secret: "s"})} + msg := OutboundMessage{InteractionID: "i", Text: "答复", SendKey: "send-key-for-test", Route: ReplyRoute{MessageID: "om_1"}} + if got := conn.Reply(context.Background(), msg); got.Outcome != Delivered || got.PlatformMessageID != "om_reply" { + t.Fatalf("reply = %+v", got) + } + if api.uuids[0] != msg.SendKey || api.texts[0] != "答复" { + t.Fatalf("uuid/text = %v %v", api.uuids, api.texts) + } + for code, want := range map[int]SendOutcome{99991400: NotDelivered, 99991663: NotDelivered, 230002: Rejected, 230011: Rejected} { + api.replyCode = code + if got := conn.Reply(context.Background(), msg); got.Outcome != want || !strings.Contains(got.SafeDetail, "错误码") { + t.Errorf("code %d = %+v", code, got) + } + } + api.replyCode, api.replyHTTP = 0, 0 + if got := conn.Reply(context.Background(), OutboundMessage{Text: "x"}); got.Outcome != Rejected || got.Code != "route_missing" { + t.Fatalf("no route = %+v", got) + } + // A transport failure after the request may have been written is unknown. + srv.Close() + if got := conn.Reply(context.Background(), msg); got.Outcome != NotDelivered && got.Outcome != Unknown { + t.Fatalf("closed server = %+v", got) + } + if got := classifyFeishuTransport(errors.New("read: connection reset")); got.Outcome != Unknown { + t.Fatalf("reset = %+v", got) + } +} + +// fakeFeishuWS implements the long-connection bootstrap and data frames. +type fakeFeishuWS struct { + t *testing.T + srv *httptest.Server + conns chan *websocket.Conn + acks chan int + closed chan struct{} +} + +func newFakeFeishuWS(t *testing.T, api http.Handler) *fakeFeishuWS { + f := &fakeFeishuWS{t: t, conns: make(chan *websocket.Conn, 4), acks: make(chan int, 16), closed: make(chan struct{}, 4)} + mux := http.NewServeMux() + mux.Handle("/open-apis/", api) + mux.HandleFunc("/callback/ws/endpoint", func(w http.ResponseWriter, r *http.Request) { + url := "ws" + strings.TrimPrefix(f.srv.URL, "http") + "/ws?device_id=d1&service_id=1" + json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"URL": url, "ClientConfig": map[string]any{"PingInterval": 120, "ReconnectCount": -1, "ReconnectInterval": 120, "ReconnectNonce": 1}}}) + }) + var upgrader websocket.Upgrader + mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { + c, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + f.conns <- c + for { + _, raw, err := c.ReadMessage() + if err != nil { + f.closed <- struct{}{} + return + } + var frame larkws.Frame + if frame.Unmarshal(raw) != nil || frame.Method != int32(larkws.FrameTypeData) { + continue + } + var resp struct { + Code int `json:"code"` + } + json.Unmarshal(frame.Payload, &resp) + f.acks <- resp.Code + } + }) + f.srv = httptest.NewServer(mux) + return f +} + +func (f *fakeFeishuWS) push(c *websocket.Conn, payload string) { + frame := larkws.Frame{SeqID: 1, LogID: 1, Service: 1, Method: int32(larkws.FrameTypeData), Headers: []larkws.Header{{Key: "type", Value: "event"}, {Key: "message_id", Value: "frame-1"}, {Key: "sum", Value: "1"}, {Key: "seq", Value: "0"}, {Key: "trace_id", Value: "trace"}}, Payload: []byte(payload)} + raw, _ := frame.Marshal() + if err := c.WriteMessage(websocket.BinaryMessage, raw); err != nil { + f.t.Fatal(err) + } +} + +func (f *fakeFeishuWS) ack(t *testing.T) int { + t.Helper() + select { + case code := <-f.acks: + return code + case <-time.After(5 * time.Second): + t.Fatal("no ack") + return 0 + } +} + +// The SDK acknowledges a stored event with 200 and a storage failure with +// 500 (Feishu then redelivers); Close stops the client. +func TestFeishuConnectAcknowledgesByPersistenceOutcome(t *testing.T) { + api := &fakeFeishuOpenAPI{} + ws := newFakeFeishuWS(t, api.handler(t)) + defer ws.srv.Close() + ch := newFeishuChannel() + ch.base = ws.srv.URL + bot := domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelFeishu, BotID: "cli_app"} + var mu sync.Mutex + var got []InboundEvent + fail := false + sink := func(ctx context.Context, ev InboundEvent) (InboundAck, error) { + mu.Lock() + defer mu.Unlock() + if _, ok := ctx.Deadline(); !ok { + t.Error("sink without an ack deadline") + } + got = append(got, ev) + if fail { + return InboundAck{}, errors.New("database unavailable") + } + return InboundAck{Accepted: true}, nil + } + conn, err := ch.Connect(context.Background(), bot, Credential{Secret: "s"}, sink) + if err != nil { + t.Fatal(err) + } + server := <-ws.conns + ws.push(server, feishuEventJSON("evt-1", "cli_app", "tenant-1", "om_1", "p2p", "text", `{"text":"你好"}`)) + if code := ws.ack(t); code != http.StatusOK { + t.Fatalf("stored event ack = %d", code) + } + mu.Lock() + fail = true + mu.Unlock() + ws.push(server, feishuEventJSON("evt-2", "cli_app", "tenant-1", "om_2", "p2p", "text", `{"text":"再来"}`)) + if code := ws.ack(t); code != http.StatusInternalServerError { + t.Fatalf("failed event ack = %d", code) + } + mu.Lock() + if len(got) != 2 || got[0].MessageID != "om_1" || got[0].Text != "你好" || got[0].TenantKey != "tenant-1" { + t.Fatalf("events = %+v", got) + } + mu.Unlock() + conn.Close() + select { + case <-conn.Done(): + case <-time.After(5 * time.Second): + t.Fatal("Close did not stop the client") + } + select { + case <-ws.closed: + case <-time.After(5 * time.Second): + t.Fatal("server connection not closed") + } +} + +func TestFeishuConnectFailsWhenBootstrapRejected(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(map[string]any{"code": 403, "msg": "forbidden"}) + })) + defer srv.Close() + ch := newFeishuChannel() + ch.base = srv.URL + ch.readyWait = 3 * time.Second + start := time.Now() + if _, err := ch.Connect(context.Background(), domain.NotificationBot{BotID: "cli_app"}, Credential{Secret: "s"}, nil); err == nil { + t.Fatal("connected with a rejected bootstrap") + } + if time.Since(start) > 3*time.Second { + t.Fatal("rejected bootstrap waited for the ready timeout") + } +} + +// A sink that blocks (slow database) is answered with a failure at the ack +// deadline so Feishu redelivers, instead of holding the ack past its window. +func TestFeishuAckBoundedWhenPersistenceBlocks(t *testing.T) { + ch := newFeishuChannel() + ch.ackTimeout = 200 * time.Millisecond + release := make(chan struct{}) + defer close(release) + start := time.Now() + err := ch.acknowledge(context.Background(), InboundEvent{MessageID: "om_1"}, func(context.Context, InboundEvent) (InboundAck, error) { + <-release + return InboundAck{Accepted: true}, nil + }) + if !errors.Is(err, errFeishuAckTimeout) || time.Since(start) > time.Second { + t.Fatalf("blocked sink = %v after %s", err, time.Since(start)) + } + if err := ch.acknowledge(context.Background(), InboundEvent{}, func(context.Context, InboundEvent) (InboundAck, error) { return InboundAck{}, nil }); err != nil { + t.Fatal(err) + } +} + +func TestFeishuGroupMentionMapping(t *testing.T) { + raw := func(mentions []map[string]any) string { + b, _ := json.Marshal(map[string]any{ + "schema": "2.0", + "header": map[string]any{"event_id": "evt-g", "event_type": "im.message.receive_v1", "app_id": "cli_app", "tenant_key": "tk"}, + "event": map[string]any{ + "sender": map[string]any{"sender_id": map[string]any{"open_id": "ou_sender"}, "sender_type": "user"}, + "message": map[string]any{"message_id": "om_g", "chat_id": "oc_group", "chat_type": "group", "message_type": "text", "content": `{"text":"@_user_1 帮我建个需求 @_user_2"}`, "mentions": mentions}, + }, + }) + return string(b) + } + parse := func(mentions []map[string]any) InboundEvent { + var ev larkim.P2MessageReceiveV1 + if err := json.Unmarshal([]byte(raw(mentions)), &ev); err != nil { + t.Fatal(err) + } + got, ok := feishuEvent(&ev) + if !ok { + t.Fatal("not mapped") + } + return got + } + got := parse([]map[string]any{{"key": "@_user_1", "name": "LoopForge", "mentioned_type": "bot"}, {"key": "@_user_2", "name": "张三", "mentioned_type": "user"}}) + if !got.Mentioned || got.Text != "帮我建个需求" || got.ChatType != "group" || got.ChatID != "oc_group" { + t.Fatalf("bot mention = %+v", got) + } + if got := parse([]map[string]any{{"key": "@_user_1", "name": "张三", "mentioned_type": "user"}}); got.Mentioned { + t.Fatal("user mention counted as @ Bot") + } + if got := parse(nil); got.Mentioned { + t.Fatal("no mention counted") + } + if got := parse([]map[string]any{{"key": "@_user_1", "name": "LoopForge"}}); !got.Mentioned { + t.Fatal("mention without type not counted") + } +} diff --git a/internal/botinteraction/channel_test.go b/internal/botinteraction/channel_test.go new file mode 100644 index 000000000..a40df0fc4 --- /dev/null +++ b/internal/botinteraction/channel_test.go @@ -0,0 +1,599 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "errors" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" + + "github.com/gorilla/websocket" +) + +// fakeChannel is a controllable in-memory channel: tests push inbound events +// through the sink captured at Connect, decide each reply outcome, and drop +// connections to exercise reconnects. +type fakeChannel struct { + mu sync.Mutex + conns chan *fakeConn + outcome func(OutboundMessage) SendResult +} + +func newFakeChannel() *fakeChannel { + return &fakeChannel{conns: make(chan *fakeConn, 8), outcome: func(OutboundMessage) SendResult { return SendResult{Outcome: Delivered} }} +} +func (f *fakeChannel) Kind() string { return "fake" } +func (f *fakeChannel) Verify(context.Context, domain.NotificationBot, Credential) (Verification, error) { + return Verification{}, nil +} +func (f *fakeChannel) Connect(ctx context.Context, bot domain.NotificationBot, cred Credential, sink InboundSink) (Conn, error) { + c := &fakeConn{channel: f, sink: sink, ctx: ctx, secret: cred.Secret, done: make(chan struct{}), replies: make(chan OutboundMessage, 16)} + f.conns <- c + return c, nil +} +func (f *fakeChannel) setOutcome(fn func(OutboundMessage) SendResult) { + f.mu.Lock() + f.outcome = fn + f.mu.Unlock() +} + +type fakeConn struct { + channel *fakeChannel + sink InboundSink + ctx context.Context + secret string + done chan struct{} + once sync.Once + replies chan OutboundMessage +} + +func (c *fakeConn) Done() <-chan struct{} { return c.done } +func (c *fakeConn) Close() error { c.once.Do(func() { close(c.done) }); return nil } +func (c *fakeConn) Reply(ctx context.Context, msg OutboundMessage) SendResult { + c.replies <- msg + c.channel.mu.Lock() + fn := c.channel.outcome + c.channel.mu.Unlock() + return fn(msg) +} +func (c *fakeConn) push(t *testing.T, ev InboundEvent) InboundAck { + t.Helper() + ack, err := c.sink(c.ctx, ev) + if err != nil { + t.Fatalf("sink: %v", err) + } + return ack +} + +func waitConn(t *testing.T, ch *fakeChannel) *fakeConn { + t.Helper() + select { + case c := <-ch.conns: + return c + case <-time.After(10 * time.Second): + t.Fatal("no connection") + return nil + } +} +func waitReplyMsg(t *testing.T, c *fakeConn) OutboundMessage { + t.Helper() + select { + case m := <-c.replies: + return m + case <-time.After(6 * time.Second): + t.Fatal("no reply") + return OutboundMessage{} + } +} + +func TestFakeChannelDrivesServiceLifecycle(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + bot := domain.NotificationBot{ID: "bot-fake", BotID: "app-fake", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + if err = store.SaveNotificationBot(bot, "fake-secret"); err != nil { + t.Fatal(err) + } + service := New(store) + fake := newFakeChannel() + service.RegisterChannel(fake) + service.ChannelKind = func(domain.NotificationBot) string { return "fake" } + var mu sync.Mutex + generated := map[string]int{} + service.Generate = func(ctx context.Context, b domain.NotificationBot, c domain.BotConversation, h []domain.BotInteraction, item domain.BotInteraction) (string, error) { + mu.Lock() + generated[item.Content]++ + mu.Unlock() + return "答复:" + item.Content, nil + } + ctx, cancel := context.WithCancel(context.Background()) + service.Start(ctx) + defer func() { cancel(); <-service.Done }() + + event := func(id, text string) InboundEvent { + return InboundEvent{Channel: "fake", AppID: "app-fake", EventID: "evt-" + id, MessageID: id, SenderID: "u1", ChatID: "u1", ChatType: "single", MsgType: "text", Text: text} + } + status := func(msgID string) string { + item, err := store.BotInteraction(repo.BotMessageID(bot.BotID, msgID)) + if err != nil { + return "" + } + return item.Status + } + + conn := waitConn(t, fake) + if conn.secret != "fake-secret" { + t.Fatalf("credential not passed to Connect: %q", conn.secret) + } + // Delivered → replied; the duplicate business message is acknowledged + // without a second interaction or model call. + if ack := conn.push(t, event("m1", "第一句")); !ack.Accepted || ack.InteractionID != repo.BotMessageID(bot.BotID, "m1") { + t.Fatalf("first ack = %+v", ack) + } + if msg := waitReplyMsg(t, conn); msg.Text != "答复:第一句" || msg.ChatID != "u1" || msg.ChatType != "single" { + t.Fatalf("reply = %+v", msg) + } + eventually(t, func() bool { return status("m1") == "replied" }) + dup := event("m1", "第一句") + dup.EventID = "evt-redelivered" + if ack := conn.push(t, dup); ack.Accepted { + t.Fatal("redelivered business message accepted twice") + } + // An event for another application never reaches the queue. + foreign := event("m-foreign", "越权") + foreign.AppID = "other-app" + if ack := conn.push(t, foreign); ack.Accepted || ack.Ignored != "app_mismatch" { + t.Fatalf("foreign ack = %+v", ack) + } + + // Rejected → reply_failed with the safe detail. + fake.setOutcome(func(OutboundMessage) SendResult { + return SendResult{Outcome: Rejected, Code: "403", SafeDetail: "平台拒绝"} + }) + conn.push(t, event("m2", "被拒")) + waitReplyMsg(t, conn) + eventually(t, func() bool { return status("m2") == "reply_failed" }) + if item, _ := store.BotInteraction(repo.BotMessageID(bot.BotID, "m2")); item.LastError != "平台拒绝" { + t.Fatalf("reject detail = %q", item.LastError) + } + + // Unknown → reply_unknown on a non-idempotent channel: no automatic + // resend and the connection stays up. A manual resend starts a new + // generation with a new key and sends only the saved reply. + sends := 0 + var keys []string + fake.setOutcome(func(msg OutboundMessage) SendResult { + sends++ + keys = append(keys, msg.SendKey) + if sends == 1 { + return SendResult{Outcome: Unknown, Code: "receipt_missing", SafeDetail: "回执超时"} + } + return SendResult{Outcome: Delivered, PlatformMessageID: "pm-1"} + }) + conn.push(t, event("m3", "未知")) + waitReplyMsg(t, conn) + eventually(t, func() bool { return status("m3") == "reply_unknown" }) + select { + case <-conn.Done(): + t.Fatal("unknown outcome dropped the connection") + case msg := <-conn.replies: + t.Fatalf("unknown outcome was resent automatically: %+v", msg) + case <-time.After(time.Second): + } + resent, err := store.ResendBotInteraction(repo.BotMessageID(bot.BotID, "m3"), 7, "action-1", "用户未收到") + if err != nil || resent.DeliveryGeneration != 2 { + t.Fatalf("resend = %+v %v", resent, err) + } + if again, err := store.ResendBotInteraction(resent.ID, 7, "action-1", "重复点击"); err != nil || again.DeliveryGeneration != 2 { + t.Fatalf("duplicate action = %+v %v", again, err) + } + if msg := waitReplyMsg(t, conn); msg.Text != "答复:未知" { + t.Fatalf("resent reply = %+v", msg) + } + eventually(t, func() bool { return status("m3") == "replied" }) + if len(keys) != 2 || keys[0] == "" || keys[0] == keys[1] { + t.Fatalf("send keys = %v", keys) + } + if item, _ := store.BotInteraction(resent.ID); item.PlatformMessageID != "pm-1" || item.ResentBy != 7 { + t.Fatalf("receipt = %+v", item) + } + fake.setOutcome(func(OutboundMessage) SendResult { return SendResult{Outcome: Delivered} }) + + // A dropped connection reconnects and keeps processing. + conn.Close() + third := waitConn(t, fake) + third.push(t, event("m4", "重连后")) + waitReplyMsg(t, third) + eventually(t, func() bool { return status("m4") == "replied" }) + + mu.Lock() + defer mu.Unlock() + for text, n := range generated { + if n != 1 { + t.Fatalf("model ran %d times for %q", n, text) + } + } + if len(generated) != 4 { + t.Fatalf("generated = %v", generated) + } +} + +func TestUnregisteredChannelReportsNotReady(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + bot := domain.NotificationBot{ID: "bot-feishu", BotID: "cli_app", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + if err = store.SaveNotificationBot(bot, "secret"); err != nil { + t.Fatal(err) + } + service := New(store) + service.ChannelKind = func(domain.NotificationBot) string { return "unregistered-kind" } + if _, err := service.Channel(bot); !errors.Is(err, ErrChannelNotReady) { + t.Fatalf("channel = %v", err) + } + ctx, cancel := context.WithCancel(context.Background()) + service.Start(ctx) + defer func() { cancel(); <-service.Done }() + eventually(t, func() bool { + state, err := store.BotConnectionState(bot.BotID) + return err == nil && state.State == "error" && state.LastError == "该渠道尚未接入" + }) +} + +func TestInteractionFromEventValidation(t *testing.T) { + bot := domain.NotificationBot{ID: "cfg", BotID: "app"} + valid := InboundEvent{AppID: "app", MessageID: "m", SenderID: "u", ChatID: "u", ChatType: "single", Text: "hi", Route: ReplyRoute{RequestID: "req"}} + item, reason := interactionFromEvent(bot, valid) + if reason != "" || item.ID != repo.BotMessageID("app", "m") || item.BotID != "cfg" || item.RequestID != "req" || item.Content != "hi" { + t.Fatalf("valid = %+v %q", item, reason) + } + long := strings.Repeat("x", 192) + cases := map[string]func(*InboundEvent){ + "app_mismatch": func(e *InboundEvent) { e.AppID = "other" }, + "missing_identity": func(e *InboundEvent) { e.SenderID = "" }, + "invalid_chat": func(e *InboundEvent) { e.ChatType = "channel" }, + "identity_too_long": func(e *InboundEvent) { e.ChatID = long }, + "empty": func(e *InboundEvent) { e.Text = " " }, + } + for want, mutate := range cases { + ev := valid + mutate(&ev) + if _, reason := interactionFromEvent(bot, ev); reason != want { + t.Errorf("%s: reason = %q", want, reason) + } + } + action := valid + action.Text, action.Action, action.DeliveryID = "", "approve", "d" + if _, reason := interactionFromEvent(bot, action); reason != "" { + t.Fatalf("card action without text rejected: %q", reason) + } +} + +func TestWeComChannelInjectedVerifyAndPush(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + service := New(store) + bot := domain.NotificationBot{ID: "cfg", BotID: "wecom-bot"} + ch, err := service.Channel(bot) + if err != nil || ch.Kind() != ChannelWeCom { + t.Fatalf("default channel = %v %v", ch, err) + } + if _, err := ch.Verify(context.Background(), bot, Credential{Secret: "s"}); !errors.Is(err, ErrChannelNotReady) { + t.Fatalf("unconfigured verify = %v", err) + } + if got := ch.(PushSender).Push(context.Background(), bot, Credential{Secret: "s"}, OutboundMessage{ChatID: "c", Text: "t"}); got.Outcome != NotDelivered { + t.Fatalf("unconfigured push = %+v", got) + } + var verified, pushed []string + service.ConfigureWeCom(func(ctx context.Context, botID, secret string) error { + verified = append(verified, botID, secret) + return nil + }, func(ctx context.Context, botID, secret, target, content string) SendResult { + pushed = append(pushed, botID, secret, target, content) + return SendResult{Outcome: Delivered} + }) + ch, _ = service.Channel(bot) + if _, err := ch.Verify(context.Background(), bot, Credential{Secret: "s"}); err != nil || strings.Join(verified, ",") != "wecom-bot,s" { + t.Fatalf("verify = %v %v", err, verified) + } + if got := ch.(PushSender).Push(context.Background(), bot, Credential{Secret: "s"}, OutboundMessage{ChatID: "chat", Text: "hello"}); got.Outcome != Delivered || strings.Join(pushed, ",") != "wecom-bot,s,chat,hello" { + t.Fatalf("push = %+v %v", got, pushed) + } +} + +func TestClassifyWeComSend(t *testing.T) { + cases := []struct { + err error + want SendOutcome + code string + }{ + {nil, Delivered, ""}, + {&ReplyError{Code: 40008}, Rejected, "40008"}, + {errorsStreamUnavailable, NotDelivered, "stream_unavailable"}, + {&sendError{phase: phaseBeforeWrite, err: context.Canceled}, NotDelivered, "not_written"}, + {&sendError{phase: phaseWrite, err: errors.New("企业微信回复连接中断")}, Unknown, "write_interrupted"}, + {&sendError{phase: phaseAwaitAck, err: errors.New("企业微信回复回执超时")}, Unknown, "receipt_missing"}, + {errors.New("other"), Unknown, "unclassified"}, + } + for _, tc := range cases { + got := classifyWeComSend(tc.err) + if got.Outcome != tc.want || got.Code != tc.code { + t.Errorf("%v: got %+v", tc.err, got) + } + if tc.err != nil && got.SafeDetail != tc.err.Error() { + t.Errorf("%v: detail %q", tc.err, got.SafeDetail) + } + } +} + +// TestSocketRequestPhases checks the evidence each request failure carries: +// nothing written, written without receipt, or an explicit platform code. +func TestSocketRequestPhases(t *testing.T) { + var upgrader websocket.Upgrader + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ws, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + defer ws.Close() + for { + var frame Frame + if ws.ReadJSON(&frame) != nil { + return + } + switch frame.Cmd { + case "silent": // written but never acknowledged + continue + case "drop": + return + case "reject": + _ = ws.WriteJSON(map[string]any{"headers": frame.Headers, "errcode": 93000}) + default: + _ = ws.WriteJSON(map[string]any{"headers": frame.Headers, "errcode": 0}) + } + } + })) + defer server.Close() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + sock, err := dial(ctx, "ws"+strings.TrimPrefix(server.URL, "http"), "bot", "secret", func(*socket, Frame) {}) + if err != nil { + t.Fatal(err) + } + defer sock.close() + + if got := classifyWeComSend(sock.request(ctx, "ok", "", nil)); got.Outcome != Delivered { + t.Fatalf("ok = %+v", got) + } + if got := classifyWeComSend(sock.request(ctx, "reject", "", nil)); got.Outcome != Rejected || got.Code != "93000" { + t.Fatalf("reject = %+v", got) + } + canceled, stop := context.WithCancel(ctx) + stop() + err = sock.request(canceled, "ok", "", nil) + if got := classifyWeComSend(err); got.Outcome != NotDelivered || !errors.Is(err, context.Canceled) { + t.Fatalf("canceled before write = %+v %v", got, err) + } + waiting, stopWaiting := context.WithTimeout(ctx, 200*time.Millisecond) + defer stopWaiting() + err = sock.request(waiting, "silent", "", nil) + if got := classifyWeComSend(err); got.Outcome != Unknown || got.Code != "receipt_missing" || !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("written without receipt = %+v %v", got, err) + } + if got := classifyWeComSend(sock.request(ctx, "drop", "", nil)); got.Outcome != Unknown { + t.Fatalf("connection closed after write = %+v", got) + } +} + +// kindChannel registers a fake adapter under a real channel kind. +type kindChannel struct { + *fakeChannel + kind string +} + +func (k kindChannel) Kind() string { return k.kind } + +// The Feishu private-chat loop through the common service: tenant discovery +// answers with a fixed notice until an administrator locks the scope; then +// text is processed once per business message, other tenants are dropped, +// group chats are ignored and non-text gets a notice without the model. +func TestFeishuPrivateChatLoopWithTenantConfirmation(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + bot := domain.NotificationBot{ID: "cfg-fs", Channel: domain.BotChannelFeishu, BotID: "cli_app", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + saved, err := store.SaveNotificationBotChecked(bot, "secret", nil) + if err != nil { + t.Fatal(err) + } + service := New(store) + fake := newFakeChannel() + service.RegisterChannel(kindChannel{fake, ChannelFeishu}) + var mu sync.Mutex + generated := map[string]int{} + service.Generate = func(ctx context.Context, b domain.NotificationBot, c domain.BotConversation, h []domain.BotInteraction, item domain.BotInteraction) (string, error) { + mu.Lock() + generated[item.Content]++ + mu.Unlock() + return "答复:" + item.Content, nil + } + ctx, cancel := context.WithCancel(context.Background()) + service.Start(ctx) + defer func() { cancel(); <-service.Done }() + conn := waitConn(t, fake) + event := func(eventID, msgID, tenant, chatType, msgType, text string) InboundEvent { + return InboundEvent{Channel: ChannelFeishu, AppID: "cli_app", TenantKey: tenant, EventID: eventID, MessageID: msgID, SenderID: "ou_1", ChatID: "oc_1", ChatType: chatType, MsgType: msgType, Text: text, Route: ReplyRoute{MessageID: msgID}} + } + + // Before confirmation: candidate recorded, fixed notice, no model call. + conn.push(t, event("e1", "om_1", "tenant-1", "single", "text", "项目状态")) + if msg := waitReplyMsg(t, conn); msg.Text != noticeTenantPending || msg.Route.MessageID != "om_1" { + t.Fatalf("pending notice = %+v", msg) + } + pending, _ := store.NotificationBot(bot.ID) + if pending.TenantCandidate == nil || pending.TenantCandidate.TenantKey != "tenant-1" || pending.ConfigVersion != saved.ConfigVersion { + t.Fatalf("candidate = %+v", pending) + } + if _, err := store.ConfirmBotTenant(bot.ID, "tenant-1", pending.ConfigVersion, 1); err != nil { + t.Fatal(err) + } + // Locking does not reconnect. + select { + case <-conn.Done(): + t.Fatal("tenant lock reconnected") + case <-time.After(500 * time.Millisecond): + } + + conn.push(t, event("e2", "om_2", "tenant-1", "single", "text", "项目状态")) + if msg := waitReplyMsg(t, conn); msg.Text != "答复:项目状态" || msg.SendKey == "" || msg.Route.MessageID != "om_2" { + t.Fatalf("reply = %+v", msg) + } + // Redelivery with a new event ID is the same business message. + if ack := conn.push(t, event("e2-retry", "om_2", "tenant-1", "single", "text", "项目状态")); ack.Accepted { + t.Fatal("redelivered message accepted twice") + } + // Another enterprise, a group message without @: dropped without a reply. + if ack := conn.push(t, event("e3", "om_3", "tenant-x", "single", "text", "越权")); ack.Accepted || ack.Ignored != "tenant_mismatch" { + t.Fatalf("foreign tenant = %+v", ack) + } + if ack := conn.push(t, event("e4", "om_4", "tenant-1", "group", "text", "群聊")); ack.Accepted || ack.Ignored != "group_not_mentioned" { + t.Fatalf("group = %+v", ack) + } + // A group message that @ the Bot is answered in the group, in a + // conversation of its own. + mentioned := event("e4b", "om_4b", "tenant-1", "group", "text", "群里问进度") + mentioned.ChatID, mentioned.Mentioned = "oc_group", true + if ack := conn.push(t, mentioned); !ack.Accepted { + t.Fatalf("mentioned group = %+v", ack) + } + if msg := waitReplyMsg(t, conn); msg.Text != "答复:群里问进度" || msg.ChatID != "oc_group" || msg.ChatType != "group" || msg.Route.MessageID != "om_4b" { + t.Fatalf("group reply = %+v", msg) + } + // Non-text: a notice, the content never reaches the model. + conn.push(t, event("e5", "om_5", "tenant-1", "single", "image", "")) + if msg := waitReplyMsg(t, conn); msg.Text != noticeUnsupported { + t.Fatalf("image notice = %+v", msg) + } + select { + case msg := <-conn.replies: + t.Fatalf("unexpected reply %+v", msg) + case <-time.After(500 * time.Millisecond): + } + mu.Lock() + defer mu.Unlock() + if len(generated) != 2 || generated["项目状态"] != 1 || generated["群里问进度"] != 1 { + t.Fatalf("model calls = %v", generated) + } + // Records use the configuration as identity scope. + if item, err := store.BotInteraction(repo.BotMessageID(bot.ID, "om_2")); err != nil || item.Status != domain.BotReplied || item.ProjectID != "p" || item.PlatformEventID != "e2" { + t.Fatalf("record = %+v %v", item, err) + } + state, _ := store.BotConnectionState(saved.LeaseKey()) + if state.IgnoredGroupMessages != 1 || !strings.HasPrefix(saved.LeaseKey(), "feishu:") { + t.Fatalf("connection = %+v key=%s", state, saved.LeaseKey()) + } +} + +// The DingTalk private-chat loop through the common service: the CorpId the +// administrator entered locks on the first matching event (and that message +// is processed), the session webhook is stored sealed and handed back to the +// adapter, an expired route ends as route_expired with the reply kept, and +// an unknown outcome is not resent automatically. +func TestDingTalkPrivateChatLoop(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + bot := domain.NotificationBot{ID: "cfg-dt", Channel: domain.BotChannelDingTalk, BotID: "ding-app", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true, TenantExpected: "ding-corp", TenantExpectedBy: 7} + saved, err := store.SaveNotificationBotChecked(bot, "secret", nil) + if err != nil || saved.TenantState != domain.BotTenantAwaiting { + t.Fatal(saved, err) + } + service := New(store) + fake := newFakeChannel() + service.RegisterChannel(kindChannel{fake, ChannelDingTalk}) + var mu sync.Mutex + generated := map[string]int{} + service.Generate = func(ctx context.Context, b domain.NotificationBot, c domain.BotConversation, h []domain.BotInteraction, item domain.BotInteraction) (string, error) { + mu.Lock() + generated[item.Content]++ + mu.Unlock() + return "答复:" + item.Content, nil + } + ctx, cancel := context.WithCancel(context.Background()) + service.Start(ctx) + defer func() { cancel(); <-service.Done }() + conn := waitConn(t, fake) + webhook := "https://oapi.dingtalk.com/robot/sendBySession?session=s-" + event := func(msgID, corp, text string, expires time.Time) InboundEvent { + return InboundEvent{Channel: ChannelDingTalk, AppID: "ding-app", TenantKey: corp, EventID: "f-" + msgID, MessageID: msgID, SenderID: "staff-1", ChatID: "cid-1", ChatType: "single", MsgType: "text", Text: text, Route: ReplyRoute{Webhook: webhook + msgID, ExpiresAt: expires}} + } + later := time.Now().Add(time.Hour) + + conn.push(t, event("m1", "ding-corp", "项目状态", later)) + msg := waitReplyMsg(t, conn) + if msg.Text != "答复:项目状态" || msg.Route.Webhook != webhook+"m1" { + t.Fatalf("first message = %+v", msg) + } + locked, _ := store.NotificationBot(bot.ID) + if locked.TenantState != domain.BotTenantLocked || locked.TenantKey != "ding-corp" || locked.TenantConfirmedBy != 7 || locked.ConfigVersion != saved.ConfigVersion+1 { + t.Fatalf("auto lock = %+v", locked) + } + // Sealed at rest, bound to the interaction; cleared once replied. + id1 := repo.BotMessageID(bot.ID, "m1") + eventually(t, func() bool { it, _ := store.BotInteraction(id1); return it.Status == domain.BotReplied }) + if it, _ := store.BotInteraction(id1); it.ReplyRoute != "" || it.RouteExpiresAtUS == 0 { + t.Fatalf("replied route = %q %d", it.ReplyRoute, it.RouteExpiresAtUS) + } + // Another enterprise after locking: dropped. + if ack := conn.push(t, event("m2", "other-corp", "越权", later)); ack.Accepted || ack.Ignored != "tenant_mismatch" { + t.Fatalf("foreign corp = %+v", ack) + } + // Unknown outcome: reply_unknown, no automatic resend. + fake.setOutcome(func(OutboundMessage) SendResult { + return SendResult{Outcome: Unknown, Code: "transport", SafeDetail: "未返回结果"} + }) + conn.push(t, event("m3", "ding-corp", "未知", later)) + waitReplyMsg(t, conn) + id3 := repo.BotMessageID(bot.ID, "m3") + eventually(t, func() bool { it, _ := store.BotInteraction(id3); return it.Status == domain.BotReplyUnknown }) + select { + case m := <-conn.replies: + t.Fatalf("unknown outcome resent: %+v", m) + case <-time.After(time.Second): + } + if it, _ := store.BotInteraction(id3); !strings.HasPrefix(it.ReplyRoute, "lf1:") || strings.Contains(it.ReplyRoute, "session") { + t.Fatalf("pending route must stay sealed for a manual resend: %q", it.ReplyRoute) + } + // Route already expiring: the reply is kept, the status is route_expired. + fake.setOutcome(func(OutboundMessage) SendResult { return SendResult{Outcome: Delivered} }) + conn.push(t, event("m4", "ding-corp", "过期", time.Now().Add(5*time.Second))) + id4 := repo.BotMessageID(bot.ID, "m4") + eventually(t, func() bool { it, _ := store.BotInteraction(id4); return it.Status == domain.BotReplyFailed }) + if it, _ := store.BotInteraction(id4); it.TerminalReason != "route_expired" || it.Reply != "答复:过期" || it.ReplyRoute != "" { + t.Fatalf("expired = %+v", it) + } + if _, err := store.ResendBotInteraction(id4, 7, "k-1", "x"); !errors.Is(err, repo.ErrBotRouteExpired) { + t.Fatalf("resend expired = %v", err) + } + select { + case m := <-conn.replies: + t.Fatalf("expired route was sent: %+v", m) + default: + } +} diff --git a/internal/botinteraction/channel_wecom.go b/internal/botinteraction/channel_wecom.go new file mode 100644 index 000000000..bd65b8d87 --- /dev/null +++ b/internal/botinteraction/channel_wecom.go @@ -0,0 +1,240 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "encoding/json" + "errors" + "log" + "strconv" + "strings" + "sync" +) + +// WeComVerifyFunc checks a WeCom Bot ID and secret through the signed +// bootstrap API, without opening a long connection. +type WeComVerifyFunc func(ctx context.Context, botID, secret string) error + +// WeComPushFunc sends a proactive message through the official wecom-cli path +// and classifies the outcome. +type WeComPushFunc func(ctx context.Context, botID, secret, target, content string) SendResult + +// wecomChannel wraps the existing WeCom AI Bot long-connection protocol. +type wecomChannel struct { + svc *Service + verify WeComVerifyFunc + push WeComPushFunc +} + +func (w *wecomChannel) Kind() string { return ChannelWeCom } + +func (w *wecomChannel) Verify(ctx context.Context, bot domain.NotificationBot, cred Credential) (Verification, error) { + if w.verify == nil { + return Verification{}, ErrChannelNotReady + } + return Verification{}, w.verify(ctx, bot.BotID, cred.Secret) +} + +func (w *wecomChannel) Push(ctx context.Context, bot domain.NotificationBot, cred Credential, msg OutboundMessage) SendResult { + if w.push == nil { + return SendResult{Outcome: NotDelivered, Code: ErrChannelNotReady.Error(), SafeDetail: "企业微信主动发送未配置"} + } + return w.push(ctx, bot.BotID, cred.Secret, msg.ChatID, msg.Text) +} + +func (w *wecomChannel) Connect(ctx context.Context, bot domain.NotificationBot, cred Credential, sink InboundSink) (Conn, error) { + ws, err := dial(ctx, w.svc.URL, bot.BotID, cred.Secret, func(received *socket, f Frame) { + w.svc.wecomFrame(ctx, bot, f, received, sink) + }) + if err != nil { + return nil, err + } + return &wecomConn{ws: ws}, nil +} + +// wecomConn adapts a socket to Conn and exposes progress and card capabilities. +type wecomConn struct { + ws *socket + closeOnce sync.Once +} + +func (c *wecomConn) Done() <-chan struct{} { return c.ws.done } +func (c *wecomConn) Close() error { + c.closeOnce.Do(c.ws.close) + return nil +} + +// Reply finishes the stream of a user message. The proactive send fallback +// runs at most once and only when the stream provably did not go out: stream +// unavailable, request never written, or an explicit platform rejection of +// the stream (for example an expired callback). A stream that may have been +// delivered is reported as unknown instead of being sent twice. Card actions +// send directly. +func (c *wecomConn) Reply(ctx context.Context, msg OutboundMessage) SendResult { + defer c.ws.streams.Delete(msg.InteractionID) + if msg.Action != "" { + return classifyWeComSend(c.ws.send(ctx, msg.ChatID, msg.Text)) + } + streamed := classifyWeComSend(c.ws.stream(ctx, domain.BotInteraction{ID: msg.InteractionID, RequestID: msg.Route.RequestID}, msg.Text, true)) + if streamed.Outcome == Delivered || streamed.Outcome == Unknown { + return streamed + } + return classifyWeComSend(c.ws.send(ctx, msg.ChatID, msg.Text)) +} + +func (c *wecomConn) progressTurn(ctx context.Context, item domain.BotInteraction) (context.Context, func()) { + return c.ws.progressTurn(ctx, item) +} + +func (c *wecomConn) sendCard(ctx context.Context, chatID string, card map[string]any) SendResult { + return classifyWeComSend(c.ws.request(ctx, "aibot_send_msg", "", map[string]any{"chatid": chatID, "msgtype": "template_card", "template_card": card})) +} + +func (c *wecomConn) updateActionCard(ctx context.Context, item domain.BotInteraction, result string, finish bool) error { + return c.ws.updateActionCard(ctx, item, result, finish) +} + +// classifyWeComSend maps socket errors to delivery outcomes by how far the +// request got. Business error codes are explicit platform rejections. +func classifyWeComSend(err error) SendResult { + if err == nil { + return SendResult{Outcome: Delivered} + } + var business *ReplyError + if errors.As(err, &business) { + return SendResult{Outcome: Rejected, Code: strconv.Itoa(business.Code), SafeDetail: business.Error()} + } + if errors.Is(err, errorsStreamUnavailable) { + return SendResult{Outcome: NotDelivered, Code: "stream_unavailable", Retryable: true, SafeDetail: err.Error()} + } + var sendErr *sendError + if errors.As(err, &sendErr) { + switch sendErr.phase { + case phaseBeforeWrite: + return SendResult{Outcome: NotDelivered, Code: "not_written", Retryable: true, SafeDetail: err.Error()} + case phaseWrite: + return SendResult{Outcome: Unknown, Code: "write_interrupted", SafeDetail: err.Error()} + default: + return SendResult{Outcome: Unknown, Code: "receipt_missing", SafeDetail: err.Error()} + } + } + return SendResult{Outcome: Unknown, Code: "unclassified", SafeDetail: err.Error()} +} + +// wecomEvent converts one WeCom callback frame into a channel-neutral event. +// It applies the WeCom-specific input rules that existed before the channel +// contract: voice transcription, unsupported types and length limits. +func wecomEvent(bot domain.NotificationBot, f Frame) (InboundEvent, bool) { + var incoming Incoming + if json.Unmarshal(f.Body, &incoming) != nil || incoming.BotID != bot.BotID || incoming.MessageID == "" || incoming.From.UserID == "" { + return InboundEvent{}, false + } + // Live WeCom callbacks wrap card parameters in template_card_event; + // also accept the flat structure used by older official SDK type definitions. + if incoming.Event.TemplateCardEvent.Key != "" { + incoming.Event.Key = incoming.Event.TemplateCardEvent.Key + } + if incoming.Event.TemplateCardEvent.TaskID != "" { + incoming.Event.TaskID = incoming.Event.TemplateCardEvent.TaskID + } + chat := incoming.ChatID + kind := incoming.ChatType + if kind == "" && f.Cmd == "aibot_event_callback" { + kind = "single" + if incoming.ChatID != "" { + kind = "group" + } + } + if kind == "single" { + chat = incoming.From.UserID + } + text := incoming.Text.Content + if incoming.Type == "voice" { + text = incoming.Voice.Content + } + action, delivery := "", "" + if f.Cmd == "aibot_event_callback" { + if incoming.Event.Type != "template_card_event" { + return InboundEvent{}, false + } + action, delivery = incoming.Event.Key, incoming.Event.TaskID + if prefix, _, found := strings.Cut(delivery, "_"); found { + delivery = prefix + } + if action == "" || action == "handled" || delivery == "" { + return InboundEvent{}, false + } + } else if incoming.Type != "text" && incoming.Type != "voice" { + text = unsupportedMessageText + } + return InboundEvent{ + Channel: ChannelWeCom, + AppID: incoming.BotID, + MessageID: incoming.MessageID, + SenderID: incoming.From.UserID, + ChatID: chat, + ChatType: kind, + MsgType: incoming.Type, + Text: domain.NotificationText(text, 16000), + Quote: domain.NotificationText(incoming.Quote.Text.Content, 8000), + Action: action, + DeliveryID: delivery, + CardTaskID: incoming.Event.TaskID, + Route: ReplyRoute{RequestID: f.Headers.RequestID}, + }, true +} + +// normalizeIncoming is kept for the WeCom frame tests: it returns the +// interaction the common inbound path would persist for this frame. +func normalizeIncoming(bot domain.NotificationBot, f Frame) (domain.BotInteraction, string, string, bool) { + ev, ok := wecomEvent(bot, f) + if !ok { + return domain.BotInteraction{}, "", "", false + } + item, reason := interactionFromEvent(bot, ev) + if reason != "" { + return domain.BotInteraction{}, "", "", false + } + return item, ev.ChatType, ev.ChatID, true +} + +// receive handles one WeCom frame with the service's own inbound sink. +func (s *Service) receive(ctx context.Context, bot domain.NotificationBot, f Frame, ws *socket) { + s.wecomFrame(ctx, bot, f, ws, func(ctx context.Context, ev InboundEvent) (InboundAck, error) { + return s.accept(ctx, bot, ev) + }) +} + +func (s *Service) wecomFrame(ctx context.Context, bot domain.NotificationBot, f Frame, ws *socket, sink InboundSink) { + ev, ok := wecomEvent(bot, f) + if !ok { + if f.Cmd == "aibot_event_callback" { + var event Incoming + _ = json.Unmarshal(f.Body, &event) + log.Printf("wecom ignored event type=%s bot_matches=%t message_id=%t sender=%t chat_type=%s action=%s task_id=%t", event.Event.Type, event.BotID == bot.BotID, event.MessageID != "", event.From.UserID != "", event.ChatType, event.Event.Key, event.Event.TaskID != "") + } + return + } + // Single-chat callbacks identify the actor by internal userid, while CLI + // discovery targets use openid. Resolve only an exact, server-issued card + // task to its frozen recipient; group callbacks still require matching chatid. + if ev.Action != "" && ev.ChatType == "single" { + delivery, e := s.Store.NotificationDelivery(ev.DeliveryID) + card, cardErr := s.Store.BotInteraction(repo.BotMessageID(bot.ID, "notification:"+ev.DeliveryID)) + if e == nil && cardErr == nil && delivery.BotID == bot.ID && delivery.ProjectID == bot.ProjectID && delivery.TargetType == "single" && card.CardTaskID == ev.CardTaskID { + ev.ChatID = delivery.TargetID + } + } + ack, err := sink(ctx, ev) + if err != nil || !ack.Accepted { + return + } + // Card operations are direct engine controls. Reply with the actual result + // within the callback window rather than consuming it with a receipt-only update. + if ev.Action == "" { + item := domain.BotInteraction{ID: ack.InteractionID, RequestID: ev.Route.RequestID} + state := ws.streamState(item.ID) + go func() { _ = ws.streamWithState(ctx, item, "已收到,正在处理…", false, state) }() + } +} diff --git a/internal/botinteraction/delivery.go b/internal/botinteraction/delivery.go new file mode 100644 index 000000000..0e16a3098 --- /dev/null +++ b/internal/botinteraction/delivery.go @@ -0,0 +1,97 @@ +package botinteraction + +import ( + "agentflow/internal/credcrypt" + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "errors" + "log" + "time" +) + +// actionSystemNotice marks an interaction whose saved content is replied +// verbatim without the model (for example the tenant confirmation notice). +const actionSystemNotice = "system_notice" + +// sendTimeout bounds one platform call; it matches repo.BotSendTimeoutUS so +// the idempotency window check in BeginBotSend covers the whole call. +const sendTimeout = 10 * time.Second + +// sendPolicy describes channel delivery guarantees. Feishu de-duplicates a +// reply carrying the same uuid for one hour, so unknown outcomes are retried +// with the persisted key. WeCom and DingTalk have no verified idempotency: +// unknown outcomes stop at reply_unknown for manual review. +func sendPolicy(bot domain.NotificationBot) repo.BotSendPolicy { + return repo.BotSendPolicy{Idempotent: bot.ChannelKind() == domain.BotChannelFeishu} +} + +// replyRoute rebuilds the routing data persisted with the interaction. A +// sealed route is the DingTalk session webhook; plain values are Feishu +// message IDs. A route that cannot be opened yields an empty route, which +// the adapter rejects as route_missing. +func (s *Service) replyRoute(item domain.BotInteraction) ReplyRoute { + route := ReplyRoute{RequestID: item.RequestID} + if item.ReplyRoute != "" { + if credcrypt.IsSealed(item.ReplyRoute) { + if webhook, err := s.Store.OpenBotRoute(item.BotID, item.ID, item.ReplyRoute); err == nil { + route.Webhook = webhook + } else { + log.Printf("bot reply route unavailable id=%s err=%v", item.ID, err) + } + } else { + route.MessageID = item.ReplyRoute + } + } + if item.RouteExpiresAtUS > 0 { + route.ExpiresAt = time.UnixMicro(item.RouteExpiresAtUS).UTC() + } + return route +} + +func outcomeFor(result SendResult) repo.BotSendOutcome { + out := repo.BotSendOutcome{PlatformMessageID: result.PlatformMessageID, Code: result.Code, Detail: result.SafeDetail} + switch result.Outcome { + case Delivered: + out.Outcome = "delivered" + case NotDelivered: + out.Outcome = "not_delivered" + if !result.Retryable { + out.Outcome = "rejected" + } + case Rejected: + out.Outcome = "rejected" + default: + out.Outcome = "unknown" + } + if out.Detail == "" { + out.Detail = "回复发送结果:" + out.Outcome + } + return out +} + +// deliver runs one send attempt of a ready interaction: mark it sending in a +// committed transaction, call the platform outside any transaction with a +// bounded timeout, then record the classified outcome. A lease too short to +// cover the call skips this tick; a state change (expired route, closed +// idempotency window) is already recorded by BeginBotSend. +func (s *Service) deliver(ctx context.Context, id string, lease repo.BotLease, policy repo.BotSendPolicy, send func(context.Context, domain.BotInteraction) SendResult) error { + sending, err := s.Store.BeginBotSend(id, lease, policy) + switch { + case errors.Is(err, repo.ErrBotStateConflict), errors.Is(err, repo.ErrBotLeaseLow): + return nil + case err != nil: + return err + } + callCtx, cancel := context.WithTimeout(ctx, sendTimeout) + result := send(callCtx, sending) + cancel() + if _, err := s.Store.FinishBotSend(id, lease, policy, outcomeFor(result)); err != nil { + // The platform result arrived after this worker lost ownership; the + // new owner resolves the item from its sending state. Keep an audit + // line with the outcome only. + log.Printf("bot late send receipt id=%s outcome=%s code=%s err=%v", id, result.Outcome, result.Code, err) + return err + } + return nil +} diff --git a/internal/botinteraction/dingtalk_fault_mysql_test.go b/internal/botinteraction/dingtalk_fault_mysql_test.go new file mode 100644 index 000000000..507d63e5d --- /dev/null +++ b/internal/botinteraction/dingtalk_fault_mysql_test.go @@ -0,0 +1,402 @@ +package botinteraction + +// Fault tests of the DingTalk private-chat loop against a real MySQL and real +// receiver processes running the production Service and Stream adapter. The +// parent plays the DingTalk gateway (connection open, Stream frames, session +// webhook) and the model; receivers are child processes killed with SIGKILL. +// +// AGENTFLOW_BOT_FAULT_TEST_MYSQL_DSN='root:root@tcp(127.0.0.1:33384)/bot_fault_dt?parseTime=true' \ +// go test -run TestDingTalkFaultMySQL -v ./internal/botinteraction +// +// Takeover waits for the 45 s connection lease; the test runs ~2 minutes. + +import ( + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "os" + "os/exec" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + "github.com/gorilla/websocket" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" +) + +// TestDingTalkFaultChild is the receiver process; a no-op unless started by +// TestDingTalkFaultMySQL. +func TestDingTalkFaultChild(t *testing.T) { + if os.Getenv(faultChildEnv) != "dingtalk" { + t.Skip("receiver child process only") + } + store, err := repo.Open(os.Getenv(faultDSNEnv)) + if err != nil { + t.Fatal(err) + } + store.SetCredentialKeyring(faultKeyring(t)) + platform := os.Getenv(faultURLEnv) + svc := New(store) + ch := newDingTalkChannel() + ch.api = platform + ch.allowInsecureStream = true + // Short keepalive so a silent gateway is detected within seconds. + ch.keepAlive, ch.pongWait = time.Second, time.Second + host := strings.TrimPrefix(platform, "http://") + ch.webhookAllowed = func(u *url.URL) bool { return u.Host == host && u.Path == "/robot/sendBySession" } + svc.RegisterChannel(ch) + svc.Generate = func(ctx context.Context, _ domain.NotificationBot, _ domain.BotConversation, _ []domain.BotInteraction, item domain.BotInteraction) (string, error) { + req, _ := http.NewRequestWithContext(ctx, http.MethodPost, platform+"/model", strings.NewReader(item.Content)) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + return string(body), nil + } + svc.Start(context.Background()) + select {} +} + +type dtGateway struct { + t *testing.T + srv *httptest.Server + + mu sync.Mutex + conn *websocket.Conn + conns int + silent bool // stop answering WebSocket pings + acks map[string]int + sent []string // session webhook posts: "|" + hang map[string]time.Duration + model map[string]int + block map[string]chan struct{} + writeMu sync.Mutex +} + +func newDTGateway(t *testing.T) *dtGateway { + g := &dtGateway{t: t, acks: map[string]int{}, hang: map[string]time.Duration{}, model: map[string]int{}, block: map[string]chan struct{}{}} + mux := http.NewServeMux() + mux.HandleFunc("/v1.0/gateway/connections/open", func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(payload.ConnectionEndpointResponse{Endpoint: "ws" + strings.TrimPrefix(g.srv.URL, "http") + "/stream", Ticket: "t"}) + }) + var upgrader websocket.Upgrader + mux.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) { + c, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + g.mu.Lock() + if g.silent { + c.SetPingHandler(func(string) error { return nil }) + } + g.conn = c + g.conns++ + g.mu.Unlock() + for { + _, raw, err := c.ReadMessage() + if err != nil { + return + } + var resp payload.DataFrameResponse + if json.Unmarshal(raw, &resp) == nil { + g.mu.Lock() + g.acks[resp.Headers["messageId"]] = resp.Code + g.mu.Unlock() + } + } + }) + mux.HandleFunc("/robot/sendBySession", func(w http.ResponseWriter, r *http.Request) { + var body struct { + Text struct { + Content string `json:"content"` + } `json:"text"` + } + json.NewDecoder(r.Body).Decode(&body) + session := r.URL.Query().Get("session") + g.mu.Lock() + g.sent = append(g.sent, session+"|"+body.Text.Content) + hold := g.hang[session] + delete(g.hang, session) + g.mu.Unlock() + if hold > 0 { + select { + case <-time.After(hold): + case <-r.Context().Done(): + } + return + } + json.NewEncoder(w).Encode(map[string]any{"errcode": 0, "errmsg": "ok"}) + }) + mux.HandleFunc("/model", func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + text := string(b) + g.mu.Lock() + g.model[text]++ + gate := g.block[text] + g.mu.Unlock() + if gate != nil { + select { + case <-gate: + case <-r.Context().Done(): + return + } + } + fmt.Fprint(w, "答复:"+text) + }) + g.srv = httptest.NewServer(mux) + return g +} + +func (g *dtGateway) push(frameID, msgID, text string, expires time.Time) { + g.t.Helper() + g.mu.Lock() + c := g.conn + g.mu.Unlock() + if c == nil { + g.t.Fatal("no stream connection") + } + data, _ := json.Marshal(map[string]any{ + "conversationId": "cid-1", "chatbotCorpId": "ding-corp", "senderCorpId": "ding-corp", "msgId": msgID, + "senderStaffId": "staff-1", "conversationType": "1", "msgtype": "text", "text": map[string]string{"content": text}, + "sessionWebhook": g.srv.URL + "/robot/sendBySession?session=" + msgID, "sessionWebhookExpiredTime": expires.UnixMilli(), + }) + frame := payload.DataFrame{SpecVersion: "1.0", Type: "CALLBACK", Headers: payload.DataFrameHeader{"topic": payload.BotMessageCallbackTopic, "messageId": frameID, "contentType": "application/json"}, Data: string(data)} + g.writeMu.Lock() + err := c.WriteMessage(websocket.TextMessage, frame.Encode()) + g.writeMu.Unlock() + if err != nil { + g.t.Fatal(err) + } +} + +func (g *dtGateway) ack(frameID string) (int, bool) { + g.mu.Lock() + defer g.mu.Unlock() + code, ok := g.acks[frameID] + return code, ok +} + +func (g *dtGateway) sentFor(msgID string) []string { + g.mu.Lock() + defer g.mu.Unlock() + var out []string + for _, s := range g.sent { + if strings.HasPrefix(s, msgID+"|") { + out = append(out, s) + } + } + return out +} + +func (g *dtGateway) modelRuns(text string) int { + g.mu.Lock() + defer g.mu.Unlock() + return g.model[text] +} + +func (g *dtGateway) gate(text string) chan struct{} { + g.mu.Lock() + defer g.mu.Unlock() + c := make(chan struct{}) + g.block[text] = c + return c +} + +func (g *dtGateway) connCount() int { + g.mu.Lock() + defer g.mu.Unlock() + return g.conns +} + +func startDingTalkReceiver(t *testing.T, dsn, platform, name string) *faultReceiver { + t.Helper() + logPath := filepath.Join(t.TempDir(), name+".log") + out, err := os.Create(logPath) + if err != nil { + t.Fatal(err) + } + cmd := exec.Command(os.Args[0], "-test.run=^TestDingTalkFaultChild$", "-test.timeout=20m") + cmd.Env = append(os.Environ(), faultChildEnv+"=dingtalk", faultDSNEnv+"="+dsn, faultURLEnv+"="+platform) + cmd.Stdout, cmd.Stderr = out, out + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + r := &faultReceiver{cmd: cmd, log: logPath} + t.Cleanup(func() { + r.kill() + if t.Failed() { + raw, _ := os.ReadFile(logPath) + lines := strings.Split(string(raw), "\n") + if len(lines) > 30 { + lines = lines[len(lines)-30:] + } + t.Logf("%s log:\n%s", name, strings.Join(lines, "\n")) + } + }) + return r +} + +func TestDingTalkFaultMySQL(t *testing.T) { + dsn := os.Getenv(faultDSNEnv) + if dsn == "" { + t.Skip("set " + faultDSNEnv + " to a dedicated, empty MySQL database") + } + store, err := repo.Open(dsn) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { store.Close() }) + store.SetCredentialKeyring(faultKeyring(t)) + bot := domain.NotificationBot{ID: "cfg-dt-fault", Channel: domain.BotChannelDingTalk, BotID: "ding-fault", Name: "fault", ProjectID: "p-fault", OwnerID: 1, Enabled: true, InteractionEnabled: true, TenantExpected: "ding-corp", TenantExpectedBy: 1} + saved, err := store.SaveNotificationBotChecked(bot, "client-secret", nil) + if err != nil { + t.Fatal(err) + } + g := newDTGateway(t) + defer g.srv.Close() + item := func(msgID string) domain.BotInteraction { + it, _ := store.BotInteraction(repo.BotMessageID(bot.ID, msgID)) + return it + } + status := func(msgID, want string) func() bool { + return func() bool { return item(msgID).Status == want } + } + report := func(name, msgID, text string) { + it := item(msgID) + t.Logf("%s: status=%s reason=%s generation=%d attempts=%d model_runs=%d webhook_posts=%d", name, it.Status, it.TerminalReason, it.DeliveryGeneration, it.SendAttempts, g.modelRuns(text), len(g.sentFor(msgID))) + } + later := func() time.Time { return time.Now().Add(time.Hour) } + + a := startDingTalkReceiver(t, dsn, g.srv.URL, "receiver-a") + waitFor(t, "receiver A stream", 30*time.Second, func() bool { return g.connCount() == 1 }) + + // D1 duplicate delivery while the model runs; the first message also + // locks the enterprise entered by the administrator. + g1 := g.gate("D1") + g.push("d1-a", "md1", "D1", later()) + waitFor(t, "D1 model start", 10*time.Second, func() bool { return g.modelRuns("D1") == 1 }) + g.push("d1-b", "md1", "D1", later()) + g.push("d1-c", "md1", "D1", later()) + waitFor(t, "D1 acks", 10*time.Second, func() bool { _, ok := g.ack("d1-c"); return ok }) + close(g1) + waitFor(t, "D1 replied", 20*time.Second, status("md1", domain.BotReplied)) + for _, f := range []string{"d1-a", "d1-b", "d1-c"} { + if code, _ := g.ack(f); code != 200 { + t.Fatalf("D1 ack %s = %d", f, code) + } + } + if g.modelRuns("D1") != 1 || len(g.sentFor("md1")) != 1 { + t.Fatalf("D1: model=%d posts=%v", g.modelRuns("D1"), g.sentFor("md1")) + } + if b, _ := store.NotificationBot(bot.ID); b.TenantState != domain.BotTenantLocked || b.ConfigVersion != saved.ConfigVersion+1 { + t.Fatalf("D1 tenant = %+v", b) + } + report("D1 duplicate delivery", "md1", "D1") + + // D2 storage blocked beyond the ack window. + db, _ := store.DB.DB() + lockConn, err := db.Conn(context.Background()) + if err != nil { + t.Fatal(err) + } + if _, err := lockConn.ExecContext(context.Background(), "FLUSH TABLES WITH READ LOCK"); err != nil { + t.Fatal(err) + } + start := time.Now() + g.push("d2-a", "md2", "D2", later()) + waitFor(t, "D2 ack under lock", 5*time.Second, func() bool { _, ok := g.ack("d2-a"); return ok }) + ackAfter := time.Since(start) + time.Sleep(4*time.Second - ackAfter) + lockConn.ExecContext(context.Background(), "UNLOCK TABLES") + lockConn.Close() + if code, _ := g.ack("d2-a"); code != 500 || ackAfter >= 3*time.Second { + t.Fatalf("D2 ack = %d after %s", code, ackAfter) + } + g.push("d2-b", "md2", "D2", later()) + waitFor(t, "D2 replied", 20*time.Second, status("md2", domain.BotReplied)) + if g.modelRuns("D2") != 1 || len(g.sentFor("md2")) != 1 { + t.Fatalf("D2: model=%d posts=%v", g.modelRuns("D2"), g.sentFor("md2")) + } + t.Logf("D2 blocked storage: first ack 500 after %s", ackAfter.Round(time.Millisecond)) + report("D2 blocked storage", "md2", "D2") + + // D3 gateway goes silent (no pongs) during a model turn, then sends a + // late frame on the dead connection — the SDK crash scenario. The + // receiver must survive, reconnect and still deliver the reply. + g3 := g.gate("D3") + g.push("d3-a", "md3", "D3", later()) + waitFor(t, "D3 model start", 10*time.Second, func() bool { return g.modelRuns("D3") == 1 }) + g.mu.Lock() + g.silent = true + old := g.conn + g.mu.Unlock() + old.SetPingHandler(func(string) error { return nil }) + time.Sleep(3 * time.Second) + g.writeMu.Lock() + _ = old.WriteMessage(websocket.TextMessage, []byte(`{"type":"SYSTEM","headers":{"topic":"ping","messageId":"late"}}`)) + g.writeMu.Unlock() + g.mu.Lock() + g.silent = false + g.mu.Unlock() + close(g3) + waitFor(t, "D3 reconnect", 40*time.Second, func() bool { return g.connCount() >= 2 }) + waitFor(t, "D3 replied", 20*time.Second, status("md3", domain.BotReplied)) + if a.cmd.ProcessState != nil { + t.Fatalf("receiver exited: %v", a.cmd.ProcessState) + } + g.push("d3-b", "md3b", "D3b", later()) + waitFor(t, "D3b replied after reconnect", 20*time.Second, status("md3b", domain.BotReplied)) + if g.modelRuns("D3") != 1 || len(g.sentFor("md3")) != 1 { + t.Fatalf("D3: model=%d posts=%v", g.modelRuns("D3"), g.sentFor("md3")) + } + report("D3 silent gateway + late frame", "md3", "D3") + + // D4 receiver crash with the reply in flight: DingTalk has no + // idempotency key, so the next receiver records reply_unknown instead of + // sending again; a manual resend sends the saved reply once more. + g.mu.Lock() + g.hang["md4"] = time.Minute + g.mu.Unlock() + conns := g.connCount() + g.push("d4-a", "md4", "D4", later()) + waitFor(t, "D4 webhook post", 20*time.Second, func() bool { return len(g.sentFor("md4")) == 1 }) + a.kill() + b := startDingTalkReceiver(t, dsn, g.srv.URL, "receiver-b") + waitFor(t, "D4 reply_unknown after takeover", 120*time.Second, status("md4", domain.BotReplyUnknown)) + waitFor(t, "receiver B stream", 30*time.Second, func() bool { return g.connCount() > conns }) + time.Sleep(2 * time.Second) + if posts := g.sentFor("md4"); len(posts) != 1 { + t.Fatalf("D4 resent automatically: %v", posts) + } + if _, err := store.ResendBotInteraction(repo.BotMessageID(bot.ID, "md4"), 1, "d4-manual", "核对后补发"); err != nil { + t.Fatal(err) + } + waitFor(t, "D4 manual resend", 20*time.Second, status("md4", domain.BotReplied)) + if posts := g.sentFor("md4"); len(posts) != 2 || item("md4").DeliveryGeneration != 2 { + t.Fatalf("D4 manual resend: posts=%v gen=%d", posts, item("md4").DeliveryGeneration) + } + report("D4 crash in send + manual resend", "md4", "D4") + + // D5 session webhook expiring before the reply is ready. + g5 := g.gate("D5") + g.push("d5-a", "md5", "D5", time.Now().Add(12*time.Second)) + waitFor(t, "D5 model start", 10*time.Second, func() bool { return g.modelRuns("D5") == 1 }) + time.Sleep(4 * time.Second) + close(g5) + waitFor(t, "D5 route_expired", 20*time.Second, status("md5", domain.BotReplyFailed)) + if it := item("md5"); it.TerminalReason != "route_expired" || it.Reply != "答复:D5" || it.ReplyRoute != "" || len(g.sentFor("md5")) != 0 { + t.Fatalf("D5 = %+v posts=%v", it, g.sentFor("md5")) + } + report("D5 expired session webhook", "md5", "D5") + b.kill() +} diff --git a/internal/botinteraction/feishu_fault_mysql_test.go b/internal/botinteraction/feishu_fault_mysql_test.go new file mode 100644 index 000000000..4622aae54 --- /dev/null +++ b/internal/botinteraction/feishu_fault_mysql_test.go @@ -0,0 +1,514 @@ +package botinteraction + +// Fault tests of the Feishu private-chat loop against a real MySQL and real +// receiver processes. The parent test plays the Feishu open platform (token, +// long-connection bootstrap, protobuf event frames, Message Reply with +// one-hour uuid de-duplication) and the model; each receiver is a child +// process running the production Service and Feishu adapter, killed with +// SIGKILL to simulate crashes. Run with a dedicated, empty database: +// +// AGENTFLOW_BOT_FAULT_TEST_MYSQL_DSN='root:root@tcp(127.0.0.1:33384)/bot_fault' \ +// go test -run TestFeishuFaultMySQL -v ./internal/botinteraction +// +// Takeover waits for the 45 s connection lease, so the test runs ~3 minutes. + +import ( + "agentflow/internal/credcrypt" + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + "github.com/gorilla/websocket" + larkws "github.com/larksuite/oapi-sdk-go/v3/ws" +) + +const ( + faultDSNEnv = "AGENTFLOW_BOT_FAULT_TEST_MYSQL_DSN" + faultChildEnv = "AGENTFLOW_BOT_FAULT_CHILD" + faultURLEnv = "AGENTFLOW_BOT_FAULT_PLATFORM" +) + +func faultKeyring(t testing.TB) *credcrypt.Keyring { + k, err := credcrypt.Parse("fault:" + base64.StdEncoding.EncodeToString([]byte("agentflow-fault-test-key-32bytes"))) + if err != nil { + t.Fatal(err) + } + return k +} + +// TestFeishuFaultChild is the receiver process. It is a no-op unless started +// by TestFeishuFaultMySQL. +func TestFeishuFaultChild(t *testing.T) { + if os.Getenv(faultChildEnv) != "1" { + t.Skip("receiver child process only") + } + store, err := repo.Open(os.Getenv(faultDSNEnv)) + if err != nil { + t.Fatal(err) + } + store.SetCredentialKeyring(faultKeyring(t)) + platform := os.Getenv(faultURLEnv) + svc := New(store) + ch := newFeishuChannel() + ch.base = platform + svc.RegisterChannel(ch) + svc.Generate = func(ctx context.Context, _ domain.NotificationBot, _ domain.BotConversation, _ []domain.BotInteraction, item domain.BotInteraction) (string, error) { + req, _ := http.NewRequestWithContext(ctx, http.MethodPost, platform+"/model", strings.NewReader(item.Content)) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + return string(body), nil + } + svc.Start(context.Background()) + select {} +} + +// faultPlatform is the simulated Feishu open platform and model. +type faultPlatform struct { + t *testing.T + srv *httptest.Server + + mu sync.Mutex + conn *websocket.Conn + conns int + acks []faultAck + frameSeq int + // replies: every Message Reply request; inbox: what the user sees after + // uuid de-duplication. + replies []faultReply + inbox map[string][]string + seen map[string]bool + hang map[string]time.Duration // reply message_id -> hold the response + model map[string]int + block map[string]chan struct{} + // pushTimes records when each frame was written, for ack latency. + pushTimes map[string]time.Time +} + +type faultAck struct { + frame string + code int + latency time.Duration +} + +type faultReply struct { + messageID, uuid, text string +} + +func newFaultPlatform(t *testing.T) *faultPlatform { + p := &faultPlatform{t: t, inbox: map[string][]string{}, seen: map[string]bool{}, hang: map[string]time.Duration{}, model: map[string]int{}, block: map[string]chan struct{}{}} + sent := map[string]time.Time{} + mux := http.NewServeMux() + mux.HandleFunc("/open-apis/auth/v3/tenant_access_token/internal", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"code": 0, "tenant_access_token": "t-token", "expire": 7200}) + }) + mux.HandleFunc("/callback/ws/endpoint", func(w http.ResponseWriter, r *http.Request) { + url := "ws" + strings.TrimPrefix(p.srv.URL, "http") + "/ws?device_id=d1&service_id=1" + json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"URL": url, "ClientConfig": map[string]any{"PingInterval": 120, "ReconnectCount": -1, "ReconnectInterval": 120, "ReconnectNonce": 1}}}) + }) + var upgrader websocket.Upgrader + mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { + c, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + p.mu.Lock() + p.conn = c + p.conns++ + p.mu.Unlock() + for { + _, raw, err := c.ReadMessage() + if err != nil { + return + } + var frame larkws.Frame + if frame.Unmarshal(raw) != nil || frame.Method != int32(larkws.FrameTypeData) { + continue + } + var resp struct { + Code int `json:"code"` + } + json.Unmarshal(frame.Payload, &resp) + id := larkws.Headers(frame.Headers).GetString("message_id") + p.mu.Lock() + p.acks = append(p.acks, faultAck{frame: id, code: resp.Code, latency: time.Since(sent[id])}) + p.mu.Unlock() + } + }) + mux.HandleFunc("/open-apis/im/v1/messages/", func(w http.ResponseWriter, r *http.Request) { + msgID := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/open-apis/im/v1/messages/"), "/reply") + var body struct { + Content string `json:"content"` + UUID string `json:"uuid"` + } + json.NewDecoder(r.Body).Decode(&body) + var content struct { + Text string `json:"text"` + } + json.Unmarshal([]byte(body.Content), &content) + p.mu.Lock() + p.replies = append(p.replies, faultReply{msgID, body.UUID, content.Text}) + // Feishu delivers a reply once per uuid within an hour. + if body.UUID == "" || !p.seen[body.UUID] { + p.seen[body.UUID] = true + p.inbox[msgID] = append(p.inbox[msgID], content.Text) + } + hold := p.hang[msgID] + delete(p.hang, msgID) + p.mu.Unlock() + if hold > 0 { + // The message is delivered; the response is lost. + select { + case <-time.After(hold): + case <-r.Context().Done(): + } + return + } + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"message_id": "om_r_" + msgID}}) + }) + mux.HandleFunc("/model", func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + text := string(b) + p.mu.Lock() + p.model[text]++ + gate := p.block[text] + p.mu.Unlock() + if gate != nil { + select { + case <-gate: + case <-r.Context().Done(): + return + } + } + fmt.Fprint(w, "答复:"+text) + }) + p.srv = httptest.NewServer(mux) + p.pushTimes = sent + return p +} + +func (p *faultPlatform) push(eventID, messageID, text string) string { + p.t.Helper() + p.mu.Lock() + c := p.conn + p.frameSeq++ + frameID := fmt.Sprintf("frame-%d", p.frameSeq) + p.pushTimes[frameID] = time.Now() + p.mu.Unlock() + if c == nil { + p.t.Fatal("no long connection") + } + content, _ := json.Marshal(map[string]string{"text": text}) + payload := feishuEventJSON(eventID, "cli_fault", "tenant-1", messageID, "p2p", "text", string(content)) + frame := larkws.Frame{SeqID: uint64(p.frameSeq), LogID: 1, Service: 1, Method: int32(larkws.FrameTypeData), Headers: []larkws.Header{{Key: "type", Value: "event"}, {Key: "message_id", Value: frameID}, {Key: "sum", Value: "1"}, {Key: "seq", Value: "0"}, {Key: "trace_id", Value: "trace"}}, Payload: []byte(payload)} + raw, _ := frame.Marshal() + p.mu.Lock() + err := c.WriteMessage(websocket.BinaryMessage, raw) + p.mu.Unlock() + if err != nil { + p.t.Fatal(err) + } + return frameID +} + +func (p *faultPlatform) ackOf(frame string) (faultAck, bool) { + p.mu.Lock() + defer p.mu.Unlock() + for _, a := range p.acks { + if a.frame == frame { + return a, true + } + } + return faultAck{}, false +} + +func (p *faultPlatform) gate(text string) chan struct{} { + p.mu.Lock() + defer p.mu.Unlock() + g := make(chan struct{}) + p.block[text] = g + return g +} + +func (p *faultPlatform) snapshot() (inbox map[string][]string, replies []faultReply, model map[string]int, conns int) { + p.mu.Lock() + defer p.mu.Unlock() + inbox = map[string][]string{} + for k, v := range p.inbox { + inbox[k] = append([]string(nil), v...) + } + model = map[string]int{} + for k, v := range p.model { + model[k] = v + } + return inbox, append([]faultReply(nil), p.replies...), model, p.conns +} + +func (p *faultPlatform) repliesFor(msgID string) []faultReply { + _, all, _, _ := p.snapshot() + var out []faultReply + for _, r := range all { + if r.messageID == msgID { + out = append(out, r) + } + } + return out +} + +type faultReceiver struct { + cmd *exec.Cmd + log string +} + +func startFaultReceiver(t *testing.T, dsn, platform, name string) *faultReceiver { + t.Helper() + logPath := filepath.Join(t.TempDir(), name+".log") + out, err := os.Create(logPath) + if err != nil { + t.Fatal(err) + } + cmd := exec.Command(os.Args[0], "-test.run=^TestFeishuFaultChild$", "-test.timeout=20m") + cmd.Env = append(os.Environ(), faultChildEnv+"=1", faultDSNEnv+"="+dsn, faultURLEnv+"="+platform) + cmd.Stdout, cmd.Stderr = out, out + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + r := &faultReceiver{cmd: cmd, log: logPath} + t.Cleanup(func() { + r.kill() + if t.Failed() { + raw, _ := os.ReadFile(logPath) + lines := strings.Split(string(raw), "\n") + if len(lines) > 40 { + lines = lines[len(lines)-40:] + } + t.Logf("%s log:\n%s", name, strings.Join(lines, "\n")) + } + }) + return r +} + +func (r *faultReceiver) kill() { + if r.cmd.ProcessState == nil { + r.cmd.Process.Kill() + r.cmd.Wait() + } +} + +func waitFor(t *testing.T, what string, timeout time.Duration, fn func() bool) { + t.Helper() + deadline := time.Now().Add(timeout) + for time.Now().Before(deadline) { + if fn() { + return + } + time.Sleep(100 * time.Millisecond) + } + t.Fatalf("timed out after %s waiting for %s", timeout, what) +} + +func TestFeishuFaultMySQL(t *testing.T) { + dsn := os.Getenv(faultDSNEnv) + if dsn == "" { + t.Skip("set " + faultDSNEnv + " to a dedicated, empty MySQL database") + } + store, err := repo.Open(dsn) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { store.Close() }) + store.SetCredentialKeyring(faultKeyring(t)) + bot := domain.NotificationBot{ID: "cfg-fault", Channel: domain.BotChannelFeishu, BotID: "cli_fault", Name: "fault", ProjectID: "p-fault", OwnerID: 1, Enabled: true, InteractionEnabled: true} + saved, err := store.SaveNotificationBotChecked(bot, "app-secret", nil) + if err != nil { + t.Fatal(err) + } + if _, err := store.RecordBotTenantCandidate(bot.ID, "tenant-1", "seed", saved.CredentialVersion); err != nil { + t.Fatal(err) + } + if _, err := store.ConfirmBotTenant(bot.ID, "tenant-1", saved.ConfigVersion, 1); err != nil { + t.Fatal(err) + } + p := newFaultPlatform(t) + defer p.srv.Close() + t.Cleanup(func() { + if !t.Failed() { + return + } + var rows []domain.BotInteraction + if err := store.DB.Where("bot_id = ?", bot.ID).Order("created_at").Find(&rows).Error; err != nil { + t.Logf("read interactions: %v", err) + } + for _, r := range rows { + t.Logf("interaction %s status=%s attempts=%d gen=%d reason=%s err=%q reply=%q", r.ID, r.Status, r.SendAttempts, r.DeliveryGeneration, r.TerminalReason, r.LastError, r.Reply) + } + _, replies, model, conns := p.snapshot() + t.Logf("platform replies=%+v model=%v conns=%d acks=%+v", replies, model, conns, p.acks) + }) + item := func(msgID string) domain.BotInteraction { + got, _ := store.BotInteraction(repo.BotMessageID(bot.ID, msgID)) + return got + } + replied := func(msgID string) func() bool { + return func() bool { return item(msgID).Status == domain.BotReplied } + } + report := func(name, msgID, text string) { + inbox, _, model, _ := p.snapshot() + it := item(msgID) + t.Logf("%s: interaction=%s status=%s generation=%d attempts=%d platform_message=%s model_runs=%d reply_requests=%d user_saw=%d", + name, it.ID, it.Status, it.DeliveryGeneration, it.SendAttempts, it.PlatformMessageID, model[text], len(p.repliesFor(msgID)), len(inbox[msgID])) + } + + a := startFaultReceiver(t, dsn, p.srv.URL, "receiver-a") + waitFor(t, "receiver A long connection", 30*time.Second, func() bool { _, _, _, n := p.snapshot(); return n == 1 }) + + // F1 duplicate delivery: the same business message arrives three times + // with different event IDs while the model is still running. + g1 := p.gate("F1") + f1 := []string{p.push("e1", "om_f1", "F1")} + waitFor(t, "F1 model start", 10*time.Second, func() bool { _, _, m, _ := p.snapshot(); return m["F1"] == 1 }) + f1 = append(f1, p.push("e1-redeliver-1", "om_f1", "F1"), p.push("e1-redeliver-2", "om_f1", "F1")) + waitFor(t, "F1 acks", 10*time.Second, func() bool { _, ok := p.ackOf(f1[2]); return ok }) + close(g1) + waitFor(t, "F1 replied", 20*time.Second, replied("om_f1")) + inbox, _, model, _ := p.snapshot() + if model["F1"] != 1 || len(inbox["om_f1"]) != 1 || len(p.repliesFor("om_f1")) != 1 { + t.Fatalf("F1 duplicate delivery: model=%d inbox=%v replies=%v", model["F1"], inbox["om_f1"], p.repliesFor("om_f1")) + } + for _, f := range f1 { + if a, _ := p.ackOf(f); a.code != http.StatusOK { + t.Fatalf("F1 ack %s = %+v", f, a) + } + } + report("F1 duplicate delivery", "om_f1", "F1") + + // F2 slow storage: writes are blocked for 4 s (FLUSH TABLES WITH READ + // LOCK), longer than Feishu's 3 s ack window. The receiver must answer + // within the window with a failure; the redelivery after the database + // recovers is stored once and processed once. + db, _ := store.DB.DB() + lockConn, err := db.Conn(context.Background()) + if err != nil { + t.Fatal(err) + } + if _, err := lockConn.ExecContext(context.Background(), "FLUSH TABLES WITH READ LOCK"); err != nil { + t.Fatal(err) + } + slow := p.push("e2", "om_f2", "F2") + time.Sleep(4 * time.Second) + ack, acked := p.ackOf(slow) + if _, err := lockConn.ExecContext(context.Background(), "UNLOCK TABLES"); err != nil { + t.Fatal(err) + } + lockConn.Close() + if !acked || ack.code == http.StatusOK || ack.latency >= 3*time.Second { + t.Fatalf("F2 ack under blocked storage = %+v acked=%v (want a failure ack within 3s)", ack, acked) + } + redeliver := p.push("e2-redeliver", "om_f2", "F2") + waitFor(t, "F2 redelivery ack", 10*time.Second, func() bool { _, ok := p.ackOf(redeliver); return ok }) + waitFor(t, "F2 replied", 20*time.Second, replied("om_f2")) + inbox, _, model, _ = p.snapshot() + if model["F2"] != 1 || len(inbox["om_f2"]) != 1 { + t.Fatalf("F2 slow storage: model=%d inbox=%v", model["F2"], inbox["om_f2"]) + } + t.Logf("F2 slow storage: first ack code=%d after %s, redelivery ack code=%d", ack.code, ack.latency.Round(time.Millisecond), func() int { a, _ := p.ackOf(redeliver); return a.code }()) + report("F2 slow storage", "om_f2", "F2") + + // F3 long connection drop during a model turn: the reply still goes out + // once over the HTTP API, the receiver reconnects and keeps working. + g3 := p.gate("F3") + p.push("e3", "om_f3", "F3") + waitFor(t, "F3 model start", 10*time.Second, func() bool { _, _, m, _ := p.snapshot(); return m["F3"] == 1 }) + p.mu.Lock() + p.conn.Close() + p.mu.Unlock() + close(g3) + waitFor(t, "F3 replied", 20*time.Second, replied("om_f3")) + waitFor(t, "F3 reconnect", 40*time.Second, func() bool { _, _, _, n := p.snapshot(); return n == 2 }) + p.push("e3b", "om_f3b", "F3b") + waitFor(t, "F3b replied after reconnect", 20*time.Second, replied("om_f3b")) + inbox, _, model, _ = p.snapshot() + if model["F3"] != 1 || len(inbox["om_f3"]) != 1 || len(inbox["om_f3b"]) != 1 { + t.Fatalf("F3 drop: model=%d inbox=%v %v", model["F3"], inbox["om_f3"], inbox["om_f3b"]) + } + report("F3 connection drop", "om_f3", "F3") + + // F4 receiver crash during the model turn: a new receiver takes over + // after the lease expires, reports the interruption instead of running + // the model again, and sends it once. + p.gate("F4") + p.push("e4", "om_f4", "F4") + waitFor(t, "F4 model start", 10*time.Second, func() bool { _, _, m, _ := p.snapshot(); return m["F4"] == 1 }) + a.kill() + crashAt := time.Now() + b := startFaultReceiver(t, dsn, p.srv.URL, "receiver-b") + waitFor(t, "F4 takeover and interruption notice", 90*time.Second, replied("om_f4")) + inbox, _, model, _ = p.snapshot() + if model["F4"] != 1 || len(inbox["om_f4"]) != 1 || !strings.Contains(inbox["om_f4"][0], "中断") { + t.Fatalf("F4 crash in turn: model=%d inbox=%v", model["F4"], inbox["om_f4"]) + } + conn, _ := store.BotConnectionState(saved.LeaseKey()) + t.Logf("F4 crash in turn: takeover after %s, epoch=%d owner_changes=%d", time.Since(crashAt).Round(time.Second), conn.Epoch, conn.OwnerChanges) + report("F4 crash in turn", "om_f4", "F4") + + // F5 receiver crash while the reply request is in flight: the platform + // delivered the message but the response is lost with the process. The + // next receiver retries with the same uuid; the user sees one message. + p.mu.Lock() + p.hang["om_f5"] = time.Minute + p.mu.Unlock() + p.push("e5", "om_f5", "F5") + waitFor(t, "F5 reply request", 20*time.Second, func() bool { return len(p.repliesFor("om_f5")) == 1 }) + if st := item("om_f5").Status; st != domain.BotSending { + t.Fatalf("F5 status before crash = %s", st) + } + b.kill() + startFaultReceiver(t, dsn, p.srv.URL, "receiver-c") + waitFor(t, "F5 recovery", 120*time.Second, replied("om_f5")) + rs := p.repliesFor("om_f5") + inbox, _, _, _ = p.snapshot() + if len(rs) != 2 || rs[0].uuid == "" || rs[0].uuid != rs[1].uuid || len(inbox["om_f5"]) != 1 { + t.Fatalf("F5 crash in send: replies=%+v inbox=%v", rs, inbox["om_f5"]) + } + report("F5 crash in send", "om_f5", "F5") + + // F6 response timeout without a crash (unknown outcome): retried with the + // same uuid after 5 s; one message reaches the user. + p.mu.Lock() + p.hang["om_f6"] = 12 * time.Second + p.mu.Unlock() + p.push("e6", "om_f6", "F6") + waitFor(t, "F6 retry", 40*time.Second, replied("om_f6")) + rs = p.repliesFor("om_f6") + inbox, _, _, _ = p.snapshot() + if len(rs) != 2 || rs[0].uuid != rs[1].uuid || len(inbox["om_f6"]) != 1 { + t.Fatalf("F6 unknown outcome: replies=%+v inbox=%v", rs, inbox["om_f6"]) + } + report("F6 response timeout", "om_f6", "F6") + + var unexpected int64 + store.DB.Model(&domain.BotInteraction{}).Where("bot_id = ? AND status <> ?", bot.ID, domain.BotReplied).Count(&unexpected) + var total int64 + store.DB.Model(&domain.BotInteraction{}).Where("bot_id = ?", bot.ID).Count(&total) + if unexpected != 0 || total != 7 { + t.Fatalf("interactions total=%d not replied=%d", total, unexpected) + } +} diff --git a/internal/botinteraction/inbound.go b/internal/botinteraction/inbound.go new file mode 100644 index 000000000..44c609dd3 --- /dev/null +++ b/internal/botinteraction/inbound.go @@ -0,0 +1,204 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "log" + "strings" + "unicode/utf8" +) + +const ( + unsupportedMessageText = "[不支持的消息类型,请发送文字]" + // New channels accept private text up to this many characters. + channelTextLimit = 8000 + + noticeTenantPending = "机器人正在完成企业确认,请稍后重发消息。" + noticeUnsupported = "目前只支持文字消息,请发送文字。" + noticeTooLong = "消息超过 8000 字,请精简后重新发送。" +) + +// interactionFromEvent validates a channel-neutral event against the Bot +// configuration of the receiving connection and builds the interaction to +// persist. A non-empty reason means the event must be dropped. +func interactionFromEvent(bot domain.NotificationBot, ev InboundEvent) (domain.BotInteraction, string) { + switch { + case ev.AppID != bot.BotID: + return domain.BotInteraction{}, "app_mismatch" + case ev.MessageID == "" || ev.SenderID == "": + return domain.BotInteraction{}, "missing_identity" + case ev.ChatID == "" || (ev.ChatType != "single" && ev.ChatType != "group"): + return domain.BotInteraction{}, "invalid_chat" + case len(ev.ChatID) > 191 || len(ev.SenderID) > 191: + return domain.BotInteraction{}, "identity_too_long" + case strings.TrimSpace(ev.Text) == "" && ev.Action == "" && (ev.Channel == "" || ev.Channel == ChannelWeCom): + // New channels classify empty and non-text messages in accept. + return domain.BotInteraction{}, "empty" + } + return domain.BotInteraction{ + ID: repo.BotMessageID(bot.IdentityScope(), ev.MessageID), + BotID: bot.ID, + ProjectID: bot.ProjectID, + SenderID: ev.SenderID, + RequestID: ev.Route.RequestID, + Content: ev.Text, + Quote: ev.Quote, + Action: ev.Action, + DeliveryID: ev.DeliveryID, + CardTaskID: ev.CardTaskID, + PlatformEventID: truncate(ev.EventID, 191), + MsgType: truncate(ev.MsgType, 32), + ContentLength: utf8.RuneCountInString(ev.Text), + ReplyRoute: ev.Route.MessageID, + }, "" +} + +func truncate(s string, n int) string { + if len(s) <= n { + return s + } + return s[:n] +} + +// accept is the common inbound sink: validate against the current +// configuration, classify and enqueue in one bounded transaction. Duplicates +// return Accepted=false without error so the adapter acknowledges them; +// storage errors are returned for redelivery. +func (s *Service) accept(ctx context.Context, connected domain.NotificationBot, ev InboundEvent) (InboundAck, error) { + // Tenant locking, names and enable flags change without reconnecting. + bot, err := s.Store.NotificationBot(connected.ID) + if err != nil { + return InboundAck{}, err + } + if !bot.Enabled || !bot.InteractionEnabled || bot.BotID != connected.BotID || bot.ChannelKind() != connected.ChannelKind() { + return s.ignore(ev, "bot_inactive"), nil + } + item, reason := interactionFromEvent(bot, ev) + if reason != "" { + return s.ignore(ev, reason), nil + } + if ev.Route.Webhook != "" { + // An authorization-bearing session webhook is stored sealed and + // bound to this interaction; its expiry decides route_expired. + if item.ReplyRoute, err = s.Store.SealBotRoute(bot.ID, item.ID, ev.Route.Webhook); err != nil { + return InboundAck{}, err + } + if !ev.Route.ExpiresAt.IsZero() { + item.RouteExpiresAtUS = ev.Route.ExpiresAt.UnixMicro() + } + } + if bot.ChannelKind() != domain.BotChannelWeCom { + s.Store.NoteBotConnectionEvent(bot.LeaseKey(), ev.ChatType == "group" && !ev.Mentioned && ev.Action == "") + if ev.ChatType == "group" && !ev.Mentioned && ev.Action == "" { + // Group chats: only messages that @ the Bot. + return s.ignore(ev, "group_not_mentioned"), nil + } + notice, gated, err := s.tenantGate(bot, ev) + if err != nil { + return InboundAck{}, err + } + if gated { + if notice == "" { + return s.ignore(ev, "tenant_mismatch"), nil + } + return s.enqueueNotice(item, ev, notice) + } + if ev.Action != "" { + return s.acceptCardAction(bot, item, ev) + } + switch { + case ev.MsgType != "text": + return s.enqueueNotice(item, ev, noticeUnsupported) + case item.ContentLength > channelTextLimit: + return s.enqueueNotice(item, ev, noticeTooLong) + case strings.TrimSpace(ev.Text) == "": + return s.ignore(ev, "empty"), nil + } + } + inserted, err := s.Store.EnqueueBotInteraction(item, ev.ChatType, ev.ChatID) + if err != nil { + log.Printf("%s event enqueue failed action=%s", channelLabel(ev.Channel), item.Action) + return InboundAck{}, err + } + return InboundAck{Accepted: inserted, InteractionID: item.ID}, nil +} + +// acceptCardAction queues a Feishu or DingTalk action card click. Only the +// recipient of the notification is placed in its conversation; anyone else +// lands in a conversation of their own and the action is refused as expired +// (NotificationCardTask checks the conversation against the delivery). The +// result goes out as a private message to the clicker. +func (s *Service) acceptCardAction(bot domain.NotificationBot, item domain.BotInteraction, ev InboundEvent) (InboundAck, error) { + if !pushCardKey(ev.Action) || ev.DeliveryID == "" { + return s.ignore(ev, "unknown_action"), nil + } + chat := ev.SenderID + if delivery, err := s.Store.NotificationDelivery(ev.DeliveryID); err == nil && delivery.BotID == bot.ID && delivery.ProjectID == bot.ProjectID && delivery.TargetType == "single" && delivery.TargetID == ev.SenderID { + chat = delivery.TargetID + } + item.Content, item.Quote, item.ContentLength = "", "", 0 + item.SendRoute = repo.BotSendRoutePush + inserted, err := s.Store.EnqueueBotInteraction(item, "single", chat) + if err != nil { + return InboundAck{}, err + } + return InboundAck{Accepted: inserted, InteractionID: item.ID}, nil +} + +// tenantGate enforces the enterprise scope of new channels. A locked scope +// must match; an unlocked one records the candidate (without bumping the +// configuration version) and answers with a fixed notice. gated=true with an +// empty notice means drop the event. +func (s *Service) tenantGate(bot domain.NotificationBot, ev InboundEvent) (string, bool, error) { + if bot.TenantState == domain.BotTenantLocked { + if ev.TenantKey == "" || ev.TenantKey != bot.TenantKey { + return "", true, nil + } + return "", false, nil + } + if ev.TenantKey == "" { + return "", true, nil + } + state, err := s.Store.RecordBotTenantCandidate(bot.ID, ev.TenantKey, ev.EventID, bot.CredentialVersion) + if err != nil { + return "", true, err + } + if state == domain.BotTenantLocked { + // Locked concurrently: apply the locked rule to the current value. + current, err := s.Store.NotificationBot(bot.ID) + if err != nil { + return "", true, err + } + if current.TenantKey == ev.TenantKey { + return "", false, nil + } + return "", true, nil + } + return noticeTenantPending, true, nil +} + +// enqueueNotice persists a fixed reply for an event the model must not see. +// Only type and length are kept, never the original content. +func (s *Service) enqueueNotice(item domain.BotInteraction, ev InboundEvent, notice string) (InboundAck, error) { + item.Action = actionSystemNotice + item.Content = notice + item.Quote = "" + inserted, err := s.Store.EnqueueBotInteraction(item, ev.ChatType, ev.ChatID) + if err != nil { + return InboundAck{}, err + } + return InboundAck{Accepted: inserted, InteractionID: item.ID}, nil +} + +func (s *Service) ignore(ev InboundEvent, reason string) InboundAck { + log.Printf("%s inbound ignored reason=%s", channelLabel(ev.Channel), reason) + return InboundAck{Ignored: reason} +} + +func channelLabel(kind string) string { + if kind == "" { + return ChannelWeCom + } + return kind +} diff --git a/internal/botinteraction/persistence.go b/internal/botinteraction/persistence.go index 45ec8fc85..e05754a0c 100644 --- a/internal/botinteraction/persistence.go +++ b/internal/botinteraction/persistence.go @@ -2,6 +2,7 @@ package botinteraction import ( "agentflow/internal/domain" + "agentflow/internal/repo" "errors" "fmt" "log" @@ -11,8 +12,8 @@ import ( // A legacy utf8mb3 table cannot store emoji. Preserve a clear terminal reply // rather than reconnecting with an orphaned running request and no explanation. -func (s *Service) saveReply(item domain.BotInteraction, reply, detail string) (string, string, error) { - err := s.Store.SaveBotReply(item, reply, detail) +func (s *Service) saveReply(item domain.BotInteraction, lease repo.BotLease, reply, detail string) (string, string, error) { + err := s.Store.SaveBotReply(item, lease, reply, detail) if err == nil { return reply, detail, nil } @@ -22,7 +23,7 @@ func (s *Service) saveReply(item domain.BotInteraction, reply, detail string) (s if sqlErr.Number == 1366 || sqlErr.Number == 3988 { reply = "AI 回复已生成,但数据库无法保存部分字符。请联系管理员将 Bot 消息表升级为 utf8mb4,然后重新发送请求。" detail = fmt.Sprintf("消息保存失败:MySQL %d,请升级 bot_interactions 字符集为 utf8mb4", sqlErr.Number) - err = s.Store.SaveBotReply(item, reply, detail) + err = s.Store.SaveBotReply(item, lease, reply, detail) return reply, detail, err } } else { diff --git a/internal/botinteraction/persistence_test.go b/internal/botinteraction/persistence_test.go index 81b80b7ed..07f59d882 100644 --- a/internal/botinteraction/persistence_test.go +++ b/internal/botinteraction/persistence_test.go @@ -18,7 +18,15 @@ func TestEmojiPersistenceFailureFinishesWithActionableReply(t *testing.T) { t.Fatal(err) } defer store.Close() - item := domain.BotInteraction{ID: "emoji-reply", BotID: "bot", Status: "running"} + bot := domain.NotificationBot{ID: "bot", BotID: "external", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + if err := store.SaveNotificationBot(bot, "s"); err != nil { + t.Fatal(err) + } + lease, _, err := store.ClaimBotConnection(bot.LeaseKey(), "owner") + if err != nil { + t.Fatal(err) + } + item := domain.BotInteraction{ID: "emoji-reply", BotID: "bot", Status: "running", LeaseOwner: lease.Owner, LeaseEpoch: lease.Epoch} if err := store.DB.Create(&item).Error; err != nil { t.Fatal(err) } @@ -36,7 +44,7 @@ func TestEmojiPersistenceFailureFinishesWithActionableReply(t *testing.T) { t.Fatal(err) } service := New(store) - reply, detail, err := service.saveReply(item, "你好 👋", "") + reply, detail, err := service.saveReply(item, lease, "你好 👋", "") if err != nil || failures != 1 || !strings.Contains(reply, "utf8mb4") || !strings.Contains(detail, fmt.Sprint(code)) { t.Fatal(reply, detail, err, failures) } diff --git a/internal/botinteraction/push.go b/internal/botinteraction/push.go new file mode 100644 index 000000000..0d366f5c1 --- /dev/null +++ b/internal/botinteraction/push.go @@ -0,0 +1,216 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "bytes" + "context" + "encoding/json" + "io" + "net/http" + "slices" + "strconv" + "strings" + "sync" + "time" + + larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" +) + +// Push sends one proactive message through the channel's application API. +// It needs no receiving connection, so notification-only configurations +// (interaction disabled) can use it. msg.ChatID is the platform target: the +// WeCom chat ID, the Feishu open_id or the DingTalk staff ID of a bound +// private-chat sender. +func (s *Service) Push(ctx context.Context, bot domain.NotificationBot, msg OutboundMessage) SendResult { + channel, err := s.Channel(bot) + if err != nil { + return SendResult{Outcome: NotDelivered, Code: ErrChannelNotReady.Error(), SafeDetail: "该渠道的主动发送未开放"} + } + sender, ok := channel.(PushSender) + if !ok { + return SendResult{Outcome: NotDelivered, Code: ErrChannelNotReady.Error(), SafeDetail: "该渠道的主动发送未开放"} + } + secret, err := s.Store.BotCredential(bot.ID) + if err != nil || secret == "" { + return SendResult{Outcome: NotDelivered, Code: "credential_unavailable", SafeDetail: "机器人凭据不可用,请重新填写 Secret"} + } + ctx, cancel := context.WithTimeout(ctx, sendTimeout) + defer cancel() + return sender.Push(ctx, bot, Credential{Secret: secret}, msg) +} + +// PushIdempotent reports whether the channel de-duplicates proactive sends +// carrying the same key (Feishu uuid, one hour), so an unknown outcome may +// be retried with that key inside the window. +func PushIdempotent(bot domain.NotificationBot) bool { + return bot.ChannelKind() == domain.BotChannelFeishu +} + +// Push creates a private message to an open_id with the persisted key as +// uuid; Feishu drops a repeated uuid within one hour. +func (f *feishuChannel) Push(ctx context.Context, bot domain.NotificationBot, cred Credential, msg OutboundMessage) SendResult { + if strings.TrimSpace(msg.ChatID) == "" { + return SendResult{Outcome: Rejected, Code: "target_missing", SafeDetail: "缺少飞书接收成员"} + } + msgType, content := "text", []byte(nil) + if msg.Card != nil { + msgType, content = "interactive", feishuCardJSON(msg.Card) + } else { + content, _ = json.Marshal(map[string]string{"text": msg.Text}) + } + body := larkim.NewCreateMessageReqBodyBuilder().ReceiveId(msg.ChatID).MsgType(msgType).Content(string(content)) + if msg.SendKey != "" { + body = body.Uuid(msg.SendKey) + } + req := larkim.NewCreateMessageReqBuilder().ReceiveIdType("open_id").Body(body.Build()).Build() + resp, err := f.client(bot, cred).Im.V1.Message.Create(ctx, req) + if err != nil { + return classifyFeishuTransport(err) + } + if !resp.Success() { + return classifyFeishuCode(resp.Code) + } + out := SendResult{Outcome: Delivered} + if resp.Data != nil { + out.PlatformMessageID = deref(resp.Data.MessageId) + } + return out +} + +// dingtalkToken caches one application access token per credential version. +type dingtalkToken struct { + key string + value string + expires time.Time +} + +var dingtalkTokens = struct { + sync.Mutex + m map[string]dingtalkToken +}{m: map[string]dingtalkToken{}} + +func dingtalkTokenKey(bot domain.NotificationBot) string { + return bot.ID + "|" + bot.BotID + "|" + strconv.FormatInt(bot.CredentialVersion, 10) +} + +// accessToken returns a cached or new application token. A failure means +// nothing was sent: rejected credentials are Rejected, anything else is +// NotDelivered and retryable. +func (d *dingtalkChannel) accessToken(ctx context.Context, bot domain.NotificationBot, cred Credential) (string, *SendResult) { + key := dingtalkTokenKey(bot) + dingtalkTokens.Lock() + cached, ok := dingtalkTokens.m[bot.ID] + dingtalkTokens.Unlock() + if ok && cached.key == key && time.Now().Before(cached.expires) { + return cached.value, nil + } + status, data, err := d.postJSON(ctx, d.api+"/v1.0/oauth2/accessToken", map[string]string{"appKey": bot.BotID, "appSecret": cred.Secret}) + if err != nil { + return "", &SendResult{Outcome: NotDelivered, Code: "token_unavailable", Retryable: true, SafeDetail: "无法获取钉钉访问凭证,稍后重试"} + } + var out struct { + AccessToken string `json:"accessToken"` + ExpireIn int64 `json:"expireIn"` + Code string `json:"code"` + } + _ = json.Unmarshal(data, &out) + if status != http.StatusOK || out.AccessToken == "" { + if status >= 500 || status == http.StatusTooManyRequests { + return "", &SendResult{Outcome: NotDelivered, Code: "token_unavailable", Retryable: true, SafeDetail: "无法获取钉钉访问凭证,稍后重试"} + } + return "", &SendResult{Outcome: Rejected, Code: "credential_invalid", SafeDetail: "钉钉拒绝了 Client ID 或 Client Secret"} + } + ttl := time.Duration(out.ExpireIn) * time.Second + if ttl <= 0 || ttl > 2*time.Hour { + ttl = 2 * time.Hour + } + dingtalkTokens.Lock() + dingtalkTokens.m[bot.ID] = dingtalkToken{key: key, value: out.AccessToken, expires: time.Now().Add(ttl - 5*time.Minute)} + dingtalkTokens.Unlock() + return out.AccessToken, nil +} + +func forgetDingTalkToken(bot domain.NotificationBot) { + dingtalkTokens.Lock() + delete(dingtalkTokens.m, bot.ID) + dingtalkTokens.Unlock() +} + +// Push sends a private message to a staff ID through the robot batch API +// (robotCode is the Client ID of the enterprise internal application). The +// API has no idempotency key: an unknown outcome is never retried +// automatically. +func (d *dingtalkChannel) Push(ctx context.Context, bot domain.NotificationBot, cred Credential, msg OutboundMessage) SendResult { + if strings.TrimSpace(msg.ChatID) == "" { + return SendResult{Outcome: Rejected, Code: "target_missing", SafeDetail: "缺少钉钉接收成员"} + } + token, failed := d.accessToken(ctx, bot, cred) + if failed != nil { + return *failed + } + if msg.Card != nil && strings.TrimSpace(bot.CardTemplateID) != "" { + out := d.deliverCard(ctx, bot, token, msg) + if out.Code == "token_invalid" { + forgetDingTalkToken(bot) + } + return out + } + param, _ := json.Marshal(map[string]string{"content": msg.Text}) + raw, _ := json.Marshal(map[string]any{"robotCode": bot.BotID, "userIds": []string{msg.ChatID}, "msgKey": "sampleText", "msgParam": string(param)}) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, d.api+"/v1.0/robot/oToMessages/batchSend", bytes.NewReader(raw)) + if err != nil { + return SendResult{Outcome: NotDelivered, Code: "request", SafeDetail: "钉钉请求无法构造"} + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", dingtalkUserAgent) + req.Header.Set("x-acs-dingtalk-access-token", token) + resp, err := d.http.Do(req) + if err != nil { + return classifyDingTalkTransport(err) + } + defer resp.Body.Close() + data, _ := io.ReadAll(io.LimitReader(resp.Body, dingtalkMaxResponse)) + out := classifyDingTalkPush(resp.StatusCode, data, msg.ChatID) + if out.Code == "token_invalid" { + forgetDingTalkToken(bot) + } + return out +} + +// classifyDingTalkPush maps the batchSend answer. The staff lists report +// per-recipient refusals inside an HTTP 200 answer. +func classifyDingTalkPush(status int, data []byte, staffID string) SendResult { + var out struct { + ProcessQueryKey string `json:"processQueryKey"` + InvalidStaffIDList []string `json:"invalidStaffIdList"` + FlowControlledStaffIDList []string `json:"flowControlledStaffIdList"` + Code string `json:"code"` + Message string `json:"message"` + } + _ = json.Unmarshal(data, &out) + switch { + case status == http.StatusTooManyRequests: + return SendResult{Outcome: NotDelivered, Code: "429", Retryable: true, SafeDetail: "钉钉限流,稍后重试"} + case status >= 500: + return SendResult{Outcome: Unknown, Code: strconv.Itoa(status), SafeDetail: "钉钉返回服务端错误,送达结果未知"} + case status == http.StatusUnauthorized || (status >= 400 && strings.Contains(strings.ToLower(out.Code), "accesstoken")): + return SendResult{Outcome: NotDelivered, Code: "token_invalid", Retryable: true, SafeDetail: "钉钉访问凭证已失效,稍后重试"} + case status != http.StatusOK: + code := out.Code + if code == "" { + code = strconv.Itoa(status) + } + return SendResult{Outcome: Rejected, Code: truncate(code, 64), SafeDetail: "钉钉拒绝了这次发送(" + truncate(code, 64) + ")"} + } + if slices.Contains(out.InvalidStaffIDList, staffID) { + return SendResult{Outcome: Rejected, Code: "invalid_staff", SafeDetail: "钉钉不接受该成员:成员不在应用可见范围内或已离职"} + } + if slices.Contains(out.FlowControlledStaffIDList, staffID) { + return SendResult{Outcome: NotDelivered, Code: "flow_controlled", Retryable: true, SafeDetail: "钉钉对该成员限流,稍后重试"} + } + if out.ProcessQueryKey == "" { + return SendResult{Outcome: Unknown, Code: "unparsed", SafeDetail: "钉钉发送响应无法识别,送达结果未知"} + } + return SendResult{Outcome: Delivered, PlatformMessageID: truncate(out.ProcessQueryKey, 191)} +} diff --git a/internal/botinteraction/push_card.go b/internal/botinteraction/push_card.go new file mode 100644 index 000000000..e4463e591 --- /dev/null +++ b/internal/botinteraction/push_card.go @@ -0,0 +1,348 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "agentflow/internal/repo" + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "strconv" + "strings" +) + +// Action cards of Feishu and DingTalk (proactive notifications of waiting or +// failed nodes). The buttons carry the delivery ID and the action key; a +// click comes back on the receiving connection as an InboundEvent with +// Action and DeliveryID set and runs through the same checks as WeCom card +// clicks (bound sender, project permission, node still in the notified +// state, action still offered). The result is sent to the clicker as a +// private message. + +// PushCard is the channel-neutral content of an action card. +type PushCard struct { + DeliveryID string + // DraftID marks a write-confirmation card: buttons carry the draft + // instead of a delivery. Body is free text shown above the rows. + DraftID string + Body string + DetailsLabel string + Title string + Tag string // 待人工确认 or 执行失败 + Reason string + Required string + Rows [][2]string + Actions []PushCardAction + DetailsURL string +} + +// PushCardAction is one button. +type PushCardAction struct { + Key string + Text string + Danger bool +} + +// PushCardKeys are the action keys a card button may carry. +var PushCardKeys = []string{"approve", "retry", "reject", "pass", "resume", "decline"} + +// Draft confirmation card actions. +const ( + DraftActionConfirm = "draft_confirm" + DraftActionConfirmStart = "draft_confirm_start" + DraftActionCancel = "draft_cancel" +) + +// DraftAction reports whether key is a draft confirmation action. +func DraftAction(key string) bool { + return key == DraftActionConfirm || key == DraftActionConfirmStart || key == DraftActionCancel +} + +// PushCardFor builds the action card of a waiting or failed node delivery, +// or nil when the node no longer offers an action. +func (s *Service) PushCardFor(item domain.NotificationDelivery, content, detailsURL string) *PushCard { + actions := ActionsForDelivery(s.Store, item) + if len(actions) == 0 { + return nil + } + run, ok := s.Store.RunState(item.RunID) + if !ok { + return nil + } + reason, required := cardReason(run, item, content) + issue := notificationCardIssue(content) + title := issue + if title == "未关联需求" { + title = run.WorkflowSnapshot.Name + } + card := &PushCard{DeliveryID: item.ID, Title: cardText(title, 60), Tag: map[string]string{"waiting": "待人工确认", "failed": "执行失败"}[item.EventType], Reason: cardText(reason, 300), Required: cardText(required, 120), DetailsURL: detailsURL} + card.Rows = [][2]string{{"需求", cardText(issue, 60)}, {"节点", cardText(item.SourceName, 60)}, {"SOP", cardText(run.WorkflowSnapshot.Name, 60)}} + buttons, _ := notificationCardActions(actions) + for _, b := range buttons { + key, _ := b["key"].(string) + text, _ := b["text"].(string) + style, _ := b["style"].(int) + if key == "reject" { + if target := cardReworkTarget(run, item.NodeID); target != "" { + card.Rows = append(card.Rows, [2]string{"返工到", cardText(target, 60)}) + } + } + card.Actions = append(card.Actions, PushCardAction{Key: key, Text: text, Danger: style == 4}) + } + return card +} + +// pushCardKey reports whether key is a card action. +func pushCardKey(key string) bool { + if DraftAction(key) { + return true + } + for _, k := range PushCardKeys { + if k == key { + return true + } + } + return false +} + +// PlainText is the card as plain text, used where cards are unavailable. +func (c *PushCard) PlainText() string { + lines := []string{c.Tag + ":" + c.Title, "原因:" + c.Reason} + if c.Required != "" { + lines = append(lines, "待处理:"+c.Required) + } + for _, row := range c.Rows { + lines = append(lines, row[0]+":"+row[1]) + } + if c.DetailsURL != "" { + lines = append(lines, "查看详情:", c.DetailsURL) + } + return strings.Join(lines, "\n") +} + +// feishuCardJSON renders a message card (JSON 1.0). User text is plain_text +// so names and reasons cannot inject Markdown. Buttons carry the action and +// delivery in value; clicks arrive as card.action.trigger callbacks. +func feishuCardJSON(c *PushCard) []byte { + plain := func(text string) map[string]any { return map[string]any{"tag": "plain_text", "content": text} } + elements := []any{} + if c.Body != "" { + elements = append(elements, map[string]any{"tag": "div", "text": plain(c.Body)}) + } + if c.Reason != "" { + elements = append(elements, map[string]any{"tag": "div", "text": plain("原因:" + c.Reason)}) + } + if c.Required != "" { + elements = append(elements, map[string]any{"tag": "div", "text": plain("待处理:" + c.Required)}) + } + fields := []any{} + for _, row := range c.Rows { + fields = append(fields, map[string]any{"is_short": true, "text": plain(row[0] + ":" + row[1])}) + } + if len(fields) > 0 { + elements = append(elements, map[string]any{"tag": "div", "fields": fields}) + } + buttons := []any{} + for _, a := range c.Actions { + kind := "primary" + if a.Danger { + kind = "danger" + } + value := map[string]string{"af_action": a.Key, "af_delivery": c.DeliveryID} + if c.DraftID != "" { + value = map[string]string{"af_action": a.Key, "af_draft": c.DraftID} + } + buttons = append(buttons, map[string]any{"tag": "button", "text": plain(a.Text), "type": kind, "value": value}) + } + if c.DetailsURL != "" { + label := c.DetailsLabel + if label == "" { + label = "查看详情" + } + buttons = append(buttons, map[string]any{"tag": "button", "text": plain(label), "type": "default", "url": c.DetailsURL}) + } + elements = append(elements, map[string]any{"tag": "action", "actions": buttons}) + template := "orange" + switch { + case c.Tag == "执行失败": + template = "red" + case c.DraftID != "": + template = "blue" + } + card := map[string]any{ + "config": map[string]any{"wide_screen_mode": true}, + "header": map[string]any{"template": template, "title": plain(c.Tag + ":" + c.Title)}, + "elements": elements, + } + out, _ := json.Marshal(card) + return out +} + +// dingtalkCardParams fills the variables of the administrator's card +// template (see docs/im-channels.md): title, content (Markdown), detail_url, +// status and one show_ switch per button. +func dingtalkCardParams(c *PushCard) map[string]string { + lines := []string{"**原因**:" + dingtalkMarkdownText(c.Reason)} + if c.Required != "" { + lines = append(lines, "**待处理**:"+dingtalkMarkdownText(c.Required)) + } + for _, row := range c.Rows { + lines = append(lines, "**"+row[0]+"**:"+dingtalkMarkdownText(row[1])) + } + params := map[string]string{"title": c.Tag + ":" + c.Title, "content": strings.Join(lines, "\n\n"), "detail_url": c.DetailsURL, "status": ""} + for _, key := range PushCardKeys { + params["show_"+key] = "false" + } + for _, a := range c.Actions { + params["show_"+a.Key] = "true" + params["text_"+a.Key] = a.Text + } + return params +} + +// dingtalkMarkdownText neutralizes Markdown control characters in user text. +func dingtalkMarkdownText(text string) string { + return strings.NewReplacer("*", "*", "_", "_", "[", "[", "]", "]", "#", "#", "`", "'", "<", "<", ">", ">").Replace(text) +} + +// dingtalkCardTrack is the card instance ID: the delivery plus its send key, +// so a resend (new generation) creates a new card. +func dingtalkCardTrack(msg OutboundMessage) string { + key := msg.SendKey + if len(key) > 12 { + key = key[:12] + } + return msg.Card.DeliveryID + "." + key +} + +// dingtalkCardDelivery extracts the delivery ID from a card instance ID. +func dingtalkCardDelivery(track string) string { + if i := strings.LastIndex(track, "."); i > 0 { + return track[:i] + } + return track +} + +// deliverCard creates the card instance and delivers it to the robot private +// chat of the staff ID, with Stream callbacks. +func (d *dingtalkChannel) deliverCard(ctx context.Context, bot domain.NotificationBot, token string, msg OutboundMessage) SendResult { + body := map[string]any{ + "cardTemplateId": strings.TrimSpace(bot.CardTemplateID), + "outTrackId": dingtalkCardTrack(msg), + "callbackType": "STREAM", + "cardData": map[string]any{"cardParamMap": dingtalkCardParams(msg.Card)}, + "openSpaceId": "dtv1.card//IM_ROBOT." + msg.ChatID, + "imRobotOpenSpaceModel": map[string]any{"supportForward": false, "lastMessageI18n": map[string]string{"ZH_CN": cardText(msg.Card.Tag+":"+msg.Card.Title, 60)}}, + "imRobotOpenDeliverModel": map[string]any{"spaceType": "IM_ROBOT", "robotCode": bot.BotID}, + "userIdType": 1, + } + raw, _ := json.Marshal(body) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, d.api+"/v1.0/card/instances/createAndDeliver", bytes.NewReader(raw)) + if err != nil { + return SendResult{Outcome: NotDelivered, Code: "request", SafeDetail: "钉钉请求无法构造"} + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", dingtalkUserAgent) + req.Header.Set("x-acs-dingtalk-access-token", token) + resp, err := d.http.Do(req) + if err != nil { + return classifyDingTalkTransport(err) + } + defer resp.Body.Close() + data, _ := io.ReadAll(io.LimitReader(resp.Body, dingtalkMaxResponse)) + return classifyDingTalkCard(resp.StatusCode, data) +} + +// classifyDingTalkCard maps the createAndDeliver answer: the instance and +// each delivery report success separately. +func classifyDingTalkCard(status int, data []byte) SendResult { + var out struct { + Success bool `json:"success"` + Code string `json:"code"` + Result struct { + DeliverResults []struct { + Success bool `json:"success"` + CarrierID string `json:"carrierId"` + ErrorMsg string `json:"errorMsg"` + } `json:"deliverResults"` + } `json:"result"` + } + _ = json.Unmarshal(data, &out) + switch { + case status == http.StatusTooManyRequests: + return SendResult{Outcome: NotDelivered, Code: "429", Retryable: true, SafeDetail: "钉钉限流,稍后重试"} + case status >= 500: + return SendResult{Outcome: Unknown, Code: strconv.Itoa(status), SafeDetail: "钉钉返回服务端错误,卡片送达结果未知"} + case status == http.StatusUnauthorized || (status >= 400 && strings.Contains(strings.ToLower(out.Code), "accesstoken")): + return SendResult{Outcome: NotDelivered, Code: "token_invalid", Retryable: true, SafeDetail: "钉钉访问凭证已失效,稍后重试"} + case status != http.StatusOK: + code := out.Code + if code == "" { + code = strconv.Itoa(status) + } + detail := "钉钉拒绝了这张卡片(" + truncate(code, 64) + ")" + if strings.Contains(code, "template") { + detail += ":请检查互动卡片模板 ID 是否正确、模板是否已发布" + } + return SendResult{Outcome: Rejected, Code: truncate(code, 64), SafeDetail: detail} + } + if len(out.Result.DeliverResults) == 0 { + return SendResult{Outcome: Unknown, Code: "unparsed", SafeDetail: "钉钉卡片响应无法识别,送达结果未知"} + } + first := out.Result.DeliverResults[0] + if !first.Success { + return SendResult{Outcome: Rejected, Code: "deliver_failed", SafeDetail: "钉钉未能投放卡片:" + truncate(first.ErrorMsg, 120)} + } + return SendResult{Outcome: Delivered, PlatformMessageID: truncate(first.CarrierID, 191)} +} + +// draftConfirmMarker starts the confirmation section the platform appends to +// a chat reply with write drafts (see httpapi.confirmBotDrafts). +const draftConfirmMarker = "\n\n以下操作需要你在平台确认后才会执行" + +// DraftCardFor builds the write-confirmation card of a chat reply: the +// model's explanation, the frozen operations, confirm / confirm-and-start / +// cancel buttons and a link to the confirmation page (to change the SOP). +// It returns nil when the reply produced no pending draft. +func (s *Service) DraftCardFor(item domain.BotInteraction, publicURL string) *PushCard { + draft, ok := s.Store.BotPendingDraftForInteraction(item.ID) + if !ok { + return nil + } + body := item.Reply + if i := strings.Index(body, draftConfirmMarker); i >= 0 { + body = body[:i] + } + ops := strings.Split(strings.TrimSpace(draft.Summary), "\n") + card := &PushCard{DraftID: draft.ID, Tag: "待你确认", Title: fmt.Sprintf("%d 项操作", len(ops)), Body: strings.TrimSpace(body)} + for _, op := range ops { + if op = strings.TrimSpace(op); op != "" { + card.Rows = append(card.Rows, [2]string{"操作", op}) + } + } + card.Required = fmt.Sprintf("%d 分钟内有效,只有你本人可以确认;每份操作只执行一次", int(repo.BotDraftTTL.Minutes())) + card.Actions = []PushCardAction{{Key: DraftActionConfirm, Text: "确认执行"}} + if strings.Contains(draft.Summary, "(SOP:") { + card.Actions = append(card.Actions, PushCardAction{Key: DraftActionConfirmStart, Text: "确认并启动 SOP"}) + } + card.Actions = append(card.Actions, PushCardAction{Key: DraftActionCancel, Text: "取消", Danger: true}) + if base := strings.TrimRight(publicURL, "/"); base != "" { + card.DetailsURL, card.DetailsLabel = base+"/bot-approve.html#draft="+draft.ID, "在平台确认(可更换 SOP)" + } + return card +} + +func (s *Service) publicURL() string { + if s.PublicURL == nil { + return "" + } + raw := strings.TrimSpace(s.PublicURL()) + if u, err := url.Parse(raw); err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" { + return "" + } + return raw +} diff --git a/internal/botinteraction/push_card_test.go b/internal/botinteraction/push_card_test.go new file mode 100644 index 000000000..ef47be148 --- /dev/null +++ b/internal/botinteraction/push_card_test.go @@ -0,0 +1,327 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" + + larkevent "github.com/larksuite/oapi-sdk-go/v3/event" + "github.com/larksuite/oapi-sdk-go/v3/event/dispatcher/callback" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" +) + +func testCard() *PushCard { + return &PushCard{DeliveryID: "d-1", Title: "登录优化", Tag: "待人工确认", Reason: "请确认 **方案**", Rows: [][2]string{{"节点", "评审"}, {"SOP", "研发交付"}}, Actions: []PushCardAction{{Key: "approve", Text: "通过并继续"}, {Key: "reject", Text: "返工", Danger: true}}, DetailsURL: "https://platform.example/#/runs/r"} +} + +func TestFeishuPushSendsActionCard(t *testing.T) { + var mu sync.Mutex + var msgType, content string + mux := http.NewServeMux() + mux.HandleFunc("/open-apis/auth/v3/tenant_access_token/internal", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"code": 0, "tenant_access_token": "t", "expire": 7200}) + }) + mux.HandleFunc("/open-apis/im/v1/messages", func(w http.ResponseWriter, r *http.Request) { + var body struct { + MsgType string `json:"msg_type"` + Content string `json:"content"` + } + json.NewDecoder(r.Body).Decode(&body) + mu.Lock() + msgType, content = body.MsgType, body.Content + mu.Unlock() + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"message_id": "om_card"}}) + }) + srv := httptest.NewServer(mux) + defer srv.Close() + ch := newFeishuChannel() + ch.base = srv.URL + res := ch.Push(context.Background(), domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelFeishu, BotID: "cli"}, Credential{Secret: "s"}, OutboundMessage{ChatID: "ou_1", Text: "fallback", SendKey: "k", Card: testCard()}) + if res.Outcome != Delivered || msgType != "interactive" { + t.Fatalf("push = %+v type %s", res, msgType) + } + var card struct { + Header struct { + Title struct{ Tag, Content string } `json:"title"` + } `json:"header"` + Elements []map[string]any `json:"elements"` + } + if err := json.Unmarshal([]byte(content), &card); err != nil { + t.Fatal(err) + } + if card.Header.Title.Content != "待人工确认:登录优化" || card.Header.Title.Tag != "plain_text" { + t.Fatalf("header = %+v", card.Header) + } + if !strings.Contains(content, `"af_action":"approve"`) || !strings.Contains(content, `"af_delivery":"d-1"`) || !strings.Contains(content, `"type":"danger"`) || !strings.Contains(content, `"url":"https://platform.example/#/runs/r"`) { + t.Fatalf("buttons = %s", content) + } + if strings.Contains(content, "lark_md") { + t.Fatal("user text rendered as Markdown") + } +} + +func TestDingTalkPushCardNeedsTemplate(t *testing.T) { + var mu sync.Mutex + var paths []string + var cardBody map[string]any + answer := func(w http.ResponseWriter) { + json.NewEncoder(w).Encode(map[string]any{"success": true, "result": map[string]any{"deliverResults": []any{map[string]any{"success": true, "carrierId": "car-1"}}}}) + } + mux := http.NewServeMux() + mux.HandleFunc("/v1.0/oauth2/accessToken", func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(map[string]any{"accessToken": "at", "expireIn": 7200}) + }) + mux.HandleFunc("/v1.0/card/instances/createAndDeliver", func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + paths = append(paths, r.URL.Path) + cardBody = map[string]any{} + json.NewDecoder(r.Body).Decode(&cardBody) + a := answer + mu.Unlock() + a(w) + }) + mux.HandleFunc("/v1.0/robot/oToMessages/batchSend", func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + paths = append(paths, r.URL.Path) + mu.Unlock() + json.NewEncoder(w).Encode(map[string]any{"processQueryKey": "pqk"}) + }) + srv := httptest.NewServer(mux) + defer srv.Close() + ch := newDingTalkChannel() + ch.api = srv.URL + bot := domain.NotificationBot{ID: "cfg-card", Channel: domain.BotChannelDingTalk, BotID: "ding-app", CredentialVersion: 1} + forgetDingTalkToken(bot) + msg := OutboundMessage{ChatID: "staff-1", Text: "文字", SendKey: "generation-one-key", Card: testCard()} + if res := ch.Push(context.Background(), bot, Credential{Secret: "s"}, msg); res.Outcome != Delivered || paths[len(paths)-1] != "/v1.0/robot/oToMessages/batchSend" { + t.Fatalf("without template = %+v %v", res, paths) + } + bot.CardTemplateID = "tpl-1.schema" + res := ch.Push(context.Background(), bot, Credential{Secret: "s"}, msg) + if res.Outcome != Delivered || res.PlatformMessageID != "car-1" || paths[len(paths)-1] != "/v1.0/card/instances/createAndDeliver" { + t.Fatalf("card = %+v %v", res, paths) + } + if cardBody["cardTemplateId"] != "tpl-1.schema" || cardBody["outTrackId"] != "d-1.generation-o" || cardBody["callbackType"] != "STREAM" || cardBody["openSpaceId"] != "dtv1.card//IM_ROBOT.staff-1" { + t.Fatalf("body = %v", cardBody) + } + if deliver := cardBody["imRobotOpenDeliverModel"].(map[string]any); deliver["robotCode"] != "ding-app" || deliver["spaceType"] != "IM_ROBOT" { + t.Fatalf("deliver = %v", deliver) + } + params := cardBody["cardData"].(map[string]any)["cardParamMap"].(map[string]any) + if params["show_approve"] != "true" || params["show_reject"] != "true" || params["show_retry"] != "false" || params["text_approve"] != "通过并继续" || params["detail_url"] != "https://platform.example/#/runs/r" { + t.Fatalf("params = %v", params) + } + if !strings.Contains(params["content"].(string), "请确认 **方案**") { + t.Fatalf("markdown not neutralized: %v", params["content"]) + } + if dingtalkCardDelivery("d-1.generation-o") != "d-1" { + t.Fatal("delivery from track") + } + cases := []struct { + status int + body string + want SendOutcome + }{ + {400, `{"code":"param.templateNotExist"}`, Rejected}, + {200, `{"success":true,"result":{"deliverResults":[{"success":false,"errorMsg":"no robot"}]}}`, Rejected}, + {200, `{"success":true}`, Unknown}, + {502, ``, Unknown}, + {429, ``, NotDelivered}, + } + for _, c := range cases { + if got := classifyDingTalkCard(c.status, []byte(c.body)); got.Outcome != c.want { + t.Errorf("%d %s = %+v", c.status, c.body, got) + } + } + if got := classifyDingTalkCard(400, []byte(`{"code":"param.templateNotExist"}`)); !strings.Contains(got.SafeDetail, "模板") { + t.Errorf("template hint missing: %+v", got) + } +} + +func TestCardCallbacksMapToActionEvents(t *testing.T) { + tenant := "tk" + ev := &callback.CardActionTriggerEvent{EventV2Base: &larkevent.EventV2Base{Header: &larkevent.EventHeader{EventID: "e1", AppID: "cli", TenantKey: "tk"}}, Event: &callback.CardActionTriggerRequest{Operator: &callback.Operator{OpenID: "ou_1", TenantKey: &tenant}, Action: &callback.CallBackAction{Value: map[string]any{"af_action": "approve", "af_delivery": "d-1"}}}} + got, ok := feishuCardEvent(ev) + if !ok || got.Action != "approve" || got.DeliveryID != "d-1" || got.SenderID != "ou_1" || got.MessageID != "card:e1" || got.AppID != "cli" || got.TenantKey != "tk" || got.ChatType != "single" { + t.Fatalf("feishu = %+v", got) + } + ev.Event.Action.Value["af_action"] = "delete_everything" + if _, ok := feishuCardEvent(ev); ok { + t.Fatal("unknown action accepted") + } + bot := domain.NotificationBot{BotID: "ding-app"} + raw := `{"type":"actionCallback","outTrackId":"d-1.abc","corpId":"corp","userId":"staff-1","content":"{\"cardPrivateData\":{\"actionIds\":[\"btn\"],\"params\":{\"action\":\"retry\"}}}"}` + dt, reason := dingtalkCardEvent(bot, raw, "f1") + if reason != "" || dt.Action != "retry" || dt.DeliveryID != "d-1" || dt.SenderID != "staff-1" || dt.TenantKey != "corp" || dt.AppID != "ding-app" || dt.MessageID != "card:f1" { + t.Fatalf("dingtalk = %+v %s", dt, reason) + } + if _, reason := dingtalkCardEvent(bot, strings.Replace(raw, "retry", "drop", 1), "f2"); reason != "unknown_action" { + t.Fatalf("unknown action = %s", reason) + } +} + +// A click on a Feishu or DingTalk action card runs the card action of the +// recipient and answers with a private message; someone else's click is +// queued in a conversation of their own (and refused by the action). +func TestCardActionClickRunsActionAndRepliesPrivately(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + bot := domain.NotificationBot{ID: "cfg-card-click", Channel: domain.BotChannelDingTalk, BotID: "ding-app", ProjectID: "p", OwnerID: 42, Enabled: true, InteractionEnabled: true, TenantKey: "corp", TenantState: domain.BotTenantLocked} + if _, err := store.SaveNotificationBotChecked(bot, "secret", nil); err != nil { + t.Fatal(err) + } + now := time.Now().UTC() + if err := store.CreateNotificationDelivery(domain.NotificationDelivery{ID: "d-click", ProjectID: "p", BotID: bot.ID, RunID: "r", NodeID: "n", EventType: "waiting", TargetID: "staff-1", TargetType: "single", Content: "x", Status: "sent", CreatedAt: now, NextAttemptAt: now}); err != nil { + t.Fatal(err) + } + service := New(store) + fake := newFakeChannel() + ch := pushKindChannel{kindChannel{fake, ChannelDingTalk}, make(chan OutboundMessage, 4)} + service.RegisterChannel(ch) + var mu sync.Mutex + seen := map[string]domain.BotConversation{} + service.Action = func(ctx context.Context, b domain.NotificationBot, item domain.BotInteraction, conv domain.BotConversation) (string, error) { + mu.Lock() + seen[item.SenderID] = conv + mu.Unlock() + return "「评审」操作已完成:" + item.Action, nil + } + ctx, cancel := context.WithCancel(context.Background()) + service.Start(ctx) + defer func() { cancel(); <-service.Done }() + conn := waitConn(t, fake) + click := func(sender, id string) { + ack := conn.push(t, InboundEvent{Channel: ChannelDingTalk, AppID: "ding-app", TenantKey: "corp", EventID: id, MessageID: "card:" + id, SenderID: sender, ChatID: sender, ChatType: "single", MsgType: "card_action", Action: "approve", DeliveryID: "d-click"}) + if !ack.Accepted { + t.Fatalf("click not accepted: %+v", ack) + } + } + click("staff-1", "f1") + select { + case msg := <-ch.pushes: + if msg.ChatID != "staff-1" || !strings.Contains(msg.Text, "操作已完成:approve") { + t.Fatalf("reply = %+v", msg) + } + case <-time.After(10 * time.Second): + t.Fatal("no private reply") + } + click("staff-2", "f2") + select { + case msg := <-ch.pushes: + if msg.ChatID != "staff-2" { + t.Fatalf("reply = %+v", msg) + } + case <-time.After(10 * time.Second): + t.Fatal("no private reply to the other clicker") + } + mu.Lock() + defer mu.Unlock() + if seen["staff-1"].ChatID != "staff-1" || seen["staff-1"].ChatType != "single" || seen["staff-2"].ChatID != "staff-2" { + t.Fatalf("conversations = %+v", seen) + } + select { + case m := <-conn.replies: + t.Fatalf("webhook used %+v", m) + default: + } +} + +func TestDingTalkCardCallbackFrameAnswersWithStatus(t *testing.T) { + var got InboundEvent + c := &dingtalkConn{channel: newDingTalkChannel(), bot: domain.NotificationBot{BotID: "ding-app"}, ctx: context.Background(), sink: func(ctx context.Context, ev InboundEvent) (InboundAck, error) { + got = ev + return InboundAck{Accepted: true}, nil + }} + frame := &payload.DataFrame{Type: "CALLBACK", Headers: payload.DataFrameHeader{payload.DataFrameHeaderKTopic: payload.CardInstanceCallbackTopic, payload.DataFrameHeaderKMessageId: "m1"}, Data: `{"outTrackId":"d-9.k","userId":"u","corpId":"c","content":"{\"cardPrivateData\":{\"params\":{\"action\":\"pass\"}}}"}`} + resp := c.handleCardCallback(frame) + if got.Action != "pass" || got.DeliveryID != "d-9" || resp.Code != payload.DataFrameResponseStatusCodeKOK || !strings.Contains(resp.Data, "已收到") || !strings.Contains(resp.Data, "updateCardDataByKey") { + t.Fatalf("event %+v resp %+v", got, resp) + } +} + +func TestDraftCardForReplyWithPendingDraft(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + svc := New(store) + item := domain.BotInteraction{ID: "turn-1", Reply: "我会创建这个需求。\n\n以下操作需要你在平台确认后才会执行(30 分钟内有效):\n1. 保存需求:登录优化(SOP:研发交付)\n在平台确认这些操作:\nhttps://p.example/bot-approve.html#draft=x"} + if svc.DraftCardFor(item, "https://p.example") != nil { + t.Fatal("card without a draft") + } + d, err := store.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: "c", UserID: 7, InteractionID: "turn-1", Summary: "1. 保存需求:登录优化(SOP:研发交付)", Drafts: "[]"}, time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + card := svc.DraftCardFor(item, "https://p.example") + if card == nil || card.DraftID != d.ID || card.Body != "我会创建这个需求。" || len(card.Rows) != 1 || card.DetailsURL != "https://p.example/bot-approve.html#draft="+d.ID { + t.Fatalf("card = %+v", card) + } + keys := []string{} + for _, a := range card.Actions { + keys = append(keys, a.Key) + } + if strings.Join(keys, ",") != "draft_confirm,draft_confirm_start,draft_cancel" { + t.Fatalf("actions = %v", keys) + } + raw := string(feishuCardJSON(card)) + if !strings.Contains(raw, `"af_draft":"`+d.ID+`"`) || strings.Contains(raw, "af_delivery") || !strings.Contains(raw, "在平台确认(可更换 SOP)") { + t.Fatalf("card json = %s", raw) + } + // A cancelled draft falls back to the text reply. + store.CancelBotPendingDraft(d.ID, 7, "x", time.Now().UTC()) + if svc.DraftCardFor(item, "") != nil { + t.Fatal("card for a cancelled draft") + } + ev := &callback.CardActionTriggerEvent{EventV2Base: &larkevent.EventV2Base{Header: &larkevent.EventHeader{EventID: "e9", AppID: "cli"}}, Event: &callback.CardActionTriggerRequest{Operator: &callback.Operator{OpenID: "ou_1"}, Action: &callback.CallBackAction{Value: map[string]any{"af_action": "draft_confirm_start", "af_draft": d.ID}}}} + if got, ok := feishuCardEvent(ev); !ok || got.Action != DraftActionConfirmStart || got.DeliveryID != d.ID { + t.Fatalf("draft click = %+v", got) + } +} + +func TestFeishuReplySendsCardWhenGiven(t *testing.T) { + var mu sync.Mutex + var msgType string + mux := http.NewServeMux() + mux.HandleFunc("/open-apis/auth/v3/tenant_access_token/internal", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"code": 0, "tenant_access_token": "t", "expire": 7200}) + }) + mux.HandleFunc("/open-apis/im/v1/messages/om_1/reply", func(w http.ResponseWriter, r *http.Request) { + var body struct { + MsgType string `json:"msg_type"` + } + json.NewDecoder(r.Body).Decode(&body) + mu.Lock() + msgType = body.MsgType + mu.Unlock() + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"message_id": "om_r"}}) + }) + srv := httptest.NewServer(mux) + defer srv.Close() + ch := newFeishuChannel() + ch.base = srv.URL + bot := domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelFeishu, BotID: "cli"} + conn := &feishuConn{channel: ch, bot: bot, api: ch.client(bot, Credential{Secret: "s"})} + card := &PushCard{DraftID: "d", Tag: "待你确认", Title: "1 项操作", Body: "x", Actions: []PushCardAction{{Key: DraftActionConfirm, Text: "确认执行"}}} + if res := conn.Reply(context.Background(), OutboundMessage{Text: "t", Route: ReplyRoute{MessageID: "om_1"}, Card: card}); res.Outcome != Delivered || msgType != "interactive" { + t.Fatalf("card reply = %+v %s", res, msgType) + } + if res := conn.Reply(context.Background(), OutboundMessage{Text: "t", Route: ReplyRoute{MessageID: "om_1"}}); res.Outcome != Delivered || msgType != "text" { + t.Fatalf("text reply = %+v %s", res, msgType) + } +} diff --git a/internal/botinteraction/push_resend_test.go b/internal/botinteraction/push_resend_test.go new file mode 100644 index 000000000..85d6a1a3e --- /dev/null +++ b/internal/botinteraction/push_resend_test.go @@ -0,0 +1,105 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "errors" + "testing" + "time" +) + +// pushKindChannel adds proactive sends to the fake receiving channel. +type pushKindChannel struct { + kindChannel + pushes chan OutboundMessage +} + +func (p pushKindChannel) Push(_ context.Context, _ domain.NotificationBot, _ Credential, msg OutboundMessage) SendResult { + p.pushes <- msg + return SendResult{Outcome: Delivered, PlatformMessageID: "pqk"} +} + +// An expired DingTalk reply is switched to a proactive private message only +// for a verified, still-bound original sender; the saved reply goes out as a +// new generation with a new key through PushSender, not the webhook. +func TestDingTalkExpiredReplyResentAsPush(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + store.PutUser(domain.User{ID: 43, LoginName: "member"}) + store.PutProject(domain.Project{ID: "p", OwnerID: 42, Members: []domain.ProjectMember{{UserID: 43, Role: "member"}}}) + bot := domain.NotificationBot{ID: "cfg-dt-push", Channel: domain.BotChannelDingTalk, BotID: "ding-app", ProjectID: "p", OwnerID: 42, Enabled: true, InteractionEnabled: true, TenantKey: "ding-corp", TenantState: domain.BotTenantLocked} + if _, err := store.SaveNotificationBotChecked(bot, "secret", nil); err != nil { + t.Fatal(err) + } + bot, _ = store.NotificationBot(bot.ID) + now := time.Now().UTC() + token, _ := store.NewBotBindingChallenge(bot, "staff-1", now, true) + code, _ := store.ClaimBotBindingChallenge(token, 43, now) + if _, err := store.ConfirmBotUserBinding(bot, "staff-1", code, now); err != nil { + t.Fatal(err) + } + service := New(store) + fake := newFakeChannel() + ch := pushKindChannel{kindChannel{fake, ChannelDingTalk}, make(chan OutboundMessage, 4)} + service.RegisterChannel(ch) + service.Generate = func(ctx context.Context, b domain.NotificationBot, c domain.BotConversation, h []domain.BotInteraction, item domain.BotInteraction) (string, error) { + return "答复:" + item.Content, nil + } + ctx, cancel := context.WithCancel(context.Background()) + service.Start(ctx) + defer func() { cancel(); <-service.Done }() + conn := waitConn(t, fake) + conn.push(t, InboundEvent{Channel: ChannelDingTalk, AppID: "ding-app", TenantKey: "ding-corp", EventID: "f-x", MessageID: "mx", SenderID: "staff-1", ChatID: "cid-1", ChatType: "single", MsgType: "text", Text: "进度", Route: ReplyRoute{Webhook: "https://oapi.dingtalk.com/robot/sendBySession?session=s", ExpiresAt: time.Now().Add(5 * time.Second)}}) + id := repo.BotMessageID(bot.ID, "mx") + eventually(t, func() bool { it, _ := store.BotInteraction(id); return it.Status == domain.BotReplyFailed }) + expired, _ := store.BotInteraction(id) + if expired.TerminalReason != "route_expired" { + t.Fatalf("expired = %+v", expired) + } + // Not yet a verified push target. + if _, err := store.PushResendBotInteraction(id, 42, 43, "k-1", "x"); !errors.Is(err, repo.ErrBotPushTargetUnverified) { + t.Fatalf("unverified = %v", err) + } + if err := store.MarkBotPushTargetVerified(bot, "staff-1", 43, now); err != nil { + t.Fatal(err) + } + // A different platform user than the binding is refused. + if _, err := store.PushResendBotInteraction(id, 42, 44, "k-2", "x"); !errors.Is(err, repo.ErrBotPushTargetUnverified) { + t.Fatalf("other user = %v", err) + } + out, err := store.PushResendBotInteraction(id, 42, 43, "k-3", "会话回复已过期") + if err != nil || out.SendRoute != repo.BotSendRoutePush || out.DeliveryGeneration != expired.DeliveryGeneration+1 || out.SendKey == expired.SendKey { + t.Fatalf("push resend = %+v %v", out, err) + } + select { + case msg := <-ch.pushes: + if msg.ChatID != "staff-1" || msg.Text != "答复:进度" || msg.SendKey != out.SendKey { + t.Fatalf("push = %+v", msg) + } + case <-time.After(10 * time.Second): + t.Fatal("expired reply not pushed") + } + eventually(t, func() bool { it, _ := store.BotInteraction(id); return it.Status == domain.BotReplied }) + final, _ := store.BotInteraction(id) + if final.ResentBy != 42 || final.SendRoute != repo.BotSendRoutePush { + t.Fatalf("final = %+v", final) + } + // The same click again returns without a second send. + if again, err := store.PushResendBotInteraction(id, 42, 43, "k-3", "x"); err != nil || again.Status != domain.BotReplied { + t.Fatalf("repeat = %+v %v", again, err) + } + if _, err := store.PushResendBotInteraction(id, 42, 43, "k-4", "x"); !errors.Is(err, repo.ErrBotStateConflict) { + t.Fatalf("after reply = %v", err) + } + select { + case m := <-ch.pushes: + t.Fatalf("second push %+v", m) + case m := <-conn.replies: + t.Fatalf("webhook used %+v", m) + case <-time.After(500 * time.Millisecond): + } +} diff --git a/internal/botinteraction/push_test.go b/internal/botinteraction/push_test.go new file mode 100644 index 000000000..05cdc228b --- /dev/null +++ b/internal/botinteraction/push_test.go @@ -0,0 +1,186 @@ +package botinteraction + +import ( + "agentflow/internal/domain" + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" +) + +func TestFeishuPushCreatesOpenIDMessageWithUUID(t *testing.T) { + var mu sync.Mutex + var got struct { + path, idType, receiveID, uuid, text string + } + code := 0 + mux := http.NewServeMux() + mux.HandleFunc("/open-apis/auth/v3/tenant_access_token/internal", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"code": 0, "tenant_access_token": "t-token", "expire": 7200}) + }) + mux.HandleFunc("/open-apis/im/v1/messages", func(w http.ResponseWriter, r *http.Request) { + var body struct { + ReceiveID string `json:"receive_id"` + MsgType string `json:"msg_type"` + Content string `json:"content"` + UUID string `json:"uuid"` + } + json.NewDecoder(r.Body).Decode(&body) + var content struct { + Text string `json:"text"` + } + json.Unmarshal([]byte(body.Content), &content) + mu.Lock() + got.path, got.idType, got.receiveID, got.uuid, got.text = r.URL.Path, r.URL.Query().Get("receive_id_type"), body.ReceiveID, body.UUID, content.Text + c := code + mu.Unlock() + w.Header().Set("Content-Type", "application/json") + if c != 0 { + json.NewEncoder(w).Encode(map[string]any{"code": c, "msg": "fail"}) + return + } + json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"message_id": "om_push"}}) + }) + srv := httptest.NewServer(mux) + defer srv.Close() + ch := newFeishuChannel() + ch.base = srv.URL + bot := domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelFeishu, BotID: "cli_app"} + msg := OutboundMessage{ChatID: "ou_target", Text: "SOP 已完成", SendKey: "send-key-for-test"} + res := ch.Push(context.Background(), bot, Credential{Secret: "s"}, msg) + if res.Outcome != Delivered || res.PlatformMessageID != "om_push" { + t.Fatalf("push = %+v", res) + } + if got.idType != "open_id" || got.receiveID != "ou_target" || got.uuid != msg.SendKey || got.text != "SOP 已完成" { + t.Fatalf("request = %+v", got) + } + code = 230013 // bot not available to the user + if res := ch.Push(context.Background(), bot, Credential{Secret: "s"}, msg); res.Outcome != Rejected { + t.Fatalf("rejected = %+v", res) + } + if res := ch.Push(context.Background(), bot, Credential{Secret: "s"}, OutboundMessage{Text: "x"}); res.Outcome != Rejected || res.Code != "target_missing" { + t.Fatalf("no target = %+v", res) + } +} + +func TestDingTalkPushTokenCacheAndClassification(t *testing.T) { + var mu sync.Mutex + tokens, sends := 0, 0 + var lastBody map[string]any + var answer func(w http.ResponseWriter) + mux := http.NewServeMux() + mux.HandleFunc("/v1.0/oauth2/accessToken", func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + tokens++ + mu.Unlock() + json.NewEncoder(w).Encode(map[string]any{"accessToken": "at-1", "expireIn": 7200}) + }) + mux.HandleFunc("/v1.0/robot/oToMessages/batchSend", func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("x-acs-dingtalk-access-token") != "at-1" { + t.Errorf("token header = %q", r.Header.Get("x-acs-dingtalk-access-token")) + } + mu.Lock() + sends++ + lastBody = map[string]any{} + json.NewDecoder(r.Body).Decode(&lastBody) + a := answer + mu.Unlock() + a(w) + }) + srv := httptest.NewServer(mux) + defer srv.Close() + ch := newDingTalkChannel() + ch.api = srv.URL + bot := domain.NotificationBot{ID: "cfg-push-test", Channel: domain.BotChannelDingTalk, BotID: "ding-client", CredentialVersion: 1} + forgetDingTalkToken(bot) + cred := Credential{Secret: "s"} + ok := func(w http.ResponseWriter) { + json.NewEncoder(w).Encode(map[string]any{"processQueryKey": "pqk-1", "invalidStaffIdList": []string{}, "flowControlledStaffIdList": []string{}}) + } + answer = ok + msg := OutboundMessage{ChatID: "staff-1", Text: "等待处理"} + if res := ch.Push(context.Background(), bot, cred, msg); res.Outcome != Delivered || res.PlatformMessageID != "pqk-1" { + t.Fatalf("push = %+v", res) + } + if lastBody["robotCode"] != "ding-client" || lastBody["msgKey"] != "sampleText" || !strings.Contains(lastBody["msgParam"].(string), "等待处理") { + t.Fatalf("body = %v", lastBody) + } + if ids, _ := lastBody["userIds"].([]any); len(ids) != 1 || ids[0] != "staff-1" { + t.Fatalf("userIds = %v", lastBody["userIds"]) + } + ch.Push(context.Background(), bot, cred, msg) + if tokens != 1 || sends != 2 { + t.Fatalf("token not cached: tokens=%d sends=%d", tokens, sends) + } + cases := []struct { + name string + answer func(http.ResponseWriter) + want SendOutcome + code string + }{ + {"invalid staff", func(w http.ResponseWriter) { + json.NewEncoder(w).Encode(map[string]any{"processQueryKey": "k", "invalidStaffIdList": []string{"staff-1"}}) + }, Rejected, "invalid_staff"}, + {"flow controlled", func(w http.ResponseWriter) { + json.NewEncoder(w).Encode(map[string]any{"processQueryKey": "k", "flowControlledStaffIdList": []string{"staff-1"}}) + }, NotDelivered, "flow_controlled"}, + {"server error", func(w http.ResponseWriter) { w.WriteHeader(502) }, Unknown, "502"}, + {"rate limit", func(w http.ResponseWriter) { w.WriteHeader(429) }, NotDelivered, "429"}, + {"robot missing", func(w http.ResponseWriter) { + w.WriteHeader(400) + json.NewEncoder(w).Encode(map[string]any{"code": "resource.not.found", "message": "robot 不存在"}) + }, Rejected, "resource.not.found"}, + {"no key", func(w http.ResponseWriter) { json.NewEncoder(w).Encode(map[string]any{}) }, Unknown, "unparsed"}, + } + for _, tc := range cases { + answer = tc.answer + if res := ch.Push(context.Background(), bot, cred, msg); res.Outcome != tc.want || res.Code != tc.code { + t.Errorf("%s = %+v", tc.name, res) + } + } + // An invalid token is not delivered and is dropped from the cache. + answer = func(w http.ResponseWriter) { + w.WriteHeader(401) + json.NewEncoder(w).Encode(map[string]any{"code": "InvalidAuthentication"}) + } + if res := ch.Push(context.Background(), bot, cred, msg); res.Outcome != NotDelivered || res.Code != "token_invalid" { + t.Fatalf("token invalid = %+v", res) + } + answer = ok + ch.Push(context.Background(), bot, cred, msg) + if tokens != 2 { + t.Fatalf("token not refreshed after 401: %d", tokens) + } + // A rotated credential uses a new token. + bot.CredentialVersion = 2 + ch.Push(context.Background(), bot, cred, msg) + if tokens != 3 { + t.Fatalf("rotation reused token: %d", tokens) + } + if res := ch.Push(context.Background(), bot, cred, OutboundMessage{Text: "x"}); res.Code != "target_missing" { + t.Fatalf("no target = %+v", res) + } +} + +func TestDingTalkPushTokenRejected(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(400) + json.NewEncoder(w).Encode(map[string]any{"code": "invalidClientSecret"}) + })) + defer srv.Close() + ch := newDingTalkChannel() + ch.api = srv.URL + bot := domain.NotificationBot{ID: "cfg-push-bad", Channel: domain.BotChannelDingTalk, BotID: "ding-client"} + forgetDingTalkToken(bot) + if res := ch.Push(context.Background(), bot, Credential{Secret: "bad"}, OutboundMessage{ChatID: "s", Text: "x"}); res.Outcome != Rejected || res.Code != "credential_invalid" { + t.Fatalf("rejected token = %+v", res) + } + srv.Close() + if res := ch.Push(context.Background(), bot, Credential{Secret: "s"}, OutboundMessage{ChatID: "s", Text: "x"}); res.Outcome != NotDelivered || !res.Retryable { + t.Fatalf("token unreachable = %+v", res) + } +} diff --git a/internal/botinteraction/retention.go b/internal/botinteraction/retention.go new file mode 100644 index 000000000..693e784e1 --- /dev/null +++ b/internal/botinteraction/retention.go @@ -0,0 +1,92 @@ +package botinteraction + +import ( + "agentflow/internal/repo" + "context" + "log" + "os" + "strconv" + "strings" + "time" +) + +// Data retention of chat Bots (stage H). Defaults: message bodies, rendered +// notification content, write drafts and native CLI logs of Bot turns are +// removed 90 days after the record finished; the records themselves 180 +// days after. The cleanup runs hourly with the binding-request cleanup, in +// batches of 1000. +const ( + defaultBodyRetentionDays = 90 + defaultRecordRetentionDays = 180 + retentionBatch = 1000 + retentionBatches = 10 +) + +// NativeLogCleaner removes the native CLI logs of finished Bot turns. It is +// provided by the HTTP layer, which knows where each engine keeps its logs. +type NativeLogCleaner func(ctx context.Context, due []repo.BotNativeCleanup) []repo.BotNativeCleanupResult + +// RetentionPolicyFromEnv reads AGENTFLOW_BOT_BODY_RETENTION_DAYS and +// AGENTFLOW_BOT_RECORD_RETENTION_DAYS. 0 disables that horizon; invalid +// values fall back to the default. A record horizon shorter than the body +// horizon is raised to it. +func RetentionPolicyFromEnv() repo.BotRetentionPolicy { + days := func(name string, def int) int { + raw := strings.TrimSpace(os.Getenv(name)) + if raw == "" { + return def + } + n, err := strconv.Atoi(raw) + if err != nil || n < 0 || n > 36500 { + log.Printf("bot retention: %s=%q is not a number of days, using %d", name, raw, def) + return def + } + return n + } + body := days("AGENTFLOW_BOT_BODY_RETENTION_DAYS", defaultBodyRetentionDays) + record := days("AGENTFLOW_BOT_RECORD_RETENTION_DAYS", defaultRecordRetentionDays) + if body > 0 && record > 0 && record < body { + log.Printf("bot retention: record retention %d days is shorter than body retention %d days, using %d", record, body, body) + record = body + } + return repo.BotRetentionPolicy{Body: time.Duration(body) * 24 * time.Hour, Record: time.Duration(record) * 24 * time.Hour, Batch: retentionBatch, Batches: retentionBatches} +} + +// RunRetention applies the policy once: database rows first, then the native +// logs whose body horizon passed. It returns the database counts and the +// number of mappings whose logs were handled. +func (s *Service) RunRetention(ctx context.Context, now time.Time, policy repo.BotRetentionPolicy) (repo.BotRetentionResult, int, error) { + res, err := s.Store.PurgeBotRetention(ctx, now, policy) + if err != nil || policy.Body <= 0 || s.NativeLogs == nil { + return res, 0, err + } + batch := policy.Batch + if batch <= 0 { + batch = retentionBatch + } + due, err := s.Store.DueBotNativeCleanups(now.Add(-policy.Body), batch) + if err != nil || len(due) == 0 { + return res, 0, err + } + handled := 0 + for _, out := range s.NativeLogs(ctx, due) { + if err := s.Store.RecordBotNativeCleanup(out, now); err != nil { + return res, handled, err + } + handled++ + } + return res, handled, nil +} + +func (s *Service) retentionTick(ctx context.Context, now time.Time) { + if s.Retention == nil { + return + } + res, logs, err := s.RunRetention(ctx, now, *s.Retention) + if err != nil { + log.Printf("bot retention: cleanup failed: %v", err) + } + if res.Total() > 0 || logs > 0 { + log.Printf("bot retention: interaction bodies=%d interactions=%d notification bodies=%d notifications=%d drafts expired=%d draft bodies=%d drafts=%d native mappings=%d native logs handled=%d", res.InteractionBodies, res.Interactions, res.DeliveryBodies, res.Deliveries, res.DraftsExpired, res.DraftBodies, res.Drafts, res.NativeMappings, logs) + } +} diff --git a/internal/botinteraction/service.go b/internal/botinteraction/service.go index db3606ca0..0a3596fbe 100644 --- a/internal/botinteraction/service.go +++ b/internal/botinteraction/service.go @@ -22,16 +22,26 @@ type Service struct { Generate Generate Action Action URL string - mu sync.Mutex - running map[string]*connection - Done chan struct{} - start sync.Once + // ChannelKind overrides adapter selection in tests; by default the + // persisted channel of the Bot decides. + ChannelKind func(domain.NotificationBot) string + // Retention is the data retention policy applied hourly (stage H); + // nil disables the cleanup. NativeLogs removes native CLI logs. + Retention *repo.BotRetentionPolicy + NativeLogs NativeLogCleaner + // PublicURL returns the platform address for links in cards. + PublicURL func() string + mu sync.Mutex + running map[string]*connection + channels map[string]Channel + Done chan struct{} + start sync.Once } type connection struct { bot domain.NotificationBot cancel context.CancelFunc done chan struct{} - socket *socket + conn Conn } func randomID() string { @@ -46,7 +56,41 @@ func New(store *repo.Store) *Service { if endpoint == "" { endpoint = "wss://openws.work.weixin.qq.com" } - return &Service{Store: store, URL: endpoint, running: map[string]*connection{}, Done: make(chan struct{})} + policy := RetentionPolicyFromEnv() + s := &Service{Store: store, URL: endpoint, Retention: &policy, running: map[string]*connection{}, channels: map[string]Channel{}, Done: make(chan struct{})} + s.channels[ChannelWeCom] = &wecomChannel{svc: s} + s.channels[ChannelFeishu] = newFeishuChannel() + s.channels[ChannelDingTalk] = newDingTalkChannel() + return s +} + +// ConfigureWeCom injects the credential check and proactive send used by the +// WeCom channel. The notification service owns the wecom-cli provider. +func (s *Service) ConfigureWeCom(verify WeComVerifyFunc, push WeComPushFunc) { + s.mu.Lock() + defer s.mu.Unlock() + s.channels[ChannelWeCom] = &wecomChannel{svc: s, verify: verify, push: push} +} + +// RegisterChannel adds or replaces the adapter for c.Kind(). +func (s *Service) RegisterChannel(c Channel) { + s.mu.Lock() + defer s.mu.Unlock() + s.channels[c.Kind()] = c +} + +// Channel returns the adapter for a Bot, or ErrChannelNotReady. +func (s *Service) Channel(bot domain.NotificationBot) (Channel, error) { + kind := bot.ChannelKind() + if s.ChannelKind != nil { + kind = s.ChannelKind(bot) + } + s.mu.Lock() + defer s.mu.Unlock() + if c, ok := s.channels[kind]; ok { + return c, nil + } + return nil, ErrChannelNotReady } func (s *Service) Start(ctx context.Context) { s.start.Do(func() { @@ -79,6 +123,7 @@ func (s *Service) Start(ctx context.Context) { } else { nextCleanup = now.Add(time.Hour) } + s.retentionTick(ctx, now) } s.reconcile(ctx) select { @@ -104,7 +149,9 @@ func (s *Service) reconcile(ctx context.Context) { defer s.mu.Unlock() for id, c := range s.running { b, ok := wanted[id] - if !ok || !b.UpdatedAt.Equal(c.bot.UpdatedAt) { + // Only connection-relevant fields reconnect; names, models, tenant + // locking and write policy are re-read per event and per turn. + if !ok || b.ConnectionFingerprint() != c.bot.ConnectionFingerprint() { c.cancel() } select { @@ -125,14 +172,15 @@ func (s *Service) reconcile(ctx context.Context) { } func (s *Service) run(ctx context.Context, c *connection) { owner := randomID() - claimed, err := s.Store.ClaimBotConnection(c.bot.BotID, owner, time.Now().UTC()) + lease, claimed, err := s.Store.ClaimBotConnection(c.bot.LeaseKey(), owner) if err != nil || !claimed { return } - defer s.Store.ReleaseBotConnection(c.bot.BotID, owner) + defer s.Store.ReleaseBotConnection(lease) runCtx, cancel := context.WithCancel(ctx) defer cancel() - if err := s.Store.RecoverBotInteractions(c.bot.ID); err != nil { + policy := sendPolicy(c.bot) + if err := s.Store.RecoverBotInteractions(c.bot.ID, lease, policy); err != nil { return } // Renew independently of model turns; a lost lease cancels every local action. @@ -144,7 +192,7 @@ func (s *Service) run(ctx context.Context, c *connection) { case <-runCtx.Done(): return case <-ticker.C: - ok, err := s.Store.RenewBotConnection(c.bot.BotID, owner, "", "", time.Now().UTC()) + ok, err := s.Store.RenewBotConnection(lease, "", "") if err != nil || !ok { cancel() return @@ -152,33 +200,43 @@ func (s *Service) run(ctx context.Context, c *connection) { } } }() + channel, err := s.Channel(c.bot) + if err != nil { + // Keep the lease so the error stays visible until the configuration + // changes or the service stops; releasing would report "offline". + s.state(lease, "error", "该渠道尚未接入") + <-runCtx.Done() + return + } + sink := func(ctx context.Context, ev InboundEvent) (InboundAck, error) { return s.accept(ctx, c.bot, ev) } backoff := time.Second for runCtx.Err() == nil { secret, err := s.Store.BotCredential(c.bot.ID) if err != nil { - s.state(c.bot, owner, "error", "机器人凭据不可用") + s.state(lease, "error", "机器人凭据不可用") return } - var ws *socket - ws, err = dial(runCtx, s.URL, c.bot.BotID, secret, func(received *socket, f Frame) { - s.receive(runCtx, c.bot, f, received) - }) + var conn Conn + conn, err = channel.Connect(runCtx, c.bot, Credential{Secret: secret}, sink) if err == nil { s.mu.Lock() - c.socket = ws + c.conn = conn s.mu.Unlock() - s.state(c.bot, owner, "connected", "") - err = s.process(runCtx, c.bot, owner, ws) - ws.close() + s.state(lease, "connected", "") + err = s.process(runCtx, c.bot, lease, conn, policy) + conn.Close() s.mu.Lock() - c.socket = nil + c.conn = nil s.mu.Unlock() backoff = time.Second + if errors.Is(err, repo.ErrBotLeaseLost) { + return + } } if runCtx.Err() != nil { return } - s.state(c.bot, owner, "reconnecting", "连接中断,正在重连") + s.state(lease, "reconnecting", "连接中断,正在重连") timer := time.NewTimer(backoff) select { case <-runCtx.Done(): @@ -191,95 +249,8 @@ func (s *Service) run(ctx context.Context, c *connection) { } } } -func (s *Service) state(bot domain.NotificationBot, owner, state, detail string) { - _, _ = s.Store.RenewBotConnection(bot.BotID, owner, state, detail, time.Now().UTC()) -} -func normalizeIncoming(bot domain.NotificationBot, f Frame) (domain.BotInteraction, string, string, bool) { - var incoming Incoming - if json.Unmarshal(f.Body, &incoming) != nil || incoming.BotID != bot.BotID || incoming.MessageID == "" || incoming.From.UserID == "" { - return domain.BotInteraction{}, "", "", false - } - // Live WeCom callbacks wrap card parameters in template_card_event; - // also accept the flat structure used by older official SDK type definitions. - if incoming.Event.TemplateCardEvent.Key != "" { - incoming.Event.Key = incoming.Event.TemplateCardEvent.Key - } - if incoming.Event.TemplateCardEvent.TaskID != "" { - incoming.Event.TaskID = incoming.Event.TemplateCardEvent.TaskID - } - chat := incoming.ChatID - kind := incoming.ChatType - if kind == "" && f.Cmd == "aibot_event_callback" { - kind = "single" - if incoming.ChatID != "" { - kind = "group" - } - } - if kind == "single" { - chat = incoming.From.UserID - } - if chat == "" || (kind != "single" && kind != "group") || len(chat) > 191 || len(incoming.From.UserID) > 191 { - return domain.BotInteraction{}, "", "", false - } - text := incoming.Text.Content - if incoming.Type == "voice" { - text = incoming.Voice.Content - } - action, delivery := "", "" - if f.Cmd == "aibot_event_callback" { - if incoming.Event.Type != "template_card_event" { - return domain.BotInteraction{}, "", "", false - } - action, delivery = incoming.Event.Key, incoming.Event.TaskID - if prefix, _, found := strings.Cut(delivery, "_"); found { - delivery = prefix - } - if action == "" || action == "handled" || delivery == "" { - return domain.BotInteraction{}, "", "", false - } - } else if incoming.Type != "text" && incoming.Type != "voice" { - text = "[不支持的消息类型,请发送文字]" - } - text = domain.NotificationText(text, 16000) - if strings.TrimSpace(text) == "" && action == "" { - return domain.BotInteraction{}, "", "", false - } - return domain.BotInteraction{ID: repo.BotMessageID(bot.BotID, incoming.MessageID), BotID: bot.ID, SenderID: incoming.From.UserID, RequestID: f.Headers.RequestID, Content: text, Quote: domain.NotificationText(incoming.Quote.Text.Content, 8000), Action: action, DeliveryID: delivery, CardTaskID: incoming.Event.TaskID}, kind, chat, true -} -func (s *Service) receive(ctx context.Context, bot domain.NotificationBot, f Frame, ws *socket) { - item, kind, chat, ok := normalizeIncoming(bot, f) - if !ok { - if f.Cmd == "aibot_event_callback" { - var event Incoming - _ = json.Unmarshal(f.Body, &event) - log.Printf("wecom ignored event type=%s bot_matches=%t message_id=%t sender=%t chat_type=%s action=%s task_id=%t", event.Event.Type, event.BotID == bot.BotID, event.MessageID != "", event.From.UserID != "", event.ChatType, event.Event.Key, event.Event.TaskID != "") - } - return - } - // Single-chat callbacks identify the actor by internal userid, while CLI - // discovery targets use openid. Resolve only an exact, server-issued card - // task to its frozen recipient; group callbacks still require matching chatid. - if item.Action != "" && kind == "single" { - delivery, e := s.Store.NotificationDelivery(item.DeliveryID) - card, cardErr := s.Store.BotInteraction(repo.BotMessageID(bot.ID, "notification:"+item.DeliveryID)) - if e == nil && cardErr == nil && delivery.BotID == bot.ID && delivery.ProjectID == bot.ProjectID && delivery.TargetType == "single" && card.CardTaskID == item.CardTaskID { - chat = delivery.TargetID - } - } - inserted, err := s.Store.EnqueueBotInteraction(item, kind, chat) - if err != nil || !inserted { - if err != nil { - log.Printf("wecom event enqueue failed action=%s", item.Action) - } - return - } - // Card operations are direct engine controls. Reply with the actual result - // within the callback window rather than consuming it with a receipt-only update. - - if item.Action == "" { - state := ws.streamState(item.ID) - go func() { _ = ws.streamWithState(ctx, item, "已收到,正在处理…", false, state) }() - } +func (s *Service) state(lease repo.BotLease, state, detail string) { + _, _ = s.Store.RenewBotConnection(lease, state, detail) } var ErrCardActionDeferred = errors.New("等待账号确认或项目授权") @@ -296,64 +267,35 @@ func newConversationCommand(content string) bool { } return text == "/new" || text == "开启新会话" || text == "新会话" } -func (s *Service) process(ctx context.Context, bot domain.NotificationBot, owner string, ws *socket) error { +func (s *Service) process(ctx context.Context, bot domain.NotificationBot, lease repo.BotLease, conn Conn, policy repo.BotSendPolicy) error { ticker := time.NewTicker(300 * time.Millisecond) defer ticker.Stop() for { select { case <-ctx.Done(): return ctx.Err() - case <-ws.done: + case <-conn.Done(): return errors.New("连接中断") case <-ticker.C: } - item, err := s.Store.ClaimBotInteraction(bot.ID, bot.BotID, owner) + item, err := s.Store.ClaimBotInteraction(bot.ID, lease, policy) if err != nil { return err } if item == nil { continue } + // Names, models and write policy may change without reconnecting. + current, err := s.Store.NotificationBot(bot.ID) + if err != nil { + return repo.ErrBotLeaseLost + } conv, err := s.Store.BotConversation(item.ConversationID) if err != nil { return err } if item.Action == "notification" { - delivery, lookupErr := s.Store.NotificationDelivery(item.DeliveryID) - valid := lookupErr == nil && len(ActionsForDelivery(s.Store, delivery)) > 0 - if !valid { - if err = s.Store.ExpireBotCard(item.ID); err != nil { - return err - } - continue - } - var card map[string]any - if json.Unmarshal([]byte(item.Quote), &card) != nil { - return errors.New("通知卡片无效") - } - if item.Status != "ready" { - if err = s.Store.SaveBotReply(*item, "", ""); err != nil { - return err - } - } - // A task_id may not be reused by the same Bot across sent cards. - // Keep the delivery prefix for callback routing; suffix each send attempt. - taskID := item.DeliveryID + "_" + randomID()[:12] - card["task_id"] = taskID - if err = s.Store.SetBotCardTaskID(item.ID, taskID); err != nil { - return err - } - if err = ws.request(ctx, "aibot_send_msg", "", map[string]any{"chatid": conv.ChatID, "msgtype": "template_card", "template_card": card}); err != nil { - var business *ReplyError - if errors.As(err, &business) { - if err = s.Store.FailBotReply(item.ID, business.Error()); err != nil { - return err - } - continue - } - return err - } - if err = s.Store.MarkBotReplySent(item.ID); err != nil { + if err = s.processCard(ctx, item, lease, conn, conv, policy); err != nil { return err } continue @@ -367,68 +309,14 @@ func (s *Service) process(ctx context.Context, bot domain.NotificationBot, owner continue } } - if item.Status != "ready" { - reply, detail := "", "" - if item.Action == "notification_details" { - reply = item.Content - } else if time.Since(item.CreatedAt) > 10*time.Minute { - reply = "这条消息等待处理的时间较长,已停止自动执行。请重新发送当前请求。" - } else if newConversationCommand(item.Content) { - err = s.Store.NewBotConversation(*item) - reply = "已开启新会话。当前聊天将从新的上下文开始,历史记录保留。" - } else if item.Content == "[不支持的消息类型,请发送文字]" { - reply = item.Content - } else { - turnCtx, cancel := context.WithTimeout(ctx, 2*time.Minute) - var stopProgress func() - if item.Action == "" { - turnCtx, stopProgress = ws.progressTurn(turnCtx, *item) - } - if item.Action != "" { - if s.Action == nil { - err = errors.New("操作服务未就绪") - } else { - reply, err = s.Action(turnCtx, bot, *item, conv) - } - } else { - history, historyErr := s.Store.BotHistory(conv.SessionID) - err = historyErr - if err == nil { - if s.Generate == nil { - err = errors.New("助手服务未就绪") - } else { - reply, err = s.Generate(turnCtx, bot, conv, history, *item) - } - } - } - if stopProgress != nil { - stopProgress() - } - cancel() - } - if errors.Is(err, ErrCardActionDeferred) { - detail = ErrCardActionDeferred.Error() - } else if err != nil { - detail = "本次处理失败" - reply = "本次处理未完成,请稍后重试或到平台查看状态。" - if errors.Is(err, context.DeadlineExceeded) { - detail = "模型回复提交超时" - reply = "本次处理已超时,未能提交最终回复。请重新发送请求;若涉及修改操作,请先在平台核对状态,避免重复执行。" - } - } - if strings.TrimSpace(reply) == "" { - detail = "助手未提交回复" - reply = "本次没有得到有效回复,请稍后重试。" - } - reply = domain.NotificationText(reply, 16000) - if reply, detail, err = s.saveReply(*item, reply, detail); err != nil { + if item.Status == domain.BotRunning { + if err = s.runTurn(ctx, current, item, lease, conn, conv); err != nil { return err } - item.Reply = reply - item.LastError = detail } - if item.Action != "" && item.Action != "notification_details" && item.CardTaskID != "" && item.LastError != ErrCardActionDeferred.Error() { - if updateErr := ws.updateActionCard(ctx, *item, item.Reply, true); updateErr != nil { + cards, hasCards := conn.(cardConn) + if hasCards && item.Action != "" && item.Action != "notification_details" && item.CardTaskID != "" && item.LastError != ErrCardActionDeferred.Error() { + if updateErr := cards.updateActionCard(ctx, *item, item.Reply, true); updateErr != nil { code := 0 var business *ReplyError if errors.As(updateErr, &business) { @@ -437,27 +325,120 @@ func (s *Service) process(ctx context.Context, bot domain.NotificationBot, owner log.Printf("wecom action card update failed action=%s code=%d", item.Action, code) } } - if item.Action == "" { - err = ws.stream(ctx, *item, item.Reply, true) - if err != nil { - err = ws.send(ctx, conv.ChatID, item.Reply) + err = s.deliver(ctx, item.ID, lease, policy, func(ctx context.Context, sending domain.BotInteraction) SendResult { + if sending.SendRoute == repo.BotSendRoutePush { + // An expired DingTalk reply the administrator switched to + // a proactive private message to the original sender. + return s.Push(ctx, current, OutboundMessage{InteractionID: sending.ID, ChatID: sending.SenderID, ChatType: "single", Text: sending.Reply, SendKey: sending.SendKey}) } - } else { - err = ws.send(ctx, conv.ChatID, item.Reply) - } - ws.streams.Delete(item.ID) + out := OutboundMessage{InteractionID: sending.ID, Action: sending.Action, ChatID: conv.ChatID, ChatType: conv.ChatType, Text: sending.Reply, SendKey: sending.SendKey, Route: s.replyRoute(sending)} + if sending.Action == "" && current.ChannelKind() == domain.BotChannelFeishu { + // A reply with write drafts becomes a confirmation card. + out.Card = s.DraftCardFor(sending, s.publicURL()) + } + return conn.Reply(ctx, out) + }) if err != nil { - var business *ReplyError - if errors.As(err, &business) { - if err = s.Store.FailBotReply(item.ID, business.Error()); err != nil { - return err + return err + } + } +} + +// runTurn executes a claimed running item and stores its reply. +func (s *Service) runTurn(ctx context.Context, bot domain.NotificationBot, item *domain.BotInteraction, lease repo.BotLease, conn Conn, conv domain.BotConversation) error { + reply, detail := "", "" + var err error + if item.Action == "notification_details" { + reply = item.Content + } else if time.Since(item.CreatedAt) > 10*time.Minute { + reply = "这条消息等待处理的时间较长,已停止自动执行。请重新发送当前请求。" + } else if item.Action == actionSystemNotice { + reply = item.Content + } else if newConversationCommand(item.Content) { + if err = s.Store.NewBotConversation(*item, lease); errors.Is(err, repo.ErrBotLeaseLost) { + return err + } + reply = "已开启新会话。当前聊天将从新的上下文开始,历史记录保留。" + } else if item.Content == unsupportedMessageText { + reply = item.Content + } else { + turnCtx, cancel := context.WithTimeout(ctx, 2*time.Minute) + var stopProgress func() + if progress, ok := conn.(progressConn); ok && item.Action == "" { + turnCtx, stopProgress = progress.progressTurn(turnCtx, *item) + } + if item.Action != "" { + if s.Action == nil { + err = errors.New("操作服务未就绪") + } else { + reply, err = s.Action(turnCtx, bot, *item, conv) + } + } else { + history, historyErr := s.Store.BotHistory(conv.SessionID) + err = historyErr + if err == nil { + if s.Generate == nil { + err = errors.New("助手服务未就绪") + } else { + reply, err = s.Generate(turnCtx, bot, conv, history, *item) } - continue } - return err } - if err = s.Store.MarkBotReplySent(item.ID); err != nil { + if stopProgress != nil { + stopProgress() + } + cancel() + } + if errors.Is(err, ErrCardActionDeferred) { + detail = ErrCardActionDeferred.Error() + } else if err != nil { + detail = "本次处理失败" + reply = "本次处理未完成,请稍后重试或到平台查看状态。" + if errors.Is(err, context.DeadlineExceeded) { + detail = "模型回复提交超时" + reply = "本次处理已超时,未能提交最终回复。请重新发送请求;若涉及修改操作,请先在平台核对状态,避免重复执行。" + } + } + if strings.TrimSpace(reply) == "" { + detail = "助手未提交回复" + reply = "本次没有得到有效回复,请稍后重试。" + } + reply = domain.NotificationText(reply, 16000) + if reply, detail, err = s.saveReply(*item, lease, reply, detail); err != nil { + return err + } + item.Reply, item.LastError, item.Status = reply, detail, domain.BotReady + return nil +} + +// processCard sends a WeCom notification card through the delivery state +// machine, or expires it when the node no longer waits. +func (s *Service) processCard(ctx context.Context, item *domain.BotInteraction, lease repo.BotLease, conn Conn, conv domain.BotConversation, policy repo.BotSendPolicy) error { + delivery, lookupErr := s.Store.NotificationDelivery(item.DeliveryID) + if lookupErr != nil || len(ActionsForDelivery(s.Store, delivery)) == 0 { + return s.Store.ExpireBotCard(item.ID) + } + var card map[string]any + if json.Unmarshal([]byte(item.Quote), &card) != nil { + return errors.New("通知卡片无效") + } + if item.Status != domain.BotReady { + if err := s.Store.SaveBotReply(*item, lease, "", ""); err != nil { return err } } + cards, ok := conn.(cardConn) + if !ok { + return s.Store.FailBotReply(item.ID, lease, "当前渠道暂不支持交互卡片") + } + // A task_id may not be reused by the same Bot across sent cards. + // Keep the delivery prefix for callback routing; suffix each send attempt. + taskID := item.DeliveryID + "_" + randomID()[:12] + card["task_id"] = taskID + if err := s.Store.SetBotCardTaskID(item.ID, lease, taskID); err != nil { + return err + } + return s.deliver(ctx, item.ID, lease, policy, func(ctx context.Context, _ domain.BotInteraction) SendResult { + return cards.sendCard(ctx, conv.ChatID, card) + }) } diff --git a/internal/botinteraction/socket.go b/internal/botinteraction/socket.go index 221c515c1..6c38693cf 100644 --- a/internal/botinteraction/socket.go +++ b/internal/botinteraction/socket.go @@ -16,6 +16,26 @@ import ( type ReplyError struct{ Code int } +// sendPhase records how far a request got, which decides whether delivery is +// provably absent (before write) or unknown (written, receipt not observed). +type sendPhase int + +const ( + phaseBeforeWrite sendPhase = iota + phaseWrite + phaseAwaitAck +) + +// sendError keeps the original user-facing message and unwraps to the cause, +// so callers using errors.Is(err, context.Canceled) keep working. +type sendError struct { + phase sendPhase + err error +} + +func (e *sendError) Error() string { return e.err.Error() } +func (e *sendError) Unwrap() error { return e.err } + func (e *ReplyError) Error() string { return "企业微信拒绝了这次回复,请检查接收会话和 Bot 授权" } @@ -149,7 +169,7 @@ func dial(ctx context.Context, url, botID, secret string, onFrame func(*socket, func (s *socket) close() { s.cancel(); s.conn.Close() } func (s *socket) request(ctx context.Context, cmd, id string, body any) error { if ctx.Err() != nil { - return ctx.Err() + return &sendError{phase: phaseBeforeWrite, err: ctx.Err()} } if id == "" { id = randomID() @@ -158,7 +178,7 @@ func (s *socket) request(ctx context.Context, cmd, id string, body any) error { s.mu.Lock() if _, exists := s.pending[id]; exists { s.mu.Unlock() - return errors.New("相同回复仍在等待回执") + return &sendError{phase: phaseBeforeWrite, err: errors.New("相同回复仍在等待回执")} } s.pending[id] = ack s.mu.Unlock() @@ -172,7 +192,8 @@ func (s *socket) request(ctx context.Context, cmd, id string, body any) error { err := s.conn.WriteJSON(payload) s.writeMu.Unlock() if err != nil { - return errors.New("企业微信回复连接中断") + // A failed write may still have flushed part or all of the frame. + return &sendError{phase: phaseWrite, err: errors.New("企业微信回复连接中断")} } timer := time.NewTimer(10 * time.Second) defer timer.Stop() @@ -183,11 +204,11 @@ func (s *socket) request(ctx context.Context, cmd, id string, body any) error { } return nil case <-timer.C: - return errors.New("企业微信回复回执超时") + return &sendError{phase: phaseAwaitAck, err: errors.New("企业微信回复回执超时")} case <-ctx.Done(): - return ctx.Err() + return &sendError{phase: phaseAwaitAck, err: ctx.Err()} case <-s.done: - return errors.New("企业微信连接已关闭") + return &sendError{phase: phaseAwaitAck, err: errors.New("企业微信连接已关闭")} } } func (s *socket) send(ctx context.Context, chatID, text string) error { diff --git a/internal/credcrypt/keyring.go b/internal/credcrypt/keyring.go new file mode 100644 index 000000000..438054ab1 --- /dev/null +++ b/internal/credcrypt/keyring.go @@ -0,0 +1,155 @@ +// Package credcrypt seals Bot credentials and authorization-bearing reply +// routes with AES-256-GCM. Keys come from AGENTFLOW_CREDENTIAL_KEYS: +// +// kid:base64(32 bytes)[,kid:base64(32 bytes)...] +// +// The first key encrypts new values; every listed key can decrypt. Sealed +// values are "lf1::". Additional data binds a +// value to its owner (configuration, channel, purpose) so a ciphertext copied +// to another row fails to open. +package credcrypt + +import ( + "crypto/aes" + "crypto/cipher" + "crypto/rand" + "encoding/base64" + "errors" + "fmt" + "os" + "regexp" + "strings" +) + +const ( + // EnvKeys is the environment variable holding the keyring. + EnvKeys = "AGENTFLOW_CREDENTIAL_KEYS" + prefix = "lf1:" +) + +var ( + // ErrKeyMissing means no key is configured for writing, or the key named + // by a sealed value is not in the keyring. + ErrKeyMissing = errors.New("credential_key_missing") + // ErrDecrypt means a sealed value failed authentication. + ErrDecrypt = errors.New("credential_decrypt_failed") + + kidPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{1,32}$`) +) + +// Keyring holds the active write key and all decryption keys. +type Keyring struct { + current string + keys map[string]cipher.AEAD +} + +// Parse builds a keyring from the environment format. Empty input returns a +// nil keyring, which can still read legacy plaintext but cannot seal. +func Parse(raw string) (*Keyring, error) { + raw = strings.TrimSpace(raw) + if raw == "" { + return nil, nil + } + k := &Keyring{keys: map[string]cipher.AEAD{}} + for i, part := range strings.Split(raw, ",") { + kid, encoded, ok := strings.Cut(strings.TrimSpace(part), ":") + if !ok || !kidPattern.MatchString(kid) { + return nil, fmt.Errorf("%s: entry %d must be kid:base64key with kid [A-Za-z0-9_-]{1,32}", EnvKeys, i+1) + } + if _, dup := k.keys[kid]; dup { + return nil, fmt.Errorf("%s: duplicate kid %q", EnvKeys, kid) + } + key, err := base64.StdEncoding.DecodeString(strings.TrimSpace(encoded)) + if err != nil || len(key) != 32 { + return nil, fmt.Errorf("%s: key %q must be base64 of exactly 32 bytes", EnvKeys, kid) + } + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + aead, err := cipher.NewGCM(block) + if err != nil { + return nil, err + } + k.keys[kid] = aead + if i == 0 { + k.current = kid + } + } + return k, nil +} + +// FromEnv parses AGENTFLOW_CREDENTIAL_KEYS. +func FromEnv() (*Keyring, error) { return Parse(os.Getenv(EnvKeys)) } + +// CurrentKID returns the write key id, or "" for a nil keyring. +func (k *Keyring) CurrentKID() string { + if k == nil { + return "" + } + return k.current +} + +// IsSealed reports whether a stored value uses the sealed format. Values with +// the prefix must always be opened as ciphertext, never treated as plaintext. +func IsSealed(value string) bool { return strings.HasPrefix(value, prefix) } + +// KID returns the key id of a sealed value, or "" when it is not sealed. +func KID(value string) string { + if !IsSealed(value) { + return "" + } + kid, _, _ := strings.Cut(strings.TrimPrefix(value, prefix), ":") + return kid +} + +// Seal encrypts plaintext with the current key and binds it to aad. +func (k *Keyring) Seal(plaintext []byte, aad string) (string, error) { + if k == nil || k.current == "" { + return "", ErrKeyMissing + } + aead := k.keys[k.current] + nonce := make([]byte, aead.NonceSize()) + if _, err := rand.Read(nonce); err != nil { + return "", err + } + sealed := aead.Seal(nonce, nonce, plaintext, []byte(aad)) + return prefix + k.current + ":" + base64.StdEncoding.EncodeToString(sealed), nil +} + +// Open decrypts a sealed value. It fails closed on unknown keys, malformed +// values and authentication failures. +func (k *Keyring) Open(value, aad string) ([]byte, error) { + if !IsSealed(value) { + return nil, ErrDecrypt + } + kid, encoded, ok := strings.Cut(strings.TrimPrefix(value, prefix), ":") + if !ok { + return nil, ErrDecrypt + } + if k == nil { + return nil, ErrKeyMissing + } + aead, found := k.keys[kid] + if !found { + return nil, ErrKeyMissing + } + raw, err := base64.StdEncoding.DecodeString(encoded) + if err != nil || len(raw) < aead.NonceSize()+aead.Overhead() { + return nil, ErrDecrypt + } + plain, err := aead.Open(nil, raw[:aead.NonceSize()], raw[aead.NonceSize():], []byte(aad)) + if err != nil { + return nil, ErrDecrypt + } + return plain, nil +} + +// NeedsRewrap reports whether a stored value should be re-encrypted: legacy +// plaintext, or a sealed value under a key other than the current one. +func (k *Keyring) NeedsRewrap(value string) bool { + if !IsSealed(value) { + return value != "" + } + return KID(value) != k.CurrentKID() +} diff --git a/internal/credcrypt/keyring_test.go b/internal/credcrypt/keyring_test.go new file mode 100644 index 000000000..8d2a63292 --- /dev/null +++ b/internal/credcrypt/keyring_test.go @@ -0,0 +1,90 @@ +package credcrypt + +import ( + "bytes" + "encoding/base64" + "errors" + "strings" + "testing" +) + +func key(b byte) string { return base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{b}, 32)) } + +func TestParseValidatesFormat(t *testing.T) { + if k, err := Parse(" "); k != nil || err != nil { + t.Fatalf("empty = %v %v", k, err) + } + for _, bad := range []string{"nokey", "k1:" + base64.StdEncoding.EncodeToString([]byte("short")), "bad kid!:" + key(1), "k1:" + key(1) + ",k1:" + key(2), "k1:%%%"} { + if _, err := Parse(bad); err == nil { + t.Errorf("accepted %q", bad) + } + } + k, err := Parse("new:" + key(2) + ", old:" + key(1)) + if err != nil || k.CurrentKID() != "new" { + t.Fatalf("parse = %v %v", k, err) + } +} + +func TestSealOpenRoundTripAndBinding(t *testing.T) { + k, _ := Parse("k1:" + key(1)) + sealed, err := k.Seal([]byte(`{"secret":"s3cr3t"}`), "bot-1|feishu") + if err != nil || !strings.HasPrefix(sealed, "lf1:k1:") || strings.Contains(sealed, "s3cr3t") { + t.Fatalf("seal = %q %v", sealed, err) + } + again, _ := k.Seal([]byte(`{"secret":"s3cr3t"}`), "bot-1|feishu") + if again == sealed { + t.Fatal("nonce reused") + } + plain, err := k.Open(sealed, "bot-1|feishu") + if err != nil || string(plain) != `{"secret":"s3cr3t"}` { + t.Fatalf("open = %q %v", plain, err) + } + if _, err := k.Open(sealed, "bot-2|feishu"); !errors.Is(err, ErrDecrypt) { + t.Fatalf("other owner opened: %v", err) + } + tampered := sealed[:len(sealed)-4] + "AAAA" + if _, err := k.Open(tampered, "bot-1|feishu"); !errors.Is(err, ErrDecrypt) { + t.Fatalf("tampered opened: %v", err) + } + if _, err := k.Open("lf1:k1:not-base64", "bot-1|feishu"); !errors.Is(err, ErrDecrypt) { + t.Fatalf("malformed opened: %v", err) + } + if _, err := k.Open("plain", "x"); !errors.Is(err, ErrDecrypt) { + t.Fatalf("plaintext opened as sealed: %v", err) + } +} + +func TestRotationAndMissingKeys(t *testing.T) { + old, _ := Parse("k1:" + key(1)) + sealed, _ := old.Seal([]byte("v"), "a") + rotated, _ := Parse("k2:" + key(2) + ",k1:" + key(1)) + if plain, err := rotated.Open(sealed, "a"); err != nil || string(plain) != "v" { + t.Fatalf("old key not readable after rotation: %q %v", plain, err) + } + if !rotated.NeedsRewrap(sealed) || rotated.NeedsRewrap(mustSeal(t, rotated, "a")) || !rotated.NeedsRewrap("legacy") || rotated.NeedsRewrap("") { + t.Fatal("rewrap classification") + } + dropped, _ := Parse("k2:" + key(2)) + if _, err := dropped.Open(sealed, "a"); !errors.Is(err, ErrKeyMissing) { + t.Fatalf("missing kid = %v", err) + } + var none *Keyring + if _, err := none.Seal([]byte("v"), "a"); !errors.Is(err, ErrKeyMissing) { + t.Fatalf("nil seal = %v", err) + } + if _, err := none.Open(sealed, "a"); !errors.Is(err, ErrKeyMissing) { + t.Fatalf("nil open = %v", err) + } + if KID(sealed) != "k1" || KID("plain") != "" || !IsSealed(sealed) { + t.Fatal("kid helpers") + } +} + +func mustSeal(t *testing.T, k *Keyring, aad string) string { + t.Helper() + v, err := k.Seal([]byte("x"), aad) + if err != nil { + t.Fatal(err) + } + return v +} diff --git a/internal/domain/bot_identity.go b/internal/domain/bot_identity.go index 29713e401..b5d6357a2 100644 --- a/internal/domain/bot_identity.go +++ b/internal/domain/bot_identity.go @@ -5,20 +5,61 @@ import "time" // BotUserBinding is scoped to the external Bot ID; do not assume user IDs // obtained from different enterprises or Bot namespaces are interchangeable. type BotUserBinding struct { - ID string `json:"id" gorm:"primaryKey;size:64"` - BotID string `json:"botId" gorm:"size:191"` - SenderID string `json:"senderId" gorm:"size:191"` - UserID int64 `json:"userId" gorm:"index"` - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` + ID string `json:"id" gorm:"primaryKey;size:64"` + // BotID is the identity scope (NotificationBot.IdentityScope): the + // external WeCom Bot ID, or the configuration ID for other channels. + BotID string `json:"botId" gorm:"size:191;index"` + // ConfigID is the owning configuration; required for new channels and + // backfilled for WeCom by migration 037 where ownership is unambiguous. + ConfigID string `json:"configId,omitempty" gorm:"size:191;index"` + Channel string `json:"channel,omitempty" gorm:"size:16"` + BotName string `json:"botName,omitempty" gorm:"-"` + // Link issuance rate limit: GuideCount links within the window that + // started at GuideWindowStart; LastLinkAt limits explicit requests. + GuideWindowStart *time.Time `json:"-"` + GuideCount int `json:"-" gorm:"not null;default:0"` + LastLinkAt *time.Time `json:"-"` + SenderID string `json:"senderId" gorm:"size:191"` + // TargetVerifiedAt records a successful trial proactive send to this + // sender (Feishu/DingTalk notification target). Cleared on revocation. + TargetVerifiedAt *time.Time `json:"targetVerifiedAt,omitempty"` + UserID int64 `json:"userId" gorm:"index"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` } type BotBindingChallenge struct { - ID string `json:"-" gorm:"primaryKey;size:64"` - BindingID string `json:"-" gorm:"index;size:64"` - UserID int64 `json:"-"` - CodeHash string `json:"-" gorm:"size:64"` - Status string `json:"-" gorm:"size:16"` - Attempts int `json:"-"` - ExpiresAt time.Time `json:"-" gorm:"index"` - CreatedAt time.Time `json:"-"` + ID string `json:"-" gorm:"primaryKey;size:64"` + BindingID string `json:"-" gorm:"index;size:64"` + ConfigID string `json:"-" gorm:"size:191;index"` + Channel string `json:"-" gorm:"size:16"` + SenderMask string `json:"-" gorm:"size:64"` + UserID int64 `json:"-"` + CodeHash string `json:"-" gorm:"size:64"` + Status string `json:"-" gorm:"size:16"` + Attempts int `json:"-"` + ExpiresAt time.Time `json:"-" gorm:"index"` + CreatedAt time.Time `json:"-"` +} + +// BotPendingDraft is a frozen set of assistant write drafts from a WeCom +// Bot in the confirm write mode, awaiting the bound user's confirmation on +// the platform page. Drafts is the JSON the assistant produced. +type BotPendingDraft struct { + ID string `json:"id"` + ConfigID string `json:"configId"` + IdentityScope string `json:"-"` + SenderID string `json:"-"` + ProjectID string `json:"projectId"` + UserID int64 `json:"-"` + InteractionID string `json:"-"` + Summary string `json:"summary"` + Drafts string `json:"-"` + Status string `json:"status"` + ExecKey string `json:"-"` + Result string `json:"result,omitempty"` + Reason string `json:"reason,omitempty"` + ExpiresAt time.Time `json:"expiresAt"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` + ExecutedAt *time.Time `json:"executedAt,omitempty"` } diff --git a/internal/domain/bot_interaction.go b/internal/domain/bot_interaction.go index fca3d9d5e..b68417598 100644 --- a/internal/domain/bot_interaction.go +++ b/internal/domain/bot_interaction.go @@ -2,14 +2,38 @@ package domain import "time" +// BotConnection is the receiving lease of one application. BotID holds the +// lease key (NotificationBot.LeaseKey): the external Bot ID for WeCom, the +// receive slot for other channels. LeaseUntilUS and the epoch are written and +// compared on the database clock; LeaseUntil is a display copy only. type BotConnection struct { - BotID string `json:"botId" gorm:"primaryKey;size:191"` - Owner string `json:"-" gorm:"size:64"` - LeaseUntil time.Time `json:"-"` - State string `json:"state" gorm:"size:32"` - LastError string `json:"lastError,omitempty" gorm:"size:255"` - UpdatedAt time.Time `json:"updatedAt"` + BotID string `json:"botId" gorm:"primaryKey;size:191"` + Owner string `json:"-" gorm:"size:64"` + LeaseUntil time.Time `json:"-"` + LeaseUntilUS int64 `json:"-" gorm:"not null;default:0"` + // Epoch increments whenever a different owner takes the lease. + Epoch int64 `json:"epoch" gorm:"not null;default:0"` + OwnerChanges int64 `json:"ownerChanges" gorm:"not null;default:0"` + LastEventAt *time.Time `json:"lastEventAt,omitempty"` + AuthFailures int64 `json:"authFailures" gorm:"not null;default:0"` + IgnoredGroupMessages int64 `json:"ignoredGroupMessages" gorm:"not null;default:0"` + State string `json:"state" gorm:"size:32"` + LastError string `json:"lastError,omitempty" gorm:"size:255"` + UpdatedAt time.Time `json:"updatedAt"` } + +// Interaction statuses (bot_interactions.status). +const ( + BotQueued = "queued" + BotRunning = "running" + BotReady = "ready" + BotSending = "sending" + BotReplied = "replied" + BotReplyFailed = "reply_failed" + BotReplyUnknown = "reply_unknown" + BotCancelled = "cancelled" +) + type BotConversation struct { ID string `json:"id" gorm:"primaryKey;size:64"` BotID string `json:"botId" gorm:"index;size:191"` @@ -20,7 +44,7 @@ type BotConversation struct { } type BotInteraction struct { ID string `json:"id" gorm:"primaryKey;size:64"` - BotID string `json:"botId" gorm:"index:idx_bot_queue,priority:1;size:191"` + BotID string `json:"botId" gorm:"index:idx_bot_queue,priority:1;index:idx_bot_send_due,priority:1;size:191"` ConversationID string `json:"conversationId" gorm:"index;size:64"` SessionID string `json:"sessionId" gorm:"index;size:64"` SenderID string `json:"senderId" gorm:"size:191"` @@ -30,9 +54,53 @@ type BotInteraction struct { Action string `json:"action,omitempty" gorm:"size:32"` CardTaskID string `json:"cardTaskId,omitempty" gorm:"size:128"` DeliveryID string `json:"deliveryId,omitempty" gorm:"size:64"` - Status string `json:"status" gorm:"index:idx_bot_queue,priority:2;size:32"` + Status string `json:"status" gorm:"index:idx_bot_queue,priority:2;index:idx_bot_send_due,priority:2;size:32"` Reply string `json:"reply,omitempty" gorm:"type:text"` LastError string `json:"lastError,omitempty" gorm:"size:255"` CreatedAt time.Time `json:"createdAt" gorm:"index:idx_bot_queue,priority:3"` UpdatedAt time.Time `json:"updatedAt"` + + // ProjectID snapshots the configuration's project for record access + // after the Bot is deleted. + ProjectID string `json:"projectId,omitempty" gorm:"size:191;index"` + // Lease fencing: owner and epoch of the connection that claimed the item. + LeaseOwner string `json:"-" gorm:"size:64"` + LeaseEpoch int64 `json:"-" gorm:"not null;default:0"` + ConfigVersion int64 `json:"configVersion,omitempty" gorm:"not null;default:0"` + // Platform evidence. + PlatformEventID string `json:"platformEventId,omitempty" gorm:"size:191"` + PlatformMessageID string `json:"platformMessageId,omitempty" gorm:"size:191"` + MsgType string `json:"msgType,omitempty" gorm:"size:32"` + ContentLength int `json:"contentLength,omitempty" gorm:"not null;default:0"` + // ReplyRoute is channel routing data (sealed when it carries authority); + // RouteExpiresAtUS is 0 when the route does not expire. + ReplyRoute string `json:"-" gorm:"type:text"` + RouteExpiresAtUS int64 `json:"-" gorm:"not null;default:0"` + // Delivery state machine. SendKey is fixed per generation; retries reuse + // it. Times are database-clock epoch microseconds. + SendKey string `json:"-" gorm:"size:64"` + DeliveryGeneration int64 `json:"deliveryGeneration" gorm:"not null;default:0"` + SendStartedAtUS int64 `json:"-" gorm:"not null;default:0"` + SendAttempts int `json:"sendAttempts" gorm:"not null;default:0"` + UndeliveredRetries int `json:"-" gorm:"not null;default:0"` + NextSendAtUS int64 `json:"-" gorm:"index:idx_bot_send_due,priority:3;not null;default:0"` + SendLeaseUntilUS int64 `json:"-" gorm:"not null;default:0"` + // Manual handling and audit. + ResentBy int64 `json:"resentBy,omitempty" gorm:"not null;default:0"` + ConfirmedBy int64 `json:"confirmedBy,omitempty" gorm:"not null;default:0"` + ConfirmedAt *time.Time `json:"confirmedAt,omitempty"` + ReceiptSource string `json:"receiptSource,omitempty" gorm:"size:16"` + TerminalReason string `json:"terminalReason,omitempty" gorm:"size:64"` + // SendRoute is empty for replies in the original conversation and + // "push" after an administrator switched an expired DingTalk reply to + // a proactive private message (stage G). + SendRoute string `json:"sendRoute,omitempty" gorm:"size:16"` + // PushResendAvailable is computed for the records page: the reply + // expired and the original sender is a verified push target. + PushResendAvailable bool `json:"pushResendAvailable,omitempty" gorm:"-"` + LastActionKey string `json:"-" gorm:"size:64"` + // BodyPurgedAt is set when the retention cleanup removed the message + // and reply text (stage H); the row stays until the record retention + // ends and can no longer be sent again. + BodyPurgedAt *time.Time `json:"bodyPurgedAt,omitempty"` } diff --git a/internal/domain/notification.go b/internal/domain/notification.go index 1c85ae648..164a6876a 100644 --- a/internal/domain/notification.go +++ b/internal/domain/notification.go @@ -1,19 +1,32 @@ package domain -import "time" +import ( + "crypto/sha256" + "encoding/hex" + "strconv" + "strings" + "time" +) type BotChat struct { ID string `json:"id"` Name string `json:"name"` Type string `json:"type"` LastMessageTime string `json:"lastMessageTime,omitempty"` + // VerifiedAt is set for Feishu/DingTalk targets after a trial + // proactive send succeeded (target_verified_at of the bound sender). + VerifiedAt *time.Time `json:"verifiedAt,omitempty"` } type NotificationBot struct { - ID string `json:"id"` - ProjectID string `json:"projectId"` - OwnerID int64 `json:"ownerId"` - Model string `json:"model,omitempty"` - Name string `json:"name"` + ID string `json:"id"` + ProjectID string `json:"projectId"` + OwnerID int64 `json:"ownerId"` + Model string `json:"model,omitempty"` + Name string `json:"name"` + // Channel is wecom, feishu or dingtalk. Empty means a legacy WeCom record. + Channel string `json:"channel,omitempty"` + // BotID is the external application identity: the WeCom Bot ID, Feishu + // App ID or DingTalk Client ID. BotID string `json:"botId"` InteractionEnabled bool `json:"interactionEnabled"` Enabled bool `json:"enabled"` @@ -23,7 +36,126 @@ type NotificationBot struct { LastCheckedAt *time.Time `json:"lastCheckedAt,omitempty"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` + + // WriteApproval is readonly, auto or confirm (WeCom only). Empty means a legacy WeCom record, + // which keeps the automatic execution it had before channels existed. + WriteApproval string `json:"writeApproval,omitempty"` + // CardTemplateID is the DingTalk interactive card template used for + // action cards of waiting or failed nodes. Empty sends plain text. + CardTemplateID string `json:"cardTemplateId,omitempty"` + // ConfigVersion increments on every saved change; CredentialVersion only + // when the secret changes. Both mirror physical columns. + ConfigVersion int64 `json:"configVersion"` + CredentialVersion int64 `json:"credentialVersion"` + // CredentialFormat reports whether the stored secret is still legacy + // plaintext ("plaintext") or sealed ("encrypted"). Filled on read. + CredentialFormat string `json:"credentialFormat,omitempty"` + + // Enterprise scope (Feishu tenant_key, DingTalk CorpId). TenantState is + // awaiting_confirmation, locked or conflict; empty for WeCom. + TenantKey string `json:"tenantKey,omitempty"` + TenantName string `json:"tenantName,omitempty"` + TenantState string `json:"tenantState,omitempty"` + TenantCandidate *TenantCandidate `json:"tenantCandidate,omitempty"` + TenantConfirmedBy int64 `json:"tenantConfirmedBy,omitempty"` + TenantConfirmedAt *time.Time `json:"tenantConfirmedAt,omitempty"` + // TenantExpected is the enterprise scope an administrator entered + // (DingTalk CorpId). The first trusted event that matches it locks the + // scope automatically; a different value moves the state to conflict. + TenantExpected string `json:"tenantExpected,omitempty"` + TenantExpectedBy int64 `json:"tenantExpectedBy,omitempty"` +} + +// TenantCandidate is the enterprise scope observed on the first trusted +// event of an authenticated connection, awaiting administrator confirmation. +type TenantCandidate struct { + TenantKey string `json:"tenantKey"` + EventID string `json:"eventId,omitempty"` + ReceivedAt time.Time `json:"receivedAt"` + CredentialVersion int64 `json:"credentialVersion"` + Conflict string `json:"conflict,omitempty"` +} + +const ( + BotChannelWeCom = "wecom" + BotChannelFeishu = "feishu" + BotChannelDingTalk = "dingtalk" + + BotWriteReadOnly = "readonly" + BotWriteAuto = "auto" + // BotWriteConfirm (WeCom, stage G): write drafts are confirmed by the + // bound user on the platform page before they run. + BotWriteConfirm = "confirm" + + BotTenantAwaiting = "awaiting_confirmation" + BotTenantLocked = "locked" + BotTenantConflict = "conflict" +) + +// ChannelKind returns the normalized channel; legacy records are WeCom. +func (b NotificationBot) ChannelKind() string { + switch strings.TrimSpace(b.Channel) { + case BotChannelFeishu: + return BotChannelFeishu + case BotChannelDingTalk: + return BotChannelDingTalk + default: + return BotChannelWeCom + } } + +// IdentityScope namespaces platform identities (bindings, message +// de-duplication). WeCom keeps its historical external Bot ID; new channels +// use the immutable configuration ID because their user IDs are app-scoped. +func (b NotificationBot) IdentityScope() string { + if b.ChannelKind() == BotChannelWeCom { + return b.BotID + } + return b.ID +} + +// ReceiveSlot identifies one receiving application within a deployment. +// Only enabled interactive configurations occupy it. +func (b NotificationBot) ReceiveSlot() string { + sum := sha256.Sum256([]byte(b.BotID)) + return b.ChannelKind() + ":" + hex.EncodeToString(sum[:]) +} + +// LeaseKey is the bot_connections.bot_id of the receiving connection. WeCom +// keeps the external Bot ID so existing rows and running peers stay valid. +func (b NotificationBot) LeaseKey() string { + if b.ChannelKind() == BotChannelWeCom { + return b.BotID + } + return b.ReceiveSlot() +} + +// EffectiveWriteApproval maps legacy WeCom records without the field to the +// automatic execution they always had. +func (b NotificationBot) EffectiveWriteApproval() string { + switch b.WriteApproval { + case BotWriteReadOnly, BotWriteAuto, BotWriteConfirm: + return b.WriteApproval + } + if b.ChannelKind() == BotChannelWeCom { + return BotWriteAuto + } + return BotWriteReadOnly +} + +// ConnectionFingerprint covers only the fields that require reconnecting. +// Names, models, tenant locking and write policy are read per event/turn. +func (b NotificationBot) ConnectionFingerprint() string { + return strings.Join([]string{b.ChannelKind(), b.BotID, strconv.FormatInt(b.CredentialVersion, 10), boolFlag(b.Enabled), boolFlag(b.InteractionEnabled)}, "|") +} + +func boolFlag(v bool) string { + if v { + return "1" + } + return "0" +} + type NotificationBinding struct { BotID string `json:"botId"` Events []string `json:"events"` // succeeded, failed, waiting @@ -73,6 +205,18 @@ type NotificationDelivery struct { LastError string `json:"lastError,omitempty" gorm:"size:255"` CreatedAt time.Time `json:"createdAt"` SentAt *time.Time `json:"sentAt,omitempty"` + + // Proactive sends of Feishu and DingTalk (stage G). SendKey is the + // persisted idempotency key of the current generation (Feishu uuid); + // SendStartedAtUS is the database time the first attempt of this + // generation started, 0 while nothing may have been sent. + SendKey string `json:"-" gorm:"size:64"` + DeliveryGeneration int `json:"deliveryGeneration,omitempty" gorm:"not null;default:0"` + SendStartedAtUS int64 `json:"-" gorm:"not null;default:0"` + PlatformMessageID string `json:"-" gorm:"size:191"` + ConfirmedBy int64 `json:"confirmedBy,omitempty"` + ConfirmedAt *time.Time `json:"confirmedAt,omitempty"` + ReceiptSource string `json:"receiptSource,omitempty" gorm:"size:16"` } func (NotificationDelivery) TableName() string { return "notification_deliveries" } diff --git a/internal/httpapi/agent_tool_mcp.go b/internal/httpapi/agent_tool_mcp.go index 45985a97d..8f5686a2e 100644 --- a/internal/httpapi/agent_tool_mcp.go +++ b/internal/httpapi/agent_tool_mcp.go @@ -365,6 +365,9 @@ func (h *agentToolGateway) dispatch(ctx context.Context, session *AgentToolSessi ctx = withAssistantApprovalReviewer(ctx, session.approvalReviewer) } if tool.Kind == "workspace" { + if session.ExecMode.ReadOnly { + return "", fmt.Errorf("%w: 只读模式不开放工作区工具 %s", errAgentToolForbidden, tool.Name) + } return h.callMainBotWorkspace(ctx, session, tool.Name, args) } if tool.Kind == "agentflow" { @@ -444,6 +447,9 @@ func (h *agentToolGateway) dispatch(ctx context.Context, session *AgentToolSessi } if session.Mode == AgentToolAssistant && mcpCallMayWrite(kind, call) { name := mcpCallName(kind, call) + if session.ExecMode.ReadOnly { + return "", fmt.Errorf("只读模式拒绝可能写入外部系统的 MCP 调用 %s,请到平台页面操作", name) + } if !session.ExecMode.AutoWrite { if err := h.waitForMCPWriteApproval(ctx, session, name, call); err != nil { return "", err diff --git a/internal/httpapi/agent_tool_mcp_test.go b/internal/httpapi/agent_tool_mcp_test.go index 9adbd8be6..37a62833a 100644 --- a/internal/httpapi/agent_tool_mcp_test.go +++ b/internal/httpapi/agent_tool_mcp_test.go @@ -445,6 +445,18 @@ func TestConversationMCPFollowsApprovalModeOnly(t *testing.T) { if out := call(assisted, "allow", write); len(called) != 1 { t.Fatalf("assisted mode runs an allowed write: %v %s", called, out) } + // IM Bot read-only mode: reads run, writes are refused at once instead of + // waiting for an approval nobody can give in the chat. + readOnly := assistantReadOnlyExecMode() + if out := call(readOnly, "allow", read); len(called) != 1 { + t.Fatalf("read-only mode must run reads: %v %s", called, out) + } + if out := call(readOnly, "allow", write); len(called) != 0 || !strings.Contains(out, "只读模式") { + t.Fatalf("read-only mode must refuse writes: %v %s", called, out) + } + if assistantPlatformToolAllowed(readOnly, "issues_save") || !assistantPlatformToolAllowed(readOnly, "issues_list") { + t.Fatal("read-only platform tool policy") + } for name, want := range map[string]bool{"merge_merge_request": true, "comments_add": true, "get_branch_list": false, "user_todo_stories_get": false, "frobnicate": true} { if got := mcpCallMayWrite("gongfeng", assistantMCPCall{Tool: name}); got != want { t.Fatalf("mcpCallMayWrite(%s)=%v", name, got) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index b696a0e39..ad7d0f564 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -30,15 +30,17 @@ type assistantConversation = domain.AssistantSession const assistantRequestBodyLimit = 32 << 20 type assistantDraft struct { - Tool string `json:"tool"` - Path map[string]any `json:"path,omitempty"` - Query map[string]any `json:"query,omitempty"` - Body json.RawMessage `json:"body,omitempty"` - Saved bool `json:"saved,omitempty"` - Auto bool `json:"auto,omitempty"` - Warnings []string `json:"warnings,omitempty"` - SaveError string `json:"saveError,omitempty"` - Review *assistantApprovalReview `json:"review,omitempty"` + Tool string `json:"tool"` + Path map[string]any `json:"path,omitempty"` + Query map[string]any `json:"query,omitempty"` + Body json.RawMessage `json:"body,omitempty"` + Saved bool `json:"saved,omitempty"` + Auto bool `json:"auto,omitempty"` + Warnings []string `json:"warnings,omitempty"` + SaveError string `json:"saveError,omitempty"` + // Result is the response body of an executed draft (not persisted). + Result string `json:"-"` + Review *assistantApprovalReview `json:"review,omitempty"` // Proposal marks a draft the assistant offers on its own to hand work to a // SOP; Decision is the server-recorded answer to it. Proposal *assistantProposal `json:"proposal,omitempty"` diff --git a/internal/httpapi/assistant_write.go b/internal/httpapi/assistant_write.go index 9774d0e89..9d92b3c92 100644 --- a/internal/httpapi/assistant_write.go +++ b/internal/httpapi/assistant_write.go @@ -24,8 +24,17 @@ type assistantExecMode struct { DeferSOP bool ConfirmDangerous bool ReviewWrites bool + // ReadOnly is the IM Bot read-only policy: platform writes are absent + // from the catalog and refused by the gateway (AutoWrite is false), + // external MCP calls that may write and workspace tools are refused + // outright instead of waiting for an approval nobody can give, and + // terminal drafts are never executed. + ReadOnly bool } +// assistantReadOnlyExecMode is the execution policy of read-only IM Bots. +func assistantReadOnlyExecMode() assistantExecMode { return assistantExecMode{ReadOnly: true} } + // assistantExecModeForApproval maps the assistant approval mode to the // session gateway write policy. Writes are detected by the gateway itself // (ReadOnly flag on each operation) — the model never hand-builds drafts. @@ -129,8 +138,9 @@ func (h *assistantHandlers) applyAssistantDrafts(ctx context.Context, user domai args["body"] = body } } - if _, err := h.dispatchAssistantMCPCall(ctx, user, projectID, assistantMCPCall{Server: "agentflow", Tool: draft.Tool, Arguments: args}); err == nil { + if result, err := h.dispatchAssistantMCPCall(ctx, user, projectID, assistantMCPCall{Server: "agentflow", Tool: draft.Tool, Arguments: args}); err == nil { applied.Saved = true + applied.Result = result } else if assistantExecModeFrom(ctx).DeferSOP && assistantDeferredSOPTool(draft.Tool) { applied.Saved = true } else { diff --git a/internal/httpapi/bot_dingtalk_save_test.go b/internal/httpapi/bot_dingtalk_save_test.go new file mode 100644 index 000000000..d162cebdc --- /dev/null +++ b/internal/httpapi/bot_dingtalk_save_test.go @@ -0,0 +1,90 @@ +package httpapi + +import ( + "agentflow/internal/botinteraction" + "agentflow/internal/domain" + "agentflow/internal/notification" + "context" + "encoding/json" + "errors" + "net/http" + "strings" + "testing" +) + +// stubChannel verifies credentials against a fixed secret. +type stubChannel struct{ kind string } + +func (s stubChannel) Kind() string { return s.kind } +func (s stubChannel) Verify(_ context.Context, _ domain.NotificationBot, cred botinteraction.Credential) (botinteraction.Verification, error) { + if cred.Secret != "good" { + return botinteraction.Verification{}, errors.New("钉钉拒绝了 Client ID 或 Client Secret(invalid)") + } + return botinteraction.Verification{}, nil +} +func (s stubChannel) Connect(context.Context, domain.NotificationBot, botinteraction.Credential, botinteraction.InboundSink) (botinteraction.Conn, error) { + return nil, errors.New("not used") +} + +func TestDingTalkBotSaveRequiresCorpIdAndVerifiesCredentials(t *testing.T) { + f := cvBNewHTTPFixture(t) + nh := ¬ificationHandlers{deps: &deps{repo: f.s, engine: f.e}, service: notification.New(f.s)} + nh.service.Interactions.RegisterChannel(stubChannel{botinteraction.ChannelDingTalk}) + create := func(body map[string]any) (int, map[string]any) { + rec := cvBDirectHandlerRequest(t, f, http.MethodPost, "/api/notification-bots", body, nh.bots, nil, true) + var out map[string]any + json.Unmarshal(rec.Body.Bytes(), &out) + return rec.Code, out + } + base := func() map[string]any { + return map[string]any{"projectId": "platform", "name": "钉钉", "channel": "dingtalk", "botId": "ding-app", "secret": "good", "enabled": true, "interactionEnabled": true} + } + if code, out := create(base()); code != 400 || out["code"] != "tenant_required" { + t.Fatalf("missing CorpId = %d %v", code, out) + } + bad := base() + bad["tenantKey"], bad["secret"] = "ding-corp", "wrong" + if code, out := create(bad); code != 400 || out["code"] != "credential_invalid" { + t.Fatalf("bad secret = %d %v", code, out) + } + if bots, _ := f.s.NotificationBots("platform"); len(bots) != 0 { + t.Fatalf("saved after a failed verification: %+v", bots) + } + auto := base() + auto["tenantKey"], auto["writeApproval"] = "ding-corp", "auto" + if code, _ := create(auto); code != 400 { + t.Fatalf("auto write on DingTalk = %d", code) + } + badTemplate := base() + badTemplate["tenantKey"] = "ding-corp" + badTemplate["cardTemplateId"] = "tpl id" + if code, _ := create(badTemplate); code != 400 { + t.Fatalf("bad template id = %d", code) + } + ok := base() + ok["tenantKey"] = " ding-corp " + ok["writeApproval"] = "confirm" + ok["cardTemplateId"] = " tpl-123.schema " + code, out := create(ok) + if code != 200 || out["tenantExpected"] != "ding-corp" || out["tenantState"] != domain.BotTenantAwaiting || out["writeApproval"] != "confirm" || out["credentialFormat"] != "encrypted" || out["cardTemplateId"] != "tpl-123.schema" { + t.Fatalf("create = %d %v", code, out) + } + id := out["id"].(string) + // Locked: the enterprise can no longer be changed. + stored, _ := f.s.NotificationBot(id) + if _, err := f.s.RecordBotTenantCandidate(id, "ding-corp", "e1", stored.CredentialVersion); err != nil { + t.Fatal(err) + } + stored, _ = f.s.NotificationBot(id) + update := map[string]any{"projectId": "platform", "name": "钉钉", "channel": "dingtalk", "botId": "ding-app", "enabled": true, "interactionEnabled": true, "tenantKey": "other-corp", "configVersion": stored.ConfigVersion} + rec := cvBDirectHandlerRequest(t, f, http.MethodPut, "/api/notification-bots/"+id, update, nh.bot, map[string]string{"id": id}, true) + if rec.Code != 400 || !strings.Contains(rec.Body.String(), "immutable_field") { + t.Fatalf("locked CorpId change = %d %s", rec.Code, rec.Body.String()) + } + update["tenantKey"] = "ding-corp" + update["name"] = "钉钉改名" + rec = cvBDirectHandlerRequest(t, f, http.MethodPut, "/api/notification-bots/"+id, update, nh.bot, map[string]string{"id": id}, true) + if rec.Code != 200 { + t.Fatalf("rename = %d %s", rec.Code, rec.Body.String()) + } +} diff --git a/internal/httpapi/bot_drafts.go b/internal/httpapi/bot_drafts.go new file mode 100644 index 000000000..cfa5d80c9 --- /dev/null +++ b/internal/httpapi/bot_drafts.go @@ -0,0 +1,574 @@ +package httpapi + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/url" + "sort" + "strings" + "time" + + "agentflow/internal/botinteraction" + "agentflow/internal/domain" + "agentflow/internal/repo" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +// WeCom "confirm" write mode (stage G). The assistant runs with platform +// writes blocked and submits drafts; the drafts are frozen in +// bot_pending_drafts and the chat reply carries a summary and a link to +// /bot-approve.html#draft=. Only the bound platform user can open and +// confirm it, after the server re-checks the binding, the Bot configuration, +// project permission, expiry and the frozen parameters. The confirmation +// moves the draft from pending to executing in one statement, then runs the +// drafts through the same preflight and execution as the web assistant. + +var ( + botDraftResources = map[string]string{"issues": "需求", "workflows": "SOP", "agents": "角色", "projects": "项目", "rules": "规则", "skills": "技能", "schedule_jobs": "定时任务", "runs": "运行", "assistant_tasks": "AI 助手任务", "sandboxes": "沙箱", "dockerfiles": "Dockerfile", "environments": "环境安装脚本", "business_domains": "业务领域", "repositories": "仓库"} + botDraftVerbs = map[string]string{"save": "保存", "delete": "删除", "dispatch": "派发", "start": "启动", "cancel": "取消", "reject": "退回", "run": "运行", "restore": "恢复", "pass": "通过", "retry": "重试", "pause": "暂停", "resume": "继续", "reopen": "重新打开", "publish": "发布", "clone": "复制"} +) + +// botDraftLabel names a platform write in Chinese ("保存需求"), falling back +// to the operation description and then the tool name. +func botDraftLabel(tool string) string { + tool = strings.TrimSpace(tool) + if i := strings.LastIndex(tool, "_"); i > 0 { + if verb, ok := botDraftVerbs[tool[i+1:]]; ok { + if resource, ok := botDraftResources[tool[:i]]; ok { + return verb + resource + } + } + } + if op, ok := assistantPublicOperations[tool]; ok && strings.TrimSpace(op.Description) != "" { + return truncateRunes(strings.TrimSpace(strings.SplitN(op.Description, ".", 2)[0]), 40) + } + return tool +} + +// setDraftBodyField returns body with key set to value. +func setDraftBodyField(body json.RawMessage, key string, value any) json.RawMessage { + obj := map[string]any{} + if len(body) > 0 { + _ = json.Unmarshal(body, &obj) + } + if value == nil { + delete(obj, key) + } else { + obj[key] = value + } + out, err := json.Marshal(obj) + if err != nil { + return body + } + return out +} + +// startBotDraftIssue dispatches the SOP of a requirement the draft created. +func (h *assistantHandlers) startBotDraftIssue(ctx context.Context, user domain.User, projectID string, d assistantDraft) (string, error) { + var created struct { + ID string `json:"id"` + } + if json.Unmarshal([]byte(d.Result), &created) != nil || strings.TrimSpace(created.ID) == "" { + return "", errors.New("没有拿到新需求的编号,请在平台手动启动") + } + result, err := h.dispatchAssistantMCPCall(withAssistantExecMode(ctx, assistantExecMode{AutoWrite: true}), user, projectID, assistantMCPCall{Server: "agentflow", Tool: "issues_dispatch", Arguments: map[string]any{"path": map[string]any{"id": created.ID}, "body": map[string]any{}}}) + if err != nil { + return "", err + } + var task struct { + RunID string `json:"runId"` + } + if json.Unmarshal([]byte(result), &task) == nil && task.RunID != "" { + return "(运行 " + task.RunID + ")", nil + } + return "", nil +} + +// botDraftSummaryLine describes one draft for the chat and the page. +func botDraftSummaryLine(d assistantDraft) string { + return botDraftSummaryLineSOP(d, "") +} + +// botDraftSummaryLineSOP adds the SOP a new requirement will be bound to. +func botDraftSummaryLineSOP(d assistantDraft, sop string) string { + line := botDraftSummaryText(d) + if sop != "" { + line += "(SOP:" + sop + ")" + } + return line +} + +func botDraftSummaryText(d assistantDraft) string { + label := botDraftLabel(d.Tool) + target := "" + var body map[string]any + if len(d.Body) > 0 && json.Unmarshal(d.Body, &body) == nil { + for _, key := range []string{"title", "name", "id"} { + if v, ok := body[key].(string); ok && strings.TrimSpace(v) != "" { + target = truncateRunes(strings.TrimSpace(v), 60) + break + } + } + } + if target == "" { + target = assistantDraftRefID(d) + } + if target != "" { + return label + ":" + target + } + return label +} + +// confirmBotDrafts freezes write drafts for page confirmation and returns +// the text appended to the chat reply. +func (h *assistantHandlers) confirmBotDrafts(ctx context.Context, bot domain.NotificationBot, user domain.User, item domain.BotInteraction, drafts []assistantDraft) string { + base := "" + if h.notifications != nil && h.notifications.service != nil { + base = strings.TrimRight(h.notifications.service.PublicURL, "/") + } + if u, err := url.Parse(base); base == "" || err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" { + return "\n\n这次需要修改平台数据,但平台外部访问地址尚未配置,无法生成确认链接,操作没有执行。请联系管理员,或到平台页面完成。" + } + drafts = h.resolveAssistantDrafts(ctx, user, bot.ProjectID, drafts, AssistantApprovalConfirm) + raw, err := json.Marshal(drafts) + if err != nil { + return "\n\n操作草稿无法保存,没有执行。请到平台页面完成。" + } + lines := make([]string, 0, len(drafts)) + newIssues := 0 + for i, d := range drafts { + sop := "" + if explicit, def, ok := h.botDraftIssueSOP(bot.ProjectID, d); ok { + sop = h.botWorkflowName(explicit) + if explicit == "" { + sop = "项目默认 · " + h.botWorkflowName(def) + } + newIssues++ + } + line := fmt.Sprintf("%d. %s", i+1, botDraftSummaryLineSOP(d, sop)) + if len(d.Warnings) > 0 { + line += "(注意:" + truncateRunes(strings.Join(d.Warnings, ";"), 120) + ")" + } + lines = append(lines, line) + } + summary := strings.Join(lines, "\n") + saved, err := h.repo.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: bot.ID, IdentityScope: bot.IdentityScope(), SenderID: item.SenderID, ProjectID: bot.ProjectID, UserID: user.ID, InteractionID: item.ID, Summary: summary, Drafts: string(raw)}, time.Now().UTC()) + if err != nil { + return "\n\n操作草稿无法保存,没有执行。请稍后重试或到平台页面完成。" + } + link := base + "/bot-approve.html#draft=" + saved.ID + head := "\n\n以下操作需要你在平台确认后才会执行(" + fmt.Sprint(int(repo.BotDraftTTL.Minutes())) + " 分钟内有效):\n" + summary + if newIssues > 0 { + head += "\n确认页可以更换 SOP,并选择创建后立即启动。" + } + if bot.ChannelKind() != domain.BotChannelWeCom { + // Feishu and DingTalk replies are plain text: the URL stands on + // its own line so auto-linking does not swallow other characters. + return head + "\n在平台确认这些操作:\n" + link + } + return head + "\n[在平台确认这些操作](" + link + ")" +} + +type botDraftView struct { + domain.BotPendingDraft + BotName string `json:"botName,omitempty"` + ProjectName string `json:"projectName,omitempty"` + Operations []string `json:"operations"` + Results []string `json:"results,omitempty"` + // NewIssues are the requirements this draft creates; the user picks + // their SOP and whether to start it right after creation. + NewIssues []botDraftIssue `json:"newIssues,omitempty"` + Workflows []botDraftWorkflow `json:"workflows,omitempty"` +} + +type botDraftIssue struct { + Index int `json:"index"` + Title string `json:"title"` + // WorkflowID is the SOP the model named; empty means the project + // default, which is DefaultWorkflowID / DefaultWorkflowName. + WorkflowID string `json:"workflowId"` + DefaultWorkflowID string `json:"defaultWorkflowId"` + DefaultWorkflowName string `json:"defaultWorkflowName"` +} + +type botDraftWorkflow struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// botDraftChoice is the user's SOP choice for one new requirement; an empty +// workflowId selects the project default SOP. +type botDraftChoice struct { + Index int `json:"index"` + WorkflowID string `json:"workflowId"` + Start bool `json:"start"` +} + +// botDraftIssueWorkflow reports the SOP a draft creating a new requirement +// binds to: the workflowId the model set, else the project default. Drafts +// that update an existing issue are not new requirements. +func (h *assistantHandlers) botDraftIssueWorkflow(projectID string, d assistantDraft) (string, bool) { + explicit, def, ok := h.botDraftIssueSOP(projectID, d) + if explicit != "" { + return explicit, ok + } + return def, ok +} + +// botDraftIssueSOP returns the SOP the model named for a new requirement +// (empty when none) and the project default it would otherwise get. +func (h *assistantHandlers) botDraftIssueSOP(projectID string, d assistantDraft) (string, string, bool) { + if strings.TrimSpace(d.Tool) != "issues_save" { + return "", "", false + } + var body map[string]any + if len(d.Body) > 0 && json.Unmarshal(d.Body, &body) != nil { + return "", "", false + } + if id, _ := body["id"].(string); strings.TrimSpace(id) != "" { + if _, exists := h.repo.Issue(strings.TrimSpace(id)); exists { + return "", "", false + } + } + source, _ := body["source"].(string) + kind, _ := body["kind"].(string) + explicit, _ := body["workflowId"].(string) + return strings.TrimSpace(explicit), h.repo.DefaultIssueWorkflow(projectID, source, kind), true +} + +func (h *assistantHandlers) botWorkflowName(id string) string { + if wf, ok := h.repo.Workflow(id); ok && strings.TrimSpace(wf.Name) != "" { + return wf.Name + } + if id == "" { + return "未绑定" + } + return id +} + +// botDraftWorkflows lists the SOPs a requirement of the project may be bound +// to, like the requirement form on the platform: the project's own SOPs, +// shared SOPs and the platform generic templates (archived ones excluded), +// plus the current choices. The project default is offered separately. +func (h *assistantHandlers) botDraftWorkflows(projectID string, include ...string) []botDraftWorkflow { + seen := map[string]bool{} + out := []botDraftWorkflow{} + add := func(id, name string) { + if id == "" || seen[id] { + return + } + seen[id] = true + out = append(out, botDraftWorkflow{ID: id, Name: name}) + } + generic := []domain.WorkflowDefinition{} + for _, wf := range h.repo.Workflows() { + if wf.Status == "archived" { + continue + } + switch { + case wf.Generic: + generic = append(generic, wf) + case wf.ProjectID == projectID || wf.ProjectID == "" || wf.Shared: + add(wf.ID, wf.Name) + } + } + for _, id := range include { + add(id, h.botWorkflowName(id)) + } + sort.SliceStable(out, func(i, j int) bool { return out[i].Name < out[j].Name }) + for _, wf := range generic { + add(wf.ID, "通用 · "+wf.Name) + } + return out +} + +func (h *assistantHandlers) botDraftNewIssues(projectID string, drafts []assistantDraft) []botDraftIssue { + out := []botDraftIssue{} + for i, d := range drafts { + explicit, def, ok := h.botDraftIssueSOP(projectID, d) + if !ok { + continue + } + out = append(out, botDraftIssue{Index: i, Title: botDraftSummaryText(d), WorkflowID: explicit, DefaultWorkflowID: def, DefaultWorkflowName: h.botWorkflowName(def)}) + } + return out +} + +func (h *assistantHandlers) botDraftUser(c *gin.Context) (domain.User, bool) { + if _, ok := bearerToken(c.Request); ok { + c.JSON(401, gin.H{"error": "请使用平台网页登录确认"}) + return domain.User{}, false + } + current, ok := h.requestUser(c.Request, false) + if !ok || current == nil || current.IsGuest || current.ID <= 0 { + c.JSON(401, gin.H{"error": "请先登录平台"}) + return domain.User{}, false + } + user, ok := h.repo.User(current.ID) + if !ok { + c.JSON(401, gin.H{"error": "请先登录平台"}) + return domain.User{}, false + } + return user, true +} + +func (h *assistantHandlers) botDraftView(d domain.BotPendingDraft) botDraftView { + view := botDraftView{BotPendingDraft: d, Operations: strings.Split(d.Summary, "\n")} + if bot, err := h.repo.NotificationBot(d.ConfigID); err == nil { + view.BotName = bot.Name + } + if project, ok := h.repo.Project(d.ProjectID); ok { + view.ProjectName = project.Name + } + if d.Result != "" { + view.Results = strings.Split(d.Result, "\n") + } + if d.Status == repo.BotDraftPending { + var drafts []assistantDraft + if json.Unmarshal([]byte(d.Drafts), &drafts) == nil { + view.NewIssues = h.botDraftNewIssues(d.ProjectID, drafts) + if len(view.NewIssues) > 0 { + ids := make([]string, 0, len(view.NewIssues)) + for _, issue := range view.NewIssues { + ids = append(ids, issue.WorkflowID) + } + view.Workflows = h.botDraftWorkflows(d.ProjectID, ids...) + } + } + } + return view +} + +// botDraft serves GET /api/bot-drafts/:id and the confirm/cancel actions. +// Drafts of other users answer 404, like unknown IDs. +func (h *assistantHandlers) botDraft(c *gin.Context) { + user, ok := h.botDraftUser(c) + if !ok { + return + } + id := strings.TrimSpace(c.Param("id")) + now := time.Now().UTC() + draft, err := h.repo.BotPendingDraft(id, now) + if err != nil || draft.UserID != user.ID { + c.JSON(404, gin.H{"error": "确认链接无效或不属于当前账号"}) + return + } + if c.Request.Method == "GET" { + c.JSON(200, h.botDraftView(draft)) + return + } + if !botBindingSameOrigin(c) { + c.JSON(403, gin.H{"error": "请从平台页面操作"}) + return + } + if strings.HasSuffix(c.Request.URL.Path, "/cancel") { + if err := h.repo.CancelBotPendingDraft(id, user.ID, "用户在平台页面取消", now); err != nil && !errors.Is(err, repo.ErrBotDraftNotPending) { + respond(c, err) + return + } + draft, _ = h.repo.BotPendingDraft(id, now) + c.JSON(200, h.botDraftView(draft)) + return + } + if draft.Status != repo.BotDraftPending { + // A repeated click returns the current state. + c.JSON(200, h.botDraftView(draft)) + return + } + var req struct { + Choices []botDraftChoice `json:"choices"` + } + if c.Request.ContentLength != 0 && !decodeLimit(c, &req, 64<<10) { + return + } + current, invalid, err := h.executeBotDraft(c.Request.Context(), user, draft, req.Choices, nil) + switch { + case err != nil: + respond(c, err) + case invalid != "": + c.JSON(409, gin.H{"error": "操作已失效:" + invalid, "code": "draft_invalidated", "draft": h.botDraftView(current)}) + default: + c.JSON(200, h.botDraftView(current)) + } +} + +// executeBotDraft re-checks and runs a pending draft for user (the platform +// page and the Feishu confirmation card share it). choices set the SOP of +// new requirements; startAll, when set, starts every new requirement's SOP. +// A failed re-check cancels the draft and returns the reason in invalid. +func (h *assistantHandlers) executeBotDraft(ctx context.Context, user domain.User, draft domain.BotPendingDraft, choices []botDraftChoice, startAll *bool) (domain.BotPendingDraft, string, error) { + id := draft.ID + now := time.Now().UTC() + invalidate := func(reason string) (domain.BotPendingDraft, string, error) { + if err := h.repo.CancelBotPendingDraft(id, user.ID, reason, now); err != nil && !errors.Is(err, repo.ErrBotDraftNotPending) && !errors.Is(err, gorm.ErrRecordNotFound) { + return draft, "", err + } + current, _ := h.repo.BotPendingDraft(id, now) + return current, reason, nil + } + bot, err := h.repo.NotificationBot(draft.ConfigID) + if err != nil || !bot.Enabled || !bot.InteractionEnabled || bot.EffectiveWriteApproval() != domain.BotWriteConfirm { + return invalidate("机器人已停用、删除或不再使用网页确认") + } + binding, err := h.repo.BotUserBinding(bot.IdentityScope(), draft.SenderID) + if err != nil || binding.UserID != user.ID { + return invalidate("聊天账号的绑定已解除或已变化") + } + if _, err := h.botExecutionUser(bot, draft.SenderID); err != nil { + return invalidate(err.Error()) + } + begun, err := h.repo.BeginBotPendingDraft(id, user.ID, now) + if errors.Is(err, repo.ErrBotDraftNotPending) { + return begun, "", nil + } + if err != nil { + return draft, "", err + } + finish := func(status, result string) (domain.BotPendingDraft, string, error) { + if err := h.repo.FinishBotPendingDraft(id, status, result, time.Now().UTC()); err != nil { + return begun, "", err + } + current, _ := h.repo.BotPendingDraft(id, time.Now().UTC()) + return current, "", nil + } + var drafts []assistantDraft + if err := json.Unmarshal([]byte(begun.Drafts), &drafts); err != nil { + return finish(repo.BotDraftFailed, "草稿内容无法读取,没有执行") + } + for i := range drafts { + drafts[i].Saved, drafts[i].Auto, drafts[i].SaveError = false, false, "" + } + // Apply the SOP choices of new requirements. Only SOPs offered on the + // page are accepted; the frozen draft itself is unchanged. + start := map[int]bool{} + sops := map[int]string{} + newIssues := h.botDraftNewIssues(draft.ProjectID, drafts) + defaults := map[int]string{} + for _, issue := range newIssues { + sops[issue.Index] = issue.WorkflowID + if issue.WorkflowID == "" { + sops[issue.Index] = issue.DefaultWorkflowID + } + defaults[issue.Index] = issue.DefaultWorkflowID + if startAll != nil { + start[issue.Index] = *startAll + } + } + if len(choices) > 0 { + ids := []string{} + for _, issue := range newIssues { + ids = append(ids, issue.WorkflowID) + } + allowed := map[string]bool{} + for _, wf := range h.botDraftWorkflows(draft.ProjectID, ids...) { + allowed[wf.ID] = true + } + for _, choice := range choices { + current, ok := sops[choice.Index] + if !ok { + continue + } + wf := strings.TrimSpace(choice.WorkflowID) + if wf == "" { + // The project default: drop any SOP the model named so the + // platform binds the requirement the usual way. + wf = defaults[choice.Index] + if wf != current { + drafts[choice.Index].Body = setDraftBodyField(drafts[choice.Index].Body, "workflowId", nil) + } + } else if !allowed[wf] { + return finish(repo.BotDraftFailed, "所选 SOP 不可用于当前项目,没有执行") + } else if wf != current { + drafts[choice.Index].Body = setDraftBodyField(drafts[choice.Index].Body, "workflowId", wf) + } + sops[choice.Index] = wf + start[choice.Index] = choice.Start + } + } + // Same preflight and execution as the web assistant: drafts with + // warnings are not executed. The context is detached so a closed tab or + // a dropped connection cannot interrupt the writes halfway. + execCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Minute) + defer cancel() + applied := h.resolveAssistantDrafts(execCtx, user, draft.ProjectID, drafts, AssistantApprovalAuto) + results := make([]string, 0, len(applied)) + status := repo.BotDraftExecuted + for i, d := range applied { + sop := "" + if wf, ok := sops[i]; ok { + sop = h.botWorkflowName(wf) + } + line := botDraftSummaryLineSOP(d, sop) + switch { + case d.Saved: + results = append(results, "已完成:"+line) + if start[i] { + if started, err := h.startBotDraftIssue(execCtx, user, draft.ProjectID, d); err != nil { + status = repo.BotDraftFailed + results = append(results, "未启动:"+sop+"("+truncateRunes(err.Error(), 200)+")") + } else { + results = append(results, "已启动:"+sop+started) + } + } + case d.SaveError != "": + status = repo.BotDraftFailed + results = append(results, "未完成:"+line+"("+d.SaveError+")") + default: + status = repo.BotDraftFailed + results = append(results, "未执行:"+line+"("+strings.Join(d.Warnings, ";")+")") + } + } + return finish(status, strings.Join(results, "\n")) +} + +// botDraftCardAction handles the buttons of a Feishu confirmation card. Only +// the platform user bound to the sender who asked can use them; the result +// is the chat reply. +func (h *assistantHandlers) botDraftCardAction(ctx context.Context, bot domain.NotificationBot, item domain.BotInteraction) (string, error) { + user, err := h.botExecutionUser(bot, item.SenderID) + if err != nil { + return err.Error(), botinteraction.ErrCardActionDeferred + } + now := time.Now().UTC() + draft, err := h.repo.BotPendingDraft(strings.TrimSpace(item.DeliveryID), now) + if err != nil || draft.UserID != user.ID || draft.ConfigID != bot.ID || draft.SenderID != item.SenderID { + return "这份操作不存在、已过期,或不是你发起的,没有执行。", nil + } + if draft.Status != repo.BotDraftPending { + return botDraftStateReply(draft), nil + } + if item.Action == botinteraction.DraftActionCancel { + if err := h.repo.CancelBotPendingDraft(draft.ID, user.ID, "用户在聊天卡片中取消", now); err != nil && !errors.Is(err, repo.ErrBotDraftNotPending) { + return "", err + } + return "已取消,这些操作不会执行。", nil + } + startAll := item.Action == botinteraction.DraftActionConfirmStart + current, invalid, err := h.executeBotDraft(ctx, user, draft, nil, &startAll) + if err != nil { + return "", err + } + if invalid != "" { + return "操作已失效:" + invalid + "。没有执行。", nil + } + return botDraftStateReply(current), nil +} + +// botDraftStateReply describes a draft that is no longer pending. +func botDraftStateReply(d domain.BotPendingDraft) string { + head := map[string]string{repo.BotDraftExecuted: "操作已执行:", repo.BotDraftFailed: "操作部分或全部未完成:", repo.BotDraftExecuting: "这份操作正在执行,请稍后在平台查看结果。", repo.BotDraftNeedsReview: "这份操作执行中断,结果待核对,平台不会自动重新执行。", repo.BotDraftExpired: "这份操作已过期,没有执行。请重新发起。", repo.BotDraftCancelled: "这份操作已取消,没有执行。"}[d.Status] + if head == "" { + head = "这份操作的状态:" + d.Status + } + if d.Result != "" && (d.Status == repo.BotDraftExecuted || d.Status == repo.BotDraftFailed) { + return head + "\n" + d.Result + } + if d.Reason != "" && d.Status == repo.BotDraftCancelled { + return head + "(" + d.Reason + ")" + } + return head +} diff --git a/internal/httpapi/bot_drafts_test.go b/internal/httpapi/bot_drafts_test.go new file mode 100644 index 000000000..3f0970948 --- /dev/null +++ b/internal/httpapi/bot_drafts_test.go @@ -0,0 +1,472 @@ +package httpapi + +import ( + "agentflow/internal/botinteraction" + "agentflow/internal/domain" + "agentflow/internal/repo" + "agentflow/internal/tracker" + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "regexp" + "strings" + "testing" + "time" + + "github.com/gin-gonic/gin" +) + +var botDraftLink = regexp.MustCompile(`/bot-approve\.html#draft=([a-f0-9]{32})`) + +// confirmModeFixture: a WeCom Bot in the confirm write mode whose model run +// submits one issues_save draft. +func confirmModeFixture(t *testing.T) (*repo.Store, *assistantHandlers, domain.NotificationBot, func() string) { + s, h, bot, ask, _ := confirmModeFixtureFor(t, domain.BotChannelWeCom) + return s, h, bot, ask +} + +// confirmModeFixtureFor also returns the last full reply text. +func confirmModeFixtureFor(t *testing.T, channel string) (*repo.Store, *assistantHandlers, domain.NotificationBot, func() string, *string) { + t.Helper() + t.Setenv("AGENTFLOW_SESSION_SECRET", "bot-draft-test-session-secret") + s, h, bot := botFixture(t) + bot.WriteApproval = domain.BotWriteConfirm + if channel != domain.BotChannelWeCom { + bot.ID, bot.BotID, bot.Channel, bot.TenantKey, bot.TenantState = "b-"+channel, "app-"+channel, channel, "tenant", domain.BotTenantLocked + } + if err := s.SaveNotificationBot(bot, "secret"); err != nil { + t.Fatal(err) + } + bot, _ = s.NotificationBot(bot.ID) + if channel != domain.BotChannelWeCom { + // Feishu/DingTalk identities are scoped by the configuration. + bindBotTestUser(t, s, bot, "new-wecom-user", 43) + } + last := new(string) + h.api = New(s, h.engine, tracker.NewService(), "", "") + title := 0 + h.generate = func(ctx context.Context, prompt string, user domain.User) (string, error) { + if !strings.Contains(prompt, "网页确认模式") { + t.Fatal("confirm policy missing from prompt") + } + session := botTestToolSession(t, user.ID) + if session.ExecMode.AutoWrite || session.ExecMode.ReadOnly { + t.Fatalf("exec mode = %+v", session.ExecMode) + } + title++ + body, _ := json.Marshal(map[string]any{"projectId": "p", "title": "网页确认的需求" + strings.Repeat("!", title)}) + session.commitFinal(AgentTerminalResult{Kind: AgentTerminalAssistant, Assistant: &assistantReply{Message: "我会创建这个需求。", Drafts: []assistantDraft{{Tool: "issues_save", Body: body}}}}) + return "", nil + } + ask := func() string { + reply, err := h.generateBotReply(context.Background(), bot, domain.BotConversation{SessionID: "bot-session", ChatType: "single"}, nil, domain.BotInteraction{ID: "turn-" + time.Now().Format("150405.000000"), SenderID: "new-wecom-user", Content: "帮我建个需求"}) + if err != nil { + t.Fatal(err) + } + *last = reply + m := botDraftLink.FindStringSubmatch(reply) + if m == nil { + t.Fatalf("no confirmation link in %q", reply) + } + return m[1] + } + return s, h, bot, ask, last +} + +// Feishu and DingTalk create requirements through web confirmation too. The +// reply is plain text, so the link stands alone on its own line. +func TestPlainTextChannelsConfirmWritesOnThePlatformPage(t *testing.T) { + for _, channel := range []string{domain.BotChannelFeishu, domain.BotChannelDingTalk} { + t.Run(channel, func(t *testing.T) { + s, h, _, ask, last := confirmModeFixtureFor(t, channel) + member, _ := s.User(43) + before := len(s.Issues()) + id := ask() + if strings.ContainsAny(*last, "[]()") { + t.Fatalf("markdown in plain-text reply: %q", *last) + } + lines := strings.Split(*last, "\n") + if lines[len(lines)-1] != "http://platform.example/bot-approve.html#draft="+id { + t.Fatalf("link not alone on the last line: %q", *last) + } + if len(s.Issues()) != before { + t.Fatal("draft executed before confirmation") + } + code, done := draftCall(t, h, member, "POST", "/api/bot-drafts/"+id+"/confirm", true) + if code != 200 || done["status"] != repo.BotDraftExecuted || len(s.Issues()) != before+1 { + t.Fatalf("confirm = %d %v issues=%d", code, done, len(s.Issues())) + } + if _, again := draftCall(t, h, member, "POST", "/api/bot-drafts/"+id+"/confirm", true); again["status"] != repo.BotDraftExecuted || len(s.Issues()) != before+1 { + t.Fatalf("repeat = %v", again) + } + }) + } +} + +func draftCall(t *testing.T, h *assistantHandlers, user domain.User, method, path string, origin bool) (int, map[string]any) { + t.Helper() + req := httptest.NewRequest(method, path, strings.NewReader("{}")) + req.Header.Set("Content-Type", "application/json") + req = authedRequest(t, req, user) + if !origin { + req.Header.Del("Origin") + } + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + c.Request = req + c.Params = gin.Params{{Key: "id", Value: strings.Split(strings.TrimPrefix(path, "/api/bot-drafts/"), "/")[0]}} + h.botDraft(c) + out := map[string]any{} + json.Unmarshal(w.Body.Bytes(), &out) + return w.Code, out +} + +func TestWeComConfirmModeExecutesConfirmedDraftOnce(t *testing.T) { + s, h, _, ask := confirmModeFixture(t) + member, _ := s.User(43) + owner, _ := s.User(42) + before := len(s.Issues()) + id := ask() + if len(s.Issues()) != before { + t.Fatal("draft executed before confirmation") + } + if code, _ := draftCall(t, h, owner, "GET", "/api/bot-drafts/"+id, false); code != 404 { + t.Fatalf("other user can read the draft: %d", code) + } + code, view := draftCall(t, h, member, "GET", "/api/bot-drafts/"+id, false) + if code != 200 || view["status"] != repo.BotDraftPending || len(view["operations"].([]any)) != 1 || !strings.HasPrefix(view["operations"].([]any)[0].(string), "1. 保存需求:网页确认的需求!(SOP:") { + t.Fatalf("view = %d %v", code, view) + } + if _, ok := view["drafts"]; ok { + t.Fatal("frozen draft JSON exposed") + } + if code, _ := draftCall(t, h, owner, "POST", "/api/bot-drafts/"+id+"/confirm", true); code != 404 { + t.Fatalf("other user confirmed: %d", code) + } + if code, _ := draftCall(t, h, member, "POST", "/api/bot-drafts/"+id+"/confirm", false); code != 403 { + t.Fatalf("cross-origin confirm = %d", code) + } + code, done := draftCall(t, h, member, "POST", "/api/bot-drafts/"+id+"/confirm", true) + if code != 200 || done["status"] != repo.BotDraftExecuted { + t.Fatalf("confirm = %d %v", code, done) + } + if len(s.Issues()) != before+1 { + t.Fatalf("issues = %d, want %d", len(s.Issues()), before+1) + } + // A repeated click returns the current state and writes nothing. + code, again := draftCall(t, h, member, "POST", "/api/bot-drafts/"+id+"/confirm", true) + if code != 200 || again["status"] != repo.BotDraftExecuted || len(s.Issues()) != before+1 { + t.Fatalf("repeat = %d %v issues=%d", code, again, len(s.Issues())) + } + // Cancel by the user. + second := ask() + if _, out := draftCall(t, h, member, "POST", "/api/bot-drafts/"+second+"/cancel", true); out["status"] != repo.BotDraftCancelled { + t.Fatalf("cancel = %v", out) + } + if _, out := draftCall(t, h, member, "POST", "/api/bot-drafts/"+second+"/confirm", true); out["status"] != repo.BotDraftCancelled || len(s.Issues()) != before+1 { + t.Fatalf("confirm after cancel = %v", out) + } +} + +func TestWeComConfirmDraftsInvalidatedByRevocationAndModeChange(t *testing.T) { + s, h, bot, ask := confirmModeFixture(t) + member, _ := s.User(43) + before := len(s.Issues()) + // Losing project permission invalidates at confirmation. + id := ask() + project, _ := s.Project("p") + project.Members = []domain.ProjectMember{} + s.PutProject(project) + if code, out := draftCall(t, h, member, "POST", "/api/bot-drafts/"+id+"/confirm", true); code != 409 || out["code"] != "draft_invalidated" { + t.Fatalf("after revoke = %d %v", code, out) + } + project.Members = []domain.ProjectMember{{UserID: 43, Role: "member"}} + s.PutProject(project) + // Unbinding cancels pending drafts of that sender. + id = ask() + binding, err := s.BotUserBinding(bot.IdentityScope(), "new-wecom-user") + if err != nil { + t.Fatal(err) + } + if err := s.RevokeBotUserBinding(binding.ID, 43, time.Now().UTC()); err != nil { + t.Fatal(err) + } + if _, out := draftCall(t, h, member, "POST", "/api/bot-drafts/"+id+"/confirm", true); out["status"] != repo.BotDraftCancelled || !strings.Contains(out["reason"].(string), "绑定") { + t.Fatalf("after unbind = %v", out) + } + // Leaving the confirm mode cancels pending drafts (another sender). + bindBotTestUser(t, s, bot, "second-sender", 43) + reply, err := h.generateBotReply(context.Background(), bot, domain.BotConversation{SessionID: "bot-session", ChatType: "single"}, nil, domain.BotInteraction{ID: "turn-mode", SenderID: "second-sender", Content: "帮我建个需求"}) + m := botDraftLink.FindStringSubmatch(reply) + if err != nil || m == nil { + t.Fatal(reply, err) + } + bot, _ = s.NotificationBot(bot.ID) + bot.WriteApproval = domain.BotWriteReadOnly + if err := s.SaveNotificationBot(bot, ""); err != nil { + t.Fatal(err) + } + if _, out := draftCall(t, h, member, "GET", "/api/bot-drafts/"+m[1], false); out["status"] != repo.BotDraftCancelled { + t.Fatalf("after mode change = %v", out) + } + if len(s.Issues()) != before { + t.Fatal("an invalidated draft was executed") + } +} + +func TestBotPendingDraftStateMachine(t *testing.T) { + s, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + now := time.Now().UTC() + d, err := s.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: "c", IdentityScope: "c", SenderID: "u", ProjectID: "p", UserID: 7, Summary: "1. x", Drafts: `[{"tool":"issues_save"}]`}, now) + if err != nil { + t.Fatal(err) + } + if _, err := s.BeginBotPendingDraft(d.ID, 8, now); err == nil { + t.Fatal("other user began the draft") + } + if _, err := s.BeginBotPendingDraft(d.ID, 7, now); err != nil { + t.Fatal(err) + } + if got, err := s.BeginBotPendingDraft(d.ID, 7, now); err != repo.ErrBotDraftNotPending || got.Status != repo.BotDraftExecuting { + t.Fatalf("second begin = %+v %v", got, err) + } + // Concurrent confirmations: exactly one moves the draft to executing. + c, _ := s.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: "c", UserID: 7, Drafts: "[]"}, now) + results := make(chan error, 8) + for i := 0; i < 8; i++ { + go func() { _, err := s.BeginBotPendingDraft(c.ID, 7, now); results <- err }() + } + won := 0 + for i := 0; i < 8; i++ { + if err := <-results; err == nil { + won++ + } + } + if won != 1 { + t.Fatalf("concurrent confirmations executed %d times", won) + } + // Interrupted while executing: reported for review, never re-run. + if got, _ := s.BotPendingDraft(d.ID, now.Add(11*time.Minute)); got.Status != repo.BotDraftNeedsReview { + t.Fatalf("stale executing = %+v", got) + } + if _, err := s.BeginBotPendingDraft(d.ID, 7, now.Add(12*time.Minute)); err != repo.ErrBotDraftNotPending { + t.Fatalf("needs_review began again: %v", err) + } + // Expiry. + e, _ := s.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: "c", UserID: 7, Drafts: "[]"}, now) + if got, err := s.BeginBotPendingDraft(e.ID, 7, now.Add(repo.BotDraftTTL)); err != repo.ErrBotDraftNotPending || got.Status != repo.BotDraftExpired { + t.Fatalf("expired begin = %+v %v", got, err) + } + // Tampered frozen parameters are refused. + tampered, _ := s.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: "c", UserID: 7, Drafts: `[{"tool":"issues_save"}]`}, now) + if err := s.DB.Table("bot_pending_drafts").Where("id = ?", tampered.ID).Update("drafts", `[{"tool":"projects_delete"}]`).Error; err != nil { + t.Fatal(err) + } + if got, err := s.BeginBotPendingDraft(tampered.ID, 7, now); err != repo.ErrBotDraftNotPending || got.Status != repo.BotDraftCancelled { + t.Fatalf("tampered = %+v %v", got, err) + } +} + +var _ = http.MethodGet + +// New requirements from chat show their SOP; the confirmation page can +// switch to another SOP of the project and start it right away. +func TestBotDraftChoosesSOPAndStartsIt(t *testing.T) { + s, h, _, ask, last := confirmModeFixtureFor(t, domain.BotChannelFeishu) + member, _ := s.User(43) + if err := s.PutAgent(domain.AgentDefinition{ID: "dev-agent", Name: "开发"}); err != nil { + t.Fatal(err) + } + for _, wf := range []domain.WorkflowDefinition{ + {ID: "wf-dev", Name: "研发交付", ProjectID: "p", Status: "published", Nodes: []domain.WorkflowNode{{ID: "n1", Name: "开发", Type: "agent", AgentID: "dev-agent"}}}, + {ID: "wf-fix", Name: "缺陷修复", ProjectID: "p", Status: "published", Nodes: []domain.WorkflowNode{{ID: "n1", Name: "修复", Type: "agent", AgentID: "dev-agent"}}}, + {ID: "wf-other", Name: "别的项目", ProjectID: "q", Status: "published"}, + } { + if err := s.PutWorkflow(wf); err != nil { + t.Fatal(err) + } + } + project, _ := s.Project("p") + project.WorkflowID = "wf-dev" + s.PutProject(project) + id := ask() + if !strings.Contains(*last, "(SOP:项目默认 · 研发交付)") || !strings.Contains(*last, "确认页可以更换 SOP") { + t.Fatalf("summary without SOP: %q", *last) + } + _, view := draftCall(t, h, member, "GET", "/api/bot-drafts/"+id, false) + issues, _ := view["newIssues"].([]any) + if len(issues) != 1 || issues[0].(map[string]any)["workflowId"] != "" || issues[0].(map[string]any)["defaultWorkflowId"] != "wf-dev" || issues[0].(map[string]any)["defaultWorkflowName"] != "研发交付" { + t.Fatalf("new issues = %v", view["newIssues"]) + } + names := []string{} + for _, wf := range view["workflows"].([]any) { + names = append(names, wf.(map[string]any)["id"].(string)) + } + if strings.Contains(strings.Join(names, ","), "wf-other") || !strings.Contains(strings.Join(names, ","), "wf-fix") { + t.Fatalf("workflows offered = %v", names) + } + // A foreign SOP is refused and nothing is created. + before := len(s.Issues()) + other := ask() + if _, out := draftChoice(t, h, member, other, `{"choices":[{"index":0,"workflowId":"wf-other","start":true}]}`); out["status"] != repo.BotDraftFailed || len(s.Issues()) != before { + t.Fatalf("foreign SOP = %v", out) + } + code, out := draftChoice(t, h, member, id, `{"choices":[{"index":0,"workflowId":"wf-fix","start":true}]}`) + if code != 200 { + t.Fatalf("confirm = %d %v", code, out) + } + var created domain.Issue + for _, issue := range s.Issues() { + if strings.HasPrefix(issue.Title, "网页确认的需求") && issue.WorkflowID == "wf-fix" { + created = issue + } + } + if created.ID == "" { + t.Fatalf("issue not bound to the chosen SOP: %v", out) + } + results := strings.Join(toStrings(out["results"]), "\n") + if !strings.Contains(results, "已完成:保存需求:网页确认的需求") || !strings.Contains(results, "缺陷修复") { + t.Fatalf("results = %q", results) + } + started := false + for _, task := range s.Tasks() { + if task.IssueID == created.ID && task.WorkflowID == "wf-fix" { + started = true + } + } + if !started || !strings.Contains(results, "已启动:缺陷修复") { + t.Fatalf("SOP not started: %q", results) + } +} + +func draftChoice(t *testing.T, h *assistantHandlers, user domain.User, id, body string) (int, map[string]any) { + t.Helper() + req := httptest.NewRequest("POST", "/api/bot-drafts/"+id+"/confirm", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + req = authedRequest(t, req, user) + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + c.Request = req + c.Params = gin.Params{{Key: "id", Value: id}} + h.botDraft(c) + out := map[string]any{} + json.Unmarshal(w.Body.Bytes(), &out) + return w.Code, out +} + +func toStrings(v any) []string { + list, _ := v.([]any) + out := make([]string, 0, len(list)) + for _, item := range list { + s, _ := item.(string) + out = append(out, s) + } + return out +} + +// The Feishu confirmation card: "确认并启动 SOP" creates the requirement and +// starts its SOP once; other people's clicks and later clicks do nothing; +// "取消" cancels. +func TestFeishuDraftCardButtons(t *testing.T) { + s, h, bot, ask, _ := confirmModeFixtureFor(t, domain.BotChannelFeishu) + if err := s.PutAgent(domain.AgentDefinition{ID: "dev-agent", Name: "开发"}); err != nil { + t.Fatal(err) + } + if err := s.PutWorkflow(domain.WorkflowDefinition{ID: "wf-dev", Name: "研发交付", ProjectID: "p", Status: "published", Nodes: []domain.WorkflowNode{{ID: "n1", Name: "开发", Type: "agent", AgentID: "dev-agent"}}}); err != nil { + t.Fatal(err) + } + project, _ := s.Project("p") + project.WorkflowID = "wf-dev" + s.PutProject(project) + bindBotTestUser(t, s, bot, "ou-stranger", 42) + click := func(sender, action, id string) string { + t.Helper() + reply, err := h.handleBotCardAction(context.Background(), bot, domain.BotInteraction{SenderID: sender, Action: action, DeliveryID: id}, domain.BotConversation{ChatID: sender, ChatType: "single"}) + if err != nil { + t.Fatal(reply, err) + } + return reply + } + before := len(s.Issues()) + id := ask() + if reply := click("ou-stranger", botinteraction.DraftActionConfirm, id); !strings.Contains(reply, "不是你发起的") || len(s.Issues()) != before { + t.Fatalf("stranger = %q", reply) + } + reply := click("new-wecom-user", botinteraction.DraftActionConfirmStart, id) + if !strings.Contains(reply, "操作已执行") || !strings.Contains(reply, "已启动:研发交付") || len(s.Issues()) != before+1 { + t.Fatalf("confirm and start = %q", reply) + } + started := 0 + for _, task := range s.Tasks() { + if task.WorkflowID == "wf-dev" { + started++ + } + } + if started != 1 { + t.Fatalf("tasks = %d", started) + } + if again := click("new-wecom-user", botinteraction.DraftActionConfirm, id); !strings.Contains(again, "操作已执行") || len(s.Issues()) != before+1 { + t.Fatalf("repeat = %q", again) + } + second := ask() + if reply := click("new-wecom-user", botinteraction.DraftActionCancel, second); !strings.Contains(reply, "已取消") { + t.Fatalf("cancel = %q", reply) + } + if reply := click("new-wecom-user", botinteraction.DraftActionConfirm, second); !strings.Contains(reply, "已取消") || len(s.Issues()) != before+1 { + t.Fatalf("confirm after cancel = %q", reply) + } +} + +// The model named an SOP; the user switches back to the project default on +// the page, so the requirement is saved without workflowId and gets the +// project default. Generic templates are offered too. +func TestBotDraftDefaultSOPOption(t *testing.T) { + s, h, _, _, _ := confirmModeFixtureFor(t, domain.BotChannelFeishu) + member, _ := s.User(43) + for _, wf := range []domain.WorkflowDefinition{ + {ID: "wf-dev", Name: "研发交付", ProjectID: "p", Status: "published"}, + {ID: "wf-fix", Name: "缺陷修复", ProjectID: "p", Status: "draft"}, + } { + if err := s.PutWorkflow(wf); err != nil { + t.Fatal(err) + } + } + project, _ := s.Project("p") + project.WorkflowID = "wf-dev" + s.PutProject(project) + body, _ := json.Marshal(map[string]any{"projectId": "p", "title": "点名 SOP 的需求", "workflowId": "wf-fix"}) + raw, _ := json.Marshal([]assistantDraft{{Tool: "issues_save", Body: body}}) + draft, err := s.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: "b-feishu", ProjectID: "p", UserID: 43, SenderID: "new-wecom-user", Summary: "1. 保存需求:点名 SOP 的需求(SOP:缺陷修复)", Drafts: string(raw)}, time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + _, view := draftCall(t, h, member, "GET", "/api/bot-drafts/"+draft.ID, false) + issue := view["newIssues"].([]any)[0].(map[string]any) + if issue["workflowId"] != "wf-fix" || issue["defaultWorkflowName"] != "研发交付" { + t.Fatalf("issue = %v", issue) + } + ids := []string{} + for _, wf := range view["workflows"].([]any) { + ids = append(ids, wf.(map[string]any)["id"].(string)) + } + if !strings.Contains(strings.Join(ids, ","), "wf-fix") { + t.Fatalf("draft SOP not offered: %v", ids) + } + if code, out := draftChoice(t, h, member, draft.ID, `{"choices":[{"index":0,"workflowId":""}]}`); code != 200 || out["status"] != repo.BotDraftExecuted { + t.Fatalf("confirm default = %d %v", code, out) + } + for _, issue := range s.Issues() { + if issue.Title == "点名 SOP 的需求" { + if issue.WorkflowID != "wf-dev" { + t.Fatalf("bound to %q, want the project default", issue.WorkflowID) + } + return + } + } + t.Fatal("requirement not created") +} diff --git a/internal/httpapi/bot_identities.go b/internal/httpapi/bot_identities.go index 1d58265a6..cd00f0e24 100644 --- a/internal/httpapi/bot_identities.go +++ b/internal/httpapi/bot_identities.go @@ -2,6 +2,7 @@ package httpapi import ( "agentflow/internal/domain" + "agentflow/internal/repo" "errors" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -33,6 +34,11 @@ func (h *botIdentityHandlers) currentUser(c *gin.Context) (domain.User, bool) { } return user, true } + +// binding serves the status and claim steps of the web confirmation. Both +// re-check that the configuration still exists and is enabled and that the +// signed-in account may use its project, and return what the page displays: +// channel, Bot, project and the masked original sender. func (h *botIdentityHandlers) binding(c *gin.Context) { user, ok := h.currentUser(c) if !ok { @@ -51,6 +57,11 @@ func (h *botIdentityHandlers) binding(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } + meta, err := h.bindingMeta(input.Token, user) + if err != nil { + c.JSON(403, gin.H{"error": err.Error()}) + return + } if strings.HasSuffix(c.Request.URL.Path, "/claim") && status != "confirmed" { if !botBindingSameOrigin(c) { c.JSON(403, gin.H{"error": "请从平台绑定页面操作"}) @@ -61,10 +72,49 @@ func (h *botIdentityHandlers) binding(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - c.JSON(200, gin.H{"status": "claimed", "loginName": user.LoginName, "command": "/bind " + code}) + meta["status"], meta["loginName"], meta["command"] = "claimed", user.LoginName, "/bind "+code + c.JSON(200, meta) return } - c.JSON(200, gin.H{"status": status, "loginName": user.LoginName}) + meta["status"], meta["loginName"] = status, user.LoginName + c.JSON(200, meta) +} + +// bindingMeta loads the configuration of a link and verifies it is usable by +// user. Legacy WeCom links without a configuration show the channel only. +func (h *botIdentityHandlers) bindingMeta(token string, user domain.User) (gin.H, error) { + configID, channel, mask, err := h.repo.BotBindingChallengeInfo(token) + if err != nil { + return nil, err + } + meta := gin.H{"channel": domain.NotificationBot{Channel: channel}.ChannelKind(), "channelName": botChannelName(domain.NotificationBot{Channel: channel}), "sender": mask} + if configID == "" { + return meta, nil + } + bot, err := h.repo.NotificationBot(configID) + if err != nil || !bot.Enabled || !bot.InteractionEnabled { + return nil, errors.New("该机器人已停用或删除,绑定链接失效") + } + project, ok := h.repo.Project(bot.ProjectID) + if !ok || !botProjectMember(project, user, h.repo.IsPlatformAdmin(user.LoginName)) { + return nil, errors.New("当前平台账号没有该机器人所属项目的执行权限") + } + meta["botName"], meta["projectName"] = bot.Name, project.Name + return meta, nil +} + +// botProjectMember mirrors botExecutionUser: owner, platform admin or a +// non-viewer member. +func botProjectMember(project domain.Project, user domain.User, admin bool) bool { + if admin || project.OwnerID == user.ID { + return true + } + for _, member := range project.Members { + if member.UserID == user.ID && !strings.EqualFold(strings.TrimSpace(member.Role), "viewer") { + return true + } + } + return false } func botBindingSameOrigin(c *gin.Context) bool { origin, err := url.Parse(c.GetHeader("Origin")) @@ -80,6 +130,16 @@ func (h *botIdentityHandlers) list(c *gin.Context) { respond(c, err) return } + for i := range bindings { + if bindings[i].ConfigID != "" { + if bot, err := h.repo.NotificationBot(bindings[i].ConfigID); err == nil { + bindings[i].BotName = bot.Name + } + } + if bindings[i].Channel == "" { + bindings[i].Channel = domain.BotChannelWeCom + } + } c.Header("Cache-Control", "no-store") c.JSON(200, bindings) } @@ -109,13 +169,15 @@ func botIdentityCommand(content string) string { } // Only platform login nominates an account. The trusted callback from the -// original WeCom sender must submit the browser-only code before it takes effect. +// original sender must submit the browser-only code before it takes effect. func (h *botIdentityHandlers) message(bot domain.NotificationBot, item domain.BotInteraction) (bool, string, error) { text := botIdentityCommand(item.Content) + channel := botChannelName(bot) + scope := bot.IdentityScope() if text == "/unbind" || text == "解除绑定" { - binding, err := h.repo.BotUserBinding(bot.BotID, item.SenderID) + binding, err := h.repo.BotUserBinding(scope, item.SenderID) if errors.Is(err, gorm.ErrRecordNotFound) { - return true, "当前企微账号尚未绑定平台账号。发送“绑定账号”可进行关联。", nil + return true, "当前" + channel + "账号尚未绑定平台账号。发送“绑定账号”可进行关联。", nil } if err != nil { return true, "", err @@ -123,11 +185,11 @@ func (h *botIdentityHandlers) message(bot domain.NotificationBot, item domain.Bo if err := h.repo.RevokeBotUserBinding(binding.ID, binding.UserID, time.Now().UTC()); err != nil { return true, "", err } - return true, "已解除当前企微账号与平台账号的绑定,旧确认码已失效。需要重新关联时,请发送“绑定账号”。", nil + return true, "已解除当前" + channel + "账号与平台账号的绑定,旧确认码已失效。需要重新关联时,请发送“绑定账号”。", nil } if strings.HasPrefix(text, "/bind ") { - binding, err := h.repo.ConfirmBotUserBinding(bot.BotID, item.SenderID, strings.TrimSpace(strings.TrimPrefix(text, "/bind ")), time.Now().UTC()) + binding, err := h.repo.ConfirmBotUserBinding(bot, item.SenderID, strings.TrimSpace(strings.TrimPrefix(text, "/bind ")), time.Now().UTC()) if err != nil { return true, err.Error(), nil } @@ -137,14 +199,15 @@ func (h *botIdentityHandlers) message(bot domain.NotificationBot, item domain.Bo } return true, "已绑定平台账号「" + user.LoginName + "」。后续操作将使用你的身份、个人凭据和项目权限。请重新发送刚才的请求。", nil } - if text == "绑定账号" || text == "/bind" { - if binding, err := h.repo.BotUserBinding(bot.BotID, item.SenderID); err == nil { + explicit := text == "绑定账号" || text == "/bind" + if explicit { + if binding, err := h.repo.BotUserBinding(scope, item.SenderID); err == nil { if user, ok := h.repo.User(binding.UserID); ok { return true, "当前已绑定「" + user.LoginName + "」。发送 /unbind 或在平台个人配置中解除绑定。", nil } } } - _, err := h.repo.BotUserBinding(bot.BotID, item.SenderID) + _, err := h.repo.BotUserBinding(scope, item.SenderID) if err == nil { return false, "", nil } @@ -155,10 +218,21 @@ func (h *botIdentityHandlers) message(bot domain.NotificationBot, item domain.Bo if err != nil || (base.Scheme != "http" && base.Scheme != "https") || base.Host == "" { return true, "请先关联平台账号;平台外部访问地址尚未配置,请联系管理员。", nil } - token, err := h.repo.NewBotBindingChallenge(bot.BotID, item.SenderID, time.Now().UTC()) - if err != nil { + token, err := h.repo.NewBotBindingChallenge(bot, item.SenderID, time.Now().UTC(), explicit) + switch { + case errors.Is(err, repo.ErrBotBindingPending): + return true, "请先打开上一条消息中的绑定链接完成确认(15 分钟内有效)。需要新链接时发送“绑定账号”。", nil + case errors.Is(err, repo.ErrBotBindingRateLimited): + return true, "获取绑定链接过于频繁,请稍后再试。", nil + case err != nil: return true, "", err } link := strings.TrimRight(h.publicURL, "/") + "/bot-bind.html#request=" + url.QueryEscape(token) - return true, "请先[关联你的平台账号](" + link + ")(15 分钟内有效)。登录确认后,将页面上的 /bind 确认码发回当前 Bot。群里其他人点击链接不会直接完成绑定。", nil + if bot.ChannelKind() != domain.BotChannelWeCom { + // Feishu and DingTalk replies are plain text: Markdown link syntax + // is shown verbatim and auto-linking swallows the closing bracket, + // so the URL stands alone on its own line. + return true, "请先关联你的平台账号(链接 15 分钟内有效):\n" + link + "\n登录确认后,将页面上的 /bind 确认码发回当前 Bot。其他人点击链接不会直接完成绑定。", nil + } + return true, "请先[关联你的平台账号](" + link + ")(15 分钟内有效)。登录确认后,将页面上的 /bind 确认码发回当前 Bot。其他人点击链接不会直接完成绑定。", nil } diff --git a/internal/httpapi/bot_identities_test.go b/internal/httpapi/bot_identities_test.go index 8decce3bf..769de9d8d 100644 --- a/internal/httpapi/bot_identities_test.go +++ b/internal/httpapi/bot_identities_test.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "net/http" "net/http/httptest" + "regexp" "strings" "testing" "time" @@ -21,7 +22,7 @@ func TestBotBindingHTTPRequiresBrowserLoginOriginAndOriginalSender(t *testing.T) router.POST("/api/bot-bindings/status", h.binding) router.GET("/api/bot-bindings", h.list) router.DELETE("/api/bot-bindings/:id", h.remove) - token, err := s.NewBotBindingChallenge(bot.BotID, "original", time.Now().UTC()) + token, err := s.NewBotBindingChallenge(bot, "original", time.Now().UTC(), true) if err != nil { t.Fatal(err) } @@ -71,7 +72,7 @@ func TestBotBindingHTTPRequiresBrowserLoginOriginAndOriginalSender(t *testing.T) if _, err = s.BotUserBinding(bot.BotID, "original"); err == nil { t.Fatal("browser claim activated binding") } - if _, err = s.ConfirmBotUserBinding(bot.BotID, "group-attacker", strings.TrimPrefix(data.Command, "/bind "), time.Now().UTC()); err == nil { + if _, err = s.ConfirmBotUserBinding(bot, "group-attacker", strings.TrimPrefix(data.Command, "/bind "), time.Now().UTC()); err == nil { t.Fatal("wrong group sender confirmed") } handled, reply, err := h.message(bot, domain.BotInteraction{SenderID: "original", Content: data.Command}) @@ -108,11 +109,37 @@ func TestBotBindingLinkSurvivesNotificationRedaction(t *testing.T) { } } +// Feishu and DingTalk send plain text: the link must be a bare URL on its +// own line, or auto-linking keeps the Markdown ")" and the token breaks. +func TestBotBindingLinkIsBareURLOnPlainTextChannels(t *testing.T) { + for _, channel := range []string{domain.BotChannelFeishu, domain.BotChannelDingTalk} { + _, assistant, bot := botFixture(t) + bot.Channel = channel + h := &botIdentityHandlers{deps: assistant.deps, publicURL: "https://platform.example/"} + handled, reply, err := h.message(bot, domain.BotInteraction{SenderID: "plain-" + channel, Content: "绑定账号"}) + if err != nil || !handled { + t.Fatal(channel, err) + } + if strings.ContainsAny(reply, "[]()") { + t.Fatal(channel, "markdown link syntax in plain-text reply", reply) + } + var line string + for _, l := range strings.Split(reply, "\n") { + if strings.HasPrefix(l, "https://") { + line = l + } + } + if !regexp.MustCompile(`^https://platform\.example/bot-bind\.html#request=[a-f0-9]{64}$`).MatchString(line) { + t.Fatal(channel, "binding URL is not alone on its line", reply) + } + } +} + func TestBotUnbindOnlyRevokesCurrentSender(t *testing.T) { s, assistant, bot := botFixture(t) h := &botIdentityHandlers{deps: assistant.deps} now := time.Now().UTC() - token, err := s.NewBotBindingChallenge(bot.BotID, "unbind-test", now) + token, err := s.NewBotBindingChallenge(bot, "unbind-test", now, true) if err != nil { t.Fatal(err) } @@ -120,7 +147,7 @@ func TestBotUnbindOnlyRevokesCurrentSender(t *testing.T) { if err != nil { t.Fatal(err) } - if _, err = s.ConfirmBotUserBinding(bot.BotID, "unbind-test", code, now); err != nil { + if _, err = s.ConfirmBotUserBinding(bot, "unbind-test", code, now); err != nil { t.Fatal(err) } handled, reply, err := h.message(bot, domain.BotInteraction{SenderID: "unbound-other", Content: "/unbind"}) @@ -137,7 +164,7 @@ func TestBotUnbindOnlyRevokesCurrentSender(t *testing.T) { if _, err = s.BotUserBinding(bot.BotID, "unbind-test"); err == nil { t.Fatal("binding survived") } - if _, err = s.ConfirmBotUserBinding(bot.BotID, "unbind-test", code, now); err == nil { + if _, err = s.ConfirmBotUserBinding(bot, "unbind-test", code, now); err == nil { t.Fatal("old code rebound") } } diff --git a/internal/httpapi/bot_interaction_records.go b/internal/httpapi/bot_interaction_records.go index eecc9d1e4..b334958cd 100644 --- a/internal/httpapi/bot_interaction_records.go +++ b/internal/httpapi/bot_interaction_records.go @@ -1,6 +1,14 @@ package httpapi -import "github.com/gin-gonic/gin" +import ( + "agentflow/internal/domain" + "agentflow/internal/repo" + "errors" + "strings" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) type botInteractionHandlers struct { *deps @@ -12,7 +20,7 @@ func (h *botInteractionHandlers) records(c *gin.Context) { if !ok { return } - conn, _ := h.repo.BotConnectionState(bot.BotID) + conn, _ := h.repo.BotConnectionState(bot.LeaseKey()) chats, err := h.repo.BotConversations(bot.ID) if err != nil { respond(c, err) @@ -23,5 +31,157 @@ func (h *botInteractionHandlers) records(c *gin.Context) { respond(c, err) return } + if bot.ChannelKind() == domain.BotChannelDingTalk { + for i := range records { + records[i].PushResendAvailable = h.pushResendTarget(bot, records[i]) == nil + } + } c.JSON(200, gin.H{"connection": conn, "conversations": chats, "messages": records}) } + +// pending lists interactions of a project awaiting manual handling +// (reply_unknown, reply_failed), including those of deleted Bots, for +// project managers. +func (h *botInteractionHandlers) pending(c *gin.Context) { + projectID := strings.TrimSpace(c.Query("projectId")) + if !h.projectManager(c, projectID) { + return + } + items, err := h.repo.BotInteractionsAwaitingReview(projectID) + if err != nil { + respond(c, err) + return + } + c.JSON(200, items) +} + +// projectManager allows platform admins, the project owner and maintainers. +func (h *botInteractionHandlers) projectManager(c *gin.Context, projectID string) bool { + if !h.notifications.access(c, projectID, true) { + return false + } + if !authRequired() || h.isPlatformAdmin(c.Request) { + return true + } + user, ok := h.requestUser(c.Request, true) + project, _ := h.repo.Project(projectID) + if ok && user != nil { + if project.OwnerID == user.ID { + return true + } + for _, m := range project.Members { + if m.UserID == user.ID && strings.EqualFold(strings.TrimSpace(m.Role), "maintainer") { + return true + } + } + } + c.JSON(403, gin.H{"error": "只有机器人创建者或项目管理员可以处理待核对记录"}) + return false +} + +// act serves the manual "confirm received" and "send again" actions. An +// existing Bot requires its managers; a deleted Bot's records stay reachable +// by project managers through the project snapshot. The action key makes a +// repeated click return the same result. +func (h *botInteractionHandlers) act(c *gin.Context) { + item, err := h.repo.BotInteraction(c.Param("interactionId")) + if err != nil { + c.JSON(404, gin.H{"error": "记录不存在"}) + return + } + bot, botErr := h.repo.NotificationBot(item.BotID) + switch { + case botErr == nil: + if !h.notifications.managed(c, bot) { + return + } + case errors.Is(botErr, gorm.ErrRecordNotFound) && item.ProjectID != "": + if !h.projectManager(c, item.ProjectID) { + return + } + default: + c.JSON(404, gin.H{"error": "记录不存在"}) + return + } + if !botBindingSameOrigin(c) { + c.JSON(403, gin.H{"error": "请从平台页面操作"}) + return + } + var req struct { + ActionKey string `json:"actionKey"` + Reason string `json:"reason"` + } + if !decode(c, &req) { + return + } + req.ActionKey = strings.TrimSpace(req.ActionKey) + if req.ActionKey == "" || len(req.ActionKey) > 64 { + c.JSON(400, gin.H{"error": "缺少操作标识"}) + return + } + actor := int64(0) + if user, ok := h.requestUser(c.Request, true); ok && user != nil { + actor = user.ID + } + var out domain.BotInteraction + if strings.HasSuffix(c.Request.URL.Path, "/push-resend") { + if botErr != nil || bot.ChannelKind() != domain.BotChannelDingTalk { + c.JSON(409, gin.H{"error": "只有钉钉的过期回复可以改用主动单聊发送", "code": "state_conflict"}) + return + } + user, targetErr := h.pushResendUser(bot, item) + if targetErr != nil { + c.JSON(409, gin.H{"error": targetErr.Error(), "code": "push_target_unverified"}) + return + } + out, err = h.repo.PushResendBotInteraction(item.ID, actor, user.ID, req.ActionKey, truncateRunes(strings.TrimSpace(req.Reason), 60)) + if errors.Is(err, repo.ErrBotPushTargetUnverified) { + c.JSON(409, gin.H{"error": "原发送者的绑定已变化或尚未通过主动发送试发,无法改用主动单聊", "code": "push_target_unverified"}) + return + } + } else if strings.HasSuffix(c.Request.URL.Path, "/resend") { + out, err = h.repo.ResendBotInteraction(item.ID, actor, req.ActionKey, truncateRunes(strings.TrimSpace(req.Reason), 60)) + } else { + out, err = h.repo.ConfirmBotInteraction(item.ID, actor, req.ActionKey) + } + switch { + case errors.Is(err, repo.ErrBotDeleted): + c.JSON(409, gin.H{"error": "机器人已删除,无法再次发送;可确认已收到", "code": "bot_deleted"}) + case errors.Is(err, repo.ErrBotRouteExpired): + c.JSON(409, gin.H{"error": "会话回复授权已过期,无法在原会话再次发送", "code": "route_expired"}) + case errors.Is(err, repo.ErrBotStateConflict): + c.JSON(409, gin.H{"error": "记录状态已变化,请刷新后再试", "code": "state_conflict"}) + case err != nil: + respond(c, err) + default: + c.JSON(200, out) + } +} + +// pushResendUser checks that the original sender of an expired DingTalk +// reply is still bound to this configuration, has passed a trial proactive +// send and keeps execution permission on the project. The repository +// re-checks the binding inside the resend transaction. +func (h *botInteractionHandlers) pushResendUser(bot domain.NotificationBot, item domain.BotInteraction) (domain.User, error) { + binding, err := h.repo.BotPushTarget(bot, item.SenderID) + if err != nil || binding.TargetVerifiedAt == nil { + return domain.User{}, errors.New("原发送者还不是已试发成功的主动发送目标:请让其完成绑定,并在通知接收人中试发一次") + } + assistant := &assistantHandlers{deps: h.deps} + user, err := assistant.botExecutionUser(bot, item.SenderID) + if err != nil { + return user, err + } + if user.ID != binding.UserID { + return user, errors.New("原发送者的绑定已变化") + } + return user, nil +} + +func (h *botInteractionHandlers) pushResendTarget(bot domain.NotificationBot, item domain.BotInteraction) error { + if item.Status != domain.BotReplyFailed || item.TerminalReason != "route_expired" || item.BodyPurgedAt != nil { + return errors.New("not expired") + } + _, err := h.pushResendUser(bot, item) + return err +} diff --git a/internal/httpapi/bot_interactions.go b/internal/httpapi/bot_interactions.go index c48d0b066..85db56e0d 100644 --- a/internal/httpapi/bot_interactions.go +++ b/internal/httpapi/bot_interactions.go @@ -4,26 +4,33 @@ import ( "agentflow/internal/botinteraction" "agentflow/internal/domain" "agentflow/internal/engine" + "agentflow/internal/repo" "context" "errors" "fmt" "strings" ) +// botExecutionUser re-reads the configuration and the binding on every turn +// and requires a live project role, so unbinding, disabling the Bot or +// removing project access applies to the next turn. func (h *assistantHandlers) botExecutionUser(bot domain.NotificationBot, senderID string) (domain.User, error) { current, err := h.repo.NotificationBot(bot.ID) - if err != nil || !current.Enabled || !current.InteractionEnabled || current.BotID != bot.BotID { + if err != nil || !current.Enabled || !current.InteractionEnabled || current.BotID != bot.BotID || current.ChannelKind() != bot.ChannelKind() { return domain.User{}, errors.New("Bot 对话已停用") } - binding, err := h.repo.BotUserBinding(bot.BotID, senderID) + binding, err := h.repo.BotUserBinding(current.IdentityScope(), senderID) if err != nil { return domain.User{}, errors.New("请先绑定平台账号") } + if current.ChannelKind() != domain.BotChannelWeCom && binding.ConfigID != current.ID { + return domain.User{}, errors.New("请先绑定平台账号") + } user, ok := h.repo.User(binding.UserID) if !ok { return user, errors.New("绑定的平台账号不可用") } - project, ok := h.repo.Project(bot.ProjectID) + project, ok := h.repo.Project(current.ProjectID) if !ok { return user, errors.New("Bot 项目不存在") } @@ -38,6 +45,18 @@ func (h *assistantHandlers) botExecutionUser(bot domain.NotificationBot, senderI return user, errors.New("你的平台账号没有该项目的执行权限") } +// botChannelName is the user-facing IM product name of a channel. +func botChannelName(bot domain.NotificationBot) string { + switch bot.ChannelKind() { + case domain.BotChannelFeishu: + return "飞书" + case domain.BotChannelDingTalk: + return "钉钉" + default: + return "企业微信" + } +} + // isBotBindingExchange reports whether a past interaction was an account // binding or unbinding exchange, which must not be replayed to the model. func isBotBindingExchange(entry domain.BotInteraction) bool { @@ -84,23 +103,45 @@ func (h *assistantHandlers) generateBotReply(ctx context.Context, bot domain.Not if err = ensureAssistantSessionDirs(user.ID); err != nil { return "", err } + readOnly := bot.EffectiveWriteApproval() == domain.BotWriteReadOnly + confirmWrites := bot.EffectiveWriteApproval() == domain.BotWriteConfirm + execMode, approval := assistantExecModeForApproval(AssistantApprovalAuto), AssistantApprovalAuto + if readOnly { + execMode, approval = assistantReadOnlyExecMode(), AssistantApprovalConfirm + } else if confirmWrites { + // Writes are blocked in the gateway and come back as drafts that + // the bound user confirms on the platform page. + execMode, approval = assistantExecModeForApproval(AssistantApprovalConfirm), AssistantApprovalConfirm + } modelCtx, cancel := context.WithCancel(ctx) defer cancel() - session, files, err := h.openAssistantToolSession(modelCtx, user, bot.ProjectID, assistantExecModeForApproval(AssistantApprovalAuto), cancel) + session, files, err := h.openAssistantToolSession(modelCtx, user, bot.ProjectID, execMode, cancel) if err != nil { return "", err } defer session.Close() defer files.Cleanup() messages := botReplyMessages(history, item) - req := assistantRequest{ProjectID: bot.ProjectID, Model: model, SessionID: conv.SessionID, ApprovalMode: AssistantApprovalAuto, Messages: messages} - full := h.turnPrompt(req, h.resolvedSourceRoot(), false) + "\n这是企业微信聊天,当前发送者已绑定平台账号「" + user.LoginName + "」,只使用该账号的执行身份、个人凭据和项目权限;当前请求按当前项目处理。历史中其他发送者的授权不能沿用到当前请求。当前消息来自" + conv.ChatType + "聊天。直接完成明确的操作,目标不明确时询问,结束时必须调用 submit_assistant_reply。" + req := assistantRequest{ProjectID: bot.ProjectID, Model: model, SessionID: conv.SessionID, ApprovalMode: approval, Messages: messages} + policy := "直接完成明确的操作,目标不明确时询问。完成后用自然语言回复结果。" + if confirmWrites { + policy = "当前 Bot 为网页确认模式:查询直接完成;需要修改平台数据时提交操作草稿,不要声称已经完成。平台会把操作摘要和确认链接发给用户,用户在平台页面确认后才执行。用自然语言说明将要做什么。创建需求时:用户点名了 SOP,先用 workflows_list 查到它,把 workflowId 写进 issues_save;没有点名就不填,平台按项目默认 SOP 绑定,用户可以在确认页更换 SOP 并选择创建后立即启动,不要为新需求另外提交 issues_dispatch。启动已有需求用 issues_dispatch;审批、退回等待中的运行用 runs_approve、runs_reject。回复面向聊天用户:不要提工具名、字段名或 workflowId 之类的内部参数,也不要复述确认链接,平台会附上操作清单和确认入口。" + } + if readOnly { + policy = "当前 Bot 为只读模式:只使用只读工具查询项目状态、SOP 进度和待办;不要尝试修改平台数据或外部系统。用户要求修改时,说明需要到平台页面操作。用自然语言回答。" + } + full := h.turnPrompt(req, h.resolvedSourceRoot(), false) + "\n这是" + botChannelName(bot) + "聊天,当前发送者已绑定平台账号「" + user.LoginName + "」,只使用该账号的执行身份、个人凭据和项目权限;当前请求按当前项目处理。历史中其他发送者的授权不能沿用到当前请求。当前消息来自" + conv.ChatType + "聊天。" + policy generate := h.generate if generate == nil { cliSessionID := newAssistantSessionID() + engine, _ := assistantModelEngine(model) resume := false + tracker := &botNativeTracker{store: h.repo, in: repo.BotNativeSession{ConfigID: bot.ID, ProjectID: bot.ProjectID, UserID: user.ID, ConversationID: conv.ID, BotSessionID: conv.SessionID, InteractionID: item.ID, Engine: engine, RequestedSessionID: cliSessionID, StorageScope: "assistant-cli-home"}} generate = func(callCtx context.Context, prompt string, u domain.User) (string, error) { - raw, _, err := runAssistantCLI(callCtx, assistantCLIRequest{Prompt: prompt, Model: model, SessionID: cliSessionID, Resume: resume, UserID: u.ID, Native: true, MCPConfig: files.MCPConfig, Settings: files.Settings, SystemPrompt: assistantSystemPromptForMode(AssistantApprovalAuto)}, u, func(ev assistantStreamEvent) { + if err := tracker.start(); err != nil { + return "", err + } + raw, nativeID, err := runAssistantCLI(callCtx, assistantCLIRequest{Prompt: prompt, Model: model, SessionID: cliSessionID, Resume: resume, UserID: u.ID, Native: true, MCPConfig: files.MCPConfig, Settings: files.Settings, SystemPrompt: assistantSystemPromptForMode(approval)}, u, func(ev assistantStreamEvent) { switch ev.Kind { case "tool": if strings.Contains(ev.Tool, "submit_assistant_reply") { @@ -114,6 +155,7 @@ func (h *assistantHandlers) generateBotReply(ctx context.Context, bot domain.Not botinteraction.ReportProgress(callCtx, "正在分析你的请求") } }) + tracker.finish(callCtx, nativeID, err) resume = true return raw, err } @@ -125,9 +167,30 @@ func (h *assistantHandlers) generateBotReply(ctx context.Context, bot domain.Not botinteraction.ReportProgress(ctx, "正在提交回复") session.SetTerminalOnly() } - _, cliErr := generate(modelCtx, prompt, user) - if final, ok := session.takeFinal(); ok && final.Assistant != nil { + raw, cliErr := generate(modelCtx, prompt, user) + final, ok := session.takeFinal() + if !ok || final.Assistant == nil { + // A completed conversational answer needs no terminal tool call + // (submit_assistant_reply only accepts drafts). Only plain text + // from a successful run is accepted: output carrying protocol + // JSON (a draft attempt, possibly filtered during parsing) and + // progress-only text still go through the terminal retry. + if cand, recovered := recoverNativeAssistantReply(raw, bot.ProjectID); cliErr == nil && recovered && !assistantProtocolJSONPresent(raw) && len(cand.Drafts) == 0 && !assistantProgressOnly(cand.Message, messages) { + final, ok = AgentTerminalResult{Kind: AgentTerminalAssistant, Assistant: &cand}, true + } + } + if ok && final.Assistant != nil { reply := *final.Assistant + if len(reply.Drafts) > 0 && readOnly { + // Read-only Bots never execute drafts; the user confirms + // writes on the platform page. + reply.Message = strings.TrimSpace(reply.Message) + "\n当前 Bot 为只读模式,修改操作没有执行。请在平台页面确认并完成操作。" + reply.Drafts = nil + } + if len(reply.Drafts) > 0 && confirmWrites { + reply.Message = strings.TrimSpace(reply.Message) + h.confirmBotDrafts(ctx, bot, user, item, reply.Drafts) + reply.Drafts = nil + } if len(reply.Drafts) > 0 { reply.Drafts = h.resolveAssistantDrafts(ctx, user, bot.ProjectID, reply.Drafts, AssistantApprovalAuto) for _, draft := range reply.Drafts { @@ -158,6 +221,9 @@ func (h *assistantHandlers) handleBotCardAction(ctx context.Context, bot domain. } return reply, botinteraction.ErrCardActionDeferred } + if botinteraction.DraftAction(item.Action) { + return h.botDraftCardAction(ctx, bot, item) + } user, err := h.botExecutionUser(bot, item.SenderID) if err != nil { return err.Error(), botinteraction.ErrCardActionDeferred @@ -181,7 +247,7 @@ func (h *assistantHandlers) handleBotCardAction(ctx context.Context, bot domain. return "节点已不存在。", nil } expected := engine.NodeExpectation{NodeID: delivery.NodeID, Status: node.Status, Attempt: delivery.Attempt, ReworkCount: delivery.ReworkCount} - note := "企业微信操作,平台账号:" + user.LoginName + ",发送者:" + item.SenderID + note := botChannelName(bot) + "操作,平台账号:" + user.LoginName + ",发送者:" + item.SenderID switch item.Action { case "approve": err = h.engine.Approve(delivery.RunID, delivery.NodeID, note, expected) @@ -203,3 +269,42 @@ func (h *assistantHandlers) handleBotCardAction(ctx context.Context, bot domain. } return fmt.Sprintf("「%s」操作已完成:%s。", delivery.SourceName, map[string]string{"approve": "确认通过,继续流程", "retry": "重试当前节点", "pass": "手动通过节点", "decline": "不再重试", "resume": "继续运行", "reject": "退回上游"}[item.Action]), nil } + +// botNativeTracker maps a Bot turn to the native CLI sessions it runs in. +// start writes the mapping before the CLI process starts (a failure stops the +// turn: an unmapped Bot log could never be located or cleaned); finish +// records the native session ID the CLI reported, adding a row when a retry +// actually created a different session. +type botNativeTracker struct { + store interface { + StartBotNativeSession(repo.BotNativeSession) (string, error) + FinishBotNativeSession(id, nativeSessionID, status string) (string, error) + } + in repo.BotNativeSession + id string +} + +func (t *botNativeTracker) start() error { + if t.id != "" { + return nil + } + id, err := t.store.StartBotNativeSession(t.in) + if err != nil { + return fmt.Errorf("Bot 会话追踪记录写入失败,本轮未启动:%w", err) + } + t.id = id + return nil +} + +func (t *botNativeTracker) finish(ctx context.Context, nativeID string, err error) { + status := "completed" + if err != nil { + status = "failed" + if ctx.Err() != nil { + status = "interrupted" + } + } + if id, finishErr := t.store.FinishBotNativeSession(t.id, nativeID, status); finishErr == nil { + t.id = id + } +} diff --git a/internal/httpapi/bot_interactions_test.go b/internal/httpapi/bot_interactions_test.go index 21136cb33..e625be39d 100644 --- a/internal/httpapi/bot_interactions_test.go +++ b/internal/httpapi/bot_interactions_test.go @@ -132,7 +132,8 @@ func TestBotMissingSubmissionRetriesOnlyTerminalStep(t *testing.T) { h.generate = func(ctx context.Context, prompt string, user domain.User) (string, error) { calls++ if calls == 1 { - return "查询结果已经准备好,但未提交终态", nil + // No answer and no terminal call: only the terminal step reruns. + return "", nil } if strings.Contains(prompt, "原始查询标记") || !strings.Contains(prompt, "不要重新查询") { t.Fatal("retry restarted task", prompt) @@ -157,10 +158,25 @@ func TestBotMissingSubmissionRetriesOnlyTerminalStep(t *testing.T) { } } +// A completed conversational answer needs no terminal tool call: the Bot +// accepts it like the web assistant instead of retrying until it fails. +func TestBotAcceptsNaturalAnswerWithoutTerminalCall(t *testing.T) { + _, h, bot := botFixture(t) + calls := 0 + h.generate = func(ctx context.Context, prompt string, user domain.User) (string, error) { + calls++ + return "项目当前有 2 个运行中的 SOP,均在实现节点。", nil + } + reply, err := h.generateBotReply(context.Background(), bot, domain.BotConversation{SessionID: "bot-session", ChatType: "single"}, nil, domain.BotInteraction{SenderID: "new-wecom-user", Content: "项目状态"}) + if err != nil || calls != 1 || !strings.Contains(reply, "2 个运行中的 SOP") { + t.Fatal(reply, err, calls) + } +} + func bindBotTestUser(t *testing.T, s *repo.Store, bot domain.NotificationBot, sender string, userID int64) { t.Helper() now := time.Now().UTC() - token, err := s.NewBotBindingChallenge(bot.BotID, sender, now) + token, err := s.NewBotBindingChallenge(bot, sender, now, true) if err != nil { t.Fatal(err) } @@ -168,7 +184,7 @@ func bindBotTestUser(t *testing.T, s *repo.Store, bot domain.NotificationBot, se if err != nil { t.Fatal(err) } - if _, err = s.ConfirmBotUserBinding(bot.BotID, sender, code, now); err != nil { + if _, err = s.ConfirmBotUserBinding(bot, sender, code, now); err != nil { t.Fatal(err) } } diff --git a/internal/httpapi/bot_native_cleanup.go b/internal/httpapi/bot_native_cleanup.go new file mode 100644 index 000000000..c436225f6 --- /dev/null +++ b/internal/httpapi/bot_native_cleanup.go @@ -0,0 +1,211 @@ +package httpapi + +import ( + "context" + "encoding/json" + "errors" + "io/fs" + "os" + "path/filepath" + "sort" + "strings" + + "agentflow/internal/repo" +) + +// Native CLI logs of Bot turns (stage H). Bot turns run in the user's shared +// CLI home (assistantCLIHome), next to logs of other sources, so the cleanup +// only removes files the mapping identifies: the requested or reported +// session ID as the file name. It never follows symbolic links, never leaves +// the home of the user the mapping belongs to, and records each removed +// path relative to that home. + +// botNativeScopeHome is the only storage scope Bot turns use. +const botNativeScopeHome = "assistant-cli-home" + +func botNativeSessionIDOK(id string) bool { + id = strings.TrimSpace(id) + return len(id) >= 8 && len(id) <= 191 && id != "." && id != ".." && !strings.ContainsAny(id, `/\*?[]`+"\x00") +} + +// botNativeMatcher reports whether a file or directory name belongs to one of +// the session IDs of an engine, and which ID. +type botNativeMatcher func(name string, ids map[string]string) (string, bool) + +func botNativeEngineRoots(engine, home string) ([]string, botNativeMatcher) { + byName := func(name string, ids map[string]string) (string, bool) { + for id, mapping := range ids { + if assistantCLISessionName(name, id) { + return mapping, true + } + } + return "", false + } + switch engine { + case "", assistantEngineCodeBuddy: + return []string{filepath.Join(home, ".codebuddy", "projects"), filepath.Join(home, ".codebuddy", "sessions")}, byName + case assistantEngineClaude: + return []string{filepath.Join(home, ".claude", "projects")}, byName + case assistantEngineCodex: + // rollout--.jsonl under sessions/YYYY/MM/DD. + return []string{filepath.Join(home, ".codex", "sessions")}, func(name string, ids map[string]string) (string, bool) { + if !strings.HasPrefix(name, "rollout-") || !strings.HasSuffix(name, ".jsonl") { + return "", false + } + for id, mapping := range ids { + if strings.HasSuffix(name, "-"+id+".jsonl") { + return mapping, true + } + } + return "", false + } + } + return nil, nil +} + +// cleanBotNativeLogs is the botinteraction.NativeLogCleaner of the platform. +// Mappings are grouped by user and engine so each log tree is walked once. +func cleanBotNativeLogs(ctx context.Context, due []repo.BotNativeCleanup) []repo.BotNativeCleanupResult { + results := make(map[string]*repo.BotNativeCleanupResult, len(due)) + order := make([]string, 0, len(due)) + type group struct { + userID int64 + engine string + } + groups := map[group]map[string]string{} + codexKeys := map[int64][]string{} + for _, item := range due { + res := &repo.BotNativeCleanupResult{ID: item.ID, Status: repo.BotNativeCleanupMissing} + results[item.ID] = res + order = append(order, item.ID) + if item.StorageScope != botNativeScopeHome || item.UserID <= 0 { + res.Status, res.Detail = repo.BotNativeCleanupUnsupported, "未知的存储范围:"+item.StorageScope + continue + } + if roots, _ := botNativeEngineRoots(item.Engine, ""); roots == nil { + res.Status, res.Detail = repo.BotNativeCleanupUnsupported, "该 CLI 的原生日志位置未登记,需人工核查:"+item.Engine + continue + } + ids := []string{item.NativeID} + if item.Engine != assistantEngineCodex { + ids = append(ids, item.RequestedSessionID) + } else if botNativeSessionIDOK(item.RequestedSessionID) { + codexKeys[item.UserID] = append(codexKeys[item.UserID], item.RequestedSessionID) + } + key := group{item.UserID, item.Engine} + for _, id := range ids { + if !botNativeSessionIDOK(id) { + continue + } + if groups[key] == nil { + groups[key] = map[string]string{} + } + groups[key][id] = item.ID + } + } + for key, ids := range groups { + if ctx.Err() != nil { + break + } + home := assistantCLIHome(key.userID) + info, err := os.Lstat(home) + if errors.Is(err, fs.ErrNotExist) { + continue + } + if err != nil || !info.IsDir() { + for _, mapping := range ids { + results[mapping].Status, results[mapping].Detail = repo.BotNativeCleanupError, "CLI HOME 不可用或不是目录" + } + continue + } + realHome, err := filepath.EvalSymlinks(home) + if err != nil { + continue + } + roots, match := botNativeEngineRoots(key.engine, home) + for _, root := range roots { + // The log tree must really live inside this home: a symbolic + // link on the way (for example ~/.codebuddy) is not followed. + if real, err := filepath.EvalSymlinks(root); err != nil || real != filepath.Join(realHome, strings.TrimPrefix(root, home)) { + continue + } + _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + if path == root { + return fs.SkipDir + } + return nil + } + if path == root || ctx.Err() != nil { + return nil + } + if d.Type()&os.ModeSymlink != 0 { + return nil + } + mapping, ok := match(d.Name(), ids) + if !ok { + return nil + } + res := results[mapping] + rel, relErr := filepath.Rel(home, path) + if relErr != nil || strings.HasPrefix(rel, "..") { + res.Status, res.Detail = repo.BotNativeCleanupError, "日志路径不在该用户的 CLI HOME 内" + return nil + } + if err := os.RemoveAll(path); err != nil { + res.Status, res.Detail = repo.BotNativeCleanupError, "删除失败:"+rel + } else { + res.Paths = append(res.Paths, filepath.ToSlash(rel)) + if res.Status != repo.BotNativeCleanupError { + res.Status = repo.BotNativeCleanupRemoved + } + } + if d.IsDir() { + return fs.SkipDir + } + return nil + }) + } + } + for userID, keys := range codexKeys { + forgetCodexThreads(assistantSessionRoot(userID), keys) + } + out := make([]repo.BotNativeCleanupResult, 0, len(order)) + for _, id := range order { + res := results[id] + sort.Strings(res.Paths) + if res.Status == repo.BotNativeCleanupMissing && res.Detail == "" { + res.Detail = "未找到映射指向的日志文件(可能已被删除)" + } + out = append(out, *res) + } + return out +} + +// forgetCodexThreads removes Bot session keys from the Codex thread index of +// a user's shared root. +func forgetCodexThreads(root string, keys []string) { + codexThreadsMu.Lock() + defer codexThreadsMu.Unlock() + data, err := os.ReadFile(codexThreadsPath(root)) + if err != nil { + return + } + threads := map[string]string{} + if json.Unmarshal(data, &threads) != nil { + return + } + changed := false + for _, key := range keys { + if _, ok := threads[key]; ok { + delete(threads, key) + changed = true + } + } + if !changed { + return + } + if data, err = json.Marshal(threads); err == nil { + _ = os.WriteFile(codexThreadsPath(root), data, 0o600) + } +} diff --git a/internal/httpapi/bot_native_cleanup_test.go b/internal/httpapi/bot_native_cleanup_test.go new file mode 100644 index 000000000..731a27e49 --- /dev/null +++ b/internal/httpapi/bot_native_cleanup_test.go @@ -0,0 +1,160 @@ +package httpapi + +import ( + "agentflow/internal/botinteraction" + "agentflow/internal/repo" + "context" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +// Mixed-source CLI homes: only the native logs that a due Bot mapping points +// at are removed; web-assistant logs in the same home, other users' homes, +// symlinked trees and unmapped files stay. +func TestBotNativeLogCleanupRemovesOnlyMappedDueLogs(t *testing.T) { + base := t.TempDir() + t.Setenv("AGENTFLOW_ASSISTANT_SESSION_DIR", base) + write := func(path string) string { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte("{}\n"), 0o600); err != nil { + t.Fatal(err) + } + return path + } + home := assistantCLIHome(7) + otherHome := assistantCLIHome(8) + outside := t.TempDir() + files := map[string]string{ + "bot-cb": write(filepath.Join(home, ".codebuddy", "projects", "-ws", "afbotturn01.jsonl")), + "bot-cb-dir": write(filepath.Join(home, ".codebuddy", "projects", "-ws", "afbotturn01", "subagent.jsonl")), + "bot-cb-sess": write(filepath.Join(home, ".codebuddy", "sessions", "afbotturn01.json")), + "web": write(filepath.Join(home, ".codebuddy", "projects", "-ws", "afwebchat001.jsonl")), + "recent-bot": write(filepath.Join(home, ".codebuddy", "projects", "-ws", "afbotrecent1.jsonl")), + "other-user": write(filepath.Join(otherHome, ".codebuddy", "projects", "-ws", "afbotturn01.jsonl")), + "codex": write(filepath.Join(home, ".codex", "sessions", "2026", "05", "01", "rollout-2026-05-01T10-00-00-0199aaaa-bbbb-cccc-dddd-eeeeffff0001.jsonl")), + "codex-other": write(filepath.Join(home, ".codex", "sessions", "2026", "05", "01", "rollout-2026-05-01T11-00-00-0199aaaa-bbbb-cccc-dddd-eeeeffff0002.jsonl")), + "claude-out": write(filepath.Join(outside, "projects", "-ws", "claudeturn01.jsonl")), + "claude-guard": write(filepath.Join(home, ".claude-real", "x")), + } + // ~/.claude is a link that leaves the home: never followed. + if err := os.Symlink(outside, filepath.Join(home, ".claude")); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(codexThreadsPath(assistantSessionRoot(7)), []byte(`{"afbotcodex01":"0199aaaa-bbbb-cccc-dddd-eeeeffff0001","afweb000001":"0199aaaa-bbbb-cccc-dddd-eeeeffff0002"}`), 0o600); err != nil { + t.Fatal(err) + } + + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + now := time.Now().UTC() + start := func(engine, requested, native string, finishedAgo time.Duration) string { + t.Helper() + id, err := store.StartBotNativeSession(repo.BotNativeSession{ConfigID: "cfg", ProjectID: "p", UserID: 7, InteractionID: "i-" + requested, Engine: engine, RequestedSessionID: requested, StorageScope: "assistant-cli-home"}) + if err != nil { + t.Fatal(err) + } + if id, err = store.FinishBotNativeSession(id, native, "completed"); err != nil { + t.Fatal(err) + } + if err := store.DB.Table("bot_native_sessions").Where("id = ?", id).Update("finished_at", now.Add(-finishedAgo)).Error; err != nil { + t.Fatal(err) + } + return id + } + day := 24 * time.Hour + cb := start("codebuddy", "afbotturn01", "afbotturn01", 100*day) + recent := start("codebuddy", "afbotrecent1", "afbotrecent1", 10*day) + codex := start("codex", "afbotcodex01", "0199aaaa-bbbb-cccc-dddd-eeeeffff0001", 100*day) + claude := start("claude-code", "claudeturn01", "claudeturn01", 100*day) + gone := start("codebuddy", "afbotgone001", "afbotgone001", 100*day) + other := start("opencode", "afbotopen001", "ses_open001", 100*day) + + svc := botinteraction.New(store) + svc.NativeLogs = cleanBotNativeLogs + policy := repo.BotRetentionPolicy{Body: 90 * day, Record: 180 * day} + if _, handled, err := svc.RunRetention(context.Background(), now, policy); err != nil || handled != 5 { + t.Fatalf("handled = %d err = %v", handled, err) + } + exists := func(key string) bool { _, err := os.Lstat(files[key]); return err == nil } + for _, key := range []string{"bot-cb", "bot-cb-dir", "bot-cb-sess", "codex"} { + if exists(key) { + t.Errorf("%s not removed", key) + } + } + for _, key := range []string{"web", "recent-bot", "other-user", "codex-other", "claude-out", "claude-guard"} { + if !exists(key) { + t.Errorf("%s removed", key) + } + } + state := func(id string) (string, string) { + status, paths, _, err := store.BotNativeCleanupState(id) + if err != nil { + t.Fatal(err) + } + return status, paths + } + if status, paths := state(cb); status != repo.BotNativeCleanupRemoved || paths != ".codebuddy/projects/-ws/afbotturn01\n.codebuddy/projects/-ws/afbotturn01.jsonl\n.codebuddy/sessions/afbotturn01.json" { + t.Errorf("codebuddy = %s %q", status, paths) + } + if status, paths := state(codex); status != repo.BotNativeCleanupRemoved || !strings.HasSuffix(paths, "eeeeffff0001.jsonl") { + t.Errorf("codex = %s %q", status, paths) + } + if status, _ := state(claude); status != repo.BotNativeCleanupMissing { + t.Errorf("claude behind a symlink = %s", status) + } + if status, _ := state(gone); status != repo.BotNativeCleanupMissing { + t.Errorf("missing = %s", status) + } + if status, _ := state(other); status != repo.BotNativeCleanupUnsupported { + t.Errorf("unsupported engine = %s", status) + } + if status, _ := state(recent); status != "" { + t.Errorf("recent mapping touched: %s", status) + } + threads, _ := os.ReadFile(codexThreadsPath(assistantSessionRoot(7))) + if strings.Contains(string(threads), "afbotcodex01") || !strings.Contains(string(threads), "afweb000001") { + t.Errorf("codex thread index = %s", threads) + } + // After the record horizon, settled mappings go; unsupported stay listed. + if _, _, err := svc.RunRetention(context.Background(), now.Add(100*day), policy); err != nil { + t.Fatal(err) + } + if status, _, _, err := store.BotNativeCleanupState(cb); err == nil { + t.Errorf("settled mapping kept: %s", status) + } + if status, _ := state(other); status != repo.BotNativeCleanupUnsupported { + t.Errorf("unsupported mapping removed: %s", status) + } +} + +func TestBotRetentionPolicyFromEnv(t *testing.T) { + day := 24 * time.Hour + cases := []struct { + body, record string + wantBody, wantRecord time.Duration + }{ + {"", "", 90 * day, 180 * day}, + {"30", "60", 30 * day, 60 * day}, + {"0", "365", 0, 365 * day}, + {"120", "60", 120 * day, 120 * day}, + {"abc", "-1", 90 * day, 180 * day}, + {"30", "0", 30 * day, 0}, + } + for _, c := range cases { + t.Setenv("AGENTFLOW_BOT_BODY_RETENTION_DAYS", c.body) + t.Setenv("AGENTFLOW_BOT_RECORD_RETENTION_DAYS", c.record) + p := botinteraction.RetentionPolicyFromEnv() + if p.Body != c.wantBody || p.Record != c.wantRecord || p.Batch != 1000 { + t.Errorf("%q/%q = %+v", c.body, c.record, p) + } + } +} diff --git a/internal/httpapi/bot_native_tracker_test.go b/internal/httpapi/bot_native_tracker_test.go new file mode 100644 index 000000000..735ba4394 --- /dev/null +++ b/internal/httpapi/bot_native_tracker_test.go @@ -0,0 +1,53 @@ +package httpapi + +import ( + "agentflow/internal/repo" + "context" + "errors" + "strings" + "testing" +) + +func TestBotNativeTrackerMapsBeforeStartAndKeepsEverySession(t *testing.T) { + s, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + tracker := &botNativeTracker{store: s, in: repo.BotNativeSession{ConfigID: "cfg", ProjectID: "p", UserID: 7, ConversationID: "c", BotSessionID: "bot-1", InteractionID: "i1", Engine: "codebuddy", RequestedSessionID: "af1", StorageScope: "assistant-cli-home"}} + if err := tracker.start(); err != nil { + t.Fatal(err) + } + if mapped, states, _ := s.BotNativeSessions("i1"); len(mapped) != 1 || states[0] != "|started" { + t.Fatalf("mapping before start = %v %v", mapped, states) + } + // First run reports a native ID different from the requested one. + tracker.finish(context.Background(), "native-a", nil) + // The retry resumes the same session: same row. + tracker.start() + tracker.finish(context.Background(), "native-a", errors.New("exit 1")) + // A later retry actually created another session: a second row. + tracker.start() + tracker.finish(context.Background(), "native-b", nil) + mapped, states, _ := s.BotNativeSessions("i1") + if len(mapped) != 2 || states[0] != "native-a|failed" || states[1] != "native-b|completed" || mapped[0].RequestedSessionID != "af1" { + t.Fatalf("mappings = %v %v", mapped, states) + } +} + +type failingNativeStore struct{ started int } + +func (f *failingNativeStore) StartBotNativeSession(repo.BotNativeSession) (string, error) { + f.started++ + return "", errors.New("database unavailable") +} +func (f *failingNativeStore) FinishBotNativeSession(string, string, string) (string, error) { + return "", nil +} + +func TestBotNativeTrackerFailureStopsTheTurn(t *testing.T) { + tracker := &botNativeTracker{store: &failingNativeStore{}} + if err := tracker.start(); err == nil || !strings.Contains(err.Error(), "本轮未启动") { + t.Fatalf("start = %v", err) + } +} diff --git a/internal/httpapi/bot_natural_reply_test.go b/internal/httpapi/bot_natural_reply_test.go new file mode 100644 index 000000000..e1011e20d --- /dev/null +++ b/internal/httpapi/bot_natural_reply_test.go @@ -0,0 +1,63 @@ +package httpapi + +import ( + "agentflow/internal/domain" + "context" + "errors" + "strings" + "testing" +) + +// Scope of the "Bots accept a natural-language final answer" change. Before +// it, a Bot turn only ended through submit_assistant_reply, which refuses a +// reply without drafts, so a plain query answer was retried twice and then +// failed with 助手未提交回复 (three model runs, the user got the generic +// failure text). The cases below pin what is now accepted and what still +// goes through the terminal retry, for read-only and legacy auto Bots alike. +func TestBotNaturalAnswerAcceptanceMatrix(t *testing.T) { + const answer = "项目当前有 2 个运行中的 SOP,均在实现节点。" + draftJSON := `{"message":"将创建需求","drafts":[{"type":"issues_save","payload":{"title":"x"}}]}` + cases := []struct { + name string + readOnly bool + outputs []string + cliErr error + wantCalls int + wantReply string + wantErr bool + }{ + {name: "answer accepted on first run (legacy auto)", outputs: []string{answer}, wantCalls: 1, wantReply: answer}, + {name: "answer accepted on first run (read-only)", readOnly: true, outputs: []string{answer}, wantCalls: 1, wantReply: answer}, + {name: "progress-only text reruns the terminal step", outputs: []string{"我先查询一下项目状态", answer}, wantCalls: 2, wantReply: answer}, + {name: "empty output reruns the terminal step", outputs: []string{"", answer}, wantCalls: 2, wantReply: answer}, + {name: "protocol JSON with drafts is not taken as text", outputs: []string{draftJSON, draftJSON, draftJSON}, wantCalls: 3, wantErr: true}, + {name: "protocol JSON without drafts is not taken as text", outputs: []string{`{"message":"已处理","drafts":[]}`, answer}, wantCalls: 2, wantReply: answer}, + {name: "output of a failed CLI run is never accepted", outputs: []string{answer, answer, answer}, cliErr: errors.New("exit status 1"), wantCalls: 3, wantErr: true}, + {name: "never answering still fails after three runs", outputs: []string{"", "", ""}, wantCalls: 3, wantErr: true}, + {name: "credentials in the answer are redacted", outputs: []string{"密钥是 other-key"}, wantCalls: 1, wantReply: "密钥是 [redacted]"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + _, h, bot := botFixture(t) + if tc.readOnly { + bot.WriteApproval = domain.BotWriteReadOnly + } + calls := 0 + h.generate = func(ctx context.Context, prompt string, user domain.User) (string, error) { + out := "" + if calls < len(tc.outputs) { + out = tc.outputs[calls] + } + calls++ + return out, tc.cliErr + } + reply, err := h.generateBotReply(context.Background(), bot, domain.BotConversation{SessionID: "bot-session", ChatType: "single"}, nil, domain.BotInteraction{SenderID: "new-wecom-user", Content: "项目状态"}) + if calls != tc.wantCalls || (err != nil) != tc.wantErr { + t.Fatalf("calls=%d err=%v reply=%q", calls, err, reply) + } + if !tc.wantErr && strings.TrimSpace(reply) != tc.wantReply { + t.Fatalf("reply = %q", reply) + } + }) + } +} diff --git a/internal/httpapi/bot_push_target_test.go b/internal/httpapi/bot_push_target_test.go new file mode 100644 index 000000000..5ff675c8c --- /dev/null +++ b/internal/httpapi/bot_push_target_test.go @@ -0,0 +1,247 @@ +package httpapi + +import ( + "bytes" + "net/http/httptest" + + "github.com/gin-gonic/gin" + + "agentflow/internal/botinteraction" + "agentflow/internal/domain" + "agentflow/internal/notification" + "context" + "encoding/json" + "net/http" + "strings" + "sync" + "testing" + "time" +) + +// stubPushChannel is a DingTalk adapter with scripted proactive sends. +type stubPushChannel struct { + stubChannel + mu sync.Mutex + results []botinteraction.SendResult + targets []string +} + +func (s *stubPushChannel) Push(_ context.Context, _ domain.NotificationBot, _ botinteraction.Credential, msg botinteraction.OutboundMessage) botinteraction.SendResult { + s.mu.Lock() + defer s.mu.Unlock() + s.targets = append(s.targets, msg.ChatID) + if len(s.results) == 0 { + return botinteraction.SendResult{Outcome: botinteraction.Delivered} + } + r := s.results[0] + s.results = s.results[1:] + return r +} + +func TestPushTargetSelectionTrialSwitchAndConfirm(t *testing.T) { + f := cvBNewHTTPFixture(t) + nh := ¬ificationHandlers{deps: &deps{repo: f.s, engine: f.e}, service: notification.New(f.s)} + push := &stubPushChannel{stubChannel: stubChannel{botinteraction.ChannelDingTalk}} + nh.service.Interactions.RegisterChannel(push) + decode := func(code int, body []byte) map[string]any { + out := map[string]any{"_code": float64(code)} + json.Unmarshal(body, &out) + return out + } + rec := cvBDirectHandlerRequest(t, f, http.MethodPost, "/api/notification-bots", map[string]any{"projectId": "platform", "name": "钉钉通知", "channel": "dingtalk", "botId": "ding-app", "secret": "good", "enabled": true, "tenantKey": "ding-corp"}, nh.bots, nil, true) + created := decode(rec.Code, rec.Body.Bytes()) + if rec.Code != 200 { + t.Fatalf("create = %v", created) + } + id := created["id"].(string) + stored, _ := f.s.NotificationBot(id) + if _, err := f.s.RecordBotTenantCandidate(id, "ding-corp", "e1", stored.CredentialVersion); err != nil { + t.Fatal(err) + } + stored, _ = f.s.NotificationBot(id) + bindBotTestUser(t, f.s, stored, "staff-1", f.user.ID) + put := func(body map[string]any) map[string]any { + t.Helper() + current, _ := f.s.NotificationBot(id) + req := map[string]any{"projectId": "platform", "name": "钉钉通知", "channel": "dingtalk", "botId": "ding-app", "enabled": true, "configVersion": current.ConfigVersion} + for k, v := range body { + req[k] = v + } + rec := cvBDirectHandlerRequest(t, f, http.MethodPut, "/api/notification-bots/"+id, req, nh.bot, map[string]string{"id": id}, true) + return decode(rec.Code, rec.Body.Bytes()) + } + rec = cvBDirectHandlerRequest(t, f, http.MethodGet, "/api/notification-bots/"+id+"/sessions", nil, nh.sessions, map[string]string{"id": id}, true) + if out := decode(rec.Code, rec.Body.Bytes()); rec.Code != 200 || out["total"].(float64) != 1 { + t.Fatalf("sessions = %v", out) + } + if out := put(map[string]any{"targetId": "stranger"}); out["code"] != "push_target_unverified" { + t.Fatalf("unbound target = %v", out) + } + push.results = []botinteraction.SendResult{{Outcome: botinteraction.Unknown, SafeDetail: "钉钉返回服务端错误,送达结果未知"}} + if out := put(map[string]any{"targetId": "staff-1"}); out["_code"].(float64) != 502 || out["code"] != "push_trial_failed" || out["outcome"] != "unknown" { + t.Fatalf("unknown trial = %v", out) + } + if b, _ := f.s.NotificationBot(id); b.Target != nil { + t.Fatal("target saved after an unknown trial") + } + if out := put(map[string]any{"targetId": "staff-1"}); out["_code"].(float64) != 200 { + t.Fatalf("trial = %v", out) + } + bot, _ := f.s.NotificationBot(id) + if bot.Target == nil || bot.Target.ID != "staff-1" || bot.Target.VerifiedAt == nil { + t.Fatalf("target = %+v", bot.Target) + } + // Saving the same verified target again sends no new trial. + trials := len(push.targets) + if out := put(map[string]any{"targetId": "staff-1", "name": "钉钉通知"}); out["_code"].(float64) != 200 || len(push.targets) != trials { + t.Fatalf("resave = %v trials %d", out, len(push.targets)-trials) + } + // Switching with queued notifications needs an explicit choice. + now := time.Now().UTC() + queued := domain.NotificationDelivery{ID: "queued", ProjectID: "platform", BotID: id, EventType: "failed", TargetID: "staff-1", TargetType: "single", Content: "x", Status: "pending", CreatedAt: now, NextAttemptAt: now.Add(time.Hour)} + if err := f.s.CreateNotificationDelivery(queued); err != nil { + t.Fatal(err) + } + bindBotTestUser(t, f.s, bot, "staff-2", f.user.ID) + if out := put(map[string]any{"targetId": "staff-2"}); out["code"] != "pending_deliveries" || out["pending"].(float64) != 1 { + t.Fatalf("switch without choice = %v", out) + } + if out := put(map[string]any{"targetId": "staff-2", "pendingTargetAction": "migrate"}); out["_code"].(float64) != 200 { + t.Fatalf("migrate = %v", out) + } + if item, _ := f.s.NotificationDelivery("queued"); item.TargetID != "staff-2" || item.Status != "pending" { + t.Fatalf("migrated = %+v", item) + } + // Manual confirmation of an unknown outcome. + unknown := domain.NotificationDelivery{ID: "unknown", ProjectID: "platform", BotID: id, EventType: "failed", TargetID: "staff-2", Content: "x", Status: "send_unknown", CreatedAt: now, NextAttemptAt: now} + if err := f.s.CreateNotificationDelivery(unknown); err != nil { + t.Fatal(err) + } + rec = cvBDirectHandlerRequest(t, f, http.MethodPost, "/api/notification-deliveries/unknown/confirm?projectId=platform", nil, nh.retry, map[string]string{"deliveryId": "unknown"}, true) + if rec.Code >= 300 { + t.Fatalf("confirm = %d %s", rec.Code, rec.Body.String()) + } + if item, _ := f.s.NotificationDelivery("unknown"); item.Status != "sent" || item.ReceiptSource != "manual" || item.ConfirmedBy != f.user.ID { + t.Fatalf("confirmed = %+v", item) + } + rec = cvBDirectHandlerRequest(t, f, http.MethodPost, "/api/notification-deliveries/queued/confirm?projectId=platform", nil, nh.retry, map[string]string{"deliveryId": "queued"}, true) + if rec.Code != 409 { + t.Fatalf("confirm pending = %d", rec.Code) + } + // The test button re-runs the trial. + rec = cvBDirectHandlerRequest(t, f, http.MethodPost, "/api/notification-bots/"+id+"/test", nil, nh.test, map[string]string{"id": id}, true) + if rec.Code != 200 || push.targets[len(push.targets)-1] != "staff-2" { + t.Fatalf("test = %d %s", rec.Code, rec.Body.String()) + } +} + +func TestPushResendEndpointRequiresVerifiedOriginalSender(t *testing.T) { + f := cvBNewHTTPFixture(t) + nh := ¬ificationHandlers{deps: &deps{repo: f.s, engine: f.e}, service: notification.New(f.s)} + push := &stubPushChannel{stubChannel: stubChannel{botinteraction.ChannelDingTalk}} + nh.service.Interactions.RegisterChannel(push) + bh := &botInteractionHandlers{deps: nh.deps, notifications: nh} + rec := cvBDirectHandlerRequest(t, f, http.MethodPost, "/api/notification-bots", map[string]any{"projectId": "platform", "name": "钉钉", "channel": "dingtalk", "botId": "ding-app", "secret": "good", "enabled": true, "interactionEnabled": true, "tenantKey": "ding-corp"}, nh.bots, nil, true) + if rec.Code != 200 { + t.Fatal(rec.Body.String()) + } + var created domain.NotificationBot + json.Unmarshal(rec.Body.Bytes(), &created) + if _, err := f.s.RecordBotTenantCandidate(created.ID, "ding-corp", "e1", created.CredentialVersion); err != nil { + t.Fatal(err) + } + bot, _ := f.s.NotificationBot(created.ID) + bindBotTestUser(t, f.s, bot, "staff-1", f.user.ID) + item := domain.BotInteraction{ID: "expired-1", BotID: bot.ID, ProjectID: "platform", SenderID: "staff-1", Content: "进度", Reply: "答复", Status: domain.BotReplyFailed, TerminalReason: "route_expired", DeliveryGeneration: 1, SendKey: "old", CreatedAt: time.Now().UTC()} + if err := f.s.DB.Create(&item).Error; err != nil { + t.Fatal(err) + } + act := func(path string) (int, map[string]any) { + raw, _ := json.Marshal(map[string]any{"actionKey": "key-" + path, "reason": "过期"}) + req := httptest.NewRequest(http.MethodPost, "/api/bot-interactions/expired-1/"+path, bytes.NewReader(raw)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Origin", "http://"+req.Host) + req = authedRequest(t, req, f.user) + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + c.Request = req + c.Params = gin.Params{{Key: "interactionId", Value: "expired-1"}} + bh.act(c) + out := map[string]any{} + json.Unmarshal(w.Body.Bytes(), &out) + return w.Code, out + } + records := func() domain.BotInteraction { + w := cvBDirectHandlerRequest(t, f, http.MethodGet, "/api/notification-bots/"+bot.ID+"/interactions", nil, bh.records, map[string]string{"id": bot.ID}, true) + var out struct { + Messages []domain.BotInteraction `json:"messages"` + } + json.Unmarshal(w.Body.Bytes(), &out) + return out.Messages[0] + } + if records().PushResendAvailable { + t.Fatal("unverified sender offered push resend") + } + if code, out := act("push-resend"); code != 409 || out["code"] != "push_target_unverified" { + t.Fatalf("unverified = %d %v", code, out) + } + rec = cvBDirectHandlerRequest(t, f, http.MethodPost, "/api/notification-bots/"+bot.ID+"/targets/verify", map[string]any{"targetId": "staff-1"}, nh.verifyTarget, map[string]string{"id": bot.ID}, true) + if rec.Code != 200 || push.targets[len(push.targets)-1] != "staff-1" { + t.Fatalf("verify = %d %s", rec.Code, rec.Body.String()) + } + if !records().PushResendAvailable { + t.Fatal("verified sender not offered push resend") + } + if code, out := act("resend"); code != 409 || out["code"] != "route_expired" { + t.Fatalf("plain resend = %d %v", code, out) + } + code, out := act("push-resend") + if code != 200 || out["sendRoute"] != "push" || out["status"] != domain.BotReady { + t.Fatalf("push resend = %d %v", code, out) + } + stored, _ := f.s.BotInteraction("expired-1") + if stored.DeliveryGeneration != 2 || stored.SendKey == "old" || stored.ResentBy != f.user.ID { + t.Fatalf("stored = %+v", stored) + } +} + +// A Feishu action card click by the notification recipient approves the +// waiting node with the recipient's platform identity; a second click on the +// same card reports it as handled. +func TestFeishuCardClickApprovesWaitingNode(t *testing.T) { + s, h, bot := botFixture(t) + bot.ID, bot.BotID, bot.Channel, bot.TenantKey, bot.TenantState = "b-feishu-card", "cli_card", domain.BotChannelFeishu, "tk", domain.BotTenantLocked + if err := s.SaveNotificationBot(bot, "secret"); err != nil { + t.Fatal(err) + } + bot, _ = s.NotificationBot(bot.ID) + bindBotTestUser(t, s, bot, "ou-member", 43) + run := domain.Run{ID: "feishu-card-run", Status: "waiting", WorkflowID: "wf", ExecutionContext: domain.TelemetryContext{ProjectID: "p"}, WorkflowSnapshot: domain.WorkflowDefinition{ID: "wf", ProjectID: "p", Nodes: []domain.WorkflowNode{{ID: "gate", Type: "human", Name: "人工确认"}}}, Nodes: map[string]*domain.NodeRun{"gate": {NodeID: "gate", Status: "waiting_approval", Attempt: 1}}} + if err := s.PutRun(run); err != nil { + t.Fatal(err) + } + delivery := domain.NotificationDelivery{ID: "d-feishu", BotID: bot.ID, ProjectID: "p", RunID: run.ID, NodeID: "gate", SourceName: "人工确认", EventType: "waiting", Attempt: 1, TargetID: "ou-member", TargetType: "single", Status: "sent", CreatedAt: time.Now().UTC()} + if err := s.CreateNotificationDelivery(delivery); err != nil { + t.Fatal(err) + } + card := h.notifications.service.Interactions.PushCardFor(delivery, "需求:登录优化\nSOP:交付", "https://platform.example/#/runs/"+run.ID) + if card == nil || card.Title != "登录优化" || len(card.Actions) == 0 || card.Actions[0].Key != "approve" { + t.Fatalf("card = %+v", card) + } + item := domain.BotInteraction{DeliveryID: delivery.ID, Action: "approve", SenderID: "ou-member"} + conv := domain.BotConversation{ChatID: "ou-member", ChatType: "single"} + reply, err := h.handleBotCardAction(context.Background(), bot, item, conv) + if err != nil || !strings.Contains(reply, "操作已完成") { + t.Fatal(reply, err) + } + updated, _ := s.RunState(run.ID) + if updated.Nodes["gate"].Status != "succeeded" { + t.Fatalf("node = %s", updated.Nodes["gate"].Status) + } + if reply, _ := h.handleBotCardAction(context.Background(), bot, item, conv); !strings.Contains(reply, "过期") { + t.Fatalf("second click = %q", reply) + } + if h.notifications.service.Interactions.PushCardFor(delivery, "", "") != nil { + t.Fatal("card offered after the node moved on") + } +} diff --git a/internal/httpapi/bot_readonly_test.go b/internal/httpapi/bot_readonly_test.go new file mode 100644 index 000000000..ad0ae6984 --- /dev/null +++ b/internal/httpapi/bot_readonly_test.go @@ -0,0 +1,143 @@ +package httpapi + +import ( + "agentflow/internal/domain" + "context" + "encoding/json" + "strings" + "testing" + "time" +) + +// A read-only Bot never executes drafts: the reply says so and nothing is +// written. The turn uses the read-only execution policy and prompt. +func TestReadOnlyBotNeverExecutesDrafts(t *testing.T) { + s, h, bot := botFixture(t) + bot.WriteApproval = domain.BotWriteReadOnly + if err := s.SaveNotificationBot(bot, ""); err != nil { + t.Fatal(err) + } + before := len(s.Issues()) + h.generate = func(ctx context.Context, prompt string, user domain.User) (string, error) { + if !strings.Contains(prompt, "只读模式") { + t.Fatal("read-only policy missing from prompt") + } + session := botTestToolSession(t, user.ID) + if !session.ExecMode.ReadOnly || session.ExecMode.AutoWrite { + t.Fatalf("exec mode = %+v", session.ExecMode) + } + body, _ := json.Marshal(map[string]any{"projectId": "p", "title": "不应创建"}) + session.commitFinal(AgentTerminalResult{Kind: AgentTerminalAssistant, Assistant: &assistantReply{Message: "已准备创建需求", Drafts: []assistantDraft{{Tool: "issues_save", Body: body}}}}) + return "", nil + } + reply, err := h.generateBotReply(context.Background(), bot, domain.BotConversation{SessionID: "bot-session", ChatType: "single"}, nil, domain.BotInteraction{SenderID: "new-wecom-user", Content: "帮我建个需求"}) + if err != nil || !strings.Contains(reply, "只读模式") { + t.Fatal(reply, err) + } + if len(s.Issues()) != before { + t.Fatal("read-only Bot executed a draft") + } +} + +func botTestToolSession(t *testing.T, userID int64) *AgentToolSession { + t.Helper() + agentToolSessions.mu.Lock() + defer agentToolSessions.mu.Unlock() + for _, candidate := range agentToolSessions.sessions { + if candidate.Mode == AgentToolAssistant && candidate.UserID == userID { + return candidate + } + } + t.Fatal("no tool session") + return nil +} + +// Removing project access or unbinding takes effect on the next turn. +func TestBotRevocationAppliesToNextTurn(t *testing.T) { + s, h, bot := botFixture(t) + if _, err := h.botExecutionUser(bot, "new-wecom-user"); err != nil { + t.Fatal(err) + } + project, _ := s.Project("p") + project.Members = []domain.ProjectMember{} + s.PutProject(project) + if _, err := h.botExecutionUser(bot, "new-wecom-user"); err == nil { + t.Fatal("removed member kept execution rights") + } + binding, _ := s.BotUserBinding(bot.IdentityScope(), "bound-member") + if err := s.RevokeBotUserBinding(binding.ID, binding.UserID, time.Now().UTC()); err != nil { + t.Fatal(err) + } + if _, err := h.botExecutionUser(bot, "bound-member"); err == nil { + t.Fatal("unbound sender kept execution rights") + } +} + +// New channels scope identities by configuration: the same platform user ID +// under another configuration is a different identity, and a challenge of +// one configuration cannot be confirmed through another. +func TestChannelBindingIsScopedToConfiguration(t *testing.T) { + s, h, _ := botFixture(t) + feishu := domain.NotificationBot{ID: "fs-a", Channel: domain.BotChannelFeishu, BotID: "cli_a", ProjectID: "p", OwnerID: 42, Enabled: true, InteractionEnabled: true, Model: domain.DefaultNotificationReportModel} + other := feishu + other.ID, other.BotID = "fs-b", "cli_b" + for _, b := range []domain.NotificationBot{feishu, other} { + if err := s.SaveNotificationBot(b, "secret"); err != nil { + t.Fatal(err) + } + } + now := time.Now().UTC() + token, err := s.NewBotBindingChallenge(feishu, "ou_same", now, false) + if err != nil { + t.Fatal(err) + } + code, err := s.ClaimBotBindingChallenge(token, 43, now) + if err != nil { + t.Fatal(err) + } + if _, err := s.ConfirmBotUserBinding(other, "ou_same", code, now); err == nil { + t.Fatal("challenge confirmed through another configuration") + } + if _, err := s.ConfirmBotUserBinding(feishu, "ou_same", code, now); err != nil { + t.Fatal(err) + } + if _, err := h.botExecutionUser(feishu, "ou_same"); err != nil { + t.Fatal(err) + } + if _, err := h.botExecutionUser(other, "ou_same"); err == nil { + t.Fatal("binding leaked to another configuration") + } + binding, _ := s.BotUserBinding(feishu.IdentityScope(), "ou_same") + if binding.ConfigID != feishu.ID || binding.Channel != domain.BotChannelFeishu || binding.BotID != feishu.ID { + t.Fatalf("binding = %+v", binding) + } + // Deleting the configuration revokes its bindings. + if err := s.DeleteNotificationBot(feishu.ID); err != nil { + t.Fatal(err) + } + if _, err := s.BotUserBinding(feishu.IdentityScope(), "ou_same"); err == nil { + t.Fatal("binding survived configuration deletion") + } +} + +// Unbound senders get one link; while it is valid further messages point to +// it, explicit requests are limited to one per minute. +func TestBindingLinkRateLimits(t *testing.T) { + _, h, bot := botFixture(t) + ask := func(text string) string { + handled, reply, err := h.identities.message(bot, domain.BotInteraction{SenderID: "fresh-user", Content: text}) + if !handled || err != nil { + t.Fatalf("%q: handled=%v err=%v", text, handled, err) + } + return reply + } + if reply := ask("项目状态"); !strings.Contains(reply, "/bot-bind.html#request=") { + t.Fatalf("first message = %s", reply) + } + if reply := ask("还在吗"); strings.Contains(reply, "/bot-bind.html") || !strings.Contains(reply, "上一条") { + t.Fatalf("second message issued a new link: %s", reply) + } + if reply := ask("绑定账号"); strings.Contains(reply, "/bot-bind.html") || !strings.Contains(reply, "频繁") { + t.Fatalf("explicit request within a minute: %s", reply) + } +} diff --git a/internal/httpapi/notifications.go b/internal/httpapi/notifications.go index e11d1df85..32697e715 100644 --- a/internal/httpapi/notifications.go +++ b/internal/httpapi/notifications.go @@ -1,10 +1,12 @@ package httpapi import ( + "agentflow/internal/botinteraction" "agentflow/internal/domain" "agentflow/internal/notification" "agentflow/internal/repo" "errors" + "fmt" "github.com/gin-gonic/gin" "gorm.io/gorm" "net/http" @@ -107,21 +109,40 @@ func (h *notificationHandlers) bot(c *gin.Context) { if !ok { return } + if !notification.BotWorkersEnabled() { + c.JSON(503, gin.H{"error": "机器人配置维护中,请稍后再试", "code": "bot_maintenance"}) + return + } respond(c, h.repo.DeleteNotificationBot(bot.ID)) default: c.JSON(405, gin.H{"error": "method not allowed"}) } } func (h *notificationHandlers) save(c *gin.Context, old *domain.NotificationBot) { + if !notification.BotWorkersEnabled() { + c.JSON(503, gin.H{"error": "机器人配置维护中,请稍后再试", "code": "bot_maintenance"}) + return + } var req struct { ProjectID string `json:"projectId"` Name string `json:"name"` + Channel string `json:"channel"` BotID string `json:"botId"` Secret string `json:"secret"` Enabled bool `json:"enabled"` Model *string `json:"model"` InteractionEnabled *bool `json:"interactionEnabled"` TargetID *string `json:"targetId"` + WriteApproval *string `json:"writeApproval"` + ConfigVersion *int64 `json:"configVersion"` + // TenantKey is the DingTalk CorpId entered by the administrator. + TenantKey *string `json:"tenantKey"` + // CardTemplateID is the DingTalk interactive card template for + // action cards; empty sends plain-text notifications. + CardTemplateID *string `json:"cardTemplateId"` + // PendingTargetAction decides what happens to queued Feishu/DingTalk + // notifications of the previous target: "cancel" or "migrate". + PendingTargetAction string `json:"pendingTargetAction"` } if !decode(c, &req) { return @@ -130,15 +151,30 @@ func (h *notificationHandlers) save(c *gin.Context, old *domain.NotificationBot) req.BotID = strings.TrimSpace(req.BotID) req.Secret = strings.TrimSpace(req.Secret) if req.Name == "" || len([]rune(req.Name)) > 80 || req.BotID == "" || len(req.BotID) > 191 || len(req.Secret) > 512 { - c.JSON(400, gin.H{"error": "请填写有效的名称、Bot ID 和 Secret"}) + c.JSON(400, gin.H{"error": "请填写有效的名称、应用标识和 Secret"}) return } - bot := domain.NotificationBot{ID: "bot-" + randomID(), ProjectID: strings.TrimSpace(req.ProjectID), Name: req.Name, BotID: req.BotID, Enabled: req.Enabled, Health: "unchecked"} + // Clients without a channel field only ever managed WeCom Bots. + channel := domain.NotificationBot{Channel: strings.TrimSpace(req.Channel)}.ChannelKind() + if req.Channel != "" && req.Channel != channel { + c.JSON(400, gin.H{"error": "不支持的渠道"}) + return + } + bot := domain.NotificationBot{ID: "bot-" + randomID(), ProjectID: strings.TrimSpace(req.ProjectID), Name: req.Name, Channel: channel, BotID: req.BotID, Enabled: req.Enabled, Health: "unchecked", WriteApproval: domain.BotWriteReadOnly} if old != nil { bot = *old bot.Name = req.Name bot.Enabled = req.Enabled + if req.Channel != "" && channel != old.ChannelKind() { + c.JSON(400, gin.H{"error": repo.ErrBotImmutableField.Error(), "code": "immutable_field"}) + return + } + channel = old.ChannelKind() if old.BotID != req.BotID { + if channel != domain.BotChannelWeCom { + c.JSON(400, gin.H{"error": repo.ErrBotImmutableField.Error(), "code": "immutable_field"}) + return + } if req.Secret == "" { c.JSON(400, gin.H{"error": "修改 Bot ID 时必须同时填写新 Secret"}) return @@ -149,9 +185,56 @@ func (h *notificationHandlers) save(c *gin.Context, old *domain.NotificationBot) bot.LastCheckedAt = nil } } + if !botChannelOpen(channel) { + c.JSON(409, gin.H{"error": "该渠道尚未开放", "code": "channel_not_ready"}) + return + } + if req.WriteApproval != nil { + switch mode := strings.TrimSpace(*req.WriteApproval); { + case mode == domain.BotWriteReadOnly, mode == domain.BotWriteConfirm: + // Web confirmation works on every channel: drafts run only + // after the bound user confirms them on the platform page. + bot.WriteApproval = mode + case mode == domain.BotWriteAuto && channel == domain.BotChannelWeCom: + bot.WriteApproval = mode + default: + c.JSON(400, gin.H{"error": "该渠道只支持只读查询或网页确认"}) + return + } + } if req.InteractionEnabled != nil { bot.InteractionEnabled = *req.InteractionEnabled } + if channel == domain.BotChannelDingTalk { + corp := "" + if req.TenantKey != nil { + corp = strings.TrimSpace(*req.TenantKey) + } + switch { + case corp == "" && old == nil: + c.JSON(400, gin.H{"error": "请填写钉钉企业 CorpId", "code": "tenant_required"}) + return + case len(corp) > 191 || strings.ContainsAny(corp, " \t\r\n"): + c.JSON(400, gin.H{"error": "钉钉企业 CorpId 格式无效"}) + return + case corp != "" && old != nil && old.TenantState == domain.BotTenantLocked && corp != old.TenantKey: + c.JSON(400, gin.H{"error": "企业已确认,更换企业请新建配置", "code": "immutable_field"}) + return + case corp != "": + bot.TenantExpected = corp + if user, ok := h.requestUser(c.Request, true); ok && user != nil { + bot.TenantExpectedBy = user.ID + } + } + if req.CardTemplateID != nil { + tpl := strings.TrimSpace(*req.CardTemplateID) + if len(tpl) > 128 || strings.ContainsAny(tpl, " \t\r\n/?#") { + c.JSON(400, gin.H{"error": "钉钉互动卡片模板 ID 格式无效"}) + return + } + bot.CardTemplateID = tpl + } + } if req.Model != nil { model := strings.TrimSpace(*req.Model) if model == "" { @@ -182,6 +265,26 @@ func (h *notificationHandlers) save(c *gin.Context, old *domain.NotificationBot) bot.SecretConfigured = true bot.Health = "unchecked" bot.LastCheckedAt = nil + // New channels verify the credential before anything is stored, outside + // the database transaction; a failure keeps the previous configuration. + if channel != domain.BotChannelWeCom { + adapter, err := h.service.Interactions.Channel(bot) + if err != nil { + c.JSON(409, gin.H{"error": "该渠道尚未开放", "code": "channel_not_ready"}) + return + } + verified, err := adapter.Verify(c.Request.Context(), bot, botinteraction.Credential{Secret: req.Secret}) + if err != nil { + c.JSON(400, gin.H{"error": "凭据校验失败:" + err.Error(), "code": "credential_invalid"}) + return + } + bot.Health = "ready" + now := time.Now().UTC() + bot.LastCheckedAt = &now + if verified.TenantName != "" { + bot.TenantName = verified.TenantName + } + } } // A target is accepted only after refreshing the real provider list. Never trust a posted name/type. if bot.InteractionEnabled { @@ -189,21 +292,47 @@ func (h *notificationHandlers) save(c *gin.Context, old *domain.NotificationBot) c.JSON(400, gin.H{"error": "请登录后创建支持对话的 Bot,平台需要记录创建者的执行身份"}) return } - bots, err := h.repo.InteractiveNotificationBots() - if err != nil { - respond(c, err) - return - } - for _, existing := range bots { - if existing.BotID == bot.BotID && existing.ID != bot.ID && bot.Enabled { - c.JSON(409, gin.H{"error": "这只企业微信 Bot 已在另一个配置中启用对话,请复用原配置"}) - return - } - } } + var switchedTarget *domain.BotChat if req.TargetID != nil { if *req.TargetID == "" { bot.Target = nil + } else if channel != domain.BotChannelWeCom { + // Feishu/DingTalk: a bound private-chat sender of this + // configuration, accepted only after a trial send succeeded. + if old == nil || req.Secret != "" { + c.JSON(400, gin.H{"error": "请先保存凭据,再选择通知接收人"}) + return + } + unchanged := old.Target != nil && old.Target.ID == *req.TargetID && old.Target.VerifiedAt != nil + if !unchanged { + if !bot.Enabled { + c.JSON(409, gin.H{"error": "请先启用此 Bot,再试发并选择通知接收人"}) + return + } + if pending, err := h.repo.PendingNotificationsForOtherTargets(bot.ID, *req.TargetID); err != nil { + respond(c, err) + return + } else if pending > 0 && req.PendingTargetAction != "cancel" && req.PendingTargetAction != "migrate" { + c.JSON(409, gin.H{"error": fmt.Sprintf("原接收人还有 %d 条待发通知,请选择取消或改发到新接收人", pending), "code": "pending_deliveries", "pending": pending}) + return + } + target, err := h.service.VerifyPushTarget(c.Request.Context(), bot, *req.TargetID) + if err != nil { + var failed *notification.PushTargetError + switch { + case errors.As(err, &failed): + c.JSON(502, gin.H{"error": "试发未成功,接收人未保存:" + failed.Error(), "code": "push_trial_failed", "outcome": failed.Result.Outcome}) + case errors.Is(err, notification.ErrPushTargetUnverified): + c.JSON(409, gin.H{"error": "只能选择已在本 Bot 单聊中绑定平台账号的成员", "code": "push_target_unverified"}) + default: + c.JSON(409, gin.H{"error": err.Error(), "code": "push_target_unavailable"}) + } + return + } + bot.Target = &target + switchedTarget = &target + } } else { if old == nil || req.Secret != "" || old.BotID != req.BotID { c.JSON(400, gin.H{"error": "请先保存凭据,再刷新并选择接收会话"}) @@ -232,27 +361,129 @@ func (h *notificationHandlers) save(c *gin.Context, old *domain.NotificationBot) bot.LastCheckedAt = &now } } - if err := h.repo.SaveNotificationBot(bot, req.Secret); err != nil { - if errors.Is(err, repo.ErrInteractiveBotAlreadyExists) { - c.JSON(409, gin.H{"error": err.Error()}) + saved, err := h.repo.SaveNotificationBotChecked(bot, req.Secret, req.ConfigVersion) + if err != nil { + botSaveError(c, err) + return + } + if switchedTarget != nil && (req.PendingTargetAction == "cancel" || req.PendingTargetAction == "migrate") { + if _, err := h.repo.SwitchNotificationTarget(saved.ID, *switchedTarget, req.PendingTargetAction); err != nil { + respond(c, err) return } + } + stored, err := h.repo.NotificationBot(saved.ID) + if err != nil { respond(c, err) return } - stored, err := h.repo.NotificationBot(bot.ID) + c.JSON(200, stored) +} + +// botChannelOpen reports the channels configurable in this release. +func botChannelOpen(channel string) bool { + return channel == domain.BotChannelWeCom || channel == domain.BotChannelFeishu || channel == domain.BotChannelDingTalk +} + +func botSaveError(c *gin.Context, err error) { + switch { + case errors.Is(err, repo.ErrInteractiveBotAlreadyExists): + c.JSON(409, gin.H{"error": "这个应用已在另一个配置中启用对话,请复用原配置", "code": "receive_slot_taken"}) + case errors.Is(err, repo.ErrBotConfigChanged): + c.JSON(409, gin.H{"error": "配置已被其他人修改,请刷新后重试", "code": "config_changed"}) + case errors.Is(err, repo.ErrBotImmutableField): + c.JSON(400, gin.H{"error": err.Error(), "code": "immutable_field"}) + case errors.Is(err, repo.ErrCredentialKeyMissing): + c.JSON(503, gin.H{"error": "平台未配置凭据加密密钥,暂时无法保存 Secret,请联系管理员", "code": "credential_key_missing"}) + default: + respond(c, err) + } +} + +// tenant reads the enterprise information the channel reports for the +// stored credential, shown next to the candidate on the confirmation page. +func (h *notificationHandlers) tenant(c *gin.Context) { + bot, ok := h.find(c, true) + if !ok { + return + } + adapter, err := h.service.Interactions.Channel(bot) + if err != nil || bot.ChannelKind() == domain.BotChannelWeCom { + c.JSON(409, gin.H{"error": "该渠道没有企业确认", "code": "channel_not_ready"}) + return + } + secret, err := h.repo.BotCredential(bot.ID) + if err != nil { + c.JSON(409, gin.H{"error": "机器人凭据不可用"}) + return + } + info := gin.H{"tenantState": bot.TenantState, "candidate": bot.TenantCandidate, "tenantKey": bot.TenantKey, "configVersion": bot.ConfigVersion} + if verified, err := adapter.Verify(c.Request.Context(), bot, botinteraction.Credential{Secret: secret}); err == nil { + info["queried"] = gin.H{"tenantKey": verified.TenantKey, "tenantName": verified.TenantName} + if verified.TenantName != "" && verified.TenantName != bot.TenantName { + _ = h.repo.SetBotTenantName(bot.ID, verified.TenantName) + } + } else { + info["queryError"] = err.Error() + } + c.JSON(200, info) +} + +// tenantConfirm locks the candidate enterprise scope (Bot managers only). +func (h *notificationHandlers) tenantConfirm(c *gin.Context) { + bot, ok := h.find(c, true) + if !ok { + return + } + if !botBindingSameOrigin(c) { + c.JSON(403, gin.H{"error": "请从平台页面操作"}) + return + } + var req struct { + TenantKey string `json:"tenantKey"` + ConfigVersion int64 `json:"configVersion"` + Rediscover bool `json:"rediscover"` + } + if !decode(c, &req) { + return + } + var saved domain.NotificationBot + var err error + if req.Rediscover { + saved, err = h.repo.RediscoverBotTenant(bot.ID, req.ConfigVersion) + } else { + actor := int64(0) + if user, ok := h.requestUser(c.Request, true); ok && user != nil { + actor = user.ID + } + saved, err = h.repo.ConfirmBotTenant(bot.ID, strings.TrimSpace(req.TenantKey), req.ConfigVersion, actor) + } + switch { + case errors.Is(err, repo.ErrBotTenantCandidate): + c.JSON(409, gin.H{"error": "企业候选已变化或与当前凭据不一致,请刷新后重新确认", "code": "tenant_candidate_mismatch"}) + return + case err != nil: + botSaveError(c, err) + return + } + stored, err := h.repo.NotificationBot(saved.ID) if err != nil { respond(c, err) return } c.JSON(200, stored) } + func (h *notificationHandlers) sessions(c *gin.Context) { bot, ok := h.find(c, true) if !ok { return } chats, err := h.service.Sessions(c.Request.Context(), bot) + if err != nil && bot.ChannelKind() != domain.BotChannelWeCom { + c.JSON(409, gin.H{"error": err.Error(), "code": "push_target_unavailable"}) + return + } if err != nil { h.providerError(c, err) return @@ -275,6 +506,20 @@ func (h *notificationHandlers) test(c *gin.Context) { c.JSON(400, gin.H{"error": "请先选择并保存接收会话"}) return } + if bot.ChannelKind() != domain.BotChannelWeCom { + // The test re-runs the trial send and refreshes the verification. + target, err := h.service.VerifyPushTarget(c.Request.Context(), bot, bot.Target.ID) + var failed *notification.PushTargetError + switch { + case errors.As(err, &failed): + c.JSON(502, gin.H{"error": failed.Error(), "code": "push_trial_failed", "outcome": failed.Result.Outcome}) + case err != nil: + c.JSON(409, gin.H{"error": err.Error(), "code": "push_target_unavailable"}) + default: + c.JSON(200, gin.H{"success": true, "targetName": target.Name}) + } + return + } content := "### 平台通知测试\n机器人:" + bot.Name + "\n这是一条测试通知,不代表实际任务状态。" now := time.Now().UTC() item := domain.NotificationDelivery{ID: "test-" + randomID(), ProjectID: bot.ProjectID, BotID: bot.ID, EventType: "test", TargetID: bot.Target.ID, TargetName: bot.Target.Name, Content: content, Status: "sending", Attempts: 1, LeaseToken: randomID(), CreatedAt: now, NextAttemptAt: now} @@ -305,6 +550,43 @@ func (h *notificationHandlers) test(c *gin.Context) { } c.JSON(200, gin.H{"success": true, "targetName": bot.Target.Name}) } + +// verifyTarget sends a trial proactive message to a bound member without +// changing the notification target, so the member becomes a verified push +// target (for example before switching an expired DingTalk reply). +func (h *notificationHandlers) verifyTarget(c *gin.Context) { + bot, ok := h.find(c, true) + if !ok { + return + } + if bot.ChannelKind() == domain.BotChannelWeCom { + c.JSON(409, gin.H{"error": "企业微信不需要试发验证", "code": "state_conflict"}) + return + } + if !botBindingSameOrigin(c) { + c.JSON(403, gin.H{"error": "请从平台页面操作"}) + return + } + var req struct { + TargetID string `json:"targetId"` + } + if !decode(c, &req) { + return + } + target, err := h.service.VerifyPushTarget(c.Request.Context(), bot, strings.TrimSpace(req.TargetID)) + var failed *notification.PushTargetError + switch { + case errors.As(err, &failed): + c.JSON(502, gin.H{"error": "试发未成功:" + failed.Error(), "code": "push_trial_failed", "outcome": failed.Result.Outcome}) + case errors.Is(err, notification.ErrPushTargetUnverified): + c.JSON(409, gin.H{"error": "只能试发给已在本 Bot 单聊中绑定平台账号的成员", "code": "push_target_unverified"}) + case err != nil: + c.JSON(409, gin.H{"error": err.Error(), "code": "push_target_unavailable"}) + default: + c.JSON(200, target) + } +} + func (h *notificationHandlers) providerError(c *gin.Context, err error) { code := 0 var pe *notification.ProviderError @@ -346,6 +628,19 @@ func (h *notificationHandlers) retry(c *gin.Context) { if !h.managed(c, bot) { return } + if c.Request.URL.Path == "/api/notification-deliveries/"+item.ID+"/confirm" { + actor := int64(0) + if user, ok := h.requestUser(c.Request, true); ok && user != nil { + actor = user.ID + } + err = h.repo.ConfirmNotificationReceived(projectID, item.ID, actor) + if errors.Is(err, gorm.ErrRecordNotFound) { + c.JSON(409, gin.H{"error": "只有送达结果未知的记录可以确认收到"}) + return + } + respond(c, err) + return + } err = h.repo.RetryNotification(projectID, item.ID) if errors.Is(err, gorm.ErrRecordNotFound) { c.JSON(409, gin.H{"error": "当前记录不可重试"}) diff --git a/internal/httpapi/server.go b/internal/httpapi/server.go index 70df9146d..42a1ff29f 100644 --- a/internal/httpapi/server.go +++ b/internal/httpapi/server.go @@ -230,9 +230,11 @@ func newWithNotifications(s *repo.Store, e *engine.Engine, syncSvc *tracker.Serv g.DELETE("/api/notification-bots/:id", api.notifications.bot) g.GET("/api/notification-bots/:id/sessions", api.notifications.sessions) g.POST("/api/notification-bots/:id/test", api.notifications.test) + g.POST("/api/notification-bots/:id/targets/verify", api.notifications.verifyTarget) g.POST("/api/notification-preview", api.notifications.preview) g.GET("/api/notification-deliveries", api.notifications.deliveries) g.POST("/api/notification-deliveries/:deliveryId/retry", api.notifications.retry) + g.POST("/api/notification-deliveries/:deliveryId/confirm", api.notifications.retry) // --- auth & users --- g.GET("/api/health", api.health) @@ -300,13 +302,23 @@ func newWithNotifications(s *repo.Store, e *engine.Engine, syncSvc *tracker.Serv if api.notifications.service.Interactions != nil { api.notifications.service.Interactions.Generate = asst.generateBotReply api.notifications.service.Interactions.Action = asst.handleBotCardAction + api.notifications.service.Interactions.NativeLogs = cleanBotNativeLogs } api.botInteractions = &botInteractionHandlers{deps: d, notifications: api.notifications} g.GET("/api/notification-bots/:id/interactions", api.botInteractions.records) + g.GET("/api/notification-bots/:id/tenant", api.notifications.tenant) + g.POST("/api/notification-bots/:id/tenant/confirm", api.notifications.tenantConfirm) + g.GET("/api/bot-interactions/pending", api.botInteractions.pending) + g.POST("/api/bot-interactions/:interactionId/resend", api.botInteractions.act) + g.POST("/api/bot-interactions/:interactionId/confirm", api.botInteractions.act) + g.POST("/api/bot-interactions/:interactionId/push-resend", api.botInteractions.act) g.POST("/api/bot-bindings/claim", api.botIdentities.binding) g.POST("/api/bot-bindings/status", api.botIdentities.binding) g.GET("/api/bot-bindings", api.botIdentities.list) g.DELETE("/api/bot-bindings/:id", api.botIdentities.remove) + g.GET("/api/bot-drafts/:id", asst.botDraft) + g.POST("/api/bot-drafts/:id/confirm", asst.botDraft) + g.POST("/api/bot-drafts/:id/cancel", asst.botDraft) g.POST("/api/assistant/chat", asst.chat) g.POST("/api/assistant/generations", asst.startGeneration) g.GET("/api/assistant/sessions/:id/generation", asst.generation) diff --git a/internal/httpapi/static_routes_test.go b/internal/httpapi/static_routes_test.go index 0ac7d19c3..08a66e5b3 100644 --- a/internal/httpapi/static_routes_test.go +++ b/internal/httpapi/static_routes_test.go @@ -38,6 +38,9 @@ func TestStaticRoutes(t *testing.T) { {"/extensions/agentflow-workspace/package.json", "json", 200}, {"/app.js", "javascript", 200}, {"/app/start.js", "javascript", 200}, + {"/bot-bind.html", "text/html", 200}, + {"/bot-approve.html", "text/html", 200}, + {"/bot-approve.js", "javascript", 200}, {"/no-such-file.js", "", 404}, {"/no/such/dir/", "", 404}, } { diff --git a/internal/notification/channel_wiring_test.go b/internal/notification/channel_wiring_test.go new file mode 100644 index 000000000..9e7848116 --- /dev/null +++ b/internal/notification/channel_wiring_test.go @@ -0,0 +1,78 @@ +package notification + +import ( + "agentflow/internal/botinteraction" + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "testing" +) + +func TestClassifyProviderSend(t *testing.T) { + cases := []struct { + err error + want botinteraction.SendOutcome + code string + }{ + {nil, botinteraction.Delivered, ""}, + {failure(850003), botinteraction.Rejected, "850003"}, + {errNotificationContentLength, botinteraction.Rejected, "invalid_content"}, + {errors.New("企业微信未确认发送成功"), botinteraction.Unknown, "unconfirmed"}, + } + for _, tc := range cases { + if got := classifyProviderSend(tc.err); got.Outcome != tc.want || got.Code != tc.code { + t.Errorf("%v: %+v", tc.err, got) + } + } +} + +func TestCLIProviderVerifyUsesSignedBootstrapOnly(t *testing.T) { + code := 0 + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + var body map[string]any + _ = json.NewDecoder(r.Body).Decode(&body) + if body["bot_id"] != "bot" || body["signature"] == "" { + t.Errorf("unsigned bootstrap: %v", body) + } + _ = json.NewEncoder(w).Encode(map[string]any{"errcode": code, "token": "tok"}) + })) + defer server.Close() + p := &CLIProvider{HTTP: server.Client(), authURL: server.URL} + if err := p.Verify(context.Background(), "bot", "secret"); err != nil { + t.Fatal(err) + } + code = 853004 + var pe *ProviderError + if err := p.Verify(context.Background(), "bot", "secret"); !errors.As(err, &pe) || pe.Code != 853004 { + t.Fatalf("invalid secret = %v", err) + } + if calls != 2 { + t.Fatalf("calls = %d", calls) + } +} + +func TestNewWiresWeComChannelPushToCurrentProvider(t *testing.T) { + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + defer store.Close() + svc := New(store) + provider := &fakeProvider{} + svc.Provider = provider + bot := domain.NotificationBot{ID: "cfg", BotID: "wecom-bot"} + ch, err := svc.Interactions.Channel(bot) + if err != nil { + t.Fatal(err) + } + got := ch.(botinteraction.PushSender).Push(context.Background(), bot, botinteraction.Credential{Secret: "s"}, botinteraction.OutboundMessage{ChatID: "chat", Text: "hello"}) + if got.Outcome != botinteraction.Delivered || provider.sent != 1 || provider.content != "hello" { + t.Fatalf("push = %+v sent=%v", got, provider.sent) + } +} diff --git a/internal/notification/provider.go b/internal/notification/provider.go index 75448cb28..6c04414fb 100644 --- a/internal/notification/provider.go +++ b/internal/notification/provider.go @@ -194,9 +194,19 @@ func (p *CLIProvider) Sessions(ctx context.Context, botID, secret string) ([]dom } return chats, nil } + +var errNotificationContentLength = errors.New("通知正文必须为 1 至 20480 字节") + +// Verify checks a Bot ID and secret through the signed bootstrap API only. +// It never opens a long connection, so a running receiver is not displaced. +func (p *CLIProvider) Verify(ctx context.Context, botID, secret string) error { + _, err := p.token(ctx, botID, secret) + return err +} + func (p *CLIProvider) Send(ctx context.Context, botID, secret, target, content string) error { if len([]byte(content)) == 0 || len([]byte(content)) > 20480 { - return errors.New("通知正文必须为 1 至 20480 字节") + return errNotificationContentLength } body, _ := json.Marshal(map[string]any{"chat_id": target, "msg_type": "markdown", "markdown": map[string]string{"content": content}}) raw, err := p.call(ctx, botID, secret, "message", "aibot", "send", "--json", string(body)) diff --git a/internal/notification/push.go b/internal/notification/push.go new file mode 100644 index 000000000..cf03c4ae9 --- /dev/null +++ b/internal/notification/push.go @@ -0,0 +1,247 @@ +package notification + +import ( + "agentflow/internal/botinteraction" + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "crypto/rand" + "errors" + "net/url" + "regexp" + "strings" + "time" +) + +// Feishu and DingTalk notifications are sent through the channel PushSender +// to a bound private-chat sender that passed a trial send. They work for +// notification-only configurations (interaction disabled). + +// pushIdempotencyWindow is how long an unknown Feishu send may be retried +// with the same uuid (Feishu de-duplicates for one hour). +const pushIdempotencyWindow = 55 * time.Minute + +// ErrPushTargetUnverified: the target is not a bound sender of this +// configuration or no trial send succeeded. +var ErrPushTargetUnverified = repo.ErrNotificationTargetUnverified + +// PushTargetError is a failed trial send with the classified evidence. +type PushTargetError struct{ Result botinteraction.SendResult } + +func (e *PushTargetError) Error() string { + if e.Result.SafeDetail != "" { + return e.Result.SafeDetail + } + return "试发失败:" + string(e.Result.Outcome) +} + +func pushChannel(bot domain.NotificationBot) bool { + return bot.ChannelKind() != domain.BotChannelWeCom +} + +// pushTargets lists bound senders for Feishu/DingTalk configurations. +func (s *Service) pushTargets(bot domain.NotificationBot) ([]domain.BotChat, error) { + if bot.TenantState != domain.BotTenantLocked { + return nil, errors.New("企业尚未确认,暂时不能选择通知接收人") + } + return s.Store.BotPushTargets(bot) +} + +// projectVisible reports whether a platform user may read the project the +// notification belongs to (owner, any member, or platform administrator). +func (s *Service) projectVisible(projectID string, userID int64) bool { + user, ok := s.Store.User(userID) + if !ok { + return false + } + project, ok := s.Store.Project(projectID) + if !ok { + return false + } + if project.OwnerID == user.ID || s.Store.IsPlatformAdmin(user.LoginName) { + return true + } + for _, member := range project.Members { + if member.UserID == user.ID { + return true + } + } + return false +} + +// checkPushTarget re-reads the target binding before every send. +func (s *Service) checkPushTarget(bot domain.NotificationBot, sender string, requireVerified bool) (domain.BotUserBinding, error) { + if bot.TenantState != domain.BotTenantLocked { + return domain.BotUserBinding{}, errors.New("企业尚未确认") + } + binding, err := s.Store.BotPushTarget(bot, sender) + if err != nil { + return binding, ErrPushTargetUnverified + } + if requireVerified && binding.TargetVerifiedAt == nil { + return binding, ErrPushTargetUnverified + } + if !s.projectVisible(bot.ProjectID, binding.UserID) { + return binding, errors.New("接收成员的平台账号已没有该项目的权限") + } + return binding, nil +} + +// VerifyPushTarget sends a trial message to a bound sender and, on a +// confirmed delivery, records target_verified_at. The trial is recorded as a +// test delivery. Unknown outcomes do not verify the target. +func (s *Service) VerifyPushTarget(ctx context.Context, bot domain.NotificationBot, sender string) (domain.BotChat, error) { + if !bot.Enabled { + return domain.BotChat{}, errors.New("机器人已停用") + } + binding, err := s.checkPushTarget(bot, sender, false) + if err != nil { + return domain.BotChat{}, err + } + var target domain.BotChat + targets, err := s.Store.BotPushTargets(bot) + if err != nil { + return target, err + } + for _, t := range targets { + if t.ID == sender { + target = t + } + } + if target.ID == "" { + return target, ErrPushTargetUnverified + } + now := time.Now().UTC() + content := "平台通知测试\n机器人:" + bot.Name + "\n这是一条试发消息,用于确认可以向你发送项目通知,不代表实际任务状态。" + item := domain.NotificationDelivery{ID: "test-" + randomToken(), ProjectID: bot.ProjectID, BotID: bot.ID, EventType: "test", TargetID: target.ID, TargetName: target.Name, TargetType: target.Type, Content: content, Status: "sending", Attempts: 1, LeaseToken: randomToken(), CreatedAt: now, NextAttemptAt: now} + until := now.Add(2 * time.Minute) + item.LeaseUntil = &until + if err := s.Store.CreateNotificationDelivery(item); err != nil { + return target, err + } + begun, _, _, err := s.Store.BeginNotificationPush(item) + if err != nil { + return target, err + } + res := s.Interactions.Push(ctx, bot, botinteraction.OutboundMessage{InteractionID: item.ID, ChatID: target.ID, ChatType: "single", Text: content, SendKey: begun.SendKey}) + finish := repo.NotificationPushResult{Status: "failed", Detail: res.SafeDetail, Next: now} + switch res.Outcome { + case botinteraction.Delivered: + finish = repo.NotificationPushResult{Status: "sent", Next: now, PlatformMessageID: res.PlatformMessageID} + case botinteraction.Unknown: + finish.Status = "send_unknown" + } + if err := s.Store.FinishNotificationPush(item, finish); err != nil { + return target, err + } + if res.Outcome != botinteraction.Delivered { + return target, &PushTargetError{Result: res} + } + if err := s.Store.MarkBotPushTargetVerified(bot, sender, binding.UserID, now); err != nil { + return target, err + } + target.VerifiedAt = &now + return target, nil +} + +// processPush sends one claimed Feishu/DingTalk delivery. +func (s *Service) processPush(ctx context.Context, item *domain.NotificationDelivery, bot domain.NotificationBot, content string, beforeSend func() error) (bool, error) { + now := time.Now().UTC() + done := func(res repo.NotificationPushResult) (bool, error) { + if res.Next.IsZero() { + res.Next = now + } + return true, s.Store.FinishNotificationPush(*item, res) + } + if _, err := s.checkPushTarget(bot, item.TargetID, true); err != nil { + return done(repo.NotificationPushResult{Status: "target_unavailable", Detail: err.Error()}) + } + if beforeSend != nil { + if err := beforeSend(); err != nil { + return done(repo.NotificationPushResult{Status: "expired", Detail: err.Error()}) + } + } + begun, elapsed, resumed, err := s.Store.BeginNotificationPush(*item) + if err != nil { + return true, err + } + idempotent := botinteraction.PushIdempotent(bot) + if resumed && (!idempotent || time.Duration(elapsed)*time.Microsecond > pushIdempotencyWindow) { + // An earlier attempt of this generation may have reached the + // platform and cannot be de-duplicated: stop for manual review. + return done(repo.NotificationPushResult{Status: "send_unknown", Detail: "上一次发送中断,无法确认是否送达,请核对后确认收到或再次发送"}) + } + msg := botinteraction.OutboundMessage{InteractionID: item.ID, ChatID: item.TargetID, ChatType: "single", Text: plainNotificationText(content), SendKey: begun.SendKey} + // Waiting and failed nodes get an action card when the Bot receives + // messages (the click comes back on its connection). + if bot.InteractionEnabled && item.NodeID != "" && (item.EventType == "waiting" || item.EventType == "failed") { + msg.Card = s.Interactions.PushCardFor(*item, content, s.runURL(item.RunID)) + } + res := s.Interactions.Push(ctx, bot, msg) + retryAt := now.Add(notificationBackoff(item.Attempts)) + switch res.Outcome { + case botinteraction.Delivered: + return done(repo.NotificationPushResult{Status: "sent", PlatformMessageID: res.PlatformMessageID}) + case botinteraction.NotDelivered: + if item.Attempts >= 5 { + return done(repo.NotificationPushResult{Status: "failed", Detail: "多次发送未成功:" + res.SafeDetail}) + } + return done(repo.NotificationPushResult{Status: "pending", Detail: res.SafeDetail, Next: retryAt}) + case botinteraction.Rejected: + status := "failed" + if res.Code == "invalid_staff" || res.Code == "target_missing" { + status = "target_unavailable" + } + return done(repo.NotificationPushResult{Status: status, Detail: res.SafeDetail}) + default: + if idempotent && item.Attempts < 5 && time.Duration(elapsed)*time.Microsecond < pushIdempotencyWindow { + return done(repo.NotificationPushResult{Status: "pending", Detail: res.SafeDetail + ",将用同一发送 key 重试", Next: now.Add(5 * time.Second), KeepStarted: true}) + } + return done(repo.NotificationPushResult{Status: "send_unknown", Detail: res.SafeDetail}) + } +} + +func notificationBackoff(attempts int) time.Duration { + delay := time.Duration(1<[ \t]?`) + plainFontPattern = regexp.MustCompile(`]*>`) +) + +// plainNotificationText turns the WeCom Markdown of rendered notifications +// into plain text for Feishu and DingTalk text messages. Links keep the URL +// on its own line so chat clients do not swallow surrounding characters. +func plainNotificationText(content string) string { + out := plainLinkPattern.ReplaceAllStringFunc(content, func(m string) string { + parts := plainLinkPattern.FindStringSubmatch(m) + label := strings.TrimSpace(parts[1]) + if label == "" || label == parts[2] { + return "\n" + parts[2] + "\n" + } + return label + ":\n" + parts[2] + "\n" + }) + out = plainHeadingPattern.ReplaceAllString(out, "") + out = plainQuotePattern.ReplaceAllString(out, "") + out = plainFontPattern.ReplaceAllString(out, "") + out = strings.ReplaceAll(out, "**", "") + for strings.Contains(out, "\n\n\n") { + out = strings.ReplaceAll(out, "\n\n\n", "\n\n") + } + return strings.TrimSpace(out) +} + +// runURL is the run page on the platform, or empty without a public URL. +func (s *Service) runURL(runID string) string { + base, err := url.Parse(s.PublicURL) + if runID == "" || err != nil || (base.Scheme != "https" && base.Scheme != "http") || base.Host == "" { + return "" + } + return strings.TrimRight(s.PublicURL, "/") + "/#/runs/" + url.PathEscape(runID) +} diff --git a/internal/notification/push_test.go b/internal/notification/push_test.go new file mode 100644 index 000000000..129a33835 --- /dev/null +++ b/internal/notification/push_test.go @@ -0,0 +1,447 @@ +package notification + +import ( + "agentflow/internal/botinteraction" + "agentflow/internal/domain" + "agentflow/internal/repo" + "context" + "errors" + "strings" + "sync" + "testing" + "time" +) + +// fakePushChannel is a Feishu or DingTalk adapter that only pushes. +type fakePushChannel struct { + kind string + mu sync.Mutex + results []botinteraction.SendResult + sent []botinteraction.OutboundMessage +} + +func (f *fakePushChannel) Kind() string { return f.kind } +func (f *fakePushChannel) Verify(context.Context, domain.NotificationBot, botinteraction.Credential) (botinteraction.Verification, error) { + return botinteraction.Verification{}, nil +} +func (f *fakePushChannel) Connect(context.Context, domain.NotificationBot, botinteraction.Credential, botinteraction.InboundSink) (botinteraction.Conn, error) { + return nil, errors.New("not used") +} +func (f *fakePushChannel) Push(_ context.Context, _ domain.NotificationBot, _ botinteraction.Credential, msg botinteraction.OutboundMessage) botinteraction.SendResult { + f.mu.Lock() + defer f.mu.Unlock() + f.sent = append(f.sent, msg) + if len(f.results) == 0 { + return botinteraction.SendResult{Outcome: botinteraction.Delivered, PlatformMessageID: "pm-" + msg.SendKey[:6]} + } + r := f.results[0] + f.results = f.results[1:] + return r +} +func (f *fakePushChannel) count() int { + f.mu.Lock() + defer f.mu.Unlock() + return len(f.sent) +} + +// pushFixture: a notification-only (interaction disabled) configuration with +// a locked enterprise and one bound member; the run has a waiting, a +// succeeded and a failed event bound to the Bot. +func pushFixture(t *testing.T, channel string) (*Service, *fakePushChannel, domain.NotificationBot) { + t.Helper() + store, err := repo.OpenTest() + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { store.Close() }) + store.PutUser(domain.User{ID: 42, LoginName: "owner"}) + store.PutUser(domain.User{ID: 43, LoginName: "member", ChineseName: "成员"}) + store.PutProject(domain.Project{ID: "p", OwnerID: 42, Members: []domain.ProjectMember{{UserID: 43, Role: "member"}}}) + svc := New(store) + svc.PublicURL = "https://platform.example" + fake := &fakePushChannel{kind: channel} + svc.Interactions.RegisterChannel(fake) + bot := domain.NotificationBot{ID: "cfg-" + channel, ProjectID: "p", OwnerID: 42, Name: "通知", Channel: channel, BotID: "app-" + channel, Enabled: true, TenantKey: "tenant", TenantState: domain.BotTenantLocked} + if err = store.SaveNotificationBot(bot, "secret"); err != nil { + t.Fatal(err) + } + bot, _ = store.NotificationBot(bot.ID) + now := time.Now().UTC() + token, err := store.NewBotBindingChallenge(bot, "member-im", now, true) + if err != nil { + t.Fatal(err) + } + code, err := store.ClaimBotBindingChallenge(token, 43, now) + if err != nil { + t.Fatal(err) + } + if _, err = store.ConfirmBotUserBinding(bot, "member-im", code, now); err != nil { + t.Fatal(err) + } + return svc, fake, bot +} + +func verifyTarget(t *testing.T, svc *Service, bot domain.NotificationBot) domain.NotificationBot { + t.Helper() + target, err := svc.VerifyPushTarget(context.Background(), bot, "member-im") + if err != nil { + t.Fatal(err) + } + bot.Target = &target + if err := svc.Store.SaveNotificationBot(bot, ""); err != nil { + t.Fatal(err) + } + bot, _ = svc.Store.NotificationBot(bot.ID) + return bot +} + +// putPushRun records the given events: gate.waiting and node.failed are node +// reminders, run.succeeded is the SOP-level completion notification. +func putPushRun(t *testing.T, svc *Service, bot domain.NotificationBot, events ...string) domain.Run { + t.Helper() + run := domain.Run{ID: "r", ExecutionContext: domain.TelemetryContext{ProjectID: "p"}, Status: "waiting", Nodes: map[string]*domain.NodeRun{}} + run.WorkflowSnapshot.Name = "交付" + run.WorkflowSnapshot.Notifications = []domain.NotificationBinding{{BotID: bot.ID, Scope: "run", Events: []string{"succeeded", "failed", "waiting"}, Template: "### {{status}}\nSOP:**{{workflow}}**\n[查看详情]({{url}})"}} + for i, ev := range events { + id := string(rune('a' + i)) + nodeID := id + if ev == "run.succeeded" { + nodeID = "" + } else { + run.WorkflowSnapshot.Nodes = append(run.WorkflowSnapshot.Nodes, domain.WorkflowNode{ID: id, Name: "节点" + id, Type: "agent"}) + status := map[string]string{"gate.waiting": "waiting_approval", "node.failed": "failed"}[ev] + run.Nodes[id] = &domain.NodeRun{NodeID: id, Status: status, Attempt: 1} + } + run.Events = append(run.Events, domain.RunEvent{ID: "e" + id, NodeID: nodeID, Type: ev, Timestamp: time.Now().UTC()}) + } + if err := svc.Store.PutRun(run); err != nil { + t.Fatal(err) + } + return run +} + +func deliveries(t *testing.T, svc *Service) map[string]domain.NotificationDelivery { + t.Helper() + items, err := svc.Store.NotificationDeliveries("p") + if err != nil { + t.Fatal(err) + } + out := map[string]domain.NotificationDelivery{} + for _, item := range items { + if item.EventType != "test" { + out[item.EventType] = item + } + } + return out +} + +func drain(t *testing.T, svc *Service) { + t.Helper() + for i := 0; i < 10; i++ { + ok, err := svc.ProcessOne(context.Background()) + if err != nil { + t.Fatal(err) + } + if !ok { + return + } + } +} + +func TestPushTargetRequiresBindingAndSuccessfulTrial(t *testing.T) { + svc, fake, bot := pushFixture(t, domain.BotChannelDingTalk) + targets, err := svc.Sessions(context.Background(), bot) + if err != nil || len(targets) != 1 || targets[0].ID != "member-im" || targets[0].VerifiedAt != nil || !strings.Contains(targets[0].Name, "成员(member)") { + t.Fatalf("targets = %+v %v", targets, err) + } + if _, err := svc.VerifyPushTarget(context.Background(), bot, "stranger"); !errors.Is(err, ErrPushTargetUnverified) { + t.Fatalf("unbound target = %v", err) + } + fake.results = []botinteraction.SendResult{{Outcome: botinteraction.Unknown, SafeDetail: "未知"}} + var trial *PushTargetError + if _, err := svc.VerifyPushTarget(context.Background(), bot, "member-im"); !errors.As(err, &trial) { + t.Fatalf("unknown trial verified: %v", err) + } + if targets, _ := svc.Sessions(context.Background(), bot); targets[0].VerifiedAt != nil { + t.Fatal("unknown trial marked target verified") + } + bot = verifyTarget(t, svc, bot) + if bot.Target == nil || bot.Target.VerifiedAt == nil { + t.Fatalf("target = %+v", bot.Target) + } + if fake.sent[len(fake.sent)-1].ChatID != "member-im" { + t.Fatal("trial went to the wrong target") + } + // Unlocked enterprises cannot list targets. + other := bot + other.TenantState = domain.BotTenantAwaiting + if _, err := svc.Sessions(context.Background(), other); err == nil { + t.Fatal("unlocked tenant listed targets") + } +} + +func TestPushSendsAllSOPEventsForNotificationOnlyBot(t *testing.T) { + for _, channel := range []string{domain.BotChannelFeishu, domain.BotChannelDingTalk} { + t.Run(channel, func(t *testing.T) { + svc, fake, bot := pushFixture(t, channel) + bot = verifyTarget(t, svc, bot) + trials := fake.count() + putPushRun(t, svc, bot, "gate.waiting", "run.succeeded", "node.failed") + drain(t, svc) + got := deliveries(t, svc) + for _, ev := range []string{"waiting", "succeeded", "failed"} { + item := got[ev] + if item.Status != "sent" || item.ReceiptSource != "platform" || item.DeliveryGeneration != 1 || item.SendKey == "" { + t.Fatalf("%s = %+v", ev, item) + } + } + if fake.count()-trials != 3 { + t.Fatalf("pushes = %d", fake.count()-trials) + } + for _, msg := range fake.sent[trials:] { + if msg.ChatID != "member-im" || strings.Contains(msg.Text, "**") || strings.Contains(msg.Text, "](") { + t.Fatalf("push message = %+v", msg) + } + } + }) + } +} + +func TestPushOutcomesFollowChannelIdempotency(t *testing.T) { + t.Run("feishu unknown retries with the same key", func(t *testing.T) { + svc, fake, bot := pushFixture(t, domain.BotChannelFeishu) + bot = verifyTarget(t, svc, bot) + putPushRun(t, svc, bot, "run.succeeded") + trials := fake.count() + fake.results = []botinteraction.SendResult{{Outcome: botinteraction.Unknown, SafeDetail: "响应丢失"}} + drain(t, svc) + item := deliveries(t, svc)["succeeded"] + if item.Status != "pending" || item.SendStartedAtUS == 0 { + t.Fatalf("after unknown = %+v", item) + } + forceDue(t, svc, item.ID) + drain(t, svc) + item = deliveries(t, svc)["succeeded"] + sent := fake.sent[trials:] + if item.Status != "sent" || len(sent) != 2 || sent[0].SendKey != sent[1].SendKey { + t.Fatalf("retry = %+v keys %v", item, sent) + } + }) + t.Run("dingtalk unknown stops for review and can be confirmed", func(t *testing.T) { + svc, fake, bot := pushFixture(t, domain.BotChannelDingTalk) + bot = verifyTarget(t, svc, bot) + putPushRun(t, svc, bot, "node.failed") + trials := fake.count() + fake.results = []botinteraction.SendResult{{Outcome: botinteraction.Unknown, SafeDetail: "钉钉返回服务端错误,送达结果未知"}} + drain(t, svc) + item := deliveries(t, svc)["failed"] + if item.Status != "send_unknown" || fake.count()-trials != 1 { + t.Fatalf("unknown = %+v", item) + } + if err := svc.Store.ConfirmNotificationReceived("p", item.ID, 42); err != nil { + t.Fatal(err) + } + item = deliveries(t, svc)["failed"] + if item.Status != "sent" || item.ReceiptSource != "manual" || item.ConfirmedBy != 42 { + t.Fatalf("confirmed = %+v", item) + } + }) + t.Run("manual resend starts a new generation", func(t *testing.T) { + svc, fake, bot := pushFixture(t, domain.BotChannelDingTalk) + bot = verifyTarget(t, svc, bot) + putPushRun(t, svc, bot, "node.failed") + trials := fake.count() + fake.results = []botinteraction.SendResult{{Outcome: botinteraction.Unknown}} + drain(t, svc) + item := deliveries(t, svc)["failed"] + if err := svc.Store.RetryNotification("p", item.ID); err != nil { + t.Fatal(err) + } + drain(t, svc) + item = deliveries(t, svc)["failed"] + sent := fake.sent[trials:] + if item.Status != "sent" || item.DeliveryGeneration != 2 || len(sent) != 2 || sent[0].SendKey == sent[1].SendKey { + t.Fatalf("resend = %+v %v", item, sent) + } + }) + t.Run("interrupted send", func(t *testing.T) { + for channel, want := range map[string]string{domain.BotChannelDingTalk: "send_unknown", domain.BotChannelFeishu: "sent"} { + svc, fake, bot := pushFixture(t, channel) + bot = verifyTarget(t, svc, bot) + putPushRun(t, svc, bot, "run.succeeded") + // Claim and begin, then "crash" before the outcome is saved. + item, err := svc.Store.ClaimNotification(time.Now().UTC(), "crashed") + if err != nil || item == nil { + t.Fatal(item, err) + } + if _, _, _, err := svc.Store.BeginNotificationPush(*item); err != nil { + t.Fatal(err) + } + trials := fake.count() + // The lease runs out; the next claim resumes the generation. + if _, err := svc.ProcessOne(context.Background()); err != nil { + t.Fatal(err) + } + got, _ := svc.Store.NotificationDelivery(item.ID) + if got.Status != "sending" { + t.Fatalf("%s: lease not held: %s", channel, got.Status) + } + expireLease(t, svc, item.ID) + drain(t, svc) + got, _ = svc.Store.NotificationDelivery(item.ID) + pushed := fake.count() - trials + if got.Status != want || (want == "send_unknown" && pushed != 0) || (want == "sent" && pushed != 1) { + t.Fatalf("%s: interrupted = %s pushes %d", channel, got.Status, pushed) + } + } + }) + t.Run("not delivered and rejected", func(t *testing.T) { + svc, fake, bot := pushFixture(t, domain.BotChannelDingTalk) + bot = verifyTarget(t, svc, bot) + putPushRun(t, svc, bot, "run.succeeded", "node.failed") + fake.results = []botinteraction.SendResult{{Outcome: botinteraction.NotDelivered, Retryable: true, SafeDetail: "限流"}, {Outcome: botinteraction.Rejected, Code: "invalid_staff", SafeDetail: "成员不可见"}} + drain(t, svc) + got := deliveries(t, svc) + statuses := []string{got["succeeded"].Status, got["failed"].Status} + if !(contains(statuses, "pending") && contains(statuses, "target_unavailable")) { + t.Fatalf("statuses = %v", statuses) + } + for _, item := range got { + if item.Status == "pending" && item.SendStartedAtUS != 0 { + t.Fatal("not-delivered attempt kept its start time") + } + } + }) +} + +func TestPushTargetRevocationStopsDelivery(t *testing.T) { + svc, fake, bot := pushFixture(t, domain.BotChannelFeishu) + bot = verifyTarget(t, svc, bot) + putPushRun(t, svc, bot, "run.succeeded") + binding, err := svc.Store.BotUserBinding(bot.IdentityScope(), "member-im") + if err != nil { + t.Fatal(err) + } + if err := svc.Store.RevokeBotUserBinding(binding.ID, 43, time.Now().UTC()); err != nil { + t.Fatal(err) + } + trials := fake.count() + drain(t, svc) + item := deliveries(t, svc)["succeeded"] + if item.Status != "target_unavailable" || fake.count() != trials { + t.Fatalf("revoked target = %+v", item) + } + reread, _ := svc.Store.NotificationBot(bot.ID) + if reread.Target == nil || reread.Target.VerifiedAt != nil { + t.Fatalf("revoked target still shown verified: %+v", reread.Target) + } + // Re-binding requires a fresh trial send (outside the link rate limit). + now := time.Now().UTC().Add(2 * time.Minute) + token, _ := svc.Store.NewBotBindingChallenge(bot, "member-im", now, true) + code, _ := svc.Store.ClaimBotBindingChallenge(token, 43, now) + if _, err := svc.Store.ConfirmBotUserBinding(bot, "member-im", code, now); err != nil { + t.Fatal(err) + } + if b, _ := svc.Store.BotUserBinding(bot.IdentityScope(), "member-im"); b.TargetVerifiedAt != nil { + t.Fatal("re-binding kept the old verification") + } +} + +func TestPushWaitingNotificationExpiresWhenNodeMoves(t *testing.T) { + svc, fake, bot := pushFixture(t, domain.BotChannelDingTalk) + bot = verifyTarget(t, svc, bot) + run := putPushRun(t, svc, bot, "gate.waiting") + run.Nodes["a"].Status = "succeeded" + run.Status = "succeeded" + if err := svc.Store.PutRun(run); err != nil { + t.Fatal(err) + } + trials := fake.count() + drain(t, svc) + if item := deliveries(t, svc)["waiting"]; item.Status != "expired" || fake.count() != trials { + t.Fatalf("stale waiting = %+v", item) + } +} + +func TestSwitchNotificationTarget(t *testing.T) { + svc, _, bot := pushFixture(t, domain.BotChannelDingTalk) + bot = verifyTarget(t, svc, bot) + putPushRun(t, svc, bot, "run.succeeded", "node.failed") + if n, _ := svc.Store.PendingNotificationsForOtherTargets(bot.ID, "new-target"); n != 2 { + t.Fatalf("pending = %d", n) + } + if n, err := svc.Store.SwitchNotificationTarget(bot.ID, domain.BotChat{ID: "new-target", Name: "新", Type: "single"}, "migrate"); err != nil || n != 2 { + t.Fatal(n, err) + } + for _, item := range deliveries(t, svc) { + if item.TargetID != "new-target" || item.DeliveryGeneration != 1 || !strings.Contains(item.LastError, "改发") { + t.Fatalf("migrated = %+v", item) + } + } + if n, _ := svc.Store.SwitchNotificationTarget(bot.ID, domain.BotChat{ID: "third"}, "cancel"); n != 2 { + t.Fatalf("cancelled = %d", n) + } + for _, item := range deliveries(t, svc) { + if item.Status != "cancelled" { + t.Fatalf("cancel = %+v", item) + } + } +} + +func TestPlainNotificationText(t *testing.T) { + in := "### SOP 已完成\n> **交付** 已完成\n成功\n[查看详情](https://platform.example/#/runs/r)(请处理)" + got := plainNotificationText(in) + want := "SOP 已完成\n交付 已完成\n成功\n查看详情:\nhttps://platform.example/#/runs/r\n(请处理)" + if got != want { + t.Fatalf("plain = %q", got) + } +} + +func contains(list []string, v string) bool { + for _, s := range list { + if s == v { + return true + } + } + return false +} + +func forceDue(t *testing.T, svc *Service, id string) { + t.Helper() + if err := svc.Store.DB.Model(&domain.NotificationDelivery{}).Where("id = ?", id).Update("next_attempt_at", time.Now().UTC().Add(-time.Second)).Error; err != nil { + t.Fatal(err) + } +} + +func expireLease(t *testing.T, svc *Service, id string) { + t.Helper() + if err := svc.Store.DB.Model(&domain.NotificationDelivery{}).Where("id = ?", id).Update("lease_until", time.Now().UTC().Add(-time.Second)).Error; err != nil { + t.Fatal(err) + } +} + +func TestPushSendsActionCardOnlyWhenTheBotReceivesClicks(t *testing.T) { + for _, interaction := range []bool{false, true} { + svc, fake, bot := pushFixture(t, domain.BotChannelFeishu) + bot = verifyTarget(t, svc, bot) + bot.InteractionEnabled = interaction + if err := svc.Store.SaveNotificationBot(bot, ""); err != nil { + t.Fatal(err) + } + trials := fake.count() + putPushRun(t, svc, bot, "gate.waiting", "node.failed", "run.succeeded") + drain(t, svc) + cards := 0 + for _, msg := range fake.sent[trials:] { + if msg.Card != nil { + cards++ + if len(msg.Card.Actions) == 0 || msg.Card.DeliveryID == "" || msg.Text == "" { + t.Fatalf("card = %+v", msg.Card) + } + } + } + if want := map[bool]int{false: 0, true: 2}[interaction]; cards != want { + t.Fatalf("interaction=%v cards = %d, want %d", interaction, cards, want) + } + } +} diff --git a/internal/notification/service.go b/internal/notification/service.go index 530291a9b..81a3f4809 100644 --- a/internal/notification/service.go +++ b/internal/notification/service.go @@ -8,8 +8,10 @@ import ( "crypto/rand" "encoding/hex" "errors" + "log" "net/url" "os" + "strconv" "strings" "time" ) @@ -24,7 +26,35 @@ type Service struct { func New(s *repo.Store) *Service { base := strings.TrimRight(os.Getenv("AGENTFLOW_PUBLIC_URL"), "/") - return &Service{Store: s, Provider: NewCLIProvider(), PublicURL: base, Interactions: botinteraction.New(s)} + provider := NewCLIProvider() + svc := &Service{Store: s, Provider: provider, PublicURL: base, Interactions: botinteraction.New(s)} + svc.Interactions.PublicURL = func() string { return svc.PublicURL } + // The WeCom channel verifies credentials through the signed bootstrap and + // pushes through the current Provider (replaceable in tests). WeCom + // notification delivery keeps calling Provider directly (cards and the + // chat list come from wecom-cli); Feishu and DingTalk deliveries go + // through the channel PushSender (push.go). + svc.Interactions.ConfigureWeCom(provider.Verify, func(ctx context.Context, botID, secret, target, content string) botinteraction.SendResult { + return classifyProviderSend(svc.Provider.Send(ctx, botID, secret, target, content)) + }) + return svc +} + +// classifyProviderSend maps wecom-cli outcomes to delivery evidence. Platform +// error codes and local content validation are explicit rejections; +// any other failure may have happened after the CLI submitted the message. +func classifyProviderSend(err error) botinteraction.SendResult { + if err == nil { + return botinteraction.SendResult{Outcome: botinteraction.Delivered} + } + var pe *ProviderError + if errors.As(err, &pe) { + return botinteraction.SendResult{Outcome: botinteraction.Rejected, Code: strconv.Itoa(pe.Code), SafeDetail: pe.Error()} + } + if errors.Is(err, errNotificationContentLength) { + return botinteraction.SendResult{Outcome: botinteraction.Rejected, Code: "invalid_content", SafeDetail: err.Error()} + } + return botinteraction.SendResult{Outcome: botinteraction.Unknown, Code: "unconfirmed", SafeDetail: err.Error()} } func (s *Service) secret(bot domain.NotificationBot) (string, error) { secret, err := s.Store.BotCredential(bot.ID) @@ -33,7 +63,20 @@ func (s *Service) secret(bot domain.NotificationBot) (string, error) { } return secret, nil } +func randomToken() string { + b := make([]byte, 16) + if _, err := rand.Read(b); err != nil { + panic(err) + } + return hex.EncodeToString(b) +} + +// Sessions lists selectable notification targets: the wecom-cli chat list, +// or the bound private-chat senders of a Feishu/DingTalk configuration. func (s *Service) Sessions(ctx context.Context, bot domain.NotificationBot) ([]domain.BotChat, error) { + if pushChannel(bot) { + return s.pushTargets(bot) + } secret, err := s.secret(bot) if err != nil { return nil, err @@ -95,7 +138,28 @@ func (s *Service) send(ctx context.Context, bot domain.NotificationBot, targetID } return s.Provider.Send(ctx, bot.BotID, secret, targetID, content) } + +// BotWorkersEnabled reads AGENTFLOW_BOT_WORKERS_ENABLED (default true). The +// upgrade maintenance window starts the new version with false: neither the +// conversation receivers nor the notification sender run, and Bot +// configuration writes are refused, while DDL and backfills are applied. +func BotWorkersEnabled() bool { + raw := strings.TrimSpace(os.Getenv("AGENTFLOW_BOT_WORKERS_ENABLED")) + if raw == "" { + return true + } + enabled, err := strconv.ParseBool(raw) + return err != nil || enabled +} + func (s *Service) Start(ctx context.Context) { + if !BotWorkersEnabled() { + log.Printf("bot workers disabled by AGENTFLOW_BOT_WORKERS_ENABLED=false: conversation receivers and notification delivery are not started") + return + } + if n, err := s.Store.UnmigratedNotificationBots(); err == nil && n > 0 { + log.Printf("bot workers: %d Bot configuration(s) lack physical columns (config_version=0); run migrations/036_enterprise_im_channels.sql — they do not receive messages until then", n) + } if s.Interactions != nil { s.Interactions.Start(ctx) } @@ -200,7 +264,7 @@ func (s *Service) ProcessOne(ctx context.Context) (bool, error) { if item.ContentSource == "" { content = strings.ReplaceAll(content, "{{url}}", link) } - err = s.send(ctx, bot, item.TargetID, content, func() error { + beforeSend := func() error { if item.EventType != "waiting" || item.WaitingPolicy == "event" { return nil } @@ -212,10 +276,17 @@ func (s *Service) ProcessOne(ctx context.Context) (bool, error) { return errNotificationExpired } return nil - }, *item) + } + if pushChannel(bot) { + return s.processPush(ctx, item, bot, content, beforeSend) + } + err = s.send(ctx, bot, item.TargetID, content, beforeSend, *item) if errors.Is(err, errNotificationExpired) || errors.Is(err, botinteraction.ErrCardExpired) { return finish("expired", err.Error()) } + if errors.Is(err, botinteraction.ErrCardDeliveryUnknown) { + return finish("send_unknown", err.Error()) + } if err == nil { return finish("sent", "") } diff --git a/internal/repo/automigrate.go b/internal/repo/automigrate.go index c05436fdc..780a5dd20 100644 --- a/internal/repo/automigrate.go +++ b/internal/repo/automigrate.go @@ -60,7 +60,7 @@ func autoMigrateIncremental(db *gorm.DB) error { func needsUTF8MB4Table(model any) bool { switch model.(type) { - case domain.BotInteraction, pageFeedbackRow, pageFeedbackImageRow: + case domain.BotInteraction, botPendingDraftRow, pageFeedbackRow, pageFeedbackImageRow: return true } return false diff --git a/internal/repo/bot_charset_mysql_test.go b/internal/repo/bot_charset_mysql_test.go index c6b98a372..98f479336 100644 --- a/internal/repo/bot_charset_mysql_test.go +++ b/internal/repo/bot_charset_mysql_test.go @@ -27,7 +27,17 @@ func TestBotCharsetMySQLMigration(t *testing.T) { if charset != "utf8mb4" { t.Fatal("new table inherited utf8mb3", charset) } - item := domain.BotInteraction{ID: "emoji-regression", Status: "running", BotID: "test"} + bot := domain.NotificationBot{ID: "emoji-bot", BotID: "emoji-external", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + store.SetCredentialKeyring(testCredentialKeyring) + if err := store.SaveNotificationBot(bot, "s"); err != nil { + t.Fatal(err) + } + defer store.DeleteNotificationBot(bot.ID) + lease, _, err := store.ClaimBotConnection(bot.LeaseKey(), "emoji-owner") + if err != nil { + t.Fatal(err) + } + item := domain.BotInteraction{ID: "emoji-regression", Status: "running", BotID: bot.ID, LeaseOwner: lease.Owner, LeaseEpoch: lease.Epoch} if err := store.DB.Create(&item).Error; err != nil { t.Fatal(err) } @@ -35,7 +45,7 @@ func TestBotCharsetMySQLMigration(t *testing.T) { if err := store.DB.Exec("ALTER TABLE bot_interactions CONVERT TO CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci").Error; err != nil { t.Fatal(err) } - err = store.SaveBotReply(item, "你好 👋", "") + err = store.SaveBotReply(item, lease, "你好 👋", "") var sqlErr *mysql.MySQLError if !errors.As(err, &sqlErr) || (sqlErr.Number != 1366 && sqlErr.Number != 3988) { t.Fatal("utf8mb3 failure not reproduced", err) @@ -47,7 +57,7 @@ func TestBotCharsetMySQLMigration(t *testing.T) { if err := store.DB.Exec(string(script)).Error; err != nil { t.Fatal(err) } - if err := store.SaveBotReply(item, "你好 👋", ""); err != nil { + if err := store.SaveBotReply(item, lease, "你好 👋", ""); err != nil { t.Fatal(err) } saved, err := store.BotInteraction(item.ID) diff --git a/internal/repo/bot_credentials.go b/internal/repo/bot_credentials.go index ac32416de..6f01b5bad 100644 --- a/internal/repo/bot_credentials.go +++ b/internal/repo/bot_credentials.go @@ -1,28 +1,241 @@ package repo import ( + "agentflow/internal/credcrypt" + "agentflow/internal/domain" + "encoding/json" + "errors" + "fmt" + "sort" + "strings" + "gorm.io/gorm" "gorm.io/gorm/clause" ) +// Bot secrets are stored per configuration ID. New values are sealed with +// the credential keyring (credcrypt, "lf1:" prefix); legacy plaintext rows +// stay readable until the explicit -credential-action reencrypt migrates them. type botCredentialRow struct { BotID string `gorm:"primaryKey;size:191"` Secret string `gorm:"column:ciphertext;type:text;not null"` } func (botCredentialRow) TableName() string { return "user_bot_credentials" } + +// ErrCredentialKeyMissing is returned when a secret must be sealed but no +// credential key is configured; HTTP maps it to 503 credential_key_missing. +var ErrCredentialKeyMissing = credcrypt.ErrKeyMissing + +type botCredentialPayload struct { + Secret string `json:"secret"` +} + +// SetCredentialKeyring installs the keyring used to seal and open Bot +// credentials. A nil keyring keeps legacy plaintext readable but refuses new +// secrets. +func (r *UsersRepo) SetCredentialKeyring(k *credcrypt.Keyring) { + r.keyringMu.Lock() + defer r.keyringMu.Unlock() + r.keyring = k +} + +func (r *UsersRepo) credentialKeyring() *credcrypt.Keyring { + r.keyringMu.RLock() + defer r.keyringMu.RUnlock() + return r.keyring +} + +// SealBotRoute encrypts authorization-bearing reply routing (the DingTalk +// sessionWebhook) bound to one configuration and interaction, so a copied +// value cannot be opened for another interaction. +func (r *UsersRepo) SealBotRoute(configID, interactionID, route string) (string, error) { + if r.credentialKeyring() == nil { + return "", ErrCredentialKeyMissing + } + return r.credentialKeyring().Seal([]byte(route), botRouteAAD(configID, interactionID)) +} + +// OpenBotRoute reverses SealBotRoute. Values that are not sealed are routing +// data without authority (Feishu message IDs) and are returned unchanged. +func (r *UsersRepo) OpenBotRoute(configID, interactionID, stored string) (string, error) { + if !credcrypt.IsSealed(stored) { + return stored, nil + } + plain, err := r.credentialKeyring().Open(stored, botRouteAAD(configID, interactionID)) + if err != nil { + return "", err + } + return string(plain), nil +} + +func botRouteAAD(configID, interactionID string) string { + return "bot-route|" + configID + "|" + interactionID + "|session_webhook" +} + +func botCredentialAAD(id, channel string) string { + return "bot-credential|" + id + "|" + domain.NotificationBot{Channel: channel}.ChannelKind() +} + +// botChannel reads the immutable channel of a configuration for credential +// binding. Rows not yet backfilled by migration 036 are legacy WeCom. +func (r *UsersRepo) botChannel(db *gorm.DB, id string) string { + var channel string + db.Table("notification_bots").Select("channel").Where("id = ?", id).Scan(&channel) + return channel +} + +func (r *UsersRepo) sealBotSecret(id, channel, secret string) (string, error) { + plain, _ := json.Marshal(botCredentialPayload{Secret: secret}) + return r.credentialKeyring().Seal(plain, botCredentialAAD(id, channel)) +} + +// openBotSecret returns legacy plaintext as-is; a sealed value must open. +func (r *UsersRepo) openBotSecret(id, channel, stored string) (string, error) { + if !credcrypt.IsSealed(stored) { + return stored, nil + } + plain, err := r.credentialKeyring().Open(stored, botCredentialAAD(id, channel)) + if err != nil { + return "", err + } + var payload botCredentialPayload + if err := json.Unmarshal(plain, &payload); err != nil { + return "", credcrypt.ErrDecrypt + } + return payload.Secret, nil +} + func (r *UsersRepo) BotCredential(botID string) (string, error) { var row botCredentialRow - err := r.DB.Where("bot_id = ?", botID).First(&row).Error - if err == gorm.ErrRecordNotFound { + if err := r.DB.Where("bot_id = ?", botID).First(&row).Error; err != nil { return "", err } - return row.Secret, err + return r.openBotSecret(botID, r.botChannel(r.DB, botID), row.Secret) } -func (r *UsersRepo) putBotCredential(tx *gorm.DB, id, secret string) error { - return tx.Clauses(clause.OnConflict{UpdateAll: true}).Create(&botCredentialRow{BotID: id, Secret: secret}).Error +// botCredentialFormats reports plaintext/encrypted per configuration ID. +func (r *UsersRepo) botCredentialFormats(ids []string) map[string]string { + out := map[string]string{} + if len(ids) == 0 { + return out + } + var rows []botCredentialRow + r.DB.Where("bot_id IN ?", ids).Find(&rows) + for _, row := range rows { + if credcrypt.IsSealed(row.Secret) { + out[row.BotID] = "encrypted" + } else { + out[row.BotID] = "plaintext" + } + } + return out +} + +func (r *UsersRepo) putBotCredential(tx *gorm.DB, id, channel, secret string) error { + sealed, err := r.sealBotSecret(id, channel, secret) + if err != nil { + return err + } + return tx.Clauses(clause.OnConflict{UpdateAll: true}).Create(&botCredentialRow{BotID: id, Secret: sealed}).Error } func (r *UsersRepo) deleteBotCredential(tx *gorm.DB, id string) error { return tx.Where("bot_id = ?", id).Delete(&botCredentialRow{}).Error } + +// CredentialReport summarizes stored Bot credentials for -credential-action. +type CredentialReport struct { + CurrentKID string `json:"currentKid"` + Total int `json:"total"` + Plaintext int `json:"plaintext"` + ByKID map[string]int `json:"byKid"` + Rewrapped int `json:"rewrapped"` + Skipped int `json:"skipped"` + Failed []string `json:"failed,omitempty"` +} + +// String prints counts and key ids only, never values. +func (c CredentialReport) String() string { + parts := []string{fmt.Sprintf("current_kid=%s total=%d plaintext=%d", c.CurrentKID, c.Total, c.Plaintext)} + kids := make([]string, 0, len(c.ByKID)) + for kid := range c.ByKID { + kids = append(kids, kid) + } + sort.Strings(kids) + for _, kid := range kids { + parts = append(parts, fmt.Sprintf("kid_%s=%d", kid, c.ByKID[kid])) + } + parts = append(parts, fmt.Sprintf("rewrapped=%d skipped=%d failed=%d", c.Rewrapped, c.Skipped, len(c.Failed))) + return strings.Join(parts, " ") +} + +// BotCredentialStatus counts plaintext rows and sealed rows per key id. +func (r *UsersRepo) BotCredentialStatus() (CredentialReport, error) { + report := CredentialReport{CurrentKID: r.credentialKeyring().CurrentKID(), ByKID: map[string]int{}} + var rows []botCredentialRow + if err := r.DB.Order("bot_id").Find(&rows).Error; err != nil { + return report, err + } + for _, row := range rows { + report.Total++ + if kid := credcrypt.KID(row.Secret); kid != "" { + report.ByKID[kid]++ + } else { + report.Plaintext++ + } + } + return report, nil +} + +// ReencryptBotCredentials seals legacy plaintext and rewraps values under old +// key ids with the current key, one row per transaction. It is re-entrant: +// rows already under the current key are skipped, and a failed row is +// reported without stopping the rest. +func (r *UsersRepo) ReencryptBotCredentials() (CredentialReport, error) { + keyring := r.credentialKeyring() + report := CredentialReport{CurrentKID: keyring.CurrentKID(), ByKID: map[string]int{}} + if keyring.CurrentKID() == "" { + return report, ErrCredentialKeyMissing + } + var ids []string + if err := r.DB.Model(&botCredentialRow{}).Order("bot_id").Pluck("bot_id", &ids).Error; err != nil { + return report, err + } + for _, id := range ids { + report.Total++ + rewrapped := false + err := r.DB.Transaction(func(tx *gorm.DB) error { + var row botCredentialRow + if err := r.locking(tx).Where("bot_id = ?", id).First(&row).Error; err != nil { + return err + } + if !keyring.NeedsRewrap(row.Secret) { + return nil + } + channel := r.botChannel(tx, id) + secret, err := r.openBotSecret(id, channel, row.Secret) + if err != nil { + return err + } + sealed, err := r.sealBotSecret(id, channel, secret) + if err != nil { + return err + } + rewrapped = true + return tx.Model(&botCredentialRow{}).Where("bot_id = ?", id).Update("ciphertext", sealed).Error + }) + switch { + case err != nil && !errors.Is(err, gorm.ErrRecordNotFound): + report.Failed = append(report.Failed, id+": "+err.Error()) + case rewrapped: + report.Rewrapped++ + default: + report.Skipped++ + } + } + final, err := r.BotCredentialStatus() + if err == nil { + report.Plaintext, report.ByKID = final.Plaintext, final.ByKID + } + return report, err +} diff --git a/internal/repo/bot_credentials_test.go b/internal/repo/bot_credentials_test.go new file mode 100644 index 000000000..937711e89 --- /dev/null +++ b/internal/repo/bot_credentials_test.go @@ -0,0 +1,250 @@ +package repo + +import ( + "agentflow/internal/credcrypt" + "agentflow/internal/domain" + "encoding/base64" + "errors" + "strings" + "testing" +) + +func testKeyring(t *testing.T, spec string) *credcrypt.Keyring { + t.Helper() + parts := []string{} + for _, kid := range strings.Split(spec, ",") { + parts = append(parts, kid+":"+base64.StdEncoding.EncodeToString([]byte(strings.Repeat(kid[:1], 32)))) + } + k, err := credcrypt.Parse(strings.Join(parts, ",")) + if err != nil { + t.Fatal(err) + } + return k +} + +func TestBotCredentialSealedLegacyReadableAndReencrypt(t *testing.T) { + s, err := OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + s.SetCredentialKeyring(testKeyring(t, "a1")) + bot := domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelFeishu, BotID: "cli_x", ProjectID: "p", OwnerID: 1} + if err := s.SaveNotificationBot(bot, "app-secret"); err != nil { + t.Fatal(err) + } + var row botCredentialRow + s.DB.First(&row, "bot_id = ?", "cfg") + if !strings.HasPrefix(row.Secret, "lf1:a1:") || strings.Contains(row.Secret, "app-secret") { + t.Fatalf("stored = %q", row.Secret) + } + if got, err := s.BotCredential("cfg"); err != nil || got != "app-secret" { + t.Fatalf("read = %q %v", got, err) + } + // A ciphertext copied to another configuration does not open. + s.DB.Create(&botCredentialRow{BotID: "other", Secret: row.Secret}) + if _, err := s.BotCredential("other"); !errors.Is(err, credcrypt.ErrDecrypt) { + t.Fatalf("copied ciphertext opened: %v", err) + } + s.DB.Where("bot_id = ?", "other").Delete(&botCredentialRow{}) + // Legacy plaintext stays readable and is reported for migration. + legacy := domain.NotificationBot{ID: "legacy", BotID: "wx", ProjectID: "p", OwnerID: 1} + if err := s.SaveNotificationBot(legacy, ""); err != nil { + t.Fatal(err) + } + s.DB.Create(&botCredentialRow{BotID: "legacy", Secret: "plain-secret"}) + if got, _ := s.BotCredential("legacy"); got != "plain-secret" { + t.Fatalf("legacy read = %q", got) + } + if b, _ := s.NotificationBot("legacy"); b.CredentialFormat != "plaintext" { + t.Fatalf("legacy format = %q", b.CredentialFormat) + } + // Rotation: new key first, old key still readable; reencrypt is re-entrant. + s.SetCredentialKeyring(testKeyring(t, "b2,a1")) + report, err := s.ReencryptBotCredentials() + if err != nil || report.Rewrapped != 2 || report.Plaintext != 0 || report.ByKID["b2"] != 2 { + t.Fatalf("reencrypt = %+v %v", report, err) + } + again, err := s.ReencryptBotCredentials() + if err != nil || again.Rewrapped != 0 || again.Skipped != 2 { + t.Fatalf("second run = %+v %v", again, err) + } + for id, want := range map[string]string{"cfg": "app-secret", "legacy": "plain-secret"} { + if got, err := s.BotCredential(id); err != nil || got != want { + t.Fatalf("%s after rotation = %q %v", id, got, err) + } + } + if strings.Contains(report.String(), "secret") { + t.Fatal("report printed a value") + } + // Without a key new secrets are refused and nothing is written. + s.SetCredentialKeyring(nil) + bot2 := bot + bot2.ID, bot2.BotID = "cfg2", "cli_y" + if err := s.SaveNotificationBot(bot2, "s"); !errors.Is(err, ErrCredentialKeyMissing) { + t.Fatalf("missing key = %v", err) + } + if _, err := s.NotificationBot("cfg2"); err == nil { + t.Fatal("configuration saved without its credential") + } + if _, err := s.BotCredential("cfg"); !errors.Is(err, credcrypt.ErrKeyMissing) { + t.Fatalf("sealed value without keyring = %v", err) + } +} + +func TestBotConfigVersionsCASAndImmutableFields(t *testing.T) { + s, err := OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + bot := domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelFeishu, BotID: "cli_x", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + saved, err := s.SaveNotificationBotChecked(bot, "s1", nil) + if err != nil || saved.ConfigVersion != 1 || saved.CredentialVersion != 1 || saved.TenantState != domain.BotTenantAwaiting { + t.Fatalf("create = %+v %v", saved, err) + } + stale := int64(0) + if _, err := s.SaveNotificationBotChecked(saved, "", &stale); !errors.Is(err, ErrBotConfigChanged) { + t.Fatalf("stale version = %v", err) + } + saved.Name = "renamed" + v := saved.ConfigVersion + renamed, err := s.SaveNotificationBotChecked(saved, "", &v) + if err != nil || renamed.ConfigVersion != 2 || renamed.CredentialVersion != 1 || renamed.ConnectionFingerprint() != saved.ConnectionFingerprint() { + t.Fatalf("rename = %+v %v", renamed, err) + } + rotated, err := s.SaveNotificationBotChecked(renamed, "s2", nil) + if err != nil || rotated.CredentialVersion != 2 || rotated.ConnectionFingerprint() == renamed.ConnectionFingerprint() { + t.Fatalf("rotate = %+v %v", rotated, err) + } + moved := rotated + moved.BotID = "cli_other" + if _, err := s.SaveNotificationBotChecked(moved, "", nil); !errors.Is(err, ErrBotImmutableField) { + t.Fatalf("app change = %v", err) + } + switched := rotated + switched.Channel = domain.BotChannelWeCom + if _, err := s.SaveNotificationBotChecked(switched, "", nil); !errors.Is(err, ErrBotImmutableField) { + t.Fatalf("channel change = %v", err) + } + // The same Feishu app and a WeCom Bot with the same external ID do not + // collide: receive slots include the channel. + wecom := domain.NotificationBot{ID: "wx", BotID: "cli_x", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + if err := s.SaveNotificationBot(wecom, "s"); err != nil { + t.Fatalf("cross-channel slot = %v", err) + } + dup := bot + dup.ID = "cfg-dup" + if err := s.SaveNotificationBot(dup, "s"); !errors.Is(err, ErrInteractiveBotAlreadyExists) { + t.Fatalf("same app twice = %v", err) + } +} + +func TestBotTenantCandidateAndConfirmation(t *testing.T) { + s, err := OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + bot := domain.NotificationBot{ID: "cfg", Channel: domain.BotChannelFeishu, BotID: "cli_x", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + saved, _ := s.SaveNotificationBotChecked(bot, "s1", nil) + if _, err := s.ConfirmBotTenant("cfg", "t1", saved.ConfigVersion, 9); !errors.Is(err, ErrBotTenantCandidate) { + t.Fatalf("confirm before discovery = %v", err) + } + state, err := s.RecordBotTenantCandidate("cfg", "t1", "evt-1", saved.CredentialVersion) + if err != nil || state != domain.BotTenantAwaiting { + t.Fatal(state, err) + } + after, _ := s.NotificationBot("cfg") + if after.ConfigVersion != saved.ConfigVersion || after.TenantCandidate == nil || after.TenantCandidate.TenantKey != "t1" { + t.Fatalf("candidate changed the version or was lost: %+v", after) + } + if _, err := s.ConfirmBotTenant("cfg", "t2", after.ConfigVersion, 9); !errors.Is(err, ErrBotTenantCandidate) { + t.Fatalf("confirm of a different key = %v", err) + } + locked, err := s.ConfirmBotTenant("cfg", "t1", after.ConfigVersion, 9) + if err != nil || locked.TenantState != domain.BotTenantLocked || locked.ConfigVersion != after.ConfigVersion+1 || locked.TenantConfirmedBy != 9 { + t.Fatalf("confirm = %+v %v", locked, err) + } + stored, _ := s.NotificationBot("cfg") + if stored.TenantKey != "t1" || stored.ConnectionFingerprint() != saved.ConnectionFingerprint() { + t.Fatalf("locked = %+v", stored) + } + // Rotating the secret keeps the lock. + rotated, _ := s.SaveNotificationBotChecked(stored, "s2", nil) + if rotated.TenantState != domain.BotTenantLocked || rotated.TenantKey != "t1" { + t.Fatalf("rotation dropped the lock: %+v", rotated) + } + // A second, different candidate on an unlocked configuration is a conflict. + other := domain.NotificationBot{ID: "cfg2", Channel: domain.BotChannelFeishu, BotID: "cli_y", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + o, _ := s.SaveNotificationBotChecked(other, "s", nil) + s.RecordBotTenantCandidate("cfg2", "t1", "e1", o.CredentialVersion) + if state, _ := s.RecordBotTenantCandidate("cfg2", "t9", "e2", o.CredentialVersion); state != domain.BotTenantConflict { + t.Fatalf("conflict = %s", state) + } + if reset, err := s.RediscoverBotTenant("cfg2", o.ConfigVersion); err != nil || reset.TenantState != domain.BotTenantAwaiting || reset.TenantCandidate != nil { + t.Fatalf("rediscover = %+v %v", reset, err) + } +} + +func TestDingTalkExpectedTenantAndSealedRoute(t *testing.T) { + s, err := OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + bot := domain.NotificationBot{ID: "dt", Channel: domain.BotChannelDingTalk, BotID: "ding-app", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true, TenantExpected: "corp-a", TenantExpectedBy: 5} + saved, err := s.SaveNotificationBotChecked(bot, "secret", nil) + if err != nil || saved.TenantState != domain.BotTenantAwaiting || saved.TenantExpected != "corp-a" { + t.Fatalf("create = %+v %v", saved, err) + } + // A different enterprise first: conflict, and it stays a conflict even + // when the expected enterprise shows up afterwards. + if state, _ := s.RecordBotTenantCandidate("dt", "corp-x", "e1", saved.CredentialVersion); state != domain.BotTenantConflict { + t.Fatalf("foreign first = %s", state) + } + if state, _ := s.RecordBotTenantCandidate("dt", "corp-a", "e2", saved.CredentialVersion); state != domain.BotTenantConflict { + t.Fatalf("conflict must be sticky, got %s", state) + } + conflicted, _ := s.NotificationBot("dt") + if conflicted.TenantCandidate == nil || conflicted.TenantCandidate.Conflict != "corp-x" || conflicted.TenantKey != "" { + t.Fatalf("conflict = %+v", conflicted) + } + // The administrator corrects the CorpId: the check starts afresh. + conflicted.TenantExpected, conflicted.TenantExpectedBy = "corp-x", 6 + corrected, err := s.SaveNotificationBotChecked(conflicted, "", nil) + if err != nil || corrected.TenantState != domain.BotTenantAwaiting || corrected.TenantCandidate != nil || corrected.TenantExpected != "corp-x" { + t.Fatalf("correct = %+v %v", corrected, err) + } + if state, _ := s.RecordBotTenantCandidate("dt", "corp-x", "e3", corrected.CredentialVersion); state != domain.BotTenantLocked { + t.Fatalf("match = %s", state) + } + locked, _ := s.NotificationBot("dt") + if locked.TenantKey != "corp-x" || locked.TenantConfirmedBy != 6 || locked.ConfigVersion != corrected.ConfigVersion+1 || locked.ConnectionFingerprint() != corrected.ConnectionFingerprint() { + t.Fatalf("locked = %+v", locked) + } + // Once locked the expected value can no longer be changed. + locked.TenantExpected = "corp-y" + after, _ := s.SaveNotificationBotChecked(locked, "", nil) + if after.TenantKey != "corp-x" || after.TenantExpected != "corp-x" || after.TenantState != domain.BotTenantLocked { + t.Fatalf("locked change = %+v", after) + } + // Session webhooks are sealed per interaction. + sealed, err := s.SealBotRoute("dt", "i1", "https://oapi.dingtalk.com/robot/sendBySession?session=x") + if err != nil || !strings.HasPrefix(sealed, "lf1:") || strings.Contains(sealed, "session") { + t.Fatalf("seal = %q %v", sealed, err) + } + if got, err := s.OpenBotRoute("dt", "i1", sealed); err != nil || !strings.HasSuffix(got, "session=x") { + t.Fatalf("open = %q %v", got, err) + } + if _, err := s.OpenBotRoute("dt", "i2", sealed); err == nil { + t.Fatal("route opened for another interaction") + } + if got, _ := s.OpenBotRoute("dt", "i1", "om_plain"); got != "om_plain" { + t.Fatalf("plain route = %q", got) + } + s.SetCredentialKeyring(nil) + if _, err := s.SealBotRoute("dt", "i1", "x"); !errors.Is(err, ErrCredentialKeyMissing) { + t.Fatalf("no keyring = %v", err) + } +} diff --git a/internal/repo/bot_dbtime_test.go b/internal/repo/bot_dbtime_test.go new file mode 100644 index 000000000..e27b42acc --- /dev/null +++ b/internal/repo/bot_dbtime_test.go @@ -0,0 +1,166 @@ +package repo + +import ( + "agentflow/internal/domain" + "errors" + "fmt" + "os" + "sync" + "testing" + "time" +) + +// The database clock expression must agree with real time within a small +// tolerance on both dialects, and stored *_us values must compare as integers. +func TestDBNowUSMatchesWallClockOnSQLite(t *testing.T) { + s, err := OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + checkDBClock(t, s) +} + +func TestDBNowUSMatchesWallClockOnMySQL(t *testing.T) { + s := openBotMySQL(t) + checkDBClock(t, s) + // SET timestamp skews NOW(6) for this session: the lease follows the + // database clock, not the API host clock. + sqlDB, _ := s.DB.DB() + sqlDB.SetMaxOpenConns(1) + if err := s.DB.Exec("SET timestamp = ?", float64(time.Now().Add(-time.Hour).UnixMicro())/1e6).Error; err != nil { + t.Fatal(err) + } + defer s.DB.Exec("SET timestamp = DEFAULT") + now, err := s.dbNowUS(s.DB) + if err != nil || time.Since(time.UnixMicro(now)) < 59*time.Minute { + t.Fatalf("skewed clock = %v %v", time.UnixMicro(now), err) + } +} + +func checkDBClock(t *testing.T, s *Store) { + t.Helper() + before := time.Now().UnixMicro() + now, err := s.dbNowUS(s.DB) + after := time.Now().UnixMicro() + if err != nil || now < before-2_000_000 || now > after+2_000_000 { + t.Fatalf("db clock %d outside [%d,%d]: %v", now, before, after, err) + } + // Integer comparison across second boundaries: a value written one + // microsecond ago is in the past, one written a second ahead is not. + key := fmt.Sprintf("clock-%d", time.Now().UnixNano()) + defer s.DB.Where("bot_id = ?", key).Delete(&domain.BotConnection{}) + if err := s.DB.Create(&domain.BotConnection{BotID: key, LeaseUntil: time.Now().UTC()}).Error; err != nil { + t.Fatal(err) + } + if err := s.DB.Model(&domain.BotConnection{}).Where("bot_id = ?", key).Update("lease_until_us", s.nowUSExpr(1_000_000)).Error; err != nil { + t.Fatal(err) + } + var live int64 + s.DB.Model(&domain.BotConnection{}).Where("bot_id = ? AND lease_until_us > "+s.dbNowUSSQL(), key).Count(&live) + if live != 1 { + t.Fatal("future lease not live") + } + s.DB.Model(&domain.BotConnection{}).Where("bot_id = ?", key).Update("lease_until_us", s.nowUSExpr(-1)) + s.DB.Model(&domain.BotConnection{}).Where("bot_id = ? AND lease_until_us > "+s.dbNowUSSQL(), key).Count(&live) + if live != 0 { + t.Fatal("past lease still live") + } +} + +func openBotMySQL(t *testing.T) *Store { + t.Helper() + dsn := os.Getenv("AGENTFLOW_TEST_MYSQL_DSN") + if dsn == "" { + t.Skip("AGENTFLOW_TEST_MYSQL_DSN is not set") + } + s, err := Open(dsn) + if err != nil { + t.Fatal(err) + } + s.SetCredentialKeyring(testCredentialKeyring) + t.Cleanup(s.Close) + return s +} + +// Concurrent saves of the same receiving application leave exactly one +// enabled receiver (unique receive_slot plus the definition lock). +func TestMySQLConcurrentSavesKeepOneReceiver(t *testing.T) { + s := openBotMySQL(t) + app := fmt.Sprintf("cli_race_%d", time.Now().UnixNano()) + var wg sync.WaitGroup + results := make([]error, 8) + for i := range results { + wg.Add(1) + go func(i int) { + defer wg.Done() + results[i] = s.SaveNotificationBot(domain.NotificationBot{ID: fmt.Sprintf("%s-%d", app, i), Channel: domain.BotChannelFeishu, BotID: app, ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true}, "s") + }(i) + } + wg.Wait() + ok := 0 + for i, err := range results { + defer s.DeleteNotificationBot(fmt.Sprintf("%s-%d", app, i)) + if err == nil { + ok++ + } else if !errors.Is(err, ErrInteractiveBotAlreadyExists) { + t.Fatalf("save %d: %v", i, err) + } + } + if ok != 1 { + t.Fatalf("enabled receivers = %d", ok) + } +} + +// Deleting a Bot while its worker records a send result must not deadlock: +// both follow config -> connection -> interaction. Whichever commits first +// wins; the other sees a state conflict. +func TestMySQLDeleteRacesSendResultWithoutDeadlock(t *testing.T) { + s := openBotMySQL(t) + for round := 0; round < 10; round++ { + id := fmt.Sprintf("race-%d-%d", time.Now().UnixNano(), round) + bot := domain.NotificationBot{ID: id, BotID: "ext-" + id, ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + if err := s.SaveNotificationBot(bot, "s"); err != nil { + t.Fatal(err) + } + lease, _, err := s.ClaimBotConnection(bot.LeaseKey(), "owner") + if err != nil { + t.Fatal(err) + } + item := domain.BotInteraction{ID: BotMessageID(bot.IdentityScope(), "m"), BotID: id, SenderID: "u", Content: "c"} + s.EnqueueBotInteraction(item, "single", "u") + claimed, err := s.ClaimBotInteraction(id, lease, BotSendPolicy{}) + if err != nil || claimed == nil { + t.Fatal(claimed, err) + } + if err := s.SaveBotReply(*claimed, lease, "r", ""); err != nil { + t.Fatal(err) + } + s.ClaimBotInteraction(id, lease, BotSendPolicy{}) + if _, err := s.BeginBotSend(item.ID, lease, BotSendPolicy{}); err != nil { + t.Fatal(err) + } + var wg sync.WaitGroup + var delErr, finErr error + wg.Add(2) + go func() { defer wg.Done(); delErr = s.DeleteNotificationBot(id) }() + go func() { + defer wg.Done() + _, finErr = s.FinishBotSend(item.ID, lease, BotSendPolicy{}, BotSendOutcome{Outcome: "delivered"}) + }() + wg.Wait() + if delErr != nil { + t.Fatalf("round %d delete: %v", round, delErr) + } + got, _ := s.BotInteraction(item.ID) + switch { + case finErr == nil && got.Status == domain.BotReplied: + case errors.Is(finErr, ErrBotLeaseLost) && got.Status == domain.BotReplyUnknown && got.TerminalReason == "bot_deleted_during_send": + default: + t.Fatalf("round %d: finish=%v status=%s reason=%s", round, finErr, got.Status, got.TerminalReason) + } + s.DB.Where("bot_id = ?", id).Delete(&domain.BotInteraction{}) + s.DB.Where("bot_id = ?", id).Delete(&domain.BotConversation{}) + s.DB.Where("bot_id = ?", bot.LeaseKey()).Delete(&domain.BotConnection{}) + } +} diff --git a/internal/repo/bot_delivery_test.go b/internal/repo/bot_delivery_test.go new file mode 100644 index 000000000..cbd81c6bb --- /dev/null +++ b/internal/repo/bot_delivery_test.go @@ -0,0 +1,346 @@ +package repo + +import ( + "agentflow/internal/domain" + "errors" + "testing" + "time" +) + +const testClockStart = int64(1_800_000_000_000_000) + +type deliveryFixture struct { + t *testing.T + s *Store + bot domain.NotificationBot + lease BotLease + clock func(int64) +} + +func newDeliveryFixture(t *testing.T, channel string) *deliveryFixture { + t.Helper() + s, err := OpenTest() + if err != nil { + t.Fatal(err) + } + t.Cleanup(s.Close) + bot := domain.NotificationBot{ID: "cfg-" + channel, Channel: channel, BotID: "app-" + channel, ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + if err := s.SaveNotificationBot(bot, "secret"); err != nil { + t.Fatal(err) + } + bot, _ = s.NotificationBot(bot.ID) + clock := pinDBClock(t, testClockStart) + lease, ok, err := s.ClaimBotConnection(bot.LeaseKey(), "owner-1") + if err != nil || !ok { + t.Fatal(ok, err) + } + return &deliveryFixture{t: t, s: s, bot: bot, lease: lease, clock: clock} +} + +// advance moves the database clock, renewing the lease every 30s as the +// receiver does. +func (f *deliveryFixture) advance(d time.Duration) { + f.t.Helper() + left := int64(d / time.Microsecond) + for { + step := min(left, int64(30*time.Second/time.Microsecond)) + f.clock(step) + left -= step + if ok, err := f.s.RenewBotConnection(f.lease, "", ""); err != nil || !ok { + f.t.Fatalf("renew while advancing %s: %v %v", d, ok, err) + } + if left == 0 { + return + } + } +} + +// ready enqueues a message and runs it to ready with a saved reply. +func (f *deliveryFixture) ready(msg string) domain.BotInteraction { + f.t.Helper() + item := domain.BotInteraction{ID: BotMessageID(f.bot.IdentityScope(), msg), BotID: f.bot.ID, ProjectID: "p", SenderID: "u", Content: msg} + if _, err := f.s.EnqueueBotInteraction(item, "single", "u"); err != nil { + f.t.Fatal(err) + } + // Older ready items are claimed first; keep claiming until ours runs. + var claimed *domain.BotInteraction + for i := 0; i < 10; i++ { + got, err := f.s.ClaimBotInteraction(f.bot.ID, f.lease, sendPolicyOf(f.bot)) + if err != nil { + f.t.Fatal(err) + } + if got != nil && got.ID == item.ID { + claimed = got + break + } + if got != nil { + // Park the older ready item so the queue moves on. + f.s.DB.Model(&domain.BotInteraction{}).Where("id = ?", got.ID).Update("next_send_at_us", testClockStart+int64(24*time.Hour/time.Microsecond)) + } + } + if claimed == nil || claimed.Status != domain.BotRunning { + f.t.Fatalf("claim = %+v", claimed) + } + if err := f.s.SaveBotReply(*claimed, f.lease, "reply:"+msg, ""); err != nil { + f.t.Fatal(err) + } + saved, _ := f.s.BotInteraction(item.ID) + if saved.Status != domain.BotReady || saved.SendKey == "" || saved.DeliveryGeneration != 1 { + f.t.Fatalf("saved = %+v", saved) + } + return saved +} + +func sendPolicyOf(bot domain.NotificationBot) BotSendPolicy { + return BotSendPolicy{Idempotent: bot.ChannelKind() == domain.BotChannelFeishu} +} + +func (f *deliveryFixture) send(id, outcome string) domain.BotInteraction { + f.t.Helper() + if _, err := f.s.ClaimBotInteraction(f.bot.ID, f.lease, sendPolicyOf(f.bot)); err != nil { + f.t.Fatal(err) + } + sending, err := f.s.BeginBotSend(id, f.lease, sendPolicyOf(f.bot)) + if err != nil { + f.t.Fatalf("begin: %v", err) + } + item, err := f.s.FinishBotSend(id, f.lease, sendPolicyOf(f.bot), BotSendOutcome{Outcome: outcome, Detail: outcome, PlatformMessageID: "pm"}) + if err != nil { + f.t.Fatalf("finish: %v", err) + } + stored, _ := f.s.BotInteraction(id) + if stored.SendKey != sending.SendKey { + f.t.Fatal("send key changed during automatic retries") + } + _ = item + return stored +} + +func TestBotDeliveryUndeliveredRetriesThenFails(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelWeCom) + item := f.ready("m") + waits := []time.Duration{5 * time.Second, 30 * time.Second, 2 * time.Minute} + for i, wait := range waits { + got := f.send(item.ID, "not_delivered") + if got.Status != domain.BotReady || got.UndeliveredRetries != i+1 { + t.Fatalf("retry %d = %+v", i, got) + } + // Not due before the backoff ends. + if claimed, _ := f.s.ClaimBotInteraction(f.bot.ID, f.lease, BotSendPolicy{}); claimed != nil { + t.Fatalf("retry %d claimed before backoff", i) + } + f.advance(wait) + } + got := f.send(item.ID, "not_delivered") + if got.Status != domain.BotReplyFailed || got.TerminalReason != "undelivered_exhausted" || got.SendAttempts != 4 { + t.Fatalf("exhausted = %+v", got) + } +} + +func TestBotDeliveryRejectedAndDelivered(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelWeCom) + rejected := f.send(f.ready("r").ID, "rejected") + if rejected.Status != domain.BotReplyFailed || rejected.TerminalReason != "rejected" { + t.Fatalf("rejected = %+v", rejected) + } + delivered := f.send(f.ready("d").ID, "delivered") + if delivered.Status != domain.BotReplied || delivered.PlatformMessageID != "pm" || delivered.ReceiptSource != "platform" { + t.Fatalf("delivered = %+v", delivered) + } +} + +func TestBotDeliveryUnknownWithoutIdempotencyNeedsReview(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelWeCom) + got := f.send(f.ready("u").ID, "unknown") + if got.Status != domain.BotReplyUnknown || got.TerminalReason != "delivery_unknown" { + t.Fatalf("unknown = %+v", got) + } + if _, err := f.s.ConfirmBotInteraction(got.ID, 9, "confirm-1"); err != nil { + t.Fatal(err) + } + confirmed, _ := f.s.BotInteraction(got.ID) + if confirmed.Status != domain.BotReplied || confirmed.ReceiptSource != "manual" || confirmed.ConfirmedBy != 9 || confirmed.ConfirmedAt == nil { + t.Fatalf("confirmed = %+v", confirmed) + } + if _, err := f.s.ConfirmBotInteraction(got.ID, 9, "confirm-1"); err != nil { + t.Fatalf("repeated confirm click: %v", err) + } + if _, err := f.s.ResendBotInteraction(got.ID, 9, "resend-x", ""); !errors.Is(err, ErrBotStateConflict) { + t.Fatalf("resend of replied = %v", err) + } +} + +// Feishu de-duplicates the same uuid for an hour: unknown outcomes retry with +// the same key until first send + 55 minutes on the database clock, across +// owner changes, then stop at reply_unknown. +func TestBotDeliveryIdempotentWindowOnDatabaseClock(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelFeishu) + item := f.ready("w") + first := f.send(item.ID, "unknown") + if first.Status != domain.BotReady || first.SendStartedAtUS != testClockStart { + t.Fatalf("first unknown = %+v", first) + } + // Owner changes: the old owner stops, a new one recovers after expiry. + f.clock(BotConnectionLeaseUS + 1) + lease, ok, err := f.s.ClaimBotConnection(f.bot.LeaseKey(), "owner-2") + if err != nil || !ok || lease.Epoch != f.lease.Epoch+1 { + t.Fatalf("takeover = %+v %v %v", lease, ok, err) + } + if _, err := f.s.BeginBotSend(item.ID, f.lease, BotSendPolicy{Idempotent: true}); !errors.Is(err, ErrBotLeaseLost) { + t.Fatalf("old owner began a send: %v", err) + } + f.lease = lease + if err := f.s.RecoverBotInteractions(f.bot.ID, lease, BotSendPolicy{Idempotent: true}); err != nil { + t.Fatal(err) + } + // 54:59 after the first send there is no room for a 10s call: closed. + f.advance(54*time.Minute + 59*time.Second - time.Duration(BotConnectionLeaseUS+1)*time.Microsecond) + f.s.DB.Model(&domain.BotInteraction{}).Where("id = ?", item.ID).Update("next_send_at_us", 0) + if _, err := f.s.ClaimBotInteraction(f.bot.ID, lease, BotSendPolicy{Idempotent: true}); err != nil { + t.Fatal(err) + } + if _, err := f.s.BeginBotSend(item.ID, lease, BotSendPolicy{Idempotent: true}); !errors.Is(err, ErrBotStateConflict) { + t.Fatalf("send inside the last call timeout: %v", err) + } + final, _ := f.s.BotInteraction(item.ID) + if final.Status != domain.BotReplyUnknown || final.TerminalReason != "window_expired" || final.SendStartedAtUS != testClockStart { + t.Fatalf("window = %+v", final) + } +} + +func TestBotDeliveryIdempotentRetryInsideWindowKeepsKey(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelFeishu) + item := f.ready("k") + first := f.send(item.ID, "unknown") + f.advance(5 * time.Second) + second := f.send(item.ID, "delivered") + if second.Status != domain.BotReplied || second.SendKey != first.SendKey || second.SendStartedAtUS != testClockStart || second.SendAttempts != 2 { + t.Fatalf("retry = %+v", second) + } +} + +func TestBotDeliveryCrashDuringSendRecoversAfterSendLease(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelWeCom) + item := f.ready("c") + if _, err := f.s.ClaimBotInteraction(f.bot.ID, f.lease, BotSendPolicy{}); err != nil { + t.Fatal(err) + } + if _, err := f.s.BeginBotSend(item.ID, f.lease, BotSendPolicy{}); err != nil { + t.Fatal(err) + } + // Same owner, send lease not over: nothing to resolve. + if claimed, _ := f.s.ClaimBotInteraction(f.bot.ID, f.lease, BotSendPolicy{}); claimed != nil { + t.Fatal("in-flight send claimed") + } + f.advance(time.Duration(BotSendLeaseUS) * time.Microsecond) + if claimed, _ := f.s.ClaimBotInteraction(f.bot.ID, f.lease, BotSendPolicy{}); claimed != nil { + t.Fatal("interrupted send returned as work") + } + got, _ := f.s.BotInteraction(item.ID) + if got.Status != domain.BotReplyUnknown { + t.Fatalf("interrupted send = %+v", got) + } + // The late result of the stopped sender cannot overwrite it. + if _, err := f.s.FinishBotSend(item.ID, f.lease, BotSendPolicy{}, BotSendOutcome{Outcome: "delivered"}); !errors.Is(err, ErrBotLeaseLost) { + t.Fatalf("late receipt = %v", err) + } +} + +func TestBotSendNeedsLeaseHeadroomAndValidRoute(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelWeCom) + item := f.ready("h") + f.clock(BotConnectionLeaseUS - BotSendMinLeaseUS + 1) + if _, err := f.s.BeginBotSend(item.ID, f.lease, BotSendPolicy{}); !errors.Is(err, ErrBotLeaseLow) { + t.Fatalf("low lease = %v", err) + } + f.advance(0) + routed := f.ready("route") + f.s.DB.Model(&domain.BotInteraction{}).Where("id = ?", routed.ID).Update("route_expires_at_us", testClockStart+int64(35*time.Second/time.Microsecond)) + if _, err := f.s.BeginBotSend(routed.ID, f.lease, BotSendPolicy{}); !errors.Is(err, ErrBotStateConflict) { + t.Fatalf("expired route = %v", err) + } + expired, _ := f.s.BotInteraction(routed.ID) + if expired.Status != domain.BotReplyFailed || expired.TerminalReason != "route_expired" { + t.Fatalf("route = %+v", expired) + } + if _, err := f.s.ResendBotInteraction(routed.ID, 1, "k", ""); !errors.Is(err, ErrBotRouteExpired) { + t.Fatalf("resend expired route = %v", err) + } +} + +func TestBotDisableKeepsReadyAndDeleteStopsWork(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelWeCom) + ready := f.ready("ready") + queued := domain.BotInteraction{ID: BotMessageID(f.bot.IdentityScope(), "queued"), BotID: f.bot.ID, SenderID: "u", Content: "q"} + f.s.EnqueueBotInteraction(queued, "single", "u") + sendingItem := f.ready("sending") + f.s.ClaimBotInteraction(f.bot.ID, f.lease, BotSendPolicy{}) + // The oldest ready item is claimed first; begin the send of the third. + f.s.DB.Model(&domain.BotInteraction{}).Where("id = ?", sendingItem.ID).Updates(map[string]any{"lease_owner": f.lease.Owner, "lease_epoch": f.lease.Epoch}) + if _, err := f.s.BeginBotSend(sendingItem.ID, f.lease, BotSendPolicy{}); err != nil { + t.Fatal(err) + } + + disabled := f.bot + disabled.InteractionEnabled = false + if err := f.s.SaveNotificationBot(disabled, ""); err != nil { + t.Fatal(err) + } + status := func(id string) (string, string) { + item, _ := f.s.BotInteraction(id) + return item.Status, item.TerminalReason + } + if st, _ := status(ready.ID); st != domain.BotReady { + t.Fatalf("disable dropped a saved reply: %s", st) + } + if st, reason := status(queued.ID); st != domain.BotCancelled || reason != "bot_disabled" { + t.Fatalf("queued after disable = %s %s", st, reason) + } + if st, reason := status(sendingItem.ID); st != domain.BotReplyUnknown || reason != "bot_disabled_during_send" { + t.Fatalf("sending after disable = %s %s", st, reason) + } + if _, err := f.s.FinishBotSend(sendingItem.ID, f.lease, BotSendPolicy{}, BotSendOutcome{Outcome: "delivered"}); !errors.Is(err, ErrBotLeaseLost) { + t.Fatalf("late receipt after disable = %v", err) + } + + if err := f.s.DeleteNotificationBot(f.bot.ID); err != nil { + t.Fatal(err) + } + if st, reason := status(ready.ID); st != domain.BotCancelled || reason != "bot_deleted" { + t.Fatalf("ready after delete = %s %s", st, reason) + } + if _, err := f.s.ResendBotInteraction(sendingItem.ID, 1, "k2", ""); !errors.Is(err, ErrBotDeleted) { + t.Fatalf("resend after delete = %v", err) + } + if _, err := f.s.ConfirmBotInteraction(sendingItem.ID, 1, "c2"); err != nil { + t.Fatalf("confirm after delete = %v", err) + } + kept, _ := f.s.BotInteraction(ready.ID) + if kept.ProjectID != "p" || kept.Reply == "" { + t.Fatalf("record snapshot lost = %+v", kept) + } +} + +func TestBotDeleteMarksInFlightNotificationsUnknown(t *testing.T) { + f := newDeliveryFixture(t, domain.BotChannelWeCom) + now := time.Now().UTC() + for _, d := range []domain.NotificationDelivery{ + {ID: "d-sending", BotID: f.bot.ID, ProjectID: "p", Status: "sending", CreatedAt: now, NextAttemptAt: now}, + {ID: "d-pending", BotID: f.bot.ID, ProjectID: "p", Status: "pending", CreatedAt: now, NextAttemptAt: now}, + {ID: "d-sent", BotID: f.bot.ID, ProjectID: "p", Status: "sent", CreatedAt: now, NextAttemptAt: now}, + } { + if err := f.s.DB.Create(&d).Error; err != nil { + t.Fatal(err) + } + } + if err := f.s.DeleteNotificationBot(f.bot.ID); err != nil { + t.Fatal(err) + } + want := map[string]string{"d-sending": "send_unknown", "d-pending": "cancelled", "d-sent": "sent"} + for id, status := range want { + d, _ := f.s.NotificationDelivery(id) + if d.Status != status { + t.Fatalf("%s = %s, want %s", id, d.Status, status) + } + } +} diff --git a/internal/repo/bot_identities.go b/internal/repo/bot_identities.go index 6e7d7c1fc..d70357323 100644 --- a/internal/repo/bot_identities.go +++ b/internal/repo/bot_identities.go @@ -14,7 +14,31 @@ import ( "time" ) -var ErrBotBindingChallenge = errors.New("绑定请求已失效,请回到企业微信重新发送消息获取链接") +var ErrBotBindingChallenge = errors.New("绑定请求已失效,请回到聊天中重新发送消息获取链接") + +// ErrBotBindingPending: a valid link was already sent; point to it instead of +// issuing another. ErrBotBindingRateLimited: too many links recently. +var ( + ErrBotBindingPending = errors.New("binding_link_pending") + ErrBotBindingRateLimited = errors.New("binding_link_rate_limited") +) + +// Link issuance limits. +const ( + botBindingGuideWindow = 10 * time.Minute + botBindingGuideMax = 3 + botBindingExplicitEvery = time.Minute +) + +// MaskBotSender shows enough of a platform user ID to recognize it. +func MaskBotSender(sender string) string { + r := []rune(sender) + if len(r) <= 4 { + return "***" + } + return string(r[:2]) + "***" + string(r[len(r)-2:]) +} + var ErrBotBindingCode = errors.New("确认码无效、已过期或不属于当前发送者,请重新确认") func botIdentityID(bot, sender string) string { return BotMessageID(bot, sender) } @@ -38,17 +62,24 @@ func (r *UsersRepo) botBindingRow(tx *gorm.DB, id string) (domain.BotUserBinding err := r.locking(tx).First(&row, "id = ?", id).Error return row, err } -func (r *UsersRepo) NewBotBindingChallenge(bot, sender string, now time.Time) (string, error) { - if bot == "" || sender == "" { + +// NewBotBindingChallenge issues a binding link for an unbound sender of bot. +// explicit is true when the sender asked for a new link. While a valid link +// exists, implicit requests get ErrBotBindingPending; explicit ones replace +// it at most once a minute. At most three links are issued per ten minutes. +// Counters are updated under the binding row lock. +func (r *UsersRepo) NewBotBindingChallenge(bot domain.NotificationBot, sender string, now time.Time, explicit bool) (string, error) { + scope := bot.IdentityScope() + if scope == "" || sender == "" { return "", ErrBotBindingChallenge } token, err := botBindingNonce(32) if err != nil { return "", err } - id := botIdentityID(bot, sender) + id := botIdentityID(scope, sender) err = r.DB.Transaction(func(tx *gorm.DB) error { - row := domain.BotUserBinding{ID: id, BotID: bot, SenderID: sender, CreatedAt: now, UpdatedAt: now} + row := domain.BotUserBinding{ID: id, BotID: scope, SenderID: sender, ConfigID: bot.ID, Channel: bot.ChannelKind(), CreatedAt: now, UpdatedAt: now} if err := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&row).Error; err != nil { return err } @@ -57,15 +88,46 @@ func (r *UsersRepo) NewBotBindingChallenge(bot, sender string, now time.Time) (s return err } if existing.UserID > 0 { - return errors.New("当前企业微信账号已绑定,请在个人配置解除后重新绑定") + return errors.New("当前聊天账号已绑定,请在个人配置解除后重新绑定") + } + var active int64 + if err := tx.Model(&domain.BotBindingChallenge{}).Where("binding_id = ? AND status IN ? AND expires_at > ?", id, []string{"pending", "claimed"}, now).Count(&active).Error; err != nil { + return err + } + if active > 0 && !explicit { + return ErrBotBindingPending + } + if explicit && existing.LastLinkAt != nil && now.Sub(*existing.LastLinkAt) < botBindingExplicitEvery { + return ErrBotBindingRateLimited + } + start, count := existing.GuideWindowStart, existing.GuideCount + if start == nil || now.Sub(*start) >= botBindingGuideWindow { + start, count = &now, 0 + } + if count >= botBindingGuideMax { + return ErrBotBindingRateLimited + } + if err := tx.Model(&domain.BotUserBinding{}).Where("id = ?", id).Updates(map[string]any{"guide_window_start": start, "guide_count": count + 1, "last_link_at": now, "config_id": bot.ID, "channel": bot.ChannelKind(), "updated_at": now}).Error; err != nil { + return err } if err := tx.Model(&domain.BotBindingChallenge{}).Where("binding_id = ? AND status IN ?", id, []string{"pending", "claimed"}).Update("status", "expired").Error; err != nil { return err } - return tx.Create(&domain.BotBindingChallenge{ID: hexSHA256(token), BindingID: id, Status: "pending", ExpiresAt: now.Add(15 * time.Minute), CreatedAt: now}).Error + return tx.Create(&domain.BotBindingChallenge{ID: hexSHA256(token), BindingID: id, ConfigID: bot.ID, Channel: bot.ChannelKind(), SenderMask: MaskBotSender(sender), Status: "pending", ExpiresAt: now.Add(15 * time.Minute), CreatedAt: now}).Error }) return token, err } + +// BotBindingChallengeInfo returns the owning configuration, channel and +// masked sender of a link for the confirmation page. +func (r *UsersRepo) BotBindingChallengeInfo(token string) (configID, channel, senderMask string, err error) { + var out domain.BotBindingChallenge + if err := r.DB.First(&out, "id = ?", hexSHA256(token)).Error; err != nil { + return "", "", "", ErrBotBindingChallenge + } + return out.ConfigID, out.Channel, out.SenderMask, nil +} + func (r *UsersRepo) BotBindingChallengeStatus(token string, userID int64, now time.Time) (string, error) { var out domain.BotBindingChallenge if err := r.DB.First(&out, "id = ?", hexSHA256(token)).Error; err != nil { @@ -102,7 +164,7 @@ func (r *UsersRepo) ClaimBotBindingChallenge(token string, userID int64, now tim return ErrBotBindingChallenge } if challenge.UserID != 0 && challenge.UserID != userID { - return errors.New("链接已由其他平台账号确认,请回企业微信重新获取绑定链接") + return errors.New("链接已由其他平台账号确认,请回到聊天中重新获取绑定链接") } if challenge.Status == "claimed" { return nil @@ -111,12 +173,16 @@ func (r *UsersRepo) ClaimBotBindingChallenge(token string, userID int64, now tim }) return strings.ToUpper(code), err } -func (r *UsersRepo) ConfirmBotUserBinding(bot, sender, code string, now time.Time) (domain.BotUserBinding, error) { + +// ConfirmBotUserBinding completes a binding from the trusted event of the +// original sender. The claimed challenge must belong to this configuration +// (legacy WeCom challenges without a configuration are accepted by scope). +func (r *UsersRepo) ConfirmBotUserBinding(bot domain.NotificationBot, sender, code string, now time.Time) (domain.BotUserBinding, error) { var binding domain.BotUserBinding denied := false err := r.DB.Transaction(func(tx *gorm.DB) error { var err error - binding, err = r.botBindingRow(tx, botIdentityID(bot, sender)) + binding, err = r.botBindingRow(tx, botIdentityID(bot.IdentityScope(), sender)) if err != nil { return ErrBotBindingCode } @@ -127,7 +193,7 @@ func (r *UsersRepo) ConfirmBotUserBinding(bot, sender, code string, now time.Tim if err = tx.Where("binding_id = ? AND status = ?", binding.ID, "claimed").Order("created_at DESC").First(&challenge).Error; err != nil { return ErrBotBindingCode } - if !challenge.ExpiresAt.After(now) || challenge.Attempts >= 5 { + if !challenge.ExpiresAt.After(now) || challenge.Attempts >= 5 || (challenge.ConfigID != "" && challenge.ConfigID != bot.ID) { return ErrBotBindingCode } if subtle.ConstantTimeCompare([]byte(challenge.CodeHash), []byte(hexSHA256(strings.ToLower(strings.TrimSpace(code))))) != 1 { @@ -138,10 +204,10 @@ func (r *UsersRepo) ConfirmBotUserBinding(bot, sender, code string, now time.Tim } return tx.Model(&challenge).Updates(updates).Error } - if err = tx.Model(&binding).Updates(map[string]any{"user_id": challenge.UserID, "updated_at": now}).Error; err != nil { + if err = tx.Model(&binding).Updates(map[string]any{"user_id": challenge.UserID, "config_id": bot.ID, "channel": bot.ChannelKind(), "guide_count": 0, "target_verified_at": nil, "updated_at": now}).Error; err != nil { return err } - binding.UserID = challenge.UserID + binding.UserID, binding.ConfigID, binding.Channel = challenge.UserID, bot.ID, bot.ChannelKind() binding.UpdatedAt = now return tx.Model(&domain.BotBindingChallenge{}).Where("binding_id = ? AND status IN ?", binding.ID, []string{"pending", "claimed"}).Updates(map[string]any{"status": "confirmed", "code_hash": ""}).Error }) @@ -164,7 +230,10 @@ func (r *UsersRepo) RevokeBotUserBinding(id string, userID int64, now time.Time) if row.UserID != userID || userID <= 0 { return gorm.ErrRecordNotFound } - if err = tx.Model(&row).Updates(map[string]any{"user_id": 0, "updated_at": now}).Error; err != nil { + if err = tx.Model(&row).Updates(map[string]any{"user_id": 0, "target_verified_at": nil, "updated_at": now}).Error; err != nil { + return err + } + if err = cancelBotDraftsWhere(tx, "账号绑定已解除", "identity_scope = ? AND sender_id = ?", row.BotID, row.SenderID); err != nil { return err } return tx.Model(&domain.BotBindingChallenge{}).Where("binding_id = ?", id).Updates(map[string]any{"status": "expired", "code_hash": ""}).Error @@ -188,3 +257,34 @@ func (r *UsersRepo) DeleteExpiredBotBindingChallenges(ctx context.Context, now t result := db.Where("id IN ? AND expires_at <= ?", ids, cutoff).Delete(&domain.BotBindingChallenge{}) return result.RowsAffected, result.Error } + +// revokeBotConfigIdentities runs inside DeleteNotificationBot after the +// configuration row is locked. New channels scope identities by the +// configuration ID, so all of them go. A legacy WeCom scope (the external Bot +// ID) is revoked only when no other configuration keeps using it. +func (r *UsersRepo) revokeBotConfigIdentities(tx *gorm.DB, bot domain.NotificationBot) error { + scope := bot.IdentityScope() + if err := cancelBotDraftsWhere(tx, "机器人已删除", "config_id = ?", bot.ID); err != nil { + return err + } + if bot.ChannelKind() == domain.BotChannelWeCom { + var others int64 + if err := tx.Table("notification_bots").Where("id <> ? AND external_app_id = ? AND (channel = ? OR channel = '')", bot.ID, bot.BotID, domain.BotChannelWeCom).Count(&others).Error; err != nil { + return err + } + if others > 0 { + return tx.Model(&domain.BotBindingChallenge{}).Where("config_id = ? AND status IN ?", bot.ID, []string{"pending", "claimed"}).Updates(map[string]any{"status": "expired", "code_hash": ""}).Error + } + } + var ids []string + if err := tx.Model(&domain.BotUserBinding{}).Where("bot_id = ?", scope).Pluck("id", &ids).Error; err != nil { + return err + } + if len(ids) == 0 { + return nil + } + if err := tx.Model(&domain.BotUserBinding{}).Where("id IN ?", ids).Updates(map[string]any{"user_id": 0, "target_verified_at": nil, "updated_at": time.Now().UTC()}).Error; err != nil { + return err + } + return tx.Model(&domain.BotBindingChallenge{}).Where("binding_id IN ?", ids).Updates(map[string]any{"status": "expired", "code_hash": ""}).Error +} diff --git a/internal/repo/bot_identities_test.go b/internal/repo/bot_identities_test.go index aad4a95f8..e6df16816 100644 --- a/internal/repo/bot_identities_test.go +++ b/internal/repo/bot_identities_test.go @@ -17,7 +17,7 @@ func TestBotBindingRequiresBothIdentitiesAndRejectsReplay(t *testing.T) { } defer s.Close() now := time.Now().UTC() - token, err := s.NewBotBindingChallenge("bot", "sender", now) + token, err := s.NewBotBindingChallenge(testWeComBot("bot"), "sender", now, true) if err != nil { t.Fatal(err) } @@ -35,10 +35,10 @@ func TestBotBindingRequiresBothIdentitiesAndRejectsReplay(t *testing.T) { if _, err = s.ClaimBotBindingChallenge(token, 43, now); err == nil { t.Fatal("other account replaced claim") } - if _, err = s.ConfirmBotUserBinding("bot", "other-sender", code, now); err == nil { + if _, err = s.ConfirmBotUserBinding(testWeComBot("bot"), "other-sender", code, now); err == nil { t.Fatal("group participant stole binding") } - if _, err = s.ConfirmBotUserBinding("other-bot", "sender", code, now); err == nil { + if _, err = s.ConfirmBotUserBinding(testWeComBot("other-bot"), "sender", code, now); err == nil { t.Fatal("cross-Bot confirmation") } var challenge domain.BotBindingChallenge @@ -52,7 +52,7 @@ func TestBotBindingRequiresBothIdentitiesAndRejectsReplay(t *testing.T) { wg.Add(1) go func() { defer wg.Done() - _, err := s.ConfirmBotUserBinding("bot", "sender", code, now) + _, err := s.ConfirmBotUserBinding(testWeComBot("bot"), "sender", code, now) success <- err == nil }() } @@ -77,7 +77,7 @@ func TestBotBindingRequiresBothIdentitiesAndRejectsReplay(t *testing.T) { if err = s.RevokeBotUserBinding(binding.ID, 42, now); err != nil { t.Fatal(err) } - if _, err = s.ConfirmBotUserBinding("bot", "sender", code, now); err == nil { + if _, err = s.ConfirmBotUserBinding(testWeComBot("bot"), "sender", code, now); err == nil { t.Fatal("old code rebound after revoke") } if _, err = s.BotBindingChallengeStatus(token, 42, now); err == nil { @@ -91,8 +91,8 @@ func TestBotBindingExpiryAttemptLimitAndLatestLink(t *testing.T) { } defer s.Close() now := time.Now().UTC() - old, _ := s.NewBotBindingChallenge("bot", "sender", now) - token, _ := s.NewBotBindingChallenge("bot", "sender", now.Add(time.Second)) + old, _ := s.NewBotBindingChallenge(testWeComBot("bot"), "sender", now, true) + token, _ := s.NewBotBindingChallenge(testWeComBot("bot"), "sender", now.Add(time.Minute+time.Second), true) if _, err = s.ClaimBotBindingChallenge(old, 42, now); err == nil { t.Fatal("old link remains usable") } @@ -105,16 +105,16 @@ func TestBotBindingExpiryAttemptLimitAndLatestLink(t *testing.T) { if claimErr != nil || again != code { t.Fatal("reopening changed code or reset attempts", claimErr) } - if _, err = s.ConfirmBotUserBinding("bot", "sender", "wrong", now); err == nil { + if _, err = s.ConfirmBotUserBinding(testWeComBot("bot"), "sender", "wrong", now); err == nil { t.Fatal("invalid code accepted") } } - if _, err = s.ConfirmBotUserBinding("bot", "sender", code, now); err == nil { + if _, err = s.ConfirmBotUserBinding(testWeComBot("bot"), "sender", code, now); err == nil { t.Fatal("attempt limit ignored") } - token, _ = s.NewBotBindingChallenge("bot", "sender", now) - code, _ = s.ClaimBotBindingChallenge(token, 42, now) - if _, err = s.ConfirmBotUserBinding("bot", "sender", code, now.Add(16*time.Minute)); err == nil { + token, _ = s.NewBotBindingChallenge(testWeComBot("bot"), "sender", now.Add(3*time.Minute), true) + code, _ = s.ClaimBotBindingChallenge(token, 42, now.Add(3*time.Minute)) + if _, err = s.ConfirmBotUserBinding(testWeComBot("bot"), "sender", code, now.Add(19*time.Minute)); err == nil { t.Fatal("expired code accepted") } } @@ -135,7 +135,7 @@ func TestBotBindingMySQLConcurrentConfirmation(t *testing.T) { id := botIdentityID(bot, sender) defer s.DB.Where("binding_id = ?", id).Delete(&domain.BotBindingChallenge{}) defer s.DB.Where("id = ?", id).Delete(&domain.BotUserBinding{}) - token, err := s.NewBotBindingChallenge(bot, sender, now) + token, err := s.NewBotBindingChallenge(testWeComBot(bot), sender, now, true) if err != nil { t.Fatal(err) } @@ -166,7 +166,7 @@ func TestBotBindingMySQLConcurrentConfirmation(t *testing.T) { wg.Add(1) go func() { defer wg.Done() - _, err := s.ConfirmBotUserBinding(bot, sender, code, now) + _, err := s.ConfirmBotUserBinding(testWeComBot(bot), sender, code, now) results <- err == nil }() } @@ -251,3 +251,9 @@ func TestBotBindingChallengeCleanupRetentionAndIsolation(t *testing.T) { t.Fatal("cancelled cleanup ignored context") } } + +// testWeComBot is a legacy WeCom configuration whose identity scope is the +// external Bot ID. +func testWeComBot(external string) domain.NotificationBot { + return domain.NotificationBot{ID: "cfg-" + external, BotID: external} +} diff --git a/internal/repo/bot_interactions.go b/internal/repo/bot_interactions.go index ae153b505..36ac729be 100644 --- a/internal/repo/bot_interactions.go +++ b/internal/repo/bot_interactions.go @@ -1,6 +1,7 @@ package repo import ( + "agentflow/internal/credcrypt" "agentflow/internal/domain" "crypto/rand" "encoding/hex" @@ -20,40 +21,193 @@ func BotMessageID(botID, msgID string) string { return hexSHA256(botID + "\x00" + msgID) } func newBotSessionID() string { - var raw [16]byte - if _, err := rand.Read(raw[:]); err != nil { + return "bot-" + randomHex(16) +} +func randomHex(n int) string { + raw := make([]byte, n) + if _, err := rand.Read(raw); err != nil { panic(err) } - return "bot-" + hex.EncodeToString(raw[:]) + return hex.EncodeToString(raw) } -func (r *BotInteractionsRepo) ClaimBotConnection(botID, owner string, now time.Time) (bool, error) { - row := domain.BotConnection{BotID: botID, State: "connecting", LeaseUntil: now.Add(-time.Second)} + +// Lease and delivery timing. All comparisons use the database clock. +const ( + BotConnectionLeaseUS = int64(45 * time.Second / time.Microsecond) + // A send starts only when the connection lease has at least this much left. + BotSendMinLeaseUS = int64(15 * time.Second / time.Microsecond) + // One SDK call is bounded by this timeout. + BotSendTimeoutUS = int64(10 * time.Second / time.Microsecond) + // The send lease blocks another owner from recovering an in-flight send. + BotSendLeaseUS = int64(20 * time.Second / time.Microsecond) + // Same-key recovery window for platforms with request de-duplication + // (Feishu uuid: one hour; 55 minutes leaves margin). + BotIdempotentWindowUS = int64(55 * time.Minute / time.Microsecond) + // Retries after an explicit not-delivered outcome. + BotUndeliveredMaxRetries = 3 +) + +var botRetryBackoffUS = []int64{int64(5 * time.Second / time.Microsecond), int64(30 * time.Second / time.Microsecond), int64(2 * time.Minute / time.Microsecond)} + +func botBackoffUS(attempt int) int64 { + if attempt < 0 { + attempt = 0 + } + if attempt >= len(botRetryBackoffUS) { + return botRetryBackoffUS[len(botRetryBackoffUS)-1] + } + return botRetryBackoffUS[attempt] +} + +var ( + // ErrBotLeaseLost: the caller no longer owns the connection lease or the + // interaction, or the configuration stopped receiving. Stop local work. + ErrBotLeaseLost = errors.New("bot_lease_lost") + // ErrBotLeaseLow: the lease is valid but too short to start a send. + ErrBotLeaseLow = errors.New("bot_lease_low") + // ErrBotStateConflict: the interaction is not in the expected state. + ErrBotStateConflict = errors.New("bot_state_conflict") + // ErrBotDeleted / ErrBotRouteExpired: manual resend is impossible. + ErrBotDeleted = errors.New("bot_deleted") + ErrBotRouteExpired = errors.New("route_expired") +) + +// BotLease identifies one ownership period of a receiving connection. +type BotLease struct { + Key string + Owner string + Epoch int64 +} + +// ClaimBotConnection takes the lease when it is free or expired, incrementing +// the epoch for a new owner. The same live owner keeps its epoch. +func (r *BotInteractionsRepo) ClaimBotConnection(key, owner string) (BotLease, bool, error) { + row := domain.BotConnection{BotID: key, State: "connecting", LeaseUntil: time.Now().UTC().Add(-time.Second)} if err := r.DB.Clauses(clause.OnConflict{DoNothing: true}).Create(&row).Error; err != nil { - return false, err + return BotLease{}, false, err } - result := r.DB.Model(&row).Where("bot_id = ? AND (owner = ? OR lease_until < ?)", botID, owner, now).Updates(map[string]any{"owner": owner, "lease_until": now.Add(45 * time.Second), "updated_at": now}) - return result.RowsAffected == 1, result.Error + now := r.dbNowUSSQL() + // Assignment order matters on MySQL (left to right, later terms see new + // values): compute epoch and changes from the previous owner first. + result := r.DB.Exec(`UPDATE bot_connections SET + epoch = CASE WHEN owner = ? AND lease_until_us > `+now+` THEN epoch ELSE epoch + 1 END, + owner_changes = CASE WHEN owner = ? THEN owner_changes ELSE owner_changes + 1 END, + owner = ?, lease_until_us = `+now+` + ?, lease_until = ?, updated_at = ? + WHERE bot_id = ? AND (owner = ? OR lease_until_us <= `+now+`)`, + owner, owner, owner, BotConnectionLeaseUS, time.Now().UTC().Add(45*time.Second), time.Now().UTC(), key, owner) + if result.Error != nil || result.RowsAffected != 1 { + return BotLease{}, false, result.Error + } + var claimed domain.BotConnection + if err := r.DB.First(&claimed, "bot_id = ?", key).Error; err != nil { + return BotLease{}, false, err + } + if claimed.Owner != owner { + return BotLease{}, false, nil + } + return BotLease{Key: key, Owner: owner, Epoch: claimed.Epoch}, true, nil } -func (r *BotInteractionsRepo) RenewBotConnection(botID, owner, state, detail string, now time.Time) (bool, error) { - updates := map[string]any{"lease_until": now.Add(45 * time.Second), "updated_at": now} + +// RenewBotConnection extends a live lease held by the same owner and epoch. +// A non-empty state also records the connection state for display. +func (r *BotInteractionsRepo) RenewBotConnection(lease BotLease, state, detail string) (bool, error) { + updates := map[string]any{"lease_until_us": r.nowUSExpr(BotConnectionLeaseUS), "lease_until": time.Now().UTC().Add(45 * time.Second), "updated_at": time.Now().UTC()} if state != "" { updates["state"] = state updates["last_error"] = detail } - result := r.DB.Model(&domain.BotConnection{}).Where("bot_id = ? AND owner = ? AND lease_until > ?", botID, owner, now).Updates(updates) + result := r.DB.Model(&domain.BotConnection{}).Where("bot_id = ? AND owner = ? AND epoch = ? AND lease_until_us > "+r.dbNowUSSQL(), lease.Key, lease.Owner, lease.Epoch).Updates(updates) return result.RowsAffected == 1, result.Error } -func (r *BotInteractionsRepo) ReleaseBotConnection(botID, owner string) error { - return r.DB.Model(&domain.BotConnection{}).Where("bot_id = ? AND owner = ?", botID, owner).Updates(map[string]any{"owner": "", "lease_until": time.Now().UTC().Add(-time.Second), "state": "offline"}).Error + +func (r *BotInteractionsRepo) ReleaseBotConnection(lease BotLease) error { + return r.DB.Model(&domain.BotConnection{}).Where("bot_id = ? AND owner = ? AND epoch = ?", lease.Key, lease.Owner, lease.Epoch).Updates(map[string]any{"owner": "", "lease_until_us": 0, "lease_until": time.Now().UTC().Add(-time.Second), "state": "offline"}).Error } -func (r *BotInteractionsRepo) BotConnectionState(botID string) (domain.BotConnection, error) { + +// BotConnectionState reports an expired lease as offline. +func (r *BotInteractionsRepo) BotConnectionState(key string) (domain.BotConnection, error) { var out domain.BotConnection - err := r.DB.First(&out, "bot_id = ?", botID).Error - if out.LeaseUntil.Before(time.Now().UTC()) { + if err := r.DB.First(&out, "bot_id = ?", key).Error; err != nil { + return out, err + } + if now, err := r.dbNowUS(r.DB); err == nil && out.LeaseUntilUS <= now { out.State = "offline" } - return out, err + return out, nil +} + +// NoteBotConnectionEvent records the last event time and counts ignored group +// messages for diagnostics. Best effort; it never fences. +func (r *BotInteractionsRepo) NoteBotConnectionEvent(key string, ignoredGroup bool) { + updates := map[string]any{"last_event_at": time.Now().UTC()} + if ignoredGroup { + updates["ignored_group_messages"] = gorm.Expr("ignored_group_messages + 1") + } + r.DB.Model(&domain.BotConnection{}).Where("bot_id = ?", key).Updates(updates) +} + +// botFenceConfig is the part of notification_bots that fences interaction +// writes. It is read here, in the lock order config -> connection -> +// interaction, so this repo does not depend on NotificationsRepo (which +// already depends on it). +type botFenceConfig struct { + ID string + Enabled bool + InteractionEnabled bool + ConfigVersion int64 +} + +func (r *BotInteractionsRepo) lockBotConfig(tx *gorm.DB, configID string) (botFenceConfig, error) { + var cfg botFenceConfig + err := r.locking(tx.Table("notification_bots")).Select("id, enabled, interaction_enabled, config_version").Where("id = ?", configID).Take(&cfg).Error + return cfg, err +} + +func (r *BotInteractionsRepo) lockConnection(tx *gorm.DB, key string) (domain.BotConnection, error) { + var conn domain.BotConnection + err := r.locking(tx).Where("bot_id = ?", key).First(&conn).Error + return conn, err +} + +// fence locks the configuration and the connection (in that order) and +// verifies that the caller still owns a live lease with at least minRemainUS +// left. It returns the database clock read inside the transaction. +func (r *BotInteractionsRepo) fence(tx *gorm.DB, configID string, lease BotLease, minRemainUS int64) (int64, botFenceConfig, error) { + cfg, err := r.lockBotConfig(tx, configID) + if err != nil || !cfg.Enabled || !cfg.InteractionEnabled { + return 0, cfg, ErrBotLeaseLost + } + conn, err := r.lockConnection(tx, lease.Key) + if err != nil { + return 0, cfg, ErrBotLeaseLost + } + now, err := r.dbNowUS(tx) + if err != nil { + return 0, cfg, err + } + if conn.Owner != lease.Owner || conn.Epoch != lease.Epoch || conn.LeaseUntilUS <= now { + return now, cfg, ErrBotLeaseLost + } + if conn.LeaseUntilUS-now < minRemainUS { + return now, cfg, ErrBotLeaseLow + } + return now, cfg, nil } + +// fencedUpdate applies updates to one interaction owned by lease in one of +// the given states. Zero affected rows is a lease or state conflict. +func (r *BotInteractionsRepo) fencedUpdate(tx *gorm.DB, id string, lease BotLease, from []string, updates map[string]any) error { + updates["updated_at"] = time.Now().UTC() + result := tx.Model(&domain.BotInteraction{}).Where("id = ? AND status IN ? AND lease_owner = ? AND lease_epoch = ?", id, from, lease.Owner, lease.Epoch).Updates(updates) + if result.Error != nil { + return result.Error + } + if result.RowsAffected != 1 { + return ErrBotLeaseLost + } + return nil +} + func (r *BotInteractionsRepo) EnqueueBotInteraction(item domain.BotInteraction, kind, chat string) (bool, error) { inserted := false err := r.DB.Transaction(func(tx *gorm.DB) error { @@ -62,7 +216,7 @@ func (r *BotInteractionsRepo) EnqueueBotInteraction(item domain.BotInteraction, return err } item.ConversationID = conversation.ID - item.Status = "queued" + item.Status = domain.BotQueued item.CreatedAt = time.Now().UTC() item.UpdatedAt = item.CreatedAt result := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&item) @@ -77,26 +231,46 @@ func (r *BotInteractionsRepo) EnqueueBotInteraction(item domain.BotInteraction, }) return inserted, err } -func (r *BotInteractionsRepo) ClaimBotInteraction(botID, leaseBotID, owner string) (*domain.BotInteraction, error) { + +// BotSendPolicy describes the channel's delivery guarantees. +type BotSendPolicy struct { + // Idempotent: the platform de-duplicates retries carrying the same key + // within BotIdempotentWindowUS (Feishu uuid). Unknown outcomes are then + // retried with the same key instead of becoming reply_unknown. + Idempotent bool +} + +// ClaimBotInteraction returns the next queued item (now running) or a ready +// item whose next send is due, stamping it with the lease. A send whose send +// lease expired (the sender stopped mid-call) is resolved as an unknown +// outcome first. +func (r *BotInteractionsRepo) ClaimBotInteraction(configID string, lease BotLease, policy BotSendPolicy) (*domain.BotInteraction, error) { var item domain.BotInteraction + resolved := false err := r.DB.Transaction(func(tx *gorm.DB) error { - var lease domain.BotConnection - if err := r.locking(tx).Where("bot_id = ? AND owner = ? AND lease_until > ?", leaseBotID, owner, time.Now().UTC()).First(&lease).Error; err != nil { + now, cfg, err := r.fence(tx, configID, lease, 0) + if err != nil { return err } - if err := r.locking(tx).Where("bot_id = ? AND status IN ?", botID, []string{"queued", "ready"}).Order("created_at, id").First(&item).Error; err != nil { + if err := r.locking(tx).Where("bot_id = ? AND (status = ? OR (status = ? AND next_send_at_us <= ?) OR (status = ? AND send_lease_until_us <= ?))", configID, domain.BotQueued, domain.BotReady, now, domain.BotSending, now).Order("created_at, id").First(&item).Error; err != nil { return err } - if item.Status == "ready" { - return nil + switch item.Status { + case domain.BotSending: + resolved = true + return r.resolveUnknown(tx, &item, now, policy, "发送过程中断,平台未返回结果") + case domain.BotReady: + item.LeaseOwner, item.LeaseEpoch = lease.Owner, lease.Epoch + return tx.Model(&item).Updates(map[string]any{"lease_owner": lease.Owner, "lease_epoch": lease.Epoch, "updated_at": time.Now().UTC()}).Error } var conv domain.BotConversation if err := tx.First(&conv, "id = ?", item.ConversationID).Error; err != nil { return err } item.SessionID = conv.SessionID - item.Status = "running" - return tx.Model(&item).Updates(map[string]any{"session_id": item.SessionID, "status": "running", "updated_at": time.Now().UTC()}).Error + item.Status = domain.BotRunning + item.LeaseOwner, item.LeaseEpoch, item.ConfigVersion = lease.Owner, lease.Epoch, cfg.ConfigVersion + return tx.Model(&item).Updates(map[string]any{"session_id": item.SessionID, "status": domain.BotRunning, "lease_owner": lease.Owner, "lease_epoch": lease.Epoch, "config_version": cfg.ConfigVersion, "updated_at": time.Now().UTC()}).Error }) if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil @@ -104,27 +278,201 @@ func (r *BotInteractionsRepo) ClaimBotInteraction(botID, leaseBotID, owner strin if err != nil { return nil, err } + if resolved { + // Resolved an interrupted send; the caller polls again. + return nil, nil + } return &item, nil } -// A model turn interrupted by process loss must never be replayed automatically: -// its platform tools may already have committed. Its reply can still be retried. -func (r *BotInteractionsRepo) RecoverBotInteractions(botID string) error { - return r.DB.Model(&domain.BotInteraction{}).Where("bot_id = ? AND status = ?", botID, "running").Updates(map[string]any{"status": "ready", "reply": "上一轮处理已中断,执行结果可能已保存。请先查询状态,再决定是否继续操作。", "last_error": "执行中断", "updated_at": time.Now().UTC()}).Error +// resolveUnknown applies the unknown-outcome rule to a sending item: same-key +// retry inside the idempotency window, otherwise reply_unknown. +func (r *BotInteractionsRepo) resolveUnknown(tx *gorm.DB, item *domain.BotInteraction, now int64, policy BotSendPolicy, detail string) error { + updates := map[string]any{"send_lease_until_us": 0, "updated_at": time.Now().UTC()} + if policy.Idempotent && item.SendStartedAtUS > 0 && now+BotSendTimeoutUS < item.SendStartedAtUS+BotIdempotentWindowUS { + retries := item.SendAttempts - item.UndeliveredRetries - 1 + updates["status"], updates["next_send_at_us"] = domain.BotReady, now+botBackoffUS(retries) + updates["last_error"] = truncateBotDetail(detail + ",将使用同一请求标识重试") + } else { + updates["status"], updates["terminal_reason"] = domain.BotReplyUnknown, "delivery_unknown" + updates["last_error"] = truncateBotDetail(detail + ",送达结果待核对") + } + item.Status = updates["status"].(string) + return tx.Model(&domain.BotInteraction{}).Where("id = ? AND status = ?", item.ID, domain.BotSending).Updates(updates).Error +} + +// RecoverBotInteractions runs when a new lease starts. A running model turn +// is never replayed (its platform tools may have committed): it becomes ready +// with an interruption notice. Sends whose send lease ended are resolved as +// unknown outcomes; ready items keep their saved replies. +func (r *BotInteractionsRepo) RecoverBotInteractions(configID string, lease BotLease, policy BotSendPolicy) error { + return r.DB.Transaction(func(tx *gorm.DB) error { + now, _, err := r.fence(tx, configID, lease, 0) + if err != nil { + return err + } + var running []domain.BotInteraction + if err := r.locking(tx).Where("bot_id = ? AND status = ?", configID, domain.BotRunning).Order("id").Find(&running).Error; err != nil { + return err + } + for _, item := range running { + updates := map[string]any{"status": domain.BotReady, "reply": "上一轮处理已中断,执行结果可能已保存。请先查询状态,再决定是否继续操作。", "last_error": "执行中断", "next_send_at_us": 0, "updated_at": time.Now().UTC()} + if item.SendKey == "" { + updates["send_key"], updates["delivery_generation"] = randomHex(16), 1 + } + if err := tx.Model(&domain.BotInteraction{}).Where("id = ? AND status = ?", item.ID, domain.BotRunning).Updates(updates).Error; err != nil { + return err + } + } + var sending []domain.BotInteraction + if err := r.locking(tx).Where("bot_id = ? AND status = ? AND send_lease_until_us <= ?", configID, domain.BotSending, now).Order("id").Find(&sending).Error; err != nil { + return err + } + for i := range sending { + if err := r.resolveUnknown(tx, &sending[i], now, policy, "发送过程中断,平台未返回结果"); err != nil { + return err + } + } + return nil + }) +} + +// SaveBotReply stores the generated reply of a running turn owned by lease +// and fixes the send key of the first delivery generation. +func (r *BotInteractionsRepo) SaveBotReply(item domain.BotInteraction, lease BotLease, reply, detail string) error { + return r.DB.Transaction(func(tx *gorm.DB) error { + if _, _, err := r.fence(tx, item.BotID, lease, 0); err != nil { + return err + } + return r.fencedUpdate(tx, item.ID, lease, []string{domain.BotRunning}, map[string]any{"status": domain.BotReady, "reply": reply, "last_error": detail, "send_key": randomHex(16), "delivery_generation": 1, "next_send_at_us": 0, "send_started_at_us": 0, "send_attempts": 0, "undelivered_retries": 0}) + }) +} + +// BeginBotSend moves a ready item owned by lease to sending. It refuses when +// the lease has less than BotSendMinLeaseUS left, fails an expired reply +// route, and closes the idempotency window before it would be exceeded. +func (r *BotInteractionsRepo) BeginBotSend(id string, lease BotLease, policy BotSendPolicy) (domain.BotInteraction, error) { + var item domain.BotInteraction + err := r.DB.Transaction(func(tx *gorm.DB) error { + if err := r.locking(tx).First(&item, "id = ?", id).Error; err != nil { + return err + } + now, _, err := r.fence(tx, item.BotID, lease, BotSendMinLeaseUS) + if err != nil { + return err + } + if item.Status != domain.BotReady || item.LeaseOwner != lease.Owner || item.LeaseEpoch != lease.Epoch { + return ErrBotLeaseLost + } + if item.RouteExpiresAtUS > 0 && now+BotSendTimeoutUS >= item.RouteExpiresAtUS { + item.Status = domain.BotReplyFailed + return r.fencedUpdate(tx, id, lease, []string{domain.BotReady}, map[string]any{"status": domain.BotReplyFailed, "terminal_reason": "route_expired", "last_error": "会话回复授权已过期,结果可在记录页查看", "reply_route": clearedRoute(item.ReplyRoute)}) + } + if policy.Idempotent && item.SendStartedAtUS > 0 && now+BotSendTimeoutUS >= item.SendStartedAtUS+BotIdempotentWindowUS { + item.Status = domain.BotReplyUnknown + return r.fencedUpdate(tx, id, lease, []string{domain.BotReady}, map[string]any{"status": domain.BotReplyUnknown, "terminal_reason": "window_expired", "last_error": "已超过平台去重窗口,送达结果待核对"}) + } + if item.SendKey == "" { + item.SendKey = randomHex(16) + } + if item.DeliveryGeneration == 0 { + item.DeliveryGeneration = 1 + } + if item.SendStartedAtUS == 0 { + item.SendStartedAtUS = now + } + item.SendAttempts++ + item.Status = domain.BotSending + item.SendLeaseUntilUS = now + BotSendLeaseUS + return r.fencedUpdate(tx, id, lease, []string{domain.BotReady}, map[string]any{"status": domain.BotSending, "send_key": item.SendKey, "delivery_generation": item.DeliveryGeneration, "send_started_at_us": item.SendStartedAtUS, "send_attempts": item.SendAttempts, "send_lease_until_us": item.SendLeaseUntilUS}) + }) + if err != nil { + return item, err + } + if item.Status != domain.BotSending { + return item, ErrBotStateConflict + } + return item, nil +} + +// BotSendOutcome is the classified result of one send attempt. +type BotSendOutcome struct { + Outcome string // delivered, not_delivered, rejected, unknown + PlatformMessageID string + Code string + Detail string +} + +// FinishBotSend records a send result on a sending item owned by lease: +// delivered -> replied; rejected -> reply_failed; not delivered -> retry by +// count (reply_failed when exhausted); unknown -> same-key retry inside the +// idempotency window for idempotent channels, else reply_unknown. +func (r *BotInteractionsRepo) FinishBotSend(id string, lease BotLease, policy BotSendPolicy, out BotSendOutcome) (domain.BotInteraction, error) { + var item domain.BotInteraction + err := r.DB.Transaction(func(tx *gorm.DB) error { + if err := r.locking(tx).First(&item, "id = ?", id).Error; err != nil { + return err + } + now, _, err := r.fence(tx, item.BotID, lease, 0) + if err != nil { + return err + } + if item.Status != domain.BotSending || item.LeaseOwner != lease.Owner || item.LeaseEpoch != lease.Epoch { + return ErrBotLeaseLost + } + updates := map[string]any{"send_lease_until_us": 0} + switch out.Outcome { + case "delivered": + updates["status"], updates["platform_message_id"], updates["receipt_source"] = domain.BotReplied, out.PlatformMessageID, "platform" + updates["terminal_reason"], updates["reply_route"] = "", clearedRoute(item.ReplyRoute) + case "rejected": + updates["status"], updates["terminal_reason"], updates["last_error"] = domain.BotReplyFailed, "rejected", truncateBotDetail(out.Detail) + case "not_delivered": + if item.UndeliveredRetries < BotUndeliveredMaxRetries { + updates["status"], updates["next_send_at_us"] = domain.BotReady, now+botBackoffUS(item.UndeliveredRetries) + updates["undelivered_retries"], updates["last_error"] = item.UndeliveredRetries+1, truncateBotDetail(out.Detail) + } else { + updates["status"], updates["terminal_reason"], updates["last_error"] = domain.BotReplyFailed, "undelivered_exhausted", truncateBotDetail("多次发送未成功:"+out.Detail) + } + default: + item.Status = domain.BotSending + return r.resolveUnknown(tx, &item, now, policy, out.Detail) + } + item.Status = updates["status"].(string) + return r.fencedUpdate(tx, id, lease, []string{domain.BotSending}, updates) + }) + return item, err } -func (r *BotInteractionsRepo) SaveBotReply(item domain.BotInteraction, reply, detail string) error { - return r.DB.Model(&domain.BotInteraction{}).Where("id = ? AND status = ?", item.ID, "running").Updates(map[string]any{"status": "ready", "reply": reply, "last_error": detail, "updated_at": time.Now().UTC()}).Error + +// clearedRoute drops an authorization-bearing (sealed) route once it can no +// longer be used; plain routing data such as a Feishu message ID is kept. +func clearedRoute(route string) string { + if credcrypt.IsSealed(route) { + return "" + } + return route } -func (r *BotInteractionsRepo) MarkBotReplySent(id string) error { - return r.DB.Model(&domain.BotInteraction{}).Where("id = ? AND status = ?", id, "ready").Updates(map[string]any{"status": "replied", "updated_at": time.Now().UTC()}).Error + +func truncateBotDetail(s string) string { + runes := []rune(s) + if len(runes) > 120 { + return string(runes[:120]) + } + return s } -func (r *BotInteractionsRepo) NewBotConversation(item domain.BotInteraction) error { + +// NewBotConversation rotates the conversation session for a /new command +// owned by lease. +func (r *BotInteractionsRepo) NewBotConversation(item domain.BotInteraction, lease BotLease) error { return r.DB.Transaction(func(tx *gorm.DB) error { + if _, _, err := r.fence(tx, item.BotID, lease, 0); err != nil { + return err + } session := newBotSessionID() - if err := tx.Model(&domain.BotConversation{}).Where("id = ?", item.ConversationID).Updates(map[string]any{"session_id": session, "updated_at": time.Now().UTC()}).Error; err != nil { + if err := r.fencedUpdate(tx, item.ID, lease, []string{domain.BotRunning}, map[string]any{"session_id": session, "action": "new"}); err != nil { return err } - return tx.Model(&domain.BotInteraction{}).Where("id = ?", item.ID).Updates(map[string]any{"session_id": session, "action": "new"}).Error + return tx.Model(&domain.BotConversation{}).Where("id = ?", item.ConversationID).Updates(map[string]any{"session_id": session, "updated_at": time.Now().UTC()}).Error }) } func (r *BotInteractionsRepo) BotConversation(id string) (domain.BotConversation, error) { @@ -134,7 +482,7 @@ func (r *BotInteractionsRepo) BotConversation(id string) (domain.BotConversation } func (r *BotInteractionsRepo) BotHistory(sessionID string) ([]domain.BotInteraction, error) { var rows []domain.BotInteraction - err := r.DB.Where("session_id = ? AND status = ? AND action = ?", sessionID, "replied", "").Order("created_at desc, id desc").Limit(20).Find(&rows).Error + err := r.DB.Where("session_id = ? AND status = ? AND action = ?", sessionID, domain.BotReplied, "").Order("created_at desc, id desc").Limit(20).Find(&rows).Error for i, j := 0, len(rows)-1; i < j; i, j = i+1, j-1 { rows[i], rows[j] = rows[j], rows[i] } @@ -145,6 +493,14 @@ func (r *BotInteractionsRepo) BotInteractionRecords(botID string) ([]domain.BotI err := r.DB.Where("bot_id = ?", botID).Order("created_at desc, id desc").Limit(50).Find(&rows).Error return rows, err } + +// BotInteractionsAwaitingReview lists a project's interactions that need a +// manual decision, including records of deleted Bots (project snapshot). +func (r *BotInteractionsRepo) BotInteractionsAwaitingReview(projectID string) ([]domain.BotInteraction, error) { + rows := []domain.BotInteraction{} + err := r.DB.Where("project_id = ? AND status IN ?", projectID, []string{domain.BotReplyUnknown, domain.BotReplyFailed}).Order("updated_at desc, id desc").Limit(50).Find(&rows).Error + return rows, err +} func (r *BotInteractionsRepo) BotConversations(botID string) ([]domain.BotConversation, error) { var rows []domain.BotConversation err := r.DB.Where("bot_id = ?", botID).Order("updated_at desc").Limit(50).Find(&rows).Error @@ -157,36 +513,229 @@ func (r *BotInteractionsRepo) BotInteraction(id string) (domain.BotInteraction, return item, err } +// ExpireBotCard closes a notification card whose node no longer waits. func (r *BotInteractionsRepo) ExpireBotCard(id string) error { - return r.DB.Model(&domain.BotInteraction{}).Where("id = ? AND action = ? AND status <> ?", id, "notification", "replied").Updates(map[string]any{"status": "replied", "last_error": "该节点已不再等待本次操作", "updated_at": time.Now().UTC()}).Error + return r.DB.Model(&domain.BotInteraction{}).Where("id = ? AND action = ? AND status IN ?", id, "notification", []string{domain.BotQueued, domain.BotRunning, domain.BotReady}).Updates(map[string]any{"status": domain.BotReplied, "last_error": "该节点已不再等待本次操作", "updated_at": time.Now().UTC()}).Error } -func (r *BotInteractionsRepo) FailBotReply(id, detail string) error { - return r.DB.Model(&domain.BotInteraction{}).Where("id = ? AND status = ?", id, "ready").Updates(map[string]any{"status": "reply_failed", "last_error": detail, "updated_at": time.Now().UTC()}).Error +// FailBotReply ends a ready item owned by lease without sending (for example +// a capability the channel lacks). +func (r *BotInteractionsRepo) FailBotReply(id string, lease BotLease, detail string) error { + return r.DB.Transaction(func(tx *gorm.DB) error { + var item domain.BotInteraction + if err := tx.First(&item, "id = ?", id).Error; err != nil { + return err + } + if _, _, err := r.fence(tx, item.BotID, lease, 0); err != nil { + return err + } + return r.fencedUpdate(tx, id, lease, []string{domain.BotReady}, map[string]any{"status": domain.BotReplyFailed, "terminal_reason": "unsupported", "last_error": truncateBotDetail(detail)}) + }) } + +// RequeueBotCard re-sends a failed notification card as a new generation. func (r *BotInteractionsRepo) RequeueBotCard(id string) error { - return r.DB.Model(&domain.BotInteraction{}).Where("id = ? AND action = ? AND status = ?", id, "notification", "reply_failed").Updates(map[string]any{"status": "ready", "last_error": "", "updated_at": time.Now().UTC()}).Error + return r.DB.Model(&domain.BotInteraction{}).Where("id = ? AND action = ? AND status = ?", id, "notification", domain.BotReplyFailed).Updates(map[string]any{"status": domain.BotReady, "last_error": "", "terminal_reason": "", "send_key": randomHex(16), "delivery_generation": gorm.Expr("delivery_generation + 1"), "send_started_at_us": 0, "send_attempts": 0, "undelivered_retries": 0, "next_send_at_us": 0, "updated_at": time.Now().UTC()}).Error } // Serializes registering the same external Bot across project configs. The // receiver later claims this same connection row, avoiding duplicate sockets. -func (r *BotInteractionsRepo) lockBotDefinition(tx *gorm.DB, botID string) error { - row := domain.BotConnection{BotID: botID, LeaseUntil: time.Now().UTC().Add(-time.Second), State: "offline"} +func (r *BotInteractionsRepo) lockBotDefinition(tx *gorm.DB, key string) error { + row := domain.BotConnection{BotID: key, LeaseUntil: time.Now().UTC().Add(-time.Second), State: "offline"} if err := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&row).Error; err != nil { return err } - return r.locking(tx).Where("bot_id = ?", botID).First(&row).Error + return r.locking(tx).Where("bot_id = ?", key).First(&row).Error } -// Store the exact opaque task ID before wire send so callbacks cannot route by -// a guessed delivery prefix or a stale card from an earlier send attempt. -func (r *BotInteractionsRepo) SetBotCardTaskID(id, taskID string) error { - result := r.DB.Model(&domain.BotInteraction{}).Where("id = ? AND action = ? AND status = ?", id, "notification", "ready").Update("card_task_id", taskID) - if result.Error != nil { - return result.Error +// SetBotCardTaskID stores the exact opaque task ID before wire send so +// callbacks cannot route by a guessed delivery prefix or a stale card. +func (r *BotInteractionsRepo) SetBotCardTaskID(id string, lease BotLease, taskID string) error { + return r.DB.Transaction(func(tx *gorm.DB) error { + var item domain.BotInteraction + if err := tx.First(&item, "id = ?", id).Error; err != nil { + return err + } + if _, _, err := r.fence(tx, item.BotID, lease, 0); err != nil { + return err + } + return r.fencedUpdate(tx, id, lease, []string{domain.BotReady}, map[string]any{"card_task_id": taskID}) + }) +} + +// ResendBotInteraction is the manual "send again" for reply_unknown or +// reply_failed: a new delivery generation with a new key, sending only the +// saved reply. The same actionKey returns the current state without a +// second generation. Lock order: configuration, then the interaction. +func (r *BotInteractionsRepo) ResendBotInteraction(id string, actor int64, actionKey, reason string) (domain.BotInteraction, error) { + var item domain.BotInteraction + err := r.DB.Transaction(func(tx *gorm.DB) error { + if err := tx.First(&item, "id = ?", id).Error; err != nil { + return err + } + cfg, cfgErr := r.lockBotConfig(tx, item.BotID) + if err := r.locking(tx).First(&item, "id = ?", id).Error; err != nil { + return err + } + if actionKey != "" && item.LastActionKey == actionKey { + return nil + } + if errors.Is(cfgErr, gorm.ErrRecordNotFound) { + return ErrBotDeleted + } + if cfgErr != nil { + return cfgErr + } + if !cfg.Enabled || !cfg.InteractionEnabled { + return ErrBotStateConflict + } + if item.Status != domain.BotReplyUnknown && item.Status != domain.BotReplyFailed { + return ErrBotStateConflict + } + if item.BodyPurgedAt != nil { + // The saved reply was removed by the retention cleanup. + return ErrBotStateConflict + } + now, err := r.dbNowUS(tx) + if err != nil { + return err + } + if item.TerminalReason == "route_expired" || (item.RouteExpiresAtUS > 0 && now+BotSendTimeoutUS >= item.RouteExpiresAtUS) { + return ErrBotRouteExpired + } + item.Status = domain.BotReady + item.DeliveryGeneration++ + item.SendKey = randomHex(16) + result := tx.Model(&domain.BotInteraction{}).Where("id = ? AND status IN ?", id, []string{domain.BotReplyUnknown, domain.BotReplyFailed}).Updates(map[string]any{"status": domain.BotReady, "delivery_generation": item.DeliveryGeneration, "send_key": item.SendKey, "send_started_at_us": 0, "send_attempts": 0, "undelivered_retries": 0, "next_send_at_us": 0, "send_lease_until_us": 0, "lease_owner": "", "lease_epoch": 0, "terminal_reason": "", "resent_by": actor, "last_action_key": actionKey, "last_error": truncateBotDetail("人工再次发送:" + reason), "updated_at": time.Now().UTC()}) + if result.Error != nil { + return result.Error + } + if result.RowsAffected != 1 { + return ErrBotStateConflict + } + return nil + }) + return item, err +} + +// ConfirmBotInteraction records that the recipient did receive a +// reply_unknown message. It works after the Bot is deleted as well. +func (r *BotInteractionsRepo) ConfirmBotInteraction(id string, actor int64, actionKey string) (domain.BotInteraction, error) { + var item domain.BotInteraction + err := r.DB.Transaction(func(tx *gorm.DB) error { + if err := tx.First(&item, "id = ?", id).Error; err != nil { + return err + } + if _, err := r.lockBotConfig(tx, item.BotID); err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + return err + } + if err := r.locking(tx).First(&item, "id = ?", id).Error; err != nil { + return err + } + if actionKey != "" && item.LastActionKey == actionKey { + return nil + } + if item.Status != domain.BotReplyUnknown { + return ErrBotStateConflict + } + at := time.Now().UTC() + item.Status, item.ConfirmedBy, item.ConfirmedAt, item.ReceiptSource = domain.BotReplied, actor, &at, "manual" + return tx.Model(&domain.BotInteraction{}).Where("id = ? AND status = ?", id, domain.BotReplyUnknown).Updates(map[string]any{"status": domain.BotReplied, "confirmed_by": actor, "confirmed_at": at, "receipt_source": "manual", "last_action_key": actionKey, "updated_at": at}).Error + }) + return item, err +} + +// stopBotInteractions applies disable or delete semantics inside the caller's +// transaction, after it locked the configuration: queued and running items +// are cancelled; sending items become reply_unknown; ready items are kept on +// disable and cancelled on delete. The connection row is locked between the +// configuration and the interactions to keep the global lock order. +func (r *BotInteractionsRepo) stopBotInteractions(tx *gorm.DB, configID, leaseKey string, deleted bool) error { + if leaseKey != "" { + if _, err := r.lockConnection(tx, leaseKey); err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + return err + } } - if result.RowsAffected != 1 { - return gorm.ErrRecordNotFound + reason, sendingReason := "bot_disabled", "bot_disabled_during_send" + cancel := []string{domain.BotQueued, domain.BotRunning} + if deleted { + reason, sendingReason = "bot_deleted", "bot_deleted_during_send" + cancel = append(cancel, domain.BotReady) } - return nil + now := time.Now().UTC() + cancelled := map[string]any{"status": domain.BotCancelled, "terminal_reason": reason, "lease_owner": "", "send_lease_until_us": 0, "updated_at": now} + if deleted { + cancelled["reply_route"] = "" + } + if err := tx.Model(&domain.BotInteraction{}).Where("bot_id = ? AND status IN ?", configID, cancel).Updates(cancelled).Error; err != nil { + return err + } + updates := map[string]any{"status": domain.BotReplyUnknown, "terminal_reason": sendingReason, "last_error": "发送过程中机器人被停用或删除,送达结果待核对", "lease_owner": "", "send_lease_until_us": 0, "updated_at": now} + if deleted { + updates["reply_route"] = "" + } + return tx.Model(&domain.BotInteraction{}).Where("bot_id = ? AND status = ?", configID, domain.BotSending).Updates(updates).Error +} + +// ErrBotPushTargetUnverified: the original sender is not a verified +// proactive target of this configuration. +var ErrBotPushTargetUnverified = errors.New("push_target_unverified") + +// BotSendRoutePush marks replies delivered as proactive private messages. +const BotSendRoutePush = "push" + +// PushResendBotInteraction switches a DingTalk reply whose session webhook +// expired to a proactive private message to the original sender. Under the +// configuration and interaction locks it re-checks that the reply failed +// for route_expired and that the sender's binding still belongs to +// expectedUser on this configuration with a successful trial send. The +// saved reply is sent again as a new generation with a new key; the action +// key makes a repeated click return the same result. +func (r *BotInteractionsRepo) PushResendBotInteraction(id string, actor, expectedUser int64, actionKey, reason string) (domain.BotInteraction, error) { + var item domain.BotInteraction + err := r.DB.Transaction(func(tx *gorm.DB) error { + if err := tx.First(&item, "id = ?", id).Error; err != nil { + return err + } + cfg, cfgErr := r.lockBotConfig(tx, item.BotID) + if err := r.locking(tx).First(&item, "id = ?", id).Error; err != nil { + return err + } + if actionKey != "" && item.LastActionKey == actionKey { + return nil + } + if errors.Is(cfgErr, gorm.ErrRecordNotFound) { + return ErrBotDeleted + } + if cfgErr != nil { + return cfgErr + } + if !cfg.Enabled || !cfg.InteractionEnabled { + return ErrBotStateConflict + } + if item.Status != domain.BotReplyFailed || item.TerminalReason != "route_expired" || item.BodyPurgedAt != nil { + return ErrBotStateConflict + } + var binding domain.BotUserBinding + err := r.locking(tx).Where("id = ? AND config_id = ? AND user_id = ? AND user_id > 0 AND target_verified_at IS NOT NULL", botIdentityID(item.BotID, item.SenderID), item.BotID, expectedUser).First(&binding).Error + if errors.Is(err, gorm.ErrRecordNotFound) { + return ErrBotPushTargetUnverified + } + if err != nil { + return err + } + item.Status = domain.BotReady + item.DeliveryGeneration++ + item.SendKey = randomHex(16) + item.SendRoute = BotSendRoutePush + result := tx.Model(&domain.BotInteraction{}).Where("id = ? AND status = ?", id, domain.BotReplyFailed).Updates(map[string]any{"status": domain.BotReady, "delivery_generation": item.DeliveryGeneration, "send_key": item.SendKey, "send_route": BotSendRoutePush, "reply_route": "", "route_expires_at_us": 0, "send_started_at_us": 0, "send_attempts": 0, "undelivered_retries": 0, "next_send_at_us": 0, "send_lease_until_us": 0, "lease_owner": "", "lease_epoch": 0, "terminal_reason": "", "resent_by": actor, "last_action_key": actionKey, "last_error": truncateBotDetail("人工改用主动单聊发送:" + reason), "updated_at": time.Now().UTC()}) + if result.Error != nil { + return result.Error + } + if result.RowsAffected != 1 { + return ErrBotStateConflict + } + return nil + }) + return item, err } diff --git a/internal/repo/bot_interactions_test.go b/internal/repo/bot_interactions_test.go index 0f8e7487f..af0785735 100644 --- a/internal/repo/bot_interactions_test.go +++ b/internal/repo/bot_interactions_test.go @@ -16,17 +16,24 @@ func TestBotConnectionLeaseAndInterruptedTurn(t *testing.T) { t.Fatal(err) } defer store.Close() - now := time.Now().UTC() - if ok, err := store.ClaimBotConnection("external", "owner-a", now); err != nil || !ok { + bot := domain.NotificationBot{ID: "b", BotID: "external", ProjectID: "p", OwnerID: 1, Enabled: true, InteractionEnabled: true} + if err := store.SaveNotificationBot(bot, "s"); err != nil { + t.Fatal(err) + } + clock := pinDBClock(t, 1_000_000_000_000) + a, ok, err := store.ClaimBotConnection("external", "owner-a") + if err != nil || !ok { t.Fatal(ok, err) } - if ok, _ := store.ClaimBotConnection("external", "owner-b", now); ok { + if _, ok, _ := store.ClaimBotConnection("external", "owner-b"); ok { t.Fatal("two connections claimed") } - if ok, _ := store.ClaimBotConnection("external", "owner-b", now.Add(time.Minute)); !ok { - t.Fatal("expired lease unavailable") + clock(BotConnectionLeaseUS + 1) + b, ok, _ := store.ClaimBotConnection("external", "owner-b") + if !ok || b.Epoch != a.Epoch+1 { + t.Fatalf("expired lease unavailable or epoch kept: %+v %+v", a, b) } - if ok, _ := store.RenewBotConnection("external", "owner-a", "connected", "", now.Add(time.Minute)); ok { + if ok, _ := store.RenewBotConnection(a, "connected", ""); ok { t.Fatal("stale owner renewed lease") } item := domain.BotInteraction{ID: BotMessageID("b", "m1"), BotID: "b", SenderID: "u", Content: "重试"} @@ -36,19 +43,28 @@ func TestBotConnectionLeaseAndInterruptedTurn(t *testing.T) { if ok, _ := store.EnqueueBotInteraction(item, "group", "g"); ok { t.Fatal("duplicate event") } - claimed, err := store.ClaimBotInteraction("b", "external", "owner-b") - if err != nil || claimed == nil { - t.Fatal(err) + if _, err := store.ClaimBotInteraction("b", a, BotSendPolicy{}); err != ErrBotLeaseLost { + t.Fatalf("old owner claimed work: %v", err) + } + claimed, err := store.ClaimBotInteraction("b", b, BotSendPolicy{}) + if err != nil || claimed == nil || claimed.Status != "running" || claimed.LeaseEpoch != b.Epoch { + t.Fatal(claimed, err) } - if err = store.RecoverBotInteractions("b"); err != nil { + // owner-b stops renewing; owner-c takes over and recovers the turn. + clock(BotConnectionLeaseUS + 1) + c, ok, _ := store.ClaimBotConnection("external", "owner-c") + if !ok { + t.Fatal("takeover failed") + } + if err = store.RecoverBotInteractions("b", c, BotSendPolicy{}); err != nil { t.Fatal(err) } - recovered, _ := store.ClaimBotInteraction("b", "external", "owner-b") + recovered, _ := store.ClaimBotInteraction("b", c, BotSendPolicy{}) if recovered == nil || recovered.Status != "ready" || recovered.Reply == "" { t.Fatal("interrupted turn was replayed") } - if err = store.SaveBotReply(*claimed, "late reply", ""); err != nil { - t.Fatal(err) + if err = store.SaveBotReply(*claimed, b, "late reply", ""); err != ErrBotLeaseLost { + t.Fatalf("stale owner saved: %v", err) } row, _ := store.BotInteraction(item.ID) if row.Reply == "late reply" { @@ -82,6 +98,9 @@ func TestBotCardDecisionCannotOverwriteConcurrentRunWrite(t *testing.T) { func TestBotInteractionMySQLLeaseCompetition(t *testing.T) { dsn := os.Getenv("AGENTFLOW_BOT_TEST_MYSQL_DSN") + if dsn == "" { + dsn = os.Getenv("AGENTFLOW_TEST_MYSQL_DSN") + } if dsn == "" { t.Skip("local Docker MySQL not configured") } @@ -98,7 +117,7 @@ func TestBotInteractionMySQLLeaseCompetition(t *testing.T) { wg.Add(1) go func(i int) { defer wg.Done() - ok, err := store.ClaimBotConnection(id, fmt.Sprintf("owner-%d", i), time.Now().UTC()) + _, ok, err := store.ClaimBotConnection(id, fmt.Sprintf("owner-%d", i)) if err != nil { t.Error(err) } diff --git a/internal/repo/bot_migrations_mysql_test.go b/internal/repo/bot_migrations_mysql_test.go new file mode 100644 index 000000000..689a7a6ff --- /dev/null +++ b/internal/repo/bot_migrations_mysql_test.go @@ -0,0 +1,109 @@ +package repo + +import ( + "agentflow/internal/domain" + "fmt" + "os" + "strings" + "testing" + "time" +) + +// execSQLScript runs a migration script statement by statement (the scripts +// contain no semicolons inside literals) and returns the rows of the last +// SELECT for inspection. +func execSQLScript(t *testing.T, s *Store, path string) { + t.Helper() + raw, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var lines []string + for _, line := range strings.Split(string(raw), "\n") { + if strings.HasPrefix(strings.TrimSpace(line), "--") { + continue + } + lines = append(lines, line) + } + for _, stmt := range strings.Split(strings.Join(lines, "\n"), ";") { + stmt = strings.TrimSpace(stmt) + if stmt == "" { + continue + } + if err := s.DB.Exec(stmt).Error; err != nil { + t.Fatalf("%s: %v\n%s", path, err, stmt) + } + } +} + +// Legacy rows (payload only, physical columns at their added defaults) are +// backfilled by 036 and receive their WeCom ownership from 037; both scripts +// can run twice. +func TestMySQLMigrations036And037BackfillLegacyRows(t *testing.T) { + s := openBotMySQL(t) + suffix := fmt.Sprintf("%d", time.Now().UnixNano()) + legacyID, sharedA, sharedB := "legacy-"+suffix, "shared-a-"+suffix, "shared-b-"+suffix + ext, sharedExt := "wx-"+suffix, "wx-shared-"+suffix + payload := func(id, botID string, enabled, interactive bool) []byte { + return []byte(fmt.Sprintf(`{"id":%q,"projectId":"p-%s","name":"n","botId":%q,"enabled":%t,"interactionEnabled":%t}`, id, suffix, botID, enabled, interactive)) + } + for _, row := range []notificationBotRow{ + {ID: legacyID, ProjectID: "p-" + suffix, Payload: payload(legacyID, ext, true, true)}, + {ID: sharedA, ProjectID: "p-" + suffix, Payload: payload(sharedA, sharedExt, true, false)}, + {ID: sharedB, ProjectID: "p-" + suffix, Payload: payload(sharedB, sharedExt, false, false)}, + } { + if err := s.DB.Exec("INSERT INTO notification_bots (id, project_id, payload) VALUES (?, ?, ?)", row.ID, row.ProjectID, string(row.Payload)).Error; err != nil { + t.Fatal(err) + } + } + s.DB.Create(&botCredentialRow{BotID: legacyID, Secret: "legacy-plain"}) + interaction := domain.BotInteraction{ID: "mi-" + suffix, BotID: legacyID, Status: "replied", CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC()} + s.DB.Create(&interaction) + bound := domain.BotUserBinding{ID: botIdentityID(ext, "u1"), BotID: ext, SenderID: "u1", UserID: 7, CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC()} + ambiguous := domain.BotUserBinding{ID: botIdentityID(sharedExt, "u2"), BotID: sharedExt, SenderID: "u2", UserID: 8, CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC()} + s.DB.Create(&bound) + s.DB.Create(&ambiguous) + t.Cleanup(func() { + s.DB.Where("id IN ?", []string{legacyID, sharedA, sharedB}).Delete(¬ificationBotRow{}) + s.DB.Where("bot_id = ?", legacyID).Delete(&botCredentialRow{}) + s.DB.Delete(&interaction) + s.DB.Where("id IN ?", []string{bound.ID, ambiguous.ID}).Delete(&domain.BotUserBinding{}) + }) + if bots, _ := s.InteractiveNotificationBots(); containsBot(bots, legacyID) { + t.Fatal("unmigrated row must not receive before 036") + } + for i := 0; i < 2; i++ { + execSQLScript(t, s, "../../migrations/036_enterprise_im_channels.sql") + execSQLScript(t, s, "../../migrations/037_bot_binding_ownership.sql") + } + var row notificationBotRow + s.DB.First(&row, "id = ?", legacyID) + legacy, _ := row.bot() + if row.Channel != "wecom" || row.ExternalAppID != ext || !row.Enabled || !row.InteractionEnabled || row.ConfigVersion != 1 || row.CredentialVersion != 1 || row.ReceiveSlot == nil || *row.ReceiveSlot != legacy.ReceiveSlot() { + t.Fatalf("backfill = %+v slot=%v", row, row.ReceiveSlot) + } + if bots, _ := s.InteractiveNotificationBots(); !containsBot(bots, legacyID) { + t.Fatal("migrated row does not receive") + } + if got, _ := s.BotInteraction(interaction.ID); got.ProjectID != "p-"+suffix { + t.Fatalf("project snapshot = %q", got.ProjectID) + } + var b1, b2 domain.BotUserBinding + s.DB.First(&b1, "id = ?", bound.ID) + s.DB.First(&b2, "id = ?", ambiguous.ID) + if b1.ConfigID != legacyID || b1.Channel != "wecom" || b2.ConfigID != "" { + t.Fatalf("ownership = %+v / %+v", b1, b2) + } + if secret, err := s.BotCredential(legacyID); err != nil || secret != "legacy-plain" { + t.Fatalf("legacy credential = %q %v", secret, err) + } +} + +func containsBot(bots []domain.NotificationBot, id string) bool { + for _, b := range bots { + if b.ID == id { + return true + } + } + return false +} diff --git a/internal/repo/bot_native_sessions.go b/internal/repo/bot_native_sessions.go new file mode 100644 index 000000000..5a654c495 --- /dev/null +++ b/internal/repo/bot_native_sessions.go @@ -0,0 +1,89 @@ +package repo + +import ( + "time" + + "gorm.io/gorm" +) + +// botNativeSessionRow maps one Bot model turn to the native CLI session it +// ran in. It is written before the CLI starts, so every Bot-originated native +// log can later be located and cleaned without touching other sources that +// share the user's CLI home. One Bot conversation maps to many rows: one per +// interaction and per native session actually created. +type botNativeSessionRow struct { + ID string `gorm:"primaryKey;size:64"` + ConfigID string `gorm:"size:191;index"` + ProjectID string `gorm:"size:191"` + UserID int64 `gorm:"index"` + ConversationID string `gorm:"size:64"` + BotSessionID string `gorm:"size:64;index"` + InteractionID string `gorm:"size:64;index"` + Engine string `gorm:"size:32"` + RequestedSessionID string `gorm:"size:191"` + NativeSessionID string `gorm:"size:191;index"` + StorageScope string `gorm:"size:64"` + LogPath string `gorm:"size:512"` + Status string `gorm:"size:16;index"` + CleanupStatus string `gorm:"size:16"` + CleanupDetail string `gorm:"size:255"` + CreatedAt time.Time `gorm:"index"` + FinishedAt *time.Time + CleanedAt *time.Time +} + +func (botNativeSessionRow) TableName() string { return "bot_native_sessions" } + +// BotNativeSession is the input for a new mapping. +type BotNativeSession struct { + ConfigID, ProjectID, ConversationID, BotSessionID, InteractionID string + UserID int64 + Engine, RequestedSessionID, StorageScope string +} + +// StartBotNativeSession records the mapping before the CLI process starts. +// A failure must stop the turn: an unmapped Bot log could not be cleaned. +func (r *AssistantRepo) StartBotNativeSession(in BotNativeSession) (string, error) { + row := botNativeSessionRow{ID: "bns-" + randomHex(12), ConfigID: in.ConfigID, ProjectID: in.ProjectID, UserID: in.UserID, ConversationID: in.ConversationID, BotSessionID: in.BotSessionID, InteractionID: in.InteractionID, Engine: in.Engine, RequestedSessionID: in.RequestedSessionID, StorageScope: in.StorageScope, Status: "started", CreatedAt: time.Now().UTC()} + return row.ID, r.DB.Create(&row).Error +} + +// FinishBotNativeSession stores the native session ID the CLI reported and the +// terminal state. When the CLI created a different session than the mapping +// already records, a new mapping row is added so both stay traceable. +func (r *AssistantRepo) FinishBotNativeSession(id, nativeSessionID, status string) (string, error) { + err := r.DB.Transaction(func(tx *gorm.DB) error { + var row botNativeSessionRow + if err := r.locking(tx).First(&row, "id = ?", id).Error; err != nil { + return err + } + now := time.Now().UTC() + if row.NativeSessionID != "" && nativeSessionID != "" && row.NativeSessionID != nativeSessionID { + extra := row + extra.ID, extra.NativeSessionID, extra.Status, extra.CreatedAt, extra.FinishedAt = "bns-"+randomHex(12), nativeSessionID, status, now, &now + id = extra.ID + return tx.Create(&extra).Error + } + updates := map[string]any{"status": status, "finished_at": now} + if nativeSessionID != "" { + updates["native_session_id"] = nativeSessionID + } + return tx.Model(&botNativeSessionRow{}).Where("id = ?", id).Updates(updates).Error + }) + return id, err +} + +// BotNativeSessions lists mappings of one interaction (diagnostics, tests). +func (r *AssistantRepo) BotNativeSessions(interactionID string) ([]BotNativeSession, []string, error) { + var rows []botNativeSessionRow + if err := r.DB.Where("interaction_id = ?", interactionID).Order("created_at, id").Find(&rows).Error; err != nil { + return nil, nil, err + } + out := make([]BotNativeSession, 0, len(rows)) + native := make([]string, 0, len(rows)) + for _, row := range rows { + out = append(out, BotNativeSession{ConfigID: row.ConfigID, ProjectID: row.ProjectID, ConversationID: row.ConversationID, BotSessionID: row.BotSessionID, InteractionID: row.InteractionID, UserID: row.UserID, Engine: row.Engine, RequestedSessionID: row.RequestedSessionID, StorageScope: row.StorageScope}) + native = append(native, row.NativeSessionID+"|"+row.Status) + } + return out, native, nil +} diff --git a/internal/repo/bot_pending_drafts.go b/internal/repo/bot_pending_drafts.go new file mode 100644 index 000000000..c3dd92122 --- /dev/null +++ b/internal/repo/bot_pending_drafts.go @@ -0,0 +1,186 @@ +package repo + +import ( + "agentflow/internal/domain" + "crypto/sha256" + "encoding/hex" + "errors" + "time" + + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +// Pending write drafts of WeCom Bots in the "confirm" write mode (stage G). +// The assistant's drafts are frozen when the reply is produced; the bound +// user confirms them on /bot-approve.html. Confirmation atomically moves a +// draft from pending to executing, so a repeated click or a second tab can +// never execute it twice. A draft stuck in executing (process ended during +// the business writes) is reported as needs_review and never re-executed. + +const ( + BotDraftPending = "pending" + BotDraftExecuting = "executing" + BotDraftExecuted = "executed" + BotDraftFailed = "failed" + BotDraftNeedsReview = "needs_review" + BotDraftExpired = "expired" + BotDraftCancelled = "cancelled" + + // BotDraftTTL is how long a draft can be confirmed. + BotDraftTTL = 30 * time.Minute + // botDraftExecutingStale: an executing draft older than this is + // considered interrupted. + botDraftExecutingStale = 10 * time.Minute +) + +var ( + ErrBotDraftNotPending = errors.New("bot_draft_not_pending") + ErrBotDraftTampered = errors.New("草稿参数校验失败,已停止执行") +) + +type botPendingDraftRow struct { + ID string `gorm:"primaryKey;size:64"` + ConfigID string `gorm:"size:191;index"` + IdentityScope string `gorm:"size:191;index:idx_bot_draft_sender,priority:1"` + SenderID string `gorm:"size:191;index:idx_bot_draft_sender,priority:2"` + ProjectID string `gorm:"size:191"` + UserID int64 `gorm:"index"` + InteractionID string `gorm:"size:64"` + Summary string `gorm:"type:text"` + Drafts string `gorm:"type:mediumtext"` + ParamsHash string `gorm:"size:64"` + Status string `gorm:"size:16;index"` + ExecKey string `gorm:"size:64"` + Result string `gorm:"type:text"` + Reason string `gorm:"size:255"` + ExpiresAt time.Time + CreatedAt time.Time + UpdatedAt time.Time + ExecutedAt *time.Time + // BodyPurgedAt: the retention cleanup removed the drafts, summary and + // result (stage H). + BodyPurgedAt *time.Time +} + +func (botPendingDraftRow) TableName() string { return "bot_pending_drafts" } + +func (row botPendingDraftRow) draft() domain.BotPendingDraft { + return domain.BotPendingDraft{ID: row.ID, ConfigID: row.ConfigID, IdentityScope: row.IdentityScope, SenderID: row.SenderID, ProjectID: row.ProjectID, UserID: row.UserID, InteractionID: row.InteractionID, Summary: row.Summary, Drafts: row.Drafts, Status: row.Status, ExecKey: row.ExecKey, Result: row.Result, Reason: row.Reason, ExpiresAt: row.ExpiresAt, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt, ExecutedAt: row.ExecutedAt} +} + +func botDraftHash(drafts string) string { + sum := sha256.Sum256([]byte(drafts)) + return hex.EncodeToString(sum[:]) +} + +// CreateBotPendingDraft freezes drafts for confirmation and returns the +// random draft ID used in the link. +func (r *AssistantRepo) CreateBotPendingDraft(d domain.BotPendingDraft, now time.Time) (domain.BotPendingDraft, error) { + row := botPendingDraftRow{ID: randomHex(16), ConfigID: d.ConfigID, IdentityScope: d.IdentityScope, SenderID: d.SenderID, ProjectID: d.ProjectID, UserID: d.UserID, InteractionID: d.InteractionID, Summary: d.Summary, Drafts: d.Drafts, ParamsHash: botDraftHash(d.Drafts), Status: BotDraftPending, ExecKey: randomHex(16), ExpiresAt: now.Add(BotDraftTTL), CreatedAt: now, UpdatedAt: now} + if err := r.DB.Create(&row).Error; err != nil { + return domain.BotPendingDraft{}, err + } + return row.draft(), nil +} + +// BotPendingDraft reads a draft, settling lazy transitions: an expired +// pending draft becomes expired, an executing draft past the stale window +// becomes needs_review. +func (r *AssistantRepo) BotPendingDraft(id string, now time.Time) (domain.BotPendingDraft, error) { + var row botPendingDraftRow + if err := r.DB.First(&row, "id = ?", id).Error; err != nil { + return domain.BotPendingDraft{}, err + } + switch { + case row.Status == BotDraftPending && !now.Before(row.ExpiresAt): + r.DB.Model(&botPendingDraftRow{}).Where("id = ? AND status = ?", id, BotDraftPending).Updates(map[string]any{"status": BotDraftExpired, "reason": "草稿已过期", "updated_at": now}) + row.Status, row.Reason = BotDraftExpired, "草稿已过期" + case row.Status == BotDraftExecuting && now.Sub(row.UpdatedAt) > botDraftExecutingStale: + r.DB.Model(&botPendingDraftRow{}).Where("id = ? AND status = ?", id, BotDraftExecuting).Updates(map[string]any{"status": BotDraftNeedsReview, "reason": "执行过程中断,结果待核对;平台不会自动重新执行", "updated_at": now}) + row.Status, row.Reason = BotDraftNeedsReview, "执行过程中断,结果待核对;平台不会自动重新执行" + } + return row.draft(), nil +} + +// BotPendingDraftForInteraction returns the pending draft a chat turn +// produced, if any (used to send the confirmation as a card). +func (r *AssistantRepo) BotPendingDraftForInteraction(interactionID string) (domain.BotPendingDraft, bool) { + var row botPendingDraftRow + if interactionID == "" || r.DB.Where("interaction_id = ? AND status = ?", interactionID, BotDraftPending).Order("created_at DESC").First(&row).Error != nil { + return domain.BotPendingDraft{}, false + } + return row.draft(), true +} + +// BeginBotPendingDraft moves a pending, unexpired draft of userID to +// executing and verifies the frozen parameters. Any other state returns the +// current draft with ErrBotDraftNotPending. +func (r *AssistantRepo) BeginBotPendingDraft(id string, userID int64, now time.Time) (domain.BotPendingDraft, error) { + var out domain.BotPendingDraft + err := r.DB.Transaction(func(tx *gorm.DB) error { + var row botPendingDraftRow + q := tx + if r.isMySQL() { + q = q.Clauses(clause.Locking{Strength: "UPDATE"}) + } + if err := q.First(&row, "id = ?", id).Error; err != nil { + return err + } + out = row.draft() + if row.UserID != userID { + return gorm.ErrRecordNotFound + } + if row.Status != BotDraftPending { + return ErrBotDraftNotPending + } + if !now.Before(row.ExpiresAt) { + out.Status, out.Reason = BotDraftExpired, "草稿已过期" + if err := tx.Model(&botPendingDraftRow{}).Where("id = ?", id).Updates(map[string]any{"status": BotDraftExpired, "reason": out.Reason, "updated_at": now}).Error; err != nil { + return err + } + return ErrBotDraftNotPending + } + if botDraftHash(row.Drafts) != row.ParamsHash { + out.Status, out.Reason = BotDraftCancelled, ErrBotDraftTampered.Error() + if err := tx.Model(&botPendingDraftRow{}).Where("id = ?", id).Updates(map[string]any{"status": BotDraftCancelled, "reason": out.Reason, "updated_at": now}).Error; err != nil { + return err + } + return ErrBotDraftNotPending + } + result := tx.Model(&botPendingDraftRow{}).Where("id = ? AND status = ?", id, BotDraftPending).Updates(map[string]any{"status": BotDraftExecuting, "updated_at": now}) + if result.Error != nil { + return result.Error + } + if result.RowsAffected != 1 { + return ErrBotDraftNotPending + } + out.Status, out.UpdatedAt = BotDraftExecuting, now + return nil + }) + return out, err +} + +// FinishBotPendingDraft records the outcome of an executing draft. +func (r *AssistantRepo) FinishBotPendingDraft(id, status, result string, now time.Time) error { + return r.DB.Model(&botPendingDraftRow{}).Where("id = ? AND status = ?", id, BotDraftExecuting).Updates(map[string]any{"status": status, "result": result, "executed_at": now, "updated_at": now}).Error +} + +// CancelBotPendingDraft lets the bound user reject a pending draft, or the +// server invalidate it after a failed re-check. +func (r *AssistantRepo) CancelBotPendingDraft(id string, userID int64, reason string, now time.Time) error { + result := r.DB.Model(&botPendingDraftRow{}).Where("id = ? AND user_id = ? AND status = ?", id, userID, BotDraftPending).Updates(map[string]any{"status": BotDraftCancelled, "reason": truncateBotDetail(reason), "updated_at": now}) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return ErrBotDraftNotPending + } + return nil +} + +// cancelBotDraftsWhere invalidates pending drafts (unbinding, Bot disabled, +// deleted or no longer in confirm mode). Executing drafts are left alone. +func cancelBotDraftsWhere(tx *gorm.DB, reason string, query string, args ...any) error { + return tx.Model(&botPendingDraftRow{}).Where("status = ?", BotDraftPending).Where(query, args...).Updates(map[string]any{"status": BotDraftCancelled, "reason": reason, "updated_at": time.Now().UTC()}).Error +} diff --git a/internal/repo/bot_retention.go b/internal/repo/bot_retention.go new file mode 100644 index 000000000..97c710be4 --- /dev/null +++ b/internal/repo/bot_retention.go @@ -0,0 +1,259 @@ +package repo + +import ( + "agentflow/internal/domain" + "context" + "strings" + "time" + + "gorm.io/gorm" +) + +// Retention of chat Bot data (stage H). Two horizons apply to records in a +// terminal state, measured from the time they actually finished: +// +// - Body: message and reply text, rendered notification content, frozen +// write drafts and the native CLI logs of Bot turns are removed. The row +// stays for audit and can no longer be sent or executed again. +// - Record: the row itself is deleted. +// +// Only terminal states are touched. Interactions in queued, running, ready, +// sending or reply_unknown, notifications that are pending, sending, +// awaiting authorization or send_unknown, and drafts that are executing or +// need review stay until someone handles them. Every statement re-checks +// its conditions on the selected IDs, so concurrent cleanups on several +// replicas are harmless. + +// BotRetentionPolicy: a zero duration disables that horizon. Batch bounds +// one statement; Batches bounds the statements per category and run. +type BotRetentionPolicy struct { + Body, Record time.Duration + Batch int + Batches int +} + +// BotRetentionResult counts rows changed by one run. +type BotRetentionResult struct { + InteractionBodies, Interactions int64 + DeliveryBodies, Deliveries int64 + DraftsExpired int64 + DraftBodies, Drafts int64 + NativeMappings int64 +} + +func (r BotRetentionResult) Total() int64 { + return r.InteractionBodies + r.Interactions + r.DeliveryBodies + r.Deliveries + r.DraftsExpired + r.DraftBodies + r.Drafts + r.NativeMappings +} + +var ( + botInteractionTerminal = []string{domain.BotReplied, domain.BotReplyFailed, domain.BotCancelled} + notificationTerminal = []string{"sent", "failed", "cancelled", "expired", "target_unavailable"} + botDraftTerminal = []string{BotDraftExecuted, BotDraftFailed, BotDraftExpired, BotDraftCancelled} + botNativeFinished = []string{"completed", "failed", "interrupted"} + botNativeCleanupSettled = []string{BotNativeCleanupRemoved, BotNativeCleanupMissing} +) + +// Native log cleanup states of bot_native_sessions.cleanup_status. +const ( + BotNativeCleanupRemoved = "removed" + BotNativeCleanupMissing = "missing" + BotNativeCleanupError = "error" + BotNativeCleanupUnsupported = "unsupported" +) + +func (p BotRetentionPolicy) normalized() BotRetentionPolicy { + if p.Batch <= 0 { + p.Batch = 1000 + } + if p.Batches <= 0 { + p.Batches = 10 + } + return p +} + +// batched runs one category: select a batch of IDs, apply the change with +// the same conditions, repeat while full batches come back. +func batched(ctx context.Context, p BotRetentionPolicy, pick func(limit int) ([]string, error), apply func(ids []string) (int64, error)) (int64, error) { + var total int64 + for i := 0; i < p.Batches; i++ { + if ctx.Err() != nil { + return total, ctx.Err() + } + ids, err := pick(p.Batch) + if err != nil || len(ids) == 0 { + return total, err + } + n, err := apply(ids) + total += n + if err != nil || len(ids) < p.Batch { + return total, err + } + } + return total, nil +} + +// PurgeBotRetention applies the policy once. +func (s *Store) PurgeBotRetention(ctx context.Context, now time.Time, policy BotRetentionPolicy) (BotRetentionResult, error) { + p := policy.normalized() + db := s.DB.WithContext(ctx) + var out BotRetentionResult + var err error + step := func(n *int64, pick func(int) ([]string, error), apply func([]string) (int64, error)) { + if err != nil { + return + } + *n, err = batched(ctx, p, pick, apply) + } + + // Drafts that expired without anyone opening them. + step(&out.DraftsExpired, func(limit int) ([]string, error) { + var ids []string + return ids, db.Model(&botPendingDraftRow{}).Where("status = ? AND expires_at <= ?", BotDraftPending, now).Order("expires_at").Limit(limit).Pluck("id", &ids).Error + }, func(ids []string) (int64, error) { + res := db.Model(&botPendingDraftRow{}).Where("id IN ? AND status = ? AND expires_at <= ?", ids, BotDraftPending, now).UpdateColumns(map[string]any{"status": BotDraftExpired, "reason": "草稿已过期", "updated_at": now}) + return res.RowsAffected, res.Error + }) + + if p.Body > 0 { + cut := now.Add(-p.Body) + step(&out.InteractionBodies, func(limit int) ([]string, error) { + var ids []string + return ids, db.Model(&domain.BotInteraction{}).Where("status IN ? AND updated_at < ? AND body_purged_at IS NULL", botInteractionTerminal, cut).Order("updated_at").Limit(limit).Pluck("id", &ids).Error + }, func(ids []string) (int64, error) { + // UpdateColumns keeps updated_at: the record horizon still + // counts from completion. + res := db.Model(&domain.BotInteraction{}).Where("id IN ? AND status IN ? AND updated_at < ? AND body_purged_at IS NULL", ids, botInteractionTerminal, cut).UpdateColumns(map[string]any{"content": "", "quote": "", "reply": "", "reply_route": "", "body_purged_at": now}) + return res.RowsAffected, res.Error + }) + step(&out.DeliveryBodies, func(limit int) ([]string, error) { + var ids []string + return ids, notificationsFinishedBefore(db, cut).Where("(content_source IS NULL OR content_source <> 'purged')").Order("created_at").Limit(limit).Pluck("id", &ids).Error + }, func(ids []string) (int64, error) { + res := notificationsFinishedBefore(db, cut).Where("id IN ? AND (content_source IS NULL OR content_source <> 'purged')", ids).UpdateColumns(map[string]any{"content": "", "report_request": "", "content_source": "purged"}) + return res.RowsAffected, res.Error + }) + step(&out.DraftBodies, func(limit int) ([]string, error) { + var ids []string + return ids, db.Model(&botPendingDraftRow{}).Where("status IN ? AND updated_at < ? AND body_purged_at IS NULL", botDraftTerminal, cut).Order("updated_at").Limit(limit).Pluck("id", &ids).Error + }, func(ids []string) (int64, error) { + res := db.Model(&botPendingDraftRow{}).Where("id IN ? AND status IN ? AND updated_at < ? AND body_purged_at IS NULL", ids, botDraftTerminal, cut).UpdateColumns(map[string]any{"drafts": "", "summary": "", "result": "", "body_purged_at": now}) + return res.RowsAffected, res.Error + }) + } + if p.Record > 0 { + cut := now.Add(-p.Record) + step(&out.Interactions, func(limit int) ([]string, error) { + var ids []string + return ids, db.Model(&domain.BotInteraction{}).Where("status IN ? AND updated_at < ?", botInteractionTerminal, cut).Order("updated_at").Limit(limit).Pluck("id", &ids).Error + }, func(ids []string) (int64, error) { + res := db.Where("id IN ? AND status IN ? AND updated_at < ?", ids, botInteractionTerminal, cut).Delete(&domain.BotInteraction{}) + return res.RowsAffected, res.Error + }) + step(&out.Deliveries, func(limit int) ([]string, error) { + var ids []string + return ids, notificationsFinishedBefore(db, cut).Order("created_at").Limit(limit).Pluck("id", &ids).Error + }, func(ids []string) (int64, error) { + res := notificationsFinishedBefore(db, cut).Where("id IN ?", ids).Delete(&domain.NotificationDelivery{}) + return res.RowsAffected, res.Error + }) + step(&out.Drafts, func(limit int) ([]string, error) { + var ids []string + return ids, db.Model(&botPendingDraftRow{}).Where("status IN ? AND updated_at < ?", botDraftTerminal, cut).Order("updated_at").Limit(limit).Pluck("id", &ids).Error + }, func(ids []string) (int64, error) { + res := db.Where("id IN ? AND status IN ? AND updated_at < ?", ids, botDraftTerminal, cut).Delete(&botPendingDraftRow{}) + return res.RowsAffected, res.Error + }) + // A mapping goes only after its native log is settled (removed or + // confirmed missing); errors and unsupported engines stay listed. + step(&out.NativeMappings, func(limit int) ([]string, error) { + var ids []string + return ids, db.Model(&botNativeSessionRow{}).Where("status IN ? AND cleanup_status IN ? AND finished_at < ?", botNativeFinished, botNativeCleanupSettled, cut).Order("finished_at").Limit(limit).Pluck("id", &ids).Error + }, func(ids []string) (int64, error) { + res := db.Where("id IN ? AND status IN ? AND cleanup_status IN ? AND finished_at < ?", ids, botNativeFinished, botNativeCleanupSettled, cut).Delete(&botNativeSessionRow{}) + return res.RowsAffected, res.Error + }) + } + return out, err +} + +// notificationsFinishedBefore selects terminal deliveries whose creation, +// last attempt and send time are all before cut. Deliveries have no +// completion column; the last attempt time is written when they finish. +func notificationsFinishedBefore(db *gorm.DB, cut time.Time) *gorm.DB { + return db.Model(&domain.NotificationDelivery{}).Where("status IN ? AND created_at < ? AND next_attempt_at < ? AND (sent_at IS NULL OR sent_at < ?)", notificationTerminal, cut, cut, cut) +} + +// BotNativeCleanup is one finished Bot turn whose native CLI log is due. +type BotNativeCleanup struct { + ID, Engine, StorageScope string + UserID int64 + RequestedSessionID, NativeID string + FinishedAt time.Time +} + +// BotNativeCleanupResult is the outcome for one mapping. Paths are relative +// to the CLI home the log was resolved in. +type BotNativeCleanupResult struct { + ID string + Status string + Paths []string + Detail string +} + +// DueBotNativeCleanups lists finished mappings older than cut whose log was +// not cleaned yet (or failed last time). A mapping is skipped while another +// mapping of the same native session is still running or not yet due, so a +// shared native log is only removed when every turn using it has expired. +func (r *AssistantRepo) DueBotNativeCleanups(cut time.Time, limit int) ([]BotNativeCleanup, error) { + var rows []botNativeSessionRow + if err := r.DB.Where("status IN ? AND finished_at < ? AND (cleanup_status = '' OR cleanup_status IS NULL OR cleanup_status = ?)", botNativeFinished, cut, BotNativeCleanupError).Order("finished_at").Limit(limit).Find(&rows).Error; err != nil { + return nil, err + } + out := make([]BotNativeCleanup, 0, len(rows)) + for _, row := range rows { + if shared := sharedNativeIDs(row); len(shared) > 0 { + var busy int64 + if err := r.DB.Model(&botNativeSessionRow{}).Where("id <> ? AND user_id = ? AND (native_session_id IN ? OR requested_session_id IN ?) AND (status NOT IN ? OR finished_at IS NULL OR finished_at >= ?)", row.ID, row.UserID, shared, shared, botNativeFinished, cut).Count(&busy).Error; err != nil { + return nil, err + } + if busy > 0 { + continue + } + } + item := BotNativeCleanup{ID: row.ID, Engine: row.Engine, StorageScope: row.StorageScope, UserID: row.UserID, RequestedSessionID: row.RequestedSessionID, NativeID: row.NativeSessionID} + if row.FinishedAt != nil { + item.FinishedAt = *row.FinishedAt + } + out = append(out, item) + } + return out, nil +} + +func sharedNativeIDs(row botNativeSessionRow) []string { + out := []string{} + for _, id := range []string{row.NativeSessionID, row.RequestedSessionID} { + if strings.TrimSpace(id) != "" { + out = append(out, id) + } + } + return out +} + +// RecordBotNativeCleanup stores the outcome of one mapping. +func (r *AssistantRepo) RecordBotNativeCleanup(res BotNativeCleanupResult, now time.Time) error { + paths := strings.Join(res.Paths, "\n") + if len(paths) > 512 { + paths = paths[:509] + "..." + } + return r.DB.Model(&botNativeSessionRow{}).Where("id = ? AND (cleanup_status = '' OR cleanup_status IS NULL OR cleanup_status = ?)", res.ID, BotNativeCleanupError).UpdateColumns(map[string]any{"cleanup_status": res.Status, "cleanup_detail": truncateBotDetail(res.Detail), "log_path": paths, "cleaned_at": now}).Error +} + +// BotNativeCleanupState returns the stored cleanup outcome of a mapping +// (tests, diagnostics). +func (r *AssistantRepo) BotNativeCleanupState(id string) (status, paths, detail string, err error) { + var row botNativeSessionRow + if err = r.DB.First(&row, "id = ?", id).Error; err != nil { + return + } + return row.CleanupStatus, row.LogPath, row.CleanupDetail, nil +} diff --git a/internal/repo/bot_retention_test.go b/internal/repo/bot_retention_test.go new file mode 100644 index 000000000..2335ff8d8 --- /dev/null +++ b/internal/repo/bot_retention_test.go @@ -0,0 +1,249 @@ +package repo + +import ( + "agentflow/internal/domain" + "context" + "errors" + "os" + "testing" + "time" +) + +// retentionCase seeds rows at known ages and checks one purge run against +// them. It runs on SQLite and, with AGENTFLOW_TEST_MYSQL_DSN, on MySQL. +func retentionCase(t *testing.T, s *Store, suffix string) { + t.Helper() + now := time.Now().UTC().Truncate(time.Second) + day := 24 * time.Hour + id := func(name string) string { return name + "-" + suffix } + interaction := func(name, status string, age time.Duration) { + at := now.Add(-age) + item := domain.BotInteraction{ID: id(name), BotID: "cfg-" + suffix, Content: "问题 " + name, Reply: "答复 " + name, ReplyRoute: "sealed", Status: status, CreatedAt: at, UpdatedAt: at} + if err := s.DB.Create(&item).Error; err != nil { + t.Fatal(err) + } + if err := s.DB.Model(&domain.BotInteraction{}).Where("id = ?", item.ID).UpdateColumn("updated_at", at).Error; err != nil { + t.Fatal(err) + } + } + interaction("i-fresh", domain.BotReplied, 10*day) + interaction("i-body", domain.BotReplied, 100*day) + interaction("i-failed-body", domain.BotReplyFailed, 100*day) + interaction("i-old", domain.BotCancelled, 200*day) + interaction("i-unknown", domain.BotReplyUnknown, 400*day) + interaction("i-ready", domain.BotReady, 400*day) + interaction("i-running", domain.BotRunning, 400*day) + + delivery := func(name, status string, age time.Duration) { + at := now.Add(-age) + d := domain.NotificationDelivery{ID: id(name), ProjectID: "p-" + suffix, BotID: "cfg-" + suffix, EventType: "failed", Content: "通知 " + name, ReportRequest: "{}", Status: status, CreatedAt: at, NextAttemptAt: at} + if status == "sent" { + d.SentAt = &at + } + if err := s.CreateNotificationDelivery(d); err != nil { + t.Fatal(err) + } + } + delivery("d-fresh", "sent", 10*day) + delivery("d-body", "failed", 100*day) + delivery("d-old", "sent", 200*day) + delivery("d-unknown", "send_unknown", 400*day) + delivery("d-pending", "pending", 400*day) + // Created long ago but retried recently: not finished long ago. + delivery("d-retried", "failed", 200*day) + if err := s.DB.Model(&domain.NotificationDelivery{}).Where("id = ?", id("d-retried")).UpdateColumn("next_attempt_at", now.Add(-day)).Error; err != nil { + t.Fatal(err) + } + + draft := func(name, status string, age time.Duration) { + at := now.Add(-age) + row := botPendingDraftRow{ID: id(name), ConfigID: "cfg-" + suffix, UserID: 1, Summary: "1. 保存需求", Drafts: `[{"tool":"issues_save"}]`, Status: status, ExpiresAt: at.Add(30 * time.Minute), CreatedAt: at, UpdatedAt: at} + if err := s.DB.Create(&row).Error; err != nil { + t.Fatal(err) + } + if err := s.DB.Model(&botPendingDraftRow{}).Where("id = ?", row.ID).UpdateColumn("updated_at", at).Error; err != nil { + t.Fatal(err) + } + } + draft("g-pending-expired", BotDraftPending, 2*day) + draft("g-body", BotDraftExecuted, 100*day) + draft("g-old", BotDraftCancelled, 200*day) + draft("g-review", BotDraftNeedsReview, 400*day) + draft("g-live", BotDraftPending, 0) + if err := s.DB.Model(&botPendingDraftRow{}).Where("id = ?", id("g-live")).UpdateColumn("expires_at", now.Add(10*time.Minute)).Error; err != nil { + t.Fatal(err) + } + + native := func(name, status, cleanup string, age time.Duration) { + at := now.Add(-age) + row := botNativeSessionRow{ID: id(name), ConfigID: "cfg-" + suffix, UserID: 1, Engine: "codebuddy", RequestedSessionID: "req-" + name + suffix, NativeSessionID: "nat-" + name + suffix, StorageScope: "assistant-cli-home", Status: status, CleanupStatus: cleanup, CreatedAt: at, FinishedAt: &at} + if err := s.DB.Create(&row).Error; err != nil { + t.Fatal(err) + } + } + native("n-removed-old", "completed", BotNativeCleanupRemoved, 200*day) + native("n-error-old", "completed", BotNativeCleanupError, 200*day) + native("n-pending-old", "completed", "", 200*day) + + policy := BotRetentionPolicy{Body: 90 * day, Record: 180 * day, Batch: 2, Batches: 5} + res, err := s.PurgeBotRetention(context.Background(), now, policy) + if err != nil { + t.Fatal(err) + } + if res.InteractionBodies < 2 || res.Interactions < 1 || res.DeliveryBodies < 1 || res.Deliveries < 1 || res.DraftsExpired < 1 || res.DraftBodies < 1 || res.Drafts < 1 || res.NativeMappings < 1 { + t.Fatalf("result = %+v", res) + } + + get := func(name string) (domain.BotInteraction, bool) { + item, err := s.BotInteraction(id(name)) + return item, err == nil + } + if item, ok := get("i-fresh"); !ok || item.Content == "" || item.BodyPurgedAt != nil { + t.Fatalf("fresh = %+v", item) + } + for _, name := range []string{"i-body", "i-failed-body"} { + item, ok := get(name) + if !ok || item.Content != "" || item.Reply != "" || item.ReplyRoute != "" || item.BodyPurgedAt == nil || item.Status == "" { + t.Fatalf("%s = %+v", name, item) + } + if !item.UpdatedAt.Before(now.Add(-99 * day)) { + t.Fatalf("%s: purge moved updated_at to %v", name, item.UpdatedAt) + } + } + if _, ok := get("i-old"); ok { + t.Fatal("old interaction kept") + } + for _, name := range []string{"i-unknown", "i-ready", "i-running"} { + if item, ok := get(name); !ok || item.Content == "" { + t.Fatalf("%s must stay untouched: %+v", name, item) + } + } + // A purged failed reply cannot be sent again. + if _, err := s.ResendBotInteraction(id("i-failed-body"), 1, "k-"+suffix, "x"); !errors.Is(err, ErrBotStateConflict) && !errors.Is(err, ErrBotDeleted) { + t.Fatalf("resend after purge = %v", err) + } + + dget := func(name string) (domain.NotificationDelivery, bool) { + d, err := s.NotificationDelivery(id(name)) + return d, err == nil + } + if d, ok := dget("d-fresh"); !ok || d.Content == "" { + t.Fatalf("fresh delivery = %+v", d) + } + if d, ok := dget("d-body"); !ok || d.Content != "" || d.ContentSource != "purged" { + t.Fatalf("delivery body = %+v", d) + } + if err := s.RetryNotification("p-"+suffix, id("d-body")); err == nil { + t.Fatal("retried a purged notification") + } + if _, ok := dget("d-old"); ok { + t.Fatal("old delivery kept") + } + for _, name := range []string{"d-unknown", "d-pending", "d-retried"} { + if d, ok := dget(name); !ok || d.Content == "" { + t.Fatalf("%s must stay: %+v", name, d) + } + } + + if d, _ := s.BotPendingDraft(id("g-pending-expired"), now); d.Status != BotDraftExpired { + t.Fatalf("expired pending draft = %+v", d) + } + if d, _ := s.BotPendingDraft(id("g-body"), now); d.Drafts != "" || d.Summary != "" || d.Status != BotDraftExecuted { + t.Fatalf("draft body = %+v", d) + } + if _, err := s.BotPendingDraft(id("g-old"), now); err == nil { + t.Fatal("old draft kept") + } + if d, _ := s.BotPendingDraft(id("g-review"), now); d.Drafts == "" { + t.Fatal("needs_review draft purged") + } + if d, _ := s.BotPendingDraft(id("g-live"), now); d.Status != BotDraftPending || d.Drafts == "" { + t.Fatalf("live draft = %+v", d) + } + + var left []string + s.DB.Model(&botNativeSessionRow{}).Where("config_id = ?", "cfg-"+suffix).Order("id").Pluck("id", &left) + if len(left) != 2 || left[0] != id("n-error-old") || left[1] != id("n-pending-old") { + t.Fatalf("native mappings left = %v", left) + } + due, err := s.DueBotNativeCleanups(now.Add(-90*day), 100) + if err != nil { + t.Fatal(err) + } + found := 0 + for _, d := range due { + if d.ID == id("n-error-old") || d.ID == id("n-pending-old") { + found++ + } + } + if found != 2 { + t.Fatalf("due = %+v", due) + } +} + +func TestBotRetentionPurge(t *testing.T) { + s, err := OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + retentionCase(t, s, "sqlite") +} + +func TestMySQLBotRetentionPurge(t *testing.T) { + dsn := os.Getenv("AGENTFLOW_BOT_TEST_MYSQL_DSN") + if dsn == "" { + dsn = os.Getenv("AGENTFLOW_TEST_MYSQL_DSN") + } + if dsn == "" { + t.Skip("MySQL required") + } + s, err := Open(dsn) + if err != nil { + t.Fatal(err) + } + defer s.Close() + retentionCase(t, s, time.Now().Format("150405.000000")) +} + +func TestBotNativeCleanupSkipsSharedSessions(t *testing.T) { + s, err := OpenTest() + if err != nil { + t.Fatal(err) + } + defer s.Close() + now := time.Now().UTC() + old := now.Add(-100 * 24 * time.Hour) + recent := now.Add(-time.Hour) + for _, row := range []botNativeSessionRow{ + {ID: "a", UserID: 1, RequestedSessionID: "shared-req", NativeSessionID: "nat-a", Status: "completed", FinishedAt: &old}, + {ID: "b", UserID: 1, RequestedSessionID: "shared-req", NativeSessionID: "nat-b", Status: "completed", FinishedAt: &recent}, + {ID: "c", UserID: 1, RequestedSessionID: "own-req", NativeSessionID: "nat-c", Status: "completed", FinishedAt: &old}, + {ID: "d", UserID: 1, RequestedSessionID: "run-req", NativeSessionID: "", Status: "started"}, + {ID: "e", UserID: 1, RequestedSessionID: "run-req", NativeSessionID: "nat-e", Status: "failed", FinishedAt: &old}, + } { + row.StorageScope = "assistant-cli-home" + row.CreatedAt = old + if err := s.DB.Create(&row).Error; err != nil { + t.Fatal(err) + } + } + due, err := s.DueBotNativeCleanups(now.Add(-90*24*time.Hour), 10) + if err != nil { + t.Fatal(err) + } + if len(due) != 1 || due[0].ID != "c" { + t.Fatalf("due = %+v", due) + } + if err := s.RecordBotNativeCleanup(BotNativeCleanupResult{ID: "c", Status: BotNativeCleanupRemoved, Paths: []string{".codebuddy/projects/x/own-req.jsonl"}}, now); err != nil { + t.Fatal(err) + } + if status, paths, _, _ := s.BotNativeCleanupState("c"); status != BotNativeCleanupRemoved || paths != ".codebuddy/projects/x/own-req.jsonl" { + t.Fatalf("state = %s %s", status, paths) + } + // A settled mapping is not overwritten by a late duplicate run. + s.RecordBotNativeCleanup(BotNativeCleanupResult{ID: "c", Status: BotNativeCleanupError, Detail: "late"}, now) + if status, _, _, _ := s.BotNativeCleanupState("c"); status != BotNativeCleanupRemoved { + t.Fatal("settled cleanup overwritten") + } +} diff --git a/internal/repo/bot_stage_g_mysql_test.go b/internal/repo/bot_stage_g_mysql_test.go new file mode 100644 index 000000000..6e7d0355b --- /dev/null +++ b/internal/repo/bot_stage_g_mysql_test.go @@ -0,0 +1,138 @@ +package repo + +import ( + "agentflow/internal/domain" + "errors" + "fmt" + "os" + "strings" + "testing" + "time" +) + +// Stage G statements on a real MySQL/MariaDB: push targets and their +// verification, two-phase notification pushes, target switching, the +// expired-reply push resend and pending draft confirmation under +// concurrency. Run with AGENTFLOW_TEST_MYSQL_DSN. +func TestMySQLStageGPushAndDrafts(t *testing.T) { + dsn := os.Getenv("AGENTFLOW_BOT_TEST_MYSQL_DSN") + if dsn == "" { + dsn = os.Getenv("AGENTFLOW_TEST_MYSQL_DSN") + } + if dsn == "" { + t.Skip("MySQL required") + } + s, err := Open(dsn) + if err != nil { + t.Fatal(err) + } + defer s.Close() + s.SetCredentialKeyring(testCredentialKeyring) + suffix := fmt.Sprint(time.Now().UnixNano()) + bot := domain.NotificationBot{ID: "g-" + suffix, Channel: domain.BotChannelDingTalk, BotID: "ding-" + suffix, ProjectID: "p-" + suffix, OwnerID: 1, Enabled: true, InteractionEnabled: true, TenantKey: "corp", TenantState: domain.BotTenantLocked} + if _, err := s.SaveNotificationBotChecked(bot, "secret", nil); err != nil { + t.Fatal(err) + } + bot, _ = s.NotificationBot(bot.ID) + now := time.Now().UTC() + token, err := s.NewBotBindingChallenge(bot, "staff", now, true) + if err != nil { + t.Fatal(err) + } + code, err := s.ClaimBotBindingChallenge(token, 1, now) + if err != nil { + t.Fatal(err) + } + if _, err := s.ConfirmBotUserBinding(bot, "staff", code, now); err != nil { + t.Fatal(err) + } + if err := s.MarkBotPushTargetVerified(bot, "staff", 1, now); err != nil { + t.Fatal(err) + } + if err := s.MarkBotPushTargetVerified(bot, "staff", 2, now); !errors.Is(err, ErrNotificationTargetUnverified) { + t.Fatalf("wrong user verified: %v", err) + } + targets, err := s.BotPushTargets(bot) + if err != nil || len(targets) != 1 || targets[0].VerifiedAt == nil { + t.Fatalf("targets = %+v %v", targets, err) + } + + // Two-phase push with database time. + d := domain.NotificationDelivery{ID: "gd-" + suffix, ProjectID: bot.ProjectID, BotID: bot.ID, EventType: "failed", TargetID: "staff", Content: "x", Status: "pending", CreatedAt: now, NextAttemptAt: now.Add(-time.Second)} + if err := s.CreateNotificationDelivery(d); err != nil { + t.Fatal(err) + } + if err := s.DB.Model(&domain.NotificationDelivery{}).Where("id = ?", d.ID).Updates(map[string]any{"status": "sending", "lease_token": "lt"}).Error; err != nil { + t.Fatal(err) + } + d.Status, d.LeaseToken = "sending", "lt" + begun, elapsed, resumed, err := s.BeginNotificationPush(d) + if err != nil || resumed || begun.SendKey == "" || begun.SendStartedAtUS == 0 || elapsed != 0 { + t.Fatalf("begin = %+v %d %v %v", begun, elapsed, resumed, err) + } + if again, _, resumed, _ := s.BeginNotificationPush(d); !resumed || again.SendKey != begun.SendKey { + t.Fatalf("second begin = %+v %v", again, resumed) + } + if err := s.FinishNotificationPush(d, NotificationPushResult{Status: "send_unknown", Detail: "未知", Next: now}); err != nil { + t.Fatal(err) + } + if err := s.ConfirmNotificationReceived(bot.ProjectID, d.ID, 1); err != nil { + t.Fatal(err) + } + if got, _ := s.NotificationDelivery(d.ID); got.Status != "sent" || got.ReceiptSource != "manual" { + t.Fatalf("confirmed = %+v", got) + } + q := domain.NotificationDelivery{ID: "gq-" + suffix, ProjectID: bot.ProjectID, BotID: bot.ID, EventType: "failed", TargetID: "staff", Content: "x", Status: "pending", CreatedAt: now, NextAttemptAt: now.Add(time.Hour)} + s.CreateNotificationDelivery(q) + if n, err := s.SwitchNotificationTarget(bot.ID, domain.BotChat{ID: "other", Name: "o", Type: "single"}, "migrate"); err != nil || n != 1 { + t.Fatalf("migrate = %d %v", n, err) + } + if got, _ := s.NotificationDelivery(q.ID); got.TargetID != "other" || got.DeliveryGeneration != 1 { + t.Fatalf("migrated = %+v", got) + } + + // Expired reply switched to a push. + item := domain.BotInteraction{ID: "gi-" + suffix, BotID: bot.ID, ProjectID: bot.ProjectID, SenderID: "staff", Reply: "答复", Status: domain.BotReplyFailed, TerminalReason: "route_expired", DeliveryGeneration: 1, SendKey: "old", CreatedAt: now, UpdatedAt: now} + if err := s.DB.Create(&item).Error; err != nil { + t.Fatal(err) + } + out, err := s.PushResendBotInteraction(item.ID, 1, 1, "k", "过期") + if err != nil || out.SendRoute != BotSendRoutePush || out.DeliveryGeneration != 2 { + t.Fatalf("push resend = %+v %v", out, err) + } + + // Pending drafts: concurrent confirmations, revocation. + draft, err := s.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: bot.ID, IdentityScope: bot.IdentityScope(), SenderID: "staff", ProjectID: bot.ProjectID, UserID: 1, Summary: "1. 保存需求:😀", Drafts: `[{"tool":"issues_save","body":{"title":"` + strings.Repeat("长", 3000) + `"}}]`}, now) + if err != nil { + t.Fatal(err) + } + results := make(chan error, 8) + for i := 0; i < 8; i++ { + go func() { _, err := s.BeginBotPendingDraft(draft.ID, 1, now); results <- err }() + } + won := 0 + for i := 0; i < 8; i++ { + if err := <-results; err == nil { + won++ + } else if !errors.Is(err, ErrBotDraftNotPending) { + t.Fatal(err) + } + } + if won != 1 { + t.Fatalf("confirmed %d times", won) + } + pending, _ := s.CreateBotPendingDraft(domain.BotPendingDraft{ConfigID: bot.ID, IdentityScope: bot.IdentityScope(), SenderID: "staff", UserID: 1, Drafts: "[]"}, now) + binding, _ := s.BotUserBinding(bot.IdentityScope(), "staff") + if err := s.RevokeBotUserBinding(binding.ID, 1, now); err != nil { + t.Fatal(err) + } + if got, _ := s.BotPendingDraft(pending.ID, now); got.Status != BotDraftCancelled { + t.Fatalf("after revoke = %+v", got) + } + if b, _ := s.UsersRepo.botBindingRow(s.DB, binding.ID); b.TargetVerifiedAt != nil { + t.Fatal("revocation kept the push verification") + } + if err := s.DeleteNotificationBot(bot.ID); err != nil { + t.Fatal(err) + } +} diff --git a/internal/repo/bot_test_helpers_test.go b/internal/repo/bot_test_helpers_test.go new file mode 100644 index 000000000..35c239427 --- /dev/null +++ b/internal/repo/bot_test_helpers_test.go @@ -0,0 +1,12 @@ +package repo + +import "testing" + +// pinDBClock fixes the database clock used for Bot leases at start (epoch +// microseconds) and returns a function that advances it. +func pinDBClock(t *testing.T, start int64) func(deltaUS int64) { + t.Helper() + dbClockOverrideUS.Store(start) + t.Cleanup(func() { dbClockOverrideUS.Store(0) }) + return func(delta int64) { dbClockOverrideUS.Add(delta) } +} diff --git a/internal/repo/dbtime.go b/internal/repo/dbtime.go new file mode 100644 index 000000000..6861070c4 --- /dev/null +++ b/internal/repo/dbtime.go @@ -0,0 +1,73 @@ +package repo + +import ( + "errors" + "strconv" + "time" + + mysqldriver "github.com/go-sql-driver/mysql" + "sync/atomic" + + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +// Bot leases, send windows, retry times and route expiry are stored as epoch +// microseconds (BIGINT, *_us columns) and are both written and compared with +// the database clock inside SQL. Go wall clocks never decide ownership, so +// replicas with skewed clocks cannot steal or keep a lease they should not. +// +// MySQL: UNIX_TIMESTAMP(NOW(6)) is independent of the session time zone (NOW +// and UNIX_TIMESTAMP use the same zone) and honours SET timestamp in tests. +// SQLite: julianday('now') in microseconds; precision is about one +// millisecond, far below any lease or window used here. +const ( + mysqlNowUS = "CAST(UNIX_TIMESTAMP(NOW(6)) * 1000000 AS SIGNED)" + sqliteNowUS = "CAST(ROUND((julianday('now') - 2440587.5) * 86400000000.0) AS INTEGER)" +) + +// dbClockOverrideUS pins the database clock for tests; 0 means the real +// clock. Only tests in this package set it. +var dbClockOverrideUS atomic.Int64 + +// dbNowUSSQL returns the SQL expression for "now" in epoch microseconds. +func (d *deps) dbNowUSSQL() string { + if v := dbClockOverrideUS.Load(); v != 0 { + return strconv.FormatInt(v, 10) + } + if d.isMySQL() { + return mysqlNowUS + } + return sqliteNowUS +} + +// nowUSExpr is dbNowUSSQL as a gorm expression, optionally offset. +func (d *deps) nowUSExpr(offsetUS int64) clause.Expr { + if offsetUS == 0 { + return gorm.Expr(d.dbNowUSSQL()) + } + return gorm.Expr("("+d.dbNowUSSQL()+" + ?)", offsetUS) +} + +// dbNowUS reads the database clock. +func (d *deps) dbNowUS(tx *gorm.DB) (int64, error) { + var now int64 + err := tx.Raw("SELECT " + d.dbNowUSSQL()).Scan(&now).Error + return now, err +} + +// retryBotDeadlock reruns a transaction that MySQL rolled back as a deadlock +// victim (error 1213), for example two creates locking the same missing index +// gap. Only that explicit error is retried: the whole transaction was undone. +func retryBotDeadlock(transaction func() error) error { + var err error + for attempt := 0; attempt < 6; attempt++ { + err = transaction() + var sqlErr *mysqldriver.MySQLError + if !errors.As(err, &sqlErr) || sqlErr.Number != 1213 || attempt == 5 { + return err + } + time.Sleep(time.Duration(1< 0", bot.IdentityScope(), bot.ID).Order("updated_at DESC").Limit(500).Find(&rows).Error; err != nil { + return nil, err + } + out := make([]domain.BotChat, 0, len(rows)) + for _, row := range rows { + name := MaskBotSender(row.SenderID) + if user, ok := r.users.User(row.UserID); ok { + label := user.ChineseName + if label == "" { + label = user.LoginName + } else if user.LoginName != "" { + label += "(" + user.LoginName + ")" + } + name = label + " · " + name + } + out = append(out, domain.BotChat{ID: row.SenderID, Name: name, Type: "single", VerifiedAt: row.TargetVerifiedAt}) + } + return out, nil +} + +// BotPushTarget returns the active binding of a target sender of bot. +func (r *NotificationsRepo) BotPushTarget(bot domain.NotificationBot, sender string) (domain.BotUserBinding, error) { + binding, err := r.users.BotUserBinding(bot.IdentityScope(), sender) + if err != nil || binding.ConfigID != bot.ID { + return binding, ErrNotificationTargetUnverified + } + return binding, nil +} + +// MarkBotPushTargetVerified records a successful trial send. The binding +// must still belong to the same platform user (revocation clears it). +func (r *NotificationsRepo) MarkBotPushTargetVerified(bot domain.NotificationBot, sender string, userID int64, at time.Time) error { + result := r.DB.Model(&domain.BotUserBinding{}).Where("id = ? AND config_id = ? AND user_id = ?", botIdentityID(bot.IdentityScope(), sender), bot.ID, userID).Update("target_verified_at", at) + if result.Error != nil { + return result.Error + } + if result.RowsAffected != 1 { + return ErrNotificationTargetUnverified + } + return nil +} + +// BeginNotificationPush is the first phase of a proactive send. It gives the +// claimed delivery a send key (first generation) and stamps the start time +// once per generation. It returns the updated row and how long ago (database +// clock) the generation started; resumed is true when an earlier attempt of +// this generation may already have reached the platform. +func (r *NotificationsRepo) BeginNotificationPush(item domain.NotificationDelivery) (out domain.NotificationDelivery, elapsedUS int64, resumed bool, err error) { + err = r.DB.Transaction(func(tx *gorm.DB) error { + var row domain.NotificationDelivery + if err := r.locking(tx).First(&row, "id = ? AND status = ? AND lease_token = ?", item.ID, "sending", item.LeaseToken).Error; err != nil { + return err + } + now, err := r.dbNowUS(tx) + if err != nil { + return err + } + updates := map[string]any{} + if row.SendKey == "" { + row.SendKey = randomHex(16) + updates["send_key"] = row.SendKey + } + if row.DeliveryGeneration == 0 { + row.DeliveryGeneration = 1 + updates["delivery_generation"] = 1 + } + resumed = row.SendStartedAtUS > 0 + if !resumed { + row.SendStartedAtUS = now + updates["send_started_at_us"] = now + } + elapsedUS = now - row.SendStartedAtUS + out = row + if len(updates) == 0 { + return nil + } + return tx.Model(&domain.NotificationDelivery{}).Where("id = ? AND lease_token = ?", row.ID, row.LeaseToken).Updates(updates).Error + }) + return out, elapsedUS, resumed, err +} + +// NotificationPushResult is the second phase: status, next attempt and the +// evidence of a proactive send. KeepStarted keeps the generation's start +// time (unknown outcome retried with the same key); otherwise a retry starts +// clean because the platform reported nothing was sent. +type NotificationPushResult struct { + Status string + Detail string + Next time.Time + PlatformMessageID string + KeepStarted bool +} + +func (r *NotificationsRepo) FinishNotificationPush(item domain.NotificationDelivery, res NotificationPushResult) error { + updates := map[string]any{"status": res.Status, "last_error": truncateBotDetail(res.Detail), "next_attempt_at": res.Next, "lease_until": nil, "lease_token": ""} + if res.Status == "sent" { + updates["sent_at"] = time.Now().UTC() + updates["receipt_source"] = "platform" + updates["platform_message_id"] = res.PlatformMessageID + } + if res.Status == "pending" && !res.KeepStarted { + updates["send_started_at_us"] = 0 + } + return r.DB.Model(&domain.NotificationDelivery{}).Where("id = ? AND status = ? AND lease_token = ?", item.ID, "sending", item.LeaseToken).Updates(updates).Error +} + +// ConfirmNotificationReceived records an administrator's check that a +// delivery with an unknown outcome was in fact received. +func (r *NotificationsRepo) ConfirmNotificationReceived(projectID, id string, actor int64) error { + now := time.Now().UTC() + result := r.DB.Model(&domain.NotificationDelivery{}).Where("id = ? AND project_id = ? AND status = ?", id, projectID, "send_unknown").Updates(map[string]any{"status": "sent", "sent_at": now, "receipt_source": "manual", "confirmed_by": actor, "confirmed_at": now, "last_error": "管理员已确认收到"}) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil +} + +// notificationPendingStatuses are the non-terminal states affected by a +// target switch. +var notificationPendingStatuses = []string{"pending", "awaiting_authorization"} + +// PendingNotificationsForOtherTargets counts queued deliveries of bot whose +// frozen target differs from target. +func (r *NotificationsRepo) PendingNotificationsForOtherTargets(botID, target string) (int64, error) { + var n int64 + err := r.DB.Model(&domain.NotificationDelivery{}).Where("bot_id = ? AND status IN ? AND target_id <> ?", botID, notificationPendingStatuses, target).Count(&n).Error + return n, err +} + +// SwitchNotificationTarget applies the administrator's explicit choice for +// queued deliveries of an old target: "cancel" stops them, "migrate" +// re-targets them (a new generation and key, nothing was sent to the new +// target yet). Rows being sent right now are left alone. +func (r *NotificationsRepo) SwitchNotificationTarget(botID string, target domain.BotChat, action string) (int64, error) { + q := r.DB.Model(&domain.NotificationDelivery{}).Where("bot_id = ? AND status IN ? AND target_id <> ?", botID, notificationPendingStatuses, target.ID) + var result *gorm.DB + switch action { + case "cancel": + result = q.Updates(map[string]any{"status": "cancelled", "last_error": "接收目标已切换,管理员取消了旧目标的待发通知"}) + case "migrate": + result = q.Updates(map[string]any{"target_id": target.ID, "target_name": target.Name, "target_type": target.Type, "send_key": "", "delivery_generation": gorm.Expr("delivery_generation + 1"), "send_started_at_us": 0, "last_error": "接收目标已切换,管理员选择改发到新目标"}) + default: + return 0, errors.New("unknown target switch action") + } + return result.RowsAffected, result.Error +} diff --git a/internal/repo/notifications.go b/internal/repo/notifications.go index 78d519f99..1d8d01f31 100644 --- a/internal/repo/notifications.go +++ b/internal/repo/notifications.go @@ -3,6 +3,7 @@ package repo import ( "agentflow/internal/domain" "encoding/json" + "errors" "fmt" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -17,85 +18,382 @@ type NotificationsRepo struct { issues *IssuesRepo interactions *BotInteractionsRepo } + +// notificationBotRow keeps the Bot entity in a JSON payload. Channel, app +// identity, tenant, receive slot, enable flags and versions are physical +// columns: lease fencing and uniqueness read only these columns, and they are +// written in the same statement as the payload. type notificationBotRow struct { - ID string `gorm:"primaryKey;size:191"` - ProjectID string `gorm:"size:191;index"` - Payload []byte `gorm:"type:json"` + ID string `gorm:"primaryKey;size:191"` + ProjectID string `gorm:"size:191;index"` + Payload []byte `gorm:"type:json"` + Channel string `gorm:"size:16;not null;default:''"` + ExternalAppID string `gorm:"size:191;not null;default:''"` + TenantKey string `gorm:"size:191;not null;default:''"` + ReceiveSlot *string `gorm:"size:191;uniqueIndex:uq_notification_bots_receive_slot"` + Enabled bool `gorm:"not null;default:false"` + InteractionEnabled bool `gorm:"not null;default:false"` + ConfigVersion int64 `gorm:"not null;default:0"` + CredentialVersion int64 `gorm:"not null;default:0"` } func (notificationBotRow) TableName() string { return "notification_bots" } -func (r *NotificationsRepo) NotificationBots(projectID string) ([]domain.NotificationBot, error) { - var rows []notificationBotRow - if err := r.DB.Where("project_id = ?", projectID).Order("id").Find(&rows).Error; err != nil { - return nil, err + +var ( + // ErrBotConfigChanged: the expected configVersion no longer matches. + ErrBotConfigChanged = errors.New("config_changed") + // ErrBotImmutableField: channel, project or (for new channels) the app + // identity cannot change; create a new configuration instead. + ErrBotImmutableField = errors.New("机器人渠道、项目和应用标识创建后不可修改,请新建配置") +) + +func (row notificationBotRow) bot() (domain.NotificationBot, error) { + var b domain.NotificationBot + if err := json.Unmarshal(row.Payload, &b); err != nil { + return b, err } + // Versions are authoritative in physical columns. + b.ConfigVersion, b.CredentialVersion = row.ConfigVersion, row.CredentialVersion + return b, nil +} + +func botRow(b domain.NotificationBot) notificationBotRow { + var slot *string + if b.Enabled && b.InteractionEnabled { + v := b.ReceiveSlot() + slot = &v + } + tenant := "" + if b.TenantState == domain.BotTenantLocked { + tenant = b.TenantKey + } + b.CredentialFormat = "" + return notificationBotRow{ID: b.ID, ProjectID: b.ProjectID, Payload: mustJSON(b), Channel: b.ChannelKind(), ExternalAppID: b.BotID, TenantKey: tenant, ReceiveSlot: slot, Enabled: b.Enabled, InteractionEnabled: b.InteractionEnabled, ConfigVersion: b.ConfigVersion, CredentialVersion: b.CredentialVersion} +} + +func (r *NotificationsRepo) decodeBots(rows []notificationBotRow) ([]domain.NotificationBot, error) { + ids := make([]string, 0, len(rows)) + for _, row := range rows { + ids = append(ids, row.ID) + } + formats := r.users.botCredentialFormats(ids) out := make([]domain.NotificationBot, 0, len(rows)) for _, row := range rows { - var b domain.NotificationBot - if err := json.Unmarshal(row.Payload, &b); err != nil { + b, err := row.bot() + if err != nil { return nil, err } + b.CredentialFormat = formats[row.ID] + if b.Target != nil && b.ChannelKind() != domain.BotChannelWeCom { + // The verification lives on the target's binding: revocation + // or re-binding clears it, so read it instead of the snapshot. + target := *b.Target + target.VerifiedAt = nil + if binding, err := r.BotPushTarget(b, target.ID); err == nil { + target.VerifiedAt = binding.TargetVerifiedAt + } + b.Target = &target + } out = append(out, b) } return out, nil } + +func (r *NotificationsRepo) NotificationBots(projectID string) ([]domain.NotificationBot, error) { + var rows []notificationBotRow + if err := r.DB.Where("project_id = ?", projectID).Order("id").Find(&rows).Error; err != nil { + return nil, err + } + return r.decodeBots(rows) +} func (r *NotificationsRepo) NotificationBot(id string) (domain.NotificationBot, error) { var row notificationBotRow if err := r.DB.Where("id = ?", id).First(&row).Error; err != nil { return domain.NotificationBot{}, err } - var b domain.NotificationBot - err := json.Unmarshal(row.Payload, &b) - return b, err + bots, err := r.decodeBots([]notificationBotRow{row}) + if err != nil { + return domain.NotificationBot{}, err + } + return bots[0], nil } -// SaveNotificationBot preserves creation metadata and atomically replaces credentials when provided. +// SaveNotificationBot saves without an expected version (internal callers +// and tests). HTTP writers use SaveNotificationBotChecked. func (r *NotificationsRepo) SaveNotificationBot(b domain.NotificationBot, secret string) error { + _, err := r.SaveNotificationBotChecked(b, secret, nil) + return err +} + +// SaveNotificationBotChecked preserves creation metadata and tenant state, +// enforces immutable fields and the expected configVersion, bumps versions, +// keeps one receiving configuration per application, and atomically replaces +// the sealed credential when a secret is provided. +func (r *NotificationsRepo) SaveNotificationBotChecked(b domain.NotificationBot, secret string, expectedVersion *int64) (domain.NotificationBot, error) { + input := b + err := retryBotDeadlock(func() error { b = input; return r.saveNotificationBot(&b, secret, expectedVersion) }) + if err != nil { + return domain.NotificationBot{}, err + } + return b, nil +} + +func (r *NotificationsRepo) saveNotificationBot(out *domain.NotificationBot, secret string, expectedVersion *int64) error { + b := *out + defer func() { *out = b }() return r.DB.Transaction(func(tx *gorm.DB) error { var old notificationBotRow err := r.locking(tx).Where("id = ?", b.ID).First(&old).Error if err == nil { - var prior domain.NotificationBot - if err = json.Unmarshal(old.Payload, &prior); err != nil { + prior, err := old.bot() + if err != nil { return err } + if expectedVersion != nil && *expectedVersion != old.ConfigVersion { + return ErrBotConfigChanged + } + if prior.ChannelKind() != b.ChannelKind() || (prior.ChannelKind() != domain.BotChannelWeCom && prior.BotID != b.BotID) { + return ErrBotImmutableField + } b.CreatedAt = prior.CreatedAt b.OwnerID = prior.OwnerID b.ProjectID = prior.ProjectID + b.Channel = prior.Channel + expected, expectedBy := b.TenantExpected, b.TenantExpectedBy + b.TenantKey, b.TenantName, b.TenantState, b.TenantCandidate = prior.TenantKey, prior.TenantName, prior.TenantState, prior.TenantCandidate + b.TenantConfirmedBy, b.TenantConfirmedAt = prior.TenantConfirmedBy, prior.TenantConfirmedAt + b.TenantExpected, b.TenantExpectedBy = prior.TenantExpected, prior.TenantExpectedBy + // An administrator may correct the expected enterprise (for + // example after a conflict) until the scope is locked; the + // next trusted event is then checked afresh. + if expected != "" && expected != prior.TenantExpected && prior.TenantState != domain.BotTenantLocked { + b.TenantExpected, b.TenantExpectedBy = expected, expectedBy + b.TenantCandidate, b.TenantState = nil, domain.BotTenantAwaiting + } + b.ConfigVersion = old.ConfigVersion + 1 + b.CredentialVersion = old.CredentialVersion + if prior.Enabled && prior.InteractionEnabled && !(b.Enabled && b.InteractionEnabled) { + if err := r.interactions.stopBotInteractions(tx, b.ID, prior.LeaseKey(), false); err != nil { + return err + } + } + if prior.Enabled && !b.Enabled { + if err := r.stopNotificationDeliveries(tx, b.ID, false); err != nil { + return err + } + } + if !(b.Enabled && b.InteractionEnabled) || b.EffectiveWriteApproval() != domain.BotWriteConfirm { + if err := cancelBotDraftsWhere(tx, "机器人已停用对话或不再使用网页确认", "config_id = ?", b.ID); err != nil { + return err + } + } } else if err != gorm.ErrRecordNotFound { return err + } else { + b.ConfigVersion = 1 + b.CredentialVersion = 0 + if b.ChannelKind() != domain.BotChannelWeCom && b.TenantState == "" { + b.TenantState = domain.BotTenantAwaiting + } + } + if secret != "" { + b.CredentialVersion++ } if b.CreatedAt.IsZero() { b.CreatedAt = time.Now().UTC() } if b.Enabled && b.InteractionEnabled { - if err := r.interactions.lockBotDefinition(tx, b.BotID); err != nil { + // Serializes registering the same application; the receiver + // later claims this connection row. + if err := r.interactions.lockBotDefinition(tx, b.LeaseKey()); err != nil { return err } - var peers []notificationBotRow - if err := tx.Where("id <> ?", b.ID).Find(&peers).Error; err != nil { + var taken int64 + if err := tx.Model(¬ificationBotRow{}).Where("receive_slot = ? AND id <> ?", b.ReceiveSlot(), b.ID).Count(&taken).Error; err != nil { return err } - for _, peer := range peers { - var other domain.NotificationBot - if err := json.Unmarshal(peer.Payload, &other); err != nil { - return err - } - if other.Enabled && other.InteractionEnabled && other.BotID == b.BotID { - return ErrInteractiveBotAlreadyExists - } + if taken > 0 || r.legacyInteractivePeer(tx, b) { + return ErrInteractiveBotAlreadyExists } } b.UpdatedAt = time.Now().UTC() - if err := tx.Clauses(clause.OnConflict{UpdateAll: true}).Create(¬ificationBotRow{ID: b.ID, ProjectID: b.ProjectID, Payload: mustJSON(b)}).Error; err != nil { + if err := tx.Clauses(clause.OnConflict{UpdateAll: true}).Create(ptr(botRow(b))).Error; err != nil { + if errors.Is(err, gorm.ErrDuplicatedKey) { + return ErrInteractiveBotAlreadyExists + } return err } if secret != "" { - return r.users.putBotCredential(tx, b.ID, secret) + return r.users.putBotCredential(tx, b.ID, b.ChannelKind(), secret) } return nil }) } + +// legacyInteractivePeer covers rows not yet backfilled by migration 036 +// (receive_slot NULL, config_version 0) so the uniqueness rule holds during +// the upgrade window as well. +func (r *NotificationsRepo) legacyInteractivePeer(tx *gorm.DB, b domain.NotificationBot) bool { + var peers []notificationBotRow + tx.Where("id <> ? AND config_version = 0", b.ID).Find(&peers) + for _, peer := range peers { + other, err := peer.bot() + if err == nil && other.Enabled && other.InteractionEnabled && other.ReceiveSlot() == b.ReceiveSlot() { + return true + } + } + return false +} + +func ptr[T any](v T) *T { return &v } + +// ErrBotTenantCandidate: the confirmation does not match the recorded +// candidate, its credential version, or the current state. +var ErrBotTenantCandidate = errors.New("tenant_candidate_mismatch") + +// RecordBotTenantCandidate stores the enterprise scope seen on a trusted +// event of an unlocked configuration. It never changes config_version, so an +// administrator page holding the version stays valid and the receiver does +// not reconnect. A different second candidate moves the state to conflict. +func (r *NotificationsRepo) RecordBotTenantCandidate(id, tenantKey, eventID string, credentialVersion int64) (string, error) { + state := "" + err := r.DB.Transaction(func(tx *gorm.DB) error { + var row notificationBotRow + if err := r.locking(tx).Where("id = ?", id).First(&row).Error; err != nil { + return err + } + b, err := row.bot() + if err != nil { + return err + } + state = b.TenantState + if b.TenantState != domain.BotTenantLocked && b.TenantExpected != "" { + return r.checkExpectedTenant(tx, row, b, tenantKey, eventID, credentialVersion, &state) + } + switch { + case b.TenantState == domain.BotTenantLocked: + return nil + case b.TenantCandidate == nil || b.TenantCandidate.CredentialVersion != credentialVersion: + b.TenantCandidate = &domain.TenantCandidate{TenantKey: tenantKey, EventID: truncateBotDetail(eventID), ReceivedAt: time.Now().UTC(), CredentialVersion: credentialVersion} + b.TenantState = domain.BotTenantAwaiting + case b.TenantCandidate.TenantKey == tenantKey: + return nil + default: + b.TenantCandidate.Conflict = tenantKey + b.TenantState = domain.BotTenantConflict + } + state = b.TenantState + b.CredentialFormat = "" + return tx.Model(¬ificationBotRow{}).Where("id = ?", id).Update("payload", mustJSON(b)).Error + }) + return state, err +} + +// checkExpectedTenant handles a configuration whose enterprise was entered by +// an administrator (DingTalk CorpId): a trusted event from that enterprise +// locks the scope at once, attributed to the administrator who entered it, +// and bumps config_version (not the connection fingerprint); an event from +// another enterprise records a conflict and is not processed. +func (r *NotificationsRepo) checkExpectedTenant(tx *gorm.DB, row notificationBotRow, b domain.NotificationBot, tenantKey, eventID string, credentialVersion int64, state *string) error { + if b.TenantState == domain.BotTenantConflict { + // Two enterprises were seen; only an administrator correction or + // rediscovery reopens the check. + return nil + } + at := time.Now().UTC() + b.CredentialFormat = "" + if tenantKey == b.TenantExpected { + b.TenantCandidate = &domain.TenantCandidate{TenantKey: tenantKey, EventID: truncateBotDetail(eventID), ReceivedAt: at, CredentialVersion: credentialVersion} + b.TenantKey, b.TenantState, b.TenantConfirmedBy, b.TenantConfirmedAt = tenantKey, domain.BotTenantLocked, b.TenantExpectedBy, &at + b.ConfigVersion = row.ConfigVersion + 1 + *state = domain.BotTenantLocked + return tx.Model(¬ificationBotRow{}).Where("id = ?", row.ID).Updates(map[string]any{"payload": mustJSON(b), "tenant_key": tenantKey, "config_version": b.ConfigVersion}).Error + } + b.TenantCandidate = &domain.TenantCandidate{TenantKey: b.TenantExpected, EventID: truncateBotDetail(eventID), ReceivedAt: at, CredentialVersion: credentialVersion, Conflict: tenantKey} + b.TenantState = domain.BotTenantConflict + *state = domain.BotTenantConflict + return tx.Model(¬ificationBotRow{}).Where("id = ?", row.ID).Update("payload", mustJSON(b)).Error +} + +// ConfirmBotTenant locks the candidate enterprise scope. The candidate must +// match the posted value and come from the current credential version; the +// expected configVersion guards concurrent edits. Locking bumps +// config_version but not the connection fingerprint. +func (r *NotificationsRepo) ConfirmBotTenant(id, tenantKey string, expectedVersion int64, actor int64) (domain.NotificationBot, error) { + var b domain.NotificationBot + err := r.DB.Transaction(func(tx *gorm.DB) error { + var row notificationBotRow + if err := r.locking(tx).Where("id = ?", id).First(&row).Error; err != nil { + return err + } + var err error + if b, err = row.bot(); err != nil { + return err + } + if row.ConfigVersion != expectedVersion { + return ErrBotConfigChanged + } + c := b.TenantCandidate + if b.TenantState != domain.BotTenantAwaiting || c == nil || c.Conflict != "" || c.TenantKey != tenantKey || c.CredentialVersion != row.CredentialVersion { + return ErrBotTenantCandidate + } + at := time.Now().UTC() + b.TenantKey, b.TenantState, b.TenantConfirmedBy, b.TenantConfirmedAt = tenantKey, domain.BotTenantLocked, actor, &at + b.ConfigVersion = row.ConfigVersion + 1 + b.UpdatedAt = at + return tx.Model(¬ificationBotRow{}).Where("id = ?", id).Updates(map[string]any{"payload": mustJSON(botPayload(b)), "tenant_key": tenantKey, "config_version": b.ConfigVersion}).Error + }) + return b, err +} + +// SetBotTenantName stores the display-only enterprise name. +func (r *NotificationsRepo) SetBotTenantName(id, name string) error { + return r.DB.Transaction(func(tx *gorm.DB) error { + var row notificationBotRow + if err := r.locking(tx).Where("id = ?", id).First(&row).Error; err != nil { + return err + } + b, err := row.bot() + if err != nil { + return err + } + b.TenantName = truncateBotDetail(name) + return tx.Model(¬ificationBotRow{}).Where("id = ?", id).Update("payload", mustJSON(botPayload(b))).Error + }) +} + +// RediscoverBotTenant clears an unlocked candidate (for example after a +// conflict) so the next trusted event is recorded afresh. +func (r *NotificationsRepo) RediscoverBotTenant(id string, expectedVersion int64) (domain.NotificationBot, error) { + var b domain.NotificationBot + err := r.DB.Transaction(func(tx *gorm.DB) error { + var row notificationBotRow + if err := r.locking(tx).Where("id = ?", id).First(&row).Error; err != nil { + return err + } + var err error + if b, err = row.bot(); err != nil { + return err + } + if row.ConfigVersion != expectedVersion { + return ErrBotConfigChanged + } + if b.TenantState == domain.BotTenantLocked || b.ChannelKind() == domain.BotChannelWeCom { + return ErrBotTenantCandidate + } + b.TenantCandidate, b.TenantState = nil, domain.BotTenantAwaiting + b.ConfigVersion = row.ConfigVersion + 1 + return tx.Model(¬ificationBotRow{}).Where("id = ?", id).Updates(map[string]any{"payload": mustJSON(botPayload(b)), "config_version": b.ConfigVersion}).Error + }) + return b, err +} + +// botPayload is the JSON payload of b without read-only derived fields. +func botPayload(b domain.NotificationBot) domain.NotificationBot { + b.CredentialFormat = "" + return b +} + func (r *NotificationsRepo) SetNotificationBotHealth(id, health string) error { return r.DB.Transaction(func(tx *gorm.DB) error { var row notificationBotRow @@ -109,20 +407,62 @@ func (r *NotificationsRepo) SetNotificationBotHealth(id, health string) error { now := time.Now().UTC() b.Health = health b.LastCheckedAt = &now + b.CredentialFormat = "" return tx.Model(&row).Update("payload", mustJSON(b)).Error }) } + +// DeleteNotificationBot removes the configuration and its credential, and +// stops its work in the lock order configuration -> connection -> +// interactions/deliveries: unsent work is cancelled; anything in flight +// becomes reply_unknown / send_unknown for manual review. func (r *NotificationsRepo) DeleteNotificationBot(id string) error { + return retryBotDeadlock(func() error { return r.deleteNotificationBot(id) }) +} + +func (r *NotificationsRepo) deleteNotificationBot(id string) error { return r.DB.Transaction(func(tx *gorm.DB) error { - if err := tx.Where("id = ?", id).Delete(¬ificationBotRow{}).Error; err != nil { + var row notificationBotRow + if err := r.locking(tx).Where("id = ?", id).First(&row).Error; err != nil { + return err + } + b, err := row.bot() + if err != nil { + return err + } + if err := r.interactions.stopBotInteractions(tx, id, b.LeaseKey(), true); err != nil { + return err + } + if err := r.stopNotificationDeliveries(tx, id, true); err != nil { + return err + } + if err := r.users.revokeBotConfigIdentities(tx, b); err != nil { return err } - if err := r.users.deleteBotCredential(tx, id); err != nil { + if err := tx.Where("id = ?", id).Delete(¬ificationBotRow{}).Error; err != nil { return err } - return tx.Model(&domain.NotificationDelivery{}).Where("bot_id = ? AND status <> ?", id, "sent").Updates(map[string]any{"status": "cancelled", "lease_token": "", "lease_until": nil}).Error + return r.users.deleteBotCredential(tx, id) }) } + +// stopNotificationDeliveries: in-flight sends become send_unknown; on delete +// every other unsent delivery is cancelled. Disable leaves pending work for +// the worker, which cancels it when it sees the disabled Bot. +func (r *NotificationsRepo) stopNotificationDeliveries(tx *gorm.DB, botID string, deleted bool) error { + reason := "机器人在发送过程中被停用,送达结果待核对" + if deleted { + reason = "机器人在发送过程中被删除,送达结果待核对" + } + if err := tx.Model(&domain.NotificationDelivery{}).Where("bot_id = ? AND status = ?", botID, "sending").Updates(map[string]any{"status": "send_unknown", "last_error": reason, "lease_token": "", "lease_until": nil}).Error; err != nil { + return err + } + if !deleted { + return nil + } + return tx.Model(&domain.NotificationDelivery{}).Where("bot_id = ? AND status IN ?", botID, []string{"pending", "failed", "awaiting_authorization", "target_unavailable"}).Updates(map[string]any{"status": "cancelled", "last_error": "机器人已删除", "lease_token": "", "lease_until": nil}).Error +} + func (r *NotificationsRepo) NotificationDeliveries(projectID string) ([]domain.NotificationDelivery, error) { rows := []domain.NotificationDelivery{} err := r.DB.Where("project_id = ?", projectID).Order("created_at desc").Limit(100).Find(&rows).Error @@ -168,7 +508,7 @@ func (r *NotificationsRepo) FinishNotification(item domain.NotificationDelivery, return r.DB.Model(&domain.NotificationDelivery{}).Where("id = ? AND status = ? AND lease_token = ?", item.ID, "sending", item.LeaseToken).Updates(updates).Error } func (r *NotificationsRepo) RetryNotification(projectID, id string) error { - result := r.DB.Model(&domain.NotificationDelivery{}).Where("id = ? AND project_id = ? AND (content_source IS NULL OR content_source <> 'invalid') AND status IN ?", id, projectID, []string{"failed", "awaiting_authorization", "target_unavailable"}).Updates(map[string]any{"status": "pending", "attempts": 0, "next_attempt_at": time.Now().UTC(), "last_error": ""}) + result := r.DB.Model(&domain.NotificationDelivery{}).Where("id = ? AND project_id = ? AND (content_source IS NULL OR content_source NOT IN ('invalid', 'purged')) AND status IN ?", id, projectID, []string{"failed", "awaiting_authorization", "target_unavailable", "send_unknown"}).Updates(map[string]any{"status": "pending", "attempts": 0, "next_attempt_at": time.Now().UTC(), "last_error": "", "send_key": "", "delivery_generation": gorm.Expr("delivery_generation + 1"), "send_started_at_us": 0}) if result.Error != nil { return result.Error } @@ -406,22 +746,23 @@ func notificationTrigger(eventType string) string { } } +// InteractiveNotificationBots lists receiving configurations by the physical +// columns that lease fencing also reads. Rows not yet backfilled by +// migration 036 are skipped; see UnmigratedNotificationBots. func (r *NotificationsRepo) InteractiveNotificationBots() ([]domain.NotificationBot, error) { var rows []notificationBotRow - if err := r.DB.Order("id").Find(&rows).Error; err != nil { + if err := r.DB.Where("enabled = ? AND interaction_enabled = ? AND config_version > 0", true, true).Order("id").Find(&rows).Error; err != nil { return nil, err } - out := []domain.NotificationBot{} - for _, row := range rows { - var b domain.NotificationBot - if err := json.Unmarshal(row.Payload, &b); err != nil { - return nil, err - } - if b.Enabled && b.InteractionEnabled { - out = append(out, b) - } - } - return out, nil + return r.decodeBots(rows) +} + +// UnmigratedNotificationBots counts configurations whose physical columns +// were never backfilled (config_version = 0). +func (r *NotificationsRepo) UnmigratedNotificationBots() (int64, error) { + var n int64 + err := r.DB.Model(¬ificationBotRow{}).Where("config_version = 0").Count(&n).Error + return n, err } func (r *NotificationsRepo) NotificationDelivery(id string) (domain.NotificationDelivery, error) { diff --git a/internal/repo/notifications_test.go b/internal/repo/notifications_test.go index 7b055eaba..3c6c4a66e 100644 --- a/internal/repo/notifications_test.go +++ b/internal/repo/notifications_test.go @@ -16,6 +16,11 @@ func notificationFixture(t *testing.T, s *Store, suffix string) (domain.Notifica if err := s.PutProject(domain.Project{ID: bot.ProjectID, Name: "研发项目"}); err != nil { t.Fatal(err) } + // Stores opened with Open (MySQL tests) have no keyring until the + // process installs one; secrets are sealed, so tests install the test key. + if s.credentialKeyring() == nil { + s.SetCredentialKeyring(testCredentialKeyring) + } if err := s.SaveNotificationBot(bot, "ciphertext-not-a-real-secret"); err != nil { t.Fatal(err) } diff --git a/internal/repo/opentest.go b/internal/repo/opentest.go index b80a2d22d..bc7821469 100644 --- a/internal/repo/opentest.go +++ b/internal/repo/opentest.go @@ -7,10 +7,12 @@ package repo // SQL scripts under migrations/, applied manually. import ( + "encoding/base64" "fmt" "sync/atomic" "time" + "agentflow/internal/credcrypt" "agentflow/internal/domain" "github.com/glebarez/sqlite" @@ -18,6 +20,16 @@ import ( "gorm.io/gorm/logger" ) +// testCredentialKeyring seals Bot credentials in test stores. The key is a +// fixed public test vector and never protects real data. +var testCredentialKeyring = func() *credcrypt.Keyring { + k, err := credcrypt.Parse("test:" + base64.StdEncoding.EncodeToString([]byte("agentflow-test-credential-key-32"))) + if err != nil { + panic(err) + } + return k +}() + // testStoreSeq gives every test store a unique named in-memory sqlite database. var testStoreSeq atomic.Uint64 @@ -40,6 +52,9 @@ func OpenTest() (*Store, error) { sqlDB.SetMaxOpenConns(1) } s := newStore(db) + // A fixed test-only keyring so Bot credentials are sealed exactly as in + // production; tests that exercise a missing key install nil explicitly. + s.SetCredentialKeyring(testCredentialKeyring) if err := db.AutoMigrate(allModels()...); err != nil { s.Close() return nil, fmt.Errorf("sqlite automigrate: %w", err) diff --git a/internal/repo/repo.go b/internal/repo/repo.go index e7367b12f..af45bcc13 100644 --- a/internal/repo/repo.go +++ b/internal/repo/repo.go @@ -13,6 +13,7 @@ import ( "sync" "time" + "agentflow/internal/credcrypt" "agentflow/internal/domain" "gorm.io/driver/mysql" @@ -112,6 +113,9 @@ type TasksRepo struct { // UsersRepo owns users and their saved credentials. type UsersRepo struct { *deps + // keyring seals Bot credentials; installed by SetCredentialKeyring. + keyringMu sync.RWMutex + keyring *credcrypt.Keyring } // TokensRepo owns API tokens. diff --git a/migrations/036_enterprise_im_channels.sql b/migrations/036_enterprise_im_channels.sql new file mode 100644 index 000000000..2e21a37da --- /dev/null +++ b/migrations/036_enterprise_im_channels.sql @@ -0,0 +1,74 @@ +-- 036 Enterprise IM channels (docs/proposals/enterprise-im.md §5.4). +-- Apply manually in the Bot maintenance window, after stopping every old +-- replica and starting the new version once with +-- AGENTFLOW_BOT_WORKERS_ENABLED=false (its startup incremental DDL adds the +-- columns, indexes and the bot_native_sessions table listed in section 1). +-- Run the sections in order; stop at the first error. Identifier equality +-- uses BINARY so tables with different collations compare exactly. + +-- Section 1. Schema check: every row below must report present = 1. If any +-- is 0, the new version has not started yet; start it (workers disabled) +-- instead of hand-writing DDL. +SELECT t.table_name, t.column_name, + (SELECT COUNT(*) FROM information_schema.columns c + WHERE c.table_schema = DATABASE() AND BINARY c.table_name = BINARY t.table_name AND BINARY c.column_name = BINARY t.column_name) AS present +FROM ( + SELECT 'notification_bots' AS table_name, 'channel' AS column_name UNION ALL + SELECT 'notification_bots', 'external_app_id' UNION ALL + SELECT 'notification_bots', 'tenant_key' UNION ALL + SELECT 'notification_bots', 'receive_slot' UNION ALL + SELECT 'notification_bots', 'enabled' UNION ALL + SELECT 'notification_bots', 'interaction_enabled' UNION ALL + SELECT 'notification_bots', 'config_version' UNION ALL + SELECT 'notification_bots', 'credential_version' UNION ALL + SELECT 'bot_connections', 'lease_until_us' UNION ALL + SELECT 'bot_connections', 'epoch' UNION ALL + SELECT 'bot_interactions', 'project_id' UNION ALL + SELECT 'bot_interactions', 'send_key' UNION ALL + SELECT 'bot_user_bindings', 'config_id' UNION ALL + SELECT 'bot_binding_challenges', 'config_id' UNION ALL + SELECT 'bot_native_sessions', 'native_session_id' +) t; + +-- Section 2. Read-only conflict check: enabled interactive configurations +-- that would share one receive slot. Resolve every row (disable or delete +-- the duplicate in the old payload) before section 3, otherwise the unique +-- index uq_notification_bots_receive_slot rejects the backfill. +SELECT COALESCE(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.channel')), ''), 'wecom') AS channel, + JSON_UNQUOTE(JSON_EXTRACT(payload, '$.botId')) AS external_app_id, + COUNT(*) AS enabled_receivers, GROUP_CONCAT(id ORDER BY id) AS config_ids +FROM notification_bots +WHERE JSON_UNQUOTE(JSON_EXTRACT(payload, '$.enabled')) = 'true' + AND JSON_UNQUOTE(JSON_EXTRACT(payload, '$.interactionEnabled')) = 'true' +GROUP BY 1, 2 +HAVING COUNT(*) > 1; + +-- Section 3. Backfill physical columns from the JSON payload. Rows already +-- written by the new version (config_version > 0) are left untouched, so the +-- section can be re-run. Legacy rows without a channel are WeCom. +START TRANSACTION; +UPDATE notification_bots +SET channel = COALESCE(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.channel')), ''), 'wecom'), + external_app_id = COALESCE(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.botId')), ''), + enabled = IF(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.enabled')) = 'true', 1, 0), + interaction_enabled = IF(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.interactionEnabled')) = 'true', 1, 0) +WHERE config_version = 0; +UPDATE notification_bots +SET receive_slot = IF(enabled = 1 AND interaction_enabled = 1, CONCAT(channel, ':', SHA2(external_app_id, 256)), NULL), + credential_version = IF(EXISTS (SELECT 1 FROM user_bot_credentials c WHERE BINARY c.bot_id = BINARY notification_bots.id), 1, 0), + config_version = 1 +WHERE config_version = 0; +COMMIT; + +-- Section 4. Interaction record project snapshot (record-page access after +-- a Bot is deleted). Re-runnable. +UPDATE bot_interactions i +JOIN notification_bots b ON BINARY b.id = BINARY i.bot_id +SET i.project_id = b.project_id +WHERE i.project_id IS NULL OR i.project_id = ''; + +-- Section 5. Verification: counts to record before and after the window. +SELECT channel, COUNT(*) AS configs, SUM(receive_slot IS NOT NULL) AS receivers, + SUM(config_version = 0) AS not_backfilled +FROM notification_bots GROUP BY channel; +SELECT COUNT(*) AS interactions_without_project FROM bot_interactions WHERE project_id IS NULL OR project_id = ''; diff --git a/migrations/037_bot_binding_ownership.sql b/migrations/037_bot_binding_ownership.sql new file mode 100644 index 000000000..54b9ae03c --- /dev/null +++ b/migrations/037_bot_binding_ownership.sql @@ -0,0 +1,47 @@ +-- 037 Bot binding ownership (docs/proposals/enterprise-im.md §5.4 step 4). +-- Run after 036; valid under ONLY_FULL_GROUP_BY (the MySQL 8.4 default). +-- WeCom bindings are scoped by the external Bot ID; this +-- script records the owning configuration where it is unambiguous and lists +-- the rest for manual review. New-channel bindings always carry config_id. + +-- Section 1. Report: binding scopes shared by several WeCom configurations. +-- These keep config_id empty; confirm the owner with the project and set it +-- by hand (UPDATE bot_user_bindings SET config_id = ... WHERE bot_id = ...). +SELECT MIN(b.external_app_id) AS bot_scope, COUNT(*) AS configs, GROUP_CONCAT(b.id ORDER BY b.id) AS config_ids, + (SELECT COUNT(*) FROM bot_user_bindings u WHERE BINARY u.bot_id = BINARY MIN(b.external_app_id) AND u.user_id > 0) AS bound_users +FROM notification_bots b +WHERE b.channel = 'wecom' +GROUP BY BINARY b.external_app_id +HAVING COUNT(*) > 1; + +-- Section 2. Report: orphan bindings whose scope matches no configuration. +SELECT u.id, u.bot_id AS bot_scope, u.user_id +FROM bot_user_bindings u +LEFT JOIN notification_bots b ON b.channel = 'wecom' AND BINARY b.external_app_id = BINARY u.bot_id +WHERE b.id IS NULL AND u.user_id > 0 AND (u.config_id IS NULL OR u.config_id = ''); + +-- Section 3. Backfill unambiguous WeCom ownership. Re-runnable. +START TRANSACTION; +UPDATE bot_user_bindings u +JOIN (SELECT MIN(external_app_id) AS external_app_id, MIN(id) AS config_id FROM notification_bots + WHERE channel = 'wecom' GROUP BY BINARY external_app_id HAVING COUNT(*) = 1) owner + ON BINARY owner.external_app_id = BINARY u.bot_id +SET u.config_id = owner.config_id, u.channel = 'wecom' +WHERE u.config_id IS NULL OR u.config_id = ''; +UPDATE bot_binding_challenges c +JOIN bot_user_bindings u ON BINARY u.id = BINARY c.binding_id +SET c.config_id = u.config_id, c.channel = u.channel +WHERE (c.config_id IS NULL OR c.config_id = '') AND u.config_id <> ''; +COMMIT; + +-- Section 4. Native CLI sessions of Bot turns before this release have no +-- mapping. List them as legacy_unmapped for manual review (stage H cleans +-- only mapped sessions); nothing is deleted here. +SELECT 'legacy_unmapped' AS state, i.id AS interaction_id, i.bot_id AS config_id, i.session_id AS bot_session_id, i.created_at +FROM bot_interactions i +LEFT JOIN bot_native_sessions m ON BINARY m.interaction_id = BINARY i.id +WHERE m.id IS NULL AND i.action = '' AND i.status IN ('replied', 'reply_failed') +ORDER BY i.created_at; + +-- Section 5. Verification. +SELECT COUNT(*) AS bindings, SUM(config_id IS NULL OR config_id = '') AS without_config FROM bot_user_bindings WHERE user_id > 0; diff --git a/scripts/configure.sh b/scripts/configure.sh index 165dbcda5..1dd1e3bb0 100755 --- a/scripts/configure.sh +++ b/scripts/configure.sh @@ -2,20 +2,26 @@ set -euo pipefail cd "$(dirname "$0")/.." python3 - <<'PY' -import os,secrets,pathlib,shlex +import os,secrets,pathlib,shlex,base64,datetime p=pathlib.Path('.env') data_dir=os.path.abspath(os.environ.get('LOOPFORGE_DATA_DIR','.data')) +def credential_keys(): + # AES-256 key sealing chat Bot secrets; keep it with the database backups. + return f"k{datetime.date.today():%Y%m}:{base64.b64encode(secrets.token_bytes(32)).decode()}" if p.exists(): text=p.read_text() + added=[] if 'LOOPFORGE_DATA_DIR=' not in text: with p.open('a') as f: f.write(f'\nLOOPFORGE_DATA_DIR={shlex.quote(data_dir)}\n') - print('Added LOOPFORGE_DATA_DIR to existing .env') - else: - print('Keeping existing .env') + added.append('LOOPFORGE_DATA_DIR') + if 'AGENTFLOW_CREDENTIAL_KEYS=' not in text: + with p.open('a') as f: f.write(f'\nAGENTFLOW_CREDENTIAL_KEYS={shlex.quote(credential_keys())}\n') + added.append('AGENTFLOW_CREDENTIAL_KEYS') + print('Added '+', '.join(added)+' to existing .env' if added else 'Keeping existing .env') else: password=secrets.token_hex(24) http_port=os.environ.get('AGENTFLOW_HTTP_PORT','8090');mysql_port=os.environ.get('LOOPFORGE_MYSQL_PORT','33315') - data={'LOOPFORGE_DB_PASSWORD':password,'LOOPFORGE_DB_ROOT_PASSWORD':secrets.token_hex(24),'LOOPFORGE_MYSQL_PORT':mysql_port,'COMPOSE_PROJECT_NAME':os.environ.get('COMPOSE_PROJECT_NAME','loopforge'),'AGENTFLOW_HTTP_PORT':http_port,'AGENTFLOW_MYSQL_DSN':f'loopforge:{password}@tcp(127.0.0.1:{mysql_port})/loopforge','AGENTFLOW_SESSION_SECRET':secrets.token_hex(32),'AGENTFLOW_AUTH_REQUIRED':'true','LOOPFORGE_DATA_DIR':data_dir,'LOOPFORGE_BIND':os.environ.get('LOOPFORGE_BIND','0.0.0.0'),'LOOPFORGE_PUBLIC_URL':os.environ.get('LOOPFORGE_PUBLIC_URL',''),'LOOPFORGE_IMAGE':os.environ.get('LOOPFORGE_IMAGE',''),'AGENTFLOW_GITHUB_CLIENT_ID':os.environ.get('AGENTFLOW_GITHUB_CLIENT_ID','')} + data={'LOOPFORGE_DB_PASSWORD':password,'LOOPFORGE_DB_ROOT_PASSWORD':secrets.token_hex(24),'LOOPFORGE_MYSQL_PORT':mysql_port,'COMPOSE_PROJECT_NAME':os.environ.get('COMPOSE_PROJECT_NAME','loopforge'),'AGENTFLOW_HTTP_PORT':http_port,'AGENTFLOW_MYSQL_DSN':f'loopforge:{password}@tcp(127.0.0.1:{mysql_port})/loopforge','AGENTFLOW_SESSION_SECRET':secrets.token_hex(32),'AGENTFLOW_CREDENTIAL_KEYS':credential_keys(),'AGENTFLOW_AUTH_REQUIRED':'true','LOOPFORGE_DATA_DIR':data_dir,'LOOPFORGE_BIND':os.environ.get('LOOPFORGE_BIND','0.0.0.0'),'LOOPFORGE_PUBLIC_URL':os.environ.get('LOOPFORGE_PUBLIC_URL',''),'LOOPFORGE_IMAGE':os.environ.get('LOOPFORGE_IMAGE',''),'AGENTFLOW_GITHUB_CLIENT_ID':os.environ.get('AGENTFLOW_GITHUB_CLIENT_ID','')} fd=os.open(str(p),os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600) with os.fdopen(fd,'w') as f: f.write(''.join(f'{k}={shlex.quote(v)}\n' for k,v in data.items())) print('Created .env with unique local secrets') diff --git a/scripts/im-sdk-probe/README.md b/scripts/im-sdk-probe/README.md new file mode 100644 index 000000000..c61017bcc --- /dev/null +++ b/scripts/im-sdk-probe/README.md @@ -0,0 +1,73 @@ +# IM SDK 诊断模块 + +本目录是独立 Go 模块(`agentflow/scripts/im-sdk-probe`),用来固定和验证企业 IM 方案评估过的第三方 SDK 版本,见 `docs/proposals/enterprise-im.md` 第 8 节。它不进入主模块的 `go.mod`;主模块只在适配器真正使用某个 SDK 时才增加依赖。 + +```bash +cd scripts/im-sdk-probe +go vet ./... && go test -count=1 -v ./... +go run golang.org/x/vuln/cmd/govulncheck@v1.9.0 ./... +``` + +升级任一 SDK 时,先在这里修改版本并重跑;结论变化时同步更新方案第 8 节。 + +## 飞书 `github.com/larksuite/oapi-sdk-go/v3@v3.13.0` + +`TestFeishuStartStopsOnContextCancel`:`ws.Client.Start(ctx)` 阻塞运行,context 取消后返回 `context canceled`。同一文件中的 `feishuSurface` 在编译期确认阶段 E 会用到的接口:消息接收事件头(`app_id`、`tenant_key`、`event_id`)、Message Reply/Create 的 `uuid`、`tenant/v2` 的 `Query`。 + +## 钉钉 `github.com/open-dingtalk/dingtalk-stream-sdk-go@v0.9.1` + +结论:**不使用 `StreamClient` 的连接循环**,阶段 F 在主模块中自行实现最小连接层。只复用接入点获取、`payload` 帧编解码和 `chatbot.BotCallbackDataModel` 事件模型。 + +### 崩溃复现 + +`TestDingTalkPongTimeoutThenFrame` 在子进程中运行 `TestDingTalkPongTimeoutChild`,并断言子进程以该 panic 退出。 + +1. 本地 HTTP 服务模拟 `POST /v1.0/gateway/connections/open`,返回指向本地 WebSocket 的 endpoint。 +2. 用 `WithOpenApiHost` 指向本地服务,`WithKeepAlive(3s)`、`WithAutoReconnect(false)`,注册机器人回调后 `Start`。 +3. 服务端吞掉 ping、不回 pong。3 秒后 SDK 发 ping,再过 5 秒判定 pong 超时,`processLoop` 返回。 +4. 服务端在第 10 秒推送一帧文本消息。 +5. 进程以 `panic: send on closed channel` 退出(exit status 2)。 + +单独查看调用栈: + +```bash +DT_CHILD=1 go test -count=1 -run TestDingTalkPongTimeoutChild -v ./... +``` + +```text +panic: send on closed channel + +goroutine 20 [running]: +github.com/open-dingtalk/dingtalk-stream-sdk-go/client.(*StreamClient).processLoop.func6() + $GOMODCACHE/github.com/open-dingtalk/dingtalk-stream-sdk-go@v0.9.1/client/client.go:165 +0x9b +created by github.com/open-dingtalk/dingtalk-stream-sdk-go/client.(*StreamClient).processLoop in goroutine 19 + $GOMODCACHE/github.com/open-dingtalk/dingtalk-stream-sdk-go@v0.9.1/client/client.go:156 +0x2c5 +``` + +### 源码位置(`client/client.go`) + +| 行 | 内容 | 问题 | +| --- | --- | --- | +| 147–149 | `defer close(closeChan)`、`close(pongChan)`、`close(readChan)` | `processLoop` 返回时关闭三个通道 | +| 156–168 | 读协程 `for { ReadMessage(); readChan <- message }` | 没有退出信号;连接未被关闭时继续读 | +| 182–197 | ping 后起协程,5 秒未收到 pong 就 `closeChan <- struct{}{}` | 超时只让主循环返回,不关闭 `cli.conn` | +| 198–199 | `case <-closeChan: return` | 主循环返回触发第 147–149 行的 close | +| 165 | `readChan <- message` | 连接仍然可读,下一帧写入已关闭通道,panic 发生在 SDK 协程里,`processLoop` 的 `recover` 捕获不到,进程退出 | + +同一路径还有:第 152 行 pong handler 和第 161、194 行向已关闭通道发送,同样会 panic。 + +### 其他不采用的原因 + +| 位置 | 问题 | +| --- | --- | +| `Start`(第 74 行起) | 内部 `dialer.Dial`,不接受 context,停止时可能卡在建连 | +| `reconnect`(第 275 行起) | `context.Background()` 无限重试,`Close` 后仍可能重连 | +| `chatbot/chatbot_replier.go` `ReplyMessage` | 默认重定向策略、固定 5 秒超时、200 响应不关闭 body | + +### 阶段 F 自行实现的要求 + +按官方 Stream 协议覆盖:接入点鉴权(`connections/open` 换 endpoint 和 ticket)、服务端 `SYSTEM/ping` 应答和客户端 WebSocket 心跳、context 取消、`SYSTEM/disconnect` 和断线后的退避重连、每个 `CALLBACK` 帧入库后的回执。测试覆盖 pong 超时后迟到的帧、关闭期间的帧、取消时的建连与重连,均要求进程不退出、连接循环按时结束并能恢复。 + +实现位置:`internal/botinteraction/channel_dingtalk.go`。单进程测试 `TestDingTalkStream*`(`-race` 连跑 3 轮)覆盖 ping/pong、回执 200/500、入库超时、`disconnect`、心跳超时后的迟到帧、关闭期间的帧、取消;多进程故障测试 `TestDingTalkFaultMySQL` 的 D3 在真实子进程中复现“网关停止回 pong 后再推迟到帧”,接收进程不退出并自动重连。 + +本探针在上游修复后会失败(不再复现 panic),届时重新评估是否改用 `StreamClient`。 diff --git a/scripts/im-sdk-probe/go.mod b/scripts/im-sdk-probe/go.mod new file mode 100644 index 000000000..75b833a93 --- /dev/null +++ b/scripts/im-sdk-probe/go.mod @@ -0,0 +1,14 @@ +module agentflow/scripts/im-sdk-probe + +go 1.26.11 + +require ( + github.com/gorilla/websocket v1.5.3 + github.com/larksuite/oapi-sdk-go/v3 v3.13.0 + github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1 +) + +require ( + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/uuid v1.3.0 // indirect +) diff --git a/scripts/im-sdk-probe/go.sum b/scripts/im-sdk-probe/go.sum new file mode 100644 index 000000000..ec49de9c8 --- /dev/null +++ b/scripts/im-sdk-probe/go.sum @@ -0,0 +1,47 @@ +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/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +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.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/larksuite/oapi-sdk-go/v3 v3.13.0 h1:v+x3V8a16HVXdcUEwj9LVwe+ku/K9WUy7Cp4WjQCVHo= +github.com/larksuite/oapi-sdk-go/v3 v3.13.0/go.mod h1:F1nwLfYBSKvD8mS/OFw90Gr+V4RIugPBR44m/ThA5jg= +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/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +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/scripts/im-sdk-probe/probe_test.go b/scripts/im-sdk-probe/probe_test.go new file mode 100644 index 000000000..368cce3c1 --- /dev/null +++ b/scripts/im-sdk-probe/probe_test.go @@ -0,0 +1,133 @@ +// Package imsdkprobe pins and exercises the Feishu and DingTalk SDK versions +// evaluated for enterprise IM channels (docs/proposals/enterprise-im.md §8). +// It is a separate module so the main go.mod only gains a dependency when an +// adapter actually uses it. Run: go test -count=1 -v ./... +package imsdkprobe + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "strings" + "testing" + "time" + + lark "github.com/larksuite/oapi-sdk-go/v3" + larkcore "github.com/larksuite/oapi-sdk-go/v3/core" + "github.com/larksuite/oapi-sdk-go/v3/event/dispatcher" + larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" + larkws "github.com/larksuite/oapi-sdk-go/v3/ws" + + dtbot "github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot" + dtclient "github.com/open-dingtalk/dingtalk-stream-sdk-go/client" + + "github.com/gorilla/websocket" +) + +// Compile-time use of the adapter surface Stage E needs. +func feishuSurface() { + api := lark.NewClient("app", "secret", lark.WithReqTimeout(10*time.Second)) + _ = larkim.NewReplyMessageReqBuilder().MessageId("om_x").Body(larkim.NewReplyMessageReqBodyBuilder().MsgType("text").Content(`{"text":"x"}`).Uuid("send-key").Build()).Build() + _ = larkim.NewCreateMessageReqBuilder().ReceiveIdType("open_id").Body(larkim.NewCreateMessageReqBodyBuilder().ReceiveId("ou").MsgType("text").Content(`{"text":"x"}`).Uuid("k").Build()).Build() + _ = api.Tenant.V2.Tenant.Query + _ = api.Im.V1.Message.Reply + d := dispatcher.NewEventDispatcher("", "").OnP2MessageReceiveV1(func(ctx context.Context, ev *larkim.P2MessageReceiveV1) error { + _ = ev.EventV2Base.Header.AppID + _ = ev.EventV2Base.Header.TenantKey + _ = ev.EventV2Base.Header.EventID + _ = *ev.Event.Message.MessageId + _ = *ev.Event.Sender.SenderId.OpenId + return nil + }) + _ = larkws.NewClient("app", "secret", larkws.WithEventHandler(d), larkws.WithLogLevel(larkcore.LogLevelError)) +} + +func TestFeishuStartStopsOnContextCancel(t *testing.T) { + feishuSurface() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(500) + })) + defer srv.Close() + d := dispatcher.NewEventDispatcher("", "") + cli := larkws.NewClient("app", "secret", larkws.WithEventHandler(d), larkws.WithDomain(srv.URL), larkws.WithLogLevel(larkcore.LogLevelError)) + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { done <- cli.Start(ctx) }() + time.Sleep(500 * time.Millisecond) + cancel() + select { + case err := <-done: + t.Logf("feishu Start returned after cancel: %v", err) + case <-time.After(5 * time.Second): + t.Fatal("feishu Start did not return after context cancel") + } +} + +// dingtalkServer returns an open API host whose endpoint points at a local +// websocket that never answers pings and later pushes a text frame. +func dingtalkServer(t *testing.T) *httptest.Server { + upgrader := websocket.Upgrader{} + mux := http.NewServeMux() + var srv *httptest.Server + mux.HandleFunc("/v1.0/gateway/connections/open", func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(map[string]string{"endpoint": "ws" + strings.TrimPrefix(srv.URL, "http") + "/ws", "ticket": "t"}) + }) + mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { + c, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + c.SetPingHandler(func(string) error { return nil }) // swallow pings: no pong + go func() { + for { + if _, _, err := c.ReadMessage(); err != nil { + return + } + } + }() + time.Sleep(10 * time.Second) // keepAlive 3s + pong timeout 5s + c.WriteMessage(websocket.TextMessage, []byte(`{"type":"SYSTEM","headers":{"topic":"ping","messageId":"m"},"data":"{}"}`)) + time.Sleep(2 * time.Second) + c.Close() + }) + srv = httptest.NewServer(mux) + return srv +} + +// Runs in a child process so an unrecovered SDK goroutine panic is observable. +func TestDingTalkPongTimeoutChild(t *testing.T) { + if os.Getenv("DT_CHILD") != "1" { + t.Skip("child only") + } + srv := dingtalkServer(t) + defer srv.Close() + cli := dtclient.NewStreamClient( + dtclient.WithAppCredential(dtclient.NewAppCredentialConfig("id", "secret")), + dtclient.WithOpenApiHost(srv.URL), + dtclient.WithAutoReconnect(false), + dtclient.WithKeepAlive(3*time.Second), + ) + cli.RegisterChatBotCallbackRouter(func(ctx context.Context, d *dtbot.BotCallbackDataModel) ([]byte, error) { return nil, nil }) + if err := cli.Start(context.Background()); err != nil { + t.Fatal(err) + } + time.Sleep(14 * time.Second) + cli.Close() +} + +// TestDingTalkPongTimeoutThenFrame documents the v0.9.1 defect behind the +// decision in §8.3: after a pong timeout, a late frame panics with "send on +// closed channel" in an SDK goroutine and terminates the host process. If +// this test fails after an SDK upgrade, re-evaluate using StreamClient. +func TestDingTalkPongTimeoutThenFrame(t *testing.T) { + cmd := exec.Command(os.Args[0], "-test.run=TestDingTalkPongTimeoutChild", "-test.v") + cmd.Env = append(os.Environ(), "DT_CHILD=1") + out, err := cmd.CombinedOutput() + if err == nil || !strings.Contains(string(out), "send on closed channel") { + t.Fatalf("known StreamClient defect no longer reproduces (exit=%v); re-evaluate docs/proposals/enterprise-im.md §8.3\n%s", err, out) + } + t.Logf("dingtalk v0.9.1 StreamClient crashed the process as documented (exit=%v)", err) +} diff --git a/scripts/test-install.py b/scripts/test-install.py index facc5f892..a9d8a04a8 100644 --- a/scripts/test-install.py +++ b/scripts/test-install.py @@ -91,6 +91,24 @@ def test_root_install_and_repeat_preserve_secrets(self): self.assertEqual((self.root / '.env').read_text(), secrets) self.assertEqual((self.root / '.env').stat().st_mode & 0o777, 0o600) + def test_credential_key_generated_once_and_added_to_existing_env(self): + import base64, re + self.assert_success(self.install()) + env = self.root / '.env' + match = re.search(r"^AGENTFLOW_CREDENTIAL_KEYS=k\d{6}:(\S+)$", env.read_text(), re.M) + self.assertIsNotNone(match, env.read_text()) + self.assertEqual(len(base64.b64decode(match.group(1))), 32) + # An installation from before chat Bots gains a key; an existing key is kept. + env.write_text(''.join(l + '\n' for l in env.read_text().splitlines() if not l.startswith('AGENTFLOW_CREDENTIAL_KEYS='))) + self.assert_success(self.install()) + first = env.read_text() + self.assertEqual(first.count('AGENTFLOW_CREDENTIAL_KEYS='), 1) + self.assert_success(self.install()) + self.assertEqual(env.read_text(), first) + compose = (ROOT / 'compose.yaml').read_text() + for name in ('AGENTFLOW_CREDENTIAL_KEYS', 'AGENTFLOW_PUBLIC_URL', 'AGENTFLOW_BOT_WORKERS_ENABLED', 'AGENTFLOW_BOT_BODY_RETENTION_DAYS', 'AGENTFLOW_BOT_RECORD_RETENTION_DAYS'): + self.assertIn(f' {name}: ${{{name}:-', compose) + def test_non_root_without_docker_group_uses_sudo_everywhere(self): self.assert_success(self.install(MOCK_USER='1000')) lines = (self.root / 'docker.log').read_text().splitlines() diff --git a/third_party/go/THIRD_PARTY_NOTICES.txt b/third_party/go/THIRD_PARTY_NOTICES.txt index 08d6efd82..2f508fa56 100644 --- a/third_party/go/THIRD_PARTY_NOTICES.txt +++ b/third_party/go/THIRD_PARTY_NOTICES.txt @@ -2429,6 +2429,62 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +============================================================================== +github.com/gogo/protobuf v1.3.2 + +--- AUTHORS --- +# This is the official list of GoGo authors for copyright purposes. +# This file is distinct from the CONTRIBUTORS file, which +# lists people. For example, employees are listed in CONTRIBUTORS, +# but not in AUTHORS, because the employer holds the copyright. + +# Names should be added to this file as one of +# Organization's name +# Individual's name +# Individual's name + +# Please keep the list sorted. + +Sendgrid, Inc +Vastech SA (PTY) LTD +Walter Schulze + +--- LICENSE --- +Copyright (c) 2013, The GoGo Authors. All rights reserved. + +Protocol Buffers for Go with Gadgets + +Go support for Protocol Buffers - Google's data interchange format + +Copyright 2010 The Go Authors. All rights reserved. +https://github.com/golang/protobuf + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ============================================================================== github.com/golang/protobuf v1.5.0 @@ -3106,6 +3162,58 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +============================================================================== +github.com/kisielk/errcheck v1.5.0 + +--- LICENSE --- +Copyright (c) 2013 Kamil Kisiel + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +============================================================================== +github.com/kisielk/gotool v1.0.0 + +--- LICENSE --- +Copyright (c) 2013 Kamil Kisiel + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + ============================================================================== github.com/klauspost/compress v1.20.3 @@ -3489,6 +3597,32 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +============================================================================== +github.com/larksuite/oapi-sdk-go/v3 v3.13.0 + +--- LICENSE --- +MIT License + +Copyright (c) 2020 Lark Technologies Pte. Ltd. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + ============================================================================== github.com/leodido/go-urn v1.4.0 @@ -3967,6 +4101,32 @@ github.com/modern-go/reflect2 v1.0.2 See the License for the specific language governing permissions and limitations under the License. +============================================================================== +github.com/open-dingtalk/dingtalk-stream-sdk-go v0.9.1 + +--- LICENSE --- +MIT License + +Copyright (c) 2023 钉钉开放平台团队 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + ============================================================================== github.com/pelletier/go-toml/v2 v2.2.4 @@ -4806,6 +4966,32 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +============================================================================== +github.com/yuin/goldmark v1.2.1 + +--- LICENSE --- +MIT License + +Copyright (c) 2019 Yusuke Inuzuka + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + ============================================================================== github.com/zalando/go-keyring v0.2.8 diff --git a/web/app/access.js b/web/app/access.js index 0a8530fe1..179502d83 100644 --- a/web/app/access.js +++ b/web/app/access.js @@ -148,7 +148,7 @@ async function revokePersonalToken(id){ let commandMatches=[],commandSelectedIndex=0; function commandCatalog(){ - const pages=[['collaboration','AI 工作台','和 AI 对话,派发与跟进任务'],['issues','需求列表','查看进度,触发或重试'],['approvals','待我确认','批准或退回卡住的需求'],['observability','统计','用量分析 · Token 与质量'],['settings','个人配置','账号、模型与执行凭据'],['projects','项目配置','项目、成员和仓库'],['bots','通道管理','企业微信单聊或群聊通知通道'],['sops','流程模板','Issue 修复与需求开发'],['runs','运行记录','查看节点日志和产物'],['aiSchedules','定时任务','提示词任务、SOP 触发与共享任务'],['agents','角色定义','管理角色和能力资产'],['mcp','能力资产 · MCP','添加项目级或个人 MCP'],['rules','能力资产 · 规则','当前项目的行为与执行约束'],['skills','能力资产 · 技能','当前项目的技能和附加文件'],['runners','执行机器','用自己的电脑或 CVM 跑任务'],['dockerfiles','环境资产 · Dockerfile','管理项目容器构建定义'],['environments','环境资产 · 安装脚本','管理隔离环境的工具安装脚本'],['sandboxes','环境资产 · 沙箱','管理项目运行隔离策略'],['guide','访问密钥','创建和撤销访问密钥']].map(([view,title,meta])=>({kind:'页面',title,meta,action:()=>switchView(view)})); + const pages=[['collaboration','AI 工作台','和 AI 对话,派发与跟进任务'],['issues','需求列表','查看进度,触发或重试'],['approvals','待我确认','批准或退回卡住的需求'],['observability','统计','用量分析 · Token 与质量'],['settings','个人配置','账号、模型与执行凭据'],['projects','项目配置','项目、成员和仓库'],['bots','通道管理','企业微信、飞书、钉钉机器人通道'],['sops','流程模板','Issue 修复与需求开发'],['runs','运行记录','查看节点日志和产物'],['aiSchedules','定时任务','提示词任务、SOP 触发与共享任务'],['agents','角色定义','管理角色和能力资产'],['mcp','能力资产 · MCP','添加项目级或个人 MCP'],['rules','能力资产 · 规则','当前项目的行为与执行约束'],['skills','能力资产 · 技能','当前项目的技能和附加文件'],['runners','执行机器','用自己的电脑或 CVM 跑任务'],['dockerfiles','环境资产 · Dockerfile','管理项目容器构建定义'],['environments','环境资产 · 安装脚本','管理隔离环境的工具安装脚本'],['sandboxes','环境资产 · 沙箱','管理项目运行隔离策略'],['guide','访问密钥','创建和撤销访问密钥']].map(([view,title,meta])=>({kind:'页面',title,meta,action:()=>switchView(view)})); const issues=(state.data.issues||[]).map(i=>({kind:'需求',title:i.title,meta:`${i.priority||'—'} · ${issueStatusLabel(i.status)}`,action:()=>openIssueDetail(i.id)})); const runs=sopRuns(state.data.runs).map(r=>({kind:'运行',title:runIssueTitle(r),meta:`${runDisplayStatus(r)} · ${r.workflowSnapshot?.name||r.workflowId}`,action:()=>openRunFromId(r.id)})); const sops=catalogWorkflows().map(w=>({kind:'SOP',title:w.name,meta:`${w.status==='published'?'已发布':'草稿'} · ${w.nodes?.length||0} 个节点`,action:()=>{state.workflow=clone(w);state.selectedNode=null;switchView('flow')}})); diff --git a/web/app/bots.js b/web/app/bots.js index 3dd633204..d9e19baf6 100644 --- a/web/app/bots.js +++ b/web/app/bots.js @@ -9,7 +9,9 @@ function bindBotModelPicker(host){ toggle.onclick=event=>{ event.stopPropagation();if(toggle.disabled||toggle.closest('fieldset')?.disabled)return; const open=menu.hidden;closeBotModelPicker();if(!open)return; - models.renderCodeBuddyList(menu,value.value,id=>{value.value=id;models.paint(toggle,id);captureBotDraft(host);closeBotModelPicker()}); + // "默认" (the CLI default model) stays selectable after choosing another. + const pick=id=>{value.value=id;models.paint(toggle,id);captureBotDraft(host);closeBotModelPicker()}; + models.renderOptions(menu,[{id:models.DEFAULT,name:'默认(平台默认模型)'},...models.codeBuddyModels()],value.value,pick); const rect=toggle.getBoundingClientRect(),width=Math.min(Math.max(rect.width,320),window.innerWidth-24),height=Math.min(320,window.innerHeight-24),below=window.innerHeight-rect.bottom-12,above=rect.top-12; menu.style.width=width+'px';menu.style.left=Math.max(12,Math.min(rect.left,window.innerWidth-width-12))+'px'; const down=below>=Math.min(height,above);const available=down?below:above; @@ -21,14 +23,24 @@ function bindBotModelPicker(host){ } // Project-scoped enterprise chat notifications. const botUI={projectId:'',items:[],deliveries:[],selected:null,sessions:[],query:'',loading:false,error:'',busy:false,busyAction:'',revision:0,tab:'list',step:1,recordsTab:'deliveries',interactionBotId:'',interaction:null,interactionLoading:false,interactionError:'',testResult:'',flashId:'',motion:null,animations:[],shownCount:null,shownError:'',watchTimer:0,watchCount:0,deletingId:''}; -const BOT_TABS=['list','edit','records'],BOT_STEPS=[[1,'填写凭据'],[2,'选择接收会话'],[3,'发送测试']],BOT_DEFAULT_MODEL='glm-5.3-flash-ioa'; +const BOT_TABS=['list','edit','records'],BOT_STEPS=[[1,'填写凭据'],[2,'选择接收会话'],[3,'发送测试']],BOT_TENANT_STEPS=[[1,'填写凭据'],[2,'企业确认'],[3,'通知接收人']],BOT_DEFAULT_MODEL='glm-5.3-flash-ioa'; +// Channels: WeCom keeps the three-step notification setup; Feishu replaces +// target selection with enterprise confirmation followed by a bound member +// who passed a trial proactive send. +const BOT_CHANNELS={wecom:{name:'企业微信',app:'Bot ID',secret:'Secret',appHint:'企业微信智能机器人 Bot ID',ready:true},feishu:{name:'飞书',app:'App ID',secret:'App Secret',appHint:'飞书企业自建应用 App ID(cli_ 开头)',ready:true},dingtalk:{name:'钉钉',app:'Client ID',secret:'Client Secret',appHint:'钉钉企业内部应用 Client ID(AppKey)',ready:true}}; +const BOT_TENANT={awaiting_confirmation:['企业待确认','warn'],locked:['企业已确认','success'],conflict:['企业冲突','error']}; +function botChannel(bot){return BOT_CHANNELS[bot?.channel]?bot.channel:'wecom'} +function botChannelInfo(bot){return BOT_CHANNELS[botChannel(bot)]} +function botSteps(bot){return botChannel(bot)==='wecom'?BOT_STEPS:BOT_TENANT_STEPS} +function botNewKey(){return (crypto.randomUUID?.()||String(Date.now())+Math.random().toString(16).slice(2)).replace(/-/g,'').slice(0,32)} // In-progress states breathe; terminal states stay still. const BOT_LIVE_STATES=['pending','sending','connecting','reconnecting','queued','running','ready']; const BOT_CONNECTION={connected:['已连接','success'],connecting:['连接中','info'],reconnecting:['重连中','info'],error:['连接异常','error'],offline:['未连接','neutral']}; -const BOT_MESSAGE={queued:['等待处理','info'],running:['处理中','info'],ready:['等待回复','info'],replied:['已回复','success'],reply_failed:['回复失败','error']}; +const BOT_MESSAGE={queued:['等待处理','info'],running:['处理中','info'],ready:['等待回复','info'],sending:['发送中','info'],replied:['已回复','success'],reply_failed:['回复失败','error'],reply_unknown:['送达待核对','warn'],cancelled:['已取消','neutral']}; +const BOT_TERMINAL_REASON={undelivered_exhausted:'多次发送未成功',rejected:'平台拒绝',delivery_unknown:'平台未确认送达',window_expired:'超过平台去重窗口',route_expired:'会话回复授权已过期',bot_disabled:'机器人已停用',bot_deleted:'机器人已删除',bot_disabled_during_send:'发送中停用',bot_deleted_during_send:'发送中删除'}; function botTypeLabel(type){return type==='group'?'群聊':'单聊'} function botHealthLabel(health){return ({ready:'消息权限可用',unchecked:'待验证',error:'连接异常',awaiting_authorization:'需要重新授权'})[health]||'待验证'} -function botStatusLabel(status){return ({pending:'等待发送',sending:'发送中',sent:'已发送',failed:'发送失败',awaiting_authorization:'等待授权',target_unavailable:'会话不可用',cancelled:'已取消',expired:'已过期'})[status]||status} +function botStatusLabel(status){return ({pending:'等待发送',sending:'发送中',sent:'已发送',failed:'发送失败',send_unknown:'送达待核对',awaiting_authorization:'等待授权',target_unavailable:'会话不可用',cancelled:'已取消',expired:'已过期'})[status]||status} function filteredBotSessions(items,query){const text=String(query||'').trim().toLocaleLowerCase();return(items||[]).filter(chat=>!text||String(chat.name||'').toLocaleLowerCase().includes(text))} function canCreateBot(){if(!state.authRequired||!state.currentUser||isPlatformAdmin())return true;const user=Number(state.currentUser.id),project=currentProject();return Number(project?.ownerId)===user||(project?.members||[]).some(m=>Number(m.userId)===user&&m.role!=='viewer')} function canManageBot(bot){ @@ -36,10 +48,12 @@ function canManageBot(bot){ const user=Number(state.currentUser.id),project=currentProject(); return Number(bot?.ownerId)===user||Number(project?.ownerId)===user||(project?.members||[]).some(m=>Number(m.userId)===user&&m.role==='maintainer'); } -function botStepAllowed(bot,step){return step===1||(step===2&&!!bot?.id)||(step===3&&!!bot?.id&&!!bot?.target&&!bot._targetChanged)} -function botStepDone(bot,step){return step===1?!!bot?.id:step===2?botStepAllowed(bot,3):false} +function botStepAllowed(bot,step){if(botChannel(bot)!=='wecom')return step===1||(step===2&&!!bot?.id)||(step===3&&!!bot?.id&&bot?.tenantState==='locked');return step===1||(step===2&&!!bot?.id)||(step===3&&!!bot?.id&&!!bot?.target&&!bot._targetChanged)} +function botStepDone(bot,step){if(botChannel(bot)!=='wecom')return step===1?!!bot?.id:step===2?bot?.tenantState==='locked':step===3&&!!bot?.target?.verifiedAt&&!bot._targetChanged;return step===1?!!bot?.id:step===2?botStepAllowed(bot,3):false} function botStoredBot(bot){return bot?.id?botUI.items.find(b=>b.id===bot.id):null} -function botCredentialsDirty(bot){const stored=botStoredBot(bot);if(!stored)return !!bot;return !!bot._secret||(bot.name||'')!==(stored.name||'')||(bot.botId||'')!==(stored.botId||'')||(bot.model||BOT_DEFAULT_MODEL)!==(stored.model||BOT_DEFAULT_MODEL)||!!bot.enabled!==!!stored.enabled||!!bot.interactionEnabled!==!!stored.interactionEnabled} +function botCredentialsDirty(bot){const stored=botStoredBot(bot);if(!stored)return !!bot;return !!bot._secret||(bot.name||'')!==(stored.name||'')||(bot.botId||'')!==(stored.botId||'')||(bot.model||BOT_DEFAULT_MODEL)!==(stored.model||BOT_DEFAULT_MODEL)||!!bot.enabled!==!!stored.enabled||!!bot.interactionEnabled!==!!stored.interactionEnabled||botWriteApproval(bot)!==botWriteApproval(stored)||(bot._tenantKey!==undefined&&bot._tenantKey.trim()!==(stored.tenantExpected||''))||(bot._cardTemplateId!==undefined&&bot._cardTemplateId.trim()!==(stored.cardTemplateId||''))} +// Legacy WeCom records without the field keep automatic execution. +function botWriteApproval(bot){return bot?.writeApproval||(botChannel(bot)==='wecom'&&bot?.id?'auto':'readonly')} function botIcon(name){return ``} // Harness StatusBadge: in-progress states breathe, settled states carry a glyph so colour is never the only cue. @@ -49,10 +63,11 @@ function botBadgeIcon(tone,state){return BOT_BADGE_OVERRIDES[state]||BOT_BADGE_G // The vendored Harness set has no plain minus, so it is drawn inline with the same stroke. function botGlyphHTML(name){return name==='minus'?'':``} function botBadge(label,tone,live,state){tone=tone||'neutral';return `${live?'':botGlyphHTML(botBadgeIcon(tone,state))}${escapeHTML(label)}`} -function botStatusTone(status){return ({sent:'success',failed:'error',target_unavailable:'error',awaiting_authorization:'warn',pending:'info',sending:'info'})[status]||'neutral'} +function botStatusTone(status){return ({sent:'success',failed:'error',target_unavailable:'error',awaiting_authorization:'warn',send_unknown:'warn',pending:'info',sending:'info'})[status]||'neutral'} function botStatusBadge(status){return botBadge(botStatusLabel(status),botStatusTone(status),BOT_LIVE_STATES.includes(status),status)} function botHealthBadge(health){return botBadge(botHealthLabel(health),({ready:'success',error:'error',awaiting_authorization:'warn'})[health]||'neutral',false,health||'unchecked')} function botEnabledBadge(enabled){return botBadge(enabled?'已启用':'已停用',enabled?'success':'neutral',false)} +function botTenantBadge(state){const [label,tone]=BOT_TENANT[state]||BOT_TENANT.awaiting_confirmation;return botBadge(label,tone,false,state)} function botConnectionBadge(connection){const [label,tone]=BOT_CONNECTION[connection]||[connection,'neutral'];return botBadge('对话连接 · '+label,tone,BOT_LIVE_STATES.includes(connection),connection)} function botMessageBadge(status){const [label,tone]=BOT_MESSAGE[status]||[status,'neutral'];return botBadge(label,tone,BOT_LIVE_STATES.includes(status),status)} function botBusyLabel(action,icon,label,busyLabel){return botUI.busy&&botUI.busyAction===action?`${busyLabel}`:`${icon?botIcon(icon):''}${label}`} @@ -74,8 +89,9 @@ function botPanelHeadHTML(){ } function botTargetHTML(target){return target?`${botTypeLabel(target.type)}${escapeHTML(target.name)}`:'尚未选择接收会话'} function botRowHTML(bot){ - const id=escapeHTML(bot.id),name=escapeHTML(bot.name),manage=canManageBot(bot),target=botTargetHTML(bot.target); - const sub=escapeHTML(bot.model||BOT_DEFAULT_MODEL)+(bot.interactionEnabled?' · 已启用对话':''); + const id=escapeHTML(bot.id),name=escapeHTML(bot.name),manage=canManageBot(bot),wecom=botChannel(bot)==='wecom'; + const target=wecom||bot.target?botTargetHTML(bot.target):`${botTenantBadge(bot.tenantState)}`; + const sub=escapeHTML((wecom?'':botChannelInfo(bot).name+' · ')+(bot.model||BOT_DEFAULT_MODEL))+(bot.interactionEnabled?({readonly:' · 已启用对话(只读)',confirm:' · 已启用对话(网页确认)'}[botWriteApproval(bot)]||' · 已启用对话'):''); return `
${botIcon('robot')}${name}${sub}${target}
${target}${botEnabledBadge(bot.enabled)}${botHealthBadge(bot.health)}
${manage?``:''}
`; } function botListHTML(items){ @@ -88,21 +104,71 @@ function botGuideHTML(){ return `
${botIcon('robot')}

让任务进展抵达企业微信

还没有机器人。三步完成配置,随后在 SOP 设置或 AI 定时任务中选择这个 Bot。

    ${steps.map(([title,text],i)=>`
  1. ${title}${text}
  2. `).join('')}
${project?.name?`

当前项目:${escapeHTML(project.name)}

`:''}
`; } function botStepsHTML(bot,step){ - return `
${BOT_STEPS.map(([n,label])=>{const done=n!==step&&botStepDone(bot,n);return ``}).join('')}
`; + const steps=botSteps(bot); + return `
${steps.map(([n,label])=>{const done=n!==step&&botStepDone(bot,n);return ``}).join('')}
`; } function botEditTabHTML(bot){ if(!bot)return botListHTML(botUI.items); const manage=!bot.id||canManageBot(bot),step=botUI.step; - const pane=step===2?botTargetPaneHTML(bot,manage):step===3?botTestPaneHTML(bot,manage):botCredentialPaneHTML(bot,manage); + const wecom=botChannel(bot)==='wecom'; + const pane=step===2?(wecom?botTargetPaneHTML(bot,manage):botTenantPaneHTML(bot,manage)):step===3?(wecom?botTestPaneHTML(bot,manage):botPushTargetPaneHTML(bot,manage)):botCredentialPaneHTML(bot,manage); return `
${botStepsHTML(bot,step)}
${pane}
${!manage?'

仅创建者或项目管理员可修改凭据和接收目标。

':''}
`; } +function botChannelFieldHTML(bot){ + if(bot.id)return ``; + return ``; +} +function botWriteFieldHTML(bot){ + const mode=botWriteApproval(bot); + // Feishu and DingTalk: writes only through web confirmation. + if(botChannel(bot)!=='wecom')return ``; + return ``; +} function botCredentialPaneHTML(bot,manage){ - return `

首次对话请按 Bot 提示关联平台账号;新单聊和群聊会自动建立上下文。发送 /new 或“开启新会话”开始新的对话。

`; + const c=botChannelInfo(bot),plaintext=bot.credentialFormat==='plaintext'; + return `
${botChannelFieldHTML(bot)}${botCorpFieldHTML(bot)}${botWriteFieldHTML(bot)}

${botChannel(bot)==='wecom'?'首次对话请按 Bot 提示关联平台账号;新单聊和群聊会自动建立上下文。发送 /new 或“开启新会话”开始新的对话。':botChannel(bot)==='dingtalk'?'处理单聊和群聊中 @ Bot 的文字消息。保存时会校验凭据;首条消息的企业与 CorpId 一致时自动确认企业。':'处理单聊和群聊中 @ Bot 的文字消息。保存时会校验凭据;首次收到消息后,需在第②步确认所属企业。'}

`; +} +// DingTalk: the administrator enters the expected CorpId; it can be +// corrected until the first matching message locks it. +function botCorpFieldHTML(bot){ + if(botChannel(bot)!=='dingtalk')return ''; + const locked=bot.tenantState==='locked',value=bot._tenantKey??bot.tenantExpected??''; + return ``; +} +// Enterprise confirmation: the candidate comes from the first trusted event +// of the authenticated connection; the platform query is shown alongside. +function botTenantPaneHTML(bot,manage){ + if(botChannel(bot)==='dingtalk')return botDingTalkTenantPaneHTML(bot,manage); + const info=botUI.tenant?.botId===bot.id?botUI.tenant:null,candidate=bot.tenantCandidate,state=bot.tenantState||'awaiting_confirmation'; + const queried=info?.queried?`${escapeHTML(info.queried.tenantName||'未返回名称')} · ${escapeHTML(info.queried.tenantKey||'未返回企业标识')}`:info?.queryError?`${escapeHTML(info.queryError)}`:'读取中…'; + const match=candidate&&info?.queried?.tenantKey?(candidate.tenantKey===info.queried.tenantKey?'与平台查询一致':'与平台查询不一致,请核实'):''; + let body; + if(state==='locked')body=`

已确认企业:${escapeHTML(bot.tenantName||'')} ${escapeHTML(bot.tenantKey||'')}

只接收该企业的消息。更换应用或企业请新建配置。

`; + else if(state==='conflict')body=`

收到了两个不同企业的消息:${escapeHTML(candidate?.tenantKey||'')} 与 ${escapeHTML(candidate?.conflict||'')}。请核实应用发布范围后重新发现。

`; + else if(!candidate)body='

等待第一条消息。请让测试成员在飞书中给机器人发送一条单聊消息,收到后这里会显示候选企业。

确认前机器人只回复“正在完成企业确认”的提示,不会处理业务。

'; + else body=`

候选企业标识:${escapeHTML(candidate.tenantKey)} ${match}

来自 ${escapeHTML(new Date(candidate.receivedAt).toLocaleString())} 收到的消息。确认后只接收该企业的消息。

`; + return `
状态${botTenantBadge(state)}平台查询${queried}
${body}
`; +} +function botDingTalkTenantPaneHTML(bot,manage){ + const state=bot.tenantState||'awaiting_confirmation',candidate=bot.tenantCandidate; + let body; + if(state==='locked')body=`

已确认企业 ${escapeHTML(bot.tenantKey||'')},只接收该企业的消息。

更换应用或企业请新建配置。

`; + else if(state==='conflict')body=`

收到的消息来自企业 ${escapeHTML(candidate?.conflict||'')},与填写的 CorpId ${escapeHTML(bot.tenantExpected||'')} 不一致,已停止处理。

核实后在第①步改正 CorpId 并保存,或点“重新发现”重新检查下一条消息。

`; + else body=`

等待第一条消息。请让测试成员在钉钉中给机器人发一条单聊消息;企业与 ${escapeHTML(bot.tenantExpected||'')} 一致时自动确认,这条消息也会正常处理。

`; + return `
状态${botTenantBadge(state)}填写的 CorpId${escapeHTML(bot.tenantExpected||'')}
${body}
`; } function botTargetPaneHTML(bot,manage){ const changed=!!bot._targetChanged; return `
接收会话

只展示机器人当前可通知的最近会话。找不到目标时,请先在企业微信与 Bot 互动,再刷新。

${bot.target?`当前目标:${botTypeLabel(bot.target.type)} ${escapeHTML(bot.target.name)}`:'尚未绑定接收会话'}
${botSessionHTML()}
`; } +// Feishu/DingTalk notifications go to a member who bound a platform account +// in a private chat with this Bot; saving sends a trial message first. +function botPushTargetPaneHTML(bot,manage){ + const changed=!!bot._targetChanged,verified=!!bot.target?.verifiedAt&&!changed,name=botChannelInfo(bot).name; + const current=bot.target?`当前接收人:${escapeHTML(bot.target.name)} ${verified?'试发成功':'未验证或已解绑,请重新试发'}`:'尚未选择通知接收人'; + const result=botUI.testResult?`
${botIcon('check-draw')}试发消息已发送到 ${escapeHTML(botUI.testResult)},可在「记录 → 发送记录」查看。
`:''; + return `
通知接收人

只列出在${escapeHTML(name)}中与本 Bot 单聊并完成账号绑定的成员。保存时先向该成员发送一条试发消息,平台确认送达后才会保存;SOP 的完成、失败和待处理通知会以单聊发送给他。关闭“启用对话”后通知仍可发送。

${current}
${botSessionHTML()}
${result}
`; +} function botTestPaneHTML(bot,manage){ const ready=!!(bot.enabled&&bot.target); const result=botUI.testResult?`
${botIcon('check-draw')}测试通知已发送到 ${escapeHTML(botUI.testResult)},可在「记录 → 发送记录」查看。
`:''; @@ -110,9 +176,9 @@ function botTestPaneHTML(bot,manage){ } function botSessionHTML(){ const chats=filteredBotSessions(botUI.sessions,botUI.query); - if(!botUI.sessions.length)return '

点击刷新获取可发送会话;列表为空时,先与 Bot 互动。

'; + if(!botUI.sessions.length)return botChannel(botUI.selected)==='wecom'?'

点击刷新获取可发送会话;列表为空时,先与 Bot 互动。

':'

点击刷新获取成员;列表为空时,请成员先在单聊中向 Bot 发送“绑定账号”完成绑定。

'; if(!chats.length)return '

没有匹配的会话,尝试其他名称。

'; - return ''+chats.map(chat=>``).join(''); + return ''+chats.map(chat=>``).join(''); } function botRecordTabsHTML(active){const item=(id,label)=>{const on=id==='interactions'?active==='interactions':active!=='interactions';return ``};return `
${item('deliveries','发送记录')}${item('interactions','对话记录')}
`} function botRecordsHTML(){ @@ -120,10 +186,12 @@ function botRecordsHTML(){ return `
${botRecordTabsHTML(botUI.recordsTab)}${deliveries?'保留最近 100 条':''}
${deliveries?botDeliveryHTML():botInteractionPanelHTML()}
`; } function botDeliveryExtraHTML(item){return `${item.reportModel?`CodeBuddy · ${escapeHTML(item.reportModel)}`:''}${item.reportError?`${escapeHTML(item.reportError)}`:''}${item.lastError?`${escapeHTML(item.lastError)}`:''}`} -function botDeliveryActionHTML(item){const bot=botUI.items.find(b=>b.id===item.botId);return ['failed','awaiting_authorization','target_unavailable'].includes(item.status)&&item.contentSource!=='invalid'&&bot&&canManageBot(bot)?``:''} +// send_unknown: the administrator checks the chat, then confirms receipt or +// sends again (a new generation and key). +function botDeliveryActionHTML(item){const bot=botUI.items.find(b=>b.id===item.botId);if(!bot||!canManageBot(bot)||item.contentSource==='invalid'||item.contentSource==='purged')return '';const id=escapeHTML(item.id);if(item.status==='send_unknown')return ``;return ['failed','awaiting_authorization','target_unavailable'].includes(item.status)?``:''} function botDeliveryHTML(){ if(!botUI.deliveries.length)return '

暂无发送记录。Agent 节点触发通知后,会在这里展示发送状态。

'; - return `
${botUI.deliveries.map(item=>{const bot=botUI.items.find(b=>b.id===item.botId);return ``}).join('')}
时间Bot接收会话来源 / 触发条件状态
${escapeHTML(new Date(item.createdAt).toLocaleString())}${escapeHTML(bot?.name||'已删除的 Bot')}${escapeHTML(item.targetName)}${escapeHTML(item.sourceName||'')}${escapeHTML(({schedule_job:'定时任务',assistant_task:'AI 助手任务',run:'流程'})[item.sourceType]||'流程')} · ${escapeHTML(({succeeded:'完成',failed:'失败',waiting:'人工处理',test:'测试通知'})[item.eventType]||item.eventType)}${item.contentSource&&item.contentSource!=='template'?`${escapeHTML(({ai:'AI 内容',simulated_ai:'模拟 AI 内容',result_fallback:'执行结果回退',context_fallback:'状态摘要回退',invalid:'内容格式异常',fallback:'标准消息回退'})[item.contentSource]||item.contentSource)}`:''}${botStatusBadge(item.status)}${botDeliveryExtraHTML(item)}${botDeliveryActionHTML(item)}
`; + return `
${botUI.deliveries.map(item=>{const bot=botUI.items.find(b=>b.id===item.botId);return ``}).join('')}
时间Bot接收会话来源 / 触发条件状态
${escapeHTML(new Date(item.createdAt).toLocaleString())}${escapeHTML(bot?.name||'已删除的 Bot')}${escapeHTML(item.targetName)}${escapeHTML(item.sourceName||'')}${escapeHTML(({schedule_job:'定时任务',assistant_task:'AI 助手任务',run:'流程'})[item.sourceType]||'流程')} · ${escapeHTML(({succeeded:'完成',failed:'失败',waiting:'人工处理',test:'测试通知'})[item.eventType]||item.eventType)}${item.contentSource&&item.contentSource!=='template'?`${escapeHTML(({ai:'AI 内容',simulated_ai:'模拟 AI 内容',result_fallback:'执行结果回退',context_fallback:'状态摘要回退',invalid:'内容格式异常',fallback:'标准消息回退',purged:'正文已按保留期清理'})[item.contentSource]||item.contentSource)}`:''}${botStatusBadge(item.status)}${botDeliveryExtraHTML(item)}${botDeliveryActionHTML(item)}
`; } function botInteractionBots(){return botUI.items.filter(bot=>canManageBot(bot))} function botInteractionPanelHTML(){ @@ -132,9 +200,38 @@ function botInteractionPanelHTML(){ const bot=bots.find(b=>b.id===botUI.interactionBotId)||bots[0],data=botUI.interaction,connection=data?.connection?.state||'offline'; const messages=(data?.messages||[]).filter(m=>m.action!=='notification').slice(0,12); const count=data?`最近对话 · ${data.conversations?.length||0} 个聊天`:''; - const body=botUI.interactionError?`

${escapeHTML(botUI.interactionError)}

`:!data&&botUI.interactionLoading?botSkeletonHTML(2):messages.map(m=>`

${escapeHTML(m.content||m.action||'')}

${m.lastError?`${escapeHTML(m.lastError)}`:''}${m.reply?`

${escapeHTML(m.reply.slice(0,240))}

`:''}
`).join('')||'

还没有对话记录

'; + const body=botUI.interactionError?`

${escapeHTML(botUI.interactionError)}

`:!data&&botUI.interactionLoading?botSkeletonHTML(2):messages.map(m=>`

${m.bodyPurgedAt?'正文已按保留期清理':escapeHTML(m.content||m.action||'')}

${m.terminalReason&&BOT_TERMINAL_REASON[m.terminalReason]?`${escapeHTML(BOT_TERMINAL_REASON[m.terminalReason])}${m.receiptSource==='manual'?' · 人工确认':''}`:m.receiptSource==='manual'?'人工确认已收到':''}${m.lastError?`${escapeHTML(m.lastError)}`:''}${m.reply?`

${escapeHTML(m.reply.slice(0,240))}

`:''}${botInteractionActionsHTML(m,bot)}
`).join('')||'

还没有对话记录

'; return `
${botConnectionBadge(connection)}${count}
${data?.connection?.lastError?`

${escapeHTML(data.connection.lastError)}

`:''}${body}
`; } +// Manual handling of a reply whose delivery is unknown or failed. The +// action key makes a repeated click return the same result. +function botInteractionActionsHTML(m,bot){ + if(!canManageBot(bot))return ''; + const id=escapeHTML(m.id),buttons=[]; + // Text removed by the retention cleanup cannot be sent again. + if(m.bodyPurgedAt)return ''; + if(m.status==='reply_unknown')buttons.push(``); + if((m.status==='reply_unknown'||m.status==='reply_failed')&&m.terminalReason!=='route_expired'&&!String(m.terminalReason||'').startsWith('bot_deleted'))buttons.push(``); + // An expired DingTalk reply can go out as a proactive private message to + // its original sender once that member passed a trial send. + if(botChannel(bot)==='dingtalk'&&m.status==='reply_failed'&&m.terminalReason==='route_expired')buttons.push(m.pushResendAvailable?``:``); + return buttons.length?`
${buttons.join('')}
`:''; +} +async function botInteractionAction(id,kind,button){ + if(kind==='resend'&&!await afConfirm({message:'再次发送会把已保存的回复重新发给对方,对方可能收到重复消息。确定继续?',confirmText:'再次发送'}))return; + if(kind==='push-resend'&&!await afConfirm({message:'会话回复授权已过期。改用主动单聊,会把已保存的回复以 Bot 单聊消息发给原发送者。确定继续?',confirmText:'改用主动单聊发送'}))return; + button.disabled=true; + try{ + await api(`/api/bot-interactions/${encodeURIComponent(id)}/${kind}`,{method:'POST',body:JSON.stringify({actionKey:botNewKey(),reason:kind==='resend'?'记录页再次发送':kind==='push-resend'?'会话回复已过期,记录页改用主动单聊':''})}); + toast(kind==='confirm'?'已标记为已收到':'已重新加入发送队列','ok');loadBotInteractions(); + }catch(e){toast(e.message,'err');button.disabled=false} +} +async function botVerifySender(sender,button){ + const bot=botInteractionBots().find(b=>b.id===botUI.interactionBotId)||botInteractionBots()[0];if(!bot)return; + button.disabled=true; + try{await api(`/api/notification-bots/${encodeURIComponent(bot.id)}/targets/verify`,{method:'POST',body:JSON.stringify({targetId:sender})});toast('试发成功,现在可以改用主动单聊发送','ok');loadBotInteractions()} + catch(e){toast(e.message,'err');button.disabled=false} +} function botSkeletonHTML(rows=3){return ``} function botLoadingShell(){return `
${botPanelHeadHTML()}
${botSkeletonHTML()}
`} @@ -161,9 +258,10 @@ function botFoldOpen(el){ const done=()=>el.classList.remove('is-folding');animation.onfinish=done;animation.oncancel=done; } function botAnimateStep(host,{dir,from},oldHeight){ + const span=Math.max(1,botSteps(botUI.selected).length-1); host.querySelector('.bot-step-pane')?.classList.add(dir>0?'bot-enter-r':'bot-enter-l'); const fill=host.querySelector('.bot-step-track i'),fold=host.querySelector('.bot-step-fold'); - if(fill?.animate&&from)botTrack(fill.animate([{width:`${(from-1)/2*100}%`},{width:`${(botUI.step-1)/2*100}%`}],{duration:botDuration('indicator'),easing:botEase('spring')})); + if(fill?.animate&&from)botTrack(fill.animate([{width:`${(from-1)/span*100}%`},{width:`${(botUI.step-1)/span*100}%`}],{duration:botDuration('indicator'),easing:botEase('spring')})); if(!fold?.animate||!oldHeight)return; const height=fold.offsetHeight;if(Math.abs(height-oldHeight)<2)return; fold.classList.add('is-folding'); @@ -215,7 +313,7 @@ function botCollapseRow(row){ function botResetProject(){ clearTimeout(botUI.watchTimer); - Object.assign(botUI,{items:[],deliveries:[],selected:null,sessions:[],query:'',tab:'list',step:1,recordsTab:'deliveries',interactionBotId:'',interaction:null,interactionLoading:false,interactionError:'',testResult:'',flashId:'',motion:null,shownCount:null,watchTimer:0,watchCount:0}); + Object.assign(botUI,{items:[],deliveries:[],selected:null,sessions:[],query:'',tab:'list',step:1,recordsTab:'deliveries',interactionBotId:'',interaction:null,interactionLoading:false,interactionError:'',testResult:'',flashId:'',motion:null,shownCount:null,watchTimer:0,watchCount:0,tenant:null}); } function botReconcileSelection(fresh){ const selected=botUI.selected; @@ -283,12 +381,13 @@ function botSwitchTab(tab){ } function botOpenEditor(bot){ if(botUI.busy||(!bot&&!canCreateBot()))return; - botUI.selected=bot?structuredClone(bot):{name:'',botId:'',enabled:true,projectId:state.projectFilter}; + botUI.selected=bot?structuredClone(bot):{name:'',botId:'',channel:'wecom',writeApproval:'readonly',enabled:true,projectId:state.projectFilter}; Object.assign(botUI,{step:1,sessions:[],query:'',testResult:'',error:''}); if(botUI.tab==='edit'){botMotion('tab',0);renderBots()}else botSwitchTab('edit'); } function botGoStep(step,options={}){ const bot=botUI.selected;if(!bot||botUI.busy||step===botUI.step)return false; + if(step===2&&botChannel(bot)!=='wecom'&&bot.id)loadBotTenant(bot.id); if(!botStepAllowed(bot,step)){toast(bot.id?'请先选择并保存接收会话':'请先保存凭据','warn');return false} if(botUI.step===1&&bot.id&&botCredentialsDirty(bot)){toast('凭据有未保存的修改,请先保存','warn');return false} botMotion('step',{dir:step>botUI.step?1:-1,from:botUI.step});botUI.step=step;botUI.testResult=''; @@ -314,11 +413,18 @@ function bindBots(host){ bindBotEditor(host); host.querySelector('#botInteractionBot')?.addEventListener('change',event=>{botUI.interactionBotId=event.target.value;botUI.interaction=null;botMotion('entries');loadBotInteractions();renderBots()}); host.querySelector('#botRefreshInteractions')?.addEventListener('click',()=>{loadBotInteractions();renderBots()}); + host.querySelectorAll('[data-interaction-confirm]').forEach(el=>el.onclick=()=>botInteractionAction(el.dataset.interactionConfirm,'confirm',el)); + host.querySelectorAll('[data-interaction-resend]').forEach(el=>el.onclick=()=>botInteractionAction(el.dataset.interactionResend,'resend',el)); + host.querySelectorAll('[data-interaction-push-resend]').forEach(el=>el.onclick=()=>botInteractionAction(el.dataset.interactionPushResend,'push-resend',el)); + host.querySelectorAll('[data-interaction-push-verify]').forEach(el=>el.onclick=()=>botVerifySender(el.dataset.interactionPushVerify,el)); } -function captureBotDraft(host){const form=host.querySelector('#botForm');if(!form||!botUI.selected)return;botUI.selected.name=form.elements.name.value;botUI.selected.botId=form.elements.botId.value;botUI.selected.enabled=form.elements.enabled.checked;botUI.selected.model=form.elements.model.value;botUI.selected.interactionEnabled=form.elements.interactionEnabled.checked;botUI.selected._secret=form.elements.secret.value} +function captureBotDraft(host){const form=host.querySelector('#botForm');if(!form||!botUI.selected)return;const bot=botUI.selected,channel=form.elements.channel?.value;if(channel&&channel!==botChannel(bot)){bot.channel=channel;bot.writeApproval='readonly';botUI.motion=null;queueMicrotask(renderBots)}bot.name=form.elements.name.value;bot.botId=form.elements.botId.value;bot.enabled=form.elements.enabled.checked;bot.model=form.elements.model.value;bot.interactionEnabled=form.elements.interactionEnabled.checked;bot._secret=form.elements.secret.value;if(form.elements.writeApproval)bot.writeApproval=form.elements.writeApproval.value;if(form.elements.tenantKey)bot._tenantKey=form.elements.tenantKey.value;if(form.elements.cardTemplateId)bot._cardTemplateId=form.elements.cardTemplateId.value} function botSaveSelected(saved){const index=botUI.items.findIndex(b=>b.id===saved.id);if(index<0)botUI.items.push(saved);else botUI.items[index]=saved;botUI.selected=structuredClone(saved);botUI.error='';nodeBotCache.delete(botUI.projectId)} function bindBotEditor(host){ host.querySelector('#botDelete')?.addEventListener('click',()=>botDelete(botStoredBot(botUI.selected)||botUI.selected,null)); + host.querySelector('#botTenantConfirm')?.addEventListener('click',()=>botTenantAction(false)); + host.querySelector('#botTenantRediscover')?.addEventListener('click',()=>botTenantAction(true)); + host.querySelector('#botTenantRefresh')?.addEventListener('click',()=>{if(botUI.selected?.id)loadBotTenant(botUI.selected.id)}); const form=host.querySelector('#botForm'); if(form){ bindBotModelPicker(host); @@ -326,9 +432,13 @@ function bindBotEditor(host){ form.onsubmit=async event=>{ event.preventDefault();if(botUI.busy)return;captureBotDraft(host); const bot=botUI.selected,projectId=botUI.projectId,revision=botUI.revision,creating=!bot.id; - const missing=['name','botId',...(creating?['secret']:[])].map(name=>form.elements[name]).filter(input=>!input.value.trim()); - if(missing.length){botShake(missing);toast(creating?'请填写名称、Bot ID 和 Secret':'请填写名称和 Bot ID','warn');return} - const payload={projectId,name:bot.name,botId:bot.botId,enabled:bot.enabled,model:bot.model,interactionEnabled:bot.interactionEnabled,secret:form.elements.secret.value}; + const dingtalk=botChannel(bot)==='dingtalk'; + const missing=['name','botId',...(creating?['secret']:[]),...(creating&&dingtalk?['tenantKey']:[])].map(name=>form.elements[name]).filter(input=>input&&!input.value.trim()); + if(missing.length){botShake(missing);const c=botChannelInfo(bot);toast(creating?`请填写名称、${c.app}、${c.secret}${dingtalk?' 和企业 CorpId':''}`:`请填写名称和 ${c.app}`,'warn');return} + const stored=botStoredBot(bot); + const payload={projectId,name:bot.name,channel:botChannel(bot),botId:bot.botId,enabled:bot.enabled,model:bot.model,interactionEnabled:bot.interactionEnabled,writeApproval:botWriteApproval(bot),secret:form.elements.secret.value}; + if(stored)payload.configVersion=stored.configVersion; + if(dingtalk){payload.tenantKey=(form.elements.tenantKey?.value||'').trim();payload.cardTemplateId=(form.elements.cardTemplateId?.value||'').trim()} if(bot.id&&!payload.secret&&bot.botId===botStoredBot(bot)?.botId&&bot._targetChanged)payload.targetId=bot.target?.id||''; if(bot._targetChanged&&(payload.secret||!bot.id)){toast('请先保存凭据,再刷新并选择目标','warn');return} botUI.busy=true;botUI.busyAction='save';renderBots(); @@ -358,13 +468,22 @@ function bindBotEditor(host){ const stored=botStoredBot(bot),projectId=botUI.projectId,revision=botUI.revision;if(!stored)return; if(botCredentialsDirty(bot)){toast('请先保存凭据,再刷新并选择目标','warn');return} botUI.busy=true;botUI.busyAction='target';renderBots(); - let saved=null; + let saved=null;const wecom=botChannel(stored)==='wecom'; + const put=pendingTargetAction=>api(`/api/notification-bots/${encodeURIComponent(bot.id)}`,{method:'PUT',body:JSON.stringify({projectId,name:stored.name,channel:botChannel(stored),botId:stored.botId,enabled:stored.enabled,model:stored.model,interactionEnabled:stored.interactionEnabled,writeApproval:botWriteApproval(stored),secret:'',targetId:bot.target?.id||'',configVersion:stored.configVersion,pendingTargetAction})}); try{ - saved=await api(`/api/notification-bots/${encodeURIComponent(bot.id)}`,{method:'PUT',body:JSON.stringify({projectId,name:stored.name,botId:stored.botId,enabled:stored.enabled,model:stored.model,interactionEnabled:stored.interactionEnabled,secret:'',targetId:bot.target?.id||''})}); + try{saved=await put('')} + catch(e){ + if(e.code!=='pending_deliveries')throw e; + // Queued notifications of the previous recipient need an explicit choice. + const action=await afConfirm({title:'原接收人还有待发通知',message:e.message+'。改发后这些通知会发给新接收人。',confirmText:'改发到新接收人',cancelText:'不改发'})?'migrate':await afConfirm({danger:true,title:'取消待发通知',message:'不改发时,原接收人的待发通知会被取消,并在发送记录中注明原因。',confirmText:'取消这些通知并切换',cancelText:'返回'})?'cancel':''; + if(!action)return; + saved=await put(action); + } if(revision!==botUI.revision||projectId!==state.projectFilter){saved=null;return} - botSaveSelected(saved);toast(saved.target?'接收会话已保存':'已清除接收会话','ok'); + botSaveSelected(saved);toast(saved.target?(wecom?'接收会话已保存':`试发成功,已保存接收人 ${saved.target.name}`):(wecom?'已清除接收会话':'已清除接收人'),'ok'); + if(!wecom&&saved.target)botUI.testResult=saved.target.name; }catch(e){if(revision===botUI.revision)botUI.error=e.message} - finally{botUI.busy=false;botUI.busyAction='';if(saved?.target&&botUI.tab==='edit'&&botUI.selected?.id===saved.id&&botGoStep(3,{render:false}))botMotion('check',2);renderBots()} + finally{botUI.busy=false;botUI.busyAction='';if(wecom&&saved?.target&&botUI.tab==='edit'&&botUI.selected?.id===saved.id&&botGoStep(3,{render:false}))botMotion('check',2);renderBots()} }); host.querySelector('#botSendTest')?.addEventListener('click',async()=>{ const bot=botUI.selected;if(botUI.busy||!bot?.id)return; @@ -374,6 +493,22 @@ function bindBotEditor(host){ catch(e){if(revision===botUI.revision)botUI.error=e.message}finally{botUI.busy=false;botUI.busyAction='';renderBots()} }); } +async function loadBotTenant(id){ + const revision=botUI.revision; + try{const info=await api(`/api/notification-bots/${encodeURIComponent(id)}/tenant`,{silent:true});if(revision!==botUI.revision||botUI.selected?.id!==id)return;botUI.tenant={...info,botId:id};const bot=botUI.selected;bot.tenantState=info.tenantState;bot.tenantCandidate=info.candidate;const stored=botStoredBot(bot);if(stored){stored.tenantState=info.tenantState;stored.tenantCandidate=info.candidate;stored.configVersion=info.configVersion}} + catch(e){if(revision===botUI.revision)botUI.tenant={botId:id,queryError:e.message}} + finally{if(revision===botUI.revision)renderBots()} +} +async function botTenantAction(rediscover){ + const bot=botUI.selected,stored=botStoredBot(bot);if(botUI.busy||!bot?.id||!stored)return; + const projectId=botUI.projectId,revision=botUI.revision;botUI.busy=true;botUI.busyAction='tenant';renderBots(); + try{ + const saved=await api(`/api/notification-bots/${encodeURIComponent(bot.id)}/tenant/confirm`,{method:'POST',body:JSON.stringify({tenantKey:bot.tenantCandidate?.tenantKey||'',configVersion:stored.configVersion,rediscover})}); + if(revision!==botUI.revision||projectId!==state.projectFilter)return; + botSaveSelected(saved);toast(rediscover?'已重新开始企业发现':'企业已确认','ok'); + }catch(e){if(revision===botUI.revision)botUI.error=e.message} + finally{botUI.busy=false;botUI.busyAction='';renderBots()} +} function bindBotSessionSelection(host){host.querySelectorAll('[name="targetId"]').forEach(el=>el.onchange=()=>{botUI.selected.target=structuredClone(botUI.sessions.find(chat=>chat.id===el.value));botUI.selected._targetChanged=botStoredBot(botUI.selected)?.target?.id!==el.value;botMotion('pick');renderBots()})} async function botDelete(bot,row){ if(!bot?.id||botUI.busy||botUI.deletingId===bot.id)return; @@ -395,7 +530,15 @@ async function botDeleteRequest(bot,row,projectId,revision){ if(botUI.interactionBotId===bot.id){botUI.interactionBotId='';botUI.interaction=null} renderBots();loadBots({silent:true}); } -function bindBotRetry(host){host.querySelectorAll('[data-delivery-retry]').forEach(el=>el.onclick=()=>botRetryDelivery(el.dataset.deliveryRetry,el))} +function bindBotRetry(host){host.querySelectorAll('[data-delivery-retry]').forEach(el=>el.onclick=()=>botRetryDelivery(el.dataset.deliveryRetry,el));host.querySelectorAll('[data-delivery-confirm]').forEach(el=>el.onclick=()=>botConfirmDelivery(el.dataset.deliveryConfirm,el))} +async function botConfirmDelivery(id,button){ + button.disabled=true; + try{ + await api(`/api/notification-deliveries/${encodeURIComponent(id)}/confirm?projectId=${encodeURIComponent(state.projectFilter)}`,{method:'POST'});toast('已记录为收到','ok'); + const item=botUI.deliveries.find(d=>d.id===id);if(item){item.status='sent';item.receiptSource='manual';item.lastError='管理员已确认收到'} + const host=$('#botsContent');if(host&&item)botPatchDeliveryRow(host,item); + }catch(e){toast(e.message,'err');button.disabled=false} +} async function botRetryDelivery(id,button){ button.disabled=true; try{ diff --git a/web/bot-approve.html b/web/bot-approve.html new file mode 100644 index 000000000..b01b2fe99 --- /dev/null +++ b/web/bot-approve.html @@ -0,0 +1 @@ +确认 Bot 操作 · LoopForge
LoopForge · 聊天操作确认

确认 Bot 将要执行的操作

正在检查登录状态…

\ No newline at end of file diff --git a/web/bot-approve.js b/web/bot-approve.js new file mode 100644 index 000000000..ef31bfa32 --- /dev/null +++ b/web/bot-approve.js @@ -0,0 +1,44 @@ +(() => { + const content=document.getElementById('content'); + const escape=s=>String(s||'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); + // The draft ID travels in the fragment; chat clients may append punctuation. + const pick=v=>(String(v||'').toLowerCase().match(/[a-f0-9]{32}/)||[''])[0]; + const linked=pick(new URLSearchParams(location.hash.slice(1)).get('draft')); + const id=linked||sessionStorage.getItem('botApproveDraft')||''; + if(linked){sessionStorage.setItem('botApproveDraft',linked);history.replaceState(null,'',location.pathname);} + async function request(path,method,body){const response=await fetch(path,{method:method||'GET',headers:method?{'Content-Type':'application/json'}:{},body:method?JSON.stringify(body||{}):undefined,credentials:'same-origin',cache:'no-store'});const data=await response.json().catch(()=>({}));if(!response.ok&&!data.draft)throw new Error(data.error||'请求未完成');return response.ok?data:Object.assign(data.draft,{notice:data.error});} + function failure(error){content.innerHTML=`

${escape(error.message)}

请回到聊天中重新发起请求。

`;} + const labels={pending:'等待确认',executing:'执行中',executed:'已执行',failed:'部分或全部未完成',needs_review:'执行中断,待核对',expired:'已过期',cancelled:'已取消'}; + function meta(d){const rows=[['机器人',d.botName],['项目',d.projectName],['状态',labels[d.status]||d.status],['有效期至',d.status==='pending'?new Date(d.expiresAt).toLocaleString():'']].filter(([,v])=>v);return `
${rows.map(([k,v])=>`
${escape(k)}
${escape(v)}
`).join('')}
`;} + function render(d){ + const ops=`
    ${(d.operations||[]).map(o=>`
  1. ${escape(String(o).replace(/^\d+\.\s*/,''))}
  2. `).join('')}
`; + const results=d.results?.length?`

执行结果

    ${d.results.map(r=>`
  1. ${escape(r)}
  2. `).join('')}
`:''; + const notice=d.notice?`

${escape(d.notice)}

`:d.reason?`

${escape(d.reason)}

`:''; + if(d.status==='pending'){ + // New requirements: pick the SOP and whether to start it right away. + // The first option is the project default (empty value), like the + // requirement form on the platform. + const options=i=>``+(d.workflows||[]).map(w=>``).join(''); + const sops=(d.newIssues||[]).length?`

新需求使用的 SOP

${d.newIssues.map(i=>`
`).join('')}`:''; + content.innerHTML=`${meta(d)}

Bot 将使用你的平台账号和项目权限执行以下操作:

${ops}${sops}${notice}

每个确认链接只能执行一次;重复点击不会重复执行。`; + document.getElementById('confirm').onclick=()=>act('confirm',{choices:[...content.querySelectorAll('.sop')].map(el=>({index:Number(el.dataset.index),workflowId:el.querySelector('select').value,start:el.querySelector('input[type=checkbox]').checked}))}); + document.getElementById('cancel').onclick=()=>act('cancel'); + return; + } + content.innerHTML=`${meta(d)}${ops}${notice}${results}${d.status==='executing'?'正在执行,页面会自动刷新…':''}

返回平台

`; + if(d.status==='executing')setTimeout(()=>load().catch(failure),2500); + } + async function act(kind,body){content.querySelectorAll('button').forEach(b=>b.disabled=true);try{render(await request(`/api/bot-drafts/${id}/${kind}`,'POST',body))}catch(error){failure(error)}} + async function load(){render(await request(`/api/bot-drafts/${id}`))} + async function start(){ + if(!/^[a-f0-9]{32}$/.test(id))throw new Error('确认链接无效'); + const auth=await request('/api/auth/current?return_url='+encodeURIComponent(location.origin+location.pathname)); + if(!auth.user||auth.user.isGuest){ + content.innerHTML='

请登录与聊天账号绑定的平台账号后确认。

'; + document.getElementById('login').onclick=()=>{if(auth.loginUrl)location.href=auth.loginUrl;else failure(new Error('登录入口不可用'));}; + return; + } + await load(); + } + start().catch(failure); +})(); diff --git a/web/bot-bind.html b/web/bot-bind.html index c9313eefa..454ced512 100644 --- a/web/bot-bind.html +++ b/web/bot-bind.html @@ -1 +1 @@ -关联企业微信账号 · LoopForge
LoopForge · 企业微信身份关联

关联你的平台账号

正在检查登录状态…

\ No newline at end of file +关联聊天账号 · LoopForge
LoopForge · 聊天身份关联

关联你的平台账号

正在检查登录状态…

\ No newline at end of file diff --git a/web/bot-bind.js b/web/bot-bind.js index f3d61aa91..0879b3f03 100644 --- a/web/bot-bind.js +++ b/web/bot-bind.js @@ -1,15 +1,23 @@ (() => { const content=document.getElementById('content'); const escape=s=>String(s||'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); - let token=new URLSearchParams(location.hash.slice(1)).get('request')||sessionStorage.getItem('wecomBindingToken')||''; - if(token){sessionStorage.setItem('wecomBindingToken',token);history.replaceState(null,'',location.pathname);} + // Plain-text chats (Feishu, DingTalk) auto-link URLs loosely and may append + // trailing punctuation or percent-escapes, so take the 64-hex token from the + // fragment or query wherever it appears instead of requiring an exact value. + const pick=v=>(String(v||'').toLowerCase().match(/[a-f0-9]{64}/)||[''])[0]; + const linked=pick(new URLSearchParams(location.hash.slice(1)).get('request'))||pick(new URLSearchParams(location.search).get('request')); + let token=linked||sessionStorage.getItem('wecomBindingToken')||''; + if(location.hash||location.search){history.replaceState(null,'',location.pathname);} + if(linked){sessionStorage.setItem('wecomBindingToken',linked);} async function request(path,body){const response=await fetch(path,{method:body?'POST':'GET',headers:body?{'Content-Type':'application/json'}:{},body:body?JSON.stringify(body):undefined,credentials:'same-origin',cache:'no-store'});const data=await response.json();if(!response.ok)throw new Error(data.error||'请求未完成');return data;} - function failure(error){content.innerHTML=`

${escape(error.message)}

请回到企业微信,向 Bot 发送“绑定账号”获取新链接。

`;} + function failure(error){content.innerHTML=`

${escape(error.message)}

请回到聊天中,向机器人发送“绑定账号”获取新链接。

`;} + // Shows what is being bound: channel, Bot, project and the masked sender. + function summary(data){const rows=[['渠道',data.channelName],['机器人',data.botName],['项目',data.projectName],['发送者',data.sender]].filter(([,v])=>v);return rows.length?`
${rows.map(([k,v])=>`
${escape(k)}
${escape(v)}
`).join('')}
`:'';} async function start(){ if(!/^[a-f0-9]{64}$/.test(token))throw new Error('绑定链接无效'); const auth=await request('/api/auth/current?return_url='+encodeURIComponent(location.origin+location.pathname)); if(!auth.user||auth.user.isGuest){ - content.innerHTML='

请登录你自己的平台账号。登录后仍需由原企微发送者确认,绑定才会生效。

'; + content.innerHTML='

请登录你自己的平台账号。登录后仍需由原聊天发送者确认,绑定才会生效。

'; document.getElementById('login').onclick=()=>{if(auth.loginUrl)location.href=auth.loginUrl;else failure(new Error('登录入口不可用'));}; const local=await request('/api/auth/local').catch(()=>({enabled:false})); if(local.enabled){const button=document.getElementById('local');button.hidden=false;button.onclick=()=>request('/api/auth/local',{}).then(start).catch(failure);} @@ -17,10 +25,10 @@ } const data=await request('/api/bot-bindings/status',{token}); if(data.status==='confirmed'){complete(data.loginName);return;} - content.innerHTML=`

当前平台账号

${escape(auth.user.chineseName)} · ${escape(data.loginName)}

绑定后,Bot 会使用此账号的个人凭据和项目权限执行你的指令。请确认这是你本人使用的账号。

群里的其他人点击链接不会直接完成绑定。

`; - document.getElementById('claim').onclick=async()=>{const button=document.getElementById('claim');button.disabled=true;try{const result=await request('/api/bot-bindings/claim',{token});if(result.status==='confirmed'){complete(result.loginName);return;}content.innerHTML=`

最后一步:由原企微发送者确认

复制下面的指令,发回提供绑定链接的 Bot。群聊请 @Bot,也可以在同一 Bot 的单聊里发送。

${escape(result.command)}

将关联到 ${escape(result.loginName)}。请勿转发确认码。

等待企业微信确认,页面会自动更新…`;document.getElementById('copy').onclick=()=>navigator.clipboard.writeText(result.command).then(()=>{document.getElementById('copy').textContent='已复制';}).catch(()=>{document.getElementById('copy').textContent='请选中上方指令复制';});poll(result.loginName);}catch(error){failure(error);}}; + content.innerHTML=`${summary(data)}

当前平台账号

${escape(auth.user.chineseName)} · ${escape(data.loginName)}

绑定后,Bot 会使用此账号的个人凭据和项目权限执行你的指令。请确认这是你本人使用的账号。

群里的其他人点击链接不会直接完成绑定。

`; + document.getElementById('claim').onclick=async()=>{const button=document.getElementById('claim');button.disabled=true;try{const result=await request('/api/bot-bindings/claim',{token});if(result.status==='confirmed'){complete(result.loginName);return;}content.innerHTML=`

最后一步:由原聊天发送者确认

复制下面的指令,发回提供绑定链接的 Bot。群聊请 @Bot,也可以在同一 Bot 的单聊里发送。

${escape(result.command)}

将关联到 ${escape(result.loginName)}。请勿转发确认码。

等待聊天中的确认,页面会自动更新…`;document.getElementById('copy').onclick=()=>navigator.clipboard.writeText(result.command).then(()=>{document.getElementById('copy').textContent='已复制';}).catch(()=>{document.getElementById('copy').textContent='请选中上方指令复制';});poll(result.loginName);}catch(error){failure(error);}}; } - function complete(login){sessionStorage.removeItem('wecomBindingToken');content.innerHTML=`

已完成关联

当前企微账号已关联到 ${escape(login)}。

请回到 Bot 重新发送刚才的请求。可在平台个人配置中解除绑定。

查看个人配置`;} + function complete(login){sessionStorage.removeItem('wecomBindingToken');content.innerHTML=`

已完成关联

当前聊天账号已关联到 ${escape(login)}。

请回到 Bot 重新发送刚才的请求。可在平台个人配置中解除绑定。

查看个人配置`;} function poll(login){const timer=setInterval(async()=>{try{const data=await request('/api/bot-bindings/status',{token});if(data.status==='confirmed'){clearInterval(timer);complete(login);}}catch(error){clearInterval(timer);failure(error);}},2500);} start().catch(failure); })(); diff --git a/web/bots.css b/web/bots.css index 6e7195323..3583e7443 100644 --- a/web/bots.css +++ b/web/bots.css @@ -240,3 +240,5 @@ #botsContent *,#botsContent *::before,#botsContent *::after,#botsView .bot-heading *{animation:none!important;transition:none!important} #botsContent .bot-spin svg{animation:none!important} } +.bot-channel-fixed{display:flex;align-items:center;gap:8px;min-height:32px} +.bot-entry-actions{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px} diff --git a/web/embed.go b/web/embed.go index 59b10f4e6..7c08b01e7 100644 --- a/web/embed.go +++ b/web/embed.go @@ -2,5 +2,5 @@ package web import "embed" -//go:embed boot.js i18n.js i18n-en.js preferences.css login.html login.js bot-bind.html bot-bind.js boot-theme.js gongfeng-oauth-done.html gongfeng-oauth-done.js index.html vscode.html vscode.js vscode.css app.js app assistant-models.css chat-codex.css orchestrate.js bots.css mcp-setup.txt styles.css assets.css platform.css observability.css artifacts.css sops.css sync.css access.css product-v3.css product-v4.css images/credentials/*.svg images/credentials/kimi-code.png images/loopforge-logo.png favicon.ico favicon.png favicon.svg apple-touch-icon.png mcp-icons monaco vendor vscode-web extensions all:native-chat +//go:embed boot.js i18n.js i18n-en.js preferences.css login.html login.js bot-bind.html bot-bind.js bot-approve.html bot-approve.js boot-theme.js gongfeng-oauth-done.html gongfeng-oauth-done.js index.html vscode.html vscode.js vscode.css app.js app assistant-models.css chat-codex.css orchestrate.js bots.css mcp-setup.txt styles.css assets.css platform.css observability.css artifacts.css sops.css sync.css access.css product-v3.css product-v4.css images/credentials/*.svg images/credentials/kimi-code.png images/loopforge-logo.png favicon.ico favicon.png favicon.svg apple-touch-icon.png mcp-icons monaco vendor vscode-web extensions all:native-chat var Files embed.FS diff --git a/web/i18n-en.js b/web/i18n-en.js index c206952fa..fe5f05459 100644 --- a/web/i18n-en.js +++ b/web/i18n-en.js @@ -41,7 +41,7 @@ Object.assign(window.LF_I18N_EN,{"了解 LoopForge 能做什么":"Discover what Object.assign(window.LF_I18N_EN,{"AI 聊天和 Agent 任务需要模型 API Key;下面的脚本体验无需 Key。":"AI chat and Agent tasks need a model API key; the script demo below does not.","项目用来归集需求、SOP 和运行记录。可以直接用体验项目,也可以新建项目,不要求绑定仓库。":"Projects organize requirements, SOPs and runs. Use the demo project or create your own; a repository is not required.","接入 GitHub 代码仓库":"Connect a GitHub repository","分析或修改仓库代码、提交 PR 时再配置仓库和 GitHub Token;聊天、文档生成及无仓库 SOP 可以跳过。":"Configure a repository and GitHub token when analyzing or changing repository code or submitting PRs. Chat, document generation and SOPs without a repository can skip this.","可选":"Optional"}); Object.assign(window.LF_I18N_EN,{"按":"Pulls issues from ","当前项目已绑定的 GitHub 仓库":"GitHub repositories bound to this project","拉取 Issue,使用个人设置里的 GitHub 访问令牌。自动同步间隔是平台级配置。":", using the GitHub access token from personal settings. The auto-sync interval is a platform-wide setting.","当前项目还没有绑定 GitHub 仓库,去「项目配置」添加。":"No GitHub repository is bound to this project yet. Add one in Project settings.","建单后按所选 SOP 启动。":"After creation, the selected SOP starts.","保存只改本平台记录,不会回写外部系统。":"Saving only updates this platform record; nothing is written back to external systems.","开启自动同步后,外部同步仍会按间隔把所有已配置来源拉进项目列表。":"With auto-sync on, every configured source is still pulled into the project list at the configured interval.","所有已配置来源":"All configured sources","需求交付明细":"Requirement delivery details","帮我梳理当前项目的待办需求":"Help me review the open requirements in this project","尚未同步。绑定 GitHub 仓库后,可在「需求列表」里点击「同步 GitHub」。":"Not synced yet. After binding a GitHub repository, click \"Sync GitHub\" in Requirements.","请在代码托管平台补充勾选":"; add the missing scopes on your code hosting platform","仓库路径":"Repository path","GitHub 令牌认证":"GitHub token auth","建单后会启动 SOP。":"The SOP starts after creation.","请选择建单方式":"Choose how to create the requirement","点下方「平台需求」":"Click \"Platform requirement\" below","你已确认可以建单。点下方按钮建单并启动 SOP。":"You confirmed it is ready. Use the button below to create it and start the SOP."}); -Object.assign(window.LF_I18N_EN,{"通道管理":"Channels","添加通道":"Add channel","配置企业微信单聊或群聊通知通道,把任务进展发出去;在 SOP 设置或定时任务中选用这些通道。":"Set up WeCom direct or group chat channels for task updates, then pick them in SOP settings or scheduled jobs.","Token 管理":"Tokens","流程与任务":"Workflows & jobs","Agent 能力":"Agent capabilities","开放与平台":"Access & platform","运行环境":"Runtime","提示词任务":"Prompt jobs","SOP 触发":"SOP triggers","SOP 自动派发":"SOP auto-dispatch","编辑触发配置":"Edit trigger","配置触发":"Set up trigger","定时任务视图":"Scheduled job views","按计划执行提示词,或自动派发 SOP,整理需求、巡检项目或汇总进展。":"Run prompts on a schedule or auto-dispatch SOPs to triage requirements, check projects, or summarize progress.","刷新定时任务":"Refresh scheduled jobs","安装脚本":"Install scripts","能力资产类型":"Capability asset types","环境资产类型":"Environment asset types","添加 MCP":"Add MCP","+ 添加 MCP":"+ Add MCP","MCP、规则和技能统一在这里维护,角色按需绑定后即可使用。":"Maintain MCP servers, rules and skills here; roles can use them once bound.","已添加":"Added","个人 MCP":"Personal MCP","项目 MCP":"Project MCP","去能力资产添加":"Add in capability assets"}); +Object.assign(window.LF_I18N_EN,{"通道管理":"Channels","企业微信、飞书、钉钉机器人通道":"WeCom, Feishu and DingTalk bot channels","添加通道":"Add channel","配置企业微信、飞书、钉钉机器人通道:在聊天中查询项目,并把任务进展发出去;在 SOP 设置或定时任务中选用这些通道。点“添加通道”后在“渠道”中选择。":"Set up WeCom, Feishu and DingTalk bot channels to query projects in chat and send task updates, then pick them in SOP settings or scheduled jobs. Choose the channel after clicking Add channel.","Token 管理":"Tokens","流程与任务":"Workflows & jobs","Agent 能力":"Agent capabilities","开放与平台":"Access & platform","运行环境":"Runtime","提示词任务":"Prompt jobs","SOP 触发":"SOP triggers","SOP 自动派发":"SOP auto-dispatch","编辑触发配置":"Edit trigger","配置触发":"Set up trigger","定时任务视图":"Scheduled job views","按计划执行提示词,或自动派发 SOP,整理需求、巡检项目或汇总进展。":"Run prompts on a schedule or auto-dispatch SOPs to triage requirements, check projects, or summarize progress.","刷新定时任务":"Refresh scheduled jobs","安装脚本":"Install scripts","能力资产类型":"Capability asset types","环境资产类型":"Environment asset types","添加 MCP":"Add MCP","+ 添加 MCP":"+ Add MCP","MCP、规则和技能统一在这里维护,角色按需绑定后即可使用。":"Maintain MCP servers, rules and skills here; roles can use them once bound.","已添加":"Added","个人 MCP":"Personal MCP","项目 MCP":"Project MCP","去能力资产添加":"Add in capability assets"}); Object.assign(window.LF_I18N_EN,{"访问密钥":"Access keys","我的访问密钥":"My access keys","创建访问密钥":"Create access key","撤销访问密钥":"Revoke access key","密钥名称":"Key name","仅复制密钥":"Copy key only","访问密钥已复制":"Access key copied","访问密钥已撤销":"Access key revoked","访问密钥已创建,请立即复制":"Access key created. Copy it now.","访问密钥列表加载失败":"Failed to load access keys","正在加载访问密钥…":"Loading access keys…","访问密钥必须绑定到登录用户。":"Access keys must belong to a signed-in user.","创建和撤销访问密钥":"Create and revoke access keys","创建和撤销访问密钥,REST API 与 MCP 共用同一个访问密钥。":"Create and revoke access keys. REST API and MCP share the same key."}); Object.assign(window.LF_I18N_EN,{"给同事开账号":"Create accounts for teammates","填好用户名和初始密码,点创建,再把登录信息发给同事。同事忘记密码时可以在下面重置。":"Enter a login name and an initial password, create the account, then send the sign-in details to your teammate. Reset the password below if they forget it.","登录页目前允许自助注册。也可以在这里直接创建账号,再把登录信息发给同事;同事忘记密码时可以在下面重置。":"Self sign-up is open on the login page. You can also create accounts here and send the sign-in details to your teammate; reset the password below if they forget it.","登录页的自助注册已关闭,同事的账号要在这里创建:填好用户名和初始密码,点创建,再把登录信息发给对方。忘记密码时可以在下面重置。":"Self sign-up is closed, so create teammates' accounts here: enter a login name and an initial password, create the account, then send them the sign-in details. Reset the password below if they forget it.","用户名,如 alice":"Login name, e.g. alice","初始密码,至少 6 位":"Initial password, at least 6 characters","新密码,至少 6 位":"New password, at least 6 characters","随机生成":"Generate","随机":"Generate","创建账号":"Create account","搜索用户名...":"Search login names...","用户名密码":"Password sign-in","GitHub / 网关登录":"GitHub / gateway sign-in","已配 CodeBuddy Key":"Has a CodeBuddy key","未配 CodeBuddy Key":"No CodeBuddy key","无平台密码":"No platform password","通过 GitHub 或网关登录的账号没有平台密码":"Accounts that sign in through GitHub or a gateway have no platform password","重置密码":"Reset password","确认重置":"Reset","没有匹配的账号":"No matching accounts","还没有账号":"No accounts yet","复制登录信息":"Copy sign-in details","已复制登录信息":"Sign-in details copied","账号已创建":"Account created","密码已重置":"Password reset","密码只显示这一次,请现在发给对方。":"The password is shown only once; send it now.","共享 CodeBuddy Key":"Shared CodeBuddy Key","给没配 Key 的成员用":"For members without their own key","成员没配自己的 CodeBuddy API Key 时,对话和 SOP 会用这里的 Key,并且只能用下面勾选的模型。成员配了自己的 Key 就用自己的,不受限制。":"Members without their own CodeBuddy API Key chat and run SOPs on this key, limited to the models checked below. Members with their own key keep using it without limits.","允许使用的模型":"Allowed models","列表里没有的模型":"Models not in the list","模型 ID,多个用逗号分隔":"Model IDs, separated by commas","默认模型":"Default model","请先勾选模型":"Check a model first","正在读取可用模型…":"Loading available models…","这个网络环境暂时没有读到模型列表,可以在下面直接填模型 ID。":"No model list was found for this network environment yet; enter model IDs below.","ck_ 开头的 CodeBuddy API Key":"CodeBuddy API Key starting with ck_","已配置,留空保持不变":"Configured; leave blank to keep it","停用共享 Key":"Stop sharing the key","使用旧配置":"Legacy setting","共享 Key 已保存":"Shared key saved","共享 Key 已停用":"Shared key turned off","还没有配置共享 Key,没配 Key 的成员需要先配自己的 Key 才能对话和跑 SOP。":"No shared key yet; members without their own key must add one before chatting or running SOPs.","停用后,没配自己 Key 的成员将不能再用共享 Key 对话和跑 SOP。确定停用?":"Members without their own key will no longer chat or run SOPs on the shared key. Turn it off?","请至少勾选一个允许使用的模型":"Check at least one allowed model","请填写 CodeBuddy API Key":"Enter the CodeBuddy API Key","平台管理分区":"Platform admin sections","没指定模型时用它;SOP 角色的模型不在允许范围内时也改用它。":"Used when no model is chosen, and for SOP roles whose model is not allowed."}); Object.assign(window.LF_I18N_EN,{"统计":"Usage","切换项目":"Switch project","/ Agent 原生研发协作平台":"/ Agent-native delivery platform","全局导航":"Main navigation","展开导航":"Expand navigation","收起导航":"Collapse navigation","返回首页":"Back to home","展开设置导航":"Expand settings navigation","收起设置导航":"Collapse settings navigation","个人配置":"Personal settings","个人配置与模型":"Personal settings & models","个人信息与操作":"Profile & actions","点击查看个人信息":"Click to view your profile","认证换票未通过":"Sign-in token exchange failed","当前为访客身份":"Browsing as a guest","搜索任务…":"Search tasks…","搜索任务":"Search tasks","任务状态筛选":"Filter tasks by status","任务责任人筛选":"Filter tasks by assignee","我负责的":"Assigned to me","待处理 / 暂停":"Pending / paused","拖动调整侧栏宽度,双击恢复默认":"Drag to resize the sidebar; double-click to reset","调整侧栏宽度":"Resize sidebar","平台集成":"Platform integrations","代码仓库与统一认证":"Code repositories & sign-in","AI 对话与编码 CLI":"AI chat & coding CLIs","仓库授权":"Repository access","推荐凭证已全部就绪":"All recommended credentials are ready","克隆私有仓库、创建任务分支、推送交付产物":"Clone private repositories, create task branches, push deliverables","AI 对话与 SOP 的 CodeBuddy 执行器":"AI chat and the CodeBuddy executor in SOPs","AI 对话(含 Claude、GPT 等模型)和 SOP 的 CodeBuddy 执行器都使用它;网络环境按账号所在环境配置。":"Used by AI chat (including Claude, GPT and other models) and by the CodeBuddy executor in SOPs. Pick the network environment your account belongs to.","AI 对话选择 Codex 执行器,以及以 Codex 运行的 SOP 节点":"AI chat with the Codex executor, and SOP steps that run on Codex","AI 对话选择 Claude 执行器,以及以 Claude Code 运行的 SOP 节点":"AI chat with the Claude executor, and SOP steps that run on Claude Code","筛选凭证":"Filter credentials","添加凭证":"Add credential","SOP 使用需求责任人(未指定时为项目 Owner)的个人凭证,保存后对下一次任务生效。":"SOPs use the personal credentials of the requirement owner (the project owner when none is set). Changes apply from the next task.","GitHub 仓库(已绑定)":"GitHub repository (bound)","需由责任人本人在个人配置中配置":"The owner has to set this in their own Personal settings","最小接入配置需要绑定到你的账号:执行器 API Key 和 GitHub Git Token 保存在个人配置里;也可以绑定本机 CLI。":"The minimal setup is tied to your account: the executor API key and GitHub Git token are stored in Personal settings. You can also connect a local CLI.","个人配置 · 一次配齐":"Personal settings · all in one place","打开个人配置 →":"Open Personal settings →","CodeBuddy 使用个人配置中保存的 API Key":"CodeBuddy uses the API key saved in Personal settings","到个人配置补充 CodeBuddy API Key":"Add a CodeBuddy API key in Personal settings","到个人配置补充 Codex API Key":"Add a Codex API key in Personal settings","到个人配置补充 Claude Code API Key":"Add a Claude Code API key in Personal settings","将使用个人配置中的 API 凭证":"The API credentials in Personal settings will be used","已切换;请先到个人配置补充该 CLI 的 API Key":"Switched. Add this CLI's API key in Personal settings first","当前以访客身份访问平台。登录后可在左侧「个人配置」配置 GitHub Git Token 和 CodeBuddy API Key。":"You are browsing as a guest. After signing in, set your GitHub Git token and CodeBuddy API key under “Personal settings” on the left.","连接后可选择自己的仓库;公开仓库也可直接填写地址。访问令牌可在个人配置中设置。":"Once connected you can pick your own repositories; public repositories can also be entered by URL. The access token is set in Personal settings.","去个人配置配置":"Set it in Personal settings","使用项目已绑定的仓库。访问私有仓库或推送代码时需要在个人配置中配置 GitHub Token;未绑定仓库时可在任务工作区直接运行。":"Uses the repositories bound to the project. Private repositories and pushing code need a GitHub token in Personal settings; without a bound repository the run works directly in the task workspace.","请检查个人配置中的 CodeBuddy API Key 配置或网络状态后重新评测。":"Check the CodeBuddy API key in Personal settings and your network, then run the evaluation again.","请先在个人配置中配置自己的 CodeBuddy API Key":"Set your own CodeBuddy API key in Personal settings first","请先配置个人 CodeBuddy API Key":"Set your personal CodeBuddy API key first","未配置个人 CodeBuddy API Key 时只能使用平台管理员允许的模型":"Without a personal CodeBuddy API key you can only use the models a platform admin allowed","要使用其他模型,请先在个人配置中配置自己的 CodeBuddy API Key":"To use other models, set your own CodeBuddy API key in Personal settings first"}); diff --git a/web/index.html b/web/index.html index 279e1a123..24174a5ee 100644 --- a/web/index.html +++ b/web/index.html @@ -328,7 +328,7 @@

执行记录
-

通道管理

配置企业微信单聊或群聊通知通道,把任务进展发出去;在 SOP 设置或定时任务中选用这些通道。

+

通道管理

配置企业微信、飞书、钉钉机器人通道:在聊天中查询项目,并把任务进展发出去;在 SOP 设置或定时任务中选用这些通道。点“添加通道”后在“渠道”中选择。

diff --git a/webtest/tests/bot-approve.test.mjs b/webtest/tests/bot-approve.test.mjs new file mode 100644 index 000000000..cf990886c --- /dev/null +++ b/webtest/tests/bot-approve.test.mjs @@ -0,0 +1,85 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { JSDOM } from 'jsdom'; + +const root = new URL('../../web/', import.meta.url); +const html = readFileSync(new URL('bot-approve.html', root), 'utf8').replace(/]*><\/script>/, ''); +const script = readFileSync(new URL('bot-approve.js', root), 'utf8'); +const id = 'ab'.repeat(16); + +function page(routes, hash = `#draft=${id})`) { + const dom = new JSDOM(html, { url: `http://127.0.0.1/bot-approve.html${hash}`, runScripts: 'outside-only' }); + const calls = []; + dom.window.fetch = async (path, options = {}) => { + const method = options.method || 'GET'; + calls.push({ path, method, body: options.body ? JSON.parse(options.body) : undefined }); + const key = `${method} ${String(path).split('?')[0]}`; + const [status, body] = routes[key] || [404, { error: 'not found' }]; + return { ok: status < 300, status, json: async () => structuredClone(typeof body === 'function' ? body() : body) }; + }; + dom.window.eval(script); + return { dom, calls, text: () => dom.window.document.getElementById('content').textContent, $: (s) => dom.window.document.querySelector(s) }; +} +const settle = async () => { for (let i = 0; i < 20; i++) await new Promise((r) => setTimeout(r, 0)); }; +const pending = { id, status: 'pending', botName: '研发 Bot', projectName: '项目一', operations: ['1. 保存需求:登录优化'], expiresAt: '2026-11-19T12:00:00Z' }; + +test('approval page confirms once and shows the result', async () => { + let state = pending; + const p = page({ + 'GET /api/auth/current': [200, { user: { id: 43 } }], + [`GET /api/bot-drafts/${id}`]: [200, () => state], + [`POST /api/bot-drafts/${id}/confirm`]: [200, () => (state = { ...pending, status: 'executed', results: ['已完成:保存需求:登录优化'] })], + }); + await settle(); + assert.match(p.text(), /保存需求:登录优化/); + assert.match(p.text(), /研发 Bot/); + assert.equal(p.dom.window.location.hash, '', 'draft id removed from the address bar'); + p.$('#confirm').click(); + await settle(); + assert.match(p.text(), /已执行/); + assert.match(p.text(), /已完成:保存需求/); + assert.equal(p.calls.filter((c) => c.method === 'POST').length, 1); + assert.equal(p.$('#confirm'), null); +}); + +test('approval page shows invalidation and login requirements', async () => { + const invalid = page({ + 'GET /api/auth/current': [200, { user: { id: 43 } }], + [`GET /api/bot-drafts/${id}`]: [200, pending], + [`POST /api/bot-drafts/${id}/confirm`]: [409, { error: '操作已失效:聊天账号的绑定已解除或已变化', code: 'draft_invalidated', draft: { ...pending, status: 'cancelled', reason: '聊天账号的绑定已解除或已变化' } }], + }); + await settle(); + invalid.$('#confirm').click(); + await settle(); + assert.match(invalid.text(), /已取消/); + assert.match(invalid.text(), /操作已失效/); + const guest = page({ 'GET /api/auth/current': [200, { user: null, loginUrl: '/login' }] }); + await settle(); + assert.match(guest.text(), /请登录/); + const bad = page({}, '#draft=xyz'); + await settle(); + assert.match(bad.text(), /确认链接无效/); +}); + +test('approval page lets the user pick the SOP of a new requirement and start it', async () => { + const draft = { ...pending, newIssues: [{ index: 0, title: '保存需求:登录优化', workflowId: '', defaultWorkflowId: 'wf-dev', defaultWorkflowName: '研发交付' }], workflows: [{ id: 'wf-dev', name: '研发交付' }, { id: 'wf-fix', name: '缺陷修复' }, { id: 'tpl', name: '通用 · 后端交付' }] }; + const p = page({ + 'GET /api/auth/current': [200, { user: { id: 43 } }], + [`GET /api/bot-drafts/${id}`]: [200, draft], + [`POST /api/bot-drafts/${id}/confirm`]: [200, { ...pending, status: 'executed', results: ['已完成:保存需求:登录优化(SOP:缺陷修复)', '已启动:缺陷修复(运行 run-1)'] }], + }); + await settle(); + const select = p.$('.sop select'); + assert.equal(select.value, '', 'project default selected'); + assert.equal(select.options[0].textContent, '项目默认 SOP(研发交付)'); + assert.equal(select.options.length, 4); + select.value = 'wf-fix'; + p.$('.sop input[type=checkbox]').checked = true; + p.$('#confirm').click(); + await settle(); + const post = p.calls.find((c) => c.method === 'POST'); + assert.deepEqual(post.body, { choices: [{ index: 0, workflowId: 'wf-fix', start: true }] }); + assert.match(p.text(), /已启动:缺陷修复/); + assert.equal(p.dom.window.document.querySelectorAll('li.ok').length, 2); +}); diff --git a/webtest/tests/bots.test.mjs b/webtest/tests/bots.test.mjs index d592918a2..f990d7d30 100644 --- a/webtest/tests/bots.test.mjs +++ b/webtest/tests/bots.test.mjs @@ -94,3 +94,275 @@ test('bot pure helpers enforce permissions, filtering, and status labels', async assert.match(values.target, /Alice/); assert.equal(values.allowed, true); }); + +test('feishu bot is created read-only and confirms its enterprise', async (t) => { + const app = await loadApp(); + t.after(app.close); + const created = { + id: 'bot-fs', projectId: 'project-1', ownerId: 'user-1', name: 'Feishu Bot', channel: 'feishu', + botId: 'cli_app', model: 'glm-5.3-flash-ioa', enabled: true, interactionEnabled: true, writeApproval: 'readonly', + secretConfigured: true, health: 'ready', configVersion: 1, tenantState: 'awaiting_confirmation', + }; + app.router.post('/api/notification-bots', (call) => ({ ...created, ...call.body, secret: undefined })); + app.router.get('/api/notification-bots/bot-fs/tenant', { + tenantState: 'awaiting_confirmation', configVersion: 1, + candidate: { tenantKey: 'tenant-1', receivedAt: '2026-11-18T03:00:00Z', credentialVersion: 1 }, + queried: { tenantKey: 'tenant-1', tenantName: '测试企业' }, + }); + app.router.post('/api/notification-bots/bot-fs/tenant/confirm', (call) => ({ ...created, configVersion: 2, tenantState: 'locked', tenantKey: call.body.tenantKey, tenantName: '测试企业' })); + app.router.get('/api/notification-deliveries', []); + app.evaluate(`botUI.projectId=state.projectFilter;botUI.items=[];botUI.loading=false;renderBots();botOpenEditor(null)`); + const host = () => app.document.querySelector('#botsContent'); + let form = host().querySelector('#botForm'); + form.elements.channel.value = 'feishu'; + form.elements.channel.dispatchEvent(new app.window.Event('change', { bubbles: true })); + await settle(4); + form = host().querySelector('#botForm'); + assert.match(host().textContent, /App ID/); + assert.match(host().textContent, /只读查询/); + // Feishu/DingTalk: read-only or web confirmation, never automatic writes. + assert.deepEqual([...form.elements.writeApproval.options].map(o => o.value), ['readonly', 'confirm']); + assert.equal(form.elements.writeApproval.value, 'readonly'); + form.elements.writeApproval.value = 'confirm'; + assert.equal(host().querySelector('option[value="dingtalk"]').disabled, false); + form.elements.name.value = 'Feishu Bot'; + form.elements.botId.value = 'cli_app'; + form.elements.secret.value = 'app-secret'; + form.dispatchEvent(new app.window.Event('input', { bubbles: true })); + form.dispatchEvent(new app.window.Event('submit', { bubbles: true, cancelable: true })); + await settle(8); + const post = app.router.calls.find(c => c.method === 'POST' && c.path === '/api/notification-bots').body; + assert.equal(post.channel, 'feishu'); + assert.equal(post.writeApproval, 'confirm'); + assert.equal(app.evaluate(`botUI.step`), 2); + await settle(6); + assert.match(host().textContent, /tenant-1/); + assert.match(host().textContent, /与平台查询一致/); + host().querySelector('#botTenantConfirm').click(); + await settle(8); + const confirm = app.router.calls.find(c => c.path === '/api/notification-bots/bot-fs/tenant/confirm').body; + assert.equal(confirm.tenantKey, 'tenant-1'); + assert.equal(confirm.configVersion, 1); + assert.match(host().textContent, /企业已确认/); +}); + +test('records offer confirm and resend for unknown deliveries', async (t) => { + const app = await loadApp(); + t.after(app.close); + const bot = { id: 'bot-a', projectId: 'project-1', ownerId: 'user-1', name: 'A', channel: 'wecom', botId: 'wx', enabled: true, interactionEnabled: true, configVersion: 3 }; + app.router.get('/api/notification-bots/bot-a/interactions', { + connection: { state: 'connected' }, conversations: [], + messages: [ + { id: 'i-unknown', createdAt: '2026-11-18T03:00:00Z', senderId: 'u', status: 'reply_unknown', terminalReason: 'delivery_unknown', content: 'q1', reply: 'r1' }, + { id: 'i-expired', createdAt: '2026-11-18T03:00:00Z', senderId: 'u', status: 'reply_failed', terminalReason: 'route_expired', content: 'q2', reply: 'r2' }, + ], + }); + app.router.post('/api/bot-interactions/i-unknown/confirm', { id: 'i-unknown', status: 'replied' }); + app.evaluate(`botUI.projectId=state.projectFilter;botUI.items=[${JSON.stringify(bot)}];botUI.loading=false;botUI.tab='records';botUI.recordsTab='interactions';botUI.interactionBotId='bot-a';renderBots()`); + await app.evaluate(`loadBotInteractions()`); + await settle(4); + const host = app.document.querySelector('#botsContent'); + assert.match(host.textContent, /送达待核对/); + assert.match(host.textContent, /会话回复授权已过期/); + assert.ok(host.querySelector('[data-interaction-confirm="i-unknown"]')); + assert.ok(host.querySelector('[data-interaction-resend="i-unknown"]')); + assert.equal(host.querySelector('[data-interaction-resend="i-expired"]'), null); + host.querySelector('[data-interaction-confirm="i-unknown"]').click(); + await settle(6); + const call = app.router.calls.find(c => c.path === '/api/bot-interactions/i-unknown/confirm'); + assert.ok(call && call.body.actionKey.length >= 16); +}); + +test('dingtalk bot requires a CorpId and shows automatic enterprise confirmation', async (t) => { + const app = await loadApp(); + t.after(app.close); + const created = { + id: 'bot-dt', projectId: 'project-1', ownerId: 'user-1', name: 'DingTalk Bot', channel: 'dingtalk', + botId: 'ding-app', model: 'glm-5.3-flash-ioa', enabled: true, interactionEnabled: true, writeApproval: 'readonly', + secretConfigured: true, health: 'ready', configVersion: 1, tenantState: 'awaiting_confirmation', tenantExpected: 'ding-corp', + }; + app.router.post('/api/notification-bots', (call) => ({ ...created, tenantExpected: call.body.tenantKey })); + app.router.get('/api/notification-bots/bot-dt/tenant', { tenantState: 'awaiting_confirmation', configVersion: 1, queried: { tenantKey: '', tenantName: '' } }); + app.router.get('/api/notification-deliveries', []); + app.evaluate(`botUI.projectId=state.projectFilter;botUI.items=[];botUI.loading=false;renderBots();botOpenEditor(null)`); + const host = () => app.document.querySelector('#botsContent'); + let form = host().querySelector('#botForm'); + form.elements.channel.value = 'dingtalk'; + form.elements.channel.dispatchEvent(new app.window.Event('change', { bubbles: true })); + await settle(4); + form = host().querySelector('#botForm'); + assert.match(host().textContent, /Client ID/); + assert.ok(form.elements.tenantKey, 'CorpId field'); + form.elements.name.value = 'DingTalk Bot'; + form.elements.botId.value = 'ding-app'; + form.elements.secret.value = 'secret'; + form.dispatchEvent(new app.window.Event('input', { bubbles: true })); + form.dispatchEvent(new app.window.Event('submit', { bubbles: true, cancelable: true })); + await settle(4); + assert.equal(app.router.calls.filter(c => c.method === 'POST' && c.path === '/api/notification-bots').length, 0, 'saved without CorpId'); + form = host().querySelector('#botForm'); + form.elements.tenantKey.value = 'ding-corp'; + assert.ok(form.elements.cardTemplateId, 'card template field'); + form.elements.cardTemplateId.value = ' tpl-1.schema '; + form.dispatchEvent(new app.window.Event('input', { bubbles: true })); + form.dispatchEvent(new app.window.Event('submit', { bubbles: true, cancelable: true })); + await settle(8); + const post = app.router.calls.find(c => c.method === 'POST' && c.path === '/api/notification-bots').body; + assert.equal(post.channel, 'dingtalk'); + assert.equal(post.tenantKey, 'ding-corp'); + assert.equal(post.cardTemplateId, 'tpl-1.schema'); + assert.equal(post.writeApproval, 'readonly'); + assert.equal(app.evaluate(`botUI.step`), 2); + await settle(6); + assert.match(host().textContent, /ding-corp/); + assert.match(host().textContent, /自动确认/); + assert.equal(host().querySelector('#botTenantConfirm'), null, 'no manual confirm for DingTalk'); +}); + +test('dingtalk notification recipient is saved after a trial and pending deliveries need a choice', async (t) => { + const app = await loadApp(); + t.after(app.close); + const bot = { + id: 'bot-dt', projectId: 'project-1', ownerId: 'user-1', name: 'DingTalk Bot', channel: 'dingtalk', + botId: 'ding-app', model: 'glm-5.3-flash-ioa', enabled: true, interactionEnabled: false, writeApproval: 'readonly', + secretConfigured: true, health: 'ready', configVersion: 4, tenantState: 'locked', tenantKey: 'ding-corp', tenantExpected: 'ding-corp', + target: { id: 'staff-old', name: '旧成员 · st***ld', type: 'single', verifiedAt: '2026-11-19T02:00:00Z' }, + }; + const member = { id: 'staff-1', name: '成员(member) · st***-1', type: 'single' }; + app.router.get('/api/notification-bots/bot-dt/tenant', { tenantState: 'locked', configVersion: 4, queried: {} }); + app.router.get('/api/notification-bots/bot-dt/sessions', { sessions: [member], total: 1 }); + app.router.get('/api/notification-deliveries', [ + { id: 'd-unknown', botId: 'bot-dt', status: 'send_unknown', eventType: 'failed', targetName: '旧成员', createdAt: '2026-11-19T02:00:00Z', lastError: '钉钉返回服务端错误,送达结果未知' }, + ]); + let puts = 0; + app.router.put('/api/notification-bots/bot-dt', (call) => { + puts++; + if (!call.body.pendingTargetAction) return { status: 409, body: { error: '原接收人还有 2 条待发通知,请选择取消或改发到新接收人', code: 'pending_deliveries', pending: 2 } }; + return { ...bot, configVersion: 5, target: { ...member, verifiedAt: '2026-11-19T03:00:00Z' } }; + }); + app.router.post('/api/notification-deliveries/d-unknown/confirm', {}); + app.evaluate(`afConfirm=()=>Promise.resolve(true);botUI.projectId=state.projectFilter;botUI.items=[${JSON.stringify(bot)}];botUI.loading=false;renderBots();botOpenEditor(botUI.items[0])`); + await settle(4); + app.evaluate(`botGoStep(3)`); + await settle(4); + const host = () => app.document.querySelector('#botsContent'); + assert.match(host().textContent, /通知接收人/); + assert.match(host().textContent, /试发成功/); + host().querySelector('#botRefreshSessions').click(); + await settle(6); + const radio = host().querySelector('input[name="targetId"][value="staff-1"]'); + assert.ok(radio, 'bound member listed'); + radio.checked = true; + radio.dispatchEvent(new app.window.Event('change', { bubbles: true })); + await settle(2); + host().querySelector('#botSaveTarget').click(); + await settle(10); + const calls = app.router.calls.filter(c => c.method === 'PUT' && c.path === '/api/notification-bots/bot-dt'); + assert.equal(calls.length, 2); + assert.equal(calls[0].body.targetId, 'staff-1'); + assert.equal(calls[0].body.pendingTargetAction, ''); + assert.equal(calls[1].body.pendingTargetAction, 'migrate'); + assert.equal(app.evaluate(`botUI.selected.target.id`), 'staff-1'); + assert.match(host().textContent, /试发消息已发送到/); + // Records: an unknown delivery can be confirmed as received. + app.evaluate(`botUI.tab='records';botUI.recordsTab='deliveries';botUI.deliveries=[{id:'d-unknown',botId:'bot-dt',status:'send_unknown',eventType:'failed',targetName:'旧成员',createdAt:'2026-11-19T02:00:00Z'}];renderBots()`); + await settle(4); + assert.ok(host().querySelector('[data-delivery-retry="d-unknown"]'), 'resend offered'); + host().querySelector('[data-delivery-confirm="d-unknown"]').click(); + await settle(6); + assert.ok(app.router.calls.find(c => c.method === 'POST' && c.path === '/api/notification-deliveries/d-unknown/confirm')); + assert.equal(puts, 2); +}); + +test('expired dingtalk replies offer a proactive resend only for verified senders', async (t) => { + const app = await loadApp(); + t.after(app.close); + const bot = { id: 'bot-dt', projectId: 'project-1', ownerId: 'user-1', name: 'DT', channel: 'dingtalk', botId: 'ding', enabled: true, interactionEnabled: true, configVersion: 3, tenantState: 'locked' }; + app.router.get('/api/notification-bots/bot-dt/interactions', { + connection: { state: 'connected' }, conversations: [], + messages: [ + { id: 'i-ready', createdAt: '2026-11-19T03:00:00Z', senderId: 'staff-1', status: 'reply_failed', terminalReason: 'route_expired', content: 'q1', reply: 'r1', pushResendAvailable: true }, + { id: 'i-unverified', createdAt: '2026-11-19T03:00:00Z', senderId: 'staff-2', status: 'reply_failed', terminalReason: 'route_expired', content: 'q2', reply: 'r2' }, + ], + }); + app.router.post('/api/bot-interactions/i-ready/push-resend', { id: 'i-ready', status: 'ready', sendRoute: 'push' }); + app.router.post('/api/notification-bots/bot-dt/targets/verify', { id: 'staff-2', verifiedAt: '2026-11-19T04:00:00Z' }); + app.evaluate(`afConfirm=()=>Promise.resolve(true);botUI.projectId=state.projectFilter;botUI.items=[${JSON.stringify(bot)}];botUI.loading=false;botUI.tab='records';botUI.recordsTab='interactions';botUI.interactionBotId='bot-dt';renderBots()`); + await app.evaluate(`loadBotInteractions()`); + await settle(4); + const host = app.document.querySelector('#botsContent'); + assert.ok(host.querySelector('[data-interaction-push-resend="i-ready"]')); + assert.equal(host.querySelector('[data-interaction-push-resend="i-unverified"]'), null, 'no push resend without a verified target'); + assert.equal(host.querySelector('[data-interaction-resend="i-ready"]'), null, 'webhook resend hidden after expiry'); + host.querySelector('[data-interaction-push-verify="staff-2"]').click(); + await settle(6); + assert.equal(app.router.calls.find(c => c.path === '/api/notification-bots/bot-dt/targets/verify').body.targetId, 'staff-2'); + app.document.querySelector('#botsContent [data-interaction-push-resend="i-ready"]').click(); + await settle(6); + const call = app.router.calls.find(c => c.path === '/api/bot-interactions/i-ready/push-resend'); + assert.ok(call && call.body.actionKey.length >= 16); +}); + +test('records hide manual actions for text removed by the retention cleanup', async (t) => { + const app = await loadApp(); + t.after(app.close); + const bot = { id: 'bot-r', projectId: 'project-1', ownerId: 'user-1', name: 'R', channel: 'wecom', botId: 'w', enabled: true, interactionEnabled: true, configVersion: 1 }; + app.router.get('/api/notification-bots/bot-r/interactions', { + connection: { state: 'connected' }, conversations: [], + messages: [ + { id: 'i-purged', createdAt: '2026-01-01T03:00:00Z', senderId: 's', status: 'reply_failed', bodyPurgedAt: '2026-04-01T00:00:00Z' }, + { id: 'i-live', createdAt: '2026-11-19T03:00:00Z', senderId: 's', status: 'reply_failed', content: 'q', reply: 'r' }, + ], + }); + app.evaluate(`botUI.projectId=state.projectFilter;botUI.items=[${JSON.stringify(bot)}];botUI.loading=false;botUI.tab='records';botUI.recordsTab='interactions';botUI.interactionBotId='bot-r';renderBots()`); + await app.evaluate(`loadBotInteractions()`); + await settle(4); + const host = app.document.querySelector('#botsContent'); + assert.match(host.textContent, /正文已按保留期清理/); + assert.equal(host.querySelector('[data-interaction-resend="i-purged"]'), null); + assert.ok(host.querySelector('[data-interaction-resend="i-live"]')); + app.evaluate(`botUI.recordsTab='deliveries';botUI.deliveries=[{id:'d-purged',botId:'bot-r',status:'failed',eventType:'failed',targetName:'群',contentSource:'purged',createdAt:'2026-01-01T00:00:00Z'}];renderBots()`); + await settle(4); + assert.ok(app.document.querySelector('#botsContent [data-delivery-row="d-purged"]'), 'delivery row rendered'); + assert.match(app.document.querySelector('#botsContent').textContent, /正文已按保留期清理/); + assert.equal(app.document.querySelector('#botsContent [data-delivery-retry="d-purged"]'), null); +}); + +test('new bot form lets the creator pick the answer model and go back to the default', async (t) => { + const app = await loadApp(); + t.after(app.close); + app.router.get('/api/assistant/models', { default: 'default', models: [{ id: 'glm-test', name: 'GLM Test', family: 'glm' }, { id: 'codex:gpt-x', name: 'GPT X', family: 'gpt', engine: 'codex' }] }); + app.router.post('/api/notification-bots', (call) => ({ id: 'bot-fs2', projectId: 'project-1', ownerId: 'user-1', name: call.body.name, channel: 'feishu', botId: call.body.botId, model: call.body.model, enabled: true, interactionEnabled: true, writeApproval: 'readonly', secretConfigured: true, configVersion: 1, tenantState: 'awaiting_confirmation' })); + app.router.get('/api/notification-bots/bot-fs2/tenant', { tenantState: 'awaiting_confirmation', configVersion: 1, queried: {} }); + app.router.get('/api/notification-deliveries', []); + await app.evaluate(`window.AgentFlowAssistantModels.load()`); + app.evaluate(`botUI.projectId=state.projectFilter;botUI.items=[];botUI.loading=false;renderBots();botOpenEditor(null)`); + await settle(4); + const host = () => app.document.querySelector('#botsContent'); + let form = host().querySelector('#botForm'); + form.elements.channel.value = 'feishu'; + form.elements.channel.dispatchEvent(new app.window.Event('change', { bubbles: true })); + await settle(4); + assert.match(host().textContent, /回答模型/); + const open = () => { host().querySelector('[data-model-toggle]').click(); return [...app.document.querySelectorAll('#botModelPicker .ai-model-option')]; }; + let options = open(); + assert.deepEqual(options.map(o => o.dataset.id), ['default', 'glm-test'], 'default plus CodeBuddy models only'); + options[1].click(); + await settle(2); + assert.equal(host().querySelector('[name="model"]').value, 'glm-test'); + options = open(); + options[0].click(); + await settle(2); + assert.equal(host().querySelector('[name="model"]').value, 'default'); + open()[1].click(); + await settle(2); + form = host().querySelector('#botForm'); + form.elements.name.value = '飞书 Bot'; + form.elements.botId.value = 'cli_x'; + form.elements.secret.value = 's'; + form.dispatchEvent(new app.window.Event('input', { bubbles: true })); + form.dispatchEvent(new app.window.Event('submit', { bubbles: true, cancelable: true })); + await settle(8); + const post = app.router.calls.find(c => c.method === 'POST' && c.path === '/api/notification-bots'); + assert.equal(post.body.model, 'glm-test'); +});