@memberjunction/ai-agents 6.2.0-edge.1 → 6.2.0-edge.2
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 +18 -0
- package/dist/AgentDecisionService.d.ts +83 -0
- package/dist/AgentDecisionService.d.ts.map +1 -0
- package/dist/AgentDecisionService.js +155 -0
- package/dist/AgentDecisionService.js.map +1 -0
- package/dist/AgentRunner.d.ts +7 -0
- package/dist/AgentRunner.d.ts.map +1 -1
- package/dist/AgentRunner.js +16 -5
- package/dist/AgentRunner.js.map +1 -1
- package/dist/KnowledgeAgent.d.ts.map +1 -1
- package/dist/KnowledgeAgent.js +6 -6
- package/dist/KnowledgeAgent.js.map +1 -1
- package/dist/PayloadFeedbackManager.d.ts +113 -14
- package/dist/PayloadFeedbackManager.d.ts.map +1 -1
- package/dist/PayloadFeedbackManager.js +228 -88
- package/dist/PayloadFeedbackManager.js.map +1 -1
- package/dist/agent-context-injector.d.ts +47 -3
- package/dist/agent-context-injector.d.ts.map +1 -1
- package/dist/agent-context-injector.js +77 -10
- package/dist/agent-context-injector.js.map +1 -1
- package/dist/agent-memory-context-builder.d.ts +13 -0
- package/dist/agent-memory-context-builder.d.ts.map +1 -1
- package/dist/agent-memory-context-builder.js +24 -5
- package/dist/agent-memory-context-builder.js.map +1 -1
- package/dist/agent-types/flow-agent-type.d.ts +97 -0
- package/dist/agent-types/flow-agent-type.d.ts.map +1 -1
- package/dist/agent-types/flow-agent-type.js +281 -24
- package/dist/agent-types/flow-agent-type.js.map +1 -1
- package/dist/agent-types/flow-graph-executor.d.ts +12 -1
- package/dist/agent-types/flow-graph-executor.d.ts.map +1 -1
- package/dist/agent-types/flow-graph-executor.js +25 -1
- package/dist/agent-types/flow-graph-executor.js.map +1 -1
- package/dist/agent-types/loop-agent-prompt-params.d.ts +239 -66
- package/dist/agent-types/loop-agent-prompt-params.d.ts.map +1 -1
- package/dist/agent-types/loop-agent-prompt-params.js +47 -2
- package/dist/agent-types/loop-agent-prompt-params.js.map +1 -1
- package/dist/agent-types/loop-agent-response-type.d.ts +12 -1
- package/dist/agent-types/loop-agent-response-type.d.ts.map +1 -1
- package/dist/agent-types/loop-agent-response-type.js.map +1 -1
- package/dist/agent-types/loop-agent-type.d.ts +6 -0
- package/dist/agent-types/loop-agent-type.d.ts.map +1 -1
- package/dist/agent-types/loop-agent-type.js +42 -3
- package/dist/agent-types/loop-agent-type.js.map +1 -1
- package/dist/base-agent.d.ts +492 -2
- package/dist/base-agent.d.ts.map +1 -1
- package/dist/base-agent.js +1686 -95
- package/dist/base-agent.js.map +1 -1
- package/dist/catalog-narrowing.d.ts +136 -0
- package/dist/catalog-narrowing.d.ts.map +1 -0
- package/dist/catalog-narrowing.js +212 -0
- package/dist/catalog-narrowing.js.map +1 -0
- package/dist/decision-discovery.d.ts +401 -0
- package/dist/decision-discovery.d.ts.map +1 -0
- package/dist/decision-discovery.js +511 -0
- package/dist/decision-discovery.js.map +1 -0
- package/dist/finish-if-mode-warnings.d.ts +39 -0
- package/dist/finish-if-mode-warnings.d.ts.map +1 -0
- package/dist/finish-if-mode-warnings.js +78 -0
- package/dist/finish-if-mode-warnings.js.map +1 -0
- package/dist/finish-if-state.d.ts +72 -0
- package/dist/finish-if-state.d.ts.map +1 -0
- package/dist/finish-if-state.js +109 -0
- package/dist/finish-if-state.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/memory-manager-agent.d.ts +108 -2
- package/dist/memory-manager-agent.d.ts.map +1 -1
- package/dist/memory-manager-agent.js +172 -9
- package/dist/memory-manager-agent.js.map +1 -1
- package/dist/memory-note-gate.d.ts +208 -0
- package/dist/memory-note-gate.d.ts.map +1 -0
- package/dist/memory-note-gate.js +294 -0
- package/dist/memory-note-gate.js.map +1 -0
- package/dist/realtime/bridge-realtime-session-factory.d.ts +5 -2
- package/dist/realtime/bridge-realtime-session-factory.d.ts.map +1 -1
- package/dist/realtime/bridge-realtime-session-factory.js +7 -4
- package/dist/realtime/bridge-realtime-session-factory.js.map +1 -1
- package/dist/realtime/bridge-room-transcript-sink.d.ts.map +1 -1
- package/dist/realtime/bridge-room-transcript-sink.js +15 -9
- package/dist/realtime/bridge-room-transcript-sink.js.map +1 -1
- package/dist/realtime/realtime-client-session-service.d.ts +54 -12
- package/dist/realtime/realtime-client-session-service.d.ts.map +1 -1
- package/dist/realtime/realtime-client-session-service.js +76 -25
- package/dist/realtime/realtime-client-session-service.js.map +1 -1
- package/dist/realtime/realtime-vendor-resolution.d.ts +8 -3
- package/dist/realtime/realtime-vendor-resolution.d.ts.map +1 -1
- package/dist/realtime/realtime-vendor-resolution.js +3 -3
- package/dist/realtime/realtime-vendor-resolution.js.map +1 -1
- package/dist/rubric-evaluation-agent-runner.d.ts +2 -0
- package/dist/rubric-evaluation-agent-runner.d.ts.map +1 -0
- package/dist/rubric-evaluation-agent-runner.js +38 -0
- package/dist/rubric-evaluation-agent-runner.js.map +1 -0
- package/dist/self-check.d.ts +98 -0
- package/dist/self-check.d.ts.map +1 -0
- package/dist/self-check.js +74 -0
- package/dist/self-check.js.map +1 -0
- package/package.json +19 -18
|
@@ -9,19 +9,53 @@
|
|
|
9
9
|
* @since 3.1.0
|
|
10
10
|
*/
|
|
11
11
|
import { LogStatus, LogError } from '@memberjunction/core';
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
|
|
12
|
+
import { PayloadWarningType } from './PayloadChangeAnalyzer.js';
|
|
13
|
+
import { AgentDecisionService } from './AgentDecisionService.js';
|
|
14
|
+
/** The probability at or above which a change is judged intended, unless configured. */
|
|
15
|
+
const DEFAULT_INTENDED_THRESHOLD = 0.5;
|
|
16
|
+
/** The questions one decision call carries, unless `maxQuestionsPerBatch` says otherwise. */
|
|
17
|
+
const DEFAULT_MAX_QUESTIONS_PER_CALL = 10;
|
|
18
|
+
/** The most characters of each reasoning or message field the decision state carries. */
|
|
19
|
+
const MAX_CONTEXT_TEXT = 4000;
|
|
20
|
+
/** The most removed key names listed for one change. */
|
|
21
|
+
const MAX_LISTED_KEYS = 10;
|
|
15
22
|
/**
|
|
16
23
|
* Manages feedback collection for suspicious payload changes
|
|
17
24
|
*/
|
|
18
25
|
export class PayloadFeedbackManager {
|
|
19
|
-
|
|
26
|
+
/**
|
|
27
|
+
* The longest the decision call may take. Past it the call is aborted and every change is
|
|
28
|
+
* accepted, as on any other failure. The same bound as the catalog narrowing decision.
|
|
29
|
+
*/
|
|
30
|
+
static { this.DECISION_TIMEOUT_MS = 30000; }
|
|
31
|
+
/**
|
|
32
|
+
* @param config - Feedback settings. See {@link PayloadFeedbackConfig}.
|
|
33
|
+
* @param decisionService - Answers the questions in {@link QueryAgent}. Defaults to a new
|
|
34
|
+
* {@link AgentDecisionService}.
|
|
35
|
+
*/
|
|
36
|
+
constructor(config, decisionService) {
|
|
20
37
|
this.config = {
|
|
21
|
-
maxQuestionsPerBatch:
|
|
38
|
+
maxQuestionsPerBatch: DEFAULT_MAX_QUESTIONS_PER_CALL,
|
|
22
39
|
temperature: 0.1,
|
|
40
|
+
intendedThreshold: DEFAULT_INTENDED_THRESHOLD,
|
|
23
41
|
...config
|
|
24
42
|
};
|
|
43
|
+
this.decisionService = decisionService ?? new AgentDecisionService();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The probability at or above which a change is judged intended: `intendedThreshold`, or 0.5
|
|
47
|
+
* when that is not a number from 0 to 1.
|
|
48
|
+
*/
|
|
49
|
+
get IntendedThreshold() {
|
|
50
|
+
const threshold = this.config.intendedThreshold;
|
|
51
|
+
return typeof threshold === 'number' && threshold >= 0 && threshold <= 1 ? threshold : DEFAULT_INTENDED_THRESHOLD;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The result of the most recent decision call {@link QueryAgent} made, so the caller can link
|
|
55
|
+
* its prompt run to a run step and count its cost. Undefined until a call is made.
|
|
56
|
+
*/
|
|
57
|
+
get LastDecisionResult() {
|
|
58
|
+
return this._lastDecisionResult;
|
|
25
59
|
}
|
|
26
60
|
/**
|
|
27
61
|
* Generate feedback questions from warnings
|
|
@@ -76,108 +110,214 @@ export class PayloadFeedbackManager {
|
|
|
76
110
|
};
|
|
77
111
|
}
|
|
78
112
|
/**
|
|
79
|
-
*
|
|
113
|
+
* Asks whether each suspicious payload change was intended: one Likelihood per question, all
|
|
114
|
+
* in one decision call. The state is the agent's reasoning and message for the step plus a
|
|
115
|
+
* compact list of the changes (paths, and sizes or types), never the payload itself.
|
|
116
|
+
*
|
|
117
|
+
* A change is `intended` when its probability is at or above {@link IntendedThreshold}, and
|
|
118
|
+
* its explanation carries the probability. When no decision answers a change (no context user,
|
|
119
|
+
* a failed, throwing, cancelled or timed-out call, a missing answer, or more questions than one
|
|
120
|
+
* call carries), the change is accepted, as it always was, and its explanation says why. Never
|
|
121
|
+
* throws.
|
|
80
122
|
*
|
|
81
|
-
* @param questions -
|
|
82
|
-
* @param
|
|
83
|
-
* @param contextUser -
|
|
84
|
-
* @returns
|
|
123
|
+
* @param questions - The questions from {@link GenerateQuestions}
|
|
124
|
+
* @param context - The agent's reasoning and message for the step, and the call's options
|
|
125
|
+
* @param contextUser - The user the decision runs as
|
|
126
|
+
* @returns One response per question, in the same order
|
|
85
127
|
*/
|
|
86
|
-
async QueryAgent(questions,
|
|
128
|
+
async QueryAgent(questions, context, contextUser) {
|
|
87
129
|
if (questions.length === 0) {
|
|
88
130
|
return [];
|
|
89
131
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const prompt = AIEngine.Instance.Prompts.find(p => p.Name.trim().toLowerCase() === promptName.toLowerCase() &&
|
|
93
|
-
p.Category?.trim().toLowerCase() === 'mj: system');
|
|
94
|
-
if (!prompt) {
|
|
95
|
-
// Fallback: accept all changes if prompt not configured
|
|
96
|
-
LogStatus(`PayloadFeedbackManager: Prompt "${promptName}" not found in MJ: System category. Accepting all changes by default.`);
|
|
97
|
-
return questions.map(q => ({
|
|
98
|
-
questionId: q.id,
|
|
99
|
-
intended: true,
|
|
100
|
-
explanation: 'Accepted by default (feedback prompt not configured)'
|
|
101
|
-
}));
|
|
102
|
-
}
|
|
103
|
-
try {
|
|
104
|
-
// Build template parameters
|
|
105
|
-
const templateData = this.buildFeedbackTemplateParams(questions);
|
|
106
|
-
// Execute the prompt
|
|
107
|
-
const runner = new AIPromptRunner();
|
|
108
|
-
const promptParams = new AIPromptParams();
|
|
109
|
-
promptParams.prompt = prompt;
|
|
110
|
-
promptParams.data = templateData;
|
|
111
|
-
if (contextUser) {
|
|
112
|
-
promptParams.contextUser = contextUser;
|
|
113
|
-
}
|
|
114
|
-
const result = await runner.ExecutePrompt(promptParams);
|
|
115
|
-
if (!result.success || !result.result?.responses) {
|
|
116
|
-
LogError('PayloadFeedbackManager: Failed to get valid response from feedback prompt', undefined, result.errorMessage);
|
|
117
|
-
// Fallback to accepting all changes
|
|
118
|
-
return questions.map(q => ({
|
|
119
|
-
questionId: q.id,
|
|
120
|
-
intended: true,
|
|
121
|
-
explanation: 'Accepted by default (prompt execution failed)'
|
|
122
|
-
}));
|
|
123
|
-
}
|
|
124
|
-
// Map LLM responses back to our format
|
|
125
|
-
return this.mapLLMResponsesToFeedback(questions, result.result.responses);
|
|
132
|
+
if (!contextUser) {
|
|
133
|
+
return questions.map(q => this.acceptedByDefault(q, 'no context user for the decision call'));
|
|
126
134
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}));
|
|
135
|
+
const asked = questions.slice(0, this.maxQuestionsPerCall());
|
|
136
|
+
const result = await this.askDecisions(asked, context ?? {}, contextUser);
|
|
137
|
+
this._lastDecisionResult = result;
|
|
138
|
+
if (!result.success) {
|
|
139
|
+
const reason = result.errorMessage || 'no error message';
|
|
140
|
+
LogError(`PayloadFeedbackManager: the payload change decisions failed, so every change is accepted: ${reason}`);
|
|
141
|
+
return questions.map(q => this.acceptedByDefault(q, `the decision call failed: ${reason}`));
|
|
135
142
|
}
|
|
143
|
+
return questions.map((question, i) => i < asked.length
|
|
144
|
+
? this.mapAnswerToFeedback(question, result.Answers[this.questionKey(i)])
|
|
145
|
+
: this.acceptedByDefault(question, `over the limit of ${asked.length} questions per decision call`));
|
|
136
146
|
}
|
|
137
147
|
/** @deprecated Use {@link QueryAgent}. */
|
|
138
|
-
async queryAgent(questions,
|
|
139
|
-
return this.QueryAgent(questions,
|
|
148
|
+
async queryAgent(questions, context, contextUser) {
|
|
149
|
+
return this.QueryAgent(questions, context, contextUser);
|
|
140
150
|
}
|
|
141
151
|
/**
|
|
142
|
-
*
|
|
152
|
+
* Describes one flagged change in a line: what the analyzer saw, where, and the names of any
|
|
153
|
+
* removed keys. The analyzer's message carries the sizes or types, never payload content.
|
|
143
154
|
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
155
|
+
DescribeChange(warning) {
|
|
156
|
+
const where = warning.path ? `"${warning.path}"` : 'the payload root';
|
|
157
|
+
const keys = this.removedKeyNames(warning);
|
|
158
|
+
const removed = keys.length > 0 ? ` (removed: ${keys.join(', ')})` : '';
|
|
159
|
+
return `${warning.message} at ${where}${removed}`;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The message that lists the changes judged unintended and asks the agent to confirm them or put
|
|
163
|
+
* them back, or `undefined` when every change was judged intended. It reverts nothing itself, and
|
|
164
|
+
* it never carries the original values, so it tells the agent how to rebuild them.
|
|
165
|
+
*/
|
|
166
|
+
BuildUnintendedChangesMessage(questions, responses) {
|
|
167
|
+
const lines = [];
|
|
168
|
+
const unintended = [];
|
|
169
|
+
for (const response of responses) {
|
|
170
|
+
const question = questions.find(q => q.id === response.questionId);
|
|
171
|
+
if (question && !response.intended) {
|
|
172
|
+
const probability = typeof response.probability === 'number'
|
|
173
|
+
? ` (probability it was intended: ${response.probability.toFixed(2)})`
|
|
174
|
+
: '';
|
|
175
|
+
lines.push(`- ${this.DescribeChange(question.warning)}${probability}`);
|
|
176
|
+
unintended.push(question.warning);
|
|
154
177
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
178
|
+
}
|
|
179
|
+
if (lines.length === 0) {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
return [
|
|
183
|
+
'Payload change check: these changes to the payload may not have been intended.',
|
|
184
|
+
...lines,
|
|
185
|
+
'Nothing was reverted. If you meant a change, confirm it in your reasoning and carry on.',
|
|
186
|
+
this.restoreInstructions(unintended)
|
|
187
|
+
].join('\n');
|
|
162
188
|
}
|
|
163
189
|
/**
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
190
|
+
* How the agent puts the listed changes back: a removed key is added again under `newElements`,
|
|
191
|
+
* and any other change is set again under `updateElements`. The message has no original values,
|
|
192
|
+
* so the agent rebuilds them from their source, or says what was lost.
|
|
193
|
+
*/
|
|
194
|
+
restoreInstructions(warnings) {
|
|
195
|
+
const steps = [];
|
|
196
|
+
if (warnings.some(w => w.type === PayloadWarningType.KeyRemoval)) {
|
|
197
|
+
steps.push('add each removed key back under newElements');
|
|
198
|
+
}
|
|
199
|
+
if (warnings.some(w => w.type !== PayloadWarningType.KeyRemoval)) {
|
|
200
|
+
steps.push('set each shortened or changed value again under updateElements');
|
|
201
|
+
}
|
|
202
|
+
return `If not, put the data back with a payloadChangeRequest: ${steps.join(', and ')}. `
|
|
203
|
+
+ 'The original values are not shown here, so rebuild each one from where it came from '
|
|
204
|
+
+ '(an action result, a sub-agent result or the conversation). If you cannot, say in your message what was lost.';
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Asks the questions in one call. Never throws, and never waits longer than
|
|
208
|
+
* {@link PayloadFeedbackManager.DECISION_TIMEOUT_MS} or past the run's cancellation: a throw, a
|
|
209
|
+
* timeout and a cancelled run each come back as a failed result, so the caller accepts every
|
|
210
|
+
* change. Stopping also aborts the call.
|
|
167
211
|
*/
|
|
168
|
-
|
|
212
|
+
async askDecisions(questions, context, contextUser) {
|
|
213
|
+
const runToken = context.CancellationToken;
|
|
214
|
+
if (runToken?.aborted) {
|
|
215
|
+
return this.failedResult('the run was cancelled');
|
|
216
|
+
}
|
|
217
|
+
const controller = new AbortController();
|
|
218
|
+
let stop = () => undefined;
|
|
219
|
+
const stopped = new Promise(resolve => {
|
|
220
|
+
stop = (reason) => {
|
|
221
|
+
controller.abort(reason);
|
|
222
|
+
resolve(this.failedResult(reason));
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
const relayRunAbort = () => stop('the run was cancelled');
|
|
226
|
+
runToken?.addEventListener('abort', relayRunAbort, { once: true });
|
|
227
|
+
const timeoutMS = PayloadFeedbackManager.DECISION_TIMEOUT_MS;
|
|
228
|
+
const timer = setTimeout(() => stop(`timed out after ${timeoutMS} ms`), timeoutMS);
|
|
229
|
+
try {
|
|
230
|
+
const ask = this.decisionService.Ask({
|
|
231
|
+
State: this.buildDecisionState(questions, context),
|
|
232
|
+
Questions: this.buildDecisionQuestions(questions),
|
|
233
|
+
ContextUser: contextUser,
|
|
234
|
+
AgentID: context.AgentID,
|
|
235
|
+
PromptName: this.config.decisionPromptName,
|
|
236
|
+
CancellationToken: controller.signal
|
|
237
|
+
});
|
|
238
|
+
return await Promise.race([ask, stopped]);
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
return this.failedResult(error instanceof Error ? error.message : String(error));
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
clearTimeout(timer);
|
|
245
|
+
runToken?.removeEventListener('abort', relayRunAbort);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/** A decision result that answered nothing, and why. */
|
|
249
|
+
failedResult(errorMessage) {
|
|
250
|
+
return { success: false, errorMessage, Answers: {} };
|
|
251
|
+
}
|
|
252
|
+
/** One Likelihood per question: "Given the agent's stated reasoning, this change was intended: ...". */
|
|
253
|
+
buildDecisionQuestions(questions) {
|
|
254
|
+
const result = {};
|
|
255
|
+
questions.forEach((question, i) => {
|
|
256
|
+
result[this.questionKey(i)] = {
|
|
257
|
+
Kind: 'Likelihood',
|
|
258
|
+
Instructions: `Given the agent's stated reasoning, this change was intended: ${this.DescribeChange(question.warning)}`
|
|
259
|
+
};
|
|
260
|
+
});
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
/** The agent's reasoning and message, then the numbered changes. Never the payload. */
|
|
264
|
+
buildDecisionState(questions, context) {
|
|
265
|
+
const sections = [
|
|
266
|
+
this.contextSection("The agent's reasoning for this step", context.Reasoning),
|
|
267
|
+
this.contextSection("The agent's reasoning for the payload change", context.ChangeReasoning),
|
|
268
|
+
this.contextSection("The agent's message", context.Message)
|
|
269
|
+
].filter((section) => section !== undefined);
|
|
270
|
+
if (sections.length === 0) {
|
|
271
|
+
sections.push('The agent gave no reasoning or message for this step.');
|
|
272
|
+
}
|
|
273
|
+
const changes = questions.map((q, i) => `${i + 1}. ${this.DescribeChange(q.warning)}`);
|
|
274
|
+
sections.push(`The payload changes in question (paths, and sizes or types):\n${changes.join('\n')}`);
|
|
275
|
+
return sections.join('\n\n');
|
|
276
|
+
}
|
|
277
|
+
/** A labelled, capped block of the agent's text, or `undefined` when there is none. */
|
|
278
|
+
contextSection(label, text) {
|
|
279
|
+
const trimmed = typeof text === 'string' ? text.trim() : '';
|
|
280
|
+
return trimmed ? `${label}:\n${trimmed.slice(0, MAX_CONTEXT_TEXT)}` : undefined;
|
|
281
|
+
}
|
|
282
|
+
/** The key names a key-removal warning recorded, if any. */
|
|
283
|
+
removedKeyNames(warning) {
|
|
284
|
+
const details = warning.details;
|
|
285
|
+
if (typeof details !== 'object' || details === null || !('removedKeys' in details) || !Array.isArray(details.removedKeys)) {
|
|
286
|
+
return [];
|
|
287
|
+
}
|
|
288
|
+
const keys = details.removedKeys;
|
|
289
|
+
return keys.filter((key) => typeof key === 'string').slice(0, MAX_LISTED_KEYS);
|
|
290
|
+
}
|
|
291
|
+
/** Maps one answer to the feedback shape. Anything but a numeric Likelihood is accepted by default. */
|
|
292
|
+
mapAnswerToFeedback(question, answer) {
|
|
293
|
+
if (answer?.Kind !== 'Likelihood' || !Number.isFinite(answer.Probability)) {
|
|
294
|
+
return this.acceptedByDefault(question, 'no usable answer for this change');
|
|
295
|
+
}
|
|
296
|
+
const threshold = this.IntendedThreshold;
|
|
169
297
|
return {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
warningType: q.warning.type,
|
|
175
|
-
severity: q.warning.severity
|
|
176
|
-
})),
|
|
177
|
-
totalQuestions: questions.length,
|
|
178
|
-
timestamp: new Date().toISOString()
|
|
298
|
+
questionId: question.id,
|
|
299
|
+
intended: answer.Probability >= threshold,
|
|
300
|
+
probability: answer.Probability,
|
|
301
|
+
explanation: `Probability the change was intended: ${answer.Probability.toFixed(2)} (threshold ${threshold})`
|
|
179
302
|
};
|
|
180
303
|
}
|
|
304
|
+
/** Accepts the change, as it always was, and says why. */
|
|
305
|
+
acceptedByDefault(question, reason) {
|
|
306
|
+
return {
|
|
307
|
+
questionId: question.id,
|
|
308
|
+
intended: true,
|
|
309
|
+
explanation: `Accepted by default (${reason})`
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
/** The question key for position `index`. Keys are labels for code; the model reads the instructions. */
|
|
313
|
+
questionKey(index) {
|
|
314
|
+
return `change_${index + 1}`;
|
|
315
|
+
}
|
|
316
|
+
/** `maxQuestionsPerBatch`, or 10 when that is not a positive whole number. */
|
|
317
|
+
maxQuestionsPerCall() {
|
|
318
|
+
const max = this.config.maxQuestionsPerBatch;
|
|
319
|
+
return typeof max === 'number' && Number.isInteger(max) && max > 0 ? max : DEFAULT_MAX_QUESTIONS_PER_CALL;
|
|
320
|
+
}
|
|
181
321
|
/**
|
|
182
322
|
* Process feedback responses and determine final result
|
|
183
323
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PayloadFeedbackManager.js","sourceRoot":"","sources":["../src/PayloadFeedbackManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAY,MAAM,sBAAsB,CAAC;AAErE,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAgD9D;;GAEG;AACH,MAAM,OAAO,sBAAsB;IAG/B,YAAY,MAA8B;QACtC,IAAI,CAAC,MAAM,GAAG;YACV,oBAAoB,EAAE,EAAE;YACxB,WAAW,EAAE,GAAG;YAChB,GAAG,MAAM;SACZ,CAAC;IACN,CAAC;IAED;;OAEG;IACI,iBAAiB,CAAC,QAA0B;QAC/C,MAAM,SAAS,GAA8B,EAAE,CAAC;QAChD,MAAM,gBAAgB,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC;QAElE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC/C,MAAM,OAAO,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;YACpC,MAAM,QAAQ,GAAG,IAAI,CAAC,yBAAyB,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;YAC5D,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC7B,CAAC;QAED,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,iDAAiD;IAC1C,iBAAiB,CAAC,QAA0B;QAC/C,OAAO,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IAC5C,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,eAAe,CAAC,MAAM,GAAG,CAAC;SAC/C,CAAC;IACN,CAAC;IAED,+CAA+C;IACxC,eAAe,CAClB,SAAoC,EACpC,SAAoC;QAEpC,OAAO,IAAI,CAAC,eAAe,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;IACtD,CAAC;IAED;;OAEG;IACI,kBAAkB,CAAC,MAA6B;QACnD,IAAI,MAAM,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAChC,OAAO;QACX,CAAC;QAED,SAAS,CAAC,uCAAuC,CAAC,CAAC;QACnD,SAAS,CAAC,0BAA0B,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC;QACrE,SAAS,CAAC,0BAA0B,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC;QAErE,IAAI,MAAM,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpC,SAAS,CAAC,6BAA6B,CAAC,CAAC;YACzC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,eAAe,EAAE,CAAC;gBACxC,SAAS,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC;YACjC,CAAC;QACL,CAAC;QAED,IAAI,MAAM,CAAC,gBAAgB,EAAE,CAAC;YAC1B,SAAS,CAAC,4CAA4C,CAAC,CAAC;QAC5D,CAAC;IACL,CAAC;IAED,kDAAkD;IAC3C,kBAAkB,CAAC,MAA6B;QACnD,OAAO,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC3C,CAAC"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { UserInfo } from "@memberjunction/core";
|
|
2
2
|
import { MJAIAgentNoteEntity, MJAIAgentExampleEntity } from "@memberjunction/core-entities";
|
|
3
3
|
import { SecondaryScopeConfig, SecondaryScopeValue } from "@memberjunction/ai-core-plus";
|
|
4
|
+
import type { MJAIAgentRunStepEntityExtended } from "@memberjunction/ai-core-plus";
|
|
4
5
|
import { RerankerConfiguration } from "@memberjunction/ai-reranker";
|
|
5
6
|
/**
|
|
6
|
-
* Options for observability integration when retrieving notes
|
|
7
|
+
* Options for observability integration when retrieving notes or examples: the rerank step's run,
|
|
8
|
+
* parent and number.
|
|
7
9
|
*/
|
|
8
10
|
export interface NotesObservabilityOptions {
|
|
9
11
|
/**
|
|
@@ -18,6 +20,11 @@ export interface NotesObservabilityOptions {
|
|
|
18
20
|
* Step sequence number for the rerank step
|
|
19
21
|
*/
|
|
20
22
|
stepNumber?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Receives the rerank step as soon as it is created, so the agent run can count it. See
|
|
25
|
+
* `RerankObservabilityOptions.OnStepCreated`.
|
|
26
|
+
*/
|
|
27
|
+
OnStepCreated?: (step: MJAIAgentRunStepEntityExtended) => void;
|
|
21
28
|
}
|
|
22
29
|
/**
|
|
23
30
|
* Parameters for retrieving notes in a specific context
|
|
@@ -72,6 +79,15 @@ export interface GetExamplesParams {
|
|
|
72
79
|
* Defines per-dimension inheritance modes and validation rules.
|
|
73
80
|
*/
|
|
74
81
|
secondaryScopeConfig?: SecondaryScopeConfig | null;
|
|
82
|
+
/**
|
|
83
|
+
* Optional reranker configuration. Examples are reranked only when it is enabled and its
|
|
84
|
+
* `rerankExamples` flag is true.
|
|
85
|
+
*/
|
|
86
|
+
rerankerConfig?: RerankerConfiguration | null;
|
|
87
|
+
/**
|
|
88
|
+
* Optional observability context for tracing the examples rerank, as for notes.
|
|
89
|
+
*/
|
|
90
|
+
observability?: NotesObservabilityOptions;
|
|
75
91
|
}
|
|
76
92
|
/**
|
|
77
93
|
* Central service for retrieving and formatting notes/examples for injection into agent context.
|
|
@@ -95,7 +111,8 @@ export declare class AgentContextInjector {
|
|
|
95
111
|
* When reranking is enabled:
|
|
96
112
|
* 1. Fetch N * retrievalMultiplier candidates via vector search
|
|
97
113
|
* 2. Rerank candidates using configured reranker
|
|
98
|
-
* 3. Return top N reranked results
|
|
114
|
+
* 3. Return top N reranked results, or the top N vector search results when no candidate reached
|
|
115
|
+
* the rerank threshold
|
|
99
116
|
*
|
|
100
117
|
* Fallback behavior (controlled by config.fallbackOnError):
|
|
101
118
|
* - If true: On reranking failure, gracefully falls back to vector search results
|
|
@@ -103,9 +120,36 @@ export declare class AgentContextInjector {
|
|
|
103
120
|
*/
|
|
104
121
|
private getNotesViaSemanticSearch;
|
|
105
122
|
/**
|
|
106
|
-
* Get examples using semantic search via AIEngine
|
|
123
|
+
* Get examples using semantic search via AIEngine.
|
|
124
|
+
* Supports an optional reranking stage, off unless the reranker configuration sets `rerankExamples`.
|
|
125
|
+
*
|
|
126
|
+
* When example reranking is on:
|
|
127
|
+
* 1. Fetch N * retrievalMultiplier candidates via vector search
|
|
128
|
+
* 2. Rerank candidates with the same reranker and threshold as notes
|
|
129
|
+
* 3. Return top N reranked results, falling back as config.fallbackOnError says, or the top N
|
|
130
|
+
* vector search results when no candidate reached the rerank threshold
|
|
107
131
|
*/
|
|
108
132
|
private getExamplesViaSemanticSearch;
|
|
133
|
+
/**
|
|
134
|
+
* Logs a rerank that kept nothing. Its caller then keeps the vector search order, as if reranking
|
|
135
|
+
* were off: rerank scores need not be calibrated (a DecisionReranker's are uncalibrated
|
|
136
|
+
* probabilities), so a threshold that drops every candidate is not evidence that none is relevant.
|
|
137
|
+
* This is not a failure, so it does not depend on fallbackOnError.
|
|
138
|
+
*/
|
|
139
|
+
private logEmptyRerank;
|
|
140
|
+
/** The observability options a rerank runs under, from the caller's, or none. */
|
|
141
|
+
private rerankObservability;
|
|
142
|
+
/**
|
|
143
|
+
* The reranker configuration for examples: the agent's configuration when it is enabled and sets
|
|
144
|
+
* `rerankExamples` to true, otherwise null.
|
|
145
|
+
*/
|
|
146
|
+
private exampleRerankerConfig;
|
|
147
|
+
/**
|
|
148
|
+
* Stage 2 for examples: reranks the vector search candidates and returns the top N. On failure, falls
|
|
149
|
+
* back to the vector search results when config.fallbackOnError is true, and throws otherwise. When no
|
|
150
|
+
* candidate reaches the threshold, returns the top N vector search results.
|
|
151
|
+
*/
|
|
152
|
+
private rerankExamples;
|
|
109
153
|
/**
|
|
110
154
|
* Build a pre-filter callback for vector search that enforces secondary scope rules.
|
|
111
155
|
* Returns undefined when no scope params are provided (no filtering needed).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-context-injector.d.ts","sourceRoot":"","sources":["../src/agent-context-injector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAErE,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAwD,MAAM,+BAA+B,CAAC;AAElJ,OAAO,EAAE,oBAAoB,EAAsB,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAC7G,OAAO,EAAE,qBAAqB,EAAmB,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"agent-context-injector.d.ts","sourceRoot":"","sources":["../src/agent-context-injector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAErE,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAwD,MAAM,+BAA+B,CAAC;AAElJ,OAAO,EAAE,oBAAoB,EAAsB,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAC7G,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,8BAA8B,CAAC;AACnF,OAAO,EAAE,qBAAqB,EAAmB,MAAM,6BAA6B,CAAC;AAGrF;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACtC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,8BAA8B,KAAK,IAAI,CAAC;CAClE;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,MAA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;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAe/B;;OAEG;IACH,0BAA0B,CAAC,QAAQ,EAAE,sBAAsB,EAAE,GAAG,MAAM;IAyBtE;;OAEG;IACH,OAAO,CAAC,kBAAkB;CAwB7B"}
|
|
@@ -39,7 +39,8 @@ export class AgentContextInjector {
|
|
|
39
39
|
* When reranking is enabled:
|
|
40
40
|
* 1. Fetch N * retrievalMultiplier candidates via vector search
|
|
41
41
|
* 2. Rerank candidates using configured reranker
|
|
42
|
-
* 3. Return top N reranked results
|
|
42
|
+
* 3. Return top N reranked results, or the top N vector search results when no candidate reached
|
|
43
|
+
* the rerank threshold
|
|
43
44
|
*
|
|
44
45
|
* Fallback behavior (controlled by config.fallbackOnError):
|
|
45
46
|
* - If true: On reranking failure, gracefully falls back to vector search results
|
|
@@ -65,11 +66,11 @@ export class AgentContextInjector {
|
|
|
65
66
|
LogStatus(`AgentContextInjector: Reranking ${matches.length} candidates to top ${params.maxNotes}`);
|
|
66
67
|
const rerankerService = RerankerService.Instance;
|
|
67
68
|
try {
|
|
68
|
-
const rerankResult = await rerankerService.rerankNotes(matches, params.currentInput, config, params.contextUser, params.observability
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
69
|
+
const rerankResult = await rerankerService.rerankNotes(matches, params.currentInput, config, params.contextUser, this.rerankObservability(params.observability));
|
|
70
|
+
if (rerankResult.notes.length === 0) {
|
|
71
|
+
this.logEmptyRerank('note', config);
|
|
72
|
+
return matches.slice(0, params.maxNotes).map(m => m.note);
|
|
73
|
+
}
|
|
73
74
|
// Return top N reranked notes
|
|
74
75
|
const result = rerankResult.notes.slice(0, params.maxNotes).map(m => m.note);
|
|
75
76
|
LogStatus(`AgentContextInjector: Returning ${result.length} notes after reranking`);
|
|
@@ -89,14 +90,80 @@ export class AgentContextInjector {
|
|
|
89
90
|
}
|
|
90
91
|
}
|
|
91
92
|
/**
|
|
92
|
-
* Get examples using semantic search via AIEngine
|
|
93
|
+
* Get examples using semantic search via AIEngine.
|
|
94
|
+
* Supports an optional reranking stage, off unless the reranker configuration sets `rerankExamples`.
|
|
95
|
+
*
|
|
96
|
+
* When example reranking is on:
|
|
97
|
+
* 1. Fetch N * retrievalMultiplier candidates via vector search
|
|
98
|
+
* 2. Rerank candidates with the same reranker and threshold as notes
|
|
99
|
+
* 3. Return top N reranked results, falling back as config.fallbackOnError says, or the top N
|
|
100
|
+
* vector search results when no candidate reached the rerank threshold
|
|
93
101
|
*/
|
|
94
102
|
async getExamplesViaSemanticSearch(params) {
|
|
103
|
+
const config = this.exampleRerankerConfig(params);
|
|
104
|
+
// Calculate candidates to fetch (more if reranking examples)
|
|
105
|
+
const fetchCount = config
|
|
106
|
+
? params.maxExamples * config.retrievalMultiplier
|
|
107
|
+
: params.maxExamples;
|
|
95
108
|
// Build scope pre-filter so FindNearest only returns scope-valid candidates
|
|
96
109
|
const scopePreFilter = this.buildScopePreFilter(params, m => m.exampleEntity);
|
|
97
|
-
const matches = await AIEngine.Instance.FindSimilarAgentExamples(params.currentInput, params.agentId, params.userId, params.companyId,
|
|
98
|
-
|
|
99
|
-
|
|
110
|
+
const matches = await AIEngine.Instance.FindSimilarAgentExamples(params.currentInput, params.agentId, params.userId, params.companyId, fetchCount, 0.5, scopePreFilter);
|
|
111
|
+
if (!config) {
|
|
112
|
+
// Return entities directly from vector service (no database round-trip)
|
|
113
|
+
return matches.map(m => m.example);
|
|
114
|
+
}
|
|
115
|
+
return this.rerankExamples(matches, params, config);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Logs a rerank that kept nothing. Its caller then keeps the vector search order, as if reranking
|
|
119
|
+
* were off: rerank scores need not be calibrated (a DecisionReranker's are uncalibrated
|
|
120
|
+
* probabilities), so a threshold that drops every candidate is not evidence that none is relevant.
|
|
121
|
+
* This is not a failure, so it does not depend on fallbackOnError.
|
|
122
|
+
*/
|
|
123
|
+
logEmptyRerank(kind, config) {
|
|
124
|
+
LogStatus(`AgentContextInjector: No ${kind} reached the rerank threshold (${config.minRelevanceThreshold}), keeping the vector search results`);
|
|
125
|
+
}
|
|
126
|
+
/** The observability options a rerank runs under, from the caller's, or none. */
|
|
127
|
+
rerankObservability(observability) {
|
|
128
|
+
return observability ? {
|
|
129
|
+
agentRunID: observability.agentRunID,
|
|
130
|
+
parentStepID: observability.parentStepID,
|
|
131
|
+
stepNumber: observability.stepNumber,
|
|
132
|
+
OnStepCreated: observability.OnStepCreated
|
|
133
|
+
} : undefined;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The reranker configuration for examples: the agent's configuration when it is enabled and sets
|
|
137
|
+
* `rerankExamples` to true, otherwise null.
|
|
138
|
+
*/
|
|
139
|
+
exampleRerankerConfig(params) {
|
|
140
|
+
const config = params.rerankerConfig;
|
|
141
|
+
return config?.enabled && config.rerankExamples === true ? config : null;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Stage 2 for examples: reranks the vector search candidates and returns the top N. On failure, falls
|
|
145
|
+
* back to the vector search results when config.fallbackOnError is true, and throws otherwise. When no
|
|
146
|
+
* candidate reaches the threshold, returns the top N vector search results.
|
|
147
|
+
*/
|
|
148
|
+
async rerankExamples(matches, params, config) {
|
|
149
|
+
LogStatus(`AgentContextInjector: Reranking ${matches.length} example candidates to top ${params.maxExamples}`);
|
|
150
|
+
try {
|
|
151
|
+
const reranked = await RerankerService.Instance.RerankExamples(matches, params.currentInput, config, params.contextUser, this.rerankObservability(params.observability));
|
|
152
|
+
if (reranked.length === 0) {
|
|
153
|
+
this.logEmptyRerank('example', config);
|
|
154
|
+
return matches.slice(0, params.maxExamples).map(m => m.example);
|
|
155
|
+
}
|
|
156
|
+
return reranked.slice(0, params.maxExamples).map(m => m.example);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
160
|
+
if (config.fallbackOnError) {
|
|
161
|
+
LogStatus(`AgentContextInjector: Example reranking failed (${message}), falling back to vector search results`);
|
|
162
|
+
return matches.slice(0, params.maxExamples).map(m => m.example);
|
|
163
|
+
}
|
|
164
|
+
LogError(`AgentContextInjector: Example reranking failed and fallbackOnError is false: ${message}`);
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
100
167
|
}
|
|
101
168
|
/**
|
|
102
169
|
* Build a pre-filter callback for vector search that enforces secondary scope rules.
|