@byollm/relay 0.1.3 → 0.1.5-beta.1

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/dist/index.js CHANGED
@@ -7,8 +7,9 @@ import {
7
7
  RelayState,
8
8
  SEAL_ATTEMPTS_BEFORE_EVICTION,
9
9
  UNSEALED_PER_PAIR,
10
- routeKey
11
- } from "./chunk-4ISUXCIO.js";
10
+ routeKey,
11
+ subStateOf
12
+ } from "./chunk-YRJDRZ6G.js";
12
13
 
13
14
  // src/index.ts
14
15
  import {
@@ -25,9 +26,14 @@ import {
25
26
  HeartbeatRequest,
26
27
  MAX_ENVELOPE_BYTES as MAX_ENVELOPE_BYTES2,
27
28
  PROTOCOL_VERSION,
29
+ TOOLS_SPENT_MESSAGE,
28
30
  ReleaseRequest,
29
31
  ResultRequest,
30
32
  RequestSignature,
33
+ ToolCallRequest,
34
+ ToolCallResponse,
35
+ ToolResultsRequest,
36
+ ToolResultsResponse,
31
37
  envelopeBytes,
32
38
  keyId,
33
39
  verifyRequest,
@@ -171,6 +177,16 @@ var fail = (status, error, message) => ({
171
177
  status,
172
178
  body: { error, message }
173
179
  });
180
+ var TOOL_CALL_REFUSALS = {
181
+ ...REFUSALS,
182
+ "not-tools": fail(400, "bad-request", "this job does not carry tools"),
183
+ budget: fail(403, "forbidden", TOOLS_SPENT_MESSAGE),
184
+ "call-conflict": fail(
185
+ 400,
186
+ "bad-request",
187
+ "that call id already names a different call"
188
+ )
189
+ };
174
190
  var DaemonPlane = class {
175
191
  #deps;
176
192
  constructor(deps) {
@@ -546,6 +562,59 @@ var DaemonPlane = class {
546
562
  return ok(recorded);
547
563
  });
548
564
  }
565
+ /**
566
+ * One sealed tool call, from the device holding the job — byollm_012 §5.2.
567
+ *
568
+ * What the relay can check without opening anything, and nothing more: the
569
+ * signature (above), the grant, the kind, the per-job count and the size.
570
+ * The envelope goes to the site as it arrived.
571
+ */
572
+ toolCall(auth, body) {
573
+ return this.#authed(
574
+ auth,
575
+ body,
576
+ ToolCallRequest,
577
+ async (request, device) => {
578
+ if (request.runnerId !== device.runnerId) {
579
+ return fail(403, "forbidden", "runner id does not match the key");
580
+ }
581
+ const bytes = envelopeBytes(request.envelope);
582
+ if (bytes > MAX_ENVELOPE_BYTES2) return tooLargeRefusal(bytes);
583
+ const recorded = await this.#deps.state.recordToolCall({
584
+ jobId: request.jobId,
585
+ runnerId: device.runnerId,
586
+ leaseId: request.leaseId,
587
+ callId: request.callId,
588
+ envelope: request.envelope
589
+ });
590
+ if ("refused" in recorded) return TOOL_CALL_REFUSALS[recorded.refused];
591
+ return ok(
592
+ ToolCallResponse.parse({
593
+ callId: recorded.callId,
594
+ expiresAt: recorded.expiresAt
595
+ })
596
+ );
597
+ }
598
+ );
599
+ }
600
+ /** The site's answers to the calls this device names — byollm_012 §5.2. */
601
+ toolResults(auth, body) {
602
+ return this.#authed(
603
+ auth,
604
+ body,
605
+ ToolResultsRequest,
606
+ async (request, device) => {
607
+ if (request.runnerId !== device.runnerId) {
608
+ return fail(403, "forbidden", "runner id does not match the key");
609
+ }
610
+ const results = await this.#deps.state.toolResults({
611
+ runnerId: device.runnerId,
612
+ calls: request.calls
613
+ });
614
+ return ok(ToolResultsResponse.parse({ results }));
615
+ }
616
+ );
617
+ }
549
618
  heartbeat(auth, body) {
550
619
  return this.#authed(
551
620
  auth,
@@ -680,7 +749,7 @@ var STATE_COLOUR = {
680
749
  };
681
750
  function jobRow(job, now) {
682
751
  const claimed = job.claimedBy;
683
- const waiting = job.state === "awaiting-payload" && job.awaitingUntil !== void 0 ? `${String(Math.max(0, job.awaitingUntil - now))}ms left` : "";
752
+ const waiting = job.state === "awaiting-payload" && job.awaitingUntil !== void 0 ? `${String(Math.max(0, job.awaitingUntil - now))}ms left` : subStateOf(job, now) ?? "";
684
753
  return `<tr>
685
754
  <td><code>${escape(job.id)}</code></td>
686
755
  <td>${escape(job.stub.kind)}</td>
@@ -1112,6 +1181,11 @@ import {
1112
1181
  JobStub,
1113
1182
  RequestSignature as RequestSignature2,
1114
1183
  SealedEnvelope,
1184
+ SiteEnqueueTools,
1185
+ SiteToolCalls,
1186
+ SiteToolResultRequest,
1187
+ SiteToolResultResponse,
1188
+ toolsRefusalText,
1115
1189
  verifySiteRequest
1116
1190
  } from "@byollm/protocol";
1117
1191
  import { z as z3 } from "zod";
@@ -1125,8 +1199,21 @@ var EnqueueRequest = z3.object({
1125
1199
  * package — a site that tried to attach a prompt here would be refused by
1126
1200
  * the schema, not by a reviewer.
1127
1201
  */
1128
- stub: JobStub
1129
- }).strict();
1202
+ stub: JobStub,
1203
+ /**
1204
+ * The job's tools, beside the stub — byollm_012 §8, byollm_021 R1.
1205
+ *
1206
+ * Exactly when the stub is `llm.tools`, and never otherwise, so no other
1207
+ * enqueue's bytes change (a field a 0.1.4 relay's `.strict()` would
1208
+ * refuse rides only on a kind it refuses anyway). `maxToolCalls` is the
1209
+ * relay's per-job budget; `toolsDigest` is handed to `satisfiable`, where
1210
+ * a deployment holding the manifest of record compares it.
1211
+ */
1212
+ tools: SiteEnqueueTools.optional()
1213
+ }).strict().refine(
1214
+ (request) => request.stub.kind === "llm.tools" === (request.tools !== void 0),
1215
+ { message: "an enqueue carries tools exactly when its kind is llm.tools" }
1216
+ );
1130
1217
  var PayloadRequest = z3.object({
1131
1218
  protocolVersion: z3.literal(PROTOCOL_VERSION2),
1132
1219
  siteId: z3.string().min(1),
@@ -1224,7 +1311,8 @@ var SitePlane = class {
1224
1311
  siteId,
1225
1312
  owner: request.stub.owner,
1226
1313
  purpose: request.stub.purpose,
1227
- kind: request.stub.kind
1314
+ kind: request.stub.kind,
1315
+ ...request.tools === void 0 ? {} : { toolsDigest: request.tools.toolsDigest }
1228
1316
  });
1229
1317
  } catch {
1230
1318
  return fail2(
@@ -1240,6 +1328,16 @@ var SitePlane = class {
1240
1328
  `this site does not declare ${request.stub.purpose ?? "that purpose"} \u2014 declare it on Developer Sites, and the people who have already connected will each map the new slot before it routes`
1241
1329
  );
1242
1330
  }
1331
+ if (answer?.verdict === "tools-mismatch") {
1332
+ return fail2(
1333
+ 400,
1334
+ "bad-request",
1335
+ toolsRefusalText("tools-manifest-mismatch", {
1336
+ site: siteId,
1337
+ purpose: request.stub.purpose ?? "its purpose"
1338
+ })
1339
+ );
1340
+ }
1243
1341
  if (answer?.verdict === "unmapped") {
1244
1342
  return fail2(
1245
1343
  409,
@@ -1257,7 +1355,8 @@ var SitePlane = class {
1257
1355
  const job = await this.#deps.state.enqueue({
1258
1356
  id: request.stub.id,
1259
1357
  siteId,
1260
- stub: request.stub
1358
+ stub: request.stub,
1359
+ ...request.tools === void 0 ? {} : { maxToolCalls: request.tools.maxToolCalls }
1261
1360
  });
1262
1361
  if ("refused" in job) {
1263
1362
  return fail2(400, "bad-request", "that stub was not accepted");
@@ -1355,6 +1454,56 @@ var SitePlane = class {
1355
1454
  }
1356
1455
  );
1357
1456
  }
1457
+ /**
1458
+ * The tool calls waiting on this site — byollm_012 §5.2.
1459
+ *
1460
+ * With each device's public identity, which the site verifies the call
1461
+ * against and seals its answer to. A directory again, as `pending` is.
1462
+ */
1463
+ calls(auth, siteId) {
1464
+ return this.#authed(
1465
+ auth,
1466
+ { siteId },
1467
+ QueryRequest,
1468
+ (request) => request.siteId,
1469
+ async (_request, site) => {
1470
+ await this.#deps.state.sweep();
1471
+ const calls = await this.#deps.state.toolCalls(site);
1472
+ return ok2(SiteToolCalls.parse({ calls }));
1473
+ }
1474
+ );
1475
+ }
1476
+ /**
1477
+ * One sealed answer — accepted only for an outstanding call of the same
1478
+ * job (`TOOL_ENVELOPES_BOUND`, the relay's half).
1479
+ */
1480
+ toolResult(auth, body) {
1481
+ return this.#authed(
1482
+ auth,
1483
+ body,
1484
+ SiteToolResultRequest,
1485
+ (request) => request.siteId,
1486
+ async (request, siteId) => {
1487
+ const bytes = envelopeBytes2(request.envelope);
1488
+ if (bytes > MAX_ENVELOPE_BYTES3) return tooLargeRefusal(bytes);
1489
+ const answered = await this.#deps.state.answerToolCall({
1490
+ siteId,
1491
+ jobId: request.jobId,
1492
+ callId: request.callId,
1493
+ envelope: request.envelope
1494
+ });
1495
+ if ("refused" in answered) {
1496
+ return answered.refused === "not-found" ? fail2(404, "not-found", "no such call is waiting on this job") : fail2(409, "too-late", "that call is no longer waiting");
1497
+ }
1498
+ return ok2(
1499
+ SiteToolResultResponse.parse({
1500
+ jobId: request.jobId,
1501
+ callId: request.callId
1502
+ })
1503
+ );
1504
+ }
1505
+ );
1506
+ }
1358
1507
  /** Sealed results, for the site to open and verify. */
1359
1508
  results(auth, siteId) {
1360
1509
  return this.#authed(
@@ -1516,6 +1665,14 @@ var Relay = class {
1516
1665
  )
1517
1666
  );
1518
1667
  }
1668
+ if (path === "/relay/site/calls") {
1669
+ return json(
1670
+ await this.#site.calls(siteAuth, url.searchParams.get("siteId") ?? "")
1671
+ );
1672
+ }
1673
+ if (path === "/relay/site/tool-result") {
1674
+ return json(await this.#site.toolResult(siteAuth, body));
1675
+ }
1519
1676
  if (path === "/relay/site/results") {
1520
1677
  return json(
1521
1678
  await this.#site.results(
@@ -1540,6 +1697,10 @@ var Relay = class {
1540
1697
  return json(await this.#daemon.heartbeat(auth, body));
1541
1698
  case "release":
1542
1699
  return json(await this.#daemon.release(auth, body));
1700
+ case "tool-call":
1701
+ return json(await this.#daemon.toolCall(auth, body));
1702
+ case "tool-results":
1703
+ return json(await this.#daemon.toolResults(auth, body));
1543
1704
  default:
1544
1705
  return json({ status: 404, body: { error: "not-found" } });
1545
1706
  }
@@ -1589,6 +1750,7 @@ export {
1589
1750
  debugPage,
1590
1751
  newDeviceCode,
1591
1752
  newUserCode,
1592
- routeKey
1753
+ routeKey,
1754
+ subStateOf
1593
1755
  };
1594
1756
  //# sourceMappingURL=index.js.map