wingbot 3.76.3 → 3.76.4-alpha.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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/LLMSession.js +57 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot",
3
- "version": "3.76.3",
3
+ "version": "3.76.4-alpha.1",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/src/LLMSession.js CHANGED
@@ -224,6 +224,9 @@ class LLMSession {
224
224
  this._res = res || llm?.res;
225
225
 
226
226
  this._preset = preset;
227
+
228
+ /** @type {PossiblyAsyncContent|null} */
229
+ this._fallbackMessage = null;
227
230
  }
228
231
 
229
232
  /**
@@ -850,6 +853,38 @@ class LLMSession {
850
853
  return this;
851
854
  }
852
855
 
856
+ /**
857
+ * Sets a message to send to the user when a subsequent `generate()` call
858
+ * fails (e.g. a provider network timeout). Instead of rejecting - which
859
+ * would surface the raw error to the user - the failure is logged and this
860
+ * message is sent in place of the model's reply, so the chain resolves
861
+ * cleanly. If the message resolves to an empty value, the original error
862
+ * is rethrown (same as when no fallback is set).
863
+ *
864
+ * Only affects `generate()`. `generateStructured()` keeps using delegated
865
+ * errors (see {@link onDelegatedError}).
866
+ *
867
+ * @param {PossiblyAsyncContent} content
868
+ * @returns {this}
869
+ */
870
+ setFallbackMessage (content) {
871
+ this._job(() => {
872
+ this._fallbackMessage = content;
873
+ }, true);
874
+ return this;
875
+ }
876
+
877
+ /**
878
+ * @returns {Promise<string>}
879
+ */
880
+ async _resolveFallbackContent () {
881
+ const fallback = this._fallbackMessage;
882
+ const content = typeof fallback === 'function'
883
+ ? fallback(this._resolveData())
884
+ : fallback;
885
+ return Promise.resolve(content);
886
+ }
887
+
853
888
  /**
854
889
  *
855
890
  * @param {LLMCallPreset} [providerOptions]
@@ -857,7 +892,28 @@ class LLMSession {
857
892
  * @returns {this}
858
893
  */
859
894
  generate (providerOptions = this._preset, logOptions = {}) {
860
- this._job(() => this._generate(providerOptions, logOptions));
895
+ this._job(async () => {
896
+ try {
897
+ return await this._generate(providerOptions, logOptions);
898
+ } catch (e) {
899
+ if (this._fallbackMessage === null) {
900
+ throw e;
901
+ }
902
+
903
+ const content = await this._resolveFallbackContent();
904
+ if (!content) {
905
+ // no usable fallback message - propagate the original error
906
+ throw e;
907
+ }
908
+ this._llm.log.error(`LLMSession.generate failed, sending fallback message: ${e.message}`, e);
909
+
910
+ /** @type {LLMMessage} */
911
+ const result = { role: ROLE_ASSISTANT, content };
912
+ this._generatedIndex = this._chat.length;
913
+ this._chat.push(result);
914
+ return result;
915
+ }
916
+ });
861
917
  return this;
862
918
  }
863
919