@aigne/core 1.0.17 → 1.1.0-beta.2

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 (252) hide show
  1. package/lib/cjs/agents/agent.d.ts +55 -0
  2. package/lib/cjs/agents/agent.js +91 -0
  3. package/lib/cjs/agents/ai-agent.d.ts +20 -0
  4. package/lib/cjs/agents/ai-agent.js +86 -0
  5. package/lib/cjs/agents/mcp-agent.d.ts +27 -0
  6. package/lib/cjs/agents/mcp-agent.js +95 -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} +62 -27
  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 +34 -0
  21. package/lib/cjs/prompt/prompt-builder.js +140 -0
  22. package/lib/cjs/prompt/template.d.ts +70 -0
  23. package/lib/cjs/prompt/template.js +172 -0
  24. package/lib/cjs/utils/logger.d.ts +6 -3
  25. package/lib/cjs/utils/logger.js +9 -14
  26. package/lib/cjs/utils/type-utils.d.ts +5 -1
  27. package/lib/cjs/utils/type-utils.js +23 -0
  28. package/lib/dts/agents/agent.d.ts +55 -0
  29. package/lib/dts/agents/ai-agent.d.ts +20 -0
  30. package/lib/dts/agents/mcp-agent.d.ts +27 -0
  31. package/lib/dts/agents/types.d.ts +9 -0
  32. package/lib/dts/execution-engine/context.d.ts +10 -0
  33. package/lib/dts/execution-engine/index.d.ts +45 -0
  34. package/lib/dts/execution-engine/message-queue.d.ts +5 -0
  35. package/lib/dts/index.d.ts +10 -21
  36. package/lib/dts/models/chat-openai.d.ts +14 -0
  37. package/lib/dts/models/chat.d.ts +78 -0
  38. package/lib/dts/prompt/prompt-builder.d.ts +34 -0
  39. package/lib/dts/prompt/template.d.ts +70 -0
  40. package/lib/dts/utils/logger.d.ts +6 -3
  41. package/lib/dts/utils/type-utils.d.ts +5 -1
  42. package/lib/esm/agents/agent.d.ts +55 -0
  43. package/lib/esm/agents/agent.js +83 -0
  44. package/lib/esm/agents/ai-agent.d.ts +20 -0
  45. package/lib/esm/agents/ai-agent.js +82 -0
  46. package/lib/esm/agents/mcp-agent.d.ts +27 -0
  47. package/lib/esm/agents/mcp-agent.js +90 -0
  48. package/lib/esm/agents/types.d.ts +9 -0
  49. package/lib/esm/agents/types.js +11 -0
  50. package/lib/esm/execution-engine/context.d.ts +10 -0
  51. package/lib/esm/execution-engine/index.d.ts +45 -0
  52. package/lib/esm/execution-engine/index.js +182 -0
  53. package/lib/esm/execution-engine/message-queue.d.ts +5 -0
  54. package/lib/esm/execution-engine/message-queue.js +5 -0
  55. package/lib/esm/index.d.ts +10 -21
  56. package/lib/esm/index.js +10 -21
  57. package/lib/esm/models/chat-openai.d.ts +14 -0
  58. package/lib/esm/{llm-models/openai-llm-model.js → models/chat-openai.js} +59 -24
  59. package/lib/esm/models/chat.d.ts +78 -0
  60. package/lib/esm/models/chat.js +87 -0
  61. package/lib/esm/prompt/prompt-builder.d.ts +34 -0
  62. package/lib/esm/prompt/prompt-builder.js +131 -0
  63. package/lib/esm/prompt/template.d.ts +70 -0
  64. package/lib/esm/prompt/template.js +158 -0
  65. package/lib/esm/utils/logger.d.ts +6 -3
  66. package/lib/esm/utils/logger.js +6 -14
  67. package/lib/esm/utils/type-utils.d.ts +5 -1
  68. package/lib/esm/utils/type-utils.js +17 -1
  69. package/package.json +18 -14
  70. package/lib/cjs/agent.d.ts +0 -96
  71. package/lib/cjs/agent.js +0 -165
  72. package/lib/cjs/constants.d.ts +0 -7
  73. package/lib/cjs/constants.js +0 -10
  74. package/lib/cjs/context.d.ts +0 -23
  75. package/lib/cjs/definitions/data-schema.d.ts +0 -61
  76. package/lib/cjs/definitions/data-schema.js +0 -47
  77. package/lib/cjs/definitions/data-type.d.ts +0 -32
  78. package/lib/cjs/definitions/data-type.js +0 -2
  79. package/lib/cjs/definitions/memory.d.ts +0 -53
  80. package/lib/cjs/definitions/memory.js +0 -23
  81. package/lib/cjs/definitions/open-api.d.ts +0 -36
  82. package/lib/cjs/definitions/open-api.js +0 -2
  83. package/lib/cjs/definitions/preload.d.ts +0 -43
  84. package/lib/cjs/definitions/preload.js +0 -34
  85. package/lib/cjs/function-agent.d.ts +0 -23
  86. package/lib/cjs/function-agent.js +0 -62
  87. package/lib/cjs/llm-agent.d.ts +0 -53
  88. package/lib/cjs/llm-agent.js +0 -140
  89. package/lib/cjs/llm-decision-agent.d.ts +0 -53
  90. package/lib/cjs/llm-decision-agent.js +0 -144
  91. package/lib/cjs/llm-model.d.ts +0 -80
  92. package/lib/cjs/llm-model.js +0 -27
  93. package/lib/cjs/llm-models/gemini-llm-model.d.ts +0 -24
  94. package/lib/cjs/llm-models/gemini-llm-model.js +0 -199
  95. package/lib/cjs/llm-models/openai-llm-model.d.ts +0 -26
  96. package/lib/cjs/memorable.d.ts +0 -198
  97. package/lib/cjs/memorable.js +0 -33
  98. package/lib/cjs/open-api-agent.d.ts +0 -26
  99. package/lib/cjs/open-api-agent.js +0 -68
  100. package/lib/cjs/pipeline-agent.d.ts +0 -56
  101. package/lib/cjs/pipeline-agent.js +0 -206
  102. package/lib/cjs/runnable.d.ts +0 -76
  103. package/lib/cjs/runnable.js +0 -37
  104. package/lib/cjs/runtime.d.ts +0 -64
  105. package/lib/cjs/runtime.js +0 -149
  106. package/lib/cjs/sandbox-function-agent.d.ts +0 -21
  107. package/lib/cjs/sandbox-function-agent.js +0 -78
  108. package/lib/cjs/sandbox-function-runner.d.ts +0 -16
  109. package/lib/cjs/sandbox-function-runner.js +0 -32
  110. package/lib/cjs/utils/constants.d.ts +0 -1
  111. package/lib/cjs/utils/constants.js +0 -4
  112. package/lib/cjs/utils/fetch-open-api.d.ts +0 -2
  113. package/lib/cjs/utils/fetch-open-api.js +0 -31
  114. package/lib/cjs/utils/fetch.d.ts +0 -1
  115. package/lib/cjs/utils/fetch.js +0 -21
  116. package/lib/cjs/utils/index.d.ts +0 -12
  117. package/lib/cjs/utils/index.js +0 -28
  118. package/lib/cjs/utils/is-non-nullable.d.ts +0 -2
  119. package/lib/cjs/utils/is-non-nullable.js +0 -20
  120. package/lib/cjs/utils/message-utils.d.ts +0 -20
  121. package/lib/cjs/utils/message-utils.js +0 -79
  122. package/lib/cjs/utils/mustache-utils.d.ts +0 -3
  123. package/lib/cjs/utils/mustache-utils.js +0 -14
  124. package/lib/cjs/utils/nullable.d.ts +0 -7
  125. package/lib/cjs/utils/nullable.js +0 -2
  126. package/lib/cjs/utils/omit.d.ts +0 -1
  127. package/lib/cjs/utils/omit.js +0 -2
  128. package/lib/cjs/utils/open-api-parameter.d.ts +0 -7
  129. package/lib/cjs/utils/open-api-parameter.js +0 -81
  130. package/lib/cjs/utils/ordered-map.d.ts +0 -37
  131. package/lib/cjs/utils/ordered-map.js +0 -103
  132. package/lib/cjs/utils/partial.d.ts +0 -3
  133. package/lib/cjs/utils/partial.js +0 -2
  134. package/lib/cjs/utils/runnable-type.d.ts +0 -5
  135. package/lib/cjs/utils/runnable-type.js +0 -2
  136. package/lib/cjs/utils/stream-utils.d.ts +0 -20
  137. package/lib/cjs/utils/stream-utils.js +0 -86
  138. package/lib/cjs/utils/structured-output-schema.d.ts +0 -3
  139. package/lib/cjs/utils/structured-output-schema.js +0 -43
  140. package/lib/cjs/utils/union.d.ts +0 -1
  141. package/lib/cjs/utils/union.js +0 -2
  142. package/lib/dts/agent.d.ts +0 -96
  143. package/lib/dts/constants.d.ts +0 -7
  144. package/lib/dts/context.d.ts +0 -23
  145. package/lib/dts/definitions/data-schema.d.ts +0 -61
  146. package/lib/dts/definitions/data-type.d.ts +0 -32
  147. package/lib/dts/definitions/memory.d.ts +0 -53
  148. package/lib/dts/definitions/open-api.d.ts +0 -36
  149. package/lib/dts/definitions/preload.d.ts +0 -43
  150. package/lib/dts/function-agent.d.ts +0 -23
  151. package/lib/dts/llm-agent.d.ts +0 -53
  152. package/lib/dts/llm-decision-agent.d.ts +0 -53
  153. package/lib/dts/llm-model.d.ts +0 -80
  154. package/lib/dts/llm-models/gemini-llm-model.d.ts +0 -24
  155. package/lib/dts/llm-models/openai-llm-model.d.ts +0 -26
  156. package/lib/dts/memorable.d.ts +0 -198
  157. package/lib/dts/open-api-agent.d.ts +0 -26
  158. package/lib/dts/pipeline-agent.d.ts +0 -56
  159. package/lib/dts/runnable.d.ts +0 -76
  160. package/lib/dts/runtime.d.ts +0 -64
  161. package/lib/dts/sandbox-function-agent.d.ts +0 -21
  162. package/lib/dts/sandbox-function-runner.d.ts +0 -16
  163. package/lib/dts/utils/constants.d.ts +0 -1
  164. package/lib/dts/utils/fetch-open-api.d.ts +0 -2
  165. package/lib/dts/utils/fetch.d.ts +0 -1
  166. package/lib/dts/utils/index.d.ts +0 -12
  167. package/lib/dts/utils/is-non-nullable.d.ts +0 -2
  168. package/lib/dts/utils/message-utils.d.ts +0 -20
  169. package/lib/dts/utils/mustache-utils.d.ts +0 -3
  170. package/lib/dts/utils/nullable.d.ts +0 -7
  171. package/lib/dts/utils/omit.d.ts +0 -1
  172. package/lib/dts/utils/open-api-parameter.d.ts +0 -7
  173. package/lib/dts/utils/ordered-map.d.ts +0 -37
  174. package/lib/dts/utils/partial.d.ts +0 -3
  175. package/lib/dts/utils/runnable-type.d.ts +0 -5
  176. package/lib/dts/utils/stream-utils.d.ts +0 -20
  177. package/lib/dts/utils/structured-output-schema.d.ts +0 -3
  178. package/lib/dts/utils/union.d.ts +0 -1
  179. package/lib/esm/agent.d.ts +0 -96
  180. package/lib/esm/agent.js +0 -161
  181. package/lib/esm/constants.d.ts +0 -7
  182. package/lib/esm/constants.js +0 -7
  183. package/lib/esm/context.d.ts +0 -23
  184. package/lib/esm/definitions/data-schema.d.ts +0 -61
  185. package/lib/esm/definitions/data-schema.js +0 -44
  186. package/lib/esm/definitions/data-type.d.ts +0 -32
  187. package/lib/esm/definitions/data-type.js +0 -1
  188. package/lib/esm/definitions/memory.d.ts +0 -53
  189. package/lib/esm/definitions/memory.js +0 -20
  190. package/lib/esm/definitions/open-api.d.ts +0 -36
  191. package/lib/esm/definitions/open-api.js +0 -1
  192. package/lib/esm/definitions/preload.d.ts +0 -43
  193. package/lib/esm/definitions/preload.js +0 -31
  194. package/lib/esm/function-agent.d.ts +0 -23
  195. package/lib/esm/function-agent.js +0 -59
  196. package/lib/esm/llm-agent.d.ts +0 -53
  197. package/lib/esm/llm-agent.js +0 -137
  198. package/lib/esm/llm-decision-agent.d.ts +0 -53
  199. package/lib/esm/llm-decision-agent.js +0 -141
  200. package/lib/esm/llm-model.d.ts +0 -80
  201. package/lib/esm/llm-model.js +0 -23
  202. package/lib/esm/llm-models/gemini-llm-model.d.ts +0 -24
  203. package/lib/esm/llm-models/gemini-llm-model.js +0 -195
  204. package/lib/esm/llm-models/openai-llm-model.d.ts +0 -26
  205. package/lib/esm/memorable.d.ts +0 -198
  206. package/lib/esm/memorable.js +0 -28
  207. package/lib/esm/open-api-agent.d.ts +0 -26
  208. package/lib/esm/open-api-agent.js +0 -65
  209. package/lib/esm/pipeline-agent.d.ts +0 -56
  210. package/lib/esm/pipeline-agent.js +0 -203
  211. package/lib/esm/runnable.d.ts +0 -76
  212. package/lib/esm/runnable.js +0 -30
  213. package/lib/esm/runtime.d.ts +0 -64
  214. package/lib/esm/runtime.js +0 -146
  215. package/lib/esm/sandbox-function-agent.d.ts +0 -21
  216. package/lib/esm/sandbox-function-agent.js +0 -74
  217. package/lib/esm/sandbox-function-runner.d.ts +0 -16
  218. package/lib/esm/sandbox-function-runner.js +0 -28
  219. package/lib/esm/utils/constants.d.ts +0 -1
  220. package/lib/esm/utils/constants.js +0 -1
  221. package/lib/esm/utils/fetch-open-api.d.ts +0 -2
  222. package/lib/esm/utils/fetch-open-api.js +0 -28
  223. package/lib/esm/utils/fetch.d.ts +0 -1
  224. package/lib/esm/utils/fetch.js +0 -18
  225. package/lib/esm/utils/index.d.ts +0 -12
  226. package/lib/esm/utils/index.js +0 -12
  227. package/lib/esm/utils/is-non-nullable.d.ts +0 -2
  228. package/lib/esm/utils/is-non-nullable.js +0 -13
  229. package/lib/esm/utils/message-utils.d.ts +0 -20
  230. package/lib/esm/utils/message-utils.js +0 -71
  231. package/lib/esm/utils/mustache-utils.d.ts +0 -3
  232. package/lib/esm/utils/mustache-utils.js +0 -8
  233. package/lib/esm/utils/nullable.d.ts +0 -7
  234. package/lib/esm/utils/nullable.js +0 -1
  235. package/lib/esm/utils/omit.d.ts +0 -1
  236. package/lib/esm/utils/omit.js +0 -1
  237. package/lib/esm/utils/open-api-parameter.d.ts +0 -7
  238. package/lib/esm/utils/open-api-parameter.js +0 -78
  239. package/lib/esm/utils/ordered-map.d.ts +0 -37
  240. package/lib/esm/utils/ordered-map.js +0 -100
  241. package/lib/esm/utils/partial.d.ts +0 -3
  242. package/lib/esm/utils/partial.js +0 -1
  243. package/lib/esm/utils/runnable-type.d.ts +0 -5
  244. package/lib/esm/utils/runnable-type.js +0 -1
  245. package/lib/esm/utils/stream-utils.d.ts +0 -20
  246. package/lib/esm/utils/stream-utils.js +0 -79
  247. package/lib/esm/utils/structured-output-schema.d.ts +0 -3
  248. package/lib/esm/utils/structured-output-schema.js +0 -37
  249. package/lib/esm/utils/union.d.ts +0 -1
  250. package/lib/esm/utils/union.js +0 -1
  251. /package/lib/cjs/{context.js → execution-engine/context.js} +0 -0
  252. /package/lib/esm/{context.js → execution-engine/context.js} +0 -0
@@ -1,21 +1,10 @@
1
- export * from "./agent";
2
- export * from "./constants";
3
- export * from "./context";
4
- export * from "./definitions/data-type";
5
- export * from "./definitions/data-schema";
6
- export * from "./definitions/memory";
7
- export * from "./definitions/open-api";
8
- export * from "./function-agent";
9
- export * from "./llm-agent";
10
- export * from "./llm-decision-agent";
11
- export * from "./llm-model";
12
- export * from "./llm-models/gemini-llm-model";
13
- export * from "./llm-models/openai-llm-model";
14
- export * from "./memorable";
15
- export * from "./open-api-agent";
16
- export * from "./pipeline-agent";
17
- export * from "./runnable";
18
- export * from "./runtime";
19
- export * from "./sandbox-function-agent";
20
- export * from "./sandbox-function-runner";
21
- export * from "./utils";
1
+ export * from "./agents/agent";
2
+ export * from "./agents/ai-agent";
3
+ export * from "./agents/mcp-agent";
4
+ export * from "./execution-engine";
5
+ export * from "./execution-engine/context";
6
+ export * from "./execution-engine/message-queue";
7
+ export * from "./models/chat";
8
+ export * from "./models/chat-openai";
9
+ export * from "./prompt/prompt-builder";
10
+ export * from "./prompt/template";
package/lib/cjs/index.js CHANGED
@@ -14,24 +14,13 @@ 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("./agent"), exports);
18
- __exportStar(require("./constants"), exports);
19
- __exportStar(require("./context"), exports);
20
- __exportStar(require("./definitions/data-type"), exports);
21
- __exportStar(require("./definitions/data-schema"), exports);
22
- __exportStar(require("./definitions/memory"), exports);
23
- __exportStar(require("./definitions/open-api"), exports);
24
- __exportStar(require("./function-agent"), exports);
25
- __exportStar(require("./llm-agent"), exports);
26
- __exportStar(require("./llm-decision-agent"), exports);
27
- __exportStar(require("./llm-model"), exports);
28
- __exportStar(require("./llm-models/gemini-llm-model"), exports);
29
- __exportStar(require("./llm-models/openai-llm-model"), exports);
30
- __exportStar(require("./memorable"), exports);
31
- __exportStar(require("./open-api-agent"), exports);
32
- __exportStar(require("./pipeline-agent"), exports);
33
- __exportStar(require("./runnable"), exports);
34
- __exportStar(require("./runtime"), exports);
35
- __exportStar(require("./sandbox-function-agent"), exports);
36
- __exportStar(require("./sandbox-function-runner"), exports);
37
- __exportStar(require("./utils"), exports);
17
+ __exportStar(require("./agents/agent"), exports);
18
+ __exportStar(require("./agents/ai-agent"), exports);
19
+ __exportStar(require("./agents/mcp-agent"), exports);
20
+ __exportStar(require("./execution-engine"), exports);
21
+ __exportStar(require("./execution-engine/context"), exports);
22
+ __exportStar(require("./execution-engine/message-queue"), exports);
23
+ __exportStar(require("./models/chat"), exports);
24
+ __exportStar(require("./models/chat-openai"), exports);
25
+ __exportStar(require("./prompt/prompt-builder"), exports);
26
+ __exportStar(require("./prompt/template"), exports);
@@ -0,0 +1,14 @@
1
+ import { ChatModel, type ChatModelInput, type ChatModelOutput } from "./chat";
2
+ export declare class ChatModelOpenAI extends ChatModel {
3
+ config?: {
4
+ apiKey?: string;
5
+ model?: string;
6
+ } | undefined;
7
+ constructor(config?: {
8
+ apiKey?: string;
9
+ model?: string;
10
+ } | undefined);
11
+ private _client?;
12
+ private get client();
13
+ process(input: ChatModelInput): Promise<ChatModelOutput>;
14
+ }
@@ -3,25 +3,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.OpenaiLLMModel = void 0;
6
+ exports.ChatModelOpenAI = void 0;
7
7
  const nanoid_1 = require("nanoid");
8
8
  const openai_1 = __importDefault(require("openai"));
9
- const llm_model_1 = require("../llm-model");
10
- const is_non_nullable_1 = require("../utils/is-non-nullable");
11
- class OpenaiLLMModel extends llm_model_1.LLMModel {
9
+ const type_utils_1 = require("../utils/type-utils");
10
+ const chat_1 = require("./chat");
11
+ const CHAT_MODEL_OPENAI_DEFAULT_MODEL = "gpt-4o-mini";
12
+ class ChatModelOpenAI extends chat_1.ChatModel {
12
13
  config;
13
14
  constructor(config) {
14
15
  super();
15
16
  this.config = config;
16
- this.client = new openai_1.default({ apiKey: this.config.apiKey });
17
17
  }
18
- client;
19
- setApiKey(apiKey) {
20
- this.client = new openai_1.default({ apiKey });
18
+ _client;
19
+ get client() {
20
+ if (!this.config?.apiKey)
21
+ throw new Error("Api Key is required for ChatModelOpenAI");
22
+ this._client ??= new openai_1.default({ apiKey: this.config.apiKey });
23
+ return this._client;
21
24
  }
22
- async *process(input) {
25
+ async process(input) {
23
26
  const res = await this.client.chat.completions.create({
24
- model: this.config.model,
27
+ model: this.config?.model || CHAT_MODEL_OPENAI_DEFAULT_MODEL,
25
28
  temperature: input.modelOptions?.temperature,
26
29
  top_p: input.modelOptions?.topP,
27
30
  frequency_penalty: input.modelOptions?.frequencyPenalty,
@@ -40,31 +43,56 @@ class OpenaiLLMModel extends llm_model_1.LLMModel {
40
43
  : undefined,
41
44
  stream: true,
42
45
  });
46
+ let text = "";
43
47
  const toolCalls = [];
44
48
  for await (const chunk of res) {
45
49
  const choice = chunk.choices?.[0];
46
50
  if (choice?.delta.tool_calls?.length) {
47
51
  for (const call of choice.delta.tool_calls) {
48
- const tool = toolCalls[call.index] ?? { id: call.id || (0, nanoid_1.nanoid)() };
49
- toolCalls[call.index] = tool;
52
+ toolCalls[call.index] ??= {
53
+ id: call.id || (0, nanoid_1.nanoid)(),
54
+ type: "function",
55
+ function: { name: "", arguments: {} },
56
+ args: "",
57
+ };
58
+ const c = toolCalls[call.index];
59
+ if (!c)
60
+ throw new Error("Tool call not found");
50
61
  if (call.type)
51
- tool.type = call.type;
52
- tool.function ??= {};
53
- tool.function.name =
54
- (tool.function.name || "") + (call.function?.name || "");
55
- tool.function.arguments = (tool.function.arguments || "").concat(call.function?.arguments || "");
62
+ c.type = call.type;
63
+ c.function.name = c.function.name + (call.function?.name || "");
64
+ c.args = c.args.concat(call.function?.arguments || "");
56
65
  }
57
66
  }
58
67
  if (choice?.delta.content)
59
- yield { $text: choice.delta.content };
68
+ text += choice.delta.content;
69
+ }
70
+ const result = {};
71
+ if (input.responseFormat?.type === "json_schema") {
72
+ result.json = JSON.parse(text);
73
+ }
74
+ else {
75
+ result.text = text;
76
+ }
77
+ if (toolCalls.length) {
78
+ result.toolCalls = toolCalls.map(({ args, ...c }) => ({
79
+ ...c,
80
+ function: { ...c.function, arguments: JSON.parse(args) },
81
+ }));
60
82
  }
61
- yield { delta: { toolCalls } };
83
+ return result;
62
84
  }
63
85
  }
64
- exports.OpenaiLLMModel = OpenaiLLMModel;
86
+ exports.ChatModelOpenAI = ChatModelOpenAI;
87
+ const ROLE_MAP = {
88
+ system: "system",
89
+ user: "user",
90
+ agent: "assistant",
91
+ tool: "tool",
92
+ };
65
93
  async function contentsFromInputMessages(messages) {
66
94
  return messages.map((i) => ({
67
- role: i.role,
95
+ role: ROLE_MAP[i.role],
68
96
  content: typeof i.content === "string"
69
97
  ? i.content
70
98
  : i.content
@@ -75,12 +103,20 @@ async function contentsFromInputMessages(messages) {
75
103
  if (c.type === "image_url") {
76
104
  return {
77
105
  type: "image_url",
78
- image_url: { url: c.imageUrl.url },
106
+ image_url: { url: c.url },
79
107
  };
80
108
  }
81
109
  })
82
- .filter(is_non_nullable_1.isNonNullable),
110
+ .filter(type_utils_1.isNonNullable),
111
+ tool_calls: i.toolCalls?.map((i) => ({
112
+ ...i,
113
+ function: {
114
+ ...i.function,
115
+ arguments: JSON.stringify(i.function.arguments),
116
+ },
117
+ })),
83
118
  tool_call_id: i.toolCallId,
119
+ name: i.name,
84
120
  }));
85
121
  }
86
122
  function toolsFromInputTools(tools) {
@@ -105,18 +141,17 @@ function jsonSchemaToOpenAIJsonSchema(schema) {
105
141
  // NOTE: All fields must be required https://platform.openai.com/docs/guides/structured-outputs/all-fields-must-be-required
106
142
  return [
107
143
  key,
108
- required?.includes(key)
109
- ? valueSchema
110
- : { anyOf: [valueSchema, { type: ["null"] }] },
144
+ required?.includes(key) ? valueSchema : { anyOf: [valueSchema, { type: ["null"] }] },
111
145
  ];
112
146
  })),
113
147
  required: Object.keys(properties),
114
148
  };
115
149
  }
116
150
  if (schema?.type === "array") {
151
+ const { items } = schema;
117
152
  return {
118
153
  ...schema,
119
- items: jsonSchemaToOpenAIJsonSchema(schema.items),
154
+ items: jsonSchemaToOpenAIJsonSchema(items),
120
155
  };
121
156
  }
122
157
  return schema;
@@ -0,0 +1,78 @@
1
+ import { Agent, type AgentInput, type AgentOutput } from "../agents/agent";
2
+ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
3
+ constructor();
4
+ }
5
+ export interface ChatModelInput extends AgentInput {
6
+ messages: ChatModelInputMessage[];
7
+ responseFormat?: ChatModelInputResponseFormat;
8
+ tools?: ChatModelInputTool[];
9
+ toolChoice?: ChatModelInputToolChoice;
10
+ modelOptions?: ChatModelOptions;
11
+ }
12
+ export type Role = "system" | "user" | "agent" | "tool";
13
+ export interface ChatModelInputMessage {
14
+ role: Role;
15
+ content?: string | ({
16
+ type: "text";
17
+ text: string;
18
+ } | {
19
+ type: "image_url";
20
+ url: string;
21
+ })[];
22
+ toolCalls?: {
23
+ id: string;
24
+ type: "function";
25
+ function: {
26
+ name: string;
27
+ arguments: AgentInput;
28
+ };
29
+ }[];
30
+ toolCallId?: string;
31
+ name?: string;
32
+ }
33
+ export type ChatModelInputResponseFormat = {
34
+ type: "text";
35
+ } | {
36
+ type: "json_schema";
37
+ jsonSchema: {
38
+ name: string;
39
+ description?: string;
40
+ schema: Record<string, unknown>;
41
+ strict?: boolean;
42
+ };
43
+ };
44
+ export interface ChatModelInputTool {
45
+ type: "function";
46
+ function: {
47
+ name: string;
48
+ description?: string;
49
+ parameters: object;
50
+ };
51
+ }
52
+ export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
53
+ type: "function";
54
+ function: {
55
+ name: string;
56
+ description?: string;
57
+ };
58
+ };
59
+ export interface ChatModelOptions {
60
+ model?: string;
61
+ temperature?: number;
62
+ topP?: number;
63
+ frequencyPenalty?: number;
64
+ presencePenalty?: number;
65
+ }
66
+ export interface ChatModelOutput extends AgentOutput {
67
+ text?: string;
68
+ json?: object;
69
+ toolCalls?: ChatModelOutputToolCall[];
70
+ }
71
+ export interface ChatModelOutputToolCall {
72
+ id: string;
73
+ type: "function";
74
+ function: {
75
+ name: string;
76
+ arguments: AgentInput;
77
+ };
78
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChatModel = void 0;
4
+ const zod_1 = require("zod");
5
+ const agent_1 = require("../agents/agent");
6
+ class ChatModel extends agent_1.Agent {
7
+ constructor() {
8
+ super({
9
+ inputSchema: chatModelInputSchema,
10
+ outputSchema: chatModelOutputSchema,
11
+ });
12
+ }
13
+ }
14
+ exports.ChatModel = ChatModel;
15
+ const chatModelInputMessageSchema = zod_1.z.object({
16
+ role: zod_1.z.union([zod_1.z.literal("system"), zod_1.z.literal("user"), zod_1.z.literal("agent"), zod_1.z.literal("tool")]),
17
+ content: zod_1.z
18
+ .union([
19
+ zod_1.z.string(),
20
+ zod_1.z.array(zod_1.z.union([
21
+ zod_1.z.object({ type: zod_1.z.literal("text"), text: zod_1.z.string() }),
22
+ zod_1.z.object({ type: zod_1.z.literal("image_url"), url: zod_1.z.string() }),
23
+ ])),
24
+ ])
25
+ .optional(),
26
+ toolCalls: zod_1.z
27
+ .array(zod_1.z.object({
28
+ id: zod_1.z.string(),
29
+ type: zod_1.z.literal("function"),
30
+ function: zod_1.z.object({
31
+ name: zod_1.z.string(),
32
+ arguments: zod_1.z.record(zod_1.z.unknown()),
33
+ }),
34
+ }))
35
+ .optional(),
36
+ toolCallId: zod_1.z.string().optional(),
37
+ name: zod_1.z.string().optional(),
38
+ });
39
+ const chatModelInputResponseFormatSchema = zod_1.z.union([
40
+ zod_1.z.literal("text"),
41
+ zod_1.z.object({
42
+ type: zod_1.z.literal("json_schema"),
43
+ jsonSchema: zod_1.z.object({
44
+ name: zod_1.z.string(),
45
+ description: zod_1.z.string().optional(),
46
+ schema: zod_1.z.record(zod_1.z.unknown()),
47
+ strict: zod_1.z.boolean().optional(),
48
+ }),
49
+ }),
50
+ ]);
51
+ const chatModelInputToolSchema = zod_1.z.object({
52
+ type: zod_1.z.literal("function"),
53
+ function: zod_1.z.object({
54
+ name: zod_1.z.string(),
55
+ description: zod_1.z.string().optional(),
56
+ parameters: zod_1.z.record(zod_1.z.unknown()),
57
+ }),
58
+ });
59
+ const chatModelInputToolChoiceSchema = zod_1.z.union([
60
+ zod_1.z.literal("auto"),
61
+ zod_1.z.literal("none"),
62
+ zod_1.z.literal("required"),
63
+ chatModelInputToolSchema,
64
+ ]);
65
+ const chatModelOptionsSchema = zod_1.z.object({
66
+ model: zod_1.z.string().optional(),
67
+ temperature: zod_1.z.number().optional(),
68
+ topP: zod_1.z.number().optional(),
69
+ frequencyPenalty: zod_1.z.number().optional(),
70
+ presencePenalty: zod_1.z.number().optional(),
71
+ });
72
+ const chatModelInputSchema = zod_1.z.object({
73
+ messages: zod_1.z.array(chatModelInputMessageSchema),
74
+ responseFormat: chatModelInputResponseFormatSchema.optional(),
75
+ tools: zod_1.z.array(chatModelInputToolSchema).optional(),
76
+ toolChoice: chatModelInputToolChoiceSchema.optional(),
77
+ modelOptions: chatModelOptionsSchema.optional(),
78
+ });
79
+ const chatModelOutputToolCallSchema = zod_1.z.object({
80
+ id: zod_1.z.string(),
81
+ type: zod_1.z.literal("function"),
82
+ function: zod_1.z.object({
83
+ name: zod_1.z.string(),
84
+ arguments: zod_1.z.record(zod_1.z.unknown()),
85
+ }),
86
+ });
87
+ const chatModelOutputSchema = zod_1.z.object({
88
+ text: zod_1.z.string().optional(),
89
+ json: zod_1.z.unknown().optional(),
90
+ toolCalls: zod_1.z.array(chatModelOutputToolCallSchema).optional(),
91
+ });
@@ -0,0 +1,34 @@
1
+ import { Agent, type AgentInput } from "../agents/agent";
2
+ import type { AIAgent } from "../agents/ai-agent";
3
+ import type { Context } from "../execution-engine/context";
4
+ import type { ChatModel, ChatModelInput, ChatModelInputMessage } from "../models/chat";
5
+ export declare const USER_INPUT_MESSAGE_KEY = "$user_input_message";
6
+ export declare function userInput(message: string | object): AgentInput;
7
+ export declare function addMessagesToInput(input: AgentInput, messages: ChatModelInputMessage[]): AgentInput;
8
+ export interface PromptBuilderOptions {
9
+ instructions?: string;
10
+ }
11
+ export interface PromptBuilderBuildOptions {
12
+ context?: Context;
13
+ agent?: AIAgent;
14
+ input?: AgentInput;
15
+ model?: ChatModel;
16
+ }
17
+ export declare class PromptBuilder {
18
+ static from(instructions: string): PromptBuilder;
19
+ static from(instructions: {
20
+ path: string;
21
+ }): Promise<PromptBuilder>;
22
+ static from(instructions: string | {
23
+ path: string;
24
+ }): PromptBuilder | Promise<PromptBuilder>;
25
+ private static fromFile;
26
+ constructor(options?: PromptBuilderOptions);
27
+ instructions?: string;
28
+ build(options: PromptBuilderBuildOptions): Promise<ChatModelInput & {
29
+ toolAgents?: Agent[];
30
+ }>;
31
+ private buildMessages;
32
+ private buildResponseFormat;
33
+ private buildTools;
34
+ }
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PromptBuilder = exports.USER_INPUT_MESSAGE_KEY = void 0;
7
+ exports.userInput = userInput;
8
+ exports.addMessagesToInput = addMessagesToInput;
9
+ const promises_1 = require("node:fs/promises");
10
+ const zod_1 = require("zod");
11
+ const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
12
+ const agent_1 = require("../agents/agent");
13
+ const template_1 = require("./template");
14
+ exports.USER_INPUT_MESSAGE_KEY = "$user_input_message";
15
+ function userInput(message) {
16
+ return { [exports.USER_INPUT_MESSAGE_KEY]: message };
17
+ }
18
+ function addMessagesToInput(input, messages) {
19
+ const originalUserInputMessages = input[exports.USER_INPUT_MESSAGE_KEY];
20
+ const newMessages = [];
21
+ if (typeof originalUserInputMessages === "string") {
22
+ newMessages.push({ role: "user", content: originalUserInputMessages });
23
+ }
24
+ else {
25
+ const messages = (0, template_1.parseChatMessages)(originalUserInputMessages);
26
+ if (messages)
27
+ newMessages.push(...messages);
28
+ else
29
+ newMessages.push({
30
+ role: "user",
31
+ content: JSON.stringify(originalUserInputMessages),
32
+ });
33
+ }
34
+ newMessages.push(...messages);
35
+ return { ...input, [exports.USER_INPUT_MESSAGE_KEY]: newMessages };
36
+ }
37
+ class PromptBuilder {
38
+ static from(instructions) {
39
+ if (typeof instructions === "string") {
40
+ return new PromptBuilder({ instructions });
41
+ }
42
+ return PromptBuilder.fromFile(instructions.path);
43
+ }
44
+ static async fromFile(path) {
45
+ const text = await (0, promises_1.readFile)(path, "utf-8");
46
+ return PromptBuilder.from(text);
47
+ }
48
+ constructor(options) {
49
+ this.instructions = options?.instructions;
50
+ }
51
+ instructions;
52
+ async build(options) {
53
+ return {
54
+ messages: this.buildMessages(options),
55
+ responseFormat: this.buildResponseFormat(options),
56
+ ...this.buildTools(options),
57
+ };
58
+ }
59
+ buildMessages(options) {
60
+ const { input } = options;
61
+ const template = template_1.ChatMessagesTemplate.from([]);
62
+ if (this.instructions)
63
+ template.messages.push(template_1.SystemMessageTemplate.from(this.instructions));
64
+ const userInputMessage = input?.[exports.USER_INPUT_MESSAGE_KEY];
65
+ if (typeof userInputMessage === "string") {
66
+ template.messages.push(template_1.UserMessageTemplate.from(userInputMessage));
67
+ }
68
+ else if (userInputMessage) {
69
+ const messages = (0, template_1.parseChatMessages)(userInputMessage);
70
+ if (messages)
71
+ template.messages.push(...messages);
72
+ else
73
+ template.messages.push(template_1.UserMessageTemplate.from(JSON.stringify(userInputMessage)));
74
+ }
75
+ return template.format(input);
76
+ }
77
+ buildResponseFormat(options) {
78
+ const outputSchema = options.agent?.outputSchema;
79
+ if (!outputSchema)
80
+ return undefined;
81
+ const isJsonOutput = !isEmptyObjectType(outputSchema);
82
+ return isJsonOutput
83
+ ? {
84
+ type: "json_schema",
85
+ jsonSchema: {
86
+ name: "output",
87
+ schema: (0, zod_to_json_schema_1.default)(outputSchema),
88
+ strict: true,
89
+ },
90
+ }
91
+ : undefined;
92
+ }
93
+ buildTools(options) {
94
+ const toolAgents = (options.context?.tools ?? [])
95
+ .concat(options.agent?.tools ?? [])
96
+ // TODO: support nested tools?
97
+ .flatMap((i) => (i.isCallable ? i.tools.concat(i) : i.tools));
98
+ const tools = toolAgents.map((i) => ({
99
+ type: "function",
100
+ function: {
101
+ name: i.name,
102
+ description: i.description,
103
+ parameters: !isEmptyObjectType(i.inputSchema) ? (0, zod_to_json_schema_1.default)(i.inputSchema) : {},
104
+ },
105
+ }));
106
+ let toolChoice;
107
+ // use manual choice if configured in the agent
108
+ const manualChoice = options.agent?.toolChoice;
109
+ if (manualChoice) {
110
+ if (manualChoice instanceof agent_1.Agent) {
111
+ toolChoice = {
112
+ type: "function",
113
+ function: {
114
+ name: manualChoice.name,
115
+ description: manualChoice.description,
116
+ },
117
+ };
118
+ }
119
+ else if (manualChoice === "router") {
120
+ toolChoice = "required";
121
+ }
122
+ else {
123
+ toolChoice = manualChoice;
124
+ }
125
+ }
126
+ // otherwise, use auto choice if there is only one tool
127
+ else {
128
+ toolChoice = tools.length ? "auto" : undefined;
129
+ }
130
+ return {
131
+ toolAgents: toolAgents.length ? toolAgents : undefined,
132
+ tools: tools.length ? tools : undefined,
133
+ toolChoice,
134
+ };
135
+ }
136
+ }
137
+ exports.PromptBuilder = PromptBuilder;
138
+ function isEmptyObjectType(schema) {
139
+ return schema instanceof zod_1.ZodObject && Object.keys(schema.shape).length === 0;
140
+ }
@@ -0,0 +1,70 @@
1
+ import type { ChatModelInputMessage, ChatModelOutputToolCall } from "../models/chat";
2
+ export declare class PromptTemplate {
3
+ template: string;
4
+ static from(template: string): PromptTemplate;
5
+ constructor(template: string);
6
+ format(variables?: Record<string, unknown>): string;
7
+ }
8
+ export declare class ChatMessageTemplate {
9
+ role: "system" | "user" | "agent" | "tool";
10
+ content?: ChatModelInputMessage["content"];
11
+ name?: string | undefined;
12
+ constructor(role: "system" | "user" | "agent" | "tool", content?: ChatModelInputMessage["content"], name?: string | undefined);
13
+ format(variables?: Record<string, unknown>): ChatModelInputMessage;
14
+ }
15
+ export declare class SystemMessageTemplate extends ChatMessageTemplate {
16
+ static from(content: string, name?: string): SystemMessageTemplate;
17
+ }
18
+ export declare class UserMessageTemplate extends ChatMessageTemplate {
19
+ static from(template: NonNullable<ChatModelInputMessage["content"]>, name?: string): UserMessageTemplate;
20
+ }
21
+ export declare class AgentMessageTemplate extends ChatMessageTemplate {
22
+ toolCalls?: ChatModelOutputToolCall[] | undefined;
23
+ static from(template: string | ChatModelOutputToolCall[], name?: string): AgentMessageTemplate;
24
+ constructor(content?: string, toolCalls?: ChatModelOutputToolCall[] | undefined, name?: string);
25
+ format(variables?: Record<string, unknown>): {
26
+ toolCalls: ChatModelOutputToolCall[] | undefined;
27
+ role: import("../models/chat").Role;
28
+ content?: string | ({
29
+ type: "text";
30
+ text: string;
31
+ } | {
32
+ type: "image_url";
33
+ url: string;
34
+ })[];
35
+ toolCallId?: string;
36
+ name?: string;
37
+ };
38
+ }
39
+ export declare class ToolMessageTemplate extends ChatMessageTemplate {
40
+ toolCallId: string;
41
+ static from(content: object | string, toolCallId: string, name?: string): ToolMessageTemplate;
42
+ constructor(content: object | string, toolCallId: string, name?: string);
43
+ format(variables?: Record<string, unknown>): {
44
+ toolCallId: string;
45
+ role: import("../models/chat").Role;
46
+ content?: string | ({
47
+ type: "text";
48
+ text: string;
49
+ } | {
50
+ type: "image_url";
51
+ url: string;
52
+ })[];
53
+ toolCalls?: {
54
+ id: string;
55
+ type: "function";
56
+ function: {
57
+ name: string;
58
+ arguments: import("..").AgentInput;
59
+ };
60
+ }[];
61
+ name?: string;
62
+ };
63
+ }
64
+ export declare class ChatMessagesTemplate {
65
+ messages: ChatMessageTemplate[];
66
+ static from(messages: ChatMessageTemplate[] | string): ChatMessagesTemplate;
67
+ constructor(messages: ChatMessageTemplate[]);
68
+ format(variables?: Record<string, unknown>): ChatModelInputMessage[];
69
+ }
70
+ export declare function parseChatMessages(messages: unknown): ChatMessageTemplate[] | undefined;