@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
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,54 @@ 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];
50
59
  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 || "");
60
+ c.type = call.type;
61
+ c.function.name = c.function.name + (call.function?.name || "");
62
+ c.args = c.args.concat(call.function?.arguments || "");
56
63
  }
57
64
  }
58
65
  if (choice?.delta.content)
59
- yield { $text: choice.delta.content };
66
+ text += choice.delta.content;
67
+ }
68
+ const result = {};
69
+ if (input.responseFormat?.type === "json_schema") {
70
+ result.json = JSON.parse(text);
71
+ }
72
+ else {
73
+ result.text = text;
74
+ }
75
+ if (toolCalls.length) {
76
+ result.toolCalls = toolCalls.map(({ args, ...c }) => ({
77
+ ...c,
78
+ function: { ...c.function, arguments: JSON.parse(args) },
79
+ }));
60
80
  }
61
- yield { delta: { toolCalls } };
81
+ return result;
62
82
  }
63
83
  }
64
- exports.OpenaiLLMModel = OpenaiLLMModel;
84
+ exports.ChatModelOpenAI = ChatModelOpenAI;
85
+ const ROLE_MAP = {
86
+ system: "system",
87
+ user: "user",
88
+ agent: "assistant",
89
+ tool: "tool",
90
+ };
65
91
  async function contentsFromInputMessages(messages) {
66
92
  return messages.map((i) => ({
67
- role: i.role,
93
+ role: ROLE_MAP[i.role],
68
94
  content: typeof i.content === "string"
69
95
  ? i.content
70
96
  : i.content
@@ -75,12 +101,20 @@ async function contentsFromInputMessages(messages) {
75
101
  if (c.type === "image_url") {
76
102
  return {
77
103
  type: "image_url",
78
- image_url: { url: c.imageUrl.url },
104
+ image_url: { url: c.url },
79
105
  };
80
106
  }
81
107
  })
82
- .filter(is_non_nullable_1.isNonNullable),
108
+ .filter(type_utils_1.isNonNullable),
109
+ tool_calls: i.toolCalls?.map((i) => ({
110
+ ...i,
111
+ function: {
112
+ ...i.function,
113
+ arguments: JSON.stringify(i.function.arguments),
114
+ },
115
+ })),
83
116
  tool_call_id: i.toolCallId,
117
+ name: i.name,
84
118
  }));
85
119
  }
86
120
  function toolsFromInputTools(tools) {
@@ -105,9 +139,7 @@ function jsonSchemaToOpenAIJsonSchema(schema) {
105
139
  // NOTE: All fields must be required https://platform.openai.com/docs/guides/structured-outputs/all-fields-must-be-required
106
140
  return [
107
141
  key,
108
- required?.includes(key)
109
- ? valueSchema
110
- : { anyOf: [valueSchema, { type: ["null"] }] },
142
+ required?.includes(key) ? valueSchema : { anyOf: [valueSchema, { type: ["null"] }] },
111
143
  ];
112
144
  })),
113
145
  required: Object.keys(properties),
@@ -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: object;
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,25 @@
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): {
7
+ $user_input_message: string | object;
8
+ };
9
+ export declare function addMessagesToInput(input: AgentInput, messages: ChatModelInputMessage[]): {
10
+ $user_input_message: ChatModelInputMessage[];
11
+ };
12
+ export interface PromptBuilderBuildOptions {
13
+ context?: Context;
14
+ agent: AIAgent;
15
+ input: AgentInput;
16
+ model?: ChatModel;
17
+ }
18
+ export declare class PromptBuilder {
19
+ build(options: PromptBuilderBuildOptions): Promise<ChatModelInput & {
20
+ toolAgents: Agent[];
21
+ }>;
22
+ private buildMessages;
23
+ private buildResponseFormat;
24
+ private buildTools;
25
+ }
@@ -0,0 +1,126 @@
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 zod_1 = require("zod");
10
+ const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema"));
11
+ const agent_1 = require("../agents/agent");
12
+ const template_1 = require("./template");
13
+ const instructions_1 = require("./templates/instructions");
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
+ async build(options) {
39
+ return {
40
+ messages: this.buildMessages(options),
41
+ responseFormat: this.buildResponseFormat(options),
42
+ ...this.buildTools(options),
43
+ };
44
+ }
45
+ buildMessages(options) {
46
+ const { agent, input } = options;
47
+ const template = template_1.ChatMessagesTemplate.from([]);
48
+ template.messages.push(template_1.SystemMessageTemplate.from(template_1.PromptTemplate.from(instructions_1.DEFAULT_INSTRUCTIONS_TEMPLATE).format({
49
+ name: agent.name,
50
+ instructions: agent.instructions,
51
+ })));
52
+ const userInputMessage = input[exports.USER_INPUT_MESSAGE_KEY];
53
+ if (typeof userInputMessage === "string") {
54
+ template.messages.push(template_1.UserMessageTemplate.from(userInputMessage));
55
+ }
56
+ else if (userInputMessage) {
57
+ const messages = (0, template_1.parseChatMessages)(userInputMessage);
58
+ if (messages)
59
+ template.messages.push(...messages);
60
+ else
61
+ template.messages.push(template_1.UserMessageTemplate.from(JSON.stringify(userInputMessage)));
62
+ }
63
+ return template.format(input);
64
+ }
65
+ buildResponseFormat(options) {
66
+ const { outputSchema } = options.agent;
67
+ const isJsonOutput = !isEmptyObjectType(outputSchema);
68
+ return isJsonOutput
69
+ ? {
70
+ type: "json_schema",
71
+ jsonSchema: {
72
+ name: "output",
73
+ schema: (0, zod_to_json_schema_1.default)(outputSchema),
74
+ strict: true,
75
+ },
76
+ }
77
+ : undefined;
78
+ }
79
+ buildTools(options) {
80
+ const toolAgents = options.agent.tools
81
+ .concat(options.context?.tools ?? [])
82
+ // TODO: support nested tools in the agent skills?
83
+ .flatMap((i) => (i.isCallable ? i.skills.concat(i) : i.skills));
84
+ const tools = toolAgents.map((i) => ({
85
+ type: "function",
86
+ function: {
87
+ name: i.name,
88
+ description: i.description,
89
+ parameters: !isEmptyObjectType(i.inputSchema) ? (0, zod_to_json_schema_1.default)(i.inputSchema) : {},
90
+ },
91
+ }));
92
+ let toolChoice;
93
+ // use manual choice if configured in the agent
94
+ const { toolChoice: manualChoice } = options.agent;
95
+ if (manualChoice) {
96
+ if (manualChoice instanceof agent_1.Agent) {
97
+ toolChoice = {
98
+ type: "function",
99
+ function: {
100
+ name: manualChoice.name,
101
+ description: manualChoice.description,
102
+ },
103
+ };
104
+ }
105
+ else if (manualChoice === "router") {
106
+ toolChoice = "required";
107
+ }
108
+ else {
109
+ toolChoice = manualChoice;
110
+ }
111
+ }
112
+ // otherwise, use auto choice if there is only one tool
113
+ else {
114
+ toolChoice = tools.length ? "auto" : undefined;
115
+ }
116
+ return {
117
+ toolAgents,
118
+ tools,
119
+ toolChoice,
120
+ };
121
+ }
122
+ }
123
+ exports.PromptBuilder = PromptBuilder;
124
+ function isEmptyObjectType(schema) {
125
+ return schema instanceof zod_1.ZodObject && Object.keys(schema.shape).length === 0;
126
+ }
@@ -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;