agentic-api 1.0.6 → 2.0.31
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 +336 -76
- 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/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 +13 -6
- package/dist/src/execute.js +351 -85
- 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 +15 -11
- package/dist/src/prompts.d.ts +3 -2
- package/dist/src/prompts.js +159 -19
- 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 +62 -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,35 +4,90 @@ 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,
|
|
18
28
|
frequency_penalty: 0.0,
|
|
19
29
|
presence_penalty: 0.0,
|
|
20
|
-
model: "gpt-
|
|
30
|
+
model: "gpt-5-nano",
|
|
31
|
+
reasoning_effort: "medium",
|
|
32
|
+
verbosity: "low",
|
|
33
|
+
stream: true
|
|
21
34
|
},
|
|
22
|
-
"MEDIUM": {
|
|
23
|
-
temperature:
|
|
35
|
+
"MEDIUM-fast": {
|
|
36
|
+
temperature: 1,
|
|
37
|
+
frequency_penalty: 0.0,
|
|
38
|
+
presence_penalty: 0.0,
|
|
39
|
+
model: "gpt-5-mini",
|
|
40
|
+
reasoning_effort: "minimal",
|
|
41
|
+
verbosity: "low",
|
|
42
|
+
stream: true
|
|
43
|
+
},
|
|
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
|
+
},
|
|
33
87
|
"HIGH-medium": {
|
|
34
|
-
model: "
|
|
88
|
+
model: "gpt-5",
|
|
35
89
|
reasoning_effort: "medium",
|
|
90
|
+
verbosity: "low",
|
|
36
91
|
stream: true
|
|
37
92
|
},
|
|
38
93
|
"SEARCH": {
|
|
@@ -56,14 +111,16 @@ function modelConfig(model, custom) {
|
|
|
56
111
|
return options;
|
|
57
112
|
}
|
|
58
113
|
function sendFeedback(params) {
|
|
59
|
-
const { agent, stdout, description, usage, state } = params;
|
|
114
|
+
const { agent, stdout, description, usage, state, verbose } = params;
|
|
60
115
|
const feedback = {
|
|
61
116
|
agent,
|
|
62
117
|
description,
|
|
63
118
|
usage,
|
|
64
119
|
state
|
|
65
120
|
};
|
|
66
|
-
//
|
|
121
|
+
// if(verbose) {
|
|
122
|
+
// console.log('--- DBG sendFeedback:',agent, description || '--', state);
|
|
123
|
+
// }
|
|
67
124
|
//
|
|
68
125
|
// send agent state and description
|
|
69
126
|
stdout.write(`\n<step>${JSON.stringify(feedback)}</step>\n`);
|
|
@@ -72,79 +129,124 @@ async function readCompletionsStream(params) {
|
|
|
72
129
|
const openai = (0, utils_1.openaiInstance)();
|
|
73
130
|
//
|
|
74
131
|
// set default context here
|
|
75
|
-
const {
|
|
76
|
-
const model =
|
|
132
|
+
const { stateGraph, discussion, agentConfig, agents, agentName, stdout, final, session, verbose } = params;
|
|
133
|
+
const model = agentConfig.model.model;
|
|
77
134
|
const accumulatedFunctionCall = final.choices[0]?.message.tool_calls || [];
|
|
78
135
|
const content = final.choices[0]?.message.content;
|
|
79
136
|
let thinking = false;
|
|
80
|
-
(0,
|
|
81
|
-
|
|
82
|
-
|
|
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;
|
|
83
155
|
if (content) {
|
|
84
|
-
|
|
85
|
-
|
|
156
|
+
if (verbose)
|
|
157
|
+
console.log("✅ Agent (1): 🌶️🌶️🌶️ save content:", content?.length);
|
|
158
|
+
stateGraph.push(agentName, { role: "assistant", content });
|
|
86
159
|
}
|
|
87
160
|
// Si le modèle décide d'appeler une fonction (par exemple "transferAgents")
|
|
88
161
|
for (const functionCall of accumulatedFunctionCall) {
|
|
89
162
|
const args = JSON.parse(functionCall?.function?.arguments || '{}');
|
|
90
163
|
if (args.justification) {
|
|
91
164
|
sendFeedback({
|
|
92
|
-
agent:
|
|
165
|
+
agent: agentConfig.name,
|
|
93
166
|
stdout,
|
|
94
167
|
description: args.justification,
|
|
95
|
-
usage:
|
|
96
|
-
state:
|
|
168
|
+
usage: discussion.usage,
|
|
169
|
+
state: '', // State will be set later if needed,
|
|
170
|
+
verbose
|
|
97
171
|
});
|
|
98
172
|
}
|
|
99
|
-
|
|
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);
|
|
100
176
|
// result can be
|
|
101
177
|
// {content, usage} {did_transfer}
|
|
102
178
|
thinking = functionCallResult.thinking;
|
|
103
179
|
if (functionCallResult.usage) {
|
|
104
|
-
|
|
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
|
+
});
|
|
105
186
|
}
|
|
106
187
|
if (functionCallResult.did_transfer) {
|
|
107
|
-
|
|
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 ✅');
|
|
108
196
|
// Mise à jour du message système avec les nouvelles instructions du nouvel agent courant
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
|
|
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);
|
|
112
201
|
// Ajout de la réponse de l'appel de fonction dans le contexte conversationnel
|
|
113
|
-
const message = functionCallResult.content ?? `Le transfert vers l'agent "${
|
|
114
|
-
|
|
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, {
|
|
115
204
|
role: "assistant",
|
|
116
|
-
name: functionCallResult.name,
|
|
117
205
|
content: message,
|
|
206
|
+
name: functionCallResult.name
|
|
118
207
|
});
|
|
208
|
+
// Immediately surface the tool confirmation to the user output
|
|
209
|
+
// feedback or message?
|
|
210
|
+
if (message) {
|
|
211
|
+
stdout.write(message + "\n");
|
|
212
|
+
}
|
|
119
213
|
}
|
|
120
214
|
//
|
|
121
215
|
// other function call have a result
|
|
122
216
|
else if (functionCallResult.content) {
|
|
123
|
-
//
|
|
124
|
-
|
|
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, {
|
|
125
225
|
role: "assistant",
|
|
126
|
-
name: functionCallResult.name,
|
|
127
226
|
content: functionCallResult.content,
|
|
227
|
+
name: functionCallResult.name
|
|
128
228
|
});
|
|
129
229
|
}
|
|
130
230
|
//
|
|
131
231
|
// send user feedback
|
|
132
232
|
if (functionCallResult.feedback) {
|
|
133
233
|
sendFeedback({
|
|
134
|
-
agent:
|
|
234
|
+
agent: agentConfig.name,
|
|
135
235
|
stdout,
|
|
136
236
|
description: functionCallResult.feedback,
|
|
137
|
-
usage:
|
|
138
|
-
state:
|
|
237
|
+
usage: discussion.usage,
|
|
238
|
+
state: '', // State tracking can be added later if needed
|
|
239
|
+
verbose
|
|
139
240
|
});
|
|
140
241
|
}
|
|
141
242
|
// Réactualisation de la liste des outils pour le nouvel agent courant
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
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;
|
|
145
247
|
//
|
|
146
248
|
// always force HIGH model for follow up
|
|
147
|
-
followUpOptions.model =
|
|
249
|
+
followUpOptions.model = currentAgent.model.model;
|
|
148
250
|
if (tools.length > 0) {
|
|
149
251
|
followUpOptions.tools = tools;
|
|
150
252
|
followUpOptions.tool_choice = "auto";
|
|
@@ -166,31 +268,55 @@ async function readCompletionsStream(params) {
|
|
|
166
268
|
// console.log("✅ DEBUG followUpOptions (OUT tool_calls):",final.choices[0]?.message.tool_calls);
|
|
167
269
|
//
|
|
168
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));
|
|
169
272
|
if (final.choices[0]?.message.tool_calls) {
|
|
170
|
-
|
|
171
|
-
|
|
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;
|
|
172
287
|
}
|
|
173
|
-
|
|
174
|
-
|
|
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
|
+
});
|
|
175
295
|
//
|
|
176
296
|
// send the cost
|
|
177
297
|
sendFeedback({
|
|
178
|
-
agent:
|
|
298
|
+
agent: currentAgent.name,
|
|
179
299
|
stdout,
|
|
180
300
|
description: '',
|
|
181
|
-
usage:
|
|
182
|
-
state:
|
|
301
|
+
usage: discussion.usage,
|
|
302
|
+
state: final.id || '',
|
|
303
|
+
verbose
|
|
183
304
|
});
|
|
184
305
|
const content = final.choices[0]?.message.content;
|
|
185
|
-
|
|
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);
|
|
186
310
|
if (content) {
|
|
187
|
-
|
|
311
|
+
stateGraph.push(agentName, { role: "assistant", content });
|
|
188
312
|
}
|
|
189
313
|
if (content?.includes('<continue>')) {
|
|
190
|
-
|
|
314
|
+
localResult.moreThinkin = true;
|
|
315
|
+
return localResult;
|
|
191
316
|
}
|
|
192
|
-
return
|
|
317
|
+
return localResult;
|
|
193
318
|
}
|
|
319
|
+
return localResult;
|
|
194
320
|
}
|
|
195
321
|
/**
|
|
196
322
|
* Executes a set of agents to process a user query
|
|
@@ -203,50 +329,64 @@ async function readCompletionsStream(params) {
|
|
|
203
329
|
* @param {ExecuteAgentSetParams} params - Execution parameters
|
|
204
330
|
* @returns {Promise<void>}
|
|
205
331
|
*/
|
|
206
|
-
async function executeAgentSet(agentSet,
|
|
332
|
+
async function executeAgentSet(agentSet, context, params) {
|
|
207
333
|
const { query, verbose } = params;
|
|
208
334
|
const openai = (0, utils_1.openaiInstance)();
|
|
209
335
|
const agents = (0, utils_1.injectTransferTools)(agentSet);
|
|
210
336
|
const home = params.home || agents[0].name;
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
//
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
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`);
|
|
217
350
|
}
|
|
218
|
-
if (!
|
|
219
|
-
throw new Error(`Agent ${
|
|
351
|
+
if (!currentAgentConfig.instructions) {
|
|
352
|
+
throw new Error(`Agent ${currentAgent} has no instructions`);
|
|
220
353
|
}
|
|
221
|
-
//
|
|
222
|
-
|
|
223
|
-
if (!
|
|
224
|
-
|
|
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 };
|
|
225
358
|
//
|
|
226
|
-
// add the initial agent to his memory
|
|
359
|
+
// add the initial agent to his memory as System
|
|
227
360
|
// Handle two-shot prompting: if instructions is an array, use the first part as a system message
|
|
228
|
-
const enrichedInstructions =
|
|
229
|
-
const instructions =
|
|
230
|
-
|
|
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;
|
|
231
368
|
}
|
|
232
369
|
// Append the user's query to the session-specific messages
|
|
233
370
|
// input: `${getMemoryString(relevantMemories)}\n${input}`,
|
|
234
|
-
|
|
371
|
+
stateGraph.push(specializedAgent, { role: "user", content: enrichedQuery });
|
|
235
372
|
// Les outils (définition des fonctions) disponibles par l'agent courant
|
|
236
|
-
const tools =
|
|
237
|
-
// console.log('--- DBG toolLogic (1)',
|
|
373
|
+
const tools = currentAgentConfig.tools;
|
|
374
|
+
// console.log('--- DBG toolLogic (1)',currentAgentConfig, currentAgentConfig?.toolLogic);
|
|
238
375
|
if (verbose) {
|
|
239
|
-
console.log('--- DBG current agent',
|
|
240
|
-
console.log('--- DBG memory len:', memory.messages.length);
|
|
241
|
-
console.log('--- DBG query:', query);
|
|
376
|
+
console.log('--- DBG current agent', currentAgentConfig.name, 'memory len:', discussion.messages.length);
|
|
242
377
|
}
|
|
243
378
|
// let shots = 1;
|
|
244
|
-
let
|
|
245
|
-
|
|
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
|
+
});
|
|
246
387
|
do {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
options.messages = memory.messages;
|
|
388
|
+
const options = Object.assign({}, currentAgentConfig?.model);
|
|
389
|
+
options.messages = discussion.messages;
|
|
250
390
|
if (tools.length > 0) {
|
|
251
391
|
options.tools = tools;
|
|
252
392
|
options.tool_choice = "auto";
|
|
@@ -268,21 +408,25 @@ async function executeAgentSet(agentSet, session, params) {
|
|
|
268
408
|
// state:memory.state!
|
|
269
409
|
// })
|
|
270
410
|
for await (const chunk of stream) {
|
|
271
|
-
if (verbose)
|
|
272
|
-
process.stdout.write(chunk.choices[0]?.delta?.content || "");
|
|
273
411
|
const delta = chunk.choices[0]?.delta;
|
|
274
412
|
if (delta?.content) {
|
|
275
413
|
params.stdout.write(delta?.content);
|
|
276
414
|
}
|
|
277
415
|
}
|
|
278
416
|
const final = await stream.finalChatCompletion();
|
|
279
|
-
|
|
280
|
-
|
|
417
|
+
const partial = await readCompletionsStream({
|
|
418
|
+
stateGraph,
|
|
419
|
+
discussion,
|
|
420
|
+
agentConfig: currentAgentConfig,
|
|
421
|
+
agents,
|
|
422
|
+
agentName: specializedAgent,
|
|
281
423
|
stdout: params.stdout,
|
|
282
|
-
session,
|
|
424
|
+
session: context,
|
|
283
425
|
final,
|
|
426
|
+
verbose,
|
|
284
427
|
enrichWithMemory: params.enrichWithMemory,
|
|
285
428
|
});
|
|
429
|
+
result = (0, types_1.executionResultMerge)(result, partial);
|
|
286
430
|
// Handle two-shot prompting: if instructions is an array, send the second part as a user message
|
|
287
431
|
// This allows for more complex agent behavior by providing additional context or instructions
|
|
288
432
|
// after the initial response, similar to chain-of-thought prompting
|
|
@@ -292,7 +436,129 @@ async function executeAgentSet(agentSet, session, params) {
|
|
|
292
436
|
// thinking = true;
|
|
293
437
|
// shots++;
|
|
294
438
|
// }
|
|
295
|
-
if (
|
|
439
|
+
if (result.moreThinkin)
|
|
296
440
|
console.log("🌶️🌶️🌶️ restart thinking:");
|
|
297
|
-
} 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
|
+
};
|
|
298
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 {};
|