byollm 0.1.4 → 0.1.5-beta.2
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/README.md +1 -1
- package/dist/bin.js +1 -1
- package/dist/{chunk-LGN2DHFP.js → chunk-GWC3CCIW.js} +2187 -261
- package/dist/chunk-GWC3CCIW.js.map +1 -0
- package/dist/index.d.ts +594 -11
- package/dist/index.js +9 -1
- package/package.json +2 -2
- package/dist/chunk-LGN2DHFP.js.map +0 -1
|
@@ -63,9 +63,9 @@ function parseSwapUsage(text) {
|
|
|
63
63
|
...total === void 0 ? {} : { swapTotalBytes: total }
|
|
64
64
|
};
|
|
65
65
|
}
|
|
66
|
-
async function readMemory(run2,
|
|
66
|
+
async function readMemory(run2, readFile16, platform2 = hostPlatform()) {
|
|
67
67
|
if (platform2 === "linux") {
|
|
68
|
-
const text = await
|
|
68
|
+
const text = await readFile16("/proc/meminfo");
|
|
69
69
|
return text === void 0 ? { kind: "unknown", why: "/proc/meminfo could not be read" } : parseMemInfo(text);
|
|
70
70
|
}
|
|
71
71
|
if (platform2 === "darwin") {
|
|
@@ -101,7 +101,7 @@ function parsePsi(text) {
|
|
|
101
101
|
if (full === null) return "unknown";
|
|
102
102
|
return Number(full[1]) >= 10 ? "critical" : "normal";
|
|
103
103
|
}
|
|
104
|
-
async function readPressure(run2,
|
|
104
|
+
async function readPressure(run2, readFile16, platform2 = hostPlatform()) {
|
|
105
105
|
if (platform2 === "darwin") {
|
|
106
106
|
const text = await run2([
|
|
107
107
|
"sysctl",
|
|
@@ -111,7 +111,7 @@ async function readPressure(run2, readFile15, platform2 = hostPlatform()) {
|
|
|
111
111
|
return text === void 0 ? "unknown" : parsePressureLevel(text);
|
|
112
112
|
}
|
|
113
113
|
if (platform2 === "linux") {
|
|
114
|
-
const text = await
|
|
114
|
+
const text = await readFile16("/proc/pressure/memory");
|
|
115
115
|
return text === void 0 ? "unknown" : parsePsi(text);
|
|
116
116
|
}
|
|
117
117
|
return "unknown";
|
|
@@ -375,6 +375,7 @@ import { mkdir, readFile as readFile2, writeFile } from "fs/promises";
|
|
|
375
375
|
import {
|
|
376
376
|
BackendIdSchema,
|
|
377
377
|
JobKind,
|
|
378
|
+
MAX_IMAGES_OUT,
|
|
378
379
|
OfferScope,
|
|
379
380
|
backendDescriptor as backendDescriptor2,
|
|
380
381
|
effectiveOfferScope,
|
|
@@ -461,7 +462,136 @@ var BASE_URL_REFUSAL_MESSAGES = Object.freeze({
|
|
|
461
462
|
"wildcard-address": "that is an address to listen on, not one to connect to \u2014 use 127.0.0.1"
|
|
462
463
|
});
|
|
463
464
|
|
|
465
|
+
// src/compose.ts
|
|
466
|
+
var SERVED_KINDS = Object.freeze([
|
|
467
|
+
"llm.generate",
|
|
468
|
+
"llm.chat",
|
|
469
|
+
"llm.tools",
|
|
470
|
+
"llm.vision",
|
|
471
|
+
"llm.image"
|
|
472
|
+
]);
|
|
473
|
+
function isServedKind(kind) {
|
|
474
|
+
return SERVED_KINDS.includes(kind);
|
|
475
|
+
}
|
|
476
|
+
function composePrompt(job) {
|
|
477
|
+
const kind = job.kind;
|
|
478
|
+
switch (kind) {
|
|
479
|
+
case "llm.generate": {
|
|
480
|
+
const payload = job.payload;
|
|
481
|
+
return joinSections([systemSection(payload.system), payload.prompt]);
|
|
482
|
+
}
|
|
483
|
+
/* `llm.tools` opens as chat does — byollm_012 §3: its payload IS chat's,
|
|
484
|
+
plus the declarations, which travel to the model as functions and
|
|
485
|
+
never as prompt text (the tool loop, `tool-loop.ts`). Composed the same
|
|
486
|
+
way so the ingress log records the same prompt for either kind. */
|
|
487
|
+
case "llm.tools":
|
|
488
|
+
case "llm.chat": {
|
|
489
|
+
const payload = job.payload;
|
|
490
|
+
const turns = payload.messages.map((message) => `${roleLabel(message.role)}: ${message.content}`).join("\n\n");
|
|
491
|
+
return joinSections([systemSection(payload.system), turns]);
|
|
492
|
+
}
|
|
493
|
+
/* `llm.vision` — byollm_021 §1. This is the TEXT of the job, for the
|
|
494
|
+
ingress log and the spend estimate: each image is a marker, never its
|
|
495
|
+
bytes or its key. The backend is sent the parts themselves
|
|
496
|
+
(`visionTurns`), because a vision model has to see where each image
|
|
497
|
+
sits among the words. */
|
|
498
|
+
case "llm.vision": {
|
|
499
|
+
const payload = job.payload;
|
|
500
|
+
const turns = payload.messages.map(
|
|
501
|
+
(message) => `${roleLabel(message.role)}: ${typeof message.content === "string" ? message.content : message.content.map(
|
|
502
|
+
(part) => part.type === "text" ? part.text : `[image ${part.image.mediaType}, ${String(part.image.width)}\xD7${String(part.image.height)}]`
|
|
503
|
+
).join("\n")}`
|
|
504
|
+
).join("\n\n");
|
|
505
|
+
return joinSections([systemSection(payload.system), turns]);
|
|
506
|
+
}
|
|
507
|
+
/* `llm.image` — the prompt, as the model is sent it. `aspect` and
|
|
508
|
+
`count` work inside the call and never route it (§1). */
|
|
509
|
+
case "llm.image":
|
|
510
|
+
return job.payload.prompt;
|
|
511
|
+
default: {
|
|
512
|
+
const unknown = kind;
|
|
513
|
+
throw new Error(`this daemon does not compose ${String(unknown)} jobs`);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function systemSection(system) {
|
|
518
|
+
if (system === void 0 || system.trim() === "") return void 0;
|
|
519
|
+
return `System instructions:
|
|
520
|
+
${system}`;
|
|
521
|
+
}
|
|
522
|
+
function roleLabel(role) {
|
|
523
|
+
switch (role) {
|
|
524
|
+
case "assistant":
|
|
525
|
+
return "Assistant";
|
|
526
|
+
case "system":
|
|
527
|
+
return "System";
|
|
528
|
+
default:
|
|
529
|
+
return "User";
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
function joinSections(sections) {
|
|
533
|
+
return sections.filter((section) => section !== void 0).join("\n\n");
|
|
534
|
+
}
|
|
535
|
+
function visionTurns(payload, images) {
|
|
536
|
+
let next = 0;
|
|
537
|
+
const turns = [];
|
|
538
|
+
if (payload.system !== void 0 && payload.system.trim() !== "") {
|
|
539
|
+
turns.push({ role: "system", content: payload.system });
|
|
540
|
+
}
|
|
541
|
+
for (const message of payload.messages) {
|
|
542
|
+
if (typeof message.content === "string") {
|
|
543
|
+
turns.push({ role: message.role, content: message.content });
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
turns.push({
|
|
547
|
+
role: "user",
|
|
548
|
+
content: message.content.map((part) => {
|
|
549
|
+
if (part.type === "text") return { type: "text", text: part.text };
|
|
550
|
+
const image = images[next];
|
|
551
|
+
next += 1;
|
|
552
|
+
if (image === void 0) {
|
|
553
|
+
throw new Error("an image part has no opened image beside it");
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
type: "image",
|
|
557
|
+
mediaType: image.mediaType,
|
|
558
|
+
bytes: image.bytes
|
|
559
|
+
};
|
|
560
|
+
})
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
if (next !== images.length) {
|
|
564
|
+
throw new Error("opened images and image parts do not line up");
|
|
565
|
+
}
|
|
566
|
+
return turns;
|
|
567
|
+
}
|
|
568
|
+
|
|
464
569
|
// src/config.ts
|
|
570
|
+
var PixelSize = z.string().regex(/^[1-9][0-9]{1,4}x[1-9][0-9]{1,4}$/, {
|
|
571
|
+
message: 'a size is WIDTHxHEIGHT in pixels, such as "1024x1024"'
|
|
572
|
+
});
|
|
573
|
+
var ImageSettings = z.object({
|
|
574
|
+
/** Each aspect a job may ask for, mapped to this backend's pixel size. */
|
|
575
|
+
sizes: z.object({
|
|
576
|
+
square: PixelSize.default("1024x1024"),
|
|
577
|
+
portrait: PixelSize.default("1024x1536"),
|
|
578
|
+
landscape: PixelSize.default("1536x1024")
|
|
579
|
+
}).strict().prefault({}),
|
|
580
|
+
/** The ceiling on a job's `count`, at most the protocol's {@link MAX_IMAGES_OUT}. */
|
|
581
|
+
maxImagesPerJob: z.number().int().min(1).max(MAX_IMAGES_OUT).default(1),
|
|
582
|
+
/**
|
|
583
|
+
* The size the image probe asks for (§5). The smallest this backend
|
|
584
|
+
* takes, because on a metered backend the probe is a paid call. Absent
|
|
585
|
+
* asks for the square size.
|
|
586
|
+
*/
|
|
587
|
+
probeSize: PixelSize.optional()
|
|
588
|
+
}).strict();
|
|
589
|
+
function httpOnlyKind(kind, type) {
|
|
590
|
+
if (kind !== "llm.vision" && kind !== "llm.image") return void 0;
|
|
591
|
+
if (backendDescriptor2(type).class === "http") return void 0;
|
|
592
|
+
const what = kind === "llm.vision" ? "read images" : "make images";
|
|
593
|
+
return `${kind} needs an HTTP backend (Ollama, LM Studio, llama.cpp or an OpenAI-compatible API) \u2014 ${type} cannot ${what} in this release, so it does not advertise ${kind}. Add "${kind}" to an HTTP service's kinds instead.`;
|
|
594
|
+
}
|
|
465
595
|
var ServiceConfig = z.object({
|
|
466
596
|
/**
|
|
467
597
|
* How it is reached — the transport, not the thing.
|
|
@@ -526,7 +656,16 @@ var ServiceConfig = z.object({
|
|
|
526
656
|
* than late.
|
|
527
657
|
*/
|
|
528
658
|
centsPerMillionTokens: z.number().positive().default(1500)
|
|
529
|
-
}).strict().optional()
|
|
659
|
+
}).strict().optional(),
|
|
660
|
+
/**
|
|
661
|
+
* How this service makes images, for `llm.image` — byollm_021 §1.
|
|
662
|
+
*
|
|
663
|
+
* A job asks for an aspect and a count, never a size, a model or a step
|
|
664
|
+
* count. The owner maps each aspect to the pixel size this backend takes,
|
|
665
|
+
* and caps how many images one job may ask for. Optional, because the
|
|
666
|
+
* defaults are the sizes OpenAI's image models take.
|
|
667
|
+
*/
|
|
668
|
+
images: ImageSettings.optional()
|
|
530
669
|
}).strict();
|
|
531
670
|
var CommunityBudget = z.object({
|
|
532
671
|
maxJobsPerHour: z.number().int().positive().default(20),
|
|
@@ -535,7 +674,16 @@ var CommunityBudget = z.object({
|
|
|
535
674
|
maxWallClockMs: z.number().int().positive().default(12e4),
|
|
536
675
|
/** Output-size ceiling for one community job. */
|
|
537
676
|
maxOutputBytes: z.number().int().positive().default(256 * 1024),
|
|
538
|
-
/**
|
|
677
|
+
/**
|
|
678
|
+
* Payload-size ceiling, in characters of the OPENED payload — stricter
|
|
679
|
+
* than the protocol's absolute limit.
|
|
680
|
+
*
|
|
681
|
+
* Measured after the payload is opened, so it is what a job really is,
|
|
682
|
+
* not what it declared. Before the fetch a job is refused only if its
|
|
683
|
+
* declared class cannot fit: a `large` job needs at least 64,001 here to
|
|
684
|
+
* be fetched at all, a `medium` one 4,001 (#5738). A streamed
|
|
685
|
+
* (`unbounded`) job is refused at any value until byollm_006.
|
|
686
|
+
*/
|
|
539
687
|
maxPayloadChars: z.number().int().positive().default(1e5),
|
|
540
688
|
/**
|
|
541
689
|
* Whether a teammate's job on a METERED service you shared counts toward
|
|
@@ -548,7 +696,18 @@ var CommunityBudget = z.object({
|
|
|
548
696
|
* and output ceilings apply either way, and work on a free service — your
|
|
549
697
|
* GPU, which no dollar ceiling bounds — is always counted.
|
|
550
698
|
*/
|
|
551
|
-
countTeamJobs: z.boolean().default(false)
|
|
699
|
+
countTeamJobs: z.boolean().default(false),
|
|
700
|
+
/**
|
|
701
|
+
* Bytes of images one teammate's job may make this device download —
|
|
702
|
+
* byollm_021 §3 ({@link MUSTS.BLOB_BUDGET_BEFORE_DOWNLOAD}).
|
|
703
|
+
*
|
|
704
|
+
* Charged from the sizes the opened payload declares, before any blob is
|
|
705
|
+
* fetched; each download then stops at its declared size. 16 MiB admits
|
|
706
|
+
* two or three ordinary phone photos and refuses one image at the
|
|
707
|
+
* protocol's 20 MiB cap: that cap is sized for an owner's own work, and
|
|
708
|
+
* on a teammate's job this device's owner pays for the bandwidth.
|
|
709
|
+
*/
|
|
710
|
+
maxImageBytes: z.number().int().positive().default(16 * 1024 * 1024)
|
|
552
711
|
}).strict();
|
|
553
712
|
var IngressRetention = z.object({
|
|
554
713
|
/**
|
|
@@ -773,6 +932,25 @@ function resolveConfig(config) {
|
|
|
773
932
|
const claimants = /* @__PURE__ */ new Map();
|
|
774
933
|
for (const [name, service] of Object.entries(config.services)) {
|
|
775
934
|
for (const kind of service.kinds) {
|
|
935
|
+
if (!isServedKind(kind)) {
|
|
936
|
+
problems.push({
|
|
937
|
+
where: `services.${name}.kinds`,
|
|
938
|
+
message: `${String(kind)} is not served by this build of byollm (it serves ${SERVED_KINDS.join(", ")}), so ${name} does not advertise it. It will be offered once you update to a byollm that runs it.`
|
|
939
|
+
});
|
|
940
|
+
continue;
|
|
941
|
+
}
|
|
942
|
+
if (kind === "llm.tools" && backendDescriptor2(service.type).class !== "http") {
|
|
943
|
+
problems.push({
|
|
944
|
+
where: `services.${name}.kinds`,
|
|
945
|
+
message: `llm.tools needs an HTTP service (Ollama, LM Studio, llama.cpp or an OpenAI-compatible API) \u2014 ${service.type} cannot use the site's tools yet, so ${name} does not advertise it. Add "llm.tools" to an HTTP service's kinds instead.`
|
|
946
|
+
});
|
|
947
|
+
continue;
|
|
948
|
+
}
|
|
949
|
+
const needsHttp2 = httpOnlyKind(kind, service.type);
|
|
950
|
+
if (needsHttp2 !== void 0) {
|
|
951
|
+
problems.push({ where: `services.${name}.kinds`, message: needsHttp2 });
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
776
954
|
claimants.set(kind, [...claimants.get(kind) ?? [], name]);
|
|
777
955
|
}
|
|
778
956
|
}
|
|
@@ -853,6 +1031,9 @@ function resolveConfig(config) {
|
|
|
853
1031
|
});
|
|
854
1032
|
}
|
|
855
1033
|
for (const kind of service.kinds) {
|
|
1034
|
+
if (!isServedKind(kind)) continue;
|
|
1035
|
+
if (kind === "llm.tools" && descriptor.class !== "http") continue;
|
|
1036
|
+
if (httpOnlyKind(kind, service.type) !== void 0) continue;
|
|
856
1037
|
routes.push({
|
|
857
1038
|
kind,
|
|
858
1039
|
service: name,
|
|
@@ -2235,6 +2416,218 @@ var OpenAiHttpBackend = class {
|
|
|
2235
2416
|
}
|
|
2236
2417
|
}
|
|
2237
2418
|
async execute(request) {
|
|
2419
|
+
const turn = await this.#complete(
|
|
2420
|
+
request,
|
|
2421
|
+
{ messages: [{ role: "user", content: request.prompt }] },
|
|
2422
|
+
/* Every kind but llm.tools reads the response as main did: a
|
|
2423
|
+
`tool_calls` field it never asked for is ignored, malformed or not
|
|
2424
|
+
(C043, other kinds unchanged — Owl, #55998). */
|
|
2425
|
+
{ readsCalls: false }
|
|
2426
|
+
);
|
|
2427
|
+
if (!turn.ok) return turn;
|
|
2428
|
+
if (turn.message.content === null) {
|
|
2429
|
+
return this.#fail(
|
|
2430
|
+
"backend-error",
|
|
2431
|
+
"the model server's response was not in OpenAI chat-completion shape",
|
|
2432
|
+
turn.started
|
|
2433
|
+
);
|
|
2434
|
+
}
|
|
2435
|
+
return {
|
|
2436
|
+
ok: true,
|
|
2437
|
+
text: turn.message.content,
|
|
2438
|
+
durationMs: Date.now() - turn.started,
|
|
2439
|
+
stop: turn.message.stop
|
|
2440
|
+
};
|
|
2441
|
+
}
|
|
2442
|
+
/**
|
|
2443
|
+
* One turn of a function-calling conversation — byollm_012 §7.
|
|
2444
|
+
*
|
|
2445
|
+
* The declarations go to the model as OpenAI `tools`; the calls it asks for
|
|
2446
|
+
* come back as text. Nothing here runs one, opens anything or reaches
|
|
2447
|
+
* anywhere but this service's own configured `chat/completions` — the tool
|
|
2448
|
+
* loop seals each call to the site that declared it.
|
|
2449
|
+
*/
|
|
2450
|
+
async converse(request) {
|
|
2451
|
+
const turn = await this.#complete(request, {
|
|
2452
|
+
messages: request.messages.map(wireMessage),
|
|
2453
|
+
...request.tools === void 0 || request.tools.length === 0 ? {} : {
|
|
2454
|
+
tools: request.tools.map((tool) => ({
|
|
2455
|
+
type: "function",
|
|
2456
|
+
function: {
|
|
2457
|
+
name: tool.name,
|
|
2458
|
+
description: tool.description,
|
|
2459
|
+
parameters: tool.parameters
|
|
2460
|
+
}
|
|
2461
|
+
}))
|
|
2462
|
+
}
|
|
2463
|
+
});
|
|
2464
|
+
if (!turn.ok) return turn;
|
|
2465
|
+
const { content, toolCalls, stop } = turn.message;
|
|
2466
|
+
if (content === null && toolCalls.length === 0) {
|
|
2467
|
+
return this.#fail(
|
|
2468
|
+
"backend-error",
|
|
2469
|
+
"the model server's response was not in OpenAI chat-completion shape",
|
|
2470
|
+
turn.started
|
|
2471
|
+
);
|
|
2472
|
+
}
|
|
2473
|
+
return {
|
|
2474
|
+
ok: true,
|
|
2475
|
+
text: content ?? "",
|
|
2476
|
+
toolCalls,
|
|
2477
|
+
stop,
|
|
2478
|
+
durationMs: Date.now() - turn.started
|
|
2479
|
+
};
|
|
2480
|
+
}
|
|
2481
|
+
/**
|
|
2482
|
+
* `llm.vision` — the same `/v1/chat/completions`, with image parts as
|
|
2483
|
+
* `image_url` data URLs (byollm_021 §6: Ollama, llama.cpp with a projector,
|
|
2484
|
+
* vLLM, LM Studio, OpenAI and the compatible endpoints all take this shape).
|
|
2485
|
+
*
|
|
2486
|
+
* A data URL, never a link: the bytes are the request, so the model server
|
|
2487
|
+
* has nothing to fetch and neither does this daemon.
|
|
2488
|
+
*/
|
|
2489
|
+
async readImages(request) {
|
|
2490
|
+
const turn = await this.#complete(
|
|
2491
|
+
request,
|
|
2492
|
+
{ messages: request.messages.map(toOpenAiTurn) },
|
|
2493
|
+
{ readsCalls: false }
|
|
2494
|
+
);
|
|
2495
|
+
if (!turn.ok) return turn;
|
|
2496
|
+
if (turn.message.content === null) {
|
|
2497
|
+
return this.#fail(
|
|
2498
|
+
"backend-error",
|
|
2499
|
+
"the model server's response was not in OpenAI chat-completion shape",
|
|
2500
|
+
turn.started
|
|
2501
|
+
);
|
|
2502
|
+
}
|
|
2503
|
+
return {
|
|
2504
|
+
ok: true,
|
|
2505
|
+
text: turn.message.content,
|
|
2506
|
+
durationMs: Date.now() - turn.started,
|
|
2507
|
+
stop: turn.message.stop
|
|
2508
|
+
};
|
|
2509
|
+
}
|
|
2510
|
+
/**
|
|
2511
|
+
* `llm.image` — `/v1/images/generations`, asking for `b64_json`.
|
|
2512
|
+
*
|
|
2513
|
+
* **Bytes, never URLs** ({@link MUSTS.IMAGE_BYTES_NOT_URLS}, C056). A
|
|
2514
|
+
* backend that answers with `url` instead is an error, and the URL is not
|
|
2515
|
+
* fetched: fetching it would be this device fetching an address a backend
|
|
2516
|
+
* chose, which byollm_012 §11.1 holds back from v1.
|
|
2517
|
+
*/
|
|
2518
|
+
async makeImages(request) {
|
|
2519
|
+
if (!Number.isFinite(request.timeoutMs) || request.timeoutMs <= 0) {
|
|
2520
|
+
return {
|
|
2521
|
+
ok: false,
|
|
2522
|
+
code: "backend-error",
|
|
2523
|
+
message: "no time limit was set for this call",
|
|
2524
|
+
durationMs: 0
|
|
2525
|
+
};
|
|
2526
|
+
}
|
|
2527
|
+
const started = Date.now();
|
|
2528
|
+
const signal = AbortSignal.any([
|
|
2529
|
+
request.signal,
|
|
2530
|
+
AbortSignal.timeout(request.timeoutMs)
|
|
2531
|
+
]);
|
|
2532
|
+
try {
|
|
2533
|
+
const response = await fetch(this.#endpoint("images/generations"), {
|
|
2534
|
+
method: "POST",
|
|
2535
|
+
headers: this.#headers(),
|
|
2536
|
+
body: JSON.stringify({
|
|
2537
|
+
model: request.model,
|
|
2538
|
+
prompt: request.prompt,
|
|
2539
|
+
n: request.count,
|
|
2540
|
+
size: request.size,
|
|
2541
|
+
response_format: "b64_json"
|
|
2542
|
+
}),
|
|
2543
|
+
redirect: "error",
|
|
2544
|
+
signal
|
|
2545
|
+
});
|
|
2546
|
+
const refused = this.#refusal(response, request.model, started);
|
|
2547
|
+
if (refused !== void 0) return refused;
|
|
2548
|
+
const text = await readCapped(response, request.maxOutputBytes, signal);
|
|
2549
|
+
if (text === null) {
|
|
2550
|
+
return this.#fail(
|
|
2551
|
+
"output-too-large",
|
|
2552
|
+
`the model produced more than ${String(request.maxOutputBytes)} bytes`,
|
|
2553
|
+
started
|
|
2554
|
+
);
|
|
2555
|
+
}
|
|
2556
|
+
const answer = extractImages(text);
|
|
2557
|
+
if (answer.kind === "url") {
|
|
2558
|
+
return this.#fail(
|
|
2559
|
+
"backend-error",
|
|
2560
|
+
"the model server answered with a URL instead of image bytes; byollm never fetches a URL a backend returns \u2014 set it to return b64_json",
|
|
2561
|
+
started
|
|
2562
|
+
);
|
|
2563
|
+
}
|
|
2564
|
+
if (answer.kind === "malformed") {
|
|
2565
|
+
return this.#fail(
|
|
2566
|
+
"backend-error",
|
|
2567
|
+
"the model server's response was not in OpenAI images shape",
|
|
2568
|
+
started
|
|
2569
|
+
);
|
|
2570
|
+
}
|
|
2571
|
+
return {
|
|
2572
|
+
ok: true,
|
|
2573
|
+
images: answer.images,
|
|
2574
|
+
text: answer.revisedPrompt,
|
|
2575
|
+
durationMs: Date.now() - started
|
|
2576
|
+
};
|
|
2577
|
+
} catch (error) {
|
|
2578
|
+
return this.#caught(error, request, started);
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
/** The statuses every call here reads the same way. */
|
|
2582
|
+
#refusal(response, model, started) {
|
|
2583
|
+
if (response.status === 401 || response.status === 403) {
|
|
2584
|
+
return this.#fail(
|
|
2585
|
+
"unauthorized",
|
|
2586
|
+
"the model server rejected our credentials",
|
|
2587
|
+
started
|
|
2588
|
+
);
|
|
2589
|
+
}
|
|
2590
|
+
if (response.status === 404) {
|
|
2591
|
+
return this.#fail(
|
|
2592
|
+
"model-not-found",
|
|
2593
|
+
`the model server does not know "${model}"`,
|
|
2594
|
+
started
|
|
2595
|
+
);
|
|
2596
|
+
}
|
|
2597
|
+
if (!response.ok) {
|
|
2598
|
+
return this.#fail(
|
|
2599
|
+
"backend-error",
|
|
2600
|
+
`the model server returned HTTP ${String(response.status)}`,
|
|
2601
|
+
started
|
|
2602
|
+
);
|
|
2603
|
+
}
|
|
2604
|
+
return void 0;
|
|
2605
|
+
}
|
|
2606
|
+
#caught(error, request, started) {
|
|
2607
|
+
if (request.signal.aborted) {
|
|
2608
|
+
return this.#fail("canceled", "the job was canceled", started);
|
|
2609
|
+
}
|
|
2610
|
+
if (isAbort(error)) {
|
|
2611
|
+
return this.#fail(
|
|
2612
|
+
"timeout",
|
|
2613
|
+
`the model did not answer within ${String(request.timeoutMs)}ms`,
|
|
2614
|
+
started
|
|
2615
|
+
);
|
|
2616
|
+
}
|
|
2617
|
+
return this.#fail(
|
|
2618
|
+
"backend-unreachable",
|
|
2619
|
+
describeFetchError(error, this.#baseUrl.origin),
|
|
2620
|
+
started
|
|
2621
|
+
);
|
|
2622
|
+
}
|
|
2623
|
+
/**
|
|
2624
|
+
* POST one chat completion and read the first choice.
|
|
2625
|
+
*
|
|
2626
|
+
* The body is this adapter's own construction: `model` from owner config,
|
|
2627
|
+
* `stream: false`, and the conversation the caller assembled. Nothing in a
|
|
2628
|
+
* payload is parsed as configuration.
|
|
2629
|
+
*/
|
|
2630
|
+
async #complete(request, conversation, reading = { readsCalls: true }) {
|
|
2238
2631
|
if (!Number.isFinite(request.timeoutMs) || request.timeoutMs <= 0) {
|
|
2239
2632
|
return {
|
|
2240
2633
|
ok: false,
|
|
@@ -2254,7 +2647,7 @@ var OpenAiHttpBackend = class {
|
|
|
2254
2647
|
// configuration, and `model` comes from owner config.
|
|
2255
2648
|
body: JSON.stringify({
|
|
2256
2649
|
model: request.model,
|
|
2257
|
-
|
|
2650
|
+
...conversation,
|
|
2258
2651
|
stream: false
|
|
2259
2652
|
}),
|
|
2260
2653
|
redirect: "error",
|
|
@@ -2289,20 +2682,15 @@ var OpenAiHttpBackend = class {
|
|
|
2289
2682
|
started
|
|
2290
2683
|
);
|
|
2291
2684
|
}
|
|
2292
|
-
const
|
|
2293
|
-
if (
|
|
2685
|
+
const message = extractMessage(text, reading.readsCalls);
|
|
2686
|
+
if (message === null) {
|
|
2294
2687
|
return this.#fail(
|
|
2295
2688
|
"backend-error",
|
|
2296
2689
|
"the model server's response was not in OpenAI chat-completion shape",
|
|
2297
2690
|
started
|
|
2298
2691
|
);
|
|
2299
2692
|
}
|
|
2300
|
-
return {
|
|
2301
|
-
ok: true,
|
|
2302
|
-
text: answer.content,
|
|
2303
|
-
durationMs: Date.now() - started,
|
|
2304
|
-
stop: answer.stop
|
|
2305
|
-
};
|
|
2693
|
+
return { ok: true, started, message };
|
|
2306
2694
|
} catch (error) {
|
|
2307
2695
|
if (request.signal.aborted) {
|
|
2308
2696
|
return this.#fail("canceled", "the job was canceled", started);
|
|
@@ -2372,7 +2760,7 @@ function concat(chunks, total) {
|
|
|
2372
2760
|
}
|
|
2373
2761
|
return out;
|
|
2374
2762
|
}
|
|
2375
|
-
function
|
|
2763
|
+
function extractMessage(raw, readsCalls) {
|
|
2376
2764
|
let parsed;
|
|
2377
2765
|
try {
|
|
2378
2766
|
parsed = JSON.parse(raw);
|
|
@@ -2385,15 +2773,116 @@ function extractContent(raw) {
|
|
|
2385
2773
|
const message = choices[0].message;
|
|
2386
2774
|
if (typeof message !== "object" || message === null) return null;
|
|
2387
2775
|
const content = message.content;
|
|
2388
|
-
if (typeof content !== "string"
|
|
2776
|
+
if (typeof content !== "string" && content !== null && content !== void 0)
|
|
2777
|
+
return null;
|
|
2778
|
+
const toolCalls = readsCalls ? extractToolCalls(message.tool_calls) : [];
|
|
2779
|
+
if (toolCalls === null) return null;
|
|
2389
2780
|
const finish = choices[0].finish_reason;
|
|
2390
2781
|
return {
|
|
2391
|
-
content,
|
|
2782
|
+
content: typeof content === "string" ? content : null,
|
|
2783
|
+
toolCalls,
|
|
2392
2784
|
/* Absent, or a value we have not mapped, is `unknown` — never `end`. An
|
|
2393
2785
|
adapter that cannot tell must not be able to claim completion. */
|
|
2394
2786
|
stop: typeof finish === "string" ? FINISH_REASONS[finish] ?? "unknown" : "unknown"
|
|
2395
2787
|
};
|
|
2396
2788
|
}
|
|
2789
|
+
function extractToolCalls(raw) {
|
|
2790
|
+
if (raw === void 0 || raw === null) return [];
|
|
2791
|
+
if (!Array.isArray(raw)) return null;
|
|
2792
|
+
const calls = [];
|
|
2793
|
+
for (const [index, entry] of raw.entries()) {
|
|
2794
|
+
if (typeof entry !== "object" || entry === null) return null;
|
|
2795
|
+
const fn = entry.function;
|
|
2796
|
+
if (typeof fn !== "object" || fn === null) return null;
|
|
2797
|
+
const name = fn.name;
|
|
2798
|
+
if (typeof name !== "string") return null;
|
|
2799
|
+
const args = fn.arguments;
|
|
2800
|
+
const id = entry.id;
|
|
2801
|
+
calls.push({
|
|
2802
|
+
/* Some local servers leave the id out. The tool turn has to name one,
|
|
2803
|
+
and the position is unique within the turn. */
|
|
2804
|
+
id: typeof id === "string" && id !== "" ? id : `call_${String(index)}`,
|
|
2805
|
+
name,
|
|
2806
|
+
/* OpenAI sends a JSON string; some local servers send the object. The
|
|
2807
|
+
loop parses either way, so the object is put back into text rather
|
|
2808
|
+
than trusted as already parsed. */
|
|
2809
|
+
arguments: typeof args === "string" ? args : args === void 0 ? "{}" : JSON.stringify(args)
|
|
2810
|
+
});
|
|
2811
|
+
}
|
|
2812
|
+
return calls;
|
|
2813
|
+
}
|
|
2814
|
+
function wireMessage(message) {
|
|
2815
|
+
switch (message.role) {
|
|
2816
|
+
case "user":
|
|
2817
|
+
return { role: "user", content: message.content };
|
|
2818
|
+
case "assistant":
|
|
2819
|
+
return {
|
|
2820
|
+
role: "assistant",
|
|
2821
|
+
content: message.content,
|
|
2822
|
+
...message.toolCalls === void 0 || message.toolCalls.length === 0 ? {} : {
|
|
2823
|
+
tool_calls: message.toolCalls.map((call) => ({
|
|
2824
|
+
id: call.id,
|
|
2825
|
+
type: "function",
|
|
2826
|
+
function: { name: call.name, arguments: call.arguments }
|
|
2827
|
+
}))
|
|
2828
|
+
}
|
|
2829
|
+
};
|
|
2830
|
+
case "tool":
|
|
2831
|
+
return {
|
|
2832
|
+
role: "tool",
|
|
2833
|
+
tool_call_id: message.toolCallId,
|
|
2834
|
+
content: message.content
|
|
2835
|
+
};
|
|
2836
|
+
}
|
|
2837
|
+
}
|
|
2838
|
+
function toOpenAiTurn(turn) {
|
|
2839
|
+
if (typeof turn.content === "string") {
|
|
2840
|
+
return { role: turn.role, content: turn.content };
|
|
2841
|
+
}
|
|
2842
|
+
return {
|
|
2843
|
+
role: turn.role,
|
|
2844
|
+
content: turn.content.map(
|
|
2845
|
+
(part) => part.type === "text" ? { type: "text", text: part.text } : {
|
|
2846
|
+
type: "image_url",
|
|
2847
|
+
image_url: {
|
|
2848
|
+
url: `data:${part.mediaType};base64,${Buffer.from(part.bytes).toString("base64")}`
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
)
|
|
2852
|
+
};
|
|
2853
|
+
}
|
|
2854
|
+
function extractImages(raw) {
|
|
2855
|
+
let parsed;
|
|
2856
|
+
try {
|
|
2857
|
+
parsed = JSON.parse(raw);
|
|
2858
|
+
} catch {
|
|
2859
|
+
return { kind: "malformed" };
|
|
2860
|
+
}
|
|
2861
|
+
if (typeof parsed !== "object" || parsed === null)
|
|
2862
|
+
return { kind: "malformed" };
|
|
2863
|
+
const data = parsed.data;
|
|
2864
|
+
if (!Array.isArray(data) || data.length === 0) return { kind: "malformed" };
|
|
2865
|
+
const images = [];
|
|
2866
|
+
let revisedPrompt = "";
|
|
2867
|
+
for (const entry of data) {
|
|
2868
|
+
if (typeof entry !== "object" || entry === null)
|
|
2869
|
+
return { kind: "malformed" };
|
|
2870
|
+
const {
|
|
2871
|
+
b64_json: b64,
|
|
2872
|
+
url,
|
|
2873
|
+
revised_prompt: revised
|
|
2874
|
+
} = entry;
|
|
2875
|
+
if (typeof b64 !== "string" || b64 === "") {
|
|
2876
|
+
return typeof url === "string" ? { kind: "url" } : { kind: "malformed" };
|
|
2877
|
+
}
|
|
2878
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(b64)) return { kind: "malformed" };
|
|
2879
|
+
images.push(new Uint8Array(Buffer.from(b64, "base64")));
|
|
2880
|
+
if (revisedPrompt === "" && typeof revised === "string") {
|
|
2881
|
+
revisedPrompt = revised;
|
|
2882
|
+
}
|
|
2883
|
+
}
|
|
2884
|
+
return { kind: "ok", images, revisedPrompt };
|
|
2885
|
+
}
|
|
2397
2886
|
function extractModelIds(body) {
|
|
2398
2887
|
if (typeof body !== "object" || body === null) return [];
|
|
2399
2888
|
const data = body.data;
|
|
@@ -2456,27 +2945,246 @@ function createProcessBackend(id) {
|
|
|
2456
2945
|
}
|
|
2457
2946
|
}
|
|
2458
2947
|
|
|
2948
|
+
// src/images.ts
|
|
2949
|
+
import {
|
|
2950
|
+
BLOB_NONCE_BYTES,
|
|
2951
|
+
BLOB_PADDING_BYTES,
|
|
2952
|
+
BLOB_TAG_BYTES,
|
|
2953
|
+
MAX_IMAGE_BYTES,
|
|
2954
|
+
blobObjectBytes,
|
|
2955
|
+
decryptBlob,
|
|
2956
|
+
encryptBlob,
|
|
2957
|
+
sniffImage,
|
|
2958
|
+
withinImageCaps
|
|
2959
|
+
} from "@byollm/protocol";
|
|
2960
|
+
import { mkdir as mkdir2, readFile as readFile4, rename, writeFile as writeFile2 } from "fs/promises";
|
|
2961
|
+
import { dirname as dirname2 } from "path";
|
|
2962
|
+
import { z as z2 } from "zod";
|
|
2963
|
+
function mib(bytes2) {
|
|
2964
|
+
return `${(bytes2 / (1024 * 1024)).toFixed(1)} MiB`;
|
|
2965
|
+
}
|
|
2966
|
+
function imageBudgetRefusal(refs, maxImageBytes, configPath = "your config") {
|
|
2967
|
+
const declared = refs.reduce((sum, ref) => sum + ref.bytes, 0);
|
|
2968
|
+
if (declared <= maxImageBytes) return void 0;
|
|
2969
|
+
return `a teammate's job declares ${mib(declared)} of images and community.maxImageBytes is ${mib(maxImageBytes)} \u2014 raise community.maxImageBytes in ${configPath} to at least ${String(declared)} to accept it`;
|
|
2970
|
+
}
|
|
2971
|
+
async function openInputImages(input) {
|
|
2972
|
+
const images = [];
|
|
2973
|
+
for (const ref of input.refs) {
|
|
2974
|
+
const url = await input.transport.url(ref.blob);
|
|
2975
|
+
const got = await input.transport.get(url, blobObjectBytes(ref.bytes));
|
|
2976
|
+
if (!got.ok) return got;
|
|
2977
|
+
const opened = await decryptBlob({
|
|
2978
|
+
object: got.bytes,
|
|
2979
|
+
ref,
|
|
2980
|
+
jobId: input.jobId,
|
|
2981
|
+
direction: "in"
|
|
2982
|
+
});
|
|
2983
|
+
if (!opened.ok) {
|
|
2984
|
+
return {
|
|
2985
|
+
ok: false,
|
|
2986
|
+
message: `an input image was refused: ${opened.message}`
|
|
2987
|
+
};
|
|
2988
|
+
}
|
|
2989
|
+
images.push({ mediaType: ref.mediaType, bytes: opened.bytes });
|
|
2990
|
+
}
|
|
2991
|
+
return { ok: true, images };
|
|
2992
|
+
}
|
|
2993
|
+
async function storeOutputImages(input) {
|
|
2994
|
+
const checked = [];
|
|
2995
|
+
for (const plaintext of input.images) {
|
|
2996
|
+
const sniffed = checkOutputImage(plaintext);
|
|
2997
|
+
if (!sniffed.ok) return sniffed;
|
|
2998
|
+
checked.push({ plaintext, sniffed });
|
|
2999
|
+
}
|
|
3000
|
+
const refs = [];
|
|
3001
|
+
for (const { plaintext, sniffed } of checked) {
|
|
3002
|
+
const slot = await input.transport.slot(blobObjectBytes(plaintext.length));
|
|
3003
|
+
const sealed = await encryptBlob({
|
|
3004
|
+
plaintext,
|
|
3005
|
+
jobId: input.jobId,
|
|
3006
|
+
blobId: slot.blob,
|
|
3007
|
+
direction: "out"
|
|
3008
|
+
});
|
|
3009
|
+
const put = await input.transport.put(slot.url, sealed.object);
|
|
3010
|
+
if (!put.ok) return put;
|
|
3011
|
+
refs.push({
|
|
3012
|
+
blob: slot.blob,
|
|
3013
|
+
mediaType: sniffed.mediaType,
|
|
3014
|
+
bytes: plaintext.length,
|
|
3015
|
+
width: sniffed.width,
|
|
3016
|
+
height: sniffed.height,
|
|
3017
|
+
sha256: sealed.sha256,
|
|
3018
|
+
key: sealed.key
|
|
3019
|
+
});
|
|
3020
|
+
}
|
|
3021
|
+
return { ok: true, refs };
|
|
3022
|
+
}
|
|
3023
|
+
function checkOutputImage(bytes2) {
|
|
3024
|
+
if (bytes2.length < 1 || bytes2.length > MAX_IMAGE_BYTES) {
|
|
3025
|
+
return {
|
|
3026
|
+
ok: false,
|
|
3027
|
+
message: `the model made an image of ${mib(bytes2.length)}, over the ${mib(MAX_IMAGE_BYTES)} cap`
|
|
3028
|
+
};
|
|
3029
|
+
}
|
|
3030
|
+
const sniffed = sniffImage(bytes2);
|
|
3031
|
+
if (sniffed === null) {
|
|
3032
|
+
return {
|
|
3033
|
+
ok: false,
|
|
3034
|
+
message: "the model's image is not a PNG, JPEG or WebP by its bytes"
|
|
3035
|
+
};
|
|
3036
|
+
}
|
|
3037
|
+
if (!withinImageCaps(sniffed)) {
|
|
3038
|
+
return {
|
|
3039
|
+
ok: false,
|
|
3040
|
+
message: `the model's image is ${String(sniffed.width)}\xD7${String(sniffed.height)}, over the pixel caps`
|
|
3041
|
+
};
|
|
3042
|
+
}
|
|
3043
|
+
return { ok: true, ...sniffed };
|
|
3044
|
+
}
|
|
3045
|
+
var RED_SQUARE_PNG = Buffer.from(
|
|
3046
|
+
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAFklEQVR42mP4z8BAEmIY1TCqYfhqAACQ+f8B8u7oVwAAAABJRU5ErkJggg==",
|
|
3047
|
+
"base64"
|
|
3048
|
+
);
|
|
3049
|
+
var VISION_ASK = "What colour is this square? Answer with one word.";
|
|
3050
|
+
var PROBE_TIMEOUT_MS = 12e4;
|
|
3051
|
+
var IMAGE_PROBE_REUSE_MS = 24 * 60 * 60 * 1e3;
|
|
3052
|
+
var ImageProbeSchema = z2.discriminatedUnion("kind", [
|
|
3053
|
+
z2.object({ kind: z2.literal("passes"), at: z2.number() }).strict(),
|
|
3054
|
+
z2.object({ kind: z2.literal("cannot"), why: z2.string(), at: z2.number() }).strict(),
|
|
3055
|
+
z2.object({ kind: z2.literal("unanswered"), why: z2.string(), at: z2.number() }).strict()
|
|
3056
|
+
]);
|
|
3057
|
+
function imageProbeKey(input) {
|
|
3058
|
+
return JSON.stringify([
|
|
3059
|
+
input.kind,
|
|
3060
|
+
input.service,
|
|
3061
|
+
input.backendId,
|
|
3062
|
+
input.baseUrl ?? "",
|
|
3063
|
+
input.model
|
|
3064
|
+
]);
|
|
3065
|
+
}
|
|
3066
|
+
function memoryImageProbeStore() {
|
|
3067
|
+
const held = /* @__PURE__ */ new Map();
|
|
3068
|
+
return {
|
|
3069
|
+
get: (key) => Promise.resolve(held.get(key)),
|
|
3070
|
+
set: (key, verdict) => {
|
|
3071
|
+
held.set(key, verdict);
|
|
3072
|
+
return Promise.resolve();
|
|
3073
|
+
}
|
|
3074
|
+
};
|
|
3075
|
+
}
|
|
3076
|
+
function fileImageProbeStore(path) {
|
|
3077
|
+
const read = async () => {
|
|
3078
|
+
try {
|
|
3079
|
+
const parsed = z2.record(z2.string(), ImageProbeSchema).safeParse(JSON.parse(await readFile4(path, "utf8")));
|
|
3080
|
+
return parsed.success ? parsed.data : {};
|
|
3081
|
+
} catch {
|
|
3082
|
+
return {};
|
|
3083
|
+
}
|
|
3084
|
+
};
|
|
3085
|
+
return {
|
|
3086
|
+
get: async (key) => (await read())[key],
|
|
3087
|
+
set: async (key, verdict) => {
|
|
3088
|
+
const all = { ...await read(), [key]: verdict };
|
|
3089
|
+
await mkdir2(dirname2(path), { recursive: true });
|
|
3090
|
+
const temporary = `${path}.${String(process.pid)}.tmp`;
|
|
3091
|
+
await writeFile2(temporary, `${JSON.stringify(all, null, 2)}
|
|
3092
|
+
`, {
|
|
3093
|
+
mode: 384
|
|
3094
|
+
});
|
|
3095
|
+
await rename(temporary, path);
|
|
3096
|
+
}
|
|
3097
|
+
};
|
|
3098
|
+
}
|
|
3099
|
+
async function probeVision(backend, model, now, signal = new AbortController().signal) {
|
|
3100
|
+
if (backend.readImages === void 0) {
|
|
3101
|
+
return { kind: "cannot", why: needsHttp(backend, "read images"), at: now };
|
|
3102
|
+
}
|
|
3103
|
+
const result = await backend.readImages({
|
|
3104
|
+
model,
|
|
3105
|
+
timeoutMs: PROBE_TIMEOUT_MS,
|
|
3106
|
+
maxOutputBytes: 16 * 1024,
|
|
3107
|
+
signal,
|
|
3108
|
+
messages: [
|
|
3109
|
+
{
|
|
3110
|
+
role: "user",
|
|
3111
|
+
content: [
|
|
3112
|
+
{ type: "text", text: VISION_ASK },
|
|
3113
|
+
{ type: "image", mediaType: "image/png", bytes: RED_SQUARE_PNG }
|
|
3114
|
+
]
|
|
3115
|
+
}
|
|
3116
|
+
]
|
|
3117
|
+
});
|
|
3118
|
+
if (!result.ok) return { kind: "unanswered", why: result.message, at: now };
|
|
3119
|
+
const word = firstWord(result.text);
|
|
3120
|
+
if (word === "red") return { kind: "passes", at: now };
|
|
3121
|
+
return {
|
|
3122
|
+
kind: "cannot",
|
|
3123
|
+
why: `${model} on ${backend.id}: sent an image, the model answered ${JSON.stringify(word === "" ? result.text.slice(0, 40) : word)} \u2014 it is not reading images`,
|
|
3124
|
+
at: now
|
|
3125
|
+
};
|
|
3126
|
+
}
|
|
3127
|
+
async function probeImages(backend, model, size, now, signal = new AbortController().signal) {
|
|
3128
|
+
if (backend.makeImages === void 0) {
|
|
3129
|
+
return { kind: "cannot", why: needsHttp(backend, "make images"), at: now };
|
|
3130
|
+
}
|
|
3131
|
+
const result = await backend.makeImages({
|
|
3132
|
+
model,
|
|
3133
|
+
prompt: "A plain red square.",
|
|
3134
|
+
size,
|
|
3135
|
+
count: 1,
|
|
3136
|
+
timeoutMs: PROBE_TIMEOUT_MS,
|
|
3137
|
+
maxOutputBytes: probeOutputCap(),
|
|
3138
|
+
signal
|
|
3139
|
+
});
|
|
3140
|
+
if (!result.ok) {
|
|
3141
|
+
return result.code === "backend-error" ? {
|
|
3142
|
+
kind: "cannot",
|
|
3143
|
+
why: `${model} on ${backend.id}: ${result.message}`,
|
|
3144
|
+
at: now
|
|
3145
|
+
} : { kind: "unanswered", why: result.message, at: now };
|
|
3146
|
+
}
|
|
3147
|
+
const [first] = result.images;
|
|
3148
|
+
if (first === void 0 || sniffImage(first) === null) {
|
|
3149
|
+
return {
|
|
3150
|
+
kind: "cannot",
|
|
3151
|
+
why: `${model} on ${backend.id}: answered with bytes that are not a PNG, JPEG or WebP`,
|
|
3152
|
+
at: now
|
|
3153
|
+
};
|
|
3154
|
+
}
|
|
3155
|
+
return { kind: "passes", at: now };
|
|
3156
|
+
}
|
|
3157
|
+
function probeOutputCap(count = 1) {
|
|
3158
|
+
return Math.ceil(MAX_IMAGE_BYTES * count * 4 / 3) + 64 * 1024;
|
|
3159
|
+
}
|
|
3160
|
+
function needsHttp(backend, what) {
|
|
3161
|
+
return `${backend.id} cannot ${what} \u2014 that needs an HTTP backend, such as Ollama, LM Studio or an OpenAI-compatible API`;
|
|
3162
|
+
}
|
|
3163
|
+
function firstWord(text) {
|
|
3164
|
+
return text.trim().toLowerCase().replace(/^[^a-z]+/, "").split(/[^a-z]/)[0] ?? "";
|
|
3165
|
+
}
|
|
3166
|
+
|
|
2459
3167
|
// src/cli.ts
|
|
2460
|
-
import { mkdir as
|
|
3168
|
+
import { mkdir as mkdir10, readFile as readFile15, rm as rm6, writeFile as writeFile10 } from "fs/promises";
|
|
2461
3169
|
import { hostname, userInfo } from "os";
|
|
2462
|
-
import { dirname as
|
|
3170
|
+
import { dirname as dirname10 } from "path";
|
|
2463
3171
|
import { createInterface as createInterface2 } from "readline/promises";
|
|
2464
3172
|
|
|
2465
3173
|
// src/health.ts
|
|
2466
|
-
import { readFile as
|
|
2467
|
-
import { dirname as
|
|
3174
|
+
import { readFile as readFile5, writeFile as writeFile3, mkdir as mkdir3 } from "fs/promises";
|
|
3175
|
+
import { dirname as dirname3 } from "path";
|
|
2468
3176
|
var FAILURES_BEFORE_ALARM = 6;
|
|
2469
3177
|
async function writeHealth(path, health) {
|
|
2470
3178
|
try {
|
|
2471
|
-
await
|
|
2472
|
-
await
|
|
3179
|
+
await mkdir3(dirname3(path), { recursive: true });
|
|
3180
|
+
await writeFile3(path, `${JSON.stringify(health)}
|
|
2473
3181
|
`, "utf8");
|
|
2474
3182
|
} catch {
|
|
2475
3183
|
}
|
|
2476
3184
|
}
|
|
2477
3185
|
async function readHealth(path) {
|
|
2478
3186
|
try {
|
|
2479
|
-
const parsed = JSON.parse(await
|
|
3187
|
+
const parsed = JSON.parse(await readFile5(path, "utf8"));
|
|
2480
3188
|
if (typeof parsed !== "object" || parsed === null || typeof parsed.consecutiveFailures !== "number") {
|
|
2481
3189
|
return void 0;
|
|
2482
3190
|
}
|
|
@@ -2488,21 +3196,21 @@ async function readHealth(path) {
|
|
|
2488
3196
|
|
|
2489
3197
|
// src/heartbeat.ts
|
|
2490
3198
|
import { randomBytes } from "crypto";
|
|
2491
|
-
import { readFile as
|
|
2492
|
-
import { dirname as
|
|
3199
|
+
import { readFile as readFile6, rename as rename2, writeFile as writeFile4, mkdir as mkdir4 } from "fs/promises";
|
|
3200
|
+
import { dirname as dirname4 } from "path";
|
|
2493
3201
|
async function writeHeartbeat(path, beat) {
|
|
2494
3202
|
try {
|
|
2495
|
-
await
|
|
3203
|
+
await mkdir4(dirname4(path), { recursive: true });
|
|
2496
3204
|
const temp = `${path}.${randomBytes(6).toString("hex")}`;
|
|
2497
|
-
await
|
|
3205
|
+
await writeFile4(temp, `${JSON.stringify(beat)}
|
|
2498
3206
|
`, "utf8");
|
|
2499
|
-
await
|
|
3207
|
+
await rename2(temp, path);
|
|
2500
3208
|
} catch {
|
|
2501
3209
|
}
|
|
2502
3210
|
}
|
|
2503
3211
|
async function readHeartbeat(path) {
|
|
2504
3212
|
try {
|
|
2505
|
-
const parsed = JSON.parse(await
|
|
3213
|
+
const parsed = JSON.parse(await readFile6(path, "utf8"));
|
|
2506
3214
|
if (typeof parsed === "object" && parsed !== null && typeof parsed.at === "number" && typeof parsed.pid === "number") {
|
|
2507
3215
|
return parsed;
|
|
2508
3216
|
}
|
|
@@ -2530,7 +3238,7 @@ import { createInterface } from "readline";
|
|
|
2530
3238
|
import { backendName, classifyCost as classifyCost2 } from "@byollm/protocol";
|
|
2531
3239
|
|
|
2532
3240
|
// src/cli-models.ts
|
|
2533
|
-
import { readFile as
|
|
3241
|
+
import { readFile as readFile7 } from "fs/promises";
|
|
2534
3242
|
import { homedir } from "os";
|
|
2535
3243
|
import { join as join3 } from "path";
|
|
2536
3244
|
var CLAUDE_ALIASES = [
|
|
@@ -2540,7 +3248,7 @@ var CLAUDE_ALIASES = [
|
|
|
2540
3248
|
];
|
|
2541
3249
|
async function readJson2(path) {
|
|
2542
3250
|
try {
|
|
2543
|
-
return JSON.parse(await
|
|
3251
|
+
return JSON.parse(await readFile7(path, "utf8"));
|
|
2544
3252
|
} catch {
|
|
2545
3253
|
return void 0;
|
|
2546
3254
|
}
|
|
@@ -2664,7 +3372,7 @@ function modelsFrom(body) {
|
|
|
2664
3372
|
}
|
|
2665
3373
|
|
|
2666
3374
|
// src/services-manage.ts
|
|
2667
|
-
import { readFile as
|
|
3375
|
+
import { readFile as readFile9 } from "fs/promises";
|
|
2668
3376
|
|
|
2669
3377
|
// src/supervised.ts
|
|
2670
3378
|
function supervisorPid(env = process.env) {
|
|
@@ -2695,7 +3403,7 @@ function howItRuns(env = process.env, isTty = process.stdout.isTTY) {
|
|
|
2695
3403
|
}
|
|
2696
3404
|
|
|
2697
3405
|
// src/spend.ts
|
|
2698
|
-
import { z as
|
|
3406
|
+
import { z as z3 } from "zod";
|
|
2699
3407
|
|
|
2700
3408
|
// src/ledger.ts
|
|
2701
3409
|
import { randomUUID } from "crypto";
|
|
@@ -2709,8 +3417,8 @@ import {
|
|
|
2709
3417
|
rmSync,
|
|
2710
3418
|
writeSync
|
|
2711
3419
|
} from "fs";
|
|
2712
|
-
import { mkdir as
|
|
2713
|
-
import { dirname as
|
|
3420
|
+
import { mkdir as mkdir5, open, readFile as readFile8, rename as rename3, rm as rm2 } from "fs/promises";
|
|
3421
|
+
import { dirname as dirname5 } from "path";
|
|
2714
3422
|
function untrusted(why) {
|
|
2715
3423
|
return { state: "untrusted", why };
|
|
2716
3424
|
}
|
|
@@ -2732,7 +3440,7 @@ function readFailure(error, path) {
|
|
|
2732
3440
|
async function readLedger(path, schema) {
|
|
2733
3441
|
let raw;
|
|
2734
3442
|
try {
|
|
2735
|
-
raw = await
|
|
3443
|
+
raw = await readFile8(path, "utf8");
|
|
2736
3444
|
} catch (error) {
|
|
2737
3445
|
const failure = readFailure(error, path);
|
|
2738
3446
|
return failure === "fresh" ? { state: "fresh" } : failure;
|
|
@@ -2750,7 +3458,7 @@ function readLedgerSync(path, schema) {
|
|
|
2750
3458
|
return interpret(raw, schema, path);
|
|
2751
3459
|
}
|
|
2752
3460
|
async function writeLedger(path, body) {
|
|
2753
|
-
await
|
|
3461
|
+
await mkdir5(dirname5(path), { recursive: true });
|
|
2754
3462
|
const temp = `${path}.${randomUUID()}.tmp`;
|
|
2755
3463
|
try {
|
|
2756
3464
|
const handle = await open(temp, "wx", 384);
|
|
@@ -2760,15 +3468,15 @@ async function writeLedger(path, body) {
|
|
|
2760
3468
|
} finally {
|
|
2761
3469
|
await handle.close();
|
|
2762
3470
|
}
|
|
2763
|
-
await
|
|
3471
|
+
await rename3(temp, path);
|
|
2764
3472
|
} catch (error) {
|
|
2765
3473
|
await rm2(temp, { force: true });
|
|
2766
3474
|
throw error;
|
|
2767
3475
|
}
|
|
2768
|
-
await syncDirectory(
|
|
3476
|
+
await syncDirectory(dirname5(path));
|
|
2769
3477
|
}
|
|
2770
3478
|
function writeLedgerSync(path, body) {
|
|
2771
|
-
mkdirSync(
|
|
3479
|
+
mkdirSync(dirname5(path), { recursive: true });
|
|
2772
3480
|
const temp = `${path}.${randomUUID()}.tmp`;
|
|
2773
3481
|
try {
|
|
2774
3482
|
const fd = openSync(temp, "wx", 384);
|
|
@@ -2783,7 +3491,7 @@ function writeLedgerSync(path, body) {
|
|
|
2783
3491
|
rmSync(temp, { force: true });
|
|
2784
3492
|
throw error;
|
|
2785
3493
|
}
|
|
2786
|
-
syncDirectorySync(
|
|
3494
|
+
syncDirectorySync(dirname5(path));
|
|
2787
3495
|
}
|
|
2788
3496
|
async function syncDirectory(path) {
|
|
2789
3497
|
try {
|
|
@@ -2833,13 +3541,13 @@ var LedgerWriter = class {
|
|
|
2833
3541
|
};
|
|
2834
3542
|
|
|
2835
3543
|
// src/spend.ts
|
|
2836
|
-
var SpendFile =
|
|
2837
|
-
version:
|
|
3544
|
+
var SpendFile = z3.object({
|
|
3545
|
+
version: z3.literal(1),
|
|
2838
3546
|
/** Per backend key: epoch-ms timestamps and the cents charged. */
|
|
2839
|
-
entries:
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
3547
|
+
entries: z3.record(
|
|
3548
|
+
z3.string(),
|
|
3549
|
+
z3.array(
|
|
3550
|
+
z3.object({ at: z3.number().int().positive(), cents: z3.number().min(0) })
|
|
2843
3551
|
)
|
|
2844
3552
|
)
|
|
2845
3553
|
}).strict();
|
|
@@ -3611,7 +4319,7 @@ async function askCap(io, row) {
|
|
|
3611
4319
|
}
|
|
3612
4320
|
async function readExistingConfig(path) {
|
|
3613
4321
|
try {
|
|
3614
|
-
const raw = await
|
|
4322
|
+
const raw = await readFile9(path, "utf8");
|
|
3615
4323
|
const parsed = JSON.parse(raw);
|
|
3616
4324
|
if (typeof parsed !== "object" || parsed === null)
|
|
3617
4325
|
return { services: {}, rest: {} };
|
|
@@ -3885,17 +4593,21 @@ import { backendDescriptor as backendDescriptor4, backendName as backendName3, c
|
|
|
3885
4593
|
import { fingerprint as fingerprint3 } from "@byollm/protocol";
|
|
3886
4594
|
|
|
3887
4595
|
// src/budgets.ts
|
|
3888
|
-
import { z as
|
|
3889
|
-
var BudgetFile =
|
|
3890
|
-
version:
|
|
4596
|
+
import { z as z4 } from "zod";
|
|
4597
|
+
var BudgetFile = z4.object({
|
|
4598
|
+
version: z4.literal(1),
|
|
3891
4599
|
/** Epoch-ms timestamps of community jobs accepted, newest last. */
|
|
3892
|
-
accepted:
|
|
4600
|
+
accepted: z4.array(z4.number().int().positive())
|
|
3893
4601
|
}).strict();
|
|
3894
|
-
var BudgetRefusal =
|
|
4602
|
+
var BudgetRefusal = z4.enum([
|
|
3895
4603
|
"hourly-cap",
|
|
3896
4604
|
"daily-cap",
|
|
3897
4605
|
"payload-too-large",
|
|
3898
|
-
"ledger-untrusted"
|
|
4606
|
+
"ledger-untrusted",
|
|
4607
|
+
/* A teammate's images over `community.maxImageBytes`, charged from the
|
|
4608
|
+
declared sizes before any download — byollm_021 §3. Charged at the
|
|
4609
|
+
opened payload, not by this ledger. */
|
|
4610
|
+
"image-bytes"
|
|
3899
4611
|
]);
|
|
3900
4612
|
var Budgets = class {
|
|
3901
4613
|
#path;
|
|
@@ -4005,6 +4717,8 @@ var Budgets = class {
|
|
|
4005
4717
|
|
|
4006
4718
|
// src/client.ts
|
|
4007
4719
|
import {
|
|
4720
|
+
BlobSlotResponse,
|
|
4721
|
+
BlobUrlResponse,
|
|
4008
4722
|
ClaimResponse,
|
|
4009
4723
|
FetchResponse,
|
|
4010
4724
|
HeartbeatResponse,
|
|
@@ -4013,17 +4727,22 @@ import {
|
|
|
4013
4727
|
PairStartResponse,
|
|
4014
4728
|
ReleaseResponse,
|
|
4015
4729
|
ResultResponse,
|
|
4016
|
-
WireError
|
|
4730
|
+
WireError,
|
|
4731
|
+
ToolCallResponse,
|
|
4732
|
+
ToolResultsResponse,
|
|
4733
|
+
isToolsSpentRefusal
|
|
4017
4734
|
} from "@byollm/protocol";
|
|
4018
4735
|
import "zod";
|
|
4019
4736
|
var ClientError = class extends Error {
|
|
4020
|
-
constructor(kind, message, retryAfter) {
|
|
4737
|
+
constructor(kind, message, retryAfter, status) {
|
|
4021
4738
|
super(message);
|
|
4022
4739
|
this.kind = kind;
|
|
4023
4740
|
this.retryAfter = retryAfter;
|
|
4741
|
+
this.status = status;
|
|
4024
4742
|
}
|
|
4025
4743
|
kind;
|
|
4026
4744
|
retryAfter;
|
|
4745
|
+
status;
|
|
4027
4746
|
name = "ClientError";
|
|
4028
4747
|
/** Is retrying this same call plausibly useful? */
|
|
4029
4748
|
get retryable() {
|
|
@@ -4120,6 +4839,157 @@ var ProtocolClient = class _ProtocolClient {
|
|
|
4120
4839
|
reason: input.reason
|
|
4121
4840
|
});
|
|
4122
4841
|
}
|
|
4842
|
+
/**
|
|
4843
|
+
* Send one tool call, sealed to the job's site — byollm_012 §5.2.
|
|
4844
|
+
*
|
|
4845
|
+
* Lease-scoped: only the device holding the job may call for it.
|
|
4846
|
+
*/
|
|
4847
|
+
async toolCall(input) {
|
|
4848
|
+
return this.#post("tool-call", ToolCallResponse, {
|
|
4849
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
4850
|
+
...input
|
|
4851
|
+
});
|
|
4852
|
+
}
|
|
4853
|
+
/**
|
|
4854
|
+
* What the site has answered, for the calls named — byollm_012 §5.2.
|
|
4855
|
+
*
|
|
4856
|
+
* Each call by job, lease and id, so the read is lease-scoped like every
|
|
4857
|
+
* other holder call.
|
|
4858
|
+
*/
|
|
4859
|
+
async toolResults(input) {
|
|
4860
|
+
return this.#post("tool-results", ToolResultsResponse, {
|
|
4861
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
4862
|
+
runnerId: input.runnerId,
|
|
4863
|
+
calls: input.calls
|
|
4864
|
+
});
|
|
4865
|
+
}
|
|
4866
|
+
/**
|
|
4867
|
+
* A download URL for one input blob of a job whose lease this daemon holds
|
|
4868
|
+
* — byollm_021 §2. No lease, no URL.
|
|
4869
|
+
*/
|
|
4870
|
+
async blobUrl(input) {
|
|
4871
|
+
return this.#post("blob-url", BlobUrlResponse, {
|
|
4872
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
4873
|
+
...input
|
|
4874
|
+
});
|
|
4875
|
+
}
|
|
4876
|
+
/**
|
|
4877
|
+
* An upload slot for one output blob of an `llm.image` job whose lease this
|
|
4878
|
+
* daemon holds, for an object of exactly `bytes` (`blobObjectBytes`).
|
|
4879
|
+
*/
|
|
4880
|
+
async blobSlot(input) {
|
|
4881
|
+
return this.#post("blob-slot", BlobSlotResponse, {
|
|
4882
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
4883
|
+
...input
|
|
4884
|
+
});
|
|
4885
|
+
}
|
|
4886
|
+
/**
|
|
4887
|
+
* GET one blob's ciphertext, **stopping at `size` bytes** — byollm_021 §3,
|
|
4888
|
+
* {@link MUSTS.BLOB_BUDGET_BEFORE_DOWNLOAD} (C052).
|
|
4889
|
+
*
|
|
4890
|
+
* `size` is the object size the reference declares (`blobObjectBytes`). A
|
|
4891
|
+
* `Content-Length` over it is refused before the body is read, and a body
|
|
4892
|
+
* that runs past it is cancelled at the first chunk that crosses it, so a
|
|
4893
|
+
* store holding 12 MiB under a 2 MiB reference costs this device at most
|
|
4894
|
+
* 2 MiB and one chunk — never the 12. Fewer bytes than declared is refused
|
|
4895
|
+
* too: `decryptBlob` opens nothing but the exact size.
|
|
4896
|
+
*
|
|
4897
|
+
* The URL is the upstream's answer to {@link blobUrl}, a capability on
|
|
4898
|
+
* presigned object storage or on the direct-lane server — never a URL a
|
|
4899
|
+
* backend or a payload chose. Redirects are refused, as on every call here.
|
|
4900
|
+
*/
|
|
4901
|
+
async getBlob(url, size, signal) {
|
|
4902
|
+
const target = blobTarget(url);
|
|
4903
|
+
if (target === void 0) {
|
|
4904
|
+
return { ok: false, message: "the upstream's blob URL is not http(s)" };
|
|
4905
|
+
}
|
|
4906
|
+
let response;
|
|
4907
|
+
try {
|
|
4908
|
+
response = await this.#fetch(target, {
|
|
4909
|
+
method: "GET",
|
|
4910
|
+
redirect: "error",
|
|
4911
|
+
signal: AbortSignal.any([
|
|
4912
|
+
AbortSignal.timeout(this.#timeoutMs * 4),
|
|
4913
|
+
...signal === void 0 ? [] : [signal]
|
|
4914
|
+
])
|
|
4915
|
+
});
|
|
4916
|
+
} catch (error) {
|
|
4917
|
+
return {
|
|
4918
|
+
ok: false,
|
|
4919
|
+
message: `could not download a blob (${error instanceof Error ? error.message : "unknown error"})`
|
|
4920
|
+
};
|
|
4921
|
+
}
|
|
4922
|
+
if (!response.ok) {
|
|
4923
|
+
await response.body?.cancel();
|
|
4924
|
+
return {
|
|
4925
|
+
ok: false,
|
|
4926
|
+
message: `the blob store returned HTTP ${String(response.status)}`
|
|
4927
|
+
};
|
|
4928
|
+
}
|
|
4929
|
+
const length = response.headers.get("content-length");
|
|
4930
|
+
if (length !== null && /^\d+$/.test(length) && Number(length) > size) {
|
|
4931
|
+
await response.body?.cancel();
|
|
4932
|
+
return { ok: false, message: LARGER_THAN_DECLARED };
|
|
4933
|
+
}
|
|
4934
|
+
const body = response.body;
|
|
4935
|
+
if (body === null) return { ok: false, message: SMALLER_THAN_DECLARED };
|
|
4936
|
+
const reader = body.getReader();
|
|
4937
|
+
const out = new Uint8Array(size);
|
|
4938
|
+
let total = 0;
|
|
4939
|
+
try {
|
|
4940
|
+
for (; ; ) {
|
|
4941
|
+
const { done, value } = await reader.read();
|
|
4942
|
+
if (done) break;
|
|
4943
|
+
if (total + value.byteLength > size) {
|
|
4944
|
+
await reader.cancel();
|
|
4945
|
+
return { ok: false, message: LARGER_THAN_DECLARED };
|
|
4946
|
+
}
|
|
4947
|
+
out.set(value, total);
|
|
4948
|
+
total += value.byteLength;
|
|
4949
|
+
}
|
|
4950
|
+
} catch (error) {
|
|
4951
|
+
return {
|
|
4952
|
+
ok: false,
|
|
4953
|
+
message: `a blob download failed (${error instanceof Error ? error.message : "unknown error"})`
|
|
4954
|
+
};
|
|
4955
|
+
} finally {
|
|
4956
|
+
reader.releaseLock();
|
|
4957
|
+
}
|
|
4958
|
+
if (total !== size) return { ok: false, message: SMALLER_THAN_DECLARED };
|
|
4959
|
+
return { ok: true, bytes: out };
|
|
4960
|
+
}
|
|
4961
|
+
/** PUT one output blob's ciphertext to the slot the upstream issued. */
|
|
4962
|
+
async putBlob(url, object, signal) {
|
|
4963
|
+
const target = blobTarget(url);
|
|
4964
|
+
if (target === void 0) {
|
|
4965
|
+
return { ok: false, message: "the upstream's slot URL is not http(s)" };
|
|
4966
|
+
}
|
|
4967
|
+
try {
|
|
4968
|
+
const response = await this.#fetch(target, {
|
|
4969
|
+
method: "PUT",
|
|
4970
|
+
headers: {
|
|
4971
|
+
"content-type": "application/octet-stream",
|
|
4972
|
+
"content-length": String(object.byteLength)
|
|
4973
|
+
},
|
|
4974
|
+
body: object,
|
|
4975
|
+
redirect: "error",
|
|
4976
|
+
signal: AbortSignal.any([
|
|
4977
|
+
AbortSignal.timeout(this.#timeoutMs * 4),
|
|
4978
|
+
...signal === void 0 ? [] : [signal]
|
|
4979
|
+
])
|
|
4980
|
+
});
|
|
4981
|
+
await response.body?.cancel();
|
|
4982
|
+
return response.ok ? { ok: true } : {
|
|
4983
|
+
ok: false,
|
|
4984
|
+
message: `the blob store refused an upload with HTTP ${String(response.status)}`
|
|
4985
|
+
};
|
|
4986
|
+
} catch (error) {
|
|
4987
|
+
return {
|
|
4988
|
+
ok: false,
|
|
4989
|
+
message: `could not upload a blob (${error instanceof Error ? error.message : "unknown error"})`
|
|
4990
|
+
};
|
|
4991
|
+
}
|
|
4992
|
+
}
|
|
4123
4993
|
async #post(endpoint, schema, body) {
|
|
4124
4994
|
const headers = {
|
|
4125
4995
|
"content-type": "application/json",
|
|
@@ -4159,11 +5029,19 @@ var ProtocolClient = class _ProtocolClient {
|
|
|
4159
5029
|
} catch {
|
|
4160
5030
|
throw new ClientError(
|
|
4161
5031
|
"malformed-response",
|
|
4162
|
-
`${this.#origin} returned HTTP ${String(response.status)} with a body that is not JSON
|
|
5032
|
+
`${this.#origin} returned HTTP ${String(response.status)} with a body that is not JSON`,
|
|
5033
|
+
void 0,
|
|
5034
|
+
response.status
|
|
4163
5035
|
);
|
|
4164
5036
|
}
|
|
4165
5037
|
if (!response.ok) {
|
|
4166
|
-
|
|
5038
|
+
const error = this.#toError(response, parsed);
|
|
5039
|
+
throw new ClientError(
|
|
5040
|
+
error.kind,
|
|
5041
|
+
error.message,
|
|
5042
|
+
error.retryAfter,
|
|
5043
|
+
response.status
|
|
5044
|
+
);
|
|
4167
5045
|
}
|
|
4168
5046
|
const result = schema.safeParse(parsed);
|
|
4169
5047
|
if (!result.success) {
|
|
@@ -4182,6 +5060,9 @@ var ProtocolClient = class _ProtocolClient {
|
|
|
4182
5060
|
if (wire.success && wire.data.error === "revoked") {
|
|
4183
5061
|
return new ClientError("revoked", message, retryAfter);
|
|
4184
5062
|
}
|
|
5063
|
+
if (wire.success && isToolsSpentRefusal(wire.data)) {
|
|
5064
|
+
return new ClientError("tools-spent", message, retryAfter);
|
|
5065
|
+
}
|
|
4185
5066
|
if (typeof body === "object" && body !== null && body.error === "unsupported-protocol-version") {
|
|
4186
5067
|
return new ClientError("version-unsupported", message, retryAfter);
|
|
4187
5068
|
}
|
|
@@ -4217,6 +5098,16 @@ var ProtocolClient = class _ProtocolClient {
|
|
|
4217
5098
|
}
|
|
4218
5099
|
}
|
|
4219
5100
|
};
|
|
5101
|
+
var LARGER_THAN_DECLARED = "a blob was larger than its reference declares, so its download was stopped at the declared size";
|
|
5102
|
+
var SMALLER_THAN_DECLARED = "a blob was smaller than its reference declares";
|
|
5103
|
+
function blobTarget(url) {
|
|
5104
|
+
try {
|
|
5105
|
+
const parsed = new URL(url);
|
|
5106
|
+
return parsed.protocol === "https:" || parsed.protocol === "http:" ? parsed : void 0;
|
|
5107
|
+
} catch {
|
|
5108
|
+
return void 0;
|
|
5109
|
+
}
|
|
5110
|
+
}
|
|
4220
5111
|
function syncTimeCommand() {
|
|
4221
5112
|
switch (process.platform) {
|
|
4222
5113
|
case "darwin":
|
|
@@ -4481,15 +5372,15 @@ var defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
4481
5372
|
|
|
4482
5373
|
// src/ingress.ts
|
|
4483
5374
|
import { createHash } from "crypto";
|
|
4484
|
-
import { appendFile, mkdir as
|
|
4485
|
-
import { dirname as
|
|
4486
|
-
import { z as
|
|
4487
|
-
var PromptEntry =
|
|
4488
|
-
type:
|
|
4489
|
-
at:
|
|
5375
|
+
import { appendFile, mkdir as mkdir6, readFile as readFile10, writeFile as writeFile5 } from "fs/promises";
|
|
5376
|
+
import { dirname as dirname6 } from "path";
|
|
5377
|
+
import { z as z5 } from "zod";
|
|
5378
|
+
var PromptEntry = z5.object({
|
|
5379
|
+
type: z5.literal("prompt"),
|
|
5380
|
+
at: z5.number().int().positive(),
|
|
4490
5381
|
/** Origin of the server that sent the job. */
|
|
4491
|
-
origin:
|
|
4492
|
-
jobId:
|
|
5382
|
+
origin: z5.string().min(1),
|
|
5383
|
+
jobId: z5.string().min(1),
|
|
4493
5384
|
/**
|
|
4494
5385
|
* Which site sent it — V1-3.
|
|
4495
5386
|
*
|
|
@@ -4500,33 +5391,33 @@ var PromptEntry = z4.object({
|
|
|
4500
5391
|
* reader that refused them would be a meter that stops working when it
|
|
4501
5392
|
* changes.
|
|
4502
5393
|
*/
|
|
4503
|
-
site:
|
|
4504
|
-
kind:
|
|
4505
|
-
audience:
|
|
5394
|
+
site: z5.string().min(1).optional(),
|
|
5395
|
+
kind: z5.string().min(1),
|
|
5396
|
+
audience: z5.string().min(1),
|
|
4506
5397
|
/** Who this prompt is *for* — which, for community work, is not you. */
|
|
4507
|
-
owner:
|
|
4508
|
-
backendId:
|
|
4509
|
-
backendClass:
|
|
4510
|
-
model:
|
|
5398
|
+
owner: z5.string().min(1),
|
|
5399
|
+
backendId: z5.string().min(1),
|
|
5400
|
+
backendClass: z5.string().min(1),
|
|
5401
|
+
model: z5.string().min(1),
|
|
4511
5402
|
/** Always present, including after retention drops the text. */
|
|
4512
|
-
promptHash:
|
|
4513
|
-
promptChars:
|
|
5403
|
+
promptHash: z5.string().length(64),
|
|
5404
|
+
promptChars: z5.number().int().nonnegative(),
|
|
4514
5405
|
/**
|
|
4515
5406
|
* The prompt itself. Absent means "not retained" — distinct from an empty
|
|
4516
5407
|
* prompt, which the protocol forbids anyway. Zero and unknown never look
|
|
4517
5408
|
* alike.
|
|
4518
5409
|
*/
|
|
4519
|
-
prompt:
|
|
5410
|
+
prompt: z5.string().optional()
|
|
4520
5411
|
}).strict();
|
|
4521
|
-
var OutcomeEntry =
|
|
4522
|
-
type:
|
|
4523
|
-
at:
|
|
4524
|
-
jobId:
|
|
5412
|
+
var OutcomeEntry = z5.object({
|
|
5413
|
+
type: z5.literal("outcome"),
|
|
5414
|
+
at: z5.number().int().positive(),
|
|
5415
|
+
jobId: z5.string().min(1),
|
|
4525
5416
|
/** Which site's job — V1-3, same reason as the prompt line. */
|
|
4526
|
-
site:
|
|
4527
|
-
outcome:
|
|
5417
|
+
site: z5.string().min(1).optional(),
|
|
5418
|
+
outcome: z5.enum(["ok", "error", "canceled", "refused"]),
|
|
4528
5419
|
/** Present for a job that ran; absent for one refused before execution. */
|
|
4529
|
-
durationMs:
|
|
5420
|
+
durationMs: z5.number().int().nonnegative().optional(),
|
|
4530
5421
|
/**
|
|
4531
5422
|
* Where that duration went, as far as it can be seen — B195.
|
|
4532
5423
|
*
|
|
@@ -4539,11 +5430,11 @@ var OutcomeEntry = z4.object({
|
|
|
4539
5430
|
* that wrote it, and a reader that required them would refuse every line
|
|
4540
5431
|
* written before today.
|
|
4541
5432
|
*/
|
|
4542
|
-
spawnMs:
|
|
4543
|
-
firstOutputMs:
|
|
4544
|
-
outputChars:
|
|
5433
|
+
spawnMs: z5.number().int().nonnegative().optional(),
|
|
5434
|
+
firstOutputMs: z5.number().int().nonnegative().optional(),
|
|
5435
|
+
outputChars: z5.number().int().nonnegative().optional(),
|
|
4545
5436
|
/** Why, for `error` and `refused`. */
|
|
4546
|
-
detail:
|
|
5437
|
+
detail: z5.string().optional(),
|
|
4547
5438
|
/**
|
|
4548
5439
|
* Why generation stopped — B064 step 3.
|
|
4549
5440
|
*
|
|
@@ -4568,7 +5459,7 @@ var OutcomeEntry = z4.object({
|
|
|
4568
5459
|
* reason a log is a log: if an adapter gains a signal in a later release,
|
|
4569
5460
|
* entries written before that should still read as they were true.
|
|
4570
5461
|
*/
|
|
4571
|
-
stopKind:
|
|
5462
|
+
stopKind: z5.enum(["declared", "unavailable", "unverified"]).optional(),
|
|
4572
5463
|
/**
|
|
4573
5464
|
* Which community brake refused this job, when one did — 10-22.
|
|
4574
5465
|
*
|
|
@@ -4583,24 +5474,60 @@ var OutcomeEntry = z4.object({
|
|
|
4583
5474
|
*/
|
|
4584
5475
|
brake: BudgetRefusal.optional()
|
|
4585
5476
|
}).strict();
|
|
4586
|
-
var MemoryEntry =
|
|
4587
|
-
type:
|
|
4588
|
-
at:
|
|
4589
|
-
jobId:
|
|
4590
|
-
backendId:
|
|
4591
|
-
admit:
|
|
4592
|
-
why:
|
|
5477
|
+
var MemoryEntry = z5.object({
|
|
5478
|
+
type: z5.literal("memory"),
|
|
5479
|
+
at: z5.number().int().positive(),
|
|
5480
|
+
jobId: z5.string().min(1).optional(),
|
|
5481
|
+
backendId: z5.string().min(1),
|
|
5482
|
+
admit: z5.boolean(),
|
|
5483
|
+
why: z5.string().min(1),
|
|
4593
5484
|
/** Absent when memory could not be read — that is the guard being off. */
|
|
4594
|
-
availableBytes:
|
|
4595
|
-
totalBytes:
|
|
4596
|
-
swapFreeBytes:
|
|
4597
|
-
swapTotalBytes:
|
|
4598
|
-
pressure:
|
|
5485
|
+
availableBytes: z5.number().int().nonnegative().optional(),
|
|
5486
|
+
totalBytes: z5.number().int().nonnegative().optional(),
|
|
5487
|
+
swapFreeBytes: z5.number().int().nonnegative().optional(),
|
|
5488
|
+
swapTotalBytes: z5.number().int().nonnegative().optional(),
|
|
5489
|
+
pressure: z5.enum(["normal", "warn", "critical", "unknown"])
|
|
5490
|
+
}).strict();
|
|
5491
|
+
var ToolCallEntry = z5.object({
|
|
5492
|
+
type: z5.literal("tool-call"),
|
|
5493
|
+
at: z5.number().int().positive(),
|
|
5494
|
+
jobId: z5.string().min(1),
|
|
5495
|
+
site: z5.string().min(1).optional(),
|
|
5496
|
+
audience: z5.string().min(1),
|
|
5497
|
+
callId: z5.string().min(1),
|
|
5498
|
+
seq: z5.number().int().positive(),
|
|
5499
|
+
name: z5.string(),
|
|
5500
|
+
argumentsHash: z5.string().length(64),
|
|
5501
|
+
argumentsChars: z5.number().int().nonnegative(),
|
|
5502
|
+
arguments: z5.string().optional()
|
|
5503
|
+
}).strict();
|
|
5504
|
+
var ToolOutcomeEntry = z5.object({
|
|
5505
|
+
type: z5.literal("tool-outcome"),
|
|
5506
|
+
at: z5.number().int().positive(),
|
|
5507
|
+
jobId: z5.string().min(1),
|
|
5508
|
+
site: z5.string().min(1).optional(),
|
|
5509
|
+
callId: z5.string().min(1),
|
|
5510
|
+
seq: z5.number().int().positive(),
|
|
5511
|
+
outcome: z5.enum(["answered", "errored", "timedOut", "refused"]),
|
|
5512
|
+
chars: z5.number().int().nonnegative().optional(),
|
|
5513
|
+
contentHash: z5.string().length(64).optional(),
|
|
5514
|
+
detail: z5.string().optional()
|
|
5515
|
+
}).strict();
|
|
5516
|
+
var ToolDroppedEntry = z5.object({
|
|
5517
|
+
type: z5.literal("tool-dropped"),
|
|
5518
|
+
at: z5.number().int().positive(),
|
|
5519
|
+
jobId: z5.string().min(1),
|
|
5520
|
+
site: z5.string().min(1).optional(),
|
|
5521
|
+
callId: z5.string().optional(),
|
|
5522
|
+
why: z5.string().min(1)
|
|
4599
5523
|
}).strict();
|
|
4600
|
-
var IngressEntry =
|
|
5524
|
+
var IngressEntry = z5.discriminatedUnion("type", [
|
|
4601
5525
|
PromptEntry,
|
|
4602
5526
|
OutcomeEntry,
|
|
4603
|
-
MemoryEntry
|
|
5527
|
+
MemoryEntry,
|
|
5528
|
+
ToolCallEntry,
|
|
5529
|
+
ToolOutcomeEntry,
|
|
5530
|
+
ToolDroppedEntry
|
|
4604
5531
|
]);
|
|
4605
5532
|
var IngressLog = class {
|
|
4606
5533
|
#options;
|
|
@@ -4646,6 +5573,51 @@ var IngressLog = class {
|
|
|
4646
5573
|
});
|
|
4647
5574
|
}
|
|
4648
5575
|
/** Record what the memory guard decided — B080. */
|
|
5576
|
+
/** A tool call, before it is sealed — see {@link ToolCallEntry}. */
|
|
5577
|
+
async recordToolCall(input) {
|
|
5578
|
+
const keepText = input.audience === "private" ? this.#options.keepSelfPrompts : true;
|
|
5579
|
+
await this.#append({
|
|
5580
|
+
type: "tool-call",
|
|
5581
|
+
at: input.at,
|
|
5582
|
+
jobId: input.jobId,
|
|
5583
|
+
...input.site === void 0 ? {} : { site: input.site },
|
|
5584
|
+
audience: input.audience,
|
|
5585
|
+
callId: input.callId,
|
|
5586
|
+
seq: input.seq,
|
|
5587
|
+
name: input.name,
|
|
5588
|
+
argumentsHash: hashText(input.arguments),
|
|
5589
|
+
argumentsChars: input.arguments.length,
|
|
5590
|
+
...keepText ? { arguments: input.arguments } : {}
|
|
5591
|
+
});
|
|
5592
|
+
}
|
|
5593
|
+
/** How a tool call ended — see {@link ToolOutcomeEntry}. */
|
|
5594
|
+
async recordToolOutcome(input) {
|
|
5595
|
+
await this.#append({
|
|
5596
|
+
type: "tool-outcome",
|
|
5597
|
+
at: input.at,
|
|
5598
|
+
jobId: input.jobId,
|
|
5599
|
+
...input.site === void 0 ? {} : { site: input.site },
|
|
5600
|
+
callId: input.callId,
|
|
5601
|
+
seq: input.seq,
|
|
5602
|
+
outcome: input.outcome,
|
|
5603
|
+
...input.content === void 0 ? {} : {
|
|
5604
|
+
chars: input.content.length,
|
|
5605
|
+
contentHash: hashText(input.content)
|
|
5606
|
+
},
|
|
5607
|
+
...input.detail === void 0 ? {} : { detail: input.detail }
|
|
5608
|
+
});
|
|
5609
|
+
}
|
|
5610
|
+
/** A result the model never saw — see {@link ToolDroppedEntry}. */
|
|
5611
|
+
async recordToolDropped(input) {
|
|
5612
|
+
await this.#append({
|
|
5613
|
+
type: "tool-dropped",
|
|
5614
|
+
at: input.at,
|
|
5615
|
+
jobId: input.jobId,
|
|
5616
|
+
...input.site === void 0 ? {} : { site: input.site },
|
|
5617
|
+
...input.callId === void 0 ? {} : { callId: input.callId },
|
|
5618
|
+
why: input.why
|
|
5619
|
+
});
|
|
5620
|
+
}
|
|
4649
5621
|
async recordMemory(input) {
|
|
4650
5622
|
const read = input.memory.kind === "read" ? input.memory : void 0;
|
|
4651
5623
|
await this.#append({
|
|
@@ -4665,7 +5637,7 @@ var IngressLog = class {
|
|
|
4665
5637
|
});
|
|
4666
5638
|
}
|
|
4667
5639
|
async #append(entry) {
|
|
4668
|
-
await
|
|
5640
|
+
await mkdir6(dirname6(this.#options.path), { recursive: true });
|
|
4669
5641
|
await appendFile(this.#options.path, `${JSON.stringify(entry)}
|
|
4670
5642
|
`, {
|
|
4671
5643
|
mode: 384
|
|
@@ -4675,7 +5647,7 @@ var IngressLog = class {
|
|
|
4675
5647
|
async read() {
|
|
4676
5648
|
let raw;
|
|
4677
5649
|
try {
|
|
4678
|
-
raw = await
|
|
5650
|
+
raw = await readFile10(this.#options.path, "utf8");
|
|
4679
5651
|
} catch {
|
|
4680
5652
|
return [];
|
|
4681
5653
|
}
|
|
@@ -4704,6 +5676,14 @@ var IngressLog = class {
|
|
|
4704
5676
|
const cutoff = now - this.#options.communityPromptDays * 864e5;
|
|
4705
5677
|
let reduced = 0;
|
|
4706
5678
|
const kept = entries.map((entry) => {
|
|
5679
|
+
if (entry.type === "tool-call") {
|
|
5680
|
+
if (entry.audience === "private" || entry.arguments === void 0 || entry.at >= cutoff) {
|
|
5681
|
+
return entry;
|
|
5682
|
+
}
|
|
5683
|
+
reduced += 1;
|
|
5684
|
+
const { arguments: _dropped2, ...rest2 } = entry;
|
|
5685
|
+
return rest2;
|
|
5686
|
+
}
|
|
4707
5687
|
if (entry.type !== "prompt") return entry;
|
|
4708
5688
|
const isCommunity = entry.audience !== "private";
|
|
4709
5689
|
if (!isCommunity || entry.prompt === void 0 || entry.at >= cutoff) {
|
|
@@ -4714,7 +5694,7 @@ var IngressLog = class {
|
|
|
4714
5694
|
return rest;
|
|
4715
5695
|
});
|
|
4716
5696
|
if (reduced > 0) {
|
|
4717
|
-
await
|
|
5697
|
+
await writeFile5(
|
|
4718
5698
|
this.#options.path,
|
|
4719
5699
|
`${kept.map((entry) => JSON.stringify(entry)).join("\n")}
|
|
4720
5700
|
`,
|
|
@@ -4874,13 +5854,13 @@ import { randomUUID as randomUUID2 } from "crypto";
|
|
|
4874
5854
|
import {
|
|
4875
5855
|
chmod,
|
|
4876
5856
|
link,
|
|
4877
|
-
mkdir as
|
|
4878
|
-
readFile as
|
|
5857
|
+
mkdir as mkdir7,
|
|
5858
|
+
readFile as readFile11,
|
|
4879
5859
|
rm as rm3,
|
|
4880
5860
|
stat,
|
|
4881
|
-
writeFile as
|
|
5861
|
+
writeFile as writeFile6
|
|
4882
5862
|
} from "fs/promises";
|
|
4883
|
-
import { dirname as
|
|
5863
|
+
import { dirname as dirname7 } from "path";
|
|
4884
5864
|
import {
|
|
4885
5865
|
StoredKeys,
|
|
4886
5866
|
fingerprint,
|
|
@@ -4909,7 +5889,7 @@ var DeviceIdentity = class {
|
|
|
4909
5889
|
if (this.#keys) return this.#keys;
|
|
4910
5890
|
let raw;
|
|
4911
5891
|
try {
|
|
4912
|
-
raw = await
|
|
5892
|
+
raw = await readFile11(this.#path, "utf8");
|
|
4913
5893
|
} catch (error) {
|
|
4914
5894
|
if (!isNotFound2(error)) throw error;
|
|
4915
5895
|
return this.#create(now);
|
|
@@ -4951,9 +5931,9 @@ var DeviceIdentity = class {
|
|
|
4951
5931
|
}
|
|
4952
5932
|
async #create(now) {
|
|
4953
5933
|
const keys = generateKeys(now);
|
|
4954
|
-
await
|
|
5934
|
+
await mkdir7(dirname7(this.#path), { recursive: true });
|
|
4955
5935
|
const temp = `${this.#path}.${randomUUID2()}.tmp`;
|
|
4956
|
-
await
|
|
5936
|
+
await writeFile6(temp, JSON.stringify(keys, null, 2), { mode: 384 });
|
|
4957
5937
|
try {
|
|
4958
5938
|
await link(temp, this.#path);
|
|
4959
5939
|
} catch (error) {
|
|
@@ -4965,7 +5945,7 @@ var DeviceIdentity = class {
|
|
|
4965
5945
|
if (code === "EPERM" || code === "ENOSYS" || code === "EXDEV") {
|
|
4966
5946
|
await rm3(temp, { force: true });
|
|
4967
5947
|
try {
|
|
4968
|
-
await
|
|
5948
|
+
await writeFile6(this.#path, JSON.stringify(keys, null, 2), {
|
|
4969
5949
|
mode: 384,
|
|
4970
5950
|
flag: "wx"
|
|
4971
5951
|
});
|
|
@@ -5399,20 +6379,20 @@ function consoleAgentUnavailable(error) {
|
|
|
5399
6379
|
}
|
|
5400
6380
|
|
|
5401
6381
|
// src/console-agent-spec.ts
|
|
5402
|
-
import { z as
|
|
6382
|
+
import { z as z6 } from "zod";
|
|
5403
6383
|
import { PublicIdentity } from "@byollm/protocol";
|
|
5404
|
-
var ConsoleAgentSpec =
|
|
6384
|
+
var ConsoleAgentSpec = z6.object({
|
|
5405
6385
|
/** `wss://…`, the broker endpoint for this session. */
|
|
5406
|
-
url:
|
|
5407
|
-
sessionId:
|
|
5408
|
-
deadlineAt:
|
|
6386
|
+
url: z6.string().min(1),
|
|
6387
|
+
sessionId: z6.string().min(1),
|
|
6388
|
+
deadlineAt: z6.number().int().positive(),
|
|
5409
6389
|
/** The browser's ephemeral identity, pinned for this session. */
|
|
5410
6390
|
browser: PublicIdentity,
|
|
5411
|
-
shell:
|
|
5412
|
-
command:
|
|
5413
|
-
args:
|
|
5414
|
-
cwd:
|
|
5415
|
-
env:
|
|
6391
|
+
shell: z6.object({
|
|
6392
|
+
command: z6.string().min(1),
|
|
6393
|
+
args: z6.array(z6.string()).default([]),
|
|
6394
|
+
cwd: z6.string().min(1),
|
|
6395
|
+
env: z6.record(z6.string(), z6.string()).default({})
|
|
5416
6396
|
}).strict()
|
|
5417
6397
|
}).strict();
|
|
5418
6398
|
|
|
@@ -5580,16 +6560,16 @@ function ConsoleAnnouncement(value) {
|
|
|
5580
6560
|
|
|
5581
6561
|
// src/pairings.ts
|
|
5582
6562
|
import { randomUUID as randomUUID3 } from "crypto";
|
|
5583
|
-
import { mkdir as
|
|
5584
|
-
import { dirname as
|
|
6563
|
+
import { mkdir as mkdir8, readFile as readFile12, rename as rename4, rm as rm4, writeFile as writeFile7 } from "fs/promises";
|
|
6564
|
+
import { dirname as dirname8 } from "path";
|
|
5585
6565
|
import { PublicIdentity as PublicIdentity2 } from "@byollm/protocol";
|
|
5586
|
-
import { z as
|
|
5587
|
-
var Pairing =
|
|
5588
|
-
origin:
|
|
5589
|
-
runnerId:
|
|
6566
|
+
import { z as z7 } from "zod";
|
|
6567
|
+
var Pairing = z7.object({
|
|
6568
|
+
origin: z7.string().min(1),
|
|
6569
|
+
runnerId: z7.string().min(1),
|
|
5590
6570
|
/** The app's id for this daemon's owner. */
|
|
5591
|
-
owner:
|
|
5592
|
-
ownerLabel:
|
|
6571
|
+
owner: z7.string().min(1),
|
|
6572
|
+
ownerLabel: z7.string().optional(),
|
|
5593
6573
|
/**
|
|
5594
6574
|
* The sites this pairing covers, keyed by each site's identity key id.
|
|
5595
6575
|
*
|
|
@@ -5610,7 +6590,7 @@ var Pairing = z6.object({
|
|
|
5610
6590
|
* repeated bug. Pre-1.0, an existing pairing file is re-paired rather
|
|
5611
6591
|
* than migrated, and the README says so.
|
|
5612
6592
|
*/
|
|
5613
|
-
sites:
|
|
6593
|
+
sites: z7.record(z7.string().min(1), PublicIdentity2),
|
|
5614
6594
|
/**
|
|
5615
6595
|
* Every site ever approved here, with the key it was approved under —
|
|
5616
6596
|
* V1-1.
|
|
@@ -5625,7 +6605,7 @@ var Pairing = z6.object({
|
|
|
5625
6605
|
* sites in it were approved", which is true — they came through
|
|
5626
6606
|
* `connect`'s fingerprint compare.
|
|
5627
6607
|
*/
|
|
5628
|
-
known:
|
|
6608
|
+
known: z7.record(z7.string().min(1), PublicIdentity2).optional(),
|
|
5629
6609
|
/**
|
|
5630
6610
|
* The control plane's grant-signing key, pinned at pairing —
|
|
5631
6611
|
* Amendment J.
|
|
@@ -5636,8 +6616,8 @@ var Pairing = z6.object({
|
|
|
5636
6616
|
* correct amount of function for a relationship whose admission authority
|
|
5637
6617
|
* was never established.
|
|
5638
6618
|
*/
|
|
5639
|
-
controlPlanePublic:
|
|
5640
|
-
pairedAt:
|
|
6619
|
+
controlPlanePublic: z7.string().min(1).optional(),
|
|
6620
|
+
pairedAt: z7.number().int().positive()
|
|
5641
6621
|
}).strict();
|
|
5642
6622
|
function siftEntries(row) {
|
|
5643
6623
|
const dropped = [];
|
|
@@ -5657,7 +6637,7 @@ function siftEntries(row) {
|
|
|
5657
6637
|
}
|
|
5658
6638
|
return dropped;
|
|
5659
6639
|
}
|
|
5660
|
-
var PairingFile =
|
|
6640
|
+
var PairingFile = z7.object({ version: z7.literal(1), pairings: z7.array(z7.unknown()) }).strict();
|
|
5661
6641
|
var RETIRED_PAIRING_FIELDS = Object.freeze([
|
|
5662
6642
|
"roster",
|
|
5663
6643
|
"rosterRefusal",
|
|
@@ -5678,7 +6658,7 @@ var Pairings = class {
|
|
|
5678
6658
|
this.#retired = [];
|
|
5679
6659
|
let file;
|
|
5680
6660
|
try {
|
|
5681
|
-
file = JSON.parse(await
|
|
6661
|
+
file = JSON.parse(await readFile12(this.#path, "utf8"));
|
|
5682
6662
|
} catch (error) {
|
|
5683
6663
|
const code = error.code;
|
|
5684
6664
|
if (code !== "ENOENT") {
|
|
@@ -5823,7 +6803,7 @@ var Pairings = class {
|
|
|
5823
6803
|
* whole point is that the new file takes the old one's place in one step.
|
|
5824
6804
|
*/
|
|
5825
6805
|
async #save() {
|
|
5826
|
-
await
|
|
6806
|
+
await mkdir8(dirname8(this.#path), { recursive: true });
|
|
5827
6807
|
const body = `${JSON.stringify(
|
|
5828
6808
|
{ version: 1, pairings: this.#pairings },
|
|
5829
6809
|
null,
|
|
@@ -5832,8 +6812,8 @@ var Pairings = class {
|
|
|
5832
6812
|
`;
|
|
5833
6813
|
const temp = `${this.#path}.${randomUUID3()}.tmp`;
|
|
5834
6814
|
try {
|
|
5835
|
-
await
|
|
5836
|
-
await
|
|
6815
|
+
await writeFile7(temp, body, { mode: 384 });
|
|
6816
|
+
await rename4(temp, this.#path);
|
|
5837
6817
|
} catch (error) {
|
|
5838
6818
|
await rm4(temp, { force: true });
|
|
5839
6819
|
throw error;
|
|
@@ -5854,12 +6834,12 @@ async function recordSites(pairings, origin, sites, extra = {}) {
|
|
|
5854
6834
|
}
|
|
5855
6835
|
|
|
5856
6836
|
// src/spent-grants.ts
|
|
5857
|
-
import { z as
|
|
6837
|
+
import { z as z8 } from "zod";
|
|
5858
6838
|
import { CLOCK_SKEW_WARN_MS, GRANT_MAX_AGE_MS } from "@byollm/protocol";
|
|
5859
|
-
var SpentFile =
|
|
5860
|
-
version:
|
|
6839
|
+
var SpentFile = z8.object({
|
|
6840
|
+
version: z8.literal(1),
|
|
5861
6841
|
/** Grant id to the moment past which it could not be replayed anyway. */
|
|
5862
|
-
spent:
|
|
6842
|
+
spent: z8.record(z8.string(), z8.number().int().positive())
|
|
5863
6843
|
}).strict();
|
|
5864
6844
|
var SpentGrants = class {
|
|
5865
6845
|
#path;
|
|
@@ -5981,6 +6961,7 @@ function daemonPaths(root = defaultRoot()) {
|
|
|
5981
6961
|
budgets: join4(root, "budgets.json"),
|
|
5982
6962
|
spend: join4(root, "spend.json"),
|
|
5983
6963
|
serviceStates: join4(root, "services.json"),
|
|
6964
|
+
imageProbes: join4(root, "image-probes.json"),
|
|
5984
6965
|
spentGrants: join4(root, "spent-grants.json"),
|
|
5985
6966
|
keys: join4(root, "keys.json"),
|
|
5986
6967
|
health: join4(root, "health.json"),
|
|
@@ -6049,6 +7030,13 @@ var FOR_SITE = Object.freeze({
|
|
|
6049
7030
|
code: "canceled",
|
|
6050
7031
|
message: "the job was canceled",
|
|
6051
7032
|
retryable: false
|
|
7033
|
+
},
|
|
7034
|
+
/* The site's own images, refused by its own references — nothing about
|
|
7035
|
+
this device or its owner is in it, so it is said plainly. */
|
|
7036
|
+
"image-refused": {
|
|
7037
|
+
code: "image_refused",
|
|
7038
|
+
message: "the job's images did not match their references",
|
|
7039
|
+
retryable: false
|
|
6052
7040
|
}
|
|
6053
7041
|
});
|
|
6054
7042
|
function outcomeForSite(code) {
|
|
@@ -6065,12 +7053,19 @@ import {
|
|
|
6065
7053
|
publicIdentityOf as publicIdentityOf2,
|
|
6066
7054
|
seal as seal2,
|
|
6067
7055
|
payloadTextLength,
|
|
6068
|
-
|
|
7056
|
+
sizeClassFloor,
|
|
6069
7057
|
fingerprint as fingerprint2,
|
|
6070
7058
|
verifyPublicIdentity as verifyPublicIdentity3,
|
|
6071
7059
|
REFUSAL_MESSAGES,
|
|
6072
7060
|
matchAudience,
|
|
6073
|
-
|
|
7061
|
+
SealedOutcomeFor,
|
|
7062
|
+
ToolsPayload,
|
|
7063
|
+
toolsConsentRefusal,
|
|
7064
|
+
toolsRefusalText,
|
|
7065
|
+
ToolCall,
|
|
7066
|
+
ImagePayload,
|
|
7067
|
+
VisionPayload,
|
|
7068
|
+
visionImages,
|
|
6074
7069
|
CLOCK_ATTRIBUTION_MS,
|
|
6075
7070
|
CLOCK_SKEW_WARN_MS as CLOCK_SKEW_WARN_MS2,
|
|
6076
7071
|
GRANT_MAX_AGE_MS as GRANT_MAX_AGE_MS2,
|
|
@@ -6078,33 +7073,434 @@ import {
|
|
|
6078
7073
|
verifyGrant
|
|
6079
7074
|
} from "@byollm/protocol";
|
|
6080
7075
|
|
|
6081
|
-
// src/
|
|
6082
|
-
|
|
6083
|
-
|
|
6084
|
-
|
|
6085
|
-
|
|
7076
|
+
// src/tool-loop.ts
|
|
7077
|
+
import { randomBytes as randomBytes2 } from "crypto";
|
|
7078
|
+
import {
|
|
7079
|
+
TOOL_LIMITS,
|
|
7080
|
+
ToolResult,
|
|
7081
|
+
toolArgumentsRefusal
|
|
7082
|
+
} from "@byollm/protocol";
|
|
7083
|
+
var TOOL_POLL_MS = 1e3;
|
|
7084
|
+
var CLOSING_TURN = "Answer from what you have.";
|
|
7085
|
+
async function runToolLoop(input) {
|
|
7086
|
+
const converse = input.backend.converse?.bind(input.backend);
|
|
7087
|
+
const started = input.now();
|
|
7088
|
+
const outcomes = { answered: 0, errored: 0, timedOut: 0, refused: 0 };
|
|
7089
|
+
const declared = new Map(input.declarations.map((d) => [d.name, d]));
|
|
7090
|
+
const tools = input.declarations.map((d) => ({
|
|
7091
|
+
name: d.name,
|
|
7092
|
+
description: d.description,
|
|
7093
|
+
parameters: d.parameters
|
|
7094
|
+
}));
|
|
7095
|
+
const messages = [{ role: "user", content: input.prompt }];
|
|
7096
|
+
const finished = /* @__PURE__ */ new Set();
|
|
7097
|
+
let calls = 0;
|
|
7098
|
+
let resultChars = 0;
|
|
7099
|
+
let turns = 0;
|
|
7100
|
+
let inputChars = 0;
|
|
7101
|
+
const run2 = (result) => ({
|
|
7102
|
+
result,
|
|
7103
|
+
toolCalls: calls,
|
|
7104
|
+
toolOutcomes: { ...outcomes },
|
|
7105
|
+
turns,
|
|
7106
|
+
inputChars
|
|
7107
|
+
});
|
|
7108
|
+
const failed = (code, message) => run2({ ok: false, code, message, durationMs: input.now() - started });
|
|
7109
|
+
if (converse === void 0) {
|
|
7110
|
+
return failed("backend-error", "this backend cannot carry a site's tools");
|
|
7111
|
+
}
|
|
7112
|
+
const turn = async (offer) => {
|
|
7113
|
+
const remaining = input.deadlineAt - input.now();
|
|
7114
|
+
if (remaining <= 0) return null;
|
|
7115
|
+
turns += 1;
|
|
7116
|
+
inputChars += messages.reduce((sum, m) => sum + m.content.length, 0);
|
|
7117
|
+
return converse({
|
|
7118
|
+
messages: [...messages],
|
|
7119
|
+
...offer ? { tools } : {},
|
|
7120
|
+
model: input.model,
|
|
7121
|
+
timeoutMs: Math.min(input.turnCeilingMs, remaining),
|
|
7122
|
+
maxOutputBytes: input.maxOutputBytes,
|
|
7123
|
+
signal: input.signal
|
|
7124
|
+
});
|
|
7125
|
+
};
|
|
7126
|
+
for (; ; ) {
|
|
7127
|
+
const answer = await turn(true);
|
|
7128
|
+
if (answer === null) {
|
|
7129
|
+
return failed("timeout", "this job's deadline passed during its tools");
|
|
7130
|
+
}
|
|
7131
|
+
if (!answer.ok)
|
|
7132
|
+
return run2({ ...answer, durationMs: input.now() - started });
|
|
7133
|
+
if (answer.toolCalls.length === 0) {
|
|
7134
|
+
return run2(okResult(answer.text, answer.stop, input.now() - started));
|
|
7135
|
+
}
|
|
7136
|
+
messages.push({
|
|
7137
|
+
role: "assistant",
|
|
7138
|
+
content: answer.text,
|
|
7139
|
+
toolCalls: answer.toolCalls
|
|
7140
|
+
});
|
|
7141
|
+
let atLimit = false;
|
|
7142
|
+
for (const asked of answer.toolCalls) {
|
|
7143
|
+
if (atLimit) {
|
|
7144
|
+
messages.push(toolTurn(asked, "Not run: this job's tools are spent."));
|
|
7145
|
+
continue;
|
|
7146
|
+
}
|
|
7147
|
+
calls += 1;
|
|
7148
|
+
if (calls >= input.budget) atLimit = true;
|
|
7149
|
+
const seq = calls;
|
|
7150
|
+
const callId = newCallId();
|
|
7151
|
+
await input.log.call({
|
|
7152
|
+
callId,
|
|
7153
|
+
seq,
|
|
7154
|
+
name: asked.name,
|
|
7155
|
+
arguments: asked.arguments
|
|
7156
|
+
});
|
|
7157
|
+
const refusal = checkCall(asked, declared);
|
|
7158
|
+
if (refusal !== null) {
|
|
7159
|
+
outcomes.refused += 1;
|
|
7160
|
+
await input.log.outcome({
|
|
7161
|
+
callId,
|
|
7162
|
+
seq,
|
|
7163
|
+
outcome: "refused",
|
|
7164
|
+
detail: refusal.why
|
|
7165
|
+
});
|
|
7166
|
+
messages.push(
|
|
7167
|
+
toolTurn(asked, `Tool error: ${refusal.why}. Nothing was sent.`)
|
|
7168
|
+
);
|
|
7169
|
+
continue;
|
|
7170
|
+
}
|
|
7171
|
+
const call = {
|
|
7172
|
+
callId,
|
|
7173
|
+
seq,
|
|
7174
|
+
name: asked.name,
|
|
7175
|
+
arguments: parsedArguments(asked)
|
|
7176
|
+
};
|
|
7177
|
+
let sent;
|
|
7178
|
+
try {
|
|
7179
|
+
sent = await input.transport.send({
|
|
7180
|
+
callId,
|
|
7181
|
+
envelope: await input.seal(call)
|
|
7182
|
+
});
|
|
7183
|
+
} catch {
|
|
7184
|
+
sent = "unsent";
|
|
7185
|
+
}
|
|
7186
|
+
const answered = sent === "sent" ? await waitForResult({
|
|
7187
|
+
...input,
|
|
7188
|
+
outstanding: { callId, seq },
|
|
7189
|
+
finished
|
|
7190
|
+
}) : { kind: sent };
|
|
7191
|
+
finished.add(callId);
|
|
7192
|
+
switch (answered.kind) {
|
|
7193
|
+
case "canceled":
|
|
7194
|
+
return run2({
|
|
7195
|
+
ok: false,
|
|
7196
|
+
code: "canceled",
|
|
7197
|
+
message: "the job was canceled",
|
|
7198
|
+
durationMs: input.now() - started
|
|
7199
|
+
});
|
|
7200
|
+
case "tools-spent": {
|
|
7201
|
+
outcomes.refused += 1;
|
|
7202
|
+
atLimit = true;
|
|
7203
|
+
await input.log.outcome({
|
|
7204
|
+
callId,
|
|
7205
|
+
seq,
|
|
7206
|
+
outcome: "refused",
|
|
7207
|
+
detail: "the upstream says this job has made every tool call it is allowed"
|
|
7208
|
+
});
|
|
7209
|
+
messages.push(
|
|
7210
|
+
toolTurn(
|
|
7211
|
+
asked,
|
|
7212
|
+
"Tool error: this job's tools are spent. Nothing was run."
|
|
7213
|
+
)
|
|
7214
|
+
);
|
|
7215
|
+
break;
|
|
7216
|
+
}
|
|
7217
|
+
case "unsent":
|
|
7218
|
+
case "timed-out": {
|
|
7219
|
+
outcomes.timedOut += 1;
|
|
7220
|
+
await input.log.outcome({
|
|
7221
|
+
callId,
|
|
7222
|
+
seq,
|
|
7223
|
+
outcome: "timedOut",
|
|
7224
|
+
detail: answered.kind === "unsent" ? "the upstream would not take the call" : `no answer within ${String(TOOL_LIMITS.callTimeoutMs)}ms`
|
|
7225
|
+
});
|
|
7226
|
+
messages.push(toolTurn(asked, "The site did not answer in time."));
|
|
7227
|
+
break;
|
|
7228
|
+
}
|
|
7229
|
+
case "result": {
|
|
7230
|
+
const result = answered.result;
|
|
7231
|
+
if (!result.ok) {
|
|
7232
|
+
outcomes.errored += 1;
|
|
7233
|
+
await input.log.outcome({
|
|
7234
|
+
callId,
|
|
7235
|
+
seq,
|
|
7236
|
+
outcome: "errored",
|
|
7237
|
+
detail: result.error
|
|
7238
|
+
});
|
|
7239
|
+
messages.push(
|
|
7240
|
+
toolTurn(asked, `Tool error from the site: ${result.error}`)
|
|
7241
|
+
);
|
|
7242
|
+
break;
|
|
7243
|
+
}
|
|
7244
|
+
if (resultChars + result.content.length > input.maxResultChars) {
|
|
7245
|
+
outcomes.refused += 1;
|
|
7246
|
+
atLimit = true;
|
|
7247
|
+
await input.log.outcome({
|
|
7248
|
+
callId,
|
|
7249
|
+
seq,
|
|
7250
|
+
outcome: "refused",
|
|
7251
|
+
content: result.content,
|
|
7252
|
+
detail: `would take this job past its ${String(input.maxResultChars)}-character tool-result limit`
|
|
7253
|
+
});
|
|
7254
|
+
messages.push(
|
|
7255
|
+
toolTurn(
|
|
7256
|
+
asked,
|
|
7257
|
+
"Not shown: this job has read all the tool results it may."
|
|
7258
|
+
)
|
|
7259
|
+
);
|
|
7260
|
+
break;
|
|
7261
|
+
}
|
|
7262
|
+
resultChars += result.content.length;
|
|
7263
|
+
outcomes.answered += 1;
|
|
7264
|
+
await input.log.outcome({
|
|
7265
|
+
callId,
|
|
7266
|
+
seq,
|
|
7267
|
+
outcome: "answered",
|
|
7268
|
+
content: result.content
|
|
7269
|
+
});
|
|
7270
|
+
messages.push(toolTurn(asked, result.content));
|
|
7271
|
+
break;
|
|
7272
|
+
}
|
|
7273
|
+
}
|
|
7274
|
+
}
|
|
7275
|
+
if (atLimit) {
|
|
7276
|
+
messages.push({ role: "user", content: CLOSING_TURN });
|
|
7277
|
+
const closing = await turn(false);
|
|
7278
|
+
if (closing === null) {
|
|
7279
|
+
return failed("timeout", "this job's deadline passed during its tools");
|
|
7280
|
+
}
|
|
7281
|
+
if (!closing.ok) {
|
|
7282
|
+
return run2({ ...closing, durationMs: input.now() - started });
|
|
7283
|
+
}
|
|
7284
|
+
return run2(okResult(closing.text, "tool-budget", input.now() - started));
|
|
7285
|
+
}
|
|
7286
|
+
}
|
|
7287
|
+
}
|
|
7288
|
+
function okResult(text, stop, durationMs) {
|
|
7289
|
+
return { ok: true, text, stop, durationMs };
|
|
7290
|
+
}
|
|
7291
|
+
function toolTurn(asked, content) {
|
|
7292
|
+
return { role: "tool", toolCallId: asked.id, content };
|
|
7293
|
+
}
|
|
7294
|
+
function newCallId() {
|
|
7295
|
+
return randomBytes2(16).toString("base64url");
|
|
7296
|
+
}
|
|
7297
|
+
function checkCall(asked, declared) {
|
|
7298
|
+
const declaration = declared.get(asked.name);
|
|
7299
|
+
if (declaration === void 0) {
|
|
7300
|
+
return {
|
|
7301
|
+
why: `"${asked.name.slice(0, 64)}" is not one of this job's tools`
|
|
7302
|
+
};
|
|
7303
|
+
}
|
|
7304
|
+
if (new TextEncoder().encode(asked.arguments).length > TOOL_LIMITS.maxArgumentBytes) {
|
|
7305
|
+
return { why: "the arguments are too large" };
|
|
7306
|
+
}
|
|
7307
|
+
let parsed;
|
|
7308
|
+
try {
|
|
7309
|
+
parsed = JSON.parse(asked.arguments);
|
|
7310
|
+
} catch {
|
|
7311
|
+
return { why: "the arguments are not JSON" };
|
|
7312
|
+
}
|
|
7313
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
7314
|
+
return { why: "the arguments are not an object" };
|
|
7315
|
+
}
|
|
7316
|
+
const refusal = toolArgumentsRefusal(
|
|
7317
|
+
declaration.parameters,
|
|
7318
|
+
parsed
|
|
7319
|
+
);
|
|
7320
|
+
return refusal === null ? null : { why: refusal };
|
|
7321
|
+
}
|
|
7322
|
+
function parsedArguments(asked) {
|
|
7323
|
+
return JSON.parse(asked.arguments);
|
|
7324
|
+
}
|
|
7325
|
+
async function waitForResult(input) {
|
|
7326
|
+
const sentAt = input.now();
|
|
7327
|
+
for (; ; ) {
|
|
7328
|
+
if (input.signal.aborted) return { kind: "canceled" };
|
|
7329
|
+
let batch = [];
|
|
7330
|
+
try {
|
|
7331
|
+
batch = await input.transport.poll(input.outstanding.callId);
|
|
7332
|
+
} catch {
|
|
7333
|
+
}
|
|
7334
|
+
for (const entry of batch) {
|
|
7335
|
+
const accepted = await acceptResult(input, entry);
|
|
7336
|
+
if (accepted !== null) return { kind: "result", result: accepted };
|
|
7337
|
+
}
|
|
7338
|
+
const now = input.now();
|
|
7339
|
+
if (now - sentAt >= TOOL_LIMITS.callTimeoutMs || now >= input.deadlineAt) {
|
|
7340
|
+
return { kind: "timed-out" };
|
|
7341
|
+
}
|
|
7342
|
+
await input.wait(TOOL_POLL_MS);
|
|
7343
|
+
}
|
|
7344
|
+
}
|
|
7345
|
+
async function acceptResult(input, entry) {
|
|
7346
|
+
const drop = async (why) => {
|
|
7347
|
+
await input.log.dropped({ callId: entry.callId, why });
|
|
7348
|
+
return null;
|
|
7349
|
+
};
|
|
7350
|
+
if (entry.jobId !== input.jobId) {
|
|
7351
|
+
return drop("a result labelled for another job");
|
|
7352
|
+
}
|
|
7353
|
+
if (entry.callId !== input.outstanding.callId) {
|
|
7354
|
+
return drop(
|
|
7355
|
+
input.finished.has(entry.callId) ? "a second answer to a call already finished" : "a result for no call this job is waiting on"
|
|
7356
|
+
);
|
|
7357
|
+
}
|
|
7358
|
+
const plaintext = await input.open(entry.envelope);
|
|
7359
|
+
if (plaintext === null) {
|
|
7360
|
+
return drop(
|
|
7361
|
+
"did not open as this job's tool-result from the site it paired with"
|
|
7362
|
+
);
|
|
7363
|
+
}
|
|
7364
|
+
let parsed;
|
|
7365
|
+
try {
|
|
7366
|
+
parsed = ToolResult.safeParse(JSON.parse(plaintext));
|
|
7367
|
+
} catch {
|
|
7368
|
+
return drop("the result is not JSON");
|
|
7369
|
+
}
|
|
7370
|
+
if (!parsed.success) return drop("the result is not a tool result");
|
|
7371
|
+
if (parsed.data.callId !== input.outstanding.callId || parsed.data.seq !== input.outstanding.seq) {
|
|
7372
|
+
return drop("the sealed result names a different call");
|
|
7373
|
+
}
|
|
7374
|
+
return parsed.data;
|
|
7375
|
+
}
|
|
7376
|
+
|
|
7377
|
+
// src/tool-transport.ts
|
|
7378
|
+
function clientToolTransport(client, holder) {
|
|
7379
|
+
const lease = { jobId: holder.jobId, leaseId: holder.leaseId };
|
|
7380
|
+
return {
|
|
7381
|
+
send: async ({ callId, envelope }) => {
|
|
7382
|
+
try {
|
|
7383
|
+
await client.toolCall({
|
|
7384
|
+
runnerId: holder.runnerId,
|
|
7385
|
+
...lease,
|
|
7386
|
+
callId,
|
|
7387
|
+
envelope
|
|
7388
|
+
});
|
|
7389
|
+
return "sent";
|
|
7390
|
+
} catch (error) {
|
|
7391
|
+
if (error instanceof ClientError && error.kind === "tools-spent") {
|
|
7392
|
+
return "tools-spent";
|
|
7393
|
+
}
|
|
7394
|
+
throw error;
|
|
7395
|
+
}
|
|
7396
|
+
},
|
|
7397
|
+
poll: async (callId) => (await client.toolResults({
|
|
7398
|
+
runnerId: holder.runnerId,
|
|
7399
|
+
calls: [{ ...lease, callId }]
|
|
7400
|
+
})).results
|
|
7401
|
+
};
|
|
7402
|
+
}
|
|
7403
|
+
|
|
7404
|
+
// src/tools-probe.ts
|
|
7405
|
+
var ECHO_NUMBER = Object.freeze({
|
|
7406
|
+
name: "echo_number",
|
|
7407
|
+
description: "Echoes the number it is given, with a confirmation code. Call it when asked to.",
|
|
7408
|
+
parameters: Object.freeze({
|
|
7409
|
+
type: "object",
|
|
7410
|
+
properties: Object.freeze({
|
|
7411
|
+
n: Object.freeze({ type: "integer", description: "The number to echo." })
|
|
7412
|
+
}),
|
|
7413
|
+
required: Object.freeze(["n"]),
|
|
7414
|
+
additionalProperties: false
|
|
7415
|
+
})
|
|
7416
|
+
});
|
|
7417
|
+
var ASK = "Call the echo_number tool with n set to 7. Then reply with the confirmation code it returns, and nothing else.";
|
|
7418
|
+
var CODE = "4091";
|
|
7419
|
+
var PROBE_TIMEOUT_MS2 = 6e4;
|
|
7420
|
+
var PROBE_MAX_OUTPUT_BYTES = 64 * 1024;
|
|
7421
|
+
async function probeTools(backend, model, signal = new AbortController().signal) {
|
|
7422
|
+
if (backend.converse === void 0) {
|
|
7423
|
+
return {
|
|
7424
|
+
kind: "cannot",
|
|
7425
|
+
why: `${backend.id} cannot carry a site's tools \u2014 llm.tools needs an HTTP service, such as Ollama, LM Studio or an OpenAI-compatible API`
|
|
7426
|
+
};
|
|
6086
7427
|
}
|
|
6087
|
-
const
|
|
6088
|
-
|
|
6089
|
-
|
|
6090
|
-
|
|
6091
|
-
|
|
6092
|
-
|
|
6093
|
-
|
|
6094
|
-
|
|
6095
|
-
|
|
6096
|
-
|
|
6097
|
-
|
|
6098
|
-
|
|
6099
|
-
|
|
6100
|
-
|
|
6101
|
-
|
|
6102
|
-
|
|
6103
|
-
|
|
7428
|
+
const base = {
|
|
7429
|
+
model,
|
|
7430
|
+
timeoutMs: PROBE_TIMEOUT_MS2,
|
|
7431
|
+
maxOutputBytes: PROBE_MAX_OUTPUT_BYTES,
|
|
7432
|
+
signal,
|
|
7433
|
+
tools: [ECHO_NUMBER]
|
|
7434
|
+
};
|
|
7435
|
+
const first = await backend.converse({
|
|
7436
|
+
...base,
|
|
7437
|
+
messages: [{ role: "user", content: ASK }]
|
|
7438
|
+
});
|
|
7439
|
+
if (!first.ok) return { kind: "unanswered", why: first.message };
|
|
7440
|
+
const call = first.toolCalls[0];
|
|
7441
|
+
if (call === void 0) {
|
|
7442
|
+
return {
|
|
7443
|
+
kind: "cannot",
|
|
7444
|
+
why: `${model} answered without calling echo_number, so it cannot use a site's tools`
|
|
7445
|
+
};
|
|
7446
|
+
}
|
|
7447
|
+
let n;
|
|
7448
|
+
try {
|
|
7449
|
+
n = JSON.parse(call.arguments).n;
|
|
7450
|
+
} catch {
|
|
7451
|
+
n = void 0;
|
|
7452
|
+
}
|
|
7453
|
+
if (call.name !== "echo_number" || n !== 7) {
|
|
7454
|
+
return {
|
|
7455
|
+
kind: "cannot",
|
|
7456
|
+
why: `${model} was asked to call echo_number with 7 and did not`
|
|
7457
|
+
};
|
|
7458
|
+
}
|
|
7459
|
+
const second = await backend.converse({
|
|
7460
|
+
...base,
|
|
7461
|
+
messages: [
|
|
7462
|
+
{ role: "user", content: ASK },
|
|
7463
|
+
{ role: "assistant", content: first.text, toolCalls: [call] },
|
|
7464
|
+
{
|
|
7465
|
+
role: "tool",
|
|
7466
|
+
toolCallId: call.id,
|
|
7467
|
+
content: `7 (confirmation code ${CODE})`
|
|
7468
|
+
}
|
|
7469
|
+
]
|
|
7470
|
+
});
|
|
7471
|
+
if (!second.ok) return { kind: "unanswered", why: second.message };
|
|
7472
|
+
if (!second.text.includes(CODE)) {
|
|
7473
|
+
return {
|
|
7474
|
+
kind: "cannot",
|
|
7475
|
+
why: `${model} called echo_number but did not use what it returned`
|
|
7476
|
+
};
|
|
6104
7477
|
}
|
|
7478
|
+
return { kind: "calls-tools" };
|
|
6105
7479
|
}
|
|
6106
|
-
|
|
6107
|
-
|
|
7480
|
+
|
|
7481
|
+
// src/upstream-predates.ts
|
|
7482
|
+
var KINDS_AFTER_0_1_4 = Object.freeze([
|
|
7483
|
+
"llm.tools",
|
|
7484
|
+
"llm.vision",
|
|
7485
|
+
"llm.image"
|
|
7486
|
+
]);
|
|
7487
|
+
function isAfter014(kind) {
|
|
7488
|
+
return KINDS_AFTER_0_1_4.includes(kind);
|
|
7489
|
+
}
|
|
7490
|
+
function predatesMessage(input) {
|
|
7491
|
+
const tools = input.kinds.includes("llm.tools");
|
|
7492
|
+
const images = input.kinds.some(
|
|
7493
|
+
(k) => k === "llm.vision" || k === "llm.image"
|
|
7494
|
+
);
|
|
7495
|
+
const what = [
|
|
7496
|
+
...tools ? ["the site's tools"] : [],
|
|
7497
|
+
...images ? ["images"] : []
|
|
7498
|
+
].join(" or ");
|
|
7499
|
+
const off = [...tools ? ["tools"] : [], ...images ? ["images"] : []].join(
|
|
7500
|
+
" and "
|
|
7501
|
+
);
|
|
7502
|
+
const services = input.services.map((s) => `\`${s}\``).join(", ");
|
|
7503
|
+
return `${input.upstream} runs byollm 0.1.4 or older and cannot carry ${what} \u2014 update it, or turn ${off} off for ${services}`;
|
|
6108
7504
|
}
|
|
6109
7505
|
|
|
6110
7506
|
// src/runner.ts
|
|
@@ -6113,6 +7509,7 @@ var GAVE_UP_TTL_MS = 10 * 6e4;
|
|
|
6113
7509
|
var MAX_JOB_ATTEMPTS = 3;
|
|
6114
7510
|
var ATTEMPT_BACKOFF_MS = 3e4;
|
|
6115
7511
|
var ATTEMPT_TTL_MS = 10 * 6e4;
|
|
7512
|
+
var UNBOUNDED_COMMUNITY_REFUSAL = "this job is streamed, so it has no size until it ends \u2014 community jobs of that kind are not accepted yet, at any payload limit";
|
|
6116
7513
|
function jobKey(job) {
|
|
6117
7514
|
return `${job.site ?? "direct"}:${job.id}`;
|
|
6118
7515
|
}
|
|
@@ -6135,6 +7532,15 @@ var POLL_JITTER_MS = { min: 200, span: 300 };
|
|
|
6135
7532
|
function sameKey(a, b) {
|
|
6136
7533
|
return a.identity === b.identity && a.encryption === b.encryption && a.encryptionSig === b.encryptionSig;
|
|
6137
7534
|
}
|
|
7535
|
+
var NO_TOOL_CALLS = Object.freeze({
|
|
7536
|
+
toolCalls: 0,
|
|
7537
|
+
toolOutcomes: Object.freeze({
|
|
7538
|
+
answered: 0,
|
|
7539
|
+
errored: 0,
|
|
7540
|
+
timedOut: 0,
|
|
7541
|
+
refused: 0
|
|
7542
|
+
})
|
|
7543
|
+
});
|
|
6138
7544
|
var Runner = class {
|
|
6139
7545
|
#options;
|
|
6140
7546
|
#backends = /* @__PURE__ */ new Map();
|
|
@@ -6553,6 +7959,12 @@ var Runner = class {
|
|
|
6553
7959
|
probed.set(route.service, usable);
|
|
6554
7960
|
}
|
|
6555
7961
|
if (!usable) continue;
|
|
7962
|
+
if (route.kind === "llm.tools" && !await this.#callsTools(route)) {
|
|
7963
|
+
continue;
|
|
7964
|
+
}
|
|
7965
|
+
if ((route.kind === "llm.vision" || route.kind === "llm.image") && !await this.#imageKindPasses(route)) {
|
|
7966
|
+
continue;
|
|
7967
|
+
}
|
|
6556
7968
|
capabilities.push({
|
|
6557
7969
|
kind: route.kind,
|
|
6558
7970
|
service: route.service,
|
|
@@ -6573,6 +7985,76 @@ var Runner = class {
|
|
|
6573
7985
|
this.#writeServiceStates();
|
|
6574
7986
|
return capabilities;
|
|
6575
7987
|
}
|
|
7988
|
+
/**
|
|
7989
|
+
* The tool probe's verdict for each `llm.tools` service, for the surfaces
|
|
7990
|
+
* that must say why it is not advertised (`byollm services`).
|
|
7991
|
+
*/
|
|
7992
|
+
get toolsProbes() {
|
|
7993
|
+
return this.#toolsProbes;
|
|
7994
|
+
}
|
|
7995
|
+
#toolsProbes = /* @__PURE__ */ new Map();
|
|
7996
|
+
/**
|
|
7997
|
+
* Ask the probe, once a session for each answer that will not change.
|
|
7998
|
+
*
|
|
7999
|
+
* A verdict from a model that answered is held: the probe costs a model
|
|
8000
|
+
* call (and, on a hosted service, money), and the same model asked again
|
|
8001
|
+
* gives the same answer. A service that did not answer is asked again on
|
|
8002
|
+
* the next pass, because that is about the server, not the model.
|
|
8003
|
+
*/
|
|
8004
|
+
async #callsTools(route) {
|
|
8005
|
+
const held = this.#toolsProbes.get(route.service);
|
|
8006
|
+
if (held !== void 0 && held.kind !== "unanswered") {
|
|
8007
|
+
return held.kind === "calls-tools";
|
|
8008
|
+
}
|
|
8009
|
+
const verdict = await probeTools(this.#backendFor(route), route.model);
|
|
8010
|
+
this.#toolsProbes.set(route.service, verdict);
|
|
8011
|
+
return verdict.kind === "calls-tools";
|
|
8012
|
+
}
|
|
8013
|
+
/**
|
|
8014
|
+
* Each image route's last probe verdict, keyed `service kind`, for the
|
|
8015
|
+
* surfaces that must say why one is not advertised (`byollm services`).
|
|
8016
|
+
*/
|
|
8017
|
+
get imageProbes() {
|
|
8018
|
+
return this.#imageVerdicts;
|
|
8019
|
+
}
|
|
8020
|
+
#imageVerdicts = /* @__PURE__ */ new Map();
|
|
8021
|
+
#imageProbeStore;
|
|
8022
|
+
/**
|
|
8023
|
+
* Ask the image probe for a route, or reuse its answer — byollm_021 §5.
|
|
8024
|
+
*
|
|
8025
|
+
* A verdict from a service that answered is reused for
|
|
8026
|
+
* {@link IMAGE_PROBE_REUSE_MS}: the image probe is a paid call on a metered
|
|
8027
|
+
* backend, so it runs when the owner adds the kind (no verdict yet) and on
|
|
8028
|
+
* `--probe-images`, never per heartbeat. A service that did not answer is
|
|
8029
|
+
* asked again next pass — that is about the server, not the model.
|
|
8030
|
+
*/
|
|
8031
|
+
async #imageKindPasses(route) {
|
|
8032
|
+
this.#imageProbeStore ??= this.#options.imageProbes ?? memoryImageProbeStore();
|
|
8033
|
+
const store = this.#imageProbeStore;
|
|
8034
|
+
const key = imageProbeKey(route);
|
|
8035
|
+
const mode = this.#options.probeImages ?? "when-due";
|
|
8036
|
+
const held = await store.get(key);
|
|
8037
|
+
const fresh = held !== void 0 && held.kind !== "unanswered" && this.#now() - held.at < IMAGE_PROBE_REUSE_MS;
|
|
8038
|
+
let verdict = held;
|
|
8039
|
+
if (mode === "now" || mode === "when-due" && !fresh) {
|
|
8040
|
+
const backend = this.#backendFor(route);
|
|
8041
|
+
const now = this.#now();
|
|
8042
|
+
if (route.kind === "llm.vision") {
|
|
8043
|
+
verdict = await probeVision(backend, route.model, now);
|
|
8044
|
+
} else {
|
|
8045
|
+
const images = this.#options.loaded.config.services[route.service]?.images;
|
|
8046
|
+
verdict = await probeImages(
|
|
8047
|
+
backend,
|
|
8048
|
+
route.model,
|
|
8049
|
+
images?.probeSize ?? images?.sizes.square ?? "1024x1024",
|
|
8050
|
+
now
|
|
8051
|
+
);
|
|
8052
|
+
}
|
|
8053
|
+
await store.set(key, verdict);
|
|
8054
|
+
}
|
|
8055
|
+
this.#imageVerdicts.set(`${route.service} ${route.kind}`, verdict);
|
|
8056
|
+
return verdict?.kind === "passes";
|
|
8057
|
+
}
|
|
6576
8058
|
#backendFor(route) {
|
|
6577
8059
|
const key = `${route.service}:${route.backendId}`;
|
|
6578
8060
|
let backend = this.#backends.get(key);
|
|
@@ -6890,26 +8372,36 @@ var Runner = class {
|
|
|
6890
8372
|
return { ok: false, reason: REFUSAL_MESSAGES[match.refusal] };
|
|
6891
8373
|
}
|
|
6892
8374
|
if (job.owner !== this.#options.owner) {
|
|
8375
|
+
if (job.sizeClass === "unbounded") {
|
|
8376
|
+
return { ok: false, reason: UNBOUNDED_COMMUNITY_REFUSAL };
|
|
8377
|
+
}
|
|
8378
|
+
const floor = sizeClassFloor(job.sizeClass);
|
|
6893
8379
|
const decision = this.#options.budgets.check(
|
|
6894
8380
|
this.#now(),
|
|
6895
|
-
|
|
8381
|
+
floor,
|
|
6896
8382
|
this.#countsTowardJobCaps(job, route)
|
|
6897
8383
|
);
|
|
6898
8384
|
if (!decision.ok) {
|
|
6899
8385
|
return {
|
|
6900
8386
|
ok: false,
|
|
6901
|
-
|
|
8387
|
+
/* `check` says "this one is N", which of a floor would claim a
|
|
8388
|
+
measurement nobody made. The declaration is what is known. */
|
|
8389
|
+
reason: decision.refusal === "payload-too-large" ? `community jobs are limited to ${String(this.#options.budgets.usage(this.#now()).limits.maxPayloadChars)} characters; this one is declared ${job.sizeClass}, which is at least ${String(floor)}` : decision.detail,
|
|
6902
8390
|
brake: decision.refusal
|
|
6903
8391
|
};
|
|
6904
8392
|
}
|
|
6905
8393
|
}
|
|
6906
|
-
|
|
8394
|
+
const withTools = job.kind === "llm.tools";
|
|
8395
|
+
const servedKey = withTools ? `${job.site} tools` : job.site;
|
|
8396
|
+
if (!this.#served.has(servedKey)) {
|
|
8397
|
+
this.#served.add(servedKey);
|
|
6907
8398
|
this.#served.add(job.site);
|
|
6908
8399
|
const site = this.#sites.get(job.site);
|
|
6909
8400
|
this.#options.onEvent?.({
|
|
6910
8401
|
type: "now-serving",
|
|
6911
8402
|
site: job.site,
|
|
6912
|
-
fingerprint: site ? fingerprint2(site.identity) : job.site
|
|
8403
|
+
fingerprint: site ? fingerprint2(site.identity) : job.site,
|
|
8404
|
+
...withTools ? { withTools: true } : {}
|
|
6913
8405
|
});
|
|
6914
8406
|
}
|
|
6915
8407
|
if (job.grant !== void 0) {
|
|
@@ -7064,6 +8556,238 @@ var Runner = class {
|
|
|
7064
8556
|
signal: context2.signal
|
|
7065
8557
|
});
|
|
7066
8558
|
}
|
|
8559
|
+
/**
|
|
8560
|
+
* Run an `llm.tools` job's loop — byollm_012 §5, §7, §8, §9.
|
|
8561
|
+
*
|
|
8562
|
+
* The budget is min(payload's `maxToolCalls`, the purpose's), computed
|
|
8563
|
+
* here; admission has already held the payload to the purpose's, and the
|
|
8564
|
+
* protocol's ceiling is not recomputed because the schema that parsed the
|
|
8565
|
+
* payload already enforces it. The context cap is the purpose's `maxToolResultChars`. Each call
|
|
8566
|
+
* is logged, then sealed to the job's own site — the pin the payload was
|
|
8567
|
+
* opened against — and sent under the job's lease.
|
|
8568
|
+
*/
|
|
8569
|
+
async #runTools(job, route, backend, prompt, context2) {
|
|
8570
|
+
await this.#ensureLocalServer(route, backend);
|
|
8571
|
+
const payload = job.payload;
|
|
8572
|
+
const identity = this.#options.identity;
|
|
8573
|
+
if (!identity) {
|
|
8574
|
+
throw new Error("this daemon has no keys, so it cannot seal a tool call");
|
|
8575
|
+
}
|
|
8576
|
+
const site = job.site;
|
|
8577
|
+
if (site === void 0) {
|
|
8578
|
+
throw new Error(`job ${job.id} names no site to send its tool calls to`);
|
|
8579
|
+
}
|
|
8580
|
+
const keys = await identity.keys();
|
|
8581
|
+
const pinned = this.#pinFor({ id: job.id, site });
|
|
8582
|
+
const { community, limits } = context2;
|
|
8583
|
+
const siteField = { site };
|
|
8584
|
+
const client = this.#options.client;
|
|
8585
|
+
const lease = { jobId: job.id, leaseId: job.lease.id };
|
|
8586
|
+
const ingress = this.#options.ingress;
|
|
8587
|
+
const at = () => this.#now();
|
|
8588
|
+
const toolWait = this.#options.toolWait ?? ((ms) => new Promise((resolve) => {
|
|
8589
|
+
setTimeout(resolve, ms);
|
|
8590
|
+
}));
|
|
8591
|
+
return runToolLoop({
|
|
8592
|
+
backend,
|
|
8593
|
+
model: route.model,
|
|
8594
|
+
jobId: job.id,
|
|
8595
|
+
prompt,
|
|
8596
|
+
declarations: payload.tools,
|
|
8597
|
+
budget: Math.min(
|
|
8598
|
+
payload.maxToolCalls ?? payload.purposeLimits.maxToolCalls,
|
|
8599
|
+
payload.purposeLimits.maxToolCalls
|
|
8600
|
+
),
|
|
8601
|
+
maxResultChars: payload.purposeLimits.maxToolResultChars,
|
|
8602
|
+
turnCeilingMs: community ? Math.min(
|
|
8603
|
+
limits.community.maxWallClockMs,
|
|
8604
|
+
limits.limits.maxWallClockMs
|
|
8605
|
+
) : limits.limits.maxWallClockMs,
|
|
8606
|
+
maxOutputBytes: community ? Math.min(
|
|
8607
|
+
limits.community.maxOutputBytes,
|
|
8608
|
+
limits.limits.maxOutputBytes
|
|
8609
|
+
) : limits.limits.maxOutputBytes,
|
|
8610
|
+
deadlineAt: context2.deadlineAt,
|
|
8611
|
+
signal: context2.signal,
|
|
8612
|
+
now: at,
|
|
8613
|
+
wait: toolWait,
|
|
8614
|
+
transport: clientToolTransport(client, {
|
|
8615
|
+
runnerId: this.#options.runnerId,
|
|
8616
|
+
...lease
|
|
8617
|
+
}),
|
|
8618
|
+
seal: (call) => seal2({
|
|
8619
|
+
/* Parsed before it is sealed, like everything this daemon seals:
|
|
8620
|
+
the site parses it strictly, and an extra key would be refused
|
|
8621
|
+
there rather than here. */
|
|
8622
|
+
plaintext: JSON.stringify(ToolCall.parse(call)),
|
|
8623
|
+
senderKeys: keys,
|
|
8624
|
+
// The job's own site, by the stub — never "the" site: a device that
|
|
8625
|
+
// serves two would otherwise send one site's call to the other.
|
|
8626
|
+
recipientEncryptionPublic: pinned.encryption,
|
|
8627
|
+
context: {
|
|
8628
|
+
jobId: job.id,
|
|
8629
|
+
senderKeyId: keyId2(publicIdentityOf2(keys).identity),
|
|
8630
|
+
recipientKeyId: keyId2(pinned.identity),
|
|
8631
|
+
deadlineAt: this.#now() + ENVELOPE_MAX_AGE_MS,
|
|
8632
|
+
direction: "tool-call"
|
|
8633
|
+
}
|
|
8634
|
+
}),
|
|
8635
|
+
open: async (envelope) => {
|
|
8636
|
+
if (envelope.senderKeyId !== site) return null;
|
|
8637
|
+
const opened = await open3({
|
|
8638
|
+
envelope,
|
|
8639
|
+
recipientKeys: keys,
|
|
8640
|
+
senderIdentityPublic: pinned.identity,
|
|
8641
|
+
expected: {
|
|
8642
|
+
jobId: job.id,
|
|
8643
|
+
senderKeyId: keyId2(pinned.identity),
|
|
8644
|
+
recipientKeyId: keyId2(publicIdentityOf2(keys).identity),
|
|
8645
|
+
direction: "tool-result"
|
|
8646
|
+
}
|
|
8647
|
+
});
|
|
8648
|
+
return opened.ok ? opened.plaintext : null;
|
|
8649
|
+
},
|
|
8650
|
+
log: {
|
|
8651
|
+
call: (entry) => ingress.recordToolCall({
|
|
8652
|
+
at: at(),
|
|
8653
|
+
jobId: job.id,
|
|
8654
|
+
...siteField,
|
|
8655
|
+
audience: job.audience,
|
|
8656
|
+
...entry
|
|
8657
|
+
}),
|
|
8658
|
+
outcome: (entry) => ingress.recordToolOutcome({
|
|
8659
|
+
at: at(),
|
|
8660
|
+
jobId: job.id,
|
|
8661
|
+
...siteField,
|
|
8662
|
+
...entry
|
|
8663
|
+
}),
|
|
8664
|
+
dropped: (entry) => ingress.recordToolDropped({
|
|
8665
|
+
at: at(),
|
|
8666
|
+
jobId: job.id,
|
|
8667
|
+
...siteField,
|
|
8668
|
+
...entry
|
|
8669
|
+
})
|
|
8670
|
+
}
|
|
8671
|
+
});
|
|
8672
|
+
}
|
|
8673
|
+
/**
|
|
8674
|
+
* Run an `llm.vision` or `llm.image` job — byollm_021 §1–§6.
|
|
8675
|
+
*
|
|
8676
|
+
* Vision: download and open each input image (stopping each download at its
|
|
8677
|
+
* declared size, refusing anything that does not decrypt, hash or sniff as
|
|
8678
|
+
* declared), then one call with the parts. Image: one call that returns
|
|
8679
|
+
* bytes — never a URL, which is never fetched — then sniff, encrypt and
|
|
8680
|
+
* upload each, and only then answer with the references.
|
|
8681
|
+
*
|
|
8682
|
+
* Shaped as a {@link BackendResult} so everything around a call — the auth
|
|
8683
|
+
* and quota withdrawals, the log, the seal — is the same for every kind.
|
|
8684
|
+
*/
|
|
8685
|
+
async #runImageKind(job, route, backend, context2) {
|
|
8686
|
+
const started = this.#now();
|
|
8687
|
+
const refused = (code, message) => ({
|
|
8688
|
+
ok: false,
|
|
8689
|
+
code,
|
|
8690
|
+
message,
|
|
8691
|
+
durationMs: this.#now() - started
|
|
8692
|
+
});
|
|
8693
|
+
const { community, limits } = context2;
|
|
8694
|
+
const ceiling = community ? Math.min(limits.community.maxWallClockMs, limits.limits.maxWallClockMs) : limits.limits.maxWallClockMs;
|
|
8695
|
+
const timeoutMs = Math.max(
|
|
8696
|
+
1,
|
|
8697
|
+
Math.min(ceiling, context2.deadlineAt - this.#now())
|
|
8698
|
+
);
|
|
8699
|
+
const transport = this.#blobTransport(job, context2.signal);
|
|
8700
|
+
await this.#ensureLocalServer(route, backend);
|
|
8701
|
+
if (job.kind === "llm.vision") {
|
|
8702
|
+
if (backend.readImages === void 0) {
|
|
8703
|
+
return refused("backend-error", `${backend.id} cannot read images`);
|
|
8704
|
+
}
|
|
8705
|
+
const payload2 = job.payload;
|
|
8706
|
+
let opened;
|
|
8707
|
+
try {
|
|
8708
|
+
opened = await openInputImages({
|
|
8709
|
+
refs: visionImages(payload2),
|
|
8710
|
+
jobId: job.id,
|
|
8711
|
+
transport
|
|
8712
|
+
});
|
|
8713
|
+
} catch (error) {
|
|
8714
|
+
return refused(
|
|
8715
|
+
"backend-error",
|
|
8716
|
+
`could not fetch the job's images (${error instanceof Error ? error.message : "unknown error"})`
|
|
8717
|
+
);
|
|
8718
|
+
}
|
|
8719
|
+
if (!opened.ok) return refused("image-refused", opened.message);
|
|
8720
|
+
return backend.readImages({
|
|
8721
|
+
messages: visionTurns(payload2, opened.images),
|
|
8722
|
+
model: route.model,
|
|
8723
|
+
timeoutMs,
|
|
8724
|
+
maxOutputBytes: community ? Math.min(
|
|
8725
|
+
limits.community.maxOutputBytes,
|
|
8726
|
+
limits.limits.maxOutputBytes
|
|
8727
|
+
) : limits.limits.maxOutputBytes,
|
|
8728
|
+
signal: context2.signal
|
|
8729
|
+
});
|
|
8730
|
+
}
|
|
8731
|
+
if (backend.makeImages === void 0) {
|
|
8732
|
+
return refused("backend-error", `${backend.id} cannot make images`);
|
|
8733
|
+
}
|
|
8734
|
+
const payload = job.payload;
|
|
8735
|
+
const settings = this.#options.loaded.config.services[route.service]?.images ?? { sizes: void 0, maxImagesPerJob: 1 };
|
|
8736
|
+
const count = Math.min(payload.count ?? 1, settings.maxImagesPerJob);
|
|
8737
|
+
const sizes = settings.sizes ?? {
|
|
8738
|
+
square: "1024x1024",
|
|
8739
|
+
portrait: "1024x1536",
|
|
8740
|
+
landscape: "1536x1024"
|
|
8741
|
+
};
|
|
8742
|
+
const made = await backend.makeImages({
|
|
8743
|
+
prompt: payload.prompt,
|
|
8744
|
+
model: route.model,
|
|
8745
|
+
size: sizes[payload.aspect ?? "square"],
|
|
8746
|
+
count,
|
|
8747
|
+
timeoutMs,
|
|
8748
|
+
maxOutputBytes: probeOutputCap(count),
|
|
8749
|
+
signal: context2.signal
|
|
8750
|
+
});
|
|
8751
|
+
if (!made.ok) return made;
|
|
8752
|
+
let stored;
|
|
8753
|
+
try {
|
|
8754
|
+
stored = await storeOutputImages({
|
|
8755
|
+
images: made.images.slice(0, count),
|
|
8756
|
+
jobId: job.id,
|
|
8757
|
+
transport
|
|
8758
|
+
});
|
|
8759
|
+
} catch (error) {
|
|
8760
|
+
return refused(
|
|
8761
|
+
"backend-error",
|
|
8762
|
+
`could not upload the images (${error instanceof Error ? error.message : "unknown error"})`
|
|
8763
|
+
);
|
|
8764
|
+
}
|
|
8765
|
+
if (!stored.ok) return refused("backend-error", stored.message);
|
|
8766
|
+
context2.madeImages(stored.refs);
|
|
8767
|
+
return {
|
|
8768
|
+
ok: true,
|
|
8769
|
+
text: made.text,
|
|
8770
|
+
durationMs: this.#now() - started
|
|
8771
|
+
};
|
|
8772
|
+
}
|
|
8773
|
+
/** The upstream's blob endpoints, under this job's lease (§2). */
|
|
8774
|
+
#blobTransport(job, signal) {
|
|
8775
|
+
const client = this.#options.client;
|
|
8776
|
+
const lease = {
|
|
8777
|
+
runnerId: this.#options.runnerId,
|
|
8778
|
+
jobId: job.id,
|
|
8779
|
+
leaseId: job.lease.id
|
|
8780
|
+
};
|
|
8781
|
+
return {
|
|
8782
|
+
url: async (blob) => (await client.blobUrl({ ...lease, blob })).url,
|
|
8783
|
+
get: (url, size) => client.getBlob(url, size, signal),
|
|
8784
|
+
slot: async (bytes2) => {
|
|
8785
|
+
const slot = await client.blobSlot({ ...lease, bytes: bytes2 });
|
|
8786
|
+
return { blob: slot.blob, url: slot.url };
|
|
8787
|
+
},
|
|
8788
|
+
put: (url, object) => client.putBlob(url, object, signal)
|
|
8789
|
+
};
|
|
8790
|
+
}
|
|
7067
8791
|
async #ensureLocalServer(route, backend) {
|
|
7068
8792
|
const spawnServer = this.#options.spawnServer;
|
|
7069
8793
|
if (spawnServer === void 0) return;
|
|
@@ -7142,17 +8866,34 @@ var Runner = class {
|
|
|
7142
8866
|
}
|
|
7143
8867
|
const backend = this.#backendFor(route);
|
|
7144
8868
|
const gate = await this.#memoryDecision(route, job.id);
|
|
8869
|
+
const refusedForMemory = gate !== void 0 && !gate.admit;
|
|
8870
|
+
let outputImages;
|
|
8871
|
+
const toolRun = job.kind === "llm.tools" && !refusedForMemory ? await this.#runTools(job, route, backend, prompt, {
|
|
8872
|
+
community,
|
|
8873
|
+
limits,
|
|
8874
|
+
signal: controller.signal,
|
|
8875
|
+
deadlineAt: job.deadlineAt ?? Number.POSITIVE_INFINITY
|
|
8876
|
+
}) : void 0;
|
|
7145
8877
|
const result = gate !== void 0 && !gate.admit ? {
|
|
7146
8878
|
ok: false,
|
|
7147
8879
|
code: "insufficient-memory",
|
|
7148
8880
|
message: gate.why,
|
|
7149
8881
|
durationMs: 0
|
|
7150
|
-
} : await this.#
|
|
8882
|
+
} : toolRun !== void 0 ? toolRun.result : job.kind === "llm.vision" || job.kind === "llm.image" ? await this.#runImageKind(job, route, backend, {
|
|
8883
|
+
community,
|
|
8884
|
+
limits,
|
|
8885
|
+
signal: controller.signal,
|
|
8886
|
+
deadlineAt: job.deadlineAt ?? Number.POSITIVE_INFINITY,
|
|
8887
|
+
madeImages: (refs) => {
|
|
8888
|
+
outputImages = refs;
|
|
8889
|
+
}
|
|
8890
|
+
}) : await this.#runOnBackend(route, backend, prompt, {
|
|
7151
8891
|
community,
|
|
7152
8892
|
limits,
|
|
7153
8893
|
signal: controller.signal,
|
|
7154
|
-
/* Absent means unclamped, which is what happened before
|
|
7155
|
-
The stub always carries it, so on the real path it
|
|
8894
|
+
/* Absent means unclamped, which is what happened before
|
|
8895
|
+
B199. The stub always carries it, so on the real path it
|
|
8896
|
+
is set. */
|
|
7156
8897
|
deadlineAt: job.deadlineAt ?? Number.POSITIVE_INFINITY
|
|
7157
8898
|
});
|
|
7158
8899
|
if (!result.ok && result.code === "unauthorized") {
|
|
@@ -7188,7 +8929,7 @@ var Runner = class {
|
|
|
7188
8929
|
...result.until === void 0 ? {} : { until: result.until }
|
|
7189
8930
|
});
|
|
7190
8931
|
}
|
|
7191
|
-
const outcome = result.ok ? { outcome: "ok", text: result.text } : result.code === "canceled" ? { outcome: "canceled" } : (
|
|
8932
|
+
const outcome = result.ok ? outputImages !== void 0 ? { outcome: "ok", text: result.text, images: [...outputImages] } : { outcome: "ok", text: result.text } : result.code === "canceled" ? { outcome: "canceled" } : (
|
|
7192
8933
|
/**
|
|
7193
8934
|
* The class, never the text — a8137b5.
|
|
7194
8935
|
*
|
|
@@ -7255,11 +8996,18 @@ var Runner = class {
|
|
|
7255
8996
|
stopKind: backend.stopReasons.kind
|
|
7256
8997
|
} : {}
|
|
7257
8998
|
});
|
|
8999
|
+
if (toolRun !== void 0 && community && this.#countsTowardJobCaps(job, route)) {
|
|
9000
|
+
for (let turn = 1; turn < toolRun.turns; turn += 1) {
|
|
9001
|
+
await this.#options.budgets.record(this.#now());
|
|
9002
|
+
}
|
|
9003
|
+
}
|
|
7258
9004
|
if (community && route.cost === "metered") {
|
|
7259
9005
|
await this.#options.spend.record(
|
|
7260
9006
|
route.service,
|
|
7261
9007
|
estimateCents(
|
|
7262
|
-
|
|
9008
|
+
/* Every turn re-sends the conversation so far, and that is what
|
|
9009
|
+
the provider bills. */
|
|
9010
|
+
toolRun?.inputChars ?? prompt.length,
|
|
7263
9011
|
result.ok ? result.text.length : 0,
|
|
7264
9012
|
route.spendCentsPerMillionTokens
|
|
7265
9013
|
),
|
|
@@ -7284,6 +9032,11 @@ var Runner = class {
|
|
|
7284
9032
|
/* The measured one. This was the literal `0` at the seal — see
|
|
7285
9033
|
`runJob`'s note. */
|
|
7286
9034
|
durationMs: result.durationMs,
|
|
9035
|
+
/* How many calls, and how each ended — on `llm.tools` only (§3). */
|
|
9036
|
+
...job.kind === "llm.tools" ? toolRun === void 0 ? NO_TOOL_CALLS : {
|
|
9037
|
+
toolCalls: toolRun.toolCalls,
|
|
9038
|
+
toolOutcomes: toolRun.toolOutcomes
|
|
9039
|
+
} : {},
|
|
7287
9040
|
/**
|
|
7288
9041
|
* **`result.timing` is NOT sealed — B304, and it was for months.**
|
|
7289
9042
|
*
|
|
@@ -7531,26 +9284,9 @@ var Runner = class {
|
|
|
7531
9284
|
});
|
|
7532
9285
|
}
|
|
7533
9286
|
async #tick() {
|
|
7534
|
-
const capabilities = await this.#
|
|
7535
|
-
|
|
7536
|
-
|
|
7537
|
-
daemonVersion: this.#options.daemonVersion,
|
|
7538
|
-
capabilities,
|
|
7539
|
-
// What this device could serve and is not, so the surfaces that must
|
|
7540
|
-
// explain a missing kind are not all on this machine. The hub decides
|
|
7541
|
-
// who is told what; the device only reports.
|
|
7542
|
-
withheld: this.#options.loaded.withheld.map((held) => ({
|
|
7543
|
-
kind: held.kind,
|
|
7544
|
-
claimants: held.services.map((service) => ({
|
|
7545
|
-
service,
|
|
7546
|
-
offer: this.#options.loaded.config.services[service]?.offer ?? "private"
|
|
7547
|
-
}))
|
|
7548
|
-
})),
|
|
7549
|
-
activeLeases: [...this.#active.entries()].map(([leaseId, held]) => ({
|
|
7550
|
-
jobId: held.jobId,
|
|
7551
|
-
leaseId
|
|
7552
|
-
}))
|
|
7553
|
-
});
|
|
9287
|
+
const { heartbeat, capabilities } = await this.#heartbeat(
|
|
9288
|
+
await this.#capabilitiesForTick()
|
|
9289
|
+
);
|
|
7554
9290
|
this.#noteClockSkew(heartbeat.serverTime);
|
|
7555
9291
|
this.#applySites(heartbeat.sites, heartbeat.successions);
|
|
7556
9292
|
this.#options.onEvent?.({
|
|
@@ -7623,6 +9359,94 @@ var Runner = class {
|
|
|
7623
9359
|
* `"go"` records the attempt; `"wait"` and `"poison"` do not, so a job
|
|
7624
9360
|
* refused inside its backoff does not burn one of its own chances.
|
|
7625
9361
|
*/
|
|
9362
|
+
/**
|
|
9363
|
+
* The heartbeat, with guard (b) — byollm_021 §Rollout R2.
|
|
9364
|
+
*
|
|
9365
|
+
* A 0.1.4 upstream parses `Capability.kind` as a closed enum, so one kind it
|
|
9366
|
+
* has never heard of refuses the WHOLE heartbeat and the device goes dark
|
|
9367
|
+
* for everything. Its refusal names no field — a fixed `400 bad-request` —
|
|
9368
|
+
* and a newer upstream's may, but must not be depended on. So this decides
|
|
9369
|
+
* by a differential retry and **never by the body**:
|
|
9370
|
+
*
|
|
9371
|
+
* - refused as a 400 while carrying a kind newer than 0.1.4 — advertised
|
|
9372
|
+
* in `capabilities` or reported in `withheld`, which 0.1.4 parses with
|
|
9373
|
+
* the same closed enum: send the same heartbeat once more with only those
|
|
9374
|
+
* kinds taken out of both;
|
|
9375
|
+
* - only if THAT succeeds, remember for the session that this upstream
|
|
9376
|
+
* predates them, keep them off every later heartbeat and claim, and say
|
|
9377
|
+
* so, naming the upstream;
|
|
9378
|
+
* - if the retry is refused too, the new kinds were not the cause: report
|
|
9379
|
+
* the original refusal as today, and blame no version.
|
|
9380
|
+
*
|
|
9381
|
+
* Returns the capabilities actually sent, which are the ones the claim
|
|
9382
|
+
* must send too: a claim carries the same matrix and the same enum.
|
|
9383
|
+
*/
|
|
9384
|
+
async #heartbeat(all) {
|
|
9385
|
+
const without = (caps) => caps.filter((capability) => !isAfter014(capability.kind));
|
|
9386
|
+
const withheldOf = (dropNew) => this.#options.loaded.withheld.filter((held) => !dropNew || !isAfter014(held.kind)).map((held) => ({
|
|
9387
|
+
kind: held.kind,
|
|
9388
|
+
claimants: held.services.map((service) => ({
|
|
9389
|
+
service,
|
|
9390
|
+
offer: this.#options.loaded.config.services[service]?.offer ?? "private"
|
|
9391
|
+
}))
|
|
9392
|
+
}));
|
|
9393
|
+
const send = (caps, dropNew) => this.#options.client.heartbeat({
|
|
9394
|
+
runnerId: this.#options.runnerId,
|
|
9395
|
+
daemonVersion: this.#options.daemonVersion,
|
|
9396
|
+
capabilities: caps,
|
|
9397
|
+
// What this device could serve and is not, so the surfaces that must
|
|
9398
|
+
// explain a missing kind are not all on this machine. The hub decides
|
|
9399
|
+
// who is told what; the device only reports.
|
|
9400
|
+
withheld: withheldOf(dropNew),
|
|
9401
|
+
activeLeases: [...this.#active.entries()].map(([leaseId, held]) => ({
|
|
9402
|
+
jobId: held.jobId,
|
|
9403
|
+
leaseId
|
|
9404
|
+
}))
|
|
9405
|
+
});
|
|
9406
|
+
const predates = this.#upstreamPredates;
|
|
9407
|
+
const capabilities = predates ? without(all) : [...all];
|
|
9408
|
+
try {
|
|
9409
|
+
return { heartbeat: await send(capabilities, predates), capabilities };
|
|
9410
|
+
} catch (refused) {
|
|
9411
|
+
const newerHeld = this.#options.loaded.withheld.filter(
|
|
9412
|
+
(held) => isAfter014(held.kind)
|
|
9413
|
+
);
|
|
9414
|
+
const newer = [
|
|
9415
|
+
...capabilities.filter((c) => isAfter014(c.kind)).map((c) => ({ kind: c.kind, services: [c.service] })),
|
|
9416
|
+
...newerHeld.map((held) => ({
|
|
9417
|
+
kind: held.kind,
|
|
9418
|
+
services: [...held.services]
|
|
9419
|
+
}))
|
|
9420
|
+
];
|
|
9421
|
+
if (predates || newer.length === 0 || !(refused instanceof ClientError) || refused.status !== 400) {
|
|
9422
|
+
throw refused;
|
|
9423
|
+
}
|
|
9424
|
+
const retried = without(capabilities);
|
|
9425
|
+
let heartbeat;
|
|
9426
|
+
try {
|
|
9427
|
+
heartbeat = await send(retried, true);
|
|
9428
|
+
} catch {
|
|
9429
|
+
throw refused;
|
|
9430
|
+
}
|
|
9431
|
+
this.#upstreamPredates = true;
|
|
9432
|
+
const kinds = [...new Set(newer.map((c) => c.kind))];
|
|
9433
|
+
const services = [...new Set(newer.flatMap((c) => c.services))];
|
|
9434
|
+
this.#options.onEvent?.({
|
|
9435
|
+
type: "upstream-predates-kinds",
|
|
9436
|
+
upstream: this.#options.client.origin,
|
|
9437
|
+
kinds,
|
|
9438
|
+
services,
|
|
9439
|
+
message: predatesMessage({
|
|
9440
|
+
upstream: this.#options.client.origin,
|
|
9441
|
+
kinds,
|
|
9442
|
+
services
|
|
9443
|
+
})
|
|
9444
|
+
});
|
|
9445
|
+
return { heartbeat, capabilities: retried };
|
|
9446
|
+
}
|
|
9447
|
+
}
|
|
9448
|
+
/** Guard (b)'s memory: this session's upstream refuses the newer kinds. */
|
|
9449
|
+
#upstreamPredates = false;
|
|
7626
9450
|
#attemptVerdict(key) {
|
|
7627
9451
|
for (const [id, entry] of this.#attempts) {
|
|
7628
9452
|
if (this.#now() - entry.at > ATTEMPT_TTL_MS) this.#attempts.delete(id);
|
|
@@ -7738,6 +9562,20 @@ var Runner = class {
|
|
|
7738
9562
|
const payload = await this.#openPayload(job, fetched.envelope);
|
|
7739
9563
|
const resolved = job.grant?.service;
|
|
7740
9564
|
const route = this.#routeFor(job.kind, resolved);
|
|
9565
|
+
if (job.kind === "llm.tools") {
|
|
9566
|
+
const parsed = ToolsPayload.safeParse(payload);
|
|
9567
|
+
const refusal = !parsed.success ? `this ${job.site} job's tools payload is malformed` : toolsConsentRefusal({
|
|
9568
|
+
payload: parsed.data,
|
|
9569
|
+
toolsDigest: job.grant?.toolsDigest
|
|
9570
|
+
}) === null ? void 0 : toolsRefusalText("tools-not-consented", {
|
|
9571
|
+
site: job.site,
|
|
9572
|
+
purpose: job.purpose ?? RESERVED_PURPOSE
|
|
9573
|
+
});
|
|
9574
|
+
if (refusal !== void 0) {
|
|
9575
|
+
await this.#refuseOnOurOwnRules(job, refusal);
|
|
9576
|
+
return;
|
|
9577
|
+
}
|
|
9578
|
+
}
|
|
7741
9579
|
if (job.owner !== this.#options.owner) {
|
|
7742
9580
|
const refusal = this.#withinCommunityBudget(job, payload, route);
|
|
7743
9581
|
if (refusal !== void 0) {
|
|
@@ -7745,6 +9583,26 @@ var Runner = class {
|
|
|
7745
9583
|
return;
|
|
7746
9584
|
}
|
|
7747
9585
|
}
|
|
9586
|
+
if (job.kind === "llm.vision" || job.kind === "llm.image") {
|
|
9587
|
+
const parsed = job.kind === "llm.vision" ? VisionPayload.safeParse(payload) : ImagePayload.safeParse(payload);
|
|
9588
|
+
if (!parsed.success) {
|
|
9589
|
+
await this.#refuseOnOurOwnRules(
|
|
9590
|
+
job,
|
|
9591
|
+
`its ${job.kind} payload is not one this device can run`
|
|
9592
|
+
);
|
|
9593
|
+
return;
|
|
9594
|
+
}
|
|
9595
|
+
if (job.kind === "llm.vision" && job.owner !== this.#options.owner) {
|
|
9596
|
+
const over = imageBudgetRefusal(
|
|
9597
|
+
visionImages(parsed.data),
|
|
9598
|
+
this.#options.loaded.config.community.maxImageBytes
|
|
9599
|
+
);
|
|
9600
|
+
if (over !== void 0) {
|
|
9601
|
+
await this.#refuseOnOurOwnRules(job, over, "image-bytes");
|
|
9602
|
+
return;
|
|
9603
|
+
}
|
|
9604
|
+
}
|
|
9605
|
+
}
|
|
7748
9606
|
const { outcome, ran } = await this.runJob({
|
|
7749
9607
|
...job,
|
|
7750
9608
|
payload,
|
|
@@ -7767,7 +9625,8 @@ var Runner = class {
|
|
|
7767
9625
|
ran ?? {
|
|
7768
9626
|
model: route?.model ?? "unknown",
|
|
7769
9627
|
backendClass: route?.backendClass ?? "http",
|
|
7770
|
-
durationMs: 0
|
|
9628
|
+
durationMs: 0,
|
|
9629
|
+
...job.kind === "llm.tools" ? NO_TOOL_CALLS : {}
|
|
7771
9630
|
}
|
|
7772
9631
|
);
|
|
7773
9632
|
await this.#safely(
|
|
@@ -7802,6 +9661,7 @@ var Runner = class {
|
|
|
7802
9661
|
throw new Error("this daemon has no keys, so it cannot seal a result");
|
|
7803
9662
|
}
|
|
7804
9663
|
const keys = await identity.keys();
|
|
9664
|
+
const Sealed = SealedOutcomeFor(job.kind);
|
|
7805
9665
|
return seal2({
|
|
7806
9666
|
// The outcome **and how it was produced**, as one signed statement —
|
|
7807
9667
|
// cloud_008 §2.5. Sealing only the outcome would leave the site
|
|
@@ -7819,7 +9679,7 @@ var Runner = class {
|
|
|
7819
9679
|
* is better than one that succeeds here and hangs forever on somebody
|
|
7820
9680
|
* else's page — which is exactly the trade B304 turned out to be.
|
|
7821
9681
|
*/
|
|
7822
|
-
plaintext: JSON.stringify(
|
|
9682
|
+
plaintext: JSON.stringify(Sealed.parse({ outcome, ran })),
|
|
7823
9683
|
senderKeys: keys,
|
|
7824
9684
|
// Back to the site that sent it, which is not necessarily the only
|
|
7825
9685
|
// site this machine serves. Sealing to `sitePinned` would send site B's
|
|
@@ -7966,11 +9826,12 @@ var Runner = class {
|
|
|
7966
9826
|
* ({@link ByollmRunner.#countsTowardJobCaps}); the payload is measured
|
|
7967
9827
|
* whatever it says.
|
|
7968
9828
|
*
|
|
7969
|
-
* The admission-time call passes `
|
|
7970
|
-
* the sender chose
|
|
7971
|
-
*
|
|
7972
|
-
*
|
|
7973
|
-
* and one that lied
|
|
9829
|
+
* The admission-time call passes `sizeClassFloor(job.sizeClass)`, derived
|
|
9830
|
+
* from a class the sender chose, and so refuses only what cannot fit. This
|
|
9831
|
+
* passes `payloadTextLength` of the opened payload, which is the same
|
|
9832
|
+
* function the site used to derive that class — so a job whose declaration
|
|
9833
|
+
* was honest is measured to the same place it declared, and one that lied
|
|
9834
|
+
* is caught here. This is the gate; admission is not.
|
|
7974
9835
|
*
|
|
7975
9836
|
* The whole `check` is re-run rather than the payload arm alone, because the
|
|
7976
9837
|
* limits it enforces are one decision: a device that has since hit its
|
|
@@ -8342,8 +10203,8 @@ function sleep(ms, signal) {
|
|
|
8342
10203
|
}
|
|
8343
10204
|
|
|
8344
10205
|
// src/install.ts
|
|
8345
|
-
import { mkdir as
|
|
8346
|
-
import { dirname as
|
|
10206
|
+
import { mkdir as mkdir9, readFile as readFile13, rm as rm5, stat as stat3, writeFile as writeFile8 } from "fs/promises";
|
|
10207
|
+
import { dirname as dirname9 } from "path";
|
|
8347
10208
|
|
|
8348
10209
|
// src/service.ts
|
|
8349
10210
|
import { stat as stat2 } from "fs/promises";
|
|
@@ -8603,7 +10464,7 @@ async function serviceState(plan, run2) {
|
|
|
8603
10464
|
return { state: "installed", detail: "registered, not running" };
|
|
8604
10465
|
}
|
|
8605
10466
|
async function installedProgram(plan) {
|
|
8606
|
-
const raw = await
|
|
10467
|
+
const raw = await readFile13(plan.unitPath).catch(() => null);
|
|
8607
10468
|
if (raw === null) return null;
|
|
8608
10469
|
const unit = raw.toString(plan.unitEncoding);
|
|
8609
10470
|
const found = plan.platform === "darwin" ? /<key>ProgramArguments<\/key>\s*<array>\s*<string>([^<]+)<\/string>/.exec(
|
|
@@ -8653,7 +10514,7 @@ async function installService(target, run2, wait = (ms) => new Promise((resolve)
|
|
|
8653
10514
|
if (refusal !== null) {
|
|
8654
10515
|
return { ok: false, lines: [refusal], plan };
|
|
8655
10516
|
}
|
|
8656
|
-
await
|
|
10517
|
+
await mkdir9(dirname9(plan.unitPath), { recursive: true });
|
|
8657
10518
|
await writeUnit(plan.unitPath, plan.unitContents, plan.unitEncoding);
|
|
8658
10519
|
for (const [index, command] of plan.activate.entries()) {
|
|
8659
10520
|
const result = await run2(command);
|
|
@@ -8661,7 +10522,7 @@ async function installService(target, run2, wait = (ms) => new Promise((resolve)
|
|
|
8661
10522
|
if (result.code !== 0 && !mayFail) {
|
|
8662
10523
|
const fallback = plan.fallback;
|
|
8663
10524
|
if (fallback !== void 0) {
|
|
8664
|
-
const fell = await
|
|
10525
|
+
const fell = await mkdir9(dirname9(fallback.unitPath), {
|
|
8665
10526
|
recursive: true
|
|
8666
10527
|
}).then(
|
|
8667
10528
|
() => writeUnit(
|
|
@@ -8895,7 +10756,7 @@ var spawnCommand = async (command) => {
|
|
|
8895
10756
|
});
|
|
8896
10757
|
};
|
|
8897
10758
|
async function writeUnit(path, contents, encoding) {
|
|
8898
|
-
await
|
|
10759
|
+
await writeFile8(
|
|
8899
10760
|
path,
|
|
8900
10761
|
encoding === "utf16le" ? `\uFEFF${contents}` : contents,
|
|
8901
10762
|
encoding
|
|
@@ -8903,24 +10764,24 @@ async function writeUnit(path, contents, encoding) {
|
|
|
8903
10764
|
}
|
|
8904
10765
|
|
|
8905
10766
|
// src/service-states.ts
|
|
8906
|
-
import { readFile as
|
|
10767
|
+
import { readFile as readFile14, writeFile as writeFile9 } from "fs/promises";
|
|
8907
10768
|
import { BackendIdSchema as BackendIdSchema2 } from "@byollm/protocol";
|
|
8908
|
-
import { z as
|
|
8909
|
-
var Stored =
|
|
8910
|
-
|
|
8911
|
-
|
|
8912
|
-
state:
|
|
8913
|
-
|
|
8914
|
-
|
|
8915
|
-
kind:
|
|
8916
|
-
detail:
|
|
10769
|
+
import { z as z9 } from "zod";
|
|
10770
|
+
var Stored = z9.record(
|
|
10771
|
+
z9.string(),
|
|
10772
|
+
z9.object({
|
|
10773
|
+
state: z9.discriminatedUnion("kind", [
|
|
10774
|
+
z9.object({ kind: z9.literal("answers"), model: z9.string() }),
|
|
10775
|
+
z9.object({
|
|
10776
|
+
kind: z9.literal("signed-out"),
|
|
10777
|
+
detail: z9.string().optional()
|
|
8917
10778
|
}),
|
|
8918
|
-
|
|
10779
|
+
z9.object({ kind: z9.literal("missing") }),
|
|
8919
10780
|
/* B098. Stopped and unstartable is not the same as absent, and the
|
|
8920
10781
|
file `byollm status` reads has to be able to tell them apart. */
|
|
8921
|
-
|
|
8922
|
-
kind:
|
|
8923
|
-
model:
|
|
10782
|
+
z9.object({
|
|
10783
|
+
kind: z9.literal("unstartable"),
|
|
10784
|
+
model: z9.string(),
|
|
8924
10785
|
/* An id this build does not know degrades to no offer, rather than
|
|
8925
10786
|
failing the whole file and blanking every service's line. */
|
|
8926
10787
|
startableAs: BackendIdSchema2.optional().catch(void 0)
|
|
@@ -8934,27 +10795,27 @@ var Stored = z8.record(
|
|
|
8934
10795
|
* itself on two screens, which is the defect this codebase keeps
|
|
8935
10796
|
* finding under other names.
|
|
8936
10797
|
*/
|
|
8937
|
-
|
|
8938
|
-
kind:
|
|
8939
|
-
model:
|
|
10798
|
+
z9.object({
|
|
10799
|
+
kind: z9.literal("stopped"),
|
|
10800
|
+
model: z9.string(),
|
|
8940
10801
|
/* B087. Absent means no — see the field's note in service-line.ts. */
|
|
8941
|
-
starts:
|
|
10802
|
+
starts: z9.boolean().optional()
|
|
8942
10803
|
}),
|
|
8943
|
-
|
|
8944
|
-
kind:
|
|
8945
|
-
detail:
|
|
8946
|
-
until:
|
|
10804
|
+
z9.object({
|
|
10805
|
+
kind: z9.literal("blocked"),
|
|
10806
|
+
detail: z9.string().optional(),
|
|
10807
|
+
until: z9.number().int().positive().optional()
|
|
8947
10808
|
}),
|
|
8948
|
-
|
|
10809
|
+
z9.object({ kind: z9.literal("unknown"), model: z9.string() })
|
|
8949
10810
|
]),
|
|
8950
|
-
signIn:
|
|
10811
|
+
signIn: z9.string().optional()
|
|
8951
10812
|
})
|
|
8952
10813
|
);
|
|
8953
10814
|
async function writeServiceStates(path, states) {
|
|
8954
10815
|
const out = {};
|
|
8955
10816
|
for (const [service, report2] of states) out[service] = report2;
|
|
8956
10817
|
try {
|
|
8957
|
-
await
|
|
10818
|
+
await writeFile9(path, `${JSON.stringify(out, null, 2)}
|
|
8958
10819
|
`, "utf8");
|
|
8959
10820
|
} catch {
|
|
8960
10821
|
}
|
|
@@ -8962,7 +10823,7 @@ async function writeServiceStates(path, states) {
|
|
|
8962
10823
|
async function readServiceStates(path) {
|
|
8963
10824
|
try {
|
|
8964
10825
|
const parsed = Stored.safeParse(
|
|
8965
|
-
JSON.parse(await
|
|
10826
|
+
JSON.parse(await readFile14(path, "utf8"))
|
|
8966
10827
|
);
|
|
8967
10828
|
if (!parsed.success) return /* @__PURE__ */ new Map();
|
|
8968
10829
|
return new Map(Object.entries(parsed.data));
|
|
@@ -9131,17 +10992,18 @@ async function runCli(argv, options = {}) {
|
|
|
9131
10992
|
options.env
|
|
9132
10993
|
);
|
|
9133
10994
|
}
|
|
9134
|
-
if (rest.length > 0) {
|
|
10995
|
+
if (rest.length > 0 && !(rest.length === 1 && rest[0] === "--probe-images")) {
|
|
9135
10996
|
io.err(
|
|
9136
10997
|
`byollm services takes no arguments, and got ${rest.map((arg) => JSON.stringify(arg)).join(" ")}.
|
|
9137
10998
|
|
|
9138
|
-
byollm services
|
|
9139
|
-
byollm services
|
|
10999
|
+
byollm services every service, and the model it runs
|
|
11000
|
+
byollm services --probe-images ask the image services again
|
|
11001
|
+
byollm services manage turn services on and off
|
|
9140
11002
|
`
|
|
9141
11003
|
);
|
|
9142
11004
|
return 2;
|
|
9143
11005
|
}
|
|
9144
|
-
return commandServices(paths, io, service);
|
|
11006
|
+
return commandServices(paths, io, service, rest[0] === "--probe-images");
|
|
9145
11007
|
case "model":
|
|
9146
11008
|
return commandModel(paths, rest, io);
|
|
9147
11009
|
/* Gone, and saying so rather than doing something adjacent — see
|
|
@@ -9255,8 +11117,8 @@ async function commandName(paths, args, io) {
|
|
|
9255
11117
|
`);
|
|
9256
11118
|
return 0;
|
|
9257
11119
|
}
|
|
9258
|
-
await
|
|
9259
|
-
await
|
|
11120
|
+
await mkdir10(dirname10(paths.label), { recursive: true });
|
|
11121
|
+
await writeFile10(paths.label, `${next.slice(0, 120)}
|
|
9260
11122
|
`, { mode: 384 });
|
|
9261
11123
|
io.out(
|
|
9262
11124
|
`This device will pair as ${next.slice(0, 120)}.
|
|
@@ -9428,7 +11290,7 @@ function connectTarget(args) {
|
|
|
9428
11290
|
async function labelFor(paths, flag) {
|
|
9429
11291
|
if (flag !== void 0 && flag !== "") return flag.slice(0, 120);
|
|
9430
11292
|
try {
|
|
9431
|
-
const saved = (await
|
|
11293
|
+
const saved = (await readFile15(paths.label, "utf8")).trim();
|
|
9432
11294
|
if (saved !== "") return saved.slice(0, 120);
|
|
9433
11295
|
} catch {
|
|
9434
11296
|
}
|
|
@@ -9552,7 +11414,8 @@ async function commandConnect(paths, args, io, signal) {
|
|
|
9552
11414
|
budgets,
|
|
9553
11415
|
spend,
|
|
9554
11416
|
spentGrants,
|
|
9555
|
-
ingress
|
|
11417
|
+
ingress,
|
|
11418
|
+
imageProbes: fileImageProbeStore(paths.imageProbes)
|
|
9556
11419
|
});
|
|
9557
11420
|
const capabilities = await runner.detectCapabilities({ canary: true });
|
|
9558
11421
|
await writeServiceStates(paths.serviceStates, runner.serviceStates);
|
|
@@ -9761,7 +11624,7 @@ function reportSkipped(pairings, io) {
|
|
|
9761
11624
|
async function retireAllowlist(paths, io) {
|
|
9762
11625
|
let raw;
|
|
9763
11626
|
try {
|
|
9764
|
-
raw = await
|
|
11627
|
+
raw = await readFile15(paths.allowlist, "utf8");
|
|
9765
11628
|
} catch {
|
|
9766
11629
|
return;
|
|
9767
11630
|
}
|
|
@@ -9902,6 +11765,9 @@ async function runLoop(paths, origins, io, signal, supervised = !process.stdout.
|
|
|
9902
11765
|
// daemon that actually runs is getting on.
|
|
9903
11766
|
healthPath: paths.health,
|
|
9904
11767
|
heartbeatPath: paths.heartbeat,
|
|
11768
|
+
/* One store for every pairing's runner and for `start` and `services`,
|
|
11769
|
+
so the paid image probe runs once a day, not once a runner. */
|
|
11770
|
+
imageProbes: fileImageProbeStore(paths.imageProbes),
|
|
9905
11771
|
// Written whenever it changes, not only at start — S2. `byollm status`
|
|
9906
11772
|
// is a different process and this file is the whole of what reaches it.
|
|
9907
11773
|
onServiceStates: (states) => writeServiceStates(paths.serviceStates, states),
|
|
@@ -10168,9 +12034,13 @@ function report(origin, event, io) {
|
|
|
10168
12034
|
`)
|
|
10169
12035
|
);
|
|
10170
12036
|
break;
|
|
12037
|
+
case "upstream-predates-kinds":
|
|
12038
|
+
io.err(`${at} ${host} ${stripControlChars(event.message)}
|
|
12039
|
+
`);
|
|
12040
|
+
break;
|
|
10171
12041
|
case "now-serving":
|
|
10172
12042
|
io.out(
|
|
10173
|
-
`${at} ${host} now serving ${event.site}, enabled from your dashboard.
|
|
12043
|
+
`${at} ${host} now serving ${event.site}${event.withTools === true ? " with its tools" : ""}, enabled from your dashboard.
|
|
10174
12044
|
fingerprint: ${event.fingerprint}
|
|
10175
12045
|
Not expected? \`byollm stop\` stops all work, and \`byollm forget\` drops the pairing.
|
|
10176
12046
|
`
|
|
@@ -10448,7 +12318,15 @@ async function commandStatus(paths, io, service) {
|
|
|
10448
12318
|
` ${String(usage.hour)} in the last hour (cap ${String(usage.limits.maxJobsPerHour)}), ${String(usage.day)} today (cap ${String(usage.limits.maxJobsPerDay)})
|
|
10449
12319
|
`
|
|
10450
12320
|
);
|
|
10451
|
-
for (const line of brakeLines(
|
|
12321
|
+
for (const line of brakeLines(
|
|
12322
|
+
entries,
|
|
12323
|
+
now,
|
|
12324
|
+
{
|
|
12325
|
+
...usage.limits,
|
|
12326
|
+
maxImageBytes: loaded.config.community.maxImageBytes
|
|
12327
|
+
},
|
|
12328
|
+
paths.config
|
|
12329
|
+
)) {
|
|
10452
12330
|
io.out(` ${line}
|
|
10453
12331
|
`);
|
|
10454
12332
|
}
|
|
@@ -10488,7 +12366,8 @@ function brakeLines(entries, now, limits, configPath) {
|
|
|
10488
12366
|
"hourly-cap": `hourly cap (${String(limits.maxJobsPerHour)}) \u2014 raise it: byollm budget community --per-hour N`,
|
|
10489
12367
|
"daily-cap": `daily cap (${String(limits.maxJobsPerDay)}) \u2014 raise it: byollm budget community --per-day N`,
|
|
10490
12368
|
"payload-too-large": `payload limit (${String(limits.maxPayloadChars)} characters) \u2014 community.maxPayloadChars in ${configPath}`,
|
|
10491
|
-
"ledger-untrusted": 'its record of community work cannot be read \u2014 see "bookkeeping this device cannot read" above'
|
|
12369
|
+
"ledger-untrusted": 'its record of community work cannot be read \u2014 see "bookkeeping this device cannot read" above',
|
|
12370
|
+
"image-bytes": `image limit (${String(limits.maxImageBytes)} bytes a job) \u2014 community.maxImageBytes in ${configPath}`
|
|
10492
12371
|
};
|
|
10493
12372
|
return [...counts].map(
|
|
10494
12373
|
([brake, count]) => `refused ${String(count)} in the last hour: ${why[brake]}`
|
|
@@ -10531,6 +12410,25 @@ async function commandLog(paths, args, io) {
|
|
|
10531
12410
|
);
|
|
10532
12411
|
continue;
|
|
10533
12412
|
}
|
|
12413
|
+
if (entry.type === "tool-call") {
|
|
12414
|
+
io.out(
|
|
12415
|
+
` tool call ${String(entry.seq)} ${stripControlChars(entry.name)}` + (entry.arguments === void 0 ? ` arguments not retained (${String(entry.argumentsChars)} chars, sha256 ${entry.argumentsHash.slice(0, 16)}\u2026)` : ` ${stripControlChars(full ? entry.arguments : entry.arguments.slice(0, 90))}`) + "\n"
|
|
12416
|
+
);
|
|
12417
|
+
continue;
|
|
12418
|
+
}
|
|
12419
|
+
if (entry.type === "tool-outcome") {
|
|
12420
|
+
io.out(
|
|
12421
|
+
` tool call ${String(entry.seq)} ${entry.outcome}` + (entry.chars === void 0 ? "" : ` ${String(entry.chars)} chars, sha256 ${(entry.contentHash ?? "").slice(0, 16)}\u2026`) + (entry.detail === void 0 ? "" : ` ${stripControlChars(entry.detail)}`) + "\n"
|
|
12422
|
+
);
|
|
12423
|
+
continue;
|
|
12424
|
+
}
|
|
12425
|
+
if (entry.type === "tool-dropped") {
|
|
12426
|
+
io.out(
|
|
12427
|
+
` tool result DROPPED ${stripControlChars(entry.why)}
|
|
12428
|
+
`
|
|
12429
|
+
);
|
|
12430
|
+
continue;
|
|
12431
|
+
}
|
|
10534
12432
|
if (entry.type === "memory") {
|
|
10535
12433
|
const gb = gigabytes;
|
|
10536
12434
|
io.out(
|
|
@@ -10643,7 +12541,7 @@ async function commandOffer(paths, args, io, serviceIo, env = process.env) {
|
|
|
10643
12541
|
}
|
|
10644
12542
|
let raw;
|
|
10645
12543
|
try {
|
|
10646
|
-
raw = await
|
|
12544
|
+
raw = await readFile15(paths.config, "utf8");
|
|
10647
12545
|
} catch {
|
|
10648
12546
|
io.err(
|
|
10649
12547
|
`no config at ${paths.config} \u2014 nothing to offer yet.
|
|
@@ -10809,7 +12707,7 @@ ${BUDGET_USAGE}`);
|
|
|
10809
12707
|
}
|
|
10810
12708
|
let raw;
|
|
10811
12709
|
try {
|
|
10812
|
-
raw = await
|
|
12710
|
+
raw = await readFile15(paths.config, "utf8");
|
|
10813
12711
|
} catch {
|
|
10814
12712
|
io.err(
|
|
10815
12713
|
`no config at ${paths.config} \u2014 nothing to set a budget on yet.
|
|
@@ -10963,7 +12861,7 @@ ${summarise(outcome).join("\n")}
|
|
|
10963
12861
|
}
|
|
10964
12862
|
return signal?.aborted === true ? 1 : 0;
|
|
10965
12863
|
}
|
|
10966
|
-
async function commandServices(paths, io, service) {
|
|
12864
|
+
async function commandServices(paths, io, service, probeImagesNow = false) {
|
|
10967
12865
|
const { loaded, configured, ingress, budgets, spend, spentGrants } = await context(paths);
|
|
10968
12866
|
if (!configured) {
|
|
10969
12867
|
io.out(
|
|
@@ -10986,7 +12884,11 @@ async function commandServices(paths, io, service) {
|
|
|
10986
12884
|
budgets,
|
|
10987
12885
|
spend,
|
|
10988
12886
|
spentGrants,
|
|
10989
|
-
ingress
|
|
12887
|
+
ingress,
|
|
12888
|
+
/* What the daemon learned, and nothing spent to learn it again unless
|
|
12889
|
+
asked: `services` is a command people run often (§5). */
|
|
12890
|
+
imageProbes: fileImageProbeStore(paths.imageProbes),
|
|
12891
|
+
probeImages: probeImagesNow ? "now" : "never"
|
|
10990
12892
|
});
|
|
10991
12893
|
const advertised = await runner.detectCapabilities();
|
|
10992
12894
|
const advertisedKinds = new Set(advertised.map((c) => c.kind));
|
|
@@ -11006,7 +12908,17 @@ async function commandServices(paths, io, service) {
|
|
|
11006
12908
|
${offered}
|
|
11007
12909
|
`
|
|
11008
12910
|
);
|
|
11009
|
-
|
|
12911
|
+
const toolsProbe = route.kind === "llm.tools" ? runner.toolsProbes.get(route.service) : void 0;
|
|
12912
|
+
const imageProbe = route.kind === "llm.vision" || route.kind === "llm.image" ? { held: runner.imageProbes.get(`${route.service} ${route.kind}`) } : void 0;
|
|
12913
|
+
if (!ok && toolsProbe !== void 0 && toolsProbe.kind !== "calls-tools") {
|
|
12914
|
+
io.out(` ${stripControlChars(toolsProbe.why)}
|
|
12915
|
+
`);
|
|
12916
|
+
} else if (!ok && imageProbe !== void 0 && imageProbe.held?.kind !== "passes") {
|
|
12917
|
+
io.out(
|
|
12918
|
+
` ${imageProbe.held === void 0 ? "not probed yet \u2014 `byollm services --probe-images` asks it (on a metered service, one paid call)" : stripControlChars(imageProbe.held.why)}
|
|
12919
|
+
`
|
|
12920
|
+
);
|
|
12921
|
+
} else if (!ok) {
|
|
11010
12922
|
const probed = runner.serviceStates.get(route.service)?.state;
|
|
11011
12923
|
const detail = probed?.kind === "signed-out" || probed?.kind === "blocked" ? probed.detail : void 0;
|
|
11012
12924
|
const hint = await diagnoseRoute({
|
|
@@ -11018,6 +12930,16 @@ async function commandServices(paths, io, service) {
|
|
|
11018
12930
|
`);
|
|
11019
12931
|
}
|
|
11020
12932
|
}
|
|
12933
|
+
for (const [name, service2] of Object.entries(loaded.config.services)) {
|
|
12934
|
+
if (service2.kinds.includes("llm.tools")) continue;
|
|
12935
|
+
if (backendDescriptor4(service2.type).class !== "http") continue;
|
|
12936
|
+
io.out(
|
|
12937
|
+
` i llm.tools ${name} does not use sites' tools (off by default)
|
|
12938
|
+
to turn them on, add "llm.tools" to services.${name}.kinds in ${paths.config}
|
|
12939
|
+
the tools run on the site; ${name}'s model reads what they return
|
|
12940
|
+
`
|
|
12941
|
+
);
|
|
12942
|
+
}
|
|
11021
12943
|
for (const held of loaded.withheld) {
|
|
11022
12944
|
io.out(
|
|
11023
12945
|
` \u2026 ${held.kind.padEnd(14)} no default \u2014 ${String(held.services.length)} services answer it (${held.services.join(", ")})
|
|
@@ -11152,7 +13074,7 @@ async function exists(path) {
|
|
|
11152
13074
|
}
|
|
11153
13075
|
|
|
11154
13076
|
// src/index.ts
|
|
11155
|
-
var DAEMON_VERSION = "0.1.
|
|
13077
|
+
var DAEMON_VERSION = "0.1.5-beta.2";
|
|
11156
13078
|
function daemonVersion() {
|
|
11157
13079
|
return {
|
|
11158
13080
|
daemon: DAEMON_VERSION,
|
|
@@ -11173,6 +13095,7 @@ export {
|
|
|
11173
13095
|
normalizeOrigin,
|
|
11174
13096
|
checkBaseUrl,
|
|
11175
13097
|
BASE_URL_REFUSAL_MESSAGES,
|
|
13098
|
+
composePrompt,
|
|
11176
13099
|
DaemonConfig,
|
|
11177
13100
|
DEFAULT_CONFIG,
|
|
11178
13101
|
loadConfig,
|
|
@@ -11203,7 +13126,10 @@ export {
|
|
|
11203
13126
|
Pairings,
|
|
11204
13127
|
daemonPaths,
|
|
11205
13128
|
defaultRoot,
|
|
11206
|
-
|
|
13129
|
+
CLOSING_TURN,
|
|
13130
|
+
runToolLoop,
|
|
13131
|
+
clientToolTransport,
|
|
13132
|
+
KINDS_AFTER_0_1_4,
|
|
11207
13133
|
Runner,
|
|
11208
13134
|
DAEMON_VERSION,
|
|
11209
13135
|
daemonVersion,
|
|
@@ -11211,4 +13137,4 @@ export {
|
|
|
11211
13137
|
runCli,
|
|
11212
13138
|
main
|
|
11213
13139
|
};
|
|
11214
|
-
//# sourceMappingURL=chunk-
|
|
13140
|
+
//# sourceMappingURL=chunk-GWC3CCIW.js.map
|