@theokit/agents 0.42.0 → 0.43.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.
@@ -414,6 +414,19 @@ interface DoneEvent {
414
414
  /** Total cost in USD for this agent run (EC-2: added for budget tracking). */
415
415
  cost?: number;
416
416
  }
417
+ /**
418
+ * Per-turn usage the translator attaches to the ai-sdk `finish` chunk's `messageMetadata`, so it
419
+ * lands on the reconstructed assistant `UIMessage.metadata` on the client (via `readUIMessageStream`)
420
+ * with NO extra header/store wiring. It is the seam that lets a surface (a TUI status bar, a web
421
+ * cost meter) show real tokens/cost for the turn it just streamed. Mirrors `DoneEvent`'s usage/cost
422
+ * (the authoritative totals the SDK reports at turn end) plus the wall-clock `durationMs`.
423
+ */
424
+ interface AgentTurnMetadata {
425
+ usage: DoneEvent['usage'];
426
+ /** Total cost in USD for the turn (present iff the SDK reported it). */
427
+ cost?: number;
428
+ durationMs: number;
429
+ }
417
430
  /** Agent run started. */
418
431
  interface RunStartedEvent {
419
432
  type: 'run_started';
@@ -1643,4 +1656,4 @@ declare function agentsPlugin(opts: AgentsPluginOptions): {
1643
1656
  register(app: PluginApp): void;
1644
1657
  };
1645
1658
 
1646
- export { type McpRequestContext as $, type AgentManifestEntry as A, type BackgroundDelegation as B, type CompiledAgentOptions as C, type DelegationResult as D, type CompiledContextWindow as E, type ContextualTool as F, DEFAULT_MAX_ITERATIONS as G, type DefineAgentConfig as H, type DelegateFn as I, type DelegateOptions as J, DelegationError as K, type LoopStrategy as L, type DoneEvent as M, type ErrorEvent as N, type FileEditEvent as O, type HitlDecision as P, type InferAgentInput as Q, type ReflectionStrategy as R, type StreamEvent as S, type InferAgentToolNames as T, type IterationEvent as U, type LLMCallContext as V, type LoopFinishReason as W, type LoopOutcome as X, type LoopStrategyConfig as Y, type McpApprovalSpec as Z, type McpRegistryConfig as _, type CompiledTool as a, resolveLoopStrategy as a$, type McpSelection as a0, type PartialToolCallEvent as a1, type ProcessInputContext as a2, type ReflectionContext as a3, type ReflectionResult as a4, type ReflectionStrategyConfig as a5, type RunStartedEvent as a6, type ScoreVerdict as a7, type ScoredDelegation as a8, type Scorer as a9, createApiErrorHandler as aA, createSdkAgentStream as aB, createThinkTagExtractor as aC, createToolHooksPlugin as aD, delegate as aE, delegateBackground as aF, delegateWithScoring as aG, extractThinkTagStream as aH, generateAgentManifest as aI, generateAgentRoutes as aJ, isAgentContext as aK, isAgentDefinition as aL, isApprovalRequired as aM, isDone as aN, isError as aO, isPartialToolCall as aP, isTextDelta as aQ, isToolCall as aR, isToolResult as aS, ladderReflectionStrategy as aT, loopStrategyConfigSchema as aU, mcpRegistry as aV, mcpToolApprovals as aW, noopReflectionStrategy as aX, projectContextMetadataOnlyKnobs as aY, reflectionStrategyConfigSchema as aZ, resolveEnabledSkills as a_, type SdkMessage as aa, type Segment as ab, type SkillsRequestContext as ac, type SkillsSelection as ad, type StateUpdateEvent as ae, type TextDeltaEvent as af, type ThinkingEvent as ag, type ToolCallEvent as ah, type ToolCallVeto as ai, type ToolHooks as aj, type ToolHooksPlugin as ak, type ToolResultEvent as al, type ToolWalkResult as am, type ToolboxWalkResult as an, agent as ao, agentsPlugin as ap, buildModelSelection as aq, compileAgent as ar, compileAgentDefinition as as, compileAgentModule as at, compileContextWindow as au, compileProjectContext as av, compileSkills as aw, compileTools as ax, contextualTool as ay, createAgentExecutionContext as az, type RoundStreamFactory as b, resolveMcpServers as b0, runWithApiErrorHandling as b1, streamAgentResponse as b2, streamAgentUIMessages as b3, translateSdkEvent as b4, translateToUIMessageStream as b5, validateUniqueRoutes as b6, walkAgentMetadata as b7, AGENT_BRAND as c, type AfterToolCallContext as d, type AgentBuilder as e, type AgentDefinition as f, AgentDefinitionError as g, type AgentExecutionContext as h, type AgentManifest as i, type AgentManifestTool as j, type AgentRoute as k, type AgentRouteContext as l, type AgentRunInfo as m, type AgentStreamEvent as n, type AgentWalkResult as o, AgentWarningCode as p, type AgentsPluginOptions as q, type ApiErrorContext as r, type ApiErrorDecision as s, type ApiErrorPolicy as t, type ApprovalRequiredEvent as u, type ArtifactChunkEvent as v, type ArtifactStartEvent as w, type BeforeToolCallContext as x, BudgetExceededError as y, type CheckpointSavedEvent as z };
1659
+ export { type McpRegistryConfig as $, type AgentManifestEntry as A, type BackgroundDelegation as B, type CompiledAgentOptions as C, type DelegationResult as D, type CheckpointSavedEvent as E, type CompiledContextWindow as F, type ContextualTool as G, DEFAULT_MAX_ITERATIONS as H, type DefineAgentConfig as I, type DelegateFn as J, type DelegateOptions as K, type LoopStrategy as L, DelegationError as M, type DoneEvent as N, type ErrorEvent as O, type FileEditEvent as P, type HitlDecision as Q, type ReflectionStrategy as R, type StreamEvent as S, type InferAgentInput as T, type InferAgentToolNames as U, type IterationEvent as V, type LLMCallContext as W, type LoopFinishReason as X, type LoopOutcome as Y, type LoopStrategyConfig as Z, type McpApprovalSpec as _, type CompiledTool as a, resolveEnabledSkills as a$, type McpRequestContext as a0, type McpSelection as a1, type PartialToolCallEvent as a2, type ProcessInputContext as a3, type ReflectionContext as a4, type ReflectionResult as a5, type ReflectionStrategyConfig as a6, type RunStartedEvent as a7, type ScoreVerdict as a8, type ScoredDelegation as a9, createAgentExecutionContext as aA, createApiErrorHandler as aB, createSdkAgentStream as aC, createThinkTagExtractor as aD, createToolHooksPlugin as aE, delegate as aF, delegateBackground as aG, delegateWithScoring as aH, extractThinkTagStream as aI, generateAgentManifest as aJ, generateAgentRoutes as aK, isAgentContext as aL, isAgentDefinition as aM, isApprovalRequired as aN, isDone as aO, isError as aP, isPartialToolCall as aQ, isTextDelta as aR, isToolCall as aS, isToolResult as aT, ladderReflectionStrategy as aU, loopStrategyConfigSchema as aV, mcpRegistry as aW, mcpToolApprovals as aX, noopReflectionStrategy as aY, projectContextMetadataOnlyKnobs as aZ, reflectionStrategyConfigSchema as a_, type Scorer as aa, type SdkMessage as ab, type Segment as ac, type SkillsRequestContext as ad, type SkillsSelection as ae, type StateUpdateEvent as af, type TextDeltaEvent as ag, type ThinkingEvent as ah, type ToolCallEvent as ai, type ToolCallVeto as aj, type ToolHooks as ak, type ToolHooksPlugin as al, type ToolResultEvent as am, type ToolWalkResult as an, type ToolboxWalkResult as ao, agent as ap, agentsPlugin as aq, buildModelSelection as ar, compileAgent as as, compileAgentDefinition as at, compileAgentModule as au, compileContextWindow as av, compileProjectContext as aw, compileSkills as ax, compileTools as ay, contextualTool as az, type RoundStreamFactory as b, resolveLoopStrategy as b0, resolveMcpServers as b1, runWithApiErrorHandling as b2, streamAgentResponse as b3, streamAgentUIMessages as b4, translateSdkEvent as b5, translateToUIMessageStream as b6, validateUniqueRoutes as b7, walkAgentMetadata as b8, AGENT_BRAND as c, type AfterToolCallContext as d, type AgentBuilder as e, type AgentDefinition as f, AgentDefinitionError as g, type AgentExecutionContext as h, type AgentManifest as i, type AgentManifestTool as j, type AgentRoute as k, type AgentRouteContext as l, type AgentRunInfo as m, type AgentStreamEvent as n, type AgentTurnMetadata as o, type AgentWalkResult as p, AgentWarningCode as q, type AgentsPluginOptions as r, type ApiErrorContext as s, type ApiErrorDecision as t, type ApiErrorPolicy as u, type ApprovalRequiredEvent as v, type ArtifactChunkEvent as w, type ArtifactStartEvent as x, type BeforeToolCallContext as y, BudgetExceededError as z };
package/dist/bridge.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { c as AGENT_BRAND, d as AfterToolCallContext, e as AgentBuilder, f as AgentDefinition, g as AgentDefinitionError, h as AgentExecutionContext, i as AgentManifest, A as AgentManifestEntry, j as AgentManifestTool, k as AgentRoute, l as AgentRouteContext, m as AgentRunInfo, n as AgentStreamEvent, o as AgentWalkResult, p as AgentWarningCode, q as AgentsPluginOptions, r as ApiErrorContext, s as ApiErrorDecision, t as ApiErrorPolicy, u as ApprovalRequiredEvent, v as ArtifactChunkEvent, w as ArtifactStartEvent, B as BackgroundDelegation, x as BeforeToolCallContext, y as BudgetExceededError, z as CheckpointSavedEvent, C as CompiledAgentOptions, E as CompiledContextWindow, a as CompiledTool, F as ContextualTool, H as DefineAgentConfig, I as DelegateFn, J as DelegateOptions, K as DelegationError, D as DelegationResult, M as DoneEvent, N as ErrorEvent, O as FileEditEvent, Q as InferAgentInput, T as InferAgentToolNames, U as IterationEvent, V as LLMCallContext, Z as McpApprovalSpec, _ as McpRegistryConfig, $ as McpRequestContext, a0 as McpSelection, a1 as PartialToolCallEvent, a2 as ProcessInputContext, a6 as RunStartedEvent, a7 as ScoreVerdict, a8 as ScoredDelegation, a9 as Scorer, aa as SdkMessage, ab as Segment, ae as StateUpdateEvent, S as StreamEvent, af as TextDeltaEvent, ag as ThinkingEvent, ah as ToolCallEvent, ai as ToolCallVeto, aj as ToolHooks, ak as ToolHooksPlugin, al as ToolResultEvent, am as ToolWalkResult, an as ToolboxWalkResult, ao as agent, ap as agentsPlugin, aq as buildModelSelection, ar as compileAgent, as as compileAgentDefinition, at as compileAgentModule, au as compileContextWindow, av as compileProjectContext, aw as compileSkills, ax as compileTools, ay as contextualTool, az as createAgentExecutionContext, aA as createApiErrorHandler, aB as createSdkAgentStream, aC as createThinkTagExtractor, aD as createToolHooksPlugin, aE as delegate, aF as delegateBackground, aG as delegateWithScoring, aH as extractThinkTagStream, aI as generateAgentManifest, aJ as generateAgentRoutes, aK as isAgentContext, aL as isAgentDefinition, aM as isApprovalRequired, aN as isDone, aO as isError, aP as isPartialToolCall, aQ as isTextDelta, aR as isToolCall, aS as isToolResult, aV as mcpRegistry, aW as mcpToolApprovals, aY as projectContextMetadataOnlyKnobs, b0 as resolveMcpServers, b1 as runWithApiErrorHandling, b2 as streamAgentResponse, b3 as streamAgentUIMessages, b4 as translateSdkEvent, b5 as translateToUIMessageStream, b6 as validateUniqueRoutes, b7 as walkAgentMetadata } from './bridge-entry-CE3ie8rG.js';
1
+ export { c as AGENT_BRAND, d as AfterToolCallContext, e as AgentBuilder, f as AgentDefinition, g as AgentDefinitionError, h as AgentExecutionContext, i as AgentManifest, A as AgentManifestEntry, j as AgentManifestTool, k as AgentRoute, l as AgentRouteContext, m as AgentRunInfo, n as AgentStreamEvent, o as AgentTurnMetadata, p as AgentWalkResult, q as AgentWarningCode, r as AgentsPluginOptions, s as ApiErrorContext, t as ApiErrorDecision, u as ApiErrorPolicy, v as ApprovalRequiredEvent, w as ArtifactChunkEvent, x as ArtifactStartEvent, B as BackgroundDelegation, y as BeforeToolCallContext, z as BudgetExceededError, E as CheckpointSavedEvent, C as CompiledAgentOptions, F as CompiledContextWindow, a as CompiledTool, G as ContextualTool, I as DefineAgentConfig, J as DelegateFn, K as DelegateOptions, M as DelegationError, D as DelegationResult, N as DoneEvent, O as ErrorEvent, P as FileEditEvent, T as InferAgentInput, U as InferAgentToolNames, V as IterationEvent, W as LLMCallContext, _ as McpApprovalSpec, $ as McpRegistryConfig, a0 as McpRequestContext, a1 as McpSelection, a2 as PartialToolCallEvent, a3 as ProcessInputContext, a7 as RunStartedEvent, a8 as ScoreVerdict, a9 as ScoredDelegation, aa as Scorer, ab as SdkMessage, ac as Segment, af as StateUpdateEvent, S as StreamEvent, ag as TextDeltaEvent, ah as ThinkingEvent, ai as ToolCallEvent, aj as ToolCallVeto, ak as ToolHooks, al as ToolHooksPlugin, am as ToolResultEvent, an as ToolWalkResult, ao as ToolboxWalkResult, ap as agent, aq as agentsPlugin, ar as buildModelSelection, as as compileAgent, at as compileAgentDefinition, au as compileAgentModule, av as compileContextWindow, aw as compileProjectContext, ax as compileSkills, ay as compileTools, az as contextualTool, aA as createAgentExecutionContext, aB as createApiErrorHandler, aC as createSdkAgentStream, aD as createThinkTagExtractor, aE as createToolHooksPlugin, aF as delegate, aG as delegateBackground, aH as delegateWithScoring, aI as extractThinkTagStream, aJ as generateAgentManifest, aK as generateAgentRoutes, aL as isAgentContext, aM as isAgentDefinition, aN as isApprovalRequired, aO as isDone, aP as isError, aQ as isPartialToolCall, aR as isTextDelta, aS as isToolCall, aT as isToolResult, aW as mcpRegistry, aX as mcpToolApprovals, aZ as projectContextMetadataOnlyKnobs, b1 as resolveMcpServers, b2 as runWithApiErrorHandling, b3 as streamAgentResponse, b4 as streamAgentUIMessages, b5 as translateSdkEvent, b6 as translateToUIMessageStream, b7 as validateUniqueRoutes, b8 as walkAgentMetadata } from './bridge-entry-Bo3LsMQF.js';
2
2
  import '@theokit/http';
3
3
  import './types-DVA4LQsA.js';
4
4
  import '@theokit/sdk';
package/dist/bridge.js CHANGED
@@ -46,7 +46,7 @@ import {
46
46
  translateToUIMessageStream,
47
47
  validateUniqueRoutes,
48
48
  walkAgentMetadata
49
- } from "./chunk-OFPS2N3U.js";
49
+ } from "./chunk-5VK63XGX.js";
50
50
  import "./chunk-NERDIS45.js";
51
51
  import "./chunk-7QVYU63E.js";
52
52
  export {
@@ -1319,6 +1319,41 @@ function* closeOpenBlock(state, textId) {
1319
1319
  state.reasoningId = null;
1320
1320
  }
1321
1321
  __name(closeOpenBlock, "closeOpenBlock");
1322
+ function* emitTextDelta(state, textId, content) {
1323
+ if (state.openBlock !== "text") {
1324
+ yield* closeOpenBlock(state, textId);
1325
+ yield {
1326
+ type: "text-start",
1327
+ id: textId
1328
+ };
1329
+ state.openBlock = "text";
1330
+ }
1331
+ yield {
1332
+ type: "text-delta",
1333
+ id: textId,
1334
+ delta: content
1335
+ };
1336
+ }
1337
+ __name(emitTextDelta, "emitTextDelta");
1338
+ function* emitReasoningDelta(state, textId, content) {
1339
+ let reasoningId = state.openBlock === "reasoning" ? state.reasoningId : null;
1340
+ if (reasoningId === null) {
1341
+ yield* closeOpenBlock(state, textId);
1342
+ reasoningId = crypto.randomUUID();
1343
+ state.reasoningId = reasoningId;
1344
+ yield {
1345
+ type: "reasoning-start",
1346
+ id: reasoningId
1347
+ };
1348
+ state.openBlock = "reasoning";
1349
+ }
1350
+ yield {
1351
+ type: "reasoning-delta",
1352
+ id: reasoningId,
1353
+ delta: content
1354
+ };
1355
+ }
1356
+ __name(emitReasoningDelta, "emitReasoningDelta");
1322
1357
  function* emitToolCall(event, seen) {
1323
1358
  seen.add(event.callId);
1324
1359
  yield {
@@ -1395,39 +1430,13 @@ async function* translateToUIMessageStream(events, opts) {
1395
1430
  reasoningId: null
1396
1431
  };
1397
1432
  const seenToolCallIds = /* @__PURE__ */ new Set();
1433
+ let turnMetadata;
1398
1434
  try {
1399
1435
  for await (const event of events) {
1400
1436
  if (event.type === "text_delta") {
1401
- if (state.openBlock !== "text") {
1402
- yield* closeOpenBlock(state, opts.textId);
1403
- yield {
1404
- type: "text-start",
1405
- id: opts.textId
1406
- };
1407
- state.openBlock = "text";
1408
- }
1409
- yield {
1410
- type: "text-delta",
1411
- id: opts.textId,
1412
- delta: event.content
1413
- };
1437
+ yield* emitTextDelta(state, opts.textId, event.content);
1414
1438
  } else if (event.type === "thinking") {
1415
- let reasoningId = state.openBlock === "reasoning" ? state.reasoningId : null;
1416
- if (reasoningId === null) {
1417
- yield* closeOpenBlock(state, opts.textId);
1418
- reasoningId = crypto.randomUUID();
1419
- state.reasoningId = reasoningId;
1420
- yield {
1421
- type: "reasoning-start",
1422
- id: reasoningId
1423
- };
1424
- state.openBlock = "reasoning";
1425
- }
1426
- yield {
1427
- type: "reasoning-delta",
1428
- id: reasoningId,
1429
- delta: event.content
1430
- };
1439
+ yield* emitReasoningDelta(state, opts.textId, event.content);
1431
1440
  } else if (event.type === "tool_call") {
1432
1441
  yield* closeOpenBlock(state, opts.textId);
1433
1442
  yield* emitToolCall(event, seenToolCallIds);
@@ -1447,6 +1456,7 @@ async function* translateToUIMessageStream(events, opts) {
1447
1456
  };
1448
1457
  break;
1449
1458
  } else if (event.type === "done") {
1459
+ turnMetadata = doneToMetadata(event);
1450
1460
  break;
1451
1461
  }
1452
1462
  }
@@ -1457,11 +1467,25 @@ async function* translateToUIMessageStream(events, opts) {
1457
1467
  };
1458
1468
  }
1459
1469
  yield* closeOpenBlock(state, opts.textId);
1460
- yield {
1470
+ yield turnMetadata ? {
1471
+ type: "finish",
1472
+ messageMetadata: turnMetadata
1473
+ } : {
1461
1474
  type: "finish"
1462
1475
  };
1463
1476
  }
1464
1477
  __name(translateToUIMessageStream, "translateToUIMessageStream");
1478
+ function doneToMetadata(event) {
1479
+ return event.cost === void 0 ? {
1480
+ usage: event.usage,
1481
+ durationMs: event.durationMs
1482
+ } : {
1483
+ usage: event.usage,
1484
+ durationMs: event.durationMs,
1485
+ cost: event.cost
1486
+ };
1487
+ }
1488
+ __name(doneToMetadata, "doneToMetadata");
1465
1489
 
1466
1490
  // src/bridge/define-agent.ts
1467
1491
  var AGENT_BRAND = /* @__PURE__ */ Symbol.for("theokit.agent.definition");
@@ -2993,4 +3017,4 @@ export {
2993
3017
  generateAgentManifest,
2994
3018
  agentsPlugin
2995
3019
  };
2996
- //# sourceMappingURL=chunk-OFPS2N3U.js.map
3020
+ //# sourceMappingURL=chunk-5VK63XGX.js.map