@recombine-ai/engine 0.10.4 → 0.11.1-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.
- package/.github/workflows/publish.yml +13 -1
- package/build/lib/ai.d.ts +34 -76
- package/build/lib/ai.d.ts.map +1 -1
- package/build/lib/ai.js +49 -4
- package/package.json +2 -2
|
@@ -39,5 +39,17 @@ jobs:
|
|
|
39
39
|
- name: 'Build'
|
|
40
40
|
run: npm run build
|
|
41
41
|
|
|
42
|
+
- name: 'Compute npm publish args'
|
|
43
|
+
id: publish_meta
|
|
44
|
+
run: |
|
|
45
|
+
VERSION="${GITHUB_REF_NAME#v}"
|
|
46
|
+
if [[ "$VERSION" == *-* ]]; then
|
|
47
|
+
PRERELEASE="${VERSION#*-}"
|
|
48
|
+
DIST_TAG="${PRERELEASE%%.*}"
|
|
49
|
+
echo "publish_args=--access public --tag ${DIST_TAG}" >> "$GITHUB_OUTPUT"
|
|
50
|
+
else
|
|
51
|
+
echo "publish_args=--access public" >> "$GITHUB_OUTPUT"
|
|
52
|
+
fi
|
|
53
|
+
|
|
42
54
|
- name: 'Publish to npm'
|
|
43
|
-
run: npm publish
|
|
55
|
+
run: npm publish ${{ steps.publish_meta.outputs.publish_args }}
|
package/build/lib/ai.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Zod from 'zod';
|
|
2
2
|
import { Logger } from './interfaces';
|
|
3
3
|
import { SendAction } from './bosun/action';
|
|
4
4
|
import { PromptFile } from './prompt-fs';
|
|
@@ -10,12 +10,12 @@ import { StepRegistry, Tracer } from './bosun/tracer';
|
|
|
10
10
|
export type BasicModel = 'o3-mini-2025-01-31' | 'o1-preview-2024-09-12' | 'gpt-4o-2024-08-06' | 'gpt-4o-2024-11-20' | 'gpt-4.1-2025-04-14' | 'o1-2024-12-17' | (string & {});
|
|
11
11
|
export interface LlmAdapter {
|
|
12
12
|
/**
|
|
13
|
-
* @param systemPrompt
|
|
14
|
-
* @param messages
|
|
13
|
+
* @param systemPrompt - rendered system prompt
|
|
14
|
+
* @param messages - stringified {@link Conversation}
|
|
15
15
|
* @param schema - optional Zod schema to pass to the model. Will overwrite any schema set in adapter options.
|
|
16
16
|
* @returns LLM Response
|
|
17
17
|
*/
|
|
18
|
-
generateResponse: (systemPrompt: string, messages: string, schema?: ZodTypeAny) => Promise<string>;
|
|
18
|
+
generateResponse: (systemPrompt: string, messages: string, schema?: Zod.ZodTypeAny) => Promise<string>;
|
|
19
19
|
/** Returns adapter's configuration/options for tracing */
|
|
20
20
|
getOptions: () => unknown;
|
|
21
21
|
}
|
|
@@ -37,8 +37,6 @@ export interface ProgrammaticStep<CTX> extends BasicStep<CTX> {
|
|
|
37
37
|
execute: (messages: Conversation, ctx: CTX, workflow: WorkflowControls) => Promise<unknown>;
|
|
38
38
|
}
|
|
39
39
|
export interface LLMStep<CTX> extends BasicStep<CTX> {
|
|
40
|
-
/** Determines if the step should be run or not */
|
|
41
|
-
runIf?: (messages: Conversation, ctx: CTX) => boolean | Promise<boolean>;
|
|
42
40
|
/** LLM to use. Can be a model name or an adapter. Defaults to gpt-4o */
|
|
43
41
|
model?: BasicModel | LlmAdapter;
|
|
44
42
|
/**
|
|
@@ -62,7 +60,7 @@ export interface WorkflowControls {
|
|
|
62
60
|
*/
|
|
63
61
|
rewindTo: (step: string) => void;
|
|
64
62
|
}
|
|
65
|
-
export interface JsonLLMStep<CTX, Schema extends ZodTypeAny> extends LLMStep<CTX> {
|
|
63
|
+
export interface JsonLLMStep<CTX, Schema extends Zod.ZodTypeAny> extends LLMStep<CTX> {
|
|
66
64
|
/**
|
|
67
65
|
* Defines the expected structure of the LLM's output. Accepts ZodSchema. When provided, the
|
|
68
66
|
* LLM's response is validated and parsed according to this schema ensuring reliable structured
|
|
@@ -113,27 +111,37 @@ export interface Workflow<CTX> {
|
|
|
113
111
|
* Runs the workflow with a given conversation context.
|
|
114
112
|
* Executes steps sequentially until completion or termination.
|
|
115
113
|
* @param conversation - The conversation context for the workflow
|
|
116
|
-
* @param
|
|
117
|
-
* @param beforeEach
|
|
114
|
+
* @param contextProvider - A provider function for the context that will be passed to all steps and to all prompts in those steps
|
|
115
|
+
* @param beforeEach - A callback, that runs before each step
|
|
118
116
|
* @returns The proposed reply if workflow completes, or null if terminated
|
|
119
117
|
*/
|
|
120
|
-
run: (conversation: Conversation,
|
|
118
|
+
run: (conversation: Conversation, contextProvider: (() => CTX) | (() => Promise<CTX>), beforeEach?: BeforeEachStep<CTX>) => Promise<string | null>;
|
|
121
119
|
/**
|
|
122
120
|
* Add a step to workflow
|
|
121
|
+
* @deprecated use {@link WorkflowConfig#steps} instead
|
|
123
122
|
*/
|
|
124
|
-
addStep<Schema extends ZodTypeAny>(step: JsonLLMStep<CTX, Schema>): void;
|
|
123
|
+
addStep<Schema extends Zod.ZodTypeAny>(step: JsonLLMStep<CTX, Schema>): void;
|
|
125
124
|
addStep(step: StringLLMStep<CTX>): void;
|
|
126
125
|
addStep(step: ProgrammaticStep<CTX>): void;
|
|
127
126
|
}
|
|
128
127
|
type WorkflowStep<CTX> = StringLLMStep<CTX> | JsonLLMStep<CTX, any> | ProgrammaticStep<CTX>;
|
|
128
|
+
/**
|
|
129
|
+
* Config object to be used in {@link AIEngine#createWorkflow}
|
|
130
|
+
*/
|
|
129
131
|
export interface WorkflowConfig<CTX> {
|
|
132
|
+
/** workflow name, to use in traces, defaults to 'workflow' */
|
|
133
|
+
name?: string;
|
|
134
|
+
/** a function that will run once, if any of the steps going to be executed */
|
|
135
|
+
beforeExecute?: (ctx: CTX) => Promise<void>;
|
|
136
|
+
/** a function that will run once, if any of the steps was executed */
|
|
137
|
+
afterExecute?: (ctx: CTX) => Promise<void>;
|
|
138
|
+
/** common error handler for workflow */
|
|
130
139
|
onError: (error: string, ctx: CTX) => Promise<unknown>;
|
|
140
|
+
/** workflow steps {@link ProgrammaticStep}, {@link StringLLMStep} or {@link JsonLLMStep} */
|
|
131
141
|
steps?: WorkflowStep<CTX>[];
|
|
132
|
-
name?: string;
|
|
133
|
-
beforeEachCallback?: () => Promise<unknown>;
|
|
134
142
|
}
|
|
135
143
|
interface StepBuilder<CTX> {
|
|
136
|
-
<Schema extends ZodTypeAny>(step: JsonLLMStep<CTX, Schema>): JsonLLMStep<CTX, Schema>;
|
|
144
|
+
<Schema extends Zod.ZodTypeAny>(step: JsonLLMStep<CTX, Schema>): JsonLLMStep<CTX, Schema>;
|
|
137
145
|
(step: StringLLMStep<CTX>): StringLLMStep<CTX>;
|
|
138
146
|
(step: ProgrammaticStep<CTX>): ProgrammaticStep<CTX>;
|
|
139
147
|
}
|
|
@@ -142,57 +150,25 @@ interface StepBuilder<CTX> {
|
|
|
142
150
|
*
|
|
143
151
|
* @example
|
|
144
152
|
* ```typescript
|
|
145
|
-
* import {
|
|
146
|
-
*
|
|
147
|
-
* // Create a new AI engine instance
|
|
148
|
-
* const ai = AIEngine.createAIEngine()
|
|
149
|
-
*
|
|
150
|
-
* // Create a conversation
|
|
151
|
-
* const conversation = ai.createConversation()
|
|
152
|
-
* conversation.addMessage('user', 'I need help with my order')
|
|
153
|
-
*
|
|
154
|
-
* // Define workflow steps
|
|
155
|
-
* const killswitch = ai.createStep({
|
|
156
|
-
* name: 'killswitch',
|
|
157
|
-
* prompt: ai.loadFile('prompts/killswitch.njk'),
|
|
158
|
-
* execute: async (reply) => {
|
|
159
|
-
* const result = JSON.parse(reply)
|
|
160
|
-
* if (result.terminate) {
|
|
161
|
-
* conversation.addDirective(`Terminating workflow: ${result.reason}`)
|
|
162
|
-
* return workflow.terminate()
|
|
163
|
-
* }
|
|
164
|
-
* },
|
|
165
|
-
* onError: async (error) => conversation.addDirective(`Error in killswitch: ${error}`)
|
|
166
|
-
* })
|
|
153
|
+
* import { createAIEngine } from '@recombine-ai/engine'
|
|
167
154
|
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
* execute: async (reply) => {
|
|
172
|
-
* const intent = JSON.parse(reply)
|
|
173
|
-
* conversation.addDirective(`User intent is: ${intent.category}`)
|
|
174
|
-
* },
|
|
175
|
-
* onError: async (error) => conversation.addDirective(`Error analyzing intent: ${error}`)
|
|
155
|
+
* Create a new AI engine instance
|
|
156
|
+
* const ai = createAIEngine({
|
|
157
|
+
* // engine configuration, see EngineConfig
|
|
176
158
|
* })
|
|
177
159
|
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* })
|
|
184
|
-
*
|
|
185
|
-
* // Create and run the workflow
|
|
186
|
-
* const workflow = await ai.createWorkflow(killswitch, analyzeIntent, mainReply)
|
|
187
|
-
* const response = await workflow.run(conversation)
|
|
188
|
-
* console.log(response)
|
|
160
|
+
* // create a conversation to be used in workflow.run(), see Conversation
|
|
161
|
+
* const conversation = ai.createConversation(messages)
|
|
162
|
+
* // create a workflow, see WorkflowConfig
|
|
163
|
+
* const workflow = ai.createWorkflow({steps})
|
|
164
|
+
* workflow.run(conversation)
|
|
189
165
|
* ```
|
|
190
166
|
*/
|
|
191
167
|
export interface AIEngine {
|
|
192
168
|
/**
|
|
193
169
|
* Creates a workflow from a sequence of steps.
|
|
194
170
|
* @param config - common parameters for a workflow
|
|
195
|
-
* @returns
|
|
171
|
+
* @returns AI workflow Workflow.
|
|
196
172
|
*/
|
|
197
173
|
createWorkflow: <CTX extends object>(config: WorkflowConfig<CTX>) => Workflow<CTX>;
|
|
198
174
|
/**
|
|
@@ -218,36 +194,18 @@ export interface AIEngine {
|
|
|
218
194
|
* Represents a conversation between a user and an AI agent.
|
|
219
195
|
* Provides methods to manage the conversation flow, format messages, and convert the conversation
|
|
220
196
|
* to a string representation.
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* ```typescript
|
|
224
|
-
* // Create a new conversation instance
|
|
225
|
-
* const conversation = new Conversation();
|
|
226
|
-
*
|
|
227
|
-
* // Set names for the participants
|
|
228
|
-
* conversation.setUserName("Client");
|
|
229
|
-
* conversation.setAgentName("Support");
|
|
230
|
-
*
|
|
231
|
-
* // Add messages to the conversation
|
|
232
|
-
* conversation.addMessage("user", "I need help with my account");
|
|
233
|
-
* conversation.addDirective("Ask for account details");
|
|
234
|
-
*
|
|
235
|
-
* // Get the conversation as a string to feed to an LLM
|
|
236
|
-
* const conversationText = conversation.toString();
|
|
237
|
-
* // Output:
|
|
238
|
-
* // Client: I need help with my account
|
|
239
|
-
* // System: Ask for account details
|
|
240
|
-
* ```
|
|
241
197
|
*/
|
|
242
198
|
export interface Conversation {
|
|
243
199
|
/**
|
|
244
200
|
* Sets the name of the user in the conversation to be used in {@link toString}.
|
|
245
201
|
* @param name - The name to set for the user.
|
|
202
|
+
* @deprecated
|
|
246
203
|
*/
|
|
247
204
|
setUserName(name: string): void;
|
|
248
205
|
/**
|
|
249
206
|
* Sets the name of the AI agent in the conversation to be used in {@link toString}.
|
|
250
207
|
* @param name - The name to set for the agent.
|
|
208
|
+
* @deprecated
|
|
251
209
|
*/
|
|
252
210
|
setAgentName(name: string): void;
|
|
253
211
|
/**
|
package/build/lib/ai.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../src/lib/ai.ts"],"names":[],"mappings":"AAIA,OAAO,
|
|
1
|
+
{"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../src/lib/ai.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,GAAG,MAAM,KAAK,CAAA;AAC1B,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAc,UAAU,EAAE,MAAM,gBAAgB,CAAA;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAmC,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAChF,OAAO,EAAiC,YAAY,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAIpF;;GAEG;AACH,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this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
37
|
};
|
|
@@ -9,7 +42,7 @@ exports.getStepBuilder = getStepBuilder;
|
|
|
9
42
|
// cspell:words lstripBlocks
|
|
10
43
|
const crypto_1 = require("crypto");
|
|
11
44
|
const nunjucks_1 = __importDefault(require("nunjucks"));
|
|
12
|
-
const
|
|
45
|
+
const Zod = __importStar(require("zod"));
|
|
13
46
|
const action_1 = require("./bosun/action");
|
|
14
47
|
const stepTracer_1 = require("./bosun/stepTracer");
|
|
15
48
|
const tracer_1 = require("./bosun/tracer");
|
|
@@ -45,12 +78,15 @@ function createAIEngine(cfg = {}) {
|
|
|
45
78
|
const stepTracer = cfg.stepTracer || (0, stepTracer_1.createStubStepTracer)(logger);
|
|
46
79
|
const registry = cfg.stepRegistry || cfg.tracer || (0, tracer_1.createStubRegistry)(logger);
|
|
47
80
|
// tokenStorage is used by the default adapter to fetch API keys (backwards compatible)
|
|
48
|
-
function createWorkflow({ onError, steps = [], name = 'workflow', }) {
|
|
81
|
+
function createWorkflow({ onError, beforeExecute, afterExecute, steps = [], name = 'workflow', }) {
|
|
49
82
|
steps.forEach(addStepToTracer);
|
|
50
83
|
return {
|
|
51
|
-
run: async (messages,
|
|
84
|
+
run: async (messages, contextProvider, beforeEach) => {
|
|
52
85
|
const state = new WorkflowState(logger, steps);
|
|
86
|
+
let beforeHookExecuted = false;
|
|
87
|
+
let afterHookExecuted = false;
|
|
53
88
|
do {
|
|
89
|
+
const ctx = await contextProvider();
|
|
54
90
|
await beforeEach?.(messages, ctx, state);
|
|
55
91
|
const step = state.getStep();
|
|
56
92
|
if (state.isTerminated()) {
|
|
@@ -58,14 +94,23 @@ function createAIEngine(cfg = {}) {
|
|
|
58
94
|
break;
|
|
59
95
|
}
|
|
60
96
|
if (!step.runIf || (await step.runIf(messages, ctx))) {
|
|
97
|
+
// TODO: drop actions, they are replaced by traces
|
|
61
98
|
const action = (0, action_1.makeAction)(cfg.sendAction, 'AI', step.name);
|
|
62
99
|
await action('started');
|
|
100
|
+
if (beforeExecute && !beforeHookExecuted) {
|
|
101
|
+
await beforeExecute(ctx);
|
|
102
|
+
beforeHookExecuted = true;
|
|
103
|
+
}
|
|
63
104
|
if ('prompt' in step) {
|
|
64
105
|
await runStep(step, messages, ctx, state);
|
|
65
106
|
}
|
|
66
107
|
else {
|
|
67
108
|
await runProgrammaticStep(step, messages, ctx, state);
|
|
68
109
|
}
|
|
110
|
+
if (afterExecute && !afterHookExecuted) {
|
|
111
|
+
await afterExecute(ctx);
|
|
112
|
+
afterHookExecuted = true;
|
|
113
|
+
}
|
|
69
114
|
await action('completed');
|
|
70
115
|
}
|
|
71
116
|
} while (state.next());
|
|
@@ -99,7 +144,7 @@ function createAIEngine(cfg = {}) {
|
|
|
99
144
|
? JSON.stringify(step.model.getOptions())
|
|
100
145
|
: 'default',
|
|
101
146
|
schema: 'schema' in step
|
|
102
|
-
? step.schema instanceof
|
|
147
|
+
? step.schema instanceof Zod.ZodSchema
|
|
103
148
|
? step.schema
|
|
104
149
|
: undefined
|
|
105
150
|
: undefined,
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@recombine-ai/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.1-beta",
|
|
4
4
|
"description": "Recombine AI engine for creating conversational AI agents",
|
|
5
5
|
"repository": {
|
|
6
|
-
"url": "https://github.com/recombine-ai/engine"
|
|
6
|
+
"url": "git+https://github.com/recombine-ai/engine.git"
|
|
7
7
|
},
|
|
8
8
|
"main": "build/index.js",
|
|
9
9
|
"types": "build/index.d.ts",
|