@chenglu.she/sandy 1.0.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.
- package/.env.example +24 -0
- package/LICENSE +21 -0
- package/README.md +135 -0
- package/assets/sandy.png +0 -0
- package/bin/feishu-cursor-bot.js +37 -0
- package/package.json +49 -0
- package/src/ask-question.ts +228 -0
- package/src/config.ts +93 -0
- package/src/cursor-agent.ts +191 -0
- package/src/feishu-docs.ts +117 -0
- package/src/feishu-files.ts +181 -0
- package/src/feishu-markdown.ts +79 -0
- package/src/feishu-tools.ts +262 -0
- package/src/feishu.ts +434 -0
- package/src/index.ts +356 -0
- package/src/pending-store.ts +56 -0
- package/src/session-queue.ts +140 -0
- package/src/session-store.ts +54 -0
- package/src/write-hook-policy.ts +36 -0
package/.env.example
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
FEISHU_APP_ID=cli_xxx
|
|
2
|
+
FEISHU_APP_SECRET=xxx
|
|
3
|
+
CURSOR_API_KEY=crsr_xxx
|
|
4
|
+
|
|
5
|
+
# Agent display name (shown in Cursor / logs)
|
|
6
|
+
AGENT_NAME=Sandy
|
|
7
|
+
|
|
8
|
+
# Workspace the agent works in (defaults to current working directory)
|
|
9
|
+
# AGENT_CWD=.
|
|
10
|
+
|
|
11
|
+
# Extra roots the agent may use, comma-separated
|
|
12
|
+
# AGENT_DIRS=~/code/api,~/code/web
|
|
13
|
+
|
|
14
|
+
# Symlink names under AGENT_CWD whose targets should also be allowlisted
|
|
15
|
+
# AGENT_DIR_LINKS=api,web
|
|
16
|
+
|
|
17
|
+
# false ≈ full local shell (SSH / git / deploy). true = Cursor sandbox.
|
|
18
|
+
AGENT_SANDBOX=false
|
|
19
|
+
|
|
20
|
+
# Model id for @cursor/sdk
|
|
21
|
+
CURSOR_MODEL=auto
|
|
22
|
+
|
|
23
|
+
# Optional Drive folder_token for feishu_doc_create default placement
|
|
24
|
+
# FEISHU_DOCS_FOLDER=
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 louis-she
|
|
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,135 @@
|
|
|
1
|
+
# Feishu ↔ Cursor Bot
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="assets/sandy.png" alt="Sandy" width="220" height="220" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center"><b>Sandy</b> — 飞书长连接机器人,背后用 <a href="https://cursor.com/docs/sdk/typescript"><code>@cursor/sdk</code></a> 跑本地 Cursor Agent。</p>
|
|
8
|
+
|
|
9
|
+
通用脚手架:在任意项目目录放一份 `.env` +(可选)`.cursor/rules`,装好依赖后启动即可。
|
|
10
|
+
|
|
11
|
+
## 需要什么
|
|
12
|
+
|
|
13
|
+
- Node.js ≥ 20
|
|
14
|
+
- [飞书企业自建应用](https://open.feishu.cn/)(开通机器人 + 长连接事件)
|
|
15
|
+
- [Cursor API Key](https://cursor.com/settings)
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
### 1. 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install -g @chenglu.she/sandy
|
|
23
|
+
# 命令:feishu-cursor-bot 或 sandy
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
或从源码:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
git clone https://github.com/louis-she/sandy.git
|
|
30
|
+
cd sandy
|
|
31
|
+
npm install
|
|
32
|
+
npm link
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2. 在「工作目录」准备 `.env`
|
|
36
|
+
|
|
37
|
+
CLI **读取你启动时的 cwd** 下的 `.env`(不是包安装目录)。例如:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
mkdir -p ~/my-agent-home && cd ~/my-agent-home
|
|
41
|
+
cp /path/to/sandy/.env.example .env
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
必填:
|
|
45
|
+
|
|
46
|
+
| 变量 | 说明 |
|
|
47
|
+
|------|------|
|
|
48
|
+
| `FEISHU_APP_ID` | 飞书应用 App ID |
|
|
49
|
+
| `FEISHU_APP_SECRET` | 飞书应用 Secret |
|
|
50
|
+
| `CURSOR_API_KEY` | Cursor API Key |
|
|
51
|
+
|
|
52
|
+
常用可选:
|
|
53
|
+
|
|
54
|
+
| 变量 | 默认 | 说明 |
|
|
55
|
+
|------|------|------|
|
|
56
|
+
| `AGENT_NAME` | `Sandy` | Agent 显示名 |
|
|
57
|
+
| `AGENT_CWD` | 当前目录 | Agent 工作区(会加载这里的 `.cursor/rules`) |
|
|
58
|
+
| `AGENT_DIRS` | (空) | 额外可访问目录,逗号分隔 |
|
|
59
|
+
| `AGENT_DIR_LINKS` | (空) | `AGENT_CWD` 下要一并放行的 symlink 名 |
|
|
60
|
+
| `AGENT_SANDBOX` | `false` | `true` 时开 Cursor 本地沙箱 |
|
|
61
|
+
| `CURSOR_MODEL` | `auto` | 模型 id |
|
|
62
|
+
| `FEISHU_DOCS_FOLDER` | (空) | 创建飞书文档时的默认 Drive `folder_token` |
|
|
63
|
+
|
|
64
|
+
### 3. (推荐)放项目规则
|
|
65
|
+
|
|
66
|
+
在 `AGENT_CWD`(默认就是 cwd)下:
|
|
67
|
+
|
|
68
|
+
```text
|
|
69
|
+
.cursor/rules/your-persona.mdc
|
|
70
|
+
.cursor/rules/your-domain.mdc
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Agent 通过 `settingSources: ["project"]` 加载这些规则。人设、业务知识都写在这里,**不要写进 bot 源码**。
|
|
74
|
+
|
|
75
|
+
### 4. 飞书后台
|
|
76
|
+
|
|
77
|
+
1. **应用能力** → 开通 **机器人**
|
|
78
|
+
2. **权限**(开通后**发布新版本**才生效),至少:
|
|
79
|
+
- 消息:获取与发送单聊/群消息、上传图片、上传文件、下载文件(如 `im:message`、`im:resource`)
|
|
80
|
+
- 文档:读正文、创建文档、编辑文档(如 `docx:document:readonly`、`docx:document` / write)
|
|
81
|
+
- 若要把新建文档放到指定文件夹:Drive 文件夹相关权限,并填 `FEISHU_DOCS_FOLDER`
|
|
82
|
+
3. **版本管理**:发布;可用范围包含你自己
|
|
83
|
+
4. **事件与回调** → 订阅方式选 **长连接**(先让 bot 进程在线再保存)
|
|
84
|
+
5. 事件:`im.message.receive_v1`
|
|
85
|
+
6. 回调:`card.action.trigger`(Agent 选择题卡片用)
|
|
86
|
+
7. **读写已有文档**:在飞书文档里把机器人加为协作者(读/编辑),仅开通 API 权限不够
|
|
87
|
+
|
|
88
|
+
### 5. 启动
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
cd ~/my-agent-home # 有 .env 的目录
|
|
92
|
+
feishu-cursor-bot # 或 sandy
|
|
93
|
+
# 或
|
|
94
|
+
npx @chenglu.she/sandy
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
看到 `ws client ready` 后,飞书里私聊机器人即可。
|
|
98
|
+
|
|
99
|
+
#### macOS 常驻(可选)
|
|
100
|
+
|
|
101
|
+
本仓库带了 launchd 包装,避免挂在 IDE 终端里被杀掉:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
bash scripts/sandy-ctl.sh install
|
|
105
|
+
bash scripts/sandy-ctl.sh status
|
|
106
|
+
bash scripts/sandy-ctl.sh logs
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
> plist 里是本机绝对路径,换机器请改 `deploy/cn.sandy.plist` 或自己写一份 LaunchAgent。
|
|
110
|
+
|
|
111
|
+
## 行为速览
|
|
112
|
+
|
|
113
|
+
| 场景 | 行为 |
|
|
114
|
+
|------|------|
|
|
115
|
+
| 私聊 | 文本 / 文件 / 图片进 Agent |
|
|
116
|
+
| 群聊 | 仅当 @机器人(文件同理,需 @) |
|
|
117
|
+
| 同会话 | `Agent.resume` 多轮;映射在 cwd 的 `.data/sessions.json` |
|
|
118
|
+
| `/new` `/reset` `重置` `新对话` | 清空会话,下次新建 Agent |
|
|
119
|
+
| 连续消息 | 按会话排队;排队 `OneSecond`,处理中 `OnIt` |
|
|
120
|
+
| `askQuestion` | 飞书交互卡片;点选或回编号后续跑 |
|
|
121
|
+
| 用户发文件/图 | 下载到 `AGENT_CWD/.data/feishu-inbox/…`,路径写入 prompt |
|
|
122
|
+
| Agent 发回文件 | 工具 `feishu_send_file`(本地路径 → 飞书回复) |
|
|
123
|
+
| 飞书文档 | `feishu_doc_read` / `feishu_doc_create` / `feishu_doc_append` |
|
|
124
|
+
|
|
125
|
+
## 开发
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
npm run dev # tsx watch
|
|
129
|
+
npm start # 等同 feishu-cursor-bot(仍读 cwd .env)
|
|
130
|
+
npm run typecheck
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
[MIT](./LICENSE)
|
package/assets/sandy.png
ADDED
|
Binary file
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CLI entry: run from any directory; loads `.env` from cwd (see src/config.ts).
|
|
4
|
+
*
|
|
5
|
+
* npx feishu-cursor-bot
|
|
6
|
+
* npm i -g . && feishu-cursor-bot
|
|
7
|
+
*/
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { createRequire } from "node:module";
|
|
12
|
+
|
|
13
|
+
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
14
|
+
const entry = path.join(packageRoot, "src", "index.ts");
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
|
|
17
|
+
let tsxCli;
|
|
18
|
+
try {
|
|
19
|
+
tsxCli = require.resolve("tsx/cli");
|
|
20
|
+
} catch {
|
|
21
|
+
console.error("Missing dependency `tsx`. Run npm install in the package root.");
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const child = spawn(process.execPath, [tsxCli, entry, ...process.argv.slice(2)], {
|
|
26
|
+
cwd: process.cwd(),
|
|
27
|
+
stdio: "inherit",
|
|
28
|
+
env: process.env,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
child.on("exit", (code, signal) => {
|
|
32
|
+
if (signal) {
|
|
33
|
+
process.kill(process.pid, signal);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
process.exit(code ?? 1);
|
|
37
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@chenglu.she/sandy",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Feishu long-connection bot that drives Cursor agents via @cursor/sdk",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/louis-she/sandy.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/louis-she/sandy",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/louis-she/sandy/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public",
|
|
17
|
+
"registry": "https://registry.npmjs.org"
|
|
18
|
+
},
|
|
19
|
+
"bin": {
|
|
20
|
+
"feishu-cursor-bot": "./bin/feishu-cursor-bot.js",
|
|
21
|
+
"sandy": "./bin/feishu-cursor-bot.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"bin",
|
|
25
|
+
"src",
|
|
26
|
+
"assets",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE",
|
|
29
|
+
".env.example"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"dev": "tsx watch src/index.ts",
|
|
33
|
+
"start": "tsx src/index.ts",
|
|
34
|
+
"typecheck": "tsc --noEmit"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=20"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@cursor/sdk": "^1.0.0",
|
|
41
|
+
"@larksuiteoapi/node-sdk": "^1.55.0",
|
|
42
|
+
"dotenv": "^16.6.1",
|
|
43
|
+
"tsx": "^4.20.3"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^22.17.0",
|
|
47
|
+
"typescript": "^5.9.2"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
export type AskOption = {
|
|
2
|
+
id: string;
|
|
3
|
+
label: string;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type AskQuestion = {
|
|
7
|
+
id: string;
|
|
8
|
+
prompt: string;
|
|
9
|
+
options: AskOption[];
|
|
10
|
+
allowMultiple: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type ParsedAskQuestion = {
|
|
14
|
+
title?: string;
|
|
15
|
+
questions: AskQuestion[];
|
|
16
|
+
callId?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
20
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
21
|
+
? (value as Record<string, unknown>)
|
|
22
|
+
: undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function parseOptions(raw: unknown): AskOption[] {
|
|
26
|
+
if (!Array.isArray(raw)) return [];
|
|
27
|
+
return raw
|
|
28
|
+
.map((item, index) => {
|
|
29
|
+
const obj = asRecord(item);
|
|
30
|
+
if (!obj) return undefined;
|
|
31
|
+
const id =
|
|
32
|
+
typeof obj.id === "string"
|
|
33
|
+
? obj.id
|
|
34
|
+
: typeof obj.value === "string"
|
|
35
|
+
? obj.value
|
|
36
|
+
: `opt_${index + 1}`;
|
|
37
|
+
const label =
|
|
38
|
+
typeof obj.label === "string"
|
|
39
|
+
? obj.label
|
|
40
|
+
: typeof obj.text === "string"
|
|
41
|
+
? obj.text
|
|
42
|
+
: typeof obj.title === "string"
|
|
43
|
+
? obj.title
|
|
44
|
+
: String(id);
|
|
45
|
+
return { id, label };
|
|
46
|
+
})
|
|
47
|
+
.filter((x): x is AskOption => Boolean(x));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function parseQuestions(raw: unknown): AskQuestion[] {
|
|
51
|
+
if (!Array.isArray(raw)) return [];
|
|
52
|
+
return raw
|
|
53
|
+
.map((item, index) => {
|
|
54
|
+
const obj = asRecord(item);
|
|
55
|
+
if (!obj) return undefined;
|
|
56
|
+
const id =
|
|
57
|
+
typeof obj.id === "string"
|
|
58
|
+
? obj.id
|
|
59
|
+
: typeof obj.questionId === "string"
|
|
60
|
+
? obj.questionId
|
|
61
|
+
: `q_${index + 1}`;
|
|
62
|
+
const prompt =
|
|
63
|
+
typeof obj.prompt === "string"
|
|
64
|
+
? obj.prompt
|
|
65
|
+
: typeof obj.question === "string"
|
|
66
|
+
? obj.question
|
|
67
|
+
: typeof obj.text === "string"
|
|
68
|
+
? obj.text
|
|
69
|
+
: `问题 ${index + 1}`;
|
|
70
|
+
const options = parseOptions(obj.options ?? obj.choices);
|
|
71
|
+
if (options.length === 0) return undefined;
|
|
72
|
+
return {
|
|
73
|
+
id,
|
|
74
|
+
prompt,
|
|
75
|
+
options,
|
|
76
|
+
allowMultiple: Boolean(obj.allowMultiple ?? obj.allow_multiple ?? obj.multiSelect),
|
|
77
|
+
};
|
|
78
|
+
})
|
|
79
|
+
.filter((x): x is AskQuestion => Boolean(x));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Defensive parse of askQuestion tool args / tool_use input. */
|
|
83
|
+
export function parseAskQuestionArgs(args: unknown): ParsedAskQuestion | undefined {
|
|
84
|
+
const root = asRecord(args);
|
|
85
|
+
if (!root) return undefined;
|
|
86
|
+
|
|
87
|
+
const nested =
|
|
88
|
+
asRecord(root.args) ??
|
|
89
|
+
asRecord(root.input) ??
|
|
90
|
+
asRecord(root.arguments) ??
|
|
91
|
+
root;
|
|
92
|
+
|
|
93
|
+
const questions = parseQuestions(
|
|
94
|
+
nested.questions ?? nested.question_list ?? nested.items,
|
|
95
|
+
);
|
|
96
|
+
if (questions.length === 0) {
|
|
97
|
+
// Single-question flattened shape
|
|
98
|
+
const options = parseOptions(nested.options ?? nested.choices);
|
|
99
|
+
if (options.length > 0) {
|
|
100
|
+
questions.push({
|
|
101
|
+
id: typeof nested.id === "string" ? nested.id : "q1",
|
|
102
|
+
prompt:
|
|
103
|
+
typeof nested.prompt === "string"
|
|
104
|
+
? nested.prompt
|
|
105
|
+
: typeof nested.question === "string"
|
|
106
|
+
? nested.question
|
|
107
|
+
: typeof nested.title === "string"
|
|
108
|
+
? nested.title
|
|
109
|
+
: "请选择",
|
|
110
|
+
options,
|
|
111
|
+
allowMultiple: Boolean(nested.allowMultiple ?? nested.allow_multiple),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (questions.length === 0) return undefined;
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
title: typeof nested.title === "string" ? nested.title : undefined,
|
|
120
|
+
questions,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function isAskQuestionToolName(name: string | undefined): boolean {
|
|
125
|
+
if (!name) return false;
|
|
126
|
+
const n = name.replace(/[_-]/g, "").toLowerCase();
|
|
127
|
+
return n === "askquestion" || n.endsWith("askquestion");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Format selected answers as a follow-up user message for Agent.send. */
|
|
131
|
+
export function formatAnswerPrompt(
|
|
132
|
+
questions: AskQuestion[],
|
|
133
|
+
answers: Array<{ questionId: string; selectedOptionIds: string[]; freeformText?: string }>,
|
|
134
|
+
): string {
|
|
135
|
+
const lines = ["【用户已完成选择题】请根据以下选择继续:"];
|
|
136
|
+
for (const q of questions) {
|
|
137
|
+
const answer = answers.find((a) => a.questionId === q.id);
|
|
138
|
+
if (!answer) {
|
|
139
|
+
lines.push(`- ${q.prompt}: (未选择)`);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (answer.freeformText?.trim()) {
|
|
143
|
+
lines.push(`- ${q.prompt}: ${answer.freeformText.trim()}`);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const labels = answer.selectedOptionIds.map((id) => {
|
|
147
|
+
const opt = q.options.find((o) => o.id === id);
|
|
148
|
+
return opt ? `${opt.label} (id=${opt.id})` : id;
|
|
149
|
+
});
|
|
150
|
+
lines.push(`- ${q.prompt}: ${labels.join("、")}`);
|
|
151
|
+
}
|
|
152
|
+
return lines.join("\n");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Parse a free-text reply against pending questions.
|
|
157
|
+
* Supports: "1", "1,3", "选项原文", or "q1=1; q2=2".
|
|
158
|
+
*/
|
|
159
|
+
export function parseTextAnswer(
|
|
160
|
+
questions: AskQuestion[],
|
|
161
|
+
text: string,
|
|
162
|
+
): Array<{ questionId: string; selectedOptionIds: string[]; freeformText?: string }> | undefined {
|
|
163
|
+
const trimmed = text.trim();
|
|
164
|
+
if (!trimmed) return undefined;
|
|
165
|
+
|
|
166
|
+
if (questions.length === 1) {
|
|
167
|
+
const q = questions[0]!;
|
|
168
|
+
const byLabel = q.options.find(
|
|
169
|
+
(o) => o.label === trimmed || o.id === trimmed,
|
|
170
|
+
);
|
|
171
|
+
if (byLabel) {
|
|
172
|
+
return [{ questionId: q.id, selectedOptionIds: [byLabel.id] }];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const nums = trimmed
|
|
176
|
+
.split(/[,,\s]+/)
|
|
177
|
+
.map((p) => Number.parseInt(p, 10))
|
|
178
|
+
.filter((n) => Number.isFinite(n) && n >= 1 && n <= q.options.length);
|
|
179
|
+
|
|
180
|
+
if (nums.length > 0) {
|
|
181
|
+
const ids = [...new Set(nums)].map((n) => q.options[n - 1]!.id);
|
|
182
|
+
if (!q.allowMultiple && ids.length > 1) return undefined;
|
|
183
|
+
return [{ questionId: q.id, selectedOptionIds: ids }];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Freeform fallback for single question
|
|
187
|
+
return [{ questionId: q.id, selectedOptionIds: [], freeformText: trimmed }];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Multi-question: expect "1:1; 2:2" or "q1=label"
|
|
191
|
+
const parts = trimmed.split(/[;;\n]+/).map((p) => p.trim()).filter(Boolean);
|
|
192
|
+
if (parts.length === 0) return undefined;
|
|
193
|
+
|
|
194
|
+
const answers: Array<{ questionId: string; selectedOptionIds: string[]; freeformText?: string }> = [];
|
|
195
|
+
for (const part of parts) {
|
|
196
|
+
const m = part.match(/^(?:q\s*)?(\d+|[A-Za-z0-9_-]+)\s*[=::]\s*(.+)$/);
|
|
197
|
+
if (!m) return undefined;
|
|
198
|
+
const key = m[1]!;
|
|
199
|
+
const value = m[2]!.trim();
|
|
200
|
+
const q =
|
|
201
|
+
questions.find((qq) => qq.id === key) ??
|
|
202
|
+
( /^\d+$/.test(key) ? questions[Number(key) - 1] : undefined);
|
|
203
|
+
if (!q) return undefined;
|
|
204
|
+
|
|
205
|
+
const idxs = value
|
|
206
|
+
.split(/[,,\s]+/)
|
|
207
|
+
.map((p) => Number.parseInt(p, 10))
|
|
208
|
+
.filter((n) => Number.isFinite(n) && n >= 1 && n <= q.options.length);
|
|
209
|
+
|
|
210
|
+
if (idxs.length > 0) {
|
|
211
|
+
answers.push({
|
|
212
|
+
questionId: q.id,
|
|
213
|
+
selectedOptionIds: [...new Set(idxs)].map((n) => q.options[n - 1]!.id),
|
|
214
|
+
});
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const opt = q.options.find((o) => o.label === value || o.id === value);
|
|
219
|
+
if (opt) {
|
|
220
|
+
answers.push({ questionId: q.id, selectedOptionIds: [opt.id] });
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
answers.push({ questionId: q.id, selectedOptionIds: [], freeformText: value });
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return answers.length > 0 ? answers : undefined;
|
|
228
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import dotenv from "dotenv";
|
|
5
|
+
|
|
6
|
+
const packageRoot = path.resolve(
|
|
7
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
8
|
+
"..",
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
/** Workspace that owns `.env` / agent cwd — usually where you run the CLI. */
|
|
12
|
+
export const workDir = (() => {
|
|
13
|
+
const override = process.env.FEISHU_CURSOR_CWD?.trim();
|
|
14
|
+
if (!override) return process.cwd();
|
|
15
|
+
return expandPath(override, process.cwd());
|
|
16
|
+
})();
|
|
17
|
+
|
|
18
|
+
// Prefer cwd `.env`, then package-local `.env` as fallback (no override).
|
|
19
|
+
dotenv.config({ path: path.join(workDir, ".env") });
|
|
20
|
+
dotenv.config({ path: path.join(packageRoot, ".env") });
|
|
21
|
+
|
|
22
|
+
function required(name: string): string {
|
|
23
|
+
const value = process.env[name]?.trim();
|
|
24
|
+
if (!value) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Missing required env: ${name} (put it in ${path.join(workDir, ".env")})`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function expandPath(raw: string, base: string = process.cwd()): string {
|
|
33
|
+
const trimmed = raw.trim();
|
|
34
|
+
if (trimmed === "~") return os.homedir();
|
|
35
|
+
if (trimmed.startsWith("~/")) {
|
|
36
|
+
return path.join(os.homedir(), trimmed.slice(2));
|
|
37
|
+
}
|
|
38
|
+
return path.isAbsolute(trimmed) ? trimmed : path.resolve(base, trimmed);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function parseBool(raw: string | undefined, defaultValue: boolean): boolean {
|
|
42
|
+
if (raw === undefined || raw.trim() === "") return defaultValue;
|
|
43
|
+
const v = raw.trim().toLowerCase();
|
|
44
|
+
if (["1", "true", "yes", "on"].includes(v)) return true;
|
|
45
|
+
if (["0", "false", "no", "off"].includes(v)) return false;
|
|
46
|
+
return defaultValue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const agentCwdRaw = process.env.AGENT_CWD?.trim() || workDir;
|
|
50
|
+
const agentDirsRaw = process.env.AGENT_DIRS?.trim() || "";
|
|
51
|
+
const agentDirs = agentDirsRaw
|
|
52
|
+
? [...new Set(agentDirsRaw.split(",").map((p) => expandPath(p)).filter(Boolean))]
|
|
53
|
+
: [];
|
|
54
|
+
|
|
55
|
+
/** Optional symlink names under AGENT_CWD to also allow (comma-separated). */
|
|
56
|
+
const agentDirLinksRaw = process.env.AGENT_DIR_LINKS?.trim() || "";
|
|
57
|
+
export const agentDirLinks = agentDirLinksRaw
|
|
58
|
+
? [...new Set(agentDirLinksRaw.split(",").map((s) => s.trim()).filter(Boolean))]
|
|
59
|
+
: [];
|
|
60
|
+
|
|
61
|
+
export const config = {
|
|
62
|
+
packageRoot,
|
|
63
|
+
workDir,
|
|
64
|
+
feishuAppId: required("FEISHU_APP_ID"),
|
|
65
|
+
feishuAppSecret: required("FEISHU_APP_SECRET"),
|
|
66
|
+
cursorApiKey: required("CURSOR_API_KEY"),
|
|
67
|
+
cursorModel: process.env.CURSOR_MODEL?.trim() || "auto",
|
|
68
|
+
/** Display name for the Cursor agent / bot persona. */
|
|
69
|
+
agentName: process.env.AGENT_NAME?.trim() || "Sandy",
|
|
70
|
+
/** Primary workspace root for the agent. Defaults to cwd. */
|
|
71
|
+
agentCwd: expandPath(String(agentCwdRaw)),
|
|
72
|
+
/** Optional extra workspace roots (multi-root). */
|
|
73
|
+
agentDirs,
|
|
74
|
+
agentDirLinks,
|
|
75
|
+
/** Enable Cursor local sandbox (FS + shell + default deny network for shell). */
|
|
76
|
+
agentSandbox: parseBool(process.env.AGENT_SANDBOX, false),
|
|
77
|
+
sessionStorePath: path.resolve(workDir, ".data", "sessions.json"),
|
|
78
|
+
pendingStorePath: path.resolve(workDir, ".data", "pending-questions.json"),
|
|
79
|
+
/** Optional Drive folder_token for feishu_doc_create default placement. */
|
|
80
|
+
feishuDocsFolder: process.env.FEISHU_DOCS_FOLDER?.trim() || "",
|
|
81
|
+
/** Where incoming Feishu files are saved (under AGENT_CWD so the agent can read them). */
|
|
82
|
+
inboxDir: path.join(expandPath(String(agentCwdRaw)), ".data", "feishu-inbox"),
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export function localAgentOptions() {
|
|
86
|
+
return {
|
|
87
|
+
cwd: config.agentCwd,
|
|
88
|
+
...(config.agentDirs.length > 0 ? { dirs: config.agentDirs } : {}),
|
|
89
|
+
...(config.agentSandbox ? { sandboxOptions: { enabled: true as const } } : {}),
|
|
90
|
+
// Load AGENTS.md / .cursor/rules from AGENT_CWD.
|
|
91
|
+
settingSources: ["project" as const],
|
|
92
|
+
};
|
|
93
|
+
}
|