@tangle-network/agent-app 0.60.278 → 0.60.280
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/chat-routes/index.d.ts +1 -0
- package/dist/chat-routes/index.js +1 -1
- package/dist/chat-routes/turn-admission.d.ts +5 -1
- package/dist/chat-routes/turn-prefetch.d.ts +43 -0
- package/dist/chat-routes/turn-route-contracts.d.ts +11 -0
- package/dist/{chunk-S2IEOSN3.js → chunk-GDBDTM7N.js} +53 -16
- package/dist/chunk-GDBDTM7N.js.map +1 -0
- package/dist/public-consultation/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-S2IEOSN3.js.map +0 -1
|
@@ -11,6 +11,7 @@ export * from './binary-sniff';
|
|
|
11
11
|
export * from './attachment-validation';
|
|
12
12
|
export * from './turn-routes';
|
|
13
13
|
export type { ChatTurnCreateThreadOptions, ChatTurnCreateThreadResult, ChatTurnNewThread, ChatTurnThreadOwner } from './minted-thread';
|
|
14
|
+
export type { ChatTurnPrefetch, ChatTurnPrefetchArgs, ChatTurnPrefetchIdentity } from './turn-prefetch';
|
|
14
15
|
export * from './turn-progress';
|
|
15
16
|
export * from './gateway-adapter';
|
|
16
17
|
export * from './stale-turn-lock';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Validated request and transcript projections at the chat-route boundary. */
|
|
2
2
|
import { type ChatMessagePart } from '../chat-store/parts';
|
|
3
3
|
import type { TurnTrace } from '../turn-trace/recorder';
|
|
4
|
-
import type { ChatTurnAuthorization } from './turn-route-contracts';
|
|
4
|
+
import type { ChatTurnAuthorization, CreateChatTurnRoutesOptions } from './turn-route-contracts';
|
|
5
5
|
import { ChatTurnInputError, type ChatTurnFilePartInput, type ChatTurnRequestPayload, type FileMention } from './wire';
|
|
6
6
|
/** A turn whose body parsed and whose caller `authorize` admitted. */
|
|
7
7
|
export type AdmittedTurn<TContext> = {
|
|
@@ -10,6 +10,8 @@ export type AdmittedTurn<TContext> = {
|
|
|
10
10
|
ok: true;
|
|
11
11
|
}>;
|
|
12
12
|
trace: TurnTrace;
|
|
13
|
+
/** The route's read-only prefetch for this admitted identity (`./turn-prefetch`). */
|
|
14
|
+
prefetched?: Promise<unknown>;
|
|
13
15
|
};
|
|
14
16
|
/** Emits one stage on a progress-first stream. */
|
|
15
17
|
export type TurnProgress = (phase: string, message: string) => void;
|
|
@@ -28,6 +30,8 @@ export interface ParsedTurnBody {
|
|
|
28
30
|
* headers alone; the same spans reach the trace sink.
|
|
29
31
|
*/
|
|
30
32
|
export declare function admissionServerTiming(trace: TurnTrace): string;
|
|
33
|
+
/** The admission policy's refusal of the model the turn names, or null when it allows it or names none. */
|
|
34
|
+
export declare function namedModelRefusal(policy: CreateChatTurnRoutesOptions<unknown>['modelPolicy'], payload: ChatTurnRequestPayload, trace: TurnTrace): Promise<Response | null>;
|
|
31
35
|
export declare function validateTurnBody(body: Record<string, unknown>, maxInlinePartBytes: number | undefined): ParsedTurnBody;
|
|
32
36
|
/** File parts persist onto the user message verbatim — the wire shape is the
|
|
33
37
|
* persisted `ChatFilePart`/`ChatImagePart` vocabulary already. Mentions get
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The turn's read-only prefetch: the inputs its prompt needs (profile, knowledge, catalogs), read while `authorize`
|
|
3
|
+
* finishes instead of after it.
|
|
4
|
+
*
|
|
5
|
+
* `authorize` calls `identified({ tenantId, userId })` once it has verified who is asking, and the route starts the
|
|
6
|
+
* product's `prefetch` then, beside the rest of `authorize` (billing gates, rate limits). Nothing it read reaches the
|
|
7
|
+
* producer unless `authorize` allows the turn for that same identity; on a refusal its signal aborts and its result
|
|
8
|
+
* is dropped. An `authorize` that never calls `identified` still gets the prefetch, started once it allows the turn.
|
|
9
|
+
*
|
|
10
|
+
* Why: Legal's turns ran `authorize` (~716 ms) and then the prompt reads (~597 ms) in series before the model call
|
|
11
|
+
* (dispatchMs median 1,567 ms, 2026-10-22); Creative overlaps the same reads by hand (creative-agent
|
|
12
|
+
* chat-vertical.ts:289). The contract: the prefetch only reads. It writes nothing and calls nothing paid or external
|
|
13
|
+
* as the member beyond what the verified identity already allows.
|
|
14
|
+
*/
|
|
15
|
+
import type { TurnTrace } from '../turn-trace/recorder';
|
|
16
|
+
import type { ChatTurnRequestPayload } from './wire';
|
|
17
|
+
/** Who `authorize` verified the caller to be: the workspace (tenant) and the member. */
|
|
18
|
+
export interface ChatTurnPrefetchIdentity {
|
|
19
|
+
tenantId: string;
|
|
20
|
+
userId: string;
|
|
21
|
+
}
|
|
22
|
+
export interface ChatTurnPrefetchArgs extends ChatTurnPrefetchIdentity {
|
|
23
|
+
request: Request;
|
|
24
|
+
body: ChatTurnRequestPayload;
|
|
25
|
+
trace: TurnTrace;
|
|
26
|
+
/** Aborts when the turn is refused; stop reading. */
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
export type ChatTurnPrefetch = (args: ChatTurnPrefetchArgs) => unknown;
|
|
30
|
+
export interface TurnPrefetchRun {
|
|
31
|
+
/** For `authorize`: the caller's identity is verified; start the prefetch now. Later calls are ignored. */
|
|
32
|
+
identified(identity: ChatTurnPrefetchIdentity): void;
|
|
33
|
+
/** `authorize` allowed the turn: the prefetch for this identity, started now if `identified` never ran or named
|
|
34
|
+
* someone else. */
|
|
35
|
+
admitted(identity: ChatTurnPrefetchIdentity): Promise<unknown>;
|
|
36
|
+
/** The turn was refused: abort the prefetch and drop what it read. */
|
|
37
|
+
discard(): void;
|
|
38
|
+
}
|
|
39
|
+
export declare function startTurnPrefetch(prefetch: ChatTurnPrefetch, input: {
|
|
40
|
+
request: Request;
|
|
41
|
+
body: ChatTurnRequestPayload;
|
|
42
|
+
trace: TurnTrace;
|
|
43
|
+
}): TurnPrefetchRun;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** Public contracts for admission, execution, persistence and chat-route hooks. */
|
|
2
|
+
import type { ChatTurnPrefetch, ChatTurnPrefetchIdentity } from './turn-prefetch';
|
|
2
3
|
import type { ChatTurnIdentity, ChatTurnProducer } from '@tangle-network/agent-runtime/durable';
|
|
3
4
|
import type { ChatMessagePart } from '../chat-store/parts';
|
|
4
5
|
import type { InteractionAnswerRoute, InteractionAnswerRouteOptions } from '../interactions/route';
|
|
@@ -164,6 +165,9 @@ export interface ChatTurnAuthorizeArgs<TThread = unknown> {
|
|
|
164
165
|
* Call it only where the product's own thread read missed; it answers 404 when the body names no valid minted
|
|
165
166
|
* thread or the member is outside the arm, and 409 when the id already exists anywhere (see `./minted-thread`). */
|
|
166
167
|
createThread?(owner: ChatTurnThreadOwner): Promise<ChatTurnCreateThreadResult<TThread>>;
|
|
168
|
+
/** Turn intent with the route's `prefetch` option set: call it as soon as the caller's identity is verified, before
|
|
169
|
+
* the rest of the checks, so the prefetch reads beside them (see `./turn-prefetch`). */
|
|
170
|
+
identified?(identity: ChatTurnPrefetchIdentity): void;
|
|
167
171
|
}
|
|
168
172
|
/** What `prepareTurn` receives: the admitted turn, before the conversation loads. */
|
|
169
173
|
export interface ChatTurnPrepareArgs<TContext> {
|
|
@@ -276,6 +280,8 @@ export interface ChatTurnProduceArgs<TContext> {
|
|
|
276
280
|
* `createChatTurnRoutes` invocation supplies it before `produce` runs.
|
|
277
281
|
*/
|
|
278
282
|
handoffCompletion?: () => Promise<void>;
|
|
283
|
+
/** What the route's `prefetch` read for this turn's admitted identity; set only when `prefetch` is configured. */
|
|
284
|
+
prefetched?: Promise<unknown>;
|
|
279
285
|
/** Set only under `dispatchFirst`, where `produce` starts before the lock. Resolves to the final args (with
|
|
280
286
|
* `userMessageId`, after `contextGate` and `beforeTurn`) once the admission chain commits, or rejects with
|
|
281
287
|
* `TurnAdmissionRefusedError` when the turn is refused. Start the model call at once; write nothing durable,
|
|
@@ -492,6 +498,11 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TThread = unknown>
|
|
|
492
498
|
* client skips the thread POST: `authorize` receives `args.createThread` and calls it where its thread read missed.
|
|
493
499
|
* Omit → an unknown thread stays the product's 404. */
|
|
494
500
|
createThread?: ChatTurnCreateThreadOptions<TThread>;
|
|
501
|
+
/** Read the turn's prompt inputs (profile, knowledge, catalogs) while `authorize` finishes: started when `authorize`
|
|
502
|
+
* calls `args.identified`, or once it allows the turn. Read only: no writes, nothing paid or external beyond what
|
|
503
|
+
* the verified identity allows. `produce` receives the result as `args.prefetched` only for an allowed turn of that
|
|
504
|
+
* identity; a refusal aborts `args.signal` and drops it. Omit → nothing is prefetched. */
|
|
505
|
+
prefetch?: ChatTurnPrefetch;
|
|
495
506
|
/** Finish the turn's context after `authorize` has admitted the caller and
|
|
496
507
|
* before the conversation loads or the lock is taken: resolve the profile,
|
|
497
508
|
* the model, anything that reads or writes product state. Keep `authorize`
|
|
@@ -240,6 +240,40 @@ var INTENT_LINE_TIMEOUT_MS = 1500;
|
|
|
240
240
|
var INTENT_LINE_HISTORY_MESSAGES = 6;
|
|
241
241
|
var INTENT_LINE_MAX_CHARS = 240;
|
|
242
242
|
|
|
243
|
+
// src/chat-routes/turn-prefetch.ts
|
|
244
|
+
function startTurnPrefetch(prefetch, input) {
|
|
245
|
+
let started;
|
|
246
|
+
const start = (identity) => {
|
|
247
|
+
const abort = new AbortController();
|
|
248
|
+
const result = input.trace.measure("admission.prefetch", "admission", async () => prefetch({
|
|
249
|
+
...input,
|
|
250
|
+
tenantId: identity.tenantId,
|
|
251
|
+
userId: identity.userId,
|
|
252
|
+
signal: abort.signal
|
|
253
|
+
}));
|
|
254
|
+
result.catch(() => {
|
|
255
|
+
});
|
|
256
|
+
started = { identity: { ...identity }, abort, result };
|
|
257
|
+
};
|
|
258
|
+
return {
|
|
259
|
+
identified(identity) {
|
|
260
|
+
if (!started) start(identity);
|
|
261
|
+
},
|
|
262
|
+
admitted(identity) {
|
|
263
|
+
if (started && (started.identity.tenantId !== identity.tenantId || started.identity.userId !== identity.userId)) {
|
|
264
|
+
started.abort.abort();
|
|
265
|
+
started = void 0;
|
|
266
|
+
}
|
|
267
|
+
if (!started) start(identity);
|
|
268
|
+
return started.result;
|
|
269
|
+
},
|
|
270
|
+
discard() {
|
|
271
|
+
started?.abort.abort();
|
|
272
|
+
started = void 0;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
243
277
|
// src/chat-routes/turn-admission.ts
|
|
244
278
|
function errorResponse(err) {
|
|
245
279
|
return Response.json({ code: err.code, error: err.message }, { status: err.status });
|
|
@@ -249,6 +283,12 @@ function admissionServerTiming(trace) {
|
|
|
249
283
|
const steps = trace.spans(now).filter((span) => span.kind === "admission").sort((a, b) => a.startMs - b.startMs).map((span) => ({ name: span.name, durMs: span.endMs - span.startMs }));
|
|
250
284
|
return formatServerTiming([{ name: "admission", durMs: now - trace.startedAt }, ...steps]);
|
|
251
285
|
}
|
|
286
|
+
async function namedModelRefusal(policy, payload, trace) {
|
|
287
|
+
const namedModel = typeof payload.model === "string" ? payload.model.trim() : "";
|
|
288
|
+
if (!policy || !namedModel) return null;
|
|
289
|
+
const decision = await trace.measure("admission.model_policy", "admission", () => checkModelPolicy(namedModel, policy.catalog, policy.filter, policy.context));
|
|
290
|
+
return decision.allowed ? null : Response.json({ code: "model_policy_denied", error: decision.reason }, { status: 403 });
|
|
291
|
+
}
|
|
252
292
|
function validateTurnBody(body, maxInlinePartBytes) {
|
|
253
293
|
const threadId = typeof body.threadId === "string" ? body.threadId.trim() : "";
|
|
254
294
|
if (!threadId) throw new ChatTurnInputError("Missing threadId");
|
|
@@ -701,6 +741,7 @@ function createAdmittedTurnRunner({
|
|
|
701
741
|
handoffCompletion,
|
|
702
742
|
trace,
|
|
703
743
|
...intentLine ? { intentLine } : {},
|
|
744
|
+
...admitted.prefetched ? { prefetched: admitted.prefetched } : {},
|
|
704
745
|
...ctx?.executionLimits ? { executionLimits: ctx.executionLimits } : {}
|
|
705
746
|
};
|
|
706
747
|
const dispatched = dispatchFirstFor(options, produceArgs, trace);
|
|
@@ -1291,22 +1332,18 @@ function createChatTurnRoutes(options) {
|
|
|
1291
1332
|
throw err;
|
|
1292
1333
|
}
|
|
1293
1334
|
parse.end();
|
|
1294
|
-
const
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
));
|
|
1305
|
-
if (!decision.allowed) {
|
|
1306
|
-
return { ok: false, response: Response.json({ code: "model_policy_denied", error: decision.reason }, { status: 403 }) };
|
|
1307
|
-
}
|
|
1335
|
+
const prefetch = options.prefetch && startTurnPrefetch(options.prefetch, { request, body: parsed.payload, trace });
|
|
1336
|
+
const auth = await trace.measure("admission.authorize", "admission", (span) => options.authorize({ request, intent: "turn", body: parsed.payload, trace, span, createThread: options.createThread && bindTurnThreadCreation(options.createThread, parsed.payload), ...prefetch ? { identified: prefetch.identified } : {} }));
|
|
1337
|
+
if (!auth.ok) {
|
|
1338
|
+
prefetch?.discard();
|
|
1339
|
+
return { ok: false, response: auth.response };
|
|
1340
|
+
}
|
|
1341
|
+
const refusal = await namedModelRefusal(options.modelPolicy, parsed.payload, trace);
|
|
1342
|
+
if (refusal) {
|
|
1343
|
+
prefetch?.discard();
|
|
1344
|
+
return { ok: false, response: refusal };
|
|
1308
1345
|
}
|
|
1309
|
-
return { ok: true, parsed, auth, trace };
|
|
1346
|
+
return { ok: true, parsed, auth, trace, ...prefetch ? { prefetched: prefetch.admitted(auth) } : {} };
|
|
1310
1347
|
}
|
|
1311
1348
|
function progressFirstTurn(request, ctx, admitted) {
|
|
1312
1349
|
const receivedAt = Date.now();
|
|
@@ -1665,4 +1702,4 @@ export {
|
|
|
1665
1702
|
createChatTurnRoutes,
|
|
1666
1703
|
streamChatRouteAsSandboxEvents
|
|
1667
1704
|
};
|
|
1668
|
-
//# sourceMappingURL=chunk-
|
|
1705
|
+
//# sourceMappingURL=chunk-GDBDTM7N.js.map
|