triangle-utils 1.4.80 → 1.4.82

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.
@@ -1,6 +1,17 @@
1
1
  export declare class UtilsAnthropic {
2
2
  private readonly anthropic;
3
3
  constructor(anthropic_api_key: string | undefined);
4
+ claude_converse(model_id: string, messages: {
5
+ role: "user" | "assistant" | "system";
6
+ text: string;
7
+ }[], options?: {
8
+ system_prompt?: string;
9
+ max_tokens?: number;
10
+ print_usage?: boolean;
11
+ json_format?: Record<string, any>;
12
+ web?: boolean;
13
+ cache_period?: "5m" | "1h";
14
+ }): Promise<string | undefined>;
4
15
  claude_multi_query(model_id: string, prompts: string[], options?: {
5
16
  max_tokens?: number;
6
17
  print_usage?: boolean;
@@ -4,6 +4,48 @@ export class UtilsAnthropic {
4
4
  constructor(anthropic_api_key) {
5
5
  this.anthropic = new Anthropic({ apiKey: anthropic_api_key });
6
6
  }
7
+ async claude_converse(model_id, messages, options = {}) {
8
+ try {
9
+ const response = await this.anthropic.messages.create({
10
+ model: model_id,
11
+ messages: messages.map(message => ({
12
+ role: message.role,
13
+ content: [
14
+ {
15
+ type: "text",
16
+ text: message.text
17
+ }
18
+ ]
19
+ })),
20
+ system: options.system_prompt,
21
+ tools: options.web === true ? [{ name: "web_search", type: "web_search_20260209" }] : [],
22
+ max_tokens: options.max_tokens || 1000,
23
+ output_config: options.json_format !== undefined ? {
24
+ format: {
25
+ type: "json_schema",
26
+ schema: options.json_format
27
+ }
28
+ } : undefined,
29
+ cache_control: {
30
+ type: "ephemeral",
31
+ ttl: options.cache_period
32
+ }
33
+ });
34
+ const usage = response.usage;
35
+ if (options.print_usage !== undefined && options.print_usage) {
36
+ console.log(usage);
37
+ }
38
+ const text = response.content.filter(content => content.type === "text")[0].text;
39
+ return text;
40
+ }
41
+ catch (error) {
42
+ if (error instanceof Error) {
43
+ console.log(error.stack);
44
+ }
45
+ console.log("Failed to get from Anthropic.");
46
+ }
47
+ return undefined;
48
+ }
7
49
  async claude_multi_query(model_id, prompts, options = {}) {
8
50
  try {
9
51
  const response = await this.anthropic.messages.create({
@@ -58,7 +100,7 @@ export class UtilsAnthropic {
58
100
  },
59
101
  title: "Attachment",
60
102
  citations: { "enabled": false },
61
- cache_control: { "type": "ephemeral" }
103
+ cache_control: { type: "ephemeral" }
62
104
  } : undefined;
63
105
  if (attachment_content !== undefined) {
64
106
  message_contents.push(attachment_content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.80",
3
+ "version": "1.4.82",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
@@ -15,7 +15,7 @@
15
15
  "description": "",
16
16
  "type": "module",
17
17
  "dependencies": {
18
- "@anthropic-ai/sdk": "^0.94.0",
18
+ "@anthropic-ai/sdk": "^0.109.1",
19
19
  "@aws-sdk/client-bedrock-runtime": "^3.953.0",
20
20
  "@aws-sdk/client-cognito-identity-provider": "^3.1004.0",
21
21
  "@aws-sdk/client-dynamodb": "^3.953.0",
@@ -8,6 +8,61 @@ export class UtilsAnthropic {
8
8
  this.anthropic = new Anthropic({ apiKey : anthropic_api_key })
9
9
  }
10
10
 
11
+ async claude_converse(
12
+ model_id : string,
13
+ messages : { role : "user" | "assistant" | "system", text : string }[],
14
+ options : {
15
+ system_prompt? : string,
16
+ max_tokens? : number,
17
+ print_usage? : boolean,
18
+ json_format? : Record<string, any>,
19
+ web? : boolean,
20
+ cache_period? : "5m" | "1h"
21
+ } = {}
22
+ ) : Promise<string | undefined> {
23
+ try {
24
+
25
+ const response = await this.anthropic.messages.create({
26
+ model : model_id,
27
+ messages : messages.map(message => ({
28
+ role : message.role,
29
+ content : [
30
+ {
31
+ type : "text",
32
+ text : message.text
33
+ }
34
+ ]
35
+ })),
36
+ system : options.system_prompt,
37
+ tools : options.web === true ? [{ name : "web_search", type : "web_search_20260209" }] : [],
38
+ max_tokens : options.max_tokens || 1000,
39
+ output_config : options.json_format !== undefined ? {
40
+ format : {
41
+ type : "json_schema",
42
+ schema : options.json_format
43
+ }
44
+ } : undefined,
45
+ cache_control : {
46
+ type : "ephemeral",
47
+ ttl : options.cache_period
48
+ }
49
+ })
50
+
51
+ const usage = response.usage
52
+ if (options.print_usage !== undefined && options.print_usage) {
53
+ console.log(usage)
54
+ }
55
+ const text = response.content.filter(content => content.type === "text")[0].text
56
+ return text
57
+ } catch (error) {
58
+ if (error instanceof Error) {
59
+ console.log(error.stack)
60
+ }
61
+ console.log("Failed to get from Anthropic.")
62
+ }
63
+ return undefined
64
+ }
65
+
11
66
  async claude_multi_query(
12
67
  model_id : string,
13
68
  prompts : string[],
@@ -83,7 +138,7 @@ export class UtilsAnthropic {
83
138
  },
84
139
  title: "Attachment",
85
140
  citations: { "enabled": false },
86
- cache_control: {"type": "ephemeral"}
141
+ cache_control: {type: "ephemeral"}
87
142
  } : undefined
88
143
 
89
144
  if (attachment_content !== undefined) {