File size: 23,549 Bytes
059f0de
 
 
 
53f8186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
 
53f8186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53f8186
059f0de
 
 
53f8186
 
 
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a41b26e
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53f8186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
 
 
53f8186
 
059f0de
53f8186
059f0de
 
 
 
 
53f8186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
 
 
 
53f8186
059f0de
 
 
 
 
 
 
 
53f8186
 
 
059f0de
 
 
 
 
 
53f8186
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53f8186
 
 
 
 
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a41b26e
059f0de
 
 
 
 
53f8186
 
059f0de
 
 
53f8186
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
53f8186
 
 
 
 
 
 
 
059f0de
53f8186
 
 
 
 
059f0de
 
 
 
53f8186
059f0de
 
53f8186
 
059f0de
 
 
 
53f8186
059f0de
 
53f8186
059f0de
 
 
 
 
 
53f8186
059f0de
 
 
 
53f8186
 
059f0de
53f8186
059f0de
 
 
53f8186
 
059f0de
 
53f8186
059f0de
 
53f8186
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b71b4b
059f0de
53f8186
059f0de
6b71b4b
059f0de
 
 
 
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
059f0de
 
53f8186
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
import { Agent } from "@earendil-works/pi-agent-core";
import { createAssistantMessageEventStream, Type } from "@earendil-works/pi-ai";
import { env, pipeline } from "@huggingface/transformers";

const LOCAL_MODELS = {
  qwen25coder: {
    id: "onnx-community/Qwen2.5-Coder-0.5B-Instruct",
    name: "Qwen2.5 Coder 0.5B ONNX",
    promptStyle: "qwen",
  },
  qwen: {
    id: "onnx-community/Qwen3-0.6B-ONNX",
    name: "Qwen3 0.6B ONNX",
    promptStyle: "qwen",
  },
  minicpm: {
    id: "Mike0021/MiniCPM5-1B-ONNX-Web",
    name: "MiniCPM5-1B ONNX Web",
    promptStyle: "json",
  },
};

const DEFAULT_LOCAL_MODEL_KEY = "qwen25coder";

function getLocalModel(key) {
  return LOCAL_MODELS[key] || LOCAL_MODELS[DEFAULT_LOCAL_MODEL_KEY];
}

function createLocalModelMetadata(model) {
  return {
    id: model.id,
    name: model.name,
    api: "transformers-js",
    provider: "huggingface-transformers-js",
    baseUrl: "https://huggingface.co",
    reasoning: false,
    input: ["text"],
    cost: {
      input: 0,
      output: 0,
      cacheRead: 0,
      cacheWrite: 0,
    },
    contextWindow: 131072,
    maxTokens: 8192,
  };
}

const EMPTY_USAGE = {
  input: 0,
  output: 0,
  cacheRead: 0,
  cacheWrite: 0,
  totalTokens: 0,
  cost: {
    input: 0,
    output: 0,
    cacheRead: 0,
    cacheWrite: 0,
    total: 0,
  },
};

function textFromContent(content) {
  if (typeof content === "string") return content;
  if (!Array.isArray(content)) return "";
  return content
    .filter((part) => part.type === "text")
    .map((part) => part.text)
    .join("\n");
}

function now() {
  return Date.now();
}

function createMessage(content, stopReason = "stop", model = getLocalModel(DEFAULT_LOCAL_MODEL_KEY)) {
  return {
    role: "assistant",
    content,
    api: "transformers-js",
    provider: "huggingface-transformers-js",
    model: model.id,
    usage: EMPTY_USAGE,
    stopReason,
    timestamp: now(),
  };
}

function stringifyToolResult(message) {
  const text = textFromContent(message.content);
  return `${message.toolName}(${message.toolCallId}) ${message.isError ? "failed" : "succeeded"}:\n${text}`;
}

function buildPrompt(context) {
  const tools = (context.tools || []).map((tool) => ({
    name: tool.name,
    description: tool.description,
    parameters: tool.parameters,
  }));
  const transcript = context.messages
    .slice(-8)
    .map((message) => {
      if (message.role === "toolResult") return `TOOL_RESULT:\n${stringifyToolResult(message)}`;
      return `${message.role.toUpperCase()}:\n${textFromContent(message.content)}`;
    })
    .join("\n\n");

  return `You are running as a pi Agent inside a browser-only app. The sandbox is a WebContainer: it has a virtual filesystem and can spawn browser-contained Node.js processes.

Use tools by returning strict JSON only. Do not use markdown.

To call tools:
{"toolCalls":[{"tool":"write_file","args":{"path":"hello.js","content":"console.log(2 + 2)\\n"}},{"tool":"run_command","args":{"command":"node","args":["hello.js"]}}]}

To answer the user after tool results:
{"final":"Short answer that explains what happened."}

Available tools:
${JSON.stringify(tools, null, 2)}

Conversation:
${transcript}

Return JSON now.`;
}

function buildQwenMessages(context) {
  const lastUserText = [...context.messages]
    .reverse()
    .find((message) => message.role === "user");
  const taskText = textFromContent(lastUserText?.content || "");
  const transcript = context.messages
    .slice(-8)
    .map((message) => {
      if (message.role === "toolResult") return `TOOL_RESULT ${message.toolName}:\n${textFromContent(message.content)}`;
      return `${message.role.toUpperCase()}:\n${textFromContent(message.content)}`;
    })
    .join("\n\n");
  const lastMessage = context.messages[context.messages.length - 1];

  if (lastMessage?.role === "toolResult") {
    return [
      {
        role: "system",
        content: "You are Pi Web Agent. Give a concise final answer from the sandbox tool results. Do not emit JSON.",
      },
      {
        role: "user",
        content: `Conversation and tool results:\n${transcript}\n\nFinal answer:`,
      },
    ];
  }

  const loweredTask = taskText.toLowerCase();
  let example = `<plan>
<write_file path="app.js">
console.log(2 * 2);
</write_file>
<run_command command="node" args="app.js" timeoutMs="10000"></run_command>
</plan>`;
  if (/\b(npm|install|package|dependency)\b/.test(loweredTask)) {
    example = `<plan>
<write_file path="pad.mjs">
import leftPad from 'left-pad';
console.log('padded: ' + leftPad('5', 3, '0'));
</write_file>
<run_command command="npm" args="install left-pad@1.3.0" timeoutMs="120000"></run_command>
<run_command command="node" args="pad.mjs" timeoutMs="10000"></run_command>
</plan>`;
  } else if (/\b(src\/|import|export|module|multi[- ]?file)\b/.test(loweredTask)) {
    example = `<plan>
<write_file path="src/util.mjs">
export function add(a, b) {
  return a + b;
}
</write_file>
<write_file path="test.mjs">
import { add } from './src/util.mjs';
console.log('sum: ' + add(2, 3));
</write_file>
<run_command command="node" args="test.mjs" timeoutMs="10000"></run_command>
</plan>`;
  }

  return [
    {
      role: "system",
      content:
        'Return only a complete <plan>...</plan>. No markdown or prose. Inside the plan use <write_file path="...">code</write_file>, <run_command command="..." args="..." timeoutMs="..."></run_command>, <read_file path="..."></read_file>, and <list_files path="..."></list_files>. Every task that says run must end with a run_command tag. Multi-file tasks must write every requested file before run_command. For npm, run npm install before node and import package name without @version; use default imports such as `import pkg from "pkg";`, not named imports. Use exact filenames. Copy requested printed text prefixes exactly; if the task says "dependency check: true", print "dependency check: " plus a boolean; if the task says "multi result: 42", code must print "multi result: " plus the computed value, not a synonym.',
    },
    {
      role: "user",
      content: `Example output:
${example}

Conversation:
${transcript}

Complete plan:`,
    },
  ];
}

function generatedTextFromResult(result) {
  const generated = result?.[0]?.generated_text;
  if (Array.isArray(generated)) return generated.at(-1)?.content ?? "";
  return generated ?? "";
}

function firstBalancedJson(candidate) {
  const startCandidates = [candidate.indexOf("{"), candidate.indexOf("[")].filter((index) => index >= 0);
  if (startCandidates.length === 0) return null;
  const start = Math.min(...startCandidates);
  const stack = [];
  let inString = false;
  let escaping = false;

  for (let index = start; index < candidate.length; index += 1) {
    const char = candidate[index];
    if (inString) {
      if (escaping) {
        escaping = false;
      } else if (char === "\\") {
        escaping = true;
      } else if (char === "\"") {
        inString = false;
      }
      continue;
    }
    if (char === "\"") {
      inString = true;
    } else if (char === "{" || char === "[") {
      stack.push(char);
    } else if (char === "}" || char === "]") {
      const expected = char === "}" ? "{" : "[";
      if (stack.pop() !== expected) return null;
      if (stack.length === 0) return candidate.slice(start, index + 1);
    }
  }
  return null;
}

function extractJsonPayload(text) {
  const trimmed = String(text || "").trim();
  const fence = trimmed.match(/```(?:json)?\s*([\s\S]*?)```/i);
  const candidate = fence ? fence[1].trim() : trimmed;
  const jsonText = firstBalancedJson(candidate);
  if (!jsonText) return null;
  try {
    return JSON.parse(jsonText);
  } catch {
    return null;
  }
}

function decodeAttribute(value = "") {
  return String(value)
    .replace(/&quot;/g, "\"")
    .replace(/&apos;/g, "'")
    .replace(/&lt;/g, "<")
    .replace(/&gt;/g, ">")
    .replace(/&amp;/g, "&");
}

function parseAttributes(source = "") {
  const attributes = {};
  const pattern = /([A-Za-z][\w-]*)\s*=\s*"([^"]*)"/g;
  let match = pattern.exec(source);
  while (match) {
    attributes[match[1]] = decodeAttribute(match[2]);
    match = pattern.exec(source);
  }
  return attributes;
}

function trimCodeBlock(text) {
  return String(text || "").replace(/^\n/, "").replace(/\n?$/, "\n");
}

function splitCommandLine(value) {
  const parts = [];
  let current = "";
  let quote = "";
  let escaping = false;
  for (const char of String(value || "")) {
    if (escaping) {
      current += char;
      escaping = false;
      continue;
    }
    if (char === "\\") {
      escaping = true;
      continue;
    }
    if (quote) {
      if (char === quote) {
        quote = "";
      } else {
        current += char;
      }
      continue;
    }
    if (char === "\"" || char === "'") {
      quote = char;
    } else if (/\s/.test(char)) {
      if (current) {
        parts.push(current);
        current = "";
      }
    } else {
      current += char;
    }
  }
  if (current) parts.push(current);
  return parts;
}

function normalizeCommandArgs(args = {}) {
  const normalized = { ...args };
  if (typeof normalized.args === "string") normalized.args = splitCommandLine(normalized.args);
  if (!Array.isArray(normalized.args)) normalized.args = [];
  if (typeof normalized.command === "string" && normalized.command.includes(" ") && normalized.args.length === 0) {
    const [command, ...rest] = splitCommandLine(normalized.command);
    normalized.command = command;
    normalized.args = rest;
  }
  if (normalized.timeoutMs !== undefined) normalized.timeoutMs = Number(normalized.timeoutMs);
  return normalized;
}

function lastUserText(context) {
  const message = [...context.messages].reverse().find((item) => item.role === "user");
  return textFromContent(message?.content || "");
}

function packageNameFromSpecifier(specifier) {
  const value = String(specifier || "");
  const versionAt = value.lastIndexOf("@");
  return versionAt > 0 ? value.slice(0, versionAt) : value;
}

function requestedPackageSpecifiers(context) {
  const text = lastUserText(context);
  const specs = [];
  const pattern = /\b(?:install|package|dependency)\b[^\n,;]*?\s((?:@[\w.-]+\/)?[\w.-]+@[0-9][^\s,;)]*)/gi;
  let match = pattern.exec(text);
  while (match) {
    specs.push(match[1]);
    match = pattern.exec(text);
  }
  return specs;
}

function isRunCommand(call, command) {
  if (call.name !== "run_command") return false;
  const args = normalizeCommandArgs(call.arguments || {});
  return args.command === command;
}

function repairToolCalls(toolCalls, context) {
  const repaired = toolCalls.map((call) => ({ ...call, arguments: { ...(call.arguments || {}) } }));
  const userText = lastUserText(context).toLowerCase();
  const packageSpecifiers = requestedPackageSpecifiers(context);

  for (const specifier of packageSpecifiers) {
    const packageName = packageNameFromSpecifier(specifier);
    const hasInstall = repaired.some((call) => {
      if (!isRunCommand(call, "npm")) return false;
      const args = normalizeCommandArgs(call.arguments || {}).args;
      return args.includes("install") && args.includes(specifier);
    });
    if (!hasInstall) {
      const firstNodeRun = repaired.findIndex((call) => isRunCommand(call, "node"));
      const installCall = {
        type: "toolCall",
        id: `tool-${now()}-repair-install`,
        name: "run_command",
        arguments: {
          command: "npm",
          args: ["install", specifier],
          timeoutMs: 120000,
        },
      };
      repaired.splice(firstNodeRun >= 0 ? firstNodeRun : repaired.length, 0, installCall);
    }

    if (packageName === "is-number" && userText.includes("dependency check")) {
      const file = repaired.find((call) => call.name === "write_file" && String(call.arguments?.path || "").endsWith(".mjs"));
      if (file) {
        file.arguments.content = "import isNumber from 'is-number';\nconsole.log('dependency check: ' + isNumber(42));\n";
      }
    }
  }

  return repaired;
}

function parseTaggedToolCalls(text) {
  const toolCalls = [];
  const source = String(text || "");
  const writePattern = /<write_file\b([^>]*)>([\s\S]*?)<\/write_file>/gi;
  let write = writePattern.exec(source);
  while (write) {
    const attributes = parseAttributes(write[1]);
    if (attributes.path) {
      toolCalls.push({
        tool: "write_file",
        args: {
          path: attributes.path,
          content: trimCodeBlock(write[2]),
        },
      });
    }
    write = writePattern.exec(source);
  }

  const emptyTagPattern = /<(run_command|read_file|list_files)\b([^>]*)>(?:\s*<\/\1>)?/gi;
  let tag = emptyTagPattern.exec(source);
  while (tag) {
    const tool = tag[1];
    const attributes = parseAttributes(tag[2]);
    if (tool === "run_command" && attributes.command) {
      toolCalls.push({
        tool,
        args: normalizeCommandArgs({
          command: attributes.command,
          args: attributes.args || [],
          timeoutMs: attributes.timeoutMs,
        }),
      });
    } else if ((tool === "read_file" || tool === "list_files") && attributes.path) {
      toolCalls.push({ tool, args: { path: attributes.path } });
    }
    tag = emptyTagPattern.exec(source);
  }

  return toolCalls;
}

function normalizeToolCalls(payload, generated = "") {
  const rawCalls = [];
  function collect(value) {
    if (!value) return;
    if (Array.isArray(value)) {
      for (const item of value) collect(item);
      return;
    }
    if (Array.isArray(value.toolCalls)) collect(value.toolCalls);
    if (Array.isArray(value.tools)) collect(value.tools);
    if (Array.isArray(value.actions)) collect(value.actions);
    if (value.tool || value.name) rawCalls.push(value);
  }
  collect(payload);
  if (rawCalls.length === 0) rawCalls.push(...parseTaggedToolCalls(generated));
  return rawCalls
    .map((call, index) => ({
      type: "toolCall",
      id: `tool-${now()}-${index}`,
      name: String(call.tool || call.name || ""),
      arguments: call.tool === "run_command" || call.name === "run_command" ? normalizeCommandArgs(call.args || call.arguments || {}) : call.args || call.arguments || {},
    }))
    .filter((call) => call.name);
}

function normalizeFinalText(payload, fallback) {
  if (payload && typeof payload.final === "string") return payload.final;
  if (payload && typeof payload.message === "string") return payload.message;
  if (payload && typeof payload.answer === "string") return payload.answer;
  return String(fallback || "")
    .replace(/```(?:\w+)?/g, "")
    .trim() || "Done.";
}

function mockPlan(context) {
  const last = context.messages[context.messages.length - 1];
  if (last?.role === "toolResult") {
    return {
      final: summarizeToolResults(context),
    };
  }

  const userText = textFromContent(last?.content || "").toLowerCase();
  if (userText.includes("read")) {
    return {
      toolCalls: [{ tool: "read_file", args: { path: "hello.js" } }],
    };
  }
  if (userText.includes("list")) {
    return {
      toolCalls: [{ tool: "list_files", args: { path: "." } }],
    };
  }
  if (userText.includes("install") || userText.includes("dependency") || userText.includes("package")) {
    return {
      toolCalls: [
        {
          tool: "write_file",
          args: {
            path: "check-package.mjs",
            content: 'import isNumber from "is-number";\nconsole.log(`dependency check: ${isNumber(42)}`);\n',
          },
        },
        {
          tool: "run_command",
          args: {
            command: "npm",
            args: ["install", "is-number@7.0.0"],
            timeoutMs: 120000,
          },
        },
        {
          tool: "run_command",
          args: {
            command: "node",
            args: ["check-package.mjs"],
          },
        },
      ],
    };
  }
  return {
    toolCalls: [
      {
        tool: "write_file",
        args: {
          path: "hello.js",
          content: 'const value = 21 * 2;\nconsole.log(`pi sandbox result: ${value}`);\n',
        },
      },
      {
        tool: "run_command",
        args: {
          command: "node",
          args: ["hello.js"],
        },
      },
    ],
  };
}

function summarizeToolResults(context) {
  const results = context.messages.filter((message) => message.role === "toolResult").slice(-4).map(stringifyToolResult);
  return `The sandbox work completed.\n\n${results.join("\n\n")}`;
}

function emitFinal(stream, message, text = "") {
  stream.push({ type: "start", partial: { ...message, content: [{ type: "text", text: "" }] } });
  if (text) {
    const partial = { ...message, content: [{ type: "text", text }] };
    stream.push({ type: "text_start", contentIndex: 0, partial: { ...message, content: [{ type: "text", text: "" }] } });
    stream.push({ type: "text_delta", contentIndex: 0, delta: text, partial });
    stream.push({ type: "text_end", contentIndex: 0, content: text, partial });
  }
  if (message.stopReason === "error" || message.stopReason === "aborted") {
    stream.push({ type: "error", reason: message.stopReason, error: message });
  } else {
    stream.push({ type: "done", reason: message.stopReason, message });
  }
}

export function createPiAgent({ sandbox, modelMode, device, maxTokens, temperature, onModelStatus = () => {} }) {
  env.allowLocalModels = false;
  env.allowRemoteModels = true;
  env.backends.onnx.wasm.numThreads = Math.min(2, navigator.hardwareConcurrency || 2);

  let generatorPromise = null;
  let generatorKey = "";

  async function getGenerator() {
    const model = getLocalModel(modelMode());
    const key = `${model.id}:${device()}`;
    if (!generatorPromise || generatorKey !== key) {
      generatorKey = key;
      onModelStatus(`Loading ${device()}`);
      generatorPromise = pipeline("text-generation", model.id, {
        dtype: "q4",
        device: device(),
        progress_callback: (event) => {
          if (event.status === "progress") {
            onModelStatus(`${event.file} ${Math.round(event.progress)}%`);
          } else if (event.status) {
            onModelStatus(event.status);
          }
        },
      });
    }
    return generatorPromise;
  }

  async function releaseGenerator() {
    if (!generatorPromise) return;
    const generator = await generatorPromise.catch(() => null);
    generatorPromise = null;
    generatorKey = "";
    await generator?.dispose?.();
  }

  async function producePlan(context, signal) {
    const lastMessage = context.messages[context.messages.length - 1];
    if (lastMessage?.role === "toolResult") {
      return JSON.stringify({ final: summarizeToolResults(context) });
    }

    if (modelMode() === "mock") {
      return JSON.stringify(mockPlan(context));
    }

    const model = getLocalModel(modelMode());
    const generator = await getGenerator();
    if (signal?.aborted) throw new Error("Aborted");
    const input = model.promptStyle === "qwen" ? buildQwenMessages(context) : buildPrompt(context);
    const result = await generator(input, {
      max_new_tokens: Number(maxTokens()) || 128,
      temperature: Number(temperature()) || 0,
      do_sample: Number(temperature()) > 0,
      return_full_text: false,
      tokenizer_encode_kwargs: model.promptStyle === "qwen" ? { enable_thinking: false } : undefined,
    });
    onModelStatus("Model ready");
    return generatedTextFromResult(result);
  }

  function streamFn(_model, context, options = {}) {
    const stream = createAssistantMessageEventStream();
    queueMicrotask(async () => {
      try {
        const model = getLocalModel(modelMode());
        const generated = await producePlan(context, options.signal);
        const payload = extractJsonPayload(generated);
        const lastMessage = context.messages[context.messages.length - 1];
        const forceFinal = lastMessage?.role === "toolResult";
        const toolCalls = forceFinal ? [] : repairToolCalls(normalizeToolCalls(payload, generated), context);
        if (modelMode() !== "mock") await releaseGenerator();
        if (toolCalls.length > 0) {
          const message = createMessage([{ type: "text", text: "Using sandbox tools." }, ...toolCalls], "toolUse", model);
          emitFinal(stream, message, "Using sandbox tools.");
          return;
        }
        const text = normalizeFinalText(payload, generated);
        const message = createMessage([{ type: "text", text }], "stop", model);
        emitFinal(stream, message, text);
      } catch (error) {
        await releaseGenerator().catch(() => {});
        const text = error instanceof Error ? error.message : String(error);
        const message = {
          ...createMessage([{ type: "text", text }], options.signal?.aborted ? "aborted" : "error", getLocalModel(modelMode())),
          errorMessage: text,
        };
        emitFinal(stream, message, text);
      }
    });
    return stream;
  }

  const tools = [
    {
      name: "list_files",
      label: "List files",
      description: "List files in the sandbox workspace.",
      parameters: Type.Object({
        path: Type.Optional(Type.String()),
      }),
      execute: async (_id, args) => {
        const output = await sandbox.listFiles(args.path || ".");
        return { content: [{ type: "text", text: output }], details: { output } };
      },
    },
    {
      name: "read_file",
      label: "Read file",
      description: "Read a UTF-8 file from the sandbox workspace.",
      parameters: Type.Object({
        path: Type.String(),
      }),
      execute: async (_id, args) => {
        const output = await sandbox.readFile(args.path);
        return { content: [{ type: "text", text: output }], details: { path: args.path, output } };
      },
    },
    {
      name: "write_file",
      label: "Write file",
      description: "Create or replace a UTF-8 file inside the sandbox workspace.",
      parameters: Type.Object({
        path: Type.String(),
        content: Type.String(),
      }),
      execute: async (_id, args) => {
        const output = await sandbox.writeFile(args.path, args.content);
        return { content: [{ type: "text", text: output }], details: { path: args.path } };
      },
    },
    {
      name: "run_command",
      label: "Run command",
      description: "Spawn a process inside the browser-only WebContainer sandbox.",
      parameters: Type.Object({
        command: Type.String(),
        args: Type.Optional(Type.Array(Type.String())),
        timeoutMs: Type.Optional(Type.Number()),
      }),
      execute: async (_id, args) => {
        const result = await sandbox.runCommand(args.command, args.args || [], args.timeoutMs || 10000);
        const text = `$ ${result.command}\nexit ${result.exitCode}\n${result.output}`;
        return {
          content: [{ type: "text", text }],
          details: result,
        };
      },
      executionMode: "sequential",
    },
  ];

  const agent = new Agent({
    initialState: {
      model: createLocalModelMetadata(getLocalModel(modelMode())),
      systemPrompt:
        "You are Pi Web Agent. Use the sandbox tools for filesystem, npm dependency, or command tasks, then give concise results.",
      tools,
    },
    streamFn,
    toolExecution: "sequential",
  });

  agent.preloadModel = async () => {
    if (modelMode() === "mock") {
      onModelStatus("Deterministic test model");
      return;
    }
    await getGenerator();
    onModelStatus("Model ready");
  };

  return agent;
}

export { DEFAULT_LOCAL_MODEL_KEY, LOCAL_MODELS };