@tangle-network/agent-app 0.62.3 → 0.63.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 +1 -1
- package/dist/agent-profiles/d1.js +1 -1
- package/dist/agent-profiles/index.d.ts +4 -1
- package/dist/agent-profiles/index.js +1 -1
- package/dist/app-auth/index.js +2 -2
- package/dist/assistant/index.js +7 -7
- package/dist/chat-react/index.js +3 -3
- package/dist/chat-routes/cancellation.d.ts +61 -0
- package/dist/chat-routes/dispatch-first.d.ts +14 -25
- package/dist/chat-routes/early-turn-work.d.ts +10 -0
- package/dist/chat-routes/hybrid-producer.d.ts +4 -3
- package/dist/chat-routes/index.d.ts +2 -1
- package/dist/chat-routes/index.js +86 -1874
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/native-turn-timing.d.ts +15 -0
- package/dist/chat-routes/producer-stream.d.ts +5 -0
- package/dist/chat-routes/route-producer.d.ts +3 -0
- package/dist/chat-routes/route-storage.d.ts +7 -0
- package/dist/chat-routes/router-producer.d.ts +31 -5
- package/dist/chat-routes/turn-admission.d.ts +2 -1
- package/dist/chat-routes/turn-execution.d.ts +6 -11
- package/dist/chat-routes/turn-ownership.d.ts +22 -0
- package/dist/chat-routes/turn-route-contracts.d.ts +112 -105
- package/dist/chat-routes/turn-routes.d.ts +4 -4
- package/dist/chat-routes/wire.d.ts +0 -7
- package/dist/chat-store/index.d.ts +1 -1
- package/dist/chat-store/index.js +21 -3
- package/dist/chat-store/index.js.map +1 -1
- package/dist/chat-store/schema.d.ts +3 -2
- package/dist/chat-store/store.d.ts +8 -0
- package/dist/{chunk-GIEUZBQQ.js → chunk-4CALWPN5.js} +3 -7
- package/dist/chunk-4CALWPN5.js.map +1 -0
- package/dist/chunk-4DBHC63F.js +54 -0
- package/dist/chunk-4DBHC63F.js.map +1 -0
- package/dist/{chunk-G7M2C72Q.js → chunk-5TSRFF5W.js} +1 -1
- package/dist/chunk-5TSRFF5W.js.map +1 -0
- package/dist/chunk-A2NQMVMF.js +485 -0
- package/dist/chunk-A2NQMVMF.js.map +1 -0
- package/dist/{chunk-PCAY23GS.js → chunk-AVBNS64P.js} +172 -2
- package/dist/chunk-AVBNS64P.js.map +1 -0
- package/dist/{chunk-4P5NFBWX.js → chunk-BI5AZAOO.js} +4 -4
- package/dist/chunk-BV3VZEKB.js +483 -0
- package/dist/chunk-BV3VZEKB.js.map +1 -0
- package/dist/{chunk-UW2DKTEM.js → chunk-IDG2XR5O.js} +20 -10
- package/dist/chunk-IDG2XR5O.js.map +1 -0
- package/dist/{chunk-KS4I7KVC.js → chunk-JRTWTADT.js} +184 -123
- package/dist/chunk-JRTWTADT.js.map +1 -0
- package/dist/chunk-KG2Y44XT.js +1 -0
- package/dist/chunk-KG2Y44XT.js.map +1 -0
- package/dist/chunk-MM3GOLHO.js +3365 -0
- package/dist/chunk-MM3GOLHO.js.map +1 -0
- package/dist/chunk-N6URQ6DM.js +225 -0
- package/dist/chunk-N6URQ6DM.js.map +1 -0
- package/dist/{chunk-ISRPOPMM.js → chunk-ND5XWLRU.js} +21 -18
- package/dist/chunk-ND5XWLRU.js.map +1 -0
- package/dist/{chunk-IUVDR3KJ.js → chunk-NHVSO47Q.js} +2 -2
- package/dist/{chunk-FAXSYC2C.js → chunk-RZLCBWGG.js} +2 -2
- package/dist/{chunk-MRZP65DZ.js → chunk-SRWQCWLR.js} +14 -3
- package/dist/chunk-SRWQCWLR.js.map +1 -0
- package/dist/{chunk-JRW6LCIA.js → chunk-V6YGLAQC.js} +2 -2
- package/dist/{chunk-54YIL7WM.js → chunk-WPPDNS63.js} +4 -4
- package/dist/{chunk-CQZVJVME.js → chunk-YCMVNXAX.js} +2 -2
- package/dist/design-canvas/index.js +3 -3
- package/dist/openui/index.js +3 -3
- package/dist/operator/index.js +2 -2
- package/dist/platform/billable-turn-admission.d.ts +13 -9
- package/dist/platform/delegated-keys.d.ts +4 -3
- package/dist/platform/delegated-turn-keys.d.ts +17 -6
- package/dist/platform/index.js +80 -23
- package/dist/platform/index.js.map +1 -1
- package/dist/preset-cloudflare/index.d.ts +2 -0
- package/dist/preset-cloudflare/index.js +344 -0
- package/dist/preset-cloudflare/index.js.map +1 -1
- package/dist/preset-cloudflare/native-completion-owner.d.ts +90 -0
- package/dist/preset-cloudflare/native-completion-store.d.ts +56 -0
- package/dist/preset-cloudflare/native-completion-workflow.d.ts +10 -0
- package/dist/public-consultation/index.js +28 -7
- package/dist/public-consultation/index.js.map +1 -1
- package/dist/rollout/d1.d.ts +103 -3
- package/dist/rollout/d1.js +152 -5
- package/dist/rollout/d1.js.map +1 -1
- package/dist/runtime/index.js +3 -4
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/router-turn.d.ts +4 -0
- package/dist/runtime/router-turn.js +2 -3
- package/dist/sandbox/index.js +3 -4
- package/dist/sandbox/sandbox-run-tool.d.ts +5 -0
- package/dist/sequences/index.js +5 -5
- package/dist/signoff/cli.js +15 -3
- package/dist/signoff/cli.js.map +1 -1
- package/dist/signoff/config.d.ts +3 -1
- package/dist/signoff/index.d.ts +2 -1
- package/dist/signoff/index.js +3 -1
- package/dist/signoff/phases.d.ts +3 -0
- package/dist/signoff/proof.js +3 -0
- package/dist/signoff/proof.js.map +1 -1
- package/dist/signoff/run.d.ts +3 -1
- package/dist/signoff/types.d.ts +13 -0
- package/dist/skills-placement/box.js +5 -5
- package/dist/skills-placement/index.js +2 -2
- package/dist/tools/index.js +1 -1
- package/dist/turn-stream/adapters.d.ts +10 -4
- package/dist/turn-stream/index.js +33 -356
- package/dist/turn-stream/index.js.map +1 -1
- package/dist/turn-trace/index.d.ts +1 -0
- package/dist/turn-trace/index.js +4 -5
- package/dist/turn-trace/phase-timing.d.ts +20 -0
- package/dist/turn-trace/recorder.d.ts +3 -0
- package/dist/turn-trace/turn-facts.d.ts +17 -0
- package/dist/web-react/agent-profile-editor.js +1 -1
- package/dist/web-react/chat-stream.d.ts +0 -7
- package/dist/web-react/index.js +7 -9
- package/dist/work-product/index.js +7 -7
- package/package.json +1 -1
- package/dist/chat-routes/minted-thread.d.ts +0 -63
- package/dist/chunk-6R73V3Y5.js +0 -17
- package/dist/chunk-6R73V3Y5.js.map +0 -1
- package/dist/chunk-CCVWR33L.js +0 -99
- package/dist/chunk-CCVWR33L.js.map +0 -1
- package/dist/chunk-EFQUAWWF.js +0 -1749
- package/dist/chunk-EFQUAWWF.js.map +0 -1
- package/dist/chunk-G7M2C72Q.js.map +0 -1
- package/dist/chunk-GIEUZBQQ.js.map +0 -1
- package/dist/chunk-ISRPOPMM.js.map +0 -1
- package/dist/chunk-KS4I7KVC.js.map +0 -1
- package/dist/chunk-MRZP65DZ.js.map +0 -1
- package/dist/chunk-PCAY23GS.js.map +0 -1
- package/dist/chunk-UW2DKTEM.js.map +0 -1
- package/dist/chunk-VK3F4N4U.js +0 -133
- package/dist/chunk-VK3F4N4U.js.map +0 -1
- /package/dist/{chunk-4P5NFBWX.js.map → chunk-BI5AZAOO.js.map} +0 -0
- /package/dist/{chunk-IUVDR3KJ.js.map → chunk-NHVSO47Q.js.map} +0 -0
- /package/dist/{chunk-FAXSYC2C.js.map → chunk-RZLCBWGG.js.map} +0 -0
- /package/dist/{chunk-JRW6LCIA.js.map → chunk-V6YGLAQC.js.map} +0 -0
- /package/dist/{chunk-54YIL7WM.js.map → chunk-WPPDNS63.js.map} +0 -0
- /package/dist/{chunk-CQZVJVME.js.map → chunk-YCMVNXAX.js.map} +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Serializable route observations transferred to the existing native admission owner. */
|
|
2
|
+
import type { NativeTurnTimingFacts } from '../turn-trace/turn-facts';
|
|
3
|
+
export type { NativeTurnTimingFacts } from '../turn-trace/turn-facts';
|
|
4
|
+
export interface NativeCompletionHandoffOptions {
|
|
5
|
+
/** Persist on the existing admission, keyed by turnStreamId. Initial origin must survive before handoff. */
|
|
6
|
+
persistTimingFacts?(facts: NativeTurnTimingFacts): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
export interface NativeTimingTransfer {
|
|
9
|
+
snapshot(): NativeTurnTimingFacts;
|
|
10
|
+
register(options?: NativeCompletionHandoffOptions): Promise<void>;
|
|
11
|
+
note(): Promise<void>;
|
|
12
|
+
flush(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/** Initial registration is fail-closed. Later observations are serialized and waitUntil-tracked by the route. */
|
|
15
|
+
export declare function createNativeTimingTransfer(read: () => NativeTurnTimingFacts, onError: (error: unknown) => void): NativeTimingTransfer;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ChatTurnRouteProducer } from './turn-route-contracts';
|
|
2
|
+
/** Replace transport while keeping every capability on its original receiver.
|
|
3
|
+
* Object.create alone breaks class private state; spreading snapshots getters.
|
|
4
|
+
* The separate target also permits wrapping an Object.freeze'd producer. */
|
|
5
|
+
export declare function withProducerStream(producer: ChatTurnRouteProducer, stream: ChatTurnRouteProducer['stream']): ChatTurnRouteProducer;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ChatTurnProduceArgs, ChatTurnRouteProducer, ChatTurnRouterBackend, ChatTurnSandboxBackend } from './turn-route-contracts';
|
|
2
|
+
export declare function routerRouteProducer<TContext, TResource>(value: ChatTurnRouterBackend, args: ChatTurnProduceArgs<TContext, TResource>): ChatTurnRouteProducer;
|
|
3
|
+
export declare function sandboxRouteProducer<TContext, TResource>(value: ChatTurnSandboxBackend, args: ChatTurnProduceArgs<TContext, TResource>): ChatTurnRouteProducer;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Shared D1 replay and authenticated thread locks from the application's bindings. */
|
|
2
|
+
import { type TurnEventStore } from '../stream/turn-buffer';
|
|
3
|
+
import type { CreateChatTurnRoutesOptions } from './turn-route-contracts';
|
|
4
|
+
export type ResolvedChatTurnRoutesOptions<TContext, TResource = unknown> = Omit<CreateChatTurnRoutesOptions<TContext, TResource>, 'turnStore'> & {
|
|
5
|
+
turnStore: TurnEventStore;
|
|
6
|
+
};
|
|
7
|
+
export declare function resolveTurnStorage<TContext, TResource>(options: CreateChatTurnRoutesOptions<TContext, TResource>): ResolvedChatTurnRoutesOptions<TContext, TResource>;
|
|
@@ -8,12 +8,38 @@
|
|
|
8
8
|
* `stream` generator drains in order, so a slow consumer never drops a frame
|
|
9
9
|
* and the engine never waits on the consumer.
|
|
10
10
|
*/
|
|
11
|
-
import { type RouterTurnEvent, type RouterTurnOptions, type RouterTurnOutcome } from '../runtime/router-turn';
|
|
11
|
+
import { composeRouterTurnPromptSections, type RouterTurnEvent, type RouterTurnFailure, type RouterTurnOptions, type RouterTurnOutcome, type RouterTurnPromptOptions } from '../runtime/router-turn';
|
|
12
12
|
import type { ChatTurnRouteProducer } from './turn-routes';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
import type { ProducerWireEvent } from './wire';
|
|
14
|
+
type RouterTurnKey = string | {
|
|
15
|
+
key: string;
|
|
16
|
+
fetchImpl?: typeof fetch;
|
|
17
|
+
revoke(): Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
type RouterTurnPrompt = string | (RouterTurnPromptOptions & {
|
|
20
|
+
profile: Parameters<typeof composeRouterTurnPromptSections>[0];
|
|
21
|
+
});
|
|
22
|
+
export type RouterTurnProducerOptions = Omit<RouterTurnOptions, 'onEvent' | 'systemPrompt' | 'apiKey'> & {
|
|
23
|
+
/** The turn's key, including any claim still in flight. A lease's fetch and retirement belong to this producer. */
|
|
24
|
+
apiKey: RouterTurnKey | Promise<RouterTurnKey>;
|
|
25
|
+
/** Domain-authored stable and per-turn bytes; composition and prefix validation are automatic. */
|
|
26
|
+
systemPrompt: RouterTurnPrompt | Promise<RouterTurnPrompt>;
|
|
27
|
+
/** Final admission: model text may start earlier, but every tool waits for this request's committed domain state. */
|
|
28
|
+
admitted?: Promise<unknown>;
|
|
29
|
+
/** Observe the finished turn or supply domain-authored final text/failure; projection and persistence stay shared. */
|
|
30
|
+
onOutcome?: (outcome: RouterTurnOutcome) => void | {
|
|
31
|
+
trailingText?: string;
|
|
32
|
+
failure?: RouterTurnFailure;
|
|
33
|
+
} | Promise<void | {
|
|
34
|
+
trailingText?: string;
|
|
35
|
+
failure?: RouterTurnFailure;
|
|
36
|
+
}>;
|
|
16
37
|
/** Observe each engine event after the producer has projected it (a box speculator, metrics). A throw is ignored. */
|
|
17
38
|
onEvent?: (event: RouterTurnEvent) => void;
|
|
39
|
+
/** Retire product resources once, including initialization failure and cancellation before the stream is read. */
|
|
40
|
+
onSettled?(): Promise<void> | void;
|
|
41
|
+
};
|
|
42
|
+
export declare function createRouterTurnProducer(options: RouterTurnProducerOptions): Omit<ChatTurnRouteProducer, 'stream'> & {
|
|
43
|
+
stream: AsyncGenerator<ProducerWireEvent, void, unknown>;
|
|
18
44
|
};
|
|
19
|
-
export
|
|
45
|
+
export {};
|
|
@@ -8,6 +8,7 @@ export type AdmittedTurn<TContext> = {
|
|
|
8
8
|
parsed: ParsedTurnBody;
|
|
9
9
|
auth: Extract<ChatTurnAuthorization<TContext>, {
|
|
10
10
|
ok: true;
|
|
11
|
+
context: TContext;
|
|
11
12
|
}>;
|
|
12
13
|
trace: TurnTrace;
|
|
13
14
|
/** The route's read-only prefetch for this admitted identity (`./turn-prefetch`). */
|
|
@@ -32,7 +33,7 @@ export interface ParsedTurnBody {
|
|
|
32
33
|
export declare function admissionServerTiming(trace: TurnTrace): string;
|
|
33
34
|
/** The admission policy's refusal of the model the turn names, or null when it allows it or names none. */
|
|
34
35
|
export declare function namedModelRefusal(policy: CreateChatTurnRoutesOptions<unknown>['modelPolicy'], payload: ChatTurnRequestPayload, trace: TurnTrace): Promise<Response | null>;
|
|
35
|
-
export declare function validateTurnBody(body: Record<string, unknown
|
|
36
|
+
export declare function validateTurnBody(body: Record<string, unknown>): ParsedTurnBody;
|
|
36
37
|
/** File parts persist onto the user message verbatim — the wire shape is the
|
|
37
38
|
* persisted `ChatFilePart`/`ChatImagePart` vocabulary already. Mentions get
|
|
38
39
|
* the one mapping step their own vocabulary needs. The typed projection is
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import type { TurnTraceSink } from '../turn-trace/recorder';
|
|
2
2
|
import { type AssistantDraftStore, type DraftPersistenceTuning } from './draft-persistence';
|
|
3
3
|
import { type AdmittedTurn, type TurnProgressCallback } from './turn-admission';
|
|
4
|
-
import type { ChatTurnRequestContext, ChatTurnTraceOptions
|
|
4
|
+
import type { ChatTurnRequestContext, ChatTurnTraceOptions } from './turn-route-contracts';
|
|
5
5
|
import type { TurnRoutePhase } from './turn-progress';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/** Free this too if the turn ends before `handOff`. */
|
|
11
|
-
add(release: () => void): void;
|
|
12
|
-
}
|
|
13
|
-
interface TurnExecutionOptions<TContext> {
|
|
14
|
-
options: CreateChatTurnRoutesOptions<TContext>;
|
|
6
|
+
import type { ResolvedChatTurnRoutesOptions } from './route-storage';
|
|
7
|
+
import type { AfterAuthorizeWork } from './early-turn-work';
|
|
8
|
+
interface TurnExecutionOptions<TContext, TResource> {
|
|
9
|
+
options: ResolvedChatTurnRoutesOptions<TContext, TResource>;
|
|
15
10
|
draftStore: AssistantDraftStore;
|
|
16
11
|
draftTuning: DraftPersistenceTuning | null;
|
|
17
12
|
progressMessages: Record<TurnRoutePhase, string>;
|
|
@@ -20,5 +15,5 @@ interface TurnExecutionOptions<TContext> {
|
|
|
20
15
|
startIntentLine(admitted: AdmittedTurn<TContext>): Promise<string | null> | undefined;
|
|
21
16
|
log(message: string, meta?: Record<string, unknown>): void;
|
|
22
17
|
}
|
|
23
|
-
export declare function createAdmittedTurnRunner<TContext>(dependencies: TurnExecutionOptions<TContext>): (request: Request, ctx: ChatTurnRequestContext | undefined, admitted: AdmittedTurn<TContext>, early: AfterAuthorizeWork
|
|
18
|
+
export declare function createAdmittedTurnRunner<TContext, TResource>(dependencies: TurnExecutionOptions<TContext, TResource>): (request: Request, ctx: ChatTurnRequestContext | undefined, admitted: AdmittedTurn<TContext>, early: AfterAuthorizeWork<TResource>, progress?: TurnProgressCallback | undefined, startedIntent?: Promise<string | null> | undefined) => Promise<Response>;
|
|
24
19
|
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AssistantDraftWriter } from './draft-persistence';
|
|
2
|
+
import type { NativeCompletionHandoffOptions, NativeTimingTransfer } from './native-turn-timing';
|
|
3
|
+
import type { BufferedTurnTap } from '../stream/index';
|
|
4
|
+
import type { ChatTurnLock } from './turn-route-contracts';
|
|
5
|
+
interface OwnershipState {
|
|
6
|
+
completionHandoff: boolean;
|
|
7
|
+
completionHandoffPromise: Promise<void> | undefined;
|
|
8
|
+
nativeTiming: NativeTimingTransfer | undefined;
|
|
9
|
+
tap: BufferedTurnTap | undefined;
|
|
10
|
+
draft: AssistantDraftWriter | undefined;
|
|
11
|
+
lockAcquired: boolean;
|
|
12
|
+
lockReleased: boolean;
|
|
13
|
+
lockHandle: unknown;
|
|
14
|
+
}
|
|
15
|
+
/** One transfer of terminal authority, retaining the route only as a live projector. */
|
|
16
|
+
export declare function createChatTurnOwnership(state: OwnershipState, log: (message: string, meta?: Record<string, unknown>) => void, turnStreamId: string, options: {
|
|
17
|
+
turnLock?: Pick<ChatTurnLock<unknown>, 'release'>;
|
|
18
|
+
}): {
|
|
19
|
+
handoffCompletion: (handoff?: NativeCompletionHandoffOptions) => Promise<void>;
|
|
20
|
+
releaseLock: () => Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** Public contracts for admission, execution, persistence and chat-route hooks. */
|
|
2
|
+
import type { NativeCompletionHandoffOptions, NativeTurnTimingFacts } from './native-turn-timing';
|
|
2
3
|
import type { ChatMessageValues } from '../chat-store/core';
|
|
3
4
|
import type { ChatTurnPrefetch, ChatTurnPrefetchIdentity } from './turn-prefetch';
|
|
4
5
|
import type { ChatTurnIdentity, ChatTurnProducer } from '@tangle-network/agent-runtime/durable';
|
|
@@ -7,11 +8,27 @@ import type { InteractionAnswerRoute, InteractionAnswerRouteOptions } from '../i
|
|
|
7
8
|
import type { ModelFailoverAttempt } from '../model-resolution/failover';
|
|
8
9
|
import type { ModelPolicy } from '../policy-filters/index';
|
|
9
10
|
import type { PersistedChatMessageForTurn, TurnEventStore } from '../stream/index';
|
|
11
|
+
import type { D1LikeForTurns } from '../stream/turn-buffer';
|
|
12
|
+
import type { TurnStreamAuth, TurnStreamNamespaceLike } from '../turn-stream/adapters';
|
|
10
13
|
import type { TurnSpan, TurnSpanAttrs, TurnTrace, TurnTraceSink, TurnTraceSummary } from '../turn-trace/recorder';
|
|
11
|
-
import type { DraftPersistenceTuning } from './draft-persistence';
|
|
12
|
-
import type { ChatTurnCreateThreadOptions, ChatTurnCreateThreadResult, ChatTurnThreadOwner } from './minted-thread';
|
|
13
14
|
import type { TurnRoutePhase } from './turn-progress';
|
|
14
15
|
import type { ChatTurnPartInput, ChatTurnRequestPayload } from './wire';
|
|
16
|
+
import type { RouterTurnProducerOptions } from './router-producer';
|
|
17
|
+
import type { HybridTurnProducerOptions } from './hybrid-producer';
|
|
18
|
+
import type { SandboxChatProducerOptions } from './sandbox-producer';
|
|
19
|
+
import type { ChatTurnCancelOptions, ChatTurnCancelRequest } from './cancellation';
|
|
20
|
+
/** The route supplies request tracing, cancellation and the admission fence.
|
|
21
|
+
* Domain callbacks supply model, credentials, policy, tools and prompt data. */
|
|
22
|
+
export type ChatTurnRouterConfiguration = Omit<RouterTurnProducerOptions, 'trace' | 'traceId' | 'signal' | 'userMessage' | 'history'> & {
|
|
23
|
+
userMessage?: RouterTurnProducerOptions['userMessage'];
|
|
24
|
+
history?: RouterTurnProducerOptions['history'];
|
|
25
|
+
};
|
|
26
|
+
export type ChatTurnHybridConfiguration = Omit<HybridTurnProducerOptions, 'trace' | 'traceId' | 'signal' | 'userMessage' | 'history'> & {
|
|
27
|
+
userMessage?: HybridTurnProducerOptions['userMessage'];
|
|
28
|
+
history?: HybridTurnProducerOptions['history'];
|
|
29
|
+
};
|
|
30
|
+
export type ChatTurnRouterBackend = ChatTurnRouteProducer | ChatTurnRouterConfiguration | ChatTurnHybridConfiguration;
|
|
31
|
+
export type ChatTurnSandboxBackend = ChatTurnRouteProducer | SandboxChatProducerOptions;
|
|
15
32
|
/** Usage receipt persisted onto the assistant message (the flattened
|
|
16
33
|
* `step-finish` shape `/chat-store`'s columns mirror). */
|
|
17
34
|
export interface ChatTurnUsage {
|
|
@@ -52,6 +69,10 @@ export interface ChatTurnMessageStore {
|
|
|
52
69
|
* drain. `createSandboxChatProducer` returns this; a router-lane producer
|
|
53
70
|
* may omit the optional members (finalText persists as a single text part). */
|
|
54
71
|
export interface ChatTurnRouteProducer extends ChatTurnProducer {
|
|
72
|
+
/** Pending resource retirement after stream termination. The route tracks it
|
|
73
|
+
* with the request's waitUntil, including when admission refuses an early
|
|
74
|
+
* Router call. Cleanup must handle and report its own failures. */
|
|
75
|
+
cleanup?: Promise<void>;
|
|
55
76
|
/** Actual backend result/done was observed; transport EOF/error alone is not completion. */
|
|
56
77
|
terminalObserved?(): boolean;
|
|
57
78
|
assistantParts?(): Array<Record<string, unknown>>;
|
|
@@ -101,6 +122,8 @@ export type ChatTurnAuthorization<TContext> = {
|
|
|
101
122
|
tenantId: string;
|
|
102
123
|
userId: string;
|
|
103
124
|
context: TContext;
|
|
125
|
+
/** Omit for existing callers; a full execution authorization may name its intent explicitly. */
|
|
126
|
+
intent?: 'turn';
|
|
104
127
|
/** When `false`, skip the `role:'user'` message insert for this turn — for
|
|
105
128
|
* a product-dispatched / synthetic turn (e.g. a follow-up the product
|
|
106
129
|
* raised itself) that must not surface a new user row. Composes with —
|
|
@@ -116,16 +139,24 @@ export type ChatTurnAuthorization<TContext> = {
|
|
|
116
139
|
* when there is no row to reuse, so a seam anchoring to the user row must
|
|
117
140
|
* handle that arm rather than assume a string. */
|
|
118
141
|
insertUserMessage?: boolean;
|
|
142
|
+
} | {
|
|
143
|
+
ok: true;
|
|
144
|
+
/** Identity/access for this operation only; it cannot start a turn. */
|
|
145
|
+
intent: 'replay' | 'running' | 'cancel';
|
|
146
|
+
tenantId: string;
|
|
147
|
+
userId: string;
|
|
119
148
|
} | {
|
|
120
149
|
ok: false;
|
|
121
150
|
response: Response;
|
|
122
151
|
};
|
|
123
152
|
/** Define arguments required to authorize a chat turn based on intent and request details */
|
|
124
|
-
export interface ChatTurnAuthorizeArgs
|
|
153
|
+
export interface ChatTurnAuthorizeArgs {
|
|
125
154
|
request: Request;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
155
|
+
/** This request's bound host continuation, when supplied to turn(request, ctx). */
|
|
156
|
+
waitUntil?: ChatTurnRequestContext['waitUntil'];
|
|
157
|
+
intent: 'turn' | 'replay' | 'running' | 'cancel';
|
|
158
|
+
/** Parsed, validated POST body for a turn or cancellation. */
|
|
159
|
+
body?: ChatTurnRequestPayload | ChatTurnCancelRequest;
|
|
129
160
|
/** The buffered turn id being replayed (replay intent only). */
|
|
130
161
|
turnId?: string;
|
|
131
162
|
/** The thread whose running turns are being discovered (running intent only). */
|
|
@@ -135,10 +166,6 @@ export interface ChatTurnAuthorizeArgs<TThread = unknown> {
|
|
|
135
166
|
trace?: TurnTrace;
|
|
136
167
|
/** The `admission.authorize` span (turn intent only): the parent for those child spans, so they nest under it. */
|
|
137
168
|
span?: TurnSpan;
|
|
138
|
-
/** Turn intent with the route's `createThread` option set: create the turn's thread from the id the client minted.
|
|
139
|
-
* Call it only where the product's own thread read missed; it answers 404 when the body names no valid minted
|
|
140
|
-
* thread or the member is outside the arm, and 409 when the id already exists anywhere (see `./minted-thread`). */
|
|
141
|
-
createThread?(owner: ChatTurnThreadOwner): Promise<ChatTurnCreateThreadResult<TThread>>;
|
|
142
169
|
/** Turn intent with the route's `prefetch` option set: call it as soon as the caller's identity is verified, before
|
|
143
170
|
* the rest of the checks, so the prefetch reads beside them (see `./turn-prefetch`). */
|
|
144
171
|
identified?(identity: ChatTurnPrefetchIdentity): void;
|
|
@@ -146,6 +173,8 @@ export interface ChatTurnAuthorizeArgs<TThread = unknown> {
|
|
|
146
173
|
/** What `prepareTurn` receives: the admitted turn, before the conversation loads. */
|
|
147
174
|
export interface ChatTurnPrepareArgs<TContext> {
|
|
148
175
|
request: Request;
|
|
176
|
+
/** This request's bound host continuation, when supplied to turn(request, ctx). */
|
|
177
|
+
waitUntil?: ChatTurnRequestContext['waitUntil'];
|
|
149
178
|
/** Parsed, validated POST body. */
|
|
150
179
|
body: ChatTurnRequestPayload;
|
|
151
180
|
tenantId: string;
|
|
@@ -167,6 +196,8 @@ export type ChatTurnPrepareResult = {
|
|
|
167
196
|
/** What `afterAuthorize` receives: the admitted turn, before the conversation loads and before any lock, read or write. */
|
|
168
197
|
export interface ChatTurnAfterAuthorizeArgs<TContext> {
|
|
169
198
|
request: Request;
|
|
199
|
+
/** This request's bound host continuation, when supplied to turn(request, ctx). */
|
|
200
|
+
waitUntil?: ChatTurnRequestContext['waitUntil'];
|
|
170
201
|
/** Parsed, validated POST body. */
|
|
171
202
|
body: ChatTurnRequestPayload;
|
|
172
203
|
tenantId: string;
|
|
@@ -178,7 +209,12 @@ export interface ChatTurnAfterAuthorizeArgs<TContext> {
|
|
|
178
209
|
trace: TurnTrace;
|
|
179
210
|
}
|
|
180
211
|
/** Frees what `afterAuthorize` started, for a turn that ends before `produce` takes it. */
|
|
181
|
-
export type ChatTurnAfterAuthorizeRelease = () => void | Promise<void
|
|
212
|
+
export type ChatTurnAfterAuthorizeRelease<TResource = unknown> = (() => void | Promise<void>) | {
|
|
213
|
+
/** Acquisition must clean up any partial resource when it rejects. */
|
|
214
|
+
resource: Promise<TResource>;
|
|
215
|
+
/** Called once when this turn ends, including a refusal before acquisition resolves. */
|
|
216
|
+
dispose(resource: TResource): void | Promise<void>;
|
|
217
|
+
};
|
|
182
218
|
/** Trusted per-turn limits supplied by a server adapter, never by the body. */
|
|
183
219
|
export interface ChatTurnExecutionLimits {
|
|
184
220
|
maxInputTokens?: number;
|
|
@@ -197,8 +233,12 @@ export interface ChatTurnRequestContext {
|
|
|
197
233
|
executionLimits?: ChatTurnExecutionLimits;
|
|
198
234
|
}
|
|
199
235
|
/** Define the arguments required to produce a chat turn with context and messaging details */
|
|
200
|
-
export interface ChatTurnProduceArgs<TContext> {
|
|
236
|
+
export interface ChatTurnProduceArgs<TContext, TResource = unknown> {
|
|
201
237
|
request: Request;
|
|
238
|
+
/** The opaque resource afterAuthorize started; the route disposes it at this turn's end. */
|
|
239
|
+
earlyResource?: Promise<TResource>;
|
|
240
|
+
/** This request's bound host continuation, when supplied to turn(request, ctx). */
|
|
241
|
+
waitUntil?: ChatTurnRequestContext['waitUntil'];
|
|
202
242
|
body: ChatTurnRequestPayload;
|
|
203
243
|
identity: ChatTurnIdentity;
|
|
204
244
|
context: TContext;
|
|
@@ -242,30 +282,34 @@ export interface ChatTurnProduceArgs<TContext> {
|
|
|
242
282
|
/** True when this turn's own insert created `userMessageId`, so no earlier
|
|
243
283
|
* attempt can have answered it; false when retry-dedup reused the row or no
|
|
244
284
|
* row was written. A gate that refuses an already-answered retry can skip
|
|
245
|
-
* its read when this is true. Already false for a reused row at
|
|
246
|
-
*
|
|
285
|
+
* its read when this is true. Already false for a reused row at backend
|
|
286
|
+
* selection and lock acquisition; undefined for a fresh row until insertion. */
|
|
247
287
|
userMessageInserted?: boolean;
|
|
248
288
|
/**
|
|
249
289
|
* Transfer completion ownership to a product-owned durable runner. The
|
|
250
|
-
* product MUST
|
|
251
|
-
*
|
|
252
|
-
*
|
|
290
|
+
* product MUST register that runner durably, then await this callback before
|
|
291
|
+
* dispatching native execution. When timing transfer is used, persist the initial
|
|
292
|
+
* timingFacts snapshot on the existing admission before registering that runner.
|
|
293
|
+
* A callback rejection after registration retains durable ownership. Once it
|
|
294
|
+
* resolves, this route remains a live event projector only: it no longer writes or settles the assistant
|
|
253
295
|
* transcript, lifecycle hooks, lock, or replay-buffer status.
|
|
254
296
|
*
|
|
255
297
|
* Optional for source compatibility with synthetic producers. Every
|
|
256
298
|
* `createChatTurnRoutes` invocation supplies it before `produce` runs.
|
|
257
299
|
*/
|
|
258
|
-
handoffCompletion?: () => Promise<void>;
|
|
300
|
+
handoffCompletion?: (options?: NativeCompletionHandoffOptions) => Promise<void>;
|
|
301
|
+
/** Serializable observations only; snapshots never predict events after handoff. */
|
|
302
|
+
timingFacts?: () => NativeTurnTimingFacts;
|
|
259
303
|
/** What the route's `prefetch` read for this turn's admitted identity; set only when `prefetch` is configured. */
|
|
260
304
|
prefetched?: Promise<unknown>;
|
|
261
|
-
/** Set
|
|
305
|
+
/** Set for a fresh Router backend, which starts before the lock. Resolves to the final args (with
|
|
262
306
|
* `userMessageId`, after `contextGate` and `beforeTurn`) once the admission chain commits, or rejects with
|
|
263
307
|
* `TurnAdmissionRefusedError` when the turn is refused. Start the model call at once; write nothing durable,
|
|
264
308
|
* and read nothing `beforeTurn` adds to `context`, until it resolves. */
|
|
265
|
-
admitted?: Promise<ChatTurnProduceArgs<TContext>>;
|
|
266
|
-
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
309
|
+
admitted?: Promise<ChatTurnProduceArgs<TContext, TResource>>;
|
|
310
|
+
/** Aborts on confirmed product Stop/supersession when `cancel` is configured. A fresh Router backend also
|
|
311
|
+
* aborts on admission refusal or when the route stops reading it. Pass it to the model transport;
|
|
312
|
+
* Router/Sandbox option data binds it automatically. Early Router handoff waits for `admitted`. */
|
|
269
313
|
admissionSignal?: AbortSignal;
|
|
270
314
|
}
|
|
271
315
|
/** One event as it crosses the route: the producer's own vocabulary, or an
|
|
@@ -326,8 +370,8 @@ export type ChatTurnLockResult = {
|
|
|
326
370
|
/** Async acquire/release wrapped around the turn. `acquire` runs before any
|
|
327
371
|
* side effect; `release` runs once when the turn settles — including on a
|
|
328
372
|
* short-circuit or a throw. */
|
|
329
|
-
export interface ChatTurnLock<TContext> {
|
|
330
|
-
acquire(args: ChatTurnProduceArgs<TContext>): ChatTurnLockResult | Promise<ChatTurnLockResult>;
|
|
373
|
+
export interface ChatTurnLock<TContext, TResource = unknown> {
|
|
374
|
+
acquire(args: ChatTurnProduceArgs<TContext, TResource>): ChatTurnLockResult | Promise<ChatTurnLockResult>;
|
|
331
375
|
release(handle: unknown): void | Promise<void>;
|
|
332
376
|
/** Bring up whatever `acquire` will talk to, as soon as the turn is admitted
|
|
333
377
|
* and while `prepareTurn` and the history read run. Never awaited; a failure
|
|
@@ -424,6 +468,8 @@ export interface ChatTurnCompleteInput<TContext> {
|
|
|
424
468
|
finalText: string;
|
|
425
469
|
context: TContext;
|
|
426
470
|
failed: boolean;
|
|
471
|
+
/** Final producer receipt, when it reports usage; absent counts remain unknown. */
|
|
472
|
+
usage?: ChatTurnUsage;
|
|
427
473
|
failureReason?: string;
|
|
428
474
|
/** Typed reason of a failed turn, when its terminal error event names one. */
|
|
429
475
|
failureCode?: string;
|
|
@@ -462,18 +508,17 @@ export interface ChatTurnTraceOptions<TContext> {
|
|
|
462
508
|
}): TurnSpanAttrs;
|
|
463
509
|
}
|
|
464
510
|
/** Define options to configure chat turn routes including authorization, storage, and event buffering */
|
|
465
|
-
export interface CreateChatTurnRoutesOptions<TContext = void,
|
|
511
|
+
export interface CreateChatTurnRoutesOptions<TContext = void, TResource = unknown> {
|
|
466
512
|
/** Names the product in `deriveExecutionId` so retries land on the same
|
|
467
513
|
* substrate execution. */
|
|
468
514
|
projectId: string;
|
|
469
515
|
/** Authenticate + authorize the caller for a turn or a replay. The only
|
|
470
516
|
* product-supplied access step: session auth, thread/workspace access,
|
|
471
517
|
* seat/balance gates, rate limits all live here. */
|
|
472
|
-
authorize(args: ChatTurnAuthorizeArgs
|
|
473
|
-
/**
|
|
474
|
-
*
|
|
475
|
-
|
|
476
|
-
createThread?: ChatTurnCreateThreadOptions<TThread>;
|
|
518
|
+
authorize(args: ChatTurnAuthorizeArgs): Promise<ChatTurnAuthorization<TContext>>;
|
|
519
|
+
/** Resolve the exact authenticated stop target. Without this capability,
|
|
520
|
+
* cancel returns 405 before parsing or authorization. */
|
|
521
|
+
cancel?: ChatTurnCancelOptions<TContext>;
|
|
477
522
|
/** Read the turn's prompt inputs (profile, knowledge, catalogs) while `authorize` finishes: started when `authorize`
|
|
478
523
|
* calls `args.identified`, or once it allows the turn. Read only: no writes, nothing paid or external beyond what
|
|
479
524
|
* the verified identity allows. `produce` receives the result as `args.prefetched` only for an allowed turn of that
|
|
@@ -491,20 +536,21 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TThread = unknown>
|
|
|
491
536
|
prepareTurn?(args: ChatTurnPrepareArgs<TContext>): ChatTurnPrepareResult | Promise<ChatTurnPrepareResult>;
|
|
492
537
|
/** Start the work this turn will need, the moment `authorize` and `modelPolicy` have admitted it, so that work overlaps
|
|
493
538
|
* `prepareTurn`, the history read, the lock, the history recheck and the user-row insert instead of following them: the
|
|
494
|
-
* workspace box, the keys a producer mints, a session ensure.
|
|
495
|
-
*
|
|
539
|
+
* workspace box, the keys a producer mints, a session ensure. Return `{resource, dispose}` to hand the route an
|
|
540
|
+
* opaque acquisition promise: `produce` receives it as `args.earlyResource`, and the route disposes the value once
|
|
541
|
+
* when the turn ends, including a refusal before acquisition resolves. Acquisition owns partial-failure cleanup.
|
|
496
542
|
*
|
|
497
543
|
* Admission has not finished, so the seam must start only work that the turn's refusal leaves harmless: a held lock
|
|
498
544
|
* (409), a changed history, a gate's answer or a throw all end the turn without `produce`, and the work already
|
|
499
545
|
* started runs on. Start nothing a second request for the same thread could not safely repeat, and nothing that
|
|
500
546
|
* writes product state.
|
|
501
547
|
*
|
|
502
|
-
* Not awaited, and it must return synchronously: start the work and return, never wait for it. It may return a
|
|
503
|
-
* function; the route calls it once, only if the turn ends without `produce` (the chain above, or a throw before the
|
|
548
|
+
* Not awaited, and it must return synchronously: start the work and return, never wait for it. It may return a
|
|
549
|
+
* legacy release function; the route calls it once, only if the turn ends without `produce` (the chain above, or a throw before the
|
|
504
550
|
* engine took the turn), so a product frees credentials or leases the unused work holds. Once `produce` runs, the
|
|
505
551
|
* work belongs to `produce` and the route never calls the release. A throw is logged and the turn goes on without
|
|
506
552
|
* the early work. Omit → nothing starts early. */
|
|
507
|
-
afterAuthorize?(args: ChatTurnAfterAuthorizeArgs<TContext>): void | ChatTurnAfterAuthorizeRelease
|
|
553
|
+
afterAuthorize?(args: ChatTurnAfterAuthorizeArgs<TContext>): void | ChatTurnAfterAuthorizeRelease<TResource>;
|
|
508
554
|
/** Product admission policy over the model a turn NAMES. When the request
|
|
509
555
|
* body carries a `model`, it is checked here — after `authorize`, before
|
|
510
556
|
* the conversation loads, the lock is taken, or any row is written — and a
|
|
@@ -524,38 +570,35 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TThread = unknown>
|
|
|
524
570
|
* `createDurableObjectTurnEventStore(env.TURN_STREAM_DO)` in production,
|
|
525
571
|
* `createMemoryTurnEventStore()` in tests). Wired by default — every turn
|
|
526
572
|
* is buffered and replayable. */
|
|
527
|
-
turnStore: TurnEventStore
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
*
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
* passes. `beforeTurn` may still mutate `context` but must not return a patch, since the prompt has already been
|
|
543
|
-
* sent; a patch throws. Omit → `produce` runs after admission.
|
|
573
|
+
turnStore: TurnEventStore | {
|
|
574
|
+
db: D1LikeForTurns;
|
|
575
|
+
lock: {
|
|
576
|
+
namespace: TurnStreamNamespaceLike;
|
|
577
|
+
auth: TurnStreamAuth;
|
|
578
|
+
};
|
|
579
|
+
};
|
|
580
|
+
/** Build the turn stream after admission, or declare its execution backends.
|
|
581
|
+
* Router calls start before the lock by default, after authorize has verified
|
|
582
|
+
* identity/access. The model service enforces spend before paying a provider.
|
|
583
|
+
* Output stays buffered until admission commits; a refusal cancels the call.
|
|
584
|
+
* Return Router/Hybrid producer options to bind tracing, cancellation and
|
|
585
|
+
* tool admission automatically. A custom Router producer must pass
|
|
586
|
+
* admissionSignal and await admitted before tools or durable effects.
|
|
587
|
+
* beforeTurn cannot patch a Router prompt already sent.
|
|
544
588
|
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
when?: (args: ChatTurnProduceArgs<TContext>) => boolean;
|
|
589
|
+
* when selects the Router backend (default true); false selects sandbox,
|
|
590
|
+
* which always starts after admission. A function also starts after admission.
|
|
591
|
+
* Reused user rows always finish admission before either backend starts. */
|
|
592
|
+
produce: ((args: ChatTurnProduceArgs<TContext, TResource>) => ChatTurnRouteProducer | Promise<ChatTurnRouteProducer>) | {
|
|
593
|
+
router(args: ChatTurnProduceArgs<TContext, TResource>): ChatTurnRouterBackend | Promise<ChatTurnRouterBackend>;
|
|
594
|
+
when?(args: ChatTurnProduceArgs<TContext, TResource>): boolean;
|
|
595
|
+
sandbox?(args: ChatTurnProduceArgs<TContext, TResource>): ChatTurnSandboxBackend | Promise<ChatTurnSandboxBackend>;
|
|
553
596
|
};
|
|
554
|
-
/** Single-flight lock acquired before
|
|
597
|
+
/** Single-flight lock acquired before durable effects and released once when
|
|
555
598
|
* the turn settles (including short-circuit/throw). `/turn-stream`'s
|
|
556
599
|
* `createDurableTurnLock` is the shared DO-backed implementation. Omit →
|
|
557
600
|
* no lock. */
|
|
558
|
-
turnLock?: ChatTurnLock<TContext>;
|
|
601
|
+
turnLock?: ChatTurnLock<TContext, TResource>;
|
|
559
602
|
/** Pre-turn readiness gate that can short-circuit with a product `Response`
|
|
560
603
|
* before the producer runs (the user row is already persisted). Runs after
|
|
561
604
|
* `turnLock.acquire`, before `beforeTurn`. Omit → always proceed.
|
|
@@ -565,7 +608,8 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TThread = unknown>
|
|
|
565
608
|
* insert was suppressed with nothing to reuse. (`turnLock.acquire` is the
|
|
566
609
|
* only seam that sees it `undefined`.)
|
|
567
610
|
* - `{proceed:false}` releases the lock and returns the product's `Response`
|
|
568
|
-
* verbatim: `beforeTurn` and
|
|
611
|
+
* verbatim: `beforeTurn` and an ordinary/Sandbox producer never run; an
|
|
612
|
+
* early Router call is cancelled and discarded. No assistant row is
|
|
569
613
|
* written, and the user row already inserted is KEPT — a real user turn
|
|
570
614
|
* whose assistant side is the gate's own response.
|
|
571
615
|
* - `{proceed:'handoff'}` releases the route-acquired lock, returns the
|
|
@@ -576,7 +620,7 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TThread = unknown>
|
|
|
576
620
|
* doubles as the one place that runs after the user row exists and before
|
|
577
621
|
* the producer, which is where per-turn analytics and readiness
|
|
578
622
|
* precomputation belong. A gate that never gates is a valid consumer. */
|
|
579
|
-
contextGate?(args: ChatTurnProduceArgs<TContext>): ChatTurnGateResult | Promise<ChatTurnGateResult>;
|
|
623
|
+
contextGate?(args: ChatTurnProduceArgs<TContext, TResource>): ChatTurnGateResult | Promise<ChatTurnGateResult>;
|
|
580
624
|
/** Observe the assembled producer input and optionally augment it (rewrite
|
|
581
625
|
* the prompt / prior messages) before the producer runs. Omit → no change.
|
|
582
626
|
*
|
|
@@ -591,7 +635,7 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TThread = unknown>
|
|
|
591
635
|
* `lifecycle.onTurnStart`, so a throw here fires no terminal lifecycle hook —
|
|
592
636
|
* the span never opened. Telemetry for a failure in this seam belongs in the
|
|
593
637
|
* seam, not in `lifecycle`. */
|
|
594
|
-
beforeTurn?(args: ChatTurnProduceArgs<TContext>,
|
|
638
|
+
beforeTurn?(args: ChatTurnProduceArgs<TContext, TResource>,
|
|
595
639
|
/** `span` is the `admission.before_turn` span: the parent for a product's own child spans. */
|
|
596
640
|
seam?: {
|
|
597
641
|
span: TurnSpan;
|
|
@@ -621,34 +665,6 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TThread = unknown>
|
|
|
621
665
|
/** Pre-persist transform of the final text (e.g. `/redact`'s `redactPII`).
|
|
622
666
|
* Live stream is never altered. */
|
|
623
667
|
transformFinalText?(text: string): string | Promise<string>;
|
|
624
|
-
/** Incremental persistence of the assistant row WHILE the turn streams
|
|
625
|
-
* (`./draft-persistence`), so a viewer arriving mid-run is served from
|
|
626
|
-
* durable storage instead of the streaming gateway's hot event buffer.
|
|
627
|
-
*
|
|
628
|
-
* This is what lets the gateway keep that hot buffer SHORT at scale: its
|
|
629
|
-
* Redis footprint is one sorted set per session refreshed on every push, so
|
|
630
|
-
* memory grows LINEARLY with `ttl x concurrent sessions`. Stretching the
|
|
631
|
-
* TTL to cover late viewers buys memory proportional to the increase and
|
|
632
|
-
* still serves nothing past the new horizon. The buffer stays a reconnect
|
|
633
|
-
* window; durable storage — kept at most one cadence interval stale by this
|
|
634
|
-
* option — is the history tier.
|
|
635
|
-
*
|
|
636
|
-
* Enabled by DEFAULT whenever `store.updateMessage` exists (every
|
|
637
|
-
* `/chat-store` consumer); a store without it keeps today's exact
|
|
638
|
-
* single-write behavior. Pass `false` to opt out, or an object to tune the
|
|
639
|
-
* cadence. Wiring it against a store that cannot patch rows throws at route
|
|
640
|
-
* construction rather than silently no-op'ing. */
|
|
641
|
-
incrementalPersistence?: false | DraftPersistenceTuning;
|
|
642
|
-
/** Deterministic durable row id for this turn's assistant message. Default
|
|
643
|
-
* `assistant:<executionId>` — already stable across retries because
|
|
644
|
-
* `deriveExecutionId` is. Override only if the product's message ids have a
|
|
645
|
-
* format constraint; it MUST stay deterministic per turn or a re-entered
|
|
646
|
-
* turn will duplicate its row instead of converging. */
|
|
647
|
-
draftMessageId?(args: {
|
|
648
|
-
identity: ChatTurnIdentity;
|
|
649
|
-
executionId: string;
|
|
650
|
-
threadId: string;
|
|
651
|
-
}): string;
|
|
652
668
|
/** Post-processing after a turn settles (billing, titles, audit). Fires with
|
|
653
669
|
* `failed:true` + `failureReason` when the turn carried a terminal error
|
|
654
670
|
* event (model 402 / rate-limit / server error) instead of a clean
|
|
@@ -679,17 +695,6 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TThread = unknown>
|
|
|
679
695
|
/** Compose the interaction-answer endpoints (`/interactions`). Omit when the
|
|
680
696
|
* product has no sidecar ask channel. */
|
|
681
697
|
interactions?: InteractionAnswerRouteOptions;
|
|
682
|
-
/** Byte budget for inline prompt parts. Default `INLINE_PARTS_MAX_BYTES`. */
|
|
683
|
-
maxInlinePartBytes?: number;
|
|
684
|
-
/** Per-flush coalescer for the turn buffer. Default `coalesceDeltas` (this
|
|
685
|
-
* assembly streams the client vocabulary's `{type:'text'|'reasoning',
|
|
686
|
-
* text}` lines, which it merges). A producer streaming raw
|
|
687
|
-
* `message.part.updated` events passes `coalesceChatStreamEvents`. */
|
|
688
|
-
coalesceTurnEvents?: (events: unknown[]) => unknown[];
|
|
689
|
-
replay?: {
|
|
690
|
-
pollMs?: number;
|
|
691
|
-
timeoutMs?: number;
|
|
692
|
-
};
|
|
693
698
|
/** Text for the route's own stages on a progress-first stream (`./turn-progress`).
|
|
694
699
|
* Omitted stages keep `TURN_ROUTE_PHASE_MESSAGES`. */
|
|
695
700
|
progressMessages?: Partial<Record<TurnRoutePhase, string>>;
|
|
@@ -740,6 +745,8 @@ export interface ChatTurnRoutes {
|
|
|
740
745
|
* `TURN_PROGRESS_HEADER` gets a progress-first stream once it is
|
|
741
746
|
* authorized (`./turn-progress`). */
|
|
742
747
|
turn(request: Request, ctx?: ChatTurnRequestContext): Promise<Response>;
|
|
748
|
+
/** POST — stop the exact authorized execution; unavailable unless configured. */
|
|
749
|
+
cancel(request: Request): Promise<Response>;
|
|
743
750
|
/** GET — replay a buffered turn from `?fromSeq=` (0 = everything), then
|
|
744
751
|
* follow it live until it completes. */
|
|
745
752
|
replay(request: Request, params: {
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
* body parse/validate → `/web` `parseJsonObjectBody` + `./wire`
|
|
6
6
|
* turn identity → `/stream` `resolveChatTurn` + agent-runtime `/durable`
|
|
7
7
|
* `deriveExecutionId`
|
|
8
|
-
* producer →
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* producer → function after admission, or declared Router
|
|
9
|
+
* and Sandbox backends. Router model calls overlap
|
|
10
|
+
* admission by default; tools wait for commit.
|
|
11
11
|
* turn engine → agent-runtime `/durable` `handleChatTurn` (verbatim)
|
|
12
12
|
* durability → `/stream` turn-buffer tap, wired BY DEFAULT
|
|
13
13
|
* (tee + drain keeps the turn running after a
|
|
@@ -53,4 +53,4 @@
|
|
|
53
53
|
import { type ChatTurnRoutes, type CreateChatTurnRoutesOptions } from './turn-route-contracts';
|
|
54
54
|
export * from './turn-route-contracts';
|
|
55
55
|
/** Build chat turn routes to handle and validate incoming chat requests with optional logging */
|
|
56
|
-
export declare function createChatTurnRoutes<TContext = void,
|
|
56
|
+
export declare function createChatTurnRoutes<TContext = void, TResource = unknown>(input: CreateChatTurnRoutesOptions<TContext, TResource>): ChatTurnRoutes;
|
|
@@ -158,13 +158,6 @@ export interface ChatTurnRequestPayload {
|
|
|
158
158
|
turnId?: string;
|
|
159
159
|
/** New token for a deliberate retry; reuse it for transport retries of that attempt. */
|
|
160
160
|
retryAttemptId?: string;
|
|
161
|
-
/** A new chat's first turn: the thread to create, `id` equal to `threadId` (`mintThreadId` in `/web-react`).
|
|
162
|
-
* Send it on every turn of a minted thread until one is admitted, so a refused first turn retries as a creating one;
|
|
163
|
-
* once the thread exists the server finds it first and creates nothing. */
|
|
164
|
-
newThread?: {
|
|
165
|
-
id: string;
|
|
166
|
-
title?: string;
|
|
167
|
-
};
|
|
168
161
|
[key: string]: unknown;
|
|
169
162
|
}
|
|
170
163
|
/** `fetch` init for the turn route — the one place the client wire shape is
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - `createChatTables` (./schema) — the table factory; per-divergence
|
|
9
9
|
* rationale lives in its docblock.
|
|
10
10
|
* - `createChatStore` (./store) — list/get/create/rename/pin/delete/bulk-
|
|
11
|
-
* delete threads +
|
|
11
|
+
* delete threads + message CRUD and declared-metadata merge patches, with access control as an injected
|
|
12
12
|
* callback seam.
|
|
13
13
|
* - `ChatMessagePart` (./parts) — the canonical stored `parts` vocabulary both
|
|
14
14
|
* transport lanes (harness `message.part.updated`, router openai-compat)
|