@cjhyy/code-shell-chat 0.9.21 → 0.9.22

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.
@@ -47,7 +47,23 @@ export class DesktopControlClient {
47
47
  }
48
48
  async petChat(input) {
49
49
  await this.ensureDesktopAvailable();
50
- return this.request("POST", "/v1/pet/chat", 150_000, input);
50
+ let result = await this.request("POST", "/v1/pet/chat/start", 30_000, input);
51
+ while (result && typeof result === "object" && "pending" in result && result.pending === true) {
52
+ if (typeof result.requestId !== "string" || !/^[a-f0-9-]{36}$/.test(result.requestId)) {
53
+ throw new DesktopControlUnavailableError("桌面端返回了无效请求标识,消息将重试");
54
+ }
55
+ result = await this.request("GET", `/v1/pet/chat/result/${result.requestId}?waitMs=25000`, 35_000);
56
+ }
57
+ if (!result ||
58
+ typeof result !== "object" ||
59
+ !("text" in result) ||
60
+ typeof result.text !== "string" ||
61
+ !("petSessionId" in result) ||
62
+ typeof result.petSessionId !== "string" ||
63
+ !result.petSessionId) {
64
+ throw new DesktopControlUnavailableError("桌面端未返回完整的 Mimi 结果,消息将重试");
65
+ }
66
+ return result;
51
67
  }
52
68
  /**
53
69
  * Ask the host where one inbound message should go. Short timeout on
@@ -172,9 +188,8 @@ export class DesktopControlClient {
172
188
  else
173
189
  signal?.addEventListener("abort", abortRequest, { once: true });
174
190
  const timer = setTimeout(() => controller.abort(), timeoutMs);
175
- let response;
176
191
  try {
177
- response = await this.fetchFn(`${descriptor.baseUrl}${path}`, {
192
+ const response = await this.fetchFn(`${descriptor.baseUrl}${path}`, {
178
193
  method,
179
194
  headers: {
180
195
  authorization: `Bearer ${descriptor.token}`,
@@ -183,27 +198,31 @@ export class DesktopControlClient {
183
198
  ...(payload === undefined ? {} : { body: JSON.stringify(payload) }),
184
199
  signal: controller.signal,
185
200
  });
201
+ // Keep the deadline and caller cancellation active until the body has
202
+ // arrived too. A dropped body is an ambiguous delivery, so the durable
203
+ // inbox must retry the same message rather than acknowledge an error.
204
+ const body = await response.json();
205
+ if (!response.ok) {
206
+ if (response.status === 503 || response.status === 502 || response.status === 504) {
207
+ throw new DesktopControlUnavailableError(typeof body?.message === "string" ? body.message : "桌面端暂时不可用,消息将重试");
208
+ }
209
+ throw new DesktopControlOperationError(typeof body?.message === "string"
210
+ ? body.message
211
+ : `桌面端操作失败(HTTP ${response.status})`);
212
+ }
213
+ return body;
186
214
  }
187
215
  catch (error) {
216
+ if (error instanceof DesktopControlOperationError ||
217
+ error instanceof DesktopControlUnavailableError) {
218
+ throw error;
219
+ }
188
220
  throw new DesktopControlUnavailableError(`无法连接桌面端:${error instanceof Error ? error.message : String(error)}`);
189
221
  }
190
222
  finally {
191
223
  clearTimeout(timer);
192
224
  signal?.removeEventListener("abort", abortRequest);
193
225
  }
194
- let body;
195
- try {
196
- body = await response.json();
197
- }
198
- catch {
199
- throw new DesktopControlOperationError(`桌面端返回了无效响应(HTTP ${response.status})`);
200
- }
201
- if (!response.ok) {
202
- throw new DesktopControlOperationError(typeof body?.message === "string"
203
- ? body.message
204
- : `桌面端操作失败(HTTP ${response.status})`);
205
- }
206
- return body;
207
226
  }
208
227
  }
209
228
  async function waitForAbort(signal) {
package/dist/gateway.js CHANGED
@@ -135,8 +135,9 @@ export function createMimiPetChat(options) {
135
135
  }
136
136
  catch (error) {
137
137
  if (error instanceof DesktopControlUnavailableError) {
138
- await reply({ text: `桌面端未在线:${error.message}` });
139
- return;
138
+ // Preserve accepted work on ambiguous disconnects. The durable inbox
139
+ // retries with the same message identity and recovers the real reply.
140
+ throw error;
140
141
  }
141
142
  if (error instanceof DesktopControlOperationError) {
142
143
  await reply({ text: `Mimi Pet 处理失败:${error.message}` });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cjhyy/code-shell-chat",
3
- "version": "0.9.21",
3
+ "version": "0.9.22",
4
4
  "description": "A standalone, transport-agnostic multi-channel chat gateway for Node.js and Bun.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",