orquesta-cli 0.2.18 → 0.2.19
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/dist/core/llm/llm-client.js +66 -28
- package/package.json +1 -1
|
@@ -6,36 +6,74 @@ import { LLMError, TokenLimitError, RateLimitError, ContextLengthError, } from '
|
|
|
6
6
|
import { logger, isLLMLogEnabled } from '../../utils/logger.js';
|
|
7
7
|
import { usageTracker } from '../usage-tracker.js';
|
|
8
8
|
import { getForcedTier, getBatutaSessionId, setLastBatutaRoute } from '../routing-state.js';
|
|
9
|
-
function
|
|
9
|
+
function extractTopLevelJsonObjects(s) {
|
|
10
|
+
const objects = [];
|
|
11
|
+
let depth = 0;
|
|
12
|
+
let startIdx = -1;
|
|
13
|
+
let inString = false;
|
|
14
|
+
let escaped = false;
|
|
15
|
+
for (let i = 0; i < s.length; i++) {
|
|
16
|
+
const ch = s[i];
|
|
17
|
+
if (inString) {
|
|
18
|
+
if (escaped)
|
|
19
|
+
escaped = false;
|
|
20
|
+
else if (ch === '\\')
|
|
21
|
+
escaped = true;
|
|
22
|
+
else if (ch === '"')
|
|
23
|
+
inString = false;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (ch === '"') {
|
|
27
|
+
inString = true;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (ch === '{') {
|
|
31
|
+
if (depth === 0)
|
|
32
|
+
startIdx = i;
|
|
33
|
+
depth++;
|
|
34
|
+
}
|
|
35
|
+
else if (ch === '}') {
|
|
36
|
+
if (depth > 0) {
|
|
37
|
+
depth--;
|
|
38
|
+
if (depth === 0 && startIdx !== -1) {
|
|
39
|
+
objects.push(s.slice(startIdx, i + 1));
|
|
40
|
+
startIdx = -1;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return objects;
|
|
46
|
+
}
|
|
47
|
+
function coerceSyntheticToolCalls(content) {
|
|
10
48
|
const trimmed = content.trim();
|
|
11
49
|
if (!trimmed || !trimmed.includes('"tool"'))
|
|
12
|
-
return
|
|
50
|
+
return [];
|
|
13
51
|
const cleaned = trimmed
|
|
14
52
|
.replace(/^```(?:json)?\s*\n?/i, '')
|
|
15
53
|
.replace(/\n?```\s*$/i, '')
|
|
16
54
|
.trim();
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
55
|
+
const calls = [];
|
|
56
|
+
for (const objStr of extractTopLevelJsonObjects(cleaned)) {
|
|
57
|
+
let parsed;
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(objStr);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (!parsed || typeof parsed !== 'object')
|
|
65
|
+
continue;
|
|
66
|
+
const obj = parsed;
|
|
67
|
+
if (typeof obj.tool !== 'string' || !obj.tool)
|
|
68
|
+
continue;
|
|
69
|
+
const args = obj.arguments && typeof obj.arguments === 'object' ? obj.arguments : {};
|
|
70
|
+
calls.push({
|
|
71
|
+
id: `call_synth_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
72
|
+
type: 'function',
|
|
73
|
+
function: { name: obj.tool, arguments: JSON.stringify(args) },
|
|
74
|
+
});
|
|
27
75
|
}
|
|
28
|
-
|
|
29
|
-
return null;
|
|
30
|
-
const obj = parsed;
|
|
31
|
-
if (typeof obj.tool !== 'string' || !obj.tool)
|
|
32
|
-
return null;
|
|
33
|
-
const args = obj.arguments && typeof obj.arguments === 'object' ? obj.arguments : {};
|
|
34
|
-
return {
|
|
35
|
-
id: `call_synth_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
36
|
-
type: 'function',
|
|
37
|
-
function: { name: obj.tool, arguments: JSON.stringify(args) },
|
|
38
|
-
};
|
|
76
|
+
return calls;
|
|
39
77
|
}
|
|
40
78
|
function buildPerRequestHeaders() {
|
|
41
79
|
const headers = {
|
|
@@ -533,12 +571,12 @@ export class LLMClient {
|
|
|
533
571
|
const assistantMessage = choice.message;
|
|
534
572
|
if ((!assistantMessage.tool_calls || assistantMessage.tool_calls.length === 0) &&
|
|
535
573
|
typeof assistantMessage.content === 'string') {
|
|
536
|
-
const coerced =
|
|
537
|
-
if (coerced) {
|
|
538
|
-
assistantMessage.tool_calls =
|
|
574
|
+
const coerced = coerceSyntheticToolCalls(assistantMessage.content);
|
|
575
|
+
if (coerced.length > 0) {
|
|
576
|
+
assistantMessage.tool_calls = coerced;
|
|
539
577
|
assistantMessage.content = '';
|
|
540
|
-
logger.flow('Recovered synthetic tool call from plain-text content', {
|
|
541
|
-
|
|
578
|
+
logger.flow('Recovered synthetic tool call(s) from plain-text content', {
|
|
579
|
+
tools: coerced.map(c => c.function.name).join(', '),
|
|
542
580
|
});
|
|
543
581
|
}
|
|
544
582
|
}
|