@suwenguang/pi-feishu-bridge 0.0.0-stage → 0.6.2
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/.pi/extensions/feishu/index.ts +11 -0
- package/.pi/feishu/debug.pi.log +81 -0
- package/.pi/feishu/state.pi.json +12 -0
- package/README.md +210 -2
- package/package.json +57 -4
- package/src/adapters/pi/PiConversationRuntime.ts +1559 -0
- package/src/adapters/pi/index.ts +1169 -0
- package/src/adapters/pi/setup.ts +108 -0
- package/src/feishu/app-register.ts +50 -0
- package/src/feishu/ask.ts +388 -0
- package/src/feishu/attachment-processor.ts +198 -0
- package/src/feishu/attachments.ts +192 -0
- package/src/feishu/bridge-runtime.ts +129 -0
- package/src/feishu/bridge-store.ts +92 -0
- package/src/feishu/card-action-webhook.ts +141 -0
- package/src/feishu/card-actions.ts +241 -0
- package/src/feishu/card-builder.ts +277 -0
- package/src/feishu/cardkit-stream.ts +483 -0
- package/src/feishu/cards.ts +413 -0
- package/src/feishu/config.ts +427 -0
- package/src/feishu/daemon-spec.ts +43 -0
- package/src/feishu/debug.ts +51 -0
- package/src/feishu/dedupe-store.ts +150 -0
- package/src/feishu/delivery.ts +185 -0
- package/src/feishu/gateway-lock.ts +256 -0
- package/src/feishu/group-trigger.ts +128 -0
- package/src/feishu/image-send.ts +256 -0
- package/src/feishu/instance.ts +170 -0
- package/src/feishu/instances.ts +148 -0
- package/src/feishu/interactive-card.ts +236 -0
- package/src/feishu/learn-approval.ts +321 -0
- package/src/feishu/live-cards.ts +128 -0
- package/src/feishu/message-handler.ts +512 -0
- package/src/feishu/messages.ts +356 -0
- package/src/feishu/oversize-progress-card.ts +101 -0
- package/src/feishu/progress-channel.ts +154 -0
- package/src/feishu/progress-note.ts +371 -0
- package/src/feishu/prompt-timeout.ts +81 -0
- package/src/feishu/reply-card-options.ts +73 -0
- package/src/feishu/reply-card.ts +498 -0
- package/src/feishu/restart.ts +286 -0
- package/src/feishu/retry.ts +102 -0
- package/src/feishu/rich-text.ts +387 -0
- package/src/feishu/runtime-config.ts +286 -0
- package/src/feishu/runtime.ts +110 -0
- package/src/feishu/slash-commands.ts +239 -0
- package/src/feishu/thinking.ts +24 -0
- package/src/feishu/transport.ts +792 -0
- package/src/feishu/types.ts +183 -0
- package/src/feishu/ui-notify.ts +44 -0
- package/src/feishu/user-name.ts +121 -0
- package/src/feishu/workspace.ts +42 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
export type Domain = "feishu" | "lark";
|
|
2
|
+
export type GroupPolicy = "open" | "mention";
|
|
3
|
+
export type CardActionMode = "webhook" | "ws";
|
|
4
|
+
|
|
5
|
+
export type FeishuConfig = {
|
|
6
|
+
appId: string;
|
|
7
|
+
appSecret: string;
|
|
8
|
+
domain: Domain;
|
|
9
|
+
groupPolicy: GroupPolicy;
|
|
10
|
+
/**
|
|
11
|
+
* 群聊关键词触发(与 mention 叠加):正文命中任一关键词即可,无需 @。
|
|
12
|
+
* 匹配:去空白后大小写不敏感子串。
|
|
13
|
+
*/
|
|
14
|
+
groupKeywords?: string[];
|
|
15
|
+
/**
|
|
16
|
+
* 群聊跟帖触发(与 mention 叠加):回复本 bot 消息时触发,无需再 @。
|
|
17
|
+
* 默认 false。
|
|
18
|
+
*/
|
|
19
|
+
groupAlsoOnReply?: boolean;
|
|
20
|
+
/** 是否忽略 sender_type=bot 的消息;默认 true */
|
|
21
|
+
ignoreBotMessages?: boolean;
|
|
22
|
+
cardActionMode?: CardActionMode;
|
|
23
|
+
cardActionWebhookHost?: string;
|
|
24
|
+
cardActionWebhookPort?: number;
|
|
25
|
+
cardActionWebhookPath?: string;
|
|
26
|
+
language?: "zh" | "en";
|
|
27
|
+
reactEmoji?: string;
|
|
28
|
+
autoStart?: boolean;
|
|
29
|
+
/** Long-task notice threshold in seconds (0 disables). */
|
|
30
|
+
promptNotifySec?: number;
|
|
31
|
+
/** Hard prompt timeout in seconds (0 waits indefinitely). */
|
|
32
|
+
promptTimeoutSec?: number;
|
|
33
|
+
/** 解析入站 interactive 卡片(默认 true) */
|
|
34
|
+
parseInteractiveCards?: boolean;
|
|
35
|
+
/** 用户回复/引用消息时展开 parent/root 正文(默认 true) */
|
|
36
|
+
includeQuotedMessage?: boolean;
|
|
37
|
+
/** 引用消息并入 prompt 的最大字符数 */
|
|
38
|
+
quotedMessageMaxChars?: number;
|
|
39
|
+
/** 出站 API 最大重试次数(不含首次) */
|
|
40
|
+
sendMaxRetries?: number;
|
|
41
|
+
/**
|
|
42
|
+
* 是否启用流式回复(默认 true)。
|
|
43
|
+
* 使用飞书 CardKit streaming_mode,客户端按 print_step 逐字打印。
|
|
44
|
+
*/
|
|
45
|
+
streamingReply?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* 正在生成回复时又收到新消息的处理方式:
|
|
48
|
+
* - queue(默认):排队,等上一条跑完再处理
|
|
49
|
+
* - interrupt:立即打断当前生成,用新消息开新一轮
|
|
50
|
+
* 可通过 /delivery 卡片或 /config messageDelivery 修改。
|
|
51
|
+
*/
|
|
52
|
+
messageDelivery?: "queue" | "interrupt";
|
|
53
|
+
/** CardKit 客户端打印间隔 ms(默认 50) */
|
|
54
|
+
streamPrintFrequencyMs?: number;
|
|
55
|
+
/** CardKit 每次打印字符数(默认 1) */
|
|
56
|
+
streamPrintStep?: number;
|
|
57
|
+
/** 服务端推送 fullText 到 CardKit 的间隔 ms(默认 120) */
|
|
58
|
+
streamPushIntervalMs?: number;
|
|
59
|
+
/**
|
|
60
|
+
* 工具执行过程展示档位(默认 off)。
|
|
61
|
+
* off 不展示;steps 累积清单;action 只显示当前工具;timeout 超阈值才提示。
|
|
62
|
+
* 修改方式:飞书 `/config progressMode <值>` 或 Pi `/feishu progress <值>`。
|
|
63
|
+
*/
|
|
64
|
+
progressMode?: "off" | "steps" | "action" | "timeout";
|
|
65
|
+
/** 清单最多保留多少行,超出折叠(默认 50) */
|
|
66
|
+
progressMaxLines?: number;
|
|
67
|
+
/** 超过该秒数才显示耗时(默认 30) */
|
|
68
|
+
progressTimeoutSeconds?: number;
|
|
69
|
+
/** @deprecated 兼容旧配置 */
|
|
70
|
+
streamFlushMs?: number;
|
|
71
|
+
streamMinChars?: number;
|
|
72
|
+
streamFirstFlushMs?: number;
|
|
73
|
+
streamMaxBodyChars?: number;
|
|
74
|
+
/** 进化审批(/learn 应用 的授权通道);未配置则 /learn 应用 不可用 */
|
|
75
|
+
learnApproval?: LearnApprovalConfig;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 进化审批配置。
|
|
80
|
+
*
|
|
81
|
+
* 审批人与表单结构不在这里,而在飞书审批后台的**审批定义**里:
|
|
82
|
+
* 建一个审批(例如「Pi 进化审批」),放一个「多行文本」控件,审批人设为本人,
|
|
83
|
+
* 再把它的 approval_code 填到这里。表单控件由代码自动识别,无需手填控件 id。
|
|
84
|
+
*/
|
|
85
|
+
export type LearnApprovalConfig = {
|
|
86
|
+
/** 飞书审批定义的 approval_code */
|
|
87
|
+
approvalCode: string;
|
|
88
|
+
/** 等审批的超时秒数(默认 600,即 10 分钟) */
|
|
89
|
+
timeoutSec?: number;
|
|
90
|
+
/** 轮询审批状态的间隔秒数(默认 5) */
|
|
91
|
+
pollIntervalSec?: number;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export type ModelSelection = {
|
|
95
|
+
provider: string;
|
|
96
|
+
id: string;
|
|
97
|
+
/** 思考强度档位(可选;Pi 存在会话内部,不在此持久化)。 */
|
|
98
|
+
thinkingLevel?: string;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export type FeishuState = {
|
|
102
|
+
sessions: Record<string, string>;
|
|
103
|
+
models?: Record<string, ModelSelection>;
|
|
104
|
+
workspaces?: Record<string, string>;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export type FeishuRoute = {
|
|
108
|
+
sessionKey: string;
|
|
109
|
+
sessionId?: string;
|
|
110
|
+
chatId: string;
|
|
111
|
+
chatType: "p2p" | "group";
|
|
112
|
+
threadMessageId?: string;
|
|
113
|
+
lastMessageId: string;
|
|
114
|
+
/** 最近一条消息的发送者 open_id:进化审批要拿它当审批发起人 */
|
|
115
|
+
senderOpenId?: string;
|
|
116
|
+
updatedAt: number;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export type FeishuJobRoute = FeishuRoute & {
|
|
120
|
+
jobId: string;
|
|
121
|
+
jobName?: string;
|
|
122
|
+
createdAt: number;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export type FeishuBridgeState = {
|
|
126
|
+
version: 1;
|
|
127
|
+
routes: Record<string, FeishuRoute>;
|
|
128
|
+
jobs: Record<string, FeishuJobRoute>;
|
|
129
|
+
sent: Record<string, number>;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export type FeishuMessage = {
|
|
133
|
+
messageId: string;
|
|
134
|
+
chatId: string;
|
|
135
|
+
chatType: "p2p" | "group";
|
|
136
|
+
chatMode?: "p2p" | "group" | "topic";
|
|
137
|
+
senderOpenId: string;
|
|
138
|
+
msgType: string;
|
|
139
|
+
content: string;
|
|
140
|
+
rootId?: string;
|
|
141
|
+
parentId?: string;
|
|
142
|
+
threadId?: string;
|
|
143
|
+
mentions?: unknown[];
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
export type FeishuAttachment = {
|
|
147
|
+
kind: "image" | "file";
|
|
148
|
+
fileKey: string;
|
|
149
|
+
fileName?: string;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export type FeishuCardAction = {
|
|
153
|
+
messageId: string;
|
|
154
|
+
chatId?: string;
|
|
155
|
+
operatorOpenId: string;
|
|
156
|
+
token?: string;
|
|
157
|
+
value: unknown;
|
|
158
|
+
/**
|
|
159
|
+
* 表单容器(form)提交时才有:key 为表单内组件 name,value 为用户提交的值。
|
|
160
|
+
* 普通按钮点击时为 undefined。飞书表单提交的事件 tag 依然是 button,
|
|
161
|
+
* 只是多了这个字段,所以不能靠 tag 区分。
|
|
162
|
+
*/
|
|
163
|
+
formValue?: unknown;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export type FeishuCopyMarkdownAction = {
|
|
167
|
+
copySourceId: string;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export type FeishuStatus =
|
|
171
|
+
| "not configured"
|
|
172
|
+
| "connecting"
|
|
173
|
+
| "connected"
|
|
174
|
+
| "owned"
|
|
175
|
+
| "bot unavailable"
|
|
176
|
+
| "disconnected";
|
|
177
|
+
|
|
178
|
+
export type ParsedMessageInput = {
|
|
179
|
+
text: string;
|
|
180
|
+
attachments: FeishuAttachment[];
|
|
181
|
+
/** 解析来源标记,便于调试 */
|
|
182
|
+
source?: string;
|
|
183
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 扩展 UI 输出回传(跨扩展事件契约)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:Pi 侧扩展(例如 knowledge-flow 的 /req、/kb、/test)把结果写进
|
|
5
|
+
* `ctx.ui.notify`。飞书场景下桥接跑在 `pi --mode rpc` 的 daemon 里,
|
|
6
|
+
* notify 只会变成 daemon stdout 上的一行 JSON,飞书用户看不到任何内容。
|
|
7
|
+
*
|
|
8
|
+
* 约定:这类扩展在 notify 的同时额外 emit 本事件,桥接负责把文本回传到
|
|
9
|
+
* 「发起该轮次的飞书会话」。
|
|
10
|
+
*
|
|
11
|
+
* 注意:事件名与载荷是跨包契约(`.pi/extensions/**` 与本包之间的约定),
|
|
12
|
+
* 修改这里必须同步修改调用方。
|
|
13
|
+
*/
|
|
14
|
+
export const FEISHU_UI_NOTIFY_EVENT = "feishu:ui:notify";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 进程级 sink 的 key。
|
|
18
|
+
*
|
|
19
|
+
* 为什么不能只用事件总线:桥接会为**每个飞书会话**单独建一个
|
|
20
|
+
* DefaultResourceLoader(见 PiConversationRuntime.createSession),
|
|
21
|
+
* 会话内的扩展实例与 daemon 主会话走的是**不同的** pi.events 总线。
|
|
22
|
+
* 而扩展的 notify 恰好发生在会话内的扩展实例上,跨会话只能靠进程级全局对象。
|
|
23
|
+
*
|
|
24
|
+
* 用 Symbol.for 而不是字符串:同一个 key 字符串在全进程(含模块重复加载)
|
|
25
|
+
* 得到同一个 Symbol,调用方无需 import 本文件。
|
|
26
|
+
*/
|
|
27
|
+
export const FEISHU_UI_NOTIFY_SINK = Symbol.for("pi.feishu.uiNotifySink");
|
|
28
|
+
|
|
29
|
+
/** 注册进程级输出回传 sink(桥接启动时调用,后注册者覆盖) */
|
|
30
|
+
export function setFeishuUiNotifySink(sink: (payload: FeishuUiNotifyPayload) => void) {
|
|
31
|
+
(globalThis as Record<symbol, unknown>)[FEISHU_UI_NOTIFY_SINK] = sink;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 读取进程级输出回传 sink;未安装桥接时返回 undefined */
|
|
35
|
+
export function getFeishuUiNotifySink(): ((payload: FeishuUiNotifyPayload) => void) | undefined {
|
|
36
|
+
const sink = (globalThis as Record<symbol, unknown>)[FEISHU_UI_NOTIFY_SINK];
|
|
37
|
+
return typeof sink === "function" ? (sink as (payload: FeishuUiNotifyPayload) => void) : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 事件载荷:text 必填;level 对齐 ctx.ui.notify 的第二个参数 */
|
|
41
|
+
export type FeishuUiNotifyPayload = {
|
|
42
|
+
text?: string;
|
|
43
|
+
level?: "info" | "warning" | "error" | string;
|
|
44
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 飞书用户姓名解析(供 `[飞书提出人] ... name=张三` 使用)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:`[飞书提出人]` 原来只有 open_id,事后完全看不出是谁提的
|
|
5
|
+
* (`知识库` 的 `需求-归档.md` 里就写着「没有姓名映射」)。飞书的 contact API 能查到,
|
|
6
|
+
* 但 name 属于**字段权限**:必须开 `contact:user.base:readonly`(或 `contact:contact:readonly`
|
|
7
|
+
* 等同组权限),并且该用户要在应用的**通讯录权限范围**内,否则接口要么 code=0 却 name 为空,
|
|
8
|
+
* 要么直接返回 41050 no user authority。
|
|
9
|
+
*
|
|
10
|
+
* 因此本模块的原则是**只增强、不阻塞**:查不到就返回 undefined,调用方原样降级回 open_id,
|
|
11
|
+
* 绝不让「查名字」把消息处理拖垮或搞失败。
|
|
12
|
+
*/
|
|
13
|
+
import { debugLog } from "./debug.ts";
|
|
14
|
+
import type { Domain } from "./types.ts";
|
|
15
|
+
|
|
16
|
+
/** 姓名缓存有效期:6 小时(改昵称不会立刻生效,但不必每条消息都打接口) */
|
|
17
|
+
const NAME_TTL_MS = 6 * 60 * 60 * 1000;
|
|
18
|
+
/** 查不到时的短缓存:10 分钟(没开权限时不要每条消息都白打一次) */
|
|
19
|
+
const MISS_TTL_MS = 10 * 60 * 1000;
|
|
20
|
+
/** 单次查询超时:查名字绝不能拖慢消息处理 */
|
|
21
|
+
const LOOKUP_TIMEOUT_MS = 2000;
|
|
22
|
+
|
|
23
|
+
export type UserNameConfig = {
|
|
24
|
+
appId: string;
|
|
25
|
+
appSecret: string;
|
|
26
|
+
domain?: Domain;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type CacheEntry = { name?: string; expires: number };
|
|
30
|
+
|
|
31
|
+
const nameCache = new Map<string, CacheEntry>();
|
|
32
|
+
let tokenCache: { token: string; expires: number } | undefined;
|
|
33
|
+
|
|
34
|
+
function baseUrl(domain?: Domain): string {
|
|
35
|
+
return domain === "lark" ? "https://open.larksuite.com" : "https://open.feishu.cn";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 取 tenant_access_token(与应用内其它模块同口径,内部缓存到过期前 60 秒) */
|
|
39
|
+
async function getTenantToken(cfg: UserNameConfig, fetchImpl: typeof fetch): Promise<string> {
|
|
40
|
+
if (tokenCache && tokenCache.expires > Date.now() + 60_000) return tokenCache.token;
|
|
41
|
+
const res = await fetchImpl(`${baseUrl(cfg.domain)}/open-apis/auth/v3/tenant_access_token/internal`, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
headers: { "Content-Type": "application/json" },
|
|
44
|
+
body: JSON.stringify({ app_id: cfg.appId, app_secret: cfg.appSecret }),
|
|
45
|
+
});
|
|
46
|
+
const json = (await res.json()) as { code?: number; msg?: string; tenant_access_token?: string; expire?: number };
|
|
47
|
+
if (json.code !== 0 || !json.tenant_access_token) {
|
|
48
|
+
throw new Error(`tenant token 获取失败:${json.msg || res.status}`);
|
|
49
|
+
}
|
|
50
|
+
tokenCache = { token: json.tenant_access_token, expires: Date.now() + (json.expire ?? 7200) * 1000 };
|
|
51
|
+
return tokenCache.token;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 本地超时包装(不引第三方依赖:查名字是可选增强,2 秒拿不到就放弃) */
|
|
55
|
+
async function withTimeout<T>(promise: Promise<T>, ms: number, message: string): Promise<T> {
|
|
56
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
57
|
+
try {
|
|
58
|
+
return await Promise.race([
|
|
59
|
+
promise,
|
|
60
|
+
new Promise<never>((_, reject) => {
|
|
61
|
+
timer = setTimeout(() => reject(new Error(message)), ms);
|
|
62
|
+
}),
|
|
63
|
+
]);
|
|
64
|
+
} finally {
|
|
65
|
+
if (timer) clearTimeout(timer);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 查飞书用户姓名。
|
|
71
|
+
*
|
|
72
|
+
* 查不到(没配应用凭证 / 缺字段权限 / 用户不在通讯录范围 / 网络失败 / 超时 / open_id 缺失)
|
|
73
|
+
* 一律返回 undefined,**绝不抛异常**。
|
|
74
|
+
*/
|
|
75
|
+
export async function resolveUserName(
|
|
76
|
+
openId: string | undefined,
|
|
77
|
+
cfg: UserNameConfig | undefined,
|
|
78
|
+
options?: { ttlMs?: number; timeoutMs?: number; fetchImpl?: typeof fetch },
|
|
79
|
+
): Promise<string | undefined> {
|
|
80
|
+
const id = openId?.trim();
|
|
81
|
+
if (!id || !cfg?.appId || !cfg.appSecret) return undefined;
|
|
82
|
+
|
|
83
|
+
const cached = nameCache.get(id);
|
|
84
|
+
if (cached && cached.expires > Date.now()) return cached.name;
|
|
85
|
+
|
|
86
|
+
const ttlMs = options?.ttlMs ?? NAME_TTL_MS;
|
|
87
|
+
const timeoutMs = options?.timeoutMs ?? LOOKUP_TIMEOUT_MS;
|
|
88
|
+
const fetchImpl = options?.fetchImpl ?? fetch;
|
|
89
|
+
try {
|
|
90
|
+
const token = await withTimeout(getTenantToken(cfg, fetchImpl), timeoutMs, "tenant token 超时");
|
|
91
|
+
const res = await withTimeout(
|
|
92
|
+
fetchImpl(`${baseUrl(cfg.domain)}/open-apis/contact/v3/users/${encodeURIComponent(id)}?user_id_type=open_id`, {
|
|
93
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
94
|
+
}),
|
|
95
|
+
timeoutMs,
|
|
96
|
+
"查询用户信息超时",
|
|
97
|
+
);
|
|
98
|
+
const json = (await res.json()) as { code?: number; msg?: string; data?: { user?: { name?: string } } };
|
|
99
|
+
const name = json.data?.user?.name?.trim() || undefined;
|
|
100
|
+
if (!name) {
|
|
101
|
+
// 两种典型:字段权限没开(code=0 但 name 为空)、用户不在通讯录权限范围(41050)
|
|
102
|
+
debugLog("feishu.user_name.missing", { openId: id, code: json.code, msg: json.msg });
|
|
103
|
+
}
|
|
104
|
+
nameCache.set(id, { name, expires: Date.now() + (name ? ttlMs : Math.min(ttlMs, MISS_TTL_MS)) });
|
|
105
|
+
return name;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
debugLog("feishu.user_name.error", {
|
|
108
|
+
openId: id,
|
|
109
|
+
error: error instanceof Error ? error.message : String(error),
|
|
110
|
+
});
|
|
111
|
+
// 失败也进短缓存,避免网络抖动时每条消息都卡 2 秒
|
|
112
|
+
nameCache.set(id, { name: undefined, expires: Date.now() + Math.min(ttlMs, MISS_TTL_MS) });
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 测试用:清空姓名与 token 缓存 */
|
|
118
|
+
export function resetUserNameCache(): void {
|
|
119
|
+
nameCache.clear();
|
|
120
|
+
tokenCache = undefined;
|
|
121
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工作区路径解析与校验(平台无关,各 Runtime 适配器共用)。
|
|
3
|
+
*/
|
|
4
|
+
import { existsSync, realpathSync, statSync } from "node:fs";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
7
|
+
|
|
8
|
+
export function resolveWorkspacePath(input: string) {
|
|
9
|
+
const trimmed = input.trim();
|
|
10
|
+
if (!trimmed) {
|
|
11
|
+
throw new Error("请在 /workspace 后面带上目录路径,例如:/workspace /Users/ax/project");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const expanded = trimmed === "~" || trimmed.startsWith("~/")
|
|
15
|
+
? join(homedir(), trimmed.slice(2))
|
|
16
|
+
: trimmed;
|
|
17
|
+
|
|
18
|
+
if (!isAbsolute(expanded)) {
|
|
19
|
+
throw new Error("当前只支持绝对路径或 ~/ 开头的路径。");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const resolved = resolve(expanded);
|
|
23
|
+
ensureWorkspaceExists(resolved);
|
|
24
|
+
return realpathSync(resolved);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function ensureWorkspaceExists(path: string) {
|
|
28
|
+
if (!existsSync(path)) {
|
|
29
|
+
throw new Error(`工作区不存在:${path}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let stat;
|
|
33
|
+
try {
|
|
34
|
+
stat = statSync(path);
|
|
35
|
+
} catch {
|
|
36
|
+
throw new Error(`无法访问工作区:${path}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!stat.isDirectory()) {
|
|
40
|
+
throw new Error(`工作区不是目录:${path}`);
|
|
41
|
+
}
|
|
42
|
+
}
|