@theokit/agents 0.7.0 → 0.8.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.
@@ -1,5 +1,5 @@
1
1
  import { ExecutionContext } from '@theokit/http';
2
- import { A as AgentOptions, T as ToolOptions, e as MainLoopMeta, a as ApprovalOptions, B as BudgetOptions, d as GatewayOptions, j as MemoryOptions, q as SkillsOptions, c as ContextWindowOptions, o as ProjectContextOptions, h as McpServersMap } from './skills-DnfvEUSg.js';
2
+ import { A as AgentOptions, T as ToolOptions, g as MainLoopMeta, a as ApprovalOptions, B as BudgetOptions, f as GatewayOptions, l as MemoryOptions, s as SkillsOptions, e as ContextWindowOptions, q as ProjectContextOptions, j as McpServersMap, b as CompactionDecoratorConfig } from './skills-BHnHPn7p.js';
3
3
  import { SkillsSettings, ContextSettings, SystemPromptResolver } from '@theokit/sdk';
4
4
  import { z } from 'zod';
5
5
 
@@ -63,6 +63,7 @@ interface AgentWalkResult {
63
63
  contextWindow?: ContextWindowOptions;
64
64
  projectContext?: ProjectContextOptions;
65
65
  mcpServers?: McpServersMap;
66
+ compaction?: CompactionDecoratorConfig;
66
67
  }
67
68
  interface ToolboxWalkResult {
68
69
  class: Function;
@@ -621,4 +622,4 @@ declare function agentsPlugin(opts: AgentsPluginOptions): {
621
622
  register(app: PluginApp): void;
622
623
  };
623
624
 
624
- export { isApprovalRequired as $, type AgentExecutionContext as A, BudgetExceededError as B, type CompiledAgentOptions as C, type DelegationResult as D, type ErrorEvent as E, type FileEditEvent as F, type ToolCallEvent as G, type ToolResultEvent as H, type IterationEvent as I, type ToolWalkResult as J, type ToolboxWalkResult as K, type LoopOutcome as L, agentsPlugin as M, compileAgent as N, compileContextWindow as O, compileProjectContext as P, compileSkills as Q, type RunStartedEvent as R, type SdkMessage as S, type TextDeltaEvent as T, compileTools as U, createAgentExecutionContext as V, createSdkAgentStream as W, delegate as X, generateAgentManifest as Y, generateAgentRoutes as Z, isAgentContext as _, type LoopStrategy as a, isDone as a0, isError as a1, isTextDelta as a2, isToolCall as a3, isToolResult as a4, loopStrategyConfigSchema as a5, projectContextMetadataOnlyKnobs as a6, resolveLoopStrategy as a7, streamAgentResponse as a8, translateSdkEvent as a9, validateUniqueRoutes as aa, walkAgentMetadata as ab, type AgentManifest as b, type AgentManifestEntry as c, type AgentManifestTool as d, type AgentRoute as e, type AgentRouteContext as f, type AgentRunInfo as g, type AgentStreamEvent as h, type AgentWalkResult as i, AgentWarningCode as j, type AgentsPluginOptions as k, type ApprovalRequiredEvent as l, type ArtifactChunkEvent as m, type ArtifactStartEvent as n, type CheckpointSavedEvent as o, type CompiledContextWindow as p, type CompiledTool as q, DEFAULT_MAX_ITERATIONS as r, type DelegateOptions as s, DelegationError as t, type DoneEvent as u, type LoopFinishReason as v, type LoopStrategyConfig as w, type StateUpdateEvent as x, type StreamEvent as y, type ThinkingEvent as z };
625
+ export { isApprovalRequired as $, type AgentExecutionContext as A, BudgetExceededError as B, type CompiledAgentOptions as C, type DelegationResult as D, type ErrorEvent as E, type FileEditEvent as F, type ToolCallEvent as G, type ToolResultEvent as H, type IterationEvent as I, type ToolWalkResult as J, type ToolboxWalkResult as K, type LoopOutcome as L, agentsPlugin as M, compileAgent as N, compileContextWindow as O, compileProjectContext as P, compileSkills as Q, type RunStartedEvent as R, type StreamEvent as S, type TextDeltaEvent as T, compileTools as U, createAgentExecutionContext as V, createSdkAgentStream as W, delegate as X, generateAgentManifest as Y, generateAgentRoutes as Z, isAgentContext as _, type LoopStrategy as a, isDone as a0, isError as a1, isTextDelta as a2, isToolCall as a3, isToolResult as a4, loopStrategyConfigSchema as a5, projectContextMetadataOnlyKnobs as a6, resolveLoopStrategy as a7, streamAgentResponse as a8, translateSdkEvent as a9, validateUniqueRoutes as aa, walkAgentMetadata as ab, type AgentManifest as b, type AgentManifestEntry as c, type AgentManifestTool as d, type AgentRoute as e, type AgentRouteContext as f, type AgentRunInfo as g, type AgentStreamEvent as h, type AgentWalkResult as i, AgentWarningCode as j, type AgentsPluginOptions as k, type ApprovalRequiredEvent as l, type ArtifactChunkEvent as m, type ArtifactStartEvent as n, type CheckpointSavedEvent as o, type CompiledContextWindow as p, type CompiledTool as q, DEFAULT_MAX_ITERATIONS as r, type DelegateOptions as s, DelegationError as t, type DoneEvent as u, type LoopFinishReason as v, type LoopStrategyConfig as w, type SdkMessage as x, type StateUpdateEvent as y, type ThinkingEvent as z };
package/dist/bridge.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { A as AgentExecutionContext, b as AgentManifest, c as AgentManifestEntry, d as AgentManifestTool, e as AgentRoute, f as AgentRouteContext, g as AgentRunInfo, h as AgentStreamEvent, i as AgentWalkResult, j as AgentWarningCode, k as AgentsPluginOptions, l as ApprovalRequiredEvent, m as ArtifactChunkEvent, n as ArtifactStartEvent, B as BudgetExceededError, o as CheckpointSavedEvent, C as CompiledAgentOptions, p as CompiledContextWindow, q as CompiledTool, s as DelegateOptions, t as DelegationError, D as DelegationResult, u as DoneEvent, E as ErrorEvent, F as FileEditEvent, I as IterationEvent, R as RunStartedEvent, S as SdkMessage, x as StateUpdateEvent, y as StreamEvent, T as TextDeltaEvent, z as ThinkingEvent, G as ToolCallEvent, H as ToolResultEvent, J as ToolWalkResult, K as ToolboxWalkResult, M as agentsPlugin, N as compileAgent, O as compileContextWindow, P as compileProjectContext, Q as compileSkills, U as compileTools, V as createAgentExecutionContext, W as createSdkAgentStream, X as delegate, Y as generateAgentManifest, Z as generateAgentRoutes, _ as isAgentContext, $ as isApprovalRequired, a0 as isDone, a1 as isError, a2 as isTextDelta, a3 as isToolCall, a4 as isToolResult, a6 as projectContextMetadataOnlyKnobs, a8 as streamAgentResponse, a9 as translateSdkEvent, aa as validateUniqueRoutes, ab as walkAgentMetadata } from './bridge-entry-CIwzw_3z.js';
1
+ export { A as AgentExecutionContext, b as AgentManifest, c as AgentManifestEntry, d as AgentManifestTool, e as AgentRoute, f as AgentRouteContext, g as AgentRunInfo, h as AgentStreamEvent, i as AgentWalkResult, j as AgentWarningCode, k as AgentsPluginOptions, l as ApprovalRequiredEvent, m as ArtifactChunkEvent, n as ArtifactStartEvent, B as BudgetExceededError, o as CheckpointSavedEvent, C as CompiledAgentOptions, p as CompiledContextWindow, q as CompiledTool, s as DelegateOptions, t as DelegationError, D as DelegationResult, u as DoneEvent, E as ErrorEvent, F as FileEditEvent, I as IterationEvent, R as RunStartedEvent, x as SdkMessage, y as StateUpdateEvent, S as StreamEvent, T as TextDeltaEvent, z as ThinkingEvent, G as ToolCallEvent, H as ToolResultEvent, J as ToolWalkResult, K as ToolboxWalkResult, M as agentsPlugin, N as compileAgent, O as compileContextWindow, P as compileProjectContext, Q as compileSkills, U as compileTools, V as createAgentExecutionContext, W as createSdkAgentStream, X as delegate, Y as generateAgentManifest, Z as generateAgentRoutes, _ as isAgentContext, $ as isApprovalRequired, a0 as isDone, a1 as isError, a2 as isTextDelta, a3 as isToolCall, a4 as isToolResult, a6 as projectContextMetadataOnlyKnobs, a8 as streamAgentResponse, a9 as translateSdkEvent, aa as validateUniqueRoutes, ab as walkAgentMetadata } from './bridge-entry-CAI1139f.js';
2
2
  import '@theokit/http';
3
- import './skills-DnfvEUSg.js';
3
+ import './skills-BHnHPn7p.js';
4
4
  import 'zod';
5
5
  import '@theokit/sdk';
package/dist/bridge.js CHANGED
@@ -25,8 +25,8 @@ import {
25
25
  translateSdkEvent,
26
26
  validateUniqueRoutes,
27
27
  walkAgentMetadata
28
- } from "./chunk-2MYIZ4OT.js";
29
- import "./chunk-MVEY7HEY.js";
28
+ } from "./chunk-BCSRKDP6.js";
29
+ import "./chunk-GVPUUKKE.js";
30
30
  import "./chunk-7QVYU63E.js";
31
31
  export {
32
32
  AgentWarningCode,
@@ -9,6 +9,7 @@ import {
9
9
  TOOL_METHODS,
10
10
  Trace,
11
11
  getAgentConfig,
12
+ getCompactionConfig,
12
13
  getContextWindowConfig,
13
14
  getGatewayConfig,
14
15
  getMcpConfig,
@@ -18,7 +19,7 @@ import {
18
19
  getProjectContextConfig,
19
20
  getSkillsConfig,
20
21
  getSubAgents
21
- } from "./chunk-MVEY7HEY.js";
22
+ } from "./chunk-GVPUUKKE.js";
22
23
  import {
23
24
  __name
24
25
  } from "./chunk-7QVYU63E.js";
@@ -205,6 +206,7 @@ function walkAgentMetadata(AgentClass, toolboxClasses = []) {
205
206
  const contextWindow = getContextWindowConfig(AgentClass);
206
207
  const projectContext = getProjectContextConfig(AgentClass);
207
208
  warnUnmappedDecoratorKnobs(AgentClass.name, contextWindow, projectContext);
209
+ const compaction = getCompactionConfig(AgentClass);
208
210
  const result = {
209
211
  agentConfig,
210
212
  mainLoop,
@@ -219,7 +221,8 @@ function walkAgentMetadata(AgentClass, toolboxClasses = []) {
219
221
  skills,
220
222
  contextWindow,
221
223
  projectContext,
222
- mcpServers
224
+ mcpServers,
225
+ compaction
223
226
  };
224
227
  if (toolboxClasses.length === 0) {
225
228
  agentWalkCache.set(AgentClass, result);
@@ -694,16 +697,47 @@ function createSdkAgentStream(compiled, compiledTools, apiKey, envModel) {
694
697
  }
695
698
  __name(createSdkAgentStream, "createSdkAgentStream");
696
699
 
697
- // src/loop/loop-strategy.ts
700
+ // src/loop/compaction-strategy.ts
701
+ import { compactTranscript } from "@theokit/sdk/compaction";
698
702
  import { z } from "zod";
703
+ var DEFAULT_KEEP_TOKENS = 8e3;
704
+ var compactionStrategyConfigSchema = z.object({
705
+ name: z.literal("token-budget"),
706
+ keepTokens: z.number().int().positive()
707
+ });
708
+ function resolveCompactionStrategy(name, config) {
709
+ const cfg = compactionStrategyConfigSchema.parse({
710
+ name,
711
+ keepTokens: config.keepTokens
712
+ });
713
+ return {
714
+ name: cfg.name,
715
+ keepTokens: cfg.keepTokens,
716
+ compact: /* @__PURE__ */ __name((messages, options) => compactTranscript(messages, {
717
+ keepTokens: options?.keepTokens ?? cfg.keepTokens,
718
+ summarize: options?.summarize,
719
+ marker: options?.marker,
720
+ summaryTemplate: options?.summaryTemplate,
721
+ // Default-safe: a thrown summarize keeps the transcript (app opts out via failSafe:false).
722
+ failSafe: options?.failSafe ?? true
723
+ }), "compact")
724
+ };
725
+ }
726
+ __name(resolveCompactionStrategy, "resolveCompactionStrategy");
727
+ var tokenBudgetCompactionStrategy = resolveCompactionStrategy("token-budget", {
728
+ keepTokens: DEFAULT_KEEP_TOKENS
729
+ });
730
+
731
+ // src/loop/loop-strategy.ts
732
+ import { z as z2 } from "zod";
699
733
  var DEFAULT_MAX_ITERATIONS = 8;
700
- var loopStrategyConfigSchema = z.object({
701
- name: z.enum([
734
+ var loopStrategyConfigSchema = z2.object({
735
+ name: z2.enum([
702
736
  "simple-chat",
703
737
  "plan-act-reflect",
704
738
  "react"
705
739
  ]),
706
- maxIterations: z.number().int().min(1)
740
+ maxIterations: z2.number().int().min(1)
707
741
  });
708
742
  function resolveLoopStrategy(strategy, maxIterations = DEFAULT_MAX_ITERATIONS) {
709
743
  const cfg = loopStrategyConfigSchema.parse({
@@ -726,9 +760,9 @@ function resolveLoopStrategy(strategy, maxIterations = DEFAULT_MAX_ITERATIONS) {
726
760
  __name(resolveLoopStrategy, "resolveLoopStrategy");
727
761
 
728
762
  // src/loop/reflection-strategy.ts
729
- import { z as z2 } from "zod";
730
- var reflectionStrategyConfigSchema = z2.object({
731
- name: z2.string().min(1)
763
+ import { z as z3 } from "zod";
764
+ var reflectionStrategyConfigSchema = z3.object({
765
+ name: z3.string().min(1)
732
766
  });
733
767
  var ladderReflectionStrategy = {
734
768
  name: "ladder",
@@ -821,9 +855,9 @@ function buildPrompt(round, maxIterations, message, feedback) {
821
855
  return hint + body;
822
856
  }
823
857
  __name(buildPrompt, "buildPrompt");
824
- async function consumeRoundOrThrow(factory, prompt, sessionId, signal, agentName) {
858
+ async function* consumeRoundOrThrow(factory, prompt, sessionId, signal, agentName) {
825
859
  try {
826
- return await consumeOneRound(factory, prompt, sessionId, signal);
860
+ return yield* consumeOneRound(factory, prompt, sessionId, signal);
827
861
  } catch (err) {
828
862
  if (err instanceof BudgetExceededError || err instanceof DelegationError) throw err;
829
863
  throw new DelegationError(agentName, err);
@@ -848,7 +882,7 @@ function deriveFinishReason(signals) {
848
882
  return "stop";
849
883
  }
850
884
  __name(deriveFinishReason, "deriveFinishReason");
851
- async function consumeOneRound(factory, prompt, sessionId, signal) {
885
+ async function* consumeOneRound(factory, prompt, sessionId, signal) {
852
886
  const r = {
853
887
  responseText: "",
854
888
  toolCalls: [],
@@ -865,6 +899,7 @@ async function consumeOneRound(factory, prompt, sessionId, signal) {
865
899
  };
866
900
  for await (const event of factory(prompt, sessionId)) {
867
901
  if (signal?.aborted) break;
902
+ yield event;
868
903
  if (event.type === "text_delta" && typeof event.content === "string") {
869
904
  r.responseText += event.content;
870
905
  } else if (event.type === "tool_result") {
@@ -888,7 +923,7 @@ async function consumeOneRound(factory, prompt, sessionId, signal) {
888
923
  return r;
889
924
  }
890
925
  __name(consumeOneRound, "consumeOneRound");
891
- async function runReflectiveLoop(factory, message, sessionId, config) {
926
+ async function* runReflectiveLoopStream(factory, message, sessionId, config) {
892
927
  const { loop, reflection, budget = Number.POSITIVE_INFINITY, signal, agentName = loop.name } = config;
893
928
  const acc = {
894
929
  response: "",
@@ -903,7 +938,7 @@ async function runReflectiveLoop(factory, message, sessionId, config) {
903
938
  let stuck = 0;
904
939
  while (!signal?.aborted) {
905
940
  const prompt = buildPrompt(round, loop.maxIterations, message, feedback);
906
- const r = await consumeRoundOrThrow(factory, prompt, sessionId, signal, agentName);
941
+ const r = yield* consumeRoundOrThrow(factory, prompt, sessionId, signal, agentName);
907
942
  acc.response += r.responseText;
908
943
  acc.toolCalls.push(...r.toolCalls);
909
944
  acc.cost += r.cost;
@@ -935,6 +970,13 @@ async function runReflectiveLoop(factory, message, sessionId, config) {
935
970
  acc.rounds = round - 1;
936
971
  return acc;
937
972
  }
973
+ __name(runReflectiveLoopStream, "runReflectiveLoopStream");
974
+ async function runReflectiveLoop(factory, message, sessionId, config) {
975
+ const gen = runReflectiveLoopStream(factory, message, sessionId, config);
976
+ let res = await gen.next();
977
+ while (!res.done) res = await gen.next();
978
+ return res.value;
979
+ }
938
980
  __name(runReflectiveLoop, "runReflectiveLoop");
939
981
 
940
982
  // src/loop/agent-runner.ts
@@ -944,25 +986,47 @@ var AgentRunner = class {
944
986
  }
945
987
  compiled;
946
988
  agentName;
989
+ /** The resolved terminal-decision strategy (parity with `delegate()`). */
947
990
  loopStrategy;
991
+ /** The resolved between-round reflection (default or `.reflection(custom)` override). */
948
992
  reflectionStrategy;
993
+ /**
994
+ * Recorded streaming preference. The reflective loop currently always streams via
995
+ * the SDK `Run.stream()`; a non-streaming collect mode is future work — the flag is
996
+ * captured + exposed here, not yet branched on (honest per G10: documented, not a
997
+ * silent no-op).
998
+ */
949
999
  streamEnabled;
950
- constructor(compiled, agentName, loopStrategy, reflectionStrategy, streamEnabled) {
951
- this.compiled = compiled;
952
- this.agentName = agentName;
953
- this.loopStrategy = loopStrategy;
954
- this.reflectionStrategy = reflectionStrategy;
955
- this.streamEnabled = streamEnabled;
1000
+ /**
1001
+ * V4-F: the resolved compaction strategy (from `@Compaction` or `.compaction()`),
1002
+ * or `undefined` when neither is declared — compaction is opt-in (EC-4). CALLABLE
1003
+ * by the app (ADR D1: `runner.compaction?.compact(messages, { summarize })`); the
1004
+ * reflective loop does NOT auto-invoke it (the SDK owns per-turn context).
1005
+ */
1006
+ compaction;
1007
+ constructor(state) {
1008
+ this.compiled = state.compiled;
1009
+ this.agentName = state.agentName;
1010
+ this.loopStrategy = state.loopStrategy;
1011
+ this.reflectionStrategy = state.reflectionStrategy;
1012
+ this.streamEnabled = state.streamEnabled;
1013
+ this.compaction = state.compaction;
956
1014
  }
957
1015
  /** Start a fluent builder for `AgentClass`. */
958
1016
  static builder(AgentClass) {
959
1017
  return new AgentRunnerBuilder(AgentClass);
960
1018
  }
961
- /** Run the agent to a terminal result via the shared reflective loop. */
962
- run(message, opts) {
1019
+ /**
1020
+ * V4-D-stream: stream the agent's events LIVE across the reflective loop, returning
1021
+ * the aggregated {@link DelegationResult} as the generator's return value. This is the
1022
+ * on-ramp for streaming-first apps (SSE) — `runReflectiveLoopStream` yields every
1023
+ * round's events before the loop terminates. `streamEnabled` is honored: when the
1024
+ * builder set `.stream(false)`, callers should use {@link run} instead.
1025
+ */
1026
+ stream(message, opts) {
963
1027
  const streamFactory = createSdkAgentStream(this.compiled, this.compiled.tools, opts.apiKey, this.compiled.model);
964
1028
  const sessionId = opts.sessionId ?? `runner-${crypto.randomUUID()}`;
965
- return runReflectiveLoop(streamFactory, message, sessionId, {
1029
+ return runReflectiveLoopStream(streamFactory, message, sessionId, {
966
1030
  loop: this.loopStrategy,
967
1031
  reflection: this.reflectionStrategy,
968
1032
  budget: opts.budget,
@@ -970,6 +1034,13 @@ var AgentRunner = class {
970
1034
  signal: opts.signal
971
1035
  });
972
1036
  }
1037
+ /** Run the agent to a terminal result via the shared reflective loop (collect mode). */
1038
+ async run(message, opts) {
1039
+ const gen = this.stream(message, opts);
1040
+ let res = await gen.next();
1041
+ while (!res.done) res = await gen.next();
1042
+ return res.value;
1043
+ }
973
1044
  };
974
1045
  var AgentRunnerBuilder = class {
975
1046
  static {
@@ -978,6 +1049,7 @@ var AgentRunnerBuilder = class {
978
1049
  AgentClass;
979
1050
  reflectionOverride;
980
1051
  streamEnabled = true;
1052
+ compactionOverride;
981
1053
  constructor(AgentClass) {
982
1054
  this.AgentClass = AgentClass;
983
1055
  }
@@ -991,6 +1063,18 @@ var AgentRunnerBuilder = class {
991
1063
  this.streamEnabled = enabled;
992
1064
  return this;
993
1065
  }
1066
+ /**
1067
+ * V4-F: declare the compaction strategy (e.g. `.compaction('token-budget', { keepTokens: 8000 })`).
1068
+ * Resolved + validated at {@link build} (EC-5 — fail-fast there, not here). This builder
1069
+ * call WINS over a `@Compaction` decorator on the same agent (EC-1 — explicit override).
1070
+ */
1071
+ compaction(name, options = {}) {
1072
+ this.compactionOverride = {
1073
+ name,
1074
+ keepTokens: options.keepTokens
1075
+ };
1076
+ return this;
1077
+ }
994
1078
  /** Walk + compile + resolve strategies — the compile→execute boundary (no I/O). */
995
1079
  build() {
996
1080
  const walk = walkAgentMetadata(this.AgentClass, []);
@@ -1001,7 +1085,18 @@ var AgentRunnerBuilder = class {
1001
1085
  const compiled = compileAgent(walk, toolboxInstances);
1002
1086
  const loopStrategy = resolveLoopStrategy(walk.mainLoop.strategy, walk.mainLoop.maxIterations);
1003
1087
  const reflectionStrategy = this.reflectionOverride ?? (walk.mainLoop.strategy === "plan-act-reflect" ? ladderReflectionStrategy : noopReflectionStrategy);
1004
- return new AgentRunner(compiled, walk.agentConfig.name, loopStrategy, reflectionStrategy, this.streamEnabled);
1088
+ const compactionDecl = this.compactionOverride ?? walk.compaction;
1089
+ const compaction = compactionDecl ? resolveCompactionStrategy(compactionDecl.name, {
1090
+ keepTokens: compactionDecl.keepTokens
1091
+ }) : void 0;
1092
+ return new AgentRunner({
1093
+ compiled,
1094
+ agentName: walk.agentConfig.name,
1095
+ loopStrategy,
1096
+ reflectionStrategy,
1097
+ streamEnabled: this.streamEnabled,
1098
+ compaction
1099
+ });
1005
1100
  }
1006
1101
  };
1007
1102
 
@@ -1193,6 +1288,10 @@ export {
1193
1288
  generateAgentRoutes,
1194
1289
  translateSdkEvent,
1195
1290
  createSdkAgentStream,
1291
+ DEFAULT_KEEP_TOKENS,
1292
+ compactionStrategyConfigSchema,
1293
+ resolveCompactionStrategy,
1294
+ tokenBudgetCompactionStrategy,
1196
1295
  DEFAULT_MAX_ITERATIONS,
1197
1296
  loopStrategyConfigSchema,
1198
1297
  resolveLoopStrategy,
@@ -1207,4 +1306,4 @@ export {
1207
1306
  generateAgentManifest,
1208
1307
  agentsPlugin
1209
1308
  };
1210
- //# sourceMappingURL=chunk-2MYIZ4OT.js.map
1309
+ //# sourceMappingURL=chunk-BCSRKDP6.js.map