triangle-utils 1.2.12 → 1.3.1

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.
@@ -0,0 +1,13 @@
1
+ import js from "@eslint/js";
2
+ import globals from "globals";
3
+ import tseslint from "typescript-eslint";
4
+ import { defineConfig } from "eslint/config";
5
+
6
+ export default defineConfig([
7
+ { files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], plugins: { js }, extends: ["js/recommended"], languageOptions: { globals: globals.browser } },
8
+ {
9
+ rules: {
10
+ "@typescript-eslint/explicit-function-return-type": "error"
11
+ },
12
+ },
13
+ ]);
package/jest.config.js ADDED
@@ -0,0 +1,19 @@
1
+ import { createDefaultPreset } from "ts-jest"
2
+
3
+ const tsJestTransformCfg = createDefaultPreset().transform;
4
+
5
+ /** @type {import("jest").Config} **/
6
+ export default {
7
+ testEnvironment: "node",
8
+ transform: {
9
+ ...tsJestTransformCfg,
10
+ },
11
+ transformIgnorePatterns: ["/node_modules/(?!@smithy|@aws-sdk).+\\.js$"],
12
+ globals : {
13
+ "ts-jest" : {
14
+ useESM : true
15
+ }
16
+ },
17
+ extensionsToTreatAsEsm : [".ts"],
18
+ testTimeout : 30000
19
+ };
package/package.json CHANGED
@@ -1,7 +1,13 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.2.12",
4
- "main": "Utils.js",
3
+ "version": "1.3.1",
4
+ "main": "dist/src/Utils.js",
5
+ "directories": {
6
+ "test": "test"
7
+ },
8
+ "scripts": {
9
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
10
+ },
5
11
  "author": "",
6
12
  "license": "ISC",
7
13
  "description": "",
@@ -13,9 +19,23 @@
13
19
  "@aws-sdk/client-s3vectors": "^3.953.0",
14
20
  "@aws-sdk/client-secrets-manager": "^3.965.0",
15
21
  "@aws-sdk/s3-request-presigner": "^3.953.0",
22
+ "@types/node": "^25.2.3",
23
+ "@types/nodemailer": "^7.0.9",
16
24
  "googleapis": "^170.0.0",
17
25
  "nodemailer": "^7.0.11",
18
- "scrapingbee": "^1.7.6",
26
+ "scrapingbee": "^1.8.2",
19
27
  "triangle-utils": "^1.0.34"
28
+ },
29
+ "devDependencies": {
30
+ "@eslint/js": "^9.39.2",
31
+ "@types/jest": "^30.0.0",
32
+ "eslint": "^9.39.2",
33
+ "globals": "^17.3.0",
34
+ "jest": "^30.2.0",
35
+ "jiti": "^2.6.1",
36
+ "ts-jest": "^29.4.6",
37
+ "tsx": "^4.21.0",
38
+ "typescript": "^5.9.3",
39
+ "typescript-eslint": "^8.55.0"
20
40
  }
21
41
  }
package/src/Utils.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { Utils_Misc } from "./Utils_Misc"
2
+ import { Utils_DynamoDB } from "./Utils_DynamoDB"
3
+ import { Utils_S3 } from "./Utils_S3"
4
+ import { Utils_Bedrock } from "./Utils_Bedrock"
5
+ import { Utils_Bee } from "./Utils_Bee"
6
+ import { Utils_S3Vectors } from "./Utils_S3Vectors"
7
+ import { Utils_Youtube } from "./Utils_Youtube"
8
+ import { Config } from "./types/Config"
9
+
10
+ export class Utils extends Utils_Misc {
11
+
12
+ readonly dynamodb : Utils_DynamoDB
13
+ readonly s3 : Utils_S3
14
+ readonly s3vectors : Utils_S3Vectors
15
+ readonly bedrock : Utils_Bedrock
16
+ readonly bee : Utils_Bee
17
+ readonly youtube : Utils_Youtube
18
+
19
+ constructor(config : Config) {
20
+ super(config)
21
+ this.dynamodb = new Utils_DynamoDB(config)
22
+ this.s3 = new Utils_S3(config)
23
+ this.s3vectors = new Utils_S3Vectors(config)
24
+ this.bedrock = new Utils_Bedrock(config)
25
+ this.bee = new Utils_Bee(config)
26
+ this.youtube = new Utils_Youtube(config)
27
+ }
28
+ }
@@ -1,13 +1,17 @@
1
1
  import { BedrockRuntime } from "@aws-sdk/client-bedrock-runtime"
2
+ import { Config } from "./types/Config"
2
3
 
3
- export default class Utils_Bedrock {
4
+ export class Utils_Bedrock {
4
5
 
5
- constructor(config) {
6
+ readonly bedrock : BedrockRuntime
7
+ readonly text_decoder : TextDecoder
8
+
9
+ constructor(config : Config) {
6
10
  this.bedrock = new BedrockRuntime({ region: config.region })
7
11
  this.text_decoder = new TextDecoder()
8
12
  }
9
13
 
10
- async llama_invoke(model_id, prompt, temperature = 0.5, max_gen_len = 512, top_p = 0.9) {
14
+ async llama_invoke(model_id : string, prompt : string, temperature : number = 0.5, max_gen_len : number = 512, top_p : number = 0.9) : Promise<string | undefined> {
11
15
  for (let i = 0; i < 3; i++) {
12
16
  try {
13
17
  const output = await this.bedrock.invokeModel({
@@ -30,10 +34,10 @@ export default class Utils_Bedrock {
30
34
  return undefined
31
35
  }
32
36
 
33
- async gpt_converse(model_id, prompt, temperature = 0.5, max_gen_len = 512, top_p = 0.9) {
37
+ async gpt_converse(model_id : string, prompt : string, temperature : number = 0.5, max_gen_len : number = 512, top_p : number = 0.9) : Promise<string | undefined> {
34
38
  for (let i = 0; i < 3; i++) {
35
39
  try {
36
- const output = await this.bedrock.converse({
40
+ const response = await this.bedrock.converse({
37
41
  modelId : model_id,
38
42
  messages : [
39
43
  { role : "user", content : [ { text : prompt } ] }
@@ -44,22 +48,27 @@ export default class Utils_Bedrock {
44
48
  maxTokens : max_gen_len
45
49
  }
46
50
  })
47
- const response = output.output
48
- for (const c of response.message.content) {
51
+ const output = response.output
52
+ if (output === undefined || output.message === undefined || output.message.content === undefined) {
53
+ continue
54
+ }
55
+ for (const c of output.message.content) {
49
56
  if (c.text !== undefined) {
50
57
  return c.text
51
58
  }
52
59
  }
53
- } catch (e) {
60
+ } catch (error) {
61
+ if (error instanceof Error) {
62
+ console.log(error.stack)
63
+ }
54
64
  console.log("Failed to get from Bedrock.")
55
- console.log(e.stack)
56
65
  }
57
66
  }
58
67
  console.log("Failed to get from Bedrock, quitting.")
59
68
  return undefined
60
69
  }
61
70
 
62
- async titan_invoke(text) {
71
+ async titan_invoke(text : string) : Promise<string | undefined> {
63
72
  for (let i = 0; i < 3; i++) {
64
73
  try {
65
74
  const output = await this.bedrock.invokeModel({
@@ -71,9 +80,11 @@ export default class Utils_Bedrock {
71
80
  })
72
81
  const response = JSON.parse(this.text_decoder.decode(output.body))
73
82
  return response.embedding
74
- } catch (e) {
83
+ } catch (error) {
84
+ if (error instanceof Error) {
85
+ console.log(error.stack)
86
+ }
75
87
  console.log("Failed to get from Bedrock.")
76
- console.log(e.stack)
77
88
  }
78
89
  }
79
90
  console.log("Failed to get from Bedrock, quitting.")
@@ -1,28 +1,38 @@
1
1
  import { ScrapingBeeClient } from "scrapingbee"
2
+ import { Config } from "./types/Config"
2
3
 
3
- export default class Utils_Bee {
4
+ export class Utils_Bee {
4
5
 
5
- constructor(config) {
6
+ readonly config : Config
7
+ readonly bee : ScrapingBeeClient | undefined
8
+ readonly text_decoder : TextDecoder
9
+
10
+ constructor(config : Config) {
6
11
  this.config = config
7
- this.bee = new ScrapingBeeClient(config.scraping_bee_api_key)
12
+ this.bee = config.scraping_bee_api_key !== undefined ? new ScrapingBeeClient(config.scraping_bee_api_key) : undefined
8
13
  this.text_decoder = new TextDecoder()
9
14
  }
10
15
 
11
- async get(url, params) {
16
+ async get(url : string, params : Record<string, string> = {}) {
17
+ if (this.bee === undefined) {
18
+ return undefined
19
+ }
12
20
  for (let i = 0; i < 3; i++) {
13
21
  try {
14
22
  const response = await this.bee.get({
15
23
  url : url,
16
24
  params : params
17
- }).catch(response => response)
25
+ })
18
26
  if (response.status !== 200) {
19
27
  console.log("Status", response.status)
20
28
  return undefined
21
29
  }
22
30
  const text = this.text_decoder.decode(response.data)
23
31
  return text
24
- } catch (e) {
25
- console.log(e.stack)
32
+ } catch (error) {
33
+ if (error instanceof Error) {
34
+ console.log(error.stack)
35
+ }
26
36
  console.log("Failed to get from Scraping Bee.")
27
37
  }
28
38
  }
@@ -30,17 +40,23 @@ export default class Utils_Bee {
30
40
  return undefined
31
41
  }
32
42
 
33
- async google_search(query, news = false) {
43
+ async google_search(query : string, news : boolean = false) {
44
+ if (this.bee === undefined) {
45
+ return undefined
46
+ }
34
47
  for (let i = 0; i < 3; i++) {
35
48
  try {
36
- const bee_url = "https://app.scrapingbee.com/api/v1/store/google?api_key=" + this.config.scraping_bee_api_key + "&search=" + encodeURI(query) + (news ? "&search_type=news" : "")
37
- console.log(bee_url)
38
- const response = await fetch(bee_url)
39
- if (!response.ok) {
49
+ const response = await this.bee.googleSearch({
50
+ search : query,
51
+ params : {
52
+ search_type : news ? "news" : undefined
53
+ }
54
+ })
55
+ if (response.status !== 200) {
40
56
  console.log("Failed to Google:", response.status)
41
57
  continue
42
58
  }
43
- const data = await response.json()
59
+ const data = response.data
44
60
  const results = news ? data.news_results : data.organic_results
45
61
 
46
62
  if (results === undefined) {
@@ -56,17 +72,21 @@ export default class Utils_Bee {
56
72
  return undefined
57
73
  }
58
74
 
59
- async youtube_search(query, options = {}) {
75
+ async youtube_search(query : string, options = {}) {
76
+ if (this.bee === undefined) {
77
+ return undefined
78
+ }
60
79
  for (let i = 0; i < 3; i++) {
61
80
  try {
62
- const bee_url = "https://app.scrapingbee.com/api/v1/youtube/search?api_key=" + this.config.scraping_bee_api_key + "&search=" + encodeURI(query) + Object.entries(options).filter(([key, value]) => value !== undefined).map(([key, value]) => "&" + key + "=" + value).join("")
63
- console.log(bee_url)
64
- const response = await fetch(bee_url)
65
- if (!response.ok) {
81
+ const response = await this.bee.youtubeSearch({
82
+ search : query,
83
+ params : options
84
+ })
85
+ if (response.status !== 200) {
66
86
  console.log("Failed to YouTube Search:", response.status)
67
87
  continue
68
88
  }
69
- const data = await response.json()
89
+ const data = response.data
70
90
  const results = data.results
71
91
 
72
92
  if (results === undefined) {