qlogicagent 0.2.1 → 0.4.0

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 (226) hide show
  1. package/README.md +45 -45
  2. package/package.json +56 -42
  3. package/dist/agent/agent.d.ts +0 -43
  4. package/dist/agent/agent.js +0 -113
  5. package/dist/agent/tool-loop.d.ts +0 -64
  6. package/dist/agent/tool-loop.js +0 -575
  7. package/dist/agent/types.d.ts +0 -175
  8. package/dist/agent/types.js +0 -14
  9. package/dist/cli/main.d.ts +0 -11
  10. package/dist/cli/main.js +0 -23
  11. package/dist/cli/stdio-server.d.ts +0 -45
  12. package/dist/cli/stdio-server.js +0 -463
  13. package/dist/config/config.d.ts +0 -17
  14. package/dist/config/config.js +0 -21
  15. package/dist/contracts/hooks.d.ts +0 -120
  16. package/dist/contracts/hooks.js +0 -7
  17. package/dist/contracts/index.d.ts +0 -10
  18. package/dist/contracts/index.js +0 -10
  19. package/dist/contracts/planner.d.ts +0 -35
  20. package/dist/contracts/planner.js +0 -2
  21. package/dist/contracts/skill-candidate.d.ts +0 -63
  22. package/dist/contracts/skill-candidate.js +0 -195
  23. package/dist/contracts/todo.d.ts +0 -14
  24. package/dist/contracts/todo.js +0 -9
  25. package/dist/index.d.ts +0 -13
  26. package/dist/index.js +0 -15
  27. package/dist/llm/builtin-providers.d.ts +0 -10
  28. package/dist/llm/builtin-providers.js +0 -531
  29. package/dist/llm/index.d.ts +0 -15
  30. package/dist/llm/index.js +0 -14
  31. package/dist/llm/llm-client.d.ts +0 -43
  32. package/dist/llm/llm-client.js +0 -67
  33. package/dist/llm/model-catalog.d.ts +0 -53
  34. package/dist/llm/model-catalog.js +0 -191
  35. package/dist/llm/provider-def.d.ts +0 -59
  36. package/dist/llm/provider-def.js +0 -12
  37. package/dist/llm/provider-registry.d.ts +0 -54
  38. package/dist/llm/provider-registry.js +0 -147
  39. package/dist/llm/transport.d.ts +0 -62
  40. package/dist/llm/transport.js +0 -27
  41. package/dist/llm/transports/anthropic-messages.d.ts +0 -31
  42. package/dist/llm/transports/anthropic-messages.js +0 -293
  43. package/dist/llm/transports/openai-chat.d.ts +0 -36
  44. package/dist/llm/transports/openai-chat.js +0 -165
  45. package/dist/orchestration/agent-registry.d.ts +0 -41
  46. package/dist/orchestration/agent-registry.js +0 -116
  47. package/dist/orchestration/approval-aware-tool-plan.d.ts +0 -32
  48. package/dist/orchestration/approval-aware-tool-plan.js +0 -87
  49. package/dist/orchestration/context-compression.d.ts +0 -220
  50. package/dist/orchestration/context-compression.js +0 -583
  51. package/dist/orchestration/conversation-repair.d.ts +0 -61
  52. package/dist/orchestration/conversation-repair.js +0 -429
  53. package/dist/orchestration/curator-scheduler.d.ts +0 -119
  54. package/dist/orchestration/curator-scheduler.js +0 -135
  55. package/dist/orchestration/embedded-failover-policy.d.ts +0 -110
  56. package/dist/orchestration/embedded-failover-policy.js +0 -168
  57. package/dist/orchestration/error-classification.d.ts +0 -12
  58. package/dist/orchestration/error-classification.js +0 -77
  59. package/dist/orchestration/failover-classification.d.ts +0 -8
  60. package/dist/orchestration/failover-classification.js +0 -381
  61. package/dist/orchestration/failover-error.d.ts +0 -33
  62. package/dist/orchestration/failover-error.js +0 -198
  63. package/dist/orchestration/fork-subagent.d.ts +0 -100
  64. package/dist/orchestration/fork-subagent.js +0 -98
  65. package/dist/orchestration/index.d.ts +0 -120
  66. package/dist/orchestration/index.js +0 -267
  67. package/dist/orchestration/memory-flush-policy.d.ts +0 -57
  68. package/dist/orchestration/memory-flush-policy.js +0 -85
  69. package/dist/orchestration/memory-provider.d.ts +0 -14
  70. package/dist/orchestration/memory-provider.js +0 -2
  71. package/dist/orchestration/parallel-tool-calls.d.ts +0 -41
  72. package/dist/orchestration/parallel-tool-calls.js +0 -59
  73. package/dist/orchestration/prompt-cache-strategy.d.ts +0 -126
  74. package/dist/orchestration/prompt-cache-strategy.js +0 -228
  75. package/dist/orchestration/reactive-compact.d.ts +0 -73
  76. package/dist/orchestration/reactive-compact.js +0 -78
  77. package/dist/orchestration/retry-loop.d.ts +0 -22
  78. package/dist/orchestration/retry-loop.js +0 -24
  79. package/dist/orchestration/skill-candidate.d.ts +0 -52
  80. package/dist/orchestration/skill-candidate.js +0 -141
  81. package/dist/orchestration/skill-consolidation.d.ts +0 -123
  82. package/dist/orchestration/skill-consolidation.js +0 -220
  83. package/dist/orchestration/skill-improvement.d.ts +0 -59
  84. package/dist/orchestration/skill-improvement.js +0 -66
  85. package/dist/orchestration/skill-similarity.d.ts +0 -98
  86. package/dist/orchestration/skill-similarity.js +0 -131
  87. package/dist/orchestration/streaming-tool-executor.d.ts +0 -73
  88. package/dist/orchestration/streaming-tool-executor.js +0 -96
  89. package/dist/orchestration/team-orchestration.d.ts +0 -195
  90. package/dist/orchestration/team-orchestration.js +0 -369
  91. package/dist/orchestration/team-tool-loop-wiring.d.ts +0 -92
  92. package/dist/orchestration/team-tool-loop-wiring.js +0 -147
  93. package/dist/orchestration/tool-choice-policy.d.ts +0 -54
  94. package/dist/orchestration/tool-choice-policy.js +0 -164
  95. package/dist/orchestration/tool-loop-state.d.ts +0 -50
  96. package/dist/orchestration/tool-loop-state.js +0 -133
  97. package/dist/orchestration/tool-schema.d.ts +0 -39
  98. package/dist/orchestration/tool-schema.js +0 -297
  99. package/dist/orchestration/transcript-repair.d.ts +0 -42
  100. package/dist/orchestration/transcript-repair.js +0 -426
  101. package/dist/orchestration/turn-loop-guard.d.ts +0 -86
  102. package/dist/orchestration/turn-loop-guard.js +0 -92
  103. package/dist/orchestration/web-browser-policy.d.ts +0 -17
  104. package/dist/orchestration/web-browser-policy.js +0 -39
  105. package/dist/runtime/context-compression.d.ts +0 -61
  106. package/dist/runtime/context-compression.js +0 -274
  107. package/dist/runtime/hook-registry.d.ts +0 -12
  108. package/dist/runtime/hook-registry.js +0 -53
  109. package/dist/runtime/memory-hooks.d.ts +0 -23
  110. package/dist/runtime/memory-hooks.js +0 -65
  111. package/dist/runtime/tool-eligibility.d.ts +0 -59
  112. package/dist/runtime/tool-eligibility.js +0 -111
  113. package/dist/skills/index.d.ts +0 -108
  114. package/dist/skills/index.js +0 -82
  115. package/dist/skills/memory-extractor.d.ts +0 -64
  116. package/dist/skills/memory-extractor.js +0 -173
  117. package/dist/skills/memory-query-tool.d.ts +0 -43
  118. package/dist/skills/memory-query-tool.js +0 -127
  119. package/dist/skills/memory-store.d.ts +0 -66
  120. package/dist/skills/memory-store.js +0 -228
  121. package/dist/skills/memory-tool.d.ts +0 -67
  122. package/dist/skills/memory-tool.js +0 -192
  123. package/dist/skills/portable-tool.d.ts +0 -71
  124. package/dist/skills/portable-tool.js +0 -14
  125. package/dist/skills/qmemory-adapter.d.ts +0 -52
  126. package/dist/skills/qmemory-adapter.js +0 -165
  127. package/dist/skills/skill-frontmatter.d.ts +0 -19
  128. package/dist/skills/skill-frontmatter.js +0 -344
  129. package/dist/skills/skill-guard.d.ts +0 -23
  130. package/dist/skills/skill-guard.js +0 -229
  131. package/dist/skills/skill-loader.d.ts +0 -16
  132. package/dist/skills/skill-loader.js +0 -303
  133. package/dist/skills/skill-source.d.ts +0 -119
  134. package/dist/skills/skill-source.js +0 -126
  135. package/dist/skills/skill-types.d.ts +0 -199
  136. package/dist/skills/skill-types.js +0 -6
  137. package/dist/skills/think-tool.d.ts +0 -16
  138. package/dist/skills/think-tool.js +0 -59
  139. package/dist/skills/todo-tool.d.ts +0 -63
  140. package/dist/skills/todo-tool.js +0 -114
  141. package/dist/skills/tools/agent-tool.d.ts +0 -91
  142. package/dist/skills/tools/agent-tool.js +0 -142
  143. package/dist/skills/tools/apply-patch-tool.d.ts +0 -29
  144. package/dist/skills/tools/apply-patch-tool.js +0 -184
  145. package/dist/skills/tools/ask-user-tool.d.ts +0 -80
  146. package/dist/skills/tools/ask-user-tool.js +0 -121
  147. package/dist/skills/tools/brief-tool.d.ts +0 -74
  148. package/dist/skills/tools/brief-tool.js +0 -95
  149. package/dist/skills/tools/browser-tool.d.ts +0 -114
  150. package/dist/skills/tools/browser-tool.js +0 -155
  151. package/dist/skills/tools/checkpoint-tool.d.ts +0 -66
  152. package/dist/skills/tools/checkpoint-tool.js +0 -102
  153. package/dist/skills/tools/config-tool.d.ts +0 -63
  154. package/dist/skills/tools/config-tool.js +0 -143
  155. package/dist/skills/tools/cron-tool.d.ts +0 -116
  156. package/dist/skills/tools/cron-tool.js +0 -175
  157. package/dist/skills/tools/edit-tool.d.ts +0 -43
  158. package/dist/skills/tools/edit-tool.js +0 -70
  159. package/dist/skills/tools/exec-tool.d.ts +0 -102
  160. package/dist/skills/tools/exec-tool.js +0 -133
  161. package/dist/skills/tools/image-generate-tool.d.ts +0 -62
  162. package/dist/skills/tools/image-generate-tool.js +0 -67
  163. package/dist/skills/tools/instructions-tool.d.ts +0 -103
  164. package/dist/skills/tools/instructions-tool.js +0 -187
  165. package/dist/skills/tools/lsp-tool.d.ts +0 -153
  166. package/dist/skills/tools/lsp-tool.js +0 -227
  167. package/dist/skills/tools/mcp-client-types.d.ts +0 -269
  168. package/dist/skills/tools/mcp-client-types.js +0 -53
  169. package/dist/skills/tools/mcp-tool.d.ts +0 -249
  170. package/dist/skills/tools/mcp-tool.js +0 -503
  171. package/dist/skills/tools/memory-tool.d.ts +0 -74
  172. package/dist/skills/tools/memory-tool.js +0 -88
  173. package/dist/skills/tools/monitor-tool.d.ts +0 -113
  174. package/dist/skills/tools/monitor-tool.js +0 -131
  175. package/dist/skills/tools/music-generate-tool.d.ts +0 -55
  176. package/dist/skills/tools/music-generate-tool.js +0 -62
  177. package/dist/skills/tools/notify-tool.d.ts +0 -53
  178. package/dist/skills/tools/notify-tool.js +0 -62
  179. package/dist/skills/tools/patch-tool.d.ts +0 -45
  180. package/dist/skills/tools/patch-tool.js +0 -505
  181. package/dist/skills/tools/pdf-tool.d.ts +0 -66
  182. package/dist/skills/tools/pdf-tool.js +0 -88
  183. package/dist/skills/tools/plan-mode-tool.d.ts +0 -59
  184. package/dist/skills/tools/plan-mode-tool.js +0 -122
  185. package/dist/skills/tools/read-tool.d.ts +0 -51
  186. package/dist/skills/tools/read-tool.js +0 -84
  187. package/dist/skills/tools/repl-tool.d.ts +0 -70
  188. package/dist/skills/tools/repl-tool.js +0 -69
  189. package/dist/skills/tools/search-tool.d.ts +0 -112
  190. package/dist/skills/tools/search-tool.js +0 -225
  191. package/dist/skills/tools/send-message-tool.d.ts +0 -51
  192. package/dist/skills/tools/send-message-tool.js +0 -76
  193. package/dist/skills/tools/skill-list-tool.d.ts +0 -33
  194. package/dist/skills/tools/skill-list-tool.js +0 -54
  195. package/dist/skills/tools/skill-manage-tool.d.ts +0 -73
  196. package/dist/skills/tools/skill-manage-tool.js +0 -153
  197. package/dist/skills/tools/skill-view-tool.d.ts +0 -37
  198. package/dist/skills/tools/skill-view-tool.js +0 -72
  199. package/dist/skills/tools/sleep-tool.d.ts +0 -49
  200. package/dist/skills/tools/sleep-tool.js +0 -81
  201. package/dist/skills/tools/structured-output-tool.d.ts +0 -116
  202. package/dist/skills/tools/structured-output-tool.js +0 -176
  203. package/dist/skills/tools/task-tool.d.ts +0 -104
  204. package/dist/skills/tools/task-tool.js +0 -161
  205. package/dist/skills/tools/team-tool.d.ts +0 -89
  206. package/dist/skills/tools/team-tool.js +0 -105
  207. package/dist/skills/tools/tool-search-tool.d.ts +0 -51
  208. package/dist/skills/tools/tool-search-tool.js +0 -110
  209. package/dist/skills/tools/tts-tool.d.ts +0 -38
  210. package/dist/skills/tools/tts-tool.js +0 -45
  211. package/dist/skills/tools/video-edit-tool.d.ts +0 -69
  212. package/dist/skills/tools/video-edit-tool.js +0 -74
  213. package/dist/skills/tools/video-generate-tool.d.ts +0 -62
  214. package/dist/skills/tools/video-generate-tool.js +0 -66
  215. package/dist/skills/tools/video-merge-tool.d.ts +0 -105
  216. package/dist/skills/tools/video-merge-tool.js +0 -92
  217. package/dist/skills/tools/video-upscale-tool.d.ts +0 -45
  218. package/dist/skills/tools/video-upscale-tool.js +0 -52
  219. package/dist/skills/tools/web-fetch-tool.d.ts +0 -78
  220. package/dist/skills/tools/web-fetch-tool.js +0 -92
  221. package/dist/skills/tools/web-search-tool.d.ts +0 -57
  222. package/dist/skills/tools/web-search-tool.js +0 -86
  223. package/dist/skills/tools/worktree-tool.d.ts +0 -69
  224. package/dist/skills/tools/worktree-tool.js +0 -147
  225. package/dist/skills/tools/write-tool.d.ts +0 -45
  226. package/dist/skills/tools/write-tool.js +0 -81
@@ -1,381 +0,0 @@
1
- const PERIODIC_USAGE_LIMIT_RE = /\b(?:daily|weekly|monthly)(?:\/(?:daily|weekly|monthly))* (?:usage )?limit(?:s)?(?: (?:exhausted|reached|exceeded))?\b/i;
2
- const ERROR_PATTERNS = {
3
- rateLimit: [
4
- /rate[_ ]limit|too many requests|429/,
5
- "model_cooldown",
6
- "exceeded your current quota",
7
- "resource has been exhausted",
8
- "quota exceeded",
9
- "resource_exhausted",
10
- "usage limit",
11
- /\btpm\b/i,
12
- "tokens per minute",
13
- "tokens per day",
14
- ],
15
- overloaded: [
16
- /overloaded_error|"type"\s*:\s*"overloaded_error"/i,
17
- "overloaded",
18
- /service[_ ]unavailable.*(?:overload|capacity|high[_ ]demand)|(?:overload|capacity|high[_ ]demand).*service[_ ]unavailable/i,
19
- "high demand",
20
- ],
21
- timeout: [
22
- "timeout",
23
- "timed out",
24
- "service unavailable",
25
- "deadline exceeded",
26
- "context deadline exceeded",
27
- "connection error",
28
- "network error",
29
- "network request failed",
30
- "fetch failed",
31
- "socket hang up",
32
- /\beconn(?:refused|reset|aborted)\b/i,
33
- /\benotfound\b/i,
34
- /\beai_again\b/i,
35
- /without sending (?:any )?chunks?/i,
36
- /\bstop reason:\s*(?:abort|error|network_error)\b/i,
37
- /\breason:\s*(?:abort|error|network_error)\b/i,
38
- /\bunhandled stop reason:\s*(?:abort|error|network_error)\b/i,
39
- ],
40
- billing: [
41
- /["']?(?:status|code)["']?\s*[:=]\s*402\b|\bhttp\s*402\b|\berror(?:\s+code)?\s*[:=]?\s*402\b|\b(?:got|returned|received)\s+(?:a\s+)?402\b|^\s*402\s+payment/i,
42
- "payment required",
43
- "insufficient credits",
44
- /insufficient[_ ]quota/i,
45
- "credit balance",
46
- "plans & billing",
47
- "insufficient balance",
48
- ],
49
- authPermanent: [
50
- /api[_ ]?key[_ ]?(?:revoked|invalid|deactivated|deleted)/i,
51
- "invalid_api_key",
52
- "key has been disabled",
53
- "key has been revoked",
54
- "account has been deactivated",
55
- /could not (?:authenticate|validate).*(?:api[_ ]?key|credentials)/i,
56
- "permission_error",
57
- "not allowed for this organization",
58
- ],
59
- auth: [
60
- /invalid[_ ]?api[_ ]?key/,
61
- "incorrect api key",
62
- "invalid token",
63
- "authentication",
64
- "re-authenticate",
65
- "oauth token refresh failed",
66
- "unauthorized",
67
- "forbidden",
68
- "access denied",
69
- "insufficient permissions",
70
- "insufficient permission",
71
- /missing scopes?:/i,
72
- "expired",
73
- "token has expired",
74
- /\b401\b/,
75
- /\b403\b/,
76
- "no credentials found",
77
- "no api key found",
78
- ],
79
- format: [
80
- "string should match pattern",
81
- "tool_use.id",
82
- "tool_use_id",
83
- "messages.1.content.1.tool_use.id",
84
- "invalid request format",
85
- /tool call id was.*must be/i,
86
- ],
87
- };
88
- const BILLING_ERROR_HEAD_RE = /^(?:error[:\s-]+)?billing(?:\s+error)?(?:[:\s-]+|$)|^(?:error[:\s-]+)?(?:credit balance|insufficient credits?|payment required|http\s*402\b)/i;
89
- const BILLING_ERROR_HARD_402_RE = /["']?(?:status|code)["']?\s*[:=]\s*402\b|\bhttp\s*402\b|\berror(?:\s+code)?\s*[:=]?\s*402\b|^\s*402\s+payment/i;
90
- const BILLING_ERROR_MAX_LENGTH = 512;
91
- const HTTP_STATUS_CODE_PREFIX_RE = /^(?:http\s*)?(\d{3})(?:\s+([\s\S]+))?$/i;
92
- const HTML_ERROR_PREFIX_RE = /^\s*(?:<!doctype\s+html\b|<html\b)/i;
93
- const CLOUDFLARE_HTML_ERROR_CODES = new Set([521, 522, 523, 524, 525, 526, 530]);
94
- const TRANSIENT_HTTP_ERROR_CODES = new Set([500, 502, 503, 504, 521, 522, 523, 524, 529]);
95
- const BILLING_402_HINTS = [
96
- "insufficient credits",
97
- "insufficient quota",
98
- "credit balance",
99
- "insufficient balance",
100
- "plans & billing",
101
- "add more credits",
102
- "top up",
103
- ];
104
- const BILLING_402_PLAN_HINTS = [
105
- "upgrade your plan",
106
- "upgrade plan",
107
- "current plan",
108
- "subscription",
109
- ];
110
- const PERIODIC_402_HINTS = ["daily", "weekly", "monthly"];
111
- const RETRYABLE_402_RETRY_HINTS = ["try again", "retry", "temporary", "cooldown"];
112
- const RETRYABLE_402_LIMIT_HINTS = ["usage limit", "rate limit", "organization usage"];
113
- const RETRYABLE_402_SCOPED_HINTS = ["organization", "workspace"];
114
- const RETRYABLE_402_SCOPED_RESULT_HINTS = ["billing period", "exceeded", "reached", "exhausted"];
115
- const RAW_402_MARKER_RE = /["']?(?:status|code)["']?\s*[:=]\s*402\b|\bhttp\s*402\b|\berror(?:\s+code)?\s*[:=]?\s*402\b|\b(?:got|returned|received)\s+(?:a\s+)?402\b|^\s*402\s+payment required\b/i;
116
- const LEADING_402_WRAPPER_RE = /^(?:error[:\s-]+)?(?:(?:http\s*)?402(?:\s+payment required)?|payment required)(?:[:\s-]+|$)/i;
117
- function matchesErrorPatterns(raw, patterns) {
118
- if (!raw) {
119
- return false;
120
- }
121
- const value = raw.toLowerCase();
122
- return patterns.some((pattern) => pattern instanceof RegExp ? pattern.test(value) : value.includes(pattern));
123
- }
124
- function matchesFormatErrorPattern(raw) {
125
- return matchesErrorPatterns(raw, ERROR_PATTERNS.format);
126
- }
127
- function isRateLimitErrorMessage(raw) {
128
- return matchesErrorPatterns(raw, ERROR_PATTERNS.rateLimit);
129
- }
130
- export function isTimeoutErrorMessage(raw) {
131
- return matchesErrorPatterns(raw, ERROR_PATTERNS.timeout);
132
- }
133
- function isPeriodicUsageLimitErrorMessage(raw) {
134
- return PERIODIC_USAGE_LIMIT_RE.test(raw);
135
- }
136
- function isBillingErrorMessage(raw) {
137
- const value = raw.toLowerCase();
138
- if (!value) {
139
- return false;
140
- }
141
- if (raw.length > BILLING_ERROR_MAX_LENGTH) {
142
- return BILLING_ERROR_HARD_402_RE.test(value);
143
- }
144
- if (matchesErrorPatterns(value, ERROR_PATTERNS.billing)) {
145
- return true;
146
- }
147
- if (!BILLING_ERROR_HEAD_RE.test(raw)) {
148
- return false;
149
- }
150
- return (value.includes("upgrade") ||
151
- value.includes("credits") ||
152
- value.includes("payment") ||
153
- value.includes("plan"));
154
- }
155
- function isAuthPermanentErrorMessage(raw) {
156
- return matchesErrorPatterns(raw, ERROR_PATTERNS.authPermanent);
157
- }
158
- function isAuthErrorMessage(raw) {
159
- return matchesErrorPatterns(raw, ERROR_PATTERNS.auth);
160
- }
161
- function isOverloadedErrorMessage(raw) {
162
- return matchesErrorPatterns(raw, ERROR_PATTERNS.overloaded);
163
- }
164
- function includesAnyHint(text, hints) {
165
- return hints.some((hint) => text.includes(hint));
166
- }
167
- function hasExplicit402BillingSignal(text) {
168
- return (includesAnyHint(text, BILLING_402_HINTS) ||
169
- (includesAnyHint(text, BILLING_402_PLAN_HINTS) && text.includes("limit")) ||
170
- text.includes("billing hard limit") ||
171
- text.includes("hard limit reached") ||
172
- (text.includes("maximum allowed") && text.includes("limit")));
173
- }
174
- function hasRetryable402TransientSignal(text) {
175
- const hasPeriodicHint = includesAnyHint(text, PERIODIC_402_HINTS);
176
- const hasSpendLimit = text.includes("spend limit") || text.includes("spending limit");
177
- const hasScopedHint = includesAnyHint(text, RETRYABLE_402_SCOPED_HINTS);
178
- return ((includesAnyHint(text, RETRYABLE_402_RETRY_HINTS) &&
179
- includesAnyHint(text, RETRYABLE_402_LIMIT_HINTS)) ||
180
- (hasPeriodicHint && (text.includes("usage limit") || hasSpendLimit)) ||
181
- (hasPeriodicHint && text.includes("limit") && text.includes("reset")) ||
182
- (hasScopedHint &&
183
- text.includes("limit") &&
184
- (hasSpendLimit || includesAnyHint(text, RETRYABLE_402_SCOPED_RESULT_HINTS))));
185
- }
186
- function normalize402Message(raw) {
187
- return raw.trim().toLowerCase().replace(LEADING_402_WRAPPER_RE, "").trim();
188
- }
189
- function classify402Message(message) {
190
- const normalized = normalize402Message(message);
191
- if (!normalized) {
192
- return "billing";
193
- }
194
- if (hasExplicit402BillingSignal(normalized)) {
195
- return "billing";
196
- }
197
- if (isRateLimitErrorMessage(normalized)) {
198
- return "rate_limit";
199
- }
200
- if (hasRetryable402TransientSignal(normalized)) {
201
- return "rate_limit";
202
- }
203
- return "billing";
204
- }
205
- function classifyFailoverReasonFrom402Text(raw) {
206
- if (!RAW_402_MARKER_RE.test(raw)) {
207
- return null;
208
- }
209
- return classify402Message(raw);
210
- }
211
- function extractLeadingHttpStatus(raw) {
212
- const match = raw.match(HTTP_STATUS_CODE_PREFIX_RE);
213
- if (!match) {
214
- return null;
215
- }
216
- const code = Number(match[1]);
217
- if (!Number.isFinite(code)) {
218
- return null;
219
- }
220
- return { code, rest: (match[2] ?? "").trim() };
221
- }
222
- function isJsonApiInternalServerError(raw) {
223
- if (!raw) {
224
- return false;
225
- }
226
- const value = raw.toLowerCase();
227
- return value.includes('"type":"api_error"') && value.includes("internal server error");
228
- }
229
- export function isCloudflareOrHtmlErrorPage(raw) {
230
- const trimmed = raw.trim();
231
- if (!trimmed) {
232
- return false;
233
- }
234
- const status = extractLeadingHttpStatus(trimmed);
235
- if (!status || status.code < 500) {
236
- return false;
237
- }
238
- if (CLOUDFLARE_HTML_ERROR_CODES.has(status.code)) {
239
- return true;
240
- }
241
- return status.code < 600 && HTML_ERROR_PREFIX_RE.test(status.rest) && /<\/html>/i.test(status.rest);
242
- }
243
- export function isTransientHttpError(raw) {
244
- const trimmed = raw.trim();
245
- if (!trimmed) {
246
- return false;
247
- }
248
- const status = extractLeadingHttpStatus(trimmed);
249
- if (!status) {
250
- return false;
251
- }
252
- return TRANSIENT_HTTP_ERROR_CODES.has(status.code);
253
- }
254
- export function classifyFailoverReasonFromHttpStatus(status, message) {
255
- if (typeof status !== "number" || !Number.isFinite(status)) {
256
- return null;
257
- }
258
- if (status === 402) {
259
- return message ? classify402Message(message) : "billing";
260
- }
261
- if (status === 429) {
262
- return "rate_limit";
263
- }
264
- if (status === 401 || status === 403) {
265
- if (message && isAuthPermanentErrorMessage(message)) {
266
- return "auth_permanent";
267
- }
268
- return "auth";
269
- }
270
- if (status === 408) {
271
- return "timeout";
272
- }
273
- if (status === 503) {
274
- if (message && isOverloadedErrorMessage(message)) {
275
- return "overloaded";
276
- }
277
- return "timeout";
278
- }
279
- if (status === 502 || status === 504) {
280
- return "timeout";
281
- }
282
- if (status === 529) {
283
- return "overloaded";
284
- }
285
- if (status === 400) {
286
- if (message && isBillingErrorMessage(message)) {
287
- return "billing";
288
- }
289
- return "format";
290
- }
291
- return null;
292
- }
293
- export function isModelNotFoundErrorMessage(raw) {
294
- if (!raw) {
295
- return false;
296
- }
297
- const lower = raw.toLowerCase();
298
- if (lower.includes("unknown model") ||
299
- lower.includes("model not found") ||
300
- lower.includes("model_not_found") ||
301
- lower.includes("not_found_error") ||
302
- (lower.includes("does not exist") && lower.includes("model")) ||
303
- (lower.includes("invalid model") && !lower.includes("invalid model reference"))) {
304
- return true;
305
- }
306
- if (/models\/[^\s]+ is not found/i.test(raw)) {
307
- return true;
308
- }
309
- if (/\b404\b/.test(raw) && /not[-_ ]?found/i.test(raw)) {
310
- return true;
311
- }
312
- return false;
313
- }
314
- function isCliSessionExpiredErrorMessage(raw) {
315
- if (!raw) {
316
- return false;
317
- }
318
- const lower = raw.toLowerCase();
319
- return (lower.includes("session not found") ||
320
- lower.includes("session does not exist") ||
321
- lower.includes("session expired") ||
322
- lower.includes("session invalid") ||
323
- lower.includes("conversation not found") ||
324
- lower.includes("conversation does not exist") ||
325
- lower.includes("conversation expired") ||
326
- lower.includes("conversation invalid") ||
327
- lower.includes("no such session") ||
328
- lower.includes("invalid session") ||
329
- lower.includes("session id not found") ||
330
- lower.includes("conversation id not found"));
331
- }
332
- export function classifyFailoverReason(raw) {
333
- if (isCliSessionExpiredErrorMessage(raw)) {
334
- return "session_expired";
335
- }
336
- if (isModelNotFoundErrorMessage(raw)) {
337
- return "model_not_found";
338
- }
339
- const reasonFrom402Text = classifyFailoverReasonFrom402Text(raw);
340
- if (reasonFrom402Text) {
341
- return reasonFrom402Text;
342
- }
343
- if (isPeriodicUsageLimitErrorMessage(raw)) {
344
- return isBillingErrorMessage(raw) ? "billing" : "rate_limit";
345
- }
346
- if (isRateLimitErrorMessage(raw)) {
347
- return "rate_limit";
348
- }
349
- if (isOverloadedErrorMessage(raw)) {
350
- return "overloaded";
351
- }
352
- if (isTransientHttpError(raw)) {
353
- const status = extractLeadingHttpStatus(raw.trim());
354
- if (status?.code === 529) {
355
- return "overloaded";
356
- }
357
- return "timeout";
358
- }
359
- if (isJsonApiInternalServerError(raw)) {
360
- return "timeout";
361
- }
362
- if (matchesFormatErrorPattern(raw)) {
363
- return "format";
364
- }
365
- if (isBillingErrorMessage(raw)) {
366
- return "billing";
367
- }
368
- if (isTimeoutErrorMessage(raw)) {
369
- return "timeout";
370
- }
371
- if (isAuthPermanentErrorMessage(raw)) {
372
- return "auth_permanent";
373
- }
374
- if (isAuthErrorMessage(raw)) {
375
- return "auth";
376
- }
377
- return null;
378
- }
379
- export function isFailoverErrorMessage(raw) {
380
- return classifyFailoverReason(raw) !== null;
381
- }
@@ -1,33 +0,0 @@
1
- import { type FailoverReason } from "./failover-classification.js";
2
- export declare class FailoverError extends Error {
3
- readonly reason: FailoverReason;
4
- readonly provider?: string;
5
- readonly model?: string;
6
- readonly profileId?: string;
7
- readonly status?: number;
8
- readonly code?: string;
9
- constructor(message: string, params: {
10
- reason: FailoverReason;
11
- provider?: string;
12
- model?: string;
13
- profileId?: string;
14
- status?: number;
15
- code?: string;
16
- cause?: unknown;
17
- });
18
- }
19
- export declare function isFailoverError(err: unknown): err is FailoverError;
20
- export declare function resolveFailoverStatus(reason: FailoverReason): number | undefined;
21
- export declare function isTimeoutError(err: unknown): boolean;
22
- export declare function resolveFailoverReasonFromError(err: unknown): FailoverReason | null;
23
- export declare function describeFailoverError(err: unknown): {
24
- message: string;
25
- reason?: FailoverReason;
26
- status?: number;
27
- code?: string;
28
- };
29
- export declare function coerceToFailoverError(err: unknown, context?: {
30
- provider?: string;
31
- model?: string;
32
- profileId?: string;
33
- }): FailoverError | null;
@@ -1,198 +0,0 @@
1
- import { classifyFailoverReason, classifyFailoverReasonFromHttpStatus, isTimeoutErrorMessage, } from "./failover-classification.js";
2
- const ABORT_TIMEOUT_RE = /request was aborted|request aborted/i;
3
- function readErrorName(err) {
4
- if (!err || typeof err !== "object") {
5
- return undefined;
6
- }
7
- const candidate = err.name;
8
- return typeof candidate === "string" && candidate.trim() ? candidate.trim() : undefined;
9
- }
10
- function getStatusCode(err) {
11
- if (!err || typeof err !== "object") {
12
- return undefined;
13
- }
14
- const candidate = err.status ??
15
- err.statusCode;
16
- if (typeof candidate === "number") {
17
- return candidate;
18
- }
19
- if (typeof candidate === "string" && /^\d+$/.test(candidate)) {
20
- return Number(candidate);
21
- }
22
- return undefined;
23
- }
24
- function getErrorCode(err) {
25
- if (!err || typeof err !== "object") {
26
- return undefined;
27
- }
28
- const candidate = err.code;
29
- if (typeof candidate !== "string") {
30
- return undefined;
31
- }
32
- const trimmed = candidate.trim();
33
- return trimmed ? trimmed : undefined;
34
- }
35
- function getErrorMessage(err) {
36
- if (err instanceof Error) {
37
- return err.message;
38
- }
39
- if (typeof err === "string") {
40
- return err;
41
- }
42
- if (typeof err === "number" || typeof err === "boolean" || typeof err === "bigint") {
43
- return String(err);
44
- }
45
- if (typeof err === "symbol") {
46
- return err.description ?? "";
47
- }
48
- if (err && typeof err === "object") {
49
- const message = err.message;
50
- if (typeof message === "string") {
51
- return message;
52
- }
53
- }
54
- return "";
55
- }
56
- function hasTimeoutHint(err) {
57
- if (!err) {
58
- return false;
59
- }
60
- if (readErrorName(err) === "TimeoutError") {
61
- return true;
62
- }
63
- const message = getErrorMessage(err);
64
- return Boolean(message && isTimeoutErrorMessage(message));
65
- }
66
- export class FailoverError extends Error {
67
- reason;
68
- provider;
69
- model;
70
- profileId;
71
- status;
72
- code;
73
- constructor(message, params) {
74
- super(message, { cause: params.cause });
75
- this.name = "FailoverError";
76
- this.reason = params.reason;
77
- this.provider = params.provider;
78
- this.model = params.model;
79
- this.profileId = params.profileId;
80
- this.status = params.status;
81
- this.code = params.code;
82
- }
83
- }
84
- export function isFailoverError(err) {
85
- return err instanceof FailoverError;
86
- }
87
- export function resolveFailoverStatus(reason) {
88
- switch (reason) {
89
- case "billing":
90
- return 402;
91
- case "rate_limit":
92
- return 429;
93
- case "overloaded":
94
- return 503;
95
- case "auth":
96
- return 401;
97
- case "auth_permanent":
98
- return 403;
99
- case "timeout":
100
- return 408;
101
- case "format":
102
- return 400;
103
- case "model_not_found":
104
- return 404;
105
- case "session_expired":
106
- return 410;
107
- default:
108
- return undefined;
109
- }
110
- }
111
- export function isTimeoutError(err) {
112
- if (hasTimeoutHint(err)) {
113
- return true;
114
- }
115
- if (!err || typeof err !== "object") {
116
- return false;
117
- }
118
- if (readErrorName(err) !== "AbortError") {
119
- return false;
120
- }
121
- const message = getErrorMessage(err);
122
- if (message && ABORT_TIMEOUT_RE.test(message)) {
123
- return true;
124
- }
125
- const cause = "cause" in err ? err.cause : undefined;
126
- const reason = "reason" in err ? err.reason : undefined;
127
- return hasTimeoutHint(cause) || hasTimeoutHint(reason);
128
- }
129
- export function resolveFailoverReasonFromError(err) {
130
- if (isFailoverError(err)) {
131
- return err.reason;
132
- }
133
- const status = getStatusCode(err);
134
- const message = getErrorMessage(err);
135
- const statusReason = classifyFailoverReasonFromHttpStatus(status, message);
136
- if (statusReason) {
137
- return statusReason;
138
- }
139
- const code = (getErrorCode(err) ?? "").toUpperCase();
140
- if ([
141
- "ETIMEDOUT",
142
- "ESOCKETTIMEDOUT",
143
- "ECONNRESET",
144
- "ECONNABORTED",
145
- "ECONNREFUSED",
146
- "ENETUNREACH",
147
- "EHOSTUNREACH",
148
- "ENETRESET",
149
- "EAI_AGAIN",
150
- ].includes(code)) {
151
- return "timeout";
152
- }
153
- if (isTimeoutError(err)) {
154
- return "timeout";
155
- }
156
- if (!message) {
157
- return null;
158
- }
159
- return classifyFailoverReason(message);
160
- }
161
- export function describeFailoverError(err) {
162
- if (isFailoverError(err)) {
163
- return {
164
- message: err.message,
165
- reason: err.reason,
166
- status: err.status,
167
- code: err.code,
168
- };
169
- }
170
- const message = getErrorMessage(err) || String(err);
171
- return {
172
- message,
173
- reason: resolveFailoverReasonFromError(err) ?? undefined,
174
- status: getStatusCode(err),
175
- code: getErrorCode(err),
176
- };
177
- }
178
- export function coerceToFailoverError(err, context) {
179
- if (isFailoverError(err)) {
180
- return err;
181
- }
182
- const reason = resolveFailoverReasonFromError(err);
183
- if (!reason) {
184
- return null;
185
- }
186
- const message = getErrorMessage(err) || String(err);
187
- const status = getStatusCode(err) ?? resolveFailoverStatus(reason);
188
- const code = getErrorCode(err);
189
- return new FailoverError(message, {
190
- reason,
191
- provider: context?.provider,
192
- model: context?.model,
193
- profileId: context?.profileId,
194
- status,
195
- code,
196
- cause: err instanceof Error ? err : undefined,
197
- });
198
- }