@aigne/core 0.4.205 → 0.4.206

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 (48) hide show
  1. package/lib/cjs/agent.js +27 -1
  2. package/lib/cjs/definitions/data-type-schema.js +2 -2
  3. package/lib/cjs/function-agent.js +34 -29
  4. package/lib/cjs/function-runner.js +6 -4
  5. package/lib/cjs/index.js +1 -1
  6. package/lib/cjs/llm-agent.js +22 -54
  7. package/lib/cjs/llm-decision-agent.js +8 -12
  8. package/lib/cjs/llm-model.js +2 -2
  9. package/lib/cjs/local-function-agent.js +7 -21
  10. package/lib/cjs/pipeline-agent.js +9 -19
  11. package/lib/cjs/runnable.js +1 -0
  12. package/lib/cjs/tsconfig.tsbuildinfo +1 -1
  13. package/lib/cjs/utils/index.js +5 -2
  14. package/lib/cjs/utils/stream-utils.js +35 -13
  15. package/lib/esm/agent.js +29 -3
  16. package/lib/esm/definitions/data-type-schema.js +2 -2
  17. package/lib/esm/function-agent.js +33 -28
  18. package/lib/esm/function-runner.js +6 -4
  19. package/lib/esm/index.js +1 -1
  20. package/lib/esm/llm-agent.js +22 -53
  21. package/lib/esm/llm-decision-agent.js +8 -11
  22. package/lib/esm/llm-model.js +2 -2
  23. package/lib/esm/local-function-agent.js +7 -20
  24. package/lib/esm/pipeline-agent.js +9 -18
  25. package/lib/esm/runnable.js +1 -0
  26. package/lib/esm/tsconfig.tsbuildinfo +1 -1
  27. package/lib/esm/utils/index.js +5 -2
  28. package/lib/esm/utils/stream-utils.js +33 -13
  29. package/lib/types/agent.d.ts +8 -9
  30. package/lib/types/context.d.ts +2 -0
  31. package/lib/types/definitions/data-type-schema.d.ts +7 -5
  32. package/lib/types/{data-type.d.ts → definitions/data-type.d.ts} +2 -2
  33. package/lib/types/function-agent.d.ts +33 -20
  34. package/lib/types/function-runner.d.ts +20 -7
  35. package/lib/types/index.d.ts +1 -1
  36. package/lib/types/llm-agent.d.ts +27 -30
  37. package/lib/types/llm-decision-agent.d.ts +15 -22
  38. package/lib/types/llm-model.d.ts +2 -2
  39. package/lib/types/local-function-agent.d.ts +31 -34
  40. package/lib/types/pipeline-agent.d.ts +21 -55
  41. package/lib/types/runnable.d.ts +1 -1
  42. package/lib/types/tsconfig.tsbuildinfo +1 -1
  43. package/lib/types/utils/index.d.ts +5 -2
  44. package/lib/types/utils/stream-utils.d.ts +5 -3
  45. package/lib/types/utils/union.d.ts +1 -2
  46. package/package.json +3 -2
  47. /package/lib/cjs/{data-type.js → definitions/data-type.js} +0 -0
  48. /package/lib/esm/{data-type.js → definitions/data-type.js} +0 -0
@@ -14,7 +14,10 @@ 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("./ordered-map"), exports);
18
17
  __exportStar(require("./is-non-nullable"), exports);
19
- __exportStar(require("./stream-utils"), exports);
20
18
  __exportStar(require("./mustache-utils"), exports);
19
+ __exportStar(require("./nullable"), exports);
20
+ __exportStar(require("./omit"), exports);
21
+ __exportStar(require("./ordered-map"), exports);
22
+ __exportStar(require("./stream-utils"), exports);
23
+ __exportStar(require("./union"), exports);
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.objectToRunnableResponseStream = objectToRunnableResponseStream;
4
4
  exports.runnableResponseStreamToObject = runnableResponseStreamToObject;
5
5
  exports.extractOutputsFromRunnableOutput = extractOutputsFromRunnableOutput;
6
+ exports.asyncGeneratorToReadableStream = asyncGeneratorToReadableStream;
7
+ exports.isAsyncGenerator = isAsyncGenerator;
6
8
  const runnable_1 = require("../runnable");
7
9
  function objectToRunnableResponseStream(obj) {
8
10
  return new ReadableStream({
@@ -33,24 +35,41 @@ async function runnableResponseStreamToObject(stream) {
33
35
  * @returns The runnable output stream or object
34
36
  */
35
37
  async function extractOutputsFromRunnableOutput(output, resolve) {
36
- if (!(output instanceof ReadableStream)) {
37
- await resolve(output);
38
- return output;
38
+ if (output instanceof ReadableStream || isAsyncGenerator(output)) {
39
+ return new ReadableStream({
40
+ async start(controller) {
41
+ try {
42
+ const result = {};
43
+ let $text = '';
44
+ for await (const value of output) {
45
+ if ((0, runnable_1.isRunnableResponseDelta)(value)) {
46
+ controller.enqueue(value);
47
+ $text += value.$text || '';
48
+ Object.assign(result, value.delta);
49
+ }
50
+ }
51
+ Object.assign(result, { $text: result.$text || $text || undefined });
52
+ await resolve(result);
53
+ }
54
+ catch (error) {
55
+ controller.error(error);
56
+ }
57
+ finally {
58
+ controller.close();
59
+ }
60
+ },
61
+ });
39
62
  }
63
+ await resolve(output);
64
+ return output;
65
+ }
66
+ function asyncGeneratorToReadableStream(generator) {
40
67
  return new ReadableStream({
41
68
  async start(controller) {
42
69
  try {
43
- const result = {};
44
- let $text = '';
45
- for await (const value of output) {
46
- if ((0, runnable_1.isRunnableResponseDelta)(value)) {
47
- $text += value.$text || '';
48
- Object.assign(result, value.delta);
49
- }
70
+ for await (const value of generator) {
71
+ controller.enqueue(value);
50
72
  }
51
- Object.assign(result, { $text: result.$text || $text || undefined });
52
- controller.enqueue({ $text: result.$text, delta: result });
53
- await resolve(result);
54
73
  }
55
74
  catch (error) {
56
75
  controller.error(error);
@@ -61,3 +80,6 @@ async function extractOutputsFromRunnableOutput(output, resolve) {
61
80
  },
62
81
  });
63
82
  }
83
+ function isAsyncGenerator(value) {
84
+ return Symbol.asyncIterator in value;
85
+ }
package/lib/esm/agent.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import logger from './logger';
2
- import { Runnable } from './runnable';
3
- import { OrderedRecord, isNonNullable, renderMessage } from './utils';
2
+ import { Runnable, } from './runnable';
3
+ import { OrderedRecord, extractOutputsFromRunnableOutput, isAsyncGenerator, isNonNullable, objectToRunnableResponseStream, renderMessage, runnableResponseStreamToObject, } from './utils';
4
4
  export class Agent extends Runnable {
5
5
  async getMemoryQuery(input, query) {
6
6
  if (query?.from === 'variable') {
@@ -53,6 +53,32 @@ export class Agent extends Runnable {
53
53
  }
54
54
  async run(input, options) {
55
55
  const memories = await this.loadMemories(input, this.context);
56
- return this.process(input, { ...options, memories });
56
+ const processResult = await this.process(input, { ...options, memories });
57
+ if (options?.stream) {
58
+ const stream = processResult instanceof ReadableStream ||
59
+ isAsyncGenerator(processResult)
60
+ ? processResult
61
+ : objectToRunnableResponseStream(processResult);
62
+ return extractOutputsFromRunnableOutput(stream, async (result) => {
63
+ // TODO: validate result against outputs schema
64
+ await this.onResult(result);
65
+ });
66
+ }
67
+ const result = processResult instanceof ReadableStream
68
+ ? await runnableResponseStreamToObject(processResult)
69
+ : Symbol.asyncIterator in processResult
70
+ ? await runnableResponseStreamToObject(processResult)
71
+ : processResult;
72
+ // TODO: validate result against outputs schema
73
+ await this.onResult(result);
74
+ return result;
75
+ }
76
+ /**
77
+ * Hook that is called before the agent result is returned.
78
+ * @param _result The agent result.
79
+ */
80
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
81
+ async onResult(_result) {
82
+ // Override this method to perform additional operations before the result is returned
57
83
  }
58
84
  }
@@ -27,13 +27,13 @@ export function schemaToDataType(dataType) {
27
27
  return {
28
28
  ...base,
29
29
  type: 'object',
30
- properties: schemaToDataType(schema.properties),
30
+ properties: schema.properties && schemaToDataType(schema.properties),
31
31
  };
32
32
  case 'array':
33
33
  return {
34
34
  ...base,
35
35
  type: 'array',
36
- items: OrderedRecord.find(schemaToDataType({ items: schema.items }), (i) => i.name === 'items'),
36
+ items: schema.items && OrderedRecord.find(schemaToDataType({ items: schema.items }), (i) => i.name === 'items'),
37
37
  };
38
38
  default: {
39
39
  throw new Error(`Unknown data type: ${schema.type}`);
@@ -10,57 +10,62 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  var __param = (this && this.__param) || function (paramIndex, decorator) {
11
11
  return function (target, key) { decorator(target, key, paramIndex); }
12
12
  };
13
- var FunctionAgent_1;
14
13
  import { nanoid } from 'nanoid';
15
14
  import { inject, injectable } from 'tsyringe';
15
+ import { Agent } from './agent';
16
16
  import { TYPES } from './constants';
17
17
  import { schemaToDataType } from './definitions/data-type-schema';
18
+ import { toRunnableMemories } from './definitions/memory';
18
19
  import { FunctionRunner } from './function-runner';
19
- import { Runnable } from './runnable';
20
- import { objectToRunnableResponseStream } from './utils';
21
- let FunctionAgent = FunctionAgent_1 = class FunctionAgent extends Runnable {
20
+ let FunctionAgent = class FunctionAgent extends Agent {
22
21
  definition;
23
22
  runner;
24
- static create(options) {
25
- const definition = createFunctionAgentDefinition(options);
26
- return new FunctionAgent_1(definition);
27
- }
28
- constructor(definition, runner) {
29
- super(definition);
23
+ static create = create;
24
+ constructor(definition, context, runner) {
25
+ super(definition, context);
30
26
  this.definition = definition;
31
27
  this.runner = runner;
28
+ this.runner ??= context?.resolveDependency(TYPES.functionRunner);
32
29
  }
33
- async run(input, options) {
34
- const { definition: { language, code, ...definition }, runner, } = this;
30
+ async process(input, options) {
31
+ const { definition: { language, code, ...definition }, runner, context, } = this;
35
32
  if (!runner)
36
33
  throw new Error('Function runner is required');
37
- if (!language || !code)
38
- throw new Error('Language and code are required');
39
- const result = await runner.run({
34
+ if (!code)
35
+ throw new Error('Code is required');
36
+ if (!context)
37
+ throw new Error('Context is required');
38
+ return await runner.run({
40
39
  name: definition.name || definition.id,
41
40
  language,
42
41
  code,
43
- arguments: input,
44
- });
45
- // TODO: validate the result against the definition.outputs
46
- return options?.stream ? objectToRunnableResponseStream(result) : result;
42
+ input,
43
+ memories: options.memories,
44
+ context: { state: context.state },
45
+ }, { stream: true });
47
46
  }
48
47
  };
49
- FunctionAgent = FunctionAgent_1 = __decorate([
48
+ FunctionAgent = __decorate([
50
49
  injectable(),
51
50
  __param(0, inject(TYPES.definition)),
52
- __param(1, inject(TYPES.functionRunner)),
53
- __metadata("design:paramtypes", [Object, FunctionRunner])
51
+ __param(1, inject(TYPES.context)),
52
+ __param(2, inject(TYPES.functionRunner)),
53
+ __metadata("design:paramtypes", [Object, Object, FunctionRunner])
54
54
  ], FunctionAgent);
55
55
  export { FunctionAgent };
56
- export function createFunctionAgentDefinition(options) {
57
- return {
58
- id: options.id || options.name || nanoid(),
56
+ export function create({ context, ...options }) {
57
+ const agentId = options.name || nanoid();
58
+ const inputs = schemaToDataType(options.inputs);
59
+ const outputs = schemaToDataType(options.outputs);
60
+ const memories = toRunnableMemories(agentId, inputs, options.memories ?? {});
61
+ return new FunctionAgent({
62
+ id: agentId,
59
63
  name: options.name,
60
64
  type: 'function_agent',
61
- inputs: schemaToDataType(options.inputs),
62
- outputs: schemaToDataType(options.outputs),
65
+ inputs,
66
+ outputs,
67
+ memories,
63
68
  language: options.language,
64
69
  code: options.code,
65
- };
70
+ }, context);
66
71
  }
@@ -1,6 +1,6 @@
1
- import { Runnable } from './runnable';
1
+ import { Agent } from './agent';
2
2
  import { OrderedRecord } from './utils';
3
- export class FunctionRunner extends Runnable {
3
+ export class FunctionRunner extends Agent {
4
4
  constructor(context) {
5
5
  super({
6
6
  id: 'function_runner',
@@ -9,9 +9,11 @@ export class FunctionRunner extends Runnable {
9
9
  description: 'Run a function',
10
10
  inputs: OrderedRecord.fromArray([
11
11
  { id: 'name', name: 'name', type: 'string', required: true },
12
- { id: 'language', name: 'language', type: 'string', required: true },
12
+ { id: 'language', name: 'language', type: 'string' },
13
13
  { id: 'code', name: 'code', type: 'string', required: true },
14
- { id: 'arguments', name: 'arguments', type: 'object', required: false },
14
+ { id: 'input', name: 'input', type: 'object', required: true },
15
+ { id: 'memories', name: 'memories', type: 'object', required: true },
16
+ { id: 'context', name: 'context', type: 'object', required: true },
15
17
  ]),
16
18
  outputs: OrderedRecord.fromArray([
17
19
  {
package/lib/esm/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './utils';
2
2
  export * from './constants';
3
- export * from './data-type';
3
+ export * from './definitions/data-type';
4
4
  export * from './definitions/data-type-schema';
5
5
  export * from './context';
6
6
  export * from './runnable';
@@ -10,7 +10,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  var __param = (this && this.__param) || function (paramIndex, decorator) {
11
11
  return function (target, key) { decorator(target, key, paramIndex); }
12
12
  };
13
- var LLMAgent_1;
14
13
  import { nanoid } from 'nanoid';
15
14
  import { inject, injectable } from 'tsyringe';
16
15
  import { Agent } from './agent';
@@ -18,24 +17,21 @@ import { StreamTextOutputName, TYPES } from './constants';
18
17
  import { schemaToDataType } from './definitions/data-type-schema';
19
18
  import { toRunnableMemories } from './definitions/memory';
20
19
  import { LLMModel } from './llm-model';
21
- import { runnableResponseStreamToObject } from './utils';
22
20
  import { prepareMessages } from './utils/message-utils';
23
21
  import { renderMessage } from './utils/mustache-utils';
24
22
  import { OrderedRecord } from './utils/ordered-map';
25
23
  import { outputsToJsonSchema } from './utils/structured-output-schema';
26
- let LLMAgent = LLMAgent_1 = class LLMAgent extends Agent {
24
+ let LLMAgent = class LLMAgent extends Agent {
27
25
  definition;
28
26
  model;
29
- static create(options) {
30
- const definition = createLLMAgentDefinition(options);
31
- return new LLMAgent_1(definition);
32
- }
27
+ static create = create;
33
28
  constructor(definition, context, model) {
34
29
  super(definition, context);
35
30
  this.definition = definition;
36
31
  this.model = model;
32
+ this.model ??= context?.resolveDependency(TYPES.llmModel);
37
33
  }
38
- async process(input, options) {
34
+ async *process(input, options) {
39
35
  const { definition, model } = this;
40
36
  if (!model)
41
37
  throw new Error('LLM model is required');
@@ -44,46 +40,21 @@ let LLMAgent = LLMAgent_1 = class LLMAgent extends Agent {
44
40
  messages: messagesWithMemory,
45
41
  modelOptions: definition.modelOptions,
46
42
  };
47
- const jsonOutput = this.runWithStructuredOutput(llmInputs);
48
- const textOutput = OrderedRecord.find(definition.outputs, (i) => i.name === StreamTextOutputName)
49
- ? await this.runWithTextOutput(llmInputs)
50
- : undefined;
51
- const updateMemories = (text, json) => {
52
- return this.updateMemories([
53
- ...originalMessages,
54
- { role: 'assistant', content: renderMessage('{{text}}\n{{json}}', { text, json }).trim() },
55
- ]);
56
- };
57
- if (options?.stream) {
58
- let $text = '';
59
- return new ReadableStream({
60
- start: async (controller) => {
61
- try {
62
- if (textOutput) {
63
- for await (const chunk of textOutput) {
64
- $text += chunk.$text || '';
65
- controller.enqueue({ $text: chunk.$text });
66
- }
67
- }
68
- const json = await jsonOutput;
69
- controller.enqueue({ delta: json });
70
- await updateMemories($text || undefined, json);
71
- }
72
- catch (error) {
73
- controller.error(error);
74
- }
75
- finally {
76
- controller.close();
77
- }
78
- },
79
- });
43
+ let $text = '';
44
+ const hasTextOutput = OrderedRecord.find(definition.outputs, (i) => i.name === StreamTextOutputName);
45
+ if (hasTextOutput) {
46
+ for await (const chunk of await this.runWithTextOutput(llmInputs)) {
47
+ $text += chunk.$text || '';
48
+ yield { $text: chunk.$text };
49
+ }
80
50
  }
81
- const [$text, json] = await Promise.all([
82
- textOutput ? runnableResponseStreamToObject(textOutput).then((res) => res.$text || undefined) : undefined,
83
- jsonOutput,
51
+ const json = await this.runWithStructuredOutput(llmInputs);
52
+ if (json)
53
+ yield { delta: json };
54
+ await this.updateMemories([
55
+ ...originalMessages,
56
+ { role: 'assistant', content: renderMessage('{{$text}}\n{{json}}', { $text, json }).trim() },
84
57
  ]);
85
- await updateMemories($text, json);
86
- return { $text, ...json };
87
58
  }
88
59
  async runWithStructuredOutput(llmInputs) {
89
60
  const jsonOutputs = OrderedRecord.filter(this.definition.outputs, (i) => i.name !== StreamTextOutputName // ignore `$text` output
@@ -107,9 +78,7 @@ let LLMAgent = LLMAgent_1 = class LLMAgent extends Agent {
107
78
  });
108
79
  if (!response.$text)
109
80
  throw new Error('No text in JSON mode response');
110
- const json = JSON.parse(response.$text);
111
- // TODO: validate json with outputJsonSchema
112
- return json;
81
+ return JSON.parse(response.$text);
113
82
  }
114
83
  async runWithTextOutput(llmInputs) {
115
84
  const { model } = this;
@@ -118,7 +87,7 @@ let LLMAgent = LLMAgent_1 = class LLMAgent extends Agent {
118
87
  return model.run(llmInputs, { stream: true });
119
88
  }
120
89
  };
121
- LLMAgent = LLMAgent_1 = __decorate([
90
+ LLMAgent = __decorate([
122
91
  injectable(),
123
92
  __param(0, inject(TYPES.definition)),
124
93
  __param(1, inject(TYPES.context)),
@@ -131,7 +100,7 @@ export { LLMAgent };
131
100
  * @param options Options to create LLMAgent.
132
101
  * @returns LLMAgent definition.
133
102
  */
134
- export function createLLMAgentDefinition(options) {
103
+ function create({ context, ...options }) {
135
104
  const agentId = options.name || nanoid();
136
105
  const inputs = schemaToDataType(options.inputs);
137
106
  const outputs = schemaToDataType(options.outputs);
@@ -147,7 +116,7 @@ export function createLLMAgentDefinition(options) {
147
116
  role: i.role,
148
117
  content: i.content,
149
118
  })));
150
- return {
119
+ return new LLMAgent({
151
120
  id: agentId,
152
121
  name: options.name,
153
122
  type: 'llm_agent',
@@ -157,5 +126,5 @@ export function createLLMAgentDefinition(options) {
157
126
  memories,
158
127
  modelOptions: options.modelOptions,
159
128
  messages,
160
- };
129
+ }, context);
161
130
  }
@@ -10,7 +10,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  var __param = (this && this.__param) || function (paramIndex, decorator) {
11
11
  return function (target, key) { decorator(target, key, paramIndex); }
12
12
  };
13
- var LLMDecisionAgent_1;
14
13
  import { nanoid } from 'nanoid';
15
14
  import { inject, injectable } from 'tsyringe';
16
15
  import { Agent } from './agent';
@@ -19,17 +18,15 @@ import { toRunnableMemories } from './definitions/memory';
19
18
  import { LLMModel } from './llm-model';
20
19
  import { OrderedRecord, extractOutputsFromRunnableOutput, renderMessage } from './utils';
21
20
  import { prepareMessages } from './utils/message-utils';
22
- let LLMDecisionAgent = LLMDecisionAgent_1 = class LLMDecisionAgent extends Agent {
21
+ let LLMDecisionAgent = class LLMDecisionAgent extends Agent {
23
22
  definition;
24
23
  model;
25
- static create(options) {
26
- const definition = createLLMDecisionAgentDefinition(options);
27
- return new LLMDecisionAgent_1(definition);
28
- }
24
+ static create = create;
29
25
  constructor(definition, context, model) {
30
26
  super(definition, context);
31
27
  this.definition = definition;
32
28
  this.model = model;
29
+ this.model ??= context?.resolveDependency(TYPES.llmModel);
33
30
  }
34
31
  async process(input, options) {
35
32
  const { definition, context, model } = this;
@@ -73,14 +70,14 @@ let LLMDecisionAgent = LLMDecisionAgent_1 = class LLMDecisionAgent extends Agent
73
70
  if (!caseToCall)
74
71
  throw new Error('Case not found');
75
72
  // TODO: check result structure and omit undefined values
76
- const output = (await caseToCall.runnable.run(input, options));
73
+ const output = await caseToCall.runnable.run(input, { stream: true });
77
74
  return extractOutputsFromRunnableOutput(output, ({ $text, ...json }) => this.updateMemories([
78
75
  ...originalMessages,
79
76
  { role: 'assistant', content: renderMessage('{{$text}}\n{{json}}', { $text, json }).trim() },
80
77
  ]));
81
78
  }
82
79
  };
83
- LLMDecisionAgent = LLMDecisionAgent_1 = __decorate([
80
+ LLMDecisionAgent = __decorate([
84
81
  injectable(),
85
82
  __param(0, inject(TYPES.definition)),
86
83
  __param(1, inject(TYPES.context)),
@@ -88,7 +85,7 @@ LLMDecisionAgent = LLMDecisionAgent_1 = __decorate([
88
85
  __metadata("design:paramtypes", [Object, Object, LLMModel])
89
86
  ], LLMDecisionAgent);
90
87
  export { LLMDecisionAgent };
91
- export function createLLMDecisionAgentDefinition(options) {
88
+ function create({ context, ...options }) {
92
89
  const agentId = options.name || nanoid();
93
90
  const cases = OrderedRecord.fromArray(Object.entries(options.cases).map(([name, c]) => ({
94
91
  id: nanoid(),
@@ -110,7 +107,7 @@ export function createLLMDecisionAgentDefinition(options) {
110
107
  role: i.role,
111
108
  content: i.content,
112
109
  })));
113
- return {
110
+ return new LLMDecisionAgent({
114
111
  id: agentId,
115
112
  name: options.name,
116
113
  type: 'llm_decision_agent',
@@ -121,5 +118,5 @@ export function createLLMDecisionAgentDefinition(options) {
121
118
  memories,
122
119
  modelOptions: options.modelOptions,
123
120
  cases,
124
- };
121
+ }, context);
125
122
  }
@@ -1,6 +1,6 @@
1
- import { Runnable } from './runnable';
1
+ import { Agent } from './agent';
2
2
  import { OrderedRecord } from './utils';
3
- export class LLMModel extends Runnable {
3
+ export class LLMModel extends Agent {
4
4
  constructor(context) {
5
5
  super({
6
6
  id: 'llm_model',
@@ -10,20 +10,15 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  var __param = (this && this.__param) || function (paramIndex, decorator) {
11
11
  return function (target, key) { decorator(target, key, paramIndex); }
12
12
  };
13
- var LocalFunctionAgent_1;
14
13
  import { nanoid } from 'nanoid';
15
14
  import { inject, injectable } from 'tsyringe';
16
15
  import { Agent } from './agent';
17
16
  import { TYPES } from './constants';
18
17
  import { schemaToDataType } from './definitions/data-type-schema';
19
18
  import { toRunnableMemories } from './definitions/memory';
20
- import { objectToRunnableResponseStream, runnableResponseStreamToObject } from './utils';
21
- let LocalFunctionAgent = LocalFunctionAgent_1 = class LocalFunctionAgent extends Agent {
19
+ let LocalFunctionAgent = class LocalFunctionAgent extends Agent {
22
20
  definition;
23
- static create(options) {
24
- const definition = createLocalFunctionAgentDefinition(options);
25
- return new LocalFunctionAgent_1(definition);
26
- }
21
+ static create = create;
27
22
  constructor(definition, context) {
28
23
  super(definition, context);
29
24
  this.definition = definition;
@@ -34,30 +29,22 @@ let LocalFunctionAgent = LocalFunctionAgent_1 = class LocalFunctionAgent extends
34
29
  throw new Error('Function is required');
35
30
  if (!context)
36
31
  throw new Error('Context is required');
37
- const result = await func(input, { context, memories: options.memories });
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;
32
+ return await func(input, { context, memories: options.memories });
46
33
  }
47
34
  };
48
- LocalFunctionAgent = LocalFunctionAgent_1 = __decorate([
35
+ LocalFunctionAgent = __decorate([
49
36
  injectable(),
50
37
  __param(0, inject(TYPES.definition)),
51
38
  __param(1, inject(TYPES.context)),
52
39
  __metadata("design:paramtypes", [Object, Object])
53
40
  ], LocalFunctionAgent);
54
41
  export { LocalFunctionAgent };
55
- export function createLocalFunctionAgentDefinition(options) {
42
+ function create({ context, ...options }) {
56
43
  const agentId = options.name || nanoid();
57
44
  const inputs = schemaToDataType(options.inputs);
58
45
  const outputs = schemaToDataType(options.outputs);
59
46
  const memories = toRunnableMemories(agentId, inputs, options.memories || {});
60
- return {
47
+ return new LocalFunctionAgent({
61
48
  id: agentId,
62
49
  name: options.name,
63
50
  type: 'local_function_agent',
@@ -65,5 +52,5 @@ export function createLocalFunctionAgentDefinition(options) {
65
52
  outputs,
66
53
  memories,
67
54
  function: options.function,
68
- };
55
+ }, context);
69
56
  }
@@ -10,7 +10,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  var __param = (this && this.__param) || function (paramIndex, decorator) {
11
11
  return function (target, key) { decorator(target, key, paramIndex); }
12
12
  };
13
- var PipelineAgent_1;
14
13
  import { get, isNil } from 'lodash';
15
14
  import { nanoid } from 'nanoid';
16
15
  import { inject, injectable } from 'tsyringe';
@@ -19,21 +18,16 @@ import { StreamTextOutputName, TYPES } from './constants';
19
18
  import { schemaToDataType } from './definitions/data-type-schema';
20
19
  import { toRunnableMemories } from './definitions/memory';
21
20
  import logger from './logger';
22
- import { runnableResponseStreamToObject } from './utils';
23
21
  import { isNonNullable } from './utils/is-non-nullable';
24
22
  import { OrderedRecord } from './utils/ordered-map';
25
- let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends Agent {
23
+ let PipelineAgent = class PipelineAgent extends Agent {
26
24
  definition;
27
- static create(options) {
28
- const definition = createPipelineAgentDefinition(options);
29
- return new PipelineAgent_1(definition);
30
- }
25
+ static create = create;
31
26
  constructor(definition, context) {
32
27
  super(definition, context);
33
28
  this.definition = definition;
34
29
  }
35
30
  async process(input, options) {
36
- // TODO: validate the input against the definition
37
31
  const { definition, context } = this;
38
32
  if (!context)
39
33
  throw new Error('Context is required');
@@ -41,7 +35,7 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends Agent {
41
35
  if (!processes?.$indexes.length) {
42
36
  throw new Error('No processes defined');
43
37
  }
44
- const result = new ReadableStream({
38
+ return new ReadableStream({
45
39
  async start(controller) {
46
40
  try {
47
41
  // NOTE: 将 input 转换为 variables,其中 key 为 inputId,value 为 input 的值
@@ -102,6 +96,8 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends Agent {
102
96
  result = Object.fromEntries(OrderedRecord.map(definition.outputs, (output) => {
103
97
  if (!output.name)
104
98
  return null;
99
+ if (output.name === StreamTextOutputName)
100
+ return null;
105
101
  let value;
106
102
  if (output.from === 'variable') {
107
103
  const v = variables[output.fromVariableId];
@@ -126,21 +122,16 @@ let PipelineAgent = PipelineAgent_1 = class PipelineAgent extends Agent {
126
122
  }
127
123
  },
128
124
  });
129
- if (options?.stream) {
130
- return result;
131
- }
132
- // TODO: validate the result against the definition.outputs
133
- return await runnableResponseStreamToObject(result);
134
125
  }
135
126
  };
136
- PipelineAgent = PipelineAgent_1 = __decorate([
127
+ PipelineAgent = __decorate([
137
128
  injectable(),
138
129
  __param(0, inject(TYPES.definition)),
139
130
  __param(1, inject(TYPES.context)),
140
131
  __metadata("design:paramtypes", [Object, Object])
141
132
  ], PipelineAgent);
142
133
  export { PipelineAgent };
143
- export function createPipelineAgentDefinition(options) {
134
+ function create({ context, ...options }) {
144
135
  const agentId = options.name || nanoid();
145
136
  const inputs = schemaToDataType(options.inputs);
146
137
  const memories = toRunnableMemories(agentId, inputs, options.memories || {});
@@ -182,7 +173,7 @@ export function createPipelineAgentDefinition(options) {
182
173
  throw new Error(`Output ${output.name} not found in inputs or processes`);
183
174
  return { ...output, from: 'variable', fromVariableId: from.id, fromVariablePropPath };
184
175
  }));
185
- return {
176
+ return new PipelineAgent({
186
177
  id: agentId,
187
178
  name: options.name,
188
179
  type: 'pipeline_agent',
@@ -190,5 +181,5 @@ export function createPipelineAgentDefinition(options) {
190
181
  memories,
191
182
  outputs,
192
183
  processes,
193
- };
184
+ }, context);
194
185
  }
@@ -5,6 +5,7 @@ export class Runnable {
5
5
  constructor(definition, context) {
6
6
  this.definition = definition;
7
7
  this.context = context;
8
+ context?.register(this);
8
9
  this.inputs = Object.fromEntries(OrderedRecord.map(definition.inputs, (i) => [i.name || i.id, i]));
9
10
  this.outputs = Object.fromEntries(OrderedRecord.map(definition.outputs, (i) => [i.name || i.id, i]));
10
11
  }