wingbot 3.76.4-alpha.1 → 3.76.4-alpha.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot",
3
- "version": "3.76.4-alpha.1",
3
+ "version": "3.76.4-alpha.3",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/src/ChatGpt.js CHANGED
@@ -441,6 +441,7 @@ class ChatGpt {
441
441
 
442
442
  this._log('#GPT request', body);
443
443
 
444
+ const startTime = Date.now();
444
445
  const response = await this._fetch(apiUrl, {
445
446
  method: 'POST',
446
447
  headers: {
@@ -470,7 +471,9 @@ class ChatGpt {
470
471
 
471
472
  const [choice] = responseData.choices;
472
473
 
473
- this._log('#GPT response', { choice, responseData });
474
+ const durationMs = Date.now() - startTime;
475
+
476
+ this._log('#GPT response', { choice, responseData, durationMs });
474
477
 
475
478
  return choice;
476
479
  } catch (e) {
package/src/LLMSession.js CHANGED
@@ -170,6 +170,10 @@ const stateData = require('./utils/stateData');
170
170
  * @prop {LLMCallPreset} [preset]
171
171
  */
172
172
 
173
+ // max number of consecutive tool-call rounds resolved within a single generate()
174
+ // before we force a tool-less final answer (guards against tool-call loops)
175
+ const MAX_TOOL_CALL_ROUNDS = 5;
176
+
173
177
  /**
174
178
  * @class LLMSession
175
179
  * @implements {PromiseLike<LLMMessage<any>>}
@@ -970,7 +974,11 @@ class LLMSession {
970
974
  async _generate (providerOptions = this._preset, logOptions = {}) {
971
975
  let result = await this._llm.generate(this, providerOptions, logOptions);
972
976
 
973
- if (result.toolCalls?.length) {
977
+ // the model may chain several rounds of tool calls before it produces
978
+ // a final text answer - keep resolving them until it stops (bounded)
979
+ let rounds = 0;
980
+ while (result.toolCalls?.length && rounds < MAX_TOOL_CALL_ROUNDS) {
981
+ rounds += 1;
974
982
  const toolCalls = [];
975
983
  const results = await Promise.all(
976
984
  result.toolCalls.map(async (tc) => {
@@ -992,27 +1000,54 @@ class LLMSession {
992
1000
  );
993
1001
  result = await this._llm.generate(this, providerOptions, logOptions);
994
1002
  } else {
995
- // everything failed
996
- /** @type {LLMCallPreset} */
997
- const overrideChoice = typeof providerOptions === 'string'
998
- ? {
999
- preset: providerOptions,
1000
- toolChoice: 'none'
1001
- }
1002
- : {
1003
- ...providerOptions,
1004
- toolChoice: 'none'
1005
- };
1006
- result = await this._llm.generate(this, overrideChoice, logOptions);
1003
+ // everything failed - force a final text answer without tools
1004
+ this._llm.log.error(
1005
+ `LLMSession: all ${result.toolCalls.length} tool call(s) failed in round ${rounds}, `
1006
+ + 'forcing a tool-less final answer',
1007
+ { toolCalls: result.toolCalls }
1008
+ );
1009
+ result = await this._generateWithoutTools(providerOptions, logOptions);
1010
+ break;
1007
1011
  }
1008
1012
  }
1009
1013
 
1014
+ // safety net: if the model is still requesting tools (e.g. it hit the
1015
+ // round limit), force one final tool-less generation so we never return
1016
+ // a tool-call message (content === null) to the send pipeline
1017
+ if (result.toolCalls?.length) {
1018
+ this._llm.log.error(
1019
+ `LLMSession: reached MAX_TOOL_CALL_ROUNDS (${MAX_TOOL_CALL_ROUNDS}), `
1020
+ + 'dropping pending tool calls and forcing a tool-less final answer',
1021
+ { toolCalls: result.toolCalls }
1022
+ );
1023
+ result = await this._generateWithoutTools(providerOptions, logOptions);
1024
+ }
1025
+
1010
1026
  this._generatedIndex = this._chat.length;
1011
1027
  this._chat.push(result);
1012
1028
 
1013
1029
  return result;
1014
1030
  }
1015
1031
 
1032
+ /**
1033
+ *
1034
+ * @param {LLMCallPreset} providerOptions
1035
+ * @param {LLMLogOptions} logOptions
1036
+ * @returns {Promise<LLMMessage<any>>}
1037
+ */
1038
+ _generateWithoutTools (providerOptions, logOptions) {
1039
+ const overrideChoice = typeof providerOptions === 'string'
1040
+ ? {
1041
+ preset: providerOptions,
1042
+ toolChoice: 'none'
1043
+ }
1044
+ : {
1045
+ ...providerOptions,
1046
+ toolChoice: 'none'
1047
+ };
1048
+ return this._llm.generate(this, overrideChoice, logOptions);
1049
+ }
1050
+
1016
1051
  /**
1017
1052
  *
1018
1053
  * @param {ToolCall} toolCall
@@ -1145,6 +1180,10 @@ class LLMSession {
1145
1180
  * @returns {LLMMessage[]}
1146
1181
  */
1147
1182
  static toMessages (result) {
1183
+ // tool-call / structured messages carry no text content - nothing to send
1184
+ if (typeof result.content !== 'string') {
1185
+ return [];
1186
+ }
1148
1187
  let filtered = result.content
1149
1188
  .replace(/\n\n\n+/g, '\n\n')
1150
1189
  .split(/\n\n+(?!\s*-)/g)