@positronic/client-anthropic 0.0.55 → 0.0.57

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/src/index.js CHANGED
@@ -275,6 +275,22 @@ export var AnthropicClient = /*#__PURE__*/ function() {
275
275
  });
276
276
  }
277
277
  _create_class(AnthropicClient, [
278
+ {
279
+ key: "createToolResultMessage",
280
+ value: function createToolResultMessage(toolCallId, toolName, result) {
281
+ // Anthropic uses a "user" message with tool_result content blocks
282
+ return {
283
+ role: 'user',
284
+ content: [
285
+ {
286
+ type: 'tool_result',
287
+ tool_use_id: toolCallId,
288
+ content: typeof result === 'string' ? result : JSON.stringify(result)
289
+ }
290
+ ]
291
+ };
292
+ }
293
+ },
278
294
  {
279
295
  key: "generateObject",
280
296
  value: function generateObject(params) {
@@ -345,109 +361,92 @@ export var AnthropicClient = /*#__PURE__*/ function() {
345
361
  key: "generateText",
346
362
  value: function generateText(params) {
347
363
  return _async_to_generator(function() {
348
- var system, messages, tools, anthropicMessages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, anthropicTools, response, text, toolCalls, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, block;
364
+ var system, messages, responseMessages, tools, anthropicMessages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, msg, contentBlocks, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, tc, anthropicTools, response, text, toolCalls, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, block, assistantMessage, updatedMessages;
349
365
  return _ts_generator(this, function(_state) {
350
366
  switch(_state.label){
351
367
  case 0:
352
- system = params.system, messages = params.messages, tools = params.tools;
353
- // Convert ToolMessage[] to Anthropic message format
354
- anthropicMessages = [];
355
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
356
- try {
357
- _loop = function() {
358
- var msg = _step.value;
359
- if (msg.role === 'user') {
360
- anthropicMessages.push({
361
- role: 'user',
362
- content: msg.content
363
- });
364
- } else if (msg.role === 'assistant') {
365
- // Check if the last message is also assistant - need to merge content blocks
366
- var lastMsg = anthropicMessages[anthropicMessages.length - 1];
367
- if (lastMsg && lastMsg.role === 'assistant') {
368
- // Merge text content into existing assistant message
369
- if (typeof lastMsg.content === 'string') {
370
- lastMsg.content = [
371
- {
372
- type: 'text',
373
- text: lastMsg.content
374
- },
375
- {
376
- type: 'text',
377
- text: msg.content
378
- }
379
- ];
380
- } else if (Array.isArray(lastMsg.content)) {
381
- lastMsg.content.push({
368
+ system = params.system, messages = params.messages, responseMessages = params.responseMessages, tools = params.tools;
369
+ if (responseMessages && responseMessages.length > 0) {
370
+ // Use the native Anthropic messages directly (preserves conversation state)
371
+ anthropicMessages = responseMessages;
372
+ } else {
373
+ // First call - convert our ToolMessage format to Anthropic format
374
+ anthropicMessages = [];
375
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
376
+ try {
377
+ for(_iterator = messages[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
378
+ msg = _step.value;
379
+ if (msg.role === 'user') {
380
+ anthropicMessages.push({
381
+ role: 'user',
382
+ content: msg.content
383
+ });
384
+ } else if (msg.role === 'assistant') {
385
+ contentBlocks = [];
386
+ if (msg.content) {
387
+ contentBlocks.push({
382
388
  type: 'text',
383
389
  text: msg.content
384
390
  });
385
391
  }
386
- } else {
387
- anthropicMessages.push({
388
- role: 'assistant',
389
- content: msg.content
390
- });
391
- }
392
- } else if (msg.role === 'tool') {
393
- // Tool results need to follow an assistant message with tool_use
394
- // Find or create the assistant message that had the tool call
395
- var lastAssistant = anthropicMessages[anthropicMessages.length - 1];
396
- if (lastAssistant && lastAssistant.role === 'assistant') {
397
- // Add tool_use block to assistant message if not present
398
- if (typeof lastAssistant.content === 'string') {
399
- lastAssistant.content = [
400
- {
401
- type: 'text',
402
- text: lastAssistant.content
403
- },
404
- {
405
- type: 'tool_use',
406
- id: msg.toolCallId,
407
- name: msg.toolName,
408
- input: {}
392
+ if (msg.toolCalls && msg.toolCalls.length > 0) {
393
+ _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
394
+ try {
395
+ for(_iterator1 = msg.toolCalls[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
396
+ tc = _step1.value;
397
+ contentBlocks.push({
398
+ type: 'tool_use',
399
+ id: tc.toolCallId,
400
+ name: tc.toolName,
401
+ input: tc.args
402
+ });
403
+ }
404
+ } catch (err) {
405
+ _didIteratorError1 = true;
406
+ _iteratorError1 = err;
407
+ } finally{
408
+ try {
409
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
410
+ _iterator1.return();
411
+ }
412
+ } finally{
413
+ if (_didIteratorError1) {
414
+ throw _iteratorError1;
415
+ }
409
416
  }
410
- ];
411
- } else if (Array.isArray(lastAssistant.content)) {
412
- // Check if tool_use already exists
413
- var hasToolUse = lastAssistant.content.some(function(block) {
414
- return block.type === 'tool_use' && block.id === msg.toolCallId;
415
- });
416
- if (!hasToolUse) {
417
- lastAssistant.content.push({
418
- type: 'tool_use',
419
- id: msg.toolCallId,
420
- name: msg.toolName,
421
- input: {}
422
- });
423
417
  }
424
418
  }
419
+ if (contentBlocks.length > 0) {
420
+ anthropicMessages.push({
421
+ role: 'assistant',
422
+ content: contentBlocks
423
+ });
424
+ }
425
+ } else if (msg.role === 'tool') {
426
+ anthropicMessages.push({
427
+ role: 'user',
428
+ content: [
429
+ {
430
+ type: 'tool_result',
431
+ tool_use_id: msg.toolCallId,
432
+ content: msg.content
433
+ }
434
+ ]
435
+ });
425
436
  }
426
- // Add user message with tool_result
427
- anthropicMessages.push({
428
- role: 'user',
429
- content: [
430
- {
431
- type: 'tool_result',
432
- tool_use_id: msg.toolCallId,
433
- content: msg.content
434
- }
435
- ]
436
- });
437
- }
438
- };
439
- for(_iterator = messages[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
440
- } catch (err) {
441
- _didIteratorError = true;
442
- _iteratorError = err;
443
- } finally{
444
- try {
445
- if (!_iteratorNormalCompletion && _iterator.return != null) {
446
- _iterator.return();
447
437
  }
438
+ } catch (err) {
439
+ _didIteratorError = true;
440
+ _iteratorError = err;
448
441
  } finally{
449
- if (_didIteratorError) {
450
- throw _iteratorError;
442
+ try {
443
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
444
+ _iterator.return();
445
+ }
446
+ } finally{
447
+ if (_didIteratorError) {
448
+ throw _iteratorError;
449
+ }
451
450
  }
452
451
  }
453
452
  }
@@ -473,10 +472,10 @@ export var AnthropicClient = /*#__PURE__*/ function() {
473
472
  case 1:
474
473
  response = _state.sent();
475
474
  toolCalls = [];
476
- _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
475
+ _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
477
476
  try {
478
- for(_iterator1 = response.content[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
479
- block = _step1.value;
477
+ for(_iterator2 = response.content[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true){
478
+ block = _step2.value;
480
479
  if (block.type === 'text') {
481
480
  text = (text || '') + block.text;
482
481
  } else if (block.type === 'tool_use') {
@@ -488,19 +487,28 @@ export var AnthropicClient = /*#__PURE__*/ function() {
488
487
  }
489
488
  }
490
489
  } catch (err) {
491
- _didIteratorError1 = true;
492
- _iteratorError1 = err;
490
+ _didIteratorError2 = true;
491
+ _iteratorError2 = err;
493
492
  } finally{
494
493
  try {
495
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
496
- _iterator1.return();
494
+ if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
495
+ _iterator2.return();
497
496
  }
498
497
  } finally{
499
- if (_didIteratorError1) {
500
- throw _iteratorError1;
498
+ if (_didIteratorError2) {
499
+ throw _iteratorError2;
501
500
  }
502
501
  }
503
502
  }
503
+ // Build assistant message from response for the conversation history
504
+ assistantMessage = {
505
+ role: 'assistant',
506
+ content: response.content
507
+ };
508
+ // Return updated conversation (input messages + new assistant response)
509
+ updatedMessages = _to_consumable_array(anthropicMessages).concat([
510
+ assistantMessage
511
+ ]);
504
512
  return [
505
513
  2,
506
514
  {
@@ -508,7 +516,8 @@ export var AnthropicClient = /*#__PURE__*/ function() {
508
516
  toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
509
517
  usage: {
510
518
  totalTokens: response.usage.input_tokens + response.usage.output_tokens
511
- }
519
+ },
520
+ responseMessages: updatedMessages
512
521
  }
513
522
  ];
514
523
  }
@@ -1,9 +1,10 @@
1
- import type { ObjectGenerator, ToolMessage } from '@positronic/core';
1
+ import type { ObjectGenerator, ToolMessage, ResponseMessage } from '@positronic/core';
2
2
  import { z } from 'zod';
3
3
  export declare class AnthropicClient implements ObjectGenerator {
4
4
  private client;
5
5
  private anthropicSdk;
6
6
  constructor();
7
+ createToolResultMessage(toolCallId: string, toolName: string, result: unknown): ResponseMessage;
7
8
  generateObject<T extends z.AnyZodObject>(params: {
8
9
  schema: T;
9
10
  schemaName: string;
@@ -18,6 +19,7 @@ export declare class AnthropicClient implements ObjectGenerator {
18
19
  generateText(params: {
19
20
  system?: string;
20
21
  messages: ToolMessage[];
22
+ responseMessages?: ResponseMessage[];
21
23
  tools: Record<string, {
22
24
  description: string;
23
25
  inputSchema: z.ZodSchema;
@@ -32,6 +34,7 @@ export declare class AnthropicClient implements ObjectGenerator {
32
34
  usage: {
33
35
  totalTokens: number;
34
36
  };
37
+ responseMessages: ResponseMessage[];
35
38
  }>;
36
39
  streamText(params: {
37
40
  system?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[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C;KACnB,GAAG,OAAO,CAAC;QACV,SAAS,EAAE,KAAK,CAAC;YACf,UAAU,EAAE,MAAM,CAAC;YACnB,QAAQ,EAAE,MAAM,CAAC;YACjB,IAAI,EAAE,OAAO,CAAC;YACd,MAAM,EAAE,OAAO,CAAC;SACjB,CAAC,CAAC;QACH,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,KAAK,EAAE;YAAE,WAAW,EAAE,MAAM,CAAA;SAAE,CAAC;KAChC,CAAC;CAyHH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@positronic/client-anthropic",
3
- "version": "0.0.55",
3
+ "version": "0.0.57",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@anthropic-ai/sdk": "^0.52.0",
24
- "@positronic/core": "^0.0.55",
24
+ "@positronic/core": "^0.0.57",
25
25
  "@instructor-ai/instructor": "^1.5.0",
26
26
  "llm-polyglot": "^2.4.0",
27
27
  "dotenv": "^16.4.7",