@nylorun/harness 0.12.0-beta → 0.13.0-beta

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.
Files changed (131) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +124 -5
  3. package/dist/definition/agent-definition.d.ts +11 -0
  4. package/dist/definition/agent-definition.js +12 -0
  5. package/dist/definition/assemble.d.ts +22 -0
  6. package/dist/{build → definition}/assemble.js +6 -4
  7. package/dist/definition/bind-agent.d.ts +9 -0
  8. package/dist/definition/bind-agent.js +21 -0
  9. package/dist/{build → definition}/bind-tool.d.ts +2 -1
  10. package/dist/{build → definition}/bind-tool.js +1 -0
  11. package/dist/definition/bound.d.ts +16 -0
  12. package/dist/definition/builder.d.ts +30 -0
  13. package/dist/definition/builder.js +94 -0
  14. package/dist/definition/declaration.d.ts +3 -0
  15. package/dist/definition/declaration.js +53 -0
  16. package/dist/{build → definition}/helpers.d.ts +1 -1
  17. package/dist/{build → definition}/manifest.d.ts +3 -1
  18. package/dist/definition/manifest.js +45 -0
  19. package/dist/{session → definition}/output-contract.d.ts +2 -2
  20. package/dist/{session → definition}/output-contract.js +1 -1
  21. package/dist/definition/registry.d.ts +10 -0
  22. package/dist/definition/registry.js +48 -0
  23. package/dist/{build → definition}/schema.d.ts +4 -4
  24. package/dist/errors.d.ts +1 -1
  25. package/dist/execution/dispatch.d.ts +2 -0
  26. package/dist/execution/dispatch.js +164 -0
  27. package/dist/execution/initial-state.d.ts +3 -0
  28. package/dist/execution/initial-state.js +14 -0
  29. package/dist/execution/invocation.d.ts +21 -0
  30. package/dist/execution/invocation.js +32 -0
  31. package/dist/{model → execution/model}/adapters.d.ts +2 -2
  32. package/dist/{model → execution/model}/adapters.js +2 -2
  33. package/dist/{model → execution/model}/normalize.d.ts +2 -2
  34. package/dist/{model → execution/model}/normalize.js +2 -2
  35. package/dist/{model → execution/model}/prepared.d.ts +3 -3
  36. package/dist/{step → execution/model}/project.d.ts +1 -1
  37. package/dist/{step → execution/model}/project.js +2 -10
  38. package/dist/execution/observe.d.ts +17 -0
  39. package/dist/execution/observe.js +53 -0
  40. package/dist/execution/options.d.ts +10 -0
  41. package/dist/execution/options.js +49 -0
  42. package/dist/execution/resume.d.ts +4 -0
  43. package/dist/execution/resume.js +52 -0
  44. package/dist/execution/run.d.ts +5 -0
  45. package/dist/execution/run.js +256 -0
  46. package/dist/execution/state.d.ts +8 -0
  47. package/dist/execution/state.js +208 -0
  48. package/dist/{step → execution/step}/canonicalize.d.ts +2 -2
  49. package/dist/{step → execution/step}/canonicalize.js +2 -2
  50. package/dist/{step → execution/step}/compose.d.ts +3 -2
  51. package/dist/{step → execution/step}/compose.js +5 -5
  52. package/dist/{step → execution/step}/context-draft.d.ts +3 -5
  53. package/dist/{step → execution/step}/context-draft.js +5 -22
  54. package/dist/execution/step/describe.d.ts +9 -0
  55. package/dist/execution/step/describe.js +22 -0
  56. package/dist/{step → execution/step}/model-configuration.d.ts +4 -3
  57. package/dist/{step → execution/step}/model-configuration.js +3 -3
  58. package/dist/execution/step/resolve.d.ts +11 -0
  59. package/dist/{step → execution/step}/resolve.js +5 -3
  60. package/dist/execution/step/run.d.ts +29 -0
  61. package/dist/{step → execution/step}/run.js +19 -53
  62. package/dist/execution/step/runtime.d.ts +32 -0
  63. package/dist/execution/step/runtime.js +1 -0
  64. package/dist/{step → execution/step}/seal.d.ts +13 -8
  65. package/dist/{step → execution/step}/seal.js +8 -6
  66. package/dist/{step → execution/step}/slot-assembly.d.ts +4 -4
  67. package/dist/{step → execution/step}/slot-assembly.js +4 -4
  68. package/dist/{step → execution/step}/step-context.d.ts +11 -12
  69. package/dist/{step → execution/step}/step-context.js +5 -27
  70. package/dist/execution/tool-result.d.ts +4 -0
  71. package/dist/execution/tool-result.js +30 -0
  72. package/dist/execution/transcript.d.ts +2 -0
  73. package/dist/{session/seed.js → execution/transcript.js} +9 -39
  74. package/dist/index.d.ts +15 -12
  75. package/dist/index.js +5 -5
  76. package/dist/types/agent.d.ts +9 -0
  77. package/dist/types/agent.js +1 -0
  78. package/dist/types/execution.d.ts +97 -0
  79. package/dist/types/execution.js +1 -0
  80. package/dist/types/manifest.d.ts +16 -13
  81. package/dist/types/middleware.d.ts +11 -40
  82. package/dist/types/model.d.ts +10 -10
  83. package/dist/types/observe.d.ts +165 -0
  84. package/dist/types/observe.js +1 -0
  85. package/dist/types/shared.d.ts +1 -225
  86. package/dist/types/tool.d.ts +19 -20
  87. package/dist/types/transcript.d.ts +65 -0
  88. package/dist/types/transcript.js +1 -0
  89. package/dist/utils/canonical.d.ts +1 -0
  90. package/dist/utils/canonical.js +10 -0
  91. package/dist/utils/immutable.d.ts +1 -0
  92. package/dist/utils/immutable.js +8 -0
  93. package/package.json +3 -3
  94. package/dist/build/agent.d.ts +0 -17
  95. package/dist/build/agent.js +0 -32
  96. package/dist/build/assemble.d.ts +0 -7
  97. package/dist/build/builder.d.ts +0 -38
  98. package/dist/build/builder.js +0 -136
  99. package/dist/build/manifest.js +0 -20
  100. package/dist/session/capability-state.d.ts +0 -14
  101. package/dist/session/capability-state.js +0 -67
  102. package/dist/session/event-log.d.ts +0 -12
  103. package/dist/session/event-log.js +0 -63
  104. package/dist/session/input-queue.d.ts +0 -41
  105. package/dist/session/input-queue.js +0 -94
  106. package/dist/session/record.d.ts +0 -11
  107. package/dist/session/record.js +0 -26
  108. package/dist/session/scheduler.d.ts +0 -62
  109. package/dist/session/scheduler.js +0 -475
  110. package/dist/session/seed.d.ts +0 -10
  111. package/dist/session/session.d.ts +0 -17
  112. package/dist/session/session.js +0 -101
  113. package/dist/session/state.d.ts +0 -15
  114. package/dist/session/state.js +0 -48
  115. package/dist/session/submission-stream.d.ts +0 -16
  116. package/dist/session/submission-stream.js +0 -46
  117. package/dist/step/resolve.d.ts +0 -10
  118. package/dist/step/run.d.ts +0 -33
  119. package/dist/turn/plan-runner.d.ts +0 -72
  120. package/dist/turn/plan-runner.js +0 -389
  121. package/dist/turn/runner.d.ts +0 -69
  122. package/dist/turn/runner.js +0 -159
  123. package/dist/types/session.d.ts +0 -230
  124. package/dist/utils/maps.d.ts +0 -1
  125. package/dist/utils/maps.js +0 -37
  126. package/dist/utils/observe.d.ts +0 -3
  127. package/dist/utils/observe.js +0 -13
  128. /package/dist/{types/session.js → definition/bound.js} +0 -0
  129. /package/dist/{build → definition}/helpers.js +0 -0
  130. /package/dist/{build → definition}/schema.js +0 -0
  131. /package/dist/{model → execution/model}/prepared.js +0 -0
@@ -1,6 +1,3 @@
1
- import type { ContextSnapshot, ModelCandidate, ModelCall, ModelConfigurationSnapshot } from "./model.js";
2
- import type { InputEvent, TranscriptEntry } from "./session.js";
3
- import type { RequiredInteraction, ToolResult } from "./tool.js";
4
1
  export type JsonPrimitive = string | number | boolean | null;
5
2
  export type JsonValue = JsonPrimitive | JsonObject | readonly JsonValue[];
6
3
  export type JsonObject = {
@@ -24,226 +21,5 @@ export interface ContextItem {
24
21
  export interface Tripwire {
25
22
  readonly code: string;
26
23
  readonly message: string;
27
- readonly scope?: "step" | "session";
24
+ readonly scope?: "step" | "execution";
28
25
  }
29
- export interface ObserveToolSnapshot {
30
- readonly name: string;
31
- readonly description?: string;
32
- readonly owner: {
33
- readonly middlewareId: string;
34
- readonly slot: string;
35
- };
36
- readonly inputSchema: {
37
- readonly jsonSchema: JsonObject;
38
- };
39
- readonly outputSchema?: {
40
- readonly jsonSchema: JsonObject;
41
- };
42
- }
43
- /** JSON-only model-configuration view suitable for an observation stream. */
44
- export interface ObserveModelConfigurationSnapshot extends Omit<ModelConfigurationSnapshot, "tools"> {
45
- readonly tools: readonly ObserveToolSnapshot[];
46
- }
47
- export interface ObserveSealedCall {
48
- readonly callId: string;
49
- readonly toolName: string;
50
- readonly args: JsonValue;
51
- readonly invocationId: string;
52
- readonly owner: {
53
- readonly middlewareId: string;
54
- readonly slot: string;
55
- };
56
- readonly interaction?: RequiredInteraction;
57
- }
58
- export interface ObserveModelRequested {
59
- /** The exact immutable logical input supplied to the ModelAdapter. */
60
- readonly call: ModelCall;
61
- /** Canonical configuration facts and attribution. */
62
- readonly configuration: ObserveModelConfigurationSnapshot;
63
- /** Current-step runtime context and attribution. */
64
- readonly context: ContextSnapshot;
65
- }
66
- export type ObserveEvent = {
67
- readonly type: "session.stopped";
68
- readonly reason: string;
69
- } | {
70
- readonly type: "capability.state.dispose.failed";
71
- readonly capabilityId: string;
72
- readonly attributes: {
73
- readonly message: string;
74
- };
75
- } | {
76
- readonly type: "session.seeded";
77
- readonly revision: number;
78
- readonly transcriptEntries: number;
79
- } | {
80
- readonly type: "session.continued";
81
- readonly inputId?: string;
82
- } | {
83
- readonly type: "session.record.failed";
84
- readonly code: string;
85
- readonly attributes: {
86
- readonly message: string;
87
- };
88
- } | {
89
- readonly type: "input.received";
90
- readonly inputId: string;
91
- readonly kind: string;
92
- } | {
93
- readonly type: "input.queued";
94
- readonly inputId: string;
95
- } | {
96
- readonly type: "input.rejected";
97
- readonly inputId: string;
98
- readonly reason: string;
99
- } | {
100
- readonly type: "input.cancelled";
101
- readonly inputId: string;
102
- readonly reason: string;
103
- } | {
104
- readonly type: "model.requested";
105
- readonly turnId: string;
106
- readonly stepId: string;
107
- readonly inputId?: string;
108
- readonly requestedModelId?: string;
109
- readonly attributes: ObserveModelRequested;
110
- } | {
111
- readonly type: "model.prepared";
112
- readonly turnId: string;
113
- readonly stepId: string;
114
- readonly inputId?: string;
115
- readonly requestedModelId?: string;
116
- readonly attributes: {
117
- readonly adapter: string;
118
- readonly call: JsonValue;
119
- };
120
- } | {
121
- readonly type: "model.completed";
122
- readonly turnId: string;
123
- readonly stepId: string;
124
- readonly inputId?: string;
125
- readonly requestedModelId?: string;
126
- readonly attributes: ModelCandidate;
127
- } | {
128
- readonly type: "model.deferred";
129
- readonly turnId: string;
130
- readonly stepId: string;
131
- readonly inputId?: string;
132
- readonly invocationId: string;
133
- readonly attributes: {
134
- readonly token?: JsonValue;
135
- };
136
- } | {
137
- readonly type: "step.started";
138
- readonly turnId: string;
139
- readonly stepId: string;
140
- readonly inputId?: string;
141
- readonly turnNumber: number;
142
- readonly stepNumber: number;
143
- readonly attributes: {
144
- readonly session?: {
145
- readonly userId?: string;
146
- readonly context?: JsonObject;
147
- };
148
- readonly arrivals: readonly InputEvent[];
149
- readonly toolResults: readonly ToolResult[];
150
- readonly transcript: readonly TranscriptEntry[];
151
- };
152
- } | {
153
- readonly type: "middleware.entered" | "middleware.completed";
154
- readonly turnId: string;
155
- readonly stepId: string;
156
- readonly inputId?: string;
157
- readonly middlewareId: string;
158
- } | {
159
- readonly type: "middleware.lease-violation";
160
- readonly turnId: string;
161
- readonly stepId: string;
162
- readonly inputId?: string;
163
- readonly middlewareId: string;
164
- readonly reason: string;
165
- } | {
166
- readonly type: "tool.sealed";
167
- readonly turnId: string;
168
- readonly stepId: string;
169
- readonly inputId?: string;
170
- readonly attributes: {
171
- readonly executable: readonly ObserveSealedCall[];
172
- readonly immediate: readonly ToolResult[];
173
- };
174
- } | {
175
- readonly type: "tool.started";
176
- readonly sessionId: string;
177
- readonly turnId: string;
178
- readonly stepId: string;
179
- readonly inputId?: string;
180
- readonly toolName: string;
181
- readonly callId: string;
182
- readonly invocationId: string;
183
- readonly middlewareId: string;
184
- readonly slot: string;
185
- readonly attributes: {
186
- readonly args: JsonValue;
187
- };
188
- } | {
189
- readonly type: "tool.completed";
190
- readonly sessionId: string;
191
- readonly turnId: string;
192
- readonly stepId: string;
193
- readonly inputId?: string;
194
- readonly toolName: string;
195
- readonly callId: string;
196
- readonly invocationId: string;
197
- readonly middlewareId: string;
198
- readonly slot: string;
199
- readonly outcome: string;
200
- readonly code?: string;
201
- readonly attributes?: ToolResult;
202
- } | {
203
- readonly type: "tool.deferred";
204
- readonly sessionId: string;
205
- readonly turnId: string;
206
- readonly stepId: string;
207
- readonly inputId?: string;
208
- readonly toolName: string;
209
- readonly callId: string;
210
- readonly invocationId: string;
211
- readonly middlewareId: string;
212
- readonly slot: string;
213
- readonly attributes: {
214
- readonly token?: JsonValue;
215
- };
216
- } | {
217
- readonly type: "turn.completed";
218
- readonly turnId: string;
219
- readonly stepId: string;
220
- readonly inputId?: string;
221
- readonly attributes: {
222
- readonly output: JsonValue;
223
- };
224
- } | {
225
- readonly type: "interaction.required";
226
- readonly turnId: string;
227
- readonly stepId: string;
228
- readonly inputId?: string;
229
- readonly interactionId: string;
230
- readonly kind: string;
231
- readonly callId?: string;
232
- readonly toolName?: string;
233
- readonly phase?: "interaction" | "execute";
234
- readonly attributes: {
235
- readonly prompt: string;
236
- readonly metadata?: JsonObject;
237
- };
238
- } | {
239
- readonly type: "tripwire";
240
- readonly turnId: string;
241
- readonly stepId?: string;
242
- readonly inputId?: string;
243
- readonly code: string;
244
- readonly scope: string;
245
- readonly attributes: {
246
- readonly message: string;
247
- };
248
- };
249
- export type Observer = (event: ObserveEvent) => void | Promise<void>;
@@ -43,21 +43,13 @@ export type ToolSchemaSource<T = unknown> = ZodType<T> | StandardToolSchema<any,
43
43
  export type SchemaOutput<Schema> = Schema extends ZodType<infer Value> ? Value : Schema extends StandardToolSchema<any, infer Value> ? Value : Schema extends ToolSchema<infer Value> ? Value : never;
44
44
  export type ToolInputSchema = ToolSchemaSource;
45
45
  export type ToolOutputSchema = ToolSchemaSource;
46
- export interface BoundToolSchema<T> extends ToolSchema<T> {
47
- }
48
46
  type OutputFor<Schema> = Schema extends ToolSchemaSource ? SchemaOutput<Schema> : ToolContent;
49
- export interface ToolDefinition<InputSchema extends ToolInputSchema = ToolInputSchema, State = never, OutputSchema extends ToolOutputSchema | undefined = undefined> {
47
+ export interface ToolDefinition<InputSchema extends ToolInputSchema = ToolInputSchema, Info = unknown, OutputSchema extends ToolOutputSchema | undefined = undefined> {
50
48
  readonly name: string;
51
49
  readonly description?: string;
52
50
  readonly inputSchema: InputSchema;
53
51
  readonly outputSchema?: OutputSchema;
54
- execute(args: SchemaOutput<InputSchema>, context: ToolExecutionContext<State>): Promise<ToolOutcome<OutputFor<OutputSchema>>>;
55
- }
56
- export interface BoundToolDefinition<State = never> extends Omit<ToolDefinition<ToolInputSchema, State, ToolOutputSchema | undefined>, "inputSchema" | "outputSchema" | "execute"> {
57
- readonly inputSchema: BoundToolSchema<unknown>;
58
- readonly outputSchema?: BoundToolSchema<unknown>;
59
- readonly execute: (args: unknown, context: ToolExecutionContext<State>) => Promise<ToolOutcome>;
60
- readonly owner: ToolOwner;
52
+ execute(args: SchemaOutput<InputSchema>, context: ToolExecutionContext<Info>): Promise<ToolOutcome<OutputFor<OutputSchema>>>;
61
53
  }
62
54
  export interface ToolOwner {
63
55
  readonly middlewareId: string;
@@ -79,25 +71,20 @@ export interface ToolExecutionResume {
79
71
  readonly value?: JsonValue;
80
72
  readonly token?: JsonValue;
81
73
  }
82
- export interface SealedToolCall {
83
- readonly callId: string;
84
- readonly toolName: string;
85
- readonly args: JsonValue;
86
- }
87
74
  interface ToolExecutionContextBase {
88
- readonly sessionId: string;
75
+ readonly executionId: string;
89
76
  readonly turnId: string;
90
77
  readonly stepId: string;
91
78
  readonly callId: string;
92
79
  readonly invocationId: string;
93
80
  readonly signal: AbortSignal;
94
- /** The session's model callable, for tools that explicitly run a child agent. */
81
+ /** The invocation's model callable, for tools that explicitly run a child agent. */
95
82
  readonly onModelCall?: ModelAdapter;
96
83
  readonly resume?: ToolExecutionResume;
97
84
  }
98
- export type ToolExecutionContext<State = never> = ToolExecutionContextBase & ([State] extends [never] ? object : {
99
- readonly state: Promise<State>;
100
- });
85
+ export type ToolExecutionContext<Info = unknown> = ToolExecutionContextBase & {
86
+ readonly info?: Info;
87
+ };
101
88
  export type ToolContent = JsonValue;
102
89
  export type ToolOutcome<Output = ToolContent> = {
103
90
  readonly kind: "completed";
@@ -136,4 +123,16 @@ export type ToolResult = {
136
123
  readonly message: string;
137
124
  readonly details?: ToolValidationFailureDetails;
138
125
  };
126
+ /** Immutable tool metadata supplied to model adapters and observers. */
127
+ export interface ToolDescriptor {
128
+ readonly name: string;
129
+ readonly description?: string;
130
+ readonly owner: ToolOwner;
131
+ readonly inputSchema: {
132
+ readonly jsonSchema: JsonObject;
133
+ };
134
+ readonly outputSchema?: {
135
+ readonly jsonSchema: JsonObject;
136
+ };
137
+ }
139
138
  export {};
@@ -0,0 +1,65 @@
1
+ import type { ModelCandidate } from "./model.js";
2
+ import type { JsonObject, JsonValue } from "./shared.js";
3
+ import type { ToolResult } from "./tool.js";
4
+ export type InputEvent = {
5
+ readonly kind: "user-message" | "interrupt";
6
+ readonly text: string;
7
+ readonly metadata?: JsonObject;
8
+ } | {
9
+ readonly kind: "user-message" | "interrupt";
10
+ readonly content: readonly UserContentPart[];
11
+ /** Undefined for content-bearing events; retained for text-event narrowing compatibility. */
12
+ readonly text?: undefined;
13
+ readonly metadata?: JsonObject;
14
+ } | {
15
+ readonly kind: "approve";
16
+ readonly interactionId: string;
17
+ readonly approved: boolean;
18
+ } | {
19
+ readonly kind: "respond";
20
+ readonly interactionId: string;
21
+ readonly value: JsonValue;
22
+ };
23
+ /** Ordered, model-visible user content. Media references stay host-owned JSON values. */
24
+ export type UserContentPart = {
25
+ readonly type: "text";
26
+ readonly text: string;
27
+ } | {
28
+ readonly type: "media";
29
+ readonly mediaType: string;
30
+ readonly reference: JsonValue;
31
+ };
32
+ export type MessageInput = string | {
33
+ readonly text: string;
34
+ readonly metadata?: JsonObject;
35
+ } | {
36
+ readonly content: readonly UserContentPart[];
37
+ readonly metadata?: JsonObject;
38
+ };
39
+ export type InteractionReply = Extract<InputEvent, {
40
+ kind: "approve" | "respond";
41
+ }>;
42
+ export interface TranscriptInputEntry {
43
+ readonly kind: "input";
44
+ readonly turnId: string;
45
+ readonly event: InputEvent;
46
+ }
47
+ export interface TranscriptCandidateEntry {
48
+ readonly kind: "candidate";
49
+ readonly turnId: string;
50
+ readonly stepId: string;
51
+ readonly candidate: ModelCandidate;
52
+ }
53
+ export interface TranscriptToolsEntry {
54
+ readonly kind: "tool-results";
55
+ readonly turnId: string;
56
+ readonly stepId: string;
57
+ readonly results: readonly ToolResult[];
58
+ }
59
+ export interface TranscriptFinalEntry {
60
+ readonly kind: "final";
61
+ readonly turnId: string;
62
+ readonly stepId: string;
63
+ readonly output: JsonValue;
64
+ }
65
+ export type TranscriptEntry = TranscriptInputEntry | TranscriptCandidateEntry | TranscriptToolsEntry | TranscriptFinalEntry;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare function canonical(value: unknown): string;
@@ -0,0 +1,10 @@
1
+ export function canonical(value) {
2
+ if (Array.isArray(value))
3
+ return `[${value.map(canonical).join(",")}]`;
4
+ if (value && typeof value === "object")
5
+ return `{${Object.entries(value)
6
+ .sort(([a], [b]) => a.localeCompare(b))
7
+ .map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`)
8
+ .join(",")}}`;
9
+ return JSON.stringify(value);
10
+ }
@@ -3,3 +3,4 @@ export declare function assertJson(value: unknown, path?: string): asserts value
3
3
  export declare function copyJson<T>(value: T): T;
4
4
  export declare function copyJsonObject(value: unknown, path: string): JsonObject;
5
5
  export declare function deepFreeze<T>(value: T): T;
6
+ export declare function freezeGraph<T>(value: T, seen?: WeakSet<object>): T;
@@ -66,3 +66,11 @@ export function deepFreeze(value) {
66
66
  }
67
67
  return value;
68
68
  }
69
+ export function freezeGraph(value, seen = new WeakSet()) {
70
+ if (!value || typeof value !== "object" || seen.has(value))
71
+ return value;
72
+ seen.add(value);
73
+ for (const item of Object.values(value))
74
+ freezeGraph(item, seen);
75
+ return Object.freeze(value);
76
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nylorun/harness",
3
- "version": "0.12.0-beta",
3
+ "version": "0.13.0-beta",
4
4
  "description": "Nylorun's TypeScript agent runtime. See github.com/nylorun/harness.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -29,8 +29,8 @@
29
29
  "import": "./dist/index.js"
30
30
  },
31
31
  "./model/adapters": {
32
- "types": "./dist/model/adapters.d.ts",
33
- "import": "./dist/model/adapters.js"
32
+ "types": "./dist/execution/model/adapters.d.ts",
33
+ "import": "./dist/execution/model/adapters.js"
34
34
  }
35
35
  },
36
36
  "files": [
@@ -1,17 +0,0 @@
1
- import type { AgentManifest } from "../types/manifest.js";
2
- import type { BoundMiddleware } from "../types/middleware.js";
3
- import type { ModelAdapter } from "../types/model.js";
4
- import type { Session, SessionRunOptions } from "../types/session.js";
5
- export interface LoopAgent {
6
- readonly middleware: readonly BoundMiddleware[];
7
- readonly invoke: ModelAdapter;
8
- }
9
- export declare class BuiltAgent {
10
- readonly middleware: readonly BoundMiddleware[];
11
- readonly manifest: AgentManifest;
12
- readonly id: string;
13
- readonly name: string;
14
- private constructor();
15
- run(options: SessionRunOptions): Session;
16
- }
17
- export declare function bindAgent(middleware: readonly BoundMiddleware[], manifest: AgentManifest): BuiltAgent;
@@ -1,32 +0,0 @@
1
- import { LiveSession } from "../session/session.js";
2
- import { createId } from "../utils/ids.js";
3
- import { HarnessError } from "../errors.js";
4
- let createBoundAgent;
5
- export class BuiltAgent {
6
- middleware;
7
- manifest;
8
- id;
9
- name;
10
- constructor(middleware, manifest) {
11
- this.middleware = middleware;
12
- this.manifest = manifest;
13
- this.id = manifest.id;
14
- this.name = manifest.name;
15
- }
16
- static {
17
- createBoundAgent = (middleware, manifest) => new BuiltAgent(middleware, manifest);
18
- }
19
- run(options) {
20
- const seeded = "seed" in options && options.seed !== undefined;
21
- if (seeded &&
22
- (("id" in options && options.id !== undefined) ||
23
- ("userId" in options && options.userId !== undefined) ||
24
- ("context" in options && options.context !== undefined)))
25
- throw new HarnessError("session.invalid-seed", "Seeded run options cannot include id, userId, or context outside the seed");
26
- const id = (seeded ? options.seed.id : "id" in options ? options.id : undefined) ?? createId("session");
27
- return new LiveSession(id, Object.freeze({ middleware: this.middleware, invoke: options.onModelCall }), options);
28
- }
29
- }
30
- export function bindAgent(middleware, manifest) {
31
- return createBoundAgent(middleware, manifest);
32
- }
@@ -1,7 +0,0 @@
1
- import type { BuildResult } from "../types/manifest.js";
2
- import type { BoundMiddleware } from "../types/middleware.js";
3
- import { type BuiltAgent } from "./agent.js";
4
- export declare function assembleAgent(middleware: readonly BoundMiddleware[], identity: Readonly<{
5
- id: string;
6
- name: string;
7
- }>): BuildResult<BuiltAgent>;
@@ -1,38 +0,0 @@
1
- import type { BoundMiddleware, CapabilityDeclaration, StepMiddleware } from "../types/middleware.js";
2
- import type { BuildDiagnostic } from "../types/shared.js";
3
- import { HarnessError } from "../errors.js";
4
- import type { BuiltAgent } from "./agent.js";
5
- export interface AgentOptions {
6
- readonly id: string;
7
- readonly name: string;
8
- readonly instructions?: string | readonly string[];
9
- }
10
- interface BuilderState {
11
- readonly id: string;
12
- readonly name: string;
13
- readonly middleware: BoundMiddleware[];
14
- sealed: boolean;
15
- agent?: BuiltAgent;
16
- error?: AgentBuildError;
17
- middlewareSeq: number;
18
- }
19
- export declare class AgentBuildError extends HarnessError {
20
- readonly diagnostics: readonly BuildDiagnostic[];
21
- constructor(diagnostics: readonly BuildDiagnostic[]);
22
- }
23
- export declare class AgentLifecycleError extends HarnessError {
24
- constructor(message: string);
25
- }
26
- export declare function Agent(options: AgentOptions): AgentBuilder;
27
- export declare class AgentBuilder {
28
- private readonly state;
29
- constructor(state: BuilderState);
30
- use(middleware: StepMiddleware): this;
31
- use(id: string, middleware: StepMiddleware): this;
32
- use<State>(declaration: CapabilityDeclaration<State>): this;
33
- build(): BuiltAgent;
34
- private nextMiddlewareId;
35
- private push;
36
- private assertOpen;
37
- }
38
- export {};
@@ -1,136 +0,0 @@
1
- import { HarnessError } from "../errors.js";
2
- import { assembleAgent } from "./assemble.js";
3
- export class AgentBuildError extends HarnessError {
4
- diagnostics;
5
- constructor(diagnostics) {
6
- super("agent.build-failed", diagnostics.map((item) => item.message).join("; ") || "Agent build failed");
7
- this.diagnostics = diagnostics;
8
- this.name = "AgentBuildError";
9
- }
10
- }
11
- export class AgentLifecycleError extends HarnessError {
12
- constructor(message) {
13
- super("agent.lifecycle-sealed", message);
14
- this.name = "AgentLifecycleError";
15
- }
16
- }
17
- export function Agent(options) {
18
- return new AgentBuilder(createState(options));
19
- }
20
- export class AgentBuilder {
21
- state;
22
- constructor(state) {
23
- this.state = state;
24
- }
25
- use(idOrMiddleware, middleware) {
26
- if (typeof idOrMiddleware === "function") {
27
- return this.push({ id: this.nextMiddlewareId(), handle: idOrMiddleware });
28
- }
29
- if (typeof idOrMiddleware === "object")
30
- return this.push(compileDeclaration(idOrMiddleware));
31
- return this.push({ id: idOrMiddleware, handle: middleware });
32
- }
33
- build() {
34
- if (this.state.agent)
35
- return this.state.agent;
36
- if (this.state.error)
37
- throw this.state.error;
38
- this.state.sealed = true;
39
- const result = assembleAgent(this.state.middleware, {
40
- id: this.state.id,
41
- name: this.state.name,
42
- });
43
- if (!result.ok) {
44
- this.state.error = new AgentBuildError(result.diagnostics);
45
- throw this.state.error;
46
- }
47
- this.state.agent = result.agent;
48
- return this.state.agent;
49
- }
50
- nextMiddlewareId() {
51
- const taken = new Set(this.state.middleware.map((item) => item.id));
52
- let id;
53
- do {
54
- this.state.middlewareSeq += 1;
55
- id = `middleware-${this.state.middlewareSeq}`;
56
- } while (taken.has(id));
57
- return id;
58
- }
59
- push(entry) {
60
- this.assertOpen();
61
- this.state.middleware.push(entry);
62
- return this;
63
- }
64
- assertOpen() {
65
- if (this.state.sealed)
66
- throw new AgentLifecycleError("AgentBuilder cannot be changed after build()");
67
- }
68
- }
69
- function createState(options) {
70
- const middleware = [];
71
- if (options.instructions !== undefined) {
72
- const instructions = typeof options.instructions === "string" ? [options.instructions] : options.instructions;
73
- middleware.push(compileDeclaration({ id: "agent", instructions }));
74
- }
75
- return {
76
- id: options.id,
77
- name: options.name,
78
- middleware,
79
- sealed: false,
80
- middlewareSeq: 0,
81
- };
82
- }
83
- function compileDeclaration(declaration) {
84
- const tools = copyItems(declaration.tools, declaration.id);
85
- const instructions = copyItems(declaration.instructions, declaration.id);
86
- const model = declaration.model;
87
- const contributions = snapshotContributions(instructions?.items, tools?.items, model);
88
- const handle = async (request, next) => {
89
- if (tools)
90
- request.configuration.tools.set(tools.slot, tools.items);
91
- if (instructions)
92
- request.configuration.instructions.set(instructions.slot, instructions.items);
93
- if (model)
94
- request.configuration.model.select(model);
95
- return declaration.middleware ? declaration.middleware(request, next) : next();
96
- };
97
- return {
98
- id: declaration.id,
99
- handle,
100
- ...(declaration.state === undefined
101
- ? {}
102
- : { state: declaration.state }),
103
- ...(contributions === undefined ? {} : { contributions }),
104
- };
105
- }
106
- function snapshotContributions(instructions, tools, model) {
107
- const snapInstructions = instructions === undefined ? undefined : Object.freeze([...instructions]);
108
- const snapTools = tools === undefined
109
- ? undefined
110
- : Object.freeze(tools.map((tool) => Object.freeze({
111
- name: tool.name,
112
- ...(tool.description === undefined ? {} : { description: tool.description }),
113
- })));
114
- const snapModel = model === undefined ? undefined : snapshotModel(model);
115
- if (snapInstructions === undefined && snapTools === undefined && snapModel === undefined) {
116
- return undefined;
117
- }
118
- return Object.freeze({
119
- ...(snapInstructions === undefined ? {} : { instructions: snapInstructions }),
120
- ...(snapTools === undefined ? {} : { tools: snapTools }),
121
- ...(snapModel === undefined ? {} : { model: snapModel }),
122
- });
123
- }
124
- function snapshotModel(model) {
125
- return Object.freeze({
126
- ...(model.id === undefined ? {} : { id: model.id }),
127
- ...(model.controls === undefined ? {} : { controls: Object.freeze({ ...model.controls }) }),
128
- });
129
- }
130
- function copyItems(value, defaultSlot) {
131
- if (value === undefined)
132
- return undefined;
133
- if (!("items" in value))
134
- return Object.freeze({ slot: defaultSlot, items: Object.freeze([...value]) });
135
- return Object.freeze({ slot: value.slot, items: Object.freeze([...value.items]) });
136
- }
@@ -1,20 +0,0 @@
1
- import { deepFreeze } from "../utils/immutable.js";
2
- export function createManifest(input) {
3
- const middleware = input.middleware.map((item) => projectMiddleware(item));
4
- return deepFreeze({
5
- id: input.id,
6
- name: input.name,
7
- middleware,
8
- });
9
- }
10
- function projectMiddleware(item) {
11
- const contributions = item.contributions;
12
- return {
13
- id: item.id,
14
- ...(contributions?.instructions === undefined
15
- ? {}
16
- : { instructions: contributions.instructions }),
17
- ...(contributions?.tools === undefined ? {} : { tools: contributions.tools }),
18
- ...(contributions?.model === undefined ? {} : { model: contributions.model }),
19
- };
20
- }