@indexnetwork/protocol 4.5.0-rc.326.1 → 4.5.0-rc.328.1
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/chat/chat.agent.d.ts +8 -2
- package/dist/chat/chat.agent.d.ts.map +1 -1
- package/dist/chat/chat.agent.js +26 -14
- package/dist/chat/chat.agent.js.map +1 -1
- package/dist/chat/chat.graph.d.ts +16 -1
- package/dist/chat/chat.graph.d.ts.map +1 -1
- package/dist/chat/chat.graph.js +19 -2
- package/dist/chat/chat.graph.js.map +1 -1
- package/dist/chat/chat.persona.d.ts +56 -0
- package/dist/chat/chat.persona.d.ts.map +1 -0
- package/dist/chat/chat.persona.js +26 -0
- package/dist/chat/chat.persona.js.map +1 -0
- package/dist/chat/negotiator.persona.d.ts +49 -0
- package/dist/chat/negotiator.persona.d.ts.map +1 -0
- package/dist/chat/negotiator.persona.js +78 -0
- package/dist/chat/negotiator.persona.js.map +1 -0
- package/dist/chat/negotiator.prompt.d.ts +24 -0
- package/dist/chat/negotiator.prompt.d.ts.map +1 -0
- package/dist/chat/negotiator.prompt.js +71 -0
- package/dist/chat/negotiator.prompt.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BaseMessage } from "@langchain/core/messages";
|
|
2
|
-
import {
|
|
2
|
+
import type { ToolContext } from "../shared/agent/tool.factory.js";
|
|
3
|
+
import { type ChatPersonaConfig } from "./chat.persona.js";
|
|
3
4
|
import type { DebugMetaToolCall, DebugMetaLlm, DebugMetaOrchestratorNegotiations, DebugMetaDiscoveryQuestions } from "./chat-streaming.types.js";
|
|
4
5
|
import type { Question, QuestionStrategy } from "../shared/schemas/question.schema.js";
|
|
5
6
|
import type { Opportunity } from "../shared/interfaces/database.interface.js";
|
|
@@ -222,6 +223,7 @@ export interface AgentIterationResult {
|
|
|
222
223
|
*/
|
|
223
224
|
export declare class ChatAgent {
|
|
224
225
|
private resolvedContext;
|
|
226
|
+
private persona;
|
|
225
227
|
private model;
|
|
226
228
|
private tools;
|
|
227
229
|
private toolsByName;
|
|
@@ -236,8 +238,12 @@ export declare class ChatAgent {
|
|
|
236
238
|
/**
|
|
237
239
|
* Async factory: creates a ChatAgent with resolved user/index context.
|
|
238
240
|
* Resolves user/network identity from DB during tool initialization.
|
|
241
|
+
*
|
|
242
|
+
* @param context - Tool context (database, userId, scope, deps)
|
|
243
|
+
* @param persona - Persona config (prompt builder, toolset, loop behaviors).
|
|
244
|
+
* Defaults to the orchestrator persona.
|
|
239
245
|
*/
|
|
240
|
-
static create(context: ToolContext): Promise<ChatAgent>;
|
|
246
|
+
static create(context: ToolContext, persona?: ChatPersonaConfig): Promise<ChatAgent>;
|
|
241
247
|
/**
|
|
242
248
|
* Run a single iteration of the agent loop.
|
|
243
249
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.agent.d.ts","sourceRoot":"/","sources":["chat/chat.agent.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,WAAW,EAA8C,MAAM,0BAA0B,CAAC;AAC9G,OAAO,
|
|
1
|
+
{"version":3,"file":"chat.agent.d.ts","sourceRoot":"/","sources":["chat/chat.agent.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,WAAW,EAA8C,MAAM,0BAA0B,CAAC;AAC9G,OAAO,KAAK,EAAa,WAAW,EAAuB,MAAM,iCAAiC,CAAC;AAInG,OAAO,EAAwB,KAAK,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAMjF,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,iCAAiC,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AACjJ,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AACvF,OAAO,KAAK,EAAE,WAAW,EAA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,MAAM,CAAC;KACxB,CAAC;IAiDF;;;;;;;;;;;OAWG;IAEG,SAAS,CACb,eAAe,EAAE,WAAW,EAAE,EAC9B,MAAM,CAAC,EAAE,YAAY,EACrB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC;QACT,YAAY,EAAE,MAAM,CAAC;QACrB,QAAQ,EAAE,WAAW,EAAE,CAAC;QACxB,cAAc,EAAE,MAAM,CAAC;QACvB,SAAS,EAAE;YAAE,KAAK,EAAE,MAAM,CAAC;YAAC,UAAU,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,iBAAiB,EAAE,CAAC;YAAC,GAAG,EAAE,YAAY,CAAC;YAAC,wBAAwB,CAAC,EAAE,iCAAiC,CAAC;YAAC,kBAAkB,CAAC,EAAE,2BAA2B,CAAA;SAAE,CAAC;KACjN,CAAC;CAwkBH"}
|
package/dist/chat/chat.agent.js
CHANGED
|
@@ -8,10 +8,10 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
import { AIMessage, SystemMessage, ToolMessage } from "@langchain/core/messages";
|
|
11
|
-
import { createChatTools } from "../shared/agent/tool.factory.js";
|
|
12
11
|
import { resolveChatContext } from "../shared/agent/tool.helpers.js";
|
|
13
12
|
import { scopeFromNetworkId } from "../shared/agent/tool.scope.js";
|
|
14
|
-
import { ITERATION_NUDGE
|
|
13
|
+
import { ITERATION_NUDGE } from "./chat.prompt.js";
|
|
14
|
+
import { ORCHESTRATOR_PERSONA } from "./chat.persona.js";
|
|
15
15
|
import { extractRecentToolCalls } from "./chat.prompt.modules.js";
|
|
16
16
|
import { protocolLogger } from "../shared/observability/protocol.logger.js";
|
|
17
17
|
import { createModel } from "../shared/agent/model.config.js";
|
|
@@ -70,8 +70,9 @@ export class ChatAgent {
|
|
|
70
70
|
/**
|
|
71
71
|
* Private constructor — use `ChatAgent.create()` instead.
|
|
72
72
|
*/
|
|
73
|
-
constructor(resolvedContext, tools, modelConfig) {
|
|
73
|
+
constructor(resolvedContext, persona, tools, modelConfig) {
|
|
74
74
|
this.resolvedContext = resolvedContext;
|
|
75
|
+
this.persona = persona;
|
|
75
76
|
this.model = createModel("chat", modelConfig);
|
|
76
77
|
// Store tools and index by name
|
|
77
78
|
this.tools = tools;
|
|
@@ -97,8 +98,12 @@ export class ChatAgent {
|
|
|
97
98
|
/**
|
|
98
99
|
* Async factory: creates a ChatAgent with resolved user/index context.
|
|
99
100
|
* Resolves user/network identity from DB during tool initialization.
|
|
101
|
+
*
|
|
102
|
+
* @param context - Tool context (database, userId, scope, deps)
|
|
103
|
+
* @param persona - Persona config (prompt builder, toolset, loop behaviors).
|
|
104
|
+
* Defaults to the orchestrator persona.
|
|
100
105
|
*/
|
|
101
|
-
static async create(context) {
|
|
106
|
+
static async create(context, persona = ORCHESTRATOR_PERSONA) {
|
|
102
107
|
const { networkId: legacyNetworkId } = context;
|
|
103
108
|
const explicitScope = context.scopeType && context.scopeId
|
|
104
109
|
? { scopeType: context.scopeType, scopeId: context.scopeId }
|
|
@@ -115,8 +120,8 @@ export class ChatAgent {
|
|
|
115
120
|
resolved.scopeType = explicitScope.scopeType;
|
|
116
121
|
resolved.scopeId = explicitScope.scopeId;
|
|
117
122
|
}
|
|
118
|
-
const tools = await
|
|
119
|
-
return new ChatAgent(resolved, tools, context.modelConfig);
|
|
123
|
+
const tools = await persona.createTools(context, resolved);
|
|
124
|
+
return new ChatAgent(resolved, persona, tools, context.modelConfig);
|
|
120
125
|
}
|
|
121
126
|
/**
|
|
122
127
|
* Run a single iteration of the agent loop.
|
|
@@ -131,7 +136,7 @@ export class ChatAgent {
|
|
|
131
136
|
currentMessage: ChatAgent.getCurrentUserMessage(messages),
|
|
132
137
|
ctx: this.resolvedContext,
|
|
133
138
|
};
|
|
134
|
-
const systemContent = buildSystemContent(this.resolvedContext, iterCtx);
|
|
139
|
+
const systemContent = this.persona.buildSystemContent(this.resolvedContext, iterCtx);
|
|
135
140
|
const fullMessages = [
|
|
136
141
|
new SystemMessage(systemContent),
|
|
137
142
|
...messages,
|
|
@@ -223,7 +228,7 @@ export class ChatAgent {
|
|
|
223
228
|
logger.verbose("Executing tool", { name: tc.name, args: tc.args });
|
|
224
229
|
let result = await tool.invoke(tc.args);
|
|
225
230
|
let resultStr = typeof result === "string" ? result : JSON.stringify(result);
|
|
226
|
-
if (tc.name === "discover_opportunities") {
|
|
231
|
+
if (tc.name === "discover_opportunities" && this.persona.loopBehaviors.createIntentCallback) {
|
|
227
232
|
const newResult = await this.handleCreateIntentCallback(resultStr, tc.args);
|
|
228
233
|
if (newResult !== null) {
|
|
229
234
|
resultStr = newResult;
|
|
@@ -394,8 +399,8 @@ export class ChatAgent {
|
|
|
394
399
|
*/
|
|
395
400
|
async normalizeToolResult(toolName, resultStr, toolArgs) {
|
|
396
401
|
let normalized = resultStr;
|
|
397
|
-
// Run create_intent callback for discover_opportunities results
|
|
398
|
-
if (toolName === "discover_opportunities") {
|
|
402
|
+
// Run create_intent callback for discover_opportunities results (orchestrator behavior)
|
|
403
|
+
if (toolName === "discover_opportunities" && this.persona.loopBehaviors.createIntentCallback) {
|
|
399
404
|
const callbackResult = await this.handleCreateIntentCallback(normalized, toolArgs);
|
|
400
405
|
if (callbackResult !== null) {
|
|
401
406
|
normalized = callbackResult;
|
|
@@ -575,7 +580,7 @@ export class ChatAgent {
|
|
|
575
580
|
currentMessage: ChatAgent.getCurrentUserMessage(messages),
|
|
576
581
|
ctx: this.resolvedContext,
|
|
577
582
|
};
|
|
578
|
-
const systemContent = buildSystemContent(this.resolvedContext, iterCtx);
|
|
583
|
+
const systemContent = this.persona.buildSystemContent(this.resolvedContext, iterCtx);
|
|
579
584
|
const fullMessages = [
|
|
580
585
|
new SystemMessage(systemContent),
|
|
581
586
|
...messages,
|
|
@@ -780,7 +785,11 @@ export class ChatAgent {
|
|
|
780
785
|
// directly instead of calling the corresponding tool. These blocks
|
|
781
786
|
// lack valid proposalIds / data and won't work in the frontend.
|
|
782
787
|
// Auto-invoke the correct tool directly instead of re-asking the LLM.
|
|
783
|
-
|
|
788
|
+
// Persona-gated: only personas with hallucinationRecovery enabled (orchestrator)
|
|
789
|
+
// run detection/auto-invoke — other personas never trigger this branch.
|
|
790
|
+
const hallucinatedBlock = this.persona.loopBehaviors.hallucinationRecovery
|
|
791
|
+
? this.detectHallucinatedBlock(iterationText, toolsDebug, iterCtx.currentMessage)
|
|
792
|
+
: null;
|
|
784
793
|
const priorAutoInvokes = hallucinatedBlock ? (hallucinationAutoInvokeCounts.get(hallucinatedBlock.type) ?? 0) : 0;
|
|
785
794
|
if (hallucinatedBlock && priorAutoInvokes < 2 && iterationCount < HARD_ITERATION_LIMIT - 1) {
|
|
786
795
|
hallucinationAutoInvokeCounts.set(hallucinatedBlock.type, priorAutoInvokes + 1);
|
|
@@ -938,8 +947,11 @@ export class ChatAgent {
|
|
|
938
947
|
}
|
|
939
948
|
// ── Final response already streamed ─────────────────────────────
|
|
940
949
|
// Defense-in-depth: strip any code blocks that require tool backing
|
|
941
|
-
// but slipped through without a successful tool call
|
|
942
|
-
|
|
950
|
+
// but slipped through without a successful tool call (persona-gated,
|
|
951
|
+
// same flag as detection since both police tool-backed block types).
|
|
952
|
+
const sanitizedText = this.persona.loopBehaviors.hallucinationRecovery
|
|
953
|
+
? this.stripUnbackedBlocks(iterationText, toolsDebug)
|
|
954
|
+
: iterationText;
|
|
943
955
|
if (sanitizedText !== iterationText) {
|
|
944
956
|
logger.warn("Streaming: stripped unbacked code blocks from final response", {
|
|
945
957
|
originalLength: iterationText.length,
|