@tangle-network/agent-app 0.43.23 → 0.43.25
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/app-auth/index.d.ts +163 -0
- package/dist/app-auth/index.js +166 -0
- package/dist/app-auth/index.js.map +1 -0
- package/dist/assets/index.d.ts +2 -2
- package/dist/assistant/index.d.ts +1 -0
- package/dist/assistant/index.js +2 -1
- package/dist/assistant/index.js.map +1 -1
- package/dist/chat-store/index.d.ts +193 -0
- package/dist/chat-store/index.js +194 -0
- package/dist/chat-store/index.js.map +1 -0
- package/dist/chunk-4H77LX3V.js +38 -0
- package/dist/chunk-4H77LX3V.js.map +1 -0
- package/dist/chunk-4TXDD6P2.js +163 -0
- package/dist/chunk-4TXDD6P2.js.map +1 -0
- package/dist/chunk-AVBANQ67.js +295 -0
- package/dist/chunk-AVBANQ67.js.map +1 -0
- package/dist/{chunk-UKYZ4O2O.js → chunk-JJGZ54EB.js} +64 -5
- package/dist/chunk-JJGZ54EB.js.map +1 -0
- package/dist/{chunk-VMY4TKMN.js → chunk-PEPXQTJ3.js} +921 -432
- package/dist/chunk-PEPXQTJ3.js.map +1 -0
- package/dist/chunk-U7DLCPJ6.js +203 -0
- package/dist/chunk-U7DLCPJ6.js.map +1 -0
- package/dist/{chunk-SAOAAA3S.js → chunk-UHXQ3KNX.js} +1 -22
- package/dist/chunk-UHXQ3KNX.js.map +1 -0
- package/dist/contract-DYbTzEDf.d.ts +122 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +172 -88
- package/dist/interactions/index.d.ts +141 -0
- package/dist/interactions/index.js +59 -0
- package/dist/interactions/index.js.map +1 -0
- package/dist/parts-BeRnK54I.d.ts +185 -0
- package/dist/platform/index.d.ts +2 -270
- package/dist/platform/index.js +12 -278
- package/dist/platform/index.js.map +1 -1
- package/dist/preset-cloudflare/index.d.ts +0 -10
- package/dist/preset-cloudflare/index.js +1 -1
- package/dist/profile/index.d.ts +33 -2
- package/dist/profile/index.js +37 -1
- package/dist/profile/index.js.map +1 -1
- package/dist/sandbox/index.d.ts +61 -5
- package/dist/sandbox/index.js +13 -1
- package/dist/sso-Df4wtL8D.d.ts +270 -0
- package/dist/teams/index.js +9 -9
- package/dist/teams/invitations-api.js +3 -3
- package/dist/web-react/index.d.ts +206 -96
- package/dist/web-react/index.js +68 -22
- package/package.json +21 -2
- package/dist/chunk-SAOAAA3S.js.map +0 -1
- package/dist/chunk-UKYZ4O2O.js.map +0 -1
- package/dist/chunk-VMY4TKMN.js.map +0 -1
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import {
|
|
2
|
+
interactionFromWireRequest,
|
|
3
|
+
isSafeInteractionFieldKey,
|
|
4
|
+
questionInteractionContentSignature
|
|
5
|
+
} from "./chunk-4TXDD6P2.js";
|
|
6
|
+
|
|
7
|
+
// src/interactions/sidecar.ts
|
|
8
|
+
var DEFAULT_TIMEOUT_MS = 5e3;
|
|
9
|
+
function sanitizeUpstreamMessage(input) {
|
|
10
|
+
const message = input instanceof Error ? input.message : String(input);
|
|
11
|
+
return message.replace(/Bearer\s+[^\s]+/gi, "Bearer [redacted]").replace(/\b(?:sk|pk|tc)[_-][A-Za-z0-9_-]{8,}\b/g, "[redacted-key]");
|
|
12
|
+
}
|
|
13
|
+
async function interactionsFetch(connection, init) {
|
|
14
|
+
const doFetch = connection.fetchImpl ?? fetch;
|
|
15
|
+
const url = `${connection.runtimeUrl.replace(/\/$/, "")}/agents/sessions/${encodeURIComponent(connection.sessionId)}/interactions`;
|
|
16
|
+
let response;
|
|
17
|
+
try {
|
|
18
|
+
response = await doFetch(url, {
|
|
19
|
+
method: init.method,
|
|
20
|
+
headers: {
|
|
21
|
+
...connection.authToken ? { Authorization: `Bearer ${connection.authToken}` } : {},
|
|
22
|
+
...init.method === "POST" ? { "Content-Type": "application/json" } : {}
|
|
23
|
+
},
|
|
24
|
+
...init.method === "POST" ? { body: JSON.stringify(init.body) } : {},
|
|
25
|
+
signal: AbortSignal.timeout(connection.timeoutMs ?? DEFAULT_TIMEOUT_MS)
|
|
26
|
+
});
|
|
27
|
+
} catch (err) {
|
|
28
|
+
return {
|
|
29
|
+
succeeded: false,
|
|
30
|
+
error: { code: "UPSTREAM_UNREACHABLE", message: sanitizeUpstreamMessage(err), status: 0 }
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const raw = await response.text().catch(() => "");
|
|
34
|
+
let parsed = {};
|
|
35
|
+
try {
|
|
36
|
+
parsed = raw ? JSON.parse(raw) : {};
|
|
37
|
+
} catch {
|
|
38
|
+
}
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
const upstreamError = parsed.error ?? {};
|
|
41
|
+
return {
|
|
42
|
+
succeeded: false,
|
|
43
|
+
error: {
|
|
44
|
+
code: typeof upstreamError.code === "string" && upstreamError.code ? upstreamError.code : "UPSTREAM_ERROR",
|
|
45
|
+
message: sanitizeUpstreamMessage(
|
|
46
|
+
typeof upstreamError.message === "string" && upstreamError.message ? upstreamError.message : `sidecar interactions ${init.method} failed (${response.status})`
|
|
47
|
+
),
|
|
48
|
+
status: response.status
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return { succeeded: true, value: parsed };
|
|
53
|
+
}
|
|
54
|
+
async function listSessionInteractions(connection) {
|
|
55
|
+
const result = await interactionsFetch(connection, { method: "GET" });
|
|
56
|
+
if (!result.succeeded) return result;
|
|
57
|
+
const data = result.value.data;
|
|
58
|
+
if (!Array.isArray(data?.interactions)) {
|
|
59
|
+
return {
|
|
60
|
+
succeeded: false,
|
|
61
|
+
error: { code: "MALFORMED_RESPONSE", message: "sidecar list returned no interactions array", status: 200 }
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return { succeeded: true, value: data.interactions };
|
|
65
|
+
}
|
|
66
|
+
async function respondToSessionInteraction(connection, response) {
|
|
67
|
+
const result = await interactionsFetch(connection, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
body: {
|
|
70
|
+
id: response.id,
|
|
71
|
+
outcome: response.outcome,
|
|
72
|
+
...response.data ? { data: response.data } : {}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
if (!result.succeeded) return result;
|
|
76
|
+
return { succeeded: true, value: void 0 };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/interactions/route.ts
|
|
80
|
+
function validateInteractionAnswerBody(body) {
|
|
81
|
+
const id = typeof body.id === "string" && body.id ? body.id : null;
|
|
82
|
+
if (!id) return { ok: false, error: "Missing interaction id" };
|
|
83
|
+
const outcome = body.outcome;
|
|
84
|
+
if (outcome !== "accepted" && outcome !== "declined") {
|
|
85
|
+
return { ok: false, error: "Invalid outcome: expected accepted or declined" };
|
|
86
|
+
}
|
|
87
|
+
if (body.data === void 0) return { ok: true, id, outcome };
|
|
88
|
+
if (!body.data || typeof body.data !== "object" || Array.isArray(body.data)) {
|
|
89
|
+
return { ok: false, error: "Invalid data: expected an object of field values" };
|
|
90
|
+
}
|
|
91
|
+
const data = {};
|
|
92
|
+
for (const [key, value] of Object.entries(body.data)) {
|
|
93
|
+
if (!isSafeInteractionFieldKey(key)) {
|
|
94
|
+
return { ok: false, error: "Invalid data: field names must contain only letters, numbers, underscores, or hyphens" };
|
|
95
|
+
}
|
|
96
|
+
const validValue = typeof value === "string" || typeof value === "number" || typeof value === "boolean" || Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
97
|
+
if (!validValue) {
|
|
98
|
+
return { ok: false, error: "Invalid data: field values must be strings, numbers, booleans, or string arrays" };
|
|
99
|
+
}
|
|
100
|
+
data[key] = value;
|
|
101
|
+
}
|
|
102
|
+
return { ok: true, id, outcome, data };
|
|
103
|
+
}
|
|
104
|
+
function mapInteractionRespondFailure(error, logger = console) {
|
|
105
|
+
if (error.code === "INVALID_INTERACTION_ANSWER") {
|
|
106
|
+
return Response.json(
|
|
107
|
+
{ code: "INVALID_INTERACTION_ANSWER", error: "This question needs an answer from the card above \u2014 pick one of the listed options." },
|
|
108
|
+
{ status: 400 }
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
if (error.status === 404) {
|
|
112
|
+
return Response.json(
|
|
113
|
+
{ code: "INTERACTION_EXPIRED", error: "This question is no longer waiting for an answer." },
|
|
114
|
+
{ status: 410 }
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
if (error.status === 501 || error.code === "NOT_IMPLEMENTED") {
|
|
118
|
+
return Response.json(
|
|
119
|
+
{ code: "INTERACTIONS_UNSUPPORTED", error: "This agent backend cannot accept answers this way." },
|
|
120
|
+
{ status: 501 }
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
logger.error("[interactions] respond failed:", error);
|
|
124
|
+
return Response.json(
|
|
125
|
+
{ code: "INTERACTION_UPSTREAM_FAILED", error: "Could not reach the agent. Try again." },
|
|
126
|
+
{ status: 503 }
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
function createInteractionAnswerRoute(options) {
|
|
130
|
+
const logger = options.logger ?? console;
|
|
131
|
+
async function list(request) {
|
|
132
|
+
const resolution = await options.resolveConnection({ request, intent: "list" });
|
|
133
|
+
if (!resolution.ok) {
|
|
134
|
+
if ("response" in resolution) return resolution.response;
|
|
135
|
+
return Response.json({ interactions: [], unavailable: resolution.unavailable });
|
|
136
|
+
}
|
|
137
|
+
const result = await listSessionInteractions(resolution.connection);
|
|
138
|
+
if (!result.succeeded) {
|
|
139
|
+
logger.warn("[interactions] list failed:", result.error);
|
|
140
|
+
return Response.json({ interactions: [], unavailable: result.error.code });
|
|
141
|
+
}
|
|
142
|
+
return Response.json({ interactions: result.value });
|
|
143
|
+
}
|
|
144
|
+
async function answer(request) {
|
|
145
|
+
const body = await request.json().catch(() => null);
|
|
146
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
147
|
+
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
148
|
+
}
|
|
149
|
+
const validation = validateInteractionAnswerBody(body);
|
|
150
|
+
if (!validation.ok) return Response.json({ error: validation.error }, { status: 400 });
|
|
151
|
+
const resolution = await options.resolveConnection({ request, intent: "answer", body });
|
|
152
|
+
if (!resolution.ok) {
|
|
153
|
+
if ("response" in resolution) return resolution.response;
|
|
154
|
+
return mapInteractionRespondFailure(
|
|
155
|
+
{ code: resolution.unavailable, message: "sandbox runtime unavailable", status: 0 },
|
|
156
|
+
logger
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
const connection = resolution.connection;
|
|
160
|
+
const answerPayload = {
|
|
161
|
+
outcome: validation.outcome,
|
|
162
|
+
...validation.data ? { data: validation.data } : {}
|
|
163
|
+
};
|
|
164
|
+
const before = await listSessionInteractions(connection);
|
|
165
|
+
const answeredRequest = before.succeeded ? before.value.find((item) => item.id === validation.id) : void 0;
|
|
166
|
+
const answeredSignature = answeredRequest ? questionInteractionContentSignature(interactionFromWireRequest(answeredRequest)) : null;
|
|
167
|
+
const result = await respondToSessionInteraction(connection, { id: validation.id, ...answerPayload });
|
|
168
|
+
if (!result.succeeded) return mapInteractionRespondFailure(result.error, logger);
|
|
169
|
+
let remaining = await listSessionInteractions(connection);
|
|
170
|
+
if (remaining.succeeded && answeredSignature) {
|
|
171
|
+
const duplicateRequests = remaining.value.filter((item) => {
|
|
172
|
+
if (item.id === validation.id) return false;
|
|
173
|
+
return questionInteractionContentSignature(interactionFromWireRequest(item)) === answeredSignature;
|
|
174
|
+
});
|
|
175
|
+
for (const duplicate of duplicateRequests) {
|
|
176
|
+
const duplicateResult = await respondToSessionInteraction(connection, { id: duplicate.id, ...answerPayload });
|
|
177
|
+
if (!duplicateResult.succeeded) break;
|
|
178
|
+
}
|
|
179
|
+
if (duplicateRequests.length > 0) remaining = await listSessionInteractions(connection);
|
|
180
|
+
}
|
|
181
|
+
if (remaining.succeeded && remaining.value.some((item) => item.id === validation.id || answeredSignature && questionInteractionContentSignature(interactionFromWireRequest(item)) === answeredSignature)) {
|
|
182
|
+
logger.error("[interactions] respond returned ok but interaction is still pending:", {
|
|
183
|
+
sessionId: connection.sessionId,
|
|
184
|
+
interactionId: validation.id
|
|
185
|
+
});
|
|
186
|
+
return Response.json(
|
|
187
|
+
{ code: "INTERACTION_STILL_PENDING", error: "The agent did not accept the answer. Try answering again." },
|
|
188
|
+
{ status: 503 }
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
return Response.json({ ok: true });
|
|
192
|
+
}
|
|
193
|
+
return { list, answer };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export {
|
|
197
|
+
listSessionInteractions,
|
|
198
|
+
respondToSessionInteraction,
|
|
199
|
+
validateInteractionAnswerBody,
|
|
200
|
+
mapInteractionRespondFailure,
|
|
201
|
+
createInteractionAnswerRoute
|
|
202
|
+
};
|
|
203
|
+
//# sourceMappingURL=chunk-U7DLCPJ6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/interactions/sidecar.ts","../src/interactions/route.ts"],"sourcesContent":["/**\n * Server-side client for the sandbox sidecar's generic interaction routes\n * (`GET/POST {runtimeUrl}/agents/sessions/{sessionId}/interactions`). The\n * pinned sandbox SDK exposes only the question-specific `session().answer()`\n * convenience; these raw calls are backend-agnostic (question/permission/plan,\n * any harness) and carry explicit outcomes (accepted/declined).\n *\n * Server-only: the sidecar bearer must never reach browser code. The caller\n * supplies the connection as a structural value (runtime URL + bearer +\n * session id) — no sandbox-SDK import, so any box-resolution strategy works.\n */\n\nimport type { InteractionData, InteractionOutcome, InteractionRequestWire } from './contract'\n\nexport interface SidecarInteractionsError {\n code: string\n message: string\n /** Upstream HTTP status; 0 when the sidecar was unreachable. */\n status: number\n}\n\nexport type SidecarInteractionsResult<T> =\n | { succeeded: true; value: T }\n | { succeeded: false; error: SidecarInteractionsError }\n\n/** Where and how to reach one session's interaction registry. */\nexport interface SidecarInteractionsConnection {\n runtimeUrl: string\n authToken?: string\n /** The sidecar agent-session id (the chat thread's session). */\n sessionId: string\n /** Request deadline. A pending interaction means the box is up and the\n * sidecar responsive; a short default keeps a wedged runtime from stalling\n * the answering request. */\n timeoutMs?: number\n /** Injection seam for tests; defaults to global fetch. */\n fetchImpl?: typeof fetch\n}\n\nconst DEFAULT_TIMEOUT_MS = 5_000\n\n/** Strips bearer tokens / key material before an upstream message is logged\n * or surfaced. */\nfunction sanitizeUpstreamMessage(input: unknown): string {\n const message = input instanceof Error ? input.message : String(input)\n return message\n .replace(/Bearer\\s+[^\\s]+/gi, 'Bearer [redacted]')\n .replace(/\\b(?:sk|pk|tc)[_-][A-Za-z0-9_-]{8,}\\b/g, '[redacted-key]')\n}\n\nasync function interactionsFetch(\n connection: SidecarInteractionsConnection,\n init: { method: 'GET' } | { method: 'POST'; body: Record<string, unknown> },\n): Promise<SidecarInteractionsResult<Record<string, unknown>>> {\n const doFetch = connection.fetchImpl ?? fetch\n const url = `${connection.runtimeUrl.replace(/\\/$/, '')}/agents/sessions/${encodeURIComponent(connection.sessionId)}/interactions`\n let response: Response\n try {\n response = await doFetch(url, {\n method: init.method,\n headers: {\n ...(connection.authToken ? { Authorization: `Bearer ${connection.authToken}` } : {}),\n ...(init.method === 'POST' ? { 'Content-Type': 'application/json' } : {}),\n },\n ...(init.method === 'POST' ? { body: JSON.stringify(init.body) } : {}),\n signal: AbortSignal.timeout(connection.timeoutMs ?? DEFAULT_TIMEOUT_MS),\n })\n } catch (err) {\n return {\n succeeded: false,\n error: { code: 'UPSTREAM_UNREACHABLE', message: sanitizeUpstreamMessage(err), status: 0 },\n }\n }\n const raw = await response.text().catch(() => '')\n let parsed: Record<string, unknown> = {}\n try {\n parsed = raw ? (JSON.parse(raw) as Record<string, unknown>) : {}\n } catch {\n // Non-JSON error bodies (proxy 502 pages) fall through to the status check.\n }\n if (!response.ok) {\n const upstreamError = (parsed.error ?? {}) as { code?: unknown; message?: unknown }\n return {\n succeeded: false,\n error: {\n code: typeof upstreamError.code === 'string' && upstreamError.code ? upstreamError.code : 'UPSTREAM_ERROR',\n message: sanitizeUpstreamMessage(\n typeof upstreamError.message === 'string' && upstreamError.message\n ? upstreamError.message\n : `sidecar interactions ${init.method} failed (${response.status})`,\n ),\n status: response.status,\n },\n }\n }\n return { succeeded: true, value: parsed }\n}\n\n/** Outstanding (unanswered) interactions for the session — the sidecar's\n * registry is authoritative, so this is the reconnect/reload source of truth. */\nexport async function listSessionInteractions(\n connection: SidecarInteractionsConnection,\n): Promise<SidecarInteractionsResult<InteractionRequestWire[]>> {\n const result = await interactionsFetch(connection, { method: 'GET' })\n if (!result.succeeded) return result\n const data = result.value.data as { interactions?: unknown } | undefined\n if (!Array.isArray(data?.interactions)) {\n return {\n succeeded: false,\n error: { code: 'MALFORMED_RESPONSE', message: 'sidecar list returned no interactions array', status: 200 },\n }\n }\n return { succeeded: true, value: data.interactions as InteractionRequestWire[] }\n}\n\n/** Resolves one interaction. `data` is required by the sidecar only for\n * `accepted` outcomes and is validated fail-closed against the answerSpec\n * (400 INVALID_INTERACTION_ANSWER on mismatch). */\nexport async function respondToSessionInteraction(\n connection: SidecarInteractionsConnection,\n response: { id: string; outcome: InteractionOutcome; data?: InteractionData },\n): Promise<SidecarInteractionsResult<void>> {\n const result = await interactionsFetch(connection, {\n method: 'POST',\n body: {\n id: response.id,\n outcome: response.outcome,\n ...(response.data ? { data: response.data } : {}),\n },\n })\n if (!result.succeeded) return result\n return { succeeded: true, value: undefined }\n}\n","/**\n * Framework-neutral interaction-answer endpoints, lifted out of the per-app\n * route files (gtm `api.chat.interactions`, legal `api.chat.interactions`,\n * tax `api.sessions.$id.interactions` — three byte-similar forks):\n *\n * list(request) — GET: outstanding asks for a live turn (reload restore)\n * answer(request) — POST `{ id, outcome, data? }`: resolve one ask\n *\n * The product supplies ONE seam, `resolveConnection`: authenticate the caller,\n * authorize the thread/session, and resolve the sidecar connection. Everything\n * behind the seam is mechanism the forks kept re-fixing:\n *\n * - body validation (safe field keys, typed values),\n * - sidecar error → client contract mapping (every \"the ask is gone\" shape\n * becomes 410 so the card flips to its expired state),\n * - duplicate resolution: after answering, every other outstanding ask with\n * the same content signature (a re-emitted duplicate) gets the same answer,\n * - unblock verification: re-list and fail loud (503 INTERACTION_STILL_PENDING)\n * when the sidecar accepted the POST but the ask is still open,\n * - best-effort list: sidecar failures return `{ interactions: [],\n * unavailable }` so a reload restore never breaks the live stream.\n *\n * Handlers return web-standard `Response`s (Workers, Node 18+, Deno) — no\n * router import anywhere.\n */\n\nimport {\n interactionFromWireRequest,\n isSafeInteractionFieldKey,\n questionInteractionContentSignature,\n type InteractionData,\n} from './contract'\nimport {\n listSessionInteractions,\n respondToSessionInteraction,\n type SidecarInteractionsConnection,\n type SidecarInteractionsError,\n} from './sidecar'\n\n// A client resolves an ask by answering (`accepted`) or refusing (`declined`).\n// Withdrawal (`cancelled`) is an agent/broker outcome delivered via the\n// `interaction.cancel` event, never a client POST, so it is not accepted here.\nexport type InteractionClientOutcome = 'accepted' | 'declined'\n\nexport type InteractionAnswerBodyValidation =\n | { ok: true; id: string; outcome: InteractionClientOutcome; data?: InteractionData }\n | { ok: false; error: string }\n\n/** Validates the client POST body: `{ id, outcome, data? }` with\n * identifier-safe field keys and primitive/string-array values only. */\nexport function validateInteractionAnswerBody(body: Record<string, unknown>): InteractionAnswerBodyValidation {\n const id = typeof body.id === 'string' && body.id ? body.id : null\n if (!id) return { ok: false, error: 'Missing interaction id' }\n const outcome = body.outcome\n if (outcome !== 'accepted' && outcome !== 'declined') {\n return { ok: false, error: 'Invalid outcome: expected accepted or declined' }\n }\n if (body.data === undefined) return { ok: true, id, outcome }\n if (!body.data || typeof body.data !== 'object' || Array.isArray(body.data)) {\n return { ok: false, error: 'Invalid data: expected an object of field values' }\n }\n const data: InteractionData = {}\n for (const [key, value] of Object.entries(body.data)) {\n if (!isSafeInteractionFieldKey(key)) {\n return { ok: false, error: 'Invalid data: field names must contain only letters, numbers, underscores, or hyphens' }\n }\n const validValue =\n typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ||\n (Array.isArray(value) && value.every((item) => typeof item === 'string'))\n if (!validValue) {\n return { ok: false, error: 'Invalid data: field values must be strings, numbers, booleans, or string arrays' }\n }\n data[key] = value as InteractionData[string]\n }\n return { ok: true, id, outcome, data }\n}\n\nexport type InteractionRouteLogger = Pick<Console, 'warn' | 'error'>\n\n/** Sidecar error → the client-actionable contract. Every \"the ask is gone\"\n * shape maps to 410 so the card flips to its expired state — a raw 404/409\n * must never surface. */\nexport function mapInteractionRespondFailure(\n error: SidecarInteractionsError,\n logger: InteractionRouteLogger = console,\n): Response {\n if (error.code === 'INVALID_INTERACTION_ANSWER') {\n return Response.json(\n { code: 'INVALID_INTERACTION_ANSWER', error: 'This question needs an answer from the card above — pick one of the listed options.' },\n { status: 400 },\n )\n }\n if (error.status === 404) {\n return Response.json(\n { code: 'INTERACTION_EXPIRED', error: 'This question is no longer waiting for an answer.' },\n { status: 410 },\n )\n }\n if (error.status === 501 || error.code === 'NOT_IMPLEMENTED') {\n return Response.json(\n { code: 'INTERACTIONS_UNSUPPORTED', error: 'This agent backend cannot accept answers this way.' },\n { status: 501 },\n )\n }\n logger.error('[interactions] respond failed:', error)\n return Response.json(\n { code: 'INTERACTION_UPSTREAM_FAILED', error: 'Could not reach the agent. Try again.' },\n { status: 503 },\n )\n}\n\n/** The product seam's verdict for one request. `response` short-circuits with\n * a product-authored Response (401/404/429…); `unavailable` means the caller\n * is fine but the sandbox runtime is not reachable — the factory shapes that\n * per intent (empty list for `list`, 503 for `answer`). */\nexport type InteractionConnectionResolution =\n | { ok: true; connection: SidecarInteractionsConnection }\n | { ok: false; response: Response }\n | { ok: false; unavailable: string }\n\nexport interface ResolveInteractionConnectionArgs {\n request: Request\n intent: 'list' | 'answer'\n /** The parsed, validated POST body (answer intent only) so the resolver can\n * read product routing fields (workspaceId/threadId) without re-parsing. */\n body?: Record<string, unknown>\n}\n\nexport interface InteractionAnswerRouteOptions {\n /** Authenticate + authorize the caller and resolve the sidecar connection.\n * This is the only product-supplied step: session auth, workspace/thread\n * access, rate limiting, and box resolution all live here. */\n resolveConnection: (args: ResolveInteractionConnectionArgs) => Promise<InteractionConnectionResolution>\n logger?: InteractionRouteLogger\n}\n\nexport interface InteractionAnswerRoute {\n /** GET — outstanding interactions for a live turn. Failures return an empty\n * list with an explicit `unavailable` code: the caller is a best-effort\n * reload restore, and the live/replayed stream must stay untouched when the\n * sidecar cannot answer. */\n list: (request: Request) => Promise<Response>\n /** POST `{ id, outcome, data?, ...productFields }` — resolve one ask, answer\n * content-identical duplicates the same way, then re-list to prove the run\n * actually unblocked. */\n answer: (request: Request) => Promise<Response>\n}\n\nexport function createInteractionAnswerRoute(options: InteractionAnswerRouteOptions): InteractionAnswerRoute {\n const logger = options.logger ?? console\n\n async function list(request: Request): Promise<Response> {\n const resolution = await options.resolveConnection({ request, intent: 'list' })\n if (!resolution.ok) {\n if ('response' in resolution) return resolution.response\n return Response.json({ interactions: [], unavailable: resolution.unavailable })\n }\n const result = await listSessionInteractions(resolution.connection)\n if (!result.succeeded) {\n logger.warn('[interactions] list failed:', result.error)\n return Response.json({ interactions: [], unavailable: result.error.code })\n }\n return Response.json({ interactions: result.value })\n }\n\n async function answer(request: Request): Promise<Response> {\n const body = await request.json().catch(() => null) as Record<string, unknown> | null\n if (!body || typeof body !== 'object' || Array.isArray(body)) {\n return Response.json({ error: 'Invalid JSON body' }, { status: 400 })\n }\n const validation = validateInteractionAnswerBody(body)\n if (!validation.ok) return Response.json({ error: validation.error }, { status: 400 })\n\n const resolution = await options.resolveConnection({ request, intent: 'answer', body })\n if (!resolution.ok) {\n if ('response' in resolution) return resolution.response\n return mapInteractionRespondFailure(\n { code: resolution.unavailable, message: 'sandbox runtime unavailable', status: 0 },\n logger,\n )\n }\n const connection = resolution.connection\n const answerPayload = {\n outcome: validation.outcome,\n ...(validation.data ? { data: validation.data } : {}),\n }\n\n // Snapshot the answered ask's content signature BEFORE resolving it, so any\n // content-identical duplicates still outstanding afterwards (the agent may\n // have re-emitted the same question N times) can be answered the same way.\n const before = await listSessionInteractions(connection)\n const answeredRequest = before.succeeded ? before.value.find((item) => item.id === validation.id) : undefined\n const answeredSignature = answeredRequest\n ? questionInteractionContentSignature(interactionFromWireRequest(answeredRequest))\n : null\n\n const result = await respondToSessionInteraction(connection, { id: validation.id, ...answerPayload })\n if (!result.succeeded) return mapInteractionRespondFailure(result.error, logger)\n\n let remaining = await listSessionInteractions(connection)\n if (remaining.succeeded && answeredSignature) {\n const duplicateRequests = remaining.value.filter((item) => {\n if (item.id === validation.id) return false\n return questionInteractionContentSignature(interactionFromWireRequest(item)) === answeredSignature\n })\n for (const duplicate of duplicateRequests) {\n const duplicateResult = await respondToSessionInteraction(connection, { id: duplicate.id, ...answerPayload })\n if (!duplicateResult.succeeded) break\n }\n if (duplicateRequests.length > 0) remaining = await listSessionInteractions(connection)\n }\n\n // Prove the run actually unblocked: neither the answered id nor any\n // content-duplicate may still be outstanding. If one is, the sidecar\n // accepted the POST but did not release the run — report it rather than\n // tell the user it was answered.\n if (remaining.succeeded && remaining.value.some((item) => item.id === validation.id || (\n answeredSignature && questionInteractionContentSignature(interactionFromWireRequest(item)) === answeredSignature\n ))) {\n logger.error('[interactions] respond returned ok but interaction is still pending:', {\n sessionId: connection.sessionId,\n interactionId: validation.id,\n })\n return Response.json(\n { code: 'INTERACTION_STILL_PENDING', error: 'The agent did not accept the answer. Try answering again.' },\n { status: 503 },\n )\n }\n\n return Response.json({ ok: true })\n }\n\n return { list, answer }\n}\n"],"mappings":";;;;;;;AAuCA,IAAM,qBAAqB;AAI3B,SAAS,wBAAwB,OAAwB;AACvD,QAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACrE,SAAO,QACJ,QAAQ,qBAAqB,mBAAmB,EAChD,QAAQ,0CAA0C,gBAAgB;AACvE;AAEA,eAAe,kBACb,YACA,MAC6D;AAC7D,QAAM,UAAU,WAAW,aAAa;AACxC,QAAM,MAAM,GAAG,WAAW,WAAW,QAAQ,OAAO,EAAE,CAAC,oBAAoB,mBAAmB,WAAW,SAAS,CAAC;AACnH,M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dAt: "created_at"
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
threads: {
|
|
25
|
-
name: "threads",
|
|
26
|
-
columns: {
|
|
27
|
-
id: "id",
|
|
28
|
-
workspaceId: "workspace_id",
|
|
29
|
-
title: "title",
|
|
30
|
-
createdAt: "created_at"
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
24
|
knowledge: {
|
|
34
25
|
name: "knowledge",
|
|
35
26
|
columns: {
|
|
@@ -70,12 +61,6 @@ var PRESET_TABLES = {
|
|
|
70
61
|
}
|
|
71
62
|
};
|
|
72
63
|
var PRESET_MIGRATION_SQL = [
|
|
73
|
-
`CREATE TABLE IF NOT EXISTS threads (
|
|
74
|
-
id TEXT PRIMARY KEY,
|
|
75
|
-
workspace_id TEXT NOT NULL,
|
|
76
|
-
title TEXT,
|
|
77
|
-
created_at INTEGER NOT NULL
|
|
78
|
-
)`,
|
|
79
64
|
`CREATE TABLE IF NOT EXISTS proposals (
|
|
80
65
|
id TEXT PRIMARY KEY,
|
|
81
66
|
workspace_id TEXT NOT NULL,
|
|
@@ -125,12 +110,6 @@ function createPresetDrizzleSchema(d) {
|
|
|
125
110
|
const { sqliteTable, text, integer, real } = d;
|
|
126
111
|
const C = PRESET_TABLES;
|
|
127
112
|
return {
|
|
128
|
-
threads: sqliteTable(C.threads.name, {
|
|
129
|
-
id: text(C.threads.columns.id).primaryKey(),
|
|
130
|
-
workspaceId: text(C.threads.columns.workspaceId).notNull(),
|
|
131
|
-
title: text(C.threads.columns.title),
|
|
132
|
-
createdAt: integer(C.threads.columns.createdAt).notNull()
|
|
133
|
-
}),
|
|
134
113
|
proposals: sqliteTable(C.proposals.name, {
|
|
135
114
|
id: text(C.proposals.columns.id).primaryKey(),
|
|
136
115
|
workspaceId: text(C.proposals.columns.workspaceId).notNull(),
|
|
@@ -315,4 +294,4 @@ export {
|
|
|
315
294
|
createPresetWorkspaceKeyStore,
|
|
316
295
|
createPresetWorkspaceKeyManager
|
|
317
296
|
};
|
|
318
|
-
//# sourceMappingURL=chunk-
|
|
297
|
+
//# sourceMappingURL=chunk-UHXQ3KNX.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/preset-cloudflare/index.ts"],"sourcesContent":["/**\n * `@tangle-network/agent-app/preset-cloudflare` — the batteries-included default\n * stack.\n *\n * Every fleet agent runs the SAME backend: Cloudflare D1 (SQLite) through\n * Drizzle for state, a KV namespace as the artifact vault, AES-GCM field crypto\n * for PII, and per-workspace budget-capped model keys. The other agent-app\n * modules are pure SEAMS — `./tools` needs an `AppToolHandlers`, `./knowledge`\n * needs a `KnowledgeStateAccessor`, `./billing` needs a `WorkspaceKeyStore` +\n * `KeyCrypto`. This module is the ONE implementation of those seams against the\n * house stack, so a consumer that runs D1 + KV stands the whole shell up with\n * config + bindings and ZERO handler code.\n *\n * Layering:\n * - Drizzle is a PEER (the consumer installs `drizzle-orm`, never bundled). The\n * schema is therefore expressed two ways that need no import here: the plain\n * DDL ({@link PRESET_MIGRATION_SQL}) a consumer runs to create the tables, and\n * a {@link createPresetDrizzleSchema} factory that takes the consumer's\n * `drizzle-orm/sqlite-core` builder module and returns the typed tables. The\n * column names in {@link PRESET_TABLES} are the contract the handlers,\n * accessor, and DDL all agree on.\n * - D1 + KV are STRUCTURAL: {@link D1Like} (Cloudflare `D1Database` satisfies it)\n * and `KvLike` from `../web` (Cloudflare `KVNamespace` satisfies it). No\n * `@cloudflare/workers-types` dependency.\n * - Crypto/billing reuse `../crypto` + `../billing` exactly — this only wires\n * them to the D1 key table.\n */\n\nimport { createFieldCrypto } from '../crypto/index'\nimport {\n createWorkspaceKeyManager,\n type KeyCrypto,\n type KeyProvisioner,\n type WorkspaceKeyManager,\n type WorkspaceKeyRecord,\n type WorkspaceKeyStore,\n} from '../billing/index'\nimport type { KnowledgeStateAccessor } from '../knowledge/index'\nimport type {\n AddCitationArgs,\n AddCitationResult,\n AppToolContext,\n AppToolHandlers,\n RenderUiArgs,\n RenderUiResult,\n ScheduleFollowupArgs,\n ScheduleFollowupResult,\n SubmitProposalArgs,\n SubmitProposalResult,\n} from '../tools/index'\nimport type { KvLike } from '../web/index'\n\n// ---------------------------------------------------------------------------\n// D1 structural seam\n//\n// The minimal surface of Cloudflare's `D1Database` the handlers + accessor use.\n// `D1Database` satisfies it structurally, so the consumer passes `env.DB`\n// directly and tests pass an in-memory fake. We keep it to the\n// prepare/bind/first/run/all shape D1 already exposes — no Drizzle here, so the\n// default handlers run on a fresh Worker with only the D1 binding.\n// ---------------------------------------------------------------------------\n\n/** A prepared, bound D1 statement. */\nexport interface D1PreparedLike {\n bind(...values: unknown[]): D1PreparedLike\n first<T = Record<string, unknown>>(colName?: string): Promise<T | null>\n run(): Promise<unknown>\n all<T = Record<string, unknown>>(): Promise<{ results: T[] }>\n}\n\n/** The D1 surface the preset needs. Cloudflare `D1Database` satisfies it. */\nexport interface D1Like {\n prepare(query: string): D1PreparedLike\n}\n\n// ---------------------------------------------------------------------------\n// Default schema\n//\n// The four tables the default handlers + accessor read/write. Column names are\n// the single source of truth shared by the DDL, the Drizzle factory, the\n// handlers, and the accessor. Every table is workspace-scoped on `workspace_id`\n// (the accessor's default `where` column) so the knowledge `count` rule and the\n// tool writes agree without per-consumer wiring.\n//\n// Deliberately NOT here: chat thread/message tables. `/chat-store` is the\n// single thread-schema owner (`createChatTables` + `createChatStore`); the\n// preset once declared an unconsumed `threads` DDL, removed so two schemas\n// can't drift.\n// ---------------------------------------------------------------------------\n\n/** The preset table + column names — the contract the DDL, Drizzle schema,\n * handlers, and accessor share. Exposed so a consumer can reference a column\n * without a string literal. */\nexport const PRESET_TABLES = {\n proposals: {\n name: 'proposals',\n columns: {\n id: 'id',\n workspaceId: 'workspace_id',\n threadId: 'thread_id',\n type: 'type',\n title: 'title',\n description: 'description',\n status: 'status',\n createdBy: 'created_by',\n createdAt: 'created_at',\n },\n },\n knowledge: {\n name: 'knowledge',\n columns: {\n id: 'id',\n workspaceId: 'workspace_id',\n path: 'path',\n kind: 'kind',\n label: 'label',\n content: 'content',\n createdAt: 'created_at',\n },\n },\n deadlines: {\n name: 'deadlines',\n columns: {\n id: 'id',\n workspaceId: 'workspace_id',\n threadId: 'thread_id',\n title: 'title',\n dueDate: 'due_date',\n priority: 'priority',\n status: 'status',\n createdAt: 'created_at',\n },\n },\n workspaceKeys: {\n name: 'workspace_keys',\n columns: {\n id: 'id',\n workspaceId: 'workspace_id',\n keyId: 'key_id',\n keyEncrypted: 'key_encrypted',\n budgetUsd: 'budget_usd',\n expiresAt: 'expires_at',\n revokedAt: 'revoked_at',\n createdAt: 'created_at',\n },\n },\n} as const\n\n/**\n * Plain DDL for the preset schema — run by a consumer to create the tables with\n * ZERO drizzle (`for (const sql of PRESET_MIGRATION_SQL) await db.prepare(sql).run()`,\n * or paste into a `.sql` migration). One statement per table so D1's\n * single-statement `prepare` accepts each. Matches {@link PRESET_TABLES} exactly.\n */\nexport const PRESET_MIGRATION_SQL: readonly string[] = [\n `CREATE TABLE IF NOT EXISTS proposals (\n id TEXT PRIMARY KEY,\n workspace_id TEXT NOT NULL,\n thread_id TEXT,\n type TEXT NOT NULL,\n title TEXT NOT NULL,\n description TEXT,\n status TEXT NOT NULL DEFAULT 'pending',\n created_by TEXT,\n created_at INTEGER NOT NULL\n )`,\n `CREATE TABLE IF NOT EXISTS knowledge (\n id TEXT PRIMARY KEY,\n workspace_id TEXT NOT NULL,\n path TEXT NOT NULL,\n kind TEXT NOT NULL,\n label TEXT,\n content TEXT,\n created_at INTEGER NOT NULL\n )`,\n `CREATE TABLE IF NOT EXISTS deadlines (\n id TEXT PRIMARY KEY,\n workspace_id TEXT NOT NULL,\n thread_id TEXT,\n title TEXT NOT NULL,\n due_date TEXT NOT NULL,\n priority TEXT,\n status TEXT NOT NULL DEFAULT 'scheduled',\n created_at INTEGER NOT NULL\n )`,\n `CREATE TABLE IF NOT EXISTS workspace_keys (\n id TEXT PRIMARY KEY,\n workspace_id TEXT NOT NULL,\n key_id TEXT NOT NULL,\n key_encrypted TEXT NOT NULL,\n budget_usd REAL NOT NULL,\n expires_at INTEGER NOT NULL,\n revoked_at INTEGER,\n created_at INTEGER NOT NULL\n )`,\n `CREATE INDEX IF NOT EXISTS idx_proposals_ws ON proposals (workspace_id, status)`,\n `CREATE INDEX IF NOT EXISTS idx_deadlines_ws ON deadlines (workspace_id, status)`,\n `CREATE INDEX IF NOT EXISTS idx_knowledge_ws ON knowledge (workspace_id)`,\n `CREATE INDEX IF NOT EXISTS idx_workspace_keys_ws ON workspace_keys (workspace_id, revoked_at)`,\n]\n\n/** A chainable column builder — every modifier returns the builder so calls\n * like `.notNull().default('pending')` typecheck. The concrete drizzle builders\n * satisfy this structurally. */\nexport interface DrizzleColumnLike {\n primaryKey: () => DrizzleColumnLike\n notNull: () => DrizzleColumnLike\n default: (v: unknown) => DrizzleColumnLike\n}\n\n/** The shape of a `drizzle-orm/sqlite-core` module — the few builders the\n * preset schema uses. The consumer passes the real module; agent-app never\n * imports it (it stays a peer). */\nexport interface DrizzleSqliteCoreLike {\n sqliteTable: (name: string, columns: Record<string, DrizzleColumnLike>) => unknown\n text: (name?: string) => DrizzleColumnLike\n integer: (name?: string, config?: unknown) => DrizzleColumnLike\n real: (name?: string) => DrizzleColumnLike\n}\n\n/**\n * Build the typed Drizzle schema for the preset, given the consumer's\n * `drizzle-orm/sqlite-core` module. Returns one table object per\n * {@link PRESET_TABLES} entry — pass to `drizzle(db, { schema })` for typed\n * queries, or to drizzle-kit for migration generation. agent-app never imports\n * drizzle; the builder module is the seam.\n *\n * ```ts\n * import * as d from 'drizzle-orm/sqlite-core'\n * const schema = createPresetDrizzleSchema(d)\n * ```\n */\nexport function createPresetDrizzleSchema(d: DrizzleSqliteCoreLike) {\n const { sqliteTable, text, integer, real } = d\n const C = PRESET_TABLES\n return {\n proposals: sqliteTable(C.proposals.name, {\n id: text(C.proposals.columns.id).primaryKey(),\n workspaceId: text(C.proposals.columns.workspaceId).notNull(),\n threadId: text(C.proposals.columns.threadId),\n type: text(C.proposals.columns.type).notNull(),\n title: text(C.proposals.columns.title).notNull(),\n description: text(C.proposals.columns.description),\n status: text(C.proposals.columns.status).notNull().default('pending'),\n createdBy: text(C.proposals.columns.createdBy),\n createdAt: integer(C.proposals.columns.createdAt).notNull(),\n }),\n knowledge: sqliteTable(C.knowledge.name, {\n id: text(C.knowledge.columns.id).primaryKey(),\n workspaceId: text(C.knowledge.columns.workspaceId).notNull(),\n path: text(C.knowledge.columns.path).notNull(),\n kind: text(C.knowledge.columns.kind).notNull(),\n label: text(C.knowledge.columns.label),\n content: text(C.knowledge.columns.content),\n createdAt: integer(C.knowledge.columns.createdAt).notNull(),\n }),\n deadlines: sqliteTable(C.deadlines.name, {\n id: text(C.deadlines.columns.id).primaryKey(),\n workspaceId: text(C.deadlines.columns.workspaceId).notNull(),\n threadId: text(C.deadlines.columns.threadId),\n title: text(C.deadlines.columns.title).notNull(),\n dueDate: text(C.deadlines.columns.dueDate).notNull(),\n priority: text(C.deadlines.columns.priority),\n status: text(C.deadlines.columns.status).notNull().default('scheduled'),\n createdAt: integer(C.deadlines.columns.createdAt).notNull(),\n }),\n workspaceKeys: sqliteTable(C.workspaceKeys.name, {\n id: text(C.workspaceKeys.columns.id).primaryKey(),\n workspaceId: text(C.workspaceKeys.columns.workspaceId).notNull(),\n keyId: text(C.workspaceKeys.columns.keyId).notNull(),\n keyEncrypted: text(C.workspaceKeys.columns.keyEncrypted).notNull(),\n budgetUsd: real(C.workspaceKeys.columns.budgetUsd).notNull(),\n expiresAt: integer(C.workspaceKeys.columns.expiresAt).notNull(),\n revokedAt: integer(C.workspaceKeys.columns.revokedAt),\n createdAt: integer(C.workspaceKeys.columns.createdAt).notNull(),\n }),\n }\n}\n\n// ---------------------------------------------------------------------------\n// Default AppToolHandlers over D1 + KV\n// ---------------------------------------------------------------------------\n\n/** The KV-backed vault. `KvLike` (from `../web`) is the structural KV contract;\n * Cloudflare `KVNamespace` satisfies it. Artifacts are stored under their path. */\nexport type VaultKv = KvLike\n\nexport interface PresetToolHandlerOptions {\n /** The D1 database (Cloudflare `D1Database` satisfies {@link D1Like}). */\n db: D1Like\n /** The KV namespace used as the artifact vault. */\n vault: VaultKv\n /** Id generator. Default `crypto.randomUUID`. Injectable for deterministic tests. */\n newId?: () => string\n /** Clock (epoch ms). Default `Date.now`. Injectable for deterministic tests. */\n now?: () => number\n /** Vault path prefix for `render_ui` artifacts. Default `'ui'`. */\n uiPathPrefix?: string\n /** Vault path prefix for `add_citation` artifacts. Default `'citations'`. */\n citationPathPrefix?: string\n}\n\nfunction slug(value: string): string {\n return value\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, '-')\n .replace(/^-+|-+$/g, '')\n .slice(0, 64) || 'item'\n}\n\n/**\n * The default {@link AppToolHandlers} for the house stack:\n * - `submit_proposal` → insert a `proposals` row (`status='pending'`), deduped\n * on (workspace, title) so a retried turn doesn't double-queue.\n * - `schedule_followup` → insert a `deadlines` row, deduped on (workspace, title, due_date).\n * - `render_ui` → write the schema JSON as a `ui/<thread>/<slug>.json`\n * vault artifact AND a `knowledge` row pointing at it.\n * - `add_citation` → write the quote as a `citations/<slug>.json` artifact AND\n * a `knowledge` row.\n *\n * Returns the EXACT persisted content from `render_ui` (per the seam contract) so\n * a completion oracle sees real bytes. Pure seam wiring: a consumer that runs\n * D1 + KV gets all four tools with no handler code.\n */\nexport function createPresetToolHandlers(opts: PresetToolHandlerOptions): AppToolHandlers {\n const { db, vault } = opts\n const newId = opts.newId ?? (() => crypto.randomUUID())\n const now = opts.now ?? (() => Date.now())\n const uiPrefix = opts.uiPathPrefix ?? 'ui'\n const citationPrefix = opts.citationPathPrefix ?? 'citations'\n const P = PRESET_TABLES.proposals\n const D = PRESET_TABLES.deadlines\n const K = PRESET_TABLES.knowledge\n\n async function persistArtifact(path: string, body: string): Promise<void> {\n await vault.put(path, body)\n }\n\n async function insertKnowledge(workspaceId: string, path: string, kind: string, label: string | null, content: string): Promise<void> {\n await db\n .prepare(\n `INSERT INTO ${K.name} (${K.columns.id}, ${K.columns.workspaceId}, ${K.columns.path}, ${K.columns.kind}, ${K.columns.label}, ${K.columns.content}, ${K.columns.createdAt}) VALUES (?, ?, ?, ?, ?, ?, ?)`,\n )\n .bind(newId(), workspaceId, path, kind, label, content, now())\n .run()\n }\n\n return {\n async submitProposal(args: SubmitProposalArgs, ctx: AppToolContext): Promise<SubmitProposalResult> {\n const existing = await db\n .prepare(`SELECT ${P.columns.id} AS id FROM ${P.name} WHERE ${P.columns.workspaceId} = ? AND ${P.columns.title} = ? LIMIT 1`)\n .bind(ctx.workspaceId, args.title)\n .first<{ id: string }>()\n if (existing) return { proposalId: existing.id, deduped: true }\n\n const id = newId()\n await db\n .prepare(\n `INSERT INTO ${P.name} (${P.columns.id}, ${P.columns.workspaceId}, ${P.columns.threadId}, ${P.columns.type}, ${P.columns.title}, ${P.columns.description}, ${P.columns.status}, ${P.columns.createdBy}, ${P.columns.createdAt}) VALUES (?, ?, ?, ?, ?, ?, 'pending', ?, ?)`,\n )\n .bind(id, ctx.workspaceId, ctx.threadId, args.type, args.title, args.description ?? null, ctx.userId, now())\n .run()\n return { proposalId: id, deduped: false }\n },\n\n async scheduleFollowup(args: ScheduleFollowupArgs, ctx: AppToolContext): Promise<ScheduleFollowupResult> {\n const existing = await db\n .prepare(\n `SELECT ${D.columns.id} AS id, ${D.columns.dueDate} AS dueDate FROM ${D.name} WHERE ${D.columns.workspaceId} = ? AND ${D.columns.title} = ? AND ${D.columns.dueDate} = ? LIMIT 1`,\n )\n .bind(ctx.workspaceId, args.title, args.dueDate)\n .first<{ id: string; dueDate: string }>()\n if (existing) return { id: existing.id, dueDate: existing.dueDate, deduped: true }\n\n const id = newId()\n await db\n .prepare(\n `INSERT INTO ${D.name} (${D.columns.id}, ${D.columns.workspaceId}, ${D.columns.threadId}, ${D.columns.title}, ${D.columns.dueDate}, ${D.columns.priority}, ${D.columns.status}, ${D.columns.createdAt}) VALUES (?, ?, ?, ?, ?, ?, 'scheduled', ?)`,\n )\n .bind(id, ctx.workspaceId, ctx.threadId, args.title, args.dueDate, args.priority ?? null, now())\n .run()\n return { id, dueDate: args.dueDate, deduped: false }\n },\n\n async renderUi(args: RenderUiArgs, ctx: AppToolContext): Promise<RenderUiResult> {\n const content = JSON.stringify(args.schema)\n const path = `${uiPrefix}/${ctx.threadId ?? 'global'}/${slug(args.title)}.json`\n await persistArtifact(path, content)\n await insertKnowledge(ctx.workspaceId, path, 'ui', args.title, content)\n return { path, content }\n },\n\n async addCitation(args: AddCitationArgs, ctx: AppToolContext): Promise<AddCitationResult> {\n const citationId = newId()\n const path = `${citationPrefix}/${slug(args.label ?? args.path)}-${citationId.slice(0, 8)}.json`\n const body = JSON.stringify({ sourcePath: args.path, quote: args.quote, label: args.label ?? null })\n await persistArtifact(path, body)\n await insertKnowledge(ctx.workspaceId, path, 'citation', args.label ?? null, body)\n return { citationId, path }\n },\n }\n}\n\n// ---------------------------------------------------------------------------\n// D1-backed KnowledgeStateAccessor\n// ---------------------------------------------------------------------------\n\nexport interface PresetKnowledgeAccessorOptions {\n db: D1Like\n /** The active workspace — every `count` is scoped to it. */\n workspaceId: string\n /** Workspace config the `satisfiedBy: { config }` rules read. A resolved\n * object (dot-path lookup), or a function the accessor calls per path. */\n config: Record<string, unknown> | ((path: string) => unknown)\n /** The default workspace fk column a `count` rule scopes on when its rule\n * omits `where`. Default `'workspace_id'` (the preset schema convention). */\n defaultWhereColumn?: string\n}\n\nfunction readDotPath(obj: Record<string, unknown>, path: string): unknown {\n let cur: unknown = obj\n for (const part of path.split('.')) {\n if (cur == null || typeof cur !== 'object') return undefined\n cur = (cur as Record<string, unknown>)[part]\n }\n return cur\n}\n\n/**\n * The {@link KnowledgeStateAccessor} over the preset D1 schema — the seam that\n * lets the declarative `satisfiedBy` rules resolve with ZERO consumer code:\n * - `config(path)` reads the supplied workspace config by dot-path.\n * - `count({ table, where, statusIn })` runs `SELECT count(*)` scoped to the\n * active workspace (the rule's `where` column, default `workspace_id`),\n * optionally filtered to `statusIn` via a parameterized `IN (...)`.\n *\n * Identifiers (table/column) are validated against a safe pattern before\n * interpolation — they originate from the product's own config, never model\n * input, but we fail loud rather than build a malformed/injectable query.\n */\nexport function createD1KnowledgeStateAccessor(opts: PresetKnowledgeAccessorOptions): KnowledgeStateAccessor {\n const { db, workspaceId } = opts\n const defaultWhere = opts.defaultWhereColumn ?? 'workspace_id'\n const configFn = typeof opts.config === 'function' ? opts.config : (path: string) => readDotPath(opts.config as Record<string, unknown>, path)\n\n const isIdentifier = (s: string) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(s)\n\n return {\n config: configFn,\n async count(query) {\n if (!isIdentifier(query.table)) throw new Error(`unsafe table identifier: ${query.table}`)\n const whereCol = query.where ?? defaultWhere\n if (!isIdentifier(whereCol)) throw new Error(`unsafe where identifier: ${whereCol}`)\n\n let sql = `SELECT count(*) AS n FROM ${query.table} WHERE ${whereCol} = ?`\n const binds: unknown[] = [workspaceId]\n if (query.statusIn && query.statusIn.length > 0) {\n sql += ` AND status IN (${query.statusIn.map(() => '?').join(', ')})`\n binds.push(...query.statusIn)\n }\n const row = await db.prepare(sql).bind(...binds).first<{ n: number }>()\n return row?.n ?? 0\n },\n }\n}\n\n// ---------------------------------------------------------------------------\n// Crypto + per-workspace key wiring\n// ---------------------------------------------------------------------------\n\n/** Build the {@link KeyCrypto} the billing key store uses — AES-256-GCM field\n * crypto bound to the product's 64-char-hex `ENCRYPTION_KEY` (or a resolver).\n * This is the concrete impl behind the `../billing` `KeyCrypto` seam. */\nexport function createPresetFieldCrypto(key: string | (() => string)): KeyCrypto {\n return createFieldCrypto(key)\n}\n\n/**\n * The {@link WorkspaceKeyStore} over the preset `workspace_keys` table — the\n * persistence seam the per-workspace key manager needs. \"Active\" = a row with a\n * null `revoked_at`. Pure D1 wiring; no key minting (that's the provisioner).\n */\nexport function createPresetWorkspaceKeyStore(db: D1Like): WorkspaceKeyStore {\n const W = PRESET_TABLES.workspaceKeys\n return {\n async getActive(workspaceId: string): Promise<WorkspaceKeyRecord | null> {\n const row = await db\n .prepare(\n `SELECT ${W.columns.id} AS id, ${W.columns.keyId} AS keyId, ${W.columns.keyEncrypted} AS keyEncrypted, ${W.columns.budgetUsd} AS budgetUsd, ${W.columns.expiresAt} AS expiresAt FROM ${W.name} WHERE ${W.columns.workspaceId} = ? AND ${W.columns.revokedAt} IS NULL ORDER BY ${W.columns.createdAt} DESC LIMIT 1`,\n )\n .bind(workspaceId)\n .first<{ id: string; keyId: string; keyEncrypted: string; budgetUsd: number; expiresAt: number | null }>()\n if (!row) return null\n return {\n id: row.id,\n keyId: row.keyId,\n keyEncrypted: row.keyEncrypted,\n budgetUsd: row.budgetUsd,\n expiresAt: row.expiresAt == null ? null : new Date(row.expiresAt),\n }\n },\n async listActive(workspaceId: string): Promise<Array<{ id: string; keyId: string }>> {\n const res = await db\n .prepare(`SELECT ${W.columns.id} AS id, ${W.columns.keyId} AS keyId FROM ${W.name} WHERE ${W.columns.workspaceId} = ? AND ${W.columns.revokedAt} IS NULL`)\n .bind(workspaceId)\n .all<{ id: string; keyId: string }>()\n return res.results\n },\n async insert(record): Promise<void> {\n await db\n .prepare(\n `INSERT INTO ${W.name} (${W.columns.id}, ${W.columns.workspaceId}, ${W.columns.keyId}, ${W.columns.keyEncrypted}, ${W.columns.budgetUsd}, ${W.columns.expiresAt}, ${W.columns.revokedAt}, ${W.columns.createdAt}) VALUES (?, ?, ?, ?, ?, ?, NULL, ?)`,\n )\n .bind(crypto.randomUUID(), record.workspaceId, record.keyId, record.keyEncrypted, record.budgetUsd, record.expiresAt.getTime(), Date.now())\n .run()\n },\n async markRevoked(id: string, now: Date): Promise<void> {\n await db\n .prepare(`UPDATE ${W.name} SET ${W.columns.revokedAt} = ? WHERE ${W.columns.id} = ?`)\n .bind(now.getTime(), id)\n .run()\n },\n }\n}\n\nexport interface PresetBillingOptions {\n db: D1Like\n /** The key provisioner (`@tangle-network/tcloud`'s client satisfies it structurally). */\n provisioner: KeyProvisioner\n /** Field-crypto key (64-char hex) or resolver — encrypts the minted key at rest. */\n encryptionKey: string | (() => string)\n /** Default monthly USD allowance when a call doesn't specify one. */\n defaultBudgetUsd: number\n /** Injectable clock. */\n now?: () => Date\n /** tcloud product the key is scoped to. Default `'router'`. */\n product?: string\n}\n\n/**\n * Stand up the per-workspace budget-capped {@link WorkspaceKeyManager} on the\n * house stack: the preset `workspace_keys` D1 store + AES-GCM field crypto +\n * the consumer's tcloud provisioner. The mint/rotate/rollover/usage LOGIC lives\n * in `../billing`; this only binds it to the preset table + crypto.\n */\nexport function createPresetWorkspaceKeyManager(opts: PresetBillingOptions): WorkspaceKeyManager {\n return createWorkspaceKeyManager({\n provisioner: opts.provisioner,\n store: createPresetWorkspaceKeyStore(opts.db),\n crypto: createPresetFieldCrypto(opts.encryptionKey),\n defaultBudgetUsd: opts.defaultBudgetUsd,\n now: opts.now,\n product: opts.product,\n })\n}\n"],"mappings":";;;;;;;;AA6FO,IAAM,gBAAgB;AAAA,EAC3B,WAAW;AAAA,IACT,MAAM;AAAA,IACN,SAAS;AAAA,MACP,IAAI;AAAA,MACJ,aAAa;AAAA,MACb,UAAU;AAAA,MACV,MAAM;AAAA,MACN,OAAO;AAAA,MACP,aAAa;AAAA,MACb,QAAQ;AAAA,MACR,WAAW;AAAA,MACX,WAAW;AAAA,IACb;AAAA,EACF;AAAA,EACA,WAAW;AAAA,IACT,MAAM;AAAA,IACN,SAAS;AAAA,MACP,IAAI;AAAA,MACJ,aAAa;AAAA,MACb,MAAM;AAAA,MACN,MAAM;AAAA,MACN,OAAO;AAAA,MACP,SAAS;AAAA,MACT,WAAW;AAAA,IACb;AAAA,EACF;AAAA,EACA,WAAW;AAAA,IACT,MAAM;AAAA,IACN,SAAS;AAAA,MACP,IAAI;AAAA,MACJ,aAAa;AAAA,MACb,UAAU;AAAA,MACV,OAAO;AAAA,MACP,SAAS;AAAA,MACT,UAAU;AAAA,MACV,QAAQ;AAAA,MACR,WAAW;AAAA,IACb;AAAA,E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IACA,MAAM,YAAY,IAAY,KAA0B;AACtD,YAAM,GACH,QAAQ,UAAU,EAAE,IAAI,QAAQ,EAAE,QAAQ,SAAS,cAAc,EAAE,QAAQ,EAAE,MAAM,EACnF,KAAK,IAAI,QAAQ,GAAG,EAAE,EACtB,IAAI;AAAA,IACT;AAAA,EACF;AACF;AAsBO,SAAS,gCAAgC,MAAiD;AAC/F,SAAO,0BAA0B;AAAA,IAC/B,aAAa,KAAK;AAAA,IAClB,OAAO,8BAA8B,KAAK,EAAE;AAAA,IAC5C,QAAQ,wBAAwB,KAAK,aAAa;AAAA,IAClD,kBAAkB,KAAK;AAAA,IACvB,KAAK,KAAK;AAAA,IACV,SAAS,KAAK;AAAA,EAChB,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { InteractionField, InteractionRequest, InteractionData } from '@tangle-network/agent-interface';
|
|
2
|
+
|
|
3
|
+
/** Sidecar → client: the agent raised an ask; data = `{ request }`. */
|
|
4
|
+
declare const INTERACTION_EVENT: "interaction";
|
|
5
|
+
/** Sidecar → client: the ask was withdrawn; data = `{ id, reason? }`. */
|
|
6
|
+
declare const INTERACTION_CANCEL_EVENT: "interaction.cancel";
|
|
7
|
+
/** An ask was answered; data = `{ id, status }`. In the wire contract so a
|
|
8
|
+
* server broadcast and a client-local mark share one event name. */
|
|
9
|
+
declare const INTERACTION_RESOLVED_EVENT: "interaction.resolved";
|
|
10
|
+
declare function isRenderableInteractionKind(kind: string): boolean;
|
|
11
|
+
/** Answer/field keys the sidecar will accept: identifier-safe and never a
|
|
12
|
+
* prototype-pollution vector. */
|
|
13
|
+
declare function isSafeInteractionFieldKey(key: string): boolean;
|
|
14
|
+
type ChatSelectField = Extract<InteractionField, {
|
|
15
|
+
type: 'select';
|
|
16
|
+
}> & {
|
|
17
|
+
allowCustom?: boolean;
|
|
18
|
+
};
|
|
19
|
+
type ChatInteractionField = Exclude<InteractionField, {
|
|
20
|
+
type: 'select';
|
|
21
|
+
}> | ChatSelectField;
|
|
22
|
+
/** `InteractionRequest` whose select fields may carry `allowCustom`. */
|
|
23
|
+
type InteractionRequestWire = Omit<InteractionRequest, 'answerSpec'> & {
|
|
24
|
+
answerSpec: {
|
|
25
|
+
fields: ChatInteractionField[];
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
type ChatInteractionStatus = 'pending' | 'answered' | 'declined' | 'cancelled' | 'expired';
|
|
29
|
+
/** The client/persisted view of one ask. `fields` come verbatim off the wire. */
|
|
30
|
+
interface ChatInteraction {
|
|
31
|
+
id: string;
|
|
32
|
+
kind: string;
|
|
33
|
+
title: string;
|
|
34
|
+
body?: string;
|
|
35
|
+
fields: ChatInteractionField[];
|
|
36
|
+
status: ChatInteractionStatus;
|
|
37
|
+
/** Set when status came from an `interaction.cancel` (e.g. "timeout"). */
|
|
38
|
+
cancelReason?: string;
|
|
39
|
+
}
|
|
40
|
+
declare function isTerminalInteractionStatus(status: ChatInteractionStatus): boolean;
|
|
41
|
+
/** Statuses only move forward (pending → terminal); a replayed/stale `pending`
|
|
42
|
+
* must never resurrect a resolved card. */
|
|
43
|
+
declare function canTransitionInteractionStatus(from: ChatInteractionStatus, to: ChatInteractionStatus): boolean;
|
|
44
|
+
/** Maps an `interaction.cancel` reason to the card's terminal status. */
|
|
45
|
+
declare function cancelStatusFor(reason: string | undefined): ChatInteractionStatus;
|
|
46
|
+
/** Content identity for duplicate safety nets. Excludes volatile ids/statuses. */
|
|
47
|
+
declare function questionInteractionContentSignature(interaction: ChatInteraction): string | null;
|
|
48
|
+
declare function dedupeQuestionInteractionsByContent(interactions: ChatInteraction[]): ChatInteraction[];
|
|
49
|
+
type ParseInteractionResult = {
|
|
50
|
+
succeeded: true;
|
|
51
|
+
value: InteractionRequestWire;
|
|
52
|
+
} | {
|
|
53
|
+
succeeded: false;
|
|
54
|
+
error: string;
|
|
55
|
+
};
|
|
56
|
+
/** Parses an `interaction` event's data (`{ request }`). Validates the shape
|
|
57
|
+
* with the agent-interface schema but returns the raw request so a field a
|
|
58
|
+
* pinned schema predates (`allowCustom`) survives. */
|
|
59
|
+
declare function parseInteractionRequest(data: Record<string, unknown> | undefined): ParseInteractionResult;
|
|
60
|
+
interface InteractionCancelData {
|
|
61
|
+
id: string;
|
|
62
|
+
reason?: string;
|
|
63
|
+
}
|
|
64
|
+
declare function parseInteractionCancel(data: Record<string, unknown> | undefined): {
|
|
65
|
+
succeeded: true;
|
|
66
|
+
value: InteractionCancelData;
|
|
67
|
+
} | {
|
|
68
|
+
succeeded: false;
|
|
69
|
+
error: string;
|
|
70
|
+
};
|
|
71
|
+
declare function fieldAcceptsFreeText(field: ChatInteractionField): boolean;
|
|
72
|
+
interface ComposerAnswerDelivery {
|
|
73
|
+
interactionId: string;
|
|
74
|
+
field: ChatInteractionField;
|
|
75
|
+
}
|
|
76
|
+
/** One delivery per pending ask: the first free-text-capable field, else the
|
|
77
|
+
* first field. Zero-field asks are skipped (nothing to carry the text). */
|
|
78
|
+
declare function composerAnswerDeliveries(pending: ChatInteraction[]): ComposerAnswerDelivery[];
|
|
79
|
+
/** Shapes composer text into the respond payload for the routed field
|
|
80
|
+
* (select answers are string arrays on the wire; text answers are strings). */
|
|
81
|
+
declare function composerAnswerData(field: ChatInteractionField, text: string): InteractionData;
|
|
82
|
+
declare function interactionPartKey(id: string): string;
|
|
83
|
+
declare function noticePartKey(id: string): string;
|
|
84
|
+
type NoticeKind = 'warning' | 'auto-declined';
|
|
85
|
+
/**
|
|
86
|
+
* Persisted-part shapes the codecs below produce — the SAME rows
|
|
87
|
+
* `/chat-store`'s `ChatInteractionPart`/`ChatNoticePart` store, typed at the
|
|
88
|
+
* source so a product pushing them into a `ChatMessagePart[]` transcript needs
|
|
89
|
+
* no cast. Type aliases (not interfaces) on purpose: the implicit index
|
|
90
|
+
* signature keeps them assignable to the `Record<string, unknown>` these
|
|
91
|
+
* codecs previously returned, so existing consumers stay source-compatible.
|
|
92
|
+
*/
|
|
93
|
+
type InteractionPersistedPart = {
|
|
94
|
+
type: 'interaction';
|
|
95
|
+
id: string;
|
|
96
|
+
kind: string;
|
|
97
|
+
title: string;
|
|
98
|
+
body?: string;
|
|
99
|
+
answerSpec: {
|
|
100
|
+
fields: ChatInteractionField[];
|
|
101
|
+
};
|
|
102
|
+
status: ChatInteractionStatus;
|
|
103
|
+
cancelReason?: string;
|
|
104
|
+
};
|
|
105
|
+
type NoticePersistedPart = {
|
|
106
|
+
type: 'notice';
|
|
107
|
+
id: string;
|
|
108
|
+
noticeKind: NoticeKind;
|
|
109
|
+
text: string;
|
|
110
|
+
};
|
|
111
|
+
/** Builds the persisted/streamed `notice` part — a one-line transcript notice
|
|
112
|
+
* explaining an out-of-band event (warning, auto-declined interaction). */
|
|
113
|
+
declare function noticePart(noticeKind: NoticeKind, id: string, text: string): NoticePersistedPart;
|
|
114
|
+
/** Reads a wire request into the client's pending `ChatInteraction`. */
|
|
115
|
+
declare function interactionFromWireRequest(request: InteractionRequestWire): ChatInteraction;
|
|
116
|
+
/** Builds the persisted/streamed `interaction` part from a wire request. */
|
|
117
|
+
declare function interactionToPersistedPart(request: InteractionRequestWire, status: ChatInteractionStatus, cancelReason?: string): InteractionPersistedPart;
|
|
118
|
+
/** Reads a persisted/streamed `interaction` part back into a `ChatInteraction`.
|
|
119
|
+
* Returns null (caller logs) when the part is not one of ours. */
|
|
120
|
+
declare function persistedPartToInteraction(part: Record<string, unknown>): ChatInteraction | null;
|
|
121
|
+
|
|
122
|
+
export { persistedPartToInteraction as A, questionInteractionContentSignature as B, type ChatInteraction as C, INTERACTION_CANCEL_EVENT as I, type NoticeKind as N, type ParseInteractionResult as P, type ChatInteractionField as a, type ChatInteractionStatus as b, type ChatSelectField as c, type ComposerAnswerDelivery as d, INTERACTION_EVENT as e, INTERACTION_RESOLVED_EVENT as f, type InteractionCancelData as g, type InteractionPersistedPart as h, type InteractionRequestWire as i, type NoticePersistedPart as j, canTransitionInteractionStatus as k, cancelStatusFor as l, composerAnswerData as m, composerAnswerDeliveries as n, dedupeQuestionInteractionsByContent as o, fieldAcceptsFreeText as p, interactionFromWireRequest as q, interactionPartKey as r, interactionToPersistedPart as s, isRenderableInteractionKind as t, isSafeInteractionFieldKey as u, isTerminalInteractionStatus as v, noticePart as w, noticePartKey as x, parseInteractionCancel as y, parseInteractionRequest as z };
|
package/dist/index.d.ts
CHANGED
|
@@ -14,12 +14,15 @@ export { DEFAULT_HARNESS, Harness, KNOWN_HARNESSES, ResolveSessionHarnessInput,
|
|
|
14
14
|
export { AgentAppConfig, AgentIdentityConfig, AgentIntegrationsConfig, AgentKnowledgeConfig, AgentTaxonomyConfig, AgentUiConfig, KnowledgeLoopConfig, KnowledgeSourceSpec, agentAppConfigJsonSchema, defineAgentApp } from './config/index.js';
|
|
15
15
|
export { D1Like, D1PreparedLike, DrizzleColumnLike, DrizzleSqliteCoreLike, PRESET_MIGRATION_SQL, PRESET_TABLES, PresetBillingOptions, PresetKnowledgeAccessorOptions, PresetToolHandlerOptions, VaultKv, createD1KnowledgeStateAccessor, createPresetDrizzleSchema, createPresetFieldCrypto, createPresetToolHandlers, createPresetWorkspaceKeyManager, createPresetWorkspaceKeyStore } from './preset-cloudflare/index.js';
|
|
16
16
|
export { KeyCrypto, KeyProvisioner, PlanLimit, PlatformBalanceInfo, PlatformBalanceManager, PlatformBalanceManagerOptions, PlatformBillingClient, PlatformIdentity, PlatformProductUsage, SharedBillingState, TcloudKeyClient, WorkspaceKeyManager, WorkspaceKeyManagerOptions, WorkspaceKeyRecord, WorkspaceKeyStore, WorkspaceModelKeyUsage, createPlatformBalanceManager, createTcloudKeyProvisioner, createWorkspaceKeyManager } from './billing/index.js';
|
|
17
|
+
export { B as BULK_DELETE_MAX_THREADS, C as ChatFilePart, a as ChatImagePart, b as ChatInteractionPart, c as ChatMessagePart, d as ChatNoticePart, e as ChatPartTime, f as ChatReasoningPart, g as ChatStepFinishPart, h as ChatStepStartPart, i as ChatStoreInputError, j as ChatSubtaskPart, k as ChatTextPart, l as ChatToolPart, m as ChatToolState, n as ChatToolStatus, o as ChatUsageTokens, S as StorableHarnessPartKind, p as isChatInteractionPart, q as isChatStepFinishPart, r as isChatTextPart, s as isChatToolPart, t as threadTitleFromMessage } from './parts-BeRnK54I.js';
|
|
17
18
|
export { DeriveKeyOptions, createFieldCrypto, decodeHexKey, decryptAesGcm, decryptBytes, decryptWithKey, deriveKey, encryptAesGcm, encryptBytes, encryptWithKey } from './crypto/index.js';
|
|
18
19
|
export { BufferedTurnEvent, BufferedTurnOptions, BufferedTurnTap, D1LikeForTurns, JsonRecord, PersistedChatMessageForTurn, PumpBufferedTurnOptions, ReplayTurnEventsOptions, ResolvedChatTurn, StreamEvent, TURN_EVENTS_MIGRATION_SQL, TURN_STATUS_SCOPE_MIGRATION_SQL, TurnEventStore, TurnStatus, asRecord, asString, buildUserTextParts, coalesceChatStreamEvents, coalesceDeltas, createBufferedTurnTap, createD1TurnEventStore, createMemoryTurnEventStore, encodeEvent, finalizeAssistantParts, getPartKey, mergePersistedPart, messageHasTurnId, normalizeClientTurnId, normalizePersistedPart, normalizeTime, normalizeToolEvent, pumpBufferedTurn, replayTurnEvents, resolveChatTurn, resolveToolId, resolveToolName } from './stream/index.js';
|
|
19
20
|
export { HubExecClient, HubExecClientOptions, HubExecErrorCode, HubExecResult, HubInvokeDeps, HubInvokeInput, HubInvokeOutcome, ParsedIntegrationAction, invokeIntegrationHub, resolveIntegrationAction } from './integrations/index.js';
|
|
21
|
+
export { C as ChatInteraction, a as ChatInteractionField, b as ChatInteractionStatus, c as ChatSelectField, d as ComposerAnswerDelivery, I as INTERACTION_CANCEL_EVENT, e as INTERACTION_EVENT, f as INTERACTION_RESOLVED_EVENT, g as InteractionCancelData, h as InteractionPersistedPart, i as InteractionRequestWire, N as NoticeKind, j as NoticePersistedPart, P as ParseInteractionResult, k as canTransitionInteractionStatus, l as cancelStatusFor, m as composerAnswerData, n as composerAnswerDeliveries, o as dedupeQuestionInteractionsByContent, p as fieldAcceptsFreeText, q as interactionFromWireRequest, r as interactionPartKey, s as interactionToPersistedPart, t as isRenderableInteractionKind, u as isSafeInteractionFieldKey, v as isTerminalInteractionStatus, w as noticePart, x as noticePartKey, y as parseInteractionCancel, z as parseInteractionRequest, A as persistedPartToInteraction, B as questionInteractionContentSignature } from './contract-DYbTzEDf.js';
|
|
22
|
+
export { InteractionAnswerBodyValidation, InteractionAnswerRoute, InteractionAnswerRouteOptions, InteractionClientOutcome, InteractionConnectionResolution, InteractionRouteLogger, ResolveInteractionConnectionArgs, SidecarInteractionsConnection, SidecarInteractionsError, SidecarInteractionsResult, createInteractionAnswerRoute, listSessionInteractions, mapInteractionRespondFailure, respondToSessionInteraction, validateInteractionAnswerBody } from './interactions/index.js';
|
|
20
23
|
export { CompleteMissionInput, CreateMissionInput, DEFAULT_MISSION_STEP_KINDS, InMemoryMissionStore, MISSION_CONTROL_CHANNEL_ID, MissionApprovalsPort, MissionAuditEvent, MissionConcurrencyError, MissionCostLedger, MissionEngine, MissionEngineOptions, MissionEventSink, MissionGateKind, MissionGateOptions, MissionGateProposal, MissionOutcome, MissionPlanRunOptions, MissionProposalResolution, MissionRecord, MissionService, MissionServiceOptions, MissionState, MissionStatus, MissionStep, MissionStepState, MissionStepStatus, MissionStorePort, MissionStreamEvent, MissionStreamStatus, MissionStreamStep, MissionStreamStepStatus, MissionUpdateGuard, MissionUpdatePatch, ParseMissionBlocksOptions, ParsedMission, ParsedMissionStep, PlanOutcome, RetryableStepError, SandboxDispatch, SandboxDispatchDoneResult, SandboxDispatchInProgressResult, SandboxDispatchInput, SandboxDispatchResult, SetStepStatusPatch, StepGateClassification, StepOutcome, applyMissionEvent, asMissionStreamEvent, budgetGateProposalId, buildAgentMissionPlan, createInMemoryMissionStore, createMissionEngine, createMissionService, isMissionStopRequested, isMissionTerminal, mergeMissionState, noopEventSink, parseMissionBlocks, parseSessionStreamEnvelope, reduceMissionEvents, stepGateProposalId, volumeGateProposalId } from './missions/index.js';
|
|
21
24
|
export { S as StepAgentActivity, W as WithAgentActivity, s as stepAgentActivity } from './agent-activity-C8ZG0F0M.js';
|
|
22
|
-
export { AppToolDescriptor, AuthenticatedSandboxUser, BuildAppToolMcpServersOptions, BuildSandboxToolFileMountsOptions, DEFAULT_SANDBOX_RESOURCES, EnsureWorkspaceSandboxOptions, LivenessProbeConfig, MemberSyncSeam, Outcome, ProfileComposeOptions, ProviderResolutionConfig, ResolveSandboxClientCredentialsOptions, ResolvedModel, SandboxApiCredentials, SandboxBuildContext, SandboxClientCredentials, SandboxCredentialEnvironment, SandboxPermissionLevel, SandboxResourceConfig, SandboxRestoreSpec, SandboxRuntimeAuthRefreshError, SandboxRuntimeConfig, SandboxRuntimeConnection, SandboxScope, SandboxStepTransition, SandboxTerminalTokenOptions, SandboxTerminalTokenResult, SandboxTerminalTokenSubject, SandboxTerminalWsMatch, SandboxToolPathOptions, SandboxToolSpec, ScopedTokenResult, SecretStore, StoppedSandboxResumeFailure, StoppedSandboxResumeRecovery, StreamSandboxPromptOptions, TerminalProxyIdentity, WorkspaceSandboxConnectionArgs, WorkspaceSandboxConnectionHandlerOptions, WorkspaceSandboxEnsureContext, WorkspaceSandboxInstanceLike, WorkspaceSandboxManager, WorkspaceSandboxManagerOptions, WorkspaceSandboxRuntimeProxyArgs, WorkspaceSandboxRuntimeProxyHandlerOptions, WorkspaceSandboxTerminalUpgradeHandlerOptions, WriteProfileFilesOptions, attachReasoningEffort, bearerSubprotocolToken, bearerToken, buildAppToolMcpServers, buildSandboxRuntimeProxyHeaders, buildSandboxToolFileMounts, buildSandboxToolPathSetupScript, classifySeveredStream, createSandboxTerminalToken, createWorkspaceSandboxConnectionHandler, createWorkspaceSandboxManager, createWorkspaceSandboxRuntimeProxyHandler, createWorkspaceSandboxTerminalUpgradeHandler, deleteSecret, detectInteractiveQuestion, driveSandboxTurn, encodeSandboxRuntimePath, ensureWorkspaceSandbox, flattenHistory, getClient, isSandboxTerminalWsUpgrade, isTerminalPromptEvent, matchSandboxTerminalWsPath, mergeExtraMcp, mintSandboxScopedToken, mintTerminalProxyToken, readSecret, resetClientCache, resolveModel, resolveSandboxClientCredentials, runSandboxPrompt, runSandboxToolPathSetup, sandboxToolBinDir, sandboxToolPath, sandboxToolRootDir, secretStoreFromClient, splitDeferredProfileFiles, storeSecret, streamSandboxPrompt, syncSandboxMemberAdd, syncSandboxMemberRemove, syncSandboxMemberRole, terminalTokenFromRequest, verifySandboxTerminalToken, verifyTerminalProxyToken, writeProfileFilesToBox } from './sandbox/index.js';
|
|
25
|
+
export { AppToolDescriptor, AuthenticatedSandboxUser, BuildAppToolMcpServersOptions, BuildSandboxToolFileMountsOptions, DEFAULT_SANDBOX_RESOURCES, ENV_TOTAL_MAX_BYTES, ENV_VALUE_MAX_BYTES, EnsureWorkspaceSandboxOptions, LivenessProbeConfig, MemberSyncSeam, Outcome, PROVISION_PAYLOAD_MAX_BYTES, ProfileComposeOptions, PromptInputPart, ProviderResolutionConfig, ProvisionPayloadSections, ProvisionProfileSection, ResolveSandboxClientCredentialsOptions, ResolvedModel, SandboxApiCredentials, SandboxBuildContext, SandboxClientCredentials, SandboxCredentialEnvironment, SandboxPermissionLevel, SandboxResourceConfig, SandboxRestoreSpec, SandboxRuntimeAuthRefreshError, SandboxRuntimeConfig, SandboxRuntimeConnection, SandboxScope, SandboxStepTransition, SandboxTerminalTokenOptions, SandboxTerminalTokenResult, SandboxTerminalTokenSubject, SandboxTerminalWsMatch, SandboxToolPathOptions, SandboxToolSpec, ScopedTokenResult, SecretStore, StoppedSandboxResumeFailure, StoppedSandboxResumeRecovery, StreamSandboxPromptOptions, TerminalProxyIdentity, WorkspaceSandboxConnectionArgs, WorkspaceSandboxConnectionHandlerOptions, WorkspaceSandboxEnsureContext, WorkspaceSandboxInstanceLike, WorkspaceSandboxManager, WorkspaceSandboxManagerOptions, WorkspaceSandboxRuntimeProxyArgs, WorkspaceSandboxRuntimeProxyHandlerOptions, WorkspaceSandboxTerminalUpgradeHandlerOptions, WriteProfileFilesOptions, assertEnvWithinLimits, assertProvisionPayloadWithinCap, attachReasoningEffort, bearerSubprotocolToken, bearerToken, buildAppToolMcpServers, buildSandboxRuntimeProxyHeaders, buildSandboxToolFileMounts, buildSandboxToolPathSetupScript, classifySeveredStream, createSandboxTerminalToken, createWorkspaceSandboxConnectionHandler, createWorkspaceSandboxManager, createWorkspaceSandboxRuntimeProxyHandler, createWorkspaceSandboxTerminalUpgradeHandler, deleteSecret, detectInteractiveQuestion, driveSandboxTurn, encodeSandboxRuntimePath, ensureWorkspaceSandbox, flattenHistory, getClient, isSandboxTerminalWsUpgrade, isTerminalPromptEvent, matchSandboxTerminalWsPath, mergeExtraMcp, mergeHistoryIntoParts, mintSandboxScopedToken, mintTerminalProxyToken, readSecret, resetClientCache, resolveModel, resolveSandboxClientCredentials, runSandboxPrompt, runSandboxToolPathSetup, sandboxToolBinDir, sandboxToolPath, sandboxToolRootDir, secretStoreFromClient, splitDeferredProfileFiles, storeSecret, streamSandboxPrompt, syncSandboxMemberAdd, syncSandboxMemberRemove, syncSandboxMemberRole, terminalTokenFromRequest, verifySandboxTerminalToken, verifyTerminalProxyToken, writeProfileFilesToBox } from './sandbox/index.js';
|
|
23
26
|
export { CookieOptions, JsonObject, KvLike, RateLimitResult, RequestContext, SecurityHeaderOptions, addSecurityHeaders, assertMediaUrl, checkRateLimit, clearCookieHeader, extractRequestContext, parseJsonObjectBody, readCookieValue, requireString, serializeCookie } from './web/index.js';
|
|
24
27
|
export { BuildRedactedDocumentOptions, DEFAULT_REDACTION_PATTERNS, RedactForIngestionOptions, RedactedDocSegment, RedactedDocument, RedactionPattern, RedactionSpan, RevealResult, RevealSpanOptions, buildRedactedDocument, detectSpans, maskSpans, redactForIngestion, revealSpan } from './redact/index.js';
|
|
25
28
|
export { ApprovalEvent, ApprovalEventSchema, AssetContentMap, AssetFormat, AssetSpec, AssetStatus, AssetVariant, BrandTokens, BrandTokensSchema, ConversionMetrics, ConversionMetricsSchema, CopyContent, CopyContentSchema, CopyPlatform, EmailBodySection, EmailContent, EmailContentSchema, EmailCtaSection, EmailDividerSection, EmailFeatureSection, EmailHeroSection, EmailSection, EmailTestimonialSection, ImageBackground, ImageContent, ImageContentSchema, ImageImageLayer, ImageLayer, ImageLayerType, ImageLogoLayer, ImageShapeLayer, ImageSlide, ImageTextLayer, VideoCaption, VideoContent, VideoContentSchema, VideoCountdownScene, VideoImageRevealScene, VideoScene, VideoSlideScene, VideoTextAnimationScene, parseAssetSpec, safeParseAssetSpec } from './assets/index.js';
|
|
@@ -36,8 +39,8 @@ export { C as CHANNEL_PRESETS, a as ChannelPreset, b as ChannelPresetId, c as Ch
|
|
|
36
39
|
export { RunToolLoopOptions as AppToolLoopOptions, ToolLoopAssistantToolCall as LoopAssistantToolCall, ToolLoopMessage as LoopMessage, ToolLoopCall as LoopToolCall, StreamToolLoopOptions as StreamAppToolLoopOptions, StreamToolLoopYield as StreamLoopYield, ToolLoopEvent, ToolLoopResult, ToolLoopStopReason, runToolLoop as runAppToolLoop, streamToolLoop as streamAppToolLoop } from '@tangle-network/agent-runtime';
|
|
37
40
|
export { CompletionRequirement, CompletionVerdict, CorrectnessChecker, ProducedState, RuntimeEventLike, SatisfiedBy, TaskGold, createLlmCorrectnessChecker, extractProducedState, verifyCompletion, weightedComposite } from '@tangle-network/agent-eval';
|
|
38
41
|
export { F as FlowSpan, a as FlowTrace } from './flow-types-Cb_AblZs.js';
|
|
42
|
+
export { InteractionData, InteractionOutcome, InteractionRequest, modelProvider } from '@tangle-network/agent-interface';
|
|
39
43
|
export { StorageConfig } from '@tangle-network/sandbox';
|
|
40
|
-
export { modelProvider } from '@tangle-network/agent-interface';
|
|
41
44
|
import '@tangle-network/agent-runtime/intelligence';
|
|
42
45
|
import '@tangle-network/agent-knowledge';
|
|
43
46
|
import 'zod';
|