chatccc 0.2.145 → 0.2.146

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chatccc",
3
- "version": "0.2.145",
3
+ "version": "0.2.146",
4
4
  "description": "Feishu bot bridge for Claude Code",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -51,5 +51,7 @@ export async function readUtf8JsonBody<T>(req: IncomingMessage, maxBytes: number
51
51
  // 解码或 JSON 解析失败,尝试下一个编码
52
52
  }
53
53
  }
54
- throw new Error("Request body must be valid UTF-8 JSON");
54
+ const err = new Error("Request body must be valid UTF-8 JSON");
55
+ (err as { rawBody?: Buffer }).rawBody = body;
56
+ throw err;
55
57
  }
@@ -18,6 +18,16 @@ function jsonReply(res: ServerResponse, status: number, data: unknown): void {
18
18
  res.end(JSON.stringify(data));
19
19
  }
20
20
 
21
+ /**
22
+ * 从原始 body 字节中用正则尽力提取 session_id。
23
+ * 兼容 JSON 编码异常(如 GBK 中文导致 JSON.parse 失败)的场景。
24
+ */
25
+ function extractSessionIdFromRaw(buf: Buffer): string | null {
26
+ // 匹配 "session_id":"<uuid>" 或 "session_id": "<uuid>"
27
+ const match = buf.toString("latin1").match(/"session_id"\s*:\s*"([^"]+)"/);
28
+ return match?.[1] ?? null;
29
+ }
30
+
21
31
  export async function handleAgentStopStuckRequest(
22
32
  req: IncomingMessage,
23
33
  res: ServerResponse,
@@ -31,11 +41,23 @@ export async function handleAgentStopStuckRequest(
31
41
  }
32
42
 
33
43
  let body: { session_id?: string; final_reply?: string };
44
+ let rawBody: Buffer | null = null;
34
45
  try {
35
46
  body = await readUtf8JsonBody<{ session_id?: string; final_reply?: string }>(req, MAX_REQUEST_BYTES);
36
47
  } catch (err) {
37
- jsonReply(res, 400, { error: `Invalid request body: ${(err as Error).message}` });
38
- return true;
48
+ // JSON 解析失败时仍尝试从原始字节中提取 session_id 并强行停止
49
+ // 宁可输出乱码,也不能让 agent 持续循环
50
+ rawBody = (err as { rawBody?: Buffer }).rawBody as Buffer | undefined ?? null;
51
+ if (!rawBody) {
52
+ jsonReply(res, 400, { error: `Invalid request body: ${(err as Error).message}` });
53
+ return true;
54
+ }
55
+ const fallbackId = extractSessionIdFromRaw(rawBody);
56
+ if (!fallbackId) {
57
+ jsonReply(res, 400, { error: `Invalid request body: ${(err as Error).message}` });
58
+ return true;
59
+ }
60
+ body = { session_id: fallbackId };
39
61
  }
40
62
 
41
63
  const sessionId = typeof body?.session_id === "string" ? body.session_id.trim() : "";