ai-sdk-provider-claude-code 3.5.1 → 3.5.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/README.md +17 -1
- package/dist/index.cjs +73 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +73 -13
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/dist/index.d.cts
CHANGED
|
@@ -1243,8 +1243,9 @@ declare function createAPICallError({ message, code, exitCode, stderr, promptExc
|
|
|
1243
1243
|
* });
|
|
1244
1244
|
* ```
|
|
1245
1245
|
*/
|
|
1246
|
-
declare function createAuthenticationError({ message }: {
|
|
1246
|
+
declare function createAuthenticationError({ message, stderr, }: {
|
|
1247
1247
|
message: string;
|
|
1248
|
+
stderr?: string;
|
|
1248
1249
|
}): LoadAPIKeyError;
|
|
1249
1250
|
/**
|
|
1250
1251
|
* Creates a timeout error for Claude Code SDK operations.
|
|
@@ -1263,8 +1264,9 @@ declare function createAuthenticationError({ message }: {
|
|
|
1263
1264
|
* });
|
|
1264
1265
|
* ```
|
|
1265
1266
|
*/
|
|
1266
|
-
declare function createTimeoutError({ message, promptExcerpt, timeoutMs, }: {
|
|
1267
|
+
declare function createTimeoutError({ message, stderr, promptExcerpt, timeoutMs, }: {
|
|
1267
1268
|
message: string;
|
|
1269
|
+
stderr?: string;
|
|
1268
1270
|
promptExcerpt?: string;
|
|
1269
1271
|
timeoutMs?: number;
|
|
1270
1272
|
}): APICallError;
|
package/dist/index.d.ts
CHANGED
|
@@ -1243,8 +1243,9 @@ declare function createAPICallError({ message, code, exitCode, stderr, promptExc
|
|
|
1243
1243
|
* });
|
|
1244
1244
|
* ```
|
|
1245
1245
|
*/
|
|
1246
|
-
declare function createAuthenticationError({ message }: {
|
|
1246
|
+
declare function createAuthenticationError({ message, stderr, }: {
|
|
1247
1247
|
message: string;
|
|
1248
|
+
stderr?: string;
|
|
1248
1249
|
}): LoadAPIKeyError;
|
|
1249
1250
|
/**
|
|
1250
1251
|
* Creates a timeout error for Claude Code SDK operations.
|
|
@@ -1263,8 +1264,9 @@ declare function createAuthenticationError({ message }: {
|
|
|
1263
1264
|
* });
|
|
1264
1265
|
* ```
|
|
1265
1266
|
*/
|
|
1266
|
-
declare function createTimeoutError({ message, promptExcerpt, timeoutMs, }: {
|
|
1267
|
+
declare function createTimeoutError({ message, stderr, promptExcerpt, timeoutMs, }: {
|
|
1267
1268
|
message: string;
|
|
1269
|
+
stderr?: string;
|
|
1268
1270
|
promptExcerpt?: string;
|
|
1269
1271
|
timeoutMs?: number;
|
|
1270
1272
|
}): APICallError;
|
package/dist/index.js
CHANGED
|
@@ -322,6 +322,21 @@ ${segmentText}`;
|
|
|
322
322
|
|
|
323
323
|
// src/errors.ts
|
|
324
324
|
import { APICallError, LoadAPIKeyError } from "@ai-sdk/provider";
|
|
325
|
+
var STDERR_TAIL_MARKER = " | stderr (tail):";
|
|
326
|
+
var ANSI_ESCAPE_SEQUENCE = (
|
|
327
|
+
// eslint-disable-next-line no-control-regex
|
|
328
|
+
/\x1b(?:\][^\x07\x1b]*(?:\x07|\x1b\\)|\[[0-?]*[ -/]*[@-~])/g
|
|
329
|
+
);
|
|
330
|
+
function stderrTail(raw) {
|
|
331
|
+
const withoutAnsi = raw.replace(ANSI_ESCAPE_SEQUENCE, "");
|
|
332
|
+
const withoutControlCharacters = Array.from(withoutAnsi).filter((character) => {
|
|
333
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
334
|
+
return character === "\r" || character === "\n" || codePoint === 8232 || codePoint === 8233 || codePoint >= 32 && codePoint < 127 || codePoint > 159;
|
|
335
|
+
}).join("");
|
|
336
|
+
const tail = withoutControlCharacters.split(/\r\n|\r|\n|\u2028|\u2029/).map((line) => line.trim()).filter((line) => line.length > 0).slice(-5).join("; ");
|
|
337
|
+
const codePoints = Array.from(tail);
|
|
338
|
+
return codePoints.length > 600 ? `\u2026${codePoints.slice(-599).join("")}` : tail;
|
|
339
|
+
}
|
|
325
340
|
function createAPICallError({
|
|
326
341
|
message,
|
|
327
342
|
code,
|
|
@@ -336,26 +351,37 @@ function createAPICallError({
|
|
|
336
351
|
stderr,
|
|
337
352
|
promptExcerpt
|
|
338
353
|
};
|
|
354
|
+
const tail = typeof stderr === "string" && stderr.length > 0 ? stderrTail(stderr) : "";
|
|
355
|
+
const enrichedMessage = tail && !message.includes(STDERR_TAIL_MARKER) ? `${message}${STDERR_TAIL_MARKER} ${tail}` : message;
|
|
339
356
|
return new APICallError({
|
|
340
|
-
message,
|
|
357
|
+
message: enrichedMessage,
|
|
341
358
|
isRetryable,
|
|
342
359
|
url: "claude-code-cli://command",
|
|
343
360
|
requestBodyValues: promptExcerpt ? { prompt: promptExcerpt } : void 0,
|
|
344
361
|
data: metadata
|
|
345
362
|
});
|
|
346
363
|
}
|
|
347
|
-
function createAuthenticationError({
|
|
348
|
-
|
|
364
|
+
function createAuthenticationError({
|
|
365
|
+
message,
|
|
366
|
+
stderr
|
|
367
|
+
}) {
|
|
368
|
+
const error = new LoadAPIKeyError({
|
|
349
369
|
message: message || "Authentication failed. Please ensure Claude Code SDK is properly authenticated."
|
|
350
370
|
});
|
|
371
|
+
if (stderr) {
|
|
372
|
+
error.data = { stderr };
|
|
373
|
+
}
|
|
374
|
+
return error;
|
|
351
375
|
}
|
|
352
376
|
function createTimeoutError({
|
|
353
377
|
message,
|
|
378
|
+
stderr,
|
|
354
379
|
promptExcerpt,
|
|
355
380
|
timeoutMs
|
|
356
381
|
}) {
|
|
357
382
|
const metadata = {
|
|
358
383
|
code: "TIMEOUT",
|
|
384
|
+
stderr,
|
|
359
385
|
promptExcerpt
|
|
360
386
|
};
|
|
361
387
|
return new APICallError({
|
|
@@ -381,6 +407,9 @@ function getErrorMetadata(error) {
|
|
|
381
407
|
if (error instanceof APICallError && error.data) {
|
|
382
408
|
return error.data;
|
|
383
409
|
}
|
|
410
|
+
if (error instanceof LoadAPIKeyError && error.data) {
|
|
411
|
+
return error.data;
|
|
412
|
+
}
|
|
384
413
|
return void 0;
|
|
385
414
|
}
|
|
386
415
|
|
|
@@ -958,10 +987,16 @@ function createVerboseLogger(logger, verbose = false) {
|
|
|
958
987
|
|
|
959
988
|
// src/claude-code-language-model.ts
|
|
960
989
|
import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
961
|
-
var PROVIDER_VERSION = "3.5.
|
|
990
|
+
var PROVIDER_VERSION = "3.5.3";
|
|
962
991
|
var DEFAULT_CLIENT_APP = `ai-sdk-provider-claude-code/${PROVIDER_VERSION}`;
|
|
963
992
|
var CLAUDE_CODE_TRUNCATION_WARNING = "Claude Code SDK output ended unexpectedly; returning truncated response from buffered text. Await upstream fix to avoid data loss.";
|
|
964
993
|
var MIN_TRUNCATION_LENGTH = 512;
|
|
994
|
+
function capStderr(raw) {
|
|
995
|
+
if (raw.length <= 4e3) {
|
|
996
|
+
return raw;
|
|
997
|
+
}
|
|
998
|
+
return Array.from(raw).slice(-4e3).join("");
|
|
999
|
+
}
|
|
965
1000
|
function isClaudeCodeTruncationError(error, bufferedText) {
|
|
966
1001
|
const isSyntaxError = error instanceof SyntaxError || // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
967
1002
|
typeof error?.name === "string" && // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1970,12 +2005,18 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1970
2005
|
return opts;
|
|
1971
2006
|
}
|
|
1972
2007
|
handleClaudeCodeError(error, messagesPrompt, collectedStderr) {
|
|
1973
|
-
if (isAbortError(error)) {
|
|
1974
|
-
throw error;
|
|
1975
|
-
}
|
|
1976
2008
|
if (error instanceof APICallError2 || error instanceof LoadAPIKeyError2) {
|
|
1977
2009
|
return error;
|
|
1978
2010
|
}
|
|
2011
|
+
const rawSdkStderr = typeof error === "object" && error !== null && "stderr" in error && typeof error.stderr === "string" ? error.stderr : void 0;
|
|
2012
|
+
const cappedSdkStderr = rawSdkStderr !== void 0 ? capStderr(rawSdkStderr) : void 0;
|
|
2013
|
+
const selectedStderr = cappedSdkStderr !== void 0 && stderrTail(cappedSdkStderr) ? cappedSdkStderr : collectedStderr ? capStderr(collectedStderr) : void 0;
|
|
2014
|
+
const stderr = selectedStderr || void 0;
|
|
2015
|
+
const tail = stderr ? stderrTail(stderr) : "";
|
|
2016
|
+
const appendStderrTail = (message) => tail && !message.includes(STDERR_TAIL_MARKER) ? `${message}${STDERR_TAIL_MARKER} ${tail}` : message;
|
|
2017
|
+
if (isAbortError(error)) {
|
|
2018
|
+
throw error;
|
|
2019
|
+
}
|
|
1979
2020
|
const isErrorWithMessage = (err) => {
|
|
1980
2021
|
return typeof err === "object" && err !== null && "message" in err;
|
|
1981
2022
|
};
|
|
@@ -1996,26 +2037,43 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
1996
2037
|
"oauth_org_not_allowed"
|
|
1997
2038
|
// SDK 0.3.x assistant error kind: OAuth org not permitted
|
|
1998
2039
|
];
|
|
2040
|
+
const stderrAuthPatterns = [
|
|
2041
|
+
"not logged in",
|
|
2042
|
+
"not authenticated",
|
|
2043
|
+
"auth failed",
|
|
2044
|
+
"please login",
|
|
2045
|
+
"please run /login",
|
|
2046
|
+
"claude login",
|
|
2047
|
+
"claude auth login",
|
|
2048
|
+
"invalid api key",
|
|
2049
|
+
"oauth token revoked",
|
|
2050
|
+
"oauth_org_not_allowed"
|
|
2051
|
+
];
|
|
1999
2052
|
const errorMessage = isErrorWithMessage(error) && error.message ? error.message.toLowerCase() : "";
|
|
2053
|
+
const stderrMessage = stderr?.toLowerCase() ?? "";
|
|
2000
2054
|
const errorKind = getStructuredErrorKind(error);
|
|
2001
2055
|
const exitCode = isErrorWithCode(error) && typeof error.exitCode === "number" ? error.exitCode : void 0;
|
|
2002
|
-
const isAuthError = errorKind === "authentication_failed" || errorKind === "oauth_org_not_allowed" || authErrorPatterns.some((pattern) => errorMessage.includes(pattern)) || exitCode === 401;
|
|
2056
|
+
const isAuthError = errorKind === "authentication_failed" || errorKind === "oauth_org_not_allowed" || authErrorPatterns.some((pattern) => errorMessage.includes(pattern)) || stderrAuthPatterns.some((pattern) => stderrMessage.includes(pattern)) || exitCode === 401;
|
|
2003
2057
|
if (isAuthError) {
|
|
2004
2058
|
return createAuthenticationError({
|
|
2005
|
-
message:
|
|
2059
|
+
message: appendStderrTail(
|
|
2060
|
+
isErrorWithMessage(error) && error.message ? error.message : "Authentication failed. Please ensure Claude Code SDK is properly authenticated."
|
|
2061
|
+
),
|
|
2062
|
+
stderr
|
|
2006
2063
|
});
|
|
2007
2064
|
}
|
|
2008
2065
|
const errorCode = isErrorWithCode(error) && typeof error.code === "string" ? error.code : "";
|
|
2009
|
-
if (errorCode === "ETIMEDOUT" || errorMessage.includes("timeout")) {
|
|
2066
|
+
if (errorCode === "ETIMEDOUT" || errorMessage.includes("timeout") || /request timed out|etimedout/i.test(stderr ?? "")) {
|
|
2010
2067
|
return createTimeoutError({
|
|
2011
|
-
message:
|
|
2068
|
+
message: appendStderrTail(
|
|
2069
|
+
isErrorWithMessage(error) && error.message ? error.message : "Request timed out"
|
|
2070
|
+
),
|
|
2071
|
+
stderr,
|
|
2012
2072
|
promptExcerpt: messagesPrompt.substring(0, 200)
|
|
2013
2073
|
// Don't specify timeoutMs since we don't know the actual timeout value
|
|
2014
2074
|
// It's controlled by the consumer via AbortSignal
|
|
2015
2075
|
});
|
|
2016
2076
|
}
|
|
2017
|
-
const stderrFromError = isErrorWithCode(error) && typeof error.stderr === "string" ? error.stderr : void 0;
|
|
2018
|
-
const stderr = stderrFromError || collectedStderr || void 0;
|
|
2019
2077
|
if (errorKind === "overloaded" || errorKind === "rate_limit" || errorMessage.includes("overloaded")) {
|
|
2020
2078
|
return createAPICallError({
|
|
2021
2079
|
message: isErrorWithMessage(error) && error.message ? error.message : "Anthropic API is overloaded. Please retry.",
|
|
@@ -2239,6 +2297,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
2239
2297
|
let collectedStderr = "";
|
|
2240
2298
|
const stderrCollector = (data) => {
|
|
2241
2299
|
collectedStderr += data;
|
|
2300
|
+
collectedStderr = capStderr(collectedStderr);
|
|
2242
2301
|
};
|
|
2243
2302
|
const sdkOptions = this.getSanitizedSdkOptions();
|
|
2244
2303
|
const effectiveResume = this.getEffectiveResume(sdkOptions);
|
|
@@ -2717,6 +2776,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
|
|
|
2717
2776
|
let collectedStderr = "";
|
|
2718
2777
|
const stderrCollector = (data) => {
|
|
2719
2778
|
collectedStderr += data;
|
|
2779
|
+
collectedStderr = capStderr(collectedStderr);
|
|
2720
2780
|
};
|
|
2721
2781
|
const sdkOptions = this.getSanitizedSdkOptions();
|
|
2722
2782
|
const effectiveResume = this.getEffectiveResume(sdkOptions);
|