@trim21/personal-pi-extensions 0.1.655 → 0.1.658
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/package.json +1 -1
- package/src/codemode/worker.js +80 -83
- package/src/codemode/worker.ts +1 -1
package/package.json
CHANGED
package/src/codemode/worker.js
CHANGED
|
@@ -5,8 +5,7 @@ import { JSException, MAX_STACK_SIZE, QuickJS } from "quickjs-wasi";
|
|
|
5
5
|
// src/codemode/prelude.ts
|
|
6
6
|
var MAX_STORE_VALUE_CHARS = 256 * 1024;
|
|
7
7
|
var MAX_STORE_TOTAL_CHARS = 1024 * 1024;
|
|
8
|
-
var IMAGE_HELPER_EXPECTS =
|
|
9
|
-
"image expects a non-empty image URL string, an object with image_url, or a raw MCP image block";
|
|
8
|
+
var IMAGE_HELPER_EXPECTS = "image expects a non-empty image URL string, an object with image_url, or a raw MCP image block";
|
|
10
9
|
var PRELUDE_SOURCE = String.raw`(function (bridge, toolsJson, storeJson) {
|
|
11
10
|
"use strict";
|
|
12
11
|
const stringify = JSON.stringify;
|
|
@@ -288,70 +287,70 @@ import { Type } from "typebox";
|
|
|
288
287
|
import { Value } from "typebox/value";
|
|
289
288
|
var outputItemSchema = Type.Union([
|
|
290
289
|
Type.Object({ type: Type.Literal("text"), text: Type.String() }),
|
|
291
|
-
Type.Object({ type: Type.Literal("image"), data: Type.String(), mimeType: Type.String() })
|
|
290
|
+
Type.Object({ type: Type.Literal("image"), data: Type.String(), mimeType: Type.String() })
|
|
292
291
|
]);
|
|
293
292
|
var toolDeclSchema = Type.Object({
|
|
294
293
|
name: Type.String(),
|
|
295
|
-
description: Type.Optional(Type.String())
|
|
294
|
+
description: Type.Optional(Type.String())
|
|
296
295
|
});
|
|
297
296
|
var startSchema = Type.Object({
|
|
298
297
|
t: Type.Literal("start"),
|
|
299
298
|
code: Type.String(),
|
|
300
299
|
tools: Type.Array(toolDeclSchema),
|
|
301
|
-
store: Type.Record(Type.String(), Type.Unknown())
|
|
300
|
+
store: Type.Record(Type.String(), Type.Unknown())
|
|
302
301
|
});
|
|
303
302
|
var resultSchema = Type.Union([
|
|
304
303
|
Type.Object({
|
|
305
304
|
t: Type.Literal("result"),
|
|
306
305
|
id: Type.Number(),
|
|
307
306
|
ok: Type.Literal(true),
|
|
308
|
-
value: Type.Unknown()
|
|
307
|
+
value: Type.Unknown()
|
|
309
308
|
}),
|
|
310
309
|
Type.Object({
|
|
311
310
|
t: Type.Literal("result"),
|
|
312
311
|
id: Type.Number(),
|
|
313
312
|
ok: Type.Literal(false),
|
|
314
|
-
error: Type.String()
|
|
315
|
-
})
|
|
313
|
+
error: Type.String()
|
|
314
|
+
})
|
|
316
315
|
]);
|
|
317
316
|
var callSchema = Type.Object({
|
|
318
317
|
t: Type.Literal("call"),
|
|
319
318
|
id: Type.Number(),
|
|
320
319
|
name: Type.String(),
|
|
321
|
-
args: Type.Unknown()
|
|
320
|
+
args: Type.Unknown()
|
|
322
321
|
});
|
|
323
322
|
var outputSchema = Type.Object({
|
|
324
323
|
t: Type.Literal("output"),
|
|
325
|
-
items: Type.Array(outputItemSchema)
|
|
324
|
+
items: Type.Array(outputItemSchema)
|
|
326
325
|
});
|
|
327
326
|
var scriptErrorSchema = Type.Object({
|
|
328
327
|
kind: Type.Union([
|
|
329
328
|
Type.Literal("script"),
|
|
330
329
|
Type.Literal("timeout"),
|
|
331
330
|
Type.Literal("aborted"),
|
|
332
|
-
Type.Literal("sandbox")
|
|
331
|
+
Type.Literal("sandbox")
|
|
333
332
|
]),
|
|
334
333
|
name: Type.Optional(Type.String()),
|
|
335
334
|
message: Type.String(),
|
|
336
|
-
stack: Type.Optional(Type.String())
|
|
335
|
+
stack: Type.Optional(Type.String())
|
|
337
336
|
});
|
|
338
337
|
var storeWritesSchema = Type.Object({
|
|
339
338
|
set: Type.Record(Type.String(), Type.Unknown()),
|
|
340
|
-
delete: Type.Array(Type.String())
|
|
339
|
+
delete: Type.Array(Type.String())
|
|
341
340
|
});
|
|
342
341
|
var doneSchema = Type.Union([
|
|
343
342
|
Type.Object({
|
|
344
343
|
t: Type.Literal("done"),
|
|
345
344
|
ok: Type.Literal(true),
|
|
346
345
|
value: Type.Unknown(),
|
|
347
|
-
writes: storeWritesSchema
|
|
346
|
+
writes: storeWritesSchema
|
|
348
347
|
}),
|
|
349
348
|
Type.Object({
|
|
350
349
|
t: Type.Literal("done"),
|
|
351
350
|
ok: Type.Literal(false),
|
|
352
351
|
error: scriptErrorSchema,
|
|
353
|
-
writes: storeWritesSchema
|
|
354
|
-
})
|
|
352
|
+
writes: storeWritesSchema
|
|
353
|
+
})
|
|
355
354
|
]);
|
|
356
355
|
var hostMessageSchema = Type.Union([startSchema, resultSchema]);
|
|
357
356
|
var workerMessageSchema = Type.Union([callSchema, outputSchema, doneSchema]);
|
|
@@ -369,7 +368,7 @@ function decodeHostMessage(value) {
|
|
|
369
368
|
// src/codemode/worker.ts
|
|
370
369
|
var MEMORY_LIMIT_BYTES = 512 * 1024 * 1024;
|
|
371
370
|
function toScriptIdentifier(name) {
|
|
372
|
-
return name.replaceAll(/[^A-Za-z0-9_$]/g, "_").replaceAll(
|
|
371
|
+
return name.replaceAll(/[^A-Za-z0-9_$]/g, "_").replaceAll(/^\d/g, "_");
|
|
373
372
|
}
|
|
374
373
|
function discardOutput(memory) {
|
|
375
374
|
return {
|
|
@@ -381,7 +380,7 @@ function discardOutput(memory) {
|
|
|
381
380
|
}
|
|
382
381
|
view.setUint32(nwrittenPtr, written, true);
|
|
383
382
|
return 0;
|
|
384
|
-
}
|
|
383
|
+
}
|
|
385
384
|
};
|
|
386
385
|
}
|
|
387
386
|
function post(message) {
|
|
@@ -427,61 +426,60 @@ async function runScript(wasm, start) {
|
|
|
427
426
|
memoryLimit: MEMORY_LIMIT_BYTES,
|
|
428
427
|
// 没有这个上限时深递归会打穿 wasm 栈变成 trap,而不是脚本里可捕获的 RangeError
|
|
429
428
|
maxStackSize: MAX_STACK_SIZE,
|
|
430
|
-
wasi: discardOutput
|
|
429
|
+
wasi: discardOutput
|
|
431
430
|
});
|
|
432
|
-
const bridge = vm.newFunction(
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
t: "call",
|
|
439
|
-
id: a.toNumber(),
|
|
440
|
-
name: stringOr(b, ""),
|
|
441
|
-
args: c === void 0 || c.isUndefined ? void 0 : JSON.parse(c.toString()),
|
|
442
|
-
});
|
|
443
|
-
break;
|
|
444
|
-
}
|
|
445
|
-
case "output": {
|
|
446
|
-
post({
|
|
447
|
-
t: "output",
|
|
448
|
-
items:
|
|
449
|
-
a.toString() === "image"
|
|
450
|
-
? [
|
|
451
|
-
{
|
|
452
|
-
type: "image",
|
|
453
|
-
data: stringOr(b, ""),
|
|
454
|
-
mimeType: stringOr(c, "application/octet-stream"),
|
|
455
|
-
},
|
|
456
|
-
]
|
|
457
|
-
: [{ type: "text", text: stringOr(b, "") }],
|
|
458
|
-
});
|
|
459
|
-
break;
|
|
460
|
-
}
|
|
461
|
-
case "done": {
|
|
462
|
-
const writes = c === void 0 || c.isUndefined ? emptyWrites() : parseWrites(c.toString());
|
|
463
|
-
if (a.toBoolean()) {
|
|
431
|
+
const bridge = vm.newFunction(
|
|
432
|
+
"bridge",
|
|
433
|
+
(kind, a, b, c) => {
|
|
434
|
+
const stringOr = (value, fallback) => value === void 0 || value.isUndefined ? fallback : value.toString();
|
|
435
|
+
switch (kind.toString()) {
|
|
436
|
+
case "call": {
|
|
464
437
|
post({
|
|
465
|
-
t: "
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
438
|
+
t: "call",
|
|
439
|
+
id: a.toNumber(),
|
|
440
|
+
name: stringOr(b, ""),
|
|
441
|
+
args: c === void 0 || c.isUndefined ? void 0 : JSON.parse(c.toString())
|
|
469
442
|
});
|
|
470
|
-
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
case "output": {
|
|
471
446
|
post({
|
|
472
|
-
t: "
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
447
|
+
t: "output",
|
|
448
|
+
items: a.toString() === "image" ? [
|
|
449
|
+
{
|
|
450
|
+
type: "image",
|
|
451
|
+
data: stringOr(b, ""),
|
|
452
|
+
mimeType: stringOr(c, "application/octet-stream")
|
|
453
|
+
}
|
|
454
|
+
] : [{ type: "text", text: stringOr(b, "") }]
|
|
476
455
|
});
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case "done": {
|
|
459
|
+
const writes = c === void 0 || c.isUndefined ? emptyWrites() : parseWrites(c.toString());
|
|
460
|
+
if (a.toBoolean()) {
|
|
461
|
+
post({
|
|
462
|
+
t: "done",
|
|
463
|
+
ok: true,
|
|
464
|
+
value: b === void 0 || b.isUndefined ? void 0 : JSON.parse(b.toString()),
|
|
465
|
+
writes
|
|
466
|
+
});
|
|
467
|
+
} else {
|
|
468
|
+
post({
|
|
469
|
+
t: "done",
|
|
470
|
+
ok: false,
|
|
471
|
+
error: scriptError(b === void 0 ? void 0 : b.toString(), "script"),
|
|
472
|
+
writes
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
break;
|
|
477
476
|
}
|
|
478
|
-
break;
|
|
479
477
|
}
|
|
478
|
+
return vm.undefined;
|
|
480
479
|
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
scope.escape(
|
|
480
|
+
);
|
|
481
|
+
const api = vm.withScope(
|
|
482
|
+
(scope) => scope.escape(
|
|
485
483
|
vm.callFunction(
|
|
486
484
|
vm.evalCode(PRELUDE_SOURCE, "codemode-prelude.js"),
|
|
487
485
|
vm.undefined,
|
|
@@ -491,13 +489,13 @@ async function runScript(wasm, start) {
|
|
|
491
489
|
start.tools.map((tool) => ({
|
|
492
490
|
name: tool.name,
|
|
493
491
|
jsName: toScriptIdentifier(tool.name),
|
|
494
|
-
description: tool.description
|
|
495
|
-
}))
|
|
496
|
-
)
|
|
492
|
+
description: tool.description
|
|
493
|
+
}))
|
|
494
|
+
)
|
|
497
495
|
),
|
|
498
|
-
vm.newString(JSON.stringify(start.store))
|
|
499
|
-
)
|
|
500
|
-
)
|
|
496
|
+
vm.newString(JSON.stringify(start.store))
|
|
497
|
+
)
|
|
498
|
+
)
|
|
501
499
|
);
|
|
502
500
|
const settle = api.getProp("settle");
|
|
503
501
|
const run = api.getProp("run");
|
|
@@ -519,21 +517,18 @@ async function runScript(wasm, start) {
|
|
|
519
517
|
api,
|
|
520
518
|
vm.newNumber(result.id),
|
|
521
519
|
result.ok ? vm.true : vm.false,
|
|
522
|
-
result.ok
|
|
523
|
-
? result.value === void 0
|
|
524
|
-
? vm.undefined
|
|
525
|
-
: vm.newString(JSON.stringify(result.value))
|
|
526
|
-
: vm.newString(result.error),
|
|
520
|
+
result.ok ? result.value === void 0 ? vm.undefined : vm.newString(JSON.stringify(result.value)) : vm.newString(result.error)
|
|
527
521
|
);
|
|
528
522
|
});
|
|
529
|
-
} catch {
|
|
523
|
+
} catch {
|
|
524
|
+
}
|
|
530
525
|
drain();
|
|
531
526
|
});
|
|
532
527
|
try {
|
|
533
528
|
const fn = vm.evalCode(
|
|
534
529
|
`(async (tools, console) => {${start.code}
|
|
535
530
|
})`,
|
|
536
|
-
"codemode.js"
|
|
531
|
+
"codemode.js"
|
|
537
532
|
);
|
|
538
533
|
vm.callFunction(run, api, fn).dispose();
|
|
539
534
|
fn.dispose();
|
|
@@ -545,16 +540,16 @@ async function runScript(wasm, start) {
|
|
|
545
540
|
ok: false,
|
|
546
541
|
error: scriptError(
|
|
547
542
|
JSON.stringify({ name: error.name, message: error.message, stack: error.stack }),
|
|
548
|
-
"script"
|
|
543
|
+
"script"
|
|
549
544
|
),
|
|
550
|
-
writes: emptyWrites()
|
|
545
|
+
writes: emptyWrites()
|
|
551
546
|
});
|
|
552
547
|
} else {
|
|
553
548
|
post({
|
|
554
549
|
t: "done",
|
|
555
550
|
ok: false,
|
|
556
551
|
error: { kind: "sandbox", message: error instanceof Error ? error.message : String(error) },
|
|
557
|
-
writes: emptyWrites()
|
|
552
|
+
writes: emptyWrites()
|
|
558
553
|
});
|
|
559
554
|
}
|
|
560
555
|
}
|
|
@@ -572,7 +567,7 @@ function main() {
|
|
|
572
567
|
t: "done",
|
|
573
568
|
ok: false,
|
|
574
569
|
error: { kind: "sandbox", message: "codemode worker: expected a start message first" },
|
|
575
|
-
writes: emptyWrites()
|
|
570
|
+
writes: emptyWrites()
|
|
576
571
|
});
|
|
577
572
|
return;
|
|
578
573
|
}
|
|
@@ -580,4 +575,6 @@ function main() {
|
|
|
580
575
|
});
|
|
581
576
|
}
|
|
582
577
|
main();
|
|
583
|
-
export {
|
|
578
|
+
export {
|
|
579
|
+
toScriptIdentifier
|
|
580
|
+
};
|
package/src/codemode/worker.ts
CHANGED
|
@@ -28,7 +28,7 @@ const MEMORY_LIMIT_BYTES = 512 * 1024 * 1024;
|
|
|
28
28
|
|
|
29
29
|
/** 工具名到脚本标识符的归一化:非法字符换成 `_`,数字开头补 `_`。 */
|
|
30
30
|
export function toScriptIdentifier(name: string): string {
|
|
31
|
-
return name.replaceAll(/[^A-Za-z0-9_$]/g, "_").replaceAll(
|
|
31
|
+
return name.replaceAll(/[^A-Za-z0-9_$]/g, "_").replaceAll(/^\d/g, "_");
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/**
|