@aigne/core 1.0.16 → 1.1.0-beta.1

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 (254) hide show
  1. package/lib/cjs/agents/agent.d.ts +49 -0
  2. package/lib/cjs/agents/agent.js +92 -0
  3. package/lib/cjs/agents/ai-agent.d.ts +21 -0
  4. package/lib/cjs/agents/ai-agent.js +84 -0
  5. package/lib/cjs/agents/mcp-agent.d.ts +26 -0
  6. package/lib/cjs/agents/mcp-agent.js +75 -0
  7. package/lib/cjs/agents/types.d.ts +9 -0
  8. package/lib/cjs/agents/types.js +16 -0
  9. package/lib/cjs/execution-engine/context.d.ts +10 -0
  10. package/lib/cjs/execution-engine/index.d.ts +45 -0
  11. package/lib/cjs/execution-engine/index.js +190 -0
  12. package/lib/cjs/execution-engine/message-queue.d.ts +5 -0
  13. package/lib/cjs/execution-engine/message-queue.js +9 -0
  14. package/lib/cjs/index.d.ts +10 -21
  15. package/lib/cjs/index.js +10 -21
  16. package/lib/cjs/models/chat-openai.d.ts +14 -0
  17. package/lib/cjs/{llm-models/openai-llm-model.js → models/chat-openai.js} +58 -26
  18. package/lib/cjs/models/chat.d.ts +78 -0
  19. package/lib/cjs/models/chat.js +91 -0
  20. package/lib/cjs/prompt/prompt-builder.d.ts +25 -0
  21. package/lib/cjs/prompt/prompt-builder.js +126 -0
  22. package/lib/cjs/prompt/template.d.ts +70 -0
  23. package/lib/cjs/prompt/template.js +172 -0
  24. package/lib/cjs/prompt/templates/instructions.d.ts +1 -0
  25. package/lib/cjs/prompt/templates/instructions.js +10 -0
  26. package/lib/cjs/utils/logger.js +1 -3
  27. package/lib/cjs/utils/type-utils.d.ts +5 -1
  28. package/lib/cjs/utils/type-utils.js +23 -0
  29. package/lib/dts/agents/agent.d.ts +49 -0
  30. package/lib/dts/agents/ai-agent.d.ts +21 -0
  31. package/lib/dts/agents/mcp-agent.d.ts +26 -0
  32. package/lib/dts/agents/types.d.ts +9 -0
  33. package/lib/dts/execution-engine/context.d.ts +10 -0
  34. package/lib/dts/execution-engine/index.d.ts +45 -0
  35. package/lib/dts/execution-engine/message-queue.d.ts +5 -0
  36. package/lib/dts/index.d.ts +10 -21
  37. package/lib/dts/models/chat-openai.d.ts +14 -0
  38. package/lib/dts/models/chat.d.ts +78 -0
  39. package/lib/dts/prompt/prompt-builder.d.ts +25 -0
  40. package/lib/dts/prompt/template.d.ts +70 -0
  41. package/lib/dts/prompt/templates/instructions.d.ts +1 -0
  42. package/lib/dts/utils/type-utils.d.ts +5 -1
  43. package/lib/esm/agents/agent.d.ts +49 -0
  44. package/lib/esm/agents/agent.js +84 -0
  45. package/lib/esm/agents/ai-agent.d.ts +21 -0
  46. package/lib/esm/agents/ai-agent.js +80 -0
  47. package/lib/esm/agents/mcp-agent.d.ts +26 -0
  48. package/lib/esm/agents/mcp-agent.js +70 -0
  49. package/lib/esm/agents/types.d.ts +9 -0
  50. package/lib/esm/agents/types.js +11 -0
  51. package/lib/esm/execution-engine/context.d.ts +10 -0
  52. package/lib/esm/execution-engine/index.d.ts +45 -0
  53. package/lib/esm/execution-engine/index.js +182 -0
  54. package/lib/esm/execution-engine/message-queue.d.ts +5 -0
  55. package/lib/esm/execution-engine/message-queue.js +5 -0
  56. package/lib/esm/index.d.ts +10 -21
  57. package/lib/esm/index.js +10 -21
  58. package/lib/esm/models/chat-openai.d.ts +14 -0
  59. package/lib/esm/{llm-models/openai-llm-model.js → models/chat-openai.js} +55 -23
  60. package/lib/esm/models/chat.d.ts +78 -0
  61. package/lib/esm/models/chat.js +87 -0
  62. package/lib/esm/prompt/prompt-builder.d.ts +25 -0
  63. package/lib/esm/prompt/prompt-builder.js +117 -0
  64. package/lib/esm/prompt/template.d.ts +70 -0
  65. package/lib/esm/prompt/template.js +158 -0
  66. package/lib/esm/prompt/templates/instructions.d.ts +1 -0
  67. package/lib/esm/prompt/templates/instructions.js +7 -0
  68. package/lib/esm/utils/logger.js +1 -3
  69. package/lib/esm/utils/type-utils.d.ts +5 -1
  70. package/lib/esm/utils/type-utils.js +17 -1
  71. package/package.json +14 -14
  72. package/lib/cjs/agent.d.ts +0 -96
  73. package/lib/cjs/agent.js +0 -165
  74. package/lib/cjs/constants.d.ts +0 -7
  75. package/lib/cjs/constants.js +0 -10
  76. package/lib/cjs/context.d.ts +0 -23
  77. package/lib/cjs/definitions/data-schema.d.ts +0 -61
  78. package/lib/cjs/definitions/data-schema.js +0 -47
  79. package/lib/cjs/definitions/data-type.d.ts +0 -32
  80. package/lib/cjs/definitions/data-type.js +0 -2
  81. package/lib/cjs/definitions/memory.d.ts +0 -53
  82. package/lib/cjs/definitions/memory.js +0 -23
  83. package/lib/cjs/definitions/open-api.d.ts +0 -36
  84. package/lib/cjs/definitions/open-api.js +0 -2
  85. package/lib/cjs/definitions/preload.d.ts +0 -43
  86. package/lib/cjs/definitions/preload.js +0 -34
  87. package/lib/cjs/function-agent.d.ts +0 -23
  88. package/lib/cjs/function-agent.js +0 -62
  89. package/lib/cjs/llm-agent.d.ts +0 -53
  90. package/lib/cjs/llm-agent.js +0 -137
  91. package/lib/cjs/llm-decision-agent.d.ts +0 -53
  92. package/lib/cjs/llm-decision-agent.js +0 -144
  93. package/lib/cjs/llm-model.d.ts +0 -80
  94. package/lib/cjs/llm-model.js +0 -27
  95. package/lib/cjs/llm-models/gemini-llm-model.d.ts +0 -24
  96. package/lib/cjs/llm-models/gemini-llm-model.js +0 -199
  97. package/lib/cjs/llm-models/openai-llm-model.d.ts +0 -26
  98. package/lib/cjs/memorable.d.ts +0 -198
  99. package/lib/cjs/memorable.js +0 -33
  100. package/lib/cjs/open-api-agent.d.ts +0 -26
  101. package/lib/cjs/open-api-agent.js +0 -68
  102. package/lib/cjs/pipeline-agent.d.ts +0 -56
  103. package/lib/cjs/pipeline-agent.js +0 -203
  104. package/lib/cjs/runnable.d.ts +0 -53
  105. package/lib/cjs/runnable.js +0 -32
  106. package/lib/cjs/runtime.d.ts +0 -64
  107. package/lib/cjs/runtime.js +0 -149
  108. package/lib/cjs/sandbox-function-agent.d.ts +0 -21
  109. package/lib/cjs/sandbox-function-agent.js +0 -78
  110. package/lib/cjs/sandbox-function-runner.d.ts +0 -16
  111. package/lib/cjs/sandbox-function-runner.js +0 -32
  112. package/lib/cjs/utils/constants.d.ts +0 -1
  113. package/lib/cjs/utils/constants.js +0 -4
  114. package/lib/cjs/utils/fetch-open-api.d.ts +0 -2
  115. package/lib/cjs/utils/fetch-open-api.js +0 -31
  116. package/lib/cjs/utils/fetch.d.ts +0 -1
  117. package/lib/cjs/utils/fetch.js +0 -21
  118. package/lib/cjs/utils/index.d.ts +0 -12
  119. package/lib/cjs/utils/index.js +0 -28
  120. package/lib/cjs/utils/is-non-nullable.d.ts +0 -2
  121. package/lib/cjs/utils/is-non-nullable.js +0 -20
  122. package/lib/cjs/utils/message-utils.d.ts +0 -20
  123. package/lib/cjs/utils/message-utils.js +0 -79
  124. package/lib/cjs/utils/mustache-utils.d.ts +0 -3
  125. package/lib/cjs/utils/mustache-utils.js +0 -14
  126. package/lib/cjs/utils/nullable.d.ts +0 -7
  127. package/lib/cjs/utils/nullable.js +0 -2
  128. package/lib/cjs/utils/omit.d.ts +0 -1
  129. package/lib/cjs/utils/omit.js +0 -2
  130. package/lib/cjs/utils/open-api-parameter.d.ts +0 -7
  131. package/lib/cjs/utils/open-api-parameter.js +0 -81
  132. package/lib/cjs/utils/ordered-map.d.ts +0 -37
  133. package/lib/cjs/utils/ordered-map.js +0 -103
  134. package/lib/cjs/utils/partial.d.ts +0 -3
  135. package/lib/cjs/utils/partial.js +0 -2
  136. package/lib/cjs/utils/runnable-type.d.ts +0 -5
  137. package/lib/cjs/utils/runnable-type.js +0 -2
  138. package/lib/cjs/utils/stream-utils.d.ts +0 -20
  139. package/lib/cjs/utils/stream-utils.js +0 -86
  140. package/lib/cjs/utils/structured-output-schema.d.ts +0 -3
  141. package/lib/cjs/utils/structured-output-schema.js +0 -43
  142. package/lib/cjs/utils/union.d.ts +0 -1
  143. package/lib/cjs/utils/union.js +0 -2
  144. package/lib/dts/agent.d.ts +0 -96
  145. package/lib/dts/constants.d.ts +0 -7
  146. package/lib/dts/context.d.ts +0 -23
  147. package/lib/dts/definitions/data-schema.d.ts +0 -61
  148. package/lib/dts/definitions/data-type.d.ts +0 -32
  149. package/lib/dts/definitions/memory.d.ts +0 -53
  150. package/lib/dts/definitions/open-api.d.ts +0 -36
  151. package/lib/dts/definitions/preload.d.ts +0 -43
  152. package/lib/dts/function-agent.d.ts +0 -23
  153. package/lib/dts/llm-agent.d.ts +0 -53
  154. package/lib/dts/llm-decision-agent.d.ts +0 -53
  155. package/lib/dts/llm-model.d.ts +0 -80
  156. package/lib/dts/llm-models/gemini-llm-model.d.ts +0 -24
  157. package/lib/dts/llm-models/openai-llm-model.d.ts +0 -26
  158. package/lib/dts/memorable.d.ts +0 -198
  159. package/lib/dts/open-api-agent.d.ts +0 -26
  160. package/lib/dts/pipeline-agent.d.ts +0 -56
  161. package/lib/dts/runnable.d.ts +0 -53
  162. package/lib/dts/runtime.d.ts +0 -64
  163. package/lib/dts/sandbox-function-agent.d.ts +0 -21
  164. package/lib/dts/sandbox-function-runner.d.ts +0 -16
  165. package/lib/dts/utils/constants.d.ts +0 -1
  166. package/lib/dts/utils/fetch-open-api.d.ts +0 -2
  167. package/lib/dts/utils/fetch.d.ts +0 -1
  168. package/lib/dts/utils/index.d.ts +0 -12
  169. package/lib/dts/utils/is-non-nullable.d.ts +0 -2
  170. package/lib/dts/utils/message-utils.d.ts +0 -20
  171. package/lib/dts/utils/mustache-utils.d.ts +0 -3
  172. package/lib/dts/utils/nullable.d.ts +0 -7
  173. package/lib/dts/utils/omit.d.ts +0 -1
  174. package/lib/dts/utils/open-api-parameter.d.ts +0 -7
  175. package/lib/dts/utils/ordered-map.d.ts +0 -37
  176. package/lib/dts/utils/partial.d.ts +0 -3
  177. package/lib/dts/utils/runnable-type.d.ts +0 -5
  178. package/lib/dts/utils/stream-utils.d.ts +0 -20
  179. package/lib/dts/utils/structured-output-schema.d.ts +0 -3
  180. package/lib/dts/utils/union.d.ts +0 -1
  181. package/lib/esm/agent.d.ts +0 -96
  182. package/lib/esm/agent.js +0 -161
  183. package/lib/esm/constants.d.ts +0 -7
  184. package/lib/esm/constants.js +0 -7
  185. package/lib/esm/context.d.ts +0 -23
  186. package/lib/esm/definitions/data-schema.d.ts +0 -61
  187. package/lib/esm/definitions/data-schema.js +0 -44
  188. package/lib/esm/definitions/data-type.d.ts +0 -32
  189. package/lib/esm/definitions/data-type.js +0 -1
  190. package/lib/esm/definitions/memory.d.ts +0 -53
  191. package/lib/esm/definitions/memory.js +0 -20
  192. package/lib/esm/definitions/open-api.d.ts +0 -36
  193. package/lib/esm/definitions/open-api.js +0 -1
  194. package/lib/esm/definitions/preload.d.ts +0 -43
  195. package/lib/esm/definitions/preload.js +0 -31
  196. package/lib/esm/function-agent.d.ts +0 -23
  197. package/lib/esm/function-agent.js +0 -59
  198. package/lib/esm/llm-agent.d.ts +0 -53
  199. package/lib/esm/llm-agent.js +0 -134
  200. package/lib/esm/llm-decision-agent.d.ts +0 -53
  201. package/lib/esm/llm-decision-agent.js +0 -141
  202. package/lib/esm/llm-model.d.ts +0 -80
  203. package/lib/esm/llm-model.js +0 -23
  204. package/lib/esm/llm-models/gemini-llm-model.d.ts +0 -24
  205. package/lib/esm/llm-models/gemini-llm-model.js +0 -195
  206. package/lib/esm/llm-models/openai-llm-model.d.ts +0 -26
  207. package/lib/esm/memorable.d.ts +0 -198
  208. package/lib/esm/memorable.js +0 -28
  209. package/lib/esm/open-api-agent.d.ts +0 -26
  210. package/lib/esm/open-api-agent.js +0 -65
  211. package/lib/esm/pipeline-agent.d.ts +0 -56
  212. package/lib/esm/pipeline-agent.js +0 -200
  213. package/lib/esm/runnable.d.ts +0 -53
  214. package/lib/esm/runnable.js +0 -26
  215. package/lib/esm/runtime.d.ts +0 -64
  216. package/lib/esm/runtime.js +0 -146
  217. package/lib/esm/sandbox-function-agent.d.ts +0 -21
  218. package/lib/esm/sandbox-function-agent.js +0 -74
  219. package/lib/esm/sandbox-function-runner.d.ts +0 -16
  220. package/lib/esm/sandbox-function-runner.js +0 -28
  221. package/lib/esm/utils/constants.d.ts +0 -1
  222. package/lib/esm/utils/constants.js +0 -1
  223. package/lib/esm/utils/fetch-open-api.d.ts +0 -2
  224. package/lib/esm/utils/fetch-open-api.js +0 -28
  225. package/lib/esm/utils/fetch.d.ts +0 -1
  226. package/lib/esm/utils/fetch.js +0 -18
  227. package/lib/esm/utils/index.d.ts +0 -12
  228. package/lib/esm/utils/index.js +0 -12
  229. package/lib/esm/utils/is-non-nullable.d.ts +0 -2
  230. package/lib/esm/utils/is-non-nullable.js +0 -13
  231. package/lib/esm/utils/message-utils.d.ts +0 -20
  232. package/lib/esm/utils/message-utils.js +0 -71
  233. package/lib/esm/utils/mustache-utils.d.ts +0 -3
  234. package/lib/esm/utils/mustache-utils.js +0 -8
  235. package/lib/esm/utils/nullable.d.ts +0 -7
  236. package/lib/esm/utils/nullable.js +0 -1
  237. package/lib/esm/utils/omit.d.ts +0 -1
  238. package/lib/esm/utils/omit.js +0 -1
  239. package/lib/esm/utils/open-api-parameter.d.ts +0 -7
  240. package/lib/esm/utils/open-api-parameter.js +0 -78
  241. package/lib/esm/utils/ordered-map.d.ts +0 -37
  242. package/lib/esm/utils/ordered-map.js +0 -100
  243. package/lib/esm/utils/partial.d.ts +0 -3
  244. package/lib/esm/utils/partial.js +0 -1
  245. package/lib/esm/utils/runnable-type.d.ts +0 -5
  246. package/lib/esm/utils/runnable-type.js +0 -1
  247. package/lib/esm/utils/stream-utils.d.ts +0 -20
  248. package/lib/esm/utils/stream-utils.js +0 -79
  249. package/lib/esm/utils/structured-output-schema.d.ts +0 -3
  250. package/lib/esm/utils/structured-output-schema.js +0 -37
  251. package/lib/esm/utils/union.d.ts +0 -1
  252. package/lib/esm/utils/union.js +0 -1
  253. /package/lib/cjs/{context.js → execution-engine/context.js} +0 -0
  254. /package/lib/esm/{context.js → execution-engine/context.js} +0 -0
@@ -1,64 +0,0 @@
1
- import { type DependencyContainer } from "tsyringe";
2
- import type { constructor as Constructor } from "tsyringe/dist/typings/types";
3
- import type { Context, ContextState } from "./context";
4
- import type { LLMModel, LLMModelConfiguration } from "./llm-model";
5
- import type { Memorable } from "./memorable";
6
- import { Runnable, type RunnableDefinition } from "./runnable";
7
- import type { SandboxFunctionRunner } from "./sandbox-function-runner";
8
- import { OrderedRecord } from "./utils/ordered-map";
9
- import type { DeepPartial } from "./utils/partial";
10
- export interface RuntimeConfiguration {
11
- llmModel?: LLMModelConfiguration;
12
- }
13
- export interface RuntimeOptions<Agents extends {
14
- [name: string]: Runnable;
15
- }, State extends ContextState> {
16
- id?: string;
17
- name?: string;
18
- config?: RuntimeConfiguration;
19
- state?: State;
20
- agents?: Agents;
21
- historyManager?: Memorable<{
22
- input: object;
23
- output: object;
24
- }>;
25
- llmModel?: LLMModel | Constructor<LLMModel>;
26
- sandboxFunctionRunner?: SandboxFunctionRunner | Constructor<SandboxFunctionRunner>;
27
- }
28
- export declare class Runtime<Agents extends {
29
- [name: string]: Runnable;
30
- } = {}, State extends ContextState = ContextState> implements Context<State> {
31
- constructor(options?: RuntimeOptions<Agents, State>);
32
- protected inner: RuntimeInner<Agents, State>;
33
- get options(): RuntimeOptions<Agents, State>;
34
- get id(): string;
35
- get name(): string | undefined;
36
- get historyManager(): Memorable<{
37
- input: object;
38
- output: object;
39
- }, Record<string, any>> | undefined;
40
- config: RuntimeConfiguration;
41
- state: State;
42
- agents: Agents;
43
- private container;
44
- setup(config: DeepPartial<RuntimeConfiguration>): void;
45
- register<R extends Array<RunnableDefinition | Runnable> = []>(...runnables: R): void;
46
- private resolveSync;
47
- resolve<T extends Runnable>(id: string | RunnableDefinition | T): Promise<T>;
48
- resolveDependency<T>(token: string | symbol): T;
49
- copy<State extends ContextState = ContextState>(options: Pick<RuntimeOptions<Agents, State>, "state" | "config">): Runtime<Agents, State>;
50
- }
51
- declare class RuntimeInner<Agents extends {
52
- [name: string]: Runnable;
53
- } = {}, State extends ContextState = ContextState> {
54
- options: RuntimeOptions<Agents, State>;
55
- constructor(options: RuntimeOptions<Agents, State>);
56
- readonly id: string;
57
- readonly name?: string;
58
- config: RuntimeConfiguration;
59
- state: State;
60
- container: DependencyContainer;
61
- runnableDefinitions: OrderedRecord<RunnableDefinition>;
62
- registerDependency<T>(token: string | symbol, dependency: Constructor<T> | T): void;
63
- }
64
- export {};
@@ -1,146 +0,0 @@
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 Runtime_1;
11
- import { produce } from "immer";
12
- import { merge } from "lodash";
13
- import { nanoid } from "nanoid";
14
- import { container, injectable } from "tsyringe";
15
- import { TYPES } from "./constants";
16
- import { FunctionAgent } from "./function-agent";
17
- import { LLMAgent } from "./llm-agent";
18
- import { LLMDecisionAgent } from "./llm-decision-agent";
19
- import { OpenAPIAgent } from "./open-api-agent";
20
- import { PipelineAgent } from "./pipeline-agent";
21
- import { Runnable } from "./runnable";
22
- import { SandboxFunctionAgent } from "./sandbox-function-agent";
23
- import { OrderedRecord } from "./utils/ordered-map";
24
- let Runtime = Runtime_1 = class Runtime {
25
- constructor(options = {}) {
26
- // support copy inner from a existing runtime, but not expose to public
27
- const inner = options.inner;
28
- if (inner instanceof RuntimeInner)
29
- this.inner = inner;
30
- else
31
- this.inner = new RuntimeInner(options);
32
- this.container = this.inner.container.createChildContainer();
33
- this.container.register(TYPES.context, { useValue: this });
34
- this.config = options.config || { ...this.inner.config };
35
- this.state = options.state || { ...this.inner.state };
36
- }
37
- inner;
38
- get options() {
39
- return this.inner.options;
40
- }
41
- get id() {
42
- return this.inner.id;
43
- }
44
- get name() {
45
- return this.inner.name;
46
- }
47
- get historyManager() {
48
- return this.inner.options.historyManager;
49
- }
50
- config;
51
- state;
52
- agents = new Proxy({}, { get: (_, prop) => this.resolveSync(prop.toString()) });
53
- container;
54
- setup(config) {
55
- this.config = produce(this.config, (draft) => {
56
- merge(draft, config);
57
- });
58
- }
59
- register(...runnables) {
60
- for (const runnable of runnables) {
61
- OrderedRecord.pushOrUpdate(this.inner.runnableDefinitions, runnable instanceof Runnable ? runnable.definition : runnable);
62
- }
63
- }
64
- resolveSync(idOrRunnable) {
65
- const runnableId = typeof idOrRunnable === "string" ? idOrRunnable : idOrRunnable.id;
66
- // Find runnable definition by id or name
67
- let definition = this.inner.runnableDefinitions[runnableId] ??
68
- OrderedRecord.find(this.inner.runnableDefinitions, (def) => def.name === runnableId);
69
- if (!definition) {
70
- // extract definition from runnable
71
- if (idOrRunnable instanceof Runnable)
72
- definition = idOrRunnable.definition;
73
- // directly use runnable as definition
74
- else if (typeof idOrRunnable === "object")
75
- definition = idOrRunnable;
76
- }
77
- if (definition) {
78
- const childContainer = this.container
79
- .createChildContainer()
80
- .register(TYPES.definition, { useValue: definition });
81
- const result = childContainer.resolve(definition.type);
82
- childContainer.dispose();
83
- return result;
84
- }
85
- throw new Error(`Runnable not found: ${idOrRunnable}`);
86
- }
87
- async resolve(id) {
88
- return this.resolveSync(id);
89
- }
90
- resolveDependency(token) {
91
- return this.container.resolve(token);
92
- }
93
- copy(options) {
94
- const clone = new Runtime_1({
95
- ...options,
96
- // Copy inner runtime
97
- ...{ inner: this.inner },
98
- });
99
- return clone;
100
- }
101
- };
102
- Runtime = Runtime_1 = __decorate([
103
- injectable(),
104
- __metadata("design:paramtypes", [Object])
105
- ], Runtime);
106
- export { Runtime };
107
- class RuntimeInner {
108
- options;
109
- constructor(options) {
110
- this.options = options;
111
- this.name = options.name;
112
- this.id = options.id || this.name || nanoid();
113
- this.config = options.config || {};
114
- this.state = options.state || {};
115
- this.container.register("pipeline_agent", { useClass: PipelineAgent });
116
- this.container.register("llm_agent", { useClass: LLMAgent });
117
- this.container.register("sandbox_function_agent", {
118
- useClass: SandboxFunctionAgent,
119
- });
120
- this.container.register("llm_decision_agent", {
121
- useClass: LLMDecisionAgent,
122
- });
123
- this.container.register("function_agent", {
124
- useClass: FunctionAgent,
125
- });
126
- this.container.register("open_api_agent", { useClass: OpenAPIAgent });
127
- if (options.sandboxFunctionRunner)
128
- this.registerDependency(TYPES.sandboxFunctionRunner, options.sandboxFunctionRunner);
129
- if (options.llmModel)
130
- this.registerDependency(TYPES.llmModel, options.llmModel);
131
- }
132
- id;
133
- name;
134
- config;
135
- state;
136
- container = container.createChildContainer();
137
- runnableDefinitions = OrderedRecord.fromArray([]);
138
- registerDependency(token, dependency) {
139
- if (typeof dependency === "function")
140
- this.container.register(token, {
141
- useClass: dependency,
142
- });
143
- else
144
- this.container.register(token, { useValue: dependency });
145
- }
146
- }
@@ -1,21 +0,0 @@
1
- import { Agent, type AgentDefinition, type AgentMemories, type AgentPreloads, type AgentProcessInput, type AgentProcessOptions, type CreateAgentInputSchema, type CreateAgentMemoriesSchema, type CreateAgentMemoriesType, type CreateAgentOptions, type CreateAgentOutputSchema, type CreateAgentPreloadsSchema, type CreateAgentPreloadsType } from "./agent";
2
- import type { Context, ContextState } from "./context";
3
- import { type SchemaToType } from "./definitions/data-schema";
4
- import type { RunnableInput, RunnableOutput } from "./runnable";
5
- import type { SandboxFunctionRunner } from "./sandbox-function-runner";
6
- export declare class SandboxFunctionAgent<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> extends Agent<I, O, State, Preloads, Memories> {
7
- definition: SandboxFunctionAgentDefinition;
8
- runner?: SandboxFunctionRunner<I, O, State, Preloads, Memories> | undefined;
9
- static create: typeof create;
10
- constructor(definition: SandboxFunctionAgentDefinition, context?: Context<State>, runner?: SandboxFunctionRunner<I, O, State, Preloads, Memories> | undefined);
11
- process(input: AgentProcessInput<I, Preloads, Memories>, options: AgentProcessOptions<Preloads, Memories>): Promise<import("./runnable").RunnableResponseStream<O>>;
12
- }
13
- export declare function create<I extends CreateAgentInputSchema, O extends CreateAgentOutputSchema, State extends ContextState, Preloads extends CreateAgentPreloadsSchema<I>, Memories extends CreateAgentMemoriesSchema<I>>(options: CreateAgentOptions<I, O, State, Preloads, Memories> & {
14
- language?: string;
15
- code: string;
16
- }): SandboxFunctionAgent<SchemaToType<I>, SchemaToType<O>, State, CreateAgentPreloadsType<I, Preloads>, CreateAgentMemoriesType<I, Memories>>;
17
- export interface SandboxFunctionAgentDefinition extends AgentDefinition {
18
- type: "sandbox_function_agent";
19
- language?: string;
20
- code?: string;
21
- }
@@ -1,74 +0,0 @@
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
- import { nanoid } from "nanoid";
14
- import { inject, injectable } from "tsyringe";
15
- import { Agent, } from "./agent";
16
- import { TYPES } from "./constants";
17
- import { schemaToDataType } from "./definitions/data-schema";
18
- import { toRunnableMemories } from "./definitions/memory";
19
- import { preloadCreatorsToPreloads } from "./definitions/preload";
20
- let SandboxFunctionAgent = class SandboxFunctionAgent extends Agent {
21
- definition;
22
- runner;
23
- static create = create;
24
- constructor(definition, context, runner) {
25
- super(definition, context);
26
- this.definition = definition;
27
- this.runner = runner;
28
- this.runner ??= context?.resolveDependency(TYPES.sandboxFunctionRunner);
29
- }
30
- async process(input, options) {
31
- const { definition: { language, code, ...definition }, runner, context, } = this;
32
- if (!runner)
33
- throw new Error("Sandbox function runner is required");
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({
39
- name: definition.name || definition.id,
40
- language,
41
- code,
42
- input,
43
- preloads: options.preloads,
44
- memories: options.memories,
45
- context: { state: context.state },
46
- }, { stream: true });
47
- }
48
- };
49
- SandboxFunctionAgent = __decorate([
50
- injectable(),
51
- __param(0, inject(TYPES.definition)),
52
- __param(1, inject(TYPES.context)),
53
- __param(2, inject(TYPES.sandboxFunctionRunner)),
54
- __metadata("design:paramtypes", [Object, Object, Function])
55
- ], SandboxFunctionAgent);
56
- export { SandboxFunctionAgent };
57
- export function create(options) {
58
- const agentId = options.name || nanoid();
59
- const inputs = schemaToDataType(options.inputs);
60
- const outputs = schemaToDataType(options.outputs);
61
- const preloads = preloadCreatorsToPreloads(inputs, options.preloads);
62
- const memories = toRunnableMemories(agentId, inputs, options.memories ?? {});
63
- return new SandboxFunctionAgent({
64
- id: agentId,
65
- name: options.name,
66
- type: "sandbox_function_agent",
67
- inputs,
68
- outputs,
69
- preloads,
70
- memories,
71
- language: options.language,
72
- code: options.code,
73
- }, options.context);
74
- }
@@ -1,16 +0,0 @@
1
- import { Agent, type AgentMemories, type AgentPreloads } from "./agent";
2
- import type { Context, ContextState } from "./context";
3
- import type { RunnableInput, RunnableOutput } from "./runnable";
4
- export type SandboxFunctionRunnerInput<Input extends RunnableInput = RunnableInput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> = {
5
- name: string;
6
- language?: string;
7
- code: string;
8
- input: Input;
9
- preloads: Preloads;
10
- memories: Memories;
11
- context: Pick<Context<State>, "state">;
12
- };
13
- export type SandboxFunctionRunnerOutput<Output extends RunnableOutput> = Output;
14
- export declare abstract class SandboxFunctionRunner<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> extends Agent<SandboxFunctionRunnerInput<I, State, Preloads, Memories>, SandboxFunctionRunnerOutput<O>> {
15
- constructor(context?: Context);
16
- }
@@ -1,28 +0,0 @@
1
- import { Agent, } from "./agent";
2
- import { OrderedRecord } from "./utils";
3
- export class SandboxFunctionRunner extends Agent {
4
- constructor(context) {
5
- super({
6
- id: "sandbox_function_runner",
7
- type: "sandbox_function_runner",
8
- name: "Sandbox Function Runner",
9
- description: "Run a function",
10
- inputs: OrderedRecord.fromArray([
11
- { id: "name", name: "name", type: "string", required: true },
12
- { id: "language", name: "language", type: "string" },
13
- { id: "code", name: "code", type: "string", required: true },
14
- { id: "input", name: "input", type: "object", required: true },
15
- { id: "preloads", name: "preloads", type: "object", required: true },
16
- { id: "memories", name: "memories", type: "object", required: true },
17
- { id: "context", name: "context", type: "object", required: true },
18
- ]),
19
- outputs: OrderedRecord.fromArray([
20
- {
21
- id: "result",
22
- name: "result",
23
- type: "object",
24
- },
25
- ]),
26
- }, context);
27
- }
28
- }
@@ -1 +0,0 @@
1
- export declare const FETCH_TIMEOUT: number;
@@ -1 +0,0 @@
1
- export const FETCH_TIMEOUT = Number(process.env.FETCH_TIMEOUT) || 30000;
@@ -1,2 +0,0 @@
1
- import type { FetchRequest } from "../definitions/open-api";
2
- export declare function fetchOpenApi<T>(request: FetchRequest): Promise<T>;
@@ -1,28 +0,0 @@
1
- import { withQuery } from "ufo";
2
- import { FETCH_TIMEOUT } from "./constants";
3
- import { checkFetchResponse } from "./fetch";
4
- export async function fetchOpenApi(request) {
5
- const cookie = request.cookies
6
- ? Object.entries(request.cookies)
7
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
8
- .join("; ")
9
- .trim()
10
- : undefined;
11
- const controller = new AbortController();
12
- const abortTimer = setTimeout(() => controller.abort(), FETCH_TIMEOUT);
13
- const response = await fetch(withQuery(request.url, request.query ?? {}), {
14
- method: request.method,
15
- headers: {
16
- "Content-Type": "application/json",
17
- ...request.headers,
18
- ...(cookie && { cookie }),
19
- },
20
- body: request.method.toLowerCase() !== "get" && request.body
21
- ? JSON.stringify(request.body)
22
- : undefined,
23
- credentials: request.cookies ? "include" : "same-origin",
24
- signal: controller.signal,
25
- }).finally(() => clearTimeout(abortTimer));
26
- await checkFetchResponse(response);
27
- return response.json();
28
- }
@@ -1 +0,0 @@
1
- export declare function checkFetchResponse(result: Response): Promise<Response>;
@@ -1,18 +0,0 @@
1
- export async function checkFetchResponse(result) {
2
- if (!result.ok) {
3
- let message;
4
- try {
5
- const json = await result.json();
6
- const msg = json.error?.message || json.message;
7
- if (msg && typeof msg === "string") {
8
- message = msg;
9
- }
10
- }
11
- catch {
12
- // ignore
13
- }
14
- throw new Error(message ||
15
- `Failed to fetch url ${result.url} with status ${result.status}`);
16
- }
17
- return result;
18
- }
@@ -1,12 +0,0 @@
1
- export * from "./fetch";
2
- export * from "./fetch-open-api";
3
- export * from "./is-non-nullable";
4
- export * from "./logger";
5
- export * from "./mustache-utils";
6
- export * from "./nullable";
7
- export * from "./omit";
8
- export * from "./open-api-parameter";
9
- export * from "./ordered-map";
10
- export * from "./partial";
11
- export * from "./stream-utils";
12
- export * from "./union";
@@ -1,12 +0,0 @@
1
- export * from "./fetch";
2
- export * from "./fetch-open-api";
3
- export * from "./is-non-nullable";
4
- export * from "./logger";
5
- export * from "./mustache-utils";
6
- export * from "./nullable";
7
- export * from "./omit";
8
- export * from "./open-api-parameter";
9
- export * from "./ordered-map";
10
- export * from "./partial";
11
- export * from "./stream-utils";
12
- export * from "./union";
@@ -1,2 +0,0 @@
1
- export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
2
- export declare function isPropsNonNullable<T, K extends keyof T>(...props: (K | K[])[]): (value: T) => value is T & Required<Pick<T, K>>;
@@ -1,13 +0,0 @@
1
- import isNil from "lodash/isNil";
2
- export function isNonNullable(value) {
3
- return !isNil(value);
4
- }
5
- export function isPropsNonNullable(...props) {
6
- return (value) => {
7
- for (const prop of props.flat()) {
8
- if (isNil(value?.[prop]))
9
- return false;
10
- }
11
- return true;
12
- };
13
- }
@@ -1,20 +0,0 @@
1
- import type { LLMAgentDefinition } from "../llm-agent";
2
- import type { LLMModelInputMessage } from "../llm-model";
3
- import type { MemoryItemWithScore } from "../memorable";
4
- export declare function mergeHistoryMessages(messages: LLMModelInputMessage[], history: LLMModelInputMessage[]): LLMModelInputMessage[];
5
- export declare function memoriesToMessages(memories: {
6
- [name: string]: MemoryItemWithScore[];
7
- }, { primaryMemoryName }?: {
8
- primaryMemoryName?: string;
9
- }): {
10
- primaryMemory: LLMModelInputMessage[];
11
- memory: string;
12
- };
13
- export declare function prepareMessages(definition: Pick<LLMAgentDefinition, "messages" | "memories" | "primaryMemoryId">, input: {
14
- [name: string]: unknown;
15
- }, memories: {
16
- [name: string]: MemoryItemWithScore[];
17
- }): {
18
- originalMessages: LLMModelInputMessage[];
19
- messagesWithMemory: LLMModelInputMessage[];
20
- };
@@ -1,71 +0,0 @@
1
- import omit from "lodash/omit";
2
- import { isNonNullable } from "./is-non-nullable";
3
- import { renderMessage } from "./mustache-utils";
4
- import { OrderedRecord } from "./ordered-map";
5
- export function mergeHistoryMessages(messages, history) {
6
- const firstUserMessageIndex = messages.findIndex((m) => m.role === "user");
7
- if (firstUserMessageIndex >= 0) {
8
- return [
9
- ...messages.slice(0, firstUserMessageIndex),
10
- ...history,
11
- ...messages.slice(firstUserMessageIndex),
12
- ];
13
- }
14
- return [...history, ...messages];
15
- }
16
- export function memoriesToMessages(memories, { primaryMemoryName } = {}) {
17
- const primary = (primaryMemoryName && memories[primaryMemoryName]) || [];
18
- const otherMemories = primaryMemoryName
19
- ? omit(memories, primaryMemoryName)
20
- : memories;
21
- const primaryMemory = primary
22
- .map((i) => {
23
- const content = renderMessage("{{memory}}", { memory: i.memory }).trim();
24
- const role = ["user", "assistant"].includes(i.metadata.role)
25
- ? i.metadata.role
26
- : undefined;
27
- if (!role || !content)
28
- return null;
29
- return { role, content };
30
- })
31
- .filter(isNonNullable);
32
- const memory = Object.values(otherMemories)
33
- .map((i) => i
34
- .map((j) => renderMessage("{{memory}}\n{{metadata}}", j).trim() || null)
35
- .filter(isNonNullable)
36
- .join("\n"))
37
- .join("\n");
38
- return {
39
- primaryMemory,
40
- memory,
41
- };
42
- }
43
- export function prepareMessages(definition, input, memories) {
44
- const originalMessages = OrderedRecord.toArray(definition.messages).map(({ role, content }) => ({
45
- role,
46
- content: typeof content === "string" ? renderMessage(content, input) : content,
47
- }));
48
- if (!originalMessages.length)
49
- throw new Error("Messages are required");
50
- const { primaryMemory, memory } = memoriesToMessages(memories, {
51
- primaryMemoryName: OrderedRecord.find(definition.memories, (i) => i.id === definition.primaryMemoryId)?.name,
52
- });
53
- let messagesWithMemory = [...originalMessages];
54
- // Add memory to a system message
55
- if (memory) {
56
- const message = {
57
- role: "system",
58
- content: `\
59
- Here are the memories about the user:
60
- ${memory}
61
- `,
62
- };
63
- const lastSystemMessageIndex = messagesWithMemory.findLastIndex((i) => i.role === "assistant");
64
- messagesWithMemory.splice(lastSystemMessageIndex + 1, 0, message);
65
- }
66
- // Add primary memory to messages
67
- if (primaryMemory.length)
68
- messagesWithMemory = mergeHistoryMessages(messagesWithMemory, primaryMemory);
69
- // TODO: support comment/image for messages
70
- return { originalMessages, messagesWithMemory };
71
- }
@@ -1,3 +0,0 @@
1
- export declare function renderMessage(template: string, variables?: {
2
- [key: string]: any;
3
- }): string;
@@ -1,8 +0,0 @@
1
- import Mustache from "mustache";
2
- export function renderMessage(template, variables) {
3
- return Mustache.render(template, variables, undefined, {
4
- escape: (v) => {
5
- return typeof v === "object" ? JSON.stringify(v) : v;
6
- },
7
- });
8
- }
@@ -1,7 +0,0 @@
1
- export type MakeNullablePropertyOptional<T extends {
2
- [key: string]: any;
3
- }> = {
4
- [K in keyof T as Extract<T[K], null | undefined> extends never ? K : never]: T[K];
5
- } & {
6
- [K in keyof T as Extract<T[K], null | undefined> extends never ? never : K]?: T[K];
7
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export type OmitPropsFromUnion<T, K extends string | number | symbol> = T extends any ? Omit<T, K> : never;
@@ -1 +0,0 @@
1
- export {};
@@ -1,7 +0,0 @@
1
- import type { AuthConfig, FetchRequest, HTTPMethod } from "../definitions/open-api";
2
- import type { OpenAPIAgentDefinition } from "../open-api-agent";
3
- export declare function formatOpenAPIRequest(api: {
4
- url: string;
5
- method: HTTPMethod;
6
- auth?: AuthConfig;
7
- }, inputs: OpenAPIAgentDefinition["inputs"], input: Record<string, any>): Promise<FetchRequest>;