ai-sdk-provider-claude-code 3.0.0-beta.1 → 3.0.1

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.d.cts CHANGED
@@ -134,6 +134,9 @@ interface ClaudeCodeSettings {
134
134
  /**
135
135
  * Filesystem settings sources to load (CLAUDE.md, settings.json, etc.)
136
136
  * When omitted, the Agent SDK loads no filesystem settings.
137
+ *
138
+ * Required for Skills support - skills are loaded from these sources.
139
+ * @example ['user', 'project']
137
140
  */
138
141
  settingSources?: Array<'user' | 'project' | 'local'>;
139
142
  /**
package/dist/index.d.ts CHANGED
@@ -134,6 +134,9 @@ interface ClaudeCodeSettings {
134
134
  /**
135
135
  * Filesystem settings sources to load (CLAUDE.md, settings.json, etc.)
136
136
  * When omitted, the Agent SDK loads no filesystem settings.
137
+ *
138
+ * Required for Skills support - skills are loaded from these sources.
139
+ * @example ['user', 'project']
137
140
  */
138
141
  settingSources?: Array<'user' | 'project' | 'local'>;
139
142
  /**
package/dist/index.js CHANGED
@@ -370,13 +370,15 @@ function getErrorMetadata(error) {
370
370
  function mapClaudeCodeFinishReason(subtype) {
371
371
  switch (subtype) {
372
372
  case "success":
373
- return "stop";
373
+ return { unified: "stop", raw: subtype };
374
374
  case "error_max_turns":
375
- return "length";
375
+ return { unified: "length", raw: subtype };
376
376
  case "error_during_execution":
377
- return "error";
377
+ return { unified: "error", raw: subtype };
378
+ case void 0:
379
+ return { unified: "stop", raw: void 0 };
378
380
  default:
379
- return "stop";
381
+ return { unified: "other", raw: subtype };
380
382
  }
381
383
  }
382
384
 
@@ -548,6 +550,11 @@ function validateSettings(settings) {
548
550
  if (validSettings.disallowedTools) {
549
551
  validateToolNames(validSettings.disallowedTools, "disallowed");
550
552
  }
553
+ if (validSettings.allowedTools?.includes("Skill") && !validSettings.settingSources) {
554
+ warnings.push(
555
+ "allowedTools includes 'Skill' but settingSources is not set. Skills require settingSources (e.g., ['user', 'project']) to load skill definitions."
556
+ );
557
+ }
551
558
  return { valid: true, warnings, errors };
552
559
  } catch (error) {
553
560
  errors.push(`Validation error: ${error instanceof Error ? error.message : String(error)}`);
@@ -652,6 +659,42 @@ function isAbortError(err) {
652
659
  return false;
653
660
  }
654
661
  var STREAMING_FEATURE_WARNING = "Claude Agent SDK features (hooks/MCP/images) require streaming input. Set `streamingInput: 'always'` or provide `canUseTool` (auto streams only when canUseTool is set).";
662
+ function createEmptyUsage() {
663
+ return {
664
+ inputTokens: {
665
+ total: 0,
666
+ noCache: 0,
667
+ cacheRead: 0,
668
+ cacheWrite: 0
669
+ },
670
+ outputTokens: {
671
+ total: 0,
672
+ text: void 0,
673
+ reasoning: void 0
674
+ },
675
+ raw: void 0
676
+ };
677
+ }
678
+ function convertClaudeCodeUsage(usage) {
679
+ const inputTokens = usage.input_tokens ?? 0;
680
+ const outputTokens = usage.output_tokens ?? 0;
681
+ const cacheWrite = usage.cache_creation_input_tokens ?? 0;
682
+ const cacheRead = usage.cache_read_input_tokens ?? 0;
683
+ return {
684
+ inputTokens: {
685
+ total: inputTokens + cacheWrite + cacheRead,
686
+ noCache: inputTokens,
687
+ cacheRead,
688
+ cacheWrite
689
+ },
690
+ outputTokens: {
691
+ total: outputTokens,
692
+ text: void 0,
693
+ reasoning: void 0
694
+ },
695
+ raw: usage
696
+ };
697
+ }
655
698
  function toAsyncIterablePrompt(messagesPrompt, outputStreamEnded, sessionId, contentParts) {
656
699
  const content = contentParts && contentParts.length > 0 ? contentParts : [{ type: "text", text: messagesPrompt }];
657
700
  const msg = {
@@ -1005,16 +1048,12 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1005
1048
  const queryOptions = this.createQueryOptions(abortController, options.responseFormat);
1006
1049
  let text = "";
1007
1050
  let structuredOutput;
1008
- let usage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
1009
- let finishReason = "stop";
1051
+ let usage = createEmptyUsage();
1052
+ let finishReason = { unified: "stop", raw: void 0 };
1010
1053
  let wasTruncated = false;
1011
1054
  let costUsd;
1012
1055
  let durationMs;
1013
- let rawUsage;
1014
- const warnings = this.generateAllWarnings(
1015
- options,
1016
- messagesPrompt
1017
- );
1056
+ const warnings = this.generateAllWarnings(options, messagesPrompt);
1018
1057
  if (messageWarnings) {
1019
1058
  messageWarnings.forEach((warning) => {
1020
1059
  warnings.push({
@@ -1077,18 +1116,13 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1077
1116
  `[claude-code] Request completed - Session: ${message.session_id}, Cost: $${costUsd?.toFixed(4) ?? "N/A"}, Duration: ${durationMs ?? "N/A"}ms`
1078
1117
  );
1079
1118
  if ("usage" in message) {
1080
- rawUsage = message.usage;
1081
- usage = {
1082
- inputTokens: (message.usage.cache_creation_input_tokens ?? 0) + (message.usage.cache_read_input_tokens ?? 0) + (message.usage.input_tokens ?? 0),
1083
- outputTokens: message.usage.output_tokens ?? 0,
1084
- totalTokens: (message.usage.cache_creation_input_tokens ?? 0) + (message.usage.cache_read_input_tokens ?? 0) + (message.usage.input_tokens ?? 0) + (message.usage.output_tokens ?? 0)
1085
- };
1119
+ usage = convertClaudeCodeUsage(message.usage);
1086
1120
  this.logger.debug(
1087
- `[claude-code] Token usage - Input: ${usage.inputTokens}, Output: ${usage.outputTokens}, Total: ${usage.totalTokens}`
1121
+ `[claude-code] Token usage - Input: ${usage.inputTokens.total}, Output: ${usage.outputTokens.total}`
1088
1122
  );
1089
1123
  }
1090
1124
  finishReason = mapClaudeCodeFinishReason(message.subtype);
1091
- this.logger.debug(`[claude-code] Finish reason: ${finishReason}`);
1125
+ this.logger.debug(`[claude-code] Finish reason: ${finishReason.unified}`);
1092
1126
  } else if (message.type === "system" && message.subtype === "init") {
1093
1127
  this.setSessionId(message.session_id);
1094
1128
  this.logger.info(`[claude-code] Session initialized: ${message.session_id}`);
@@ -1108,7 +1142,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1108
1142
  `[claude-code] Detected truncated response, returning ${text.length} characters of buffered text`
1109
1143
  );
1110
1144
  wasTruncated = true;
1111
- finishReason = "length";
1145
+ finishReason = { unified: "length", raw: "truncation" };
1112
1146
  warnings.push({
1113
1147
  type: "other",
1114
1148
  message: CLAUDE_CODE_TRUNCATION_WARNING
@@ -1140,7 +1174,6 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1140
1174
  ...this.sessionId !== void 0 && { sessionId: this.sessionId },
1141
1175
  ...costUsd !== void 0 && { costUsd },
1142
1176
  ...durationMs !== void 0 && { durationMs },
1143
- ...rawUsage !== void 0 && { rawUsage },
1144
1177
  ...wasTruncated && { truncated: true }
1145
1178
  }
1146
1179
  }
@@ -1170,10 +1203,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1170
1203
  if (queryOptions.includePartialMessages === void 0) {
1171
1204
  queryOptions.includePartialMessages = true;
1172
1205
  }
1173
- const warnings = this.generateAllWarnings(
1174
- options,
1175
- messagesPrompt
1176
- );
1206
+ const warnings = this.generateAllWarnings(options, messagesPrompt);
1177
1207
  if (messageWarnings) {
1178
1208
  messageWarnings.forEach((warning) => {
1179
1209
  warnings.push({
@@ -1238,7 +1268,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1238
1268
  }
1239
1269
  toolStates.clear();
1240
1270
  };
1241
- let usage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
1271
+ let usage = createEmptyUsage();
1242
1272
  let accumulatedText = "";
1243
1273
  let textPartId;
1244
1274
  let streamedTextLength = 0;
@@ -1536,22 +1566,16 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1536
1566
  this.logger.info(
1537
1567
  `[claude-code] Stream completed - Session: ${message.session_id}, Cost: $${message.total_cost_usd?.toFixed(4) ?? "N/A"}, Duration: ${message.duration_ms ?? "N/A"}ms`
1538
1568
  );
1539
- let rawUsage;
1540
1569
  if ("usage" in message) {
1541
- rawUsage = message.usage;
1542
- usage = {
1543
- inputTokens: (message.usage.cache_creation_input_tokens ?? 0) + (message.usage.cache_read_input_tokens ?? 0) + (message.usage.input_tokens ?? 0),
1544
- outputTokens: message.usage.output_tokens ?? 0,
1545
- totalTokens: (message.usage.cache_creation_input_tokens ?? 0) + (message.usage.cache_read_input_tokens ?? 0) + (message.usage.input_tokens ?? 0) + (message.usage.output_tokens ?? 0)
1546
- };
1570
+ usage = convertClaudeCodeUsage(message.usage);
1547
1571
  this.logger.debug(
1548
- `[claude-code] Stream token usage - Input: ${usage.inputTokens}, Output: ${usage.outputTokens}, Total: ${usage.totalTokens}`
1572
+ `[claude-code] Stream token usage - Input: ${usage.inputTokens.total}, Output: ${usage.outputTokens.total}`
1549
1573
  );
1550
1574
  }
1551
1575
  const finishReason = mapClaudeCodeFinishReason(
1552
1576
  message.subtype
1553
1577
  );
1554
- this.logger.debug(`[claude-code] Stream finish reason: ${finishReason}`);
1578
+ this.logger.debug(`[claude-code] Stream finish reason: ${finishReason.unified}`);
1555
1579
  this.setSessionId(message.session_id);
1556
1580
  const structuredOutput = "structured_output" in message ? message.structured_output : void 0;
1557
1581
  const alreadyStreamedJson = textPartId && options.responseFormat?.type === "json" && hasReceivedStreamEvents;
@@ -1610,7 +1634,6 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1610
1634
  costUsd: message.total_cost_usd
1611
1635
  },
1612
1636
  ...message.duration_ms !== void 0 && { durationMs: message.duration_ms },
1613
- ...rawUsage !== void 0 && { rawUsage },
1614
1637
  // JSON validation warnings are collected during streaming and included
1615
1638
  // in providerMetadata since the AI SDK's finish event doesn't support
1616
1639
  // a top-level warnings field (unlike stream-start which was already emitted)
@@ -1673,7 +1696,7 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1673
1696
  const warningsJson = this.serializeWarningsForMetadata(streamWarnings);
1674
1697
  controller.enqueue({
1675
1698
  type: "finish",
1676
- finishReason: "length",
1699
+ finishReason: { unified: "length", raw: "truncation" },
1677
1700
  usage,
1678
1701
  providerMetadata: {
1679
1702
  "claude-code": {