agentic-api 1.0.5 → 2.0.26
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 +118 -22
- package/dist/src/agents/agents.example.d.ts +3 -0
- package/dist/src/agents/agents.example.js +38 -0
- package/dist/src/agents/authentication.js +2 -0
- package/dist/src/agents/digestor.js +25 -16
- package/dist/src/agents/prompts.d.ts +2 -2
- package/dist/src/agents/prompts.js +112 -49
- package/dist/src/agents/reducer.core.d.ts +12 -0
- package/dist/src/agents/reducer.core.js +207 -0
- package/dist/src/agents/reducer.d.ts +3 -0
- package/dist/src/agents/reducer.example.d.ts +28 -0
- package/dist/src/agents/reducer.example.js +118 -0
- package/dist/src/agents/reducer.js +19 -0
- package/dist/src/agents/reducer.loaders.d.ts +34 -0
- package/dist/src/agents/reducer.loaders.js +122 -0
- package/dist/src/agents/reducer.process.d.ts +16 -0
- package/dist/src/agents/reducer.process.js +143 -0
- package/dist/src/agents/reducer.tools.d.ts +29 -0
- package/dist/src/agents/reducer.tools.js +157 -0
- package/dist/src/agents/reducer.types.d.ts +50 -0
- package/dist/src/agents/reducer.types.js +5 -0
- package/dist/src/agents/simulator.d.ts +47 -0
- package/dist/src/agents/simulator.executor.d.ts +26 -0
- package/dist/src/agents/simulator.executor.js +132 -0
- package/dist/src/agents/simulator.js +205 -0
- package/dist/src/agents/simulator.prompts.d.ts +16 -0
- package/dist/src/agents/simulator.prompts.js +108 -0
- package/dist/src/agents/simulator.types.d.ts +42 -0
- package/dist/src/agents/simulator.types.js +2 -0
- package/dist/src/agents/simulator.utils.d.ts +20 -0
- package/dist/src/agents/simulator.utils.js +87 -0
- package/dist/src/execute.d.ts +14 -7
- package/dist/src/execute.js +359 -84
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +14 -0
- package/dist/src/princing.openai.d.ts +9 -2
- package/dist/src/princing.openai.js +16 -11
- package/dist/src/prompts.d.ts +3 -2
- package/dist/src/prompts.js +207 -72
- package/dist/src/rag/embeddings.d.ts +103 -0
- package/dist/src/rag/embeddings.js +466 -0
- package/dist/src/rag/index.d.ts +12 -0
- package/dist/src/rag/index.js +40 -0
- package/dist/src/rag/lucene.d.ts +45 -0
- package/dist/src/rag/lucene.js +227 -0
- package/dist/src/rag/parser.d.ts +68 -0
- package/dist/src/rag/parser.js +192 -0
- package/dist/src/rag/tools.d.ts +76 -0
- package/dist/src/rag/tools.js +196 -0
- package/dist/src/rag/types.d.ts +178 -0
- package/dist/src/rag/types.js +21 -0
- package/dist/src/rag/usecase.d.ts +16 -0
- package/dist/src/rag/usecase.js +79 -0
- package/dist/src/rules/errors.d.ts +60 -0
- package/dist/src/rules/errors.js +97 -0
- package/dist/src/rules/git/git.e2e.helper.d.ts +104 -0
- package/dist/src/rules/git/git.e2e.helper.js +488 -0
- package/dist/src/rules/git/git.health.d.ts +66 -0
- package/dist/src/rules/git/git.health.js +354 -0
- package/dist/src/rules/git/git.helper.d.ts +129 -0
- package/dist/src/rules/git/git.helper.js +53 -0
- package/dist/src/rules/git/index.d.ts +6 -0
- package/dist/src/rules/git/index.js +76 -0
- package/dist/src/rules/git/repo.d.ts +128 -0
- package/dist/src/rules/git/repo.js +900 -0
- package/dist/src/rules/git/repo.pr.d.ts +137 -0
- package/dist/src/rules/git/repo.pr.js +589 -0
- package/dist/src/rules/git/repo.tools.d.ts +134 -0
- package/dist/src/rules/git/repo.tools.js +730 -0
- package/dist/src/rules/index.d.ts +8 -0
- package/dist/src/rules/index.js +25 -0
- package/dist/src/rules/messages.d.ts +17 -0
- package/dist/src/rules/messages.js +21 -0
- package/dist/src/rules/types.ctrl.d.ts +28 -0
- package/dist/src/rules/types.ctrl.js +2 -0
- package/dist/src/rules/types.d.ts +510 -0
- package/dist/src/rules/types.helpers.d.ts +132 -0
- package/dist/src/rules/types.helpers.js +2 -0
- package/dist/src/rules/types.js +33 -0
- package/dist/src/rules/user.mapper.d.ts +61 -0
- package/dist/src/rules/user.mapper.js +160 -0
- package/dist/src/rules/utils/slug.d.ts +22 -0
- package/dist/src/rules/utils/slug.js +35 -0
- package/dist/src/rules/utils.matter.d.ts +66 -0
- package/dist/src/rules/utils.matter.js +208 -0
- package/dist/src/rules/utils.slug.d.ts +22 -0
- package/dist/src/rules/utils.slug.js +35 -0
- package/dist/src/scrapper.d.ts +3 -2
- package/dist/src/scrapper.js +33 -37
- package/dist/src/stategraph/index.d.ts +8 -0
- package/dist/src/stategraph/index.js +21 -0
- package/dist/src/stategraph/stategraph.d.ts +91 -0
- package/dist/src/stategraph/stategraph.js +241 -0
- package/dist/src/stategraph/stategraph.storage.d.ts +41 -0
- package/dist/src/stategraph/stategraph.storage.js +166 -0
- package/dist/src/stategraph/types.d.ts +139 -0
- package/dist/src/stategraph/types.js +19 -0
- package/dist/src/types.d.ts +68 -39
- package/dist/src/types.js +53 -89
- package/dist/src/usecase.d.ts +4 -0
- package/dist/src/usecase.js +44 -0
- package/dist/src/utils.d.ts +12 -5
- package/dist/src/utils.js +30 -13
- package/package.json +9 -3
package/dist/src/execute.js
CHANGED
|
@@ -4,32 +4,92 @@ exports.modelConfig = modelConfig;
|
|
|
4
4
|
exports.sendFeedback = sendFeedback;
|
|
5
5
|
exports.readCompletionsStream = readCompletionsStream;
|
|
6
6
|
exports.executeAgentSet = executeAgentSet;
|
|
7
|
+
exports.executeAgent = executeAgent;
|
|
7
8
|
const princing_openai_1 = require("./princing.openai");
|
|
8
9
|
const types_1 = require("./types");
|
|
9
10
|
const utils_1 = require("./utils");
|
|
11
|
+
const stategraph_1 = require("./stategraph");
|
|
10
12
|
function modelConfig(model, custom) {
|
|
11
13
|
const defaultOptions = Object.assign({
|
|
12
|
-
max_completion_tokens: 15192,
|
|
13
14
|
stream_options: { "include_usage": true },
|
|
14
15
|
}, custom || {});
|
|
15
16
|
const mapping = {
|
|
17
|
+
"LOW-fast": {
|
|
18
|
+
temperature: 1,
|
|
19
|
+
frequency_penalty: 0.0,
|
|
20
|
+
presence_penalty: 0.0,
|
|
21
|
+
model: "gpt-5-nano",
|
|
22
|
+
reasoning_effort: "minimal",
|
|
23
|
+
verbosity: "low",
|
|
24
|
+
stream: true
|
|
25
|
+
},
|
|
16
26
|
"LOW": {
|
|
17
|
-
temperature:
|
|
27
|
+
temperature: 1,
|
|
28
|
+
frequency_penalty: 0.0,
|
|
29
|
+
presence_penalty: 0.0,
|
|
30
|
+
model: "gpt-5-nano",
|
|
31
|
+
reasoning_effort: "medium",
|
|
32
|
+
verbosity: "low",
|
|
33
|
+
stream: true
|
|
34
|
+
},
|
|
35
|
+
"MEDIUM-fast": {
|
|
36
|
+
temperature: 1,
|
|
18
37
|
frequency_penalty: 0.0,
|
|
19
38
|
presence_penalty: 0.0,
|
|
20
|
-
model: "gpt-
|
|
39
|
+
model: "gpt-5-mini",
|
|
40
|
+
reasoning_effort: "minimal",
|
|
41
|
+
verbosity: "low",
|
|
42
|
+
stream: true
|
|
21
43
|
},
|
|
22
|
-
"
|
|
23
|
-
temperature:
|
|
44
|
+
"LOW-4.1": {
|
|
45
|
+
temperature: .2,
|
|
46
|
+
frequency_penalty: 0.0,
|
|
47
|
+
presence_penalty: 0.0,
|
|
48
|
+
model: "gpt-4.1-nano",
|
|
49
|
+
stream: true
|
|
50
|
+
},
|
|
51
|
+
"MEDIUM-4.1-mini": {
|
|
52
|
+
temperature: .2,
|
|
24
53
|
frequency_penalty: 0.0,
|
|
25
54
|
presence_penalty: 0.0,
|
|
26
55
|
model: "gpt-4.1-mini",
|
|
56
|
+
stream: true
|
|
27
57
|
},
|
|
28
|
-
"
|
|
29
|
-
temperature:
|
|
58
|
+
"MEDIUM-4.1": {
|
|
59
|
+
temperature: .2,
|
|
60
|
+
frequency_penalty: 0.0,
|
|
61
|
+
presence_penalty: 0.0,
|
|
30
62
|
model: "gpt-4.1",
|
|
31
63
|
stream: true
|
|
32
64
|
},
|
|
65
|
+
"MEDIUM": {
|
|
66
|
+
temperature: 1,
|
|
67
|
+
frequency_penalty: 0.0,
|
|
68
|
+
presence_penalty: 0.0,
|
|
69
|
+
model: "gpt-5-mini",
|
|
70
|
+
reasoning_effort: "low",
|
|
71
|
+
verbosity: "low",
|
|
72
|
+
stream: true
|
|
73
|
+
},
|
|
74
|
+
"HIGH-fast": {
|
|
75
|
+
model: "gpt-5",
|
|
76
|
+
reasoning_effort: "minimal",
|
|
77
|
+
verbosity: "low",
|
|
78
|
+
temperature: 1,
|
|
79
|
+
stream: true
|
|
80
|
+
},
|
|
81
|
+
"HIGH-low": {
|
|
82
|
+
model: "gpt-5",
|
|
83
|
+
reasoning_effort: "low",
|
|
84
|
+
verbosity: "low",
|
|
85
|
+
stream: true
|
|
86
|
+
},
|
|
87
|
+
"HIGH-medium": {
|
|
88
|
+
model: "gpt-5",
|
|
89
|
+
reasoning_effort: "medium",
|
|
90
|
+
verbosity: "low",
|
|
91
|
+
stream: true
|
|
92
|
+
},
|
|
33
93
|
"SEARCH": {
|
|
34
94
|
temperature: 0.2,
|
|
35
95
|
frequency_penalty: 0.0,
|
|
@@ -51,14 +111,16 @@ function modelConfig(model, custom) {
|
|
|
51
111
|
return options;
|
|
52
112
|
}
|
|
53
113
|
function sendFeedback(params) {
|
|
54
|
-
const { agent, stdout, description, usage, state } = params;
|
|
114
|
+
const { agent, stdout, description, usage, state, verbose } = params;
|
|
55
115
|
const feedback = {
|
|
56
116
|
agent,
|
|
57
117
|
description,
|
|
58
118
|
usage,
|
|
59
119
|
state
|
|
60
120
|
};
|
|
61
|
-
//
|
|
121
|
+
// if(verbose) {
|
|
122
|
+
// console.log('--- DBG sendFeedback:',agent, description || '--', state);
|
|
123
|
+
// }
|
|
62
124
|
//
|
|
63
125
|
// send agent state and description
|
|
64
126
|
stdout.write(`\n<step>${JSON.stringify(feedback)}</step>\n`);
|
|
@@ -67,79 +129,124 @@ async function readCompletionsStream(params) {
|
|
|
67
129
|
const openai = (0, utils_1.openaiInstance)();
|
|
68
130
|
//
|
|
69
131
|
// set default context here
|
|
70
|
-
const {
|
|
71
|
-
const model =
|
|
132
|
+
const { stateGraph, discussion, agentConfig, agents, agentName, stdout, final, session, verbose } = params;
|
|
133
|
+
const model = agentConfig.model.model;
|
|
72
134
|
const accumulatedFunctionCall = final.choices[0]?.message.tool_calls || [];
|
|
73
135
|
const content = final.choices[0]?.message.content;
|
|
74
136
|
let thinking = false;
|
|
75
|
-
(0,
|
|
76
|
-
|
|
77
|
-
|
|
137
|
+
let localResult = (0, types_1.enrichExecutionResult)({
|
|
138
|
+
runId: `${agentName}-${Date.now()}`,
|
|
139
|
+
startQuery: '',
|
|
140
|
+
actions: [],
|
|
141
|
+
lastMessage: '',
|
|
142
|
+
usage: { prompt: 0, completion: 0, total: 0, cost: 0 },
|
|
143
|
+
moreThinkin: false,
|
|
144
|
+
});
|
|
145
|
+
// Accumulate cost in the discussion usage
|
|
146
|
+
(0, princing_openai_1.accumulateCost)(discussion.usage, model, final.usage);
|
|
147
|
+
stateGraph.updateTokens(agentName, {
|
|
148
|
+
prompt: final.usage?.prompt_tokens || 0,
|
|
149
|
+
completion: final.usage?.completion_tokens || 0,
|
|
150
|
+
total: final.usage?.total_tokens || 0,
|
|
151
|
+
cost: 0 // Cost already accumulated directly in discussion.usage
|
|
152
|
+
});
|
|
153
|
+
// Store state (implementation can be added later if needed)
|
|
154
|
+
// discussion.state = final.id;
|
|
78
155
|
if (content) {
|
|
79
|
-
|
|
80
|
-
|
|
156
|
+
if (verbose)
|
|
157
|
+
console.log("✅ Agent (1): 🌶️🌶️🌶️ save content:", content?.length);
|
|
158
|
+
stateGraph.push(agentName, { role: "assistant", content });
|
|
81
159
|
}
|
|
82
160
|
// Si le modèle décide d'appeler une fonction (par exemple "transferAgents")
|
|
83
161
|
for (const functionCall of accumulatedFunctionCall) {
|
|
84
162
|
const args = JSON.parse(functionCall?.function?.arguments || '{}');
|
|
85
163
|
if (args.justification) {
|
|
86
164
|
sendFeedback({
|
|
87
|
-
agent:
|
|
165
|
+
agent: agentConfig.name,
|
|
88
166
|
stdout,
|
|
89
167
|
description: args.justification,
|
|
90
|
-
usage:
|
|
91
|
-
state:
|
|
168
|
+
usage: discussion.usage,
|
|
169
|
+
state: '', // State will be set later if needed,
|
|
170
|
+
verbose
|
|
92
171
|
});
|
|
93
172
|
}
|
|
94
|
-
|
|
173
|
+
// Créer une référence mutable pour handleTransferCall
|
|
174
|
+
const currentAgentRef = { name: agentConfig.name };
|
|
175
|
+
const functionCallResult = await (0, utils_1.handleTransferCall)(discussion, currentAgentRef, agents, functionCall, session);
|
|
95
176
|
// result can be
|
|
96
177
|
// {content, usage} {did_transfer}
|
|
97
178
|
thinking = functionCallResult.thinking;
|
|
98
179
|
if (functionCallResult.usage) {
|
|
99
|
-
|
|
180
|
+
stateGraph.updateTokens(agentName, {
|
|
181
|
+
prompt: functionCallResult.usage.prompt || 0,
|
|
182
|
+
completion: functionCallResult.usage.completion || 0,
|
|
183
|
+
total: functionCallResult.usage.total || 0,
|
|
184
|
+
cost: functionCallResult.usage.cost || 0
|
|
185
|
+
});
|
|
100
186
|
}
|
|
101
187
|
if (functionCallResult.did_transfer) {
|
|
102
|
-
|
|
188
|
+
// record transfer action
|
|
189
|
+
localResult.actions.push({
|
|
190
|
+
action: 'transfert',
|
|
191
|
+
content: functionCallResult.destination_agent || currentAgentRef.name,
|
|
192
|
+
feedback: functionCallResult.feedback,
|
|
193
|
+
});
|
|
194
|
+
if (verbose)
|
|
195
|
+
console.log("✅ Agent transfer response:", functionCallResult.source_agent, ' call function ', functionCall.function.name, '::to', functionCallResult.destination_agent, ' transfer done ✅');
|
|
103
196
|
// Mise à jour du message système avec les nouvelles instructions du nouvel agent courant
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
|
|
197
|
+
const transferredAgent = agents.find(a => a.name === currentAgentRef.name) || agentConfig;
|
|
198
|
+
const instructions = transferredAgent.instructions;
|
|
199
|
+
const enrichedInstructions = await params.enrichWithMemory?.("system", transferredAgent, session);
|
|
200
|
+
stateGraph.set(agentName, instructions + '\n' + enrichedInstructions);
|
|
107
201
|
// Ajout de la réponse de l'appel de fonction dans le contexte conversationnel
|
|
108
|
-
const message = functionCallResult.content ?? `Le transfert vers l'agent "${
|
|
109
|
-
|
|
202
|
+
const message = functionCallResult.content ?? `Le transfert vers l'agent "${transferredAgent.name}" a été effectué, Tu dois répondre immédiatement à la question.`;
|
|
203
|
+
stateGraph.push(agentName, {
|
|
110
204
|
role: "assistant",
|
|
111
|
-
name: functionCallResult.name,
|
|
112
205
|
content: message,
|
|
206
|
+
name: functionCallResult.name
|
|
113
207
|
});
|
|
208
|
+
// Immediately surface the tool confirmation to the user output
|
|
209
|
+
// feedback or message?
|
|
210
|
+
if (message) {
|
|
211
|
+
stdout.write(message + "\n");
|
|
212
|
+
}
|
|
114
213
|
}
|
|
115
214
|
//
|
|
116
215
|
// other function call have a result
|
|
117
216
|
else if (functionCallResult.content) {
|
|
118
|
-
//
|
|
119
|
-
|
|
217
|
+
// record tool action
|
|
218
|
+
localResult.actions.push({
|
|
219
|
+
action: functionCall?.function?.name,
|
|
220
|
+
content: functionCallResult.content || '',
|
|
221
|
+
feedback: functionCallResult.feedback,
|
|
222
|
+
});
|
|
223
|
+
// console.log("✅ Agent tool response:",agentConfig.name,'::',functionCall.function.name, ' with content',functionCallResult.content);
|
|
224
|
+
stateGraph.push(agentName, {
|
|
120
225
|
role: "assistant",
|
|
121
|
-
name: functionCallResult.name,
|
|
122
226
|
content: functionCallResult.content,
|
|
227
|
+
name: functionCallResult.name
|
|
123
228
|
});
|
|
124
229
|
}
|
|
125
230
|
//
|
|
126
231
|
// send user feedback
|
|
127
232
|
if (functionCallResult.feedback) {
|
|
128
233
|
sendFeedback({
|
|
129
|
-
agent:
|
|
234
|
+
agent: agentConfig.name,
|
|
130
235
|
stdout,
|
|
131
236
|
description: functionCallResult.feedback,
|
|
132
|
-
usage:
|
|
133
|
-
state:
|
|
237
|
+
usage: discussion.usage,
|
|
238
|
+
state: '', // State tracking can be added later if needed
|
|
239
|
+
verbose
|
|
134
240
|
});
|
|
135
241
|
}
|
|
136
242
|
// Réactualisation de la liste des outils pour le nouvel agent courant
|
|
137
|
-
const
|
|
138
|
-
const
|
|
139
|
-
followUpOptions
|
|
243
|
+
const currentAgent = agents.find(a => a.name === currentAgentRef.name) || agentConfig;
|
|
244
|
+
const tools = currentAgent?.tools || [];
|
|
245
|
+
const followUpOptions = Object.assign({}, currentAgent?.model);
|
|
246
|
+
followUpOptions.messages = discussion.messages;
|
|
140
247
|
//
|
|
141
248
|
// always force HIGH model for follow up
|
|
142
|
-
followUpOptions.model =
|
|
249
|
+
followUpOptions.model = currentAgent.model.model;
|
|
143
250
|
if (tools.length > 0) {
|
|
144
251
|
followUpOptions.tools = tools;
|
|
145
252
|
followUpOptions.tool_choice = "auto";
|
|
@@ -161,28 +268,55 @@ async function readCompletionsStream(params) {
|
|
|
161
268
|
// console.log("✅ DEBUG followUpOptions (OUT tool_calls):",final.choices[0]?.message.tool_calls);
|
|
162
269
|
//
|
|
163
270
|
// when called a function, agent must continue the conversation
|
|
271
|
+
// if(verbose) console.log("✅ Agent ( followUp - OUT):",currentAgent.name, 'with tool_calls ',!!(final.choices[0]?.message.tool_calls),' and content:' ,!!(final.choices[0]?.message.content));
|
|
164
272
|
if (final.choices[0]?.message.tool_calls) {
|
|
165
|
-
|
|
166
|
-
|
|
273
|
+
const partial = await readCompletionsStream({
|
|
274
|
+
stateGraph,
|
|
275
|
+
discussion,
|
|
276
|
+
agentConfig: currentAgent,
|
|
277
|
+
agents,
|
|
278
|
+
agentName,
|
|
279
|
+
stdout,
|
|
280
|
+
final,
|
|
281
|
+
session,
|
|
282
|
+
verbose,
|
|
283
|
+
enrichWithMemory: params.enrichWithMemory
|
|
284
|
+
});
|
|
285
|
+
localResult = (0, types_1.executionResultMerge)(localResult, partial);
|
|
286
|
+
return localResult;
|
|
167
287
|
}
|
|
168
|
-
|
|
169
|
-
|
|
288
|
+
// Accumulate final cost
|
|
289
|
+
stateGraph.updateTokens(agentName, {
|
|
290
|
+
prompt: final.usage?.prompt_tokens || 0,
|
|
291
|
+
completion: final.usage?.completion_tokens || 0,
|
|
292
|
+
total: final.usage?.total_tokens || 0,
|
|
293
|
+
cost: 0 // Cost calculation handled internally
|
|
294
|
+
});
|
|
170
295
|
//
|
|
171
296
|
// send the cost
|
|
172
297
|
sendFeedback({
|
|
173
|
-
agent:
|
|
298
|
+
agent: currentAgent.name,
|
|
174
299
|
stdout,
|
|
175
300
|
description: '',
|
|
176
|
-
usage:
|
|
177
|
-
state:
|
|
301
|
+
usage: discussion.usage,
|
|
302
|
+
state: final.id || '',
|
|
303
|
+
verbose
|
|
178
304
|
});
|
|
179
305
|
const content = final.choices[0]?.message.content;
|
|
180
|
-
|
|
306
|
+
//
|
|
307
|
+
// capture new memory with the last message
|
|
308
|
+
await params.enrichWithMemory?.("assistant", currentAgent, session);
|
|
309
|
+
// if(verbose) console.log("✅ Agent (OUT):",currentAgent.name, 'with content length',!!content);
|
|
181
310
|
if (content) {
|
|
182
|
-
|
|
311
|
+
stateGraph.push(agentName, { role: "assistant", content });
|
|
312
|
+
}
|
|
313
|
+
if (content?.includes('<continue>')) {
|
|
314
|
+
localResult.moreThinkin = true;
|
|
315
|
+
return localResult;
|
|
183
316
|
}
|
|
184
|
-
return
|
|
317
|
+
return localResult;
|
|
185
318
|
}
|
|
319
|
+
return localResult;
|
|
186
320
|
}
|
|
187
321
|
/**
|
|
188
322
|
* Executes a set of agents to process a user query
|
|
@@ -195,49 +329,64 @@ async function readCompletionsStream(params) {
|
|
|
195
329
|
* @param {ExecuteAgentSetParams} params - Execution parameters
|
|
196
330
|
* @returns {Promise<void>}
|
|
197
331
|
*/
|
|
198
|
-
async function executeAgentSet(agentSet,
|
|
199
|
-
const { query,
|
|
332
|
+
async function executeAgentSet(agentSet, context, params) {
|
|
333
|
+
const { query, verbose } = params;
|
|
200
334
|
const openai = (0, utils_1.openaiInstance)();
|
|
201
335
|
const agents = (0, utils_1.injectTransferTools)(agentSet);
|
|
202
|
-
const
|
|
203
|
-
//
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
336
|
+
const home = params.home || agents[0].name;
|
|
337
|
+
// 🎯 Récupération du StateGraph depuis le context (qui contient session, user, credential, etc.)
|
|
338
|
+
const stateGraph = (0, stategraph_1.sessionStateGraphGet)(context);
|
|
339
|
+
// 📍 Agent spécialisé (persistant) vs Agent contextuel (temporaire)
|
|
340
|
+
const specializedAgent = home;
|
|
341
|
+
const discussion = stateGraph.createOrRestore(specializedAgent);
|
|
342
|
+
let currentAgent = (0, stategraph_1.getSpecializedAgent)(discussion) || specializedAgent;
|
|
343
|
+
// Référence mutable pour handleTransferCall
|
|
344
|
+
const currentAgentRef = { name: currentAgent };
|
|
345
|
+
// Trouver la config de l'agent courant
|
|
346
|
+
const currentAgentConfig = agents.find(a => a.name === currentAgent);
|
|
347
|
+
discussion.description = currentAgentConfig?.publicDescription;
|
|
348
|
+
if (!currentAgentConfig) {
|
|
349
|
+
throw new Error(`Agent ${currentAgent} not found`);
|
|
208
350
|
}
|
|
209
|
-
if (!
|
|
210
|
-
throw new Error(`Agent ${
|
|
351
|
+
if (!currentAgentConfig.instructions) {
|
|
352
|
+
throw new Error(`Agent ${currentAgent} has no instructions`);
|
|
211
353
|
}
|
|
212
|
-
//
|
|
213
|
-
|
|
214
|
-
if (!
|
|
215
|
-
|
|
354
|
+
// 🔧 Setup system message si pas encore fait
|
|
355
|
+
let enrichedQuery = query;
|
|
356
|
+
if (!discussion.messages.length) {
|
|
357
|
+
discussion.usage = { prompt: 0, completion: 0, total: 0, cost: 0 };
|
|
216
358
|
//
|
|
217
|
-
// add the initial agent to his memory
|
|
359
|
+
// add the initial agent to his memory as System
|
|
218
360
|
// Handle two-shot prompting: if instructions is an array, use the first part as a system message
|
|
219
|
-
const enrichedInstructions =
|
|
220
|
-
const instructions =
|
|
221
|
-
|
|
361
|
+
const enrichedInstructions = await params.enrichWithMemory?.("system", currentAgentConfig, context);
|
|
362
|
+
const instructions = currentAgentConfig.instructions + '\n' + enrichedInstructions;
|
|
363
|
+
stateGraph.set(specializedAgent, instructions);
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
// enrich the user query with memory as User
|
|
367
|
+
enrichedQuery = (await params.enrichWithMemory?.("user", currentAgentConfig, context)) || query;
|
|
222
368
|
}
|
|
223
369
|
// Append the user's query to the session-specific messages
|
|
224
370
|
// input: `${getMemoryString(relevantMemories)}\n${input}`,
|
|
225
|
-
|
|
371
|
+
stateGraph.push(specializedAgent, { role: "user", content: enrichedQuery });
|
|
226
372
|
// Les outils (définition des fonctions) disponibles par l'agent courant
|
|
227
|
-
const tools =
|
|
228
|
-
// console.log('--- DBG toolLogic (1)',
|
|
373
|
+
const tools = currentAgentConfig.tools;
|
|
374
|
+
// console.log('--- DBG toolLogic (1)',currentAgentConfig, currentAgentConfig?.toolLogic);
|
|
229
375
|
if (verbose) {
|
|
230
|
-
console.log('--- DBG current agent',
|
|
231
|
-
console.log('--- DBG memory len:', memory.messages.length);
|
|
232
|
-
console.log('--- DBG query:', query);
|
|
376
|
+
console.log('--- DBG current agent', currentAgentConfig.name, 'memory len:', discussion.messages.length);
|
|
233
377
|
}
|
|
234
378
|
// let shots = 1;
|
|
235
|
-
let
|
|
236
|
-
|
|
379
|
+
let result = (0, types_1.enrichExecutionResult)({
|
|
380
|
+
runId: `${specializedAgent}-${Date.now()}`,
|
|
381
|
+
startQuery: query,
|
|
382
|
+
actions: [],
|
|
383
|
+
lastMessage: '',
|
|
384
|
+
usage: { prompt: 0, completion: 0, total: 0, cost: 0 },
|
|
385
|
+
moreThinkin: false,
|
|
386
|
+
});
|
|
237
387
|
do {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
options.messages = memory.messages;
|
|
388
|
+
const options = Object.assign({}, currentAgentConfig?.model);
|
|
389
|
+
options.messages = discussion.messages;
|
|
241
390
|
if (tools.length > 0) {
|
|
242
391
|
options.tools = tools;
|
|
243
392
|
options.tool_choice = "auto";
|
|
@@ -259,21 +408,25 @@ async function executeAgentSet(agentSet, session, params) {
|
|
|
259
408
|
// state:memory.state!
|
|
260
409
|
// })
|
|
261
410
|
for await (const chunk of stream) {
|
|
262
|
-
if (verbose)
|
|
263
|
-
process.stdout.write(chunk.choices[0]?.delta?.content || "");
|
|
264
411
|
const delta = chunk.choices[0]?.delta;
|
|
265
412
|
if (delta?.content) {
|
|
266
413
|
params.stdout.write(delta?.content);
|
|
267
414
|
}
|
|
268
415
|
}
|
|
269
416
|
const final = await stream.finalChatCompletion();
|
|
270
|
-
|
|
271
|
-
|
|
417
|
+
const partial = await readCompletionsStream({
|
|
418
|
+
stateGraph,
|
|
419
|
+
discussion,
|
|
420
|
+
agentConfig: currentAgentConfig,
|
|
421
|
+
agents,
|
|
422
|
+
agentName: specializedAgent,
|
|
272
423
|
stdout: params.stdout,
|
|
273
|
-
session,
|
|
424
|
+
session: context,
|
|
274
425
|
final,
|
|
426
|
+
verbose,
|
|
275
427
|
enrichWithMemory: params.enrichWithMemory,
|
|
276
428
|
});
|
|
429
|
+
result = (0, types_1.executionResultMerge)(result, partial);
|
|
277
430
|
// Handle two-shot prompting: if instructions is an array, send the second part as a user message
|
|
278
431
|
// This allows for more complex agent behavior by providing additional context or instructions
|
|
279
432
|
// after the initial response, similar to chain-of-thought prompting
|
|
@@ -283,7 +436,129 @@ async function executeAgentSet(agentSet, session, params) {
|
|
|
283
436
|
// thinking = true;
|
|
284
437
|
// shots++;
|
|
285
438
|
// }
|
|
286
|
-
if (
|
|
439
|
+
if (result.moreThinkin)
|
|
287
440
|
console.log("🌶️🌶️🌶️ restart thinking:");
|
|
288
|
-
} while (
|
|
441
|
+
} while (result.moreThinkin);
|
|
442
|
+
// 💾 Auto-save du StateGraph à la fin (context contient session, user, credential, etc.)
|
|
443
|
+
(0, stategraph_1.sessionStateGraphSet)(context, stateGraph);
|
|
444
|
+
// finalize result (usage accumulated via discussion.usage once here)
|
|
445
|
+
result.lastMessage = discussion.messages?.[discussion.messages.length - 1]?.content || '';
|
|
446
|
+
// Ensure usage reflects the aggregated discussion usage (prompt/completion/total)
|
|
447
|
+
if (discussion?.usage) {
|
|
448
|
+
result.usage = {
|
|
449
|
+
prompt: discussion.usage.prompt || 0,
|
|
450
|
+
completion: discussion.usage.completion || 0,
|
|
451
|
+
total: discussion.usage.total || 0,
|
|
452
|
+
cost: discussion.usage.cost || 0,
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
result = (0, types_1.enrichExecutionResult)(result);
|
|
456
|
+
return result;
|
|
457
|
+
}
|
|
458
|
+
async function executeAgent(agentSet, params) {
|
|
459
|
+
const { query, verbose } = params;
|
|
460
|
+
const openai = (0, utils_1.openaiInstance)();
|
|
461
|
+
const agent = agentSet.find(a => a.name === params.home);
|
|
462
|
+
if (!agent) {
|
|
463
|
+
throw new Error(`Agent ${params.home} not found`);
|
|
464
|
+
}
|
|
465
|
+
if (!agent.instructions) {
|
|
466
|
+
throw new Error(`Agent ${agent.name} has no instructions`);
|
|
467
|
+
}
|
|
468
|
+
// Simple message array without memory manager - but preserve conversation
|
|
469
|
+
const messages = [
|
|
470
|
+
{ role: "system", content: agent.instructions },
|
|
471
|
+
{ role: "user", content: query }
|
|
472
|
+
];
|
|
473
|
+
let usage = { prompt: 0, completion: 0, total: 0, cost: 0 };
|
|
474
|
+
let state = '';
|
|
475
|
+
let maxIterations = 10; // Prevent infinite loops
|
|
476
|
+
let iterations = 0;
|
|
477
|
+
if (verbose) {
|
|
478
|
+
console.log('--- DBG executeAgent (simple):', agent.name);
|
|
479
|
+
console.log('--- DBG query:', `${query?.substring(0, 100)}...`);
|
|
480
|
+
}
|
|
481
|
+
// Execute the agent with tool call handling loop
|
|
482
|
+
while (iterations < maxIterations) {
|
|
483
|
+
iterations++;
|
|
484
|
+
const options = Object.assign({}, agent.model);
|
|
485
|
+
options.messages = messages;
|
|
486
|
+
const tools = agent.tools || [];
|
|
487
|
+
if (tools.length > 0) {
|
|
488
|
+
options.tools = tools;
|
|
489
|
+
options.tool_choice = "auto";
|
|
490
|
+
}
|
|
491
|
+
if (verbose) {
|
|
492
|
+
console.log('--- DBG executeAgent (simple):', agent.name, 'iterations:', iterations, '\n', messages.length, '\n---', messages[messages.length - 1]?.content);
|
|
493
|
+
}
|
|
494
|
+
const stream = await openai.beta.chat.completions.stream(options);
|
|
495
|
+
// Stream the response
|
|
496
|
+
for await (const chunk of stream) {
|
|
497
|
+
const delta = chunk.choices[0]?.delta;
|
|
498
|
+
if (delta?.content) {
|
|
499
|
+
params.stdout.write(delta?.content);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
const final = await stream.finalChatCompletion();
|
|
503
|
+
// Update usage and state
|
|
504
|
+
const model = agent.model?.model;
|
|
505
|
+
(0, princing_openai_1.accumulateCost)(usage, model, final.usage);
|
|
506
|
+
state = final.id;
|
|
507
|
+
// Add assistant response to messages
|
|
508
|
+
const content = final.choices[0]?.message.content;
|
|
509
|
+
if (content) {
|
|
510
|
+
messages.push({ role: "assistant", content });
|
|
511
|
+
}
|
|
512
|
+
// Handle tool calls if any
|
|
513
|
+
const toolCalls = final.choices[0]?.message.tool_calls;
|
|
514
|
+
let hasToolCalls = false;
|
|
515
|
+
if (toolCalls && toolCalls.length > 0) {
|
|
516
|
+
hasToolCalls = true;
|
|
517
|
+
// First, update the assistant message with tool_calls
|
|
518
|
+
const lastAssistant = messages[messages.length - 1];
|
|
519
|
+
if (lastAssistant && lastAssistant.role === 'assistant') {
|
|
520
|
+
lastAssistant.tool_calls = toolCalls;
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
// If no assistant message, add one with tool calls
|
|
524
|
+
messages.push({
|
|
525
|
+
role: "assistant",
|
|
526
|
+
content: content || null,
|
|
527
|
+
tool_calls: toolCalls
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
// Then execute tools and add tool responses
|
|
531
|
+
for (const toolCall of toolCalls) {
|
|
532
|
+
const args = JSON.parse(toolCall.function.arguments || '{}');
|
|
533
|
+
// Execute tool if it exists in agent's toolLogic
|
|
534
|
+
if (agent.toolLogic && agent.toolLogic[toolCall.function.name]) {
|
|
535
|
+
try {
|
|
536
|
+
const result = await agent.toolLogic[toolCall.function.name](args, { state });
|
|
537
|
+
messages.push({
|
|
538
|
+
role: "tool",
|
|
539
|
+
tool_call_id: toolCall.id,
|
|
540
|
+
content: typeof result === 'string' ? result : JSON.stringify(result)
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
catch (error) {
|
|
544
|
+
messages.push({
|
|
545
|
+
role: "tool",
|
|
546
|
+
tool_call_id: toolCall.id,
|
|
547
|
+
content: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
// If no tool calls, we're done
|
|
554
|
+
if (!hasToolCalls) {
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return {
|
|
559
|
+
usage,
|
|
560
|
+
content: messages[messages.length - 1]?.content || '',
|
|
561
|
+
messages,
|
|
562
|
+
state
|
|
563
|
+
};
|
|
289
564
|
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -4,8 +4,17 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export * from './utils';
|
|
6
6
|
export * from './types';
|
|
7
|
+
export * from './stategraph';
|
|
7
8
|
export * from './execute';
|
|
8
9
|
export * from './princing.openai';
|
|
9
10
|
export * from './scrapper';
|
|
11
|
+
export * from './agents/reducer';
|
|
10
12
|
export * from './agents/semantic';
|
|
11
13
|
export * from './agents/system';
|
|
14
|
+
export * from './rag';
|
|
15
|
+
export * from './usecase';
|
|
16
|
+
export * from './rules';
|
|
17
|
+
export * from './agents/simulator';
|
|
18
|
+
export * from './agents/simulator.types';
|
|
19
|
+
export * from './agents/simulator.prompts';
|
|
20
|
+
export * from './agents/simulator.utils';
|
package/dist/src/index.js
CHANGED
|
@@ -22,11 +22,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
22
22
|
__exportStar(require("./utils"), exports);
|
|
23
23
|
// Types
|
|
24
24
|
__exportStar(require("./types"), exports);
|
|
25
|
+
// StateGraph - nouvelle architecture de gestion des discussions
|
|
26
|
+
__exportStar(require("./stategraph"), exports);
|
|
25
27
|
// Execute
|
|
26
28
|
__exportStar(require("./execute"), exports);
|
|
27
29
|
__exportStar(require("./princing.openai"), exports);
|
|
28
30
|
// Scrapper
|
|
29
31
|
__exportStar(require("./scrapper"), exports);
|
|
30
32
|
// Agents
|
|
33
|
+
__exportStar(require("./agents/reducer"), exports);
|
|
31
34
|
__exportStar(require("./agents/semantic"), exports);
|
|
32
35
|
__exportStar(require("./agents/system"), exports);
|
|
36
|
+
// RAG Library
|
|
37
|
+
__exportStar(require("./rag"), exports);
|
|
38
|
+
// Usecase
|
|
39
|
+
__exportStar(require("./usecase"), exports);
|
|
40
|
+
// Rules API (test export)
|
|
41
|
+
__exportStar(require("./rules"), exports);
|
|
42
|
+
// Simulator
|
|
43
|
+
__exportStar(require("./agents/simulator"), exports);
|
|
44
|
+
__exportStar(require("./agents/simulator.types"), exports);
|
|
45
|
+
__exportStar(require("./agents/simulator.prompts"), exports);
|
|
46
|
+
__exportStar(require("./agents/simulator.utils"), exports);
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { CompletionUsage } from "openai/resources";
|
|
2
|
-
import {
|
|
2
|
+
import { Usage } from "./types";
|
|
3
|
+
type ModelPricing = {
|
|
4
|
+
input: number;
|
|
5
|
+
cachedInput?: number;
|
|
6
|
+
output: number;
|
|
7
|
+
};
|
|
8
|
+
export declare const modelPricing: Record<string, ModelPricing>;
|
|
3
9
|
export declare function calculateCost(model: string, usage?: CompletionUsage): number;
|
|
4
|
-
export declare function accumulateCost(
|
|
10
|
+
export declare function accumulateCost(currentUsage: Usage, model: string, usage?: CompletionUsage): number;
|
|
11
|
+
export {};
|