@yushaw/sanqian-sdk 0.2.1 → 0.2.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/dist/index.mjs CHANGED
@@ -233,7 +233,7 @@ var DiscoveryManager = class {
233
233
  "/Contents/MacOS/Sanqian",
234
234
  ""
235
235
  );
236
- spawn("open", ["-a", appPath, "--args", "--hidden"], {
236
+ spawn("open", ["-g", "-a", appPath, "--args", "--hidden"], {
237
237
  detached: true,
238
238
  stdio: "ignore"
239
239
  }).unref();
@@ -262,6 +262,172 @@ var DiscoveryManager = class {
262
262
  }
263
263
  };
264
264
 
265
+ // src/errors.ts
266
+ var SANQIAN_WEBSITE = "https://sanqian.io";
267
+ var SDKErrorCode = /* @__PURE__ */ ((SDKErrorCode2) => {
268
+ SDKErrorCode2["NOT_INSTALLED"] = "NOT_INSTALLED";
269
+ SDKErrorCode2["NOT_RUNNING"] = "NOT_RUNNING";
270
+ SDKErrorCode2["CONNECTION_TIMEOUT"] = "CONNECTION_TIMEOUT";
271
+ SDKErrorCode2["STARTUP_TIMEOUT"] = "STARTUP_TIMEOUT";
272
+ SDKErrorCode2["REGISTRATION_FAILED"] = "REGISTRATION_FAILED";
273
+ SDKErrorCode2["REQUEST_TIMEOUT"] = "REQUEST_TIMEOUT";
274
+ SDKErrorCode2["REQUEST_FAILED"] = "REQUEST_FAILED";
275
+ SDKErrorCode2["DISCONNECTED"] = "DISCONNECTED";
276
+ SDKErrorCode2["WEBSOCKET_ERROR"] = "WEBSOCKET_ERROR";
277
+ SDKErrorCode2["AGENT_NOT_FOUND"] = "AGENT_NOT_FOUND";
278
+ SDKErrorCode2["CONVERSATION_NOT_FOUND"] = "CONVERSATION_NOT_FOUND";
279
+ SDKErrorCode2["TOOL_NOT_FOUND"] = "TOOL_NOT_FOUND";
280
+ SDKErrorCode2["TOOL_EXECUTION_TIMEOUT"] = "TOOL_EXECUTION_TIMEOUT";
281
+ return SDKErrorCode2;
282
+ })(SDKErrorCode || {});
283
+ var ErrorMessages = {
284
+ ["NOT_INSTALLED" /* NOT_INSTALLED */]: {
285
+ en: `Sanqian is not installed on this computer.`,
286
+ zh: `Sanqian \u5C1A\u672A\u5B89\u88C5\u5728\u6B64\u7535\u8111\u4E0A\u3002`,
287
+ hint: {
288
+ en: `Please download and install Sanqian from ${SANQIAN_WEBSITE}`,
289
+ zh: `\u8BF7\u8BBF\u95EE ${SANQIAN_WEBSITE} \u4E0B\u8F7D\u5B89\u88C5 Sanqian`
290
+ }
291
+ },
292
+ ["NOT_RUNNING" /* NOT_RUNNING */]: {
293
+ en: `Sanqian is not running.`,
294
+ zh: `Sanqian \u672A\u5728\u8FD0\u884C\u3002`,
295
+ hint: {
296
+ en: `Please start Sanqian first, or enable autoLaunchSanqian option in SDK config.`,
297
+ zh: `\u8BF7\u5148\u542F\u52A8 Sanqian\uFF0C\u6216\u5728 SDK \u914D\u7F6E\u4E2D\u542F\u7528 autoLaunchSanqian \u9009\u9879\u3002`
298
+ }
299
+ },
300
+ ["CONNECTION_TIMEOUT" /* CONNECTION_TIMEOUT */]: {
301
+ en: `Failed to connect to Sanqian (connection timeout).`,
302
+ zh: `\u8FDE\u63A5 Sanqian \u5931\u8D25\uFF08\u8FDE\u63A5\u8D85\u65F6\uFF09\u3002`,
303
+ hint: {
304
+ en: `Please check if Sanqian is running properly. If the problem persists, try restarting Sanqian.`,
305
+ zh: `\u8BF7\u68C0\u67E5 Sanqian \u662F\u5426\u6B63\u5E38\u8FD0\u884C\u3002\u5982\u95EE\u9898\u6301\u7EED\uFF0C\u8BF7\u5C1D\u8BD5\u91CD\u542F Sanqian\u3002`
306
+ }
307
+ },
308
+ ["STARTUP_TIMEOUT" /* STARTUP_TIMEOUT */]: {
309
+ en: `Sanqian failed to start within 2 minutes.`,
310
+ zh: `Sanqian \u5728 2 \u5206\u949F\u5185\u672A\u80FD\u542F\u52A8\u3002`,
311
+ hint: {
312
+ en: `Please try starting Sanqian manually. If it fails to start, reinstall from ${SANQIAN_WEBSITE}`,
313
+ zh: `\u8BF7\u5C1D\u8BD5\u624B\u52A8\u542F\u52A8 Sanqian\u3002\u5982\u4ECD\u65E0\u6CD5\u542F\u52A8\uFF0C\u8BF7\u4ECE ${SANQIAN_WEBSITE} \u91CD\u65B0\u5B89\u88C5\u3002`
314
+ }
315
+ },
316
+ ["REGISTRATION_FAILED" /* REGISTRATION_FAILED */]: {
317
+ en: `Failed to register with Sanqian.`,
318
+ zh: `\u5411 Sanqian \u6CE8\u518C\u5931\u8D25\u3002`,
319
+ hint: {
320
+ en: `Please check your SDK configuration (appName, tools). If the problem persists, try restarting Sanqian.`,
321
+ zh: `\u8BF7\u68C0\u67E5 SDK \u914D\u7F6E\uFF08appName\u3001tools\uFF09\u3002\u5982\u95EE\u9898\u6301\u7EED\uFF0C\u8BF7\u5C1D\u8BD5\u91CD\u542F Sanqian\u3002`
322
+ }
323
+ },
324
+ ["REQUEST_TIMEOUT" /* REQUEST_TIMEOUT */]: {
325
+ en: `Request timed out.`,
326
+ zh: `\u8BF7\u6C42\u8D85\u65F6\u3002`,
327
+ hint: {
328
+ en: `The operation took too long. Please try again.`,
329
+ zh: `\u64CD\u4F5C\u8017\u65F6\u8FC7\u957F\uFF0C\u8BF7\u91CD\u8BD5\u3002`
330
+ }
331
+ },
332
+ ["REQUEST_FAILED" /* REQUEST_FAILED */]: {
333
+ en: `Request failed.`,
334
+ zh: `\u8BF7\u6C42\u5931\u8D25\u3002`,
335
+ hint: {
336
+ en: `Please check the error details and try again.`,
337
+ zh: `\u8BF7\u68C0\u67E5\u9519\u8BEF\u8BE6\u60C5\u540E\u91CD\u8BD5\u3002`
338
+ }
339
+ },
340
+ ["DISCONNECTED" /* DISCONNECTED */]: {
341
+ en: `Disconnected from Sanqian.`,
342
+ zh: `\u4E0E Sanqian \u7684\u8FDE\u63A5\u5DF2\u65AD\u5F00\u3002`,
343
+ hint: {
344
+ en: `The SDK will automatically reconnect. If problems persist, check if Sanqian is still running.`,
345
+ zh: `SDK \u4F1A\u81EA\u52A8\u91CD\u8FDE\u3002\u5982\u95EE\u9898\u6301\u7EED\uFF0C\u8BF7\u68C0\u67E5 Sanqian \u662F\u5426\u4ECD\u5728\u8FD0\u884C\u3002`
346
+ }
347
+ },
348
+ ["WEBSOCKET_ERROR" /* WEBSOCKET_ERROR */]: {
349
+ en: `WebSocket connection error.`,
350
+ zh: `WebSocket \u8FDE\u63A5\u9519\u8BEF\u3002`,
351
+ hint: {
352
+ en: `Please check your network and firewall settings. Sanqian uses local WebSocket on port shown in settings.`,
353
+ zh: `\u8BF7\u68C0\u67E5\u7F51\u7EDC\u548C\u9632\u706B\u5899\u8BBE\u7F6E\u3002Sanqian \u4F7F\u7528\u672C\u5730 WebSocket\uFF0C\u7AEF\u53E3\u53EF\u5728\u8BBE\u7F6E\u4E2D\u67E5\u770B\u3002`
354
+ }
355
+ },
356
+ ["AGENT_NOT_FOUND" /* AGENT_NOT_FOUND */]: {
357
+ en: `Agent not found.`,
358
+ zh: `\u627E\u4E0D\u5230\u8BE5 Agent\u3002`,
359
+ hint: {
360
+ en: `Please check the agent ID. Use listAgents() to see available agents.`,
361
+ zh: `\u8BF7\u68C0\u67E5 Agent ID\u3002\u4F7F\u7528 listAgents() \u67E5\u770B\u53EF\u7528\u7684 Agent\u3002`
362
+ }
363
+ },
364
+ ["CONVERSATION_NOT_FOUND" /* CONVERSATION_NOT_FOUND */]: {
365
+ en: `Conversation not found.`,
366
+ zh: `\u627E\u4E0D\u5230\u8BE5\u5BF9\u8BDD\u3002`,
367
+ hint: {
368
+ en: `The conversation may have been deleted. Start a new conversation with startConversation().`,
369
+ zh: `\u8BE5\u5BF9\u8BDD\u53EF\u80FD\u5DF2\u88AB\u5220\u9664\u3002\u4F7F\u7528 startConversation() \u5F00\u59CB\u65B0\u5BF9\u8BDD\u3002`
370
+ }
371
+ },
372
+ ["TOOL_NOT_FOUND" /* TOOL_NOT_FOUND */]: {
373
+ en: `Tool not found.`,
374
+ zh: `\u627E\u4E0D\u5230\u8BE5\u5DE5\u5177\u3002`,
375
+ hint: {
376
+ en: `Please check that the tool is registered in your SDK config.`,
377
+ zh: `\u8BF7\u68C0\u67E5\u8BE5\u5DE5\u5177\u662F\u5426\u5DF2\u5728 SDK \u914D\u7F6E\u4E2D\u6CE8\u518C\u3002`
378
+ }
379
+ },
380
+ ["TOOL_EXECUTION_TIMEOUT" /* TOOL_EXECUTION_TIMEOUT */]: {
381
+ en: `Tool execution timed out.`,
382
+ zh: `\u5DE5\u5177\u6267\u884C\u8D85\u65F6\u3002`,
383
+ hint: {
384
+ en: `The tool took too long to execute. Consider increasing toolExecutionTimeout in SDK config.`,
385
+ zh: `\u5DE5\u5177\u6267\u884C\u65F6\u95F4\u8FC7\u957F\u3002\u53EF\u5728 SDK \u914D\u7F6E\u4E2D\u589E\u52A0 toolExecutionTimeout\u3002`
386
+ }
387
+ }
388
+ };
389
+ var SanqianSDKError = class extends Error {
390
+ code;
391
+ messageZh;
392
+ hint;
393
+ hintZh;
394
+ constructor(code, details) {
395
+ const msg = ErrorMessages[code];
396
+ const fullMessage = details ? `${msg.en} ${details}` : msg.en;
397
+ super(fullMessage);
398
+ this.name = "SanqianSDKError";
399
+ this.code = code;
400
+ this.messageZh = details ? `${msg.zh} ${details}` : msg.zh;
401
+ this.hint = msg.hint?.en;
402
+ this.hintZh = msg.hint?.zh;
403
+ }
404
+ /**
405
+ * Get full error message with hint (English)
406
+ */
407
+ getFullMessage() {
408
+ return this.hint ? `${this.message}
409
+ ${this.hint}` : this.message;
410
+ }
411
+ /**
412
+ * Get full error message with hint (Chinese)
413
+ */
414
+ getFullMessageZh() {
415
+ return this.hintZh ? `${this.messageZh}
416
+ ${this.hintZh}` : this.messageZh;
417
+ }
418
+ /**
419
+ * Get bilingual error message
420
+ */
421
+ getBilingualMessage() {
422
+ return `${this.getFullMessage()}
423
+
424
+ ${this.getFullMessageZh()}`;
425
+ }
426
+ };
427
+ function createSDKError(code, details) {
428
+ return new SanqianSDKError(code, details);
429
+ }
430
+
265
431
  // src/client.ts
266
432
  var SanqianSDK = class _SanqianSDK {
267
433
  config;
@@ -328,7 +494,7 @@ var SanqianSDK = class _SanqianSDK {
328
494
  console.log(`[SDK] Connecting to ${url}`);
329
495
  this.ws = new WebSocket(url);
330
496
  const connectTimeout = setTimeout(() => {
331
- reject(new Error("WebSocket connection timeout"));
497
+ reject(createSDKError("CONNECTION_TIMEOUT" /* CONNECTION_TIMEOUT */));
332
498
  this.ws?.close();
333
499
  }, 1e4);
334
500
  this.ws.on("open", async () => {
@@ -409,7 +575,7 @@ var SanqianSDK = class _SanqianSDK {
409
575
  1e4
410
576
  );
411
577
  if (!response.success) {
412
- throw new Error(response.error || "Registration failed");
578
+ throw createSDKError("REGISTRATION_FAILED" /* REGISTRATION_FAILED */, response.error);
413
579
  }
414
580
  this.state.registering = false;
415
581
  this.state.registered = true;
@@ -541,7 +707,7 @@ var SanqianSDK = class _SanqianSDK {
541
707
  this.state.registered = false;
542
708
  this.emit("disconnected", reason);
543
709
  for (const [, pending] of this.pendingRequests) {
544
- pending.reject(new Error("Disconnected"));
710
+ pending.reject(createSDKError("DISCONNECTED" /* DISCONNECTED */));
545
711
  }
546
712
  this.pendingRequests.clear();
547
713
  this.scheduleReconnect();
@@ -633,7 +799,7 @@ var SanqianSDK = class _SanqianSDK {
633
799
  return new Promise((resolve, reject) => {
634
800
  const timer = setTimeout(() => {
635
801
  this.pendingRequests.delete(id);
636
- reject(new Error("Request timeout"));
802
+ reject(createSDKError("REQUEST_TIMEOUT" /* REQUEST_TIMEOUT */));
637
803
  }, timeout);
638
804
  this.pendingRequests.set(id, {
639
805
  resolve: (value) => {
@@ -712,14 +878,12 @@ var SanqianSDK = class _SanqianSDK {
712
878
  console.log("[SDK] Sanqian not running, attempting to launch...");
713
879
  const launched = this.discovery.launchSanqian(this.config.sanqianPath);
714
880
  if (!launched) {
715
- throw new Error(
716
- "Sanqian executable not found. Please ensure Sanqian is installed, or provide a custom path via sanqianPath config option."
717
- );
881
+ throw createSDKError("NOT_INSTALLED" /* NOT_INSTALLED */);
718
882
  }
719
883
  console.log("[SDK] Sanqian launch initiated, waiting for startup...");
720
884
  info = await this.waitForSanqianStartup();
721
885
  } else {
722
- throw new Error("Sanqian is not running. Please start it manually.");
886
+ throw createSDKError("NOT_RUNNING" /* NOT_RUNNING */);
723
887
  }
724
888
  }
725
889
  await this.connectWithInfo(info);
@@ -738,9 +902,7 @@ var SanqianSDK = class _SanqianSDK {
738
902
  }
739
903
  await new Promise((resolve) => setTimeout(resolve, pollInterval));
740
904
  }
741
- throw new Error(
742
- "Sanqian startup timeout (2 minutes). Please check if Sanqian started correctly."
743
- );
905
+ throw createSDKError("STARTUP_TIMEOUT" /* STARTUP_TIMEOUT */);
744
906
  }
745
907
  /**
746
908
  * Update tool list dynamically
@@ -786,7 +948,7 @@ var SanqianSDK = class _SanqianSDK {
786
948
  };
787
949
  const response = await this.sendAndWait(message, msgId, 1e4);
788
950
  if (!response.success) {
789
- throw new Error(response.error || "Failed to create agent");
951
+ throw createSDKError("AGENT_NOT_FOUND" /* AGENT_NOT_FOUND */, response.error);
790
952
  }
791
953
  if (response.agent) {
792
954
  return response.agent;
@@ -827,7 +989,7 @@ var SanqianSDK = class _SanqianSDK {
827
989
  };
828
990
  const response = await this.sendAndWait(message, msgId, 1e4);
829
991
  if (!response.success) {
830
- throw new Error(response.error || "Failed to delete agent");
992
+ throw createSDKError("AGENT_NOT_FOUND" /* AGENT_NOT_FOUND */, response.error);
831
993
  }
832
994
  }
833
995
  /**
@@ -849,7 +1011,7 @@ var SanqianSDK = class _SanqianSDK {
849
1011
  };
850
1012
  const response = await this.sendAndWait(message, msgId, 1e4);
851
1013
  if (!response.success || !response.agent) {
852
- throw new Error(response.error || "Failed to update agent");
1014
+ throw createSDKError("AGENT_NOT_FOUND" /* AGENT_NOT_FOUND */, response.error);
853
1015
  }
854
1016
  return response.agent;
855
1017
  }
@@ -894,7 +1056,7 @@ var SanqianSDK = class _SanqianSDK {
894
1056
  };
895
1057
  const response = await this.sendAndWait(message, msgId, 1e4);
896
1058
  if (!response.success || !response.conversation) {
897
- throw new Error(response.error || "Failed to get conversation");
1059
+ throw createSDKError("CONVERSATION_NOT_FOUND" /* CONVERSATION_NOT_FOUND */, response.error);
898
1060
  }
899
1061
  return response.conversation;
900
1062
  }
@@ -911,7 +1073,7 @@ var SanqianSDK = class _SanqianSDK {
911
1073
  };
912
1074
  const response = await this.sendAndWait(message, msgId, 1e4);
913
1075
  if (!response.success) {
914
- throw new Error(response.error || "Failed to delete conversation");
1076
+ throw createSDKError("CONVERSATION_NOT_FOUND" /* CONVERSATION_NOT_FOUND */, response.error);
915
1077
  }
916
1078
  }
917
1079
  // ============================================
@@ -949,7 +1111,11 @@ var SanqianSDK = class _SanqianSDK {
949
1111
  };
950
1112
  const response = await this.sendAndWait(message, msgId, 6e5);
951
1113
  if (!response.success) {
952
- throw new Error(response.error || "Chat request failed");
1114
+ const errorLower = (response.error || "").toLowerCase();
1115
+ if (errorLower.includes("agent") && errorLower.includes("not found")) {
1116
+ throw createSDKError("AGENT_NOT_FOUND" /* AGENT_NOT_FOUND */, response.error);
1117
+ }
1118
+ throw createSDKError("REQUEST_FAILED" /* REQUEST_FAILED */, response.error);
953
1119
  }
954
1120
  return {
955
1121
  message: response.message,
@@ -1097,7 +1263,7 @@ var SanqianSDK = class _SanqianSDK {
1097
1263
  }
1098
1264
  createTimeout(ms) {
1099
1265
  return new Promise((_, reject) => {
1100
- setTimeout(() => reject(new Error("Timeout")), ms);
1266
+ setTimeout(() => reject(createSDKError("TOOL_EXECUTION_TIMEOUT" /* TOOL_EXECUTION_TIMEOUT */)), ms);
1101
1267
  });
1102
1268
  }
1103
1269
  };
@@ -1180,6 +1346,11 @@ var Conversation = class {
1180
1346
  export {
1181
1347
  Conversation,
1182
1348
  DiscoveryManager,
1183
- SanqianSDK
1349
+ ErrorMessages,
1350
+ SANQIAN_WEBSITE,
1351
+ SDKErrorCode,
1352
+ SanqianSDK,
1353
+ SanqianSDKError,
1354
+ createSDKError
1184
1355
  };
1185
1356
  //# sourceMappingURL=index.mjs.map