@tangle-network/agent-app 3.0.32 → 3.0.33

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.
@@ -1,3 +1,5 @@
1
+ /** The template's backend assembly. Products supply policy; routes own lane selection. */
2
+ import type { SandboxInstance } from '@tangle-network/sandbox';
1
3
  import type { Harness } from '../harness/index.js';
2
4
  import type { SandboxRuntimeConfig } from '../sandbox/configuration.js';
3
5
  import type { ProviderResolutionConfig } from '../sandbox/model.js';
@@ -38,6 +40,16 @@ export interface ChatTurnDefaultBackend<TContext = unknown, TResource = unknown>
38
40
  providers?: Readonly<Record<string, ProviderResolutionConfig | ((args: ChatTurnProduceArgs<TContext, TResource>) => ProviderResolutionConfig | Promise<ProviderResolutionConfig>)>>;
39
41
  /** Explicitly keep every turn on the sandbox lane. */
40
42
  router?: false;
43
+ /** A durable owner for a sandbox turn, so its answer outlives the request (a closed tab, a lost connection): called
44
+ * after the box is resolved and the turn lock is held, before the prompt is dispatched, with the session and execution the dispatch uses.
45
+ * It registers the owner (preset-cloudflare `createNativeCompletionOwner`, as creative-agent does) and then calls
46
+ * `args.handoffCompletion()`. A throw fails the turn before any native work starts. */
47
+ completionOwner?(input: {
48
+ args: ChatTurnProduceArgs<TContext, TResource>;
49
+ box: SandboxInstance;
50
+ sessionId: string;
51
+ executionId: string;
52
+ }): Promise<void>;
41
53
  /** Who pays for the Router model call: the turn's key, or a per-turn lease (`createDelegatedTurnKeys`) the
42
54
  * producer revokes when the turn settles. Default `TANGLE_API_KEY`. Started before the turn lock, like the call. */
43
55
  funding?(args: ChatTurnProduceArgs<TContext, TResource>): RouterTurnKey | Promise<RouterTurnKey>;
@@ -88,7 +88,7 @@ import {
88
88
  turnPromptText,
89
89
  valueRedactor,
90
90
  withProducerStream
91
- } from "../chunk-S4QFJSLR.js";
91
+ } from "../chunk-KNVFMTC2.js";
92
92
  import "../chunk-SBHFMDR7.js";
93
93
  import "../chunk-H6BRJU5M.js";
94
94
  import "../chunk-4DBHC63F.js";
@@ -334,6 +334,10 @@ export interface ChatTurnProduceArgs<TContext, TResource = unknown> {
334
334
  * `createChatTurnRoutes` invocation supplies it before `produce` runs.
335
335
  */
336
336
  handoffCompletion?: (options?: NativeCompletionHandoffOptions) => Promise<void>;
337
+ /** The route's turn-lock handle, read when needed: undefined before the lock is acquired and without a `turnLock`.
338
+ * A durable completion owner releases it when it settles the turn after `handoffCompletion`
339
+ * (preset-cloudflare `NativeCompletionPayload.lock`). */
340
+ lock?: () => unknown;
337
341
  /** Serializable observations only; snapshots never predict events after handoff. */
338
342
  timingFacts?: () => NativeTurnTimingFacts;
339
343
  /** What the route's `prefetch` read for this turn's admitted identity; set only when `prefetch` is configured. */
@@ -2940,6 +2940,7 @@ async function runAdmittedTurn({
2940
2940
  }));
2941
2941
  const { handoffCompletion, releaseLock } = createChatTurnOwnership(state, log, turnStreamId, options);
2942
2942
  let produceArgs = {
2943
+ lock: () => state.lockHandle,
2943
2944
  request,
2944
2945
  waitUntil: ctx?.waitUntil,
2945
2946
  earlyResource: early.resource,
@@ -3691,17 +3692,23 @@ function createDefaultChatBackend(projectId, env, config, bindings, modelPolicy)
3691
3692
  const own = config.providers?.[model];
3692
3693
  const provider = typeof own === "function" ? await own(args) : own;
3693
3694
  const box = await acquire(args);
3695
+ const completionOwner = config.completionOwner;
3696
+ const dispatch = (signal) => streamSandboxPrompt(
3697
+ provider ? { ...shell, provider } : shell,
3698
+ box,
3699
+ normalizeChatPromptForSandbox(args.prompt),
3700
+ { ...promptOptions(args), model, signal }
3701
+ );
3694
3702
  return {
3695
3703
  model,
3696
3704
  modelPolicy,
3697
3705
  modelFailover: false,
3698
3706
  errorNotice: true,
3699
- openEvents: ({ signal }) => streamSandboxPrompt(
3700
- provider ? { ...shell, provider } : shell,
3701
- box,
3702
- normalizeChatPromptForSandbox(args.prompt),
3703
- { ...promptOptions(args), model, signal }
3704
- )
3707
+ // The route pulls openEvents only after the turn lock, so the durable owner registers holding it.
3708
+ openEvents: ({ signal }) => completionOwner ? (async function* () {
3709
+ await completionOwner({ args, box, sessionId: args.identity.sessionId, executionId: args.executionId });
3710
+ yield* dispatch(signal);
3711
+ })() : dispatch(signal)
3705
3712
  };
3706
3713
  }
3707
3714
  };
@@ -4220,4 +4227,4 @@ export {
4220
4227
  createChatTurnRoutes,
4221
4228
  streamChatRouteAsSandboxEvents
4222
4229
  };
4223
- //# sourceMappingURL=chunk-S4QFJSLR.js.map
4230
+ //# sourceMappingURL=chunk-KNVFMTC2.js.map