@yushaw/sanqian-sdk 0.2.1 → 0.2.3

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