@wabot-dev/framework 0.9.11 → 0.9.13

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.
@@ -8,6 +8,7 @@ import '../../../feature/chat-bot/ChatOperator.js';
8
8
  import '../../../feature/chat-bot/UnionChatAdapter.js';
9
9
  import { extractChatMessageText } from '../../../feature/chat-bot/extractChatMessageText.js';
10
10
  import { isChatMessageEmpty } from '../../../feature/chat-bot/isChatMessageEmpty.js';
11
+ import { isRetryableError } from '../../../feature/chat-bot/isRetryableError.js';
11
12
  import { chatAdapter } from '../../../feature/chat-bot/metadata/@chatAdapter.js';
12
13
  import 'uuid';
13
14
  import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
@@ -39,15 +40,29 @@ let OpenRouterChatAdapter = class OpenRouterChatAdapter {
39
40
  const modelNames = req.models.map((m) => m.model);
40
41
  const [primary, ...fallbacks] = modelNames;
41
42
  this.logger.debug(`Call OpenRouter with model: ${primary}, fallbacks: ${fallbacks.length}, messages: ${messages.length}, tools: ${tools.length}`);
42
- const response = await this.openRouter.chat.send({
43
- chatRequest: {
44
- model: primary,
45
- models: fallbacks.length > 0 ? fallbacks : undefined,
46
- messages,
47
- tools: tools.length > 0 ? tools : undefined,
48
- },
49
- });
50
- return this.mapResponse(response);
43
+ const maxAttempts = 3;
44
+ const backoffMs = [500, 1000, 2000];
45
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
46
+ try {
47
+ const response = await this.openRouter.chat.send({
48
+ chatRequest: {
49
+ model: primary,
50
+ models: fallbacks.length > 0 ? fallbacks : undefined,
51
+ messages,
52
+ tools: tools.length > 0 ? tools : undefined,
53
+ },
54
+ });
55
+ return this.mapResponse(response);
56
+ }
57
+ catch (err) {
58
+ if (attempt === maxAttempts || !isRetryableError(err))
59
+ throw err;
60
+ const message = err instanceof Error ? err.message : String(err);
61
+ this.logger.warn(`OpenRouter call failed (attempt ${attempt}/${maxAttempts}), retrying in ${backoffMs[attempt - 1]}ms: ${message}`);
62
+ await new Promise((resolve) => setTimeout(resolve, backoffMs[attempt - 1]));
63
+ }
64
+ }
65
+ throw new Error('OpenRouter retry loop exited without result');
51
66
  }
52
67
  mapChatItems(chatItems) {
53
68
  const messages = [];
@@ -143,7 +158,12 @@ let OpenRouterChatAdapter = class OpenRouterChatAdapter {
143
158
  };
144
159
  }
145
160
  mapResponse(response) {
146
- const { toolCalls: responseToolCalls, content: responseText } = response.choices?.[0]?.message ?? {};
161
+ const choice = response.choices?.[0];
162
+ const message = choice?.message;
163
+ const finishReason = choice?.finishReason ?? null;
164
+ const responseText = typeof message?.content === 'string' ? message.content : undefined;
165
+ const responseToolCalls = message?.toolCalls;
166
+ const refusal = message?.refusal;
147
167
  const nextItems = [];
148
168
  if (responseText) {
149
169
  nextItems.push({ type: 'botMessage', botMessage: { text: responseText } });
@@ -163,7 +183,18 @@ let OpenRouterChatAdapter = class OpenRouterChatAdapter {
163
183
  }
164
184
  }
165
185
  if (nextItems.length === 0) {
166
- throw new Error('Not supported OpenRouter Response');
186
+ const diagnostics = {
187
+ model: response.model,
188
+ finishReason,
189
+ hasChoices: (response.choices?.length ?? 0) > 0,
190
+ hasMessage: !!message,
191
+ contentType: message?.content === undefined ? 'undefined' : typeof message.content,
192
+ hasReasoning: !!message?.reasoning,
193
+ refusal: refusal ?? null,
194
+ toolCallsCount: responseToolCalls?.length ?? 0,
195
+ };
196
+ this.logger.warn(`Empty OpenRouter response: ${JSON.stringify(diagnostics)}`);
197
+ throw new Error(`Empty OpenRouter response (model: ${response.model}, finishReason: ${finishReason ?? 'null'}${refusal ? ', refused' : ''})`);
167
198
  }
168
199
  let usage;
169
200
  if (response.usage) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.9.11",
3
+ "version": "0.9.13",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",