@ellyco/agentic 0.1.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.
Files changed (103) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +560 -0
  3. package/dist/graphs/graph.d.ts +218 -0
  4. package/dist/graphs/graph.d.ts.map +1 -0
  5. package/dist/graphs/graph.js +334 -0
  6. package/dist/graphs/graph.js.map +1 -0
  7. package/dist/graphs/index.d.ts +7 -0
  8. package/dist/graphs/index.d.ts.map +1 -0
  9. package/dist/graphs/index.js +15 -0
  10. package/dist/graphs/index.js.map +1 -0
  11. package/dist/graphs/iterator.d.ts +138 -0
  12. package/dist/graphs/iterator.d.ts.map +1 -0
  13. package/dist/graphs/iterator.js +184 -0
  14. package/dist/graphs/iterator.js.map +1 -0
  15. package/dist/graphs/merge-state.d.ts +22 -0
  16. package/dist/graphs/merge-state.d.ts.map +1 -0
  17. package/dist/graphs/merge-state.js +56 -0
  18. package/dist/graphs/merge-state.js.map +1 -0
  19. package/dist/graphs/node-sequence.d.ts +63 -0
  20. package/dist/graphs/node-sequence.d.ts.map +1 -0
  21. package/dist/graphs/node-sequence.js +84 -0
  22. package/dist/graphs/node-sequence.js.map +1 -0
  23. package/dist/graphs/registry.d.ts +5 -0
  24. package/dist/graphs/registry.d.ts.map +1 -0
  25. package/dist/graphs/registry.js +6 -0
  26. package/dist/graphs/registry.js.map +1 -0
  27. package/dist/graphs/runtime-context.d.ts +189 -0
  28. package/dist/graphs/runtime-context.d.ts.map +1 -0
  29. package/dist/graphs/runtime-context.js +254 -0
  30. package/dist/graphs/runtime-context.js.map +1 -0
  31. package/dist/graphs/state-machine.d.ts +105 -0
  32. package/dist/graphs/state-machine.d.ts.map +1 -0
  33. package/dist/graphs/state-machine.js +130 -0
  34. package/dist/graphs/state-machine.js.map +1 -0
  35. package/dist/graphs/store/base-store.d.ts +90 -0
  36. package/dist/graphs/store/base-store.d.ts.map +1 -0
  37. package/dist/graphs/store/base-store.js +50 -0
  38. package/dist/graphs/store/base-store.js.map +1 -0
  39. package/dist/graphs/store/sqlite-store.d.ts +88 -0
  40. package/dist/graphs/store/sqlite-store.d.ts.map +1 -0
  41. package/dist/graphs/store/sqlite-store.js +109 -0
  42. package/dist/graphs/store/sqlite-store.js.map +1 -0
  43. package/dist/graphs/store/stored-run.d.ts +77 -0
  44. package/dist/graphs/store/stored-run.d.ts.map +1 -0
  45. package/dist/graphs/store/stored-run.js +88 -0
  46. package/dist/graphs/store/stored-run.js.map +1 -0
  47. package/dist/graphs/types.d.ts +15 -0
  48. package/dist/graphs/types.d.ts.map +1 -0
  49. package/dist/graphs/types.js +3 -0
  50. package/dist/graphs/types.js.map +1 -0
  51. package/dist/messages/index.d.ts +6 -0
  52. package/dist/messages/index.d.ts.map +1 -0
  53. package/dist/messages/index.js +19 -0
  54. package/dist/messages/index.js.map +1 -0
  55. package/dist/messages/message.d.ts +143 -0
  56. package/dist/messages/message.d.ts.map +1 -0
  57. package/dist/messages/message.js +172 -0
  58. package/dist/messages/message.js.map +1 -0
  59. package/dist/messages/tool.d.ts +160 -0
  60. package/dist/messages/tool.d.ts.map +1 -0
  61. package/dist/messages/tool.js +173 -0
  62. package/dist/messages/tool.js.map +1 -0
  63. package/dist/models/BaseModel.d.ts +232 -0
  64. package/dist/models/BaseModel.d.ts.map +1 -0
  65. package/dist/models/BaseModel.js +247 -0
  66. package/dist/models/BaseModel.js.map +1 -0
  67. package/dist/models/BedrockModel.d.ts +112 -0
  68. package/dist/models/BedrockModel.d.ts.map +1 -0
  69. package/dist/models/BedrockModel.js +315 -0
  70. package/dist/models/BedrockModel.js.map +1 -0
  71. package/dist/models/TestModel.d.ts +135 -0
  72. package/dist/models/TestModel.d.ts.map +1 -0
  73. package/dist/models/TestModel.js +191 -0
  74. package/dist/models/TestModel.js.map +1 -0
  75. package/dist/nodes/function-node.d.ts +59 -0
  76. package/dist/nodes/function-node.d.ts.map +1 -0
  77. package/dist/nodes/function-node.js +72 -0
  78. package/dist/nodes/function-node.js.map +1 -0
  79. package/dist/nodes/index.d.ts +4 -0
  80. package/dist/nodes/index.d.ts.map +1 -0
  81. package/dist/nodes/index.js +9 -0
  82. package/dist/nodes/index.js.map +1 -0
  83. package/dist/nodes/interrupt-node.d.ts +51 -0
  84. package/dist/nodes/interrupt-node.d.ts.map +1 -0
  85. package/dist/nodes/interrupt-node.js +65 -0
  86. package/dist/nodes/interrupt-node.js.map +1 -0
  87. package/dist/nodes/model-node.d.ts +72 -0
  88. package/dist/nodes/model-node.d.ts.map +1 -0
  89. package/dist/nodes/model-node.js +80 -0
  90. package/dist/nodes/model-node.js.map +1 -0
  91. package/dist/nodes/types.d.ts +5 -0
  92. package/dist/nodes/types.d.ts.map +1 -0
  93. package/dist/nodes/types.js +3 -0
  94. package/dist/nodes/types.js.map +1 -0
  95. package/dist/tools.d.ts +65 -0
  96. package/dist/tools.d.ts.map +1 -0
  97. package/dist/tools.js +56 -0
  98. package/dist/tools.js.map +1 -0
  99. package/dist/types.d.ts +17 -0
  100. package/dist/types.d.ts.map +1 -0
  101. package/dist/types.js +3 -0
  102. package/dist/types.js.map +1 -0
  103. package/package.json +32 -0
@@ -0,0 +1,232 @@
1
+ import * as z from "zod";
2
+ import { AgentMessage, MessageContent, ModelMessages, SystemMessage, ToolRequest } from "../messages";
3
+ import { ToolDefinition } from "../tools";
4
+ /**
5
+ * Configuration options for model initialization.
6
+ *
7
+ * @interface BaseModelConfig
8
+ * @property {number} [temperature] - Controls randomness/creativity (0-1). Lower = more deterministic
9
+ * @property {number} [topP] - Nucleus sampling parameter (0-1). Controls diversity of output
10
+ * @property {number} [maxTokens] - Maximum number of tokens in the response
11
+ */
12
+ export interface BaseModelConfig {
13
+ temperature?: number;
14
+ topP?: number;
15
+ maxTokens?: number;
16
+ }
17
+ /**
18
+ * Token usage information for a model invocation.
19
+ *
20
+ * @interface InvokeResponseUsage
21
+ * @property {number} inputTokens - Number of tokens in the input/prompt
22
+ * @property {number} outputTokens - Number of tokens in the output/response
23
+ */
24
+ export interface InvokeResponseUsage {
25
+ inputTokens: number;
26
+ outputTokens: number;
27
+ }
28
+ /**
29
+ * Enum representing the reason why model generation stopped.
30
+ *
31
+ * @enum {string}
32
+ * @property {string} UNKNOWN - Stop reason is not determined
33
+ * @property {string} STOP_SEQUENCE - Generation ended at a stop sequence
34
+ * @property {string} MAX_TOKENS - Generation reached maximum token limit
35
+ * @property {string} END_TURN - Model indicated end of turn
36
+ * @property {string} TOOL_USE - Generation stopped to request tool use
37
+ */
38
+ export declare enum InvokeResponseStopReason {
39
+ UNKNOWN = "unknown",
40
+ STOP_SEQUENCE = "stop_sequence",
41
+ MAX_TOKENS = "max_tokens",
42
+ END_TURN = "end_turn",
43
+ TOOL_USE = "tool_use"
44
+ }
45
+ /**
46
+ * Response from invoking a model.
47
+ *
48
+ * @interface InvokeResponse
49
+ * @property {(AgentMessage | ToolRequest)[]} messages - Messages generated by the model
50
+ * @property {InvokeResponseUsage} usage - Token usage statistics
51
+ * @property {InvokeResponseStopReason} [stopReason] - Reason why generation stopped
52
+ */
53
+ export interface InvokeResponse {
54
+ messages: (AgentMessage | ToolRequest)[];
55
+ usage: InvokeResponseUsage;
56
+ stopReason?: InvokeResponseStopReason;
57
+ }
58
+ /**
59
+ * Abstract base class for AI model implementations.
60
+ * Provides common functionality for configuring and invoking models.
61
+ * Subclasses must implement runModel() for specific model providers.
62
+ *
63
+ * @abstract
64
+ * @example
65
+ * ```typescript
66
+ * class MyCustomModel extends BaseModel {
67
+ * protected async runModel(messages: ModelMessages[]): Promise<InvokeResponse> {
68
+ * // Implementation specific to your model provider
69
+ * }
70
+ * }
71
+ *
72
+ * const model = new MyCustomModel({ temperature: 0.7 })
73
+ * .withSystemMessage("You are a helpful assistant")
74
+ * .withTools([searchTool, calculatorTool]);
75
+ *
76
+ * const response = await model.invoke([userMessage]);
77
+ * ```
78
+ */
79
+ export declare abstract class BaseModel {
80
+ protected temperature?: number;
81
+ protected topP?: number;
82
+ protected maxTokens?: number;
83
+ protected systemMessage?: SystemMessage;
84
+ protected tools: ToolDefinition[];
85
+ protected structuredOutput?: z.ZodSchema<any>;
86
+ /**
87
+ * Creates a new model instance with the provided configuration.
88
+ * Configuration values are clamped to valid ranges.
89
+ *
90
+ * @param {BaseModelConfig} config - Model configuration
91
+ */
92
+ constructor(config: BaseModelConfig);
93
+ /**
94
+ * Sets the system message for the model.
95
+ * The system message provides context and instructions for the model's behavior.
96
+ *
97
+ * @param {SystemMessage | MessageContent} message - The system message
98
+ * @returns {this} The model instance for method chaining
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * model.withSystemMessage("You are an expert Python developer");
103
+ * ```
104
+ */
105
+ withSystemMessage(message: SystemMessage | MessageContent): this;
106
+ /**
107
+ * Sets the tools available to the model.
108
+ * Cannot be combined with structured output.
109
+ *
110
+ * @param {ToolDefinition[]} tools - Array of tool definitions
111
+ * @returns {this} The model instance for method chaining
112
+ * @throws {Error} If structured output is already configured
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * model.withTools([webSearchTool, calculatorTool]);
117
+ * ```
118
+ */
119
+ withTools(tools: ToolDefinition[]): this;
120
+ /**
121
+ * Configures structured output using a Zod schema.
122
+ * The model will be forced to output data matching this schema.
123
+ * Cannot be combined with tools.
124
+ *
125
+ * @template T - The Zod schema type
126
+ * @param {T} schema - Zod schema defining the output structure
127
+ * @returns {StructuredOutputWrapper<T>} A wrapper for invoking with structured output
128
+ * @throws {Error} If tools are already configured
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * const schema = z.object({
133
+ * sentiment: z.enum(["positive", "negative", "neutral"]),
134
+ * confidence: z.number().min(0).max(1)
135
+ * });
136
+ * const wrapper = model.withStructuredOutput(schema);
137
+ * const result = await wrapper.invoke(messages);
138
+ * ```
139
+ */
140
+ withStructuredOutput<T extends z.ZodObject>(schema: T): StructuredOutputWrapper<T>;
141
+ /**
142
+ * Internal method to run the model implementation.
143
+ * Must be implemented by subclasses.
144
+ *
145
+ * @protected
146
+ * @abstract
147
+ * @param {ModelMessages[]} messages - Messages to send to the model
148
+ * @returns {Promise<InvokeResponse>} The model's response
149
+ */
150
+ protected abstract runModel(messages: ModelMessages[]): Promise<InvokeResponse>;
151
+ /**
152
+ * Invokes the model with messages and optional template variable substitution.
153
+ * Messages can contain template variables like {name} that will be replaced
154
+ * with values from the properties object.
155
+ *
156
+ * @param {ModelMessages[]} messages - Messages to send to the model
157
+ * @param {Record<string, any>} [properties] - Optional property values for template substitution
158
+ * @returns {Promise<InvokeResponse>} The model's response
159
+ * @throws {Error} If a template variable is not found in properties
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * const response = await model.invoke(
164
+ * [new UserMessage("Hello, my name is {name}")],
165
+ * { name: "Alice" }
166
+ * );
167
+ * ```
168
+ */
169
+ invoke(messages: ModelMessages[], properties?: Record<string, any>): Promise<InvokeResponse>;
170
+ }
171
+ /**
172
+ * Error thrown when response is not structured output as expected.
173
+ *
174
+ * @class ResponseNotStructuredOutputError
175
+ * @extends {Error}
176
+ */
177
+ export declare class ResponseNotStructuredOutputError extends Error {
178
+ /**
179
+ * Creates a new error instance.
180
+ */
181
+ constructor();
182
+ }
183
+ /**
184
+ * Wrapper class for models configured with structured output.
185
+ * Ensures responses conform to the specified Zod schema.
186
+ *
187
+ * @template T - The Zod schema type
188
+ *
189
+ * @example
190
+ * ```typescript
191
+ * const schema = z.object({ message: z.string(), score: z.number() });
192
+ * const wrapper = model.withStructuredOutput(schema);
193
+ * const parsed = await wrapper.invoke(messages);
194
+ * // parsed is guaranteed to match the schema type
195
+ * ```
196
+ */
197
+ export declare class StructuredOutputWrapper<T extends z.ZodObject> {
198
+ private readonly model;
199
+ private readonly schema;
200
+ /**
201
+ * Creates a wrapper for structured output.
202
+ *
203
+ * @param {BaseModel} model - The model instance
204
+ * @param {T} schema - The output schema
205
+ */
206
+ constructor(model: BaseModel, schema: T);
207
+ /**
208
+ * Sets the system message for this model instance.
209
+ *
210
+ * @param {SystemMessage | MessageContent} message - The system message
211
+ * @returns {this} The wrapper instance for method chaining
212
+ */
213
+ withSystemMessage(message: SystemMessage | MessageContent): this;
214
+ /**
215
+ * Invokes the model and parses the response using the configured schema.
216
+ * The model is forced to call an "output" tool containing the structured data.
217
+ *
218
+ * @param {ModelMessages[]} messages - Messages to send to the model
219
+ * @param {Record<string, any>} [properties] - Optional template variables
220
+ * @returns {Promise<z.infer<T>>} The parsed response matching the schema
221
+ * @throws {ResponseNotStructuredOutputError} If response doesn't match expected format
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const result = await wrapper.invoke([userMessage]);
226
+ * console.log(result.message); // TypeScript knows this is a string
227
+ * console.log(result.score); // TypeScript knows this is a number
228
+ * ```
229
+ */
230
+ invoke(messages: ModelMessages[], properties?: Record<string, any>): Promise<z.infer<T>>;
231
+ }
232
+ //# sourceMappingURL=BaseModel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BaseModel.d.ts","sourceRoot":"","sources":["../../src/models/BaseModel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EACH,YAAY,EAEZ,cAAc,EACd,aAAa,EACb,aAAa,EACb,WAAW,EACd,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE1C;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,mBAAmB;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;GASG;AACH,oBAAY,wBAAwB;IAChC,OAAO,YAAY;IACnB,aAAa,kBAAkB;IAC/B,UAAU,eAAe;IACzB,QAAQ,aAAa;IACrB,QAAQ,aAAa;CACxB;AAED;;;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AAQ,KAAK;IACvD;;OAEG;;CAKN;AAED;;;;;;;;;;;;;GAaG;AACH,qBAAa,uBAAuB,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS;IAO1C,OAAO,CAAC,QAAQ,CAAC,KAAK;IAAa,OAAO,CAAC,QAAQ,CAAC,MAAM;IANtE;;;;;OAKG;gBAC0B,KAAK,EAAE,SAAS,EAAmB,MAAM,EAAE,CAAC;IAEzE;;;;;OAKG;IACH,iBAAiB,CAAC,OAAO,EAAE,aAAa,GAAG,cAAc,GAAG,IAAI;IAKhE;;;;;;;;;;;;;;;OAeG;IACG,MAAM,CACR,QAAQ,EAAE,aAAa,EAAE,EACzB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;CAWzB"}
@@ -0,0 +1,247 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StructuredOutputWrapper = exports.ResponseNotStructuredOutputError = exports.BaseModel = exports.InvokeResponseStopReason = void 0;
4
+ const messages_1 = require("../messages");
5
+ /**
6
+ * Enum representing the reason why model generation stopped.
7
+ *
8
+ * @enum {string}
9
+ * @property {string} UNKNOWN - Stop reason is not determined
10
+ * @property {string} STOP_SEQUENCE - Generation ended at a stop sequence
11
+ * @property {string} MAX_TOKENS - Generation reached maximum token limit
12
+ * @property {string} END_TURN - Model indicated end of turn
13
+ * @property {string} TOOL_USE - Generation stopped to request tool use
14
+ */
15
+ var InvokeResponseStopReason;
16
+ (function (InvokeResponseStopReason) {
17
+ InvokeResponseStopReason["UNKNOWN"] = "unknown";
18
+ InvokeResponseStopReason["STOP_SEQUENCE"] = "stop_sequence";
19
+ InvokeResponseStopReason["MAX_TOKENS"] = "max_tokens";
20
+ InvokeResponseStopReason["END_TURN"] = "end_turn";
21
+ InvokeResponseStopReason["TOOL_USE"] = "tool_use";
22
+ })(InvokeResponseStopReason || (exports.InvokeResponseStopReason = InvokeResponseStopReason = {}));
23
+ /**
24
+ * Abstract base class for AI model implementations.
25
+ * Provides common functionality for configuring and invoking models.
26
+ * Subclasses must implement runModel() for specific model providers.
27
+ *
28
+ * @abstract
29
+ * @example
30
+ * ```typescript
31
+ * class MyCustomModel extends BaseModel {
32
+ * protected async runModel(messages: ModelMessages[]): Promise<InvokeResponse> {
33
+ * // Implementation specific to your model provider
34
+ * }
35
+ * }
36
+ *
37
+ * const model = new MyCustomModel({ temperature: 0.7 })
38
+ * .withSystemMessage("You are a helpful assistant")
39
+ * .withTools([searchTool, calculatorTool]);
40
+ *
41
+ * const response = await model.invoke([userMessage]);
42
+ * ```
43
+ */
44
+ class BaseModel {
45
+ temperature;
46
+ topP;
47
+ maxTokens;
48
+ systemMessage;
49
+ tools = [];
50
+ structuredOutput;
51
+ /**
52
+ * Creates a new model instance with the provided configuration.
53
+ * Configuration values are clamped to valid ranges.
54
+ *
55
+ * @param {BaseModelConfig} config - Model configuration
56
+ */
57
+ constructor(config) {
58
+ if (config.temperature) {
59
+ this.temperature = Math.max(0, Math.min(1, config.temperature));
60
+ }
61
+ if (config.topP) {
62
+ this.topP = Math.max(0, Math.min(1, config.topP));
63
+ }
64
+ if (config.maxTokens) {
65
+ this.maxTokens = Math.max(0, config.maxTokens);
66
+ }
67
+ }
68
+ /**
69
+ * Sets the system message for the model.
70
+ * The system message provides context and instructions for the model's behavior.
71
+ *
72
+ * @param {SystemMessage | MessageContent} message - The system message
73
+ * @returns {this} The model instance for method chaining
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * model.withSystemMessage("You are an expert Python developer");
78
+ * ```
79
+ */
80
+ withSystemMessage(message) {
81
+ if (message instanceof messages_1.SystemMessage) {
82
+ this.systemMessage = message;
83
+ }
84
+ else {
85
+ this.systemMessage = new messages_1.SystemMessage(message);
86
+ }
87
+ return this;
88
+ }
89
+ /**
90
+ * Sets the tools available to the model.
91
+ * Cannot be combined with structured output.
92
+ *
93
+ * @param {ToolDefinition[]} tools - Array of tool definitions
94
+ * @returns {this} The model instance for method chaining
95
+ * @throws {Error} If structured output is already configured
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * model.withTools([webSearchTool, calculatorTool]);
100
+ * ```
101
+ */
102
+ withTools(tools) {
103
+ if (this.structuredOutput) {
104
+ throw new Error("Cannot set tools with structured output");
105
+ }
106
+ this.tools = tools;
107
+ return this;
108
+ }
109
+ /**
110
+ * Configures structured output using a Zod schema.
111
+ * The model will be forced to output data matching this schema.
112
+ * Cannot be combined with tools.
113
+ *
114
+ * @template T - The Zod schema type
115
+ * @param {T} schema - Zod schema defining the output structure
116
+ * @returns {StructuredOutputWrapper<T>} A wrapper for invoking with structured output
117
+ * @throws {Error} If tools are already configured
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * const schema = z.object({
122
+ * sentiment: z.enum(["positive", "negative", "neutral"]),
123
+ * confidence: z.number().min(0).max(1)
124
+ * });
125
+ * const wrapper = model.withStructuredOutput(schema);
126
+ * const result = await wrapper.invoke(messages);
127
+ * ```
128
+ */
129
+ withStructuredOutput(schema) {
130
+ this.tools = [];
131
+ this.structuredOutput = schema;
132
+ return new StructuredOutputWrapper(this, schema);
133
+ }
134
+ /**
135
+ * Invokes the model with messages and optional template variable substitution.
136
+ * Messages can contain template variables like {name} that will be replaced
137
+ * with values from the properties object.
138
+ *
139
+ * @param {ModelMessages[]} messages - Messages to send to the model
140
+ * @param {Record<string, any>} [properties] - Optional property values for template substitution
141
+ * @returns {Promise<InvokeResponse>} The model's response
142
+ * @throws {Error} If a template variable is not found in properties
143
+ *
144
+ * @example
145
+ * ```typescript
146
+ * const response = await model.invoke(
147
+ * [new UserMessage("Hello, my name is {name}")],
148
+ * { name: "Alice" }
149
+ * );
150
+ * ```
151
+ */
152
+ async invoke(messages, properties) {
153
+ if (!properties) {
154
+ return this.runModel(messages);
155
+ }
156
+ return this.runModel(messages.map((message) => {
157
+ if (message instanceof messages_1.BaseMessage) {
158
+ message.interpolate(properties);
159
+ }
160
+ return message;
161
+ }));
162
+ }
163
+ }
164
+ exports.BaseModel = BaseModel;
165
+ /**
166
+ * Error thrown when response is not structured output as expected.
167
+ *
168
+ * @class ResponseNotStructuredOutputError
169
+ * @extends {Error}
170
+ */
171
+ class ResponseNotStructuredOutputError extends Error {
172
+ /**
173
+ * Creates a new error instance.
174
+ */
175
+ constructor() {
176
+ super("Response is not structured output");
177
+ this.name = "ResponseNotStructuredOutputError";
178
+ }
179
+ }
180
+ exports.ResponseNotStructuredOutputError = ResponseNotStructuredOutputError;
181
+ /**
182
+ * Wrapper class for models configured with structured output.
183
+ * Ensures responses conform to the specified Zod schema.
184
+ *
185
+ * @template T - The Zod schema type
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * const schema = z.object({ message: z.string(), score: z.number() });
190
+ * const wrapper = model.withStructuredOutput(schema);
191
+ * const parsed = await wrapper.invoke(messages);
192
+ * // parsed is guaranteed to match the schema type
193
+ * ```
194
+ */
195
+ class StructuredOutputWrapper {
196
+ model;
197
+ schema;
198
+ /**
199
+ * Creates a wrapper for structured output.
200
+ *
201
+ * @param {BaseModel} model - The model instance
202
+ * @param {T} schema - The output schema
203
+ */
204
+ constructor(model, schema) {
205
+ this.model = model;
206
+ this.schema = schema;
207
+ }
208
+ /**
209
+ * Sets the system message for this model instance.
210
+ *
211
+ * @param {SystemMessage | MessageContent} message - The system message
212
+ * @returns {this} The wrapper instance for method chaining
213
+ */
214
+ withSystemMessage(message) {
215
+ this.model.withSystemMessage(message);
216
+ return this;
217
+ }
218
+ /**
219
+ * Invokes the model and parses the response using the configured schema.
220
+ * The model is forced to call an "output" tool containing the structured data.
221
+ *
222
+ * @param {ModelMessages[]} messages - Messages to send to the model
223
+ * @param {Record<string, any>} [properties] - Optional template variables
224
+ * @returns {Promise<z.infer<T>>} The parsed response matching the schema
225
+ * @throws {ResponseNotStructuredOutputError} If response doesn't match expected format
226
+ *
227
+ * @example
228
+ * ```typescript
229
+ * const result = await wrapper.invoke([userMessage]);
230
+ * console.log(result.message); // TypeScript knows this is a string
231
+ * console.log(result.score); // TypeScript knows this is a number
232
+ * ```
233
+ */
234
+ async invoke(messages, properties) {
235
+ const response = await this.model.invoke(messages, properties);
236
+ if (!response.messages.length) {
237
+ throw new ResponseNotStructuredOutputError();
238
+ }
239
+ if (!(response.messages[0] instanceof messages_1.ToolRequest)) {
240
+ throw new ResponseNotStructuredOutputError();
241
+ }
242
+ const toolRequest = response.messages[0];
243
+ return this.schema.parse(toolRequest.input);
244
+ }
245
+ }
246
+ exports.StructuredOutputWrapper = StructuredOutputWrapper;
247
+ //# sourceMappingURL=BaseModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BaseModel.js","sourceRoot":"","sources":["../../src/models/BaseModel.ts"],"names":[],"mappings":";;;AACA,0CAOqB;AA6BrB;;;;;;;;;GASG;AACH,IAAY,wBAMX;AAND,WAAY,wBAAwB;IAChC,+CAAmB,CAAA;IACnB,2DAA+B,CAAA;IAC/B,qDAAyB,CAAA;IACzB,iDAAqB,CAAA;IACrB,iDAAqB,CAAA;AACzB,CAAC,EANW,wBAAwB,wCAAxB,wBAAwB,QAMnC;AAgBD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAsB,SAAS;IACjB,WAAW,CAAU;IACrB,IAAI,CAAU;IACd,SAAS,CAAU;IAEnB,aAAa,CAAiB;IAC9B,KAAK,GAAqB,EAAE,CAAC;IAC7B,gBAAgB,CAAoB;IAE9C;;;;;OAKG;IACH,YAAY,MAAuB;QAC/B,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;YACrB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;QACpE,CAAC;QACD,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;YACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAA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AC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IAChD,CAAC;CACJ;AAlDD,0DAkDC"}
@@ -0,0 +1,112 @@
1
+ import { BaseModel, type BaseModelConfig, InvokeResponse } from "./BaseModel";
2
+ import { BedrockRuntimeClientConfig } from "@aws-sdk/client-bedrock-runtime";
3
+ import { ModelMessages } from "../messages";
4
+ /**
5
+ * Configuration for BedrockModel combining BaseModel config with AWS-specific settings.
6
+ *
7
+ * @interface BedrockModelConfig
8
+ * @extends {BaseModelConfig}
9
+ * @property {string} modelId - The ID of the Bedrock model (e.g., "anthropic.claude-3-sonnet-20240229-v1:0")
10
+ * @property {BedrockRuntimeClientConfig} [aws] - Optional AWS SDK configuration
11
+ */
12
+ export type BedrockModelConfig = BaseModelConfig & {
13
+ modelId: string;
14
+ aws?: BedrockRuntimeClientConfig;
15
+ };
16
+ /**
17
+ * Model implementation using AWS Bedrock as the backend.
18
+ * Handles communication with Bedrock API for inference, tools, and structured output.
19
+ *
20
+ * @class BedrockModel
21
+ * @extends {BaseModel}
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const model = new BedrockModel({
26
+ * modelId: "anthropic.claude-3-sonnet-20240229-v1:0",
27
+ * temperature: 0.7,
28
+ * maxTokens: 2048
29
+ * });
30
+ *
31
+ * model.withSystemMessage("You are a helpful assistant")
32
+ * .withTools([searchTool]);
33
+ *
34
+ * const response = await model.invoke([userMessage]);
35
+ * ```
36
+ */
37
+ export declare class BedrockModel extends BaseModel {
38
+ private modelId;
39
+ private client;
40
+ /**
41
+ * Creates a new Bedrock model instance.
42
+ *
43
+ * @param {BedrockModelConfig} config - Configuration including model ID and AWS settings
44
+ */
45
+ constructor(config: BedrockModelConfig);
46
+ /**
47
+ * Converts internal message format to Bedrock API message format.
48
+ * Handles BaseMessages (user, agent, system) and ToolUse messages (requests, responses, errors).
49
+ *
50
+ * @private
51
+ * @param {ModelMessages[]} messages - Messages to convert
52
+ * @returns {Message[]} Messages in Bedrock format
53
+ */
54
+ private convertMessagesToBedrockMessages;
55
+ /**
56
+ * Converts a tool definition to Bedrock tool specification format.
57
+ *
58
+ * @private
59
+ * @param {ToolDefinition} tool - The tool definition to convert
60
+ * @returns {Tool} Bedrock formatted tool specification
61
+ */
62
+ private convertToolDefinitionToBedrockTool;
63
+ /**
64
+ * Builds the tool configuration for Bedrock.
65
+ * For structured output, creates a single "output" tool that captures the schema.
66
+ * For regular tools, converts all tool definitions.
67
+ *
68
+ * @private
69
+ * @returns {ToolConfiguration} Tool configuration for Bedrock
70
+ */
71
+ private getBedrockToolConfig;
72
+ /**
73
+ * Maps Bedrock stop reason to internal stop reason enum.
74
+ *
75
+ * @private
76
+ * @param {ConverseCommandOutput} response - The Bedrock API response
77
+ * @returns {InvokeResponseStopReason} Mapped stop reason
78
+ */
79
+ private mapStopReason;
80
+ /**
81
+ * Builds inference configuration chunk with temperature, topP, and maxTokens if set.
82
+ *
83
+ * @private
84
+ * @returns {{inferenceConfig?: InferenceConfiguration}} Configuration chunk or empty object
85
+ */
86
+ private getInferenceConfigChunk;
87
+ /**
88
+ * Builds system message chunk.
89
+ * For structured output, adds a directive to only call the output tool.
90
+ *
91
+ * @private
92
+ * @returns {{system?: SystemContentBlock[]}} System messages or empty object
93
+ */
94
+ private getSystemMessageChunk;
95
+ /**
96
+ * Builds tool configuration chunk.
97
+ *
98
+ * @private
99
+ * @returns {{toolConfig?: ToolConfiguration}} Tool configuration or empty object
100
+ */
101
+ private getToolConfigChunk;
102
+ /**
103
+ * Executes the model inference with Bedrock API.
104
+ * Sends messages to Bedrock, processes the response, and converts it to internal format.
105
+ *
106
+ * @protected
107
+ * @param {ModelMessages[]} inputMessages - Messages to send to the model
108
+ * @returns {Promise<InvokeResponse>} The model's response with messages and usage stats
109
+ */
110
+ protected runModel(inputMessages: ModelMessages[]): Promise<InvokeResponse>;
111
+ }
112
+ //# sourceMappingURL=BedrockModel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BedrockModel.d.ts","sourceRoot":"","sources":["../../src/models/BedrockModel.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,SAAS,EACT,KAAK,eAAe,EACpB,cAAc,EAGjB,MAAM,aAAa,CAAC;AACrB,OAAO,EAEH,0BAA0B,EAY7B,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAIH,aAAa,EAKhB,MAAM,aAAa,CAAC;AAIrB;;;;;;;GAOG;AACH,MAAM,MAAM,kBAAkB,GAAG,eAAe,GAAG;IAC/C,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,0BAA0B,CAAC;CACpC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,YAAa,SAAQ,SAAS;IACvC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,MAAM,CAAuB;IAErC;;;;OAIG;gBACS,MAAM,EAAE,kBAAkB;IAMtC;;;;;;;OAOG;IACH,OAAO,CAAC,gCAAgC;IA+ExC;;;;;;OAMG;IACH,OAAO,CAAC,kCAAkC;IAY1C;;;;;;;OAOG;IACH,OAAO,CAAC,oBAAoB;IA4B5B;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAiBrB;;;;;OAKG;IACH,OAAO,CAAC,uBAAuB;IAc/B;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IA8B7B;;;;;OAKG;IACH,OAAO,CAAC,kBAAkB;IAU1B;;;;;;;OAOG;cACa,QAAQ,CACpB,aAAa,EAAE,aAAa,EAAE,GAC/B,OAAO,CAAC,cAAc,CAAC;CAuD7B"}