@positronic/client-anthropic 0.0.36 → 0.0.38
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 +214 -0
- package/dist/types/index.d.ts +20 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +5 -3
package/dist/src/index.js
CHANGED
|
@@ -3,6 +3,9 @@ function _array_like_to_array(arr, len) {
|
|
|
3
3
|
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
4
|
return arr2;
|
|
5
5
|
}
|
|
6
|
+
function _array_with_holes(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return arr;
|
|
8
|
+
}
|
|
6
9
|
function _array_without_holes(arr) {
|
|
7
10
|
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
8
11
|
}
|
|
@@ -70,6 +73,33 @@ function _define_property(obj, key, value) {
|
|
|
70
73
|
function _iterable_to_array(iter) {
|
|
71
74
|
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
72
75
|
}
|
|
76
|
+
function _iterable_to_array_limit(arr, i) {
|
|
77
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
78
|
+
if (_i == null) return;
|
|
79
|
+
var _arr = [];
|
|
80
|
+
var _n = true;
|
|
81
|
+
var _d = false;
|
|
82
|
+
var _s, _e;
|
|
83
|
+
try {
|
|
84
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
85
|
+
_arr.push(_s.value);
|
|
86
|
+
if (i && _arr.length === i) break;
|
|
87
|
+
}
|
|
88
|
+
} catch (err) {
|
|
89
|
+
_d = true;
|
|
90
|
+
_e = err;
|
|
91
|
+
} finally{
|
|
92
|
+
try {
|
|
93
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
94
|
+
} finally{
|
|
95
|
+
if (_d) throw _e;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return _arr;
|
|
99
|
+
}
|
|
100
|
+
function _non_iterable_rest() {
|
|
101
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
102
|
+
}
|
|
73
103
|
function _non_iterable_spread() {
|
|
74
104
|
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
75
105
|
}
|
|
@@ -112,6 +142,9 @@ function _object_spread_props(target, source) {
|
|
|
112
142
|
}
|
|
113
143
|
return target;
|
|
114
144
|
}
|
|
145
|
+
function _sliced_to_array(arr, i) {
|
|
146
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
147
|
+
}
|
|
115
148
|
function _to_consumable_array(arr) {
|
|
116
149
|
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
117
150
|
}
|
|
@@ -216,6 +249,8 @@ function _ts_generator(thisArg, body) {
|
|
|
216
249
|
}
|
|
217
250
|
import Instructor from '@instructor-ai/instructor';
|
|
218
251
|
import { createLLMClient } from 'llm-polyglot';
|
|
252
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
253
|
+
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
219
254
|
import { config } from 'dotenv';
|
|
220
255
|
config();
|
|
221
256
|
var anthropic = createLLMClient({
|
|
@@ -230,10 +265,14 @@ export var AnthropicClient = /*#__PURE__*/ function() {
|
|
|
230
265
|
function AnthropicClient() {
|
|
231
266
|
_class_call_check(this, AnthropicClient);
|
|
232
267
|
_define_property(this, "client", void 0);
|
|
268
|
+
_define_property(this, "anthropicSdk", void 0);
|
|
233
269
|
this.client = Instructor({
|
|
234
270
|
client: anthropic,
|
|
235
271
|
mode: 'TOOLS'
|
|
236
272
|
});
|
|
273
|
+
this.anthropicSdk = new Anthropic({
|
|
274
|
+
apiKey: process.env.ANTHROPIC_API_KEY
|
|
275
|
+
});
|
|
237
276
|
}
|
|
238
277
|
_create_class(AnthropicClient, [
|
|
239
278
|
{
|
|
@@ -301,6 +340,181 @@ export var AnthropicClient = /*#__PURE__*/ function() {
|
|
|
301
340
|
});
|
|
302
341
|
}).call(this);
|
|
303
342
|
}
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
key: "generateText",
|
|
346
|
+
value: function generateText(params) {
|
|
347
|
+
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;
|
|
349
|
+
return _ts_generator(this, function(_state) {
|
|
350
|
+
switch(_state.label){
|
|
351
|
+
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({
|
|
382
|
+
type: 'text',
|
|
383
|
+
text: msg.content
|
|
384
|
+
});
|
|
385
|
+
}
|
|
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: {}
|
|
409
|
+
}
|
|
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
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
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
|
+
}
|
|
448
|
+
} finally{
|
|
449
|
+
if (_didIteratorError) {
|
|
450
|
+
throw _iteratorError;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
// Convert tools to Anthropic format
|
|
455
|
+
anthropicTools = Object.entries(tools).map(function(param) {
|
|
456
|
+
var _param = _sliced_to_array(param, 2), name = _param[0], tool = _param[1];
|
|
457
|
+
return {
|
|
458
|
+
name: name,
|
|
459
|
+
description: tool.description,
|
|
460
|
+
input_schema: zodToJsonSchema(tool.inputSchema)
|
|
461
|
+
};
|
|
462
|
+
});
|
|
463
|
+
return [
|
|
464
|
+
4,
|
|
465
|
+
this.anthropicSdk.messages.create({
|
|
466
|
+
model: 'claude-sonnet-4-20250514',
|
|
467
|
+
max_tokens: 8192,
|
|
468
|
+
system: system,
|
|
469
|
+
messages: anthropicMessages,
|
|
470
|
+
tools: anthropicTools
|
|
471
|
+
})
|
|
472
|
+
];
|
|
473
|
+
case 1:
|
|
474
|
+
response = _state.sent();
|
|
475
|
+
toolCalls = [];
|
|
476
|
+
_iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
|
|
477
|
+
try {
|
|
478
|
+
for(_iterator1 = response.content[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
479
|
+
block = _step1.value;
|
|
480
|
+
if (block.type === 'text') {
|
|
481
|
+
text = (text || '') + block.text;
|
|
482
|
+
} else if (block.type === 'tool_use') {
|
|
483
|
+
toolCalls.push({
|
|
484
|
+
toolCallId: block.id,
|
|
485
|
+
toolName: block.name,
|
|
486
|
+
args: block.input
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
} catch (err) {
|
|
491
|
+
_didIteratorError1 = true;
|
|
492
|
+
_iteratorError1 = err;
|
|
493
|
+
} finally{
|
|
494
|
+
try {
|
|
495
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
496
|
+
_iterator1.return();
|
|
497
|
+
}
|
|
498
|
+
} finally{
|
|
499
|
+
if (_didIteratorError1) {
|
|
500
|
+
throw _iteratorError1;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return [
|
|
505
|
+
2,
|
|
506
|
+
{
|
|
507
|
+
text: text || undefined,
|
|
508
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
509
|
+
usage: {
|
|
510
|
+
totalTokens: response.usage.input_tokens + response.usage.output_tokens
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
];
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}).call(this);
|
|
517
|
+
}
|
|
304
518
|
}
|
|
305
519
|
]);
|
|
306
520
|
return AnthropicClient;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { ObjectGenerator } from '@positronic/core';
|
|
1
|
+
import type { ObjectGenerator, ToolMessage } from '@positronic/core';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
export declare class AnthropicClient implements ObjectGenerator {
|
|
4
4
|
private client;
|
|
5
|
+
private anthropicSdk;
|
|
5
6
|
constructor();
|
|
6
7
|
generateObject<T extends z.AnyZodObject>(params: {
|
|
7
8
|
schema: T;
|
|
@@ -14,5 +15,23 @@ export declare class AnthropicClient implements ObjectGenerator {
|
|
|
14
15
|
}[];
|
|
15
16
|
system?: string;
|
|
16
17
|
}): Promise<z.infer<T>>;
|
|
18
|
+
generateText(params: {
|
|
19
|
+
system?: string;
|
|
20
|
+
messages: ToolMessage[];
|
|
21
|
+
tools: Record<string, {
|
|
22
|
+
description: string;
|
|
23
|
+
inputSchema: z.ZodSchema;
|
|
24
|
+
}>;
|
|
25
|
+
}): Promise<{
|
|
26
|
+
text?: string;
|
|
27
|
+
toolCalls?: Array<{
|
|
28
|
+
toolCallId: string;
|
|
29
|
+
toolName: string;
|
|
30
|
+
args: unknown;
|
|
31
|
+
}>;
|
|
32
|
+
usage: {
|
|
33
|
+
totalTokens: number;
|
|
34
|
+
};
|
|
35
|
+
}>;
|
|
17
36
|
}
|
|
18
37
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAIrE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAexB,qBAAa,eAAgB,YAAW,eAAe;IACrD,OAAO,CAAC,MAAM,CAAqC;IACnD,OAAO,CAAC,YAAY,CAAY;;IAY1B,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,YAAY,EAAE,MAAM,EAAE;QACrD,MAAM,EAAE,CAAC,CAAC;QACV,UAAU,EAAE,MAAM,CAAC;QACnB,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE;YAAE,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,QAAQ,CAAC;YAAC,OAAO,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;QACxE,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAoCjB,YAAY,CAAC,MAAM,EAAE;QACzB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,WAAW,EAAE,CAAC;QACxB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE;YAAE,WAAW,EAAE,MAAM,CAAC;YAAC,WAAW,EAAE,CAAC,CAAC,SAAS,CAAA;SAAE,CAAC,CAAC;KAC1E,GAAG,OAAO,CAAC;QACV,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,SAAS,CAAC,EAAE,KAAK,CAAC;YAAE,UAAU,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,OAAO,CAAA;SAAE,CAAC,CAAC;QAC3E,KAAK,EAAE;YAAE,WAAW,EAAE,MAAM,CAAA;SAAE,CAAC;KAChC,CAAC;CA2GH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@positronic/client-anthropic",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.38",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -20,10 +20,12 @@
|
|
|
20
20
|
"clean": "rm -rf tsconfig.tsbuildinfo dist node_modules"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@
|
|
23
|
+
"@anthropic-ai/sdk": "^0.52.0",
|
|
24
|
+
"@positronic/core": "^0.0.38",
|
|
24
25
|
"@instructor-ai/instructor": "^1.5.0",
|
|
25
26
|
"llm-polyglot": "^2.4.0",
|
|
26
27
|
"dotenv": "^16.4.7",
|
|
27
|
-
"zod": "^3.24.1"
|
|
28
|
+
"zod": "^3.24.1",
|
|
29
|
+
"zod-to-json-schema": "^3.24.1"
|
|
28
30
|
}
|
|
29
31
|
}
|