@link-assistant/agent 0.16.8 → 0.16.9

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": "@link-assistant/agent",
3
- "version": "0.16.8",
3
+ "version": "0.16.9",
4
4
  "description": "A minimal, public domain AI CLI agent compatible with OpenCode's JSON interface. Bun-only runtime.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/cli/output.ts CHANGED
@@ -62,16 +62,62 @@ export function isCompactJson(): boolean {
62
62
  }
63
63
 
64
64
  /**
65
- * Format a message as JSON string
65
+ * Safe JSON replacer that handles cyclic references and non-serializable values.
66
+ * Returns a replacer function that tracks seen objects to detect cycles.
67
+ *
68
+ * @see https://github.com/link-assistant/agent/issues/200
69
+ */
70
+ function createSafeReplacer(): (key: string, value: unknown) => unknown {
71
+ const seen = new WeakSet();
72
+ return (_key: string, value: unknown) => {
73
+ if (typeof value === 'object' && value !== null) {
74
+ if (seen.has(value)) {
75
+ return '[Circular]';
76
+ }
77
+ seen.add(value);
78
+ }
79
+ // Convert Error objects to plain objects
80
+ if (value instanceof Error) {
81
+ return {
82
+ name: value.name,
83
+ message: value.message,
84
+ stack: value.stack,
85
+ ...(value.cause ? { cause: value.cause } : {}),
86
+ };
87
+ }
88
+ // Handle BigInt (not JSON-serializable by default)
89
+ if (typeof value === 'bigint') {
90
+ return value.toString();
91
+ }
92
+ return value;
93
+ };
94
+ }
95
+
96
+ /**
97
+ * Format a message as JSON string.
98
+ * Handles cyclic references and non-serializable values safely.
99
+ *
66
100
  * @param message - The message object to format
67
101
  * @param compact - Override the global compact setting
102
+ * @see https://github.com/link-assistant/agent/issues/200
68
103
  */
69
104
  export function formatJson(message: OutputMessage, compact?: boolean): string {
70
- // Check local, global, and Flag settings for compact mode
71
105
  const useCompact = compact ?? isCompactJson();
72
- return useCompact
73
- ? JSON.stringify(message)
74
- : JSON.stringify(message, null, 2);
106
+ try {
107
+ return useCompact
108
+ ? JSON.stringify(message, createSafeReplacer())
109
+ : JSON.stringify(message, createSafeReplacer(), 2);
110
+ } catch (_e) {
111
+ // Last resort fallback - should never happen with safe replacer
112
+ const fallback: OutputMessage = {
113
+ type: message.type ?? 'error',
114
+ message: 'Failed to serialize output',
115
+ serializationError: _e instanceof Error ? _e.message : String(_e),
116
+ };
117
+ return useCompact
118
+ ? JSON.stringify(fallback)
119
+ : JSON.stringify(fallback, null, 2);
120
+ }
75
121
  }
76
122
 
77
123
  /**
package/src/index.js CHANGED
@@ -55,28 +55,56 @@ try {
55
55
  let hasError = false;
56
56
 
57
57
  // Install global error handlers to ensure non-zero exit codes
58
+ // All output is JSON to ensure machine-parsability (#200)
58
59
  process.on('uncaughtException', (error) => {
59
60
  hasError = true;
60
- outputError({
61
- errorType: error.name || 'UncaughtException',
62
- message: error.message,
63
- stack: error.stack,
64
- });
61
+ try {
62
+ outputError({
63
+ errorType: error?.name || 'UncaughtException',
64
+ message: error?.message || String(error),
65
+ stack: error?.stack,
66
+ ...(error?.cause ? { cause: String(error.cause) } : {}),
67
+ });
68
+ } catch (_serializationError) {
69
+ // Last resort: write minimal JSON directly to stderr
70
+ process.stderr.write(
71
+ `${JSON.stringify({
72
+ type: 'error',
73
+ errorType: 'UncaughtException',
74
+ message: String(error),
75
+ })}\n`
76
+ );
77
+ }
65
78
  process.exit(1);
66
79
  });
67
80
 
68
81
  process.on('unhandledRejection', (reason, _promise) => {
69
82
  hasError = true;
70
- const errorOutput = {
71
- errorType: 'UnhandledRejection',
72
- message: reason?.message || String(reason),
73
- stack: reason?.stack,
74
- };
75
- // If the error has a data property with a suggestion (e.g., ProviderModelNotFoundError), add it as a hint
76
- if (reason?.data?.suggestion) {
77
- errorOutput.hint = reason.data.suggestion;
83
+ try {
84
+ const errorOutput = {
85
+ errorType: 'UnhandledRejection',
86
+ message: reason?.message || String(reason),
87
+ stack: reason?.stack,
88
+ };
89
+ // If the error has a data property with a suggestion (e.g., ProviderModelNotFoundError), add it as a hint
90
+ if (reason?.data?.suggestion) {
91
+ errorOutput.hint = reason.data.suggestion;
92
+ }
93
+ // Include error data for debugging (#200)
94
+ if (reason?.data) {
95
+ errorOutput.data = reason.data;
96
+ }
97
+ outputError(errorOutput);
98
+ } catch (_serializationError) {
99
+ // Last resort: write minimal JSON directly to stderr
100
+ process.stderr.write(
101
+ `${JSON.stringify({
102
+ type: 'error',
103
+ errorType: 'UnhandledRejection',
104
+ message: String(reason),
105
+ })}\n`
106
+ );
78
107
  }
79
- outputError(errorOutput);
80
108
  process.exit(1);
81
109
  });
82
110
 
@@ -83,7 +83,7 @@ export namespace ModelsDev {
83
83
  *
84
84
  * This prevents ProviderModelNotFoundError when:
85
85
  * - User runs agent for the first time (no cache)
86
- * - User has outdated cache missing new models like kimi-k2.5-free
86
+ * - User has outdated cache missing new models like glm-5-free
87
87
  *
88
88
  * @see https://github.com/link-assistant/agent/issues/175
89
89
  */
@@ -362,10 +362,24 @@ export namespace SessionPrompt {
362
362
 
363
363
  let model;
364
364
  try {
365
+ // Log model resolution attempt for debugging (#200)
366
+ log.info(() => ({
367
+ message: 'resolving model',
368
+ providerID: lastUser.model.providerID,
369
+ modelID: lastUser.model.modelID,
370
+ sessionID,
371
+ step,
372
+ }));
365
373
  model = await Provider.getModel(
366
374
  lastUser.model.providerID,
367
375
  lastUser.model.modelID
368
376
  );
377
+ log.info(() => ({
378
+ message: 'model resolved successfully',
379
+ providerID: lastUser.model.providerID,
380
+ modelID: lastUser.model.modelID,
381
+ resolvedModelID: model.language?.modelId,
382
+ }));
369
383
  } catch (error) {
370
384
  // When an explicit provider is specified, do NOT silently fall back to default
371
385
  // This ensures user's explicit choice is respected
@@ -376,6 +390,8 @@ export namespace SessionPrompt {
376
390
  providerID: lastUser.model.providerID,
377
391
  modelID: lastUser.model.modelID,
378
392
  error: error instanceof Error ? error.message : String(error),
393
+ stack: error instanceof Error ? error.stack : undefined,
394
+ hint: 'Check that the model exists in the provider. Use --verbose for more details.',
379
395
  }));
380
396
  // Re-throw the error so it can be handled by the caller
381
397
  throw error;