agentlang 0.10.1 → 0.10.3
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 +7 -14
- package/out/api/http.d.ts +4 -0
- package/out/api/http.d.ts.map +1 -1
- package/out/api/http.js +307 -26
- package/out/api/http.js.map +1 -1
- package/out/cli/main.d.ts.map +1 -1
- package/out/cli/main.js +3 -0
- package/out/cli/main.js.map +1 -1
- package/out/extension/main.cjs +250 -250
- package/out/extension/main.cjs.map +2 -2
- package/out/language/agentlang-validator.d.ts.map +1 -1
- package/out/language/agentlang-validator.js +4 -0
- package/out/language/agentlang-validator.js.map +1 -1
- package/out/language/error-reporter.d.ts +53 -0
- package/out/language/error-reporter.d.ts.map +1 -0
- package/out/language/error-reporter.js +879 -0
- package/out/language/error-reporter.js.map +1 -0
- package/out/language/generated/ast.d.ts +77 -2
- package/out/language/generated/ast.d.ts.map +1 -1
- package/out/language/generated/ast.js +60 -0
- package/out/language/generated/ast.js.map +1 -1
- package/out/language/generated/grammar.d.ts.map +1 -1
- package/out/language/generated/grammar.js +342 -206
- package/out/language/generated/grammar.js.map +1 -1
- package/out/language/main.cjs +901 -710
- package/out/language/main.cjs.map +3 -3
- package/out/language/parser.d.ts +4 -2
- package/out/language/parser.d.ts.map +1 -1
- package/out/language/parser.js +58 -99
- package/out/language/parser.js.map +1 -1
- package/out/language/syntax.d.ts +16 -0
- package/out/language/syntax.d.ts.map +1 -1
- package/out/language/syntax.js +66 -27
- package/out/language/syntax.js.map +1 -1
- package/out/runtime/api.d.ts +2 -0
- package/out/runtime/api.d.ts.map +1 -1
- package/out/runtime/api.js +25 -0
- package/out/runtime/api.js.map +1 -1
- package/out/runtime/datefns.d.ts +34 -0
- package/out/runtime/datefns.d.ts.map +1 -0
- package/out/runtime/datefns.js +82 -0
- package/out/runtime/datefns.js.map +1 -0
- package/out/runtime/defs.d.ts +1 -0
- package/out/runtime/defs.d.ts.map +1 -1
- package/out/runtime/defs.js +2 -1
- package/out/runtime/defs.js.map +1 -1
- package/out/runtime/document-retriever.d.ts +24 -0
- package/out/runtime/document-retriever.d.ts.map +1 -0
- package/out/runtime/document-retriever.js +258 -0
- package/out/runtime/document-retriever.js.map +1 -0
- package/out/runtime/embeddings/chunker.d.ts +18 -0
- package/out/runtime/embeddings/chunker.d.ts.map +1 -1
- package/out/runtime/embeddings/chunker.js +47 -15
- package/out/runtime/embeddings/chunker.js.map +1 -1
- package/out/runtime/embeddings/openai.d.ts.map +1 -1
- package/out/runtime/embeddings/openai.js +22 -9
- package/out/runtime/embeddings/openai.js.map +1 -1
- package/out/runtime/embeddings/provider.d.ts +1 -0
- package/out/runtime/embeddings/provider.d.ts.map +1 -1
- package/out/runtime/embeddings/provider.js +20 -1
- package/out/runtime/embeddings/provider.js.map +1 -1
- package/out/runtime/exec-graph.d.ts.map +1 -1
- package/out/runtime/exec-graph.js +22 -3
- package/out/runtime/exec-graph.js.map +1 -1
- package/out/runtime/integration-client.d.ts +21 -0
- package/out/runtime/integration-client.d.ts.map +1 -0
- package/out/runtime/integration-client.js +112 -0
- package/out/runtime/integration-client.js.map +1 -0
- package/out/runtime/integrations.d.ts.map +1 -1
- package/out/runtime/integrations.js +20 -9
- package/out/runtime/integrations.js.map +1 -1
- package/out/runtime/interpreter.d.ts +10 -0
- package/out/runtime/interpreter.d.ts.map +1 -1
- package/out/runtime/interpreter.js +221 -22
- package/out/runtime/interpreter.js.map +1 -1
- package/out/runtime/loader.d.ts.map +1 -1
- package/out/runtime/loader.js +70 -7
- package/out/runtime/loader.js.map +1 -1
- package/out/runtime/logger.d.ts.map +1 -1
- package/out/runtime/logger.js +8 -1
- package/out/runtime/logger.js.map +1 -1
- package/out/runtime/module.d.ts +18 -0
- package/out/runtime/module.d.ts.map +1 -1
- package/out/runtime/module.js +91 -3
- package/out/runtime/module.js.map +1 -1
- package/out/runtime/modules/ai.d.ts +16 -5
- package/out/runtime/modules/ai.d.ts.map +1 -1
- package/out/runtime/modules/ai.js +286 -88
- package/out/runtime/modules/ai.js.map +1 -1
- package/out/runtime/modules/core.d.ts.map +1 -1
- package/out/runtime/modules/core.js +5 -1
- package/out/runtime/modules/core.js.map +1 -1
- package/out/runtime/monitor.d.ts +6 -0
- package/out/runtime/monitor.d.ts.map +1 -1
- package/out/runtime/monitor.js +21 -1
- package/out/runtime/monitor.js.map +1 -1
- package/out/runtime/relgraph.d.ts.map +1 -1
- package/out/runtime/relgraph.js +7 -3
- package/out/runtime/relgraph.js.map +1 -1
- package/out/runtime/resolvers/interface.d.ts +7 -2
- package/out/runtime/resolvers/interface.d.ts.map +1 -1
- package/out/runtime/resolvers/interface.js +17 -3
- package/out/runtime/resolvers/interface.js.map +1 -1
- package/out/runtime/resolvers/sqldb/database.d.ts +2 -0
- package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -1
- package/out/runtime/resolvers/sqldb/database.js +142 -126
- package/out/runtime/resolvers/sqldb/database.js.map +1 -1
- package/out/runtime/resolvers/sqldb/dbutil.d.ts.map +1 -1
- package/out/runtime/resolvers/sqldb/dbutil.js +25 -4
- package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -1
- package/out/runtime/resolvers/sqldb/impl.d.ts +2 -1
- package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -1
- package/out/runtime/resolvers/sqldb/impl.js +24 -7
- package/out/runtime/resolvers/sqldb/impl.js.map +1 -1
- package/out/runtime/resolvers/vector/lancedb-store.d.ts +16 -0
- package/out/runtime/resolvers/vector/lancedb-store.d.ts.map +1 -0
- package/out/runtime/resolvers/vector/lancedb-store.js +159 -0
- package/out/runtime/resolvers/vector/lancedb-store.js.map +1 -0
- package/out/runtime/resolvers/vector/types.d.ts +32 -0
- package/out/runtime/resolvers/vector/types.d.ts.map +1 -0
- package/out/runtime/resolvers/vector/types.js +2 -0
- package/out/runtime/resolvers/vector/types.js.map +1 -0
- package/out/runtime/services/documentFetcher.d.ts.map +1 -1
- package/out/runtime/services/documentFetcher.js +21 -6
- package/out/runtime/services/documentFetcher.js.map +1 -1
- package/out/runtime/state.d.ts +19 -1
- package/out/runtime/state.d.ts.map +1 -1
- package/out/runtime/state.js +36 -1
- package/out/runtime/state.js.map +1 -1
- package/out/runtime/util.d.ts +3 -2
- package/out/runtime/util.d.ts.map +1 -1
- package/out/runtime/util.js +13 -2
- package/out/runtime/util.js.map +1 -1
- package/out/syntaxes/agentlang.monarch.js +1 -1
- package/out/syntaxes/agentlang.monarch.js.map +1 -1
- package/out/test-harness.d.ts +36 -0
- package/out/test-harness.d.ts.map +1 -0
- package/out/test-harness.js +341 -0
- package/out/test-harness.js.map +1 -0
- package/package.json +22 -19
- package/src/api/http.ts +336 -38
- package/src/cli/main.ts +3 -0
- package/src/language/agentlang-validator.ts +3 -0
- package/src/language/agentlang.langium +6 -2
- package/src/language/error-reporter.ts +1028 -0
- package/src/language/generated/ast.ts +94 -1
- package/src/language/generated/grammar.ts +342 -206
- package/src/language/parser.ts +64 -101
- package/src/language/syntax.ts +79 -24
- package/src/runtime/api.ts +36 -0
- package/src/runtime/datefns.ts +112 -0
- package/src/runtime/defs.ts +2 -1
- package/src/runtime/document-retriever.ts +311 -0
- package/src/runtime/embeddings/chunker.ts +52 -14
- package/src/runtime/embeddings/openai.ts +27 -9
- package/src/runtime/embeddings/provider.ts +22 -1
- package/src/runtime/exec-graph.ts +23 -2
- package/src/runtime/integration-client.ts +158 -0
- package/src/runtime/integrations.ts +20 -11
- package/src/runtime/interpreter.ts +221 -15
- package/src/runtime/loader.ts +83 -5
- package/src/runtime/logger.ts +12 -1
- package/src/runtime/module.ts +104 -3
- package/src/runtime/modules/ai.ts +341 -107
- package/src/runtime/modules/core.ts +5 -1
- package/src/runtime/monitor.ts +27 -1
- package/src/runtime/relgraph.ts +7 -3
- package/src/runtime/resolvers/interface.ts +23 -3
- package/src/runtime/resolvers/sqldb/database.ts +158 -130
- package/src/runtime/resolvers/sqldb/dbutil.ts +28 -6
- package/src/runtime/resolvers/sqldb/impl.ts +25 -7
- package/src/runtime/resolvers/vector/lancedb-store.ts +187 -0
- package/src/runtime/resolvers/vector/types.ts +39 -0
- package/src/runtime/services/documentFetcher.ts +21 -6
- package/src/runtime/state.ts +40 -1
- package/src/runtime/util.ts +19 -2
- package/src/syntaxes/agentlang.monarch.ts +1 -1
- package/src/test-harness.ts +423 -0
|
@@ -6,6 +6,12 @@ export declare const CoreAIModuleName: string;
|
|
|
6
6
|
export declare const AgentEntityName = "Agent";
|
|
7
7
|
export declare const LlmEntityName = "LLM";
|
|
8
8
|
export declare const AgentLearnerType = "learner";
|
|
9
|
+
export declare class AgentCancelledException extends Error {
|
|
10
|
+
constructor(chatId: string);
|
|
11
|
+
}
|
|
12
|
+
export declare function cancelAgent(chatId: string): Promise<string>;
|
|
13
|
+
export declare function checkCancelled(chatId: string): Promise<void>;
|
|
14
|
+
export declare function clearCancellation(chatId: string): Promise<void>;
|
|
9
15
|
declare const _default: string;
|
|
10
16
|
export default _default;
|
|
11
17
|
declare enum AgentCacheType {
|
|
@@ -23,12 +29,14 @@ export declare class AgentInstance {
|
|
|
23
29
|
type: string;
|
|
24
30
|
tools: string | undefined;
|
|
25
31
|
documents: string | undefined;
|
|
32
|
+
topics: string | undefined;
|
|
26
33
|
channels: string | undefined;
|
|
27
34
|
runWorkflows: boolean;
|
|
28
35
|
role: string | undefined;
|
|
29
36
|
flows: string | undefined;
|
|
30
37
|
validate: string | undefined;
|
|
31
38
|
retry: string | undefined;
|
|
39
|
+
saveResponseAs: string | undefined;
|
|
32
40
|
stateless: boolean;
|
|
33
41
|
private toolsArray;
|
|
34
42
|
private hasModuleTools;
|
|
@@ -64,8 +72,9 @@ export declare class AgentInstance {
|
|
|
64
72
|
static ResetCache(fqName: string, type: AgentCacheType): void;
|
|
65
73
|
private getFullInstructionsHelper;
|
|
66
74
|
private static UserTag;
|
|
75
|
+
private static FileTag;
|
|
67
76
|
private getFullInstructions;
|
|
68
|
-
private
|
|
77
|
+
private maybeFillInFileContents;
|
|
69
78
|
maybeValidateJsonResponse(response: string | undefined): object | undefined;
|
|
70
79
|
getFqName(): string;
|
|
71
80
|
markAsFlowExecutor(): AgentInstance;
|
|
@@ -81,8 +90,7 @@ export declare class AgentInstance {
|
|
|
81
90
|
private getValidationEvent;
|
|
82
91
|
private getExternalToolSpecs;
|
|
83
92
|
private maybeAddRelevantDocuments;
|
|
84
|
-
private
|
|
85
|
-
private static getDocumentTitle;
|
|
93
|
+
private queryRemoteKnowledgeService;
|
|
86
94
|
private static ToolsCache;
|
|
87
95
|
private toolsAsString;
|
|
88
96
|
}
|
|
@@ -93,11 +101,14 @@ export declare function saveAgentChatSession(chatId: string, messages: any[], en
|
|
|
93
101
|
export declare function agentName(agentInstance: Instance): string;
|
|
94
102
|
export declare function normalizeGeneratedCode(code: string | undefined): string;
|
|
95
103
|
export declare function processAgentLearning(moduleName: string, agentName: string, instruction: string, env: Environment): Promise<any>;
|
|
96
|
-
export declare function saveFlowStepResultLocally(chatId: string, step: string, result: string, suspensionId
|
|
97
|
-
export declare function saveFlowStepResult(chatId: string, step: string, result: string, suspensionId: string, env: Environment): Promise<
|
|
104
|
+
export declare function saveFlowStepResultLocally(chatId: string, step: string, result: string, suspensionId?: string | undefined): Promise<string>;
|
|
105
|
+
export declare function saveFlowStepResult(chatId: string, step: string, result: string, suspensionId: string, env: Environment): Promise<boolean>;
|
|
98
106
|
export declare function loadLocalFlowStepResults(chatId: string): Promise<Instance[]>;
|
|
99
107
|
export declare function loadFlowStepResults(chatId: string): Promise<Instance[]>;
|
|
108
|
+
export declare function loadLocalAgentResult(chatId: string, step: string): Promise<string | undefined>;
|
|
100
109
|
export declare function loadLocalFlowStep(chatId: string, step: string): Promise<Instance | undefined>;
|
|
101
110
|
export declare function loadFlowStep(chatId: string, step: string): Promise<Instance | undefined>;
|
|
111
|
+
export declare function registerTopic(name: string, documents: string | undefined, _env: Environment): Promise<any>;
|
|
102
112
|
export declare function fetchAndCreateDocument(title: string, url: string, retrievalConfig: Map<string, any> | undefined, embeddingConfig: Map<string, any> | undefined, _env: Environment): Promise<any>;
|
|
113
|
+
export declare function saveAgentResponse(fileName: string, response: string, env: Environment): Promise<string>;
|
|
103
114
|
//# sourceMappingURL=ai.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../../src/runtime/modules/ai.ts"],"names":[],"mappings":"AAgBA,OAAO,EACL,WAAW,EAIZ,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAEL,cAAc,EAEd,QAAQ,
|
|
1
|
+
{"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../../src/runtime/modules/ai.ts"],"names":[],"mappings":"AAgBA,OAAO,EACL,WAAW,EAIZ,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAEL,cAAc,EAEd,QAAQ,EAKR,QAAQ,EAaT,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,oBAAoB,EAKrB,MAAM,uBAAuB,CAAC;AAsB/B,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAO9C,eAAO,MAAM,gBAAgB,QAA2B,CAAC;AACzD,eAAO,MAAM,eAAe,UAAU,CAAC;AACvC,eAAO,MAAM,aAAa,QAAQ,CAAC;AACnC,eAAO,MAAM,gBAAgB,YAAY,CAAC;AAM1C,qBAAa,uBAAwB,SAAQ,KAAK;gBACpC,MAAM,EAAE,MAAM;CAI3B;AAED,wBAAsB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAIjE;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAgBlE;AAED,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CASrE;;AAED,wBA0JE;AAEF,aAAK,cAAc;IACjB,SAAS,IAAA;IACT,QAAQ,IAAA;IACR,QAAQ,IAAA;IACR,OAAO,IAAA;CACR;AAoFD,eA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AAsB,wBAAwB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CAiBlF;AAED,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CAY7E;AAED,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAe7B;AAED,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAQ/B;AAED,wBAAsB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAkB9F;AAED,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,IAAI,EAAE,WAAW,GAChB,OAAO,CAAC,GAAG,CAAC,CA4Dd;AAED,wBAAsB,sBAAsB,CAC1C,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EACX,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,EAC7C,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,EAC7C,IAAI,EAAE,WAAW,GAChB,OAAO,CAAC,GAAG,CAAC,CAoDd;AAED,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,WAAW,GACf,OAAO,CAAC,MAAM,CAAC,CAQjB"}
|
|
@@ -1,20 +1,58 @@
|
|
|
1
1
|
import { DefaultModuleName, escapeSpecialChars, extractAndRemoveAllXmlTaggedText, extractFencedCodeBlocks, isFqName, isString, makeCoreModuleName, makeFqName, nameToPath, restoreSpecialChars, sleepMilliseconds, splitFqName, } from '../util.js';
|
|
2
2
|
import { GlobalEnvironment, makeEventEvaluator, parseAndEvaluateStatement, } from '../interpreter.js';
|
|
3
|
-
import { Agent, asJSONSchema, fetchModule, getDecision, getGlobalRetry, Instance, instanceToObject, isAgent, isInstanceOfType, isModule, makeInstance, newInstanceAttributes, objectToInstanceAttributes, Record, resolveDocumentAliases, } from '../module.js';
|
|
3
|
+
import { Agent, asJSONSchema, fetchModule, getAllDocumentsForTopics, getDecision, getGlobalRetry, Instance, instanceToObject, isAgent, isInstanceOfType, isModule, makeInstance, newInstanceAttributes, objectToInstanceAttributes, Record, registerTopic as registerTopicInRegistry, resolveDocumentAliases, resolveTopicNames, } from '../module.js';
|
|
4
4
|
import { provider } from '../agents/registry.js';
|
|
5
5
|
import { assistantMessage, humanMessage, systemMessage, } from '../agents/provider.js';
|
|
6
6
|
import { AIMessage, HumanMessage, SystemMessage } from '@langchain/core/messages';
|
|
7
7
|
import { DecisionAgentInstructions, EvalInstructions, FlowExecInstructions, getAgentDirectives, getAgentGlossary, getAgentResponseSchema, getAgentScenarios, getAgentScratchNames, LearningAgentInstructions as LearnerAgentInstructions, newAgentDirective, newAgentGlossaryEntry, newAgentScenario, PlannerInstructions, } from '../agents/common.js';
|
|
8
8
|
import { logger } from '../logger.js';
|
|
9
|
-
import
|
|
10
|
-
import { isMonitoringEnabled, TtlCache } from '../state.js';
|
|
9
|
+
import { getKnowledgeGraphConfig, isMonitoringEnabled, TtlCache } from '../state.js';
|
|
11
10
|
import { isNodeEnv } from '../../utils/runtime.js';
|
|
12
11
|
import { getFileSystem } from '../../utils/fs-utils.js';
|
|
12
|
+
import { getDocumentRetriever } from '../document-retriever.js';
|
|
13
13
|
export const CoreAIModuleName = makeCoreModuleName('ai');
|
|
14
14
|
export const AgentEntityName = 'Agent';
|
|
15
15
|
export const LlmEntityName = 'LLM';
|
|
16
16
|
export const AgentLearnerType = 'learner';
|
|
17
17
|
const AgentEvalType = 'eval';
|
|
18
|
+
// --- Agent cancellation infrastructure ---
|
|
19
|
+
export class AgentCancelledException extends Error {
|
|
20
|
+
constructor(chatId) {
|
|
21
|
+
super(`Agent cancelled for chatId: ${chatId}`);
|
|
22
|
+
this.name = 'AgentCancelledException';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export async function cancelAgent(chatId) {
|
|
26
|
+
const t = `${CoreAIModuleName}/${CancelledAgentEntity}`;
|
|
27
|
+
await parseAndEvaluateStatement(`{${t} {chatId "${chatId}"}, @upsert}`);
|
|
28
|
+
return `Cancellation requested for chatId: ${chatId}`;
|
|
29
|
+
}
|
|
30
|
+
export async function checkCancelled(chatId) {
|
|
31
|
+
if (!chatId)
|
|
32
|
+
return;
|
|
33
|
+
try {
|
|
34
|
+
const results = await parseAndEvaluateStatement(`{${CoreAIModuleName}/${CancelledAgentEntity} {chatId? "${chatId}"}}`);
|
|
35
|
+
if (results && results.length > 0) {
|
|
36
|
+
await parseAndEvaluateStatement(`purge {${CoreAIModuleName}/${CancelledAgentEntity} {chatId? "${chatId}"}}`);
|
|
37
|
+
throw new AgentCancelledException(chatId);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
if (err instanceof AgentCancelledException)
|
|
42
|
+
throw err;
|
|
43
|
+
logger.debug(`checkCancelled DB error for ${chatId}: ${err}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export async function clearCancellation(chatId) {
|
|
47
|
+
if (!chatId)
|
|
48
|
+
return;
|
|
49
|
+
try {
|
|
50
|
+
await parseAndEvaluateStatement(`purge {${CoreAIModuleName}/${CancelledAgentEntity} {chatId? "${chatId}"}}`);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
logger.debug(`clearCancellation DB error for ${chatId}: ${err}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
18
56
|
export default `module ${CoreAIModuleName}
|
|
19
57
|
|
|
20
58
|
import "./modules/ai.js" @as ai
|
|
@@ -33,12 +71,14 @@ entity ${AgentEntityName} {
|
|
|
33
71
|
stateless Boolean @default(false),
|
|
34
72
|
instruction String @optional,
|
|
35
73
|
tools String @optional, // comma-separated list of tool names
|
|
36
|
-
documents String @optional, //
|
|
74
|
+
documents String @optional, // list of document names
|
|
75
|
+
topics String @optional, // list of topic names
|
|
37
76
|
channels String @optional, // comma-separated list of channel names
|
|
38
77
|
role String @optional,
|
|
39
78
|
flows String @optional,
|
|
40
79
|
validate String @optional,
|
|
41
80
|
retry String @optional,
|
|
81
|
+
saveResponseAs String @optional,
|
|
42
82
|
llm String
|
|
43
83
|
}
|
|
44
84
|
|
|
@@ -79,6 +119,15 @@ workflow processDoc {
|
|
|
79
119
|
await ai.fetchAndCreateDocument(doc.title, doc.url, doc.retrievalConfig, doc.embeddingConfig)
|
|
80
120
|
}
|
|
81
121
|
|
|
122
|
+
event topic {
|
|
123
|
+
name String,
|
|
124
|
+
documents String @optional // comma-separated or array of document titles
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
workflow processTopic {
|
|
128
|
+
await ai.registerTopic(topic.name, topic.documents)
|
|
129
|
+
}
|
|
130
|
+
|
|
82
131
|
entity Directive {
|
|
83
132
|
id UUID @id @default(uuid()),
|
|
84
133
|
agentFqName String @indexed,
|
|
@@ -134,6 +183,10 @@ entity AgentFlowStep {
|
|
|
134
183
|
suspensionId String
|
|
135
184
|
}
|
|
136
185
|
|
|
186
|
+
entity CancelledAgent {
|
|
187
|
+
chatId String @id
|
|
188
|
+
}
|
|
189
|
+
|
|
137
190
|
@public event restartFlow {
|
|
138
191
|
chatId String,
|
|
139
192
|
step String,
|
|
@@ -146,6 +199,14 @@ workflow restartFlow {
|
|
|
146
199
|
{${DefaultModuleName}/restartSuspension {id fs.suspensionId, data restartFlow.userInput}}
|
|
147
200
|
}
|
|
148
201
|
}
|
|
202
|
+
|
|
203
|
+
@public event cancelAgent {
|
|
204
|
+
chatId String
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
workflow cancelAgent {
|
|
208
|
+
await ai.cancelAgent(cancelAgent.chatId)
|
|
209
|
+
}
|
|
149
210
|
`;
|
|
150
211
|
var AgentCacheType;
|
|
151
212
|
(function (AgentCacheType) {
|
|
@@ -462,7 +523,7 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
462
523
|
const spad = env.getScratchPad();
|
|
463
524
|
if (spad !== undefined && Object.keys(spad).length > 0) {
|
|
464
525
|
if (finalInstruction.indexOf('{{') > 0) {
|
|
465
|
-
return
|
|
526
|
+
return env.maybeRewriteTemplatePatterns(finalInstruction, spad);
|
|
466
527
|
}
|
|
467
528
|
else {
|
|
468
529
|
const ctx = JSON.stringify(spad);
|
|
@@ -483,9 +544,22 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
483
544
|
return { extracted: undefined, updatedText: finalInstruction };
|
|
484
545
|
}
|
|
485
546
|
}
|
|
486
|
-
|
|
487
|
-
const
|
|
488
|
-
|
|
547
|
+
async maybeFillInFileContents(instructions, env) {
|
|
548
|
+
const chatId = env.getActiveChatId();
|
|
549
|
+
const FC = '$FC';
|
|
550
|
+
if (chatId && instructions.indexOf(`<${AgentInstance.FileTag}>`) > 0) {
|
|
551
|
+
const ext = extractAndRemoveAllXmlTaggedText(instructions, AgentInstance.FileTag, FC);
|
|
552
|
+
if (ext.extracted) {
|
|
553
|
+
instructions = ext.updatedText;
|
|
554
|
+
for (let i = 0; i < ext.extracted.length; ++i) {
|
|
555
|
+
const fileName = ext.extracted[i];
|
|
556
|
+
const contents = await loadLocalAgentResult(chatId, fileName);
|
|
557
|
+
if (contents)
|
|
558
|
+
instructions = instructions.replace(FC, contents);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return instructions;
|
|
489
563
|
}
|
|
490
564
|
maybeValidateJsonResponse(response) {
|
|
491
565
|
if (response) {
|
|
@@ -600,6 +674,7 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
600
674
|
const p = await findProviderForLLM(this.llm, env);
|
|
601
675
|
const agentName = this.name;
|
|
602
676
|
const chatId = env.getAgentChatId() || agentName;
|
|
677
|
+
await checkCancelled(chatId);
|
|
603
678
|
let isplnr = this.isPlanner();
|
|
604
679
|
const isflow = !isplnr && this.isFlowExecutor();
|
|
605
680
|
const iseval = !isplnr && !isflow && this.isEvaluator();
|
|
@@ -664,7 +739,8 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
664
739
|
}
|
|
665
740
|
let tmpMsg = message;
|
|
666
741
|
if (extractedText === null || extractedText === void 0 ? void 0 : extractedText.extracted) {
|
|
667
|
-
|
|
742
|
+
const s = await this.maybeFillInFileContents(extractedText.extracted.join('\n'), env);
|
|
743
|
+
tmpMsg = `${tmpMsg}\n${s}`;
|
|
668
744
|
}
|
|
669
745
|
const hmsg = await this.maybeAddRelevantDocuments(this.maybeAddFlowContext(tmpMsg, env), env);
|
|
670
746
|
if (hmsg.length > 0) {
|
|
@@ -693,7 +769,7 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
693
769
|
let response = await p.invoke(msgs, externalToolSpecs);
|
|
694
770
|
const v = this.getValidationEvent();
|
|
695
771
|
if (v) {
|
|
696
|
-
response = await this.handleValidation(response, v, msgs, p);
|
|
772
|
+
response = await this.handleValidation(response, v, msgs, p, chatId);
|
|
697
773
|
}
|
|
698
774
|
if (!iseval)
|
|
699
775
|
await AgentInstance.maybeEvaluateResponse(this, message, msgsContent, response.content, env);
|
|
@@ -706,6 +782,13 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
706
782
|
}
|
|
707
783
|
if (monitoringEnabled)
|
|
708
784
|
env.setMonitorEntryLlmResponse(response.content);
|
|
785
|
+
if (monitoringEnabled && response.sysMsg.usage_metadata) {
|
|
786
|
+
const u = response.sysMsg.usage_metadata;
|
|
787
|
+
env.setMonitorEntryLlmTokenUsage(u.input_tokens, u.output_tokens, u.total_tokens);
|
|
788
|
+
}
|
|
789
|
+
if (this.saveResponseAs) {
|
|
790
|
+
await saveAgentResponse(this.saveResponseAs, response.content, env);
|
|
791
|
+
}
|
|
709
792
|
env.setLastResult(response.content);
|
|
710
793
|
}
|
|
711
794
|
catch (err) {
|
|
@@ -760,7 +843,7 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
760
843
|
return r;
|
|
761
844
|
}
|
|
762
845
|
}
|
|
763
|
-
async handleValidation(response, validationEventName, msgs, provider) {
|
|
846
|
+
async handleValidation(response, validationEventName, msgs, provider, chatId) {
|
|
764
847
|
let r = await this.invokeValidator(response, validationEventName);
|
|
765
848
|
const status = r.lookup('status');
|
|
766
849
|
if (status === 'ok') {
|
|
@@ -772,6 +855,8 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
772
855
|
let attempt = 0;
|
|
773
856
|
let delay = this.retryObj.getNextDelayMs(attempt);
|
|
774
857
|
while (delay) {
|
|
858
|
+
if (chatId)
|
|
859
|
+
await checkCancelled(chatId);
|
|
775
860
|
msgs.push(assistantMessage(resp.content));
|
|
776
861
|
const vs = JSON.stringify(r.asSerializableObject());
|
|
777
862
|
msgs.push(humanMessage(`Validation for your last response failed with this result: \n${vs}\n\nFix the errors.`));
|
|
@@ -816,77 +901,106 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
816
901
|
}
|
|
817
902
|
return result;
|
|
818
903
|
}
|
|
819
|
-
async maybeAddRelevantDocuments(message,
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
return message.concat('\n\nRelevant context from documents:\n').concat(docs
|
|
863
|
-
.map((v) => {
|
|
864
|
-
return v.lookup('content');
|
|
865
|
-
})
|
|
866
|
-
.join('\n\n'));
|
|
904
|
+
async maybeAddRelevantDocuments(message, _env) {
|
|
905
|
+
var _a;
|
|
906
|
+
const hasDocuments = this.documents && this.documents.length > 0;
|
|
907
|
+
const hasTopics = this.topics && this.topics.length > 0;
|
|
908
|
+
if (!hasDocuments && !hasTopics) {
|
|
909
|
+
return message;
|
|
910
|
+
}
|
|
911
|
+
try {
|
|
912
|
+
let containerTags = [];
|
|
913
|
+
let topicDocumentTitles = [];
|
|
914
|
+
if (hasTopics) {
|
|
915
|
+
const topicNames = resolveTopicNames(this.topics);
|
|
916
|
+
containerTags = topicNames;
|
|
917
|
+
topicDocumentTitles = getAllDocumentsForTopics(topicNames);
|
|
918
|
+
}
|
|
919
|
+
if (containerTags.length === 0) {
|
|
920
|
+
containerTags = [this.getFqName()];
|
|
921
|
+
}
|
|
922
|
+
let documentTitles = [];
|
|
923
|
+
let documentRefs = [];
|
|
924
|
+
if (hasDocuments) {
|
|
925
|
+
const documentEntries = this.documents.split(',')
|
|
926
|
+
.map(d => d.trim())
|
|
927
|
+
.filter(Boolean);
|
|
928
|
+
documentTitles = resolveDocumentAliases(documentEntries);
|
|
929
|
+
documentRefs = documentEntries.filter(d => d.startsWith('document-service://'));
|
|
930
|
+
}
|
|
931
|
+
const allDocumentTitles = [...new Set([...topicDocumentTitles, ...documentTitles])];
|
|
932
|
+
const kgConfig = getKnowledgeGraphConfig();
|
|
933
|
+
const serviceUrl = ((_a = kgConfig === null || kgConfig === void 0 ? void 0 : kgConfig.serviceUrl) === null || _a === void 0 ? void 0 : _a.trim()) || process.env.KNOWLEDGE_SERVICE_URL || null;
|
|
934
|
+
if (serviceUrl) {
|
|
935
|
+
return await this.queryRemoteKnowledgeService(message, serviceUrl, containerTags, allDocumentTitles, documentRefs);
|
|
936
|
+
}
|
|
937
|
+
// Local mode: embed documents into pgvector/lancedb and query
|
|
938
|
+
const retriever = getDocumentRetriever();
|
|
939
|
+
const aiModule = isModule(DefaultModuleName + '.ai')
|
|
940
|
+
? fetchModule(DefaultModuleName + '.ai')
|
|
941
|
+
: null;
|
|
942
|
+
if (aiModule) {
|
|
943
|
+
for (const title of allDocumentTitles) {
|
|
944
|
+
const url = aiModule.getDocument(title);
|
|
945
|
+
if (url) {
|
|
946
|
+
await retriever.processDocument(title, url);
|
|
867
947
|
}
|
|
868
948
|
}
|
|
869
949
|
}
|
|
870
|
-
|
|
871
|
-
|
|
950
|
+
const contextString = await retriever.query(message, allDocumentTitles.length > 0 ? allDocumentTitles : undefined, 10);
|
|
951
|
+
if (contextString.trim().length > 0) {
|
|
952
|
+
return message.concat('\n\nRelevant context from documents:\n').concat(contextString);
|
|
872
953
|
}
|
|
873
954
|
}
|
|
955
|
+
catch (err) {
|
|
956
|
+
logger.debug(`[KNOWLEDGE] Knowledge retrieval failed: ${err}`);
|
|
957
|
+
}
|
|
874
958
|
return message;
|
|
875
959
|
}
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
960
|
+
async queryRemoteKnowledgeService(message, serviceUrl, containerTags, documentTitles, documentRefs) {
|
|
961
|
+
const options = {
|
|
962
|
+
chunkLimit: 10,
|
|
963
|
+
entityLimit: 20,
|
|
964
|
+
includeChunks: true,
|
|
965
|
+
includeEntities: true,
|
|
966
|
+
includeEdges: true,
|
|
967
|
+
documentTitles: documentTitles.length > 0 ? documentTitles : undefined,
|
|
968
|
+
documentRefs: documentRefs.length > 0 ? documentRefs : undefined,
|
|
969
|
+
};
|
|
970
|
+
let response = await fetch(`${serviceUrl}/api/knowledge/query`, {
|
|
971
|
+
method: 'POST',
|
|
972
|
+
headers: { 'Content-Type': 'application/json' },
|
|
973
|
+
body: JSON.stringify(Object.assign({ query: message, containerTags }, options)),
|
|
974
|
+
});
|
|
975
|
+
if (!response.ok) {
|
|
976
|
+
response = await fetch(`${serviceUrl}/knowledge.core/ApiKnowledgeQuery`, {
|
|
977
|
+
method: 'POST',
|
|
978
|
+
headers: { 'Content-Type': 'application/json' },
|
|
979
|
+
body: JSON.stringify({
|
|
980
|
+
queryText: message,
|
|
981
|
+
containerTagsJson: JSON.stringify(containerTags),
|
|
982
|
+
documentTitlesJson: JSON.stringify(options.documentTitles || []),
|
|
983
|
+
documentRefsJson: JSON.stringify(options.documentRefs || []),
|
|
984
|
+
optionsJson: JSON.stringify({
|
|
985
|
+
includeChunks: options.includeChunks,
|
|
986
|
+
includeEntities: options.includeEntities,
|
|
987
|
+
includeEdges: options.includeEdges,
|
|
988
|
+
chunkLimit: options.chunkLimit,
|
|
989
|
+
entityLimit: options.entityLimit,
|
|
990
|
+
}),
|
|
991
|
+
}),
|
|
992
|
+
});
|
|
885
993
|
}
|
|
886
|
-
|
|
887
|
-
|
|
994
|
+
if (response.ok) {
|
|
995
|
+
const rawPayload = await response.json();
|
|
996
|
+
const first = Array.isArray(rawPayload) ? rawPayload[0] : rawPayload;
|
|
997
|
+
const payload = first && typeof first === 'object' && first.KnowledgeQuery ? first.KnowledgeQuery : first;
|
|
998
|
+
const contextString = (payload === null || payload === void 0 ? void 0 : payload.contextString) || '';
|
|
999
|
+
if (contextString.trim().length > 0) {
|
|
1000
|
+
return message.concat('\n\nRelevant context from documents:\n').concat(contextString);
|
|
1001
|
+
}
|
|
888
1002
|
}
|
|
889
|
-
return
|
|
1003
|
+
return message;
|
|
890
1004
|
}
|
|
891
1005
|
toolsAsString() {
|
|
892
1006
|
const cachedTools = AgentInstance.ToolsCache.get(this.name);
|
|
@@ -942,6 +1056,7 @@ AgentInstance.GlossaryCache = new TtlCache(AgentInstance.CACHE_TTL_MS);
|
|
|
942
1056
|
AgentInstance.SummariesCache = new TtlCache(AgentInstance.CACHE_TTL_MS);
|
|
943
1057
|
AgentInstance.ScenariosCache = new TtlCache(AgentInstance.CACHE_TTL_MS);
|
|
944
1058
|
AgentInstance.UserTag = 'user';
|
|
1059
|
+
AgentInstance.FileTag = 'file';
|
|
945
1060
|
AgentInstance.AgentEvaluators = new Map();
|
|
946
1061
|
AgentInstance.ToolsCache = new Map();
|
|
947
1062
|
function extractScratchData(scratchNames, inst) {
|
|
@@ -1147,14 +1262,9 @@ export async function processAgentLearning(moduleName, agentName, instruction, e
|
|
|
1147
1262
|
const LocalAgentgFlow = true;
|
|
1148
1263
|
const LocalFlowStepsRootDirName = 'flows';
|
|
1149
1264
|
const AgentFlowStep = 'AgentFlowStep';
|
|
1265
|
+
const CancelledAgentEntity = 'CancelledAgent';
|
|
1150
1266
|
export async function saveFlowStepResultLocally(chatId, step, result, suspensionId) {
|
|
1151
1267
|
const fs = await getFileSystem();
|
|
1152
|
-
const attrs = newInstanceAttributes()
|
|
1153
|
-
.set('chatId', chatId)
|
|
1154
|
-
.set('step', step)
|
|
1155
|
-
.set('result', result)
|
|
1156
|
-
.set('suspensionId', suspensionId);
|
|
1157
|
-
const inst = makeInstance(CoreAIModuleName, AgentFlowStep, attrs);
|
|
1158
1268
|
const rootDirName = LocalFlowStepsRootDirName;
|
|
1159
1269
|
if (!(await fs.exists(rootDirName))) {
|
|
1160
1270
|
await fs.mkdir(rootDirName);
|
|
@@ -1167,12 +1277,23 @@ export async function saveFlowStepResultLocally(chatId, step, result, suspension
|
|
|
1167
1277
|
if (await fs.exists(fileName)) {
|
|
1168
1278
|
await fs.unlink(fileName);
|
|
1169
1279
|
}
|
|
1170
|
-
|
|
1171
|
-
|
|
1280
|
+
let s = result;
|
|
1281
|
+
if (suspensionId) {
|
|
1282
|
+
const attrs = newInstanceAttributes()
|
|
1283
|
+
.set('chatId', chatId)
|
|
1284
|
+
.set('step', step)
|
|
1285
|
+
.set('result', result)
|
|
1286
|
+
.set('suspensionId', suspensionId);
|
|
1287
|
+
const inst = makeInstance(CoreAIModuleName, AgentFlowStep, attrs);
|
|
1288
|
+
s = JSON.stringify(inst.attributesAsObject(true));
|
|
1289
|
+
}
|
|
1290
|
+
await fs.writeFile(fileName, s);
|
|
1291
|
+
return s;
|
|
1172
1292
|
}
|
|
1173
1293
|
export async function saveFlowStepResult(chatId, step, result, suspensionId, env) {
|
|
1174
1294
|
if (LocalAgentgFlow) {
|
|
1175
|
-
|
|
1295
|
+
await saveFlowStepResultLocally(chatId, step, result, suspensionId);
|
|
1296
|
+
return true;
|
|
1176
1297
|
}
|
|
1177
1298
|
else {
|
|
1178
1299
|
const t = `${CoreAIModuleName}/${AgentFlowStep}`;
|
|
@@ -1180,13 +1301,13 @@ export async function saveFlowStepResult(chatId, step, result, suspensionId, env
|
|
|
1180
1301
|
await parseAndEvaluateStatement(`purge {${t} {chatId? "${chatId}", step? "${step}"}}`, undefined, env);
|
|
1181
1302
|
const inst = await parseAndEvaluateStatement(`{${t} {chatId "${chatId}", step "${step}", result "${escapeSpecialChars(result)}", suspensionId "${suspensionId}"}}`, undefined, env);
|
|
1182
1303
|
if (isInstanceOfType(inst, t))
|
|
1183
|
-
return
|
|
1304
|
+
return true;
|
|
1184
1305
|
else
|
|
1185
|
-
return
|
|
1306
|
+
return false;
|
|
1186
1307
|
}
|
|
1187
1308
|
catch (reason) {
|
|
1188
1309
|
logger.error(`failed to save flow result for step ${step} - ${reason}`);
|
|
1189
|
-
return
|
|
1310
|
+
return false;
|
|
1190
1311
|
}
|
|
1191
1312
|
}
|
|
1192
1313
|
}
|
|
@@ -1222,7 +1343,7 @@ export async function loadFlowStepResults(chatId) {
|
|
|
1222
1343
|
}
|
|
1223
1344
|
}
|
|
1224
1345
|
}
|
|
1225
|
-
export async function
|
|
1346
|
+
export async function loadLocalAgentResult(chatId, step) {
|
|
1226
1347
|
const fs = await getFileSystem();
|
|
1227
1348
|
const dirName = `${LocalFlowStepsRootDirName}/${chatId}`;
|
|
1228
1349
|
if (await fs.exists(dirName)) {
|
|
@@ -1230,8 +1351,7 @@ export async function loadLocalFlowStep(chatId, step) {
|
|
|
1230
1351
|
for (let i = 0; i < fileNames.length; ++i) {
|
|
1231
1352
|
const fileName = fileNames[i];
|
|
1232
1353
|
if (fileName === step) {
|
|
1233
|
-
|
|
1234
|
-
return makeInstance(CoreAIModuleName, AgentFlowStep, attrs);
|
|
1354
|
+
return await fs.readFile(`${dirName}/${fileName}`);
|
|
1235
1355
|
}
|
|
1236
1356
|
}
|
|
1237
1357
|
return undefined;
|
|
@@ -1240,6 +1360,16 @@ export async function loadLocalFlowStep(chatId, step) {
|
|
|
1240
1360
|
return undefined;
|
|
1241
1361
|
}
|
|
1242
1362
|
}
|
|
1363
|
+
export async function loadLocalFlowStep(chatId, step) {
|
|
1364
|
+
const contents = await loadLocalAgentResult(chatId, step);
|
|
1365
|
+
if (contents) {
|
|
1366
|
+
const attrs = objectToInstanceAttributes(JSON.parse(contents));
|
|
1367
|
+
return makeInstance(CoreAIModuleName, AgentFlowStep, attrs);
|
|
1368
|
+
}
|
|
1369
|
+
else {
|
|
1370
|
+
return undefined;
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1243
1373
|
export async function loadFlowStep(chatId, step) {
|
|
1244
1374
|
if (LocalAgentgFlow) {
|
|
1245
1375
|
return await loadLocalFlowStep(chatId, step);
|
|
@@ -1261,6 +1391,64 @@ export async function loadFlowStep(chatId, step) {
|
|
|
1261
1391
|
}
|
|
1262
1392
|
}
|
|
1263
1393
|
}
|
|
1394
|
+
export async function registerTopic(name, documents, _env) {
|
|
1395
|
+
var _a;
|
|
1396
|
+
// Register in local registry
|
|
1397
|
+
registerTopicInRegistry(name, documents !== null && documents !== void 0 ? documents : undefined);
|
|
1398
|
+
// Sync to knowledge service if configured
|
|
1399
|
+
const kgConfig = getKnowledgeGraphConfig();
|
|
1400
|
+
const serviceUrl = ((_a = kgConfig === null || kgConfig === void 0 ? void 0 : kgConfig.serviceUrl) === null || _a === void 0 ? void 0 : _a.trim()) || process.env.KNOWLEDGE_SERVICE_URL || null;
|
|
1401
|
+
if (serviceUrl) {
|
|
1402
|
+
try {
|
|
1403
|
+
// Parse document list
|
|
1404
|
+
const docList = documents
|
|
1405
|
+
? documents
|
|
1406
|
+
.split(',')
|
|
1407
|
+
.map(d => d.trim())
|
|
1408
|
+
.filter(Boolean)
|
|
1409
|
+
: [];
|
|
1410
|
+
// Create or update topic in knowledge service
|
|
1411
|
+
let response = await fetch(`${serviceUrl}/api/knowledge/topics`, {
|
|
1412
|
+
method: 'POST',
|
|
1413
|
+
headers: {
|
|
1414
|
+
'Content-Type': 'application/json',
|
|
1415
|
+
},
|
|
1416
|
+
body: JSON.stringify({
|
|
1417
|
+
name,
|
|
1418
|
+
description: `Topic ${name} with ${docList.length} documents`,
|
|
1419
|
+
documentTitles: docList,
|
|
1420
|
+
}),
|
|
1421
|
+
});
|
|
1422
|
+
if (!response.ok) {
|
|
1423
|
+
// Fallback to Agentlang entity path for deployed knowledge-service
|
|
1424
|
+
response = await fetch(`${serviceUrl}/knowledge.core/Topic`, {
|
|
1425
|
+
method: 'POST',
|
|
1426
|
+
headers: {
|
|
1427
|
+
'Content-Type': 'application/json',
|
|
1428
|
+
},
|
|
1429
|
+
body: JSON.stringify({
|
|
1430
|
+
name,
|
|
1431
|
+
description: `Topic ${name} with ${docList.length} documents`,
|
|
1432
|
+
type: 'manual',
|
|
1433
|
+
documentCount: docList.length,
|
|
1434
|
+
}),
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
if (!response.ok) {
|
|
1438
|
+
const errorText = await response.text();
|
|
1439
|
+
logger.debug(`[KNOWLEDGE] Failed to sync topic to service: ${errorText}`);
|
|
1440
|
+
}
|
|
1441
|
+
else {
|
|
1442
|
+
logger.debug(`[KNOWLEDGE] Synced topic "${name}" to knowledge service`);
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
catch (err) {
|
|
1446
|
+
// Don't fail topic registration if service sync fails
|
|
1447
|
+
logger.debug(`[KNOWLEDGE] Error syncing topic to service: ${err}`);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
return { topic: { name, documents: documents !== null && documents !== void 0 ? documents : '' } };
|
|
1451
|
+
}
|
|
1264
1452
|
export async function fetchAndCreateDocument(title, url, retrievalConfig, embeddingConfig, _env) {
|
|
1265
1453
|
if (!isNodeEnv) {
|
|
1266
1454
|
throw new Error('Document fetching is only available in Node.js environment');
|
|
@@ -1308,4 +1496,14 @@ export async function fetchAndCreateDocument(title, url, retrievalConfig, embedd
|
|
|
1308
1496
|
throw new Error(`Failed to fetch document: ${title} from ${url}`);
|
|
1309
1497
|
}
|
|
1310
1498
|
}
|
|
1499
|
+
export async function saveAgentResponse(fileName, response, env) {
|
|
1500
|
+
const chatId = env.getActiveChatId();
|
|
1501
|
+
if (chatId) {
|
|
1502
|
+
await saveFlowStepResultLocally(chatId, fileName, response);
|
|
1503
|
+
}
|
|
1504
|
+
else {
|
|
1505
|
+
logger.warn(`No chatId set, ${fileName} was not saved.`);
|
|
1506
|
+
}
|
|
1507
|
+
return response;
|
|
1508
|
+
}
|
|
1311
1509
|
//# sourceMappingURL=ai.js.map
|