@superatomai/sdk-node 0.0.8-mds → 0.0.9-mds
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/index.js +252 -170
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +252 -170
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -4963,6 +4963,130 @@ async function withProgressHeartbeat(operation, progressMessage, streamBuffer, i
|
|
|
4963
4963
|
}
|
|
4964
4964
|
}
|
|
4965
4965
|
|
|
4966
|
+
// src/userResponse/knowledge-base.ts
|
|
4967
|
+
var getKnowledgeBase = async ({
|
|
4968
|
+
prompt,
|
|
4969
|
+
collections,
|
|
4970
|
+
topK = 1
|
|
4971
|
+
}) => {
|
|
4972
|
+
try {
|
|
4973
|
+
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["query"]) {
|
|
4974
|
+
logger.warn("[KnowledgeBase] knowledge-base.query collection not registered, skipping");
|
|
4975
|
+
return "";
|
|
4976
|
+
}
|
|
4977
|
+
const result = await collections["knowledge-base"]["query"]({
|
|
4978
|
+
prompt,
|
|
4979
|
+
topK
|
|
4980
|
+
});
|
|
4981
|
+
if (!result || !result.content) {
|
|
4982
|
+
logger.warn("[KnowledgeBase] No knowledge base results returned");
|
|
4983
|
+
return "";
|
|
4984
|
+
}
|
|
4985
|
+
logger.info(`[KnowledgeBase] Retrieved knowledge base context (${result.content.length} chars)`);
|
|
4986
|
+
if (result.metadata?.sources && result.metadata.sources.length > 0) {
|
|
4987
|
+
logger.warn(`[KnowledgeBase] Sources: ${result.metadata.sources.map((s) => s.title).join(", ")}`);
|
|
4988
|
+
}
|
|
4989
|
+
return result.content;
|
|
4990
|
+
} catch (error) {
|
|
4991
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
4992
|
+
logger.warn(`[KnowledgeBase] Error querying knowledge base: ${errorMsg}`);
|
|
4993
|
+
return "";
|
|
4994
|
+
}
|
|
4995
|
+
};
|
|
4996
|
+
var getGlobalKnowledgeBase = async ({
|
|
4997
|
+
collections,
|
|
4998
|
+
limit = 100
|
|
4999
|
+
}) => {
|
|
5000
|
+
try {
|
|
5001
|
+
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["getGlobal"]) {
|
|
5002
|
+
logger.warn("[KnowledgeBase] knowledge-base.getGlobal collection not registered, skipping");
|
|
5003
|
+
return "";
|
|
5004
|
+
}
|
|
5005
|
+
const result = await collections["knowledge-base"]["getGlobal"]({ limit });
|
|
5006
|
+
if (!result || !result.content) {
|
|
5007
|
+
logger.warn("[KnowledgeBase] No global knowledge base nodes found");
|
|
5008
|
+
return "";
|
|
5009
|
+
}
|
|
5010
|
+
logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} global knowledge base nodes`);
|
|
5011
|
+
return result.content;
|
|
5012
|
+
} catch (error) {
|
|
5013
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
5014
|
+
logger.warn(`[KnowledgeBase] Error fetching global knowledge base: ${errorMsg}`);
|
|
5015
|
+
return "";
|
|
5016
|
+
}
|
|
5017
|
+
};
|
|
5018
|
+
var getUserKnowledgeBase = async ({
|
|
5019
|
+
collections,
|
|
5020
|
+
userId,
|
|
5021
|
+
limit = 100
|
|
5022
|
+
}) => {
|
|
5023
|
+
try {
|
|
5024
|
+
if (!userId) {
|
|
5025
|
+
logger.warn("[KnowledgeBase] No userId provided, skipping user knowledge base");
|
|
5026
|
+
return "";
|
|
5027
|
+
}
|
|
5028
|
+
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["getByUser"]) {
|
|
5029
|
+
logger.warn("[KnowledgeBase] knowledge-base.getByUser collection not registered, skipping");
|
|
5030
|
+
return "";
|
|
5031
|
+
}
|
|
5032
|
+
const result = await collections["knowledge-base"]["getByUser"]({
|
|
5033
|
+
userId: String(userId),
|
|
5034
|
+
limit
|
|
5035
|
+
});
|
|
5036
|
+
if (!result || !result.content) {
|
|
5037
|
+
logger.info(`[KnowledgeBase] No user knowledge base nodes found for userId: ${userId}`);
|
|
5038
|
+
return "";
|
|
5039
|
+
}
|
|
5040
|
+
logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} user knowledge base nodes for userId: ${userId}`);
|
|
5041
|
+
return result.content;
|
|
5042
|
+
} catch (error) {
|
|
5043
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
5044
|
+
logger.warn(`[KnowledgeBase] Error fetching user knowledge base: ${errorMsg}`);
|
|
5045
|
+
return "";
|
|
5046
|
+
}
|
|
5047
|
+
};
|
|
5048
|
+
var getAllKnowledgeBase = async ({
|
|
5049
|
+
prompt,
|
|
5050
|
+
collections,
|
|
5051
|
+
userId,
|
|
5052
|
+
topK = 3
|
|
5053
|
+
}) => {
|
|
5054
|
+
const [globalContext, userContext, queryContext] = await Promise.all([
|
|
5055
|
+
getGlobalKnowledgeBase({ collections }),
|
|
5056
|
+
getUserKnowledgeBase({ collections, userId }),
|
|
5057
|
+
getKnowledgeBase({ prompt, collections, topK })
|
|
5058
|
+
]);
|
|
5059
|
+
let combinedContext = "";
|
|
5060
|
+
if (globalContext) {
|
|
5061
|
+
combinedContext += "## Global Knowledge Base\n";
|
|
5062
|
+
combinedContext += "The following information applies to all queries:\n\n";
|
|
5063
|
+
combinedContext += globalContext + "\n\n";
|
|
5064
|
+
}
|
|
5065
|
+
if (userContext) {
|
|
5066
|
+
combinedContext += "## User-Specific Knowledge Base\n";
|
|
5067
|
+
combinedContext += "The following information is specific to this user:\n\n";
|
|
5068
|
+
combinedContext += userContext + "\n\n";
|
|
5069
|
+
}
|
|
5070
|
+
if (queryContext) {
|
|
5071
|
+
combinedContext += "## Relevant Knowledge Base (Query-Matched)\n";
|
|
5072
|
+
combinedContext += "The following information is semantically relevant to the current query:\n\n";
|
|
5073
|
+
combinedContext += queryContext + "\n\n";
|
|
5074
|
+
}
|
|
5075
|
+
return {
|
|
5076
|
+
globalContext,
|
|
5077
|
+
userContext,
|
|
5078
|
+
queryContext,
|
|
5079
|
+
combinedContext: combinedContext.trim()
|
|
5080
|
+
};
|
|
5081
|
+
};
|
|
5082
|
+
var KB = {
|
|
5083
|
+
getKnowledgeBase,
|
|
5084
|
+
getGlobalKnowledgeBase,
|
|
5085
|
+
getUserKnowledgeBase,
|
|
5086
|
+
getAllKnowledgeBase
|
|
5087
|
+
};
|
|
5088
|
+
var knowledge_base_default = KB;
|
|
5089
|
+
|
|
4966
5090
|
// src/llm.ts
|
|
4967
5091
|
import Anthropic from "@anthropic-ai/sdk";
|
|
4968
5092
|
import Groq from "groq-sdk";
|
|
@@ -6541,6 +6665,48 @@ function getCurrentDateTimeForPrompt() {
|
|
|
6541
6665
|
});
|
|
6542
6666
|
}
|
|
6543
6667
|
|
|
6668
|
+
// src/userResponse/prompt-extractor.ts
|
|
6669
|
+
function extractPromptText(content) {
|
|
6670
|
+
if (content === null || content === void 0) {
|
|
6671
|
+
return "";
|
|
6672
|
+
}
|
|
6673
|
+
if (typeof content === "string") {
|
|
6674
|
+
return content;
|
|
6675
|
+
}
|
|
6676
|
+
if (Array.isArray(content)) {
|
|
6677
|
+
return content.map((item) => extractContentBlockText(item)).filter((text) => text.length > 0).join("\n\n---\n\n");
|
|
6678
|
+
}
|
|
6679
|
+
if (content && typeof content === "object") {
|
|
6680
|
+
return extractObjectText(content);
|
|
6681
|
+
}
|
|
6682
|
+
return String(content);
|
|
6683
|
+
}
|
|
6684
|
+
function extractContentBlockText(item) {
|
|
6685
|
+
if (typeof item === "string") {
|
|
6686
|
+
return item;
|
|
6687
|
+
}
|
|
6688
|
+
if (item && typeof item === "object") {
|
|
6689
|
+
const obj = item;
|
|
6690
|
+
if (typeof obj.text === "string") {
|
|
6691
|
+
return obj.text;
|
|
6692
|
+
}
|
|
6693
|
+
if (typeof obj.content === "string") {
|
|
6694
|
+
return obj.content;
|
|
6695
|
+
}
|
|
6696
|
+
return JSON.stringify(item, null, 2);
|
|
6697
|
+
}
|
|
6698
|
+
return String(item);
|
|
6699
|
+
}
|
|
6700
|
+
function extractObjectText(obj) {
|
|
6701
|
+
if (typeof obj.text === "string") {
|
|
6702
|
+
return obj.text;
|
|
6703
|
+
}
|
|
6704
|
+
if (typeof obj.content === "string") {
|
|
6705
|
+
return obj.content;
|
|
6706
|
+
}
|
|
6707
|
+
return JSON.stringify(obj, null, 2);
|
|
6708
|
+
}
|
|
6709
|
+
|
|
6544
6710
|
// src/userResponse/agents/agent-prompt-builder.ts
|
|
6545
6711
|
function buildSourceSummaries(externalTools) {
|
|
6546
6712
|
return externalTools.map((tool) => {
|
|
@@ -6683,6 +6849,8 @@ var SourceAgent = class {
|
|
|
6683
6849
|
}
|
|
6684
6850
|
try {
|
|
6685
6851
|
const prompts = await this.buildPrompt(intent, aggregation);
|
|
6852
|
+
logger.logLLMPrompt(`sourceAgent:${this.tool.name}`, "system", extractPromptText(prompts.system));
|
|
6853
|
+
logger.logLLMPrompt(`sourceAgent:${this.tool.name}`, "user", prompts.user);
|
|
6686
6854
|
const llmTool = this.buildLLMToolDefinition();
|
|
6687
6855
|
let executedTool = null;
|
|
6688
6856
|
let resultData = [];
|
|
@@ -6867,7 +7035,9 @@ Analyze the error and try again with a corrected query.`;
|
|
|
6867
7035
|
FULL_SCHEMA: fullSchema,
|
|
6868
7036
|
MAX_ROWS: String(this.config.maxRowsPerSource),
|
|
6869
7037
|
AGGREGATION_MODE: aggregation,
|
|
7038
|
+
GLOBAL_KNOWLEDGE_BASE: this.config.globalKnowledgeBase || "No global knowledge base available.",
|
|
6870
7039
|
CURRENT_DATETIME: getCurrentDateTimeForPrompt(),
|
|
7040
|
+
KNOWLEDGE_BASE_CONTEXT: this.config.knowledgeBaseContext || "No additional knowledge base context available.",
|
|
6871
7041
|
INTENT: intent
|
|
6872
7042
|
});
|
|
6873
7043
|
return { system: prompts.system, user: prompts.user };
|
|
@@ -6963,6 +7133,8 @@ var MainAgent = class {
|
|
|
6963
7133
|
const summaries = buildSourceSummaries(this.externalTools);
|
|
6964
7134
|
logger.info(`[MainAgent] ${summaries.length} source(s) available`);
|
|
6965
7135
|
const systemPrompt = await this.buildSystemPrompt(summaries, conversationHistory);
|
|
7136
|
+
logger.logLLMPrompt("mainAgent", "system", extractPromptText(systemPrompt));
|
|
7137
|
+
logger.logLLMPrompt("mainAgent", "user", userPrompt);
|
|
6966
7138
|
const tools = this.buildSourceToolDefinitions(summaries);
|
|
6967
7139
|
const sourceResults = [];
|
|
6968
7140
|
const executedTools = [];
|
|
@@ -7021,7 +7193,9 @@ var MainAgent = class {
|
|
|
7021
7193
|
const prompts = await promptLoader.loadPrompts("agent-main", {
|
|
7022
7194
|
SOURCE_SUMMARIES: summariesText,
|
|
7023
7195
|
MAX_ROWS: String(this.config.maxRowsPerSource),
|
|
7196
|
+
GLOBAL_KNOWLEDGE_BASE: this.config.globalKnowledgeBase || "No global knowledge base available.",
|
|
7024
7197
|
CURRENT_DATETIME: getCurrentDateTimeForPrompt(),
|
|
7198
|
+
KNOWLEDGE_BASE_CONTEXT: this.config.knowledgeBaseContext || "No additional knowledge base context available.",
|
|
7025
7199
|
CONVERSATION_HISTORY: conversationHistory || "No previous conversation"
|
|
7026
7200
|
});
|
|
7027
7201
|
return prompts.system;
|
|
@@ -7186,172 +7360,6 @@ function processComponentProps(props, executedTools, config) {
|
|
|
7186
7360
|
return cleanedProps;
|
|
7187
7361
|
}
|
|
7188
7362
|
|
|
7189
|
-
// src/userResponse/prompt-extractor.ts
|
|
7190
|
-
function extractPromptText(content) {
|
|
7191
|
-
if (content === null || content === void 0) {
|
|
7192
|
-
return "";
|
|
7193
|
-
}
|
|
7194
|
-
if (typeof content === "string") {
|
|
7195
|
-
return content;
|
|
7196
|
-
}
|
|
7197
|
-
if (Array.isArray(content)) {
|
|
7198
|
-
return content.map((item) => extractContentBlockText(item)).filter((text) => text.length > 0).join("\n\n---\n\n");
|
|
7199
|
-
}
|
|
7200
|
-
if (content && typeof content === "object") {
|
|
7201
|
-
return extractObjectText(content);
|
|
7202
|
-
}
|
|
7203
|
-
return String(content);
|
|
7204
|
-
}
|
|
7205
|
-
function extractContentBlockText(item) {
|
|
7206
|
-
if (typeof item === "string") {
|
|
7207
|
-
return item;
|
|
7208
|
-
}
|
|
7209
|
-
if (item && typeof item === "object") {
|
|
7210
|
-
const obj = item;
|
|
7211
|
-
if (typeof obj.text === "string") {
|
|
7212
|
-
return obj.text;
|
|
7213
|
-
}
|
|
7214
|
-
if (typeof obj.content === "string") {
|
|
7215
|
-
return obj.content;
|
|
7216
|
-
}
|
|
7217
|
-
return JSON.stringify(item, null, 2);
|
|
7218
|
-
}
|
|
7219
|
-
return String(item);
|
|
7220
|
-
}
|
|
7221
|
-
function extractObjectText(obj) {
|
|
7222
|
-
if (typeof obj.text === "string") {
|
|
7223
|
-
return obj.text;
|
|
7224
|
-
}
|
|
7225
|
-
if (typeof obj.content === "string") {
|
|
7226
|
-
return obj.content;
|
|
7227
|
-
}
|
|
7228
|
-
return JSON.stringify(obj, null, 2);
|
|
7229
|
-
}
|
|
7230
|
-
|
|
7231
|
-
// src/userResponse/knowledge-base.ts
|
|
7232
|
-
var getKnowledgeBase = async ({
|
|
7233
|
-
prompt,
|
|
7234
|
-
collections,
|
|
7235
|
-
topK = 1
|
|
7236
|
-
}) => {
|
|
7237
|
-
try {
|
|
7238
|
-
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["query"]) {
|
|
7239
|
-
logger.warn("[KnowledgeBase] knowledge-base.query collection not registered, skipping");
|
|
7240
|
-
return "";
|
|
7241
|
-
}
|
|
7242
|
-
const result = await collections["knowledge-base"]["query"]({
|
|
7243
|
-
prompt,
|
|
7244
|
-
topK
|
|
7245
|
-
});
|
|
7246
|
-
if (!result || !result.content) {
|
|
7247
|
-
logger.warn("[KnowledgeBase] No knowledge base results returned");
|
|
7248
|
-
return "";
|
|
7249
|
-
}
|
|
7250
|
-
logger.info(`[KnowledgeBase] Retrieved knowledge base context (${result.content.length} chars)`);
|
|
7251
|
-
if (result.metadata?.sources && result.metadata.sources.length > 0) {
|
|
7252
|
-
logger.warn(`[KnowledgeBase] Sources: ${result.metadata.sources.map((s) => s.title).join(", ")}`);
|
|
7253
|
-
}
|
|
7254
|
-
return result.content;
|
|
7255
|
-
} catch (error) {
|
|
7256
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
7257
|
-
logger.warn(`[KnowledgeBase] Error querying knowledge base: ${errorMsg}`);
|
|
7258
|
-
return "";
|
|
7259
|
-
}
|
|
7260
|
-
};
|
|
7261
|
-
var getGlobalKnowledgeBase = async ({
|
|
7262
|
-
collections,
|
|
7263
|
-
limit = 100
|
|
7264
|
-
}) => {
|
|
7265
|
-
try {
|
|
7266
|
-
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["getGlobal"]) {
|
|
7267
|
-
logger.warn("[KnowledgeBase] knowledge-base.getGlobal collection not registered, skipping");
|
|
7268
|
-
return "";
|
|
7269
|
-
}
|
|
7270
|
-
const result = await collections["knowledge-base"]["getGlobal"]({ limit });
|
|
7271
|
-
if (!result || !result.content) {
|
|
7272
|
-
logger.warn("[KnowledgeBase] No global knowledge base nodes found");
|
|
7273
|
-
return "";
|
|
7274
|
-
}
|
|
7275
|
-
logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} global knowledge base nodes`);
|
|
7276
|
-
return result.content;
|
|
7277
|
-
} catch (error) {
|
|
7278
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
7279
|
-
logger.warn(`[KnowledgeBase] Error fetching global knowledge base: ${errorMsg}`);
|
|
7280
|
-
return "";
|
|
7281
|
-
}
|
|
7282
|
-
};
|
|
7283
|
-
var getUserKnowledgeBase = async ({
|
|
7284
|
-
collections,
|
|
7285
|
-
userId,
|
|
7286
|
-
limit = 100
|
|
7287
|
-
}) => {
|
|
7288
|
-
try {
|
|
7289
|
-
if (!userId) {
|
|
7290
|
-
logger.warn("[KnowledgeBase] No userId provided, skipping user knowledge base");
|
|
7291
|
-
return "";
|
|
7292
|
-
}
|
|
7293
|
-
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["getByUser"]) {
|
|
7294
|
-
logger.warn("[KnowledgeBase] knowledge-base.getByUser collection not registered, skipping");
|
|
7295
|
-
return "";
|
|
7296
|
-
}
|
|
7297
|
-
const result = await collections["knowledge-base"]["getByUser"]({
|
|
7298
|
-
userId: String(userId),
|
|
7299
|
-
limit
|
|
7300
|
-
});
|
|
7301
|
-
if (!result || !result.content) {
|
|
7302
|
-
logger.info(`[KnowledgeBase] No user knowledge base nodes found for userId: ${userId}`);
|
|
7303
|
-
return "";
|
|
7304
|
-
}
|
|
7305
|
-
logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} user knowledge base nodes for userId: ${userId}`);
|
|
7306
|
-
return result.content;
|
|
7307
|
-
} catch (error) {
|
|
7308
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
7309
|
-
logger.warn(`[KnowledgeBase] Error fetching user knowledge base: ${errorMsg}`);
|
|
7310
|
-
return "";
|
|
7311
|
-
}
|
|
7312
|
-
};
|
|
7313
|
-
var getAllKnowledgeBase = async ({
|
|
7314
|
-
prompt,
|
|
7315
|
-
collections,
|
|
7316
|
-
userId,
|
|
7317
|
-
topK = 3
|
|
7318
|
-
}) => {
|
|
7319
|
-
const [globalContext, userContext, queryContext] = await Promise.all([
|
|
7320
|
-
getGlobalKnowledgeBase({ collections }),
|
|
7321
|
-
getUserKnowledgeBase({ collections, userId }),
|
|
7322
|
-
getKnowledgeBase({ prompt, collections, topK })
|
|
7323
|
-
]);
|
|
7324
|
-
let combinedContext = "";
|
|
7325
|
-
if (globalContext) {
|
|
7326
|
-
combinedContext += "## Global Knowledge Base\n";
|
|
7327
|
-
combinedContext += "The following information applies to all queries:\n\n";
|
|
7328
|
-
combinedContext += globalContext + "\n\n";
|
|
7329
|
-
}
|
|
7330
|
-
if (userContext) {
|
|
7331
|
-
combinedContext += "## User-Specific Knowledge Base\n";
|
|
7332
|
-
combinedContext += "The following information is specific to this user:\n\n";
|
|
7333
|
-
combinedContext += userContext + "\n\n";
|
|
7334
|
-
}
|
|
7335
|
-
if (queryContext) {
|
|
7336
|
-
combinedContext += "## Relevant Knowledge Base (Query-Matched)\n";
|
|
7337
|
-
combinedContext += "The following information is semantically relevant to the current query:\n\n";
|
|
7338
|
-
combinedContext += queryContext + "\n\n";
|
|
7339
|
-
}
|
|
7340
|
-
return {
|
|
7341
|
-
globalContext,
|
|
7342
|
-
userContext,
|
|
7343
|
-
queryContext,
|
|
7344
|
-
combinedContext: combinedContext.trim()
|
|
7345
|
-
};
|
|
7346
|
-
};
|
|
7347
|
-
var KB = {
|
|
7348
|
-
getKnowledgeBase,
|
|
7349
|
-
getGlobalKnowledgeBase,
|
|
7350
|
-
getUserKnowledgeBase,
|
|
7351
|
-
getAllKnowledgeBase
|
|
7352
|
-
};
|
|
7353
|
-
var knowledge_base_default = KB;
|
|
7354
|
-
|
|
7355
7363
|
// src/userResponse/agents/agent-component-generator.ts
|
|
7356
7364
|
async function generateAgentComponents(params) {
|
|
7357
7365
|
const startTime = Date.now();
|
|
@@ -7379,6 +7387,7 @@ async function generateAgentComponents(params) {
|
|
|
7379
7387
|
} else {
|
|
7380
7388
|
databaseRules = await promptLoader.loadDatabaseRules();
|
|
7381
7389
|
}
|
|
7390
|
+
let globalKnowledgeBase = "No global knowledge base available.";
|
|
7382
7391
|
let knowledgeBaseContext = "No additional knowledge base context available.";
|
|
7383
7392
|
if (collections) {
|
|
7384
7393
|
const kbResult = await knowledge_base_default.getAllKnowledgeBase({
|
|
@@ -7387,7 +7396,15 @@ async function generateAgentComponents(params) {
|
|
|
7387
7396
|
userId,
|
|
7388
7397
|
topK: KNOWLEDGE_BASE_TOP_K
|
|
7389
7398
|
});
|
|
7390
|
-
|
|
7399
|
+
globalKnowledgeBase = kbResult.globalContext || globalKnowledgeBase;
|
|
7400
|
+
const dynamicParts = [];
|
|
7401
|
+
if (kbResult.userContext) {
|
|
7402
|
+
dynamicParts.push("## User-Specific Knowledge Base\n" + kbResult.userContext);
|
|
7403
|
+
}
|
|
7404
|
+
if (kbResult.queryContext) {
|
|
7405
|
+
dynamicParts.push("## Relevant Knowledge Base (Query-Matched)\n" + kbResult.queryContext);
|
|
7406
|
+
}
|
|
7407
|
+
knowledgeBaseContext = dynamicParts.join("\n\n") || knowledgeBaseContext;
|
|
7391
7408
|
}
|
|
7392
7409
|
const prompts = await promptLoader.loadPrompts("match-text-components", {
|
|
7393
7410
|
USER_PROMPT: userPrompt || "",
|
|
@@ -7397,6 +7414,7 @@ async function generateAgentComponents(params) {
|
|
|
7397
7414
|
DATABASE_RULES: databaseRules,
|
|
7398
7415
|
DEFERRED_TOOLS: "No deferred external tools for this request.",
|
|
7399
7416
|
EXECUTED_TOOLS: executedToolsText,
|
|
7417
|
+
GLOBAL_KNOWLEDGE_BASE: globalKnowledgeBase,
|
|
7400
7418
|
KNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext,
|
|
7401
7419
|
CURRENT_DATETIME: getCurrentDateTimeForPrompt()
|
|
7402
7420
|
});
|
|
@@ -9780,6 +9798,41 @@ var OpenAILLM = class extends BaseLLM {
|
|
|
9780
9798
|
var openaiLLM = new OpenAILLM();
|
|
9781
9799
|
|
|
9782
9800
|
// src/userResponse/agent-user-response.ts
|
|
9801
|
+
function rehydrateCachedComponent(component) {
|
|
9802
|
+
const qMap = component?.props?.config?._queryMap;
|
|
9803
|
+
if (!qMap || typeof qMap !== "object" || Object.keys(qMap).length === 0) return component;
|
|
9804
|
+
const cloned = JSON.parse(JSON.stringify(component));
|
|
9805
|
+
const allValid = Object.keys(qMap).every((qId) => queryCache.getQuery(qId) !== null);
|
|
9806
|
+
if (allValid) {
|
|
9807
|
+
logger.info(`[AgentFlow] All ${Object.keys(qMap).length} cached queryIds still valid \u2014 reusing`);
|
|
9808
|
+
delete cloned.props.config._queryMap;
|
|
9809
|
+
return cloned;
|
|
9810
|
+
}
|
|
9811
|
+
const idMap = {};
|
|
9812
|
+
for (const [oldId, sql] of Object.entries(qMap)) {
|
|
9813
|
+
if (queryCache.getQuery(oldId) !== null) {
|
|
9814
|
+
idMap[oldId] = oldId;
|
|
9815
|
+
} else {
|
|
9816
|
+
idMap[oldId] = queryCache.storeQuery(sql);
|
|
9817
|
+
}
|
|
9818
|
+
}
|
|
9819
|
+
const components = cloned.props?.config?.components;
|
|
9820
|
+
if (Array.isArray(components)) {
|
|
9821
|
+
for (const comp of components) {
|
|
9822
|
+
const etqId = comp.props?.externalTool?.parameters?.queryId;
|
|
9823
|
+
if (etqId && idMap[etqId]) {
|
|
9824
|
+
comp.props.externalTool.parameters.queryId = idMap[etqId];
|
|
9825
|
+
}
|
|
9826
|
+
if (comp.props?.queryId && idMap[comp.props.queryId]) {
|
|
9827
|
+
comp.props.queryId = idMap[comp.props.queryId];
|
|
9828
|
+
}
|
|
9829
|
+
}
|
|
9830
|
+
}
|
|
9831
|
+
delete cloned.props.config._queryMap;
|
|
9832
|
+
const rehydratedCount = Object.values(idMap).filter((newId) => !Object.keys(qMap).includes(newId)).length;
|
|
9833
|
+
logger.info(`[AgentFlow] Re-hydrated ${rehydratedCount}/${Object.keys(qMap).length} expired queries with fresh queryIds`);
|
|
9834
|
+
return cloned;
|
|
9835
|
+
}
|
|
9783
9836
|
function getLLMInstance(provider) {
|
|
9784
9837
|
switch (provider) {
|
|
9785
9838
|
case "anthropic":
|
|
@@ -9799,6 +9852,8 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9799
9852
|
const providers = llmProviders || ["anthropic"];
|
|
9800
9853
|
const provider = providers[0];
|
|
9801
9854
|
const llmInstance = getLLMInstance(provider);
|
|
9855
|
+
logger.clearFile();
|
|
9856
|
+
logger.logLLMPrompt("agentUserResponse", "user", `User Prompt: ${prompt}`);
|
|
9802
9857
|
logger.info(`[AgentFlow] Starting | provider: ${provider} | prompt: "${prompt.substring(0, 50)}..."`);
|
|
9803
9858
|
try {
|
|
9804
9859
|
const conversationMatch = await conversation_search_default.searchConversationsWithReranking({
|
|
@@ -9819,11 +9874,12 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9819
9874
|
}
|
|
9820
9875
|
const elapsedTime2 = Date.now() - startTime;
|
|
9821
9876
|
logger.info(`[AgentFlow] Exact match \u2014 returning cached result (${elapsedTime2}ms)`);
|
|
9877
|
+
const rehydratedComponent = component ? rehydrateCachedComponent(component) : null;
|
|
9822
9878
|
return {
|
|
9823
9879
|
success: true,
|
|
9824
9880
|
data: {
|
|
9825
9881
|
text: cachedTextResponse,
|
|
9826
|
-
component,
|
|
9882
|
+
component: rehydratedComponent,
|
|
9827
9883
|
actions: conversationMatch.uiBlock?.actions || [],
|
|
9828
9884
|
reasoning: `Exact match from previous conversation`,
|
|
9829
9885
|
method: `${provider}-agent-semantic-match`,
|
|
@@ -9861,9 +9917,30 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9861
9917
|
userProvidedData: t.userProvidedData,
|
|
9862
9918
|
params: t.params
|
|
9863
9919
|
}));
|
|
9920
|
+
let globalKnowledgeBase = "";
|
|
9921
|
+
let knowledgeBaseContext = "";
|
|
9922
|
+
if (collections) {
|
|
9923
|
+
const kbResult = await knowledge_base_default.getAllKnowledgeBase({
|
|
9924
|
+
prompt,
|
|
9925
|
+
collections,
|
|
9926
|
+
userId,
|
|
9927
|
+
topK: KNOWLEDGE_BASE_TOP_K
|
|
9928
|
+
});
|
|
9929
|
+
globalKnowledgeBase = kbResult.globalContext || "";
|
|
9930
|
+
const dynamicParts = [];
|
|
9931
|
+
if (kbResult.userContext) {
|
|
9932
|
+
dynamicParts.push("## User-Specific Knowledge Base\n" + kbResult.userContext);
|
|
9933
|
+
}
|
|
9934
|
+
if (kbResult.queryContext) {
|
|
9935
|
+
dynamicParts.push("## Relevant Knowledge Base (Query-Matched)\n" + kbResult.queryContext);
|
|
9936
|
+
}
|
|
9937
|
+
knowledgeBaseContext = dynamicParts.join("\n\n") || "";
|
|
9938
|
+
}
|
|
9864
9939
|
const agentConfig = {
|
|
9865
9940
|
...DEFAULT_AGENT_CONFIG,
|
|
9866
|
-
apiKey: apiKey || void 0
|
|
9941
|
+
apiKey: apiKey || void 0,
|
|
9942
|
+
globalKnowledgeBase,
|
|
9943
|
+
knowledgeBaseContext
|
|
9867
9944
|
};
|
|
9868
9945
|
const streamBuffer = new StreamBuffer(streamCallback);
|
|
9869
9946
|
const mainAgent = new MainAgent(agentTools, agentConfig, streamBuffer);
|
|
@@ -9932,6 +10009,7 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9932
10009
|
layoutDescription = matchResult.layoutDescription;
|
|
9933
10010
|
actions = matchResult.actions;
|
|
9934
10011
|
}
|
|
10012
|
+
const queryMap = {};
|
|
9935
10013
|
const securedComponents = matchedComponents.map((comp) => {
|
|
9936
10014
|
const props = { ...comp.props };
|
|
9937
10015
|
const sqlValue = props.externalTool?.parameters?.sql || props.externalTool?.parameters?.query;
|
|
@@ -9939,6 +10017,7 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9939
10017
|
const { sql, query, ...restParams } = props.externalTool.parameters;
|
|
9940
10018
|
const cachedData = queryCache.get(sqlValue);
|
|
9941
10019
|
const queryId = queryCache.storeQuery(sqlValue, cachedData);
|
|
10020
|
+
queryMap[queryId] = sqlValue;
|
|
9942
10021
|
props.externalTool = {
|
|
9943
10022
|
...props.externalTool,
|
|
9944
10023
|
parameters: { queryId, ...restParams }
|
|
@@ -9947,6 +10026,7 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9947
10026
|
if (props.query) {
|
|
9948
10027
|
const { query, ...restProps } = props;
|
|
9949
10028
|
const queryId = queryCache.storeQuery(query);
|
|
10029
|
+
queryMap[queryId] = query;
|
|
9950
10030
|
return { ...comp, props: { ...restProps, queryId } };
|
|
9951
10031
|
}
|
|
9952
10032
|
return { ...comp, props };
|
|
@@ -9962,7 +10042,9 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9962
10042
|
config: {
|
|
9963
10043
|
title: layoutTitle,
|
|
9964
10044
|
description: layoutDescription,
|
|
9965
|
-
components: securedComponents
|
|
10045
|
+
components: securedComponents,
|
|
10046
|
+
// Persist queryId → SQL mapping so cached responses can re-register queries
|
|
10047
|
+
...Object.keys(queryMap).length > 0 && { _queryMap: queryMap }
|
|
9966
10048
|
},
|
|
9967
10049
|
actions
|
|
9968
10050
|
}
|