orquesta-cli 0.2.18 → 0.2.20

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.
@@ -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 coerceSyntheticToolCall(content) {
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 null;
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 start = cleaned.indexOf('{');
18
- const end = cleaned.lastIndexOf('}');
19
- if (start === -1 || end === -1 || end <= start)
20
- return null;
21
- let parsed;
22
- try {
23
- parsed = JSON.parse(cleaned.slice(start, end + 1));
24
- }
25
- catch {
26
- return null;
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
- if (!parsed || typeof parsed !== 'object')
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 = coerceSyntheticToolCall(assistantMessage.content);
537
- if (coerced) {
538
- assistantMessage.tool_calls = [coerced];
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
- tool: coerced.function.name,
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
  }
@@ -51,9 +51,81 @@ Example:
51
51
  },
52
52
  },
53
53
  };
54
+ function extractTopLevelJsonObjects(s) {
55
+ const objects = [];
56
+ let depth = 0;
57
+ let startIdx = -1;
58
+ let inString = false;
59
+ let escaped = false;
60
+ for (let i = 0; i < s.length; i++) {
61
+ const ch = s[i];
62
+ if (inString) {
63
+ if (escaped)
64
+ escaped = false;
65
+ else if (ch === '\\')
66
+ escaped = true;
67
+ else if (ch === '"')
68
+ inString = false;
69
+ continue;
70
+ }
71
+ if (ch === '"') {
72
+ inString = true;
73
+ continue;
74
+ }
75
+ if (ch === '{') {
76
+ if (depth === 0)
77
+ startIdx = i;
78
+ depth++;
79
+ }
80
+ else if (ch === '}') {
81
+ if (depth > 0) {
82
+ depth--;
83
+ if (depth === 0 && startIdx !== -1) {
84
+ objects.push(s.slice(startIdx, i + 1));
85
+ startIdx = -1;
86
+ }
87
+ }
88
+ }
89
+ }
90
+ return objects;
91
+ }
92
+ function unwrapNestedFinalResponse(message, depth = 0) {
93
+ if (depth > 5)
94
+ return message;
95
+ const trimmed = message.trim();
96
+ if (!trimmed.includes('"tool"') || !trimmed.includes('final_response'))
97
+ return message;
98
+ const cleaned = trimmed
99
+ .replace(/^```(?:json)?\s*\n?/i, '')
100
+ .replace(/\n?```\s*$/i, '')
101
+ .trim();
102
+ for (const objStr of extractTopLevelJsonObjects(cleaned)) {
103
+ let parsed;
104
+ try {
105
+ parsed = JSON.parse(objStr);
106
+ }
107
+ catch {
108
+ continue;
109
+ }
110
+ if (!parsed || typeof parsed !== 'object')
111
+ continue;
112
+ const obj = parsed;
113
+ if (obj.tool === 'final_response' && obj.arguments && typeof obj.arguments.message === 'string') {
114
+ return unwrapNestedFinalResponse(obj.arguments.message, depth + 1);
115
+ }
116
+ }
117
+ return message;
118
+ }
54
119
  async function executeFinalResponse(args) {
55
120
  logger.enter('executeFinalResponse', args);
56
- const message = args['message'];
121
+ let message = args['message'];
122
+ if (typeof message === 'string') {
123
+ const unwrapped = unwrapNestedFinalResponse(message);
124
+ if (unwrapped !== message) {
125
+ logger.flow('final_response: unwrapped nested synthetic tool-call JSON from message');
126
+ message = unwrapped;
127
+ }
128
+ }
57
129
  if (!message || typeof message !== 'string') {
58
130
  logger.warn('Missing or invalid message');
59
131
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orquesta-cli",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
4
4
  "description": "Orquesta CLI - AI-powered coding assistant with team collaboration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",