dsh-im-lark 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.en.md +77 -3
  2. package/README.md +71 -3
  3. package/docs/adr/0012-/346/250/241/345/236/213/345/267/245/345/205/267/344/270/216/346/226/207/346/241/243/350/203/275/345/212/233.md +32 -0
  4. package/docs/adr/0013-/346/214/211/345/272/224/347/224/250/346/235/203/351/231/220/346/263/250/345/206/214/346/250/241/345/236/213/345/267/245/345/205/267.md +32 -0
  5. package/docs/adr/README.md +2 -0
  6. package/lib/client.js +226 -13
  7. package/lib/src/host/agent-tool-sync.js +37 -0
  8. package/lib/src/host/agent-tools.js +442 -0
  9. package/lib/src/host/bot-identity.js +40 -0
  10. package/lib/src/host/config.js +8 -0
  11. package/lib/src/host/docs.js +365 -0
  12. package/lib/src/host/inbound-media.js +150 -0
  13. package/lib/src/host/inbound-pipeline.js +79 -3
  14. package/lib/src/host/index.js +109 -69
  15. package/lib/src/host/lark/api.js +56 -0
  16. package/lib/src/host/lark/app-info.js +72 -0
  17. package/lib/src/host/lark/docs.js +210 -0
  18. package/lib/src/host/lark/slash-commands.js +197 -0
  19. package/lib/src/host/local-file.js +49 -0
  20. package/lib/src/host/panel-host.js +12 -0
  21. package/lib/src/host/panel-runner.js +135 -0
  22. package/lib/src/host/permissions.js +226 -0
  23. package/lib/src/host/routing.js +57 -0
  24. package/lib/src/host/session-text.js +14 -0
  25. package/lib/src/host/slash-panel.js +226 -0
  26. package/lib/src/host/stack.js +41 -11
  27. package/lib/src/host/standalone-command.js +12 -2
  28. package/lib/src/host/standalone-models.js +275 -0
  29. package/lib/src/host/standalone-send.js +2 -1
  30. package/lib/src/host/standalone-status.js +36 -0
  31. package/lib/src/host/standalone.js +84 -32
  32. package/lib/src/host/state/credentials.js +5 -35
  33. package/lib/src/host/state/docs.js +92 -0
  34. package/lib/src/host/workspaces.js +10 -0
  35. package/lib/src/shared/version.js +2 -2
  36. package/locale/en.json +27 -1
  37. package/locale/zh.json +27 -1
  38. package/package.json +1 -1
package/README.en.md CHANGED
@@ -17,8 +17,8 @@ The two modes are mutually exclusive: the core is used only when "Use my super a
17
17
 
18
18
  ## Features
19
19
 
20
- - **Inbound**: text, rich text, images, files; voice, video and stickers get an explicit notice instead of being dropped silently
21
- - **Outbound**: Markdown cards and images; long answers are split by both character count and card size limits
20
+ - **Inbound**: text, rich text, images and files; images and files are saved to the plugin workspace (the session working directory in standalone mode) and their local paths are included in the message; voice, video and stickers get an explicit notice instead of being dropped silently
21
+ - **Outbound**: Markdown cards, images and files; long answers are split by both character count and card size limits
22
22
  - **Quoted replies**: answers quote the message that triggered them, in DMs and groups alike
23
23
  - **Processing feedback**: an incoming message gets a `Get` reaction first; `DONE` is added to the original message only after the answer is actually delivered, and the two stay side by side. Being queued is not completion (the core queues the message and runs the task asynchronously). In core mode the plugin follows the core's `completes` markers (stall notices, dispatch receipts and transfer confirmations never carry them; cancelled, failed, approval-rejected or token-limit-truncated rounds do not carry them either, because they have no normal answer; older cores do not send them at all). Standalone mode aligns reactions per turn, and reminder answers are marked at settlement time (when only some questions are answered the reminder is still waiting, so no `DONE`). On failure only `Get` remains — never a false "done". Each message is marked once. Feishu has no official "typing" API
24
24
  - **Card updates**: streaming answers update the same card in place; after platform limits (14 days, 5 QPS per card) a new card is sent
@@ -26,6 +26,7 @@ The two modes are mutually exclusive: the core is used only when "Use my super a
26
26
  - **Groups**: only messages that @mention the bot are handled (only the @-scoped group permissions are requested); the whole group is never read
27
27
  - **Task reminders**: in standalone mode, a long turn (30 minutes by default) sends a reminder with elapsed time and conclusion when it ends; a failed or externally aborted turn is reported immediately with the original error. A stop pressed by the owner is not a failure
28
28
  - **Panel**: create an app with one scan on the Web plugin page, or enter App ID / App Secret manually; see connection state, toggle send/receive and core mode, change message behaviour; in standalone mode, adjust in-turn steering, session workspace, question/approval reminders and long-task reminders
29
+ - **Permission awareness**: after binding, the plugin reads the app's enabled permissions and registers model tools accordingly; a collapsed "Bot permissions" card shows the chat/doc permission count in its title once bound, and expands to the enabled tools plus the matching chat/document scopes
29
30
 
30
31
  > Question cards have been verified in a real Feishu tenant in both standalone and core mode, in **both shapes — one question and several questions inside one form**: single and multi select are distinguished by card copy, several questions are laid out as ①②③ with bold headings, and a submission returns the checked options plus the "other" input per question. Rejected answers give visible feedback in both modes: a card that can still be answered is **re-rendered whole** with the reason (Feishu form submissions are one-shot; a text-only hint would leave the card unclickable), previously checked and typed content is restored, and you can adjust and submit again; a card that can no longer be answered becomes invalid and loses its checkboxes. `Get` / `DONE` reactions and card layout vary slightly with tenant settings; what you see is what the platform does.
31
32
 
@@ -74,6 +75,13 @@ The created app still needs its permissions and event subscriptions confirmed (n
74
75
  - `im:message:send_as_bot` (send messages as the bot)
75
76
  - `im:resource` (upload/download images and files)
76
77
  - Reaction permissions (optional): the reaction-related scopes in the console. Missing them does not break the main flow; the "processing" hint degrades to not being sent
78
+ - Cloud docs (optional, for the model tools): `docx:document:readonly`, `docx:document:write_only`,
79
+ `docx:document.block:convert`, `wiki:node:read`; add `docx:document` to create documents,
80
+ `drive:drive` to delete them, and `docs:permission.member:create` to share a document with the owner.
81
+ After adding scopes, **create and publish a version**, otherwise they do not take effect
82
+ - Read the permission list (for the panel): `application:application:self_manage`. Without it the
83
+ permission card shows a read failure and tools fall back to the full set; existing send/receive
84
+ behaviour is unaffected
77
85
  4. **Events & callbacks**:
78
86
  - "Event subscriptions": choose the **long connection** mode and subscribe to `im.message.receive_v1`;
79
87
  - "Callback configuration": add `card.action.trigger` (needed for card interactions), also over the long connection
@@ -100,10 +108,55 @@ Static config lives in the profile patch; restart to apply:
100
108
  longTaskReminders: true # standalone: send a completion reminder when a turn exceeds the threshold
101
109
  longTaskMinutes: 30 # long-task threshold (1–1440 minutes)
102
110
  failureReminders: true # standalone: report a failed or externally aborted turn immediately
111
+ agentTools: true # register model tools (send messages/images/files, read/write cloud docs)
112
+ docBaseUrl: '' # prefix for generated doc links, e.g. https://x.feishu.cn; empty falls back to the file list
113
+ docFolderToken: '' # app folder for new documents; empty uses the app root
103
114
  ```
104
115
 
105
116
  Items editable in the panel are written to `$DSH_HOME/.dsh-im-lark/config.json` and survive a reinstall. Credentials live in `credentials.json` (directory 0700, file 0600); logs and errors only ever show the last six characters of the App ID. The panel shows the full App ID and binding time; the Secret is never echoed.
106
117
 
118
+ ## Model tools
119
+
120
+ The plugin exposes its existing capabilities as model tools, available to agents in both Web and IM sessions:
121
+
122
+ | Tool | Effect |
123
+ | --- | --- |
124
+ | `lark_send_message` | Render Markdown as a Feishu card and send it to the owner's DM, with an optional title |
125
+ | `lark_send_image` | Send a local image to the owner's DM (png/jpg/jpeg/webp/gif, ≤10 MB) |
126
+ | `lark_send_file` | Send a local file to the owner's DM (≤30 MB, type inferred from the extension) |
127
+ | `lark_doc_read` | Read docx plain text by URL or token; `include_blocks` also returns blocks and `block_id`s |
128
+ | `lark_doc_create` | Create a docx with Markdown, then try to add the owner as a collaborator |
129
+ | `lark_doc_append` | Append Markdown to an existing docx |
130
+ | `lark_doc_update` | Replace the text of one text block (get `block_id` via `lark_doc_read` first) |
131
+ | `lark_doc_delete` | Delete a document **created by this plugin**; only IDs recorded in `documents.json` are accepted |
132
+
133
+ The send tools have no `to` parameter: the recipient is always the owner. `lark_send_message` sends a card
134
+ rather than plain text, so headings, lists, code blocks, quotes and tables render as Markdown; images and
135
+ files are uploaded before sending.
136
+
137
+ Doc tools use the app identity only, so new documents live in the app's cloud space. For the owner to see one in the web UI, the app must
138
+ add them as a collaborator (`docs:permission.member:create`); creation attempts this automatically and only
139
+ warns on failure. To read an existing owner document, the owner must add the app via "… → More → Add document app".
140
+ Reading a `/wiki/` link also needs `wiki:node:read` (or `wiki:wiki:readonly`).
141
+ `lark_doc_read` truncates plain text at 20,000 characters and returns at most 500 blocks.
142
+
143
+ Tools are not always registered as a full set: after binding, the plugin reads the app permissions and
144
+ registers only tools whose scopes are satisfied (only scopes usable by a tenant token count — user-only
145
+ scopes are not treated as granted); if reading fails it keeps the full set as a fallback.
146
+ The "Bot permissions" card is collapsed by default, shows the permission count in its title once bound,
147
+ and expands to the enabled tools plus only **chat and cloud-document** scopes (grouped as "Chat" and
148
+ "Cloud documents"). Use "Refresh permissions" after changing scopes to re-read immediately. Other scope
149
+ categories are not shown in this card.
150
+
151
+ > All eight tools are registered and unit-tested. On 2026-12-02 the Markdown card, image and file send
152
+ > paths were verified against a real tenant; create, read, append, update-block and delete were verified
153
+ > on a scratch document, which was then cleaned up. If reading permissions fails, the full tool set is
154
+ > still used as a fallback.
155
+
156
+ This phase covers chat sending, inbound images/files and cloud-document operations. Group management,
157
+ message management, sheets/bitable, wiki, calendar and similar capabilities are out of scope; see
158
+ [`docs/design/一期范围与后续能力.md`](docs/design/一期范围与后续能力.md) for the follow-up plan.
159
+
107
160
  ## Standalone commands
108
161
 
109
162
  | Command | Effect |
@@ -114,12 +167,25 @@ Items editable in the panel are written to `$DSH_HOME/.dsh-im-lark/config.json`
114
167
  | `/switch <index, 0, front, or session id>` | Switch the current session; `0` and `front` return to the front session |
115
168
  | `/status` | Current session and runtime state |
116
169
  | `/stop` | Abort the running turn |
170
+ | `/models` | List available models with numbers, marking the current one |
171
+ | `/model [index, provider/model-id]` | Show or switch the current session's model |
117
172
  | `/trace` | Model and elapsed time of the last answer |
118
173
  | `/send <index, 0, or session id> <text>` | Send to that session; its answer comes back here. `0` is the front session. Images allowed; mid-turn behavior follows the steer setting |
119
174
  | `/queue <index, 0, or session id> <text>` | Same as `/send`, but always queues until the target's current turn ends |
120
175
 
176
+ Numbers from `/models` are a snapshot of that listing and stay valid for 15 minutes; re-list after it expires or the catalog changes. `/model 2` switches by number, or pass `provider/model-id` directly. The switch takes effect for the current session immediately and **also becomes the default model for new sessions** (the host saves the default alongside the session choice); send a message first if there is no session yet. The current reasoning effort is preserved; if the target model does not accept it the plugin retries without any effort and says it fell back to that model's default. When none was ever chosen the host default applies. A host rejection returns only the error code and a fixed suggestion, with the raw error kept in the plugin log. The reply footer reads the model from each answer's declared `source`, so it shows the real model even when a fresh session never picked one explicitly; `/new` and `/switch` forget the previous session's model and last duration.
177
+
121
178
  `/send` behaves like a normal message: while the target session is running a turn, the panel's steer setting decides whether it joins that turn or queues. `/queue` takes the same arguments but always queues until the current turn ends, regardless of the setting. A `/switch` binding and its prefix survive restarts and are cleared by `/new`; after switching to a non-front session, replies are prefixed with `**session name>**`, and returning to the front session (index `0`) drops it. The front session is always index `0` and is omitted from `/sessions`, so returning to it does not produce a self-referential list. Switching is refused while a delivery is still waiting for its answer. If an older `direct.json` has no front-session record, `/switch 0` asks you to start a new session with `/new`.
122
179
 
180
+ The `/` command panel in the Feishu input box is aligned automatically once the plugin connects, **choosing its list by mode**:
181
+
182
+ - standalone mode uses the 11 commands above, ordered by frequency (session actions first, status and help last);
183
+ - with the core attached it switches to the core's own command table (`help` last), because the core only accepts its own commands; if that table is unavailable the plugin leaves the panel alone instead of deleting anything;
184
+ - switching mode also removes commands that only belonged to the other list (`/switch`, `/queue`, `/models`, `/model` under the core; `/pair`, `/quiet`, `/weekly` and friends under standalone), so no dead entries are left behind;
185
+ - commands left over from the older `dsh-im` bridge that this list does not implement are removed (including core-only ones such as `/compact` and `/clear`), so clicking them cannot send plain text to the model or hit an unknown command. Removal is by name, so **a hand-made command with the same name is taken over too**; hand-made commands outside the list are kept.
186
+
187
+ The panel is app-level server-side state and Feishu clients cache it for a few minutes. The sync reads before writing, writes nothing when already aligned, creates one command per second, and on partial failure only logs and retries at the next trigger (startup, enabling the connection, switching mode). Without the app slash-command scopes it likewise only logs and messaging is unaffected.
188
+
123
189
  ## CLI and slash command
124
190
 
125
191
  - **`/lark`** (Web input box): `/lark <text>` sends a piece of text to your own Feishu; `/lark status` shows whether credentials and the recipient are ready. No model involved.
@@ -130,10 +196,18 @@ The recipient is always the "owner": in standalone mode the first person who DMs
130
196
  ## Scope notes
131
197
 
132
198
  - Standalone: while a question or tool approval is waiting for the owner, the plugin sends a Feishu reminder (delay configurable): single questions with options are answered by tapping the card; multi-question, approval and free-form input still use text (reply with a number or the option text). Long-task completion reminders remain the core's job.
133
- - Standalone: received files only get a notice; their contents are not fed into the session. Images are attached as input.
199
+ - Standalone: images are attached as input; both images and files are first saved to the plugin workspace and their absolute paths are included in the message. File contents are not forwarded as input directly, but the model can read them by path.
200
+ - Core mode saves images/files to the plugin workspace and includes their paths too; images are also passed to the core through the protocol. In core mode the **session directory is decided by the core**: the plugin only guarantees the attachments live in its own workspace and reports absolute paths — whether the session can read them depends on the host's file-access policy.
201
+ - Inbound attachments live in `<workspace>/inbox/<messageId>/` and are kept for 7 days or 50 message folders, whichever comes first; only plugin-created folders in that directory are cleaned.
134
202
  - In core mode `DONE` first translates the inbound `messageId` from the core into a platform message id; that table keeps only the latest 500 entries (bounded to avoid unbounded memory): after a plugin restart, or when a message is reported complete more than 500 inbound messages later, the id cannot be resolved and no `DONE` is added — this only affects the completion reaction, never answer delivery.
135
203
  - IM cards carry a whole group of questions (protocol limit: 10) in one submission, so a prompt never splits into several cards and never leaves a card that looks clickable but can never be completed. Approvals still go back to the Web UI; beyond 200 components or 28 KB the delivery degrades to a plain-text card that still lists the questions and options.
136
204
 
205
+ ## Security boundaries
206
+
207
+ - Proactive sending (the three send tools, reminders, `/lark`, `dsh-lark-send`) only targets the owner's **direct chat**: an @ in a group affects replies only and never changes the recipient.
208
+ - `lark_send_file` / `lark_send_image` accept any absolute local path the model supplies except the plugin state directory (credentials such as `credentials.json` are rejected by real path, so symlinks do not get around it) — that is both the capability and an exfiltration surface. Turn `agentTools` off when the model should not be able to send local files at all.
209
+ - Inbound attachments are stored only for the **owner**: when the owner is known, only that person; when unknown, only direct chats — group attachments are never written to disk. Failed writes are reported in the message instead of being dropped silently.
210
+
137
211
  ## Limits (platform-determined, not implementation choices)
138
212
 
139
213
  - Event handling must return within **3 seconds**; heavy work is always done asynchronously
package/README.md CHANGED
@@ -18,8 +18,8 @@
18
18
 
19
19
  ## 能力
20
20
 
21
- - **入站**:文本、富文本、图片、文件;语音/视频/表情包等给一句明确提示,不静默丢弃
22
- - **出站**:Markdown 卡片、图片;长回答按字符与卡片体积双上限切段
21
+ - **入站**:文本、富文本、图片、文件;图片与文件保存到插件工作区(独立模式下即会话工作目录),消息里带上本地路径;语音/视频/表情包等给一句明确提示,不静默丢弃
22
+ - **出站**:Markdown 卡片、图片、文件;长回答按字符与卡片体积双上限切段
23
23
  - **引用回复**:回答引用触发它的那条消息,单聊与群里都成立
24
24
  - **处理反馈**:收到消息先打 `Get`(收到)表情,**回答真正送达后**才在原消息追加 `DONE`,两个标记并排保留——入队不算完成(核心把消息入队后任务才异步跑);核心模式按核心的 `completes` 完成标记打(停滞、派发回执、转投确认等都不带;取消、失败、审批被拒、超出 token 上限而中断的回合也不带,因为它们没有正常答复;旧核心不打),独立模式按回合对齐,提醒作答在**结算**后当场打(多题只答一半时提醒还在等,不盖 `DONE`);失败只保留 `Get`,不留「已完成」的假信号;同一消息只补一次。飞书没有官方「正在输入」接口
25
25
  - **卡片更新**:流式回答原地更新同一张卡片;超过平台限制(14 天、单卡 5 QPS)后改发新卡
@@ -32,6 +32,7 @@
32
32
  - **群聊**:只响应 @机器人 的消息(群聊权限只申请 @ 系列),不读全群
33
33
  - **任务提醒**:独立模式下,回合跑得久(默认超过 30 分钟)会在结束时发一条带耗时与结论的提醒;回合失败或被外部中止时立刻提醒并带上原始错误。主人自己按的停止不算失败
34
34
  - **面板**:Web 端插件页扫码一键创建应用,或手动填 App ID / App Secret;看连接状态、开关收发/核心接入、改消息行为;独立模式可调回合内插话、会话工作区、问题/审批提醒与长程任务提醒
35
+ - **权限感知**:绑定后读取应用已开通的权限,按权限注册模型工具;设置页新增默认收起的「机器人权限」卡片,绑定后标题显示聊天/文档权限数量,展开可看已启用工具与对应 scope
35
36
 
36
37
  > 提问卡片已在独立模式与核心模式的真实飞书租户验收,**单题与多题收进同一张表单**两种形态
37
38
  > 都跑通了:单选/多选靠卡片文案区分,多题按 ①②③ 排开、题干加粗,提交后按题号回传勾选项
@@ -88,6 +89,12 @@ dsh plugin --profile web add file:/绝对路径/dsh-im-lark
88
89
  - `im:resource`(上传/下载图片与文件)
89
90
  - 表情回应(可选):控制台里与 reaction 相关的权限。没有也不影响主流程,
90
91
  「正在处理」提示会自动降级为不发
92
+ - 云文档(可选,模型工具用):`docx:document:readonly`、`docx:document:write_only`、
93
+ `docx:document.block:convert`、`wiki:node:read`;要建文档再加 `docx:document`,
94
+ 要删文档再加 `drive:drive`,要把文档分享给主人再加 `docs:permission.member:create`。
95
+ 加完权限记得**创建并发布版本**,否则不生效
96
+ - 读取权限列表(面板用):`application:application:self_manage`。没有它时权限卡片显示读取失败,
97
+ 工具退回全量注册,不影响已有收发能力
91
98
  4. **事件与回调**:
92
99
  - 「事件订阅」选择**长连接**,订阅 `im.message.receive_v1`;
93
100
  - 「回调配置」添加 `card.action.trigger`(需要卡片交互时),同样走长连接
@@ -114,10 +121,50 @@ dsh plugin --profile web add file:/绝对路径/dsh-im-lark
114
121
  longTaskReminders: true # 独立模式:回合耗时超过阈值时发完成提醒
115
122
  longTaskMinutes: 30 # 长任务阈值(1–1440 分钟)
116
123
  failureReminders: true # 独立模式:回合失败或被外部中止时立刻提醒
124
+ agentTools: true # 注册模型工具(发消息/图片/文件、读写云文档)
125
+ docBaseUrl: '' # 生成文档链接的前缀,如 https://x.feishu.cn;留空则查文件列表
126
+ docFolderToken: '' # 新建文档放进哪个应用文件夹;留空用应用根目录
117
127
  ```
118
128
 
119
129
  面板能改的项会落到 `$DSH_HOME/.dsh-im-lark/config.json`,卸载重装后沿用。凭据单独存 `credentials.json`,目录 0700、文件 0600,日志与错误信息里只出现 App ID 尾号;面板显示完整 App ID 与绑定时间,Secret 不回显。
120
130
 
131
+ ## 模型工具
132
+
133
+ 插件把已有能力注册成模型工具,Web 会话与 IM 会话里的 agent 都能直接调用:
134
+
135
+ | 工具 | 作用 |
136
+ | --- | --- |
137
+ | `lark_send_message` | 把一段 Markdown 渲染成飞书卡片发到主人私聊,可带卡片标题 |
138
+ | `lark_send_image` | 把本机一张图片发到主人私聊(png/jpg/jpeg/webp/gif,≤10 MB) |
139
+ | `lark_send_file` | 把本机一个文件发到主人私聊(≤30 MB,类型按扩展名推断) |
140
+ | `lark_doc_read` | 按 URL 或 token 读 docx 纯文本;`include_blocks` 时附带块列表与 `block_id` |
141
+ | `lark_doc_create` | 新建 docx 并写入 Markdown,然后尝试把主人加为协作者 |
142
+ | `lark_doc_append` | 向已有 docx 末尾追加 Markdown |
143
+ | `lark_doc_update` | 替换一个文本块的文字(先用 `lark_doc_read` 取 `block_id`) |
144
+ | `lark_doc_delete` | 删除**本插件创建过**的文档;只认 `documents.json` 里登记过的 id |
145
+
146
+ 发送类工具没有 `to` 参数,收件人恒为主人。`lark_send_message` 走卡片而不是纯文本,因此
147
+ 标题、列表、代码块、引用和表格能按 Markdown 渲染;图片与文件先上传再发送。
148
+
149
+ 文档工具只用应用身份:新建的文档在「应用云空间」,
150
+ 要主人在网页端看到,需要 `docs:permission.member:create` 把它加为协作者(创建时会自动尝试,
151
+ 失败只给警告);读主人已有的文档,则需要所有者在文档「… → 更多 → 添加文档应用」里把应用加进去。
152
+ 读 `/wiki/` 链接还需要 `wiki:node:read`(或 `wiki:wiki:readonly`)。
153
+ `lark_doc_read` 的纯文本截断到 20,000 字符,块列表最多 500 条。
154
+
155
+ 工具不是无条件全部注册:绑定后插件读取应用权限,只注册 scope 满足条件的工具(只统计 tenant token 能用的
156
+ scope,user-only 的不算已授权);读取失败时保留全量
157
+ 工具兜底。设置页的「机器人权限」卡片默认收起,绑定后标题显示权限数量,展开只列出**聊天与云文档**
158
+ 相关的 scope(按「聊天」「云文档」分组),以及已启用的工具;改完权限后可以点「刷新权限」立即重读。
159
+ 其他类别的 scope 不在这张卡片里展示。
160
+
161
+ > 这八个工具已注册并有单元测试覆盖。2026-12-02 已在真实租户验收:Markdown 卡片、图片、文件三条
162
+ > 发送链路成功;文档的建、读、追加、改块、删除五条链路成功,测试文档已清理。权限读取失败时仍按
163
+ > 全量工具兜底。
164
+
165
+ 本期覆盖聊天发送、入站图片/文件与云文档操作;群管理、消息管理、表格/多维表格、知识库、日历等能力
166
+ 不在本期范围,后续规划见 [`docs/design/一期范围与后续能力.md`](docs/design/一期范围与后续能力.md)。
167
+
121
168
  ## 独立会话命令
122
169
 
123
170
  | 命令 | 作用 |
@@ -128,12 +175,25 @@ dsh plugin --profile web add file:/绝对路径/dsh-im-lark
128
175
  | `/switch <序号、0、front 或会话 id>` | 切换当前会话;`0` 与 `front` 回到前台会话 |
129
176
  | `/status` | 当前会话与运行状态 |
130
177
  | `/stop` | 中止正在跑的回合 |
178
+ | `/models` | 列出可用模型与序号,标出当前模型 |
179
+ | `/model [序号、provider/模型ID]` | 查看或切换当前会话的模型 |
131
180
  | `/trace` | 查看上一条回答的模型与耗时 |
132
181
  | `/send <序号、0 或会话 id> <内容>` | 发到指定会话,那一轮的回答回到这里;`0` 是前台会话;可附图;回合正在跑时按「回合内插话」设置处理 |
133
182
  | `/queue <序号、0 或会话 id> <内容>` | 与 `/send` 相同,但始终排队到目标当前回合之后 |
134
183
 
184
+ `/models` 的序号是那一次列表的快照,15 分钟内有效,过期或换了列表要重新列一次;`/model 2` 按序号切换,也可以直接写 `provider/模型ID`。切换对当前会话立即生效,**同时会成为新会话的默认模型**(宿主在记录会话选择时一并保存默认值);没有会话时先发一条消息再切。切换会保留当前会话已用的推理等级;目标模型不接受该等级时自动去掉它重试一次,并说明「已改用目标模型的默认」;从未选过时交给宿主默认。宿主拒绝时只回错误码与固定建议,原始错误只进插件日志。回复末尾的状态行里,模型取自每条回答声明的 `source`,新会话没有显式选过模型也会显示实际用的那个;`/new` 与 `/switch` 会忘掉上一个会话的模型与上一条耗时。
185
+
135
186
  `/send` 与普通消息一样,在目标会话正在跑回合时按面板的「回合内插话」设置决定插进当前回合还是排队;`/queue` 参数相同,但始终排队到当前回合之后,不受该设置影响。`/switch` 的换绑与前缀会跨重启保留,`/new` 会清掉;切到非前台会话后,回复前会加 `**会话名>**`,切回前台(序号 `0`)后前缀消失。前台会话固定为序号 `0`,不出现在 `/sessions` 里,避免回到前台后列表里又套一个自己。目标会话还有投递在等回答时不允许切换。旧版 `direct.json` 没有前台记录时,`/switch 0` 会提示用 `/new` 重新建会话。
136
187
 
188
+ 飞书输入框的 `/` 指令面板由插件在连接后自动对齐,**按当前模式选清单**:
189
+
190
+ - 独立模式用上表这 11 条,按使用频率排序(会话操作在前,状态与说明在后);
191
+ - 接入核心后改用核心的命令表(`help` 垫底),因为核心只认自己的命令;拿不到命令表就不接管面板,也不删任何东西;
192
+ - 换模式时会把另一套清单里多出来的命令一起清掉(`/switch`、`/queue`、`/models`、`/model` 之于核心;`/pair`、`/quiet`、`/weekly` 等之于独立模式),避免留下点了没用的入口;
193
+ - 旧桥 `dsh-im` 注册、本清单不支持的指令会被删掉(含 `/compact`、`/clear` 这类核心才有的),避免点了以后当普通文本发给模型或被当作未知命令。删除按名字进行,所以**手工在飞书后台建的同名条目也会被接管**;名单之外的手工条目保留。
194
+
195
+ 面板是应用级服务端状态,改完飞书客户端有几分钟缓存;同步先读后写、已对齐时不写平台,创建按秒级间隔逐条进行,中途失败只记日志并在下次触发(启动、打开接入开关、切换模式)时重试;缺少应用指令读写权限时同样只记日志,不影响收发消息。
196
+
137
197
  ## 命令行与斜杠命令
138
198
 
139
199
  - **`/lark`**(Web 输入框):`/lark <正文>` 把一段文字发到自己的飞书;`/lark status`
@@ -147,7 +207,9 @@ dsh plugin --profile web add file:/绝对路径/dsh-im-lark
147
207
 
148
208
  - 独立模式:问题与工具授权在等主人时,插件发飞书提醒(延迟可调):单题有选项的直接
149
209
  点卡片作答,多题/审批/自由输入仍用文字(回序号或选项文字)。长任务完成提醒仍由核心负责。
150
- - 独立模式收到文件只给一句提示,不把文件内容送进会话;图片会作为输入附上。
210
+ - 独立模式收到图片会作为输入附上;图片与文件都会先保存到插件工作区,消息里带绝对路径。文件内容不直接作为输入转发,模型可按路径读取。
211
+ - 核心模式同样把图片/文件保存到插件工作区并带上路径;图片还会按协议交给核心。核心模式下**会话目录由核心决定**,插件只保证附件存在自己的工作区、并给出绝对路径;能不能按这个路径读到文件取决于宿主的文件访问策略。
212
+ - 入站附件在 `<工作区>/inbox/<messageId>/`,保留 7 天、最多 50 条消息目录,超出的自动清理;只清这一层自己建的目录,不动工作区其他文件。
151
213
  - 核心模式的 `DONE` 要先把握手的入站 `messageId` 翻成平台消息 id,这张表只留最近 500 条
152
214
  (有界,防常驻内存增长):插件重启后,或一条消息在 500 条之后才被核心报完成,
153
215
  就翻不到平台 id、不打 `DONE`——只影响完成表情,不影响答案送达。
@@ -155,6 +217,12 @@ dsh plugin --profile web add file:/绝对路径/dsh-im-lark
155
217
  「点了按钮但永远答不完」的卡,也不会把一次提问拆成多张卡。审批仍交回 Web 端;超过
156
218
  200 组件或 28 KB 时降级为纯文本卡片,题目与选项照常发出。
157
219
 
220
+ ## 安全边界
221
+
222
+ - 主动发送(三个发送工具、提醒、`/lark`、`dsh-lark-send`)只发主人**单聊**:群里的 @ 只影响回复,不会把收件人改成群。
223
+ - `lark_send_file` / `lark_send_image` 接受模型给的任意本机绝对路径(插件状态目录除外:`credentials.json` 等凭据文件按真实路径拒绝,符号链接也绕不过去),这既是能力也是外传面。不需要模型能把本机文件发出去时,关掉 `agentTools`(整组工具都不注册)。
224
+ - 入站附件只落**主人**发来的图片与文件:主人已知时只认本人,主人未知时只认私聊,群消息不落盘;落盘失败的附件会在消息里说明,不静默丢。
225
+
158
226
  ## 限制(平台决定,不是实现取舍)
159
227
 
160
228
  - 事件处理必须 **3 秒内** 返回,重活一律异步做
@@ -0,0 +1,32 @@
1
+ # 0012 在插件内提供模型工具与云文档能力
2
+
3
+ ## 背景
4
+
5
+ 官方 `@deepseek-ai/dsh-feishu` connector 用 `lark-cli` + 用户扫码登录,本机按它给的步骤启用失败;它和本插件的应用凭据也是两套身份。用户要的是「agent 干活时能发消息、发图片、读写文档」,不想再维护第二个飞书客户端和一套登录。
6
+
7
+ 本插件已经有应用凭据(`tenant_access_token`)、主人 `openid`、出站发送链路与状态目录,缺的只是把这几样暴露成模型工具,并把 docx 的读、建、追加、改、删接上。平台事实与 scope 见 [`../protocol/00-飞书开放平台事实.md`](../protocol/00-飞书开放平台事实.md) 第 15 节。
8
+
9
+ ## 决策
10
+
11
+ - **不接官方 connector,也不复用 `lark-cli` 登录**:在 `dsh-im-lark` 内用现有应用凭据原生实现工具。`lark-cli` 即使指向同一个应用,也拿不到本插件的 token;同一个应用同时跑两个长连接客户端还会互抢事件,而文档链路并不需要长连接。
12
+ - **只用应用身份**:不引入 `user_access_token` / OAuth。代价是应用建的文档在「应用云空间」,用户网页端看不到;创建后尽力把主人加成协作者(`docs:permission.member:create`),失败只回警告、不回滚文档。读用户已有文档则要求所有者在「添加文档应用」里授权。
13
+ - **固定八个工具**:`lark_send_message`、`lark_send_image`、`lark_send_file`、`lark_doc_read`、`lark_doc_create`、`lark_doc_append`、`lark_doc_update`、`lark_doc_delete`。发送类固定发给主人**单聊**(群里的 `@` 不会改变收件人),不提供 `to` 参数;这与 `dsh-lark-send` 的安全边界一致,避免模型把内容发到任意会话。
14
+ - **删除只动自己登记过的文档**:`state/docs.ts` 维护 `documents.json`(0600、上限 200 条、只存元数据)。`lark_doc_delete` 先查登记表,没有记录一律拒绝;不能靠标题或模型自述判断归属。
15
+ - **工具注册走可选注入**:`injectOptional(ctx, 'tools', ...)`,`apply()` 仍是 `void`;宿主没有 `tools` 服务时插件照常收发消息。注册返回 disposer,交给作用域清理;`agentTools: false` 时不注册。
16
+ - **文档适配器收在 `lark/` 内**:`lark/docs.ts` 只描述结构化的 `LarkDocsApi` 端口,SDK 调用留在 `sdk.ts`;`host/docs.ts` 负责 URL 解析、Markdown 转块、块树裁剪与错误翻译。
17
+ - **配置开关 `agentTools` 默认开**,新增 `docBaseUrl`(生成文档链接)与 `docFolderToken`(指定应用文件夹)。
18
+
19
+ ## 后果
20
+
21
+ - Agent 在 Web 会话与 IM 会话里都能直接调这八个工具;不需要另装 connector,也不新增运行时依赖。
22
+ - 创建文档需要补 `docx:document`(或 `docx:document:create`),删除需要 `drive:drive`(或 `space:document:delete`),分享需要 `docs:permission.member:create`;缺 scope 时返回明确的缺权限文案。真机验收时这些 scope 已补齐并发布版本,建、读、追加、改块、删除五条链路均跑通(记录在 `docs/review/`,该目录不入库)。
23
+ - 用户看不到应用云空间里的文档,除非被加成协作者或文件夹被共享;这是应用身份的固有限制,不是插件缺陷。README 的能力描述必须带上这一条。
24
+ - 应用身份能读到的文档范围取决于所有者是否添加了文档应用;`raw_content` 截断到 20,000 字符,块列表同时受 500 条与 20,000 字符预算约束,超出部分明确告知模型。
25
+ - `documents.json` 是删除边界的唯一依据:手工删掉它,已建文档就再也删不了;这是有意的保守设计。
26
+
27
+ ## 备选
28
+
29
+ - **接官方 connector**:用户本地启用失败,且要再维护一套 `lark-cli` 登录;插件已有应用凭据与发送链路,重复建设,未采用。
30
+ - **让工具走 `user_access_token`**:能直接读写用户云空间、文档天然可见,但要加 OAuth 回调、刷新 token、多用户绑定,超出本次范围,未采用。
31
+ - **允许删除任意 `document_id`**:实现简单,但应用身份下一条错误 id 就可能删掉用户文档;登记表守卫更稳,未采用。
32
+ - **发送工具带 `to` 参数**:灵活,但等于把「给谁发」交给模型,和现有主人专属边界冲突,未采用。
@@ -0,0 +1,32 @@
1
+ # 0013 按应用权限注册模型工具
2
+
3
+ ## 背景
4
+
5
+ 0012 把工具一次性注册给模型。应用没开通某个 scope 时,工具仍然出现在模型面前,只有真正调用才撞 403;用户也无法在面板里确认机器人到底有哪些权限。飞书提供了「获取应用信息」接口,`data.app.scopes` 就是应用当前已开通的权限列表,可以在绑定后读取并据此裁剪工具。
6
+
7
+ ## 决策
8
+
9
+ - **绑定后读一次权限**:用 `tenant_access_token` 调 `GET /open-apis/application/v6/applications/me?lang=zh_cn`,取 `data.app.scopes`。这要求应用开通 `application:application:self_manage`(或 `admin:app.info:readonly`);没开通时读取失败,不阻断连接。
10
+ - **工具按 scope 注册**:每个工具声明「任一 / 全部」的 scope 组合,例如发图片/文件要 `im:message:send_as_bot` + `im:resource`,读文档要 `docx:document:readonly`(或 `docx:document`),建文档要 `docx:document`(或 `docx:document:create`)+ `docx:document.block:convert`。**只在调用期才需要的 scope 不参与注册判定**:解析 `/wiki/` 链接要 `wiki:node:read`(或 `wiki:wiki:readonly`),但注册期不知道模型会传什么链接,把它写成「与」会让只有 docx 权限的应用永远注册不到 `lark_doc_read`;缺它时由调用期错误文案提示。权限变化时先注销旧工具再注册新集合,模型看到的工具表始终与权限一致。
11
+ - **只认应用身份能用的 scope**:插件全程 `tenant_access_token`,`data.app.scopes[].token_types` 只含 `user` 的 scope 出现在列表里也调不通,不能算「已授权」(否则工具一调就 403);字段缺失时按未知处理、不拦。
12
+ - **读取失败退回全量工具**:网络错误、缺 `self_manage`、接口异常时保留八个工具,只在面板显示失败原因。权限查询是增强信息,不该让原本可用的能力因为一次查询失败而消失;失败时保留上一次成功的 scope 列表,避免一次网络抖动让面板从「134 项」变成空。
13
+ - **读取成功但列表为空时注册零个工具**:这表示应用确实没有开通权限,继续暴露工具只会让模型反复撞 403,和「读取失败」区分开。
14
+ - **面板新增「机器人权限」卡片**:插在「应用凭证」「消息行为」之后,默认收起;绑定且成功读取后标题追加「(N)」。展开后先列已启用的工具,再按「聊天」「云文档」分组列 scope(等宽字体 + 本地化描述,限高滚动),并提供手动刷新。只展示 `im:` 与 `docx:` / `docs:` / `drive:` / `wiki:` / `space:` 前缀,其他类别不进入卡片;N 是过滤后的数量。
15
+ - **面板契约从 2 升到 3**:快照新增 `permissions` 与 `permissions.refresh` 端点,旧 host/新 client 或新 host/旧 client 都通过契约版本拦截,提示重启。
16
+ - **权限只缓存在内存**:每次启动/换应用重新读取;面板手动刷新可重试。权限是平台状态,不落盘,避免拿旧列表误判新版本。
17
+
18
+ ## 后果
19
+
20
+ - 用户要在开放平台补 `application:application:self_manage` 才能看到权限数量;没补时卡片显示读取失败,工具仍按全量兜底。
21
+ - 应用权限列表可能有几百项(当前应用 331 项),过滤后仍有 134 项(聊天 58、云文档 76),卡片用滚动区承载;标题只显示过滤后的数量,不展开时不会撑高页面。
22
+ - 工具注册会随权限刷新重建;正在进行的工具调用不受影响,但刷新瞬间模型看到的工具表可能变化。
23
+ - 新增 scope 后必须发布应用版本才会出现在 `scopes` 里;面板刷新不会绕过发布流程。
24
+ - 权限读取多一次 OpenAPI 调用(50 次/秒限额),绑定与手动刷新各一次,成本可忽略。
25
+
26
+ ## 备选
27
+
28
+ - **继续全量注册、调用时再报错**:实现最简单,但模型会反复尝试没有权限的工具,用户也看不到权限现状,未采用。
29
+ - **面板平铺全部 331 个 scope**:信息完整但噪声过大,聊天/文档之外的能力也不在本期范围,未采用。
30
+ - **逐个 API 探测权限**:要发大量请求,部分接口还有副作用,且探测结果不等于 scope 列表,未采用。
31
+ - **读取失败就不注册任何工具**:最保守,但一次网络抖动会让消息/文档能力全部消失,未采用。
32
+ - **把权限列表落盘**:省一次请求,但应用发布新版本后旧列表会误导面板与工具注册,未采用。
@@ -16,6 +16,8 @@
16
16
  | [0009](0009-独立模式switch切换并标记回复来源.md) | 独立模式 `/switch` 切换会话并标记回复来源 | 已接受 |
17
17
  | [0010](0010-独立模式前台会话与switch-0.md) | 独立模式的前台会话与 `/switch 0` | 已接受 |
18
18
  | [0011](0011-独立模式send遵循插话设置与queue命令.md) | 独立模式 `/send` 遵循插话设置,新增 `/queue` | 已接受 |
19
+ | [0012](0012-模型工具与文档能力.md) | 在插件内提供模型工具与云文档能力 | 已接受 |
20
+ | [0013](0013-按应用权限注册模型工具.md) | 按应用权限注册模型工具 | 已接受 |
19
21
 
20
22
  新增决策时:编号连续、标题用「做了什么」而不是「为什么」、状态只有
21
23
  `提议 / 已接受 / 已废弃 / 被 NNNN 取代`。改主意不要改旧 ADR 的结论,追加新的一条