aotrautils-srv 0.0.2041 → 0.0.2044

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- /*utils COMMONS library associated with aotra version : «1_29072022-2359 (18/10/2026-02:46:46)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (29/11/2026-08:00:13)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -5510,7 +5510,7 @@ window.AOTRAUTILS_LIB_IS_LOADED=true;
5510
5510
 
5511
5511
 
5512
5512
 
5513
- /*utils AI library associated with aotra version : «1_29072022-2359 (18/10/2026-02:46:46)»*/
5513
+ /*utils AI library associated with aotra version : «1_29072022-2359 (29/11/2026-08:00:13)»*/
5514
5514
  /*-----------------------------------------------------------------------------*/
5515
5515
 
5516
5516
 
@@ -5541,16 +5541,16 @@ if(typeof(window)==="undefined") window=global;
5541
5541
  //=========================================================================
5542
5542
  // GLOBAL CONSTANTS :
5543
5543
 
5544
- window.DEFAULT_LLM_PROVIDER_NAME="claude";
5544
+ window.DEFAULT_LLM_PROVIDER_NAME="custom";
5545
5545
 
5546
5546
  window.LLM_PROVIDERS_PARAMETERS={
5547
5547
  "openai":{
5548
- modelName:"gpt-3.5-turbo",
5549
- apiURL:"https://api.openai.com/v1/chat/completions"
5548
+ AIAPIModelName:"gpt-3.5-turbo",
5549
+ AIAPIServerURL:"https://api.openai.com/v1/chat/completions"
5550
5550
  },
5551
5551
  "claude":{
5552
- modelName:"claude-sonnet-4-8",
5553
- apiURL:"https://api.anthropic.com/v1/messages"
5552
+ AIAPIModelName:"claude-sonnet-4-8",
5553
+ AIAPIServerURL:"https://api.anthropic.com/v1/messages"
5554
5554
  },
5555
5555
  };
5556
5556
 
@@ -5568,18 +5568,14 @@ window.LLM_PROVIDERS_PARAMETERS={
5568
5568
 
5569
5569
  window.LLMAPIClient=class LLMAPIClient{
5570
5570
 
5571
- constructor(modelName, apiURL, agentRole, defaultPrompt){
5572
-
5573
- this.modelName=modelName;
5574
- this.apiURL=apiURL;
5571
+ constructor(agentRole, defaultPrompt){
5575
5572
  this.agentRole=agentRole;
5576
5573
  this.defaultPrompt=defaultPrompt;
5577
5574
  }
5578
5575
 
5579
- async getAnswer(AIAPIKey, userPrompt, defaultPromptParameters={})
5576
+ /*public*/async getAnswer(AIAPIConf, userPrompt, defaultPromptParameters={}, doOnEachChunkReceived=null)
5580
5577
  /*THROWS LLMTextGenerationError*/
5581
5578
  {
5582
-
5583
5579
  const PARAMETERS_DELIMITERS=["<",">"];
5584
5580
 
5585
5581
  let newDefaultPrompt=this.defaultPrompt;
@@ -5591,7 +5587,9 @@ window.LLMAPIClient=class LLMAPIClient{
5591
5587
  const messages=this.getMessages(newDefaultPrompt, userPrompt);
5592
5588
 
5593
5589
  try{
5594
- const result=await this.launchLLMRequest(AIAPIKey, messages);
5590
+ const result= (doOnEachChunkReceived ?
5591
+ await this.launchLLMRequestStreamed(AIAPIConf, messages, doOnEachChunkReceived)
5592
+ : await this.launchLLMRequest(AIAPIConf, messages));
5595
5593
  return result;
5596
5594
  }catch(llmTextGenerationError){
5597
5595
  // TRACE
@@ -5600,34 +5598,35 @@ window.LLMAPIClient=class LLMAPIClient{
5600
5598
  }
5601
5599
 
5602
5600
  }
5603
-
5601
+
5604
5602
 
5605
5603
  /*protected*/getMessages(defaultPrompt, userPrompt){
5606
5604
  return [
5607
5605
  { role: "system", content: this.agentRole },
5608
- { role: "user", content: (defaultPrompt+"\n"
5609
- +userPrompt) },
5606
+ { role: "user", content: (defaultPrompt+"\n"+userPrompt) },
5610
5607
  ];
5611
5608
  }
5612
5609
 
5610
+
5613
5611
  // DO NOT USE DIRECTLY !!!
5614
- /*private*/launchLLMRequest(AIAPIKey, messages){
5612
+ /*private*/launchLLMRequest(AIAPIConf, messages){
5615
5613
 
5616
5614
  const self=this;
5617
5615
 
5616
+ const headers=this.getAPIRequestHeaders(AIAPIConf);
5617
+ const parameters=this.getAPIRequestParameters(messages, AIAPIConf.AIAPIModelName);
5618
+ const AIAPIServerURL=(AIAPIConf && AIAPIConf.AIAPIServerURL ? AIAPIConf.AIAPIServerURL : this.AIAPIServerURL);
5618
5619
 
5619
- const headers=this.getAPIRequestHeaders(AIAPIKey);
5620
-
5621
- const parameters=this.getAPIRequestParameters(messages);
5622
-
5623
5620
  return new Promise((resolve,reject)=>{
5624
- performHTTPRequest(this.apiURL, "POST", headers, parameters, true).then((responseObj)=>{
5621
+ performHTTPRequest(AIAPIServerURL, "POST", headers, parameters, true).then((responseObj)=>{
5625
5622
  const responseData=responseObj.responseData;
5626
5623
  const data=responseData;
5627
5624
 
5628
5625
  // DBG
5629
- console.log("DEBUG : ~~~~~~~~~~~data :",data);
5630
-
5626
+ console.log("DEBUG : ~~~~~~~~~~~~~~~~~~~~~~");
5627
+ console.log("DEBUG : ~~~~~~~~~~~data :",data);
5628
+ console.log("DEBUG : ~~~~~~~~~~~~~~~~~~~~~~");
5629
+
5631
5630
  if(data.error){
5632
5631
  const error=data.error;
5633
5632
  reject(error);
@@ -5642,23 +5641,91 @@ window.LLMAPIClient=class LLMAPIClient{
5642
5641
  reject(error);
5643
5642
  });
5644
5643
  });
5644
+ }
5645
+
5646
+
5647
+ // DO NOT USE DIRECTLY !!!
5648
+ /*private*/async launchLLMRequestStreamed(AIAPIConf, messages, doOnEachChunk=null){
5649
+
5650
+ const self=this;
5651
+
5652
+ const headers=this.getAPIRequestHeaders(AIAPIConf);
5653
+ const parameters=this.getAPIRequestParameters(messages, AIAPIConf.AIAPIModelName, true);
5654
+ const AIAPIServerURL=(AIAPIConf && AIAPIConf.AIAPIServerURL ? AIAPIConf.AIAPIServerURL : this.AIAPIServerURL);
5655
+
5656
+
5657
+ const response=await fetch(AIAPIServerURL, {
5658
+ method: "POST",
5659
+ headers: headers,
5660
+ body: JSON.stringify(parameters)
5661
+ });
5662
+
5663
+
5664
+ if (!response.ok) {
5665
+ const errorText=await response.text();
5666
+ throw {
5667
+ errorMessage:errorText,
5668
+ httpStatus:response.status,
5669
+ error:new Error("ERROR : HTTP request error ${response.status}: ${errorText}")
5670
+ };
5671
+ }
5645
5672
 
5646
5673
 
5674
+ const reader=response.body.getReader();
5675
+ const decoder=new TextDecoder("utf-8");
5676
+
5677
+ let assistantReply="";
5678
+ for( let buffer="",chunk={} ; chunk.done ; chunk=await reader.read() ){
5679
+
5680
+ buffer+=decoder.decode(chunk.value, { stream: true });
5681
+
5682
+ const events=buffer.split("\n\n");
5683
+
5684
+ // (We keep last event in buffer for it may be incomplete)
5685
+ buffer=events.pop();
5686
+
5687
+ for(const event of events){
5688
+ const eventLine=event.trim();
5689
+
5690
+ // We ignore empty line events or SSE comments (starting with ":")
5691
+ if (!eventLine || !eventLine.startsWith("data: ")) continue;
5692
+ const dataStr=eventLine.slice(6); // We remove "data: " prefix
5693
+ if(dataStr==="[DONE]") return;
5694
+
5695
+ try{
5696
+ const parsedData=JSON.parse(dataStr);
5697
+ const assistantReplyForLine=self.getAssistantReply(parsedData);
5698
+
5699
+ assistantReply+=assistantReplyForLine;
5700
+ if(doOnEachChunk) doOnEachChunk(assistantReplyForLine);
5701
+ } catch (err) {
5702
+ throw {
5703
+ errorMessage:err,
5704
+ error:new Error("ERROR : Data \"${data}\" is not parsable JSON:${err}")
5705
+ };
5706
+ }
5707
+
5708
+ }
5709
+ }
5710
+
5711
+ return assistantReply;
5647
5712
  }
5648
5713
 
5649
5714
 
5650
- /*protected*/getAPIRequestHeaders(AIAPIKey){
5715
+ /*protected*/getAPIRequestHeaders(AIAPIConf){
5651
5716
  return {
5652
- "Authorization": "Bearer "+AIAPIKey,
5717
+ "Authorization": "Bearer "+AIAPIConf.AIAPIKey,
5653
5718
  "Content-Type": "application/json",
5654
5719
  };
5655
5720
  }
5656
5721
 
5657
- /*protected*/getAPIRequestParameters(messages){
5658
- return{
5659
- model:this.modelName,
5722
+ /*protected*/getAPIRequestParameters(messages, modelName, isStream){
5723
+ const result={
5724
+ model:modelName,
5660
5725
  messages:messages,
5661
5726
  };
5727
+ if(isStream) result.stream=true;
5728
+ return result;
5662
5729
  }
5663
5730
 
5664
5731
  /*protected*/getAssistantReply(data){
@@ -5677,33 +5744,32 @@ window.LLMAPIClient=class LLMAPIClient{
5677
5744
 
5678
5745
  window.ClaudeAPIClient=class ClaudeAPIClient extends LLMAPIClient{
5679
5746
 
5680
- constructor(modelName, apiURL, agentRole, defaultPrompt){
5681
- super(modelName, apiURL, agentRole, defaultPrompt);
5682
-
5747
+ constructor(agentRole, defaultPrompt){
5748
+ super(agentRole, defaultPrompt);
5683
5749
  }
5684
5750
 
5685
-
5686
- /*protected*/getAPIRequestHeaders(AIAPIKey){
5751
+ /*protected*/getAPIRequestHeaders(AIAPIConf){
5687
5752
  return {
5688
- "x-api-key": AIAPIKey,
5753
+ "x-api-key": AIAPIConf.AIAPIKey,
5689
5754
  "anthropic-version": "2023-06-01",
5690
5755
  "Content-Type": "application/json"
5691
5756
  };
5692
5757
  }
5693
5758
 
5694
- /*protected*/getAPIRequestParameters(messages){
5695
- return{
5696
- model:this.modelName,
5759
+ /*protected*/getAPIRequestParameters(messages, modelName, isStream){
5760
+ const result={
5761
+ model:modelName,
5697
5762
  messages:messages,
5698
5763
  "max_tokens": 2048,
5699
5764
  system:this.agentRole,
5700
5765
  };
5766
+ if(isStream) result.stream=true;
5767
+ return result;
5701
5768
  }
5702
5769
 
5703
5770
  /*protected*/getMessages(defaultPrompt, userPrompt){
5704
5771
  return [
5705
- { role: "user", content: (defaultPrompt+"\n"
5706
- +userPrompt) },
5772
+ { role: "user", content: (defaultPrompt+"\n"+userPrompt) },
5707
5773
  ];
5708
5774
  }
5709
5775
 
@@ -5722,30 +5788,31 @@ window.ClaudeAPIClient=class ClaudeAPIClient extends LLMAPIClient{
5722
5788
 
5723
5789
  window.OpenAIAPIClient=class OpenAIAPIClient extends LLMAPIClient{
5724
5790
 
5725
- constructor(modelName, apiURL, agentRole, defaultPrompt){
5726
- super(modelName, apiURL, agentRole, defaultPrompt);
5791
+ constructor(agentRole, defaultPrompt){
5792
+ super(agentRole, defaultPrompt);
5727
5793
 
5728
5794
  }
5729
5795
 
5730
- /*protected*/getAPIRequestHeaders(AIAPIKey){
5796
+ /*protected*/getAPIRequestHeaders(AIAPIConf){
5731
5797
  return {
5732
- "Authorization": "Bearer "+AIAPIKey,
5798
+ "Authorization": "Bearer "+AIAPIConf.AIAPIKey,
5733
5799
  "Content-Type": "application/json",
5734
5800
  };
5735
5801
  }
5736
5802
 
5737
- /*protected*/getAPIRequestParameters(messages){
5738
- return {
5739
- model:this.modelName,
5803
+ /*protected*/getAPIRequestParameters(messages, modelName, isStream){
5804
+ const result={
5805
+ model:modelName,
5740
5806
  messages:messages,
5741
5807
  };
5808
+ if(isStream) result.stream=true;
5809
+ return result;
5742
5810
  }
5743
5811
 
5744
5812
  /*protected*/getMessages(defaultPrompt, userPrompt){
5745
5813
  return [
5746
5814
  { role: "system", content: this.agentRole },
5747
- { role: "user", content: (defaultPrompt+"\n"
5748
- +userPrompt) },
5815
+ { role: "user", content: (defaultPrompt+"\n"+userPrompt) },
5749
5816
  ];
5750
5817
  }
5751
5818
 
@@ -5760,21 +5827,78 @@ window.OpenAIAPIClient=class OpenAIAPIClient extends LLMAPIClient{
5760
5827
 
5761
5828
 
5762
5829
 
5830
+ // Agents
5831
+
5832
+
5833
+
5834
+
5835
+ window.LLMAgent=class LLMAgent{
5836
+
5837
+
5838
+ constructor(agentRole, defaultPrompt){
5839
+ this.agentRole=agentRole;
5840
+ this.defaultPrompt=defaultPrompt;
5841
+
5842
+ this.aiClient=window.getLLMAPIClient(agentRole, defaultPrompt);
5843
+ }
5844
+
5845
+ async getAnswer(AIAPIConf, pageSummarizedDescription, promptParameters)
5846
+ /*THROWS LLMTextGenerationError*/
5847
+ {
5848
+ //EX.: {numberOfTests:numberOfTestsToGenerate}
5849
+ const answer=await this.aiClient.getAnswer(AIAPIConf, pageSummarizedDescription, promptParameters);
5850
+ return answer;
5851
+ }
5852
+
5853
+ }
5854
+
5855
+
5856
+
5857
+ // ****************************************************************
5858
+
5859
+ window.LLMAPIClientMultiProviders=class LLMAPIClientMultiProviders{
5860
+ constructor(agentRole, defaultPrompt){
5861
+ this.aiClients={
5862
+ "openai":new OpenAIAPIClient(agentRole, defaultPrompt),
5863
+ "claude":new ClaudeAPIClient(agentRole, defaultPrompt),
5864
+ "custom":new LLMAPIClient(agentRole, defaultPrompt)
5865
+ };
5866
+ }
5867
+
5868
+ async getAnswer(AIAPIConf, userPrompt, defaultPromptParameters={})
5869
+ /*THROWS LLMTextGenerationError*/
5870
+ {
5871
+ const AIAPIProviderName=nonull(AIAPIConf.AIAPIProviderName, DEFAULT_LLM_PROVIDER_NAME);
5872
+ const aiClient=this.aiClients[AIAPIProviderName]
5873
+ if(!aiClient){
5874
+ // TRACE
5875
+ lognow("ERROR : Unsupported provider «"+AIAPIProviderName+"» (must be one of «"+Object.keys(this.aiClients)+"»). Cannot get answer.");
5876
+ return null;
5877
+ }
5878
+
5879
+ const defaultConf=LLM_PROVIDERS_PARAMETERS[AIAPIProviderName];
5880
+ if(defaultConf){
5881
+ if(!AIAPIConf.AIAPIModelName) AIAPIConf.AIAPIModelName=defaultConf.AIAPIModelName;
5882
+ if(!AIAPIConf.AIAPIServerURL) AIAPIConf.AIAPIServerURL=defaultConf.AIAPIServerURL;
5883
+ }
5884
+
5885
+ return await aiClient.getAnswer(AIAPIConf, userPrompt, defaultPromptParameters);
5886
+ }
5887
+
5888
+ }
5763
5889
 
5764
5890
  // ****************************************************************
5765
5891
 
5766
5892
  // Nodejs compatibility :
5767
- window.getLLMAPIClient=(modelName, apiURL, agentRole, defaultPrompt, llmProviderName=DEFAULT_LLM_PROVIDER_NAME)=>{
5768
- if(llmProviderName=="openai")
5769
- return new OpenAIAPIClient(modelName, apiURL, agentRole, defaultPrompt);
5770
- return new ClaudeAPIClient(modelName, apiURL, agentRole, defaultPrompt);
5893
+ window.getLLMAPIClient=(agentRole, defaultPrompt)=>{
5894
+ return new LLMAPIClientMultiProviders(agentRole, defaultPrompt);
5771
5895
  };
5772
5896
 
5773
5897
 
5774
5898
 
5775
5899
 
5776
5900
 
5777
- /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (18/10/2026-02:46:46)»*/
5901
+ /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (29/11/2026-08:00:13)»*/
5778
5902
  /*-----------------------------------------------------------------------------*/
5779
5903
 
5780
5904
 
@@ -8135,7 +8259,7 @@ window.getConsoleParam=function(indexOrName=0, argsOffset=2/*arg 0 is «node» a
8135
8259
  }
8136
8260
 
8137
8261
 
8138
- window.getConsoleCLI=(doOnCommands={"makeSandiwch":()=>{}}, promptText="Enter command> ")=>{
8262
+ window.getConsoleCLI=(doOnCommands={"makeSandiwch":()=>{lognow("mkkaaay.... -_-'");}}, promptText="Enter command> ")=>{
8139
8263
 
8140
8264
  const readline = require("node:readline");
8141
8265
 
@@ -8158,7 +8282,17 @@ window.getConsoleCLI=(doOnCommands={"makeSandiwch":()=>{}}, promptText="Enter co
8158
8282
  console.log("Available commands: quit, help and :", Object.keys(doOnCommands));
8159
8283
  break;
8160
8284
  default:
8161
- if(doOnCommands) doOnCommands[input]();
8285
+ if(doOnCommands){
8286
+ let foundCommand=doOnCommands[input];
8287
+ if(foundCommand){
8288
+ foundCommand();
8289
+ }else{
8290
+ foundCommand=doOnCommands["*"];
8291
+ if(foundCommand){
8292
+ foundCommand(input);
8293
+ }
8294
+ }
8295
+ }
8162
8296
  break;
8163
8297
  }
8164
8298
 
@@ -9163,8 +9297,8 @@ window.performHTTPRequest=function(completeURL, httpMethod="GET", headers={}, re
9163
9297
  data.httpStatus=status;
9164
9298
  resolve(data);
9165
9299
  }).catch(error => {
9166
- // TRACE
9167
- console.error("Error:", error);
9300
+ // // TRACE
9301
+ // console.error("ERROR : Error:", error);
9168
9302
  reject({error:`${error}`, httpStatus:null});
9169
9303
  });
9170
9304
  });
@@ -9259,8 +9393,8 @@ window.performHTTPRequest=function(completeURL, httpMethod="GET", headers={}, re
9259
9393
 
9260
9394
  // Handle errors
9261
9395
  request.on("error", (error) => {
9262
- // TRACE
9263
- console.error("Error:", error);
9396
+ // // TRACE
9397
+ // console.error("ERROR : Error:", error);
9264
9398
  reject({error:`${error}`, httpStatus:null});
9265
9399
  });
9266
9400
 
@@ -10075,12 +10209,16 @@ window.NodeServerInstance=class NodeServerInstance{
10075
10209
  // Channel information is stored in exchanged data :
10076
10210
  let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
10077
10211
 
10078
- if(!clientSocketParam) return dataWrapped;
10212
+ if(!clientSocketParam){
10213
+ // TRACE
10214
+ lognow("(SERVER) WARN : clientSocketParam is null ! Not sending.");
10215
+ return dataWrapped;
10216
+ }
10079
10217
 
10080
- if(isString(clientSocketParam) && clientSocketParam=="*"){
10218
+ if(isString(clientSocketParam) && clientSocketParam=="*"){ // (Case send to all clients !)
10081
10219
 
10082
- // DBG
10083
- lognow("(SERVER) (server sends to all clients)");
10220
+ // TRACE
10221
+ lognow("(SERVER) INFO : Server sends to all clients.");
10084
10222
 
10085
10223
  let serverClients;
10086
10224
  // if(!window.WebsocketImplementation.useSocketIOImplementation)
@@ -10162,16 +10300,16 @@ window.NodeServerInstance=class NodeServerInstance{
10162
10300
  /*private*/sendError(error, clientSocketParam=null){
10163
10301
  // CAUTION ! NEVER DO THIS : clientSocketParam.send(...) ! (because it directly calls a single socket implementation !)
10164
10302
 
10165
- let errorMessageToSend=error;
10166
- if(contains(typeof(error),"Error")){
10167
- errorMessageToSend={
10168
- type: error.type,
10169
- httpStatus:error.httpStatus,
10170
- error:(isString(error) ?
10171
- {errorMessage:error}
10172
- : {errorMessage:error.errorMessage, cause:(error.cause?{message:error.cause.message}:null)})
10173
- };
10174
- }
10303
+ //let errorMessageToSend=error;
10304
+ // NO : if(contains(typeof(error),"Error")){
10305
+ const errorMessageToSend={
10306
+ type:error.type,
10307
+ httpStatus:error.httpStatus,
10308
+ error:(isString(error) ?
10309
+ {errorMessage:error}
10310
+ : {errorMessage:error.errorMessage, cause:(error.cause?{message:error.cause.message}:null)})
10311
+ };
10312
+ // }
10175
10313
 
10176
10314
  if(clientSocketParam){
10177
10315
  return this.send(ERRORS_CHANNEL_NAME, errorMessageToSend, null, clientSocketParam);
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.2041",
3
+ "version": "0.0.2044",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "aotrautils-srv",
9
- "version": "0.0.2041",
9
+ "version": "0.0.2044",
10
10
  "license": "HGPL-1.0",
11
11
  "dependencies": {
12
12
  "atob": "^2.1.2",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.2041",
3
+ "version": "0.0.2044",
4
4
  "main": "aotrautils-srv.build.js",
5
5
  "description": "A library for vanilla javascript utils (server-side) used in aotra javascript CMS",
6
6
  "author": "Jeremie Ratomposon <info@alqemia.com> (https://alqemia.com)",