utilitas 1999.1.61 → 1999.1.63

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/lib/alan.mjs CHANGED
@@ -780,10 +780,13 @@ const buildPrompts = async (model, input, options = {}) => {
780
780
  assert(!(
781
781
  options.reasoning && !model?.reasoning
782
782
  ), `This model does not support reasoning: ${options.model}`);
783
- let [systemPrompt, history, content, prompt, _system, _user, _assistant] = [
784
- null, null, input || ATTACHMENTS, null, // length hack: ATTACHMENTS
785
- { role: system }, { role: user }, { role: assistant },
786
- ];
783
+ let [
784
+ systemPrompt, history, content, prompt, _system, _model, _assistant,
785
+ _history,
786
+ ] = [
787
+ null, null, input, null, { role: system }, { role: MODEL },
788
+ { role: assistant }, null,
789
+ ];
787
790
  options.systemPrompt = options.systemPrompt || INSTRUCTIONS;
788
791
  options.attachments = (
789
792
  options.attachments?.length ? options.attachments : []
@@ -793,68 +796,71 @@ const buildPrompts = async (model, input, options = {}) => {
793
796
  switch (options.flavor) {
794
797
  case OPENAI:
795
798
  systemPrompt = buildGptMessage(options.systemPrompt, _system);
796
- prompt = buildGptMessage(content, options);
797
799
  break;
798
800
  case ANTHROPIC:
799
801
  systemPrompt = options.systemPrompt;
800
- prompt = buildClaudeMessage(content, { ...options, cache_control: true });
801
802
  break;
802
803
  case GEMINI:
803
804
  const _role = {
804
- role: [GEMINI_20_FLASH].includes(options.model)
805
- ? user : system
805
+ role: [GEMINI_20_FLASH].includes(options.model) ? user : system
806
806
  };
807
807
  systemPrompt = buildGeminiHistory(options.systemPrompt, _role);
808
- prompt = options.toolsResult?.[options.toolsResult?.length - 1]?.parts
809
- || buildGeminiMessage(content, options)
810
808
  break;
811
809
  }
812
810
  const msgBuilder = () => {
813
- history = [];
811
+ [history, _history] = [[], []];
814
812
  (options.messages?.length ? options.messages : []).map((x, i) => {
815
813
  switch (options.flavor) {
816
814
  case OPENAI:
817
- history.push(buildGptMessage(x.request, _user));
818
- history.push(buildGptMessage(x.response, _assistant));
815
+ _history.push(buildGptMessage(x.request));
816
+ _history.push(buildGptMessage(x.response, _assistant));
819
817
  break;
820
818
  case ANTHROPIC:
821
- history.push(buildClaudeMessage(x.request, _user));
822
- history.push(buildClaudeMessage(x.response, _assistant));
819
+ _history.push(buildClaudeMessage(x.request));
820
+ _history.push(buildClaudeMessage(x.response, _assistant));
823
821
  break;
824
822
  case GEMINI:
825
823
  // https://github.com/google/generative-ai-js/blob/main/samples/node/advanced-chat.js
826
824
  // Google's bug: history is not allowed while using inline_data?
827
825
  if (options.attachments?.length) { return; }
828
- history.push(buildGeminiHistory(x.request, _user));
829
- history.push(buildGeminiHistory(x.response, { role: MODEL }));
826
+ _history.push(buildGeminiHistory(x.request));
827
+ _history.push(buildGeminiHistory(x.response, _model));
830
828
  break;
831
829
  }
832
830
  });
833
831
  switch (options.flavor) {
834
832
  case OPENAI:
835
833
  history = messages([
836
- systemPrompt, ...history, prompt,
834
+ systemPrompt, ..._history, buildGptMessage(content, options),
837
835
  ...options.toolsResult?.length ? options.toolsResult : []
838
836
  ]);
839
837
  break;
840
838
  case ANTHROPIC:
841
839
  history = messages([
842
- ...history, prompt,
843
- ...options.toolsResult?.length ? options.toolsResult : []
840
+ ..._history, buildClaudeMessage(content, {
841
+ ...options, cache_control: true
842
+ }), ...options.toolsResult?.length ? options.toolsResult : []
844
843
  ]);
845
844
  break;
846
845
  case GEMINI:
847
- history.push(
848
- ...options.toolsResult?.length ? [
849
- buildGeminiHistory(content, { ...options, role: user }),
850
- ...options.toolsResult.slice(0, options.toolsResult.length - 1)
851
- ] : []
852
- );
846
+ [history, prompt] = options.toolsResult?.length ? [
847
+ [
848
+ ..._history,
849
+ buildGeminiHistory(content, options),
850
+ ...options.toolsResult.slice(0, options.toolsResult.length - 1),
851
+ ],
852
+ options.toolsResult[options.toolsResult?.length - 1].parts,
853
+ ] : [
854
+ [..._history],
855
+ buildGeminiMessage(content, options),
856
+ ];
853
857
  break;
854
858
  }
855
859
  };
856
860
  msgBuilder();
857
- await trimPrompt(() => [systemPrompt, history, content], () => {
861
+ await trimPrompt(() => [
862
+ systemPrompt, _history, content, options.toolsResult
863
+ ], () => {
858
864
  if (options.messages?.length) {
859
865
  options.messages?.shift();
860
866
  msgBuilder();
@@ -973,7 +979,7 @@ const promptOpenAI = async (aiId, content, options = {}) => {
973
979
  const { history }
974
980
  = await buildPrompts(MODELS[options.model], content, options);
975
981
  model = MODELS[options.model];
976
- model.reasoning && !azure && !options.reasoning_effort
982
+ model?.reasoning && !azure && !options.reasoning_effort
977
983
  && (options.reasoning_effort = GPT_REASONING_EFFORT);
978
984
  const modalities = options.modalities
979
985
  || (options.audioMode ? [TEXT, AUDIO] : undefined);
@@ -985,7 +991,7 @@ const promptOpenAI = async (aiId, content, options = {}) => {
985
991
  modalities, audio: options.audio || (
986
992
  modalities?.find?.(x => x === AUDIO)
987
993
  && { voice: DEFAULT_MODELS[OPENAI_VOICE], format: 'pcm16' }
988
- ), ...model.tools && !azure ? {
994
+ ), ...model?.tools && !azure ? {
989
995
  tools: options.tools ?? (await toolsOpenAI()).map(x => x.def),
990
996
  tool_choice: 'auto',
991
997
  } : {}, ...azure ? {} : { store: true }, stream: true,
@@ -1384,7 +1390,7 @@ const resetSession = async (sessionId, options) => {
1384
1390
 
1385
1391
  const talk = async (input, options = {}) => {
1386
1392
  let [chat, sessionId] =
1387
- [{ request: input || ATTACHMENTS }, options.sessionId || newSessionId()];
1393
+ [{ request: input }, options.sessionId || newSessionId()];
1388
1394
  const session = await getSession(sessionId, options);
1389
1395
  const resp = await prompt(input, {
1390
1396
  messages: session.messages, log: true, ...options,
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1999.1.61",
4
+ "version": "1999.1.63",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1999.1.61",
4
+ "version": "1999.1.63",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",