generated from susom/redcap-em-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureChatAI.php
More file actions
1519 lines (1304 loc) · 58.9 KB
/
SecureChatAI.php
File metadata and controls
1519 lines (1304 loc) · 58.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Stanford\SecureChatAI;
require_once "emLoggerTrait.php";
require_once "classes/SecureChatLog.php";
require_once "classes/Models/ModelInterface.php";
require_once "classes/Models/BaseModelRequest.php";
require_once "classes/Models/GPTModelRequest.php";
require_once "classes/Models/WhisperModelRequest.php";
require_once "classes/Models/GeminiModelRequest.php";
require_once "classes/Models/ClaudeModelRequest.php";
require_once "classes/Models/GenericModelRequest.php";
require_once "classes/Models/GPT4oMiniTTSModelRequest.php";
require_once __DIR__ . '/vendor/autoload.php';
use Google\Exception;
use GuzzleHttp\Client;
use Yethee\Tiktoken\EncoderProvider;
class SecureChatAI extends \ExternalModules\AbstractExternalModule
{
use emLoggerTrait;
private array $defaultParams;
private $guzzleClient = null;
private $guzzleTimeout = 5.0;
private array $modelConfig = [];
public function __construct()
{
parent::__construct();
}
private function initSecureChatAI()
{
// Set default LLM model parameters
$this->defaultParams = [
'temperature' => (float)$this->getSystemSetting('gpt-temperature') ?: 0.7,
'top_p' => (float)$this->getSystemSetting('gpt-top-p') ?: 0.9,
'frequency_penalty' => (float)$this->getSystemSetting('gpt-frequency-penalty') ?: 0.5,
'presence_penalty' => (float)$this->getSystemSetting('gpt-presence-penalty') ?: 0,
'max_tokens' => (int)$this->getSystemSetting('gpt-max-tokens') ?: 16384,
'reasoning_effort' => $this->getSystemSetting('reasoning-effort'),
'stop' => null,
'model' => 'gpt-4o'
];
// Initialize the model configurations from system settings
$apiSettings = $this->framework->getSubSettings('api-settings');
foreach ($apiSettings as $setting) {
$modelAlias = $setting['model-alias'];
$modelID = $setting['model-id'];
$this->modelConfig[$modelAlias] = [
'api_url' => $setting['api-url'],
'api_token' => $setting['api-token'],
'api_key_var' => $setting['api-key-var'],
'required' => $setting['api-input-var'],
'model_id' => $modelID
];
if (isset($setting['default-model']) && $setting['default-model']) {
$this->defaultParams['model'] = $modelAlias;
}
}
// Set Guzzle info
$timeout = $this->getSystemSetting('guzzle-timeout') ? (float)(strip_tags($this->getSystemSetting('guzzle-timeout'))) : $this->getGuzzleTimeout();
$this->setGuzzleTimeout($timeout);
$this->guzzleClient = $this->getGuzzleClient();
}
public function getSecureChatLogs($offset)
{
$offset = intval($offset);
return SecureChatLog::getAllLogs($this, $offset);
}
/**
* Get logs for a specific session ID
* @param string $session_id
* @param int|null $project_id Optional project filter
* @return array
*/
public function getSecureChatLogsBySession($session_id, $project_id = null)
{
return SecureChatLog::getLogsBySession($this, $session_id, $project_id);
}
private function filterDefaultParamsForModel($model, $params)
{
// Embedding models don't use chat defaultParams
if ($model === 'ada-002' || $model === 'text-embedding-3-small') {
return array_merge([
'model' => $this->modelConfig[$model]['model_id'] ?? $model
], $params);
}
$merged = array_merge($this->defaultParams, $params);
// Only o1/o3-mini/gpt-5 get reasoning params
if (!in_array($model, ['o1', 'o3-mini', 'gpt-5'])) {
unset($merged['reasoning']);
unset($merged['reasoning_effort']);
}
// Only models supporting json_schema
$schemaModels = ['gpt-4.1', 'o1', 'o3-mini', 'gpt-5', 'llama3370b'];
if (!in_array($model, $schemaModels)) {
unset($merged['json_schema']);
}
// Only o1/o3-mini/gpt-5 have strict param set (use max_completion_tokens)
if (in_array($model, ['o1', 'o3-mini', 'gpt-5'])) {
$strict = [
'model' => $model,
'messages' => $merged['messages'] ?? [],
'max_completion_tokens' => $merged['max_completion_tokens'] ?? ($merged['max_tokens'] ?? 32000),
];
if (isset($merged['reasoning_effort'])) {
$strict['reasoning_effort'] = $merged['reasoning_effort'];
}
// Preserve json_schema for o1/o3-mini/gpt-5
if (isset($merged['json_schema'])) {
$strict['json_schema'] = $merged['json_schema'];
}
return $strict;
}
// Remove max_tokens for all non-o1/o3-mini
unset($merged['max_tokens']);
return $merged;
}
public function callAI($model, $params = [], $project_id = null)
{
$startTotal = microtime(true);
$retries = 2;
$attempt = 0;
while ($attempt <= $retries) {
try {
$startAttempt = microtime(true);
// --- Agent Mode Gate ---
$agent_mode_requested = !empty($params['agent_mode']);
$agent_mode_enabled = (bool) $this->getSystemSetting('enable_agent_mode');
$response = null;
if ($agent_mode_requested && $agent_mode_enabled) {
$startAgentLoop = microtime(true);
$response = $this->runAgentLoop(
model: $model,
params: $params,
project_id: $project_id
);
$this->emDebug("callAI timing - runAgentLoop", [
'duration_ms' => round((microtime(true) - $startAgentLoop) * 1000, 2)
]);
} else {
if($agent_mode_requested && !$agent_mode_enabled){
$this->emDebug("Agent mode requested but not enabled in system settings. Proceeding with normal LLM call.");
unset($params['agent_mode']);
}
// Normal single-call path
$startLLMCall = microtime(true);
$response = $this->callLLMOnce($model, $params, $project_id);
$this->emDebug("callAI timing - callLLMOnce", [
'duration_ms' => round((microtime(true) - $startLLMCall) * 1000, 2)
]);
}
// ✅ ALWAYS sanitize output before returning to UI
$startSanitize = microtime(true);
$sanitizedResponse = $this->sanitizeOutputForUI($response);
$this->emDebug("callAI timing - sanitizeOutputForUI", [
'duration_ms' => round((microtime(true) - $startSanitize) * 1000, 2)
]);
$this->emDebug("callAI timing - attempt complete", [
'attempt' => $attempt + 1,
'attempt_duration_ms' => round((microtime(true) - $startAttempt) * 1000, 2),
'total_duration_ms' => round((microtime(true) - $startTotal) * 1000, 2)
]);
return $sanitizedResponse;
} catch (\Exception $e) {
$attempt++;
$this->emDebug("Attempt $attempt: Error", $e->getMessage());
$this->emDebug("Model: $model", "$params : $params");
if ($attempt > $retries) {
$error = [
'error' => true,
'type' => 'NETWORK_ERROR',
'message' => "Error after $retries retries: " . $e->getMessage()
];
if ($project_id) {
$this->logErrorInteraction($project_id, $params, $error);
} else {
$this->emDebug("Skipping error logging due to missing project ID (pid).");
}
$this->emDebug("callAI timing - failed after retries", [
'total_duration_ms' => round((microtime(true) - $startTotal) * 1000, 2)
]);
// ✅ Sanitize errors too
return $this->sanitizeOutputForUI($error);
}
}
}
$this->emDebug("callAI timing - unknown error fallback", [
'total_duration_ms' => round((microtime(true) - $startTotal) * 1000, 2)
]);
// ✅ Sanitize fallback error
return $this->sanitizeOutputForUI([
'error' => true,
'type' => 'UNKNOWN_ERROR',
'message' => 'Unknown error'
]);
}
private function loadToolsForProject(?int $pid): array
{
if (empty($pid)) return [];
$json = $this->getSystemSetting('agent_tool_registry');
if (empty($json)) return [];
$registry = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE || !is_array($registry)) {
$this->emError("Invalid agent_tool_registry JSON: " . json_last_error_msg());
return [];
}
$tools = $registry[(string)$pid] ?? $registry[$pid] ?? [];
if (!is_array($tools)) return [];
// Validate each tool definition
$validatedTools = [];
foreach ($tools as $tool) {
$validation = $this->validateToolDefinition($tool);
if ($validation['valid']) {
$validatedTools[] = $tool;
} else {
$this->emError("Invalid tool definition skipped", [
'tool_name' => $tool['name'] ?? 'unnamed',
'errors' => $validation['errors']
]);
}
}
return $validatedTools;
}
/**
* Validate tool definition structure
* Returns ['valid' => bool, 'errors' => array]
*/
private function validateToolDefinition(array $tool): array
{
$errors = [];
// Required fields
if (empty($tool['name'])) {
$errors[] = "Missing 'name' field";
} elseif (!preg_match('/^[a-zA-Z][a-zA-Z0-9_.]*$/', $tool['name'])) {
$errors[] = "Invalid 'name' format (must start with letter, alphanumeric + _ . only)";
}
if (empty($tool['description'])) {
$errors[] = "Missing 'description' field";
}
if (empty($tool['endpoint'])) {
$errors[] = "Missing 'endpoint' field";
} elseif (!in_array($tool['endpoint'], ['module_api', 'redcap_api', 'http'])) {
$errors[] = "Invalid 'endpoint' value (must be: module_api, redcap_api, or http)";
}
// Validate parameters structure
if (isset($tool['parameters'])) {
if (!is_array($tool['parameters'])) {
$errors[] = "'parameters' must be an object";
} elseif (!isset($tool['parameters']['type']) || $tool['parameters']['type'] !== 'object') {
$errors[] = "'parameters.type' must be 'object'";
}
}
// Validate endpoint-specific requirements
if ($tool['endpoint'] === 'redcap_api') {
if (empty($tool['redcap']['prefix'])) {
$errors[] = "Missing 'redcap.prefix' for redcap_api endpoint";
}
if (empty($tool['redcap']['action'])) {
$errors[] = "Missing 'redcap.action' for redcap_api endpoint";
}
} elseif ($tool['endpoint'] === 'module_api') {
if (empty($tool['module']['action'])) {
$errors[] = "Missing 'module.action' for module_api endpoint";
}
}
return [
'valid' => empty($errors),
'errors' => $errors
];
}
private function getAgentRouterPrompt(): string
{
$prompt = (string) ($this->getSystemSetting('agent_router_system_prompt') ?? '');
if (!empty(trim($prompt))) return $prompt;
// Safe default if not configured yet
return implode("\n", [
"You are the REDCap Agent Router.",
"Your job:",
"- Interpret user intent.",
"- Decide if a tool is required.",
"- Choose the correct tool(s) and provide a tool_call in strict JSON.",
"- Ask for missing required parameters instead of guessing.",
"- Never invent tool names or arguments.",
"",
"Output one of:",
"1) A tool_call JSON object",
"2) A clarification question",
"3) A final natural-language answer"
]);
}
private function buildToolCatalogText(array $tools): string
{
if (empty($tools)) {
return "TOOLS AVAILABLE: (none)";
}
$lines = ["TOOLS AVAILABLE:"];
foreach ($tools as $t) {
$name = $t['name'] ?? '(missing name)';
$desc = $t['description'] ?? '';
$required = $t['parameters']['required'] ?? [];
$reqStr = !empty($required) ? implode(", ", $required) : "(none)";
$lines[] = "- {$name}: {$desc} | required: {$reqStr}";
}
return implode("\n", $lines);
}
private function injectAgentSystemContext(array $messages, array $tools): array
{
$routerPrompt = $this->getAgentRouterPrompt();
// ALWAYS include tool catalog in agent mode
$routerPrompt .= "\n\n" . $this->buildToolCatalogText($tools);
array_unshift($messages, [
'role' => 'system',
'content' => $routerPrompt
]);
return $messages;
}
private function runAgentLoop(string $model, array $params, ?int $project_id): array
{
$messages = $params['messages'] ?? [];
$tools = $this->loadToolsForProject($project_id);
// Force schema-capable model for agent mode
$schemaModels = ['gpt-4.1', 'o1', 'o3-mini', 'gpt-5', 'llama3370b'];
if (!in_array($model, $schemaModels)) {
$this->emDebug("Agent mode requires schema-capable model, switching from {$model} to o1");
$model = 'o3-mini'; // Default fallback for agent mode
}
$this->emDebug("AGENT MODE ENABLED", [
'pid' => $project_id,
'model' => $model,
'tool_count' => count($tools),
'tool_names' => array_column($tools, 'name')
]);
// Initialize safety limits
$max_steps = (int) ($this->getSystemSetting('agent_max_steps') ?? 8);
$max_tools = (int) ($this->getSystemSetting('agent_max_tools_per_run') ?? 15);
$timeout = (int) ($this->getSystemSetting('agent_timeout_seconds') ?? 120);
$max_tool_result_chars = (int) ($this->getSystemSetting('agent_max_tool_result_chars') ?? 8000);
$start_time = time();
// CRITICAL: Agent mode needs higher token limit to avoid truncation mid-JSON
// o1 models use max_completion_tokens, others use max_tokens
if (in_array($model, ['o1', 'o3-mini'])) {
$params['max_completion_tokens'] = 32000; // Enough for full responses
} else {
$params['max_tokens'] = 4000;
}
$step = 0;
$tools_called = 0;
$tool_call_history = []; // Track tool calls to detect loops
$tools_used = []; // Track tools used for UI display
// Inject router system prompt + tool catalog
$messages = $this->injectAgentSystemContext($messages, $tools);
$params['messages'] = $messages;
// ⚠️ IMPORTANT: disable native OpenAI tools for agent mode
unset($params['tools'], $params['tool_choice']);
// Prevent recursion
unset($params['agent_mode']);
// Add JSON schema for agent responses
// Note: strict=false because tool arguments are dynamic (incompatible with strict mode)
$params['json_schema'] = [
'name' => 'agent_response',
'strict' => false,
'schema' => [
'type' => 'object',
'properties' => [
'tool_call' => [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'arguments' => ['type' => 'object']
],
'required' => ['name', 'arguments'],
'additionalProperties' => false
],
'final_answer' => ['type' => 'string'],
'thinking' => ['type' => 'string']
],
'additionalProperties' => false
]
];
while ($step < $max_steps) {
$step++;
// Step-by-step logging
$this->emDebug("AGENT STEP {$step}/{$max_steps}", [
'messages_count' => count($messages),
'last_message_preview' => substr(end($messages)['content'] ?? '', 0, 100)
]);
$response = $this->callLLMOnce($model, $params, $project_id);
$this->emDebug("AGENT RAW RESPONSE", $response);
$content = trim($response['content'] ?? '');
// Decode HTML entities (REDCap may encode response)
$content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
// Clean ALL control characters (including literal newlines/tabs) that break JSON parsing
// This collapses pretty-printed JSON into single line
// Escaped sequences like \n are preserved since they're not actual control chars
$cleanContent = preg_replace('/[\x00-\x1F]/', '', $content);
$decoded = json_decode($cleanContent, true);
// Fallback strategy for non-JSON responses
if (json_last_error() !== JSON_ERROR_NONE) {
$this->emDebug("JSON parse failed, trying regex extraction", [
'error' => json_last_error_msg(),
'content_preview' => substr($content, 0, 200)
]);
// Try to extract tool_call JSON pattern from text
if (preg_match('/\{[\s\S]*"tool_call"[\s\S]*\}/', $content, $matches)) {
$decoded = json_decode($matches[0], true);
if (json_last_error() === JSON_ERROR_NONE) {
$this->emDebug("Regex extraction succeeded");
}
}
// If still no valid JSON, treat as plain text final answer
if (!is_array($decoded) || json_last_error() !== JSON_ERROR_NONE) {
$this->emDebug("Treating plain text response as final answer");
return [
'role' => 'assistant',
'content' => $content
];
}
}
if (isset($decoded['tool_call'])) {
$tool_call = $decoded['tool_call'];
$tool_name = $tool_call['name'] ?? null;
$arguments = $tool_call['arguments'] ?? [];
if (!$tool_name) {
return $this->agentError("INVALID_TOOL_CALL", "Tool name missing");
}
$execution = $this->executeToolCall(
tool_name: $tool_name,
arguments: $arguments,
tools: $tools,
project_id: $project_id
);
// Return errors immediately (but not MISSING_PARAMETERS - let agent handle that)
if ($execution['error'] ?? false) {
return $execution;
}
// Cap tool result size to prevent token overflow
$execution['result'] = $this->capToolResultSize(
$execution['result'],
$max_tool_result_chars
);
// Track tool usage for UI display
$tools_used[] = [
'name' => $tool_name,
'arguments' => $arguments,
'step' => $step
];
// Detect tool ping-pong loops (same tool+args called repeatedly)
$callSignature = $tool_name . ':' . json_encode($arguments);
$tool_call_history[] = $callSignature;
// Check for loops: same signature appearing 3+ times in last 5 calls
if (count($tool_call_history) >= 5) {
$recentCalls = array_slice($tool_call_history, -5);
$signatureCounts = array_count_values($recentCalls);
if (max($signatureCounts) >= 3) {
return $this->agentError(
"TOOL_LOOP_DETECTED",
"Agent is repeatedly calling the same tool. Breaking loop to prevent infinite execution."
);
}
}
// Increment tool counter and check limit
$tools_called++;
if ($tools_called >= $max_tools) {
return $this->agentError(
"MAX_TOOLS_EXCEEDED",
"Agent called {$tools_called} tools (limit: {$max_tools})"
);
}
// Check timeout
if (time() - $start_time > $timeout) {
return $this->agentError(
"TIMEOUT",
"Agent exceeded {$timeout} second time limit"
);
}
// ✅ Inject tool result as USER context (standard practice)
$messages[] = [
'role' => 'user',
'content' =>
"TOOL RESULT [{$tool_name}]:\n" .
json_encode($execution['result'], JSON_PRETTY_PRINT)
];
$params['messages'] = $messages;
continue;
} elseif (isset($decoded['final_answer'])) {
return [
'role' => 'assistant',
'content' => $decoded['final_answer'],
'tools_used' => $tools_used // Include tool metadata for UI
];
} else {
// No tool_call or final_answer - treat entire response as final answer
$this->emDebug("No tool_call or final_answer field, using raw content");
return [
'role' => 'assistant',
'content' => $content,
'tools_used' => $tools_used
];
}
}
return $this->agentError(
"AGENT_MAX_STEPS_EXCEEDED",
"Agent exceeded maximum allowed steps ({$max_steps})"
);
}
private function callLLMOnce(string $model, array $params, ?int $project_id): array
{
$this->initSecureChatAI();
if (!isset($this->modelConfig[$model])) {
throw new Exception('Unsupported model: ' . $model);
}
$modelConfig = $this->modelConfig[$model];
$api_endpoint = $modelConfig['api_url'];
foreach ($modelConfig['required'] as $param) {
if (empty($params[$param])) {
throw new Exception('Missing required parameter: ' . $param);
}
}
$filteredParams = $this->filterDefaultParamsForModel($model, $params);
[$paramName, $dynamicMax, $promptTokens] = $this->computeDynamicMaxTokens($model, $fullPrompt ?? $filteredParams['messages'][0]['content'] ?? '');
$filteredParams[$paramName] = (int)$dynamicMax;
$this->emDebug("Dynamic tokens: prompt={$promptTokens}, max={$dynamicMax} for {$model}");
switch ($model) {
case 'gpt-4o':
case 'ada-002':
case 'text-embedding-3-small':
$gpt = new GPTModelRequest($this, $modelConfig, $this->defaultParams, $model);
$responseData = $gpt->sendRequest($api_endpoint, $filteredParams);
break;
case 'deepseek':
$generic = new GenericModelRequest($this, $modelConfig, $this->defaultParams, $model);
$responseData = $generic->sendRequest($api_endpoint, $filteredParams);
break;
case 'whisper':
$whisper = new WhisperModelRequest($this, $modelConfig, $this->defaultParams, $model);
$responseData = $whisper->sendRequest($api_endpoint, $params);
break;
case 'gpt-4.1':
case 'o1':
case 'o3-mini':
case 'gpt-5':
case 'llama3370b':
case 'llama-Maverick':
$filteredParams = $this->filterDefaultParamsForModel($model, $params);
$generic = new GenericModelRequest($this, $modelConfig, [], $model);
$responseData = $generic->sendRequest($api_endpoint, $filteredParams);
$this->emDebug("RAW GenericModelRequest API RESPONSE", $responseData);
break;
case 'claude':
$claude = new ClaudeModelRequest($this, $modelConfig, $this->defaultParams, $model);
$responseData = $claude->sendRequest($api_endpoint, $filteredParams);
break;
case 'gemini20flash':
case 'gemini25pro':
$gemini = new GeminiModelRequest($this, $modelConfig, $this->defaultParams, $model);
$responseData = $gemini->sendRequest($api_endpoint, $filteredParams);
break;
case 'gpt-4o-tts':
case 'tts':
$tts = new GPT4oMiniTTSModelRequest($this, $modelConfig, $this->defaultParams, $model);
$responseData = $tts->sendRequest($api_endpoint, $params);
break;
default:
throw new Exception("Unsupported model configuration for: $model");
}
$normalizedResponse = $this->normalizeResponse($responseData, $model);
if ($project_id) {
$this->logInteraction($project_id, $params, $responseData);
}
return $normalizedResponse;
}
private function executeToolCall(
string $tool_name,
array $arguments,
array $tools,
?int $project_id
): array {
$tool = null;
foreach ($tools as $t) {
if (($t['name'] ?? null) === $tool_name) {
$tool = $t;
break;
}
}
if (!$tool) {
return $this->agentError("UNKNOWN_TOOL", "Tool '{$tool_name}' not registered");
}
// ---- Required args check ----
$required = $tool['parameters']['required'] ?? [];
$missing = array_diff($required, array_keys($arguments));
if (!empty($missing)) {
// Don't return error - let the agent ask the user naturally
return [
'error' => false,
'type' => 'MISSING_PARAMETERS',
'missing' => array_values($missing),
'result' => [
'status' => 'incomplete',
'message' => "Missing required parameters: " . implode(", ", $missing),
'missing_fields' => array_values($missing)
]
];
}
// ---- Same-project EM call ----
if (($tool['endpoint'] ?? '') === 'module_api') {
return [
'error' => false,
'result' => $this->redcap_module_api(
$tool['module']['action'],
$arguments
)
];
}
// ---- Cross-project REDCap API call ----
if (($tool['endpoint'] ?? '') === 'redcap_api') {
try {
$apiUrl = rtrim($this->getSystemSetting('agent_tools_redcap_api_url'), '/') . '/';
$apiToken = $this->getSystemSetting('agent_tools_project_api_key');
if (empty($apiUrl) || empty($apiToken)) {
return $this->agentError(
"MISCONFIGURED_AGENT_TOOLS",
"Missing agent_tools_redcap_api_url or agent_tools_project_api_key"
);
}
$payload = array_merge([
'token' => $apiToken,
'content' => 'externalModule',
'format' => 'json',
'returnFormat' => 'json',
'prefix' => $tool['redcap']['prefix'],
'action' => $tool['redcap']['action'],
], $arguments);
$client = new \GuzzleHttp\Client([
'timeout' => 10
]);
$response = $client->post($apiUrl, [
'form_params' => $payload
]);
$body = json_decode((string) $response->getBody(), true);
return [
'error' => false,
'result' => $body
];
} catch (\Throwable $e) {
return $this->agentError(
"REDCAP_API_ERROR",
$e->getMessage()
);
}
}
return $this->agentError(
"UNSUPPORTED_TOOL_ENDPOINT",
"Endpoint '{$tool['endpoint']}' not supported"
);
}
private function askForMoreInfoText(string $tool_name, array $missing_fields): string
{
return "I need: " . implode(", ", $missing_fields) . " to use {$tool_name}.";
}
private function agentError(string $type, string $message): array
{
return [
'error' => true,
'type' => $type,
'message' => $message
];
}
/**
* Cap tool result size to prevent token overflow
* Intelligently truncates while preserving structure and adding metadata
*/
private function capToolResultSize($result, int $maxChars)
{
$jsonResult = json_encode($result, JSON_UNESCAPED_UNICODE);
$originalSize = strlen($jsonResult);
// If under limit, return as-is
if ($originalSize <= $maxChars) {
return $result;
}
$truncated = $result;
$wasTruncated = false;
// Handle arrays - truncate items
if (is_array($result) && array_is_list($result)) {
$itemCount = count($result);
$kept = [];
$currentSize = 2; // [] brackets
foreach ($result as $item) {
$itemJson = json_encode($item, JSON_UNESCAPED_UNICODE);
$itemSize = strlen($itemJson) + 1; // +1 for comma
if ($currentSize + $itemSize > $maxChars - 200) { // Reserve 200 chars for metadata
$wasTruncated = true;
break;
}
$kept[] = $item;
$currentSize += $itemSize;
}
$truncated = $kept;
if ($wasTruncated) {
$truncated[] = [
'_truncated' => true,
'_original_count' => $itemCount,
'_returned_count' => count($kept),
'_message' => "Result truncated to fit token budget. Showing " . count($kept) . " of $itemCount items."
];
}
}
// Handle objects - recursively cap nested values
elseif (is_array($result)) {
$currentSize = 2; // {} brackets
$truncated = [];
foreach ($result as $key => $value) {
$valueJson = json_encode($value, JSON_UNESCAPED_UNICODE);
$itemSize = strlen($key) + strlen($valueJson) + 4; // key + value + quotes/colon
if ($currentSize + $itemSize > $maxChars - 200) {
$wasTruncated = true;
break;
}
$truncated[$key] = $value;
$currentSize += $itemSize;
}
if ($wasTruncated) {
$truncated['_truncated'] = true;
$truncated['_message'] = "Result truncated to fit token budget.";
}
}
// Handle strings - simple truncation
elseif (is_string($result)) {
$truncated = substr($result, 0, $maxChars - 100);
$truncated .= "\n\n[... truncated " . ($originalSize - strlen($truncated)) . " characters to fit token budget]";
}
return $truncated;
}
/**
* Final cleanup: ensure response is always clean, user-ready text
* - Extract text from stringified JSON (agent schema or accidental)
* - Convert error objects to polite messages
* - Never return raw JSON strings or error arrays to the UI
*/
private function sanitizeOutputForUI(array $response): array
{
// Embedding responses (ada-002) should pass through unchanged
// They have 'data' field with embedding vectors, not 'content'
if (isset($response['data']) && !isset($response['content'])) {
$this->emDebug("Passing through embedding response in sanitizeOutputForUI");
return $response;
}
// TTS responses should pass through unchanged (have audio_base64)
if (isset($response['audio_base64'])) {
$this->emDebug("Passing through TTS response in sanitizeOutputForUI");
return $response;
}
// STT/Whisper responses should pass through unchanged (have 'text' from transcription)
// Raw whisper responses don't have 'role' field
if (isset($response['text']) && !isset($response['role'])) {
$this->emDebug("Passing through STT response in sanitizeOutputForUI");
return $response;
}
// Handle error responses with friendly messages
if (!empty($response['error'])) {
$type = $response['type'] ?? 'UNKNOWN_ERROR';
$msg = $response['message'] ?? 'An error occurred';
$friendlyMessages = [
'TIMEOUT' => "I apologize, but that request took longer than expected. Please try again in a moment.",
'MAX_TOOLS_EXCEEDED' => "I apologize, but I needed to use too many tools for that request. Could you try simplifying your question?",
'AGENT_MAX_STEPS_EXCEEDED' => "I apologize, but I couldn't complete that task efficiently. Could you try breaking it into smaller requests?",
'TOOL_LOOP_DETECTED' => "I apologize, but I got stuck in a loop trying to complete that request. Could you try rephrasing your question?",
'UNKNOWN_TOOL' => "I apologize, but I don't have access to that capability right now.",
'MISCONFIGURED_AGENT_TOOLS' => "I apologize, but I'm experiencing technical difficulties. Please contact your administrator.",
'REDCAP_API_ERROR' => "I apologize, but I'm having trouble accessing that data right now. Please try again in a moment.",
'NETWORK_ERROR' => "I apologize, but I'm experiencing network difficulties. Please wait a moment and try again.",
];
$politeMessage = $friendlyMessages[$type] ??
"I apologize, but I'm experiencing technical difficulties. Please wait a moment and try again.";
return [
'role' => 'assistant',
'content' => $politeMessage
];
}
$content = trim($response['content'] ?? '');
// Decode HTML entities (in case of double-encoding)
$content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
// Try to detect and parse JSON responses
if (!empty($content) && ($content[0] === '{' || str_starts_with($content, '```json'))) {
// Strip markdown code fences if present
$content = preg_replace('/^```json\s*|\s*```$/s', '', $content);
$content = trim($content);
// Clean control characters that break JSON parsing (same as agent loop)
$cleanContent = preg_replace('/[\x00-\x1F]/', '', $content);
$decoded = json_decode($cleanContent, true);
$parseError = json_last_error();
if ($parseError === JSON_ERROR_NONE && is_array($decoded)) {
if (!empty($response['preserve_structure'])) {
// Keep the JSON string as-is for structured output
$response['content'] = $content;
return $response;
}
// Extract from agent schema format
if (isset($decoded['final_answer'])) {
$content = $decoded['final_answer'];
} elseif (isset($decoded['tool_call'])) {
// If we're seeing a tool_call in final output, something went wrong
$content = "I apologize, but I wasn't able to complete that request properly. Could you try rephrasing?";
} elseif (isset($decoded['content'])) {
// Some models nest content in JSON
$content = $decoded['content'];
} elseif (isset($decoded['message'])) {
$content = $decoded['message'];
}
// Otherwise leave the JSON string as-is (might be intentional structured output)
}
}
// EMERGENCY BACKSTOP: If content STILL looks like our JSON schema, strip it one more time
// This handles edge cases where json_decode failed or we didn't extract properly
// Works even with truncated JSON (missing closing brace/quote)
if (!empty($content) && preg_match('/\{\s*"final_answer"\s*:\s*"(.*)$/s', $content, $match)) {
$extracted = $match[1];
// Remove trailing garbage (incomplete JSON structure)
$extracted = preg_replace('/["}\s]*$/', '', $extracted);
// Unescape JSON string escapes
$content = str_replace(['\n', '\r', '\t', '\"', '\\\\'], ["\n", "\r", "\t", '"', '\\'], $extracted);
}
$sanitized = [
'role' => $response['role'] ?? 'assistant',
'content' => $content,
'model' => $response['model'] ?? null,
'usage' => $response['usage'] ?? null
];
// Preserve tool metadata if present (for UI display)
if (!empty($response['tools_used'])) {
$sanitized['tools_used'] = $response['tools_used'];
}
return $sanitized;
}
private function estimateTokens(string $text, string $model): int {
$provider = new EncoderProvider(); // Caches encoders automatically
$encoder = $provider->getForModel($model); // Maps 'o1', 'gpt-4.1' → cl100k_base
return count($encoder->encode($text)); // Returns token count
}
private function computeDynamicMaxTokens(string $model, string $prompt): array {
$modelSpecs = [
'o1' => [
'context' => 200000,
'output_max' => 100000,
'param' => 'max_completion_tokens',
'buffer' => 25000
],
'gpt-4.1' => [
'context' => 1000000,
'output_max' => 128000,
'param' => 'max_tokens',
'buffer' => 2000
],