@recombine-ai/engine 0.4.0 → 0.6.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/build/lib/ai.d.ts CHANGED
@@ -5,8 +5,8 @@ export declare namespace AIEngine {
5
5
  /**
6
6
  * Represents a basic model name for LLMs.
7
7
  */
8
- type BasicModel = 'o3-mini-2025-01-31' | 'o1-preview-2024-09-12' | 'gpt-4o-2024-11-20' | 'o1-2024-12-17' | (string & {});
9
- interface ProgrammaticStep {
8
+ export type BasicModel = 'o3-mini-2025-01-31' | 'o1-preview-2024-09-12' | 'gpt-4o-2024-11-20' | 'o1-2024-12-17' | (string & {});
9
+ export interface ProgrammaticStep {
10
10
  /** Step name for debugging */
11
11
  name: string;
12
12
  /** Determines if the step should be run or not */
@@ -16,7 +16,7 @@ export declare namespace AIEngine {
16
16
  /** Error handler called if an error occurred during in `execute` function */
17
17
  onError: (error: string) => Promise<unknown>;
18
18
  }
19
- interface LLMStep {
19
+ export interface LLMStep {
20
20
  /** Step name for debugging */
21
21
  name: string;
22
22
  /** Determines if the step should be run or not */
@@ -29,12 +29,16 @@ export declare namespace AIEngine {
29
29
  */
30
30
  prompt: string | File;
31
31
  /**
32
- * Schema for structured LLM output using {@link zod https://zod.dev/}
33
- * library.
32
+ * Defines the expected structure of the LLM's output.
33
+ * Accepts either a boolean (for plain text or JSON responses) or a ZodSchema, which is automatically
34
+ * converted to a JSON schema. When provided, the LLM's response is validated and parsed according
35
+ * to this schema ensuring reliable structured output.
34
36
  */
35
- schema?: ZodSchema;
36
- /** Exclude directives from message history passed to the LLM for this step */
37
- ignoreDirectives?: boolean;
37
+ json: boolean | ZodSchema;
38
+ /**
39
+ * Do not put messages that were added via {@link Conversation.addMessage} into the prompt.
40
+ */
41
+ ignoreAddedMessages?: boolean;
38
42
  /**
39
43
  * Additional data to be inserted into the prompt. Accessible via Nunjucks variables.
40
44
  * @example
@@ -78,15 +82,16 @@ export declare namespace AIEngine {
78
82
  /**
79
83
  * A useful trace of a step execution. It's properties are filled during the execution. There is no guarantee that any of them will be filled.
80
84
  */
81
- type StepTrace = {
85
+ export type StepTrace = {
82
86
  renderedPrompt?: string;
83
87
  receivedContext?: Record<string, unknown>;
84
88
  receivedPrompt?: string;
89
+ stringifiedConversation?: string;
85
90
  };
86
91
  /**
87
92
  * An AI workflow composed of steps.
88
93
  */
89
- interface Workflow {
94
+ export interface Workflow {
90
95
  /**
91
96
  * Terminates the workflow, preventing further steps from being executed.
92
97
  */
@@ -165,7 +170,7 @@ export declare namespace AIEngine {
165
170
  * console.log(response)
166
171
  * ```
167
172
  */
168
- interface AIEngine {
173
+ export interface AIEngine {
169
174
  /**
170
175
  * Creates a workflow from a sequence of steps.
171
176
  * @param steps - An array of LLM or programmatic steps to be executed in order.
@@ -190,6 +195,13 @@ export declare namespace AIEngine {
190
195
  * @returns A new Conversation object.
191
196
  */
192
197
  createConversation: (messages?: Message[]) => Conversation;
198
+ /**
199
+ * Renders a prompt string using Nunjucks templating engine.
200
+ * @param prompt - The prompt string to render.
201
+ * @param context - Optional context object to use for rendering the prompt.
202
+ * @returns The rendered prompt string.
203
+ */
204
+ renderPrompt: typeof renderPrompt;
193
205
  }
194
206
  /**
195
207
  * Represents a conversation between a user and an AI agent.
@@ -215,7 +227,7 @@ export declare namespace AIEngine {
215
227
  * // System: Ask for account details
216
228
  * ```
217
229
  */
218
- interface Conversation {
230
+ export interface Conversation {
219
231
  /**
220
232
  * Sets the name of the user in the conversation to be used in {@link toString}.
221
233
  * @param name - The name to set for the user.
@@ -227,36 +239,26 @@ export declare namespace AIEngine {
227
239
  */
228
240
  setAgentName(name: string): void;
229
241
  /**
230
- * Converts the conversation to a string representation to be fed to an LLM.
231
- * @param ignoreDirectives - Whether to ignore directives in the string output.
232
- * @returns The string representation of the conversation.
242
+ * Sets the default formatter for stringifying messages when toString is called.
243
+ * @param formatter - A function that takes a message and returns a formatted string.
233
244
  */
234
- toString: (ignoreDirectives?: boolean) => string;
245
+ setDefaultFormatter: (formatter: (message: Message) => string) => void;
235
246
  /**
236
- * Adds a directive message to the conversation.
237
- * @param message - The directive message to add.
247
+ * Converts the conversation to a string representation to be fed to an LLM.
248
+ * @param filter - A function that filters messages based on certain criteria.
238
249
  * @example
239
- * ```
240
- * // Add a directive to guide the LLM response
241
- * conversation.addDirective("Ask the user for their preferred date and time for the reservation");
242
- *
243
- * // The resulting conversation string might look like:
244
- * // User: I'd like to book a table at your restaurant.
245
- * // System: Ask the user for their preferred date and time for the reservation
246
- * ```
250
+ * @returns The string representation of the conversation.
247
251
  */
248
- addDirective: (message: string) => void;
252
+ toString: (options?: {
253
+ ignoreAddedMessages?: boolean;
254
+ }) => string;
249
255
  /**
250
256
  * Adds a message from a specified sender to the conversation.
251
- * @param name - The sender of the message.
252
- * @param message - The content of the message.
253
- */
254
- addMessage: (name: Message['sender'], message: string) => void;
255
- /**
256
- * Sets a custom formatter for directive messages.
257
- * @param formatter - A function that takes a Message and returns a formatted string.
257
+ * @param message - The message to add to the conversation.
258
258
  */
259
- setDirectiveFormatter: (formatter: (message: Message) => string) => void;
259
+ addMessage: (message: Message, opts?: {
260
+ formatter?: (message: Message) => string;
261
+ }) => void;
260
262
  /**
261
263
  * Sets a custom formatter for proposed messages.
262
264
  * @param formatter - A function that takes a message string and returns a formatted string.
@@ -274,6 +276,7 @@ export declare namespace AIEngine {
274
276
  getProposedReply: () => string | null;
275
277
  /**
276
278
  * Gets the history of all messages in the conversation.
279
+ * Returns {@link Message} rather than {@link ConversationMessage} because none of the {@link ConversationMessage} properties should be accessed outside of the {@link Conversation} context.
277
280
  * @returns An array of Message objects representing the conversation history.
278
281
  */
279
282
  getHistory: () => Message[];
@@ -282,7 +285,7 @@ export declare namespace AIEngine {
282
285
  * Represents a message in a conversation between a user and an agent, or a system message.
283
286
  * Messages can contain text and optionally an image URL. To be used in the {@link Conversation} interface.
284
287
  */
285
- interface Message {
288
+ export interface Message {
286
289
  /** The sender of the message, which can be one of the following: 'user', 'agent', or 'system' */
287
290
  sender: 'user' | 'agent' | 'system';
288
291
  /** The text content of the message */
@@ -290,13 +293,13 @@ export declare namespace AIEngine {
290
293
  /** Optional URL of an image associated with the message */
291
294
  imageUrl?: string;
292
295
  }
293
- interface File {
296
+ export interface File {
294
297
  content: () => Promise<string>;
295
298
  }
296
299
  /**
297
300
  * Configuration options for the Engine.
298
301
  */
299
- interface EngineConfig {
302
+ export interface EngineConfig {
300
303
  /**
301
304
  * Optional token storage object that provides access to authentication tokens.
302
305
  * @property {object} tokenStorage - Object containing method to retrieve token.
@@ -344,6 +347,9 @@ export declare namespace AIEngine {
344
347
  * const reply = await workflow.run(conversation);
345
348
  * ```
346
349
  */
347
- function createAIEngine(cfg?: EngineConfig): AIEngine;
350
+ export function createAIEngine(cfg?: EngineConfig): AIEngine;
351
+ function renderPrompt(prompt: string, context?: Record<string, unknown>): string;
352
+ export function createConversation(initialMessages?: Message[]): Conversation;
353
+ export {};
348
354
  }
349
355
  //# sourceMappingURL=ai.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../src/lib/ai.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,SAAS,EAAE,MAAM,KAAK,CAAA;AAE/B,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAc,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAGvD,yBAAiB,QAAQ,CAAC;IACtB;;OAEG;IACH,KAAY,UAAU,GAChB,oBAAoB,GACpB,uBAAuB,GACvB,mBAAmB,GACnB,eAAe,GACf,CAAC,MAAM,GAAG,EAAE,CAAC,CAAA;IAEnB,UAAiB,gBAAgB;QAC7B,8BAA8B;QAC9B,IAAI,EAAE,MAAM,CAAA;QAEZ,kDAAkD;QAClD,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,YAAY,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;QAE9D,0BAA0B;QAC1B,OAA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AK,IAAI,CAAA;QAE7E;;;WAGG;QACH,gBAAgB,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;QAE3C;;;WAGG;QACH,gBAAgB,EAAE,MAAM,MAAM,GAAG,IAAI,CAAA;QAErC;;;;WAIG;QACH,UAAU,EAAE,MAAM,OAAO,EAAE,CAAA;KAC9B;IAED;;;OAGG;IACH,MAAM,WAAW,OAAO;QACpB,iGAAiG;QACjG,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAA;QACnC,sCAAsC;QACtC,IAAI,EAAE,MAAM,CAAA;QACZ,2DAA2D;QAC3D,QAAQ,CAAC,EAAE,MAAM,CAAA;KACpB;IAED,MAAM,WAAW,IAAI;QACjB,OAAO,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAA;KACjC;IAED;;OAEG;IACH,MAAM,WAAW,YAAY;QACzB;;;;WAIG;QACH,YAAY,CAAC,EAAE;YAAE,QAAQ,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;SAAE,CAAA;QACzD;;WAEG;QACH,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB;;WAEG;QACH,MAAM,CAAC,EAAE,MAAM,CAAA;QACf;;WAEG;QACH,UAAU,CAAC,EAAE,UAAU,CAAA;KAC1B;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,MAAM,UAAU,cAAc,CAAC,GAAG,GAAE,YAAiB,GAAG,QAAQ,CAuN/D;IAgCD,SAAS,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAO/E;IAED,MAAM,UAAU,kBAAkB,CAAC,eAAe,GAAE,OAAO,EAAO,GAAG,YAAY,CAgDhF;;CACJ"}
package/build/lib/ai.js CHANGED
@@ -54,48 +54,6 @@ var AIEngine;
54
54
  function createStep(step) {
55
55
  return step;
56
56
  }
57
- function getConversation(messages = []) {
58
- let directivesFormatter = (message) => `${message.sender}: ${message.text}`;
59
- let proposedFormatter = (message) => `Proposed reply: ${message}`;
60
- let proposedReply = null;
61
- const names = {
62
- agent: 'Agent',
63
- user: 'User',
64
- system: 'System',
65
- };
66
- return {
67
- toString: (ignoreDirectives = false) => messages
68
- .map((msg) => {
69
- if (msg.sender === 'system') {
70
- return ignoreDirectives ? null : directivesFormatter(msg);
71
- }
72
- return `${names[msg.sender]}: ${msg.text}`;
73
- })
74
- .filter((msg) => msg !== null)
75
- .join('\n') +
76
- (proposedReply ? `\n${proposedFormatter(proposedReply)}` : ''),
77
- addMessage: (sender, text) => messages.push({ sender, text }),
78
- addDirective: (message) => {
79
- logger.debug(`AI Engine: add directive: ${message}`);
80
- messages.push({ sender: 'system', text: message });
81
- },
82
- setDirectiveFormatter: (formatter) => {
83
- directivesFormatter = formatter;
84
- },
85
- setProposedMessageFormatter: (formatter) => {
86
- proposedFormatter = formatter;
87
- },
88
- setProposedReply: (message) => (proposedReply = message),
89
- getProposedReply: () => proposedReply,
90
- getHistory: () => messages,
91
- setUserName: (name) => {
92
- names.user = name;
93
- },
94
- setAgentName: (name) => {
95
- names.agent = name;
96
- },
97
- };
98
- }
99
57
  async function createWorkflow(...steps) {
100
58
  const apiKey = await tokenStorage.getToken();
101
59
  let shouldRun = true;
@@ -164,15 +122,12 @@ var AIEngine;
164
122
  let prompt = typeof step.prompt === 'string' ? step.prompt : await step.prompt.content();
165
123
  stepTrace.receivedPrompt = prompt;
166
124
  logger.debug('AI Engine: context', step.context);
167
- logger.debug('AI Engine: messages', messages.toString(step.ignoreDirectives || false));
168
- nunjucks_1.default.configure({
169
- autoescape: true,
170
- trimBlocks: true,
171
- lstripBlocks: true,
172
- });
173
- prompt = nunjucks_1.default.renderString(prompt, step.context || {});
125
+ logger.debug('AI Engine: messages', messages.toString({ ignoreAddedMessages: step.ignoreAddedMessages }));
126
+ prompt = renderPrompt(prompt, step.context);
174
127
  stepTrace.renderedPrompt = prompt;
175
- response = await runLLM(apiKey, prompt, messages.toString(step.ignoreDirectives || false), step.schema, step.model);
128
+ const stringifiedMessages = messages.toString({ ignoreAddedMessages: step.ignoreAddedMessages });
129
+ stepTrace.stringifiedConversation = stringifiedMessages;
130
+ response = await runLLM(apiKey, prompt, stringifiedMessages, step.json, step.model);
176
131
  if (!response) {
177
132
  throw new Error('No response from OpenAI');
178
133
  }
@@ -228,16 +183,17 @@ var AIEngine;
228
183
  attempts.set(step, 0);
229
184
  }
230
185
  }
231
- async function runLLM(apiKey, systemPrompt, messages, schema, model = 'gpt-4o-2024-08-06') {
186
+ async function runLLM(apiKey, systemPrompt, messages, json, model = 'gpt-4o-2024-08-06') {
232
187
  logger.debug('AI Engine: model:', model);
233
188
  logger.debug('----------- RENDERED PROMPT ---------------');
234
189
  logger.debug(systemPrompt);
235
190
  logger.debug('-------------------------------------------');
236
191
  if (apiKey === '__TESTING__') {
237
192
  await (0, core_1.sleep)(100);
238
- return schema
239
- ? JSON.stringify({ message: 'canned response', reasons: [] })
240
- : 'canned response';
193
+ if (typeof json === 'boolean') {
194
+ return json ? JSON.stringify({ message: 'canned response', reasons: [] }) : 'canned response';
195
+ }
196
+ return JSON.stringify({ message: 'canned response', reasons: [] });
241
197
  }
242
198
  const client = new openai_1.default({ apiKey });
243
199
  const response = await client.chat.completions.create({
@@ -245,7 +201,7 @@ var AIEngine;
245
201
  { role: 'system', content: systemPrompt },
246
202
  { role: 'user', content: messages },
247
203
  ],
248
- ...getOpenAiOptions(model, schema),
204
+ ...getOpenAiOptions(model, json),
249
205
  });
250
206
  if (!response.choices[0].message.content) {
251
207
  throw new Error('No response from OpenAI');
@@ -265,11 +221,12 @@ var AIEngine;
265
221
  createWorkflow: createWorkflow,
266
222
  createStep,
267
223
  loadFile,
268
- createConversation: getConversation,
224
+ createConversation,
225
+ renderPrompt
269
226
  };
270
227
  }
271
228
  AIEngine.createAIEngine = createAIEngine;
272
- function getOpenAiOptions(model, schema) {
229
+ function getOpenAiOptions(model, json) {
273
230
  const options = {
274
231
  model,
275
232
  };
@@ -282,18 +239,79 @@ var AIEngine;
282
239
  else {
283
240
  options.temperature = 0.1;
284
241
  }
285
- if (schema) {
242
+ if (typeof json !== 'boolean') {
286
243
  options.response_format = {
287
244
  type: 'json_schema',
288
245
  json_schema: {
289
246
  name: 'detector_response',
290
- schema: (0, zod_to_json_schema_1.zodToJsonSchema)(schema),
247
+ schema: (0, zod_to_json_schema_1.zodToJsonSchema)(json),
291
248
  },
292
249
  };
293
250
  }
251
+ else if (json) {
252
+ options.response_format = { type: 'json_object' };
253
+ }
294
254
  else {
295
255
  options.response_format = { type: 'text' };
296
256
  }
297
257
  return options;
298
258
  }
259
+ function renderPrompt(prompt, context) {
260
+ nunjucks_1.default.configure({
261
+ autoescape: false,
262
+ trimBlocks: true,
263
+ lstripBlocks: true,
264
+ });
265
+ return nunjucks_1.default.renderString(prompt, context || {});
266
+ }
267
+ function createConversation(initialMessages = []) {
268
+ const messages = initialMessages.map((msg) => ({
269
+ ...msg,
270
+ isAddedMessage: false,
271
+ formatter: undefined
272
+ }));
273
+ const names = {
274
+ agent: 'Agent',
275
+ user: 'User',
276
+ system: 'System',
277
+ };
278
+ let defaultFormatter = (message) => `${names[message.sender]}: ${message.text}`;
279
+ let proposedFormatter = (message) => `Proposed reply: ${message}`;
280
+ let proposedReply = null;
281
+ return {
282
+ toString: (options) => {
283
+ return messages
284
+ .filter((msg) => !options?.ignoreAddedMessages || !msg.isAddedMessage)
285
+ .map((msg) => {
286
+ return msg.formatter ? msg.formatter(msg) : defaultFormatter(msg);
287
+ })
288
+ .filter((msg) => msg !== null)
289
+ .join('\n') +
290
+ (proposedReply ? `\n${proposedFormatter(proposedReply)}` : '');
291
+ },
292
+ addMessage: (message, opts) => {
293
+ messages.push({
294
+ ...message,
295
+ isAddedMessage: true,
296
+ formatter: opts?.formatter ?? defaultFormatter,
297
+ });
298
+ },
299
+ setDefaultFormatter: (formatter) => {
300
+ defaultFormatter = formatter;
301
+ },
302
+ setProposedMessageFormatter: (formatter) => {
303
+ proposedFormatter = formatter;
304
+ },
305
+ setProposedReply: (message) => (proposedReply = message),
306
+ getProposedReply: () => proposedReply,
307
+ getHistory: () => messages,
308
+ setUserName: (name) => {
309
+ names.user = name;
310
+ },
311
+ setAgentName: (name) => {
312
+ names.agent = name;
313
+ }
314
+ };
315
+ }
316
+ AIEngine.createConversation = createConversation;
299
317
  })(AIEngine || (exports.AIEngine = AIEngine = {}));
@@ -28,7 +28,7 @@ export declare namespace Bosun {
28
28
  scheduler: Scheduler.Scheduler;
29
29
  ai: AIEngine.AIEngine;
30
30
  getMessages: () => AIEngine.Message[];
31
- sendMessage: (message: string) => Promise<void>;
31
+ sendMessage: (message: string | AIEngine.Message) => Promise<void>;
32
32
  sendAction: SendAction;
33
33
  ctx: Context<CTX>;
34
34
  }
@@ -1 +1 @@
1
- {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../../src/lib/bosun/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEnC;;;;;;;;;;;;;;;;;;GAkBG;AACH,yBAAiB,KAAK,CAAA;IAClB,KAAK,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACzC,MAAM,WAAW,oBAAoB,CAAC,GAAG,SAAS,cAAc,GAAG,cAAc;QAC7E,MAAM,EAAE,MAAM,CAAA;QACd,SAAS,EAAE,SAAS,CAAC,SAAS,CAAA;QAC9B,EAAE,EAAE,QAAQ,CAAC,QAAQ,CAAA;QACrB,WAAW,EAAE,MAAM,QAAQ,CAAC,OAAO,EAAE,CAAA;QACrC,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,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;QAC9B,EAAE,EAAE,QAAQ,CAAC,QAAQ,CAAA;QACrB,WAAW,EAAE,MAAM,QAAQ,CAAC,OAAO,EAAE,CAAA;QACrC,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;QAClE,UAAU,EAAE,UAAU,CAAA;QACtB,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,CAAA;KACpB;IAED,MAAM,WAAW,SAAS;QACtB,KAAK,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAA;QAC7B,cAAc,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAA;QACtC,gBAAgB,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAA;QACxC,UAAU,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAA;QAClC,YAAY,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;KACnD;IAED,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,cAAc,GAAG,cAAc,IAAI,CACtE,KAAK,EAAE,oBAAoB,CAAC,CAAC,CAAC,KAC7B,SAAS,CAAA;IAEd,MAAM,UAAU,sBAAsB,CAAC,CAAC,SAAS,cAAc,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,uBAE5F;;CAEJ"}
package/changelog.md CHANGED
@@ -1,6 +1,30 @@
1
1
  # Changelog
2
2
 
3
- ### 0.3.2 → 0.4.o (unstable)
3
+ ### 0.5.0 → 0.6.0 (unstable)
4
+
5
+ Breaking changes:
6
+
7
+ - addDirective is removed. Use addMessage with role: 'system' instead.
8
+ - ignoreDirectives → ignoreAddedMessages
9
+
10
+ Other changes:
11
+
12
+ - AIEngine.sendMessage now accepts "Message" rather than a string
13
+
14
+ ### 0.4.0 → 0.5.0 (unstable)
15
+
16
+ Breaking changes:
17
+
18
+ - `schema` property replaced with `json` which can be boolean,
19
+ - `setDirectiveFormatter` →
20
+
21
+ Other changes:
22
+
23
+ - `renderPrompt` method added
24
+ - `addDirective` accepts optional formatter function
25
+ - `formatter` optional method added to `Message`
26
+
27
+ ### 0.3.2 → 0.4.0 (unstable)
4
28
 
5
29
  Breaking changes:
6
30
 
@@ -22,7 +46,7 @@ Breaking changes:
22
46
  Breaking changes:
23
47
 
24
48
  - Break down the library into namespace: AIEngine, Scheduler
25
- - Models → BasicModel
26
- - Step → LLMStep & ProgrammaticStep
27
- - makeMessagesList → getConversation
28
- - Deprecation of shouldExecute (discouraged to use if there's no `maxAttempts` in a step)
49
+ - `Models``BasicModel`
50
+ - `Step``LLMStep` & `ProgrammaticStep`
51
+ - `makeMessagesList``getConversation`
52
+ - Deprecation of `shouldExecute` (discouraged to use if there's no `maxAttempts` in a step)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@recombine-ai/engine",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Recombine AI engine for creating conversational AI agents",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -18,13 +18,13 @@
18
18
  "@types/node": "^22.8.1",
19
19
  "@types/nunjucks": "^3.2.6",
20
20
  "prettier": "^3.3.3",
21
- "typescript": "^5.7.3",
21
+ "typescript": "^5.8.3",
22
22
  "vitest": "^3.0.6"
23
23
  },
24
24
  "dependencies": {
25
25
  "nunjucks": "^3.2.4",
26
26
  "openai": "^4.68.4",
27
27
  "zod": "3.23.8",
28
- "zod-to-json-schema": "^3.23.5"
28
+ "zod-to-json-schema": "^3.24.6"
29
29
  }
30
30
  }