agentlang 0.10.2 → 0.10.4
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 +171 -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 +66 -6
- package/out/language/generated/ast.d.ts.map +1 -1
- package/out/language/generated/ast.js +48 -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 +320 -190
- package/out/language/generated/grammar.js.map +1 -1
- package/out/language/main.cjs +870 -694
- 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 +31 -98
- package/out/language/parser.js.map +1 -1
- package/out/language/syntax.d.ts +2 -0
- package/out/language/syntax.d.ts.map +1 -1
- package/out/language/syntax.js +6 -0
- package/out/language/syntax.js.map +1 -1
- package/out/runtime/api.d.ts.map +1 -1
- package/out/runtime/api.js +22 -0
- package/out/runtime/api.js.map +1 -1
- 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/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 +1 -0
- package/out/runtime/interpreter.d.ts.map +1 -1
- package/out/runtime/interpreter.js +172 -19
- 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 +10 -0
- package/out/runtime/module.d.ts.map +1 -1
- package/out/runtime/module.js +68 -3
- package/out/runtime/module.js.map +1 -1
- package/out/runtime/modules/ai.d.ts +9 -2
- package/out/runtime/modules/ai.d.ts.map +1 -1
- package/out/runtime/modules/ai.js +219 -67
- 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 +3 -0
- package/out/runtime/modules/core.js.map +1 -1
- package/out/runtime/modules/messaging.d.ts +10 -0
- package/out/runtime/modules/messaging.d.ts.map +1 -0
- package/out/runtime/modules/messaging.js +210 -0
- package/out/runtime/modules/messaging.js.map +1 -0
- package/out/runtime/resolvers/interface.d.ts +4 -0
- package/out/runtime/resolvers/interface.d.ts.map +1 -1
- package/out/runtime/resolvers/interface.js +14 -1
- 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 +8 -0
- package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -1
- package/out/runtime/resolvers/sqldb/impl.d.ts +1 -0
- package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -1
- package/out/runtime/resolvers/sqldb/impl.js +7 -0
- 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/syntaxes/agentlang.monarch.js +1 -1
- package/out/syntaxes/agentlang.monarch.js.map +1 -1
- package/package.json +19 -19
- package/src/api/http.ts +197 -37
- package/src/cli/main.ts +3 -0
- package/src/language/agentlang-validator.ts +3 -0
- package/src/language/agentlang.langium +3 -1
- package/src/language/error-reporter.ts +1028 -0
- package/src/language/generated/ast.ts +77 -5
- package/src/language/generated/grammar.ts +320 -190
- package/src/language/parser.ts +31 -100
- package/src/language/syntax.ts +8 -0
- package/src/runtime/api.ts +31 -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/integration-client.ts +158 -0
- package/src/runtime/integrations.ts +20 -11
- package/src/runtime/interpreter.ts +164 -14
- package/src/runtime/loader.ts +83 -5
- package/src/runtime/logger.ts +12 -1
- package/src/runtime/module.ts +78 -3
- package/src/runtime/modules/ai.ts +263 -76
- package/src/runtime/modules/core.ts +4 -0
- package/src/runtime/modules/messaging.ts +228 -0
- package/src/runtime/resolvers/interface.ts +19 -1
- package/src/runtime/resolvers/sqldb/database.ts +158 -130
- package/src/runtime/resolvers/sqldb/dbutil.ts +8 -0
- package/src/runtime/resolvers/sqldb/impl.ts +8 -0
- 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/syntaxes/agentlang.monarch.ts +1 -1
|
@@ -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,6 +29,7 @@ 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;
|
|
@@ -83,8 +90,7 @@ export declare class AgentInstance {
|
|
|
83
90
|
private getValidationEvent;
|
|
84
91
|
private getExternalToolSpecs;
|
|
85
92
|
private maybeAddRelevantDocuments;
|
|
86
|
-
private
|
|
87
|
-
private static getDocumentTitle;
|
|
93
|
+
private queryRemoteKnowledgeService;
|
|
88
94
|
private static ToolsCache;
|
|
89
95
|
private toolsAsString;
|
|
90
96
|
}
|
|
@@ -102,6 +108,7 @@ export declare function loadFlowStepResults(chatId: string): Promise<Instance[]>
|
|
|
102
108
|
export declare function loadLocalAgentResult(chatId: string, step: string): Promise<string | undefined>;
|
|
103
109
|
export declare function loadLocalFlowStep(chatId: string, step: string): Promise<Instance | undefined>;
|
|
104
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>;
|
|
105
112
|
export declare function fetchAndCreateDocument(title: string, url: string, retrievalConfig: Map<string, any> | undefined, embeddingConfig: Map<string, any> | undefined, _env: Environment): Promise<any>;
|
|
106
113
|
export declare function saveAgentResponse(fileName: string, response: string, env: Environment): Promise<string>;
|
|
107
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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S,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,19 +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 { isMonitoringEnabled, TtlCache } from '../state.js';
|
|
9
|
+
import { getKnowledgeGraphConfig, isMonitoringEnabled, TtlCache } from '../state.js';
|
|
10
10
|
import { isNodeEnv } from '../../utils/runtime.js';
|
|
11
11
|
import { getFileSystem } from '../../utils/fs-utils.js';
|
|
12
|
+
import { getDocumentRetriever } from '../document-retriever.js';
|
|
12
13
|
export const CoreAIModuleName = makeCoreModuleName('ai');
|
|
13
14
|
export const AgentEntityName = 'Agent';
|
|
14
15
|
export const LlmEntityName = 'LLM';
|
|
15
16
|
export const AgentLearnerType = 'learner';
|
|
16
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
|
+
}
|
|
17
56
|
export default `module ${CoreAIModuleName}
|
|
18
57
|
|
|
19
58
|
import "./modules/ai.js" @as ai
|
|
@@ -32,7 +71,8 @@ entity ${AgentEntityName} {
|
|
|
32
71
|
stateless Boolean @default(false),
|
|
33
72
|
instruction String @optional,
|
|
34
73
|
tools String @optional, // comma-separated list of tool names
|
|
35
|
-
documents String @optional, //
|
|
74
|
+
documents String @optional, // list of document names
|
|
75
|
+
topics String @optional, // list of topic names
|
|
36
76
|
channels String @optional, // comma-separated list of channel names
|
|
37
77
|
role String @optional,
|
|
38
78
|
flows String @optional,
|
|
@@ -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) {
|
|
@@ -613,6 +674,7 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
613
674
|
const p = await findProviderForLLM(this.llm, env);
|
|
614
675
|
const agentName = this.name;
|
|
615
676
|
const chatId = env.getAgentChatId() || agentName;
|
|
677
|
+
await checkCancelled(chatId);
|
|
616
678
|
let isplnr = this.isPlanner();
|
|
617
679
|
const isflow = !isplnr && this.isFlowExecutor();
|
|
618
680
|
const iseval = !isplnr && !isflow && this.isEvaluator();
|
|
@@ -707,7 +769,7 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
707
769
|
let response = await p.invoke(msgs, externalToolSpecs);
|
|
708
770
|
const v = this.getValidationEvent();
|
|
709
771
|
if (v) {
|
|
710
|
-
response = await this.handleValidation(response, v, msgs, p);
|
|
772
|
+
response = await this.handleValidation(response, v, msgs, p, chatId);
|
|
711
773
|
}
|
|
712
774
|
if (!iseval)
|
|
713
775
|
await AgentInstance.maybeEvaluateResponse(this, message, msgsContent, response.content, env);
|
|
@@ -781,7 +843,7 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
781
843
|
return r;
|
|
782
844
|
}
|
|
783
845
|
}
|
|
784
|
-
async handleValidation(response, validationEventName, msgs, provider) {
|
|
846
|
+
async handleValidation(response, validationEventName, msgs, provider, chatId) {
|
|
785
847
|
let r = await this.invokeValidator(response, validationEventName);
|
|
786
848
|
const status = r.lookup('status');
|
|
787
849
|
if (status === 'ok') {
|
|
@@ -793,6 +855,8 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
793
855
|
let attempt = 0;
|
|
794
856
|
let delay = this.retryObj.getNextDelayMs(attempt);
|
|
795
857
|
while (delay) {
|
|
858
|
+
if (chatId)
|
|
859
|
+
await checkCancelled(chatId);
|
|
796
860
|
msgs.push(assistantMessage(resp.content));
|
|
797
861
|
const vs = JSON.stringify(r.asSerializableObject());
|
|
798
862
|
msgs.push(humanMessage(`Validation for your last response failed with this result: \n${vs}\n\nFix the errors.`));
|
|
@@ -837,77 +901,106 @@ Only return a pure JSON object with no extra text, annotations etc.`;
|
|
|
837
901
|
}
|
|
838
902
|
return result;
|
|
839
903
|
}
|
|
840
|
-
async maybeAddRelevantDocuments(message,
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
return message.concat('\n\nRelevant context from documents:\n').concat(docs
|
|
884
|
-
.map((v) => {
|
|
885
|
-
return v.lookup('content');
|
|
886
|
-
})
|
|
887
|
-
.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);
|
|
888
947
|
}
|
|
889
948
|
}
|
|
890
949
|
}
|
|
891
|
-
|
|
892
|
-
|
|
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);
|
|
893
953
|
}
|
|
894
954
|
}
|
|
955
|
+
catch (err) {
|
|
956
|
+
logger.debug(`[KNOWLEDGE] Knowledge retrieval failed: ${err}`);
|
|
957
|
+
}
|
|
895
958
|
return message;
|
|
896
959
|
}
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
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
|
+
});
|
|
906
993
|
}
|
|
907
|
-
|
|
908
|
-
|
|
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
|
+
}
|
|
909
1002
|
}
|
|
910
|
-
return
|
|
1003
|
+
return message;
|
|
911
1004
|
}
|
|
912
1005
|
toolsAsString() {
|
|
913
1006
|
const cachedTools = AgentInstance.ToolsCache.get(this.name);
|
|
@@ -1169,6 +1262,7 @@ export async function processAgentLearning(moduleName, agentName, instruction, e
|
|
|
1169
1262
|
const LocalAgentgFlow = true;
|
|
1170
1263
|
const LocalFlowStepsRootDirName = 'flows';
|
|
1171
1264
|
const AgentFlowStep = 'AgentFlowStep';
|
|
1265
|
+
const CancelledAgentEntity = 'CancelledAgent';
|
|
1172
1266
|
export async function saveFlowStepResultLocally(chatId, step, result, suspensionId) {
|
|
1173
1267
|
const fs = await getFileSystem();
|
|
1174
1268
|
const rootDirName = LocalFlowStepsRootDirName;
|
|
@@ -1297,6 +1391,64 @@ export async function loadFlowStep(chatId, step) {
|
|
|
1297
1391
|
}
|
|
1298
1392
|
}
|
|
1299
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
|
+
}
|
|
1300
1452
|
export async function fetchAndCreateDocument(title, url, retrievalConfig, embeddingConfig, _env) {
|
|
1301
1453
|
if (!isNodeEnv) {
|
|
1302
1454
|
throw new Error('Document fetching is only available in Node.js environment');
|