File size: 11,156 Bytes
21e6b9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Agent } from "@earendil-works/pi-agent-core";
import { createAssistantMessageEventStream, Type } from "@earendil-works/pi-ai";
import { env, pipeline } from "@huggingface/transformers";

const MODEL_ID = "Mike0021/MiniCPM5-1B-ONNX-Web";

const LOCAL_MODEL = {
  id: MODEL_ID,
  name: "MiniCPM5-1B ONNX Web",
  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: 4096,
  maxTokens: 512,
};

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") {
  return {
    role: "assistant",
    content,
    api: LOCAL_MODEL.api,
    provider: LOCAL_MODEL.provider,
    model: LOCAL_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 `${context.systemPrompt || ""}

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 extractJsonPayload(text) {
  const trimmed = String(text || "").trim();
  const fence = trimmed.match(/```(?:json)?\s*([\s\S]*?)```/i);
  const candidate = fence ? fence[1].trim() : trimmed;
  const firstBrace = candidate.indexOf("{");
  const firstBracket = candidate.indexOf("[");
  const starts = [firstBrace, firstBracket].filter((index) => index >= 0);
  if (starts.length === 0) return null;
  const start = Math.min(...starts);
  const lastBrace = candidate.lastIndexOf("}");
  const lastBracket = candidate.lastIndexOf("]");
  const end = Math.max(lastBrace, lastBracket);
  if (end <= start) return null;
  try {
    return JSON.parse(candidate.slice(start, end + 1));
  } catch {
    return null;
  }
}

function normalizeToolCalls(payload) {
  if (!payload) return [];
  const rawCalls = Array.isArray(payload) ? payload : payload.toolCalls || payload.tools || payload.actions || [];
  if (!Array.isArray(rawCalls)) return [];
  return rawCalls
    .map((call, index) => ({
      type: "toolCall",
      id: `tool-${now()}-${index}`,
      name: String(call.tool || call.name || ""),
      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 || "").trim() || "Done.";
}

function mockPlan(context) {
  const last = context.messages[context.messages.length - 1];
  if (last?.role === "toolResult") {
    const results = context.messages.filter((message) => message.role === "toolResult").slice(-4).map(stringifyToolResult);
    return {
      final: `The sandbox work completed.\n\n${results.join("\n\n")}`,
    };
  }

  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: "." } }],
    };
  }
  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 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(4, navigator.hardwareConcurrency || 4);

  let generatorPromise = null;
  let generatorKey = "";

  async function getGenerator() {
    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 producePlan(context, signal) {
    if (modelMode() === "mock") {
      return JSON.stringify(mockPlan(context));
    }

    const generator = await getGenerator();
    if (signal?.aborted) throw new Error("Aborted");
    const result = await generator(buildPrompt(context), {
      max_new_tokens: Number(maxTokens()) || 128,
      temperature: Number(temperature()) || 0,
      do_sample: Number(temperature()) > 0,
      return_full_text: false,
    });
    onModelStatus("Model ready");
    return result?.[0]?.generated_text ?? "";
  }

  function streamFn(_model, context, options = {}) {
    const stream = createAssistantMessageEventStream();
    queueMicrotask(async () => {
      try {
        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 fallbackPayload = payload ? null : mockPlan(context);
        const toolCalls = forceFinal ? [] : normalizeToolCalls(payload || fallbackPayload);
        if (toolCalls.length > 0) {
          const message = createMessage([{ type: "text", text: "Using sandbox tools." }, ...toolCalls], "toolUse");
          emitFinal(stream, message, "Using sandbox tools.");
          return;
        }
        const text = normalizeFinalText(payload || fallbackPayload, generated);
        const message = createMessage([{ type: "text", text }], "stop");
        emitFinal(stream, message, text);
      } catch (error) {
        const text = error instanceof Error ? error.message : String(error);
        const message = {
          ...createMessage([{ type: "text", text }], options.signal?.aborted ? "aborted" : "error"),
          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",
    },
  ];

  return new Agent({
    initialState: {
      model: LOCAL_MODEL,
      systemPrompt:
        "You are Pi Web Agent. Use the sandbox tools for filesystem or command tasks, then give concise results.",
      tools,
    },
    streamFn,
    toolExecution: "sequential",
  });
}

export { MODEL_ID };