@aigne/core 1.0.11 → 1.0.12-beta.10

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.
@@ -32,6 +32,7 @@ export declare abstract class Agent<I extends RunnableInput = RunnableInput, O e
32
32
  run(input: I, options?: RunOptions & {
33
33
  stream?: false;
34
34
  }): Promise<O>;
35
+ private addHistory;
35
36
  /**
36
37
  * Hook that is called before the agent result is returned.
37
38
  * @param _result The agent result.
package/lib/cjs/agent.js CHANGED
@@ -118,6 +118,7 @@ class Agent extends runnable_1.Runnable {
118
118
  logger_1.logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
119
119
  result,
120
120
  });
121
+ await this.addHistory({ input, output: result });
121
122
  await this.onResult(result);
122
123
  });
123
124
  }
@@ -130,9 +131,17 @@ class Agent extends runnable_1.Runnable {
130
131
  result,
131
132
  });
132
133
  // TODO: validate result against outputs schema
134
+ await this.addHistory({ input, output: result });
133
135
  await this.onResult(result);
134
136
  return result;
135
137
  }
138
+ async addHistory({ input, output }) {
139
+ await this.context?.historyManager?.create({ input, output }, {
140
+ userId: this.context?.state.userId,
141
+ sessionId: this.context?.state.sessionId,
142
+ agentId: this.id,
143
+ });
144
+ }
136
145
  /**
137
146
  * Hook that is called before the agent result is returned.
138
147
  * @param _result The agent result.
@@ -1,4 +1,5 @@
1
1
  import type { LLMModelConfiguration } from "./llm-model";
2
+ import type { Memorable } from "./memorable";
2
3
  import type { Runnable, RunnableDefinition } from "./runnable";
3
4
  export interface ContextState {
4
5
  userId?: string;
@@ -9,8 +10,13 @@ export interface ContextConfig {
9
10
  llmModel?: LLMModelConfiguration;
10
11
  }
11
12
  export interface Context<State extends ContextState = ContextState, Config extends ContextConfig = ContextConfig> {
13
+ id: string;
12
14
  state: State;
13
15
  config: Config;
16
+ historyManager?: Memorable<{
17
+ input: object;
18
+ output: object;
19
+ }>;
14
20
  resolve<T extends Runnable>(id: string | RunnableDefinition | T): Promise<T>;
15
21
  register<R extends Array<RunnableDefinition | Runnable> = []>(...definition: R): void;
16
22
  resolveDependency<T>(token: string | symbol): T;
@@ -1,3 +1,4 @@
1
+ import { Agent } from "./agent";
1
2
  import type { Context } from "./context";
2
3
  import type { LLMModelInputMessage } from "./llm-model";
3
4
  import { Runnable } from "./runnable";
@@ -25,6 +26,7 @@ export interface MemoryItem<T> {
25
26
  id: string;
26
27
  userId?: string;
27
28
  sessionId?: string;
29
+ agentId?: string;
28
30
  createdAt: string;
29
31
  updatedAt: string;
30
32
  memory: T;
@@ -41,6 +43,7 @@ export type MemoryActions<T> = {
41
43
  options?: {
42
44
  userId?: string;
43
45
  sessionId?: string;
46
+ agentId?: string;
44
47
  metadata?: MemoryMetadata;
45
48
  };
46
49
  };
@@ -55,6 +58,7 @@ export type MemoryActions<T> = {
55
58
  k?: number;
56
59
  userId?: string;
57
60
  sessionId?: string;
61
+ agentId?: string;
58
62
  filter?: MemoryMetadata;
59
63
  sort?: MemorySortOptions;
60
64
  };
@@ -69,6 +73,7 @@ export type MemoryActions<T> = {
69
73
  k?: number;
70
74
  userId?: string;
71
75
  sessionId?: string;
76
+ agentId?: string;
72
77
  filter?: MemoryMetadata;
73
78
  sort?: MemorySortOptions;
74
79
  };
@@ -91,6 +96,7 @@ export type MemoryActions<T> = {
91
96
  options?: {
92
97
  userId?: string;
93
98
  sessionId?: string;
99
+ agentId?: string;
94
100
  metadata?: MemoryMetadata;
95
101
  };
96
102
  };
@@ -128,7 +134,7 @@ export interface SortItem {
128
134
  direction: "asc" | "desc";
129
135
  }
130
136
  export type MemorySortOptions = SortItem | SortItem[];
131
- export declare abstract class Memorable<T, C = undefined> extends Runnable<MemoryActions<T>, MemoryActions<T>["outputs"]> {
137
+ export declare abstract class Memorable<T, C extends Record<string, any> = Record<string, any>> extends Runnable<MemoryActions<T>, MemoryActions<T>["outputs"]> {
132
138
  constructor(context?: Context);
133
139
  abstract runner?: MemoryRunner<T, C>;
134
140
  abstract add(messages: Extract<MemoryActions<T>, {
@@ -174,10 +180,11 @@ export declare abstract class Memorable<T, C = undefined> extends Runnable<Memor
174
180
  }>["outputs"]>;
175
181
  abstract reset(): Promise<void>;
176
182
  }
177
- export type MemoryRunnerInput<C = undefined> = {
183
+ export type MemoryRunnerInput<C extends Record<string, any> = Record<string, any>> = {
178
184
  messages: MemoryMessage[];
179
185
  userId?: string;
180
186
  sessionId?: string;
187
+ agentId?: string;
181
188
  metadata?: MemoryMetadata;
182
189
  filter?: MemoryMetadata;
183
190
  customData: C;
@@ -185,7 +192,7 @@ export type MemoryRunnerInput<C = undefined> = {
185
192
  export type MemoryRunnerOutput<T> = {
186
193
  actions: MemoryActionItem<T>[];
187
194
  };
188
- export declare abstract class MemoryRunner<T, C = undefined> extends Runnable<MemoryRunnerInput<C>, MemoryRunnerOutput<T>> {
195
+ export declare abstract class MemoryRunner<T, C extends Record<string, any> = Record<string, any>> extends Agent<MemoryRunnerInput<C>, MemoryRunnerOutput<T>> {
189
196
  constructor(name: string);
190
197
  }
191
198
  export type MemorableSearchOutput<T extends Memorable<unknown>> = Awaited<ReturnType<T["search"]>>["results"];
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MemoryRunner = exports.Memorable = void 0;
4
4
  const lodash_1 = require("lodash");
5
+ const agent_1 = require("./agent");
5
6
  const runnable_1 = require("./runnable");
6
7
  const utils_1 = require("./utils");
7
8
  class Memorable extends runnable_1.Runnable {
@@ -16,7 +17,7 @@ class Memorable extends runnable_1.Runnable {
16
17
  }
17
18
  }
18
19
  exports.Memorable = Memorable;
19
- class MemoryRunner extends runnable_1.Runnable {
20
+ class MemoryRunner extends agent_1.Agent {
20
21
  constructor(name) {
21
22
  const id = `${(0, lodash_1.camelCase)(name)}_runner`;
22
23
  super({
@@ -2,6 +2,7 @@ import { type DependencyContainer } from "tsyringe";
2
2
  import type { constructor as Constructor } from "tsyringe/dist/typings/types";
3
3
  import type { Context, ContextState } from "./context";
4
4
  import type { LLMModel, LLMModelConfiguration } from "./llm-model";
5
+ import type { Memorable } from "./memorable";
5
6
  import { Runnable, type RunnableDefinition } from "./runnable";
6
7
  import type { SandboxFunctionRunner } from "./sandbox-function-runner";
7
8
  import { OrderedRecord } from "./utils/ordered-map";
@@ -17,6 +18,10 @@ export interface RuntimeOptions<Agents extends {
17
18
  config?: RuntimeConfiguration;
18
19
  state?: State;
19
20
  agents?: Agents;
21
+ historyManager?: Memorable<{
22
+ input: object;
23
+ output: object;
24
+ }>;
20
25
  llmModel?: LLMModel | Constructor<LLMModel>;
21
26
  sandboxFunctionRunner?: SandboxFunctionRunner | Constructor<SandboxFunctionRunner>;
22
27
  }
@@ -28,6 +33,10 @@ export declare class Runtime<Agents extends {
28
33
  get options(): RuntimeOptions<Agents, State>;
29
34
  get id(): string;
30
35
  get name(): string | undefined;
36
+ get historyManager(): Memorable<{
37
+ input: object;
38
+ output: object;
39
+ }, Record<string, any>> | undefined;
31
40
  config: RuntimeConfiguration;
32
41
  state: State;
33
42
  agents: Agents;
@@ -47,6 +47,9 @@ let Runtime = Runtime_1 = class Runtime {
47
47
  get name() {
48
48
  return this.inner.name;
49
49
  }
50
+ get historyManager() {
51
+ return this.inner.options.historyManager;
52
+ }
50
53
  config;
51
54
  state;
52
55
  agents = new Proxy({}, { get: (_, prop) => this.resolveSync(prop.toString()) });
@@ -32,6 +32,7 @@ export declare abstract class Agent<I extends RunnableInput = RunnableInput, O e
32
32
  run(input: I, options?: RunOptions & {
33
33
  stream?: false;
34
34
  }): Promise<O>;
35
+ private addHistory;
35
36
  /**
36
37
  * Hook that is called before the agent result is returned.
37
38
  * @param _result The agent result.
@@ -1,4 +1,5 @@
1
1
  import type { LLMModelConfiguration } from "./llm-model";
2
+ import type { Memorable } from "./memorable";
2
3
  import type { Runnable, RunnableDefinition } from "./runnable";
3
4
  export interface ContextState {
4
5
  userId?: string;
@@ -9,8 +10,13 @@ export interface ContextConfig {
9
10
  llmModel?: LLMModelConfiguration;
10
11
  }
11
12
  export interface Context<State extends ContextState = ContextState, Config extends ContextConfig = ContextConfig> {
13
+ id: string;
12
14
  state: State;
13
15
  config: Config;
16
+ historyManager?: Memorable<{
17
+ input: object;
18
+ output: object;
19
+ }>;
14
20
  resolve<T extends Runnable>(id: string | RunnableDefinition | T): Promise<T>;
15
21
  register<R extends Array<RunnableDefinition | Runnable> = []>(...definition: R): void;
16
22
  resolveDependency<T>(token: string | symbol): T;
@@ -1,3 +1,4 @@
1
+ import { Agent } from "./agent";
1
2
  import type { Context } from "./context";
2
3
  import type { LLMModelInputMessage } from "./llm-model";
3
4
  import { Runnable } from "./runnable";
@@ -25,6 +26,7 @@ export interface MemoryItem<T> {
25
26
  id: string;
26
27
  userId?: string;
27
28
  sessionId?: string;
29
+ agentId?: string;
28
30
  createdAt: string;
29
31
  updatedAt: string;
30
32
  memory: T;
@@ -41,6 +43,7 @@ export type MemoryActions<T> = {
41
43
  options?: {
42
44
  userId?: string;
43
45
  sessionId?: string;
46
+ agentId?: string;
44
47
  metadata?: MemoryMetadata;
45
48
  };
46
49
  };
@@ -55,6 +58,7 @@ export type MemoryActions<T> = {
55
58
  k?: number;
56
59
  userId?: string;
57
60
  sessionId?: string;
61
+ agentId?: string;
58
62
  filter?: MemoryMetadata;
59
63
  sort?: MemorySortOptions;
60
64
  };
@@ -69,6 +73,7 @@ export type MemoryActions<T> = {
69
73
  k?: number;
70
74
  userId?: string;
71
75
  sessionId?: string;
76
+ agentId?: string;
72
77
  filter?: MemoryMetadata;
73
78
  sort?: MemorySortOptions;
74
79
  };
@@ -91,6 +96,7 @@ export type MemoryActions<T> = {
91
96
  options?: {
92
97
  userId?: string;
93
98
  sessionId?: string;
99
+ agentId?: string;
94
100
  metadata?: MemoryMetadata;
95
101
  };
96
102
  };
@@ -128,7 +134,7 @@ export interface SortItem {
128
134
  direction: "asc" | "desc";
129
135
  }
130
136
  export type MemorySortOptions = SortItem | SortItem[];
131
- export declare abstract class Memorable<T, C = undefined> extends Runnable<MemoryActions<T>, MemoryActions<T>["outputs"]> {
137
+ export declare abstract class Memorable<T, C extends Record<string, any> = Record<string, any>> extends Runnable<MemoryActions<T>, MemoryActions<T>["outputs"]> {
132
138
  constructor(context?: Context);
133
139
  abstract runner?: MemoryRunner<T, C>;
134
140
  abstract add(messages: Extract<MemoryActions<T>, {
@@ -174,10 +180,11 @@ export declare abstract class Memorable<T, C = undefined> extends Runnable<Memor
174
180
  }>["outputs"]>;
175
181
  abstract reset(): Promise<void>;
176
182
  }
177
- export type MemoryRunnerInput<C = undefined> = {
183
+ export type MemoryRunnerInput<C extends Record<string, any> = Record<string, any>> = {
178
184
  messages: MemoryMessage[];
179
185
  userId?: string;
180
186
  sessionId?: string;
187
+ agentId?: string;
181
188
  metadata?: MemoryMetadata;
182
189
  filter?: MemoryMetadata;
183
190
  customData: C;
@@ -185,7 +192,7 @@ export type MemoryRunnerInput<C = undefined> = {
185
192
  export type MemoryRunnerOutput<T> = {
186
193
  actions: MemoryActionItem<T>[];
187
194
  };
188
- export declare abstract class MemoryRunner<T, C = undefined> extends Runnable<MemoryRunnerInput<C>, MemoryRunnerOutput<T>> {
195
+ export declare abstract class MemoryRunner<T, C extends Record<string, any> = Record<string, any>> extends Agent<MemoryRunnerInput<C>, MemoryRunnerOutput<T>> {
189
196
  constructor(name: string);
190
197
  }
191
198
  export type MemorableSearchOutput<T extends Memorable<unknown>> = Awaited<ReturnType<T["search"]>>["results"];
@@ -2,6 +2,7 @@ import { type DependencyContainer } from "tsyringe";
2
2
  import type { constructor as Constructor } from "tsyringe/dist/typings/types";
3
3
  import type { Context, ContextState } from "./context";
4
4
  import type { LLMModel, LLMModelConfiguration } from "./llm-model";
5
+ import type { Memorable } from "./memorable";
5
6
  import { Runnable, type RunnableDefinition } from "./runnable";
6
7
  import type { SandboxFunctionRunner } from "./sandbox-function-runner";
7
8
  import { OrderedRecord } from "./utils/ordered-map";
@@ -17,6 +18,10 @@ export interface RuntimeOptions<Agents extends {
17
18
  config?: RuntimeConfiguration;
18
19
  state?: State;
19
20
  agents?: Agents;
21
+ historyManager?: Memorable<{
22
+ input: object;
23
+ output: object;
24
+ }>;
20
25
  llmModel?: LLMModel | Constructor<LLMModel>;
21
26
  sandboxFunctionRunner?: SandboxFunctionRunner | Constructor<SandboxFunctionRunner>;
22
27
  }
@@ -28,6 +33,10 @@ export declare class Runtime<Agents extends {
28
33
  get options(): RuntimeOptions<Agents, State>;
29
34
  get id(): string;
30
35
  get name(): string | undefined;
36
+ get historyManager(): Memorable<{
37
+ input: object;
38
+ output: object;
39
+ }, Record<string, any>> | undefined;
31
40
  config: RuntimeConfiguration;
32
41
  state: State;
33
42
  agents: Agents;
@@ -32,6 +32,7 @@ export declare abstract class Agent<I extends RunnableInput = RunnableInput, O e
32
32
  run(input: I, options?: RunOptions & {
33
33
  stream?: false;
34
34
  }): Promise<O>;
35
+ private addHistory;
35
36
  /**
36
37
  * Hook that is called before the agent result is returned.
37
38
  * @param _result The agent result.
package/lib/esm/agent.js CHANGED
@@ -115,6 +115,7 @@ export class Agent extends Runnable {
115
115
  logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
116
116
  result,
117
117
  });
118
+ await this.addHistory({ input, output: result });
118
119
  await this.onResult(result);
119
120
  });
120
121
  }
@@ -127,9 +128,17 @@ export class Agent extends Runnable {
127
128
  result,
128
129
  });
129
130
  // TODO: validate result against outputs schema
131
+ await this.addHistory({ input, output: result });
130
132
  await this.onResult(result);
131
133
  return result;
132
134
  }
135
+ async addHistory({ input, output }) {
136
+ await this.context?.historyManager?.create({ input, output }, {
137
+ userId: this.context?.state.userId,
138
+ sessionId: this.context?.state.sessionId,
139
+ agentId: this.id,
140
+ });
141
+ }
133
142
  /**
134
143
  * Hook that is called before the agent result is returned.
135
144
  * @param _result The agent result.
@@ -1,4 +1,5 @@
1
1
  import type { LLMModelConfiguration } from "./llm-model";
2
+ import type { Memorable } from "./memorable";
2
3
  import type { Runnable, RunnableDefinition } from "./runnable";
3
4
  export interface ContextState {
4
5
  userId?: string;
@@ -9,8 +10,13 @@ export interface ContextConfig {
9
10
  llmModel?: LLMModelConfiguration;
10
11
  }
11
12
  export interface Context<State extends ContextState = ContextState, Config extends ContextConfig = ContextConfig> {
13
+ id: string;
12
14
  state: State;
13
15
  config: Config;
16
+ historyManager?: Memorable<{
17
+ input: object;
18
+ output: object;
19
+ }>;
14
20
  resolve<T extends Runnable>(id: string | RunnableDefinition | T): Promise<T>;
15
21
  register<R extends Array<RunnableDefinition | Runnable> = []>(...definition: R): void;
16
22
  resolveDependency<T>(token: string | symbol): T;
@@ -1,3 +1,4 @@
1
+ import { Agent } from "./agent";
1
2
  import type { Context } from "./context";
2
3
  import type { LLMModelInputMessage } from "./llm-model";
3
4
  import { Runnable } from "./runnable";
@@ -25,6 +26,7 @@ export interface MemoryItem<T> {
25
26
  id: string;
26
27
  userId?: string;
27
28
  sessionId?: string;
29
+ agentId?: string;
28
30
  createdAt: string;
29
31
  updatedAt: string;
30
32
  memory: T;
@@ -41,6 +43,7 @@ export type MemoryActions<T> = {
41
43
  options?: {
42
44
  userId?: string;
43
45
  sessionId?: string;
46
+ agentId?: string;
44
47
  metadata?: MemoryMetadata;
45
48
  };
46
49
  };
@@ -55,6 +58,7 @@ export type MemoryActions<T> = {
55
58
  k?: number;
56
59
  userId?: string;
57
60
  sessionId?: string;
61
+ agentId?: string;
58
62
  filter?: MemoryMetadata;
59
63
  sort?: MemorySortOptions;
60
64
  };
@@ -69,6 +73,7 @@ export type MemoryActions<T> = {
69
73
  k?: number;
70
74
  userId?: string;
71
75
  sessionId?: string;
76
+ agentId?: string;
72
77
  filter?: MemoryMetadata;
73
78
  sort?: MemorySortOptions;
74
79
  };
@@ -91,6 +96,7 @@ export type MemoryActions<T> = {
91
96
  options?: {
92
97
  userId?: string;
93
98
  sessionId?: string;
99
+ agentId?: string;
94
100
  metadata?: MemoryMetadata;
95
101
  };
96
102
  };
@@ -128,7 +134,7 @@ export interface SortItem {
128
134
  direction: "asc" | "desc";
129
135
  }
130
136
  export type MemorySortOptions = SortItem | SortItem[];
131
- export declare abstract class Memorable<T, C = undefined> extends Runnable<MemoryActions<T>, MemoryActions<T>["outputs"]> {
137
+ export declare abstract class Memorable<T, C extends Record<string, any> = Record<string, any>> extends Runnable<MemoryActions<T>, MemoryActions<T>["outputs"]> {
132
138
  constructor(context?: Context);
133
139
  abstract runner?: MemoryRunner<T, C>;
134
140
  abstract add(messages: Extract<MemoryActions<T>, {
@@ -174,10 +180,11 @@ export declare abstract class Memorable<T, C = undefined> extends Runnable<Memor
174
180
  }>["outputs"]>;
175
181
  abstract reset(): Promise<void>;
176
182
  }
177
- export type MemoryRunnerInput<C = undefined> = {
183
+ export type MemoryRunnerInput<C extends Record<string, any> = Record<string, any>> = {
178
184
  messages: MemoryMessage[];
179
185
  userId?: string;
180
186
  sessionId?: string;
187
+ agentId?: string;
181
188
  metadata?: MemoryMetadata;
182
189
  filter?: MemoryMetadata;
183
190
  customData: C;
@@ -185,7 +192,7 @@ export type MemoryRunnerInput<C = undefined> = {
185
192
  export type MemoryRunnerOutput<T> = {
186
193
  actions: MemoryActionItem<T>[];
187
194
  };
188
- export declare abstract class MemoryRunner<T, C = undefined> extends Runnable<MemoryRunnerInput<C>, MemoryRunnerOutput<T>> {
195
+ export declare abstract class MemoryRunner<T, C extends Record<string, any> = Record<string, any>> extends Agent<MemoryRunnerInput<C>, MemoryRunnerOutput<T>> {
189
196
  constructor(name: string);
190
197
  }
191
198
  export type MemorableSearchOutput<T extends Memorable<unknown>> = Awaited<ReturnType<T["search"]>>["results"];
@@ -1,4 +1,5 @@
1
1
  import { camelCase, startCase } from "lodash";
2
+ import { Agent } from "./agent";
2
3
  import { Runnable } from "./runnable";
3
4
  import { OrderedRecord } from "./utils";
4
5
  export class Memorable extends Runnable {
@@ -12,7 +13,7 @@ export class Memorable extends Runnable {
12
13
  }, context);
13
14
  }
14
15
  }
15
- export class MemoryRunner extends Runnable {
16
+ export class MemoryRunner extends Agent {
16
17
  constructor(name) {
17
18
  const id = `${camelCase(name)}_runner`;
18
19
  super({
@@ -2,6 +2,7 @@ import { type DependencyContainer } from "tsyringe";
2
2
  import type { constructor as Constructor } from "tsyringe/dist/typings/types";
3
3
  import type { Context, ContextState } from "./context";
4
4
  import type { LLMModel, LLMModelConfiguration } from "./llm-model";
5
+ import type { Memorable } from "./memorable";
5
6
  import { Runnable, type RunnableDefinition } from "./runnable";
6
7
  import type { SandboxFunctionRunner } from "./sandbox-function-runner";
7
8
  import { OrderedRecord } from "./utils/ordered-map";
@@ -17,6 +18,10 @@ export interface RuntimeOptions<Agents extends {
17
18
  config?: RuntimeConfiguration;
18
19
  state?: State;
19
20
  agents?: Agents;
21
+ historyManager?: Memorable<{
22
+ input: object;
23
+ output: object;
24
+ }>;
20
25
  llmModel?: LLMModel | Constructor<LLMModel>;
21
26
  sandboxFunctionRunner?: SandboxFunctionRunner | Constructor<SandboxFunctionRunner>;
22
27
  }
@@ -28,6 +33,10 @@ export declare class Runtime<Agents extends {
28
33
  get options(): RuntimeOptions<Agents, State>;
29
34
  get id(): string;
30
35
  get name(): string | undefined;
36
+ get historyManager(): Memorable<{
37
+ input: object;
38
+ output: object;
39
+ }, Record<string, any>> | undefined;
31
40
  config: RuntimeConfiguration;
32
41
  state: State;
33
42
  agents: Agents;
@@ -44,6 +44,9 @@ let Runtime = Runtime_1 = class Runtime {
44
44
  get name() {
45
45
  return this.inner.name;
46
46
  }
47
+ get historyManager() {
48
+ return this.inner.options.historyManager;
49
+ }
47
50
  config;
48
51
  state;
49
52
  agents = new Proxy({}, { get: (_, prop) => this.resolveSync(prop.toString()) });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.0.11",
3
+ "version": "1.0.12-beta.10",
4
4
  "description": "AIGNE core library",
5
5
  "publishConfig": {
6
6
  "access": "public"