@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,199 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
17
+ var PipelineAgent_1;
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.PipelineAgent = void 0;
20
+ exports.createPipelineAgentDefinition = createPipelineAgentDefinition;
21
+ const lodash_1 = require("lodash");
22
+ const nanoid_1 = require("nanoid");
23
+ const tsyringe_1 = require("tsyringe");
24
+ const constants_1 = require("./constants");
25
+ const logger_1 = __importDefault(require("./logger"));
26
+ const runnable_1 = require("./runnable");
27
+ const utils_1 = require("./utils");
28
+ const is_non_nullable_1 = require("./utils/is-non-nullable");
29
+ const ordered_map_1 = require("./utils/ordered-map");
30
+ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends runnable_1.Runnable {
31
+ definition;
32
+ context;
33
+ static create(options) {
34
+ const definition = createPipelineAgentDefinition(options);
35
+ return new PipelineAgent_1(definition);
36
+ }
37
+ constructor(definition, context) {
38
+ super(definition);
39
+ this.definition = definition;
40
+ this.context = context;
41
+ }
42
+ async run(input, options) {
43
+ // TODO: validate the input against the definition
44
+ const { definition, context } = this;
45
+ if (!context)
46
+ throw new Error('Context is required');
47
+ const { processes } = definition;
48
+ if (!processes?.$indexes.length) {
49
+ throw new Error('No processes defined');
50
+ }
51
+ const result = new ReadableStream({
52
+ async start(controller) {
53
+ try {
54
+ // NOTE: 将 input 转换为 variables,其中 key 为 inputId,value 为 input 的值
55
+ const variables = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.inputs, (i) => {
56
+ const value = input[i.name || i.id];
57
+ if ((0, lodash_1.isNil)(value))
58
+ return null;
59
+ return [i.id, value];
60
+ }).filter(is_non_nullable_1.isNonNullable));
61
+ const outputs = ordered_map_1.OrderedRecord.toArray(definition.outputs);
62
+ const textStreamOutput = outputs.find((i) => i.name === constants_1.StreamTextOutputName);
63
+ let result = {};
64
+ for (const process of ordered_map_1.OrderedRecord.iterator(processes)) {
65
+ if (!process.runnable?.id) {
66
+ logger_1.default.warn('Runnable id is required for process', process);
67
+ continue;
68
+ }
69
+ const runnable = await context.resolve(process.runnable.id);
70
+ if (!runnable)
71
+ continue;
72
+ const inputValues = Object.fromEntries(Object.entries(process.input ?? {})
73
+ .map(([inputId, input]) => {
74
+ const targetInput = runnable.definition.inputs?.[inputId];
75
+ if (!targetInput?.name)
76
+ return null;
77
+ let value;
78
+ if (input.from === 'variable') {
79
+ const v = variables[input.fromVariableId];
80
+ value = input.fromVariablePropPath?.length ? (0, lodash_1.get)(v, input.fromVariablePropPath) : v;
81
+ }
82
+ else {
83
+ throw new Error('Unsupported input source');
84
+ }
85
+ return [targetInput.name, value];
86
+ })
87
+ .filter(is_non_nullable_1.isNonNullable));
88
+ const stream = await runnable.run(inputValues, { stream: true });
89
+ const needRespondTextStream = textStreamOutput?.from === 'variable' && textStreamOutput.fromVariableId === process.id;
90
+ const needRespondJsonStream = outputs.some((i) => i.name !== constants_1.StreamTextOutputName && i.from === 'variable' && i.fromVariableId === process.id);
91
+ for await (const chunk of stream) {
92
+ if (chunk.$text && needRespondTextStream) {
93
+ controller.enqueue({ $text: chunk.$text });
94
+ }
95
+ if (chunk.delta) {
96
+ variables[process.id] = chunk.delta;
97
+ if (needRespondJsonStream) {
98
+ result = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.outputs, (output) => {
99
+ if (!output.name)
100
+ return null;
101
+ let value;
102
+ if (output.from === 'variable') {
103
+ const v = variables[output.fromVariableId];
104
+ value = output.fromVariablePropPath?.length ? (0, lodash_1.get)(v, output.fromVariablePropPath) : v;
105
+ }
106
+ else {
107
+ throw new Error(`Unsupported output source ${output.from}`);
108
+ }
109
+ return [output.name, value];
110
+ }).filter(is_non_nullable_1.isNonNullable));
111
+ controller.enqueue({ delta: result });
112
+ }
113
+ }
114
+ }
115
+ }
116
+ }
117
+ catch (error) {
118
+ controller.error(error);
119
+ }
120
+ finally {
121
+ controller.close();
122
+ }
123
+ },
124
+ });
125
+ if (options?.stream) {
126
+ return result;
127
+ }
128
+ // TODO: validate the result against the definition.outputs
129
+ return await (0, utils_1.runnableResponseStreamToObject)(result);
130
+ }
131
+ };
132
+ exports.PipelineAgent = PipelineAgent;
133
+ exports.PipelineAgent = PipelineAgent = PipelineAgent_1 = __decorate([
134
+ (0, tsyringe_1.injectable)(),
135
+ __param(0, (0, tsyringe_1.inject)(constants_1.TYPES.definition)),
136
+ __param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
137
+ __metadata("design:paramtypes", [Object, Object])
138
+ ], PipelineAgent);
139
+ function createPipelineAgentDefinition(options) {
140
+ const inputs = ordered_map_1.OrderedRecord.fromArray(options.inputs?.map((i) => ({
141
+ id: (0, nanoid_1.nanoid)(),
142
+ name: i.name,
143
+ type: i.type,
144
+ required: i.required,
145
+ })));
146
+ const processes = ordered_map_1.OrderedRecord.fromArray([]);
147
+ for (const p of options.processes || []) {
148
+ ordered_map_1.OrderedRecord.push(processes, {
149
+ id: (0, nanoid_1.nanoid)(),
150
+ name: p.name || p.runnable?.name,
151
+ runnable: { id: p.runnable.id },
152
+ input: Object.fromEntries(ordered_map_1.OrderedRecord.map(p.runnable.definition.inputs, (inputOfProcess) => {
153
+ const i = p.input?.[inputOfProcess.name || inputOfProcess.id];
154
+ if (!i) {
155
+ if (inputOfProcess.required) {
156
+ throw new Error(`Input ${inputOfProcess.name || inputOfProcess.id} for case ${p.runnable.name || p.runnable.id} is required`);
157
+ }
158
+ // ignore optional input
159
+ return null;
160
+ }
161
+ const inputFromPipeline = ordered_map_1.OrderedRecord.find(inputs, (input) => input.name === i.fromVariable) ||
162
+ ordered_map_1.OrderedRecord.find(processes, (p) => p.name === i.fromVariable);
163
+ if (!inputFromPipeline)
164
+ throw new Error(`Input ${i.fromVariable} not found`);
165
+ return [
166
+ inputOfProcess.id,
167
+ {
168
+ from: 'variable',
169
+ fromVariableId: inputFromPipeline.id,
170
+ fromVariablePropPath: i.fromVariablePropPath,
171
+ },
172
+ ];
173
+ }).filter(is_non_nullable_1.isNonNullable)),
174
+ });
175
+ }
176
+ const outputs = ordered_map_1.OrderedRecord.fromArray(options.outputs.map((output) => {
177
+ const from = ordered_map_1.OrderedRecord.find(inputs, (i) => i.name === output.fromVariable) ||
178
+ ordered_map_1.OrderedRecord.find(processes, (p) => p.name === output.fromVariable);
179
+ if (!from)
180
+ throw new Error(`Output ${output.name} not found in inputs or processes`);
181
+ return {
182
+ id: (0, nanoid_1.nanoid)(),
183
+ name: output.name,
184
+ type: output.type,
185
+ required: output.required,
186
+ from: 'variable',
187
+ fromVariableId: from.id,
188
+ fromVariablePropPath: output.fromVariablePropPath,
189
+ };
190
+ }));
191
+ return {
192
+ id: options.id || options.name || (0, nanoid_1.nanoid)(),
193
+ name: options.name,
194
+ type: 'pipeline_agent',
195
+ inputs,
196
+ outputs,
197
+ processes,
198
+ };
199
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Runnable = void 0;
4
+ exports.isRunnableResponseDelta = isRunnableResponseDelta;
5
+ exports.isRunnableResponseError = isRunnableResponseError;
6
+ const ordered_map_1 = require("./utils/ordered-map");
7
+ class Runnable {
8
+ definition;
9
+ constructor(definition) {
10
+ this.definition = definition;
11
+ this.inputs = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.inputs, (i) => [i.name || i.id, i]));
12
+ this.outputs = Object.fromEntries(ordered_map_1.OrderedRecord.map(definition.outputs, (i) => [i.name || i.id, i]));
13
+ }
14
+ get id() {
15
+ return this.definition.id;
16
+ }
17
+ get name() {
18
+ return this.definition.name;
19
+ }
20
+ inputs;
21
+ outputs;
22
+ }
23
+ exports.Runnable = Runnable;
24
+ function isRunnableResponseDelta(chunk) {
25
+ return '$text' in chunk || 'delta' in chunk;
26
+ }
27
+ function isRunnableResponseError(chunk) {
28
+ return 'error' in chunk;
29
+ }