@xmanrui/dsh-im 1.0.0 → 1.0.1

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": "@xmanrui/dsh-im",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "把九种 IM 机器人和公网 AI Office 接入本机 DeepSeek Harness。 Connect nine IM channels and a public AI Office to a local DeepSeek Harness.",
5
5
  "keywords": [
6
6
  "deepseek-harness",
@@ -305,7 +305,14 @@ const EN = Object.freeze({
305
305
  '微信已授权,但登录凭据无法写入 DSH 凭据存储。请检查凭据存储是否可写。': 'WeChat was authorized, but the login credential could not be written to the DSH credential store. Check that the store is writable.',
306
306
  '微信已授权,但账号配置无法写入本机。请检查 DSH_HOME 目录权限。': 'WeChat was authorized, but the account configuration could not be saved locally. Check the DSH_HOME directory permissions.',
307
307
  '微信已授权,但无法初始化账号状态或工作区。请检查 DSH_HOME 和工作区目录。': 'WeChat was authorized, but the account state or workspace could not be initialized. Check DSH_HOME and the workspace directory.',
308
- '微信已授权,但插件无法连接本机 Harness。请确认 dsh web 已正常启动。': 'WeChat was authorized, but the plugin could not reach the local Harness. Confirm that dsh web is running normally.',
308
+ '微信已授权,但插件无法连接本机 Harness。请检查 dsh web 地址和端口。': 'WeChat was authorized, but the plugin could not connect to the local Harness. Check the dsh web address and port.',
309
+ '微信已授权,但 Harness 健康检查超时。请确认 dsh web 未阻塞。': 'WeChat was authorized, but the Harness health check timed out. Confirm that dsh web is not blocked.',
310
+ '微信已授权,但 Harness 拒绝了本机健康检查。请检查 Host 信任配置。': 'WeChat was authorized, but Harness denied the local health check. Check the Host trust configuration.',
311
+ '微信已授权,但找不到 Harness 健康检查接口。请确认 Harness 与插件版本兼容。': 'WeChat was authorized, but the Harness health endpoint was not found. Confirm that Harness and the plugin are compatible.',
312
+ '微信已授权,但 Harness 健康检查返回服务错误。请查看 dsh web 日志。': 'WeChat was authorized, but the Harness health check returned a service error. Check the dsh web logs.',
313
+ '微信已授权,但 Harness 返回了无法识别的响应。请确认 Harness 与插件版本兼容。': 'WeChat was authorized, but Harness returned an unrecognized response. Confirm that Harness and the plugin are compatible.',
314
+ '微信已授权,但 Harness 拒绝了健康检查请求。请查看 dsh web 日志。': 'WeChat was authorized, but Harness rejected the health-check request. Check the dsh web logs.',
315
+ '微信已授权,但 Harness 健康检查发生未知错误。请查看 dsh web 日志。': 'WeChat was authorized, but the Harness health check failed unexpectedly. Check the dsh web logs.',
309
316
  '微信已授权,但消息连接初始化失败。请查看 dsh web 日志后重试。': 'WeChat was authorized, but the message connection could not be initialized. Check the dsh web logs and try again.',
310
317
  '微信已授权,但激活过程中发生未知错误。请查看 dsh web 日志。': 'WeChat was authorized, but an unknown error occurred during activation. Check the dsh web logs.',
311
318
  '微信已绑定,可以开始向已绑定的机器人发消息。': 'WeChat is connected and ready for messages.',
@@ -108,7 +108,7 @@ export class DiscordApi {
108
108
  headers: {
109
109
  authorization: `Bot ${this.#token}`,
110
110
  'content-type': 'application/json',
111
- 'user-agent': 'DeepSeek-Harness-dsh-im (https://github.com/xmanrui/dsh-im, 1.0.0)',
111
+ 'user-agent': 'DeepSeek-Harness-dsh-im (https://github.com/xmanrui/dsh-im, 1.0.1)',
112
112
  },
113
113
  ...(body === undefined ? {} : { body: JSON.stringify(body) }),
114
114
  signal: requestSignal(signal, timeoutMs),
@@ -364,6 +364,26 @@ export class HarnessRpcError extends Error {
364
364
  }
365
365
  }
366
366
 
367
+ export class HarnessTransportError extends Error {
368
+ constructor(code, method, { cause, status } = {}) {
369
+ const statusDetail = Number.isInteger(status) ? `, HTTP ${status}` : '';
370
+ super(`Harness ${method} transport failed (${code}${statusDetail})`, { cause });
371
+ this.name = 'HarnessTransportError';
372
+ this.code = code;
373
+ this.method = method;
374
+ if (Number.isInteger(status)) this.status = status;
375
+ }
376
+ }
377
+
378
+ export class HarnessHealthError extends Error {
379
+ constructor(cause) {
380
+ super('Harness health RPC was rejected', { cause });
381
+ this.name = 'HarnessHealthError';
382
+ this.code = 'harness-rpc-rejected';
383
+ this.method = 'host.describe';
384
+ }
385
+ }
386
+
367
387
  export class HarnessInteractionError extends Error {
368
388
  constructor(code, message) {
369
389
  super(message);
@@ -455,24 +475,60 @@ export class HarnessClient {
455
475
  const signal = options.signal
456
476
  ? AbortSignal.any([options.signal, timeoutSignal])
457
477
  : timeoutSignal;
458
- const response = await this.#fetch(new URL(`/api/${method}`, this.#baseUrl), {
459
- method: 'POST',
460
- headers: { 'content-type': 'application/json' },
461
- body: JSON.stringify({ type: 'client-request', rpcId, method, payload }),
462
- signal,
463
- });
464
- if (!response.ok) throw new Error(`Harness transport ${method} failed: HTTP ${response.status}`);
465
- const body = await response.json();
478
+ let response;
479
+ try {
480
+ response = await this.#fetch(new URL(`/api/${method}`, this.#baseUrl), {
481
+ method: 'POST',
482
+ headers: { 'content-type': 'application/json' },
483
+ body: JSON.stringify({ type: 'client-request', rpcId, method, payload }),
484
+ signal,
485
+ });
486
+ } catch (error) {
487
+ // Preserve an explicit caller cancellation; it is control flow, not a
488
+ // Harness availability diagnosis.
489
+ if (options.signal?.aborted) throw error;
490
+ throw new HarnessTransportError(
491
+ timeoutSignal.aborted ? 'harness-timeout' : 'harness-connect-failed',
492
+ method,
493
+ { cause: error },
494
+ );
495
+ }
496
+ if (!response.ok) {
497
+ const code = response.status === 401 || response.status === 403
498
+ ? 'harness-access-denied'
499
+ : response.status === 404
500
+ ? 'harness-api-not-found'
501
+ : 'harness-http-failed';
502
+ throw new HarnessTransportError(code, method, { status: response.status });
503
+ }
504
+ let body;
505
+ try {
506
+ body = await response.json();
507
+ } catch (error) {
508
+ throw new HarnessTransportError('harness-response-invalid', method, { cause: error });
509
+ }
466
510
  if (body?.type !== 'server-response' || body?.rpcId !== rpcId) {
467
- throw new Error(`Harness returned an invalid response for ${method}`);
511
+ throw new HarnessTransportError('harness-response-invalid', method, {
512
+ cause: new Error(`Harness returned an invalid response for ${method}`),
513
+ });
514
+ }
515
+ if (!body.result || typeof body.result !== 'object' || typeof body.result.ok !== 'boolean') {
516
+ throw new HarnessTransportError('harness-response-invalid', method, {
517
+ cause: new Error(`Harness returned an invalid result for ${method}`),
518
+ });
468
519
  }
469
520
  if (!body.result?.ok) throw new HarnessRpcError(method, body.result?.error);
470
521
  return body.result.value;
471
522
  }
472
523
 
473
524
  async health(options = {}) {
474
- await this.rpc('host.describe', {}, 5_000, options);
475
- return true;
525
+ try {
526
+ await this.rpc('host.describe', {}, 5_000, options);
527
+ return true;
528
+ } catch (error) {
529
+ if (error instanceof HarnessRpcError) throw new HarnessHealthError(error);
530
+ throw error;
531
+ }
476
532
  }
477
533
 
478
534
  async ensureRunning(options = {}) {
@@ -506,7 +562,8 @@ export class HarnessClient {
506
562
  lastError = error;
507
563
  }
508
564
  }
509
- throw new Error(`Harness did not become ready: ${lastError?.message ?? 'timeout'}`);
565
+ if (lastError) throw lastError;
566
+ throw new HarnessTransportError('harness-timeout', 'host.describe');
510
567
  }
511
568
 
512
569
  async listWorkspaces(options = {}) {
@@ -184,7 +184,7 @@ function authenticatedHeaders(token) {
184
184
  function baseInfo() {
185
185
  return {
186
186
  channel_version: WEIXIN_PROTOCOL_VERSION,
187
- bot_agent: 'DeepSeekHarness/1.0.0',
187
+ bot_agent: 'DeepSeekHarness/1.0.1',
188
188
  };
189
189
  }
190
190
 
@@ -25,7 +25,14 @@ const ACTIVATION_ERROR_MESSAGES = Object.freeze({
25
25
  'credential-save-failed': '微信已授权,但登录凭据无法写入 DSH 凭据存储。请检查凭据存储是否可写。',
26
26
  'account-config-save-failed': '微信已授权,但账号配置无法写入本机。请检查 DSH_HOME 目录权限。',
27
27
  'runtime-prepare-failed': '微信已授权,但无法初始化账号状态或工作区。请检查 DSH_HOME 和工作区目录。',
28
- 'harness-unreachable': '微信已授权,但插件无法连接本机 Harness。请确认 dsh web 已正常启动。',
28
+ 'harness-connect-failed': '微信已授权,但插件无法连接本机 Harness。请检查 dsh web 地址和端口。',
29
+ 'harness-timeout': '微信已授权,但 Harness 健康检查超时。请确认 dsh web 未阻塞。',
30
+ 'harness-access-denied': '微信已授权,但 Harness 拒绝了本机健康检查。请检查 Host 信任配置。',
31
+ 'harness-api-not-found': '微信已授权,但找不到 Harness 健康检查接口。请确认 Harness 与插件版本兼容。',
32
+ 'harness-http-failed': '微信已授权,但 Harness 健康检查返回服务错误。请查看 dsh web 日志。',
33
+ 'harness-response-invalid': '微信已授权,但 Harness 返回了无法识别的响应。请确认 Harness 与插件版本兼容。',
34
+ 'harness-rpc-rejected': '微信已授权,但 Harness 拒绝了健康检查请求。请查看 dsh web 日志。',
35
+ 'harness-check-unknown-failed': '微信已授权,但 Harness 健康检查发生未知错误。请查看 dsh web 日志。',
29
36
  'connection-start-failed': '微信已授权,但消息连接初始化失败。请查看 dsh web 日志后重试。',
30
37
  });
31
38
 
@@ -6,6 +6,15 @@ import {
6
6
  } from '../shared/connection-test.mjs';
7
7
 
8
8
  const DEFAULT_START_RETRY_DELAYS_MS = Object.freeze([250, 1_000, 3_000]);
9
+ const HARNESS_HEALTH_ERROR_CODES = new Set([
10
+ 'harness-connect-failed',
11
+ 'harness-timeout',
12
+ 'harness-access-denied',
13
+ 'harness-api-not-found',
14
+ 'harness-http-failed',
15
+ 'harness-response-invalid',
16
+ 'harness-rpc-rejected',
17
+ ]);
9
18
 
10
19
  function startRetryDelays(value) {
11
20
  if (value === undefined) return [...DEFAULT_START_RETRY_DELAYS_MS];
@@ -32,6 +41,13 @@ function runtimeStartError(code, cause) {
32
41
  return error;
33
42
  }
34
43
 
44
+ function harnessHealthError(cause) {
45
+ const code = HARNESS_HEALTH_ERROR_CODES.has(cause?.code)
46
+ ? cause.code
47
+ : 'harness-check-unknown-failed';
48
+ return runtimeStartError(code, cause);
49
+ }
50
+
35
51
  function delay(ms, signal) {
36
52
  return new Promise((resolve, reject) => {
37
53
  if (signal?.aborted) {
@@ -127,7 +143,7 @@ export class WeixinRuntime {
127
143
  try {
128
144
  await this.#harness.ensureRunning();
129
145
  } catch (error) {
130
- throw runtimeStartError('harness-unreachable', error);
146
+ throw harnessHealthError(error);
131
147
  }
132
148
  this.#status.harnessReachable = true;
133
149
  await this.#notifyStart();