feishu-docs-cli 0.1.0-beta.10

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 (120) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +403 -0
  3. package/README.zh.md +402 -0
  4. package/bin/feishu-docs.js +8 -0
  5. package/dist/auth.d.ts +76 -0
  6. package/dist/auth.js +512 -0
  7. package/dist/auth.js.map +1 -0
  8. package/dist/cli.d.ts +5 -0
  9. package/dist/cli.js +197 -0
  10. package/dist/cli.js.map +1 -0
  11. package/dist/client.d.ts +28 -0
  12. package/dist/client.js +256 -0
  13. package/dist/client.js.map +1 -0
  14. package/dist/commands/authorize.d.ts +12 -0
  15. package/dist/commands/authorize.js +73 -0
  16. package/dist/commands/authorize.js.map +1 -0
  17. package/dist/commands/cat.d.ts +6 -0
  18. package/dist/commands/cat.js +159 -0
  19. package/dist/commands/cat.js.map +1 -0
  20. package/dist/commands/cp.d.ts +9 -0
  21. package/dist/commands/cp.js +70 -0
  22. package/dist/commands/cp.js.map +1 -0
  23. package/dist/commands/create.d.ts +6 -0
  24. package/dist/commands/create.js +120 -0
  25. package/dist/commands/create.js.map +1 -0
  26. package/dist/commands/delete.d.ts +6 -0
  27. package/dist/commands/delete.js +91 -0
  28. package/dist/commands/delete.js.map +1 -0
  29. package/dist/commands/info.d.ts +6 -0
  30. package/dist/commands/info.js +69 -0
  31. package/dist/commands/info.js.map +1 -0
  32. package/dist/commands/install-skill.d.ts +5 -0
  33. package/dist/commands/install-skill.js +28 -0
  34. package/dist/commands/install-skill.js.map +1 -0
  35. package/dist/commands/login.d.ts +10 -0
  36. package/dist/commands/login.js +93 -0
  37. package/dist/commands/login.js.map +1 -0
  38. package/dist/commands/ls.d.ts +6 -0
  39. package/dist/commands/ls.js +79 -0
  40. package/dist/commands/ls.js.map +1 -0
  41. package/dist/commands/mkdir.d.ts +6 -0
  42. package/dist/commands/mkdir.js +49 -0
  43. package/dist/commands/mkdir.js.map +1 -0
  44. package/dist/commands/mv.d.ts +9 -0
  45. package/dist/commands/mv.js +72 -0
  46. package/dist/commands/mv.js.map +1 -0
  47. package/dist/commands/read.d.ts +6 -0
  48. package/dist/commands/read.js +439 -0
  49. package/dist/commands/read.js.map +1 -0
  50. package/dist/commands/search.d.ts +7 -0
  51. package/dist/commands/search.js +92 -0
  52. package/dist/commands/search.js.map +1 -0
  53. package/dist/commands/share.d.ts +13 -0
  54. package/dist/commands/share.js +266 -0
  55. package/dist/commands/share.js.map +1 -0
  56. package/dist/commands/spaces.d.ts +6 -0
  57. package/dist/commands/spaces.js +43 -0
  58. package/dist/commands/spaces.js.map +1 -0
  59. package/dist/commands/tree.d.ts +6 -0
  60. package/dist/commands/tree.js +101 -0
  61. package/dist/commands/tree.js.map +1 -0
  62. package/dist/commands/update.d.ts +9 -0
  63. package/dist/commands/update.js +217 -0
  64. package/dist/commands/update.js.map +1 -0
  65. package/dist/commands/wiki.d.ts +6 -0
  66. package/dist/commands/wiki.js +286 -0
  67. package/dist/commands/wiki.js.map +1 -0
  68. package/dist/parser/block-types.d.ts +141 -0
  69. package/dist/parser/block-types.js +167 -0
  70. package/dist/parser/block-types.js.map +1 -0
  71. package/dist/parser/blocks-to-md.d.ts +26 -0
  72. package/dist/parser/blocks-to-md.js +666 -0
  73. package/dist/parser/blocks-to-md.js.map +1 -0
  74. package/dist/parser/text-elements.d.ts +13 -0
  75. package/dist/parser/text-elements.js +91 -0
  76. package/dist/parser/text-elements.js.map +1 -0
  77. package/dist/scopes.d.ts +21 -0
  78. package/dist/scopes.js +48 -0
  79. package/dist/scopes.js.map +1 -0
  80. package/dist/services/block-writer.d.ts +29 -0
  81. package/dist/services/block-writer.js +131 -0
  82. package/dist/services/block-writer.js.map +1 -0
  83. package/dist/services/doc-blocks.d.ts +8 -0
  84. package/dist/services/doc-blocks.js +26 -0
  85. package/dist/services/doc-blocks.js.map +1 -0
  86. package/dist/services/markdown-convert.d.ts +68 -0
  87. package/dist/services/markdown-convert.js +217 -0
  88. package/dist/services/markdown-convert.js.map +1 -0
  89. package/dist/services/wiki-nodes.d.ts +20 -0
  90. package/dist/services/wiki-nodes.js +46 -0
  91. package/dist/services/wiki-nodes.js.map +1 -0
  92. package/dist/types/index.d.ts +236 -0
  93. package/dist/types/index.js +5 -0
  94. package/dist/types/index.js.map +1 -0
  95. package/dist/utils/document-resolver.d.ts +26 -0
  96. package/dist/utils/document-resolver.js +46 -0
  97. package/dist/utils/document-resolver.js.map +1 -0
  98. package/dist/utils/drive-types.d.ts +4 -0
  99. package/dist/utils/drive-types.js +16 -0
  100. package/dist/utils/drive-types.js.map +1 -0
  101. package/dist/utils/errors.d.ts +23 -0
  102. package/dist/utils/errors.js +114 -0
  103. package/dist/utils/errors.js.map +1 -0
  104. package/dist/utils/member.d.ts +11 -0
  105. package/dist/utils/member.js +30 -0
  106. package/dist/utils/member.js.map +1 -0
  107. package/dist/utils/scope-prompt.d.ts +39 -0
  108. package/dist/utils/scope-prompt.js +134 -0
  109. package/dist/utils/scope-prompt.js.map +1 -0
  110. package/dist/utils/url-parser.d.ts +5 -0
  111. package/dist/utils/url-parser.js +55 -0
  112. package/dist/utils/url-parser.js.map +1 -0
  113. package/dist/utils/validate.d.ts +8 -0
  114. package/dist/utils/validate.js +15 -0
  115. package/dist/utils/validate.js.map +1 -0
  116. package/dist/utils/version.d.ts +12 -0
  117. package/dist/utils/version.js +128 -0
  118. package/dist/utils/version.js.map +1 -0
  119. package/package.json +53 -0
  120. package/skills/feishu-docs/SKILL.md +194 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 cliff-byte
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,403 @@
1
+ # feishu-docs-cli
2
+
3
+ [中文文档](./README.zh.md)
4
+
5
+ CLI tool for AI Agents to read/write Feishu (Lark) docs via shell commands.
6
+
7
+ ## Why feishu-docs-cli?
8
+
9
+ Feishu/Lark already offers the official [lark-mcp](https://github.com/larksuite/lark-openapi-mcp) MCP server. Here's what this project does differently:
10
+
11
+ | Capability | feishu-docs-cli | lark-mcp |
12
+ |------------|:-:|:-:|
13
+ | Read docs as Markdown | **Yes** — 30+ block types rendered | No — returns raw Block JSON |
14
+ | Write docs from Markdown | **Yes** — auto-convert, auto-batch (>1000 blocks) | No |
15
+ | Knowledge base tree browsing | **Yes** — `spaces` → `tree` → `cat` workflow | Search/get single node only |
16
+ | Batch read entire wiki subtree | **Yes** — `cat` recursively exports Markdown | No |
17
+ | Write safety (backup/restore) | **Yes** — auto-backup before overwrite, auto-recover on failure | No |
18
+ | OAuth user login | **Yes** — full OAuth v2 with auto-refresh, tiered scope management, interactive recovery | Yes — basic OAuth login |
19
+ | Works with any AI agent | **Yes** — standard CLI, pipes, scripts | MCP protocol only |
20
+ | IM / messaging | No | Yes |
21
+ | Bitable CRUD | Read-only (rendered as table) | Yes |
22
+ | Contact lookup | Via `share add` only | Yes |
23
+
24
+ **In short**: lark-mcp is a thin wrapper over Feishu APIs with broad coverage. feishu-docs-cli is purpose-built for **document workflows** — it lets AI agents truly read, understand, and write Feishu documents as Markdown, with safety guardrails that the raw API doesn't provide.
25
+
26
+ ## Features
27
+
28
+ - **Read** documents as Markdown, raw text, or original Block JSON
29
+ - **Create** documents in knowledge bases or cloud folders
30
+ - **Update** documents with overwrite or append mode (auto-batch for large content)
31
+ - **Delete** documents (move to recycle bin)
32
+ - **Info** — view document metadata (title, type, URL, revision)
33
+ - **Browse** knowledge base structure (spaces, tree, cat)
34
+ - **Search** documents by keyword
35
+ - **Share** — manage collaborators (list, add, set public mode)
36
+ - **List** files in cloud folders
37
+ - Written in TypeScript with strict mode
38
+ - Zero runtime dependencies — uses native `fetch` for all API calls
39
+ - Agent-friendly output — pure text or JSON, no interactive UI
40
+
41
+ ## Install
42
+
43
+ ```bash
44
+ npm install -g feishu-docs-cli
45
+ ```
46
+
47
+ Or use directly with npx:
48
+
49
+ ```bash
50
+ npx feishu-docs-cli read <url>
51
+ ```
52
+
53
+ You can also install from GitHub:
54
+
55
+ ```bash
56
+ npm install -g github:cliff-byte/feishu-docs-cli
57
+ ```
58
+
59
+ Requires Node.js >= 18.3.
60
+
61
+ ## Setup
62
+
63
+ ### 1. Create a Feishu App
64
+
65
+ 1. Go to [Feishu Open Platform](https://open.feishu.cn/app) (or [Lark Developer](https://open.larksuite.com/app) for international)
66
+ 2. Click **Create Custom App**, fill in the app name and description
67
+ 3. After creation, go to the app's **Credentials & Basic Info** page. Copy the **App ID** (`cli_xxx`) and **App Secret** — you'll need them for environment variables
68
+ 4. Go to **Permissions & Scopes**, search and add the following scopes:
69
+
70
+ **Base scopes** (no admin review needed — requested automatically during `feishu-docs login`):
71
+
72
+ | Scope | Description |
73
+ |-------|-------------|
74
+ | `wiki:wiki` | Knowledge base read/write |
75
+ | `docx:document` | Document read/write |
76
+ | `docx:document.block:convert` | Markdown to block conversion (create/update) |
77
+ | `sheets:spreadsheet:readonly` | Embedded spreadsheet read (read command) |
78
+ | `board:whiteboard:node:read` | Whiteboard export as image (read command) |
79
+ | `bitable:app:readonly` | Embedded bitable/table read (read command) |
80
+
81
+ **Additional scopes** are requested reactively — when an API call needs a scope you haven't authorized, the CLI detects this from the API error response and prompts you. Common ones:
82
+
83
+ | Scope | Description |
84
+ |-------|-------------|
85
+ | `drive:drive` | Cloud file management (ls, delete, share, mv, cp, mkdir) |
86
+ | `contact:contact.base:readonly` | User lookup by email/phone |
87
+ | `drive:drive.search:readonly` | Document search |
88
+
89
+ 5. Go to **Security Settings**, add the OAuth callback URL to the **Redirect URLs** allowlist:
90
+ - Default: `http://127.0.0.1:3456/callback`
91
+ - This must match exactly what you use during `feishu-docs login`
92
+
93
+ 6. **Publish the app version**: Go to **App Release** → **Create Version** → Submit for review → Approve (self-built apps in your org are usually auto-approved)
94
+
95
+ > **Note**: For tenant-level access (e.g., CI/CD), the app must be granted access to specific docs or knowledge bases. Add the app as a collaborator, or use the admin console to authorize document scope.
96
+
97
+ ### 2. Set Environment Variables
98
+
99
+ ```bash
100
+ export FEISHU_APP_ID="cli_xxx" # From step 3 above
101
+ export FEISHU_APP_SECRET="xxx" # From step 3 above
102
+ ```
103
+
104
+ ### 3. Login (for user-level access)
105
+
106
+ User-level access enables personal docs, search, and collaboration features.
107
+
108
+ ```bash
109
+ feishu-docs login
110
+ ```
111
+
112
+ This opens a browser for OAuth authorization and saves the encrypted token to `~/.feishu-docs/auth.json`.
113
+
114
+ If your app's registered redirect URL differs from the default (`http://127.0.0.1:3456/callback`), pass the exact same value:
115
+
116
+ ```bash
117
+ # Match the exact redirect URI registered in Feishu Open Platform
118
+ feishu-docs login --redirect-uri http://127.0.0.1:3456/callback
119
+
120
+ # Or change only the port and keep the default localhost path
121
+ feishu-docs login --port 4567
122
+ ```
123
+
124
+ ## Usage
125
+
126
+ ### Read
127
+
128
+ ```bash
129
+ # Read document as Markdown
130
+ feishu-docs read https://xxx.feishu.cn/wiki/wikcnXXX
131
+
132
+ # Read by token
133
+ feishu-docs read wikcnXXX
134
+
135
+ # Raw Block JSON (lossless)
136
+ feishu-docs read <url> --blocks
137
+
138
+ # Plain text only
139
+ feishu-docs read <url> --raw
140
+
141
+ # With metadata header
142
+ feishu-docs read <url> --with-meta
143
+ ```
144
+
145
+ ### Knowledge Base
146
+
147
+ ```bash
148
+ # List all knowledge bases
149
+ feishu-docs spaces
150
+
151
+ # Show node tree
152
+ feishu-docs tree <space_id>
153
+ feishu-docs tree <space_id> --depth 2
154
+
155
+ # Recursively read all documents
156
+ feishu-docs cat <space_id> --max-docs 20
157
+ feishu-docs cat <space_id> --node <token> --title-only
158
+ ```
159
+
160
+ ### Search
161
+
162
+ ```bash
163
+ feishu-docs search "API design" --type docx --limit 10
164
+ ```
165
+
166
+ Requires user access token. Use `feishu-docs login` first.
167
+
168
+ ### Create
169
+
170
+ ```bash
171
+ # In knowledge base
172
+ feishu-docs create "API Docs" --wiki <space_id> --body ./api.md
173
+
174
+ # In cloud folder
175
+ feishu-docs create "API Docs" --folder <folder_token> --body ./api.md
176
+
177
+ # Empty document
178
+ feishu-docs create "API Docs"
179
+
180
+ # From stdin
181
+ cat design.md | feishu-docs create "Design" --wiki <space_id> --body -
182
+ ```
183
+
184
+ ### Update
185
+
186
+ ```bash
187
+ # Overwrite (backs up first)
188
+ feishu-docs update <url> --body ./updated.md
189
+
190
+ # Append
191
+ feishu-docs update <url> --body ./extra.md --append
192
+
193
+ # From stdin
194
+ echo "## New Section" | feishu-docs update <url> --body - --append
195
+
196
+ # Restore from backup
197
+ feishu-docs update <url> --restore ~/.feishu-docs/backups/xxx.json
198
+ ```
199
+
200
+ ### Delete
201
+
202
+ ```bash
203
+ feishu-docs delete <url> --confirm
204
+ ```
205
+
206
+ Moves document to recycle bin (recoverable for 30 days).
207
+
208
+ ### Info
209
+
210
+ ```bash
211
+ feishu-docs info <url|token>
212
+ feishu-docs info <url> --json
213
+ ```
214
+
215
+ ### List Files
216
+
217
+ ```bash
218
+ # List root folder
219
+ feishu-docs ls
220
+
221
+ # List specific folder
222
+ feishu-docs ls <folder_token>
223
+
224
+ # Filter by type
225
+ feishu-docs ls --type docx --limit 20
226
+ ```
227
+
228
+ ### File Operations
229
+
230
+ ```bash
231
+ # Move file to a folder
232
+ feishu-docs mv <url|token> <target_folder_token>
233
+
234
+ # Copy file (auto-names as "Title - 副本")
235
+ feishu-docs cp <url|token> <target_folder_token>
236
+
237
+ # Copy with custom name
238
+ feishu-docs cp <url|token> <target_folder_token> --name "My Copy"
239
+
240
+ # Create a folder
241
+ feishu-docs mkdir "New Folder" --parent <parent_folder_token>
242
+ ```
243
+
244
+ ### Share
245
+
246
+ ```bash
247
+ # List collaborators
248
+ feishu-docs share list <url>
249
+
250
+ # Add collaborator
251
+ feishu-docs share add <url> user@example.com --role view
252
+ feishu-docs share add <url> ou_xxx --role edit
253
+
254
+ # Remove collaborator
255
+ feishu-docs share remove <url> user@example.com
256
+
257
+ # Update collaborator role
258
+ feishu-docs share update <url> ou_xxx --role manage
259
+
260
+ # Set public sharing mode
261
+ feishu-docs share set <url> --public tenant # org-wide readable
262
+ feishu-docs share set <url> --public tenant:edit # org-wide editable
263
+ feishu-docs share set <url> --public open # anyone readable
264
+ feishu-docs share set <url> --public closed # disable link sharing
265
+ ```
266
+
267
+ Roles: `view`, `edit`, `manage`. Member types are auto-detected (email, openid, unionid, openchat, userid).
268
+
269
+ ### Auth
270
+
271
+ ```bash
272
+ feishu-docs login # OAuth login (default callback: http://127.0.0.1:3456/callback)
273
+ feishu-docs logout # Clear saved credentials
274
+ feishu-docs whoami # Show current auth status
275
+ ```
276
+
277
+ ## Global Options
278
+
279
+ | Option | Description |
280
+ |--------|-------------|
281
+ | `--auth <user\|tenant\|auto>` | Auth mode (default: auto) |
282
+ | `--json` | Output JSON format |
283
+ | `--lark` | Use Lark (international) domain |
284
+ | `--help` | Show help |
285
+ | `-v, --version` | Show version |
286
+
287
+ ## Auth Modes
288
+
289
+ | Mode | Token Type | Use Case |
290
+ |------|-----------|----------|
291
+ | `user` | user_access_token | Personal docs, collaboration, search |
292
+ | `tenant` | tenant_access_token | App-managed docs, CI/CD |
293
+ | `auto` | Best available | Default — tries user first, falls back to tenant |
294
+
295
+ ## AI Agent Integration
296
+
297
+ ### Claude Code
298
+
299
+ Install the skill via [skills.sh](https://skills.sh) (works with Claude Code, Cursor, Codex, and 37+ agents):
300
+
301
+ ```bash
302
+ npx skills add cliff-byte/feishu-docs-cli
303
+ ```
304
+
305
+ Or install directly via the CLI:
306
+
307
+ ```bash
308
+ feishu-docs install-skill
309
+ ```
310
+
311
+ After installation, use `/feishu-docs` in Claude Code to activate the skill.
312
+
313
+ ### Other Agents
314
+
315
+ Add these instructions to your agent's system prompt or configuration:
316
+
317
+ ```
318
+ Read Feishu docs: feishu-docs read <url>
319
+ Search docs: feishu-docs search <keyword>
320
+ Browse wiki: feishu-docs tree <space_id>
321
+ Batch read: feishu-docs cat <space_id> --max-docs 10
322
+ Create doc: feishu-docs create <title> --wiki <space_id> --body <file>
323
+ Update doc: feishu-docs update <url> --body <file>
324
+ Use --json for structured output. Run feishu-docs --help for all commands.
325
+ ```
326
+
327
+ ### Programmatic Usage
328
+
329
+ All commands output to stdout (results) and stderr (errors/warnings). Exit codes:
330
+
331
+ | Code | Meaning |
332
+ |------|---------|
333
+ | 0 | Success |
334
+ | 1 | Invalid args |
335
+ | 2 | Auth failure |
336
+ | 3 | API error |
337
+
338
+ Use `--json` for structured output that agents can parse.
339
+
340
+ ## Write Safety
341
+
342
+ Overwrite operations (`update` without `--append`) automatically:
343
+
344
+ 1. **Back up** current document to `~/.feishu-docs/backups/`
345
+ 2. **Clear** then **rewrite** the document
346
+ 3. **Auto-recover** from backup if write fails
347
+ 4. **Rotate** backups (keeps last 10)
348
+
349
+ Feishu also maintains version history — you can always roll back in the Feishu client.
350
+
351
+ ## Development
352
+
353
+ ```bash
354
+ git clone https://github.com/cliff-byte/feishu-docs-cli.git
355
+ cd feishu-docs-cli
356
+ npm install
357
+
358
+ # Type check
359
+ npm run build:check
360
+
361
+ # Build (outputs to dist/)
362
+ npm run build
363
+
364
+ # Run tests
365
+ npm test
366
+
367
+ # Run CLI from source
368
+ npm run build && node bin/feishu-docs.js --help
369
+ ```
370
+
371
+ ### Project Structure
372
+
373
+ ```
374
+ src/
375
+ types/ # Shared TypeScript type definitions
376
+ commands/ # CLI command handlers
377
+ services/ # API service layer
378
+ parser/ # Block-to-Markdown parser
379
+ utils/ # Validation, error handling, URL parsing
380
+ test/ # Unit tests (node:test)
381
+ bin/ # CLI entry point (JS shim → dist/)
382
+ dist/ # Compiled output (git-ignored)
383
+ ```
384
+
385
+ ## Roadmap
386
+
387
+ - [x] Feishu cloud document operations (read, create, update, delete, info)
388
+ - [x] Knowledge base operations (spaces, tree, cat, wiki management, share, search)
389
+ - [ ] Feishu Bitable (multi-dimensional table) operations
390
+ - [ ] Feishu Sheets (spreadsheet) operations
391
+
392
+ ## Limitations
393
+
394
+ - **Supported**: docx (new documents)
395
+ - **Embedded content**: sheet (rendered as table), bitable (rendered as table), board/whiteboard (exported as image)
396
+ - **Link only**: mindnote
397
+ - **Not supported**: doc (legacy format)
398
+ - Markdown conversion is lossy (colors, merged cells, layouts are dropped). Use `--blocks` for lossless JSON.
399
+ - Image write is not supported (read returns temporary URLs valid ~24h)
400
+
401
+ ## License
402
+
403
+ MIT