@code-yeongyu/senpi-ai 2026.9.30 → 2026.10.1-2

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 (274) hide show
  1. package/README.md +189 -19
  2. package/dist/api/anthropic-messages.js +168 -32
  3. package/dist/api/azure-openai-responses.js +23 -10
  4. package/dist/api/bedrock-converse-stream.js +12 -4
  5. package/dist/api/cloudflare-workers-ai-system-one.d.ts +4 -0
  6. package/dist/api/cloudflare-workers-ai-system-one.js +43 -0
  7. package/dist/api/cloudflare-workers-ai-system-one.lazy.d.ts +3 -0
  8. package/dist/api/cloudflare-workers-ai-system-one.lazy.js +4 -0
  9. package/dist/api/cloudflare.d.ts +2 -0
  10. package/dist/api/cloudflare.js +2 -0
  11. package/dist/api/context-room.d.ts +2 -2
  12. package/dist/api/cursor-agent.js +19 -10
  13. package/dist/api/devin-agent/request.d.ts +9 -9
  14. package/dist/api/devin-agent/request.js +14 -10
  15. package/dist/api/google-generative-ai.js +20 -80
  16. package/dist/api/google-shared.d.ts +13 -4
  17. package/dist/api/google-shared.js +54 -4
  18. package/dist/api/google-vertex.js +19 -62
  19. package/dist/api/llama-cpp-classify.d.ts +33 -0
  20. package/dist/api/llama-cpp-classify.js +365 -0
  21. package/dist/api/llama-cpp-classify.lazy.d.ts +3 -0
  22. package/dist/api/llama-cpp-classify.lazy.js +4 -0
  23. package/dist/api/mistral-conversations.d.ts +1 -1
  24. package/dist/api/mistral-conversations.js +36 -29
  25. package/dist/api/openai-codex-responses.d.ts +1 -1
  26. package/dist/api/openai-codex-responses.js +72 -37
  27. package/dist/api/openai-completions.d.ts +3 -2
  28. package/dist/api/openai-completions.js +76 -50
  29. package/dist/api/openai-images-params.d.ts +2 -2
  30. package/dist/api/openai-images.d.ts +1 -1
  31. package/dist/api/openai-responses-shared.d.ts +31 -8
  32. package/dist/api/openai-responses-shared.js +115 -27
  33. package/dist/api/openai-responses.d.ts +1 -1
  34. package/dist/api/openai-responses.js +38 -33
  35. package/dist/api/openrouter-images.d.ts +2 -1
  36. package/dist/api/openrouter-images.js +1 -0
  37. package/dist/api/pi-messages.d.ts +3 -3
  38. package/dist/api/pi-messages.js +3 -2
  39. package/dist/api/simple-options.d.ts +2 -2
  40. package/dist/api/simple-options.js +1 -0
  41. package/dist/api/system-one-shared.d.ts +23 -0
  42. package/dist/api/system-one-shared.js +183 -0
  43. package/dist/api/transform-messages.js +5 -2
  44. package/dist/api/typesafe-system-one.d.ts +4 -0
  45. package/dist/api/typesafe-system-one.js +19 -0
  46. package/dist/api/typesafe-system-one.lazy.d.ts +3 -0
  47. package/dist/api/typesafe-system-one.lazy.js +4 -0
  48. package/dist/api-registry.d.ts +3 -3
  49. package/dist/auth/helpers.js +1 -1
  50. package/dist/auth/oauth/anthropic-callback-listener.js +1 -1
  51. package/dist/auth/oauth/callback-server.d.ts +55 -0
  52. package/dist/auth/oauth/callback-server.js +146 -0
  53. package/dist/auth/oauth/chatgpt-subscription.d.ts +1 -1
  54. package/dist/auth/oauth/chatgpt-subscription.js +20 -124
  55. package/dist/auth/oauth/devin-callback.js +1 -1
  56. package/dist/auth/oauth/load.d.ts +4 -0
  57. package/dist/auth/oauth/load.js +10 -0
  58. package/dist/auth/oauth/meta.d.ts +17 -0
  59. package/dist/auth/oauth/meta.js +190 -0
  60. package/dist/auth/oauth/openai-chatgpt.d.ts +9 -0
  61. package/dist/auth/oauth/openai-chatgpt.js +266 -0
  62. package/dist/auth/oauth/openrouter.d.ts +1 -1
  63. package/dist/auth/oauth/openrouter.js +19 -138
  64. package/dist/auth/oauth/radius.d.ts +1 -1
  65. package/dist/auth/oauth/radius.js +21 -89
  66. package/dist/auth/resolve.d.ts +3 -8
  67. package/dist/auth/resolve.js +3 -18
  68. package/dist/auth/types.d.ts +10 -1
  69. package/dist/bun-oauth.js +4 -0
  70. package/dist/cli.js +3 -1
  71. package/dist/compat.js +17 -14
  72. package/dist/env-api-keys.js +2 -0
  73. package/dist/image-models.d.ts +19 -8
  74. package/dist/image-models.js +14 -13
  75. package/dist/images-api-registry.d.ts +8 -8
  76. package/dist/images.d.ts +7 -2
  77. package/dist/images.js +5 -0
  78. package/dist/index.d.ts +2 -2
  79. package/dist/index.js +2 -2
  80. package/dist/model-catalog.d.ts +27 -9
  81. package/dist/model-catalog.js +24 -2
  82. package/dist/model.d.ts +13 -13
  83. package/dist/models-store.d.ts +3 -2
  84. package/dist/models.d.ts +106 -36
  85. package/dist/models.generated.d.ts +142 -42
  86. package/dist/models.generated.js +142 -42
  87. package/dist/models.js +174 -36
  88. package/dist/providers/alibaba-token-plan.models.d.ts +4 -2
  89. package/dist/providers/alibaba-token-plan.models.js +4 -2
  90. package/dist/providers/all.d.ts +72 -19
  91. package/dist/providers/all.js +28 -22
  92. package/dist/providers/amazon-bedrock.models.d.ts +4 -2
  93. package/dist/providers/amazon-bedrock.models.js +4 -2
  94. package/dist/providers/ant-ling.models.d.ts +4 -2
  95. package/dist/providers/ant-ling.models.js +4 -2
  96. package/dist/providers/anthropic.models.d.ts +4 -2
  97. package/dist/providers/anthropic.models.js +4 -2
  98. package/dist/providers/azure-openai-responses.models.d.ts +4 -2
  99. package/dist/providers/azure-openai-responses.models.js +4 -2
  100. package/dist/providers/bai.models.d.ts +4 -2
  101. package/dist/providers/bai.models.js +4 -2
  102. package/dist/providers/baseten.models.d.ts +4 -2
  103. package/dist/providers/baseten.models.js +4 -2
  104. package/dist/providers/cerebras.models.d.ts +4 -2
  105. package/dist/providers/cerebras.models.js +4 -2
  106. package/dist/providers/chatgpt-subscription.models.d.ts +4 -2
  107. package/dist/providers/chatgpt-subscription.models.js +4 -2
  108. package/dist/providers/cloudflare-ai-gateway.models.d.ts +4 -2
  109. package/dist/providers/cloudflare-ai-gateway.models.js +4 -2
  110. package/dist/providers/cloudflare-stream.d.ts +6 -2
  111. package/dist/providers/cloudflare-stream.js +6 -0
  112. package/dist/providers/cloudflare-workers-ai.js +10 -3
  113. package/dist/providers/cloudflare-workers-ai.models.d.ts +4 -2
  114. package/dist/providers/cloudflare-workers-ai.models.js +4 -2
  115. package/dist/providers/data/.manifest.json +1 -1
  116. package/dist/providers/data/alibaba-token-plan.json +1 -1
  117. package/dist/providers/data/amazon-bedrock.json +1 -1
  118. package/dist/providers/data/ant-ling.json +1 -1
  119. package/dist/providers/data/anthropic.json +1 -1
  120. package/dist/providers/data/azure-openai-responses.json +1 -1
  121. package/dist/providers/data/bai.json +1 -1
  122. package/dist/providers/data/baseten.json +1 -1
  123. package/dist/providers/data/cerebras.json +1 -1
  124. package/dist/providers/data/chatgpt-subscription.json +1 -1
  125. package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
  126. package/dist/providers/data/cloudflare-workers-ai.json +1 -1
  127. package/dist/providers/data/deepseek.json +1 -1
  128. package/dist/providers/data/fireworks.json +1 -1
  129. package/dist/providers/data/github-copilot.json +1 -1
  130. package/dist/providers/data/google-vertex.json +1 -1
  131. package/dist/providers/data/google.json +1 -1
  132. package/dist/providers/data/groq.json +1 -1
  133. package/dist/providers/data/huggingface.json +1 -1
  134. package/dist/providers/data/meta.json +1 -0
  135. package/dist/providers/data/minimax-cn.json +1 -1
  136. package/dist/providers/data/minimax.json +1 -1
  137. package/dist/providers/data/mistral.json +1 -1
  138. package/dist/providers/data/moonshotai-cn.json +1 -1
  139. package/dist/providers/data/moonshotai.json +1 -1
  140. package/dist/providers/data/nvidia.json +1 -1
  141. package/dist/providers/data/openai.json +1 -1
  142. package/dist/providers/data/opencode-go.json +1 -1
  143. package/dist/providers/data/opencode.json +1 -1
  144. package/dist/providers/data/opengateway.json +1 -1
  145. package/dist/providers/data/openrouter.json +1 -1
  146. package/dist/providers/data/qwen-token-plan-cn.json +1 -1
  147. package/dist/providers/data/qwen-token-plan-individual.json +1 -1
  148. package/dist/providers/data/qwen-token-plan.json +1 -1
  149. package/dist/providers/data/radius.json +1 -0
  150. package/dist/providers/data/together.json +1 -1
  151. package/dist/providers/data/typesafe.json +1 -0
  152. package/dist/providers/data/venice.json +1 -1
  153. package/dist/providers/data/vercel-ai-gateway.json +1 -1
  154. package/dist/providers/data/xai.json +1 -1
  155. package/dist/providers/data/xiaomi-token-plan-ams.json +1 -1
  156. package/dist/providers/data/xiaomi-token-plan-cn.json +1 -1
  157. package/dist/providers/data/xiaomi-token-plan-sgp.json +1 -1
  158. package/dist/providers/data/xiaomi.json +1 -1
  159. package/dist/providers/data/zai-coding-cn.json +1 -1
  160. package/dist/providers/data/zai.json +1 -1
  161. package/dist/providers/deepseek.models.d.ts +4 -2
  162. package/dist/providers/deepseek.models.js +4 -2
  163. package/dist/providers/faux.d.ts +7 -2
  164. package/dist/providers/faux.js +31 -22
  165. package/dist/providers/fireworks.models.d.ts +4 -2
  166. package/dist/providers/fireworks.models.js +4 -2
  167. package/dist/providers/github-copilot.models.d.ts +4 -2
  168. package/dist/providers/github-copilot.models.js +4 -2
  169. package/dist/providers/google-vertex.models.d.ts +4 -2
  170. package/dist/providers/google-vertex.models.js +4 -2
  171. package/dist/providers/google.models.d.ts +4 -2
  172. package/dist/providers/google.models.js +4 -2
  173. package/dist/providers/groq.models.d.ts +4 -2
  174. package/dist/providers/groq.models.js +4 -2
  175. package/dist/providers/huggingface.models.d.ts +4 -2
  176. package/dist/providers/huggingface.models.js +4 -2
  177. package/dist/providers/images/register-builtins.d.ts +2 -2
  178. package/dist/providers/kimi-coding.models.d.ts +54 -7
  179. package/dist/providers/kimi-coding.models.js +34 -7
  180. package/dist/providers/meta.d.ts +3 -0
  181. package/dist/providers/meta.js +24 -0
  182. package/dist/providers/meta.models.d.ts +6 -0
  183. package/dist/providers/meta.models.js +8 -0
  184. package/dist/providers/minimax-cn.models.d.ts +4 -2
  185. package/dist/providers/minimax-cn.models.js +4 -2
  186. package/dist/providers/minimax.models.d.ts +4 -2
  187. package/dist/providers/minimax.models.js +4 -2
  188. package/dist/providers/mistral.models.d.ts +4 -2
  189. package/dist/providers/mistral.models.js +4 -2
  190. package/dist/providers/moonshotai-cn.models.d.ts +4 -2
  191. package/dist/providers/moonshotai-cn.models.js +4 -2
  192. package/dist/providers/moonshotai.models.d.ts +4 -2
  193. package/dist/providers/moonshotai.models.js +4 -2
  194. package/dist/providers/nvidia.models.d.ts +4 -2
  195. package/dist/providers/nvidia.models.js +4 -2
  196. package/dist/providers/openai.js +4 -2
  197. package/dist/providers/openai.models.d.ts +4 -2
  198. package/dist/providers/openai.models.js +4 -2
  199. package/dist/providers/opencode-go.models.d.ts +4 -2
  200. package/dist/providers/opencode-go.models.js +4 -2
  201. package/dist/providers/opencode.d.ts +3 -1
  202. package/dist/providers/opencode.js +5 -2
  203. package/dist/providers/opencode.models.d.ts +4 -2
  204. package/dist/providers/opencode.models.js +4 -2
  205. package/dist/providers/opengateway.models.d.ts +4 -2
  206. package/dist/providers/opengateway.models.js +4 -2
  207. package/dist/providers/openrouter.js +11 -2
  208. package/dist/providers/openrouter.models.d.ts +4 -2
  209. package/dist/providers/openrouter.models.js +4 -2
  210. package/dist/providers/qwen-token-plan-cn.models.d.ts +4 -2
  211. package/dist/providers/qwen-token-plan-cn.models.js +4 -2
  212. package/dist/providers/qwen-token-plan-individual.models.d.ts +4 -2
  213. package/dist/providers/qwen-token-plan-individual.models.js +4 -2
  214. package/dist/providers/qwen-token-plan.models.d.ts +4 -2
  215. package/dist/providers/qwen-token-plan.models.js +4 -2
  216. package/dist/providers/radius.js +19 -5
  217. package/dist/providers/radius.models.d.ts +6 -0
  218. package/dist/providers/radius.models.js +8 -0
  219. package/dist/providers/together.models.d.ts +4 -2
  220. package/dist/providers/together.models.js +4 -2
  221. package/dist/providers/typesafe.d.ts +3 -0
  222. package/dist/providers/typesafe.js +18 -0
  223. package/dist/providers/typesafe.models.d.ts +6 -0
  224. package/dist/providers/typesafe.models.js +8 -0
  225. package/dist/providers/venice.models.d.ts +4 -2
  226. package/dist/providers/venice.models.js +4 -2
  227. package/dist/providers/vercel-ai-gateway.js +5 -2
  228. package/dist/providers/vercel-ai-gateway.models.d.ts +4 -2
  229. package/dist/providers/vercel-ai-gateway.models.js +4 -2
  230. package/dist/providers/xai.models.d.ts +4 -2
  231. package/dist/providers/xai.models.js +4 -2
  232. package/dist/providers/xiaomi-token-plan-ams.models.d.ts +4 -2
  233. package/dist/providers/xiaomi-token-plan-ams.models.js +4 -2
  234. package/dist/providers/xiaomi-token-plan-cn.models.d.ts +4 -2
  235. package/dist/providers/xiaomi-token-plan-cn.models.js +4 -2
  236. package/dist/providers/xiaomi-token-plan-sgp.models.d.ts +4 -2
  237. package/dist/providers/xiaomi-token-plan-sgp.models.js +4 -2
  238. package/dist/providers/xiaomi.models.d.ts +4 -2
  239. package/dist/providers/xiaomi.models.js +4 -2
  240. package/dist/providers/zai-coding-cn.models.d.ts +4 -2
  241. package/dist/providers/zai-coding-cn.models.js +4 -2
  242. package/dist/providers/zai.models.d.ts +4 -2
  243. package/dist/providers/zai.models.js +4 -2
  244. package/dist/tool-call-middleware/context-transformer.d.ts +8 -5
  245. package/dist/tool-call-middleware/context-transformer.js +44 -15
  246. package/dist/types.d.ts +262 -28
  247. package/dist/utils/diagnostics.d.ts +3 -2
  248. package/dist/utils/estimate.d.ts +2 -2
  249. package/dist/utils/estimate.js +22 -30
  250. package/dist/utils/headers.d.ts +1 -1
  251. package/dist/utils/headers.js +10 -8
  252. package/dist/utils/model-operations.d.ts +11 -0
  253. package/dist/utils/model-operations.js +47 -0
  254. package/dist/utils/models-error.d.ts +8 -0
  255. package/dist/utils/models-error.js +18 -0
  256. package/dist/utils/overflow.d.ts +1 -0
  257. package/dist/utils/overflow.js +11 -5
  258. package/dist/utils/prompt-cache-ttl.js +10 -3
  259. package/dist/utils/retry.js +9 -0
  260. package/dist/utils/text.d.ts +9 -1
  261. package/dist/utils/text.js +26 -0
  262. package/dist/utils/transcript.d.ts +85 -0
  263. package/dist/utils/transcript.js +205 -0
  264. package/package.json +3 -4
  265. package/dist/image-models.generated.d.ts +0 -925
  266. package/dist/image-models.generated.js +0 -927
  267. package/dist/images-models.d.ts +0 -95
  268. package/dist/images-models.js +0 -141
  269. package/dist/providers/openai-images.d.ts +0 -3
  270. package/dist/providers/openai-images.js +0 -16
  271. package/dist/providers/openrouter-images.d.ts +0 -3
  272. package/dist/providers/openrouter-images.js +0 -22
  273. /package/dist/{auth/oauth → utils}/oauth-page.d.ts +0 -0
  274. /package/dist/{auth/oauth → utils}/oauth-page.js +0 -0
package/dist/types.d.ts CHANGED
@@ -21,13 +21,17 @@ export type { SessionAffinityFormat } from "./openai-responses-compat.ts";
21
21
  export type OpenAIResponsesCompat = BaseOpenAIResponsesCompat & {
22
22
  /** Whether the model supports message-anchored `additional_tools` input items. Default: false. */
23
23
  supportsAdditionalTools?: boolean;
24
+ /** Whether the exact model accepts developer or system messages after the conversation has started. When false, later system messages are folded into the leading system message. Default: false; the generated model catalog enables it for verified models. */
25
+ supportsMidConvoSystemMessages?: boolean;
24
26
  };
25
27
  export type { AssistantMessageEventStream } from "./utils/event-stream.ts";
26
28
  export type KnownApi = "openai-completions" | "mistral-conversations" | "openai-responses" | "azure-openai-responses" | "openai-codex-responses" | "cursor-agent" | "devin-agent" | "anthropic-messages" | "bedrock-converse-stream" | "google-generative-ai" | "google-vertex" | "pi-messages";
27
29
  export type Api = KnownApi | (string & {});
28
- export type KnownImagesApi = "openrouter-images" | "openai-images";
29
- export type ImagesApi = KnownImagesApi | (string & {});
30
- export type KnownProvider = "alibaba-token-plan" | "amazon-bedrock" | "ant-ling" | "anthropic" | "google" | "google-vertex" | "openai" | "azure-openai-responses" | "bai" | "openai-codex" | "chatgpt-subscription" | "ollama" | "cursor" | "radius" | "nvidia" | "deepseek" | "github-copilot" | "xai" | "groq" | "cerebras" | "openrouter" | "vercel-ai-gateway" | "opengateway" | "zai" | "zai-coding-cn" | "mistral" | "minimax" | "minimax-cn" | "moonshotai" | "moonshotai-cn" | "huggingface" | "fireworks" | "together" | "baseten" | "opencode" | "opencode-go" | "kimi-coding" | "cloudflare-workers-ai" | "cloudflare-ai-gateway" | "qwen-token-plan" | "qwen-token-plan-cn" | "qwen-token-plan-individual" | "xiaomi" | "xiaomi-token-plan-cn" | "xiaomi-token-plan-ams" | "xiaomi-token-plan-sgp" | "venice";
30
+ export type KnownImageApi = "openrouter-images" | "openai-images";
31
+ export type ImageApi = KnownImageApi | (string & {});
32
+ export type KnownClassifierApi = "typesafe-system-one" | "cloudflare-workers-ai-system-one" | "llama-cpp-classify";
33
+ export type ClassifierApi = KnownClassifierApi | (string & {});
34
+ export type KnownProvider = "alibaba-token-plan" | "amazon-bedrock" | "ant-ling" | "anthropic" | "google" | "google-vertex" | "openai" | "azure-openai-responses" | "bai" | "openai-codex" | "chatgpt-subscription" | "ollama" | "cursor" | "radius" | "typesafe" | "nvidia" | "deepseek" | "github-copilot" | "xai" | "groq" | "cerebras" | "openrouter" | "vercel-ai-gateway" | "opengateway" | "zai" | "zai-coding-cn" | "mistral" | "minimax" | "minimax-cn" | "moonshotai" | "moonshotai-cn" | "huggingface" | "fireworks" | "together" | "baseten" | "opencode" | "opencode-go" | "kimi-coding" | "meta" | "cloudflare-workers-ai" | "cloudflare-ai-gateway" | "qwen-token-plan" | "qwen-token-plan-cn" | "qwen-token-plan-individual" | "xiaomi" | "xiaomi-token-plan-cn" | "xiaomi-token-plan-ams" | "xiaomi-token-plan-sgp" | "venice";
31
35
  export type ProviderId = KnownProvider | string;
32
36
  export type KnownImagesProvider = "openai" | "openrouter";
33
37
  export type ImagesProviderId = KnownImagesProvider | string;
@@ -61,6 +65,11 @@ export interface ThinkingBudgets {
61
65
  max?: number;
62
66
  }
63
67
  export type CacheRetention = "none" | "short" | "long";
68
+ /**
69
+ * Best-effort prompt cache lifetime in seconds for each retention tier a request can ask for.
70
+ * A missing tier means the lifetime is unknown; pi does not warm such caches.
71
+ */
72
+ export type ModelPromptCache = Partial<Record<Exclude<CacheRetention, "none">, number>>;
64
73
  export type Transport = "sse" | "websocket" | "websocket-cached" | "auto";
65
74
  /** Provider-scoped environment overrides. Values take precedence over process.env. */
66
75
  export type ProviderEnv = Record<string, string>;
@@ -151,6 +160,12 @@ export interface ProviderRequestOptions<TModel = Model<Api>> {
151
160
  maxRetryDelayMs?: number;
152
161
  }
153
162
  export interface StreamOptions extends ProviderRequestOptions<Model<Api>> {
163
+ /**
164
+ * Optional observer for each parsed provider stream event before Pi normalization.
165
+ * Event data is adapter-owned and must be treated as read-only.
166
+ * Adapter support is explicit; unsupported adapters do not invoke it.
167
+ */
168
+ onProviderStreamEvent?: (data: unknown, model: Model<Api>) => void | Promise<void>;
154
169
  temperature?: number;
155
170
  /**
156
171
  * Arbitrary sampling parameters merged into the request body as-is, after the named request
@@ -240,21 +255,33 @@ export type ApiStreamOptions<TApi extends Api> = TApi extends keyof ApiOptionsMa
240
255
  * `Provider.stream()` via `ApiStreamOptions`.
241
256
  */
242
257
  export interface ProviderStreams {
243
- stream(model: Model<Api>, context: Context, options?: StreamOptions): AssistantMessageEventStream;
244
- streamSimple(model: Model<Api>, context: Context, options?: SimpleStreamOptions): AssistantMessageEventStream;
258
+ stream(model: Model<Api>, context: TranscriptContext, options?: StreamOptions): AssistantMessageEventStream;
259
+ streamSimple(model: Model<Api>, context: TranscriptContext, options?: SimpleStreamOptions): AssistantMessageEventStream;
245
260
  fetchDeferred?(model: Model<Api>, handle: DeferredHandle, options?: DeferredFetchOptions): AssistantMessageEventStream;
246
261
  cancelDeferred?(model: Model<Api>, handle: DeferredHandle, options?: DeferredCancelOptions): Promise<void>;
247
262
  }
248
263
  /**
249
264
  * The uniform contract of an image-generation API implementation module:
250
265
  * every image API module under `src/api/` exports exactly `generateImages`,
251
- * so the module itself satisfies this interface. Lazy wrappers and image
252
- * provider factories pass these around as values.
266
+ * so the module itself satisfies this interface. Lazy wrappers and
267
+ * `createProvider({ images })` pass these around as values.
253
268
  */
254
269
  export interface ProviderImages {
255
- generateImages(model: ImagesModel<ImagesApi>, context: ImagesContext, options?: ImagesOptions): Promise<AssistantImages>;
270
+ generateImages(model: ImageModel<ImageApi>, context: ImagesContext, options?: ImagesOptions): Promise<AssistantImages>;
256
271
  }
257
- export interface ImagesOptions extends ProviderRequestOptions<ImagesModel<ImagesApi>> {
272
+ /** The uniform contract implemented by classifier API modules. */
273
+ export interface ProviderClassifier {
274
+ classify(model: ClassifierModel<ClassifierApi>, context: ClassifierContext, options?: ClassifierOptions): Promise<ClassifierResult>;
275
+ }
276
+ export interface ClassifierOptions extends ProviderRequestOptions<ClassifierModel<ClassifierApi>> {
277
+ /**
278
+ * Divides the answer logits by this value before they are normalized into probabilities.
279
+ * Values above 1 soften the distribution; values below 1 sharpen it. Must be positive.
280
+ * APIs that cannot apply it ignore it.
281
+ */
282
+ temperature?: number;
283
+ }
284
+ export interface ImagesOptions extends ProviderRequestOptions<ImageModel<ImageApi>> {
258
285
  /**
259
286
  * Optional metadata to include in API requests.
260
287
  * Providers extract the fields they understand and ignore the rest.
@@ -295,9 +322,10 @@ export interface SimpleStreamOptions extends StreamOptions {
295
322
  serviceTier?: ServiceTierPreference;
296
323
  }
297
324
  /** Tier preferences a caller can express; providers map `"auto"` to their default lane. */
298
- export type ServiceTierPreference = "auto" | "flex" | "priority";
299
- export type StreamFunction<TApi extends Api = Api, TOptions extends StreamOptions = StreamOptions> = (model: Model<TApi>, context: Context, options?: TOptions) => AssistantMessageEventStream;
300
- export type ImagesFunction<TApi extends ImagesApi = ImagesApi, TOptions extends ImagesOptions = ImagesOptions> = (model: ImagesModel<TApi>, context: ImagesContext, options?: TOptions) => Promise<AssistantImages>;
325
+ export type ServiceTierPreference = "auto" | "flex" | "priority" | "ultrafast";
326
+ export type StreamFunction<TApi extends Api = Api, TOptions extends StreamOptions = StreamOptions> = (model: Model<TApi>, context: TranscriptContext, options?: TOptions) => AssistantMessageEventStream;
327
+ export type ImagesFunction<TOptions extends ImagesOptions = ImagesOptions> = (model: ImageModel<ImageApi>, context: ImagesContext, options?: TOptions) => Promise<AssistantImages>;
328
+ export type ClassifierFunction<TOptions extends ClassifierOptions = ClassifierOptions> = (model: ClassifierModel<ClassifierApi>, context: ClassifierContext, options?: TOptions) => Promise<ClassifierResult>;
301
329
  export interface TextSignatureV1 {
302
330
  v: 1;
303
331
  id: string;
@@ -337,7 +365,7 @@ export interface ToolCall {
337
365
  type: "toolCall";
338
366
  id: string;
339
367
  name: string;
340
- arguments: Record<string, any>;
368
+ arguments: JsonObject;
341
369
  /** Set by text tool-call middleware when a truncated call could not be recovered. Carriers of `incomplete` MUST NOT be executed. */
342
370
  incomplete?: true;
343
371
  /** Error explaining why an incomplete tool call could not be recovered. */
@@ -403,9 +431,23 @@ export interface PromptCacheDiagnostics {
403
431
  comparisonReusableTokens?: number;
404
432
  }
405
433
  export type StopReason = "pending" | "stop" | "length" | "toolUse" | "error" | "aborted" | "deferred";
406
- export type JsonValue = null | boolean | number | string | JsonValue[] | {
434
+ export type JsonValue = null | boolean | number | string | readonly JsonValue[] | JsonObject;
435
+ export type JsonObject = {
407
436
  [key: string]: JsonValue;
408
437
  };
438
+ type IsAny<T> = 0 extends 1 & T ? true : false;
439
+ type IsExactlyJsonValue<T> = [T] extends [JsonValue] ? ([JsonValue] extends [T] ? true : false) : false;
440
+ type IsJsonProperty<T> = IsAny<T> extends true ? false : unknown extends T ? false : [Exclude<T, undefined>] extends [never] ? true : IsJsonCompatible<Exclude<T, undefined>>;
441
+ type InvalidJsonKeys<T extends object> = {
442
+ [TKey in keyof T]-?: TKey extends string | number ? (IsJsonProperty<T[TKey]> extends true ? never : TKey) : TKey;
443
+ }[keyof T];
444
+ type IsJsonCompatible<T> = IsAny<T> extends true ? false : unknown extends T ? false : IsExactlyJsonValue<T> extends true ? true : T extends null | boolean | number | string ? true : T extends undefined ? false : T extends readonly (infer TItem)[] ? IsJsonCompatible<TItem> : T extends (...args: never[]) => unknown ? false : T extends object ? [InvalidJsonKeys<T>] extends [never] ? true : false : false;
445
+ /** The JSON representation of a typed in-memory value. Optional object properties remain optional. */
446
+ export type JsonRepresentation<T> = IsAny<T> extends true ? JsonValue : unknown extends T ? JsonValue : [T] extends [JsonValue] ? T : T extends readonly unknown[] ? {
447
+ [TKey in keyof T]: JsonRepresentation<Exclude<T[TKey], undefined>>;
448
+ } : T extends object ? {
449
+ [TKey in keyof T]: JsonRepresentation<Exclude<T[TKey], undefined>>;
450
+ } : never;
409
451
  export interface DeferredHandle {
410
452
  provider: string;
411
453
  modelId: string;
@@ -423,6 +465,33 @@ export type AssistantStopDetails = {
423
465
  } | {
424
466
  type: "sensitive";
425
467
  };
468
+ /**
469
+ * System instructions and tool declarations at one point in the transcript.
470
+ *
471
+ * The leading system message is the system prompt. Later system messages change it:
472
+ * `content` adds instructions from that point on, `sections` replace or remove named
473
+ * prompt sections, and `toolsAdded`/`toolsRemoved` change the tool set. Replaying
474
+ * every system message in order yields the current prompt and tools. Providers that
475
+ * accept system messages mid-conversation send each one in place; other providers
476
+ * rebuild the leading system message from the replayed state.
477
+ */
478
+ export interface SystemMessage {
479
+ role: "system";
480
+ /** Instruction text. On the leading message this is the base prompt; later, additional instructions. */
481
+ content: string | TextContent[];
482
+ /**
483
+ * Named, ordered prompt sections rendered verbatim after `content`. The leading message
484
+ * declares them; later messages replace sections by name, and `null` removes one. Keep
485
+ * each section self-delimiting (a tag, a heading) so the model can relate an update to
486
+ * the original. Avoid integer-like names; JSON objects reorder those.
487
+ */
488
+ sections?: Record<string, string | null>;
489
+ /** Complete definitions of tools that become available at this point. */
490
+ toolsAdded?: Tool[];
491
+ /** Tools that stop being available at this point. */
492
+ toolsRemoved?: ToolReference[];
493
+ timestamp: number;
494
+ }
426
495
  export interface UserMessage {
427
496
  role: "user";
428
497
  content: string | (TextContent | ImageContent)[];
@@ -445,6 +514,8 @@ export interface AssistantMessage {
445
514
  responseId?: string;
446
515
  /** Exact provider-native effort level used for this response. Absent for legacy or unmanaged responses. */
447
516
  providerThinkingLevel?: string;
517
+ /** Pi thinking level the agent loop requested for this response. Absent outside the agent loop and for legacy responses. */
518
+ thinkingLevel?: ModelThinkingLevel;
448
519
  diagnostics?: AssistantMessageDiagnostic[];
449
520
  /** Provider prompt-cache comparison against the previous response of the same model, when reported. */
450
521
  promptCacheDiagnostics?: PromptCacheDiagnostics;
@@ -465,24 +536,45 @@ export interface AssistantMessage {
465
536
  endTurn?: boolean;
466
537
  timestamp: number;
467
538
  }
468
- export interface ToolResultMessage<TDetails = any> {
539
+ /** A tool call that another tool made while it ran, for example from a codemode script. */
540
+ export interface NestedToolCallRecord {
541
+ id: string;
542
+ name: string;
543
+ /** Omitted when over the size limits; `argumentsBytes` then gives their size. */
544
+ arguments?: JsonObject;
545
+ /** UTF-8 size of the arguments as JSON, set when `arguments` is omitted. */
546
+ argumentsBytes?: number;
547
+ /** `unfinished`: the call was still running when the calling tool finished. */
548
+ status: "ok" | "error" | "unfinished";
549
+ durationMs?: number;
550
+ /** Error text, truncated. */
551
+ error?: string;
552
+ }
553
+ /** Bounded record of the nested calls a tool made. Results are not recorded. */
554
+ export interface NestedToolCalls {
555
+ calls: NestedToolCallRecord[];
556
+ /** False when calls were dropped, arguments omitted, or calls had not finished. */
557
+ complete: boolean;
558
+ }
559
+ export type ToolResultMessage<TDetails = JsonValue> = IsJsonCompatible<TDetails> extends true ? {
469
560
  role: "toolResult";
470
561
  toolCallId: string;
471
562
  toolName: string;
472
563
  content: (TextContent | ImageContent)[];
473
- details?: TDetails;
564
+ details?: JsonRepresentation<TDetails>;
474
565
  /** Usage from the tool execution itself, if available. Not part of main LLM context accounting. */
475
566
  usage?: Usage;
567
+ /** Calls this tool made to other tools. Kept for the session record; not sent to the model. */
568
+ nestedCalls?: NestedToolCalls;
476
569
  /**
477
- * Names from `Context.tools` that became available after this result.
478
- * Providers with native deferred tool loading use this as the load point;
479
- * other providers ignore it and use `Context.tools` normally.
570
+ * Names from `Context.tools` that became available after this result. Fork tool-search
571
+ * native deferred loading (tool_reference / tool_search / kimi) uses this as the load point.
480
572
  */
481
573
  addedToolNames?: string[];
482
574
  isError: boolean;
483
575
  timestamp: number;
484
- }
485
- export type Message = UserMessage | AssistantMessage | ToolResultMessage | ConfigurationUpdateMessage;
576
+ } : never;
577
+ export type Message = SystemMessage | UserMessage | AssistantMessage | ToolResultMessage | ConfigurationUpdateMessage;
486
578
  export type ImagesInputContent = TextContent | ImageContent;
487
579
  export type ImagesOutputContent = TextContent | ImageContent;
488
580
  export interface ImagesContext {
@@ -490,8 +582,8 @@ export interface ImagesContext {
490
582
  }
491
583
  export type ImagesStopReason = "stop" | "error" | "aborted";
492
584
  export interface AssistantImages {
493
- api: ImagesApi;
494
- provider: ImagesProviderId;
585
+ api: ImageApi;
586
+ provider: ProviderId;
495
587
  model: string;
496
588
  output: ImagesOutputContent[];
497
589
  responseId?: string;
@@ -502,6 +594,57 @@ export interface AssistantImages {
502
594
  errorMessage?: string;
503
595
  timestamp: number;
504
596
  }
597
+ export interface ClassifierChoiceQuestion {
598
+ type: "choice";
599
+ instructions: string;
600
+ criteria: Record<string, string>;
601
+ }
602
+ export interface ClassifierScoreQuestion {
603
+ type: "score";
604
+ instructions: string;
605
+ criteria: string[];
606
+ }
607
+ export interface ClassifierBoolQuestion {
608
+ type: "bool";
609
+ instructions: string;
610
+ criteria: {
611
+ true: string;
612
+ false: string;
613
+ };
614
+ }
615
+ export type ClassifierQuestion = ClassifierChoiceQuestion | ClassifierScoreQuestion | ClassifierBoolQuestion;
616
+ export interface ClassifierContext {
617
+ state: JsonObject;
618
+ questions: Record<string, ClassifierQuestion>;
619
+ }
620
+ export interface ClassifierChoiceAnswer {
621
+ type: "choice";
622
+ choice: string;
623
+ probabilities: Record<string, number>;
624
+ confidence: number;
625
+ }
626
+ export interface ClassifierScoreAnswer {
627
+ type: "score";
628
+ score: number;
629
+ confidence: number;
630
+ }
631
+ export interface ClassifierBoolAnswer {
632
+ type: "bool";
633
+ probability: number;
634
+ }
635
+ export type ClassifierAnswer = ClassifierChoiceAnswer | ClassifierScoreAnswer | ClassifierBoolAnswer;
636
+ export type ClassifierStopReason = "stop" | "error" | "aborted";
637
+ export interface ClassifierResult {
638
+ api: ClassifierApi;
639
+ provider: ProviderId;
640
+ model: string;
641
+ answers: Record<string, ClassifierAnswer>;
642
+ /** Token usage and its cost at the model's catalog price, when the service reports token counts. */
643
+ usage?: Usage;
644
+ stopReason: ClassifierStopReason;
645
+ errorMessage?: string;
646
+ timestamp: number;
647
+ }
505
648
  import type { TSchema } from "typebox";
506
649
  export interface FreeformToolFormat {
507
650
  type: "grammar";
@@ -532,6 +675,15 @@ export interface Tool<TParameters extends TSchema = TSchema> {
532
675
  freeform?: FreeformToolFormat;
533
676
  constrainedSampling?: false | ConstrainedSamplingConfig;
534
677
  }
678
+ export interface ToolReference {
679
+ name: string;
680
+ }
681
+ /**
682
+ * Request input accepted by the public stream entry points (`Models.stream()`,
683
+ * `streamSimple()`, ...). `systemPrompt` and `tools` are shorthand for a leading
684
+ * system message; `normalizeContext()` folds them into one before the request
685
+ * reaches a provider.
686
+ */
535
687
  export interface Context {
536
688
  systemPrompt?: string;
537
689
  messages: Message[];
@@ -543,6 +695,19 @@ export interface Context {
543
695
  */
544
696
  activeToolNames?: string[];
545
697
  }
698
+ declare const transcriptContextBrand: unique symbol;
699
+ /**
700
+ * Normalized request context passed to providers and API implementations. The
701
+ * prompt and tool declarations are carried by the transcript's system messages.
702
+ * Only `normalizeContext()` produces this type, so a raw `Context` cannot reach
703
+ * provider code by accident.
704
+ */
705
+ export type TranscriptContext = {
706
+ messages: Message[];
707
+ /** Carried from `Context.activeToolNames` by `normalizeContext()` (senpi#2095); absent means every declared tool is callable. */
708
+ activeToolNames?: string[];
709
+ readonly [transcriptContextBrand]: true;
710
+ };
546
711
  /**
547
712
  * Event protocol for AssistantMessageEventStream.
548
713
  *
@@ -676,7 +841,11 @@ export interface OpenAICompletionsCompat {
676
841
  supportsThinkingTokenBudget?: boolean;
677
842
  /** Whether the provider supports OpenAI custom tools with Lark/regex grammar formats. When false, grammar-constrained tools fall back to normal function tools. Default: false; the generated model catalog enables it for capable models. */
678
843
  supportsOpenAIGrammarTools?: boolean;
679
- /** Whether the provider supports the `strict` field in tool definitions. Default: true. */
844
+ /** Whether the exact model accepts system or developer messages after the conversation has started. When false, later system messages are folded into the leading system message. Default: false; the generated model catalog enables it for verified models. */
845
+ supportsMidConvoSystemMessages?: boolean;
846
+ /** Whether system messages can introduce additional tools mid-conversation. Requires `supportsMidConvoSystemMessages`. Default: false; the generated model catalog enables it for capable models. */
847
+ supportsMidConvoToolAdditions?: boolean;
848
+ /** Whether the provider supports the `strict` field in tool definitions. Default: false; generated capable models enable it explicitly. */
680
849
  supportsStrictMode?: boolean;
681
850
  /**
682
851
  * Provider-specific JSON Schema flavor for tool parameters. `"moonshot-mfjs"`
@@ -693,7 +862,7 @@ export interface OpenAICompletionsCompat {
693
862
  cacheControlFormat?: "anthropic";
694
863
  /** Whether to send session-affinity data from `options.sessionId`. Default: true for OpenRouter endpoints, false otherwise. */
695
864
  sendSessionAffinityHeaders?: boolean;
696
- /** Provider-specific deferred tool serialization mode. */
865
+ /** Provider-specific deferred tool serialization mode (fork tool-search native loading). */
697
866
  deferredToolsMode?: "kimi";
698
867
  /** Session-affinity header format: `openai` sends `session_id`, `x-client-request-id`, and `x-session-affinity`; `openai-nosession` sends `x-client-request-id` and `x-session-affinity`; `openrouter` sends `x-session-id`. Does not affect the `prompt_cache_key` body param, which is governed by cache retention. Default: auto-detected. */
699
868
  sessionAffinityFormat?: SessionAffinityFormat;
@@ -783,6 +952,10 @@ export interface AnthropicMessagesCompat {
783
952
  supportsStrictTools?: boolean;
784
953
  /** Whether the exact model transport supports effort-only system messages and thinking binding controls. Default: false. */
785
954
  supportsMidConvoEffort?: boolean;
955
+ /** Whether the exact model accepts system-role messages inside the conversation. When false, later system messages are folded into the top-level system prompt. Default: false. */
956
+ supportsMidConvoSystemMessages?: boolean;
957
+ /** Whether the exact model accepts mid-conversation `tool_addition` and `tool_removal` blocks. Requires `supportsMidConvoSystemMessages`. Default: false. */
958
+ supportsMidConvoToolChanges?: boolean;
786
959
  /**
787
960
  * How to replay thinking blocks that have no usable Anthropic signature.
788
961
  * `"text"` demotes them to text; `"empty-signature"` preserves Anthropic's
@@ -818,6 +991,11 @@ export interface BedrockCompat {
818
991
  /** Whether the model supports Bedrock strict tool schemas. Default: false. */
819
992
  supportsStrictMode?: boolean;
820
993
  }
994
+ /** Compatibility settings for the Mistral chat API. */
995
+ export interface MistralConversationsCompat {
996
+ /** Whether the exact model accepts system messages after the conversation has started. When false, later system messages are folded into the leading system message. Default: false. */
997
+ supportsMidConvoSystemMessages?: boolean;
998
+ }
821
999
  /**
822
1000
  * OpenRouter provider routing preferences.
823
1001
  * Controls which upstream providers OpenRouter routes requests to.
@@ -911,14 +1089,70 @@ export interface ModelCost extends ModelCostRates {
911
1089
  /** Request-wide pricing tiers. The highest matching input threshold applies to the full request. */
912
1090
  tiers?: ModelCostTier[];
913
1091
  }
1092
+ export interface ModelImageResizeOptions {
1093
+ maxWidth?: number;
1094
+ maxHeight?: number;
1095
+ /** Maximum base64-encoded payload size in bytes. */
1096
+ maxBytes?: number;
1097
+ jpegQuality?: number;
1098
+ }
1099
+ export interface ModelImageInputLimits {
1100
+ /** Cache-safe resize profile applied before a new image enters conversation history. */
1101
+ resize?: ModelImageResizeOptions;
1102
+ /** Maximum images accepted in one provider message. */
1103
+ maxPerMessage?: number;
1104
+ /** Maximum images accepted across one provider request. */
1105
+ maxPerRequest?: number;
1106
+ }
1107
+ export interface ModelInputLimits {
1108
+ /** Maximum serialized provider request size in bytes. */
1109
+ maxRequestBytes?: number;
1110
+ images?: ModelImageInputLimits;
1111
+ }
1112
+ /** Fields shared by every catalog entry, regardless of what you can do with it. */
1113
+ export interface BaseModel<TApi extends string> {
1114
+ id: string;
1115
+ name: string;
1116
+ api: TApi;
1117
+ provider: ProviderId;
1118
+ baseUrl: string;
1119
+ /** `"video"` rides the image block for models that declare it (see `isVideoMimeType`). */
1120
+ input: ("text" | "image" | "video")[];
1121
+ /** Provider input limits and cache-safe preprocessing metadata. */
1122
+ inputLimits?: ModelInputLimits;
1123
+ cost: ModelCost;
1124
+ headers?: Record<string, string>;
1125
+ }
914
1126
  export interface ImagesModelCost extends ModelCost {
915
1127
  /** $/million image input tokens (reference and edit inputs). Falls back to `input` when absent. */
916
1128
  imageInput?: number;
917
1129
  }
918
- export interface ImagesModel<TApi extends ImagesApi> extends Omit<Model<Api>, "api" | "provider" | "reasoning" | "contextWindow" | "maxTokens" | "compat" | "cost"> {
919
- api: TApi;
920
- provider: ImagesProviderId;
1130
+ /** Image-generation model: usable with `generateImages()` only. */
1131
+ export interface ImageModel<TApi extends ImageApi> extends BaseModel<TApi> {
1132
+ type: "image";
1133
+ /** Output modalities. Always includes `"image"`; `"text"` means the model can also return text blocks. */
921
1134
  output: ("text" | "image")[];
922
1135
  cost: ImagesModelCost;
923
1136
  }
1137
+ /** Fork alias of {@link KnownImageApi}. */
1138
+ export type KnownImagesApi = KnownImageApi;
1139
+ /** Fork alias of {@link ImageApi}. */
1140
+ export type ImagesApi = ImageApi;
1141
+ /** Fork alias of {@link ImageModel}; entries must carry `type: "image"`. */
1142
+ export type ImagesModel<TApi extends ImageApi = ImageApi> = ImageModel<TApi>;
1143
+ /** Structured classifier model: usable with `classify()` only. */
1144
+ export interface ClassifierModel<TApi extends ClassifierApi> extends BaseModel<TApi> {
1145
+ type: "classifier";
1146
+ contextWindow: number;
1147
+ }
1148
+ /** Model shape for each model type. */
1149
+ export interface ModelTypeMap {
1150
+ chat: Model<Api>;
1151
+ image: ImageModel<ImageApi>;
1152
+ classifier: ClassifierModel<ClassifierApi>;
1153
+ }
1154
+ /** What a catalog entry is for. Decides which `Models` operation accepts it. */
1155
+ export type ModelType = keyof ModelTypeMap;
1156
+ /** Anything a provider can list. Narrow with `isModelType()`. */
1157
+ export type AnyModel = ModelTypeMap[ModelType];
924
1158
  //# sourceMappingURL=types.d.ts.map
@@ -1,3 +1,4 @@
1
+ import type { JsonObject } from "../types.ts";
1
2
  export interface DiagnosticErrorInfo {
2
3
  name?: string;
3
4
  message: string;
@@ -8,11 +9,11 @@ export interface AssistantMessageDiagnostic {
8
9
  type: string;
9
10
  timestamp: number;
10
11
  error?: DiagnosticErrorInfo;
11
- details?: Record<string, unknown>;
12
+ details?: JsonObject;
12
13
  }
13
14
  export declare function formatThrownValue(value: unknown): string;
14
15
  export declare function extractDiagnosticError(error: unknown): DiagnosticErrorInfo;
15
- export declare function createAssistantMessageDiagnostic(type: string, error: unknown, details?: Record<string, unknown>): AssistantMessageDiagnostic;
16
+ export declare function createAssistantMessageDiagnostic(type: string, error: unknown, details?: JsonObject): AssistantMessageDiagnostic;
16
17
  export declare function appendAssistantMessageDiagnostic<T extends {
17
18
  diagnostics?: AssistantMessageDiagnostic[];
18
19
  }>(message: T, diagnostic: AssistantMessageDiagnostic): void;
@@ -1,4 +1,4 @@
1
- import type { Context, ImageContent, Message, TextContent, Usage } from "../types.ts";
1
+ import type { ImageContent, Message, TextContent, TranscriptContext, Usage } from "../types.ts";
2
2
  export interface ContextUsageEstimate {
3
3
  /** Estimated total context tokens. */
4
4
  tokens: number;
@@ -13,5 +13,5 @@ export declare function calculateContextTokens(usage: Usage): number;
13
13
  export declare function estimateTextTokens(text: string): number;
14
14
  export declare function estimateTextAndImageContentTokens(content: string | Array<TextContent | ImageContent>): number;
15
15
  export declare function estimateMessageTokens(message: Message): number;
16
- export declare function estimateContextTokens(context: Context | readonly Message[]): ContextUsageEstimate;
16
+ export declare function estimateContextTokens(context: TranscriptContext | readonly Message[]): ContextUsageEstimate;
17
17
  //# sourceMappingURL=estimate.d.ts.map
@@ -1,3 +1,5 @@
1
+ import { getSystemMessageText } from "./text.js";
2
+ import { getCurrentTools } from "./transcript.js";
1
3
  const CHARS_PER_TOKEN = 4;
2
4
  const ESTIMATED_IMAGE_CHARS = 4800;
3
5
  export function calculateContextTokens(usage) {
@@ -27,6 +29,11 @@ export function estimateTextAndImageContentTokens(content) {
27
29
  }
28
30
  export function estimateMessageTokens(message) {
29
31
  let chars = 0;
32
+ if (message.role === "system") {
33
+ return (estimateTextTokens(getSystemMessageText(message)) +
34
+ estimateToolsTokens(message.toolsAdded) +
35
+ estimateToolsTokens(message.toolsRemoved));
36
+ }
30
37
  if (message.role === "user")
31
38
  return estimateTextAndImageContentTokens(message.content);
32
39
  if (message.role === "toolResult")
@@ -70,11 +77,12 @@ function getLastAssistantUsageInfo(messages) {
70
77
  }
71
78
  return usageInfo;
72
79
  }
73
- function estimateMessages(messages) {
80
+ export function estimateContextTokens(context) {
81
+ const messages = "messages" in context ? context.messages : context;
74
82
  const usageInfo = getLastAssistantUsageInfo(messages);
75
83
  if (usageInfo) {
76
84
  const usageTokens = calculateContextTokens(usageInfo.usage);
77
- let trailingTokens = 0;
85
+ let trailingTokens = estimateLazilyAddedToolsTokens(messages, usageInfo.index);
78
86
  for (let i = usageInfo.index + 1; i < messages.length; i++) {
79
87
  trailingTokens += estimateMessageTokens(messages[i]);
80
88
  }
@@ -85,37 +93,21 @@ function estimateMessages(messages) {
85
93
  tokens += estimateMessageTokens(message);
86
94
  return { tokens, usageTokens: 0, trailingTokens: tokens, lastUsageIndex: null };
87
95
  }
96
+ /**
97
+ * Tools a later tool result activated (`addedToolNames`, fork lazy activation) are declared in the
98
+ * leading system message, before the last billed usage, yet were never billed: count them as trailing.
99
+ */
100
+ function estimateLazilyAddedToolsTokens(messages, lastUsageIndex) {
101
+ const addedNames = new Set(messages
102
+ .slice(lastUsageIndex + 1)
103
+ .flatMap((message) => (message.role === "toolResult" ? (message.addedToolNames ?? []) : [])));
104
+ if (addedNames.size === 0)
105
+ return 0;
106
+ return estimateToolsTokens(getCurrentTools(messages).filter((tool) => addedNames.has(tool.name)));
107
+ }
88
108
  function estimateToolsTokens(tools) {
89
109
  if (!tools || tools.length === 0)
90
110
  return 0;
91
111
  return estimateTextTokens(safeJsonStringify(tools));
92
112
  }
93
- function isMessageArray(value) {
94
- return Array.isArray(value);
95
- }
96
- export function estimateContextTokens(context) {
97
- if (isMessageArray(context))
98
- return estimateMessages(context);
99
- const estimate = estimateMessages(context.messages);
100
- if (estimate.lastUsageIndex !== null) {
101
- const addedNames = new Set(context.messages
102
- .slice(estimate.lastUsageIndex + 1)
103
- .filter((message) => message.role === "toolResult")
104
- .flatMap((message) => message.addedToolNames ?? []));
105
- const addedToolTokens = estimateToolsTokens(context.tools?.filter((tool) => addedNames.has(tool.name)));
106
- return {
107
- tokens: estimate.tokens + addedToolTokens,
108
- usageTokens: estimate.usageTokens,
109
- trailingTokens: estimate.trailingTokens + addedToolTokens,
110
- lastUsageIndex: estimate.lastUsageIndex,
111
- };
112
- }
113
- const prefixTokens = (context.systemPrompt ? estimateTextTokens(context.systemPrompt) : 0) + estimateToolsTokens(context.tools);
114
- return {
115
- tokens: estimate.tokens + prefixTokens,
116
- usageTokens: estimate.usageTokens,
117
- trailingTokens: estimate.trailingTokens + prefixTokens,
118
- lastUsageIndex: estimate.lastUsageIndex,
119
- };
120
- }
121
113
  //# sourceMappingURL=estimate.js.map
@@ -1,4 +1,4 @@
1
1
  import type { ProviderHeaders } from "../types.ts";
2
2
  export declare function headersToRecord(headers: Headers): Record<string, string>;
3
- export declare function providerHeadersToRecord(headers: ProviderHeaders | undefined): Record<string, string> | undefined;
3
+ export declare function providerHeadersToRecord(...headerSources: (ProviderHeaders | undefined)[]): Record<string, string> | undefined;
4
4
  //# sourceMappingURL=headers.d.ts.map
@@ -5,14 +5,16 @@ export function headersToRecord(headers) {
5
5
  }
6
6
  return result;
7
7
  }
8
- export function providerHeadersToRecord(headers) {
9
- if (!headers)
10
- return undefined;
11
- const result = {};
12
- for (const [key, value] of Object.entries(headers)) {
13
- if (value !== null)
14
- result[key] = value;
8
+ export function providerHeadersToRecord(...headerSources) {
9
+ const merged = new Map();
10
+ for (const source of headerSources) {
11
+ for (const [name, value] of Object.entries(source ?? {})) {
12
+ const normalizedName = name.toLowerCase();
13
+ merged.delete(normalizedName);
14
+ if (value !== null)
15
+ merged.set(normalizedName, [name, value]);
16
+ }
15
17
  }
16
- return Object.keys(result).length > 0 ? result : undefined;
18
+ return merged.size > 0 ? Object.fromEntries(merged.values()) : undefined;
17
19
  }
18
20
  //# sourceMappingURL=headers.js.map
@@ -0,0 +1,11 @@
1
+ import type { AnyModel, Api, AssistantImages, ClassifierApi, ClassifierModel, ClassifierResult, ImageApi, ImageModel, Model, ModelType, ModelTypeMap } from "../types.ts";
2
+ /** The type of a model. Models without `type` are chat models. */
3
+ export declare function getModelType(model: AnyModel): ModelType;
4
+ /** Runtime-checked model type narrowing, including legacy chat models without `type`. */
5
+ export declare function isModelType<TType extends ModelType>(model: AnyModel, type: TType): model is ModelTypeMap[TType];
6
+ export declare function assertChatModel(model: AnyModel): asserts model is Model<Api>;
7
+ export declare function assertImageModel(model: AnyModel): asserts model is ImageModel<ImageApi>;
8
+ export declare function assertClassifierModel(model: AnyModel): asserts model is ClassifierModel<ClassifierApi>;
9
+ export declare function imageErrorResult(model: ImageModel<ImageApi>, error: unknown, aborted?: boolean): AssistantImages;
10
+ export declare function classifierErrorResult(model: ClassifierModel<ClassifierApi>, error: unknown, aborted?: boolean): ClassifierResult;
11
+ //# sourceMappingURL=model-operations.d.ts.map