返回 CodeWhale
README.md
1 # DingTalk bridge (advanced self-hosted integration)
2
3 This source adapter connects a DingTalk enterprise robot using Stream mode to an
4 existing `codewhale serve --http` Engine. It forwards tasks, approvals, answers,
5 stop requests and Engine events; the Engine owns prompts, tools, permissions,
6 turns and conversation history. No second agent runtime is started.
7
8 Codewhale-hosted bots are the intended default product experience. This optional
9 operator-managed adapter is for an enterprise's own DingTalk application and
10 Engine; its credential and Node.js setup is not the default user onboarding flow.
11 It does not implement hosted installation, Codewhale account linking, enterprise
12 ISV authorization or a tenant credential service.
13
14 The adapter is source-ready for offline verification. No DingTalk application,
15 credential, production connection or deployment is configured by this repository.
16 Live qualification requires a separately authorized tenant and runtime.
17
18 ## Dependencies and setup
19
20 Use Node.js 22 or newer. From this directory, install the lockfile with
21 `npm ci --ignore-scripts`. The official MIT-licensed
22 [`dingtalk-stream`](https://github.com/open-dingtalk/dingtalk-stream-sdk-nodejs/tree/v2.1.7-beta.1)
23 SDK is pinned to registry release `2.1.7-beta.1`; this is an explicit prerelease
24 selection, including the SDK's reconnect and disconnect lifecycle fixes. The
25 lockfile pins its transitive dependencies. No SDK implementation was copied.
26
27 Create an enterprise robot with Stream mode and subscribe to robot messages.
28 The adapter uses the SDK's outgoing connection, not a public callback server.
29 Follow the official [Stream protocol](https://open-dingtalk.github.io/developerpedia/docs/learn/stream/protocol/)
30 and [SDK example](https://github.com/open-dingtalk/dingtalk-stream-sdk-nodejs/blob/v2.1.7-beta.1/example/index.ts).
31 Do not enable or connect an application until its owner has approved activation.
32
33 Copy `.env.example` to `.env`, fill it locally, and use
34 `node --env-file=.env src/index.mjs --validate-config` to validate without opening
35 any connection. The normal entry point is `node --env-file=.env src/index.mjs`.
36 `npm run check` performs syntax checks and does not connect.
37
38 | Setting | Meaning |
39 | --- | --- |
40 | `DINGTALK_CLIENT_ID`, `DINGTALK_CLIENT_SECRET` | Enterprise application credentials |
41 | `DINGTALK_TENANT_ALLOWLIST` | Comma-separated exact corporate IDs; both robot and sender corporation must be listed |
42 | `DINGTALK_ACTOR_ALLOWLIST` | Comma-separated exact `senderCorpId:senderStaffId` pairs |
43 | `DINGTALK_ALLOW_GROUPS` | `false` by default; only literal `true` or `false` is accepted |
44 | `DINGTALK_CONVERSATION_ALLOWLIST` | Required for groups; exact `chatbotCorpId:conversationId` pairs |
45 | `CODEWHALE_RUNTIME_URL`, `CODEWHALE_RUNTIME_TOKEN` | Existing authenticated Engine HTTP endpoint; HTTPS or loopback HTTP |
46 | `CODEWHALE_MODEL` | Existing Engine model or route, defaults to `auto` |
47 | `CODEWHALE_WORKSPACE` | Optional workspace passed to Engine thread creation |
48 | `CODEWHALE_ALLOW_SHELL` | Defaults to `false`; enabling shell availability still requires Engine approval |
49 | `DINGTALK_STATE_PATH` | Defaults to `.state/dingtalk.json` relative to the working directory |
50
51 Tenant and actor allowlists are mandatory; there is no allow-unlisted option.
52 Group requests also require `isInAtList: true` on every message. Sessions are
53 separate for each application, bot tenant, conversation, sender tenant and staff
54 ID. Group responses are visible to that group, so only enable appropriate groups.
55 Allowlisting a user does not enable trust mode or blanket approval: both are
56 always disabled on this adapter's requests.
57
58 ## Conversation controls
59
60 Send a text task, then use these commands in the same conversation and identity:
61
62 - `/status` reports the bound thread, active turn, retained admission and uncertain deliveries.
63 - `/stop` or `/interrupt` requests cancellation of that human's accepted active turn.
64 - `/allow APPROVAL_ID` or `/deny APPROVAL_ID` decides one live Engine approval.
65 - `/answer QUESTION_ID response` answers a live question. Multiple questions need one answer per line. A plain reply also answers the sole pending question for the current turn.
66 - `/help` shows the available controls.
67
68 Every approval and answer is checked against the current Engine pending request
69 and immutable recorded initiating actor. Another user's message cannot authorize
70 the turn. Incoming `sessionWebhook` credentials never enter the Engine prompt.
71
72 ## Durability and recovery
73
74 Run one process per state file. An exclusive `.lock` file rejects concurrent
75 instances. State is private (`0700` directory and `0600` file), replaced through
76 the shared fsync-backed store. Retain it across restarts; it contains sensitive
77 conversation text and short-lived reply credentials. Do not commit or publish it.
78
79 The adapter durably saves an authenticated inbox entry and its message identity
80 before acknowledging Stream delivery. It retains 2,000 completed message keys;
81 queued messages and pending admissions are independently retained. The queue is
82 bounded, and a full queue is not acknowledged. Unsupported or unauthorized input
83 is acknowledged without contacting the Engine. Text-only input is supported.
84
85 Turn submission uses the shared bridge-core admission helper. When the Engine
86 advertises both operation idempotency and lookup, the original request and key
87 are retained, and recovery queries that exact key before retrying. Only an exact
88 404 permits the unchanged request to be submitted again. Conflicts and lookup
89 failures retain the receipt. Without both capabilities, an uncertain submission
90 is never repeated. Reconnect resumes the same Engine event cursor and accepted
91 turn. SDK Stream reconnect is enabled independently of Engine SSE reconnect.
92
93 Each output chunk is saved before sending and marked in flight before its one
94 HTTP attempt. A timeout, rejected response, restart during sending or uncertain
95 receipt marks that chunk uncertain; it is not resent automatically. A successful
96 response requires HTTP success and DingTalk `errcode: 0`. Unattempted output waits
97 when its session webhook expires, and a new authorized message can supply a fresh
98 route for the same session. Only HTTPS `oapi.dingtalk.com/robot/sendBySession`
99 routes with a future expiry are accepted; redirects are rejected. No proactive
100 message API or alternate recipient is used. SDK warning objects are suppressed
101 in the dedicated process to avoid exposing transient connection credentials.
102
103 After an abrupt shutdown, verify that no bridge process still owns the state
104 file before removing a stale `.lock`. Back up the state first. Operators must
105 reconcile uncertain thread creation, non-idempotent command effects, admission
106 without lookup support, and uncertain output against Engine/DingTalk receipts.
107 Do not remove retained receipts or reset the state as a retry mechanism. A
108 thread-creation request without a confirmed ID is deliberately not retried.
109
110 This first adapter does not support attachments, interactive cards, sharing a
111 single conversation between different staff identities, browsing existing Engine
112 threads or automatic output reconciliation. Commands interrupted during a crash
113 are reported uncertain rather than replayed. Response capture is capped at one
114 million characters with an explicit transcript notice. A host filesystem and one
115 process provide durability; this is not a distributed queue. Keep provider calls,
116 tenant messaging and production deployment separate from offline checks.
117
117 lines MARKDOWN