triangle-utils 1.4.53 → 1.4.55

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.
@@ -4,5 +4,6 @@ export declare class UtilsAnthropic {
4
4
  claude_query(model_id: string, prompt: string, attachment_pdf?: string, options?: {
5
5
  max_tokens?: number;
6
6
  print_usage?: boolean;
7
+ json_format?: Record<string, any>;
7
8
  }): Promise<string | undefined>;
8
9
  }
@@ -32,7 +32,13 @@ export class UtilsAnthropic {
32
32
  role: "user",
33
33
  content: message_contents
34
34
  }],
35
- max_tokens: options.max_tokens || 1000
35
+ max_tokens: options.max_tokens || 1000,
36
+ output_config: options.json_format !== undefined ? {
37
+ format: {
38
+ type: "json_schema",
39
+ schema: options.json_format
40
+ }
41
+ } : undefined
36
42
  });
37
43
  const usage = response.usage;
38
44
  if (options.print_usage !== undefined && options.print_usage) {
@@ -6,4 +6,5 @@ export declare class UtilsBee {
6
6
  get(url: string, params?: Record<string, string | number | boolean>): Promise<string | Error | undefined>;
7
7
  google_search(query: string, news?: boolean): Promise<Record<string, any>[] | undefined>;
8
8
  youtube_search(query: string, options?: {}): Promise<Record<string, any>[] | undefined>;
9
+ youtube_get_subtitles(video_id: string): Promise<Record<string, any> | undefined>;
9
10
  }
@@ -109,4 +109,31 @@ export class UtilsBee {
109
109
  console.log("Failed to YouTube Search thrice, quitting.");
110
110
  return undefined;
111
111
  }
112
+ async youtube_get_subtitles(video_id) {
113
+ if (this.bee === undefined) {
114
+ return undefined;
115
+ }
116
+ for (let i = 0; i < 3; i++) {
117
+ try {
118
+ const url = "https://app.scrapingbee.com/api/v1/youtube/subtitles?video_id=" + video_id + "&api_key=" + this.bee.api_key;
119
+ const response = await fetch(url);
120
+ if (response.status !== 200) {
121
+ console.log("Failed to get YouTube Subtitles:", response.status);
122
+ continue;
123
+ }
124
+ const data = await response.json();
125
+ const subtitles = data.subtitles;
126
+ if (subtitles === undefined) {
127
+ console.log("Failed to YouTube Search.");
128
+ continue;
129
+ }
130
+ return subtitles;
131
+ }
132
+ catch {
133
+ console.log("Failed to YouTube Search.");
134
+ }
135
+ }
136
+ console.log("Failed to YouTube Search thrice, quitting.");
137
+ return undefined;
138
+ }
112
139
  }
@@ -6,8 +6,8 @@ interface Encryption {
6
6
  export declare class UtilsMisc {
7
7
  readonly config: TriangleUtilsConfig;
8
8
  constructor(config: TriangleUtilsConfig);
9
- safe_run(f: () => Promise<any>): Promise<void>;
10
- iterate<T>(inputs: T[], f: (input: T, iterator_id: number, index: number) => Promise<any>, num_iterators?: number, print_option?: number | boolean): Promise<void>;
9
+ safe_run<T>(f: () => Promise<T>): Promise<T | undefined>;
10
+ iterate<T, U>(inputs: T[], f: (input: T, iterator_id: number, index: number) => Promise<U>, num_iterators?: number, print_option?: number | boolean): Promise<(U | undefined)[]>;
11
11
  static wait(duration: number): Promise<unknown>;
12
12
  static get_current_time(): string;
13
13
  static get_current_milliseconds(): number;
@@ -6,11 +6,11 @@ export class UtilsMisc {
6
6
  }
7
7
  async safe_run(f) {
8
8
  try {
9
- await f();
9
+ return await f();
10
10
  }
11
11
  catch (error) {
12
12
  if (!(error instanceof Error)) {
13
- return;
13
+ return undefined;
14
14
  }
15
15
  if (error.stack !== undefined) {
16
16
  console.log(error.stack.toString());
@@ -19,11 +19,13 @@ export class UtilsMisc {
19
19
  console.log(error.toString());
20
20
  }
21
21
  }
22
+ return undefined;
22
23
  }
23
24
  async iterate(inputs, f, num_iterators = 1, print_option) {
24
25
  const print_freq = typeof print_option === "boolean" ? (print_option === true ? 1 : 0) : print_option;
25
26
  let index = 0;
26
27
  let iterators = [];
28
+ const outputs = []; //new Array(inputs.length).fill(undefined)
27
29
  for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
28
30
  iterators.push((async () => {
29
31
  while (index < inputs.length) {
@@ -31,11 +33,12 @@ export class UtilsMisc {
31
33
  if (print_freq !== undefined && ((index - 1) % print_freq === 0)) {
32
34
  console.log(iterator_id + ":" + (index - 1) + "/" + inputs.length);
33
35
  }
34
- await this.safe_run(() => f(inputs[index - 1], iterator_id, index));
36
+ const output = await this.safe_run(() => f(inputs[index - 1], iterator_id, index));
37
+ outputs.push(output);
35
38
  }
36
39
  })());
37
40
  }
38
- await Promise.all(iterators);
41
+ return outputs;
39
42
  }
40
43
  static async wait(duration) {
41
44
  return new Promise(resolve => setTimeout(resolve, duration));
package/dist/src/f.js CHANGED
@@ -17,7 +17,7 @@ console.log(config);
17
17
  const utils = new TriangleUtils(config);
18
18
  // const foods = await utils.dynamodb.query("triage_docket_documents:register_document_id", { register_document_id : "E6-17065" })
19
19
  // console.log(foods)
20
- const text = await utils.anthropic.claude_query("claude-opus-4-6", "hi", undefined, { print_usage: true });
20
+ const text = await utils.bee.get("https://www.doyourjobs.org", { return_page_text: true });
21
21
  console.log(text);
22
22
  // const text = await utils.bedrock.claude_invoke("global.anthropic.claude-opus-4-6-v1", "hi", undefined, { print_usage : true })
23
23
  // console.log(text)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.53",
3
+ "version": "1.4.55",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
@@ -14,7 +14,8 @@ export class UtilsAnthropic {
14
14
  attachment_pdf? : string,
15
15
  options : {
16
16
  max_tokens? : number,
17
- print_usage? : boolean
17
+ print_usage? : boolean,
18
+ json_format? : Record<string, any>
18
19
  } = {}
19
20
  ) : Promise<string | undefined> {
20
21
  try {
@@ -46,7 +47,13 @@ export class UtilsAnthropic {
46
47
  role : "user",
47
48
  content : message_contents
48
49
  }],
49
- max_tokens : options.max_tokens || 1000
50
+ max_tokens : options.max_tokens || 1000,
51
+ output_config : options.json_format !== undefined ? {
52
+ format : {
53
+ type : "json_schema",
54
+ schema : options.json_format
55
+ }
56
+ } : undefined
50
57
  })
51
58
 
52
59
  const usage = response.usage
package/src/UtilsBee.ts CHANGED
@@ -118,4 +118,32 @@ export class UtilsBee {
118
118
  console.log("Failed to YouTube Search thrice, quitting.")
119
119
  return undefined
120
120
  }
121
+
122
+ async youtube_get_subtitles(video_id : string) : Promise<Record<string, any> | undefined> {
123
+ if (this.bee === undefined) {
124
+ return undefined
125
+ }
126
+ for (let i = 0; i < 3; i++) {
127
+ try {
128
+ const url = "https://app.scrapingbee.com/api/v1/youtube/subtitles?video_id=" + video_id + "&api_key=" + this.bee.api_key
129
+ const response = await fetch(url)
130
+ if (response.status !== 200) {
131
+ console.log("Failed to get YouTube Subtitles:", response.status)
132
+ continue
133
+ }
134
+ const data : Record<string, any> = await response.json()
135
+ const subtitles = data.subtitles
136
+
137
+ if (subtitles === undefined) {
138
+ console.log("Failed to YouTube Search.")
139
+ continue
140
+ }
141
+ return subtitles
142
+ } catch {
143
+ console.log("Failed to YouTube Search.")
144
+ }
145
+ }
146
+ console.log("Failed to YouTube Search thrice, quitting.")
147
+ return undefined
148
+ }
121
149
  }
package/src/UtilsMisc.ts CHANGED
@@ -15,12 +15,12 @@ export class UtilsMisc {
15
15
  this.config = config
16
16
  }
17
17
 
18
- async safe_run(f : () => Promise<any>) : Promise<void> {
18
+ async safe_run<T>(f : () => Promise<T>) : Promise<T | undefined> {
19
19
  try {
20
- await f()
20
+ return await f()
21
21
  } catch (error) {
22
22
  if (!(error instanceof Error)) {
23
- return
23
+ return undefined
24
24
  }
25
25
  if (error.stack !== undefined) {
26
26
  console.log(error.stack.toString())
@@ -28,12 +28,14 @@ export class UtilsMisc {
28
28
  console.log(error.toString())
29
29
  }
30
30
  }
31
+ return undefined
31
32
  }
32
33
 
33
- async iterate<T>(inputs : T[], f : (input : T, iterator_id : number, index : number) => Promise<any>, num_iterators : number = 1, print_option? : number | boolean) {
34
+ async iterate<T, U>(inputs : T[], f : (input : T, iterator_id : number, index : number) => Promise<U>, num_iterators : number = 1, print_option? : number | boolean) {
34
35
  const print_freq : number | undefined = typeof print_option === "boolean" ? (print_option === true ? 1 : 0) : print_option
35
36
  let index = 0
36
37
  let iterators = []
38
+ const outputs : (U | undefined)[] = []//new Array(inputs.length).fill(undefined)
37
39
  for (let iterator_id = 0; iterator_id < num_iterators; iterator_id++) {
38
40
  iterators.push((async () => {
39
41
  while (index < inputs.length) {
@@ -41,11 +43,12 @@ export class UtilsMisc {
41
43
  if (print_freq !== undefined && ((index - 1) % print_freq === 0)) {
42
44
  console.log(iterator_id + ":" + (index - 1) + "/" + inputs.length)
43
45
  }
44
- await this.safe_run(() => f(inputs[index - 1], iterator_id, index))
46
+ const output = await this.safe_run(() => f(inputs[index - 1], iterator_id, index))
47
+ outputs.push(output)
45
48
  }
46
49
  })())
47
50
  }
48
- await Promise.all(iterators)
51
+ return outputs
49
52
  }
50
53
 
51
54
 
package/src/f.ts CHANGED
@@ -28,7 +28,7 @@ const utils = new TriangleUtils(config)
28
28
 
29
29
  // console.log(foods)
30
30
 
31
- const text = await utils.anthropic.claude_query("claude-opus-4-6", "hi", undefined, { print_usage : true })
31
+ const text = await utils.bee.get("https://www.doyourjobs.org", { return_page_text : true })
32
32
 
33
33
  console.log(text)
34
34