@theokit/agents 2.0.0 → 4.0.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/dist/{bridge-entry-F9kHGaIn.d.ts → bridge-entry-Ddu3Ug_3.d.ts} +44 -22
- package/dist/bridge.d.ts +1 -1
- package/dist/bridge.js +5 -5
- package/dist/{chunk-K3VX4S65.js → chunk-OKG5LRGV.js} +87 -22
- package/dist/chunk-OKG5LRGV.js.map +1 -0
- package/dist/index.d.ts +133 -20
- package/dist/index.js +210 -119
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-K3VX4S65.js.map +0 -1
|
@@ -821,7 +821,7 @@ interface DefineAgentConfig<TInput extends z.ZodType = z.ZodType> {
|
|
|
821
821
|
/**
|
|
822
822
|
* A branded agent definition — the value {@link defineAgent} returns.
|
|
823
823
|
*
|
|
824
|
-
* `TTools` (M8) is a phantom type parameter carrying the tool-name union: the `
|
|
824
|
+
* `TTools` (M8) is a phantom type parameter carrying the tool-name union: the `AgentBuilder.create()` builder
|
|
825
825
|
* threads its accumulated literal tool names here (`.build()` returns `AgentDefinition<TInput,
|
|
826
826
|
* 'a' | 'b'>`), so the generated client (`.theokit/agents.d.ts`) can expose them via
|
|
827
827
|
* {@link InferAgentToolNames}. `defineAgent` leaves it `string` (its tools array carries no literal
|
|
@@ -835,7 +835,7 @@ type AgentDefinition<TInput extends z.ZodType = z.ZodType, TTools extends string
|
|
|
835
835
|
type InferAgentInput<T> = T extends AgentDefinition<infer S> ? (S extends z.ZodType ? z.infer<S> : never) : never;
|
|
836
836
|
/**
|
|
837
837
|
* Infer the tool-name union of an agent definition (M8). Yields the literal union for agents built
|
|
838
|
-
* with the `
|
|
838
|
+
* with the `AgentBuilder.create()` builder (`'read_file' | 'count_lines'`), or `string` for `defineAgent` agents
|
|
839
839
|
* whose tools array carries no literal names.
|
|
840
840
|
*/
|
|
841
841
|
type InferAgentToolNames<T> = T extends AgentDefinition<z.ZodType, infer N> ? N : never;
|
|
@@ -962,7 +962,7 @@ interface SdkAgentHandle {
|
|
|
962
962
|
/**
|
|
963
963
|
* #12 — bridge a builder/`defineAgent` {@link TheokitAgentDefinition} to a real `SDKAgent` FACTORY,
|
|
964
964
|
* so surfaces that require an `SDKAgent` (or `(sessionId) => SDKAgent`) — notably `theokit acp`,
|
|
965
|
-
* whose entry default-export must be one — can serve an agent defined with the `
|
|
965
|
+
* whose entry default-export must be one — can serve an agent defined with the `AgentBuilder.create()` chain.
|
|
966
966
|
*
|
|
967
967
|
* The factory reuses the SAME projection the streaming path uses (`compileAgentDefinition` → tools /
|
|
968
968
|
* model / `assembleM8CreateOptions`), so the served agent has identical tools, model, system prompt,
|
|
@@ -1065,9 +1065,9 @@ declare function presentUIMessageStream(events: AsyncIterable<AgentStreamEvent>,
|
|
|
1065
1065
|
}): AsyncGenerator<UIMessageChunk, void, unknown>;
|
|
1066
1066
|
|
|
1067
1067
|
/**
|
|
1068
|
-
* M8 — `
|
|
1068
|
+
* M8 — `AgentBuilder.create()`, the fluent agent builder with accumulative **type-state**.
|
|
1069
1069
|
*
|
|
1070
|
-
* `
|
|
1070
|
+
* `AgentBuilder.create().context<C>().tool(t).model(id).system(s).build()` accumulates type parameters the way
|
|
1071
1071
|
* the most-loved TS DX does (tRPC `t.procedure.input().query()`, Zod, Hono) and resolves to the
|
|
1072
1072
|
* **SAME branded `AgentDefinition`** that {@link defineAgent} produces — one runtime, N syntaxes
|
|
1073
1073
|
* (ADR-B1). The value is entirely at the type level; `.build()` delegates to `defineAgent`, so
|
|
@@ -1106,7 +1106,7 @@ type EmptyContext = Record<never, never>;
|
|
|
1106
1106
|
/**
|
|
1107
1107
|
* A tool that MAY declare, at the type level, the run-context shape it needs. A plain
|
|
1108
1108
|
* {@link CustomTool} (`TRequired = unknown`) is satisfied by any agent context. Build one that
|
|
1109
|
-
* carries a literal name + optional required-context via {@link
|
|
1109
|
+
* carries a literal name + optional required-context via {@link ContextualTool.of}.
|
|
1110
1110
|
*/
|
|
1111
1111
|
interface ContextualTool<TName extends string = string, TRequired = unknown> extends CustomTool {
|
|
1112
1112
|
readonly name: TName;
|
|
@@ -1114,13 +1114,21 @@ interface ContextualTool<TName extends string = string, TRequired = unknown> ext
|
|
|
1114
1114
|
readonly __requiredContext?: TRequired;
|
|
1115
1115
|
}
|
|
1116
1116
|
/**
|
|
1117
|
-
*
|
|
1118
|
-
*
|
|
1119
|
-
*
|
|
1117
|
+
* Static factory for {@link ContextualTool}. M57: OO surface (`ContextualTool.of(...)`) aligned with
|
|
1118
|
+
* the SDK's `X.create()` shape — was the free `contextualTool(...)` function. `ContextualTool` is a
|
|
1119
|
+
* TYPE (the interface above) and a VALUE (this const) at once, the same dual-space pattern the SDK
|
|
1120
|
+
* uses for `Tool`/`Agent`. No instance state, so the factory is `static`-style (`of`), not `create`.
|
|
1120
1121
|
*/
|
|
1121
|
-
declare
|
|
1122
|
-
|
|
1123
|
-
}
|
|
1122
|
+
declare const ContextualTool: {
|
|
1123
|
+
/**
|
|
1124
|
+
* Tag a {@link CustomTool} with a literal name (so `.tool()` can accumulate the tool-name union)
|
|
1125
|
+
* and, optionally, a required run-context type. The `requiredContext` argument is a type-only
|
|
1126
|
+
* witness — pass `undefined as C` or a sample value; it is never read at runtime.
|
|
1127
|
+
*/
|
|
1128
|
+
of<TName extends string, TRequired = unknown>(tool: CustomTool & {
|
|
1129
|
+
name: TName;
|
|
1130
|
+
}, _requiredContext?: TRequired): ContextualTool<TName, TRequired>;
|
|
1131
|
+
};
|
|
1124
1132
|
/**
|
|
1125
1133
|
* The fluent builder. Each method returns a NEW builder type with the relevant type parameter
|
|
1126
1134
|
* advanced (immutable chaining). Runtime state is a plain {@link DefineAgentConfig} accumulator.
|
|
@@ -1183,7 +1191,7 @@ interface AgentBuilder<TInput extends z.ZodType | UnsetMarker = UnsetMarker, TMo
|
|
|
1183
1191
|
* it runs; the other events are observational.
|
|
1184
1192
|
*
|
|
1185
1193
|
* ```ts
|
|
1186
|
-
*
|
|
1194
|
+
* AgentBuilder.create().hooks({
|
|
1187
1195
|
* pre_tool_call: (c) => guard(c.name) ? undefined : { block: true, message: 'not allowed' },
|
|
1188
1196
|
* on_session_start: () => log('session up'),
|
|
1189
1197
|
* })
|
|
@@ -1226,10 +1234,18 @@ interface AgentBuilder<TInput extends z.ZodType | UnsetMarker = UnsetMarker, TMo
|
|
|
1226
1234
|
readonly __toolNames?: TTools;
|
|
1227
1235
|
}
|
|
1228
1236
|
/**
|
|
1229
|
-
*
|
|
1230
|
-
*
|
|
1237
|
+
* Static entry point for the fluent builder. M57: OO surface (`AgentBuilder.create()`) aligned with
|
|
1238
|
+
* the SDK's `X.create()` shape — was the free `agent()` function. `AgentBuilder` is a TYPE (the
|
|
1239
|
+
* generic interface above) and a VALUE (this const) at once; in type position the interface wins, in
|
|
1240
|
+
* value position the const wins — no collision, the same dual-space pattern the SDK uses for `Agent`.
|
|
1231
1241
|
*/
|
|
1232
|
-
declare
|
|
1242
|
+
declare const AgentBuilder: {
|
|
1243
|
+
/**
|
|
1244
|
+
* Start a fluent agent definition. Chain `.model()` (required) + `.context()` / `.system()` /
|
|
1245
|
+
* `.input()` / `.tool()` / `.use()`, then `.build()` to get the branded {@link AgentDefinition}.
|
|
1246
|
+
*/
|
|
1247
|
+
create(): AgentBuilder;
|
|
1248
|
+
};
|
|
1233
1249
|
|
|
1234
1250
|
/**
|
|
1235
1251
|
* M4 (theokit-ai-first) — the HITL producer: a `pre_tool_call` plugin that pauses the SDK run
|
|
@@ -1363,11 +1379,17 @@ interface LoopOutcome {
|
|
|
1363
1379
|
}
|
|
1364
1380
|
/** The terminal-decision contract. `shouldContinue` is the inverted `stopWhen`. */
|
|
1365
1381
|
interface LoopStrategy {
|
|
1366
|
-
/**
|
|
1367
|
-
|
|
1368
|
-
|
|
1382
|
+
/**
|
|
1383
|
+
* The strategy name, surfaced in `finalize`/logs. M54 relaxes this from
|
|
1384
|
+
* `MainLoopMeta['strategy']` to `string` so a caller-injected `.loopStrategy(custom)` can name
|
|
1385
|
+
* itself freely — the three built-ins keep their names, but the seam is no longer union-locked.
|
|
1386
|
+
* The INTERNAL resolution (`loopStrategyConfigSchema`) still validates the three built-in names
|
|
1387
|
+
* via `z.enum`; a custom never passes through it (it enters by the seam, not by name).
|
|
1388
|
+
*/
|
|
1389
|
+
readonly name: string;
|
|
1390
|
+
/** Hard ceiling on rounds — guarantees termination (enforced by the runner since M54). */
|
|
1369
1391
|
readonly maxIterations: number;
|
|
1370
|
-
/** True ⇒ re-enter for another round; false ⇒ terminate.
|
|
1392
|
+
/** True ⇒ re-enter for another round; false ⇒ terminate. The runner caps it at `maxIterations`. */
|
|
1371
1393
|
shouldContinue(outcome: LoopOutcome): boolean;
|
|
1372
1394
|
}
|
|
1373
1395
|
/** Default round ceiling when `@MainLoop` declares no `maxIterations` (EC-3: finite, never Infinity). */
|
|
@@ -1397,7 +1419,7 @@ type LoopStrategyConfig = z.infer<typeof loopStrategyConfigSchema>;
|
|
|
1397
1419
|
*
|
|
1398
1420
|
* Throws (Zod) when `maxIterations < 1` — fail fast, never a silent infinite loop.
|
|
1399
1421
|
*/
|
|
1400
|
-
declare function resolveLoopStrategy(strategy:
|
|
1422
|
+
declare function resolveLoopStrategy(strategy: string, maxIterations?: number): LoopStrategy;
|
|
1401
1423
|
|
|
1402
1424
|
/**
|
|
1403
1425
|
* Shared delegation value types + typed errors.
|
|
@@ -2040,4 +2062,4 @@ declare function agentsPlugin(opts: AgentsPluginOptions): {
|
|
|
2040
2062
|
register(app: PluginApp): void;
|
|
2041
2063
|
};
|
|
2042
2064
|
|
|
2043
|
-
export { type
|
|
2065
|
+
export { type FileEditEvent as $, type ApprovalOptions as A, type ArtifactChunkEvent as B, type CompiledAgentOptions as C, type DelegationResult as D, type ArtifactStartEvent as E, type BackgroundDelegation as F, type Guardrail as G, type HumanInTheLoopOptions as H, type BeforeToolCallContext as I, BudgetExceededError as J, type BudgetOptions as K, type LoopStrategy as L, type MainLoopMeta as M, type CheckpointSavedEvent as N, type CompiledContextWindow as O, ContextualTool as P, CostBudgetExceededError as Q, type ReflectionStrategy as R, type StreamEvent as S, type ToolOptions as T, DEFAULT_MAX_ITERATIONS as U, type DefineAgentConfig as V, type DelegateFn as W, type DelegateOptions as X, DelegationError as Y, type DoneEvent as Z, type ErrorEvent as _, type CompiledTool as a, isAgentContext as a$, type GuardrailAction as a0, type GuardrailPhase as a1, type GuardrailResult as a2, GuardrailViolationError as a3, type HitlDecision as a4, type InferAgentInput as a5, type InferAgentToolNames as a6, type IterationEvent as a7, type LLMCallContext as a8, type LoopFinishReason as a9, type ToolCallEvent as aA, type ToolCallVeto as aB, type ToolHooks as aC, type ToolHooksPlugin as aD, type ToolResultEvent as aE, type ToolWalkResult as aF, type ToolboxOptions as aG, type ToolboxWalkResult as aH, agentsPlugin as aI, buildModelSelection as aJ, compileAgentDefinition as aK, compileAgentModule as aL, compileContextWindow as aM, compileProjectContext as aN, compileSkills as aO, compileTools as aP, createAgentExecutionContext as aQ, createApiErrorHandler as aR, createSdkAgentStream as aS, createThinkTagExtractor as aT, createToolHooksPlugin as aU, delegate as aV, delegateBackground as aW, delegateWithScoring as aX, extractThinkTagStream as aY, generateAgentManifest as aZ, generateAgentRoutes as a_, type LoopOutcome as aa, type LoopStrategyConfig as ab, type MainLoopOptions as ac, type McpApprovalSpec as ad, type McpRegistryConfig as ae, type McpRequestContext as af, type McpSelection as ag, type PartialToolCallEvent as ah, type PolicyHandler as ai, type ProcessInputContext as aj, type ReflectionContext as ak, type ReflectionResult as al, type ReflectionStrategyConfig as am, type RunStartedEvent as an, type ScoreVerdict as ao, type ScoredDelegation as ap, type Scorer as aq, type SdkAgentHandle as ar, type SdkMessage as as, type Segment as at, type SkillsRequestContext as au, type SkillsSelection as av, type StateUpdateEvent as aw, type TextDeltaEvent as ax, type ThinkingEvent as ay, type TimeoutAction as az, type ReasoningEffort as b, isAgentDefinition as b0, isApprovalRequired as b1, isDone as b2, isError as b3, isPartialToolCall as b4, isTextDelta as b5, isToolCall as b6, isToolResult as b7, ladderReflectionStrategy as b8, loopStrategyConfigSchema as b9, mcpRegistry as ba, mcpToolApprovals as bb, noopReflectionStrategy as bc, presentUIMessageStream as bd, projectContextMetadataOnlyKnobs as be, reflectionStrategyConfigSchema as bf, resolveEnabledSkills as bg, resolveLoopStrategy as bh, resolveMcpServers as bi, runWithApiErrorHandling as bj, streamAgentResponse as bk, streamAgentUIMessages as bl, toAgentFactory as bm, translateSdkEvent as bn, type RoundStreamFactory as c, type ContextWindowOptions as d, type SkillsOptions as e, type AgentManifestEntry as f, AGENT_BRAND as g, type AfterToolCallContext as h, AgentBuilder as i, type AgentDefinition as j, AgentDefinitionError as k, type AgentExecutionContext as l, type AgentManifest as m, type AgentManifestSource as n, type AgentManifestTool as o, type AgentOptions as p, type AgentRoute as q, type AgentRouteContext as r, type AgentRunInfo as s, type AgentStreamEvent as t, type AgentTurnMetadata as u, type AgentsPluginOptions as v, type ApiErrorContext as w, type ApiErrorDecision as x, type ApiErrorPolicy as y, type ApprovalRequiredEvent as z };
|
package/dist/bridge.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { g as AGENT_BRAND, h as AfterToolCallContext, i as AgentBuilder, j as AgentDefinition, k as AgentDefinitionError, l as AgentExecutionContext, m as AgentManifest, f as AgentManifestEntry, n as AgentManifestSource, o as AgentManifestTool, q as AgentRoute, r as AgentRouteContext, s as AgentRunInfo, t as AgentStreamEvent, u as AgentTurnMetadata, v as AgentsPluginOptions, w as ApiErrorContext, x as ApiErrorDecision, y as ApiErrorPolicy, z as ApprovalRequiredEvent, B as ArtifactChunkEvent, E as ArtifactStartEvent, F as BackgroundDelegation, I as BeforeToolCallContext, J as BudgetExceededError, N as CheckpointSavedEvent, C as CompiledAgentOptions, O as CompiledContextWindow, a as CompiledTool, P as ContextualTool, V as DefineAgentConfig, W as DelegateFn, X as DelegateOptions, Y as DelegationError, D as DelegationResult, Z as DoneEvent, _ as ErrorEvent, $ as FileEditEvent, a5 as InferAgentInput, a6 as InferAgentToolNames, a7 as IterationEvent, a8 as LLMCallContext, ad as McpApprovalSpec, ae as McpRegistryConfig, af as McpRequestContext, ag as McpSelection, ah as PartialToolCallEvent, aj as ProcessInputContext, an as RunStartedEvent, ao as ScoreVerdict, ap as ScoredDelegation, aq as Scorer, ar as SdkAgentHandle, as as SdkMessage, at as Segment, aw as StateUpdateEvent, S as StreamEvent, ax as TextDeltaEvent, ay as ThinkingEvent, aA as ToolCallEvent, aB as ToolCallVeto, aC as ToolHooks, aD as ToolHooksPlugin, aE as ToolResultEvent, aF as ToolWalkResult, aH as ToolboxWalkResult, aI as agentsPlugin, aJ as buildModelSelection, aK as compileAgentDefinition, aL as compileAgentModule, aM as compileContextWindow, aN as compileProjectContext, aO as compileSkills, aP as compileTools, aQ as createAgentExecutionContext, aR as createApiErrorHandler, aS as createSdkAgentStream, aT as createThinkTagExtractor, aU as createToolHooksPlugin, aV as delegate, aW as delegateBackground, aX as delegateWithScoring, aY as extractThinkTagStream, aZ as generateAgentManifest, a_ as generateAgentRoutes, a$ as isAgentContext, b0 as isAgentDefinition, b1 as isApprovalRequired, b2 as isDone, b3 as isError, b4 as isPartialToolCall, b5 as isTextDelta, b6 as isToolCall, b7 as isToolResult, ba as mcpRegistry, bb as mcpToolApprovals, bd as presentUIMessageStream, be as projectContextMetadataOnlyKnobs, bi as resolveMcpServers, bj as runWithApiErrorHandling, bk as streamAgentResponse, bl as streamAgentUIMessages, bm as toAgentFactory, bn as translateSdkEvent } from './bridge-entry-Ddu3Ug_3.js';
|
|
2
2
|
import '@theokit/http';
|
|
3
3
|
import '@theokit/sdk';
|
|
4
4
|
import 'zod';
|
package/dist/bridge.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AGENT_BRAND,
|
|
3
|
+
AgentBuilder,
|
|
3
4
|
AgentDefinitionError,
|
|
4
5
|
BudgetExceededError,
|
|
6
|
+
ContextualTool,
|
|
5
7
|
DelegationError,
|
|
6
|
-
agent,
|
|
7
8
|
agentsPlugin,
|
|
8
9
|
buildModelSelection,
|
|
9
10
|
compileAgentDefinition,
|
|
@@ -12,7 +13,6 @@ import {
|
|
|
12
13
|
compileProjectContext,
|
|
13
14
|
compileSkills,
|
|
14
15
|
compileTools,
|
|
15
|
-
contextualTool,
|
|
16
16
|
createAgentExecutionContext,
|
|
17
17
|
createApiErrorHandler,
|
|
18
18
|
createSdkAgentStream,
|
|
@@ -43,14 +43,15 @@ import {
|
|
|
43
43
|
streamAgentUIMessages,
|
|
44
44
|
toAgentFactory,
|
|
45
45
|
translateSdkEvent
|
|
46
|
-
} from "./chunk-
|
|
46
|
+
} from "./chunk-OKG5LRGV.js";
|
|
47
47
|
import "./chunk-7QVYU63E.js";
|
|
48
48
|
export {
|
|
49
49
|
AGENT_BRAND,
|
|
50
|
+
AgentBuilder,
|
|
50
51
|
AgentDefinitionError,
|
|
51
52
|
BudgetExceededError,
|
|
53
|
+
ContextualTool,
|
|
52
54
|
DelegationError,
|
|
53
|
-
agent,
|
|
54
55
|
agentsPlugin,
|
|
55
56
|
buildModelSelection,
|
|
56
57
|
compileAgentDefinition,
|
|
@@ -59,7 +60,6 @@ export {
|
|
|
59
60
|
compileProjectContext,
|
|
60
61
|
compileSkills,
|
|
61
62
|
compileTools,
|
|
62
|
-
contextualTool,
|
|
63
63
|
createAgentExecutionContext,
|
|
64
64
|
createApiErrorHandler,
|
|
65
65
|
createSdkAgentStream,
|
|
@@ -249,13 +249,13 @@ function compileTools(toolboxes, toolboxInstances) {
|
|
|
249
249
|
__name(compileTools, "compileTools");
|
|
250
250
|
|
|
251
251
|
// src/bridge/agent-execution-context.ts
|
|
252
|
-
function createAgentExecutionContext(base,
|
|
252
|
+
function createAgentExecutionContext(base, agent, run, toolCall) {
|
|
253
253
|
return {
|
|
254
254
|
getRequest: /* @__PURE__ */ __name(() => base.getRequest(), "getRequest"),
|
|
255
255
|
getUrl: /* @__PURE__ */ __name(() => base.getUrl(), "getUrl"),
|
|
256
256
|
getClass: /* @__PURE__ */ __name(() => base.getClass(), "getClass"),
|
|
257
257
|
getMethodName: /* @__PURE__ */ __name(() => base.getMethodName(), "getMethodName"),
|
|
258
|
-
getAgent: /* @__PURE__ */ __name(() =>
|
|
258
|
+
getAgent: /* @__PURE__ */ __name(() => agent, "getAgent"),
|
|
259
259
|
getRun: /* @__PURE__ */ __name(() => run, "getRun"),
|
|
260
260
|
getToolCall: /* @__PURE__ */ __name(() => toolCall ?? null, "getToolCall"),
|
|
261
261
|
isAgentContext: /* @__PURE__ */ __name(() => true, "isAgentContext")
|
|
@@ -1254,7 +1254,7 @@ async function* streamSdkAgent(rt, compiled, sdkTools, opts) {
|
|
|
1254
1254
|
projectContext: applied.includes("projectContext")
|
|
1255
1255
|
});
|
|
1256
1256
|
}
|
|
1257
|
-
const
|
|
1257
|
+
const agent = await Agent.getOrCreate(sessionId, {
|
|
1258
1258
|
apiKey,
|
|
1259
1259
|
model: buildModelSelection(model, reasoningEffort),
|
|
1260
1260
|
tools: sdkTools,
|
|
@@ -1272,7 +1272,7 @@ async function* streamSdkAgent(rt, compiled, sdkTools, opts) {
|
|
|
1272
1272
|
text: message,
|
|
1273
1273
|
images: overrides.images
|
|
1274
1274
|
} : message;
|
|
1275
|
-
const sendPromise =
|
|
1275
|
+
const sendPromise = agent.send(sendInput, sendOptions);
|
|
1276
1276
|
const openStream = /* @__PURE__ */ __name(async () => (await sendPromise).stream(), "openStream");
|
|
1277
1277
|
const merged = mergeDeltaStream(queue, openStream, runId, state);
|
|
1278
1278
|
for await (const event of applyTextTransforms(merged, {
|
|
@@ -1285,7 +1285,7 @@ async function* streamSdkAgent(rt, compiled, sdkTools, opts) {
|
|
|
1285
1285
|
yield realUsageDone(await (await sendPromise).wait(), t0);
|
|
1286
1286
|
}
|
|
1287
1287
|
} finally {
|
|
1288
|
-
await
|
|
1288
|
+
await agent.dispose();
|
|
1289
1289
|
}
|
|
1290
1290
|
}
|
|
1291
1291
|
__name(streamSdkAgent, "streamSdkAgent");
|
|
@@ -1315,14 +1315,14 @@ function toAgentFactory(def, opts) {
|
|
|
1315
1315
|
baseDir: overrides.baseDir
|
|
1316
1316
|
};
|
|
1317
1317
|
const extra = buildExtraCreateOptions(overrides, compiled);
|
|
1318
|
-
const
|
|
1318
|
+
const agent = await rt.Agent.getOrCreate(sessionId, {
|
|
1319
1319
|
apiKey: opts.apiKey,
|
|
1320
1320
|
model: buildModelSelection(model, reasoningEffort),
|
|
1321
1321
|
tools: sdkTools,
|
|
1322
1322
|
...m8,
|
|
1323
1323
|
...extra
|
|
1324
1324
|
});
|
|
1325
|
-
return
|
|
1325
|
+
return agent;
|
|
1326
1326
|
};
|
|
1327
1327
|
}
|
|
1328
1328
|
__name(toAgentFactory, "toAgentFactory");
|
|
@@ -1962,10 +1962,16 @@ async function* presentUIMessageStream(events, opts) {
|
|
|
1962
1962
|
__name(presentUIMessageStream, "presentUIMessageStream");
|
|
1963
1963
|
|
|
1964
1964
|
// src/bridge/agent-builder.ts
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
}
|
|
1968
|
-
|
|
1965
|
+
var ContextualTool = {
|
|
1966
|
+
/**
|
|
1967
|
+
* Tag a {@link CustomTool} with a literal name (so `.tool()` can accumulate the tool-name union)
|
|
1968
|
+
* and, optionally, a required run-context type. The `requiredContext` argument is a type-only
|
|
1969
|
+
* witness — pass `undefined as C` or a sample value; it is never read at runtime.
|
|
1970
|
+
*/
|
|
1971
|
+
of(tool, _requiredContext) {
|
|
1972
|
+
return tool;
|
|
1973
|
+
}
|
|
1974
|
+
};
|
|
1969
1975
|
function makeBuilder(config) {
|
|
1970
1976
|
const runtime = {
|
|
1971
1977
|
input: /* @__PURE__ */ __name((schema) => makeBuilder({
|
|
@@ -2047,10 +2053,15 @@ function makeBuilder(config) {
|
|
|
2047
2053
|
return runtime;
|
|
2048
2054
|
}
|
|
2049
2055
|
__name(makeBuilder, "makeBuilder");
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2056
|
+
var AgentBuilder = {
|
|
2057
|
+
/**
|
|
2058
|
+
* Start a fluent agent definition. Chain `.model()` (required) + `.context()` / `.system()` /
|
|
2059
|
+
* `.input()` / `.tool()` / `.use()`, then `.build()` to get the branded {@link AgentDefinition}.
|
|
2060
|
+
*/
|
|
2061
|
+
create() {
|
|
2062
|
+
return makeBuilder({});
|
|
2063
|
+
}
|
|
2064
|
+
};
|
|
2054
2065
|
|
|
2055
2066
|
// src/bridge/hitl-plugin.ts
|
|
2056
2067
|
function createHitlPlugin(wiring) {
|
|
@@ -2470,14 +2481,22 @@ var tokenBudgetCompactionStrategy = resolveCompactionStrategy("token-budget", {
|
|
|
2470
2481
|
// src/loop/loop-strategy.ts
|
|
2471
2482
|
import { z as z2 } from "zod";
|
|
2472
2483
|
var DEFAULT_MAX_ITERATIONS = 8;
|
|
2484
|
+
var maxIterationsSchema = z2.number().int().min(1);
|
|
2473
2485
|
var loopStrategyConfigSchema = z2.object({
|
|
2474
2486
|
name: z2.enum([
|
|
2475
2487
|
"simple-chat",
|
|
2476
2488
|
"plan-act-reflect",
|
|
2477
2489
|
"react"
|
|
2478
2490
|
]),
|
|
2479
|
-
maxIterations:
|
|
2491
|
+
maxIterations: maxIterationsSchema
|
|
2480
2492
|
});
|
|
2493
|
+
function assertValidCustomLoopStrategy(strategy) {
|
|
2494
|
+
const result = maxIterationsSchema.safeParse(strategy.maxIterations);
|
|
2495
|
+
if (!result.success) {
|
|
2496
|
+
throw new Error(`loopStrategy: maxIterations inv\xE1lido (${String(strategy.maxIterations)}) \u2014 deve ser um inteiro finito \u2265 1 (sen\xE3o o teto round < maxIterations nunca termina)`);
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
__name(assertValidCustomLoopStrategy, "assertValidCustomLoopStrategy");
|
|
2481
2500
|
function resolveLoopStrategy(strategy, maxIterations = DEFAULT_MAX_ITERATIONS) {
|
|
2482
2501
|
const cfg = loopStrategyConfigSchema.parse({
|
|
2483
2502
|
name: strategy,
|
|
@@ -2795,7 +2814,7 @@ async function* runReflectiveLoopStream(factory, message, sessionId, config) {
|
|
|
2795
2814
|
responseText: r.responseText
|
|
2796
2815
|
};
|
|
2797
2816
|
const reflectionResult = reflection.reflect(outcome, reflectionContext);
|
|
2798
|
-
if (!(reflectionResult.continue && loop.shouldContinue(outcome))) {
|
|
2817
|
+
if (!(reflectionResult.continue && loop.shouldContinue(outcome) && round < loop.maxIterations)) {
|
|
2799
2818
|
const reason = terminalReason(reflectionResult.continue, r.finishReason, round, loop.maxIterations);
|
|
2800
2819
|
return finalize(acc, round, reason, loop.name);
|
|
2801
2820
|
}
|
|
@@ -2823,6 +2842,8 @@ var AgentRunner = class {
|
|
|
2823
2842
|
agentName;
|
|
2824
2843
|
/** The resolved terminal-decision strategy (parity with `delegate()`). */
|
|
2825
2844
|
loopStrategy;
|
|
2845
|
+
/** M54 — whether {@link loopStrategy} is a caller-injected custom (see {@link AgentRunnerState}). */
|
|
2846
|
+
loopStrategyIsCustom;
|
|
2826
2847
|
/** The resolved between-round reflection (default or `.reflection(custom)` override). */
|
|
2827
2848
|
reflectionStrategy;
|
|
2828
2849
|
/**
|
|
@@ -2843,6 +2864,7 @@ var AgentRunner = class {
|
|
|
2843
2864
|
this.compiled = state.compiled;
|
|
2844
2865
|
this.agentName = state.agentName;
|
|
2845
2866
|
this.loopStrategy = state.loopStrategy;
|
|
2867
|
+
this.loopStrategyIsCustom = state.loopStrategyIsCustom ?? false;
|
|
2846
2868
|
this.reflectionStrategy = state.reflectionStrategy;
|
|
2847
2869
|
this.streamEnabled = state.streamEnabled;
|
|
2848
2870
|
this.compaction = state.compaction;
|
|
@@ -2874,7 +2896,7 @@ var AgentRunner = class {
|
|
|
2874
2896
|
const tools = opts.tools ? [
|
|
2875
2897
|
...opts.tools
|
|
2876
2898
|
] : this.compiled.tools;
|
|
2877
|
-
const loop =
|
|
2899
|
+
const loop = this.resolvePerRunLoop(opts.maxIterations);
|
|
2878
2900
|
const streamFactory = opts.streamFactory ?? createSdkAgentStream(this.compiled, tools, opts.apiKey, {
|
|
2879
2901
|
model: opts.model,
|
|
2880
2902
|
reasoningEffort: opts.reasoningEffort,
|
|
@@ -2900,6 +2922,23 @@ var AgentRunner = class {
|
|
|
2900
2922
|
});
|
|
2901
2923
|
}
|
|
2902
2924
|
/** Run the agent to a terminal result via the shared reflective loop (collect mode). */
|
|
2925
|
+
/**
|
|
2926
|
+
* Apply a per-call `maxIterations` override (M54 D4). A custom strategy must NOT be re-resolved by
|
|
2927
|
+
* name — its name is outside the `z.enum`, which would throw — and its `shouldContinue` closure
|
|
2928
|
+
* must survive; since the runner enforces the ceiling via `round < loop.maxIterations` (T1.1),
|
|
2929
|
+
* overriding just that field bounds a custom without touching its logic. A built-in keeps the
|
|
2930
|
+
* by-name re-resolution (zod fail-loud on `< 1`) — unchanged, zero-behavior.
|
|
2931
|
+
*/
|
|
2932
|
+
resolvePerRunLoop(maxIterations) {
|
|
2933
|
+
if (maxIterations == null) return this.loopStrategy;
|
|
2934
|
+
if (this.loopStrategyIsCustom) {
|
|
2935
|
+
const base = this.loopStrategy;
|
|
2936
|
+
return Object.assign(Object.create(Object.getPrototypeOf(base)), base, {
|
|
2937
|
+
maxIterations
|
|
2938
|
+
});
|
|
2939
|
+
}
|
|
2940
|
+
return resolveLoopStrategy(this.loopStrategy.name, maxIterations);
|
|
2941
|
+
}
|
|
2903
2942
|
async run(message, opts) {
|
|
2904
2943
|
const gen = this.stream(message, opts);
|
|
2905
2944
|
let res = await gen.next();
|
|
@@ -2915,6 +2954,7 @@ var AgentRunnerBuilder = class {
|
|
|
2915
2954
|
reflectionOverride;
|
|
2916
2955
|
streamEnabled = true;
|
|
2917
2956
|
compactionOverride;
|
|
2957
|
+
loopStrategyOverride;
|
|
2918
2958
|
constructor(spec) {
|
|
2919
2959
|
this.spec = spec;
|
|
2920
2960
|
}
|
|
@@ -2923,6 +2963,29 @@ var AgentRunnerBuilder = class {
|
|
|
2923
2963
|
if (strategy) this.reflectionOverride = strategy;
|
|
2924
2964
|
return this;
|
|
2925
2965
|
}
|
|
2966
|
+
/**
|
|
2967
|
+
* M54 — inject a custom terminal-decision strategy (the fourth OCP axis, alongside
|
|
2968
|
+
* `.reflection()`/`.compaction()`/`streamFactory`). WINS over the strategy the spec's name would
|
|
2969
|
+
* resolve to, exactly as `.compaction()` outranks the spec. The runner caps ANY strategy at
|
|
2970
|
+
* `custom.maxIterations` (T1.1), so a `shouldContinue: () => true` still terminates — never an
|
|
2971
|
+
* infinite loop.
|
|
2972
|
+
*
|
|
2973
|
+
* @example
|
|
2974
|
+
* ```ts
|
|
2975
|
+
* // Stop as soon as the confidence in the last round crosses 0.9, else run to the ceiling.
|
|
2976
|
+
* const stopWhenConfident: LoopStrategy = {
|
|
2977
|
+
* name: 'confident',
|
|
2978
|
+
* maxIterations: 8,
|
|
2979
|
+
* shouldContinue: (o) => !o.responseText.includes('confidence: high'),
|
|
2980
|
+
* }
|
|
2981
|
+
* AgentRunner.fromSpec(spec).loopStrategy(stopWhenConfident).build()
|
|
2982
|
+
* ```
|
|
2983
|
+
*/
|
|
2984
|
+
loopStrategy(custom) {
|
|
2985
|
+
assertValidCustomLoopStrategy(custom);
|
|
2986
|
+
this.loopStrategyOverride = custom;
|
|
2987
|
+
return this;
|
|
2988
|
+
}
|
|
2926
2989
|
/** Record the streaming preference (see {@link AgentRunner.streamEnabled}). */
|
|
2927
2990
|
stream(enabled = true) {
|
|
2928
2991
|
this.streamEnabled = enabled;
|
|
@@ -2944,7 +3007,8 @@ var AgentRunnerBuilder = class {
|
|
|
2944
3007
|
build() {
|
|
2945
3008
|
const { spec } = this;
|
|
2946
3009
|
const strategy = spec.strategy ?? "simple-chat";
|
|
2947
|
-
const
|
|
3010
|
+
const loopStrategyIsCustom = this.loopStrategyOverride !== void 0;
|
|
3011
|
+
const loopStrategy = this.loopStrategyOverride ?? resolveLoopStrategy(strategy, spec.maxIterations);
|
|
2948
3012
|
const reflectionStrategy = this.reflectionOverride ?? (strategy === "plan-act-reflect" ? ladderReflectionStrategy : noopReflectionStrategy);
|
|
2949
3013
|
const compactionDecl = this.compactionOverride ?? spec.compaction;
|
|
2950
3014
|
const compaction = compactionDecl ? resolveCompactionStrategy(compactionDecl.name, {
|
|
@@ -2954,6 +3018,7 @@ var AgentRunnerBuilder = class {
|
|
|
2954
3018
|
compiled: spec.compiled,
|
|
2955
3019
|
agentName: spec.name,
|
|
2956
3020
|
loopStrategy,
|
|
3021
|
+
loopStrategyIsCustom,
|
|
2957
3022
|
reflectionStrategy,
|
|
2958
3023
|
streamEnabled: this.streamEnabled,
|
|
2959
3024
|
compaction
|
|
@@ -3382,8 +3447,8 @@ export {
|
|
|
3382
3447
|
createSdkAgentStream,
|
|
3383
3448
|
toAgentFactory,
|
|
3384
3449
|
presentUIMessageStream,
|
|
3385
|
-
|
|
3386
|
-
|
|
3450
|
+
ContextualTool,
|
|
3451
|
+
AgentBuilder,
|
|
3387
3452
|
AgentDefinitionError,
|
|
3388
3453
|
compileAgentModule,
|
|
3389
3454
|
streamAgentUIMessages,
|
|
@@ -3424,4 +3489,4 @@ export {
|
|
|
3424
3489
|
generateAgentManifest,
|
|
3425
3490
|
agentsPlugin
|
|
3426
3491
|
};
|
|
3427
|
-
//# sourceMappingURL=chunk-
|
|
3492
|
+
//# sourceMappingURL=chunk-OKG5LRGV.js.map
|