@theoremjs/agents 0.3.4-next.727.g8a08b91 → 0.3.4-next.731.g189a95d

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.
@@ -163,6 +163,14 @@ export const REQUEST_FIELDS = {
163
163
  doc: 'Runs before each agent tool call, to shape the request to the called agent or to refuse the call.',
164
164
  unset: 'The agent runs on what the model sent',
165
165
  },
166
+ 'runTurn.onEvent': {
167
+ type: '(event: TurnEvent) => void | Promise<void>',
168
+ doc: "Reads each of the turn's events as it leaves runTurn, before the caller gets it. A throw ends the turn with that error.",
169
+ },
170
+ 'runTurn.answersHeldCall': {
171
+ type: 'true',
172
+ doc: "Set on the turn TurnWiring.prepare is given for a held call that goes on once answered: no model runs, and input is the paused turn's.",
173
+ },
166
174
  };
167
175
  /** The catalog of the exports a builder calls by name, keyed by that name. */
168
176
  export const API_EXPORTS = {
@@ -4,10 +4,7 @@ import type { KernelRegistry } from '../../registry/kernel-registry.js';
4
4
  import type { AgentCaller } from '../../tools/agent.js';
5
5
  import type { InvokeToolRequest } from '../../tools/types.js';
6
6
  import type { CompactHistoryRequest, CompactionResult, SessionRequest, TurnEvent, TurnModel, TurnRequest } from '../../types.js';
7
- /**
8
- * The trace record is written however the turn ends, including when the host stops reading early;
9
- * spans still open then close as `ERROR` / `unclosed`.
10
- */
7
+ /** A turn's events, each read by `req.onEvent` before the caller gets it. */
11
8
  declare function runTurnInRegistry(registry: KernelRegistry, req: TurnRequest, provider: TurnModel, sinkOverride?: TraceSink): AsyncGenerator<TurnEvent>;
12
9
  /**
13
10
  * A host's own call's agent tools: the host's provider, or the one `onAgentCall`
@@ -380,7 +380,7 @@ async function* runTracedTurn(ctx, provider) {
380
380
  * The trace record is written however the turn ends, including when the host stops reading early;
381
381
  * spans still open then close as `ERROR` / `unclosed`.
382
382
  */
383
- async function* runTurnInRegistry(registry, req, provider, sinkOverride) {
383
+ async function* tracedTurnInRegistry(registry, req, provider, sinkOverride) {
384
384
  const tree = startTrace(`invoke_agent ${req.profile}`, {
385
385
  ...turnSpanOptions(req),
386
386
  ...(req.traceparent ? { traceparent: req.traceparent } : {}),
@@ -454,6 +454,13 @@ async function* emitCancelledDoneAfterAbort(ctx) {
454
454
  yield* emitObservedStage(ctx, stageEv, profile);
455
455
  }
456
456
  }
457
+ /** A turn's events, each read by `req.onEvent` before the caller gets it. */
458
+ async function* runTurnInRegistry(registry, req, provider, sinkOverride) {
459
+ for await (const event of tracedTurnInRegistry(registry, req, provider, sinkOverride)) {
460
+ await req.onEvent?.(event);
461
+ yield event;
462
+ }
463
+ }
457
464
  async function* runTurnBody(ctx, provider) {
458
465
  assertBackgroundCoordinator(ctx.req.background, ctx.req.execution);
459
466
  const checkStart = performance.now();
@@ -839,6 +839,17 @@ export interface TurnRequest extends AgentRunRequest {
839
839
  * so an answered call does not give the reply its steps back. Read `done.steps`.
840
840
  */
841
841
  stepsTaken?: number;
842
+ /**
843
+ * Reads each of this turn's events as it leaves `runTurn`, before the caller gets it. The app
844
+ * sets it in `TurnWiring.prepare` to act on a turn someone else runs, such as saving the reply
845
+ * when `done` arrives. A throw ends the turn with that error. `invokeTool` does not call it.
846
+ */
847
+ onEvent?: (event: TurnEvent) => void | Promise<void>;
848
+ /**
849
+ * Set on the turn `TurnWiring.prepare` is given for a held call that goes on once answered. No
850
+ * model runs and `input` is the paused turn's, so it is not a new message.
851
+ */
852
+ answersHeldCall?: true;
842
853
  input?: TurnInput;
843
854
  }
844
855
  /**
@@ -854,8 +865,8 @@ export interface TurnWiring {
854
865
  /**
855
866
  * The turn the application runs, from the one it is given: the profile, the input with the
856
867
  * conversation's history, and the application's context as `host`. Set what the application
857
- * sets per turn, such as `system`, `credentials`, `onStage`, `onAgentCall`, `path` and
858
- * `metadata`. Text, image and speech turns run through it.
868
+ * sets per turn, such as `system`, `credentials`, `onStage`, `onEvent`, `onAgentCall`, `path`
869
+ * and `metadata`. Text, image and speech turns run through it.
859
870
  */
860
871
  prepare?: (turn: NormalizedTurnRequest) => TurnRequest | Promise<TurnRequest>;
861
872
  }
@@ -83,10 +83,17 @@ export function wireMessageContent(parts) {
83
83
  if (part.type === 'audio') {
84
84
  return wireAudioPart(part);
85
85
  }
86
+ if (part.type === 'video') {
87
+ // why: a video sent as a `file` part is refused; OpenRouter reads it only as `video_url`.
88
+ return {
89
+ type: 'video_url',
90
+ video_url: { url: `data:${part.mimeType};base64,${part.data}` },
91
+ };
92
+ }
86
93
  return {
87
94
  type: 'file',
88
95
  file: {
89
- filename: part.type === 'video' ? 'clip.bin' : 'document.bin',
96
+ filename: 'document.bin',
90
97
  file_data: `data:${part.mimeType};base64,${part.data}`,
91
98
  },
92
99
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.4-next.727.g8a08b91",
3
+ "version": "0.3.4-next.731.g189a95d",
4
4
  "description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
5
5
  "keywords": [
6
6
  "agent",