pi_agent_rust 0.1.13

High-performance AI coding agent CLI - Rust port of Pi Agent
Documentation
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
//! Node.js `http` and `https` shim — pure-JS implementation for the QuickJS
//! extension runtime.
//!
//! Provides `http.request`, `http.get`, `https.request`, `https.get` that route
//! all HTTP traffic through the capability-gated `pi.http()` hostcall. Uses the
//! `EventEmitter` from `node:events` for the standard Node.js event-based API.

/// The JS source for the `node:http` virtual module.
pub const NODE_HTTP_JS: &str = r#"
import EventEmitter from "node:events";

// ─── STATUS_CODES ────────────────────────────────────────────────────────────

const STATUS_CODES = {
  200: 'OK', 201: 'Created', 204: 'No Content',
  301: 'Moved Permanently', 302: 'Found', 304: 'Not Modified',
  400: 'Bad Request', 401: 'Unauthorized', 403: 'Forbidden',
  404: 'Not Found', 405: 'Method Not Allowed', 408: 'Request Timeout',
  500: 'Internal Server Error', 502: 'Bad Gateway', 503: 'Service Unavailable',
};

const METHODS = [
  'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT',
  'OPTIONS', 'TRACE', 'PATCH',
];

function __pi_http_is_binary_chunk(chunk) {
  if (typeof Buffer !== 'undefined' && Buffer.isBuffer && Buffer.isBuffer(chunk)) {
    return true;
  }
  if (chunk instanceof Uint8Array || chunk instanceof ArrayBuffer) {
    return true;
  }
  return !!(ArrayBuffer.isView && ArrayBuffer.isView(chunk));
}

function __pi_http_to_uint8(chunk) {
  if (typeof Buffer !== 'undefined' && Buffer.isBuffer && Buffer.isBuffer(chunk)) {
    return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
  }
  if (chunk instanceof Uint8Array) {
    return chunk;
  }
  if (chunk instanceof ArrayBuffer) {
    return new Uint8Array(chunk);
  }
  if (ArrayBuffer.isView && ArrayBuffer.isView(chunk)) {
    return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
  }
  return new TextEncoder().encode(String(chunk ?? ''));
}

function __pi_http_clone_body_chunk(chunk) {
  const view = __pi_http_to_uint8(chunk);
  if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {
    return Buffer.from(view);
  }
  return new Uint8Array(view);
}

function __pi_http_chunks_to_base64(chunks) {
  const parts = chunks.map((chunk) => __pi_http_to_uint8(chunk));
  const total = parts.reduce((sum, part) => sum + part.byteLength, 0);
  const merged =
    typeof Buffer !== 'undefined' && typeof Buffer.alloc === 'function'
      ? Buffer.alloc(total)
      : new Uint8Array(total);

  let offset = 0;
  for (const part of parts) {
    merged.set(part, offset);
    offset += part.byteLength;
  }

  if (typeof globalThis.__pi_base64_encode_bytes_native === 'function') {
    return __pi_base64_encode_bytes_native(merged);
  }

  // Fallback for older runtime bounds
  let binary = '';
  let chunk = [];
  for (let i = 0; i < merged.length; i++) {
    chunk.push(merged[i]);
    if (chunk.length >= 4096) {
      binary += String.fromCharCode.apply(null, chunk);
      chunk.length = 0;
    }
  }
  if (chunk.length > 0) {
    binary += String.fromCharCode.apply(null, chunk);
  }
  return __pi_base64_encode_native(binary);
}

function __pi_http_decode_body_bytes(bodyBytes) {
  const encoded = String(bodyBytes ?? '');
  const binary = globalThis.__pi_base64_decode_native(encoded);
  const out =
    typeof Buffer !== 'undefined' && typeof Buffer.alloc === 'function'
      ? Buffer.alloc(binary.length)
      : new Uint8Array(binary.length);

  for (let i = 0; i < binary.length; i++) {
    out[i] = binary.charCodeAt(i) & 0xff;
  }
  return out;
}

function __pi_http_decode_chunk(chunk, encoding) {
  if (!encoding || typeof chunk === 'string') {
    return chunk;
  }

  const bytes = __pi_http_to_uint8(chunk);
  if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {
    return Buffer.from(bytes).toString(encoding);
  }
  return new TextDecoder(encoding).decode(bytes);
}

// ─── IncomingMessage ─────────────────────────────────────────────────────────

class IncomingMessage extends EventEmitter {
  constructor(statusCode, headers, body) {
    super();
    this.statusCode = statusCode;
    this.statusMessage = STATUS_CODES[statusCode] || 'Unknown';
    this.headers = headers || {};
    this._body = body || '';
    this._destroyed = false;
    this.complete = false;
    this.httpVersion = '1.1';
    this.method = null;
    this.url = '';
  }

  _deliver() {
    if (this._destroyed) {
      return;
    }

    const chunk = __pi_http_decode_chunk(this._body, this._encoding);
    if (chunk && chunk.length > 0) {
      this.emit('data', chunk);
    }

    if (this._destroyed) {
      return;
    }

    this.complete = true;
    this.emit('end');
  }

  setEncoding(encoding) {
    this._encoding = encoding ? String(encoding) : 'utf8';
    return this;
  }
  resume() { return this; }
  pause() { return this; }
  destroy() {
    if (this._destroyed) {
      return this;
    }
    this._destroyed = true;
    this.emit('close');
    return this;
  }
}

// ─── ClientRequest ───────────────────────────────────────────────────────────

class ClientRequest extends EventEmitter {
  constructor(options, callback) {
    super();
    this._options = options;
    this._body = [];
    this._ended = false;
    this._aborted = false;
    this._headers = {};
    this.socket = { remoteAddress: '127.0.0.1', remotePort: 0 };
    this.method = options.method || 'GET';
    this.path = options.path || '/';

    if (options.headers) {
      for (const [k, v] of Object.entries(options.headers)) {
        this._headers[String(k).toLowerCase()] = String(v);
      }
    }

    if (typeof callback === 'function') {
      this.once('response', callback);
    }
  }

  write(chunk) {
    if (!this._ended && !this._aborted) {
      this._body.push(
        __pi_http_is_binary_chunk(chunk)
          ? __pi_http_clone_body_chunk(chunk)
          : String(chunk)
      );
    }
    return true;
  }

  end(chunk, _encoding, callback) {
    if (typeof chunk === 'function') { callback = chunk; chunk = undefined; }
    if (typeof _encoding === 'function') { callback = _encoding; }
    if (chunk) this.write(chunk);
    if (typeof callback === 'function') this.once('finish', callback);

    this._ended = true;
    this._send();
    return this;
  }

  abort() {
    this._aborted = true;
    this.emit('abort');
    this.destroy();
  }

  destroy(error) {
    this._aborted = true;
    if (error) this.emit('error', error);
    this.emit('close');
    return this;
  }

  setTimeout(ms, callback) {
    if (typeof callback === 'function') this.once('timeout', callback);
    this._timeoutMs = ms;
    return this;
  }

  setNoDelay() { return this; }
  setSocketKeepAlive() { return this; }
  flushHeaders() {}
  getHeader(name) { return this._headers[String(name).toLowerCase()]; }
  setHeader(name, value) {
    if (!this._ended && !this._aborted) {
      this._headers[String(name).toLowerCase()] = String(value);
    }
    return this;
  }
  removeHeader(name) {
    if (!this._ended && !this._aborted) {
      delete this._headers[String(name).toLowerCase()];
    }
    return this;
  }

  _send() {
    if (this._aborted) {
      return;
    }

    const opts = this._options;
    const protocol = opts.protocol || 'http:';
    const hostname = opts.hostname || opts.host || 'localhost';
    const port = opts.port ? `:${opts.port}` : '';
    const path = opts.path || '/';
    const url = `${protocol}//${hostname}${port}${path}`;

    const headers = { ...this._headers };

    const method = (opts.method || 'GET').toUpperCase();
    const request = { url, method, headers };
    if (this._body.length > 0) {
      const hasBinaryChunk = this._body.some((chunk) => __pi_http_is_binary_chunk(chunk));
      if (hasBinaryChunk) {
        request.body_bytes = __pi_http_chunks_to_base64(this._body);
      } else {
        request.body = this._body.join('');
      }
    }
    if (this._timeoutMs) request.timeout = this._timeoutMs;

    // Use pi.http() hostcall if available
    if (typeof globalThis.pi === 'object' && typeof globalThis.pi.http === 'function') {
      try {
        const promise = globalThis.pi.http(request);
        if (promise && typeof promise.then === 'function') {
          promise.then(
            (result) => {
              if (!this._aborted) {
                this._handleResponse(result);
              }
            },
            (err) => {
              if (!this._aborted) {
                this.emit('error', typeof err === 'string' ? new Error(err) : err);
              }
            }
          );
        } else {
          this._handleResponse(promise);
        }
      } catch (err) {
        this.emit('error', err);
      }
    } else {
      // No pi.http available — emit error
      this.emit('error', new Error('HTTP requests require pi.http() hostcall'));
    }

    this.emit('finish');
  }

  _handleResponse(result) {
    if (this._aborted) {
      return;
    }

    if (!result || typeof result !== 'object') {
      this.emit('error', new Error('Invalid HTTP response from hostcall'));
      return;
    }

    const statusCode = result.status || result.statusCode || 200;
    const headers = result.headers || {};
    const body =
      result.body_bytes !== undefined && result.body_bytes !== null
        ? __pi_http_decode_body_bytes(result.body_bytes)
        : (result.body || result.data || '');

    const res = new IncomingMessage(statusCode, headers, body);
    this.emit('response', res);
    // Deliver body asynchronously (in next microtask)
    Promise.resolve().then(() => {
      if (!this._aborted) {
        res._deliver();
      }
    });
  }
}

// ─── Module API ──────────────────────────────────────────────────────────────

function _parseOptions(input, options) {
  if (typeof input === 'string') {
    try {
      const url = new URL(input);
      return {
        protocol: url.protocol,
        hostname: url.hostname,
        port: url.port || undefined,
        path: url.pathname + url.search,
        ...(options || {}),
      };
    } catch (_e) {
      return { path: input, ...(options || {}) };
    }
  }
  if (input && typeof input === 'object' && !(input instanceof URL)) {
    return { ...input };
  }
  if (input instanceof URL) {
    return {
      protocol: input.protocol,
      hostname: input.hostname,
      port: input.port || undefined,
      path: input.pathname + input.search,
      ...(options || {}),
    };
  }
  return options || {};
}

export function request(input, optionsOrCallback, callback) {
  let options;
  if (typeof optionsOrCallback === 'function') {
    callback = optionsOrCallback;
    options = _parseOptions(input);
  } else {
    options = _parseOptions(input, optionsOrCallback);
  }
  if (!options.protocol) options.protocol = 'http:';
  return new ClientRequest(options, callback);
}

export function get(input, optionsOrCallback, callback) {
  const req = request(input, optionsOrCallback, callback);
  req.end();
  return req;
}

export function createServer() {
  throw new Error('node:http.createServer is not available in PiJS');
}

export { STATUS_CODES, METHODS, IncomingMessage, ClientRequest };
export default { request, get, createServer, STATUS_CODES, METHODS, IncomingMessage, ClientRequest };
"#;

/// The JS source for the `node:https` virtual module.
pub const NODE_HTTPS_JS: &str = r#"
import EventEmitter from "node:events";
import * as http from "node:http";

export function request(input, optionsOrCallback, callback) {
  let options;
  if (typeof optionsOrCallback === 'function') {
    callback = optionsOrCallback;
    options = typeof input === 'string' || input instanceof URL
      ? { ...(typeof input === 'string' ? (() => { try { const u = new URL(input); return { protocol: u.protocol, hostname: u.hostname, port: u.port, path: u.pathname + u.search }; } catch(_) { return { path: input }; } })() : { protocol: input.protocol, hostname: input.hostname, port: input.port, path: input.pathname + input.search }) }
      : { ...(input || {}) };
  } else {
    options = typeof input === 'string' || input instanceof URL
      ? { ...(typeof input === 'string' ? (() => { try { const u = new URL(input); return { protocol: u.protocol, hostname: u.hostname, port: u.port, path: u.pathname + u.search }; } catch(_) { return { path: input }; } })() : { protocol: input.protocol, hostname: input.hostname, port: input.port, path: input.pathname + input.search }), ...(optionsOrCallback || {}) }
      : { ...(input || {}), ...(optionsOrCallback || {}) };
  }
  if (!options.protocol) options.protocol = 'https:';
  return http.request(options, callback);
}

export function get(input, optionsOrCallback, callback) {
  const req = request(input, optionsOrCallback, callback);
  req.end();
  return req;
}

export function createServer() {
  throw new Error('node:https.createServer is not available in PiJS');
}

export const globalAgent = {};

export default { request, get, createServer, globalAgent };
"#;