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