@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.js
CHANGED
|
@@ -5020,6 +5020,130 @@ async function withProgressHeartbeat(operation, progressMessage, streamBuffer, i
|
|
|
5020
5020
|
}
|
|
5021
5021
|
}
|
|
5022
5022
|
|
|
5023
|
+
// src/userResponse/knowledge-base.ts
|
|
5024
|
+
var getKnowledgeBase = async ({
|
|
5025
|
+
prompt,
|
|
5026
|
+
collections,
|
|
5027
|
+
topK = 1
|
|
5028
|
+
}) => {
|
|
5029
|
+
try {
|
|
5030
|
+
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["query"]) {
|
|
5031
|
+
logger.warn("[KnowledgeBase] knowledge-base.query collection not registered, skipping");
|
|
5032
|
+
return "";
|
|
5033
|
+
}
|
|
5034
|
+
const result = await collections["knowledge-base"]["query"]({
|
|
5035
|
+
prompt,
|
|
5036
|
+
topK
|
|
5037
|
+
});
|
|
5038
|
+
if (!result || !result.content) {
|
|
5039
|
+
logger.warn("[KnowledgeBase] No knowledge base results returned");
|
|
5040
|
+
return "";
|
|
5041
|
+
}
|
|
5042
|
+
logger.info(`[KnowledgeBase] Retrieved knowledge base context (${result.content.length} chars)`);
|
|
5043
|
+
if (result.metadata?.sources && result.metadata.sources.length > 0) {
|
|
5044
|
+
logger.warn(`[KnowledgeBase] Sources: ${result.metadata.sources.map((s) => s.title).join(", ")}`);
|
|
5045
|
+
}
|
|
5046
|
+
return result.content;
|
|
5047
|
+
} catch (error) {
|
|
5048
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
5049
|
+
logger.warn(`[KnowledgeBase] Error querying knowledge base: ${errorMsg}`);
|
|
5050
|
+
return "";
|
|
5051
|
+
}
|
|
5052
|
+
};
|
|
5053
|
+
var getGlobalKnowledgeBase = async ({
|
|
5054
|
+
collections,
|
|
5055
|
+
limit = 100
|
|
5056
|
+
}) => {
|
|
5057
|
+
try {
|
|
5058
|
+
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["getGlobal"]) {
|
|
5059
|
+
logger.warn("[KnowledgeBase] knowledge-base.getGlobal collection not registered, skipping");
|
|
5060
|
+
return "";
|
|
5061
|
+
}
|
|
5062
|
+
const result = await collections["knowledge-base"]["getGlobal"]({ limit });
|
|
5063
|
+
if (!result || !result.content) {
|
|
5064
|
+
logger.warn("[KnowledgeBase] No global knowledge base nodes found");
|
|
5065
|
+
return "";
|
|
5066
|
+
}
|
|
5067
|
+
logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} global knowledge base nodes`);
|
|
5068
|
+
return result.content;
|
|
5069
|
+
} catch (error) {
|
|
5070
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
5071
|
+
logger.warn(`[KnowledgeBase] Error fetching global knowledge base: ${errorMsg}`);
|
|
5072
|
+
return "";
|
|
5073
|
+
}
|
|
5074
|
+
};
|
|
5075
|
+
var getUserKnowledgeBase = async ({
|
|
5076
|
+
collections,
|
|
5077
|
+
userId,
|
|
5078
|
+
limit = 100
|
|
5079
|
+
}) => {
|
|
5080
|
+
try {
|
|
5081
|
+
if (!userId) {
|
|
5082
|
+
logger.warn("[KnowledgeBase] No userId provided, skipping user knowledge base");
|
|
5083
|
+
return "";
|
|
5084
|
+
}
|
|
5085
|
+
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["getByUser"]) {
|
|
5086
|
+
logger.warn("[KnowledgeBase] knowledge-base.getByUser collection not registered, skipping");
|
|
5087
|
+
return "";
|
|
5088
|
+
}
|
|
5089
|
+
const result = await collections["knowledge-base"]["getByUser"]({
|
|
5090
|
+
userId: String(userId),
|
|
5091
|
+
limit
|
|
5092
|
+
});
|
|
5093
|
+
if (!result || !result.content) {
|
|
5094
|
+
logger.info(`[KnowledgeBase] No user knowledge base nodes found for userId: ${userId}`);
|
|
5095
|
+
return "";
|
|
5096
|
+
}
|
|
5097
|
+
logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} user knowledge base nodes for userId: ${userId}`);
|
|
5098
|
+
return result.content;
|
|
5099
|
+
} catch (error) {
|
|
5100
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
5101
|
+
logger.warn(`[KnowledgeBase] Error fetching user knowledge base: ${errorMsg}`);
|
|
5102
|
+
return "";
|
|
5103
|
+
}
|
|
5104
|
+
};
|
|
5105
|
+
var getAllKnowledgeBase = async ({
|
|
5106
|
+
prompt,
|
|
5107
|
+
collections,
|
|
5108
|
+
userId,
|
|
5109
|
+
topK = 3
|
|
5110
|
+
}) => {
|
|
5111
|
+
const [globalContext, userContext, queryContext] = await Promise.all([
|
|
5112
|
+
getGlobalKnowledgeBase({ collections }),
|
|
5113
|
+
getUserKnowledgeBase({ collections, userId }),
|
|
5114
|
+
getKnowledgeBase({ prompt, collections, topK })
|
|
5115
|
+
]);
|
|
5116
|
+
let combinedContext = "";
|
|
5117
|
+
if (globalContext) {
|
|
5118
|
+
combinedContext += "## Global Knowledge Base\n";
|
|
5119
|
+
combinedContext += "The following information applies to all queries:\n\n";
|
|
5120
|
+
combinedContext += globalContext + "\n\n";
|
|
5121
|
+
}
|
|
5122
|
+
if (userContext) {
|
|
5123
|
+
combinedContext += "## User-Specific Knowledge Base\n";
|
|
5124
|
+
combinedContext += "The following information is specific to this user:\n\n";
|
|
5125
|
+
combinedContext += userContext + "\n\n";
|
|
5126
|
+
}
|
|
5127
|
+
if (queryContext) {
|
|
5128
|
+
combinedContext += "## Relevant Knowledge Base (Query-Matched)\n";
|
|
5129
|
+
combinedContext += "The following information is semantically relevant to the current query:\n\n";
|
|
5130
|
+
combinedContext += queryContext + "\n\n";
|
|
5131
|
+
}
|
|
5132
|
+
return {
|
|
5133
|
+
globalContext,
|
|
5134
|
+
userContext,
|
|
5135
|
+
queryContext,
|
|
5136
|
+
combinedContext: combinedContext.trim()
|
|
5137
|
+
};
|
|
5138
|
+
};
|
|
5139
|
+
var KB = {
|
|
5140
|
+
getKnowledgeBase,
|
|
5141
|
+
getGlobalKnowledgeBase,
|
|
5142
|
+
getUserKnowledgeBase,
|
|
5143
|
+
getAllKnowledgeBase
|
|
5144
|
+
};
|
|
5145
|
+
var knowledge_base_default = KB;
|
|
5146
|
+
|
|
5023
5147
|
// src/llm.ts
|
|
5024
5148
|
var import_sdk = __toESM(require("@anthropic-ai/sdk"));
|
|
5025
5149
|
var import_groq_sdk = __toESM(require("groq-sdk"));
|
|
@@ -6598,6 +6722,48 @@ function getCurrentDateTimeForPrompt() {
|
|
|
6598
6722
|
});
|
|
6599
6723
|
}
|
|
6600
6724
|
|
|
6725
|
+
// src/userResponse/prompt-extractor.ts
|
|
6726
|
+
function extractPromptText(content) {
|
|
6727
|
+
if (content === null || content === void 0) {
|
|
6728
|
+
return "";
|
|
6729
|
+
}
|
|
6730
|
+
if (typeof content === "string") {
|
|
6731
|
+
return content;
|
|
6732
|
+
}
|
|
6733
|
+
if (Array.isArray(content)) {
|
|
6734
|
+
return content.map((item) => extractContentBlockText(item)).filter((text) => text.length > 0).join("\n\n---\n\n");
|
|
6735
|
+
}
|
|
6736
|
+
if (content && typeof content === "object") {
|
|
6737
|
+
return extractObjectText(content);
|
|
6738
|
+
}
|
|
6739
|
+
return String(content);
|
|
6740
|
+
}
|
|
6741
|
+
function extractContentBlockText(item) {
|
|
6742
|
+
if (typeof item === "string") {
|
|
6743
|
+
return item;
|
|
6744
|
+
}
|
|
6745
|
+
if (item && typeof item === "object") {
|
|
6746
|
+
const obj = item;
|
|
6747
|
+
if (typeof obj.text === "string") {
|
|
6748
|
+
return obj.text;
|
|
6749
|
+
}
|
|
6750
|
+
if (typeof obj.content === "string") {
|
|
6751
|
+
return obj.content;
|
|
6752
|
+
}
|
|
6753
|
+
return JSON.stringify(item, null, 2);
|
|
6754
|
+
}
|
|
6755
|
+
return String(item);
|
|
6756
|
+
}
|
|
6757
|
+
function extractObjectText(obj) {
|
|
6758
|
+
if (typeof obj.text === "string") {
|
|
6759
|
+
return obj.text;
|
|
6760
|
+
}
|
|
6761
|
+
if (typeof obj.content === "string") {
|
|
6762
|
+
return obj.content;
|
|
6763
|
+
}
|
|
6764
|
+
return JSON.stringify(obj, null, 2);
|
|
6765
|
+
}
|
|
6766
|
+
|
|
6601
6767
|
// src/userResponse/agents/agent-prompt-builder.ts
|
|
6602
6768
|
function buildSourceSummaries(externalTools) {
|
|
6603
6769
|
return externalTools.map((tool) => {
|
|
@@ -6740,6 +6906,8 @@ var SourceAgent = class {
|
|
|
6740
6906
|
}
|
|
6741
6907
|
try {
|
|
6742
6908
|
const prompts = await this.buildPrompt(intent, aggregation);
|
|
6909
|
+
logger.logLLMPrompt(`sourceAgent:${this.tool.name}`, "system", extractPromptText(prompts.system));
|
|
6910
|
+
logger.logLLMPrompt(`sourceAgent:${this.tool.name}`, "user", prompts.user);
|
|
6743
6911
|
const llmTool = this.buildLLMToolDefinition();
|
|
6744
6912
|
let executedTool = null;
|
|
6745
6913
|
let resultData = [];
|
|
@@ -6924,7 +7092,9 @@ Analyze the error and try again with a corrected query.`;
|
|
|
6924
7092
|
FULL_SCHEMA: fullSchema,
|
|
6925
7093
|
MAX_ROWS: String(this.config.maxRowsPerSource),
|
|
6926
7094
|
AGGREGATION_MODE: aggregation,
|
|
7095
|
+
GLOBAL_KNOWLEDGE_BASE: this.config.globalKnowledgeBase || "No global knowledge base available.",
|
|
6927
7096
|
CURRENT_DATETIME: getCurrentDateTimeForPrompt(),
|
|
7097
|
+
KNOWLEDGE_BASE_CONTEXT: this.config.knowledgeBaseContext || "No additional knowledge base context available.",
|
|
6928
7098
|
INTENT: intent
|
|
6929
7099
|
});
|
|
6930
7100
|
return { system: prompts.system, user: prompts.user };
|
|
@@ -7020,6 +7190,8 @@ var MainAgent = class {
|
|
|
7020
7190
|
const summaries = buildSourceSummaries(this.externalTools);
|
|
7021
7191
|
logger.info(`[MainAgent] ${summaries.length} source(s) available`);
|
|
7022
7192
|
const systemPrompt = await this.buildSystemPrompt(summaries, conversationHistory);
|
|
7193
|
+
logger.logLLMPrompt("mainAgent", "system", extractPromptText(systemPrompt));
|
|
7194
|
+
logger.logLLMPrompt("mainAgent", "user", userPrompt);
|
|
7023
7195
|
const tools = this.buildSourceToolDefinitions(summaries);
|
|
7024
7196
|
const sourceResults = [];
|
|
7025
7197
|
const executedTools = [];
|
|
@@ -7078,7 +7250,9 @@ var MainAgent = class {
|
|
|
7078
7250
|
const prompts = await promptLoader.loadPrompts("agent-main", {
|
|
7079
7251
|
SOURCE_SUMMARIES: summariesText,
|
|
7080
7252
|
MAX_ROWS: String(this.config.maxRowsPerSource),
|
|
7253
|
+
GLOBAL_KNOWLEDGE_BASE: this.config.globalKnowledgeBase || "No global knowledge base available.",
|
|
7081
7254
|
CURRENT_DATETIME: getCurrentDateTimeForPrompt(),
|
|
7255
|
+
KNOWLEDGE_BASE_CONTEXT: this.config.knowledgeBaseContext || "No additional knowledge base context available.",
|
|
7082
7256
|
CONVERSATION_HISTORY: conversationHistory || "No previous conversation"
|
|
7083
7257
|
});
|
|
7084
7258
|
return prompts.system;
|
|
@@ -7243,172 +7417,6 @@ function processComponentProps(props, executedTools, config) {
|
|
|
7243
7417
|
return cleanedProps;
|
|
7244
7418
|
}
|
|
7245
7419
|
|
|
7246
|
-
// src/userResponse/prompt-extractor.ts
|
|
7247
|
-
function extractPromptText(content) {
|
|
7248
|
-
if (content === null || content === void 0) {
|
|
7249
|
-
return "";
|
|
7250
|
-
}
|
|
7251
|
-
if (typeof content === "string") {
|
|
7252
|
-
return content;
|
|
7253
|
-
}
|
|
7254
|
-
if (Array.isArray(content)) {
|
|
7255
|
-
return content.map((item) => extractContentBlockText(item)).filter((text) => text.length > 0).join("\n\n---\n\n");
|
|
7256
|
-
}
|
|
7257
|
-
if (content && typeof content === "object") {
|
|
7258
|
-
return extractObjectText(content);
|
|
7259
|
-
}
|
|
7260
|
-
return String(content);
|
|
7261
|
-
}
|
|
7262
|
-
function extractContentBlockText(item) {
|
|
7263
|
-
if (typeof item === "string") {
|
|
7264
|
-
return item;
|
|
7265
|
-
}
|
|
7266
|
-
if (item && typeof item === "object") {
|
|
7267
|
-
const obj = item;
|
|
7268
|
-
if (typeof obj.text === "string") {
|
|
7269
|
-
return obj.text;
|
|
7270
|
-
}
|
|
7271
|
-
if (typeof obj.content === "string") {
|
|
7272
|
-
return obj.content;
|
|
7273
|
-
}
|
|
7274
|
-
return JSON.stringify(item, null, 2);
|
|
7275
|
-
}
|
|
7276
|
-
return String(item);
|
|
7277
|
-
}
|
|
7278
|
-
function extractObjectText(obj) {
|
|
7279
|
-
if (typeof obj.text === "string") {
|
|
7280
|
-
return obj.text;
|
|
7281
|
-
}
|
|
7282
|
-
if (typeof obj.content === "string") {
|
|
7283
|
-
return obj.content;
|
|
7284
|
-
}
|
|
7285
|
-
return JSON.stringify(obj, null, 2);
|
|
7286
|
-
}
|
|
7287
|
-
|
|
7288
|
-
// src/userResponse/knowledge-base.ts
|
|
7289
|
-
var getKnowledgeBase = async ({
|
|
7290
|
-
prompt,
|
|
7291
|
-
collections,
|
|
7292
|
-
topK = 1
|
|
7293
|
-
}) => {
|
|
7294
|
-
try {
|
|
7295
|
-
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["query"]) {
|
|
7296
|
-
logger.warn("[KnowledgeBase] knowledge-base.query collection not registered, skipping");
|
|
7297
|
-
return "";
|
|
7298
|
-
}
|
|
7299
|
-
const result = await collections["knowledge-base"]["query"]({
|
|
7300
|
-
prompt,
|
|
7301
|
-
topK
|
|
7302
|
-
});
|
|
7303
|
-
if (!result || !result.content) {
|
|
7304
|
-
logger.warn("[KnowledgeBase] No knowledge base results returned");
|
|
7305
|
-
return "";
|
|
7306
|
-
}
|
|
7307
|
-
logger.info(`[KnowledgeBase] Retrieved knowledge base context (${result.content.length} chars)`);
|
|
7308
|
-
if (result.metadata?.sources && result.metadata.sources.length > 0) {
|
|
7309
|
-
logger.warn(`[KnowledgeBase] Sources: ${result.metadata.sources.map((s) => s.title).join(", ")}`);
|
|
7310
|
-
}
|
|
7311
|
-
return result.content;
|
|
7312
|
-
} catch (error) {
|
|
7313
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
7314
|
-
logger.warn(`[KnowledgeBase] Error querying knowledge base: ${errorMsg}`);
|
|
7315
|
-
return "";
|
|
7316
|
-
}
|
|
7317
|
-
};
|
|
7318
|
-
var getGlobalKnowledgeBase = async ({
|
|
7319
|
-
collections,
|
|
7320
|
-
limit = 100
|
|
7321
|
-
}) => {
|
|
7322
|
-
try {
|
|
7323
|
-
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["getGlobal"]) {
|
|
7324
|
-
logger.warn("[KnowledgeBase] knowledge-base.getGlobal collection not registered, skipping");
|
|
7325
|
-
return "";
|
|
7326
|
-
}
|
|
7327
|
-
const result = await collections["knowledge-base"]["getGlobal"]({ limit });
|
|
7328
|
-
if (!result || !result.content) {
|
|
7329
|
-
logger.warn("[KnowledgeBase] No global knowledge base nodes found");
|
|
7330
|
-
return "";
|
|
7331
|
-
}
|
|
7332
|
-
logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} global knowledge base nodes`);
|
|
7333
|
-
return result.content;
|
|
7334
|
-
} catch (error) {
|
|
7335
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
7336
|
-
logger.warn(`[KnowledgeBase] Error fetching global knowledge base: ${errorMsg}`);
|
|
7337
|
-
return "";
|
|
7338
|
-
}
|
|
7339
|
-
};
|
|
7340
|
-
var getUserKnowledgeBase = async ({
|
|
7341
|
-
collections,
|
|
7342
|
-
userId,
|
|
7343
|
-
limit = 100
|
|
7344
|
-
}) => {
|
|
7345
|
-
try {
|
|
7346
|
-
if (!userId) {
|
|
7347
|
-
logger.warn("[KnowledgeBase] No userId provided, skipping user knowledge base");
|
|
7348
|
-
return "";
|
|
7349
|
-
}
|
|
7350
|
-
if (!collections || !collections["knowledge-base"] || !collections["knowledge-base"]["getByUser"]) {
|
|
7351
|
-
logger.warn("[KnowledgeBase] knowledge-base.getByUser collection not registered, skipping");
|
|
7352
|
-
return "";
|
|
7353
|
-
}
|
|
7354
|
-
const result = await collections["knowledge-base"]["getByUser"]({
|
|
7355
|
-
userId: String(userId),
|
|
7356
|
-
limit
|
|
7357
|
-
});
|
|
7358
|
-
if (!result || !result.content) {
|
|
7359
|
-
logger.info(`[KnowledgeBase] No user knowledge base nodes found for userId: ${userId}`);
|
|
7360
|
-
return "";
|
|
7361
|
-
}
|
|
7362
|
-
logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} user knowledge base nodes for userId: ${userId}`);
|
|
7363
|
-
return result.content;
|
|
7364
|
-
} catch (error) {
|
|
7365
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
7366
|
-
logger.warn(`[KnowledgeBase] Error fetching user knowledge base: ${errorMsg}`);
|
|
7367
|
-
return "";
|
|
7368
|
-
}
|
|
7369
|
-
};
|
|
7370
|
-
var getAllKnowledgeBase = async ({
|
|
7371
|
-
prompt,
|
|
7372
|
-
collections,
|
|
7373
|
-
userId,
|
|
7374
|
-
topK = 3
|
|
7375
|
-
}) => {
|
|
7376
|
-
const [globalContext, userContext, queryContext] = await Promise.all([
|
|
7377
|
-
getGlobalKnowledgeBase({ collections }),
|
|
7378
|
-
getUserKnowledgeBase({ collections, userId }),
|
|
7379
|
-
getKnowledgeBase({ prompt, collections, topK })
|
|
7380
|
-
]);
|
|
7381
|
-
let combinedContext = "";
|
|
7382
|
-
if (globalContext) {
|
|
7383
|
-
combinedContext += "## Global Knowledge Base\n";
|
|
7384
|
-
combinedContext += "The following information applies to all queries:\n\n";
|
|
7385
|
-
combinedContext += globalContext + "\n\n";
|
|
7386
|
-
}
|
|
7387
|
-
if (userContext) {
|
|
7388
|
-
combinedContext += "## User-Specific Knowledge Base\n";
|
|
7389
|
-
combinedContext += "The following information is specific to this user:\n\n";
|
|
7390
|
-
combinedContext += userContext + "\n\n";
|
|
7391
|
-
}
|
|
7392
|
-
if (queryContext) {
|
|
7393
|
-
combinedContext += "## Relevant Knowledge Base (Query-Matched)\n";
|
|
7394
|
-
combinedContext += "The following information is semantically relevant to the current query:\n\n";
|
|
7395
|
-
combinedContext += queryContext + "\n\n";
|
|
7396
|
-
}
|
|
7397
|
-
return {
|
|
7398
|
-
globalContext,
|
|
7399
|
-
userContext,
|
|
7400
|
-
queryContext,
|
|
7401
|
-
combinedContext: combinedContext.trim()
|
|
7402
|
-
};
|
|
7403
|
-
};
|
|
7404
|
-
var KB = {
|
|
7405
|
-
getKnowledgeBase,
|
|
7406
|
-
getGlobalKnowledgeBase,
|
|
7407
|
-
getUserKnowledgeBase,
|
|
7408
|
-
getAllKnowledgeBase
|
|
7409
|
-
};
|
|
7410
|
-
var knowledge_base_default = KB;
|
|
7411
|
-
|
|
7412
7420
|
// src/userResponse/agents/agent-component-generator.ts
|
|
7413
7421
|
async function generateAgentComponents(params) {
|
|
7414
7422
|
const startTime = Date.now();
|
|
@@ -7436,6 +7444,7 @@ async function generateAgentComponents(params) {
|
|
|
7436
7444
|
} else {
|
|
7437
7445
|
databaseRules = await promptLoader.loadDatabaseRules();
|
|
7438
7446
|
}
|
|
7447
|
+
let globalKnowledgeBase = "No global knowledge base available.";
|
|
7439
7448
|
let knowledgeBaseContext = "No additional knowledge base context available.";
|
|
7440
7449
|
if (collections) {
|
|
7441
7450
|
const kbResult = await knowledge_base_default.getAllKnowledgeBase({
|
|
@@ -7444,7 +7453,15 @@ async function generateAgentComponents(params) {
|
|
|
7444
7453
|
userId,
|
|
7445
7454
|
topK: KNOWLEDGE_BASE_TOP_K
|
|
7446
7455
|
});
|
|
7447
|
-
|
|
7456
|
+
globalKnowledgeBase = kbResult.globalContext || globalKnowledgeBase;
|
|
7457
|
+
const dynamicParts = [];
|
|
7458
|
+
if (kbResult.userContext) {
|
|
7459
|
+
dynamicParts.push("## User-Specific Knowledge Base\n" + kbResult.userContext);
|
|
7460
|
+
}
|
|
7461
|
+
if (kbResult.queryContext) {
|
|
7462
|
+
dynamicParts.push("## Relevant Knowledge Base (Query-Matched)\n" + kbResult.queryContext);
|
|
7463
|
+
}
|
|
7464
|
+
knowledgeBaseContext = dynamicParts.join("\n\n") || knowledgeBaseContext;
|
|
7448
7465
|
}
|
|
7449
7466
|
const prompts = await promptLoader.loadPrompts("match-text-components", {
|
|
7450
7467
|
USER_PROMPT: userPrompt || "",
|
|
@@ -7454,6 +7471,7 @@ async function generateAgentComponents(params) {
|
|
|
7454
7471
|
DATABASE_RULES: databaseRules,
|
|
7455
7472
|
DEFERRED_TOOLS: "No deferred external tools for this request.",
|
|
7456
7473
|
EXECUTED_TOOLS: executedToolsText,
|
|
7474
|
+
GLOBAL_KNOWLEDGE_BASE: globalKnowledgeBase,
|
|
7457
7475
|
KNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext,
|
|
7458
7476
|
CURRENT_DATETIME: getCurrentDateTimeForPrompt()
|
|
7459
7477
|
});
|
|
@@ -9837,6 +9855,41 @@ var OpenAILLM = class extends BaseLLM {
|
|
|
9837
9855
|
var openaiLLM = new OpenAILLM();
|
|
9838
9856
|
|
|
9839
9857
|
// src/userResponse/agent-user-response.ts
|
|
9858
|
+
function rehydrateCachedComponent(component) {
|
|
9859
|
+
const qMap = component?.props?.config?._queryMap;
|
|
9860
|
+
if (!qMap || typeof qMap !== "object" || Object.keys(qMap).length === 0) return component;
|
|
9861
|
+
const cloned = JSON.parse(JSON.stringify(component));
|
|
9862
|
+
const allValid = Object.keys(qMap).every((qId) => queryCache.getQuery(qId) !== null);
|
|
9863
|
+
if (allValid) {
|
|
9864
|
+
logger.info(`[AgentFlow] All ${Object.keys(qMap).length} cached queryIds still valid \u2014 reusing`);
|
|
9865
|
+
delete cloned.props.config._queryMap;
|
|
9866
|
+
return cloned;
|
|
9867
|
+
}
|
|
9868
|
+
const idMap = {};
|
|
9869
|
+
for (const [oldId, sql] of Object.entries(qMap)) {
|
|
9870
|
+
if (queryCache.getQuery(oldId) !== null) {
|
|
9871
|
+
idMap[oldId] = oldId;
|
|
9872
|
+
} else {
|
|
9873
|
+
idMap[oldId] = queryCache.storeQuery(sql);
|
|
9874
|
+
}
|
|
9875
|
+
}
|
|
9876
|
+
const components = cloned.props?.config?.components;
|
|
9877
|
+
if (Array.isArray(components)) {
|
|
9878
|
+
for (const comp of components) {
|
|
9879
|
+
const etqId = comp.props?.externalTool?.parameters?.queryId;
|
|
9880
|
+
if (etqId && idMap[etqId]) {
|
|
9881
|
+
comp.props.externalTool.parameters.queryId = idMap[etqId];
|
|
9882
|
+
}
|
|
9883
|
+
if (comp.props?.queryId && idMap[comp.props.queryId]) {
|
|
9884
|
+
comp.props.queryId = idMap[comp.props.queryId];
|
|
9885
|
+
}
|
|
9886
|
+
}
|
|
9887
|
+
}
|
|
9888
|
+
delete cloned.props.config._queryMap;
|
|
9889
|
+
const rehydratedCount = Object.values(idMap).filter((newId) => !Object.keys(qMap).includes(newId)).length;
|
|
9890
|
+
logger.info(`[AgentFlow] Re-hydrated ${rehydratedCount}/${Object.keys(qMap).length} expired queries with fresh queryIds`);
|
|
9891
|
+
return cloned;
|
|
9892
|
+
}
|
|
9840
9893
|
function getLLMInstance(provider) {
|
|
9841
9894
|
switch (provider) {
|
|
9842
9895
|
case "anthropic":
|
|
@@ -9856,6 +9909,8 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9856
9909
|
const providers = llmProviders || ["anthropic"];
|
|
9857
9910
|
const provider = providers[0];
|
|
9858
9911
|
const llmInstance = getLLMInstance(provider);
|
|
9912
|
+
logger.clearFile();
|
|
9913
|
+
logger.logLLMPrompt("agentUserResponse", "user", `User Prompt: ${prompt}`);
|
|
9859
9914
|
logger.info(`[AgentFlow] Starting | provider: ${provider} | prompt: "${prompt.substring(0, 50)}..."`);
|
|
9860
9915
|
try {
|
|
9861
9916
|
const conversationMatch = await conversation_search_default.searchConversationsWithReranking({
|
|
@@ -9876,11 +9931,12 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9876
9931
|
}
|
|
9877
9932
|
const elapsedTime2 = Date.now() - startTime;
|
|
9878
9933
|
logger.info(`[AgentFlow] Exact match \u2014 returning cached result (${elapsedTime2}ms)`);
|
|
9934
|
+
const rehydratedComponent = component ? rehydrateCachedComponent(component) : null;
|
|
9879
9935
|
return {
|
|
9880
9936
|
success: true,
|
|
9881
9937
|
data: {
|
|
9882
9938
|
text: cachedTextResponse,
|
|
9883
|
-
component,
|
|
9939
|
+
component: rehydratedComponent,
|
|
9884
9940
|
actions: conversationMatch.uiBlock?.actions || [],
|
|
9885
9941
|
reasoning: `Exact match from previous conversation`,
|
|
9886
9942
|
method: `${provider}-agent-semantic-match`,
|
|
@@ -9918,9 +9974,30 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9918
9974
|
userProvidedData: t.userProvidedData,
|
|
9919
9975
|
params: t.params
|
|
9920
9976
|
}));
|
|
9977
|
+
let globalKnowledgeBase = "";
|
|
9978
|
+
let knowledgeBaseContext = "";
|
|
9979
|
+
if (collections) {
|
|
9980
|
+
const kbResult = await knowledge_base_default.getAllKnowledgeBase({
|
|
9981
|
+
prompt,
|
|
9982
|
+
collections,
|
|
9983
|
+
userId,
|
|
9984
|
+
topK: KNOWLEDGE_BASE_TOP_K
|
|
9985
|
+
});
|
|
9986
|
+
globalKnowledgeBase = kbResult.globalContext || "";
|
|
9987
|
+
const dynamicParts = [];
|
|
9988
|
+
if (kbResult.userContext) {
|
|
9989
|
+
dynamicParts.push("## User-Specific Knowledge Base\n" + kbResult.userContext);
|
|
9990
|
+
}
|
|
9991
|
+
if (kbResult.queryContext) {
|
|
9992
|
+
dynamicParts.push("## Relevant Knowledge Base (Query-Matched)\n" + kbResult.queryContext);
|
|
9993
|
+
}
|
|
9994
|
+
knowledgeBaseContext = dynamicParts.join("\n\n") || "";
|
|
9995
|
+
}
|
|
9921
9996
|
const agentConfig = {
|
|
9922
9997
|
...DEFAULT_AGENT_CONFIG,
|
|
9923
|
-
apiKey: apiKey || void 0
|
|
9998
|
+
apiKey: apiKey || void 0,
|
|
9999
|
+
globalKnowledgeBase,
|
|
10000
|
+
knowledgeBaseContext
|
|
9924
10001
|
};
|
|
9925
10002
|
const streamBuffer = new StreamBuffer(streamCallback);
|
|
9926
10003
|
const mainAgent = new MainAgent(agentTools, agentConfig, streamBuffer);
|
|
@@ -9989,6 +10066,7 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9989
10066
|
layoutDescription = matchResult.layoutDescription;
|
|
9990
10067
|
actions = matchResult.actions;
|
|
9991
10068
|
}
|
|
10069
|
+
const queryMap = {};
|
|
9992
10070
|
const securedComponents = matchedComponents.map((comp) => {
|
|
9993
10071
|
const props = { ...comp.props };
|
|
9994
10072
|
const sqlValue = props.externalTool?.parameters?.sql || props.externalTool?.parameters?.query;
|
|
@@ -9996,6 +10074,7 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
9996
10074
|
const { sql, query, ...restParams } = props.externalTool.parameters;
|
|
9997
10075
|
const cachedData = queryCache.get(sqlValue);
|
|
9998
10076
|
const queryId = queryCache.storeQuery(sqlValue, cachedData);
|
|
10077
|
+
queryMap[queryId] = sqlValue;
|
|
9999
10078
|
props.externalTool = {
|
|
10000
10079
|
...props.externalTool,
|
|
10001
10080
|
parameters: { queryId, ...restParams }
|
|
@@ -10004,6 +10083,7 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
10004
10083
|
if (props.query) {
|
|
10005
10084
|
const { query, ...restProps } = props;
|
|
10006
10085
|
const queryId = queryCache.storeQuery(query);
|
|
10086
|
+
queryMap[queryId] = query;
|
|
10007
10087
|
return { ...comp, props: { ...restProps, queryId } };
|
|
10008
10088
|
}
|
|
10009
10089
|
return { ...comp, props };
|
|
@@ -10019,7 +10099,9 @@ var get_agent_user_response = async (prompt, components, anthropicApiKey, groqAp
|
|
|
10019
10099
|
config: {
|
|
10020
10100
|
title: layoutTitle,
|
|
10021
10101
|
description: layoutDescription,
|
|
10022
|
-
components: securedComponents
|
|
10102
|
+
components: securedComponents,
|
|
10103
|
+
// Persist queryId → SQL mapping so cached responses can re-register queries
|
|
10104
|
+
...Object.keys(queryMap).length > 0 && { _queryMap: queryMap }
|
|
10023
10105
|
},
|
|
10024
10106
|
actions
|
|
10025
10107
|
}
|