@link-assistant/agent 0.16.8 → 0.16.10
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 +1 -1
- package/src/cli/output.ts +51 -5
- package/src/index.js +73 -14
- package/src/provider/models.ts +1 -1
- package/src/provider/provider.ts +54 -5
- package/src/session/prompt.ts +16 -0
package/package.json
CHANGED
package/src/cli/output.ts
CHANGED
|
@@ -62,16 +62,62 @@ export function isCompactJson(): boolean {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
*
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
@@ -54,29 +54,88 @@ try {
|
|
|
54
54
|
// Track if any errors occurred during execution
|
|
55
55
|
let hasError = false;
|
|
56
56
|
|
|
57
|
+
// Intercept stderr writes to ensure ALL output is JSON (#200)
|
|
58
|
+
// Bun runtime may print errors in plain text format (e.g., stack traces with source code)
|
|
59
|
+
// This interceptor wraps any non-JSON stderr output in a JSON envelope
|
|
60
|
+
const originalStderrWrite = process.stderr.write.bind(process.stderr);
|
|
61
|
+
process.stderr.write = function (chunk, encoding, callback) {
|
|
62
|
+
const str = typeof chunk === 'string' ? chunk : chunk.toString();
|
|
63
|
+
const trimmed = str.trim();
|
|
64
|
+
if (!trimmed) {
|
|
65
|
+
return originalStderrWrite(chunk, encoding, callback);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Check if it's already valid JSON
|
|
69
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
70
|
+
try {
|
|
71
|
+
JSON.parse(trimmed);
|
|
72
|
+
// Already JSON, pass through
|
|
73
|
+
return originalStderrWrite(chunk, encoding, callback);
|
|
74
|
+
} catch (_e) {
|
|
75
|
+
// Not valid JSON, wrap it
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Wrap non-JSON stderr output in JSON envelope
|
|
80
|
+
const wrapped = `${JSON.stringify({
|
|
81
|
+
type: 'error',
|
|
82
|
+
errorType: 'RuntimeError',
|
|
83
|
+
message: trimmed,
|
|
84
|
+
})}\n`;
|
|
85
|
+
return originalStderrWrite(wrapped, encoding, callback);
|
|
86
|
+
};
|
|
87
|
+
|
|
57
88
|
// Install global error handlers to ensure non-zero exit codes
|
|
89
|
+
// All output is JSON to ensure machine-parsability (#200)
|
|
58
90
|
process.on('uncaughtException', (error) => {
|
|
59
91
|
hasError = true;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
92
|
+
try {
|
|
93
|
+
outputError({
|
|
94
|
+
errorType: error?.name || 'UncaughtException',
|
|
95
|
+
message: error?.message || String(error),
|
|
96
|
+
stack: error?.stack,
|
|
97
|
+
...(error?.cause ? { cause: String(error.cause) } : {}),
|
|
98
|
+
});
|
|
99
|
+
} catch (_serializationError) {
|
|
100
|
+
// Last resort: write minimal JSON directly to stderr
|
|
101
|
+
process.stderr.write(
|
|
102
|
+
`${JSON.stringify({
|
|
103
|
+
type: 'error',
|
|
104
|
+
errorType: 'UncaughtException',
|
|
105
|
+
message: String(error),
|
|
106
|
+
})}\n`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
65
109
|
process.exit(1);
|
|
66
110
|
});
|
|
67
111
|
|
|
68
112
|
process.on('unhandledRejection', (reason, _promise) => {
|
|
69
113
|
hasError = true;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
114
|
+
try {
|
|
115
|
+
const errorOutput = {
|
|
116
|
+
errorType: 'UnhandledRejection',
|
|
117
|
+
message: reason?.message || String(reason),
|
|
118
|
+
stack: reason?.stack,
|
|
119
|
+
};
|
|
120
|
+
// If the error has a data property with a suggestion (e.g., ProviderModelNotFoundError), add it as a hint
|
|
121
|
+
if (reason?.data?.suggestion) {
|
|
122
|
+
errorOutput.hint = reason.data.suggestion;
|
|
123
|
+
}
|
|
124
|
+
// Include error data for debugging (#200)
|
|
125
|
+
if (reason?.data) {
|
|
126
|
+
errorOutput.data = reason.data;
|
|
127
|
+
}
|
|
128
|
+
outputError(errorOutput);
|
|
129
|
+
} catch (_serializationError) {
|
|
130
|
+
// Last resort: write minimal JSON directly to stderr
|
|
131
|
+
process.stderr.write(
|
|
132
|
+
`${JSON.stringify({
|
|
133
|
+
type: 'error',
|
|
134
|
+
errorType: 'UnhandledRejection',
|
|
135
|
+
message: String(reason),
|
|
136
|
+
})}\n`
|
|
137
|
+
);
|
|
78
138
|
}
|
|
79
|
-
outputError(errorOutput);
|
|
80
139
|
process.exit(1);
|
|
81
140
|
});
|
|
82
141
|
|
package/src/provider/models.ts
CHANGED
|
@@ -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
|
|
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
|
*/
|
package/src/provider/provider.ts
CHANGED
|
@@ -1416,21 +1416,70 @@ export namespace Provider {
|
|
|
1416
1416
|
providerID === 'link-assistant' || providerID === 'link-assistant/cache';
|
|
1417
1417
|
|
|
1418
1418
|
// For synthetic providers, we don't need model info from the database
|
|
1419
|
-
|
|
1419
|
+
let info = isSyntheticProvider ? null : provider.info.models[modelID];
|
|
1420
1420
|
if (!isSyntheticProvider && !info) {
|
|
1421
|
+
// Model not in provider's known list - try refreshing the cache first (#200)
|
|
1422
|
+
// This handles stale bundled data or expired cache (1-hour TTL)
|
|
1423
|
+
log.info(() => ({
|
|
1424
|
+
message: 'model not in catalog - refreshing models cache',
|
|
1425
|
+
providerID,
|
|
1426
|
+
modelID,
|
|
1427
|
+
}));
|
|
1428
|
+
try {
|
|
1429
|
+
await ModelsDev.refresh();
|
|
1430
|
+
const freshDB = await ModelsDev.get();
|
|
1431
|
+
const freshProvider = freshDB[providerID];
|
|
1432
|
+
if (freshProvider?.models[modelID]) {
|
|
1433
|
+
// Model found after refresh - update provider info and use it
|
|
1434
|
+
provider.info.models[modelID] = freshProvider.models[modelID];
|
|
1435
|
+
info = freshProvider.models[modelID];
|
|
1436
|
+
log.info(() => ({
|
|
1437
|
+
message: 'model found after cache refresh',
|
|
1438
|
+
providerID,
|
|
1439
|
+
modelID,
|
|
1440
|
+
}));
|
|
1441
|
+
}
|
|
1442
|
+
} catch (refreshError) {
|
|
1443
|
+
log.warn(() => ({
|
|
1444
|
+
message: 'cache refresh failed',
|
|
1445
|
+
error:
|
|
1446
|
+
refreshError instanceof Error
|
|
1447
|
+
? refreshError.message
|
|
1448
|
+
: String(refreshError),
|
|
1449
|
+
}));
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
if (!isSyntheticProvider && !info) {
|
|
1454
|
+
// Still not found after refresh - create fallback info and try anyway
|
|
1455
|
+
// Provider may support unlisted models
|
|
1421
1456
|
const availableInProvider = Object.keys(provider.info.models).slice(
|
|
1422
1457
|
0,
|
|
1423
1458
|
10
|
|
1424
1459
|
);
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1460
|
+
log.warn(() => ({
|
|
1461
|
+
message:
|
|
1462
|
+
'model not in provider catalog after refresh - attempting anyway (may be unlisted)',
|
|
1428
1463
|
providerID,
|
|
1429
1464
|
modelID,
|
|
1430
1465
|
availableModels: availableInProvider,
|
|
1431
1466
|
totalModels: Object.keys(provider.info.models).length,
|
|
1432
1467
|
}));
|
|
1433
|
-
|
|
1468
|
+
|
|
1469
|
+
// Create a minimal fallback model info so SDK loading can proceed
|
|
1470
|
+
// Use sensible defaults - the provider will reject if the model truly doesn't exist
|
|
1471
|
+
info = {
|
|
1472
|
+
id: modelID,
|
|
1473
|
+
name: modelID,
|
|
1474
|
+
release_date: '',
|
|
1475
|
+
attachment: false,
|
|
1476
|
+
reasoning: false,
|
|
1477
|
+
temperature: true,
|
|
1478
|
+
tool_call: true,
|
|
1479
|
+
cost: { input: 0, output: 0 },
|
|
1480
|
+
limit: { context: 128000, output: 16384 },
|
|
1481
|
+
options: {},
|
|
1482
|
+
} as ModelsDev.Model;
|
|
1434
1483
|
}
|
|
1435
1484
|
|
|
1436
1485
|
try {
|
package/src/session/prompt.ts
CHANGED
|
@@ -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;
|