NakliTechie commited on
Commit
90ff48b
·
1 Parent(s): 47563a3

Sync from GitHub 2026-10-31T08:26:02Z

Browse files
Files changed (1) hide show
  1. index.html +65 -14
index.html CHANGED
@@ -8728,8 +8728,11 @@ self.onmessage = async (e) => {
8728
  let ai = 0;
8729
  const msgs = [];
8730
  for (const m of (d.messages || [])) {
8731
- // Role 'tool' arrives only for toolFormat 'arg-key' (Flint), whose engine renders it as <|observation|>.
 
8732
  const role = (m.role === 'system' || m.role === 'assistant' || m.role === 'tool') ? m.role : 'user';
 
 
8733
  if (vision && Array.isArray(m.content) && m.content.some((b) => b && (b.type === 'image' || (media && b.type === 'audio')))) {
8734
  const parts = [];
8735
  for (const b of m.content) {
@@ -8747,7 +8750,7 @@ self.onmessage = async (e) => {
8747
  const content = typeof m.content === 'string' ? m.content
8748
  : Array.isArray(m.content) ? m.content.filter((b) => b && b.type === 'text').map((b) => b.text).join('\n')
8749
  : String(m.content == null ? '' : m.content);
8750
- msgs.push({ role, content });
8751
  }
8752
  if ((msgs.length <= 1 || msgs.length < lastHistoryLen) && typeof model.reset === 'function') {
8753
  try { model.reset(); } catch (_) {}
@@ -8776,19 +8779,22 @@ self.onmessage = async (e) => {
8776
  // A reasoning-only model (LFM2.5 24B: alwaysThinks) opens <think> in every prompt, so its reply always starts as reasoning.
8777
  let inThink = (wantThink || model.alwaysThinks === true) && model.thinkInPrompt !== false;
8778
  if (inThink) post({ type: 'token', token: '<|think|>' });
8779
- let prev = '', skip = '', last = '';
8780
  try {
8781
  const stream = model.generate(msgs, {
8782
  maxNewTokens: cfg.max_new_tokens || cfg.max_tokens || 1024,
8783
  signal: abortController.signal,
8784
  enableThinking: wantThink,
8785
  ...(speech ? { speech: true } : {}),
 
8786
  });
8787
  let toolMarkup = false;
8788
  for await (const out of stream) {
8789
  if (stopFlag) break;
8790
  const full = out && typeof out.text === 'string' ? out.text : '';
8791
  last = full;
 
 
8792
  // The model's speech, until a tool call starts (the interleave writes its text before its sound, so the
8793
  // markup is never voiced); the page plays it.
8794
  if (speech && !toolMarkup && (full.includes('<tool_call>') || full.includes('<function='))) toolMarkup = true;
@@ -8815,6 +8821,7 @@ self.onmessage = async (e) => {
8815
  }
8816
  if (last.length > prev.length) post({ type: 'token', token: last.slice(prev.length) });
8817
  if (inThink) post({ type: 'token', token: '<|/think|>' });
 
8818
  post({ type: 'complete' });
8819
  } else if (d.type === 'stop') {
8820
  stopFlag = true;
@@ -16359,6 +16366,16 @@ self.addEventListener('message', async (e) => {
16359
  return stripToolCallTags(text.replace(/<(?:\|(?:t(?:o(?:o(?:l(?:_(?:c(?:a(?:l(?:l(?:_(?:s(?:t(?:a(?:r(?:t(?:\|)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?|t(?:o(?:o(?:l(?:_(?:c(?:a(?:l(?:l)?)?)?)?)?)?)?)?)?$/, ''));
16360
  }
16361
 
 
 
 
 
 
 
 
 
 
 
16362
  function saveChat() {
16363
  try {
16364
  // Strip blobs from messages for sessionStorage (only persist text)
@@ -16425,6 +16442,7 @@ self.addEventListener('message', async (e) => {
16425
  // The tool prompts and the reply parser live in src/tool-calls.js (DOM-free, vendored by NakliOS).
16426
  const {
16427
  _adapterRepairJSON,
 
16428
  _adapterBuildToolPrompt,
16429
  _adapterPyJson,
16430
  _adapterBuildQwenXmlToolPrompt,
@@ -16974,7 +16992,13 @@ self.addEventListener('message', async (e) => {
16974
  // Native Qwen3-Coder XML tool calls (model entry toolFormat 'qwen3-xml', in-browser only): the tool block
16975
  // goes into the system turn in the template's own words and tool results stay role 'tool', which the
16976
  // template renders as <tool_response>. Endpoints (incl. a router escalation) keep the JSON prompt.
16977
- const nativeXmlTools = !!(activeModel && activeModel.toolFormat === 'qwen3-xml')
 
 
 
 
 
 
16978
  && !(LocalMind.runtime.isEndpoint && LocalMind.runtime.isEndpoint());
16979
  // Tools through the model's own chat template (model entry toolFormat 'template', Transformers.js worker only:
16980
  // LFM3): the worker passes them to apply_chat_template, so the model sees the tool block it was trained on;
@@ -17030,6 +17054,9 @@ self.addEventListener('message', async (e) => {
17030
  // placeholder blocks, extract binary data into attData.
17031
  const processed = await _adapterProcessContentBlocks(messages);
17032
  chatMessages = processed.chatMessages.map(m => {
 
 
 
17033
  if (m.role === 'tool') {
17034
  // Tool-role messages don't carry media; collapse via
17035
  // the same translation as the text path.
@@ -17059,6 +17086,7 @@ self.addEventListener('message', async (e) => {
17059
  content = String(content == null ? '' : content);
17060
  }
17061
  if (m.role === 'tool') {
 
17062
  if (nativeXmlTools || templateTools || argKeyTools) return { role: 'tool', name: m.name || 'tool', content };
17063
  const toolPayload = (() => {
17064
  try { return JSON.parse(content); } catch { return content; }
@@ -17068,7 +17096,7 @@ self.addEventListener('message', async (e) => {
17068
  content: _adapterFormatToolResultMessage(m.name || 'tool', toolPayload),
17069
  };
17070
  }
17071
- return { role: m.role, content };
17072
  });
17073
  }
17074
 
@@ -17084,7 +17112,7 @@ self.addEventListener('message', async (e) => {
17084
  }
17085
  const hint = toolsSwitchedOffHint(tools);
17086
  if (hint) userSysContent = userSysContent ? userSysContent + '\n\n' + hint : hint;
17087
- if (templateTools) {
17088
  genConfig.tools = tools.map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: t.parameters } }));
17089
  if (userSysContent) chatMessages = [{ role: 'system', content: userSysContent }].concat(chatMessages);
17090
  } else {
@@ -17101,6 +17129,7 @@ self.addEventListener('message', async (e) => {
17101
  return;
17102
  }
17103
 
 
17104
  const inferencePromise = runInference({
17105
  chatMessages,
17106
  attData: attDataForWorker,
@@ -17129,6 +17158,7 @@ self.addEventListener('message', async (e) => {
17129
  completionTokens++;
17130
  iter.push({ type: 'token', text: token });
17131
  },
 
17132
  };
17133
  },
17134
  });
@@ -17142,7 +17172,19 @@ self.addEventListener('message', async (e) => {
17142
  // tools — otherwise the same model output is just plain
17143
  // text and we don't want phantom calls from regex hits.
17144
  let reason = aborted ? 'abort' : 'stop';
17145
- if (toolsEnabled && !aborted && typeof fullText === 'string' && fullText.length) {
 
 
 
 
 
 
 
 
 
 
 
 
17146
  const { toolCalls } = _adapterParseToolCalls(fullText, knownToolNames, tools);
17147
  if (toolCalls.length > 0) {
17148
  reason = 'tool_calls';
@@ -19174,6 +19216,13 @@ self.addEventListener('message', async (e) => {
19174
  }
19175
  }
19176
 
 
 
 
 
 
 
 
19177
  else if (data.type === 'complete') {
19178
  // If agentic loop is awaiting, resolve promise (loop manages state)
19179
  if (generateResolve) {
@@ -20429,13 +20478,13 @@ self.addEventListener('message', async (e) => {
20429
  stepResults.push({ tc, toolResult });
20430
  }
20431
 
20432
- loopMessages.push({ role: 'assistant', content: turn.fullText });
20433
  for (const { tc, toolResult } of stepResults) {
20434
  // Crush oversized tool output (scraped pages, search hits, logs, RAG)
20435
  // before it enters the context — the choke point for every tool.
20436
  let payload = JSON.stringify(toolResult);
20437
  if (tokenSaverOn()) payload = compressContext(payload, toolResultBudget());
20438
- loopMessages.push({ role: 'tool', name: tc.name, content: payload });
20439
  }
20440
 
20441
  if (iter === STEP_MAX_ITER - 1) {
@@ -21032,9 +21081,9 @@ self.addEventListener('message', async (e) => {
21032
 
21033
  // Append the assistant turn once (with raw tags so the model sees
21034
  // its own format), then all tool-role results in order.
21035
- loopMessages.push({ role: 'assistant', content: turn.fullText });
21036
  for (const { tc, toolResult } of toolResults) {
21037
- loopMessages.push({ role: 'tool', name: tc.name, content: JSON.stringify(toolResult) });
21038
  }
21039
 
21040
  // Reset streaming state for next generation pass
@@ -24304,8 +24353,10 @@ export class AnyformerModel {
24304
  // </think> first appears, the text up to it goes out, then the same text again with token THINK_CLOSE (an empty
24305
  // delta), so the worker closes the thought with no text lost. With speech (LFM3-Omni) a chunk also carries `audio`,
24306
  // the new 24 kHz PCM since the last chunk, and `sampleRate`.
24307
- async *generate(messages, { maxNewTokens = 1024, signal, enableThinking = false, speech = false } = {}) {
24308
- const opts = { maxNewTokens, signal, thinking: enableThinking, ...(speech ? { speech: true } : {}) };
 
 
24309
  if (this.think) {
24310
  for await (const c of this.session.generate(messages, opts)) yield c;
24311
  return;
@@ -24317,7 +24368,7 @@ export class AnyformerModel {
24317
  // The worker posts no text on its close event, so the thought's last text goes out first.
24318
  if (at >= 0 && !closed) { closed = true; yield { text: raw.slice(0, at) }; yield { text: raw.slice(0, at), token: THINK_CLOSE }; }
24319
  const text = at >= 0 ? raw.slice(0, at) + raw.slice(at + '</think>'.length).replace(/^\n+/, '') : raw;
24320
- yield { text, tokens: c.tokens, tokPerSec: c.tokPerSec, ...(c.audio ? { audio: c.audio, sampleRate: c.sampleRate } : {}) };
24321
  }
24322
  }
24323
 
 
8728
  let ai = 0;
8729
  const msgs = [];
8730
  for (const m of (d.messages || [])) {
8731
+ // Role 'tool' arrives for toolFormat 'arg-key' (Flint), whose engine renders it as <|observation|>, and for anyformer
8732
+ // tool turns, which keep tool_calls / tool_call_id for anyformer to write in the model family's format.
8733
  const role = (m.role === 'system' || m.role === 'assistant' || m.role === 'tool') ? m.role : 'user';
8734
+ const toolFields = { ...(Array.isArray(m.tool_calls) && m.tool_calls.length ? { tool_calls: m.tool_calls } : {}),
8735
+ ...(m.tool_call_id ? { tool_call_id: m.tool_call_id } : {}), ...(role === 'tool' && m.name ? { name: m.name } : {}) };
8736
  if (vision && Array.isArray(m.content) && m.content.some((b) => b && (b.type === 'image' || (media && b.type === 'audio')))) {
8737
  const parts = [];
8738
  for (const b of m.content) {
 
8750
  const content = typeof m.content === 'string' ? m.content
8751
  : Array.isArray(m.content) ? m.content.filter((b) => b && b.type === 'text').map((b) => b.text).join('\n')
8752
  : String(m.content == null ? '' : m.content);
8753
+ msgs.push({ role, content, ...toolFields });
8754
  }
8755
  if ((msgs.length <= 1 || msgs.length < lastHistoryLen) && typeof model.reset === 'function') {
8756
  try { model.reset(); } catch (_) {}
 
8779
  // A reasoning-only model (LFM2.5 24B: alwaysThinks) opens <think> in every prompt, so its reply always starts as reasoning.
8780
  let inThink = (wantThink || model.alwaysThinks === true) && model.thinkInPrompt !== false;
8781
  if (inThink) post({ type: 'token', token: '<|think|>' });
8782
+ let prev = '', skip = '', last = '', toolCalls = null;
8783
  try {
8784
  const stream = model.generate(msgs, {
8785
  maxNewTokens: cfg.max_new_tokens || cfg.max_tokens || 1024,
8786
  signal: abortController.signal,
8787
  enableThinking: wantThink,
8788
  ...(speech ? { speech: true } : {}),
8789
+ ...(Array.isArray(cfg.tools) && cfg.tools.length ? { tools: cfg.tools } : {}),
8790
  });
8791
  let toolMarkup = false;
8792
  for await (const out of stream) {
8793
  if (stopFlag) break;
8794
  const full = out && typeof out.text === 'string' ? out.text : '';
8795
  last = full;
8796
+ // anyformer's parsed calls (the latest chunk that holds them); the page uses these instead of parsing the text.
8797
+ if (out && Array.isArray(out.toolCalls) && out.toolCalls.length) toolCalls = out.toolCalls;
8798
  // The model's speech, until a tool call starts (the interleave writes its text before its sound, so the
8799
  // markup is never voiced); the page plays it.
8800
  if (speech && !toolMarkup && (full.includes('<tool_call>') || full.includes('<function='))) toolMarkup = true;
 
8821
  }
8822
  if (last.length > prev.length) post({ type: 'token', token: last.slice(prev.length) });
8823
  if (inThink) post({ type: 'token', token: '<|/think|>' });
8824
+ if (toolCalls) post({ type: 'tool_calls', toolCalls });
8825
  post({ type: 'complete' });
8826
  } else if (d.type === 'stop') {
8827
  stopFlag = true;
 
16366
  return stripToolCallTags(text.replace(/<(?:\|(?:t(?:o(?:o(?:l(?:_(?:c(?:a(?:l(?:l(?:_(?:s(?:t(?:a(?:r(?:t(?:\|)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?|t(?:o(?:o(?:l(?:_(?:c(?:a(?:l(?:l)?)?)?)?)?)?)?)?)?$/, ''));
16367
  }
16368
 
16369
+ // The assistant turn an agent loop keeps in its history. Calls anyformer parsed (tool_call events marked native) go in
16370
+ // tool_calls, and the content is the reply without reasoning or call markup, so anyformer writes the calls back in the
16371
+ // model family's format; other paths keep the raw text, which their parser and prompt read again.
16372
+ function assistantTurnMessage(fullText, toolCalls) {
16373
+ const native = (toolCalls || []).filter((tc) => tc && tc.native);
16374
+ if (!native.length) return { role: 'assistant', content: fullText };
16375
+ const content = streamDisplayText(String(fullText || '').replace(/<\|think\|>[\s\S]*?(?:<\|\/think\|>|$)/g, '')) || '';
16376
+ return { role: 'assistant', content, tool_calls: native.map((tc) => ({ id: tc.id, type: 'function', function: { name: tc.name, arguments: JSON.stringify(tc.arguments || {}) } })) };
16377
+ }
16378
+
16379
  function saveChat() {
16380
  try {
16381
  // Strip blobs from messages for sessionStorage (only persist text)
 
16442
  // The tool prompts and the reply parser live in src/tool-calls.js (DOM-free, vendored by NakliOS).
16443
  const {
16444
  _adapterRepairJSON,
16445
+ _adapterResolveToolName,
16446
  _adapterBuildToolPrompt,
16447
  _adapterPyJson,
16448
  _adapterBuildQwenXmlToolPrompt,
 
16992
  // Native Qwen3-Coder XML tool calls (model entry toolFormat 'qwen3-xml', in-browser only): the tool block
16993
  // goes into the system turn in the template's own words and tool results stay role 'tool', which the
16994
  // template renders as <tool_response>. Endpoints (incl. a router escalation) keep the JSON prompt.
16995
+ // Tools through anyformer (backend 'anyformer-*', anyformer 0.4.20+): generate({ tools }) writes them into the prompt
16996
+ // in the model family's format and returns parsed calls, which the SSD worker posts as 'tool_calls'; tool results and
16997
+ // the assistant's calls stay structured (tool_calls / tool_call_id). Not Flint (toolFormat 'arg-key': anyformer does
16998
+ // not parse <arg_key> calls) and not endpoints. Chirag, 2026-10-31: "switch anyformer models to its parsed tool calls".
16999
+ const anyformerTools = !!(activeModel && /^anyformer-/.test(activeModel.backend || '') && activeModel.toolFormat !== 'arg-key')
17000
+ && !(LocalMind.runtime.isEndpoint && LocalMind.runtime.isEndpoint());
17001
+ const nativeXmlTools = !!(activeModel && activeModel.toolFormat === 'qwen3-xml') && !anyformerTools
17002
  && !(LocalMind.runtime.isEndpoint && LocalMind.runtime.isEndpoint());
17003
  // Tools through the model's own chat template (model entry toolFormat 'template', Transformers.js worker only:
17004
  // LFM3): the worker passes them to apply_chat_template, so the model sees the tool block it was trained on;
 
17054
  // placeholder blocks, extract binary data into attData.
17055
  const processed = await _adapterProcessContentBlocks(messages);
17056
  chatMessages = processed.chatMessages.map(m => {
17057
+ if (m.role === 'tool' && anyformerTools) {
17058
+ return { role: 'tool', name: m.name || 'tool', content: typeof m.content === 'string' ? m.content : '', ...(m.tool_call_id ? { tool_call_id: m.tool_call_id } : {}) };
17059
+ }
17060
  if (m.role === 'tool') {
17061
  // Tool-role messages don't carry media; collapse via
17062
  // the same translation as the text path.
 
17086
  content = String(content == null ? '' : content);
17087
  }
17088
  if (m.role === 'tool') {
17089
+ if (anyformerTools) return { role: 'tool', name: m.name || 'tool', content, ...(m.tool_call_id ? { tool_call_id: m.tool_call_id } : {}) };
17090
  if (nativeXmlTools || templateTools || argKeyTools) return { role: 'tool', name: m.name || 'tool', content };
17091
  const toolPayload = (() => {
17092
  try { return JSON.parse(content); } catch { return content; }
 
17096
  content: _adapterFormatToolResultMessage(m.name || 'tool', toolPayload),
17097
  };
17098
  }
17099
+ return { role: m.role, content, ...(anyformerTools && Array.isArray(m.tool_calls) && m.tool_calls.length ? { tool_calls: m.tool_calls } : {}) };
17100
  });
17101
  }
17102
 
 
17112
  }
17113
  const hint = toolsSwitchedOffHint(tools);
17114
  if (hint) userSysContent = userSysContent ? userSysContent + '\n\n' + hint : hint;
17115
+ if (templateTools || anyformerTools) {
17116
  genConfig.tools = tools.map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: t.parameters } }));
17117
  if (userSysContent) chatMessages = [{ role: 'system', content: userSysContent }].concat(chatMessages);
17118
  } else {
 
17129
  return;
17130
  }
17131
 
17132
+ let nativeCalls = null; // anyformer's parsed calls for this reply (anyformerTools only)
17133
  const inferencePromise = runInference({
17134
  chatMessages,
17135
  attData: attDataForWorker,
 
17158
  completionTokens++;
17159
  iter.push({ type: 'token', text: token });
17160
  },
17161
+ onToolCalls: (calls) => { nativeCalls = calls; },
17162
  };
17163
  },
17164
  });
 
17172
  // tools — otherwise the same model output is just plain
17173
  // text and we don't want phantom calls from regex hits.
17174
  let reason = aborted ? 'abort' : 'stop';
17175
+ if (toolsEnabled && !aborted && anyformerTools && Array.isArray(nativeCalls) && nativeCalls.length) {
17176
+ // anyformer's calls; a name that is not one of this turn's tools never runs. native marks them so the agent
17177
+ // loop records them as tool_calls on the assistant turn.
17178
+ for (const c of nativeCalls) {
17179
+ const fn = (c && c.function) || {};
17180
+ const name = _adapterResolveToolName(fn.name, knownToolNames);
17181
+ if (name == null) continue;
17182
+ let args = {};
17183
+ try { args = typeof fn.arguments === 'string' ? JSON.parse(fn.arguments || '{}') : (fn.arguments || {}); } catch {}
17184
+ reason = 'tool_calls';
17185
+ iter.push({ type: 'tool_call', id: c.id || 'tc-' + Math.random().toString(36).slice(2, 10), name, arguments: args, native: true });
17186
+ }
17187
+ } else if (toolsEnabled && !aborted && typeof fullText === 'string' && fullText.length) {
17188
  const { toolCalls } = _adapterParseToolCalls(fullText, knownToolNames, tools);
17189
  if (toolCalls.length > 0) {
17190
  reason = 'tool_calls';
 
19216
  }
19217
  }
19218
 
19219
+ // anyformer's parsed tool calls for this reply (SSD worker), before its 'complete'.
19220
+ else if (data.type === 'tool_calls') {
19221
+ if (currentInferenceContext && typeof currentInferenceContext.onToolCalls === 'function') {
19222
+ try { currentInferenceContext.onToolCalls(data.toolCalls); } catch (e) { console.error('onToolCalls threw:', e); }
19223
+ }
19224
+ }
19225
+
19226
  else if (data.type === 'complete') {
19227
  // If agentic loop is awaiting, resolve promise (loop manages state)
19228
  if (generateResolve) {
 
20478
  stepResults.push({ tc, toolResult });
20479
  }
20480
 
20481
+ loopMessages.push(assistantTurnMessage(turn.fullText, turn.toolCalls));
20482
  for (const { tc, toolResult } of stepResults) {
20483
  // Crush oversized tool output (scraped pages, search hits, logs, RAG)
20484
  // before it enters the context — the choke point for every tool.
20485
  let payload = JSON.stringify(toolResult);
20486
  if (tokenSaverOn()) payload = compressContext(payload, toolResultBudget());
20487
+ loopMessages.push({ role: 'tool', name: tc.name, content: payload, ...(tc.native ? { tool_call_id: tc.id } : {}) });
20488
  }
20489
 
20490
  if (iter === STEP_MAX_ITER - 1) {
 
21081
 
21082
  // Append the assistant turn once (with raw tags so the model sees
21083
  // its own format), then all tool-role results in order.
21084
+ loopMessages.push(assistantTurnMessage(turn.fullText, turn.toolCalls));
21085
  for (const { tc, toolResult } of toolResults) {
21086
+ loopMessages.push({ role: 'tool', name: tc.name, content: JSON.stringify(toolResult), ...(tc.native ? { tool_call_id: tc.id } : {}) });
21087
  }
21088
 
21089
  // Reset streaming state for next generation pass
 
24353
  // </think> first appears, the text up to it goes out, then the same text again with token THINK_CLOSE (an empty
24354
  // delta), so the worker closes the thought with no text lost. With speech (LFM3-Omni) a chunk also carries `audio`,
24355
  // the new 24 kHz PCM since the last chunk, and `sampleRate`.
24356
+ // tools (OpenAI-style, anyformer 0.4.20+): anyformer writes them into the prompt in the model family's format, and a
24357
+ // chunk whose text holds complete calls carries them as toolCalls (OpenAI tool_calls), its text without them.
24358
+ async *generate(messages, { maxNewTokens = 1024, signal, enableThinking = false, speech = false, tools = null } = {}) {
24359
+ const opts = { maxNewTokens, signal, thinking: enableThinking, ...(speech ? { speech: true } : {}), ...(tools && tools.length ? { tools } : {}) };
24360
  if (this.think) {
24361
  for await (const c of this.session.generate(messages, opts)) yield c;
24362
  return;
 
24368
  // The worker posts no text on its close event, so the thought's last text goes out first.
24369
  if (at >= 0 && !closed) { closed = true; yield { text: raw.slice(0, at) }; yield { text: raw.slice(0, at), token: THINK_CLOSE }; }
24370
  const text = at >= 0 ? raw.slice(0, at) + raw.slice(at + '</think>'.length).replace(/^\n+/, '') : raw;
24371
+ yield { text, tokens: c.tokens, tokPerSec: c.tokPerSec, ...(c.audio ? { audio: c.audio, sampleRate: c.sampleRate } : {}), ...(c.toolCalls ? { toolCalls: c.toolCalls } : {}) };
24372
  }
24373
  }
24374