Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* AI Card 流式响应模块
* 支持 AI Card 创建、流式更新、完成
*/
import axios from "axios";
import type { DingtalkConfig } from "../../types/index.ts";
import { DINGTALK_API, getAccessToken } from "../../utils/token.ts";
// ============ 常量 ============
const AI_CARD_TEMPLATE_ID = "02fcf2f4-5e02-4a85-b672-46d1f715543e.schema";
/** AI Card 状态 */
const AICardStatus = {
PROCESSING: "1",
INPUTING: "2",
FINISHED: "3",
EXECUTING: "4",
FAILED: "5",
} as const;
/** AI Card 实例接口 */
export interface AICardInstance {
cardInstanceId: string;
accessToken: string;
inputingStarted: boolean;
}
/** AI Card 投放目标类型 */
export type AICardTarget =
| { type: "user"; userId: string }
| { type: "group"; openConversationId: string };
// ============ Markdown 格式修正 ============
/**
* 确保 Markdown 表格前有空行,否则钉钉无法正确渲染表格
*/
function ensureTableBlankLines(text: string): string {
const lines = text.split("\n");
const result: string[] = [];
const tableDividerRegex = /^\s*\|?\s*:?-+:?\s*(\|?\s*:?-+:?\s*)+\|?\s*$/;
const tableRowRegex = /^\s*\|?.*\|.*\|?\s*$/;
const isDivider = (line: string) =>
line &&
typeof line === "string" &&
line.includes("|") &&
tableDividerRegex.test(line);
for (let i = 0; i < lines.length; i++) {
const currentLine = lines[i];
const nextLine = lines[i + 1] ?? "";
if (
tableRowRegex.test(currentLine) &&
isDivider(nextLine) &&
i > 0 &&
lines[i - 1].trim() !== "" &&
!tableRowRegex.test(lines[i - 1])
) {
result.push("");
}
result.push(currentLine);
}
return result.join("\n");
}
// ============ AI Card 相关 ============
/**
* 构建卡片投放请求体
*/
function buildDeliverBody(
cardInstanceId: string,
target: AICardTarget,
robotCode: string,
): any {
const base = { outTrackId: cardInstanceId, userIdType: 1 };
if (target.type === "group") {
return {
...base,
openSpaceId: `dtv1.card//IM_GROUP.${target.openConversationId}`,
imGroupOpenDeliverModel: {
robotCode,
extension: {
dynamicSummary: "true",
},
},
};
}
return {
...base,
openSpaceId: `dtv1.card//IM_ROBOT.${target.userId}`,
imRobotOpenDeliverModel: {
spaceType: 'IM_ROBOT',
robotCode,
extension: {
dynamicSummary: 'true',
},
},
};
}
/**
* 通用 AI Card 创建函数
*/
export async function createAICardForTarget(
config: DingtalkConfig,
target: AICardTarget,
log?: any,
): Promise<AICardInstance | null> {
const targetDesc =
target.type === "group"
? `群聊 ${target.openConversationId}`
: `用户 ${target.userId}`;
try {
const token = await getAccessToken(config);
const cardInstanceId = `card_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
log?.info?.(
`[DingTalk][AICard] 开始创建卡片:${targetDesc}, outTrackId=${cardInstanceId}`,
);
// 1. 创建卡片实例
const createBody = {
cardTemplateId: AI_CARD_TEMPLATE_ID,
outTrackId: cardInstanceId,
cardData: { cardParamMap: {} },
callbackType: "STREAM",
imGroupOpenSpaceModel: { supportForward: true },
imRobotOpenSpaceModel: { supportForward: true },
};
const createResp = await axios.post(
`${DINGTALK_API}/v1.0/card/instances`,
createBody,
{
headers: {
"x-acs-dingtalk-access-token": token,
"Content-Type": "application/json",
},
},
);
// 2. 投放卡片
const deliverBody = buildDeliverBody(
cardInstanceId,
target,
String(config.clientId ?? ""),
);
const deliverResp = await axios.post(
`${DINGTALK_API}/v1.0/card/instances/deliver`,
deliverBody,
{
headers: {
"x-acs-dingtalk-access-token": token,
"Content-Type": "application/json",
},
},
);
return { cardInstanceId, accessToken: token, inputingStarted: false };
} catch (err: any) {
log?.error?.(
`[DingTalk][AICard] 创建卡片失败 (${targetDesc}): ${err.message}`,
);
if (err.response) {
log?.error?.(
`[DingTalk][AICard] 错误响应:status=${err.response.status}`,
);
}
return null;
}
}
/**
* 流式更新 AI Card 内容
*/
export async function streamAICard(
card: AICardInstance,
content: string,
finished: boolean = false,
log?: any,
): Promise<void> {
if (!card.inputingStarted) {
const statusBody = {
outTrackId: card.cardInstanceId,
cardData: {
cardParamMap: {
flowStatus: AICardStatus.INPUTING,
msgContent: content,
staticMsgContent: "",
sys_full_json_obj: JSON.stringify({
order: ["msgContent"],
}),
},
},
};
try {
const statusResp = await axios.put(
`${DINGTALK_API}/v1.0/card/instances`,
statusBody,
{
headers: {
"x-acs-dingtalk-access-token": card.accessToken,
"Content-Type": "application/json",
},
},
);
log?.info?.(
`[DingTalk][AICard] INPUTING 响应:status=${statusResp.status}`,
);
} catch (err: any) {
log?.error?.(`[DingTalk][AICard] INPUTING 切换失败:${err.message}`);
throw err;
}
card.inputingStarted = true;
}
const fixedContent = ensureTableBlankLines(content);
const body = {
outTrackId: card.cardInstanceId,
guid: `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
key: "msgContent",
content: fixedContent,
isFull: true,
isFinalize: finished,
isError: false,
};
log?.info?.(
`[DingTalk][AICard] PUT /v1.0/card/streaming contentLen=${content.length} isFinalize=${finished}`,
);
try {
const streamResp = await axios.put(
`${DINGTALK_API}/v1.0/card/streaming`,
body,
{
headers: {
"x-acs-dingtalk-access-token": card.accessToken,
"Content-Type": "application/json",
},
},
);
log?.info?.(
`[DingTalk][AICard] streaming 响应:status=${streamResp.status}`,
);
} catch (err: any) {
log?.error?.(`[DingTalk][AICard] streaming 更新失败:${err.message}`);
throw err;
}
}
/**
* 完成 AI Card
*/
export async function finishAICard(
card: AICardInstance,
content: string,
log?: any,
): Promise<void> {
const fixedContent = ensureTableBlankLines(content);
log?.info?.(
`[DingTalk][AICard] 开始 finish,最终内容长度=${fixedContent.length}`,
);
await streamAICard(card, fixedContent, true, log);
const body = {
outTrackId: card.cardInstanceId,
cardData: {
cardParamMap: {
flowStatus: AICardStatus.FINISHED,
msgContent: fixedContent,
staticMsgContent: "",
sys_full_json_obj: JSON.stringify({
order: ["msgContent"],
}),
},
},
cardUpdateOptions: { updateCardDataByKey: true },
};
try {
const finishResp = await axios.put(
`${DINGTALK_API}/v1.0/card/instances`,
body,
{
headers: {
"x-acs-dingtalk-access-token": card.accessToken,
"Content-Type": "application/json",
},
},
);
log?.info?.(
`[DingTalk][AICard] FINISHED 响应:status=${finishResp.status}`,
);
} catch (err: any) {
log?.error?.(`[DingTalk][AICard] FINISHED 更新失败:${err.message}`);
}
}
|