@oh-my-pi/pi-ai 16.2.1 → 16.2.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.
Files changed (170) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/types/auth-gateway/server.d.ts +0 -19
  3. package/dist/types/auth-retry.d.ts +2 -10
  4. package/dist/types/auth-storage.d.ts +1 -2
  5. package/dist/types/dialect/demotion.d.ts +22 -0
  6. package/dist/types/dialect/index.d.ts +2 -0
  7. package/dist/types/error/abort.d.ts +14 -0
  8. package/dist/types/error/auth-classify.d.ts +16 -0
  9. package/dist/types/error/auth.d.ts +27 -0
  10. package/dist/types/error/aws.d.ts +23 -0
  11. package/dist/types/error/classes.d.ts +102 -0
  12. package/dist/types/error/finalize.d.ts +39 -0
  13. package/dist/types/error/flags.d.ts +70 -0
  14. package/dist/types/error/format.d.ts +20 -0
  15. package/dist/types/error/gateway.d.ts +20 -0
  16. package/dist/types/error/index.d.ts +13 -0
  17. package/dist/types/error/oauth.d.ts +43 -0
  18. package/dist/types/error/provider.d.ts +42 -0
  19. package/dist/types/{rate-limit-utils.d.ts → error/rate-limit.d.ts} +14 -1
  20. package/dist/types/error/retryable.d.ts +27 -0
  21. package/dist/types/error/validation.d.ts +32 -0
  22. package/dist/types/index.d.ts +1 -3
  23. package/dist/types/providers/amazon-bedrock.d.ts +0 -5
  24. package/dist/types/providers/anthropic-client.d.ts +2 -16
  25. package/dist/types/providers/anthropic.d.ts +6 -1
  26. package/dist/types/providers/aws-eventstream.d.ts +2 -1
  27. package/dist/types/providers/cursor.d.ts +8 -7
  28. package/dist/types/providers/google-gemini-cli.d.ts +0 -5
  29. package/dist/types/providers/google-shared.d.ts +0 -5
  30. package/dist/types/providers/ollama.d.ts +0 -5
  31. package/dist/types/providers/openai-codex/request-transformer.d.ts +2 -2
  32. package/dist/types/providers/openai-codex/response-handler.d.ts +1 -1
  33. package/dist/types/providers/openai-codex-responses.d.ts +2 -2
  34. package/dist/types/providers/openai-responses-server-schema.d.ts +6 -0
  35. package/dist/types/providers/openai-responses-wire.d.ts +1 -1
  36. package/dist/types/providers/openai-responses.d.ts +3 -2
  37. package/dist/types/providers/openai-shared.d.ts +8 -6
  38. package/dist/types/providers/pi-native-client.d.ts +0 -9
  39. package/dist/types/registry/oauth/xai-oauth.d.ts +0 -9
  40. package/dist/types/types.d.ts +6 -0
  41. package/dist/types/usage/openai-codex-base-url.d.ts +17 -0
  42. package/dist/types/utils/block-symbols.d.ts +20 -0
  43. package/dist/types/utils/openai-http.d.ts +4 -8
  44. package/dist/types/utils/retry.d.ts +2 -14
  45. package/dist/types/utils/stream-markup-healing.d.ts +14 -5
  46. package/dist/types/utils/thinking-loop.d.ts +3 -1
  47. package/package.json +8 -4
  48. package/src/api-registry.ts +3 -1
  49. package/src/auth-broker/discover.ts +7 -2
  50. package/src/auth-broker/remote-store.ts +8 -7
  51. package/src/auth-gateway/server.ts +27 -115
  52. package/src/auth-retry.ts +9 -21
  53. package/src/auth-storage.ts +34 -49
  54. package/src/dialect/demotion.ts +31 -0
  55. package/src/dialect/factory.ts +0 -2
  56. package/src/dialect/index.ts +2 -0
  57. package/src/dialect/owned-stream.ts +0 -1
  58. package/src/dialect/thinking.ts +22 -10
  59. package/src/error/abort.ts +18 -0
  60. package/src/error/auth-classify.ts +30 -0
  61. package/src/error/auth.ts +48 -0
  62. package/src/error/aws.ts +31 -0
  63. package/src/error/classes.ts +186 -0
  64. package/src/error/finalize.ts +69 -0
  65. package/src/error/flags.ts +486 -0
  66. package/src/error/format.ts +36 -0
  67. package/src/error/gateway.ts +96 -0
  68. package/src/error/index.ts +13 -0
  69. package/src/error/oauth.ts +58 -0
  70. package/src/error/provider.ts +56 -0
  71. package/src/{rate-limit-utils.ts → error/rate-limit.ts} +9 -3
  72. package/src/error/retryable.ts +70 -0
  73. package/src/error/validation.ts +44 -0
  74. package/src/index.ts +1 -3
  75. package/src/providers/amazon-bedrock.ts +61 -57
  76. package/src/providers/anthropic-client.ts +13 -41
  77. package/src/providers/anthropic-messages-server.ts +9 -2
  78. package/src/providers/anthropic.ts +84 -147
  79. package/src/providers/aws-credentials.ts +41 -11
  80. package/src/providers/aws-eventstream.ts +12 -21
  81. package/src/providers/azure-openai-responses.ts +27 -17
  82. package/src/providers/cursor.ts +59 -53
  83. package/src/providers/devin.ts +28 -20
  84. package/src/providers/gitlab-duo-workflow.ts +30 -10
  85. package/src/providers/gitlab-duo.ts +22 -6
  86. package/src/providers/google-auth.ts +15 -5
  87. package/src/providers/google-gemini-cli.ts +46 -60
  88. package/src/providers/google-shared.ts +40 -38
  89. package/src/providers/google-vertex.ts +3 -2
  90. package/src/providers/google.ts +5 -1
  91. package/src/providers/mock.ts +10 -7
  92. package/src/providers/ollama.ts +40 -30
  93. package/src/providers/openai-chat-server.ts +2 -1
  94. package/src/providers/openai-codex/request-transformer.ts +13 -12
  95. package/src/providers/openai-codex/response-handler.ts +1 -1
  96. package/src/providers/openai-codex-responses.ts +931 -1012
  97. package/src/providers/openai-completions.ts +46 -36
  98. package/src/providers/openai-responses-server-schema.ts +3 -0
  99. package/src/providers/openai-responses-server.ts +82 -30
  100. package/src/providers/openai-responses-wire.ts +1 -1
  101. package/src/providers/openai-responses.ts +50 -24
  102. package/src/providers/openai-shared.ts +118 -51
  103. package/src/providers/pi-native-client.ts +12 -18
  104. package/src/providers/pi-native-server.ts +9 -6
  105. package/src/providers/register-builtins.ts +5 -2
  106. package/src/providers/transform-messages.ts +31 -63
  107. package/src/registry/alibaba-coding-plan.ts +6 -5
  108. package/src/registry/api-key-login.ts +4 -3
  109. package/src/registry/api-key-validation.ts +4 -3
  110. package/src/registry/cloudflare-ai-gateway.ts +4 -3
  111. package/src/registry/coreweave.ts +2 -1
  112. package/src/registry/deepseek.ts +2 -1
  113. package/src/registry/google-antigravity.ts +2 -1
  114. package/src/registry/google-gemini-cli.ts +2 -1
  115. package/src/registry/kagi.ts +4 -3
  116. package/src/registry/kilo.ts +32 -10
  117. package/src/registry/litellm.ts +4 -3
  118. package/src/registry/llama-cpp.ts +3 -2
  119. package/src/registry/lm-studio.ts +3 -2
  120. package/src/registry/nvidia.ts +7 -7
  121. package/src/registry/oauth/anthropic.ts +23 -6
  122. package/src/registry/oauth/callback-server.ts +5 -3
  123. package/src/registry/oauth/cursor.ts +18 -4
  124. package/src/registry/oauth/devin.ts +14 -3
  125. package/src/registry/oauth/github-copilot.ts +21 -10
  126. package/src/registry/oauth/gitlab-duo-workflow.ts +15 -3
  127. package/src/registry/oauth/gitlab-duo.ts +30 -6
  128. package/src/registry/oauth/google-antigravity.ts +14 -5
  129. package/src/registry/oauth/google-gemini-cli.ts +24 -7
  130. package/src/registry/oauth/google-oauth-shared.ts +6 -3
  131. package/src/registry/oauth/index.ts +19 -8
  132. package/src/registry/oauth/kimi.ts +40 -10
  133. package/src/registry/oauth/openai-codex.ts +26 -11
  134. package/src/registry/oauth/opencode.ts +4 -3
  135. package/src/registry/oauth/perplexity.ts +33 -11
  136. package/src/registry/oauth/xai-oauth.ts +62 -18
  137. package/src/registry/oauth/xiaomi.ts +21 -9
  138. package/src/registry/ollama-cloud.ts +5 -4
  139. package/src/registry/ollama.ts +3 -2
  140. package/src/registry/parallel.ts +4 -3
  141. package/src/registry/qwen-portal.ts +4 -3
  142. package/src/registry/tavily.ts +4 -3
  143. package/src/registry/vercel-ai-gateway.ts +4 -3
  144. package/src/registry/vllm.ts +3 -2
  145. package/src/stream.ts +79 -24
  146. package/src/types.ts +6 -0
  147. package/src/usage/claude.ts +2 -1
  148. package/src/usage/github-copilot.ts +4 -3
  149. package/src/usage/google-antigravity.ts +2 -1
  150. package/src/usage/openai-codex-base-url.ts +30 -10
  151. package/src/utils/abort.ts +4 -2
  152. package/src/utils/block-symbols.ts +32 -0
  153. package/src/utils/event-stream.ts +15 -3
  154. package/src/utils/http-inspector.ts +4 -4
  155. package/src/utils/idle-iterator.ts +6 -5
  156. package/src/utils/openai-http.ts +11 -46
  157. package/src/utils/parse-bind.ts +7 -5
  158. package/src/utils/proxy.ts +2 -1
  159. package/src/utils/retry.ts +7 -23
  160. package/src/utils/schema/normalize.ts +3 -2
  161. package/src/utils/stream-markup-healing.ts +41 -18
  162. package/src/utils/thinking-loop.ts +15 -11
  163. package/src/utils/validation.ts +4 -3
  164. package/dist/types/dialect/pi.d.ts +0 -9
  165. package/dist/types/errors.d.ts +0 -24
  166. package/dist/types/utils/overflow.d.ts +0 -55
  167. package/src/dialect/pi.md +0 -55
  168. package/src/dialect/pi.ts +0 -600
  169. package/src/errors.ts +0 -32
  170. package/src/utils/overflow.ts +0 -140
@@ -1,140 +0,0 @@
1
- import type { AssistantMessage } from "../types";
2
-
3
- /**
4
- * Regex patterns to detect context overflow errors from different providers.
5
- *
6
- * These patterns match error messages returned when the input exceeds
7
- * the model's context window.
8
- *
9
- * Provider-specific patterns (with example error messages):
10
- *
11
- * - Anthropic: "prompt is too long: 213462 tokens > 200000 maximum"
12
- * - OpenAI: "Your input exceeds the context window of this model"
13
- * - Google: "The input token count (1196265) exceeds the maximum number of tokens allowed (1048575)"
14
- * - xAI: "This model's maximum prompt length is 131072 but the request contains 537812 tokens"
15
- * - Groq: "Please reduce the length of the messages or completion"
16
- * - OpenRouter: "This endpoint's maximum context length is X tokens. However, you requested about Y tokens"
17
- * - llama.cpp: "the request exceeds the available context size, try increasing it"
18
- * - LM Studio: "tokens to keep from the initial prompt is greater than the context length"
19
- * - GitHub Copilot: "prompt token count of X exceeds the limit of Y"
20
- * - MiniMax: "invalid params, context window exceeds limit"
21
- * - Kimi For Coding: "Your request exceeded model token limit: X (requested: Y)"
22
- * - Anthropic 413: "request_too_large" / "Request exceeds the maximum size" (payload too large)
23
- * - HTTP 413 variants: "Payload Too Large" / "Request Entity Too Large"
24
- * - z.ai / GLM: Returns finish_reason: "model_context_window_exceeded" mapped to error message
25
- * - z.ai: Does NOT error, accepts overflow silently - handled via usage.input > contextWindow
26
- * - Ollama OpenAI-compatible: "prompt filled the context window" after empty finish_reason:length
27
- * - Ollama native: Silently truncates input - not detectable via error message
28
- */
29
- const OVERFLOW_PATTERNS = [
30
- /prompt is too long/i, // Anthropic
31
- /input is too long for requested model/i, // Amazon Bedrock
32
- /exceeds the context window/i, // OpenAI (Completions & Responses API)
33
- /input token count.*exceeds the maximum/i, // Google (Gemini)
34
- /maximum prompt length is \d+/i, // xAI (Grok)
35
- /reduce the length of the messages/i, // Groq
36
- /maximum context length is \d+ tokens/i, // OpenRouter (all backends)
37
- /exceeds the limit of \d+/i, // GitHub Copilot
38
- /exceeds the available context size/i, // llama.cpp server
39
- /requested tokens?.*exceed.*context (window|length|size)/i, // llama.cpp / OpenAI-compatible local servers
40
- /context (window|length|size).*(exceeded|overflow|too small)/i, // Generic local server variants
41
- /(prompt|input).*(too long|too large).*(context|n_ctx)/i, // llama.cpp phrasing variants
42
- /requested tokens?.*(exceeds?|greater than).*(n_ctx|context)/i, // llama.cpp n_ctx variants
43
- /greater than the context length/i, // LM Studio
44
- /context window exceeds limit/i, // MiniMax
45
- /exceeded model token limit/i, // Kimi For Coding
46
- /context[_ ]length[_ ]exceeded/i, // Generic fallback
47
- /too many tokens/i, // Generic fallback
48
- /token limit exceeded/i, // Generic fallback
49
- /request_too_large/i, // Anthropic 413 (request body too large)
50
- /request exceeds the maximum size/i, // Anthropic 413 variant
51
- /payload too large/i, // Generic HTTP 413 variant
52
- /entity too large/i, // Generic HTTP 413 variant
53
- /\b413\b.*\b(request|payload|entity)\b.*\btoo large\b/i, // "413 Request Entity Too Large" variants
54
- /model_context_window_exceeded/i, // z.ai non-standard finish_reason surfaced as error text
55
- /prompt filled the context window/i, // Ollama OpenAI-compatible empty length completion
56
- ];
57
- /**
58
- * Check if an assistant message represents a context overflow error.
59
- *
60
- * This handles two cases:
61
- * 1. Error-based overflow: Most providers return stopReason "error" with a
62
- * specific error message pattern.
63
- * 2. Silent overflow: Some providers accept overflow requests and return
64
- * successfully. For these, we check if usage.input exceeds the context window.
65
- *
66
- * ## Reliability by Provider
67
- *
68
- * **Reliable detection (returns error with detectable message):**
69
- * - Anthropic: "prompt is too long: X tokens > Y maximum"
70
- * - OpenAI (Completions & Responses): "exceeds the context window"
71
- * - Google Gemini: "input token count exceeds the maximum"
72
- * - xAI (Grok): "maximum prompt length is X but request contains Y"
73
- * - Groq: "reduce the length of the messages"
74
- * - Cerebras: 400/413 status code (no body)
75
- * - Mistral: 400/413 status code (no body)
76
- * - HTTP 413 payload/entity-too-large variants
77
- * - OpenRouter (all backends): "maximum context length is X tokens"
78
- * - llama.cpp: "exceeds the available context size"
79
- * - LM Studio: "greater than the context length"
80
- * - Kimi For Coding: "exceeded model token limit: X (requested: Y)"
81
- * - Anthropic 413: "request_too_large" (request body exceeds size limit)
82
- * - HTTP 413: "Payload Too Large" / "Request Entity Too Large"
83
- * - Ollama OpenAI-compatible: "prompt filled the context window"
84
- *
85
- * **Unreliable detection:**
86
- * - z.ai: Sometimes accepts overflow silently (detectable via usage.input > contextWindow),
87
- * sometimes returns rate limit errors. Pass contextWindow param to detect silent overflow.
88
- * - Ollama native: Silently truncates input without error. Cannot be detected via this function.
89
- * The response will have usage.input < expected, but we don't know the expected value.
90
- *
91
- * ## Custom Providers
92
- *
93
- * If you've added custom models via settings.json, this function may not detect
94
- * overflow errors from those providers. To add support:
95
- *
96
- * 1. Send a request that exceeds the model's context window
97
- * 2. Check the errorMessage in the response
98
- * 3. Create a regex pattern that matches the error
99
- * 4. The pattern should be added to OVERFLOW_PATTERNS in this file, or
100
- * check the errorMessage yourself before calling this function
101
- *
102
- * @param message - The assistant message to check
103
- * @param contextWindow - Optional context window size for detecting silent overflow (z.ai)
104
- * @returns true if the message indicates a context overflow
105
- */
106
- export function isContextOverflow(message: AssistantMessage, contextWindow?: number): boolean {
107
- // Case 1: Check error message patterns
108
- if (message.stopReason === "error" && message.errorMessage) {
109
- // Check known patterns
110
- if (OVERFLOW_PATTERNS.some(p => p.test(message.errorMessage!))) {
111
- return true;
112
- }
113
-
114
- // Cerebras and Mistral return 400/413 with no body for context overflow.
115
- // Proxy providers (e.g. api.synthetic.new) wrap upstream 400/413 no-body
116
- // responses in a JSON envelope, so the status code phrase may appear
117
- // anywhere in the message rather than at its start.
118
- // Note: 429 is rate limiting (requests/tokens per time), NOT context overflow
119
- if (/\b4(00|13)\s*(status code)?\s*\(no body\)/i.test(message.errorMessage)) {
120
- return true;
121
- }
122
- }
123
-
124
- // Case 2: Usage-based overflow (silent or provider-specific)
125
- if (contextWindow) {
126
- const inputTokens = message.usage.input + message.usage.cacheRead + message.usage.cacheWrite;
127
- if (inputTokens > contextWindow) {
128
- return true;
129
- }
130
- }
131
-
132
- return false;
133
- }
134
-
135
- /**
136
- * Get the overflow patterns for testing purposes.
137
- */
138
- export function getOverflowPatterns(): RegExp[] {
139
- return [...OVERFLOW_PATTERNS];
140
- }