@tanstack/ai-anthropic 0.2.0 → 0.3.0

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.
@@ -52,20 +52,19 @@ class AnthropicSummarizeAdapter extends BaseSummarizeAdapter {
52
52
  const delta = event.delta.text;
53
53
  accumulatedContent += delta;
54
54
  yield {
55
- type: "content",
56
- id,
55
+ type: "TEXT_MESSAGE_CONTENT",
56
+ messageId: id,
57
57
  model,
58
58
  timestamp: Date.now(),
59
59
  delta,
60
- content: accumulatedContent,
61
- role: "assistant"
60
+ content: accumulatedContent
62
61
  };
63
62
  }
64
63
  } else if (event.type === "message_delta") {
65
64
  outputTokens = event.usage.output_tokens;
66
65
  yield {
67
- type: "done",
68
- id,
66
+ type: "RUN_FINISHED",
67
+ runId: id,
69
68
  model,
70
69
  timestamp: Date.now(),
71
70
  finishReason: event.delta.stop_reason,
@@ -1 +1 @@
1
- {"version":3,"file":"summarize.js","sources":["../../../src/adapters/summarize.ts"],"sourcesContent":["import { BaseSummarizeAdapter } from '@tanstack/ai/adapters'\nimport {\n createAnthropicClient,\n generateId,\n getAnthropicApiKeyFromEnv,\n} from '../utils'\nimport type { ANTHROPIC_MODELS } from '../model-meta'\nimport type {\n StreamChunk,\n SummarizationOptions,\n SummarizationResult,\n} from '@tanstack/ai'\nimport type { AnthropicClientConfig } from '../utils'\n\n/**\n * Configuration for Anthropic summarize adapter\n */\nexport interface AnthropicSummarizeConfig extends AnthropicClientConfig {}\n\n/**\n * Anthropic-specific provider options for summarization\n */\nexport interface AnthropicSummarizeProviderOptions {\n /** Temperature for response generation (0-1) */\n temperature?: number\n /** Maximum tokens in the response */\n maxTokens?: number\n}\n\n/** Model type for Anthropic summarization */\nexport type AnthropicSummarizeModel = (typeof ANTHROPIC_MODELS)[number]\n\n/**\n * Anthropic Summarize Adapter\n *\n * Tree-shakeable adapter for Anthropic summarization functionality.\n * Import only what you need for smaller bundle sizes.\n */\nexport class AnthropicSummarizeAdapter<\n TModel extends AnthropicSummarizeModel,\n> extends BaseSummarizeAdapter<TModel, AnthropicSummarizeProviderOptions> {\n readonly kind = 'summarize' as const\n readonly name = 'anthropic' as const\n\n private client: ReturnType<typeof createAnthropicClient>\n\n constructor(config: AnthropicSummarizeConfig, model: TModel) {\n super({}, model)\n this.client = createAnthropicClient(config)\n }\n\n async summarize(options: SummarizationOptions): Promise<SummarizationResult> {\n const systemPrompt = this.buildSummarizationPrompt(options)\n\n const response = await this.client.messages.create({\n model: options.model,\n messages: [{ role: 'user', content: options.text }],\n system: systemPrompt,\n max_tokens: options.maxLength || 500,\n temperature: 0.3,\n stream: false,\n })\n\n const content = response.content\n .map((c) => (c.type === 'text' ? c.text : ''))\n .join('')\n\n return {\n id: response.id,\n model: response.model,\n summary: content,\n usage: {\n promptTokens: response.usage.input_tokens,\n completionTokens: response.usage.output_tokens,\n totalTokens: response.usage.input_tokens + response.usage.output_tokens,\n },\n }\n }\n\n async *summarizeStream(\n options: SummarizationOptions,\n ): AsyncIterable<StreamChunk> {\n const systemPrompt = this.buildSummarizationPrompt(options)\n const id = generateId(this.name)\n const model = options.model\n let accumulatedContent = ''\n let inputTokens = 0\n let outputTokens = 0\n\n const stream = await this.client.messages.create({\n model: options.model,\n messages: [{ role: 'user', content: options.text }],\n system: systemPrompt,\n max_tokens: options.maxLength || 500,\n temperature: 0.3,\n stream: true,\n })\n\n for await (const event of stream) {\n if (event.type === 'message_start') {\n inputTokens = event.message.usage.input_tokens\n } else if (event.type === 'content_block_delta') {\n if (event.delta.type === 'text_delta') {\n const delta = event.delta.text\n accumulatedContent += delta\n yield {\n type: 'content',\n id,\n model,\n timestamp: Date.now(),\n delta,\n content: accumulatedContent,\n role: 'assistant',\n }\n }\n } else if (event.type === 'message_delta') {\n outputTokens = event.usage.output_tokens\n yield {\n type: 'done',\n id,\n model,\n timestamp: Date.now(),\n finishReason: event.delta.stop_reason as\n | 'stop'\n | 'length'\n | 'content_filter'\n | null,\n usage: {\n promptTokens: inputTokens,\n completionTokens: outputTokens,\n totalTokens: inputTokens + outputTokens,\n },\n }\n }\n }\n }\n\n private buildSummarizationPrompt(options: SummarizationOptions): string {\n let prompt = 'You are a professional summarizer. '\n\n switch (options.style) {\n case 'bullet-points':\n prompt += 'Provide a summary in bullet point format. '\n break\n case 'paragraph':\n prompt += 'Provide a summary in paragraph format. '\n break\n case 'concise':\n prompt += 'Provide a very concise summary in 1-2 sentences. '\n break\n default:\n prompt += 'Provide a clear and concise summary. '\n }\n\n if (options.focus && options.focus.length > 0) {\n prompt += `Focus on the following aspects: ${options.focus.join(', ')}. `\n }\n\n if (options.maxLength) {\n prompt += `Keep the summary under ${options.maxLength} tokens. `\n }\n\n return prompt\n }\n}\n\n/**\n * Creates an Anthropic summarize adapter with explicit API key.\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'claude-sonnet-4-5', 'claude-3-5-haiku-latest')\n * @param apiKey - Your Anthropic API key\n * @param config - Optional additional configuration\n * @returns Configured Anthropic summarize adapter instance with resolved types\n */\nexport function createAnthropicSummarize<\n TModel extends AnthropicSummarizeModel,\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<AnthropicSummarizeConfig, 'apiKey'>,\n): AnthropicSummarizeAdapter<TModel> {\n return new AnthropicSummarizeAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates an Anthropic summarize adapter with automatic API key detection.\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'claude-sonnet-4-5', 'claude-3-5-haiku-latest')\n * @param config - Optional configuration (excluding apiKey which is auto-detected)\n * @returns Configured Anthropic summarize adapter instance with resolved types\n */\nexport function anthropicSummarize<TModel extends AnthropicSummarizeModel>(\n model: TModel,\n config?: Omit<AnthropicSummarizeConfig, 'apiKey'>,\n): AnthropicSummarizeAdapter<TModel> {\n const apiKey = getAnthropicApiKeyFromEnv()\n return createAnthropicSummarize(model, apiKey, config)\n}\n"],"names":[],"mappings":";;AAsCO,MAAM,kCAEH,qBAAgE;AA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ACmC;AACnC,QAAM,SAAS,0BAAA;AACf,SAAO,yBAAyB,OAAO,QAAQ,MAAM;AACvD;"}
1
+ {"version":3,"file":"summarize.js","sources":["../../../src/adapters/summarize.ts"],"sourcesContent":["import { BaseSummarizeAdapter } from '@tanstack/ai/adapters'\nimport {\n createAnthropicClient,\n generateId,\n getAnthropicApiKeyFromEnv,\n} from '../utils'\nimport type { ANTHROPIC_MODELS } from '../model-meta'\nimport type {\n StreamChunk,\n SummarizationOptions,\n SummarizationResult,\n} from '@tanstack/ai'\nimport type { AnthropicClientConfig } from '../utils'\n\n/**\n * Configuration for Anthropic summarize adapter\n */\nexport interface AnthropicSummarizeConfig extends AnthropicClientConfig {}\n\n/**\n * Anthropic-specific provider options for summarization\n */\nexport interface AnthropicSummarizeProviderOptions {\n /** Temperature for response generation (0-1) */\n temperature?: number\n /** Maximum tokens in the response */\n maxTokens?: number\n}\n\n/** Model type for Anthropic summarization */\nexport type AnthropicSummarizeModel = (typeof ANTHROPIC_MODELS)[number]\n\n/**\n * Anthropic Summarize Adapter\n *\n * Tree-shakeable adapter for Anthropic summarization functionality.\n * Import only what you need for smaller bundle sizes.\n */\nexport class AnthropicSummarizeAdapter<\n TModel extends AnthropicSummarizeModel,\n> extends BaseSummarizeAdapter<TModel, AnthropicSummarizeProviderOptions> {\n readonly kind = 'summarize' as const\n readonly name = 'anthropic' as const\n\n private client: ReturnType<typeof createAnthropicClient>\n\n constructor(config: AnthropicSummarizeConfig, model: TModel) {\n super({}, model)\n this.client = createAnthropicClient(config)\n }\n\n async summarize(options: SummarizationOptions): Promise<SummarizationResult> {\n const systemPrompt = this.buildSummarizationPrompt(options)\n\n const response = await this.client.messages.create({\n model: options.model,\n messages: [{ role: 'user', content: options.text }],\n system: systemPrompt,\n max_tokens: options.maxLength || 500,\n temperature: 0.3,\n stream: false,\n })\n\n const content = response.content\n .map((c) => (c.type === 'text' ? c.text : ''))\n .join('')\n\n return {\n id: response.id,\n model: response.model,\n summary: content,\n usage: {\n promptTokens: response.usage.input_tokens,\n completionTokens: response.usage.output_tokens,\n totalTokens: response.usage.input_tokens + response.usage.output_tokens,\n },\n }\n }\n\n async *summarizeStream(\n options: SummarizationOptions,\n ): AsyncIterable<StreamChunk> {\n const systemPrompt = this.buildSummarizationPrompt(options)\n const id = generateId(this.name)\n const model = options.model\n let accumulatedContent = ''\n let inputTokens = 0\n let outputTokens = 0\n\n const stream = await this.client.messages.create({\n model: options.model,\n messages: [{ role: 'user', content: options.text }],\n system: systemPrompt,\n max_tokens: options.maxLength || 500,\n temperature: 0.3,\n stream: true,\n })\n\n for await (const event of stream) {\n if (event.type === 'message_start') {\n inputTokens = event.message.usage.input_tokens\n } else if (event.type === 'content_block_delta') {\n if (event.delta.type === 'text_delta') {\n const delta = event.delta.text\n accumulatedContent += delta\n yield {\n type: 'TEXT_MESSAGE_CONTENT',\n messageId: id,\n model,\n timestamp: Date.now(),\n delta,\n content: accumulatedContent,\n }\n }\n } else if (event.type === 'message_delta') {\n outputTokens = event.usage.output_tokens\n yield {\n type: 'RUN_FINISHED',\n runId: id,\n model,\n timestamp: Date.now(),\n finishReason: event.delta.stop_reason as\n | 'stop'\n | 'length'\n | 'content_filter'\n | null,\n usage: {\n promptTokens: inputTokens,\n completionTokens: outputTokens,\n totalTokens: inputTokens + outputTokens,\n },\n }\n }\n }\n }\n\n private buildSummarizationPrompt(options: SummarizationOptions): string {\n let prompt = 'You are a professional summarizer. '\n\n switch (options.style) {\n case 'bullet-points':\n prompt += 'Provide a summary in bullet point format. '\n break\n case 'paragraph':\n prompt += 'Provide a summary in paragraph format. '\n break\n case 'concise':\n prompt += 'Provide a very concise summary in 1-2 sentences. '\n break\n default:\n prompt += 'Provide a clear and concise summary. '\n }\n\n if (options.focus && options.focus.length > 0) {\n prompt += `Focus on the following aspects: ${options.focus.join(', ')}. `\n }\n\n if (options.maxLength) {\n prompt += `Keep the summary under ${options.maxLength} tokens. `\n }\n\n return prompt\n }\n}\n\n/**\n * Creates an Anthropic summarize adapter with explicit API key.\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'claude-sonnet-4-5', 'claude-3-5-haiku-latest')\n * @param apiKey - Your Anthropic API key\n * @param config - Optional additional configuration\n * @returns Configured Anthropic summarize adapter instance with resolved types\n */\nexport function createAnthropicSummarize<\n TModel extends AnthropicSummarizeModel,\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<AnthropicSummarizeConfig, 'apiKey'>,\n): AnthropicSummarizeAdapter<TModel> {\n return new AnthropicSummarizeAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates an Anthropic summarize adapter with automatic API key detection.\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'claude-sonnet-4-5', 'claude-3-5-haiku-latest')\n * @param config - Optional configuration (excluding apiKey which is auto-detected)\n * @returns Configured Anthropic summarize adapter instance with resolved types\n */\nexport function anthropicSummarize<TModel extends AnthropicSummarizeModel>(\n model: TModel,\n config?: Omit<AnthropicSummarizeConfig, 'apiKey'>,\n): AnthropicSummarizeAdapter<TModel> {\n const apiKey = getAnthropicApiKeyFromEnv()\n return createAnthropicSummarize(model, apiKey, config)\n}\n"],"names":[],"mappings":";;AAsCO,MAAM,kCAEH,qBAAgE;AAAA,EAMxE,YAAY,QAAkC,OAAe;AAC3D,UAAM,CAAA,GAAI,KAAK;AANjB,SAAS,OAAO;AAChB,SAAS,OAAO;AAMd,SAAK,SAAS,sBAAsB,MAAM;AAAA,EAC5C;AAAA,EAEA,MAAM,UAAU,SAA6D;AAC3E,UAAM,eAAe,KAAK,yBAAyB,OAAO;AAE1D,UAAM,WAAW,MAAM,KAAK,OAAO,SAAS,OAAO;AAAA,MACjD,OAAO,QAAQ;AAAA,MACf,UAAU,CAAC,EAAE,MAAM,QAAQ,SAAS,QAAQ,MAAM;AAAA,MAClD,QAAQ;AAAA,MACR,YAAY,QAAQ,aAAa;AAAA,MACjC,aAAa;AAAA,MACb,QAAQ;AAAA,IAAA,CACT;AAED,UAAM,UAAU,SAAS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AAAA,UACA,WAAW,KAAK,IAAA;AAAA,UAChB,cAAc,MAAM,MAAM;AAAA,UAK1B,OAAO;AAAA,YACL,cAAc;AAAA,YACd,kBAAkB;AAAA,YAClB,aAAa,cAAc;AAAA,UAAA;AAAA,QAC7B;AAAA,MAEJ;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,yBAAyB,SAAuC;AACtE,QAAI,SAAS;AAEb,YAAQ,QAAQ,OAAA;AAAA,MACd,KAAK;AACH,kBAAU;AACV;AAAA,MACF,KAAK;AACH,kBAAU;AACV;AAAA,MACF,KAAK;AACH,kBAAU;AACV;AAAA,MACF;AACE,kBAAU;AAAA,IAAA;AAGd,QAAI,QAAQ,SAAS,QAAQ,MAAM,SAAS,GAAG;AAC7C,gBAAU,mCAAmC,QAAQ,MAAM,KAAK,IAAI,CAAC;AAAA,IACvE;AAEA,QAAI,QAAQ,WAAW;AACrB,gBAAU,0BAA0B,QAAQ,SAAS;AAAA,IACvD;AAEA,WAAO;AAAA,EACT;AACF;AAWO,SAAS,yBAGd,OACA,QACA,QACmC;AACnC,SAAO,IAAI,0BAA0B,EAAE,QAAQ,GAAG,OAAA,GAAU,KAAK;AACnE;AAUO,SAAS,mBACd,OACA,QACmC;AACnC,QAAM,SAAS,0BAAA;AACf,SAAO,yBAAyB,OAAO,QAAQ,MAAM;AACvD;"}
@@ -27,8 +27,7 @@ class AnthropicTextAdapter extends BaseTextAdapter {
27
27
  } catch (error) {
28
28
  const err = error;
29
29
  yield {
30
- type: "error",
31
- id: generateId(this.name),
30
+ type: "RUN_ERROR",
32
31
  model: options.model,
33
32
  timestamp: Date.now(),
34
33
  error: {
@@ -279,38 +278,70 @@ class AnthropicTextAdapter extends BaseTextAdapter {
279
278
  const timestamp = Date.now();
280
279
  const toolCallsMap = /* @__PURE__ */ new Map();
281
280
  let currentToolIndex = -1;
281
+ const runId = genId();
282
+ const messageId = genId();
283
+ let stepId = null;
284
+ let hasEmittedRunStarted = false;
285
+ let hasEmittedTextMessageStart = false;
282
286
  try {
283
287
  for await (const event of stream) {
288
+ if (!hasEmittedRunStarted) {
289
+ hasEmittedRunStarted = true;
290
+ yield {
291
+ type: "RUN_STARTED",
292
+ runId,
293
+ model,
294
+ timestamp
295
+ };
296
+ }
284
297
  if (event.type === "content_block_start") {
285
298
  if (event.content_block.type === "tool_use") {
286
299
  currentToolIndex++;
287
300
  toolCallsMap.set(currentToolIndex, {
288
301
  id: event.content_block.id,
289
302
  name: event.content_block.name,
290
- input: ""
303
+ input: "",
304
+ started: false
291
305
  });
292
306
  } else if (event.content_block.type === "thinking") {
293
307
  accumulatedThinking = "";
308
+ stepId = genId();
309
+ yield {
310
+ type: "STEP_STARTED",
311
+ stepId,
312
+ model,
313
+ timestamp,
314
+ stepType: "thinking"
315
+ };
294
316
  }
295
317
  } else if (event.type === "content_block_delta") {
296
318
  if (event.delta.type === "text_delta") {
319
+ if (!hasEmittedTextMessageStart) {
320
+ hasEmittedTextMessageStart = true;
321
+ yield {
322
+ type: "TEXT_MESSAGE_START",
323
+ messageId,
324
+ model,
325
+ timestamp,
326
+ role: "assistant"
327
+ };
328
+ }
297
329
  const delta = event.delta.text;
298
330
  accumulatedContent += delta;
299
331
  yield {
300
- type: "content",
301
- id: genId(),
332
+ type: "TEXT_MESSAGE_CONTENT",
333
+ messageId,
302
334
  model,
303
335
  timestamp,
304
336
  delta,
305
- content: accumulatedContent,
306
- role: "assistant"
337
+ content: accumulatedContent
307
338
  };
308
339
  } else if (event.delta.type === "thinking_delta") {
309
340
  const delta = event.delta.thinking;
310
341
  accumulatedThinking += delta;
311
342
  yield {
312
- type: "thinking",
313
- id: genId(),
343
+ type: "STEP_FINISHED",
344
+ stepId: stepId || genId(),
314
345
  model,
315
346
  timestamp,
316
347
  delta,
@@ -319,47 +350,69 @@ class AnthropicTextAdapter extends BaseTextAdapter {
319
350
  } else if (event.delta.type === "input_json_delta") {
320
351
  const existing = toolCallsMap.get(currentToolIndex);
321
352
  if (existing) {
353
+ if (!existing.started) {
354
+ existing.started = true;
355
+ yield {
356
+ type: "TOOL_CALL_START",
357
+ toolCallId: existing.id,
358
+ toolName: existing.name,
359
+ model,
360
+ timestamp,
361
+ index: currentToolIndex
362
+ };
363
+ }
322
364
  existing.input += event.delta.partial_json;
323
365
  yield {
324
- type: "tool_call",
325
- id: genId(),
366
+ type: "TOOL_CALL_ARGS",
367
+ toolCallId: existing.id,
326
368
  model,
327
369
  timestamp,
328
- toolCall: {
329
- id: existing.id,
330
- type: "function",
331
- function: {
332
- name: existing.name,
333
- arguments: event.delta.partial_json
334
- }
335
- },
336
- index: currentToolIndex
370
+ delta: event.delta.partial_json,
371
+ args: existing.input
337
372
  };
338
373
  }
339
374
  }
340
375
  } else if (event.type === "content_block_stop") {
341
376
  const existing = toolCallsMap.get(currentToolIndex);
342
- if (existing && existing.input === "") {
377
+ if (existing) {
378
+ if (!existing.started) {
379
+ existing.started = true;
380
+ yield {
381
+ type: "TOOL_CALL_START",
382
+ toolCallId: existing.id,
383
+ toolName: existing.name,
384
+ model,
385
+ timestamp,
386
+ index: currentToolIndex
387
+ };
388
+ }
389
+ let parsedInput = {};
390
+ try {
391
+ parsedInput = existing.input ? JSON.parse(existing.input) : {};
392
+ } catch {
393
+ parsedInput = {};
394
+ }
343
395
  yield {
344
- type: "tool_call",
345
- id: genId(),
396
+ type: "TOOL_CALL_END",
397
+ toolCallId: existing.id,
398
+ toolName: existing.name,
346
399
  model,
347
400
  timestamp,
348
- toolCall: {
349
- id: existing.id,
350
- type: "function",
351
- function: {
352
- name: existing.name,
353
- arguments: "{}"
354
- }
355
- },
356
- index: currentToolIndex
401
+ input: parsedInput
402
+ };
403
+ }
404
+ if (hasEmittedTextMessageStart && accumulatedContent) {
405
+ yield {
406
+ type: "TEXT_MESSAGE_END",
407
+ messageId,
408
+ model,
409
+ timestamp
357
410
  };
358
411
  }
359
412
  } else if (event.type === "message_stop") {
360
413
  yield {
361
- type: "done",
362
- id: genId(),
414
+ type: "RUN_FINISHED",
415
+ runId,
363
416
  model,
364
417
  timestamp,
365
418
  finishReason: "stop"
@@ -369,8 +422,8 @@ class AnthropicTextAdapter extends BaseTextAdapter {
369
422
  switch (event.delta.stop_reason) {
370
423
  case "tool_use": {
371
424
  yield {
372
- type: "done",
373
- id: genId(),
425
+ type: "RUN_FINISHED",
426
+ runId,
374
427
  model,
375
428
  timestamp,
376
429
  finishReason: "tool_calls",
@@ -384,8 +437,8 @@ class AnthropicTextAdapter extends BaseTextAdapter {
384
437
  }
385
438
  case "max_tokens": {
386
439
  yield {
387
- type: "error",
388
- id: genId(),
440
+ type: "RUN_ERROR",
441
+ runId,
389
442
  model,
390
443
  timestamp,
391
444
  error: {
@@ -397,8 +450,8 @@ class AnthropicTextAdapter extends BaseTextAdapter {
397
450
  }
398
451
  default: {
399
452
  yield {
400
- type: "done",
401
- id: genId(),
453
+ type: "RUN_FINISHED",
454
+ runId,
402
455
  model,
403
456
  timestamp,
404
457
  finishReason: "stop",
@@ -416,8 +469,8 @@ class AnthropicTextAdapter extends BaseTextAdapter {
416
469
  } catch (error) {
417
470
  const err = error;
418
471
  yield {
419
- type: "error",
420
- id: genId(),
472
+ type: "RUN_ERROR",
473
+ runId,
421
474
  model,
422
475
  timestamp,
423
476
  error: {
@@ -1 +1 @@
1
- {"version":3,"file":"text.js","sources":["../../../src/adapters/text.ts"],"sourcesContent":["import { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { convertToolsToProviderFormat } from '../tools/tool-converter'\nimport { validateTextProviderOptions } from '../text/text-provider-options'\nimport {\n createAnthropicClient,\n generateId,\n getAnthropicApiKeyFromEnv,\n} from '../utils'\nimport type {\n ANTHROPIC_MODELS,\n AnthropicChatModelProviderOptionsByName,\n AnthropicModelInputModalitiesByName,\n} from '../model-meta'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type {\n Base64ImageSource,\n Base64PDFSource,\n DocumentBlockParam,\n ImageBlockParam,\n MessageParam,\n TextBlockParam,\n URLImageSource,\n URLPDFSource,\n} from '@anthropic-ai/sdk/resources/messages'\nimport type Anthropic_SDK from '@anthropic-ai/sdk'\nimport type {\n ContentPart,\n Modality,\n ModelMessage,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\nimport type {\n ExternalTextProviderOptions,\n InternalTextProviderOptions,\n} from '../text/text-provider-options'\nimport type {\n AnthropicDocumentMetadata,\n AnthropicImageMetadata,\n AnthropicMessageMetadataByModality,\n AnthropicTextMetadata,\n} from '../message-types'\nimport type { AnthropicClientConfig } from '../utils'\n\n/**\n * Configuration for Anthropic text adapter\n */\nexport interface AnthropicTextConfig extends AnthropicClientConfig {}\n\n/**\n * Anthropic-specific provider options for text/chat\n */\nexport type AnthropicTextProviderOptions = ExternalTextProviderOptions\n\ntype AnthropicContentBlocks =\n Extract<MessageParam['content'], Array<unknown>> extends Array<infer Block>\n ? Array<Block>\n : never\ntype AnthropicContentBlock =\n AnthropicContentBlocks extends Array<infer Block> ? Block : never\n\n// ===========================\n// Type Resolution Helpers\n// ===========================\n\n/**\n * Resolve provider options for a specific model.\n * If the model has explicit options in the map, use those; otherwise use base options.\n */\ntype ResolveProviderOptions<TModel extends string> =\n TModel extends keyof AnthropicChatModelProviderOptionsByName\n ? AnthropicChatModelProviderOptionsByName[TModel]\n : AnthropicTextProviderOptions\n\n/**\n * Resolve input modalities for a specific model.\n * If the model has explicit modalities in the map, use those; otherwise use default.\n */\ntype ResolveInputModalities<TModel extends string> =\n TModel extends keyof AnthropicModelInputModalitiesByName\n ? AnthropicModelInputModalitiesByName[TModel]\n : readonly ['text', 'image', 'document']\n\n// ===========================\n// Adapter Implementation\n// ===========================\n\n/**\n * Anthropic Text (Chat) Adapter\n *\n * Tree-shakeable adapter for Anthropic chat/text completion functionality.\n * Import only what you need for smaller bundle sizes.\n */\nexport class AnthropicTextAdapter<\n TModel extends (typeof ANTHROPIC_MODELS)[number],\n TProviderOptions extends object = ResolveProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n AnthropicMessageMetadataByModality\n> {\n readonly kind = 'text' as const\n readonly name = 'anthropic' as const\n\n private client: Anthropic_SDK\n\n constructor(config: AnthropicTextConfig, model: TModel) {\n super({}, model)\n this.client = createAnthropicClient(config)\n }\n\n async *chatStream(\n options: TextOptions<AnthropicTextProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n try {\n const requestParams = this.mapCommonOptionsToAnthropic(options)\n\n const stream = await this.client.beta.messages.create(\n { ...requestParams, stream: true },\n {\n signal: options.request?.signal,\n headers: options.request?.headers,\n },\n )\n\n yield* this.processAnthropicStream(stream, options.model, () =>\n generateId(this.name),\n )\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\n yield {\n type: 'error',\n id: generateId(this.name),\n model: options.model,\n timestamp: Date.now(),\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n },\n }\n }\n }\n\n /**\n * Generate structured output using Anthropic's tool-based approach.\n * Anthropic doesn't have native structured output, so we use a tool with the schema\n * and force the model to call it.\n * The outputSchema is already JSON Schema (converted in the ai layer).\n */\n async structuredOutput(\n options: StructuredOutputOptions<AnthropicTextProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n\n const requestParams = this.mapCommonOptionsToAnthropic(chatOptions)\n\n // Create a tool that will capture the structured output\n // Anthropic's SDK requires input_schema with type: 'object' literal\n const structuredOutputTool = {\n name: 'structured_output',\n description:\n 'Use this tool to provide your response in the required structured format.',\n input_schema: {\n type: 'object' as const,\n properties: outputSchema.properties ?? {},\n required: outputSchema.required ?? [],\n },\n }\n\n try {\n // Make non-streaming request with tool_choice forced to our structured output tool\n const response = await this.client.messages.create(\n {\n ...requestParams,\n stream: false,\n tools: [structuredOutputTool],\n tool_choice: { type: 'tool', name: 'structured_output' },\n },\n {\n signal: chatOptions.request?.signal,\n headers: chatOptions.request?.headers,\n },\n )\n\n // Extract the tool use content from the response\n let parsed: unknown = null\n let rawText = ''\n\n for (const block of response.content) {\n if (block.type === 'tool_use' && block.name === 'structured_output') {\n parsed = block.input\n rawText = JSON.stringify(block.input)\n break\n }\n }\n\n if (parsed === null) {\n // Fallback: try to extract text content and parse as JSON\n rawText = response.content\n .map((b) => {\n if (b.type === 'text') {\n return b.text\n }\n return ''\n })\n .join('')\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to extract structured output from response. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n }\n\n return {\n data: parsed,\n rawText,\n }\n } catch (error: unknown) {\n const err = error as Error\n throw new Error(\n `Structured output generation failed: ${err.message || 'Unknown error occurred'}`,\n )\n }\n }\n\n private mapCommonOptionsToAnthropic(\n options: TextOptions<AnthropicTextProviderOptions>,\n ) {\n const modelOptions = options.modelOptions as\n | InternalTextProviderOptions\n | undefined\n\n const formattedMessages = this.formatMessages(options.messages)\n const tools = options.tools\n ? convertToolsToProviderFormat(options.tools)\n : undefined\n\n const validProviderOptions: Partial<InternalTextProviderOptions> = {}\n if (modelOptions) {\n const validKeys: Array<keyof InternalTextProviderOptions> = [\n 'container',\n 'context_management',\n 'mcp_servers',\n 'service_tier',\n 'stop_sequences',\n 'system',\n 'thinking',\n 'tool_choice',\n 'top_k',\n ]\n for (const key of validKeys) {\n if (key in modelOptions) {\n const value = modelOptions[key]\n if (key === 'tool_choice' && typeof value === 'string') {\n ;(validProviderOptions as Record<string, unknown>)[key] = {\n type: value,\n }\n } else {\n ;(validProviderOptions as Record<string, unknown>)[key] = value\n }\n }\n }\n }\n\n const thinkingBudget =\n validProviderOptions.thinking?.type === 'enabled'\n ? validProviderOptions.thinking.budget_tokens\n : undefined\n const defaultMaxTokens = options.maxTokens || 1024\n const maxTokens =\n thinkingBudget && thinkingBudget >= defaultMaxTokens\n ? thinkingBudget + 1\n : defaultMaxTokens\n\n const requestParams: InternalTextProviderOptions = {\n model: options.model,\n max_tokens: maxTokens,\n temperature: options.temperature,\n top_p: options.topP,\n messages: formattedMessages,\n system: options.systemPrompts?.join('\\n'),\n tools: tools,\n ...validProviderOptions,\n }\n validateTextProviderOptions(requestParams)\n return requestParams\n }\n\n private convertContentPartToAnthropic(\n part: ContentPart,\n ): TextBlockParam | ImageBlockParam | DocumentBlockParam {\n switch (part.type) {\n case 'text': {\n const metadata = part.metadata as AnthropicTextMetadata | undefined\n return {\n type: 'text',\n text: part.content,\n ...metadata,\n }\n }\n\n case 'image': {\n const metadata = part.metadata as AnthropicImageMetadata | undefined\n const imageSource: Base64ImageSource | URLImageSource =\n part.source.type === 'data'\n ? {\n type: 'base64',\n data: part.source.value,\n media_type: metadata?.mediaType ?? 'image/jpeg',\n }\n : {\n type: 'url',\n url: part.source.value,\n }\n const { mediaType: _mediaType, ...meta } = metadata || {}\n return {\n type: 'image',\n source: imageSource,\n ...meta,\n }\n }\n case 'document': {\n const metadata = part.metadata as AnthropicDocumentMetadata | undefined\n const docSource: Base64PDFSource | URLPDFSource =\n part.source.type === 'data'\n ? {\n type: 'base64',\n data: part.source.value,\n media_type: 'application/pdf',\n }\n : {\n type: 'url',\n url: part.source.value,\n }\n return {\n type: 'document',\n source: docSource,\n ...metadata,\n }\n }\n case 'audio':\n case 'video':\n throw new Error(\n `Anthropic does not support ${part.type} content directly`,\n )\n default: {\n const _exhaustiveCheck: never = part\n throw new Error(\n `Unsupported content part type: ${(_exhaustiveCheck as ContentPart).type}`,\n )\n }\n }\n }\n\n private formatMessages(\n messages: Array<ModelMessage>,\n ): InternalTextProviderOptions['messages'] {\n const formattedMessages: InternalTextProviderOptions['messages'] = []\n\n for (const message of messages) {\n const role = message.role\n\n if (role === 'tool' && message.toolCallId) {\n formattedMessages.push({\n role: 'user',\n content: [\n {\n type: 'tool_result',\n tool_use_id: message.toolCallId,\n content:\n typeof message.content === 'string' ? message.content : '',\n },\n ],\n })\n continue\n }\n\n if (role === 'assistant' && message.toolCalls?.length) {\n const contentBlocks: AnthropicContentBlocks = []\n\n if (message.content) {\n const content =\n typeof message.content === 'string' ? message.content : ''\n const textBlock: AnthropicContentBlock = {\n type: 'text',\n text: content,\n }\n contentBlocks.push(textBlock)\n }\n\n for (const toolCall of message.toolCalls) {\n let parsedInput: unknown = {}\n try {\n parsedInput = toolCall.function.arguments\n ? JSON.parse(toolCall.function.arguments)\n : {}\n } catch {\n parsedInput = toolCall.function.arguments\n }\n\n const toolUseBlock: AnthropicContentBlock = {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.function.name,\n input: parsedInput,\n }\n contentBlocks.push(toolUseBlock)\n }\n\n formattedMessages.push({\n role: 'assistant',\n content: contentBlocks,\n })\n\n continue\n }\n\n if (role === 'user' && Array.isArray(message.content)) {\n const contentBlocks = message.content.map((part) =>\n this.convertContentPartToAnthropic(part),\n )\n formattedMessages.push({\n role: 'user',\n content: contentBlocks,\n })\n continue\n }\n\n formattedMessages.push({\n role: role === 'assistant' ? 'assistant' : 'user',\n content:\n typeof message.content === 'string'\n ? message.content\n : message.content\n ? message.content.map((c) =>\n this.convertContentPartToAnthropic(c),\n )\n : '',\n })\n }\n\n return formattedMessages\n }\n\n private async *processAnthropicStream(\n stream: AsyncIterable<Anthropic_SDK.Beta.BetaRawMessageStreamEvent>,\n model: string,\n genId: () => string,\n ): AsyncIterable<StreamChunk> {\n let accumulatedContent = ''\n let accumulatedThinking = ''\n const timestamp = Date.now()\n const toolCallsMap = new Map<\n number,\n { id: string; name: string; input: string }\n >()\n let currentToolIndex = -1\n\n try {\n for await (const event of stream) {\n if (event.type === 'content_block_start') {\n if (event.content_block.type === 'tool_use') {\n currentToolIndex++\n toolCallsMap.set(currentToolIndex, {\n id: event.content_block.id,\n name: event.content_block.name,\n input: '',\n })\n } else if (event.content_block.type === 'thinking') {\n accumulatedThinking = ''\n }\n } else if (event.type === 'content_block_delta') {\n if (event.delta.type === 'text_delta') {\n const delta = event.delta.text\n accumulatedContent += delta\n yield {\n type: 'content',\n id: genId(),\n model: model,\n timestamp,\n delta,\n content: accumulatedContent,\n role: 'assistant',\n }\n } else if (event.delta.type === 'thinking_delta') {\n const delta = event.delta.thinking\n accumulatedThinking += delta\n yield {\n type: 'thinking',\n id: genId(),\n model: model,\n timestamp,\n delta,\n content: accumulatedThinking,\n }\n } else if (event.delta.type === 'input_json_delta') {\n const existing = toolCallsMap.get(currentToolIndex)\n if (existing) {\n existing.input += event.delta.partial_json\n\n yield {\n type: 'tool_call',\n id: genId(),\n model: model,\n timestamp,\n toolCall: {\n id: existing.id,\n type: 'function',\n function: {\n name: existing.name,\n arguments: event.delta.partial_json,\n },\n },\n index: currentToolIndex,\n }\n }\n }\n } else if (event.type === 'content_block_stop') {\n const existing = toolCallsMap.get(currentToolIndex)\n if (existing && existing.input === '') {\n yield {\n type: 'tool_call',\n id: genId(),\n model: model,\n timestamp,\n toolCall: {\n id: existing.id,\n type: 'function',\n function: {\n name: existing.name,\n arguments: '{}',\n },\n },\n index: currentToolIndex,\n }\n }\n } else if (event.type === 'message_stop') {\n yield {\n type: 'done',\n id: genId(),\n model: model,\n timestamp,\n finishReason: 'stop',\n }\n } else if (event.type === 'message_delta') {\n if (event.delta.stop_reason) {\n switch (event.delta.stop_reason) {\n case 'tool_use': {\n yield {\n type: 'done',\n id: genId(),\n model: model,\n timestamp,\n finishReason: 'tool_calls',\n usage: {\n promptTokens: event.usage.input_tokens || 0,\n completionTokens: event.usage.output_tokens || 0,\n totalTokens:\n (event.usage.input_tokens || 0) +\n (event.usage.output_tokens || 0),\n },\n }\n break\n }\n case 'max_tokens': {\n yield {\n type: 'error',\n id: genId(),\n model: model,\n timestamp,\n error: {\n message:\n 'The response was cut off because the maximum token limit was reached.',\n code: 'max_tokens',\n },\n }\n break\n }\n default: {\n yield {\n type: 'done',\n id: genId(),\n model: model,\n timestamp,\n finishReason: 'stop',\n usage: {\n promptTokens: event.usage.input_tokens || 0,\n completionTokens: event.usage.output_tokens || 0,\n totalTokens:\n (event.usage.input_tokens || 0) +\n (event.usage.output_tokens || 0),\n },\n }\n }\n }\n }\n }\n }\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\n\n yield {\n type: 'error',\n id: genId(),\n model: model,\n timestamp,\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n },\n }\n }\n }\n}\n\n/**\n * Creates an Anthropic chat adapter with explicit API key.\n * Type resolution happens here at the call site.\n */\nexport function createAnthropicChat<\n TModel extends (typeof ANTHROPIC_MODELS)[number],\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<AnthropicTextConfig, 'apiKey'>,\n): AnthropicTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>\n> {\n return new AnthropicTextAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates an Anthropic text adapter with automatic API key detection.\n * Type resolution happens here at the call site.\n */\nexport function anthropicText<TModel extends (typeof ANTHROPIC_MODELS)[number]>(\n model: TModel,\n config?: Omit<AnthropicTextConfig, 'apiKey'>,\n): AnthropicTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>\n> {\n const apiKey = getAnthropicApiKeyFromEnv()\n return createAnthropicChat(model, apiKey, config)\n}\n"],"names":[],"mappings":";;;;AAgGO,MAAM,6BAKH,gBAKR;AAAA,EAMA,YAAY,QAA6B,OAAe;AACtD,UAAM,CAAA,GAAI,KAAK;AANjB,SAAS,OAAO;AAChB,SAAS,OAAO;AAMd,SAAK,SAAS,sBAAsB,MAAM;AAAA,EAC5C;AAAA,EAEA,OAAO,WACL,SAC4B;AAC5B,QAAI;AACF,YAAM,gBAAgB,KAAK,4BAA4B,OAAO;AAE9D,YAAM,SAAS,MAAM,KAAK,OAAO,KAAK,SAAS;AAAA,QAC7C,EAAE,GAAG,eAAe,QAAQ,KAAA;AAAA,QAC5B;AAAA,UACE,QAAQ,QAAQ,SAAS;AAAA,UACzB,SAAS,QAAQ,SAAS;AAAA,QAAA;AAAA,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AI,MAAM;AAAA,QAAA;AAAA,MACrC;AAAA,IAEJ;AAAA,EACF;AACF;AAMO,SAAS,oBAGd,OACA,QACA,QAKA;AACA,SAAO,IAAI,qBAAqB,EAAE,QAAQ,GAAG,OAAA,GAAU,KAAK;AAC9D;AAMO,SAAS,cACd,OACA,QAKA;AACA,QAAM,SAAS,0BAAA;AACf,SAAO,oBAAoB,OAAO,QAAQ,MAAM;AAClD;"}
1
+ {"version":3,"file":"text.js","sources":["../../../src/adapters/text.ts"],"sourcesContent":["import { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { convertToolsToProviderFormat } from '../tools/tool-converter'\nimport { validateTextProviderOptions } from '../text/text-provider-options'\nimport {\n createAnthropicClient,\n generateId,\n getAnthropicApiKeyFromEnv,\n} from '../utils'\nimport type {\n ANTHROPIC_MODELS,\n AnthropicChatModelProviderOptionsByName,\n AnthropicModelInputModalitiesByName,\n} from '../model-meta'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type {\n Base64ImageSource,\n Base64PDFSource,\n DocumentBlockParam,\n ImageBlockParam,\n MessageParam,\n TextBlockParam,\n URLImageSource,\n URLPDFSource,\n} from '@anthropic-ai/sdk/resources/messages'\nimport type Anthropic_SDK from '@anthropic-ai/sdk'\nimport type {\n ContentPart,\n Modality,\n ModelMessage,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\nimport type {\n ExternalTextProviderOptions,\n InternalTextProviderOptions,\n} from '../text/text-provider-options'\nimport type {\n AnthropicDocumentMetadata,\n AnthropicImageMetadata,\n AnthropicMessageMetadataByModality,\n AnthropicTextMetadata,\n} from '../message-types'\nimport type { AnthropicClientConfig } from '../utils'\n\n/**\n * Configuration for Anthropic text adapter\n */\nexport interface AnthropicTextConfig extends AnthropicClientConfig {}\n\n/**\n * Anthropic-specific provider options for text/chat\n */\nexport type AnthropicTextProviderOptions = ExternalTextProviderOptions\n\ntype AnthropicContentBlocks =\n Extract<MessageParam['content'], Array<unknown>> extends Array<infer Block>\n ? Array<Block>\n : never\ntype AnthropicContentBlock =\n AnthropicContentBlocks extends Array<infer Block> ? Block : never\n\n// ===========================\n// Type Resolution Helpers\n// ===========================\n\n/**\n * Resolve provider options for a specific model.\n * If the model has explicit options in the map, use those; otherwise use base options.\n */\ntype ResolveProviderOptions<TModel extends string> =\n TModel extends keyof AnthropicChatModelProviderOptionsByName\n ? AnthropicChatModelProviderOptionsByName[TModel]\n : AnthropicTextProviderOptions\n\n/**\n * Resolve input modalities for a specific model.\n * If the model has explicit modalities in the map, use those; otherwise use default.\n */\ntype ResolveInputModalities<TModel extends string> =\n TModel extends keyof AnthropicModelInputModalitiesByName\n ? AnthropicModelInputModalitiesByName[TModel]\n : readonly ['text', 'image', 'document']\n\n// ===========================\n// Adapter Implementation\n// ===========================\n\n/**\n * Anthropic Text (Chat) Adapter\n *\n * Tree-shakeable adapter for Anthropic chat/text completion functionality.\n * Import only what you need for smaller bundle sizes.\n */\nexport class AnthropicTextAdapter<\n TModel extends (typeof ANTHROPIC_MODELS)[number],\n TProviderOptions extends object = ResolveProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n AnthropicMessageMetadataByModality\n> {\n readonly kind = 'text' as const\n readonly name = 'anthropic' as const\n\n private client: Anthropic_SDK\n\n constructor(config: AnthropicTextConfig, model: TModel) {\n super({}, model)\n this.client = createAnthropicClient(config)\n }\n\n async *chatStream(\n options: TextOptions<AnthropicTextProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n try {\n const requestParams = this.mapCommonOptionsToAnthropic(options)\n\n const stream = await this.client.beta.messages.create(\n { ...requestParams, stream: true },\n {\n signal: options.request?.signal,\n headers: options.request?.headers,\n },\n )\n\n yield* this.processAnthropicStream(stream, options.model, () =>\n generateId(this.name),\n )\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\n yield {\n type: 'RUN_ERROR',\n model: options.model,\n timestamp: Date.now(),\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n },\n }\n }\n }\n\n /**\n * Generate structured output using Anthropic's tool-based approach.\n * Anthropic doesn't have native structured output, so we use a tool with the schema\n * and force the model to call it.\n * The outputSchema is already JSON Schema (converted in the ai layer).\n */\n async structuredOutput(\n options: StructuredOutputOptions<AnthropicTextProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n\n const requestParams = this.mapCommonOptionsToAnthropic(chatOptions)\n\n // Create a tool that will capture the structured output\n // Anthropic's SDK requires input_schema with type: 'object' literal\n const structuredOutputTool = {\n name: 'structured_output',\n description:\n 'Use this tool to provide your response in the required structured format.',\n input_schema: {\n type: 'object' as const,\n properties: outputSchema.properties ?? {},\n required: outputSchema.required ?? [],\n },\n }\n\n try {\n // Make non-streaming request with tool_choice forced to our structured output tool\n const response = await this.client.messages.create(\n {\n ...requestParams,\n stream: false,\n tools: [structuredOutputTool],\n tool_choice: { type: 'tool', name: 'structured_output' },\n },\n {\n signal: chatOptions.request?.signal,\n headers: chatOptions.request?.headers,\n },\n )\n\n // Extract the tool use content from the response\n let parsed: unknown = null\n let rawText = ''\n\n for (const block of response.content) {\n if (block.type === 'tool_use' && block.name === 'structured_output') {\n parsed = block.input\n rawText = JSON.stringify(block.input)\n break\n }\n }\n\n if (parsed === null) {\n // Fallback: try to extract text content and parse as JSON\n rawText = response.content\n .map((b) => {\n if (b.type === 'text') {\n return b.text\n }\n return ''\n })\n .join('')\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to extract structured output from response. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n }\n\n return {\n data: parsed,\n rawText,\n }\n } catch (error: unknown) {\n const err = error as Error\n throw new Error(\n `Structured output generation failed: ${err.message || 'Unknown error occurred'}`,\n )\n }\n }\n\n private mapCommonOptionsToAnthropic(\n options: TextOptions<AnthropicTextProviderOptions>,\n ) {\n const modelOptions = options.modelOptions as\n | InternalTextProviderOptions\n | undefined\n\n const formattedMessages = this.formatMessages(options.messages)\n const tools = options.tools\n ? convertToolsToProviderFormat(options.tools)\n : undefined\n\n const validProviderOptions: Partial<InternalTextProviderOptions> = {}\n if (modelOptions) {\n const validKeys: Array<keyof InternalTextProviderOptions> = [\n 'container',\n 'context_management',\n 'mcp_servers',\n 'service_tier',\n 'stop_sequences',\n 'system',\n 'thinking',\n 'tool_choice',\n 'top_k',\n ]\n for (const key of validKeys) {\n if (key in modelOptions) {\n const value = modelOptions[key]\n if (key === 'tool_choice' && typeof value === 'string') {\n ;(validProviderOptions as Record<string, unknown>)[key] = {\n type: value,\n }\n } else {\n ;(validProviderOptions as Record<string, unknown>)[key] = value\n }\n }\n }\n }\n\n const thinkingBudget =\n validProviderOptions.thinking?.type === 'enabled'\n ? validProviderOptions.thinking.budget_tokens\n : undefined\n const defaultMaxTokens = options.maxTokens || 1024\n const maxTokens =\n thinkingBudget && thinkingBudget >= defaultMaxTokens\n ? thinkingBudget + 1\n : defaultMaxTokens\n\n const requestParams: InternalTextProviderOptions = {\n model: options.model,\n max_tokens: maxTokens,\n temperature: options.temperature,\n top_p: options.topP,\n messages: formattedMessages,\n system: options.systemPrompts?.join('\\n'),\n tools: tools,\n ...validProviderOptions,\n }\n validateTextProviderOptions(requestParams)\n return requestParams\n }\n\n private convertContentPartToAnthropic(\n part: ContentPart,\n ): TextBlockParam | ImageBlockParam | DocumentBlockParam {\n switch (part.type) {\n case 'text': {\n const metadata = part.metadata as AnthropicTextMetadata | undefined\n return {\n type: 'text',\n text: part.content,\n ...metadata,\n }\n }\n\n case 'image': {\n const metadata = part.metadata as AnthropicImageMetadata | undefined\n const imageSource: Base64ImageSource | URLImageSource =\n part.source.type === 'data'\n ? {\n type: 'base64',\n data: part.source.value,\n media_type: metadata?.mediaType ?? 'image/jpeg',\n }\n : {\n type: 'url',\n url: part.source.value,\n }\n const { mediaType: _mediaType, ...meta } = metadata || {}\n return {\n type: 'image',\n source: imageSource,\n ...meta,\n }\n }\n case 'document': {\n const metadata = part.metadata as AnthropicDocumentMetadata | undefined\n const docSource: Base64PDFSource | URLPDFSource =\n part.source.type === 'data'\n ? {\n type: 'base64',\n data: part.source.value,\n media_type: 'application/pdf',\n }\n : {\n type: 'url',\n url: part.source.value,\n }\n return {\n type: 'document',\n source: docSource,\n ...metadata,\n }\n }\n case 'audio':\n case 'video':\n throw new Error(\n `Anthropic does not support ${part.type} content directly`,\n )\n default: {\n const _exhaustiveCheck: never = part\n throw new Error(\n `Unsupported content part type: ${(_exhaustiveCheck as ContentPart).type}`,\n )\n }\n }\n }\n\n private formatMessages(\n messages: Array<ModelMessage>,\n ): InternalTextProviderOptions['messages'] {\n const formattedMessages: InternalTextProviderOptions['messages'] = []\n\n for (const message of messages) {\n const role = message.role\n\n if (role === 'tool' && message.toolCallId) {\n formattedMessages.push({\n role: 'user',\n content: [\n {\n type: 'tool_result',\n tool_use_id: message.toolCallId,\n content:\n typeof message.content === 'string' ? message.content : '',\n },\n ],\n })\n continue\n }\n\n if (role === 'assistant' && message.toolCalls?.length) {\n const contentBlocks: AnthropicContentBlocks = []\n\n if (message.content) {\n const content =\n typeof message.content === 'string' ? message.content : ''\n const textBlock: AnthropicContentBlock = {\n type: 'text',\n text: content,\n }\n contentBlocks.push(textBlock)\n }\n\n for (const toolCall of message.toolCalls) {\n let parsedInput: unknown = {}\n try {\n parsedInput = toolCall.function.arguments\n ? JSON.parse(toolCall.function.arguments)\n : {}\n } catch {\n parsedInput = toolCall.function.arguments\n }\n\n const toolUseBlock: AnthropicContentBlock = {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.function.name,\n input: parsedInput,\n }\n contentBlocks.push(toolUseBlock)\n }\n\n formattedMessages.push({\n role: 'assistant',\n content: contentBlocks,\n })\n\n continue\n }\n\n if (role === 'user' && Array.isArray(message.content)) {\n const contentBlocks = message.content.map((part) =>\n this.convertContentPartToAnthropic(part),\n )\n formattedMessages.push({\n role: 'user',\n content: contentBlocks,\n })\n continue\n }\n\n formattedMessages.push({\n role: role === 'assistant' ? 'assistant' : 'user',\n content:\n typeof message.content === 'string'\n ? message.content\n : message.content\n ? message.content.map((c) =>\n this.convertContentPartToAnthropic(c),\n )\n : '',\n })\n }\n\n return formattedMessages\n }\n\n private async *processAnthropicStream(\n stream: AsyncIterable<Anthropic_SDK.Beta.BetaRawMessageStreamEvent>,\n model: string,\n genId: () => string,\n ): AsyncIterable<StreamChunk> {\n let accumulatedContent = ''\n let accumulatedThinking = ''\n const timestamp = Date.now()\n const toolCallsMap = new Map<\n number,\n { id: string; name: string; input: string; started: boolean }\n >()\n let currentToolIndex = -1\n\n // AG-UI lifecycle tracking\n const runId = genId()\n const messageId = genId()\n let stepId: string | null = null\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n\n try {\n for await (const event of stream) {\n // Emit RUN_STARTED on first event\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: 'RUN_STARTED',\n runId,\n model,\n timestamp,\n }\n }\n\n if (event.type === 'content_block_start') {\n if (event.content_block.type === 'tool_use') {\n currentToolIndex++\n toolCallsMap.set(currentToolIndex, {\n id: event.content_block.id,\n name: event.content_block.name,\n input: '',\n started: false,\n })\n } else if (event.content_block.type === 'thinking') {\n accumulatedThinking = ''\n // Emit STEP_STARTED for thinking\n stepId = genId()\n yield {\n type: 'STEP_STARTED',\n stepId,\n model,\n timestamp,\n stepType: 'thinking',\n }\n }\n } else if (event.type === 'content_block_delta') {\n if (event.delta.type === 'text_delta') {\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: 'TEXT_MESSAGE_START',\n messageId,\n model,\n timestamp,\n role: 'assistant',\n }\n }\n\n const delta = event.delta.text\n accumulatedContent += delta\n yield {\n type: 'TEXT_MESSAGE_CONTENT',\n messageId,\n model,\n timestamp,\n delta,\n content: accumulatedContent,\n }\n } else if (event.delta.type === 'thinking_delta') {\n const delta = event.delta.thinking\n accumulatedThinking += delta\n yield {\n type: 'STEP_FINISHED',\n stepId: stepId || genId(),\n model,\n timestamp,\n delta,\n content: accumulatedThinking,\n }\n } else if (event.delta.type === 'input_json_delta') {\n const existing = toolCallsMap.get(currentToolIndex)\n if (existing) {\n // Emit TOOL_CALL_START on first args delta\n if (!existing.started) {\n existing.started = true\n yield {\n type: 'TOOL_CALL_START',\n toolCallId: existing.id,\n toolName: existing.name,\n model,\n timestamp,\n index: currentToolIndex,\n }\n }\n\n existing.input += event.delta.partial_json\n\n yield {\n type: 'TOOL_CALL_ARGS',\n toolCallId: existing.id,\n model,\n timestamp,\n delta: event.delta.partial_json,\n args: existing.input,\n }\n }\n }\n } else if (event.type === 'content_block_stop') {\n const existing = toolCallsMap.get(currentToolIndex)\n if (existing) {\n // If tool call wasn't started yet (no args), start it now\n if (!existing.started) {\n existing.started = true\n yield {\n type: 'TOOL_CALL_START',\n toolCallId: existing.id,\n toolName: existing.name,\n model,\n timestamp,\n index: currentToolIndex,\n }\n }\n\n // Emit TOOL_CALL_END\n let parsedInput: unknown = {}\n try {\n parsedInput = existing.input ? JSON.parse(existing.input) : {}\n } catch {\n parsedInput = {}\n }\n\n yield {\n type: 'TOOL_CALL_END',\n toolCallId: existing.id,\n toolName: existing.name,\n model,\n timestamp,\n input: parsedInput,\n }\n }\n\n // Emit TEXT_MESSAGE_END if we had text content\n if (hasEmittedTextMessageStart && accumulatedContent) {\n yield {\n type: 'TEXT_MESSAGE_END',\n messageId,\n model,\n timestamp,\n }\n }\n } else if (event.type === 'message_stop') {\n yield {\n type: 'RUN_FINISHED',\n runId,\n model,\n timestamp,\n finishReason: 'stop',\n }\n } else if (event.type === 'message_delta') {\n if (event.delta.stop_reason) {\n switch (event.delta.stop_reason) {\n case 'tool_use': {\n yield {\n type: 'RUN_FINISHED',\n runId,\n model,\n timestamp,\n finishReason: 'tool_calls',\n usage: {\n promptTokens: event.usage.input_tokens || 0,\n completionTokens: event.usage.output_tokens || 0,\n totalTokens:\n (event.usage.input_tokens || 0) +\n (event.usage.output_tokens || 0),\n },\n }\n break\n }\n case 'max_tokens': {\n yield {\n type: 'RUN_ERROR',\n runId,\n model,\n timestamp,\n error: {\n message:\n 'The response was cut off because the maximum token limit was reached.',\n code: 'max_tokens',\n },\n }\n break\n }\n default: {\n yield {\n type: 'RUN_FINISHED',\n runId,\n model,\n timestamp,\n finishReason: 'stop',\n usage: {\n promptTokens: event.usage.input_tokens || 0,\n completionTokens: event.usage.output_tokens || 0,\n totalTokens:\n (event.usage.input_tokens || 0) +\n (event.usage.output_tokens || 0),\n },\n }\n }\n }\n }\n }\n }\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\n\n yield {\n type: 'RUN_ERROR',\n runId,\n model,\n timestamp,\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n },\n }\n }\n }\n}\n\n/**\n * Creates an Anthropic chat adapter with explicit API key.\n * Type resolution happens here at the call site.\n */\nexport function createAnthropicChat<\n TModel extends (typeof ANTHROPIC_MODELS)[number],\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<AnthropicTextConfig, 'apiKey'>,\n): AnthropicTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>\n> {\n return new AnthropicTextAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates an Anthropic text adapter with automatic API key detection.\n * Type resolution happens here at the call site.\n */\nexport function anthropicText<TModel extends (typeof ANTHROPIC_MODELS)[number]>(\n model: TModel,\n config?: Omit<AnthropicTextConfig, 'apiKey'>,\n): AnthropicTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>\n> {\n const apiKey = getAnthropicApiKeyFromEnv()\n return createAnthropicChat(model, apiKey, config)\n}\n"],"names":[],"mappings":";;;;AAgGO,MAAM,6BAKH,gBAKR;AAAA,EAMA,YAAY,QAA6B,OA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rC;AAAA,IAEJ;AAAA,EACF;AACF;AAMO,SAAS,oBAGd,OACA,QACA,QAKA;AACA,SAAO,IAAI,qBAAqB,EAAE,QAAQ,GAAG,OAAA,GAAU,KAAK;AAC9D;AAMO,SAAS,cACd,OACA,QAKA;AACA,QAAM,SAAS,0BAAA;AACf,SAAO,oBAAoB,OAAO,QAAQ,MAAM;AAClD;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai-anthropic",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Anthropic Claude adapter for TanStack AI",
5
5
  "author": "",
6
6
  "license": "MIT",
@@ -32,12 +32,14 @@
32
32
  "dependencies": {
33
33
  "@anthropic-ai/sdk": "^0.71.0"
34
34
  },
35
- "devDependencies": {
36
- "@vitest/coverage-v8": "4.0.14"
37
- },
38
35
  "peerDependencies": {
39
36
  "zod": "^4.0.0",
40
- "@tanstack/ai": "^0.2.0"
37
+ "@tanstack/ai": "^0.3.0"
38
+ },
39
+ "devDependencies": {
40
+ "@vitest/coverage-v8": "4.0.14",
41
+ "zod": "^4.2.0",
42
+ "@tanstack/ai": "0.3.0"
41
43
  },
42
44
  "scripts": {
43
45
  "build": "vite build",
@@ -104,20 +104,19 @@ export class AnthropicSummarizeAdapter<
104
104
  const delta = event.delta.text
105
105
  accumulatedContent += delta
106
106
  yield {
107
- type: 'content',
108
- id,
107
+ type: 'TEXT_MESSAGE_CONTENT',
108
+ messageId: id,
109
109
  model,
110
110
  timestamp: Date.now(),
111
111
  delta,
112
112
  content: accumulatedContent,
113
- role: 'assistant',
114
113
  }
115
114
  }
116
115
  } else if (event.type === 'message_delta') {
117
116
  outputTokens = event.usage.output_tokens
118
117
  yield {
119
- type: 'done',
120
- id,
118
+ type: 'RUN_FINISHED',
119
+ runId: id,
121
120
  model,
122
121
  timestamp: Date.now(),
123
122
  finishReason: event.delta.stop_reason as
@@ -135,8 +135,7 @@ export class AnthropicTextAdapter<
135
135
  } catch (error: unknown) {
136
136
  const err = error as Error & { status?: number; code?: string }
137
137
  yield {
138
- type: 'error',
139
- id: generateId(this.name),
138
+ type: 'RUN_ERROR',
140
139
  model: options.model,
141
140
  timestamp: Date.now(),
142
141
  error: {
@@ -460,12 +459,30 @@ export class AnthropicTextAdapter<
460
459
  const timestamp = Date.now()
461
460
  const toolCallsMap = new Map<
462
461
  number,
463
- { id: string; name: string; input: string }
462
+ { id: string; name: string; input: string; started: boolean }
464
463
  >()
465
464
  let currentToolIndex = -1
466
465
 
466
+ // AG-UI lifecycle tracking
467
+ const runId = genId()
468
+ const messageId = genId()
469
+ let stepId: string | null = null
470
+ let hasEmittedRunStarted = false
471
+ let hasEmittedTextMessageStart = false
472
+
467
473
  try {
468
474
  for await (const event of stream) {
475
+ // Emit RUN_STARTED on first event
476
+ if (!hasEmittedRunStarted) {
477
+ hasEmittedRunStarted = true
478
+ yield {
479
+ type: 'RUN_STARTED',
480
+ runId,
481
+ model,
482
+ timestamp,
483
+ }
484
+ }
485
+
469
486
  if (event.type === 'content_block_start') {
470
487
  if (event.content_block.type === 'tool_use') {
471
488
  currentToolIndex++
@@ -473,30 +490,51 @@ export class AnthropicTextAdapter<
473
490
  id: event.content_block.id,
474
491
  name: event.content_block.name,
475
492
  input: '',
493
+ started: false,
476
494
  })
477
495
  } else if (event.content_block.type === 'thinking') {
478
496
  accumulatedThinking = ''
497
+ // Emit STEP_STARTED for thinking
498
+ stepId = genId()
499
+ yield {
500
+ type: 'STEP_STARTED',
501
+ stepId,
502
+ model,
503
+ timestamp,
504
+ stepType: 'thinking',
505
+ }
479
506
  }
480
507
  } else if (event.type === 'content_block_delta') {
481
508
  if (event.delta.type === 'text_delta') {
509
+ // Emit TEXT_MESSAGE_START on first text content
510
+ if (!hasEmittedTextMessageStart) {
511
+ hasEmittedTextMessageStart = true
512
+ yield {
513
+ type: 'TEXT_MESSAGE_START',
514
+ messageId,
515
+ model,
516
+ timestamp,
517
+ role: 'assistant',
518
+ }
519
+ }
520
+
482
521
  const delta = event.delta.text
483
522
  accumulatedContent += delta
484
523
  yield {
485
- type: 'content',
486
- id: genId(),
487
- model: model,
524
+ type: 'TEXT_MESSAGE_CONTENT',
525
+ messageId,
526
+ model,
488
527
  timestamp,
489
528
  delta,
490
529
  content: accumulatedContent,
491
- role: 'assistant',
492
530
  }
493
531
  } else if (event.delta.type === 'thinking_delta') {
494
532
  const delta = event.delta.thinking
495
533
  accumulatedThinking += delta
496
534
  yield {
497
- type: 'thinking',
498
- id: genId(),
499
- model: model,
535
+ type: 'STEP_FINISHED',
536
+ stepId: stepId || genId(),
537
+ model,
500
538
  timestamp,
501
539
  delta,
502
540
  content: accumulatedThinking,
@@ -504,49 +542,79 @@ export class AnthropicTextAdapter<
504
542
  } else if (event.delta.type === 'input_json_delta') {
505
543
  const existing = toolCallsMap.get(currentToolIndex)
506
544
  if (existing) {
545
+ // Emit TOOL_CALL_START on first args delta
546
+ if (!existing.started) {
547
+ existing.started = true
548
+ yield {
549
+ type: 'TOOL_CALL_START',
550
+ toolCallId: existing.id,
551
+ toolName: existing.name,
552
+ model,
553
+ timestamp,
554
+ index: currentToolIndex,
555
+ }
556
+ }
557
+
507
558
  existing.input += event.delta.partial_json
508
559
 
509
560
  yield {
510
- type: 'tool_call',
511
- id: genId(),
512
- model: model,
561
+ type: 'TOOL_CALL_ARGS',
562
+ toolCallId: existing.id,
563
+ model,
513
564
  timestamp,
514
- toolCall: {
515
- id: existing.id,
516
- type: 'function',
517
- function: {
518
- name: existing.name,
519
- arguments: event.delta.partial_json,
520
- },
521
- },
522
- index: currentToolIndex,
565
+ delta: event.delta.partial_json,
566
+ args: existing.input,
523
567
  }
524
568
  }
525
569
  }
526
570
  } else if (event.type === 'content_block_stop') {
527
571
  const existing = toolCallsMap.get(currentToolIndex)
528
- if (existing && existing.input === '') {
572
+ if (existing) {
573
+ // If tool call wasn't started yet (no args), start it now
574
+ if (!existing.started) {
575
+ existing.started = true
576
+ yield {
577
+ type: 'TOOL_CALL_START',
578
+ toolCallId: existing.id,
579
+ toolName: existing.name,
580
+ model,
581
+ timestamp,
582
+ index: currentToolIndex,
583
+ }
584
+ }
585
+
586
+ // Emit TOOL_CALL_END
587
+ let parsedInput: unknown = {}
588
+ try {
589
+ parsedInput = existing.input ? JSON.parse(existing.input) : {}
590
+ } catch {
591
+ parsedInput = {}
592
+ }
593
+
594
+ yield {
595
+ type: 'TOOL_CALL_END',
596
+ toolCallId: existing.id,
597
+ toolName: existing.name,
598
+ model,
599
+ timestamp,
600
+ input: parsedInput,
601
+ }
602
+ }
603
+
604
+ // Emit TEXT_MESSAGE_END if we had text content
605
+ if (hasEmittedTextMessageStart && accumulatedContent) {
529
606
  yield {
530
- type: 'tool_call',
531
- id: genId(),
532
- model: model,
607
+ type: 'TEXT_MESSAGE_END',
608
+ messageId,
609
+ model,
533
610
  timestamp,
534
- toolCall: {
535
- id: existing.id,
536
- type: 'function',
537
- function: {
538
- name: existing.name,
539
- arguments: '{}',
540
- },
541
- },
542
- index: currentToolIndex,
543
611
  }
544
612
  }
545
613
  } else if (event.type === 'message_stop') {
546
614
  yield {
547
- type: 'done',
548
- id: genId(),
549
- model: model,
615
+ type: 'RUN_FINISHED',
616
+ runId,
617
+ model,
550
618
  timestamp,
551
619
  finishReason: 'stop',
552
620
  }
@@ -555,9 +623,9 @@ export class AnthropicTextAdapter<
555
623
  switch (event.delta.stop_reason) {
556
624
  case 'tool_use': {
557
625
  yield {
558
- type: 'done',
559
- id: genId(),
560
- model: model,
626
+ type: 'RUN_FINISHED',
627
+ runId,
628
+ model,
561
629
  timestamp,
562
630
  finishReason: 'tool_calls',
563
631
  usage: {
@@ -572,9 +640,9 @@ export class AnthropicTextAdapter<
572
640
  }
573
641
  case 'max_tokens': {
574
642
  yield {
575
- type: 'error',
576
- id: genId(),
577
- model: model,
643
+ type: 'RUN_ERROR',
644
+ runId,
645
+ model,
578
646
  timestamp,
579
647
  error: {
580
648
  message:
@@ -586,9 +654,9 @@ export class AnthropicTextAdapter<
586
654
  }
587
655
  default: {
588
656
  yield {
589
- type: 'done',
590
- id: genId(),
591
- model: model,
657
+ type: 'RUN_FINISHED',
658
+ runId,
659
+ model,
592
660
  timestamp,
593
661
  finishReason: 'stop',
594
662
  usage: {
@@ -608,9 +676,9 @@ export class AnthropicTextAdapter<
608
676
  const err = error as Error & { status?: number; code?: string }
609
677
 
610
678
  yield {
611
- type: 'error',
612
- id: genId(),
613
- model: model,
679
+ type: 'RUN_ERROR',
680
+ runId,
681
+ model,
614
682
  timestamp,
615
683
  error: {
616
684
  message: err.message || 'Unknown error occurred',