@aigne/core 0.4.194 → 0.4.196

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 (66) hide show
  1. package/lib/cjs/constants.js +11 -0
  2. package/lib/cjs/function-agent.js +81 -0
  3. package/lib/cjs/function-runner.js +29 -0
  4. package/lib/cjs/index.js +13 -1
  5. package/lib/cjs/llm-agent.js +166 -0
  6. package/lib/cjs/llm-decision-agent.js +169 -0
  7. package/lib/cjs/llm-model.js +27 -0
  8. package/lib/cjs/local-function-agent.js +80 -0
  9. package/lib/cjs/memory.js +32 -0
  10. package/lib/cjs/pipeline-agent.js +199 -0
  11. package/lib/cjs/runnable.js +29 -0
  12. package/lib/cjs/tsconfig.tsbuildinfo +1 -0
  13. package/lib/cjs/{types → utils}/index.js +4 -2
  14. package/lib/cjs/utils/is-non-nullable.js +20 -0
  15. package/lib/cjs/utils/mustache-utils.js +14 -0
  16. package/lib/cjs/utils/omit.js +2 -0
  17. package/lib/cjs/utils/ordered-map.js +71 -0
  18. package/lib/cjs/utils/stream-utils.js +25 -0
  19. package/lib/esm/constants.js +8 -0
  20. package/lib/esm/function-agent.js +77 -0
  21. package/lib/esm/function-runner.js +25 -0
  22. package/lib/esm/index.js +13 -1
  23. package/lib/esm/llm-agent.js +162 -0
  24. package/lib/esm/llm-decision-agent.js +165 -0
  25. package/lib/esm/llm-model.js +23 -0
  26. package/lib/esm/local-function-agent.js +76 -0
  27. package/lib/esm/memory.js +27 -0
  28. package/lib/esm/pipeline-agent.js +192 -0
  29. package/lib/esm/runnable.js +23 -0
  30. package/lib/esm/tsconfig.tsbuildinfo +1 -0
  31. package/lib/esm/utils/index.js +4 -0
  32. package/lib/esm/utils/is-non-nullable.js +13 -0
  33. package/lib/esm/utils/mustache-utils.js +8 -0
  34. package/lib/esm/utils/omit.js +1 -0
  35. package/lib/esm/utils/ordered-map.js +68 -0
  36. package/lib/esm/utils/stream-utils.js +21 -0
  37. package/lib/types/constants.d.ts +8 -0
  38. package/lib/types/context.d.ts +5 -0
  39. package/lib/types/data-type.d.ts +32 -0
  40. package/lib/types/function-agent.d.ts +36 -0
  41. package/lib/types/function-runner.d.ts +11 -0
  42. package/lib/types/index.d.ts +13 -1
  43. package/lib/types/llm-agent.d.ts +37 -0
  44. package/lib/types/llm-decision-agent.d.ts +66 -0
  45. package/lib/types/llm-model.d.ts +79 -0
  46. package/lib/types/local-function-agent.d.ts +38 -0
  47. package/lib/types/memory.d.ts +186 -0
  48. package/lib/types/pipeline-agent.d.ts +69 -0
  49. package/lib/types/runnable.d.ts +49 -0
  50. package/lib/types/tsconfig.tsbuildinfo +1 -0
  51. package/lib/types/utils/index.d.ts +4 -0
  52. package/lib/types/utils/is-non-nullable.d.ts +2 -0
  53. package/lib/types/utils/mustache-utils.d.ts +3 -0
  54. package/lib/types/utils/omit.d.ts +1 -0
  55. package/lib/types/utils/ordered-map.d.ts +28 -0
  56. package/lib/types/utils/stream-utils.d.ts +7 -0
  57. package/package.json +6 -4
  58. package/tsconfig.json +3 -7
  59. package/lib/esm/types/index.js +0 -2
  60. package/lib/types/types/agent.d.ts +0 -5
  61. package/lib/types/types/index.d.ts +0 -2
  62. package/lib/types/types/runnable.d.ts +0 -20
  63. /package/lib/cjs/{types/agent.js → context.js} +0 -0
  64. /package/lib/cjs/{types/runnable.js → data-type.js} +0 -0
  65. /package/lib/esm/{types/agent.js → context.js} +0 -0
  66. /package/lib/esm/{types/runnable.js → data-type.js} +0 -0
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StreamTextOutputName = exports.TYPES = void 0;
4
+ exports.TYPES = {
5
+ context: Symbol.for('AIGNE_CONTEXT'),
6
+ definition: Symbol.for('AIGNE_DEFINITION'),
7
+ llmModel: Symbol.for('AIGNE_LLM_MODEL'),
8
+ llmModelConfiguration: Symbol.for('AIGNE_LLM_MODEL_CONFIGURATION'),
9
+ functionRunner: Symbol.for('AIGNE_FUNCTION_RUNNER'),
10
+ };
11
+ exports.StreamTextOutputName = '$text';
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var FunctionAgent_1;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.FunctionAgent = void 0;
17
+ exports.createFunctionAgentDefinition = createFunctionAgentDefinition;
18
+ const nanoid_1 = require("nanoid");
19
+ const tsyringe_1 = require("tsyringe");
20
+ const constants_1 = require("./constants");
21
+ const function_runner_1 = require("./function-runner");
22
+ const runnable_1 = require("./runnable");
23
+ const utils_1 = require("./utils");
24
+ let FunctionAgent = FunctionAgent_1 = class FunctionAgent extends runnable_1.Runnable {
25
+ definition;
26
+ runner;
27
+ static create(options) {
28
+ const definition = createFunctionAgentDefinition(options);
29
+ return new FunctionAgent_1(definition);
30
+ }
31
+ constructor(definition, runner) {
32
+ super(definition);
33
+ this.definition = definition;
34
+ this.runner = runner;
35
+ }
36
+ async run(input, options) {
37
+ const { definition: { language, code, ...definition }, runner, } = this;
38
+ if (!runner)
39
+ throw new Error('Function runner is required');
40
+ if (!language || !code)
41
+ throw new Error('Language and code are required');
42
+ const result = await runner.run({
43
+ name: definition.name || definition.id,
44
+ language,
45
+ code,
46
+ arguments: input,
47
+ });
48
+ // TODO: validate the result against the definition.outputs
49
+ return options?.stream ? (0, utils_1.objectToRunnableResponseStream)(result) : result;
50
+ }
51
+ };
52
+ exports.FunctionAgent = FunctionAgent;
53
+ exports.FunctionAgent = FunctionAgent = FunctionAgent_1 = __decorate([
54
+ (0, tsyringe_1.injectable)(),
55
+ __param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
56
+ __param(1, (0, tsyringe_1.inject)(constants_1.TYPES.functionRunner)),
57
+ __metadata("design:paramtypes", [Object, function_runner_1.FunctionRunner])
58
+ ], FunctionAgent);
59
+ function createFunctionAgentDefinition(options) {
60
+ const inputs = utils_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
61
+ id: (0, nanoid_1.nanoid)(),
62
+ name: i.name,
63
+ type: i.type,
64
+ required: i.required,
65
+ })));
66
+ const outputs = utils_1.OrderedRecord.fromArray(options.outputs?.map((i) => ({
67
+ id: (0, nanoid_1.nanoid)(),
68
+ name: i.name,
69
+ type: i.type,
70
+ required: i.required,
71
+ })));
72
+ return {
73
+ id: options.id || options.name || (0, nanoid_1.nanoid)(),
74
+ name: options.name,
75
+ type: 'function_agent',
76
+ inputs,
77
+ outputs,
78
+ language: options.language,
79
+ code: options.code,
80
+ };
81
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FunctionRunner = void 0;
4
+ const runnable_1 = require("./runnable");
5
+ const utils_1 = require("./utils");
6
+ class FunctionRunner extends runnable_1.Runnable {
7
+ constructor() {
8
+ super({
9
+ id: 'function_runner',
10
+ type: 'function_runner',
11
+ name: 'Function Runner',
12
+ description: 'Run a function',
13
+ inputs: utils_1.OrderedRecord.fromArray([
14
+ { id: 'name', name: 'name', type: 'string', required: true },
15
+ { id: 'language', name: 'language', type: 'string', required: true },
16
+ { id: 'code', name: 'code', type: 'string', required: true },
17
+ { id: 'arguments', name: 'arguments', type: 'object', required: false },
18
+ ]),
19
+ outputs: utils_1.OrderedRecord.fromArray([
20
+ {
21
+ id: 'result',
22
+ name: 'result',
23
+ type: 'object',
24
+ },
25
+ ]),
26
+ });
27
+ }
28
+ }
29
+ exports.FunctionRunner = FunctionRunner;
package/lib/cjs/index.js CHANGED
@@ -14,4 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./types"), exports);
17
+ __exportStar(require("./utils"), exports);
18
+ __exportStar(require("./constants"), exports);
19
+ __exportStar(require("./data-type"), exports);
20
+ __exportStar(require("./context"), exports);
21
+ __exportStar(require("./runnable"), exports);
22
+ __exportStar(require("./pipeline-agent"), exports);
23
+ __exportStar(require("./llm-agent"), exports);
24
+ __exportStar(require("./llm-model"), exports);
25
+ __exportStar(require("./function-agent"), exports);
26
+ __exportStar(require("./function-runner"), exports);
27
+ __exportStar(require("./llm-decision-agent"), exports);
28
+ __exportStar(require("./local-function-agent"), exports);
29
+ __exportStar(require("./memory"), exports);
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var LLMAgent_1;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.LLMAgent = void 0;
17
+ exports.createLLMAgentDefinition = createLLMAgentDefinition;
18
+ const lodash_1 = require("lodash");
19
+ const nanoid_1 = require("nanoid");
20
+ const tsyringe_1 = require("tsyringe");
21
+ const constants_1 = require("./constants");
22
+ const llm_model_1 = require("./llm-model");
23
+ const runnable_1 = require("./runnable");
24
+ const utils_1 = require("./utils");
25
+ const mustache_utils_1 = require("./utils/mustache-utils");
26
+ const ordered_map_1 = require("./utils/ordered-map");
27
+ let LLMAgent = LLMAgent_1 = class LLMAgent extends runnable_1.Runnable {
28
+ definition;
29
+ model;
30
+ static create(options) {
31
+ const definition = createLLMAgentDefinition(options);
32
+ return new LLMAgent_1(definition);
33
+ }
34
+ constructor(definition, model) {
35
+ super(definition);
36
+ this.definition = definition;
37
+ this.model = model;
38
+ }
39
+ async run(input, options) {
40
+ const { definition, model } = this;
41
+ if (!model)
42
+ throw new Error('LLM model is required');
43
+ const messages = ordered_map_1.OrderedRecord.toArray(definition.messages);
44
+ if (!messages.length)
45
+ throw new Error('Messages are required');
46
+ // TODO: support comment/image for messages
47
+ const llmInputs = {
48
+ messages: messages.map(({ role, content }) => ({
49
+ role,
50
+ content: (0, mustache_utils_1.renderMessage)(content, input),
51
+ })),
52
+ modelOptions: definition.modelOptions,
53
+ };
54
+ const outputs = ordered_map_1.OrderedRecord.toArray(definition.outputs).filter((0, utils_1.isPropsNonNullable)('name'));
55
+ const textOutput = outputs.find((i) => i.name === constants_1.StreamTextOutputName);
56
+ const jsonOutputs = outputs.filter((i) => i.name !== constants_1.StreamTextOutputName);
57
+ const outputJsonSchema = jsonOutputs.length ? outputsToJsonSchema(ordered_map_1.OrderedRecord.fromArray(jsonOutputs)) : undefined;
58
+ const jsonOutput = outputJsonSchema
59
+ ? model
60
+ .run({
61
+ ...llmInputs,
62
+ responseFormat: outputJsonSchema && {
63
+ type: 'json_schema',
64
+ jsonSchema: {
65
+ name: 'output',
66
+ schema: outputJsonSchema,
67
+ strict: true,
68
+ },
69
+ },
70
+ })
71
+ .then(async (response) => {
72
+ if (!response.$text)
73
+ throw new Error('No text in JSON mode response');
74
+ const json = JSON.parse(response.$text);
75
+ // TODO: validate json with outputJsonSchema
76
+ return json;
77
+ })
78
+ : undefined;
79
+ if (options?.stream) {
80
+ return new ReadableStream({
81
+ start: async (controller) => {
82
+ try {
83
+ if (textOutput) {
84
+ const textStreamOutput = await model.run(llmInputs, { stream: true });
85
+ for await (const chunk of textStreamOutput) {
86
+ controller.enqueue({ $text: chunk.$text });
87
+ }
88
+ }
89
+ controller.enqueue({ delta: await jsonOutput });
90
+ }
91
+ catch (error) {
92
+ controller.error(error);
93
+ }
94
+ finally {
95
+ controller.close();
96
+ }
97
+ },
98
+ });
99
+ }
100
+ const text = textOutput ? await model.run(llmInputs) : undefined;
101
+ return {
102
+ $text: text,
103
+ ...(await jsonOutput),
104
+ };
105
+ }
106
+ };
107
+ exports.LLMAgent = LLMAgent;
108
+ exports.LLMAgent = LLMAgent = LLMAgent_1 = __decorate([
109
+ (0, tsyringe_1.injectable)(),
110
+ __param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
111
+ __param(1, (0, tsyringe_1.inject)(constants_1.TYPES.llmModel)),
112
+ __metadata("design:paramtypes", [Object, llm_model_1.LLMModel])
113
+ ], LLMAgent);
114
+ function createLLMAgentDefinition(options) {
115
+ const inputs = ordered_map_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
116
+ id: (0, nanoid_1.nanoid)(),
117
+ name: i.name,
118
+ type: i.type,
119
+ required: i.required,
120
+ })));
121
+ const outputs = ordered_map_1.OrderedRecord.fromArray(options.outputs?.map((i) => ({ ...i, id: (0, nanoid_1.nanoid)() })));
122
+ const messages = ordered_map_1.OrderedRecord.fromArray(options.messages?.map((i) => ({
123
+ id: (0, nanoid_1.nanoid)(),
124
+ role: i.role,
125
+ content: i.content,
126
+ })));
127
+ return {
128
+ id: options.id || options.name || (0, nanoid_1.nanoid)(),
129
+ name: options.name,
130
+ type: 'llm_agent',
131
+ inputs,
132
+ outputs,
133
+ messages,
134
+ modelOptions: options.modelOptions,
135
+ };
136
+ }
137
+ function outputsToJsonSchema(outputs) {
138
+ const outputToSchema = (output) => {
139
+ const properties = output.type === 'object' && output.properties?.$indexes.length
140
+ ? ordered_map_1.OrderedRecord.map(output.properties, (property) => {
141
+ if (!property.name)
142
+ return null;
143
+ const schema = outputToSchema(property);
144
+ if (!schema)
145
+ return null;
146
+ return { schema, property };
147
+ }).filter(utils_1.isNonNullable)
148
+ : undefined;
149
+ return (0, lodash_1.omitBy)({
150
+ type: output.type,
151
+ description: output.description,
152
+ properties: properties?.length
153
+ ? Object.fromEntries(properties.map((p) => [p.property.name, p.schema]))
154
+ : undefined,
155
+ items: output.type === 'array' && output.items ? outputToSchema(output.items) : undefined,
156
+ additionalProperties: output.type === 'object' ? false : undefined,
157
+ required: properties?.length
158
+ ? properties.filter((i) => i.property.required).map((i) => i.property.name)
159
+ : undefined,
160
+ }, (v) => v === undefined);
161
+ };
162
+ return outputToSchema({
163
+ type: 'object',
164
+ properties: outputs,
165
+ });
166
+ }
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var LLMDecisionAgent_1;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.LLMDecisionAgent = void 0;
17
+ exports.createLLMDecisionAgentDefinition = createLLMDecisionAgentDefinition;
18
+ const lodash_1 = require("lodash");
19
+ const nanoid_1 = require("nanoid");
20
+ const tsyringe_1 = require("tsyringe");
21
+ const constants_1 = require("./constants");
22
+ const llm_model_1 = require("./llm-model");
23
+ const runnable_1 = require("./runnable");
24
+ const utils_1 = require("./utils");
25
+ let LLMDecisionAgent = LLMDecisionAgent_1 = class LLMDecisionAgent extends runnable_1.Runnable {
26
+ definition;
27
+ model;
28
+ context;
29
+ static create(options) {
30
+ const definition = createLLMDecisionAgentDefinition(options);
31
+ return new LLMDecisionAgent_1(definition);
32
+ }
33
+ constructor(definition, model, context) {
34
+ super(definition);
35
+ this.definition = definition;
36
+ this.model = model;
37
+ this.context = context;
38
+ }
39
+ async run(input, options) {
40
+ const { definition, context, model } = this;
41
+ if (!model)
42
+ throw new Error('LLM model is required');
43
+ if (!context)
44
+ throw new Error('Context is required');
45
+ const messages = utils_1.OrderedRecord.toArray(definition.messages);
46
+ if (!messages.length)
47
+ throw new Error('Messages are required');
48
+ const cases = await Promise.all(utils_1.OrderedRecord.map(definition.cases, async (t) => {
49
+ if (!t.runnable?.id)
50
+ throw new Error('Runnable is required');
51
+ const runnable = await context.resolve(t.runnable.id);
52
+ // TODO: auto generate name by llm model if needed
53
+ const name = t.name || runnable.name;
54
+ if (!name)
55
+ throw new Error('Case name is required');
56
+ return { name, description: t.description, runnable, input: t.input };
57
+ }));
58
+ const llmInputs = {
59
+ messages: messages.map(({ role, content }) => ({
60
+ role,
61
+ content: typeof content === 'string' ? (0, utils_1.renderMessage)(content, input) : content,
62
+ })),
63
+ modelOptions: definition.modelOptions,
64
+ tools: cases.map((t) => {
65
+ // TODO: auto generate parameters by llm model if needed
66
+ return {
67
+ type: 'function',
68
+ function: {
69
+ name: t.name,
70
+ description: t.description,
71
+ parameters: {},
72
+ },
73
+ };
74
+ }),
75
+ toolChoice: 'required',
76
+ };
77
+ const { toolCalls } = await model.run(llmInputs);
78
+ // TODO: support run multiple calls
79
+ const functionNameToCall = toolCalls?.[0].function?.name;
80
+ if (!functionNameToCall)
81
+ throw new Error('No any runnable called');
82
+ const caseToCall = cases.find((i) => i.name === functionNameToCall);
83
+ if (!caseToCall)
84
+ throw new Error('Case not found');
85
+ // NOTE: 将 input 转换为 variables,其中 key 为 inputId,value 为 input 的值
86
+ const variables = Object.fromEntries(utils_1.OrderedRecord.map(this.definition.inputs, (i) => {
87
+ const value = input[i.name || i.id];
88
+ if ((0, lodash_1.isNil)(value))
89
+ return null;
90
+ return [i.id, value];
91
+ }).filter(utils_1.isNonNullable));
92
+ const inputForCase = Object.fromEntries(Object.entries(caseToCall.input ?? {})
93
+ .map(([inputId, { from, fromVariableId, fromVariablePropPath }]) => {
94
+ const targetInput = utils_1.OrderedRecord.find(caseToCall.runnable.definition.inputs, (i) => i.id === inputId);
95
+ if (!targetInput?.name)
96
+ return null;
97
+ if (from !== 'variable' || !fromVariableId)
98
+ return null;
99
+ const v = variables[fromVariableId];
100
+ const value = fromVariablePropPath?.length ? (0, lodash_1.get)(v, fromVariablePropPath) : v;
101
+ return [targetInput.name, value];
102
+ })
103
+ .filter(utils_1.isNonNullable));
104
+ // TODO: check result structure and omit undefined values
105
+ return (await caseToCall.runnable.run(inputForCase, options));
106
+ }
107
+ };
108
+ exports.LLMDecisionAgent = LLMDecisionAgent;
109
+ exports.LLMDecisionAgent = LLMDecisionAgent = LLMDecisionAgent_1 = __decorate([
110
+ (0, tsyringe_1.injectable)(),
111
+ __param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
112
+ __param(1, (0, tsyringe_1.inject)(constants_1.TYPES.llmModel)),
113
+ __param(2, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
114
+ __metadata("design:paramtypes", [Object, llm_model_1.LLMModel, Object])
115
+ ], LLMDecisionAgent);
116
+ function createLLMDecisionAgentDefinition(options) {
117
+ const inputs = utils_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
118
+ id: (0, nanoid_1.nanoid)(),
119
+ name: i.name,
120
+ type: i.type,
121
+ required: i.required,
122
+ })));
123
+ const messages = utils_1.OrderedRecord.fromArray([
124
+ {
125
+ id: (0, nanoid_1.nanoid)(),
126
+ role: 'system',
127
+ content: options.messages,
128
+ },
129
+ ]);
130
+ const cases = utils_1.OrderedRecord.fromArray(options.cases.map((c) => ({
131
+ id: (0, nanoid_1.nanoid)(),
132
+ name: c.name || c.runnable.name,
133
+ description: c.description,
134
+ runnable: { id: c.runnable.id },
135
+ // TODO: pass input from decision to case runnable
136
+ input: Object.fromEntries(utils_1.OrderedRecord.map(c.runnable.definition.inputs, (inputOfCase) => {
137
+ const i = c.input?.[inputOfCase.name || inputOfCase.id];
138
+ if (!i) {
139
+ if (inputOfCase.required) {
140
+ throw new Error(`Input ${inputOfCase.name || inputOfCase.id} for case ${c.runnable.name || c.runnable.id} is required`);
141
+ }
142
+ // ignore optional input
143
+ return null;
144
+ }
145
+ const inputFromDecision = utils_1.OrderedRecord.find(inputs, (input) => input.name === i.fromVariable);
146
+ if (!inputFromDecision)
147
+ throw new Error(`Input ${i.fromVariable} not found`);
148
+ return [
149
+ inputOfCase.id,
150
+ {
151
+ from: 'variable',
152
+ fromVariableId: inputFromDecision.id,
153
+ fromVariablePropPath: i.fromVariablePropPath,
154
+ },
155
+ ];
156
+ }).filter(utils_1.isNonNullable)),
157
+ })));
158
+ return {
159
+ id: options.id || options.name || (0, nanoid_1.nanoid)(),
160
+ name: options.name,
161
+ type: 'llm_decision_agent',
162
+ inputs,
163
+ // TODO: decision agent outputs should be the union of all case outputs
164
+ outputs: utils_1.OrderedRecord.fromArray([]),
165
+ messages,
166
+ modelOptions: options.modelOptions,
167
+ cases,
168
+ };
169
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LLMModel = void 0;
4
+ const runnable_1 = require("./runnable");
5
+ const utils_1 = require("./utils");
6
+ class LLMModel extends runnable_1.Runnable {
7
+ constructor() {
8
+ super({
9
+ id: 'llm_model',
10
+ type: 'llm_model',
11
+ name: 'LLM Model',
12
+ description: 'Run a LLM model',
13
+ inputs: utils_1.OrderedRecord.fromArray([
14
+ { id: 'messages', name: 'messages', type: 'array', required: true },
15
+ { id: 'responseFormat', name: 'responseFormat', type: 'object' },
16
+ { id: 'tools', name: 'tools', type: 'array' },
17
+ { id: 'toolChoice', name: 'toolChoice', type: 'object' },
18
+ { id: 'modelOptions', name: 'modelOptions', type: 'object' },
19
+ ]),
20
+ outputs: utils_1.OrderedRecord.fromArray([
21
+ { id: '$text', name: '$text', type: 'string' },
22
+ { id: 'toolCalls', name: 'toolCalls', type: 'object' },
23
+ ]),
24
+ });
25
+ }
26
+ }
27
+ exports.LLMModel = LLMModel;
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var LocalFunctionAgent_1;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.LocalFunctionAgent = void 0;
17
+ exports.createLocalFunctionAgentDefinition = createLocalFunctionAgentDefinition;
18
+ const nanoid_1 = require("nanoid");
19
+ const tsyringe_1 = require("tsyringe");
20
+ const constants_1 = require("./constants");
21
+ const runnable_1 = require("./runnable");
22
+ const utils_1 = require("./utils");
23
+ let LocalFunctionAgent = LocalFunctionAgent_1 = class LocalFunctionAgent extends runnable_1.Runnable {
24
+ definition;
25
+ context;
26
+ static create(options) {
27
+ const definition = createLocalFunctionAgentDefinition(options);
28
+ return new LocalFunctionAgent_1(definition);
29
+ }
30
+ constructor(definition, context) {
31
+ super(definition);
32
+ this.definition = definition;
33
+ this.context = context;
34
+ }
35
+ async run(input, options) {
36
+ const { definition: { function: func }, context, } = this;
37
+ if (!func)
38
+ throw new Error('Function is required');
39
+ if (!context)
40
+ throw new Error('Context is required');
41
+ const result = (await func(input, { context }));
42
+ // TODO: validate the result against the definition.outputs
43
+ return options?.stream
44
+ ? result instanceof ReadableStream
45
+ ? result
46
+ : (0, utils_1.objectToRunnableResponseStream)(result)
47
+ : result instanceof ReadableStream
48
+ ? (0, utils_1.runnableResponseStreamToObject)(result)
49
+ : result;
50
+ }
51
+ };
52
+ exports.LocalFunctionAgent = LocalFunctionAgent;
53
+ exports.LocalFunctionAgent = LocalFunctionAgent = LocalFunctionAgent_1 = __decorate([
54
+ (0, tsyringe_1.injectable)(),
55
+ __param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
56
+ __param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
57
+ __metadata("design:paramtypes", [Object, Object])
58
+ ], LocalFunctionAgent);
59
+ function createLocalFunctionAgentDefinition(options) {
60
+ const inputs = utils_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
61
+ id: (0, nanoid_1.nanoid)(),
62
+ name: i.name,
63
+ type: i.type,
64
+ required: i.required,
65
+ })));
66
+ const outputs = utils_1.OrderedRecord.fromArray(options.outputs?.map((i) => ({
67
+ id: (0, nanoid_1.nanoid)(),
68
+ name: i.name,
69
+ type: i.type,
70
+ required: i.required,
71
+ })));
72
+ return {
73
+ id: options.id || options.name || (0, nanoid_1.nanoid)(),
74
+ name: options.name,
75
+ type: 'local_function_agent',
76
+ inputs,
77
+ outputs,
78
+ function: options.function,
79
+ };
80
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryRunner = exports.Memory = void 0;
4
+ const lodash_1 = require("lodash");
5
+ const runnable_1 = require("./runnable");
6
+ const utils_1 = require("./utils");
7
+ class Memory extends runnable_1.Runnable {
8
+ constructor() {
9
+ super({
10
+ id: 'memory',
11
+ type: 'memory',
12
+ name: 'Memory',
13
+ inputs: utils_1.OrderedRecord.fromArray([]),
14
+ outputs: utils_1.OrderedRecord.fromArray([]),
15
+ });
16
+ }
17
+ }
18
+ exports.Memory = Memory;
19
+ class MemoryRunner extends runnable_1.Runnable {
20
+ constructor(name) {
21
+ const id = `${(0, lodash_1.camelCase)(name)}_runner`;
22
+ super({
23
+ id,
24
+ type: id,
25
+ name: `${(0, lodash_1.startCase)(name)} Runner`,
26
+ description: `${(0, lodash_1.startCase)(name)} Runner`,
27
+ inputs: utils_1.OrderedRecord.fromArray([]),
28
+ outputs: utils_1.OrderedRecord.fromArray([]),
29
+ });
30
+ }
31
+ }
32
+ exports.MemoryRunner = MemoryRunner;