glove-foundry 0.11.0 → 0.13.0

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.
@@ -33,6 +33,9 @@ function startedThreadIds(context) {
33
33
  function isOpenThread(thread) {
34
34
  return thread.status === "working" || thread.status === "waiting";
35
35
  }
36
+ function isTakenOverBy(thread, agentId) {
37
+ return Boolean(thread.takenOver?.agentIds.includes(agentId));
38
+ }
36
39
  function clip(text, max) {
37
40
  const flat = text.replace(/\s+/g, " ").trim();
38
41
  return flat.length > max ? `${flat.slice(0, max - 1)}\u2026` : flat;
@@ -43,13 +46,16 @@ function threadSubject(content) {
43
46
  function escapeAttribute(value) {
44
47
  return value.replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;");
45
48
  }
46
- function renderMeshMessage(message, side) {
49
+ function renderMeshMessage(message, side, peer) {
47
50
  const who = `${message.from.name} (${message.from.agentId})`;
48
51
  const status = message.kind === "message" ? "" : ` status="${message.kind === "reply" ? "replied" : message.kind}"`;
49
- const open = `<mesh-message id="${escapeAttribute(message.id)}" thread="${escapeAttribute(message.threadId)}" from="${escapeAttribute(message.from.agentId)}" name="${escapeAttribute(message.from.name)}"${status}>`;
52
+ const human = message.from.kind === "human";
53
+ const open = `<mesh-message id="${escapeAttribute(message.id)}" thread="${escapeAttribute(message.threadId)}" from="${escapeAttribute(message.from.agentId)}" name="${escapeAttribute(message.from.name)}"${human ? ' party="human"' : ""}${status}>`;
50
54
  const body = message.kind === "failed" ? `It stopped: ${message.content}` : message.content;
51
55
  const lines = [open, body, "</mesh-message>"];
52
- if (side === "recipient") {
56
+ if (human) {
57
+ lines.push(side === "recipient" ? `${message.from.name} is a person in this thread, not an agent. Your final reply goes to the thread on its own.` : `${message.from.name} is a person in your thread${peer ? ` with ${peer}` : ""}. To answer in the thread, use glove_mesh_send_message${peer ? ` to "${peer}"` : ""} with in_reply_to "${message.id}".`);
58
+ } else if (side === "recipient") {
53
59
  lines.push(
54
60
  `${who} is another agent, not the user. Your final reply goes back to them on its own, so make it the answer itself.`,
55
61
  `If you cannot go on without something only they or their user know, ask with glove_mesh_send_message to "${message.from.agentId}".`
@@ -212,6 +218,11 @@ function createMeshTools(context) {
212
218
  }
213
219
  ];
214
220
  }
221
+ var DEFAULT_MESH_REFUSAL = "Your message could not be sent.";
222
+ var RECORD_ID = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,199}$/;
223
+ function isBindableThreadId(value) {
224
+ return RECORD_ID.test(value) && !value.includes("..");
225
+ }
215
226
 
216
227
  // src/delegation.ts
217
228
  import { createHash, randomUUID as randomUUID2 } from "node:crypto";
@@ -289,7 +300,7 @@ function renderDelegatedTask(input) {
289
300
  }
290
301
  function renderDelivery(delivery) {
291
302
  if (delivery.kind === "transmission" || delivery.kind === "agent-message") return delivery.message;
292
- if (delivery.kind === "mesh") return renderMeshMessage(delivery.message, delivery.side);
303
+ if (delivery.kind === "mesh") return renderMeshMessage(delivery.message, delivery.side, delivery.peer);
293
304
  if (delivery.kind === "reply") {
294
305
  return [
295
306
  `<delegation-reply id="${escapeAttribute2(delivery.delegationId)}" from="${escapeAttribute2(delivery.from)}">`,
@@ -445,8 +456,11 @@ function createWorkerTools(context, delegation, options) {
445
456
  }
446
457
  ];
447
458
  }
459
+ function mailboxName(id) {
460
+ return createHash("sha256").update(id, "utf8").digest("hex");
461
+ }
448
462
  function conversationDirectory(directory, conversationId) {
449
- return join(directory, Buffer.from(conversationId, "utf8").toString("hex"));
463
+ return join(directory, mailboxName(conversationId));
450
464
  }
451
465
  async function writeMailboxDelivery(directory, conversationId, delivery) {
452
466
  const target = conversationDirectory(directory, conversationId);
@@ -462,11 +476,12 @@ var DELIVERY_SCHEMA = z2.intersection(z2.object({ request: z2.custom().optional(
462
476
  z2.object({
463
477
  kind: z2.literal("mesh"),
464
478
  side: z2.enum(["recipient", "starter"]),
479
+ peer: z2.string().optional(),
465
480
  message: z2.object({
466
481
  kind: z2.enum(["message", "reply", "failed", "ack"]),
467
482
  id: z2.string(),
468
483
  threadId: z2.string(),
469
- from: z2.object({ agentId: z2.string(), name: z2.string() }),
484
+ from: z2.object({ agentId: z2.string(), name: z2.string(), kind: z2.enum(["agent", "human"]).optional() }),
470
485
  to: z2.string(),
471
486
  // A read receipt's note is optional, and JSON drops it when absent.
472
487
  content: z2.string().default(""),
@@ -806,7 +821,7 @@ var RunEventQueue = class {
806
821
  }
807
822
  };
808
823
  function leaseFile(directory, runId) {
809
- return join(directory, ".leases", Buffer.from(runId, "utf8").toString("hex"));
824
+ return join(directory, ".leases", mailboxName(runId));
810
825
  }
811
826
  async function writeConversationLease(directory, runId) {
812
827
  await mkdir(join(directory, ".leases"), { recursive: true, mode: 448 });
@@ -887,12 +902,15 @@ export {
887
902
  threadFromContext,
888
903
  startedThreadIds,
889
904
  isOpenThread,
905
+ isTakenOverBy,
890
906
  threadSubject,
891
907
  renderMeshMessages,
892
908
  renderOpenThreads,
893
909
  renderOwnThread,
894
910
  isMeshCommand,
895
911
  createMeshTools,
912
+ DEFAULT_MESH_REFUSAL,
913
+ isBindableThreadId,
896
914
  FOUNDRY_MAILBOX_DIRECTORY_ENV,
897
915
  FOUNDRY_CONVERSATION_LISTEN_EVENT,
898
916
  FOUNDRY_CONVERSATION_LEASE_EVENT,
@@ -374,6 +374,39 @@ var FoundryClient = class {
374
374
  }
375
375
  ));
376
376
  }
377
+ /** Mesh threads, newest first: by who started them, the conversation that did, the recipient or status. */
378
+ async threads(filter = {}) {
379
+ const query = new URLSearchParams();
380
+ for (const [key, value] of Object.entries(filter)) if (typeof value === "string") query.set(key, value);
381
+ const suffix = query.size ? `?${query}` : "";
382
+ return readResponse(await this.fetcher(`${this.baseUrl}/api/threads${suffix}`));
383
+ }
384
+ async thread(threadId) {
385
+ return readResponse(await this.fetcher(`${this.baseUrl}/api/threads/${encodeURIComponent(threadId)}`));
386
+ }
387
+ /** A person's message in a thread, from your record: `runtime.meshHumanMessage`. */
388
+ async meshHumanMessage(threadId, input) {
389
+ return readResponse(await this.fetcher(`${this.baseUrl}/api/threads/${encodeURIComponent(threadId)}/messages`, {
390
+ method: "POST",
391
+ headers: { "content-type": "application/json" },
392
+ body: JSON.stringify(input)
393
+ }));
394
+ }
395
+ /** A person takes a thread over: `runtime.meshTakeOver`. */
396
+ async meshTakeOver(threadId, options) {
397
+ return readResponse(await this.fetcher(`${this.baseUrl}/api/threads/${encodeURIComponent(threadId)}/take-over`, {
398
+ method: "POST",
399
+ headers: { "content-type": "application/json" },
400
+ body: JSON.stringify(options)
401
+ }));
402
+ }
403
+ /** Deliver or discard a message the application's gate held: `runtime.releaseMeshMessage`. */
404
+ async releaseMeshMessage(threadId, messageId, decision) {
405
+ return readResponse(await this.fetcher(
406
+ `${this.baseUrl}/api/threads/${encodeURIComponent(threadId)}/held/${encodeURIComponent(messageId)}`,
407
+ { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ decision }) }
408
+ ));
409
+ }
377
410
  async manifest() {
378
411
  return readResponse(
379
412
  await this.fetcher(`${this.baseUrl}/api/manifest`)
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  assertEventReceipt,
3
3
  matchesReceiptFilter
4
- } from "./chunk-2JCISE2U.js";
4
+ } from "./chunk-5ZYOBBOK.js";
5
5
  import {
6
6
  DURABLE_SEGMENT,
7
7
  EMPTY_CAPABILITY_REGISTRY,
@@ -25,7 +25,7 @@ import {
25
25
  installationKey,
26
26
  internalAgentName,
27
27
  waitForFoundryCoreCommandResult
28
- } from "./chunk-NWFOY3L7.js";
28
+ } from "./chunk-WK2O4GIB.js";
29
29
 
30
30
  // src/application.ts
31
31
  var FOUNDRY_APPLICATION_BRAND = /* @__PURE__ */ Symbol.for(
@@ -2114,6 +2114,98 @@ function createFoundryCoreTools(context, desiredSchedules = [], outbound = {}) {
2114
2114
  ];
2115
2115
  }
2116
2116
 
2117
+ // src/builtins.ts
2118
+ var FOUNDRY_BUILTIN_TOOLS = Object.freeze([
2119
+ { name: "glove_foundry_spawn", group: "spawning", workspaceWide: true },
2120
+ { name: "glove_foundry_background", group: "spawning", workspaceWide: false },
2121
+ { name: "glove_foundry_schedule", group: "scheduling", workspaceWide: false },
2122
+ { name: "glove_foundry_schedules", group: "scheduling", workspaceWide: false },
2123
+ { name: "glove_foundry_sleep", group: "scheduling", workspaceWide: false },
2124
+ { name: "glove_foundry_transmit", group: "transmission", workspaceWide: false },
2125
+ { name: "glove_foundry_conversations", group: "conversations", workspaceWide: true },
2126
+ { name: "glove_foundry_workspace", group: "workspace", workspaceWide: true },
2127
+ { name: "glove_foundry_shared_inbox", group: "workspace", workspaceWide: true },
2128
+ { name: "glove_foundry_tasks", group: "workspace", workspaceWide: true },
2129
+ { name: "glove_foundry_environment", group: "environment", workspaceWide: false },
2130
+ { name: "glove_foundry_delegate", group: "delegation", workspaceWide: false },
2131
+ { name: "glove_foundry_reply", group: "delegation", workspaceWide: false },
2132
+ { name: "glove_foundry_ask", group: "delegation", workspaceWide: false },
2133
+ { name: "glove_foundry_message", group: "messaging", workspaceWide: false },
2134
+ { name: "glove_mesh_list_agents", group: "mesh", workspaceWide: false },
2135
+ { name: "glove_mesh_send_message", group: "mesh", workspaceWide: false },
2136
+ { name: "glove_mesh_broadcast", group: "mesh", workspaceWide: false },
2137
+ { name: "glove_mesh_acknowledge", group: "mesh", workspaceWide: false }
2138
+ ].map((tool) => Object.freeze(tool)));
2139
+ var BY_NAME = new Map(FOUNDRY_BUILTIN_TOOLS.map((tool) => [tool.name, tool]));
2140
+ function isFoundryBuiltin(name) {
2141
+ return BY_NAME.has(name);
2142
+ }
2143
+ function matcher(patterns) {
2144
+ const exact = new Set(patterns.filter((pattern) => !pattern.endsWith("*")));
2145
+ const prefixes = patterns.filter((pattern) => pattern.endsWith("*")).map((pattern) => pattern.slice(0, -1));
2146
+ return (name) => exact.has(name) || prefixes.some((prefix) => name.startsWith(prefix));
2147
+ }
2148
+ function builtinAllowed(policy) {
2149
+ if (policy === void 0) return () => true;
2150
+ let decide;
2151
+ const form = policy;
2152
+ if (Array.isArray(policy)) {
2153
+ const allowed = matcher(policy);
2154
+ decide = (tool) => allowed(tool.name);
2155
+ } else if (Array.isArray(form.allow)) {
2156
+ const allowed = matcher(form.allow);
2157
+ decide = (tool) => allowed(tool.name);
2158
+ } else if (Array.isArray(form.block)) {
2159
+ const blocked = matcher(form.block);
2160
+ decide = (tool) => !blocked(tool.name);
2161
+ } else if (typeof form.filter === "function") {
2162
+ decide = form.filter;
2163
+ } else {
2164
+ throw new Error("builtins must be a list of names, { allow }, { block } or { filter }.");
2165
+ }
2166
+ return (name) => {
2167
+ const tool = BY_NAME.get(name);
2168
+ return tool ? Boolean(decide(tool)) : true;
2169
+ };
2170
+ }
2171
+ function builtinsForCommand(type) {
2172
+ switch (type) {
2173
+ case "spawn":
2174
+ return ["glove_foundry_spawn"];
2175
+ case "background":
2176
+ return ["glove_foundry_background"];
2177
+ case "schedule":
2178
+ return ["glove_foundry_schedule"];
2179
+ case "schedule.update":
2180
+ case "schedule.pause":
2181
+ case "schedule.resume":
2182
+ case "schedule.cancel":
2183
+ return ["glove_foundry_schedules"];
2184
+ case "sleep":
2185
+ return ["glove_foundry_sleep"];
2186
+ case "delegate":
2187
+ return ["glove_foundry_delegate"];
2188
+ case "delegate.reply":
2189
+ return ["glove_foundry_reply"];
2190
+ case "delegate.ask":
2191
+ return ["glove_foundry_ask"];
2192
+ case "agent.message":
2193
+ return ["glove_foundry_message"];
2194
+ case "mesh.list":
2195
+ return ["glove_mesh_list_agents", "glove_mesh_broadcast"];
2196
+ case "mesh.send":
2197
+ return ["glove_mesh_send_message", "glove_mesh_broadcast"];
2198
+ case "mesh.ack":
2199
+ return ["glove_mesh_acknowledge"];
2200
+ // Installed apps' own transmission tools send it too; see the runtime.
2201
+ case "transmit":
2202
+ return ["glove_foundry_transmit"];
2203
+ default:
2204
+ return null;
2205
+ }
2206
+ }
2207
+ var FOUNDRY_BUILTINS_MOUNTED_EVENT = "foundry.builtins.mounted";
2208
+
2117
2209
  // src/approval.ts
2118
2210
  import { randomUUID as randomUUID3 } from "node:crypto";
2119
2211
  import { mkdir, readFile, readdir as readdir2, writeFile } from "node:fs/promises";
@@ -2141,6 +2233,9 @@ function readApprovalPolicy(value) {
2141
2233
  }
2142
2234
  }
2143
2235
  var APPROVAL_ID = /^approval_[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
2236
+ function isFoundryApprovalId(value) {
2237
+ return APPROVAL_ID.test(value);
2238
+ }
2144
2239
  function approvalPath(directory, id2, suffix) {
2145
2240
  if (!APPROVAL_ID.test(id2)) throw new Error("Invalid Foundry approval id.");
2146
2241
  return join(directory, `${id2}${suffix}`);
@@ -2649,11 +2744,17 @@ export {
2649
2744
  installedApplicationTransmissionToolName,
2650
2745
  createInstalledApplicationTransmissionTools,
2651
2746
  createFoundryCoreTools,
2747
+ FOUNDRY_BUILTIN_TOOLS,
2748
+ isFoundryBuiltin,
2749
+ builtinAllowed,
2750
+ builtinsForCommand,
2751
+ FOUNDRY_BUILTINS_MOUNTED_EVENT,
2652
2752
  FOUNDRY_APPROVAL_DIRECTORY_ENV,
2653
2753
  FOUNDRY_APPROVAL_POLICY_ENV,
2654
2754
  DEFAULT_APPROVAL_WAIT_MS,
2655
2755
  DEFAULT_APPROVAL_EXPIRES_AFTER_MS,
2656
2756
  readApprovalPolicy,
2757
+ isFoundryApprovalId,
2657
2758
  createFoundryApproval,
2658
2759
  listFoundryApprovals,
2659
2760
  resolveFoundryApproval,