glove-foundry 0.7.0 → 0.8.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.
- package/README.md +80 -4
- package/dist/{chunk-64UBLGYM.js → chunk-CYOIHSUX.js} +131 -7
- package/dist/{chunk-2GXPSIRL.js → chunk-DTFCEX67.js} +1384 -104
- package/dist/{chunk-EJZJAVNG.js → chunk-EUGKVW3J.js} +335 -7
- package/dist/{chunk-AC6SRI5V.js → chunk-J6NHL4SM.js} +357 -56
- package/dist/{chunk-EEHGAWXM.js → chunk-L5JQVTHD.js} +4 -0
- package/dist/{chunk-IPASF7PK.js → chunk-X3HZYVZS.js} +36 -1
- package/dist/cli.js +4 -4
- package/dist/{client-BgpgsnoO.d.ts → client-CekanYLW.d.ts} +96 -56
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -1
- package/dist/config.d.ts +7 -0
- package/dist/{event-delivery-BaYe6d9w.d.ts → event-delivery-CNCsVqi8.d.ts} +379 -11
- package/dist/execution-agent.js +1 -1
- package/dist/execution-daemon.js +10 -5
- package/dist/execution-loader.js +174 -17
- package/dist/index.d.ts +17 -6
- package/dist/index.js +114 -15
- package/dist/voice.d.ts +11 -7
- package/dist/voice.js +51 -10
- package/docs/README.md +1 -1
- package/docs/building-with-foundry.md +128 -4
- package/docs/durable-subagents.md +101 -23
- package/docs/execution.md +4 -0
- package/docs/inspector.md +137 -29
- package/package.json +12 -11
- package/templates/travel-concierge/README.md +1 -1
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
installationKey,
|
|
22
22
|
internalAgentName,
|
|
23
23
|
waitForFoundryCoreCommandResult
|
|
24
|
-
} from "./chunk-
|
|
24
|
+
} from "./chunk-CYOIHSUX.js";
|
|
25
25
|
|
|
26
26
|
// src/application.ts
|
|
27
27
|
var FOUNDRY_APPLICATION_BRAND = /* @__PURE__ */ Symbol.for(
|
|
@@ -230,7 +230,7 @@ var CREDENTIAL_FIELD_NAMES = /* @__PURE__ */ new Set([
|
|
|
230
230
|
"credentials"
|
|
231
231
|
]);
|
|
232
232
|
function assertSchemaContainsNoCredentialFields(transmissionId, area, schema) {
|
|
233
|
-
const
|
|
233
|
+
const json2 = JSONSchema.make(schema);
|
|
234
234
|
const visit = (value, path) => {
|
|
235
235
|
if (!value || typeof value !== "object") return;
|
|
236
236
|
if (Array.isArray(value)) {
|
|
@@ -256,7 +256,7 @@ function assertSchemaContainsNoCredentialFields(transmissionId, area, schema) {
|
|
|
256
256
|
if (name !== "properties") visit(child, path);
|
|
257
257
|
}
|
|
258
258
|
};
|
|
259
|
-
visit(
|
|
259
|
+
visit(json2, area);
|
|
260
260
|
}
|
|
261
261
|
function isFoundryTransmission(value) {
|
|
262
262
|
return Boolean(
|
|
@@ -839,6 +839,7 @@ var MemoryFoundryDataAdapter = class {
|
|
|
839
839
|
inboundDeliveries = /* @__PURE__ */ new Map();
|
|
840
840
|
events = /* @__PURE__ */ new Map();
|
|
841
841
|
pendingInbound = /* @__PURE__ */ new Map();
|
|
842
|
+
approvals = /* @__PURE__ */ new Map();
|
|
842
843
|
activations = /* @__PURE__ */ new Map();
|
|
843
844
|
conversations = /* @__PURE__ */ new Map();
|
|
844
845
|
workspace = /* @__PURE__ */ new Map();
|
|
@@ -922,6 +923,7 @@ var MemoryFoundryDataAdapter = class {
|
|
|
922
923
|
const conversationIds = new Set([...this.conversations.values()].filter((item) => item.agentId === agentId).map((item) => item.id));
|
|
923
924
|
for (const [id2, item] of this.events) if (item.target.agentId === agentId) this.events.delete(id2);
|
|
924
925
|
for (const [id2, item] of this.pendingInbound) if (item.request.agentId === agentId) this.pendingInbound.delete(id2);
|
|
926
|
+
for (const [id2, item] of this.approvals) if (item.agentId === agentId) this.approvals.delete(id2);
|
|
925
927
|
for (const [id2, item] of this.activations) if (item.agentId === agentId) this.activations.delete(id2);
|
|
926
928
|
for (const id2 of conversationIds) this.conversations.delete(id2);
|
|
927
929
|
for (const [id2, item] of this.inbox) if (item.agentId === agentId || conversationIds.has(item.conversationId ?? "")) this.inbox.delete(id2);
|
|
@@ -1021,6 +1023,20 @@ var MemoryFoundryDataAdapter = class {
|
|
|
1021
1023
|
listPendingInbound() {
|
|
1022
1024
|
return Effect2.sync(() => [...this.pendingInbound.values()].map((record) => structuredClone(record)));
|
|
1023
1025
|
}
|
|
1026
|
+
putApproval(approval) {
|
|
1027
|
+
return Effect2.sync(() => {
|
|
1028
|
+
this.approvals.set(approval.id, structuredClone(approval));
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
getApproval(id2) {
|
|
1032
|
+
return Effect2.sync(() => {
|
|
1033
|
+
const approval = this.approvals.get(id2);
|
|
1034
|
+
return approval ? structuredClone(approval) : null;
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
listApprovals() {
|
|
1038
|
+
return Effect2.sync(() => [...this.approvals.values()].map((approval) => structuredClone(approval)));
|
|
1039
|
+
}
|
|
1024
1040
|
getActivation(id2) {
|
|
1025
1041
|
return Effect2.succeed(this.activations.get(id2) ?? null);
|
|
1026
1042
|
}
|
|
@@ -1128,6 +1144,8 @@ function createAgentInstance(definitionId, options = {}, provisioningKey) {
|
|
|
1128
1144
|
...options.durableSubagent ? { durableSubagent: options.durableSubagent } : {},
|
|
1129
1145
|
workspaceId: options.workspaceId ?? "default",
|
|
1130
1146
|
...provisioningKey ? { provisioningKey } : {},
|
|
1147
|
+
...options.name !== void 0 ? { name: options.name } : {},
|
|
1148
|
+
...options.description !== void 0 ? { description: options.description } : {},
|
|
1131
1149
|
context: options.context ?? {},
|
|
1132
1150
|
installations: options.installations ?? [],
|
|
1133
1151
|
playbooks: options.playbooks ?? [],
|
|
@@ -1140,6 +1158,8 @@ function defineAgentInstance(definition, options = {}) {
|
|
|
1140
1158
|
const seed = {
|
|
1141
1159
|
id: options.id ?? `agent_${randomUUID()}`,
|
|
1142
1160
|
workspaceId: options.workspaceId ?? "default",
|
|
1161
|
+
...options.name !== void 0 ? { name: options.name } : {},
|
|
1162
|
+
...options.description !== void 0 ? { description: options.description } : {},
|
|
1143
1163
|
context: options.context ?? {},
|
|
1144
1164
|
installations: options.installations ?? [],
|
|
1145
1165
|
playbooks: options.playbooks ?? [],
|
|
@@ -1256,7 +1276,7 @@ function agentScheduleRevision(schedule) {
|
|
|
1256
1276
|
|
|
1257
1277
|
// src/core-tools.ts
|
|
1258
1278
|
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
1259
|
-
import { Effect as Effect3, JSONSchema as JSONSchema2, Schema as Schema2 } from "effect";
|
|
1279
|
+
import { Effect as Effect3, Either, JSONSchema as JSONSchema2, ParseResult, Schema as Schema2 } from "effect";
|
|
1260
1280
|
import { z } from "zod";
|
|
1261
1281
|
var FOUNDRY_CORE_COMMAND_EVENT = "foundry.core.command";
|
|
1262
1282
|
function commandId() {
|
|
@@ -1428,7 +1448,30 @@ function success(command) {
|
|
|
1428
1448
|
}
|
|
1429
1449
|
};
|
|
1430
1450
|
}
|
|
1431
|
-
function
|
|
1451
|
+
function declaredOutbound(playbooks, applications) {
|
|
1452
|
+
const outboundOf = (owners) => owners.flatMap((application) => (application.transmissions ?? []).filter((transmission) => transmission.outbound));
|
|
1453
|
+
const routes = /* @__PURE__ */ new Map();
|
|
1454
|
+
for (const playbook of playbooks) {
|
|
1455
|
+
if (playbook.enabled === false) continue;
|
|
1456
|
+
for (const outbound of playbook.outbound ?? []) {
|
|
1457
|
+
const owners = outbound.applicationId ? applications.filter((application) => application.id === outbound.applicationId) : applications;
|
|
1458
|
+
const candidates = routes.get(outbound.routeId) ?? /* @__PURE__ */ new Set();
|
|
1459
|
+
for (const transmission of outboundOf(owners)) candidates.add(transmission);
|
|
1460
|
+
routes.set(outbound.routeId, candidates);
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
return new Map([...routes].map(([routeId, candidates]) => [routeId, [...candidates]]));
|
|
1464
|
+
}
|
|
1465
|
+
function asksFirst(transmission, payload) {
|
|
1466
|
+
const rule = transmission.outbound?.requiresPermission;
|
|
1467
|
+
if (rule === void 0) return false;
|
|
1468
|
+
if (typeof rule !== "function") return rule;
|
|
1469
|
+
const decoded = Schema2.decodeUnknownEither(transmission.outbound.input)(payload);
|
|
1470
|
+
if (Either.isRight(decoded)) return rule(decoded.right);
|
|
1471
|
+
return ParseResult.ArrayFormatter.formatIssueSync(decoded.left.issue).some((issue) => issue._tag === "Forbidden");
|
|
1472
|
+
}
|
|
1473
|
+
function createFoundryCoreTools(context, desiredSchedules = [], outbound = {}) {
|
|
1474
|
+
const routes = declaredOutbound(outbound.playbooks ?? context.agentInstance.playbooks, outbound.applications ?? []);
|
|
1432
1475
|
const emit = (command) => {
|
|
1433
1476
|
context.controls.commands.push(command);
|
|
1434
1477
|
context.controls.emit({ type: FOUNDRY_CORE_COMMAND_EVENT, data: command });
|
|
@@ -1707,7 +1750,22 @@ function createFoundryCoreTools(context, desiredSchedules = []) {
|
|
|
1707
1750
|
routeId: z.string(),
|
|
1708
1751
|
payload: z.unknown()
|
|
1709
1752
|
}),
|
|
1753
|
+
// The route's transmission decides, as for its application's send tool. When
|
|
1754
|
+
// more than one could own the route, any that would ask makes this ask.
|
|
1755
|
+
requiresPermission: (input) => {
|
|
1756
|
+
const answers = (routes.get(input.routeId) ?? []).map((transmission) => asksFirst(transmission, input.payload));
|
|
1757
|
+
if (answers.some((answer) => answer === true)) return true;
|
|
1758
|
+
const pending = answers.filter((answer) => typeof answer !== "boolean");
|
|
1759
|
+
return pending.length ? Promise.all(pending).then((all) => all.some(Boolean)) : false;
|
|
1760
|
+
},
|
|
1710
1761
|
async do(input) {
|
|
1762
|
+
if (!routes.has(input.routeId)) {
|
|
1763
|
+
return {
|
|
1764
|
+
status: "error",
|
|
1765
|
+
data: null,
|
|
1766
|
+
message: `Route "${input.routeId}" is not declared by this agent's playbooks.`
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1711
1769
|
const command = {
|
|
1712
1770
|
id: commandId(),
|
|
1713
1771
|
type: "transmit",
|
|
@@ -1889,11 +1947,26 @@ import { mkdir, readFile, readdir as readdir2, writeFile } from "node:fs/promise
|
|
|
1889
1947
|
import { join } from "node:path";
|
|
1890
1948
|
import {
|
|
1891
1949
|
Displaymanager,
|
|
1950
|
+
ToolPermissionDeferred,
|
|
1892
1951
|
permissionKey
|
|
1893
1952
|
} from "glove-core";
|
|
1894
1953
|
var FOUNDRY_APPROVAL_DIRECTORY_ENV = "GLOVE_FOUNDRY_APPROVAL_DIRECTORY";
|
|
1954
|
+
var FOUNDRY_APPROVAL_POLICY_ENV = "GLOVE_FOUNDRY_APPROVAL_POLICY";
|
|
1895
1955
|
var REQUEST_SUFFIX = ".request.json";
|
|
1896
1956
|
var RESOLUTION_SUFFIX = ".resolution.json";
|
|
1957
|
+
var SUSPENDED_SUFFIX = ".suspended.json";
|
|
1958
|
+
var DEFAULT_APPROVAL_WAIT_MS = 6e4;
|
|
1959
|
+
var DEFAULT_APPROVAL_EXPIRES_AFTER_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
1960
|
+
function readApprovalPolicy(value) {
|
|
1961
|
+
if (!value) return void 0;
|
|
1962
|
+
try {
|
|
1963
|
+
const parsed = JSON.parse(value);
|
|
1964
|
+
if (typeof parsed.durable !== "boolean" || typeof parsed.waitMs !== "number" || typeof parsed.expiresAfterMs !== "number") return void 0;
|
|
1965
|
+
return { durable: parsed.durable, waitMs: parsed.waitMs, expiresAfterMs: parsed.expiresAfterMs };
|
|
1966
|
+
} catch {
|
|
1967
|
+
return void 0;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1897
1970
|
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;
|
|
1898
1971
|
function approvalPath(directory, id2, suffix) {
|
|
1899
1972
|
if (!APPROVAL_ID.test(id2)) throw new Error("Invalid Foundry approval id.");
|
|
@@ -1949,7 +2022,7 @@ async function writeResolution(directory, id2, status, rejectExisting = false) {
|
|
|
1949
2022
|
async function createFoundryApproval(directory, input) {
|
|
1950
2023
|
await mkdir(directory, { recursive: true, mode: 448 });
|
|
1951
2024
|
const requestedAt = /* @__PURE__ */ new Date();
|
|
1952
|
-
const timeoutMs = Math.min(Math.max(input.timeoutMs ?? 3e5, 1e3), 6e5);
|
|
2025
|
+
const timeoutMs = input.expiresAfterMs !== void 0 ? Math.max(input.expiresAfterMs, 1e3) : Math.min(Math.max(input.timeoutMs ?? 3e5, 1e3), 6e5);
|
|
1953
2026
|
const request = Object.freeze({
|
|
1954
2027
|
id: `approval_${randomUUID3()}`,
|
|
1955
2028
|
runId: input.runId,
|
|
@@ -1959,6 +2032,7 @@ async function createFoundryApproval(directory, input) {
|
|
|
1959
2032
|
workspaceId: input.workspaceId,
|
|
1960
2033
|
toolName: input.toolName,
|
|
1961
2034
|
toolInput: serializable(input.toolInput),
|
|
2035
|
+
...input.toolCallId ? { toolCallId: input.toolCallId } : {},
|
|
1962
2036
|
requestedAt: requestedAt.toISOString(),
|
|
1963
2037
|
expiresAt: new Date(requestedAt.getTime() + timeoutMs).toISOString()
|
|
1964
2038
|
});
|
|
@@ -2012,6 +2086,40 @@ async function resolveFoundryApproval(directory, id2, decision) {
|
|
|
2012
2086
|
);
|
|
2013
2087
|
return Object.freeze({ ...request, status: resolution.status, resolvedAt: resolution.resolvedAt });
|
|
2014
2088
|
}
|
|
2089
|
+
async function foundryApprovalRequested(directory, id2) {
|
|
2090
|
+
return requestRecord(await readJson(approvalPath(directory, id2, REQUEST_SUFFIX))) !== null;
|
|
2091
|
+
}
|
|
2092
|
+
async function readFoundryApprovalResolution(directory, id2) {
|
|
2093
|
+
return resolutionRecord(await readJson(approvalPath(directory, id2, RESOLUTION_SUFFIX)));
|
|
2094
|
+
}
|
|
2095
|
+
async function markFoundryApprovalSuspended(directory, id2) {
|
|
2096
|
+
try {
|
|
2097
|
+
await writeFile(
|
|
2098
|
+
approvalPath(directory, id2, SUSPENDED_SUFFIX),
|
|
2099
|
+
`${JSON.stringify({ id: id2, suspendedAt: (/* @__PURE__ */ new Date()).toISOString() })}
|
|
2100
|
+
`,
|
|
2101
|
+
{ encoding: "utf8", flag: "wx", mode: 384 }
|
|
2102
|
+
);
|
|
2103
|
+
return true;
|
|
2104
|
+
} catch (cause) {
|
|
2105
|
+
if (cause.code === "EEXIST") return false;
|
|
2106
|
+
throw cause;
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
async function foundryApprovalSuspended(directory, id2) {
|
|
2110
|
+
return await readJson(approvalPath(directory, id2, SUSPENDED_SUFFIX)) !== null;
|
|
2111
|
+
}
|
|
2112
|
+
async function waitForDecision(directory, approval, signal, waitMs) {
|
|
2113
|
+
const until = Math.min(Date.now() + waitMs, Date.parse(approval.expiresAt));
|
|
2114
|
+
for (; ; ) {
|
|
2115
|
+
const resolved = resolutionRecord(
|
|
2116
|
+
await readJson(approvalPath(directory, approval.id, RESOLUTION_SUFFIX))
|
|
2117
|
+
);
|
|
2118
|
+
if (resolved) return resolved;
|
|
2119
|
+
if (signal.aborted || Date.now() >= until) return null;
|
|
2120
|
+
await new Promise((resolveWait) => setTimeout(resolveWait, 100));
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2015
2123
|
async function waitForResolution(directory, approval, signal) {
|
|
2016
2124
|
for (; ; ) {
|
|
2017
2125
|
const resolved = resolutionRecord(
|
|
@@ -2043,6 +2151,9 @@ var FoundryApprovalDisplayManager = class extends Displaymanager {
|
|
|
2043
2151
|
});
|
|
2044
2152
|
return false;
|
|
2045
2153
|
}
|
|
2154
|
+
const toolCallId = typeof request.toolCallId === "string" ? request.toolCallId : void 0;
|
|
2155
|
+
const durable = this.context.policy?.durable ? this.context.policy : void 0;
|
|
2156
|
+
if (durable) return this.durableDecision(this.context.directory, toolName, request.toolInput, toolCallId, durable);
|
|
2046
2157
|
try {
|
|
2047
2158
|
const approval = await createFoundryApproval(this.context.directory, {
|
|
2048
2159
|
runId: this.context.runId,
|
|
@@ -2051,7 +2162,9 @@ var FoundryApprovalDisplayManager = class extends Displaymanager {
|
|
|
2051
2162
|
conversationId: this.context.conversationId,
|
|
2052
2163
|
workspaceId: this.context.workspaceId,
|
|
2053
2164
|
toolName,
|
|
2054
|
-
toolInput: request.toolInput
|
|
2165
|
+
toolInput: request.toolInput,
|
|
2166
|
+
...toolCallId ? { toolCallId } : {},
|
|
2167
|
+
...this.context.policy ? { timeoutMs: this.context.policy.waitMs } : {}
|
|
2055
2168
|
});
|
|
2056
2169
|
this.context.emit({ type: "foundry.approval.requested", data: approval });
|
|
2057
2170
|
const resolution = await waitForResolution(
|
|
@@ -2072,6 +2185,52 @@ var FoundryApprovalDisplayManager = class extends Displaymanager {
|
|
|
2072
2185
|
return false;
|
|
2073
2186
|
}
|
|
2074
2187
|
}
|
|
2188
|
+
/**
|
|
2189
|
+
* A durable approval: wait a while for the decision in this run. If nobody
|
|
2190
|
+
* decides in time, or the run is stopping, the approval stays open and the
|
|
2191
|
+
* turn suspends at this tool call (`ToolPermissionDeferred`), with no
|
|
2192
|
+
* result recorded for it. The runtime resumes it when someone decides.
|
|
2193
|
+
*/
|
|
2194
|
+
async durableDecision(directory, toolName, toolInput, toolCallId, policy) {
|
|
2195
|
+
let approval;
|
|
2196
|
+
try {
|
|
2197
|
+
approval = await createFoundryApproval(directory, {
|
|
2198
|
+
runId: this.context.runId,
|
|
2199
|
+
definitionId: this.context.definitionId,
|
|
2200
|
+
agentId: this.context.agentId,
|
|
2201
|
+
conversationId: this.context.conversationId,
|
|
2202
|
+
workspaceId: this.context.workspaceId,
|
|
2203
|
+
toolName,
|
|
2204
|
+
toolInput,
|
|
2205
|
+
...toolCallId ? { toolCallId } : {},
|
|
2206
|
+
expiresAfterMs: policy.expiresAfterMs
|
|
2207
|
+
});
|
|
2208
|
+
} catch (cause) {
|
|
2209
|
+
this.context.emit({
|
|
2210
|
+
type: "foundry.approval.failed",
|
|
2211
|
+
data: { toolName, error: cause instanceof Error ? cause.message : String(cause) }
|
|
2212
|
+
});
|
|
2213
|
+
return false;
|
|
2214
|
+
}
|
|
2215
|
+
this.context.emit({ type: "foundry.approval.requested", data: approval });
|
|
2216
|
+
let resolution = await waitForDecision(directory, approval, this.context.signal, policy.waitMs);
|
|
2217
|
+
if (!resolution) {
|
|
2218
|
+
await markFoundryApprovalSuspended(directory, approval.id);
|
|
2219
|
+
resolution = resolutionRecord(await readJson(approvalPath(directory, approval.id, RESOLUTION_SUFFIX)));
|
|
2220
|
+
}
|
|
2221
|
+
if (resolution) {
|
|
2222
|
+
this.context.emit({
|
|
2223
|
+
type: "foundry.approval.settled",
|
|
2224
|
+
data: { approvalId: approval.id, toolName, status: resolution.status }
|
|
2225
|
+
});
|
|
2226
|
+
return resolution.status === "approved";
|
|
2227
|
+
}
|
|
2228
|
+
this.context.emit({ type: "foundry.approval.suspended", data: { approvalId: approval.id, toolName } });
|
|
2229
|
+
throw new ToolPermissionDeferred(
|
|
2230
|
+
`Approval ${approval.id} for ${toolName} waits for a decision.`,
|
|
2231
|
+
{ approvalId: approval.id, toolName }
|
|
2232
|
+
);
|
|
2233
|
+
}
|
|
2075
2234
|
};
|
|
2076
2235
|
function withFoundryPermissions(store) {
|
|
2077
2236
|
if (store.getPermission && store.setPermission) return store;
|
|
@@ -2094,11 +2253,172 @@ function withFoundryPermissions(store) {
|
|
|
2094
2253
|
});
|
|
2095
2254
|
}
|
|
2096
2255
|
|
|
2256
|
+
// src/request-trace.ts
|
|
2257
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
2258
|
+
var FOUNDRY_MODEL_REQUESTS_ENV = "GLOVE_FOUNDRY_MODEL_REQUESTS";
|
|
2259
|
+
var TEXT_LIMIT = 16e3;
|
|
2260
|
+
var JSON_LIMIT = 16e3;
|
|
2261
|
+
var SYSTEM_PROMPT_LIMIT = 64e3;
|
|
2262
|
+
var TOOL_SCHEMA_LIMIT = 8e3;
|
|
2263
|
+
var MESSAGES_BUDGET = 4e5;
|
|
2264
|
+
function hash(value) {
|
|
2265
|
+
return createHash3("sha256").update(value).digest("base64url").slice(0, 16);
|
|
2266
|
+
}
|
|
2267
|
+
function json(value) {
|
|
2268
|
+
try {
|
|
2269
|
+
return JSON.stringify(value) ?? "null";
|
|
2270
|
+
} catch {
|
|
2271
|
+
return JSON.stringify(String(value));
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
function cut(text, limit) {
|
|
2275
|
+
return text.length > limit ? { text: text.slice(0, limit), chars: text.length } : { text };
|
|
2276
|
+
}
|
|
2277
|
+
function capped(value, limit = JSON_LIMIT) {
|
|
2278
|
+
if (value === void 0) return void 0;
|
|
2279
|
+
if (typeof value === "string") {
|
|
2280
|
+
const { text, chars } = cut(value, limit);
|
|
2281
|
+
return chars === void 0 ? text : `${text}\u2026 [${chars} characters]`;
|
|
2282
|
+
}
|
|
2283
|
+
const serialized = json(value);
|
|
2284
|
+
if (serialized.length <= limit) return JSON.parse(serialized);
|
|
2285
|
+
return { truncated: true, chars: serialized.length, preview: serialized.slice(0, limit) };
|
|
2286
|
+
}
|
|
2287
|
+
function tracePart(part) {
|
|
2288
|
+
if (part.type === "text") return { type: "text", text: cut(part.text ?? "", TEXT_LIMIT).text };
|
|
2289
|
+
return {
|
|
2290
|
+
type: part.type,
|
|
2291
|
+
...part.name ? { name: part.name } : {},
|
|
2292
|
+
...part.source?.media_type ? { media_type: part.source.media_type } : {}
|
|
2293
|
+
};
|
|
2294
|
+
}
|
|
2295
|
+
function traceCall(call) {
|
|
2296
|
+
return { ...call.id ? { id: call.id } : {}, tool_name: call.tool_name, input_args: capped(call.input_args) };
|
|
2297
|
+
}
|
|
2298
|
+
function traceResult(result) {
|
|
2299
|
+
return {
|
|
2300
|
+
...result.call_id ? { call_id: result.call_id } : {},
|
|
2301
|
+
tool_name: result.tool_name,
|
|
2302
|
+
status: result.result.status,
|
|
2303
|
+
data: capped(result.result.data),
|
|
2304
|
+
...result.result.message ? { message: cut(result.result.message, TEXT_LIMIT).text } : {},
|
|
2305
|
+
...result.duration_ms !== void 0 ? { duration_ms: result.duration_ms } : {}
|
|
2306
|
+
};
|
|
2307
|
+
}
|
|
2308
|
+
function traceMessage(message) {
|
|
2309
|
+
const { text, chars } = cut(message.text ?? "", TEXT_LIMIT);
|
|
2310
|
+
return {
|
|
2311
|
+
sender: message.sender,
|
|
2312
|
+
text,
|
|
2313
|
+
...chars !== void 0 ? { text_chars: chars } : {},
|
|
2314
|
+
...message.framework_context ? { framework_context: message.framework_context } : {},
|
|
2315
|
+
...message.is_compaction ? { is_compaction: true } : {},
|
|
2316
|
+
...message.is_compaction_request ? { is_compaction_request: true } : {},
|
|
2317
|
+
...message.is_skill_injection ? { is_skill_injection: true } : {},
|
|
2318
|
+
...message.content?.length ? { content: message.content.map(tracePart) } : {},
|
|
2319
|
+
...message.tool_calls?.length ? { tool_calls: message.tool_calls.map(traceCall) } : {},
|
|
2320
|
+
...message.tool_results?.length ? { tool_results: message.tool_results.map(traceResult) } : {},
|
|
2321
|
+
...message.reasoning_content ? { reasoning_omitted: true } : {}
|
|
2322
|
+
};
|
|
2323
|
+
}
|
|
2324
|
+
function traceTool(tool) {
|
|
2325
|
+
const schema = capped(tool.input_schema, TOOL_SCHEMA_LIMIT);
|
|
2326
|
+
return {
|
|
2327
|
+
name: tool.name,
|
|
2328
|
+
description: cut(tool.description ?? "", TEXT_LIMIT).text,
|
|
2329
|
+
input_schema: schema && typeof schema === "object" ? schema : {}
|
|
2330
|
+
};
|
|
2331
|
+
}
|
|
2332
|
+
function requestTracer() {
|
|
2333
|
+
const prompts = /* @__PURE__ */ new Set();
|
|
2334
|
+
const toolSets = /* @__PURE__ */ new Set();
|
|
2335
|
+
const threads = /* @__PURE__ */ new Map();
|
|
2336
|
+
const messageHashes = /* @__PURE__ */ new WeakMap();
|
|
2337
|
+
const messageHash = (message) => {
|
|
2338
|
+
let value = messageHashes.get(message);
|
|
2339
|
+
if (!value) {
|
|
2340
|
+
value = hash(json(message));
|
|
2341
|
+
messageHashes.set(message, value);
|
|
2342
|
+
}
|
|
2343
|
+
return value;
|
|
2344
|
+
};
|
|
2345
|
+
return {
|
|
2346
|
+
request(data) {
|
|
2347
|
+
let contextStart = data.messages.length;
|
|
2348
|
+
while (contextStart > 0 && data.messages[contextStart - 1].framework_context === "runtime") contextStart--;
|
|
2349
|
+
const history = data.messages.slice(0, contextStart);
|
|
2350
|
+
const context = data.messages.slice(contextStart);
|
|
2351
|
+
const promptHash = hash(data.system_prompt);
|
|
2352
|
+
const firstPrompt = !prompts.has(promptHash);
|
|
2353
|
+
prompts.add(promptHash);
|
|
2354
|
+
const toolsHash = hash(json(data.tools.map((tool) => [tool.name, tool.description, tool.input_schema])));
|
|
2355
|
+
const firstTools = !toolSets.has(toolsHash);
|
|
2356
|
+
toolSets.add(toolsHash);
|
|
2357
|
+
const hashes = history.map(messageHash);
|
|
2358
|
+
const previous = threads.get(promptHash);
|
|
2359
|
+
let kept = 0;
|
|
2360
|
+
if (previous) {
|
|
2361
|
+
const limit = Math.min(previous.hashes.length, hashes.length);
|
|
2362
|
+
while (kept < limit && previous.hashes[kept] === hashes[kept]) kept++;
|
|
2363
|
+
}
|
|
2364
|
+
threads.set(promptHash, { requestId: data.request_id, hashes });
|
|
2365
|
+
const added = [];
|
|
2366
|
+
let budget = MESSAGES_BUDGET;
|
|
2367
|
+
for (let index = history.length - 1; index >= kept; index--) {
|
|
2368
|
+
const traced = traceMessage(history[index]);
|
|
2369
|
+
budget -= json(traced).length;
|
|
2370
|
+
if (budget < 0 && added.length) break;
|
|
2371
|
+
added.unshift(traced);
|
|
2372
|
+
}
|
|
2373
|
+
const skipped = history.length - kept - added.length;
|
|
2374
|
+
return {
|
|
2375
|
+
request_id: data.request_id,
|
|
2376
|
+
model: data.model,
|
|
2377
|
+
purpose: data.purpose,
|
|
2378
|
+
system_prompt: {
|
|
2379
|
+
hash: promptHash,
|
|
2380
|
+
chars: data.system_prompt.length,
|
|
2381
|
+
...firstPrompt ? { text: cut(data.system_prompt, SYSTEM_PROMPT_LIMIT).text } : {}
|
|
2382
|
+
},
|
|
2383
|
+
tools: {
|
|
2384
|
+
hash: toolsHash,
|
|
2385
|
+
names: data.tools.map((tool) => tool.name),
|
|
2386
|
+
...firstTools ? { list: data.tools.map(traceTool) } : {}
|
|
2387
|
+
},
|
|
2388
|
+
messages: {
|
|
2389
|
+
total: history.length,
|
|
2390
|
+
...previous && kept > 0 ? { base: previous.requestId } : {},
|
|
2391
|
+
kept: previous && kept > 0 ? kept : 0,
|
|
2392
|
+
...skipped > 0 ? { skipped } : {},
|
|
2393
|
+
added
|
|
2394
|
+
},
|
|
2395
|
+
context: context.map(traceMessage)
|
|
2396
|
+
};
|
|
2397
|
+
},
|
|
2398
|
+
end(data) {
|
|
2399
|
+
return {
|
|
2400
|
+
request_id: data.request_id,
|
|
2401
|
+
model: data.model,
|
|
2402
|
+
duration_ms: data.duration_ms,
|
|
2403
|
+
status: data.status,
|
|
2404
|
+
...data.error ? { error: cut(data.error, TEXT_LIMIT).text } : {},
|
|
2405
|
+
...data.messages ? { response: data.messages.map(traceMessage) } : {},
|
|
2406
|
+
...data.tokens_in !== void 0 ? { tokens_in: data.tokens_in } : {},
|
|
2407
|
+
...data.tokens_out !== void 0 ? { tokens_out: data.tokens_out } : {},
|
|
2408
|
+
...data.cache_read_input_tokens !== void 0 ? { cache_read_input_tokens: data.cache_read_input_tokens } : {},
|
|
2409
|
+
...data.cache_creation_input_tokens !== void 0 ? { cache_creation_input_tokens: data.cache_creation_input_tokens } : {}
|
|
2410
|
+
};
|
|
2411
|
+
}
|
|
2412
|
+
};
|
|
2413
|
+
}
|
|
2414
|
+
|
|
2097
2415
|
export {
|
|
2098
2416
|
FOUNDRY_APPLICATION_BRAND,
|
|
2099
2417
|
isFoundryApplication,
|
|
2100
2418
|
defineApplication,
|
|
2101
2419
|
EMPTY_FOUNDRY_APPLICATION,
|
|
2420
|
+
FOUNDRY_MODEL_REQUESTS_ENV,
|
|
2421
|
+
requestTracer,
|
|
2102
2422
|
composeAgent,
|
|
2103
2423
|
EMPTY_AGENT_COMPOSITION,
|
|
2104
2424
|
FOUNDRY_CONNECTION_BRAND,
|
|
@@ -2147,9 +2467,17 @@ export {
|
|
|
2147
2467
|
createInstalledApplicationTransmissionTools,
|
|
2148
2468
|
createFoundryCoreTools,
|
|
2149
2469
|
FOUNDRY_APPROVAL_DIRECTORY_ENV,
|
|
2470
|
+
FOUNDRY_APPROVAL_POLICY_ENV,
|
|
2471
|
+
DEFAULT_APPROVAL_WAIT_MS,
|
|
2472
|
+
DEFAULT_APPROVAL_EXPIRES_AFTER_MS,
|
|
2473
|
+
readApprovalPolicy,
|
|
2150
2474
|
createFoundryApproval,
|
|
2151
2475
|
listFoundryApprovals,
|
|
2152
2476
|
resolveFoundryApproval,
|
|
2477
|
+
foundryApprovalRequested,
|
|
2478
|
+
readFoundryApprovalResolution,
|
|
2479
|
+
markFoundryApprovalSuspended,
|
|
2480
|
+
foundryApprovalSuspended,
|
|
2153
2481
|
FoundryApprovalDisplayManager,
|
|
2154
2482
|
withFoundryPermissions
|
|
2155
2483
|
};
|