@stackfactor/agent-utils 1.0.22 → 1.0.24

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/README.md CHANGED
@@ -62,32 +62,6 @@ const result = await langChain.runPromptWithModel(
62
62
 
63
63
  ---
64
64
 
65
- ### `langChain.runChatPromptWithModel(modelName, config, prompt, onProgressReport?)`
66
-
67
- Sends a conversational chat prompt to an LLM and returns a plain HTML response. Built for the StackFactor Mentor chat feature — system messages are wrapped with topic-constraint instructions that decline off-topic questions.
68
-
69
- | Parameter | Type | Default | Description |
70
- | ------------------ | -------------------- | ------- | ----------------------------------------------------- |
71
- | `modelName` | `string` | — | Model identifier |
72
- | `config` | `object` | — | API keys and optional `temperature` |
73
- | `prompt` | `string \| object[]` | — | Plain string or array of `{ role, content }` messages |
74
- | `onProgressReport` | `function \| null` | `null` | Streaming progress callback |
75
-
76
- **Returns:** Raw HTML string.
77
-
78
- ```typescript
79
- const html = await langChain.runChatPromptWithModel(
80
- "claude-3-5-sonnet",
81
- { anthropicAPIKey: "sk-ant-..." },
82
- [
83
- { role: "system", content: "Topic: JavaScript closures" },
84
- { role: "user", content: "Explain closures with an example" },
85
- ],
86
- );
87
- ```
88
-
89
- ---
90
-
91
65
  ### `langChain.createAIAgent(name, modelName, systemPrompt, tools?, responseFormat?, config, onReportProgress?, minPercent?, maxPercent?)`
92
66
 
93
67
  Constructs a LangChain agent with a model, system prompt, and tools. When `onReportProgress` is provided, a `report_progress` tool is automatically added.
@@ -1,10 +1,11 @@
1
1
  declare const _default: {
2
+ checkIfAIProviderConfigured: (config: any) => void;
2
3
  createAgent: (name: string, modelName: string, systemPrompt: string, tools: any[], responseFormat: any, config: any) => any;
3
4
  runAgent: (agent: any, prompt: string, config: any, onProgress?: Function | null) => Promise<any>;
4
- runChatPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any) => any;
5
5
  runPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any, minPercent?: number, maxPercent?: number, expectsJsonResponse?: boolean, schema?: any, agentName?: string, tools?: any[]) => Promise<any>;
6
6
  runPromptWithModelForImageGeneration: (modelName: string, config: any, prompt: string, options?: any) => Promise<any>;
7
7
  throwErrorIfNotSuccessful: (response: any) => string;
8
+ validateModel: (selectedModel: string, supportedModels: string[]) => string;
8
9
  };
9
10
  export default _default;
10
11
  //# sourceMappingURL=langChain.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAogBQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,KACV,GAAG;sBAyBG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCA2xBF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCA1bO,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDAsvBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CAngC8B,GAAG,KAAG,MAAM;;AA8jCzD,wBAOE"}
1
+ {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";0CAgD6C,GAAG,KAAG,IAAI;wBAke/C,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,KACV,GAAG;sBAyBG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;oCAuXF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDA6rBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CA59B8B,GAAG,KAAG,MAAM;mCAoBxC,MAAM,mBACJ,MAAM,EAAE,KACxB,MAAM;;AAigCT,wBAQE"}
@@ -36,6 +36,14 @@ const assertQuotaAvailable = () => {
36
36
  throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.PAYMENT_REQUIRED, const_js_1.default.ERROR.QUOTA_EXHAUSTED);
37
37
  }
38
38
  };
39
+ const checkIfAIProviderConfigured = (config) => {
40
+ if (!config ||
41
+ !config.openAIAPIKey ||
42
+ !config.googleAPIKey ||
43
+ !config.anthropicAPIKey) {
44
+ throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR, "The integration is not properly configured with all AI providers.");
45
+ }
46
+ };
39
47
  /**
40
48
  * Subtracts the given USD cost from `global.quota.remaining` and adds it to
41
49
  * `global.quota.usedThisSession`. Silently no-ops when the quota globals are not
@@ -581,6 +589,20 @@ const throwErrorIfNotSuccessful = (response) => {
581
589
  throw errorHandling_js_1.default.create(const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR, const_js_1.default.ERROR.UNABLE_TO_GENERATE_CONTENT);
582
590
  }
583
591
  };
592
+ /**
593
+ * Returns `selectedModel` when it appears in `supportedModels`, otherwise returns
594
+ * the first entry in `supportedModels` as a safe fallback. Use this to coerce a
595
+ * caller-supplied model identifier to one the current integration is known to
596
+ * support, instead of failing or silently passing an unsupported model to
597
+ * `getLLMModel`. When `supportedModels` is empty there is no fallback to pick,
598
+ * so the original `selectedModel` is returned unchanged.
599
+ */
600
+ const validateModel = (selectedModel, supportedModels) => {
601
+ if (supportedModels.includes(selectedModel)) {
602
+ return selectedModel;
603
+ }
604
+ return supportedModels[0] ?? selectedModel;
605
+ };
584
606
  const MAX_VALIDATION_RETRIES = 3;
585
607
  const MAX_RATE_LIMIT_RETRIES = 2;
586
608
  const RATE_LIMIT_MIN_WAIT_MS = 30_000;
@@ -1137,47 +1159,6 @@ const runPromptWithModel = async (modelName, config, prompt, onProgressReport, m
1137
1159
  }
1138
1160
  }
1139
1161
  };
1140
- /**
1141
- * Sends a conversational chat prompt to an LLM and returns a plain HTML response
1142
- * intended for end-user display. This function is purpose-built for the StackFactor
1143
- * Mentor chat feature: when the prompt is an array of messages, each `system` message
1144
- * is wrapped in an enhanced system prompt that instructs the model to answer only
1145
- * questions related to the provided topic, decline off-topic questions, and return
1146
- * results as simple HTML without markdown code-block notation. The call is delegated to
1147
- * `runPromptWithModel` with `expectsJsonResponse` set to `false`.
1148
- * @param modelName - The model identifier, e.g. `"gpt-4o"`, `"claude-3-5-sonnet"`
1149
- * @param config - Configuration object with API keys and optional `temperature`
1150
- * @param prompt - A plain string user message, or an array of `{ role, content }`
1151
- * message objects; `system` messages have the topic-constraint wrapper applied
1152
- * @param onProgressReport - Optional callback for streaming progress updates passed
1153
- * through to `runPromptWithModel`
1154
- * @returns The model's response as a raw HTML string
1155
- */
1156
- const runChatPromptWithModel = (modelName, config, prompt, onProgressReport) => {
1157
- let messages = prompt;
1158
- // If prompt is an array, check for system message and enhance it
1159
- if (Array.isArray(prompt)) {
1160
- messages = prompt.map((msg) => {
1161
- if (msg.role === "system") {
1162
- return {
1163
- ...msg,
1164
- content: `You are StackFactor Mentor, an AI assistant that helps users by providing information related to the specified topic.
1165
-
1166
- ### Objective:
1167
- Respond to the user question considering just related to the selected topic and all previous interactions:
1168
- - If the question is unrelated decline to respond.
1169
- - Return the results as a simple HMTL but don't include notations for formatting blocks.
1170
-
1171
- ### TOPIC INFORMATION:\n
1172
- ${msg.content}
1173
- `,
1174
- };
1175
- }
1176
- return msg;
1177
- });
1178
- }
1179
- return runPromptWithModel(modelName, config, messages, onProgressReport, 0, 100, false);
1180
- };
1181
1162
  /**
1182
1163
  * Determines the image generation provider for a given model name based on its prefix.
1183
1164
  * `dall-e-` and `gpt-image-` prefixes map to `"openai"`. `gemini-` and `imagen-`
@@ -1439,10 +1420,11 @@ const runPromptWithModelForImageGeneration = async (modelName, config, prompt, o
1439
1420
  }
1440
1421
  };
1441
1422
  exports.default = {
1423
+ checkIfAIProviderConfigured,
1442
1424
  createAgent,
1443
1425
  runAgent,
1444
- runChatPromptWithModel,
1445
1426
  runPromptWithModel,
1446
1427
  runPromptWithModelForImageGeneration,
1447
1428
  throwErrorIfNotSuccessful,
1429
+ validateModel,
1448
1430
  };
@@ -1,10 +1,11 @@
1
1
  declare const _default: {
2
+ checkIfAIProviderConfigured: (config: any) => void;
2
3
  createAgent: (name: string, modelName: string, systemPrompt: string, tools: any[], responseFormat: any, config: any) => any;
3
4
  runAgent: (agent: any, prompt: string, config: any, onProgress?: Function | null) => Promise<any>;
4
- runChatPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any) => any;
5
5
  runPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any, minPercent?: number, maxPercent?: number, expectsJsonResponse?: boolean, schema?: any, agentName?: string, tools?: any[]) => Promise<any>;
6
6
  runPromptWithModelForImageGeneration: (modelName: string, config: any, prompt: string, options?: any) => Promise<any>;
7
7
  throwErrorIfNotSuccessful: (response: any) => string;
8
+ validateModel: (selectedModel: string, supportedModels: string[]) => string;
8
9
  };
9
10
  export default _default;
10
11
  //# sourceMappingURL=langChain.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";wBAogBQ,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,KACV,GAAG;sBAyBG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;wCA2xBF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,KACpB,GAAG;oCA1bO,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDAsvBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CAngC8B,GAAG,KAAG,MAAM;;AA8jCzD,wBAOE"}
1
+ {"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":";0CAgD6C,GAAG,KAAG,IAAI;wBAke/C,MAAM,aACD,MAAM,gBACH,MAAM,SACb,GAAG,EAAE,kBACI,GAAG,UACX,GAAG,KACV,GAAG;sBAyBG,GAAG,UACF,MAAM,UACN,GAAG,eACC,QAAQ,GAAG,IAAI,KAC1B,OAAO,CAAC,GAAG,CAAC;oCAuXF,MAAM,UACT,GAAG,UACH,GAAG,oBACO,GAAG,eACT,MAAM,eACN,MAAM,wBACG,OAAO,WACpB,GAAG,cACA,MAAM,UACV,GAAG,EAAE,KACX,OAAO,CAAC,GAAG,CAAC;sDA6rBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,KACX,OAAO,CAAC,GAAG,CAAC;0CA59B8B,GAAG,KAAG,MAAM;mCAoBxC,MAAM,mBACJ,MAAM,EAAE,KACxB,MAAM;;AAigCT,wBAQE"}
@@ -31,6 +31,14 @@ const assertQuotaAvailable = () => {
31
31
  throw errorHandlingHelper.create(constants.HTTP_CODES.PAYMENT_REQUIRED, constants.ERROR.QUOTA_EXHAUSTED);
32
32
  }
33
33
  };
34
+ const checkIfAIProviderConfigured = (config) => {
35
+ if (!config ||
36
+ !config.openAIAPIKey ||
37
+ !config.googleAPIKey ||
38
+ !config.anthropicAPIKey) {
39
+ throw errorHandlingHelper.create(constants.HTTP_CODES.INTERNAL_SERVER_ERROR, "The integration is not properly configured with all AI providers.");
40
+ }
41
+ };
34
42
  /**
35
43
  * Subtracts the given USD cost from `global.quota.remaining` and adds it to
36
44
  * `global.quota.usedThisSession`. Silently no-ops when the quota globals are not
@@ -576,6 +584,20 @@ const throwErrorIfNotSuccessful = (response) => {
576
584
  throw errorHandlingHelper.create(constants.HTTP_CODES.INTERNAL_SERVER_ERROR, constants.ERROR.UNABLE_TO_GENERATE_CONTENT);
577
585
  }
578
586
  };
587
+ /**
588
+ * Returns `selectedModel` when it appears in `supportedModels`, otherwise returns
589
+ * the first entry in `supportedModels` as a safe fallback. Use this to coerce a
590
+ * caller-supplied model identifier to one the current integration is known to
591
+ * support, instead of failing or silently passing an unsupported model to
592
+ * `getLLMModel`. When `supportedModels` is empty there is no fallback to pick,
593
+ * so the original `selectedModel` is returned unchanged.
594
+ */
595
+ const validateModel = (selectedModel, supportedModels) => {
596
+ if (supportedModels.includes(selectedModel)) {
597
+ return selectedModel;
598
+ }
599
+ return supportedModels[0] ?? selectedModel;
600
+ };
579
601
  const MAX_VALIDATION_RETRIES = 3;
580
602
  const MAX_RATE_LIMIT_RETRIES = 2;
581
603
  const RATE_LIMIT_MIN_WAIT_MS = 30_000;
@@ -1132,47 +1154,6 @@ const runPromptWithModel = async (modelName, config, prompt, onProgressReport, m
1132
1154
  }
1133
1155
  }
1134
1156
  };
1135
- /**
1136
- * Sends a conversational chat prompt to an LLM and returns a plain HTML response
1137
- * intended for end-user display. This function is purpose-built for the StackFactor
1138
- * Mentor chat feature: when the prompt is an array of messages, each `system` message
1139
- * is wrapped in an enhanced system prompt that instructs the model to answer only
1140
- * questions related to the provided topic, decline off-topic questions, and return
1141
- * results as simple HTML without markdown code-block notation. The call is delegated to
1142
- * `runPromptWithModel` with `expectsJsonResponse` set to `false`.
1143
- * @param modelName - The model identifier, e.g. `"gpt-4o"`, `"claude-3-5-sonnet"`
1144
- * @param config - Configuration object with API keys and optional `temperature`
1145
- * @param prompt - A plain string user message, or an array of `{ role, content }`
1146
- * message objects; `system` messages have the topic-constraint wrapper applied
1147
- * @param onProgressReport - Optional callback for streaming progress updates passed
1148
- * through to `runPromptWithModel`
1149
- * @returns The model's response as a raw HTML string
1150
- */
1151
- const runChatPromptWithModel = (modelName, config, prompt, onProgressReport) => {
1152
- let messages = prompt;
1153
- // If prompt is an array, check for system message and enhance it
1154
- if (Array.isArray(prompt)) {
1155
- messages = prompt.map((msg) => {
1156
- if (msg.role === "system") {
1157
- return {
1158
- ...msg,
1159
- content: `You are StackFactor Mentor, an AI assistant that helps users by providing information related to the specified topic.
1160
-
1161
- ### Objective:
1162
- Respond to the user question considering just related to the selected topic and all previous interactions:
1163
- - If the question is unrelated decline to respond.
1164
- - Return the results as a simple HMTL but don't include notations for formatting blocks.
1165
-
1166
- ### TOPIC INFORMATION:\n
1167
- ${msg.content}
1168
- `,
1169
- };
1170
- }
1171
- return msg;
1172
- });
1173
- }
1174
- return runPromptWithModel(modelName, config, messages, onProgressReport, 0, 100, false);
1175
- };
1176
1157
  /**
1177
1158
  * Determines the image generation provider for a given model name based on its prefix.
1178
1159
  * `dall-e-` and `gpt-image-` prefixes map to `"openai"`. `gemini-` and `imagen-`
@@ -1434,10 +1415,11 @@ const runPromptWithModelForImageGeneration = async (modelName, config, prompt, o
1434
1415
  }
1435
1416
  };
1436
1417
  export default {
1418
+ checkIfAIProviderConfigured,
1437
1419
  createAgent,
1438
1420
  runAgent,
1439
- runChatPromptWithModel,
1440
1421
  runPromptWithModel,
1441
1422
  runPromptWithModelForImageGeneration,
1442
1423
  throwErrorIfNotSuccessful,
1424
+ validateModel,
1443
1425
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "restricted"
5
5
  },
6
- "version": "1.0.22",
6
+ "version": "1.0.24",
7
7
  "description": "",
8
8
  "main": "dist/cjs/index.js",
9
9
  "module": "dist/esm/index.js",