@ax-llm/ax 12.0.5 → 12.0.6

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.
package/index.d.cts CHANGED
@@ -686,7 +686,7 @@ type AxAIOpenAIChatRequest<TModel> = {
686
686
  } | {
687
687
  role: 'user';
688
688
  content: string | ({
689
- type: 'text';
689
+ type: string;
690
690
  text: string;
691
691
  } | {
692
692
  type: 'image_url';
@@ -700,11 +700,20 @@ type AxAIOpenAIChatRequest<TModel> = {
700
700
  data: string;
701
701
  format?: 'wav';
702
702
  };
703
+ } | {
704
+ type: 'file';
705
+ file: {
706
+ file_data: string;
707
+ filename: string;
708
+ };
703
709
  })[];
704
710
  name?: string;
705
711
  } | {
706
712
  role: 'assistant';
707
- content: string;
713
+ content: string | {
714
+ type: string;
715
+ text: string;
716
+ };
708
717
  name?: string;
709
718
  tool_calls?: {
710
719
  type: 'function';
@@ -2335,6 +2344,7 @@ declare class AxPromptTemplate {
2335
2344
  private readonly thoughtFieldName;
2336
2345
  private readonly functions?;
2337
2346
  constructor(sig: Readonly<AxSignature>, options?: Readonly<AxPromptTemplateOptions>, fieldTemplates?: Record<string, AxFieldTemplateFn>);
2347
+ private renderSingleValueUserContent;
2338
2348
  render: <T extends AxGenIn>(values: T | ReadonlyArray<AxMessage<T>>, // Allow T (AxGenIn) or array of AxMessages
2339
2349
  { examples, demos, }: Readonly<{
2340
2350
  skipSystemPrompt?: boolean;
package/index.d.ts CHANGED
@@ -686,7 +686,7 @@ type AxAIOpenAIChatRequest<TModel> = {
686
686
  } | {
687
687
  role: 'user';
688
688
  content: string | ({
689
- type: 'text';
689
+ type: string;
690
690
  text: string;
691
691
  } | {
692
692
  type: 'image_url';
@@ -700,11 +700,20 @@ type AxAIOpenAIChatRequest<TModel> = {
700
700
  data: string;
701
701
  format?: 'wav';
702
702
  };
703
+ } | {
704
+ type: 'file';
705
+ file: {
706
+ file_data: string;
707
+ filename: string;
708
+ };
703
709
  })[];
704
710
  name?: string;
705
711
  } | {
706
712
  role: 'assistant';
707
- content: string;
713
+ content: string | {
714
+ type: string;
715
+ text: string;
716
+ };
708
717
  name?: string;
709
718
  tool_calls?: {
710
719
  type: 'function';
@@ -2335,6 +2344,7 @@ declare class AxPromptTemplate {
2335
2344
  private readonly thoughtFieldName;
2336
2345
  private readonly functions?;
2337
2346
  constructor(sig: Readonly<AxSignature>, options?: Readonly<AxPromptTemplateOptions>, fieldTemplates?: Record<string, AxFieldTemplateFn>);
2347
+ private renderSingleValueUserContent;
2338
2348
  render: <T extends AxGenIn>(values: T | ReadonlyArray<AxMessage<T>>, // Allow T (AxGenIn) or array of AxMessages
2339
2349
  { examples, demos, }: Readonly<{
2340
2350
  skipSystemPrompt?: boolean;
package/index.js CHANGED
@@ -3328,7 +3328,7 @@ var AxAIGoogleGeminiImpl = class {
3328
3328
  }
3329
3329
  ];
3330
3330
  return {
3331
- role: "model",
3331
+ role: "user",
3332
3332
  parts
3333
3333
  };
3334
3334
  }
@@ -4051,8 +4051,11 @@ var AxAIMistral = class extends AxAIOpenAIBase {
4051
4051
  for (const message of messages) {
4052
4052
  if (message.role === "user" && Array.isArray(message.content)) {
4053
4053
  const contentUpdated = message.content.map((item) => {
4054
- if (typeof item === "object" && item !== null && item.type === "image_url") {
4055
- return { type: "image_url", image_url: item.image_url?.url };
4054
+ if (typeof item === "object" && item !== null && "image_url" in item) {
4055
+ return {
4056
+ type: "image_url",
4057
+ image_url: { url: item.image_url?.url }
4058
+ };
4056
4059
  }
4057
4060
  return item;
4058
4061
  });
@@ -4725,8 +4728,6 @@ var AxAIOpenAIResponsesImpl = class {
4725
4728
  baseResult.content = event.delta;
4726
4729
  break;
4727
4730
  case "response.output_text.done":
4728
- baseResult.id = event.item_id;
4729
- baseResult.content = event.text;
4730
4731
  break;
4731
4732
  case "response.function_call_arguments.delta":
4732
4733
  baseResult.id = event.item_id;
@@ -5549,9 +5550,6 @@ var MemoryImpl = class {
5549
5550
  functionCalls
5550
5551
  }) {
5551
5552
  const isContentEmpty = typeof content === "string" && content.trim() === "";
5552
- if (isContentEmpty && (!functionCalls || functionCalls.length === 0)) {
5553
- return;
5554
- }
5555
5553
  if (isContentEmpty) {
5556
5554
  this.addMemory({ name, role: "assistant", functionCalls });
5557
5555
  } else {
@@ -5576,17 +5574,16 @@ var MemoryImpl = class {
5576
5574
  }) {
5577
5575
  const lastItem = this.data.at(-1);
5578
5576
  if (!lastItem || lastItem.chat.role !== "assistant") {
5579
- this.addResultMessage({ content, name, functionCalls });
5580
- } else {
5581
- if ("content" in lastItem.chat && typeof content === "string" && content.trim() !== "") {
5582
- lastItem.chat.content = content;
5583
- }
5584
- if ("name" in lastItem.chat && name) {
5585
- lastItem.chat.name = name;
5586
- }
5587
- if ("functionCalls" in lastItem.chat && functionCalls) {
5588
- lastItem.chat.functionCalls = functionCalls;
5589
- }
5577
+ throw new Error("No assistant message to update");
5578
+ }
5579
+ if (typeof content === "string" && content.trim() !== "") {
5580
+ lastItem.chat.content = content;
5581
+ }
5582
+ if (name && name.trim() !== "") {
5583
+ lastItem.chat.name = name;
5584
+ }
5585
+ if (functionCalls && functionCalls.length > 0) {
5586
+ lastItem.chat.functionCalls = functionCalls;
5590
5587
  }
5591
5588
  if (this.options?.debug) {
5592
5589
  if (delta && typeof delta === "string") {
@@ -6116,6 +6113,12 @@ ${outputFields}`);
6116
6113
  text: task.join("\n\n")
6117
6114
  };
6118
6115
  }
6116
+ renderSingleValueUserContent = (values, renderedExamples, renderedDemos, examplesInSystemPrompt) => {
6117
+ const completion = this.renderInputFields(values);
6118
+ const promptList = examplesInSystemPrompt ? completion : [...renderedExamples, ...renderedDemos, ...completion];
6119
+ const prompt = promptList.filter((v) => v !== void 0);
6120
+ return prompt.every((v) => v.type === "text") ? prompt.map((v) => v.text).join("\n") : prompt.reduce(combineConsecutiveStrings("\n"), []);
6121
+ };
6119
6122
  render = (values, {
6120
6123
  examples,
6121
6124
  demos
@@ -6144,60 +6147,49 @@ ${outputFields}`);
6144
6147
  role: "system",
6145
6148
  content: systemContent
6146
6149
  };
6147
- let userMessages = [];
6148
6150
  if (Array.isArray(values)) {
6151
+ let userMessages = [];
6149
6152
  const history = values;
6150
- let lastRole = void 0;
6151
- for (const message of history) {
6152
- let messageContent = "";
6153
- if (message.role === "user") {
6154
- const userMsgParts = this.renderInputFields(
6155
- message.values
6156
- // Cast message.values (AxGenIn) to T (which extends AxGenIn)
6153
+ for (const [index, message] of history.entries()) {
6154
+ let content;
6155
+ if (index === 0) {
6156
+ content = this.renderSingleValueUserContent(
6157
+ message.values,
6158
+ renderedExamples,
6159
+ renderedDemos,
6160
+ examplesInSystemPrompt
6157
6161
  );
6158
- messageContent = userMsgParts.map((part) => part.type === "text" ? part.text : "").join("").trim();
6159
- } else if (message.role === "assistant") {
6160
- const assistantMsgParts = this.renderInputFields(
6161
- message.values
6162
- // Cast message.values (AxGenIn) to T (which extends AxGenIn)
6162
+ } else {
6163
+ content = this.renderSingleValueUserContent(
6164
+ message.values,
6165
+ [],
6166
+ [],
6167
+ false
6163
6168
  );
6164
- messageContent = assistantMsgParts.map((part) => part.type === "text" ? part.text : "").join("").trim();
6165
6169
  }
6166
- if (messageContent) {
6167
- if (lastRole === message.role && userMessages.length > 0) {
6168
- const lastMessage = userMessages[userMessages.length - 1];
6169
- if (lastMessage) {
6170
- lastMessage.content += "\n" + messageContent;
6171
- }
6172
- } else {
6173
- if (message.role === "user") {
6174
- userMessages.push({ role: "user", content: messageContent });
6175
- } else if (message.role === "assistant") {
6176
- userMessages.push({ role: "assistant", content: messageContent });
6177
- }
6178
- }
6179
- lastRole = message.role;
6170
+ if (message.role === "user") {
6171
+ userMessages.push({ role: "user", content });
6172
+ continue;
6180
6173
  }
6174
+ if (message.role !== "assistant") {
6175
+ throw new Error("Invalid message role");
6176
+ }
6177
+ if (typeof content !== "string") {
6178
+ throw new Error(
6179
+ "Assistant message cannot contain non-text content like images, files,etc"
6180
+ );
6181
+ }
6182
+ userMessages.push({ role: "assistant", content });
6181
6183
  }
6182
- } else {
6183
- const currentValues = values;
6184
- const completion = this.renderInputFields(currentValues);
6185
- const promptList = examplesInSystemPrompt ? completion : [...renderedExamples, ...renderedDemos, ...completion];
6186
- const promptFilter = promptList.filter((v) => v !== void 0);
6187
- let userContent;
6188
- if (promptFilter.every((v) => v.type === "text")) {
6189
- userContent = promptFilter.map((v) => v.text).join("\n");
6190
- } else {
6191
- userContent = promptFilter.map((part) => {
6192
- if (part.type === "text") return part.text;
6193
- if (part.type === "image") return "[IMAGE]";
6194
- if (part.type === "audio") return "[AUDIO]";
6195
- return "";
6196
- }).join("\n").trim();
6197
- }
6198
- userMessages.push({ role: "user", content: userContent });
6184
+ return [systemPrompt, ...userMessages];
6199
6185
  }
6200
- return [systemPrompt, ...userMessages];
6186
+ const userContent = this.renderSingleValueUserContent(
6187
+ values,
6188
+ renderedExamples,
6189
+ renderedDemos,
6190
+ examplesInSystemPrompt
6191
+ );
6192
+ return [systemPrompt, { role: "user", content: userContent }];
6201
6193
  };
6202
6194
  renderExtraFields = (extraFields) => {
6203
6195
  const prompt = [];
@@ -6264,9 +6256,6 @@ ${outputFields}`);
6264
6256
  if ("text" in v) {
6265
6257
  v.text = v.text + "\n";
6266
6258
  }
6267
- if ("image" in v) {
6268
- v.image = v.image;
6269
- }
6270
6259
  list.push(v);
6271
6260
  });
6272
6261
  }
@@ -6297,9 +6286,6 @@ ${outputFields}`);
6297
6286
  if ("text" in v) {
6298
6287
  v.text = v.text + "\n";
6299
6288
  }
6300
- if ("image" in v) {
6301
- v.image = v.image;
6302
- }
6303
6289
  list.push(v);
6304
6290
  });
6305
6291
  }
@@ -8950,6 +8936,14 @@ var AxGen = class extends AxProgramWithSignature {
8950
8936
  s: -1
8951
8937
  };
8952
8938
  let content = "";
8939
+ mem.addResult(
8940
+ {
8941
+ content: "",
8942
+ name: "initial",
8943
+ functionCalls: []
8944
+ },
8945
+ sessionId
8946
+ );
8953
8947
  for await (const v of res) {
8954
8948
  const result = v.results[0];
8955
8949
  if (!result) {