@blockrun/franklin 3.29.0 → 3.29.2
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/agent/llm.js +12 -2
- package/dist/agent/loop.js +9 -1
- package/dist/agent/repair/scavenge.js +80 -15
- package/package.json +1 -1
package/dist/agent/llm.js
CHANGED
|
@@ -949,9 +949,16 @@ export class ModelClient {
|
|
|
949
949
|
}
|
|
950
950
|
if (currentText) {
|
|
951
951
|
if (textEmission.mode === 'hold' && isRoleplayedJsonToolCallText(currentText)) {
|
|
952
|
+
// A roleplayed JSON tool call. Do NOT stream it to the UI,
|
|
953
|
+
// but DO keep it in `collected` so the loop's scavenge pass
|
|
954
|
+
// (src/agent/repair/scavenge.ts) can turn it into a real
|
|
955
|
+
// tool_use and execute it. If scavenge can't recover a valid
|
|
956
|
+
// call, the loop strips this text, restoring the old
|
|
957
|
+
// "non-productive → switch model" recovery.
|
|
958
|
+
collected.push({ type: 'text', text: currentText });
|
|
952
959
|
if (this.debug) {
|
|
953
960
|
console.error(`[franklin] Model ${request.model} emitted a raw JSON function-call object as text. ` +
|
|
954
|
-
'
|
|
961
|
+
'Held from display; loop will scavenge it into a tool_use or strip it.');
|
|
955
962
|
}
|
|
956
963
|
}
|
|
957
964
|
else if (textEmission.mode === 'hold' && isNemotronProse) {
|
|
@@ -1038,9 +1045,12 @@ export class ModelClient {
|
|
|
1038
1045
|
}
|
|
1039
1046
|
if (currentText) {
|
|
1040
1047
|
if (textEmission.mode === 'hold' && isRoleplayedJsonToolCallText(currentText)) {
|
|
1048
|
+
// See the matching branch in content_block_stop: keep it for the
|
|
1049
|
+
// loop's scavenge pass instead of dropping it.
|
|
1050
|
+
collected.push({ type: 'text', text: currentText });
|
|
1041
1051
|
if (this.debug) {
|
|
1042
1052
|
console.error(`[franklin] Model ${request.model} emitted a raw JSON function-call object as text. ` +
|
|
1043
|
-
'
|
|
1053
|
+
'Held from display; loop will scavenge it into a tool_use or strip it.');
|
|
1044
1054
|
}
|
|
1045
1055
|
}
|
|
1046
1056
|
else if (textEmission.mode === 'hold' && isNemotronProse) {
|
package/dist/agent/loop.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Franklin Agent Loop
|
|
3
3
|
* The core reasoning-action cycle: prompt → model → extract capabilities → execute → repeat.
|
|
4
4
|
*/
|
|
5
|
-
import { ModelClient } from './llm.js';
|
|
5
|
+
import { ModelClient, isRoleplayedJsonToolCallText } from './llm.js';
|
|
6
6
|
import { autoCompactIfNeeded, forceCompact, microCompact, projectCompactionSavings } from './compact.js';
|
|
7
7
|
import { estimateHistoryTokens, updateActualTokens, resetTokenAnchor, getAnchoredTokenCount, getContextWindow, setEstimationModel } from './tokens.js';
|
|
8
8
|
import { handleSlashCommand } from './commands.js';
|
|
@@ -1368,6 +1368,14 @@ export async function interactiveSession(config, getUserInput, onEvent, onAbortR
|
|
|
1368
1368
|
responseParts = [...responseParts, ...novelCalls];
|
|
1369
1369
|
logger.warn(`[franklin] scavenged ${repaired.report.scavenged} leaked tool call(s) from ${config.model}: ${repaired.report.notes.join('; ')}`);
|
|
1370
1370
|
}
|
|
1371
|
+
// Strip pure roleplayed-JSON tool-call text parts. The LLM client
|
|
1372
|
+
// holds these out of the live stream (never shown) but keeps them
|
|
1373
|
+
// in `content` so the scavenge above can recover the call. Whether
|
|
1374
|
+
// or not a call was recovered, the raw JSON must not linger as the
|
|
1375
|
+
// assistant's "answer" — drop it. When nothing was scavenged this
|
|
1376
|
+
// also empties the turn, so the empty-response recovery below can
|
|
1377
|
+
// switch models, preserving the prior behavior.
|
|
1378
|
+
responseParts = responseParts.filter(p => !(p.type === 'text' && isRoleplayedJsonToolCallText(p.text)));
|
|
1371
1379
|
}
|
|
1372
1380
|
// ── Empty response recovery ──
|
|
1373
1381
|
// If the model returns nothing, DON'T just retry the same model with the same input.
|
|
@@ -37,10 +37,11 @@ export function scavengeToolCalls(text, opts) {
|
|
|
37
37
|
for (const invoke of iterateDsmlInvokes(text)) {
|
|
38
38
|
if (out.length >= max)
|
|
39
39
|
break;
|
|
40
|
-
|
|
40
|
+
const resolved = resolveAllowedName(invoke.name, opts.allowedNames);
|
|
41
|
+
if (!resolved)
|
|
41
42
|
continue;
|
|
42
|
-
out.push(makeInvocation(
|
|
43
|
-
notes.push(`scavenged DSML call: ${
|
|
43
|
+
out.push(makeInvocation(resolved, invoke.args));
|
|
44
|
+
notes.push(`scavenged DSML call: ${resolved}`);
|
|
44
45
|
}
|
|
45
46
|
// Pattern B — raw JSON objects in the three canonical shapes. Strip
|
|
46
47
|
// DSML blocks first so their parameter payloads don't get re-scavenged
|
|
@@ -49,8 +50,9 @@ export function scavengeToolCalls(text, opts) {
|
|
|
49
50
|
for (const candidate of iterateJsonObjects(nonDsml)) {
|
|
50
51
|
if (out.length >= max)
|
|
51
52
|
break;
|
|
52
|
-
const call
|
|
53
|
-
|
|
53
|
+
for (const call of coerceToInvocations(candidate, opts.allowedNames)) {
|
|
54
|
+
if (out.length >= max)
|
|
55
|
+
break;
|
|
54
56
|
out.push(call);
|
|
55
57
|
notes.push(`scavenged call: ${call.name}`);
|
|
56
58
|
}
|
|
@@ -133,33 +135,96 @@ function* iterateJsonObjects(text) {
|
|
|
133
135
|
}
|
|
134
136
|
}
|
|
135
137
|
}
|
|
136
|
-
function
|
|
138
|
+
function coerceToInvocations(candidateJson, allowedNames) {
|
|
137
139
|
let parsed;
|
|
138
140
|
try {
|
|
139
141
|
parsed = JSON.parse(candidateJson);
|
|
140
142
|
}
|
|
141
143
|
catch {
|
|
142
|
-
return
|
|
144
|
+
return [];
|
|
143
145
|
}
|
|
144
146
|
if (!parsed || typeof parsed !== 'object')
|
|
145
|
-
return
|
|
147
|
+
return [];
|
|
146
148
|
const obj = parsed;
|
|
147
|
-
//
|
|
148
|
-
|
|
149
|
-
|
|
149
|
+
// Envelope A — OpenAI assistant message { tool_calls: [ {...}, ... ] }.
|
|
150
|
+
// Gateway models that lack native tool-calling sometimes dump the whole
|
|
151
|
+
// assistant-message JSON into the text channel.
|
|
152
|
+
if (Array.isArray(obj.tool_calls)) {
|
|
153
|
+
const calls = [];
|
|
154
|
+
for (const tc of obj.tool_calls) {
|
|
155
|
+
const c = coerceObj(tc, allowedNames);
|
|
156
|
+
if (c)
|
|
157
|
+
calls.push(c);
|
|
158
|
+
}
|
|
159
|
+
return calls;
|
|
160
|
+
}
|
|
161
|
+
// Envelope B — legacy single { function_call: { name, arguments } }.
|
|
162
|
+
if (obj.function_call && typeof obj.function_call === 'object') {
|
|
163
|
+
const c = coerceObj({ type: 'function', function: obj.function_call }, allowedNames);
|
|
164
|
+
return c ? [c] : [];
|
|
165
|
+
}
|
|
166
|
+
const c = coerceObj(obj, allowedNames);
|
|
167
|
+
return c ? [c] : [];
|
|
168
|
+
}
|
|
169
|
+
function coerceObj(value, allowedNames) {
|
|
170
|
+
if (!value || typeof value !== 'object')
|
|
171
|
+
return null;
|
|
172
|
+
const obj = value;
|
|
173
|
+
// Pattern 1 — { name, arguments } (Anthropic-ish flat form) AND the flat
|
|
174
|
+
// OpenAI leak { type: "function", name, parameters } emitted by the free
|
|
175
|
+
// DeepSeek model — both carry the name at the top level. Args land under
|
|
176
|
+
// `arguments` or `parameters` depending on the model's chat template.
|
|
177
|
+
if (typeof obj.name === 'string') {
|
|
178
|
+
const resolved = resolveAllowedName(obj.name, allowedNames);
|
|
179
|
+
if (resolved) {
|
|
180
|
+
return makeInvocation(resolved, normalizeArgs(obj.arguments ?? obj.parameters));
|
|
181
|
+
}
|
|
150
182
|
}
|
|
151
183
|
// Pattern 2 — OpenAI-style { type: "function", function: { name, arguments } }.
|
|
152
184
|
if (obj.type === 'function' &&
|
|
153
185
|
obj.function &&
|
|
154
186
|
typeof obj.function === 'object') {
|
|
155
187
|
const fn = obj.function;
|
|
156
|
-
|
|
157
|
-
|
|
188
|
+
const resolved = typeof fn.name === 'string' ? resolveAllowedName(fn.name, allowedNames) : null;
|
|
189
|
+
if (resolved) {
|
|
190
|
+
return makeInvocation(resolved, normalizeArgs(fn.arguments ?? fn.parameters));
|
|
158
191
|
}
|
|
159
192
|
}
|
|
160
193
|
// Pattern 3 — { tool_name, tool_args } (R1 free-form variant).
|
|
161
|
-
if (typeof obj.tool_name === 'string'
|
|
162
|
-
|
|
194
|
+
if (typeof obj.tool_name === 'string') {
|
|
195
|
+
const resolved = resolveAllowedName(obj.tool_name, allowedNames);
|
|
196
|
+
if (resolved) {
|
|
197
|
+
return makeInvocation(resolved, normalizeArgs(obj.tool_args ?? obj.tool_parameters));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
/** Collapse a tool name to a comparison key: lowercase, drop separators.
|
|
203
|
+
* `activate_tool` / `web_search` → `activatetool` / `websearch`. */
|
|
204
|
+
function normalizeName(name) {
|
|
205
|
+
return name.toLowerCase().replace(/[_\-\s]/g, '');
|
|
206
|
+
}
|
|
207
|
+
/** Resolve a leaked tool name to its canonical registry spelling. Exact
|
|
208
|
+
* match wins; otherwise a separator/case-insensitive match lets models
|
|
209
|
+
* that rewrite names to OpenAI snake_case (`web_search` → `WebSearch`)
|
|
210
|
+
* still be recovered. Returns null if no allowed tool matches. */
|
|
211
|
+
function resolveAllowedName(name, allowedNames) {
|
|
212
|
+
if (!name || typeof name !== 'string')
|
|
213
|
+
return null;
|
|
214
|
+
if (allowedNames.has(name))
|
|
215
|
+
return name;
|
|
216
|
+
// Try the name as-is, then with any provider namespace stripped
|
|
217
|
+
// (`functions.web_search` / `tools.web_search` → `web_search`), which
|
|
218
|
+
// Gemini- and OpenAI-style function declarations sometimes prepend.
|
|
219
|
+
const candidates = [name];
|
|
220
|
+
if (name.includes('.'))
|
|
221
|
+
candidates.push(name.slice(name.lastIndexOf('.') + 1));
|
|
222
|
+
for (const candidate of candidates) {
|
|
223
|
+
const target = normalizeName(candidate);
|
|
224
|
+
for (const allowed of allowedNames) {
|
|
225
|
+
if (normalizeName(allowed) === target)
|
|
226
|
+
return allowed;
|
|
227
|
+
}
|
|
163
228
|
}
|
|
164
229
|
return null;
|
|
165
230
|
}
|
package/package.json
CHANGED