ai-sdk-provider-claude-code 2.2.1 → 2.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.cjs CHANGED
@@ -1189,6 +1189,9 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1189
1189
  options.abortSignal.addEventListener("abort", abortListener, { once: true });
1190
1190
  }
1191
1191
  const queryOptions = this.createQueryOptions(abortController, options.responseFormat);
1192
+ if (queryOptions.includePartialMessages === void 0) {
1193
+ queryOptions.includePartialMessages = true;
1194
+ }
1192
1195
  const warnings = this.generateAllWarnings(
1193
1196
  options,
1194
1197
  messagesPrompt
@@ -1260,6 +1263,8 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1260
1263
  let usage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
1261
1264
  let accumulatedText = "";
1262
1265
  let textPartId;
1266
+ let streamedTextLength = 0;
1267
+ let hasReceivedStreamEvents = false;
1263
1268
  try {
1264
1269
  controller.enqueue({ type: "stream-start", warnings });
1265
1270
  if (this.settings.canUseTool && this.settings.permissionPromptToolName) {
@@ -1282,6 +1287,54 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1282
1287
  });
1283
1288
  for await (const message of response) {
1284
1289
  this.logger.debug(`[claude-code] Stream received message type: ${message.type}`);
1290
+ if (message.type === "stream_event") {
1291
+ const streamEvent = message;
1292
+ const event = streamEvent.event;
1293
+ if (event.type === "content_block_delta" && event.delta.type === "text_delta" && "text" in event.delta && event.delta.text) {
1294
+ const deltaText = event.delta.text;
1295
+ hasReceivedStreamEvents = true;
1296
+ if (options.responseFormat?.type === "json") {
1297
+ accumulatedText += deltaText;
1298
+ streamedTextLength += deltaText.length;
1299
+ continue;
1300
+ }
1301
+ if (!textPartId) {
1302
+ textPartId = (0, import_provider_utils.generateId)();
1303
+ controller.enqueue({
1304
+ type: "text-start",
1305
+ id: textPartId
1306
+ });
1307
+ }
1308
+ controller.enqueue({
1309
+ type: "text-delta",
1310
+ id: textPartId,
1311
+ delta: deltaText
1312
+ });
1313
+ accumulatedText += deltaText;
1314
+ streamedTextLength += deltaText.length;
1315
+ }
1316
+ if (event.type === "content_block_delta" && event.delta.type === "input_json_delta" && "partial_json" in event.delta && event.delta.partial_json) {
1317
+ const jsonDelta = event.delta.partial_json;
1318
+ hasReceivedStreamEvents = true;
1319
+ if (options.responseFormat?.type === "json") {
1320
+ if (!textPartId) {
1321
+ textPartId = (0, import_provider_utils.generateId)();
1322
+ controller.enqueue({
1323
+ type: "text-start",
1324
+ id: textPartId
1325
+ });
1326
+ }
1327
+ controller.enqueue({
1328
+ type: "text-delta",
1329
+ id: textPartId,
1330
+ delta: jsonDelta
1331
+ });
1332
+ accumulatedText += jsonDelta;
1333
+ streamedTextLength += jsonDelta.length;
1334
+ }
1335
+ }
1336
+ continue;
1337
+ }
1285
1338
  if (message.type === "assistant") {
1286
1339
  if (!message.message?.content) {
1287
1340
  this.logger.warn(
@@ -1344,20 +1397,41 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1344
1397
  }
1345
1398
  const text = content.map((c) => c.type === "text" ? c.text : "").join("");
1346
1399
  if (text) {
1347
- accumulatedText += text;
1348
- if (options.responseFormat?.type !== "json") {
1349
- if (!textPartId) {
1350
- textPartId = (0, import_provider_utils.generateId)();
1400
+ if (hasReceivedStreamEvents) {
1401
+ const newTextStart = streamedTextLength;
1402
+ const deltaText = text.length > newTextStart ? text.slice(newTextStart) : "";
1403
+ accumulatedText = text;
1404
+ if (options.responseFormat?.type !== "json" && deltaText) {
1405
+ if (!textPartId) {
1406
+ textPartId = (0, import_provider_utils.generateId)();
1407
+ controller.enqueue({
1408
+ type: "text-start",
1409
+ id: textPartId
1410
+ });
1411
+ }
1351
1412
  controller.enqueue({
1352
- type: "text-start",
1353
- id: textPartId
1413
+ type: "text-delta",
1414
+ id: textPartId,
1415
+ delta: deltaText
1416
+ });
1417
+ }
1418
+ streamedTextLength = text.length;
1419
+ } else {
1420
+ accumulatedText += text;
1421
+ if (options.responseFormat?.type !== "json") {
1422
+ if (!textPartId) {
1423
+ textPartId = (0, import_provider_utils.generateId)();
1424
+ controller.enqueue({
1425
+ type: "text-start",
1426
+ id: textPartId
1427
+ });
1428
+ }
1429
+ controller.enqueue({
1430
+ type: "text-delta",
1431
+ id: textPartId,
1432
+ delta: text
1354
1433
  });
1355
1434
  }
1356
- controller.enqueue({
1357
- type: "text-delta",
1358
- id: textPartId,
1359
- delta: text
1360
- });
1361
1435
  }
1362
1436
  }
1363
1437
  } else if (message.type === "user") {
@@ -1502,7 +1576,13 @@ var ClaudeCodeLanguageModel = class _ClaudeCodeLanguageModel {
1502
1576
  this.logger.debug(`[claude-code] Stream finish reason: ${finishReason}`);
1503
1577
  this.setSessionId(message.session_id);
1504
1578
  const structuredOutput = "structured_output" in message ? message.structured_output : void 0;
1505
- if (structuredOutput !== void 0) {
1579
+ const alreadyStreamedJson = textPartId && options.responseFormat?.type === "json" && hasReceivedStreamEvents;
1580
+ if (alreadyStreamedJson && textPartId) {
1581
+ controller.enqueue({
1582
+ type: "text-end",
1583
+ id: textPartId
1584
+ });
1585
+ } else if (structuredOutput !== void 0) {
1506
1586
  const jsonTextId = (0, import_provider_utils.generateId)();
1507
1587
  const jsonText = JSON.stringify(structuredOutput);
1508
1588
  controller.enqueue({