Skip to content

Commit 67a0b04

Browse files
improved flow execution with conversation history
1 parent 191c1d3 commit 67a0b04

File tree

11 files changed

+141
-67
lines changed

11 files changed

+141
-67
lines changed

app/base/ai/Actions/Orchestrator.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,17 @@ public function registerTool(string $name, callable $handler) : void
4444
/**
4545
* Run a flow
4646
*/
47-
public function runFlow(string $userPrompt) : array
47+
public function runFlow(string $userPrompt, array &$history = []) : array
4848
{
49-
$payload = $this->llm->buildFlowInitialRequest($this->flow, $userPrompt);
49+
$payload = $this->llm->buildFlowInitialRequest($this->flow, $userPrompt, $history);
5050

5151
// extract messages for history tracking
52-
$messages = $payload['messages'] ?? $payload['contents'] ?? [];
52+
$initialMessages = $payload['messages'] ?? $payload['contents'] ?? [];
53+
54+
$messages = [];
55+
foreach ($initialMessages as $msg) {
56+
$messages[] = $msg;
57+
}
5358

5459
$response = $this->llm->sendRaw($payload);
5560

@@ -97,6 +102,26 @@ public function runFlow(string $userPrompt) : array
97102
$normalized = $this->llm->normalizeCompletionsResponse($response);
98103
}
99104

105+
// as user prompt is already in initialMessages and is probably not reported into history, add it.
106+
$history[] = $this->llm->formatUserMessage($userPrompt);
107+
108+
// save flow messages to history, we can skip technical messages and previous history
109+
$history = array_merge($history, array_slice($this->filterTechMessages($messages), count($initialMessages)));
110+
111+
// add assistant final message to history
112+
$history[] = $this->llm->formatAssistantMessage($normalized['assistantText'] ?? '');
113+
100114
return $normalized;
101115
}
116+
117+
protected function filterTechMessages(array $messages) : array
118+
{
119+
return array_filter($messages, function ($msg) {
120+
$msg = json_decode(json_encode($msg), true); // force array
121+
if ($msg['role'] == 'model' && isset($msg['parts'][0]['functionCall'])) {
122+
return false;
123+
}
124+
return !in_array($msg['role'] ?? '', ['system', 'tool']);
125+
});
126+
}
102127
}

app/base/ai/Models/ChatGPT.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ public function normalizeCompletionsResponse(array $raw): array
153153
];
154154
}
155155

156-
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?string $model = null): array
156+
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, array &$history = [], ?string $model = null): array
157157
{
158-
$messages = [
159-
[
160-
'role' => 'system',
161-
'content' => $flow->systemPrompt()
162-
],
158+
$messages = [];
159+
160+
$messages[] = [
161+
'role' => 'system',
162+
'content' => $flow->systemPrompt()
163163
];
164164

165165
if ($flow->schema()) {
@@ -169,6 +169,11 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
169169
];
170170
}
171171

172+
// add previous history
173+
foreach ($history as $msg) {
174+
$messages[] = $msg;
175+
}
176+
172177
$messages[] = $this->formatUserMessage($userPrompt);
173178

174179
$tools = [];
@@ -183,16 +188,16 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
183188
}
184189

185190
return [
186-
'model' => $this->getDefaultModel(),
191+
'model' => $this->getModel($model),
187192
'tools' => $tools,
188193
'messages' => array_values($messages),
189194
];
190195
}
191196

192-
public function sendFunctionResponse(string $name, array $result, ?array $tools = null, array &$history = [], ?string $id = null): array
197+
public function sendFunctionResponse(string $name, array $result, ?array $tools = null, array &$history = [], ?string $model = null, ?string $id = null): array
193198
{
194199
return $this->sendRaw([
195-
'model' => $this->getDefaultModel(),
200+
'model' => $this->getModel($model),
196201
'messages' => [
197202
[
198203
'role' => 'tool',

app/base/ai/Models/Claude.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ public function normalizeCompletionsResponse(array $raw): array
151151
];
152152
}
153153

154-
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?string $model = null): array
154+
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, array &$history = [], ?string $model = null): array
155155
{
156-
$messages = [
157-
[
158-
'role' => 'system',
159-
'content' => $flow->systemPrompt()
160-
],
156+
$messages = [];
157+
158+
$messages[] = [
159+
'role' => 'system',
160+
'content' => $flow->systemPrompt()
161161
];
162162

163163
if ($flow->schema()) {
@@ -167,6 +167,11 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
167167
];
168168
}
169169

170+
// add previous history
171+
foreach ($history as $msg) {
172+
$messages[] = $msg;
173+
}
174+
170175
$messages[] = $this->formatUserMessage($userPrompt);
171176

172177
$tools = [];
@@ -181,16 +186,16 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
181186
}
182187

183188
return [
184-
'model' => $this->getDefaultModel(),
189+
'model' => $this->getModel($model),
185190
'tools' => $tools,
186191
'messages' => array_values($messages),
187192
];
188193
}
189194

190-
public function sendFunctionResponse(string $functionName, array $result, ?array $tools = null, array &$history = [], ?string $id = null): array
195+
public function sendFunctionResponse(string $functionName, array $result, ?array $tools = null, array &$history = [], ?string $model = null, ?string $id = null): array
191196
{
192197
return $this->sendRaw([
193-
'model' => $this->getDefaultModel(),
198+
'model' => $this->getModel($model),
194199
'messages' => [
195200
[
196201
'role' => 'assistant',

app/base/ai/Models/GoogleGemini.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ public function formatUserMessage(string $prompt): array
8787
public function formatAssistantMessage(mixed $message, ?string $messageType = null): array
8888
{
8989
return [
90-
'role' => 'assistant',
91-
$messageType ?? 'contents' => $message
90+
'role' => 'model',
91+
'parts' => [
92+
[$messageType ?? 'text' => $message]
93+
]
9294
];
9395
}
9496

@@ -171,17 +173,22 @@ public function normalizeCompletionsResponse(array $raw): array
171173
];
172174
}
173175

174-
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?string $model = null): array
176+
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, array &$history = [], ?string $model = null): array
175177
{
176-
$messages = [
177-
(object) $this->formatUserMessage($flow->systemPrompt()),
178-
];
178+
$messages = [];
179+
180+
$messages[] = (object) $this->formatUserMessage($flow->systemPrompt());
179181

180182
// googlegemini does not support system messages after the first one, add schema as user message
181183
if ($flow->schema()) {
182184
$messages[] = (object) $this->formatUserMessage("Ecco il tuo schema GraphQL:\n" . $flow->schema());
183185
}
184186

187+
// add previous history
188+
foreach ($history as $msg) {
189+
$messages[] = (object) $msg;
190+
}
191+
185192
$messages[] = (object) $this->formatUserMessage($userPrompt);
186193

187194
// Costruiamo le dichiarazioni dei tool
@@ -202,7 +209,7 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
202209
];
203210
}
204211

205-
public function sendFunctionResponse(string $name, array $result, ?array $tools = null, array &$history = [], ?string $id = null): array
212+
public function sendFunctionResponse(string $name, array $result, ?array $tools = null, array &$history = [], ?string $model = null, ?string $id = null): array
206213
{
207214
$history[] = [
208215
'role' => 'tool',

app/base/ai/Models/Groq.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ public function normalizeCompletionsResponse(array $raw): array
134134
];
135135
}
136136

137-
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?string $model = null): array
137+
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, array &$history = [], ?string $model = null): array
138138
{
139-
$messages = [
140-
[
141-
'role' => 'system',
142-
'content' => $flow->systemPrompt()
143-
],
139+
$messages = [];
140+
141+
$messages[] = [
142+
'role' => 'system',
143+
'content' => $flow->systemPrompt()
144144
];
145145

146146
if ($flow->schema()) {
@@ -150,25 +150,27 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
150150
];
151151
}
152152

153-
$messages[] = [
154-
'role' => 'assistant',
155-
'content' => json_encode($flow->tools())
156-
];
153+
$messages[] = $this->formatAssistantMessage(json_encode($flow->tools()));
154+
155+
// add previous history
156+
foreach ($history as $msg) {
157+
$messages[] = $msg;
158+
}
157159

158160
$messages[] = $this->formatUserMessage($userPrompt);
159161

160162
return [
161-
'model' => $this->getDefaultModel(),
163+
'model' => $this->getModel($model),
162164
'messages' => array_values($messages),
163165
];
164166
}
165167

166-
public function sendFunctionResponse(string $functionName, array $result, ?array $tools = null, array &$history = [], ?string $id = null): array
168+
public function sendFunctionResponse(string $functionName, array $result, ?array $tools = null, array &$history = [], ?string $model = null, ?string $id = null): array
167169
{
168170
$history[] = $this->formatUserMessage("Tool response for call to function $functionName".(!is_null($id)?" (id: $id)":"").": " . json_encode($result));
169171

170172
return $this->sendRaw([
171-
'model' => $this->getDefaultModel(),
173+
'model' => $this->getModel($model),
172174
'messages' => $history
173175
]);
174176
}

app/base/ai/Models/Mistral.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ public function normalizeCompletionsResponse(array $raw): array
139139
];
140140
}
141141

142-
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?string $model = null): array
142+
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, array &$history = [], ?string $model = null): array
143143
{
144-
$messages = [
145-
[
146-
'role' => 'system',
147-
'content' => $flow->systemPrompt()
148-
],
144+
$messages = [];
145+
146+
$messages[] = [
147+
'role' => 'system',
148+
'content' => $flow->systemPrompt()
149149
];
150150

151151
if ($flow->schema()) {
@@ -155,6 +155,11 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
155155
];
156156
}
157157

158+
// add previous history
159+
foreach ($history as $msg) {
160+
$messages[] = $msg;
161+
}
162+
158163
$messages[] = $this->formatUserMessage($userPrompt);
159164

160165
$tools = [];
@@ -171,19 +176,19 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
171176
}
172177

173178
return [
174-
'model' => $this->getDefaultModel(),
179+
'model' => $this->getModel($model),
175180
'tools' => $tools,
176181
'messages' => array_values($messages),
177182
];
178183
}
179184

180-
public function sendFunctionResponse(string $functionName, array $result, ?array $tools = null, array &$history = [], ?string $id = null): array
185+
public function sendFunctionResponse(string $functionName, array $result, ?array $tools = null, array &$history = [], ?string $model = null, ?string $id = null): array
181186
{
182187

183188
$history[] = $this->formatUserMessage("Tool response for call to function $functionName".(!is_null($id)?" (id: $id)":"").": " . json_encode($result));
184189

185190
return $this->sendRaw([
186-
'model' => $this->getDefaultModel(),
191+
'model' => $this->getModel($model),
187192
'messages' => $history
188193
]);
189194
}

app/base/ai/Models/Perplexity.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ public function normalizeCompletionsResponse(array $raw): array
112112
];
113113
}
114114

115-
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?string $model = null): array
115+
public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, array &$history = [], ?string $model = null): array
116116
{
117-
$messages = [
118-
[
119-
'role' => 'system',
120-
'content' => $flow->systemPrompt()
121-
],
117+
$messages = [];
118+
119+
$messages[] = [
120+
'role' => 'system',
121+
'content' => $flow->systemPrompt()
122122
];
123123

124124
if ($flow->schema()) {
@@ -128,22 +128,27 @@ public function buildFlowInitialRequest(BaseFlow $flow, string $userPrompt, ?str
128128
];
129129
}
130130

131-
$messages[] = [
132-
'role' => 'assistant',
133-
'content' => json_encode($flow->tools())
134-
];
131+
$messages[] = $this->formatAssistantMessage(json_encode($flow->tools()));
132+
133+
// add previous history
134+
foreach ($history as $msg) {
135+
$messages[] = $msg;
136+
}
135137

136138
$messages[] = $this->formatUserMessage($userPrompt);
137139

138-
return array_values($messages);
140+
return [
141+
'model' => $this->getModel($model),
142+
'messages' => array_values($messages),
143+
];
139144
}
140145

141-
public function sendFunctionResponse(string $functionName, array $result, ?array $tools = null, array &$history = [], ?string $id = null): array
146+
public function sendFunctionResponse(string $functionName, array $result, ?array $tools = null, array &$history = [], ?string $model = null, ?string $id = null): array
142147
{
143148
$history[] = $this->formatUserMessage("Tool response for call to function $functionName".(!is_null($id)?" (id: $id)":"").": " . json_encode($result));
144149

145150
return $this->sendRaw([
146-
'model' => $this->getDefaultModel(),
151+
'model' => $this->getModel($model),
147152
'messages' => $history
148153
]);
149154
}

app/base/blocks/AIChatBlock.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,16 @@ function sendMessage() {
181181
const text = $(\'#ai-chat-text\').val().trim();
182182
if (!text) return;
183183
184+
const first_interaction = $(\'#ai-chat-messages .ai-chat-msg\').length === 0;
185+
184186
$(\'#ai-chat-text\').val(\'\');
185187
appendMessage("user", text);
186188
187189
$(\'#ai-chat-loading\').show();
188190
189191
$.ajax({
190192
method: "POST",
191-
url: "/commerce/chatbot/chat?ai='.$aiModel.'",
193+
url: "/commerce/chatbot/chat?ai='.$aiModel.'" + (!first_interaction ? "" : "&reset_history=1"),
192194
contentType: "application/json",
193195
data: JSON.stringify({ prompt: text }),
194196
success: function(res) {

0 commit comments

Comments
 (0)