@gajae-code/ai 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (349) hide show
  1. package/CHANGELOG.md +2644 -0
  2. package/README.md +1181 -0
  3. package/dist/types/api-registry.d.ts +30 -0
  4. package/dist/types/auth-broker/client.d.ts +66 -0
  5. package/dist/types/auth-broker/index.d.ts +5 -0
  6. package/dist/types/auth-broker/refresher.d.ts +25 -0
  7. package/dist/types/auth-broker/remote-store.d.ts +96 -0
  8. package/dist/types/auth-broker/server.d.ts +32 -0
  9. package/dist/types/auth-broker/types.d.ts +105 -0
  10. package/dist/types/auth-broker/wire-schemas.d.ts +412 -0
  11. package/dist/types/auth-gateway/http.d.ts +39 -0
  12. package/dist/types/auth-gateway/index.d.ts +3 -0
  13. package/dist/types/auth-gateway/server.d.ts +17 -0
  14. package/dist/types/auth-gateway/types.d.ts +115 -0
  15. package/dist/types/auth-storage.d.ts +641 -0
  16. package/dist/types/cli.d.ts +2 -0
  17. package/dist/types/index.d.ts +49 -0
  18. package/dist/types/model-cache.d.ts +17 -0
  19. package/dist/types/model-manager.d.ts +62 -0
  20. package/dist/types/model-thinking.d.ts +71 -0
  21. package/dist/types/models.d.ts +12 -0
  22. package/dist/types/provider-details.d.ts +24 -0
  23. package/dist/types/provider-models/bundled-references.d.ts +4 -0
  24. package/dist/types/provider-models/descriptors.d.ts +48 -0
  25. package/dist/types/provider-models/google.d.ts +20 -0
  26. package/dist/types/provider-models/index.d.ts +5 -0
  27. package/dist/types/provider-models/ollama.d.ts +7 -0
  28. package/dist/types/provider-models/openai-compat.d.ts +237 -0
  29. package/dist/types/provider-models/special.d.ts +16 -0
  30. package/dist/types/providers/amazon-bedrock.d.ts +36 -0
  31. package/dist/types/providers/anthropic-messages-server-schema.d.ts +450 -0
  32. package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
  33. package/dist/types/providers/anthropic.d.ts +188 -0
  34. package/dist/types/providers/aws-credentials.d.ts +43 -0
  35. package/dist/types/providers/aws-eventstream.d.ts +38 -0
  36. package/dist/types/providers/aws-sigv4.d.ts +55 -0
  37. package/dist/types/providers/azure-openai-responses.d.ts +15 -0
  38. package/dist/types/providers/cursor/gen/agent_pb.d.ts +13022 -0
  39. package/dist/types/providers/cursor.d.ts +42 -0
  40. package/dist/types/providers/error-message.d.ts +27 -0
  41. package/dist/types/providers/github-copilot-headers.d.ts +40 -0
  42. package/dist/types/providers/gitlab-duo.d.ts +27 -0
  43. package/dist/types/providers/google-auth.d.ts +24 -0
  44. package/dist/types/providers/google-gemini-cli.d.ts +72 -0
  45. package/dist/types/providers/google-gemini-headers.d.ts +18 -0
  46. package/dist/types/providers/google-shared.d.ts +163 -0
  47. package/dist/types/providers/google-types.d.ts +138 -0
  48. package/dist/types/providers/google-vertex.d.ts +7 -0
  49. package/dist/types/providers/google.d.ts +4 -0
  50. package/dist/types/providers/grammar.d.ts +1 -0
  51. package/dist/types/providers/kimi.d.ts +27 -0
  52. package/dist/types/providers/mock.d.ts +175 -0
  53. package/dist/types/providers/ollama.d.ts +6 -0
  54. package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
  55. package/dist/types/providers/openai-chat-server-schema.d.ts +814 -0
  56. package/dist/types/providers/openai-chat-server.d.ts +16 -0
  57. package/dist/types/providers/openai-codex/constants.d.ts +26 -0
  58. package/dist/types/providers/openai-codex/request-transformer.d.ts +49 -0
  59. package/dist/types/providers/openai-codex/response-handler.d.ts +17 -0
  60. package/dist/types/providers/openai-codex-responses.d.ts +67 -0
  61. package/dist/types/providers/openai-completions-compat.d.ts +25 -0
  62. package/dist/types/providers/openai-completions.d.ts +33 -0
  63. package/dist/types/providers/openai-responses-server-schema.d.ts +392 -0
  64. package/dist/types/providers/openai-responses-server.d.ts +17 -0
  65. package/dist/types/providers/openai-responses-shared.d.ts +89 -0
  66. package/dist/types/providers/openai-responses.d.ts +32 -0
  67. package/dist/types/providers/pi-native-client.d.ts +13 -0
  68. package/dist/types/providers/pi-native-server.d.ts +68 -0
  69. package/dist/types/providers/register-builtins.d.ts +31 -0
  70. package/dist/types/providers/synthetic.d.ts +26 -0
  71. package/dist/types/providers/transform-messages.d.ts +12 -0
  72. package/dist/types/providers/vision-guard.d.ts +8 -0
  73. package/dist/types/rate-limit-utils.d.ts +19 -0
  74. package/dist/types/stream.d.ts +24 -0
  75. package/dist/types/types.d.ts +746 -0
  76. package/dist/types/usage/claude.d.ts +3 -0
  77. package/dist/types/usage/gemini.d.ts +2 -0
  78. package/dist/types/usage/github-copilot.d.ts +7 -0
  79. package/dist/types/usage/google-antigravity.d.ts +2 -0
  80. package/dist/types/usage/kimi.d.ts +2 -0
  81. package/dist/types/usage/minimax-code.d.ts +2 -0
  82. package/dist/types/usage/openai-codex.d.ts +3 -0
  83. package/dist/types/usage/shared.d.ts +1 -0
  84. package/dist/types/usage/zai.d.ts +2 -0
  85. package/dist/types/usage.d.ts +258 -0
  86. package/dist/types/utils/abort.d.ts +19 -0
  87. package/dist/types/utils/anthropic-auth.d.ts +31 -0
  88. package/dist/types/utils/discovery/antigravity.d.ts +61 -0
  89. package/dist/types/utils/discovery/codex.d.ts +38 -0
  90. package/dist/types/utils/discovery/cursor.d.ts +23 -0
  91. package/dist/types/utils/discovery/gemini.d.ts +25 -0
  92. package/dist/types/utils/discovery/index.d.ts +4 -0
  93. package/dist/types/utils/discovery/openai-compatible.d.ts +72 -0
  94. package/dist/types/utils/event-stream.d.ts +28 -0
  95. package/dist/types/utils/fireworks-model-id.d.ts +10 -0
  96. package/dist/types/utils/foundry.d.ts +1 -0
  97. package/dist/types/utils/h2-fetch.d.ts +22 -0
  98. package/dist/types/utils/http-inspector.d.ts +31 -0
  99. package/dist/types/utils/idle-iterator.d.ts +67 -0
  100. package/dist/types/utils/json-parse.d.ts +10 -0
  101. package/dist/types/utils/oauth/alibaba-coding-plan.d.ts +18 -0
  102. package/dist/types/utils/oauth/anthropic.d.ts +22 -0
  103. package/dist/types/utils/oauth/api-key-login.d.ts +35 -0
  104. package/dist/types/utils/oauth/api-key-validation.d.ts +27 -0
  105. package/dist/types/utils/oauth/callback-server.d.ts +57 -0
  106. package/dist/types/utils/oauth/cerebras.d.ts +1 -0
  107. package/dist/types/utils/oauth/cloudflare-ai-gateway.d.ts +18 -0
  108. package/dist/types/utils/oauth/cursor.d.ts +15 -0
  109. package/dist/types/utils/oauth/deepseek.d.ts +10 -0
  110. package/dist/types/utils/oauth/firepass.d.ts +1 -0
  111. package/dist/types/utils/oauth/fireworks.d.ts +1 -0
  112. package/dist/types/utils/oauth/github-copilot.d.ts +38 -0
  113. package/dist/types/utils/oauth/gitlab-duo.d.ts +3 -0
  114. package/dist/types/utils/oauth/google-antigravity.d.ts +11 -0
  115. package/dist/types/utils/oauth/google-gemini-cli.d.ts +10 -0
  116. package/dist/types/utils/oauth/google-oauth-shared.d.ts +28 -0
  117. package/dist/types/utils/oauth/huggingface.d.ts +19 -0
  118. package/dist/types/utils/oauth/index.d.ts +38 -0
  119. package/dist/types/utils/oauth/kagi.d.ts +17 -0
  120. package/dist/types/utils/oauth/kilo.d.ts +5 -0
  121. package/dist/types/utils/oauth/kimi.d.ts +21 -0
  122. package/dist/types/utils/oauth/litellm.d.ts +18 -0
  123. package/dist/types/utils/oauth/lm-studio.d.ts +17 -0
  124. package/dist/types/utils/oauth/minimax-code.d.ts +28 -0
  125. package/dist/types/utils/oauth/moonshot.d.ts +1 -0
  126. package/dist/types/utils/oauth/nanogpt.d.ts +1 -0
  127. package/dist/types/utils/oauth/nvidia.d.ts +18 -0
  128. package/dist/types/utils/oauth/ollama-cloud.d.ts +2 -0
  129. package/dist/types/utils/oauth/ollama.d.ts +18 -0
  130. package/dist/types/utils/oauth/openai-codex.d.ts +21 -0
  131. package/dist/types/utils/oauth/opencode.d.ts +18 -0
  132. package/dist/types/utils/oauth/parallel.d.ts +17 -0
  133. package/dist/types/utils/oauth/perplexity.d.ts +9 -0
  134. package/dist/types/utils/oauth/pkce.d.ts +8 -0
  135. package/dist/types/utils/oauth/qianfan.d.ts +17 -0
  136. package/dist/types/utils/oauth/qwen-portal.d.ts +19 -0
  137. package/dist/types/utils/oauth/synthetic.d.ts +1 -0
  138. package/dist/types/utils/oauth/tavily.d.ts +17 -0
  139. package/dist/types/utils/oauth/together.d.ts +1 -0
  140. package/dist/types/utils/oauth/types.d.ts +44 -0
  141. package/dist/types/utils/oauth/venice.d.ts +18 -0
  142. package/dist/types/utils/oauth/vercel-ai-gateway.d.ts +18 -0
  143. package/dist/types/utils/oauth/vllm.d.ts +16 -0
  144. package/dist/types/utils/oauth/xiaomi.d.ts +19 -0
  145. package/dist/types/utils/oauth/zai.d.ts +18 -0
  146. package/dist/types/utils/oauth/zenmux.d.ts +1 -0
  147. package/dist/types/utils/overflow.d.ts +54 -0
  148. package/dist/types/utils/parse-bind.d.ts +23 -0
  149. package/dist/types/utils/provider-response.d.ts +3 -0
  150. package/dist/types/utils/retry-after.d.ts +3 -0
  151. package/dist/types/utils/retry.d.ts +26 -0
  152. package/dist/types/utils/schema/adapt.d.ts +24 -0
  153. package/dist/types/utils/schema/compatibility.d.ts +30 -0
  154. package/dist/types/utils/schema/dereference.d.ts +11 -0
  155. package/dist/types/utils/schema/draft.d.ts +10 -0
  156. package/dist/types/utils/schema/equality.d.ts +4 -0
  157. package/dist/types/utils/schema/fields.d.ts +49 -0
  158. package/dist/types/utils/schema/index.d.ts +13 -0
  159. package/dist/types/utils/schema/json-schema-validator.d.ts +12 -0
  160. package/dist/types/utils/schema/meta-validator.d.ts +2 -0
  161. package/dist/types/utils/schema/normalize.d.ts +93 -0
  162. package/dist/types/utils/schema/spill.d.ts +8 -0
  163. package/dist/types/utils/schema/stamps.d.ts +25 -0
  164. package/dist/types/utils/schema/types.d.ts +4 -0
  165. package/dist/types/utils/schema/wire.d.ts +54 -0
  166. package/dist/types/utils/schema/zod-decontaminate.d.ts +31 -0
  167. package/dist/types/utils/sse-debug.d.ts +10 -0
  168. package/dist/types/utils/tool-call-healing.d.ts +71 -0
  169. package/dist/types/utils/tool-choice.d.ts +50 -0
  170. package/dist/types/utils/validation.d.ts +17 -0
  171. package/dist/types/utils.d.ts +28 -0
  172. package/package.json +146 -0
  173. package/src/api-registry.ts +96 -0
  174. package/src/auth-broker/client.ts +358 -0
  175. package/src/auth-broker/index.ts +5 -0
  176. package/src/auth-broker/refresher.ts +127 -0
  177. package/src/auth-broker/remote-store.ts +623 -0
  178. package/src/auth-broker/server.ts +644 -0
  179. package/src/auth-broker/types.ts +127 -0
  180. package/src/auth-broker/wire-schemas.ts +200 -0
  181. package/src/auth-gateway/http.ts +194 -0
  182. package/src/auth-gateway/index.ts +3 -0
  183. package/src/auth-gateway/server.ts +717 -0
  184. package/src/auth-gateway/types.ts +134 -0
  185. package/src/auth-storage.ts +4104 -0
  186. package/src/cli.ts +262 -0
  187. package/src/index.ts +54 -0
  188. package/src/model-cache.ts +129 -0
  189. package/src/model-manager.ts +450 -0
  190. package/src/model-thinking.ts +691 -0
  191. package/src/models.json +73853 -0
  192. package/src/models.json.d.ts +9 -0
  193. package/src/models.ts +56 -0
  194. package/src/prompts/turn-aborted-guidance.md +4 -0
  195. package/src/provider-details.ts +90 -0
  196. package/src/provider-models/bundled-references.ts +38 -0
  197. package/src/provider-models/descriptors.ts +308 -0
  198. package/src/provider-models/google.ts +91 -0
  199. package/src/provider-models/index.ts +5 -0
  200. package/src/provider-models/ollama.ts +153 -0
  201. package/src/provider-models/openai-compat.ts +2275 -0
  202. package/src/provider-models/special.ts +67 -0
  203. package/src/providers/amazon-bedrock.ts +849 -0
  204. package/src/providers/anthropic-messages-server-schema.ts +229 -0
  205. package/src/providers/anthropic-messages-server.ts +677 -0
  206. package/src/providers/anthropic.ts +2696 -0
  207. package/src/providers/aws-credentials.ts +501 -0
  208. package/src/providers/aws-eventstream.ts +185 -0
  209. package/src/providers/aws-sigv4.ts +218 -0
  210. package/src/providers/azure-openai-responses.ts +337 -0
  211. package/src/providers/cursor/gen/agent_pb.ts +15274 -0
  212. package/src/providers/cursor/proto/agent.proto +3526 -0
  213. package/src/providers/cursor/proto/buf.gen.yaml +6 -0
  214. package/src/providers/cursor/proto/buf.yaml +17 -0
  215. package/src/providers/cursor.ts +2561 -0
  216. package/src/providers/error-message.ts +21 -0
  217. package/src/providers/github-copilot-headers.ts +140 -0
  218. package/src/providers/gitlab-duo.ts +372 -0
  219. package/src/providers/google-auth.ts +252 -0
  220. package/src/providers/google-gemini-cli.ts +795 -0
  221. package/src/providers/google-gemini-headers.ts +41 -0
  222. package/src/providers/google-shared.ts +902 -0
  223. package/src/providers/google-types.ts +167 -0
  224. package/src/providers/google-vertex.ts +88 -0
  225. package/src/providers/google.ts +41 -0
  226. package/src/providers/grammar.ts +70 -0
  227. package/src/providers/kimi.ts +52 -0
  228. package/src/providers/mock.ts +500 -0
  229. package/src/providers/ollama.ts +544 -0
  230. package/src/providers/openai-anthropic-shim.ts +138 -0
  231. package/src/providers/openai-chat-server-schema.ts +243 -0
  232. package/src/providers/openai-chat-server.ts +628 -0
  233. package/src/providers/openai-codex/constants.ts +43 -0
  234. package/src/providers/openai-codex/request-transformer.ts +161 -0
  235. package/src/providers/openai-codex/response-handler.ts +81 -0
  236. package/src/providers/openai-codex-responses.ts +2598 -0
  237. package/src/providers/openai-completions-compat.ts +279 -0
  238. package/src/providers/openai-completions.ts +1853 -0
  239. package/src/providers/openai-responses-server-schema.ts +290 -0
  240. package/src/providers/openai-responses-server.ts +1183 -0
  241. package/src/providers/openai-responses-shared.ts +800 -0
  242. package/src/providers/openai-responses.ts +621 -0
  243. package/src/providers/pi-native-client.ts +228 -0
  244. package/src/providers/pi-native-server.ts +210 -0
  245. package/src/providers/register-builtins.ts +412 -0
  246. package/src/providers/synthetic.ts +50 -0
  247. package/src/providers/transform-messages.ts +309 -0
  248. package/src/providers/vision-guard.ts +31 -0
  249. package/src/rate-limit-utils.ts +84 -0
  250. package/src/stream.ts +895 -0
  251. package/src/types.ts +884 -0
  252. package/src/usage/claude.ts +431 -0
  253. package/src/usage/gemini.ts +250 -0
  254. package/src/usage/github-copilot.ts +421 -0
  255. package/src/usage/google-antigravity.ts +201 -0
  256. package/src/usage/kimi.ts +271 -0
  257. package/src/usage/minimax-code.ts +31 -0
  258. package/src/usage/openai-codex.ts +503 -0
  259. package/src/usage/shared.ts +10 -0
  260. package/src/usage/zai.ts +247 -0
  261. package/src/usage.ts +183 -0
  262. package/src/utils/abort.ts +51 -0
  263. package/src/utils/anthropic-auth.ts +87 -0
  264. package/src/utils/discovery/antigravity.ts +261 -0
  265. package/src/utils/discovery/codex.ts +371 -0
  266. package/src/utils/discovery/cursor.ts +306 -0
  267. package/src/utils/discovery/gemini.ts +248 -0
  268. package/src/utils/discovery/index.ts +4 -0
  269. package/src/utils/discovery/openai-compatible.ts +224 -0
  270. package/src/utils/event-stream.ts +142 -0
  271. package/src/utils/fireworks-model-id.ts +30 -0
  272. package/src/utils/foundry.ts +8 -0
  273. package/src/utils/h2-fetch.ts +60 -0
  274. package/src/utils/http-inspector.ts +176 -0
  275. package/src/utils/idle-iterator.ts +250 -0
  276. package/src/utils/json-parse.ts +148 -0
  277. package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
  278. package/src/utils/oauth/anthropic.ts +200 -0
  279. package/src/utils/oauth/api-key-login.ts +87 -0
  280. package/src/utils/oauth/api-key-validation.ts +92 -0
  281. package/src/utils/oauth/callback-server.ts +276 -0
  282. package/src/utils/oauth/cerebras.ts +16 -0
  283. package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
  284. package/src/utils/oauth/cursor.ts +157 -0
  285. package/src/utils/oauth/deepseek.ts +53 -0
  286. package/src/utils/oauth/firepass.ts +24 -0
  287. package/src/utils/oauth/fireworks.ts +15 -0
  288. package/src/utils/oauth/github-copilot.ts +362 -0
  289. package/src/utils/oauth/gitlab-duo.ts +123 -0
  290. package/src/utils/oauth/google-antigravity.ts +200 -0
  291. package/src/utils/oauth/google-gemini-cli.ts +256 -0
  292. package/src/utils/oauth/google-oauth-shared.ts +110 -0
  293. package/src/utils/oauth/huggingface.ts +62 -0
  294. package/src/utils/oauth/index.ts +444 -0
  295. package/src/utils/oauth/kagi.ts +47 -0
  296. package/src/utils/oauth/kilo.ts +87 -0
  297. package/src/utils/oauth/kimi.ts +254 -0
  298. package/src/utils/oauth/litellm.ts +47 -0
  299. package/src/utils/oauth/lm-studio.ts +38 -0
  300. package/src/utils/oauth/minimax-code.ts +78 -0
  301. package/src/utils/oauth/moonshot.ts +16 -0
  302. package/src/utils/oauth/nanogpt.ts +15 -0
  303. package/src/utils/oauth/nvidia.ts +70 -0
  304. package/src/utils/oauth/oauth.html +199 -0
  305. package/src/utils/oauth/ollama-cloud.ts +28 -0
  306. package/src/utils/oauth/ollama.ts +47 -0
  307. package/src/utils/oauth/openai-codex.ts +299 -0
  308. package/src/utils/oauth/opencode.ts +49 -0
  309. package/src/utils/oauth/parallel.ts +46 -0
  310. package/src/utils/oauth/perplexity.ts +206 -0
  311. package/src/utils/oauth/pkce.ts +18 -0
  312. package/src/utils/oauth/qianfan.ts +58 -0
  313. package/src/utils/oauth/qwen-portal.ts +60 -0
  314. package/src/utils/oauth/synthetic.ts +16 -0
  315. package/src/utils/oauth/tavily.ts +46 -0
  316. package/src/utils/oauth/together.ts +16 -0
  317. package/src/utils/oauth/types.ts +94 -0
  318. package/src/utils/oauth/venice.ts +59 -0
  319. package/src/utils/oauth/vercel-ai-gateway.ts +47 -0
  320. package/src/utils/oauth/vllm.ts +40 -0
  321. package/src/utils/oauth/xiaomi.ts +137 -0
  322. package/src/utils/oauth/zai.ts +60 -0
  323. package/src/utils/oauth/zenmux.ts +15 -0
  324. package/src/utils/overflow.ts +137 -0
  325. package/src/utils/parse-bind.ts +54 -0
  326. package/src/utils/provider-response.ts +30 -0
  327. package/src/utils/retry-after.ts +110 -0
  328. package/src/utils/retry.ts +54 -0
  329. package/src/utils/schema/CONSTRAINTS.md +164 -0
  330. package/src/utils/schema/adapt.ts +36 -0
  331. package/src/utils/schema/compatibility.ts +435 -0
  332. package/src/utils/schema/dereference.ts +98 -0
  333. package/src/utils/schema/draft.ts +341 -0
  334. package/src/utils/schema/equality.ts +97 -0
  335. package/src/utils/schema/fields.ts +190 -0
  336. package/src/utils/schema/index.ts +13 -0
  337. package/src/utils/schema/json-schema-validator.ts +577 -0
  338. package/src/utils/schema/meta-validator.ts +167 -0
  339. package/src/utils/schema/normalize.ts +1588 -0
  340. package/src/utils/schema/spill.ts +43 -0
  341. package/src/utils/schema/stamps.ts +97 -0
  342. package/src/utils/schema/types.ts +11 -0
  343. package/src/utils/schema/wire.ts +213 -0
  344. package/src/utils/schema/zod-decontaminate.ts +331 -0
  345. package/src/utils/sse-debug.ts +289 -0
  346. package/src/utils/tool-call-healing.ts +271 -0
  347. package/src/utils/tool-choice.ts +99 -0
  348. package/src/utils/validation.ts +1019 -0
  349. package/src/utils.ts +166 -0
package/README.md ADDED
@@ -0,0 +1,1181 @@
1
+ # @gajae-code/ai
2
+
3
+ Unified LLM API with automatic model discovery, provider configuration, token and cost tracking, and simple context persistence and hand-off to other models mid-session.
4
+
5
+ **Note**: This library only includes models that support tool calling (function calling), as this is essential for agentic workflows.
6
+
7
+ ## Table of Contents
8
+
9
+ - [Supported Providers](#supported-providers)
10
+ - [Installation](#installation)
11
+ - [Quick Start](#quick-start)
12
+ - [Tools](#tools)
13
+ - [Defining Tools](#defining-tools)
14
+ - [Handling Tool Calls](#handling-tool-calls)
15
+ - [Streaming Tool Calls with Partial JSON](#streaming-tool-calls-with-partial-json)
16
+ - [Validating Tool Arguments](#validating-tool-arguments)
17
+ - [Complete Event Reference](#complete-event-reference)
18
+ - [Image Input](#image-input)
19
+ - [Thinking/Reasoning](#thinkingreasoning)
20
+ - [Unified Interface](#unified-interface-streamsimplecompletesimple)
21
+ - [Provider-Specific Options](#provider-specific-options-streamcomplete)
22
+ - [Streaming Thinking Content](#streaming-thinking-content)
23
+ - [Stop Reasons](#stop-reasons)
24
+ - [Error Handling](#error-handling)
25
+ - [Aborting Requests](#aborting-requests)
26
+ - [Continuing After Abort](#continuing-after-abort)
27
+ - [APIs, Models, and Providers](#apis-models-and-providers)
28
+ - [Providers and Models](#providers-and-models)
29
+ - [Querying Providers and Models](#querying-providers-and-models)
30
+ - [Custom Models](#custom-models)
31
+ - [OpenAI Compatibility Settings](#openai-compatibility-settings)
32
+ - [Type Safety](#type-safety)
33
+ - [Cross-Provider Handoffs](#cross-provider-handoffs)
34
+ - [Context Serialization](#context-serialization)
35
+ - [Browser Usage](#browser-usage)
36
+ - [Environment Variables](#environment-variables-nodejs-only)
37
+ - [Checking Environment Variables](#checking-environment-variables)
38
+ - [OAuth Providers](#oauth-providers)
39
+ - [Vertex AI (ADC)](#vertex-ai-adc)
40
+ - [CLI Login](#cli-login)
41
+ - [Programmatic OAuth](#programmatic-oauth)
42
+ - [Login Flow Example](#login-flow-example)
43
+ - [Using OAuth Tokens](#using-oauth-tokens)
44
+ - [Provider Notes](#provider-notes)
45
+ - [License](#license)
46
+
47
+ ## Supported Providers
48
+
49
+ - **OpenAI**
50
+ - **OpenAI code provider** (ChatGPT Plus/Pro subscription, requires OAuth, see below)
51
+ - **Anthropic**
52
+ - **Google**
53
+ - **Vertex AI** (Gemini via Vertex AI)
54
+ - **Mistral**
55
+ - **Groq**
56
+ - **Cerebras**
57
+ - **Together**
58
+ - **Moonshot** (requires `MOONSHOT_API_KEY`)
59
+ - **Qianfan** (requires `QIANFAN_API_KEY`)
60
+ - **NVIDIA** (requires `NVIDIA_API_KEY`)
61
+ - **NanoGPT** (requires `NANO_GPT_API_KEY`)
62
+ - **Hugging Face Inference**
63
+ - **xAI**
64
+ - **Venice** (requires `VENICE_API_KEY`)
65
+ - **OpenRouter**
66
+ - **Kilo Gateway** (supports OAuth `/login kilo` or `KILO_API_KEY`)
67
+ - **LiteLLM** (requires `LITELLM_API_KEY`)
68
+ - **zAI** (requires `ZAI_API_KEY`)
69
+ - **MiniMax Coding Plan** (requires `MINIMAX_CODE_API_KEY` or `MINIMAX_CODE_CN_API_KEY`)
70
+ - **Xiaomi MiMo** (requires `XIAOMI_API_KEY`)
71
+ - **ZenMux** (requires `ZENMUX_API_KEY`)
72
+ - **Qwen Portal** (supports `QWEN_OAUTH_TOKEN` or `QWEN_PORTAL_API_KEY`)
73
+ - **Cloudflare AI Gateway** (requires `CLOUDFLARE_AI_GATEWAY_API_KEY` and provider-specific gateway base URL)
74
+ - **Ollama** (local OpenAI-compatible runtime; optional `OLLAMA_API_KEY`)
75
+ - **Ollama Cloud** (hosted native Ollama API; requires `OLLAMA_CLOUD_API_KEY`)
76
+ - **llama.cpp** (local OpenAI and Anthropic compatible inference server)
77
+ - **vLLM** (OpenAI-compatible server; `VLLM_API_KEY` for secured deployments)
78
+ - **GitHub Copilot** (requires OAuth, see below)
79
+ - **Google Gemini CLI** (requires OAuth, see below)
80
+ - **Antigravity** (requires OAuth, see below)
81
+ - **Any OpenAI-compatible API**: LM Studio, custom proxies, etc.
82
+
83
+ ## Installation
84
+
85
+ ```bash
86
+ npm install @gajae-code/ai
87
+ ```
88
+
89
+ ## Quick Start
90
+
91
+ ```typescript
92
+ import { z, getModel, stream, complete, Context, Tool } from "@gajae-code/ai";
93
+
94
+ // Fully typed with auto-complete support for both providers and models
95
+ const model = getModel("openai", "gpt-4o-mini");
96
+
97
+ // Define tools with Zod schemas for type safety and validation
98
+ const tools: Tool[] = [
99
+ {
100
+ name: "get_time",
101
+ description: "Get the current time",
102
+ parameters: z.object({
103
+ timezone: z
104
+ .string()
105
+ .optional()
106
+ .describe("Optional timezone (e.g., America/New_York)"),
107
+ }),
108
+ },
109
+ ];
110
+
111
+ // Build a conversation context (easily serializable and transferable between models)
112
+ const context: Context = {
113
+ systemPrompt: ["You are a helpful assistant."],
114
+ messages: [{ role: "user", content: "What time is it?" }],
115
+ tools,
116
+ };
117
+
118
+ // Option 1: Streaming with all event types
119
+ const s = stream(model, context);
120
+
121
+ for await (const event of s) {
122
+ switch (event.type) {
123
+ case "start":
124
+ console.log(`Starting with ${event.partial.model}`);
125
+ break;
126
+ case "text_start":
127
+ console.log("\n[Text started]");
128
+ break;
129
+ case "text_delta":
130
+ process.stdout.write(event.delta);
131
+ break;
132
+ case "text_end":
133
+ console.log("\n[Text ended]");
134
+ break;
135
+ case "thinking_start":
136
+ console.log("[Model is thinking...]");
137
+ break;
138
+ case "thinking_delta":
139
+ process.stdout.write(event.delta);
140
+ break;
141
+ case "thinking_end":
142
+ console.log("[Thinking complete]");
143
+ break;
144
+ case "toolcall_start":
145
+ console.log(`\n[Tool call started: index ${event.contentIndex}]`);
146
+ break;
147
+ case "toolcall_delta":
148
+ // Partial tool arguments are being streamed
149
+ const partialCall = event.partial.content[event.contentIndex];
150
+ if (partialCall.type === "toolCall") {
151
+ console.log(`[Streaming args for ${partialCall.name}]`);
152
+ }
153
+ break;
154
+ case "toolcall_end":
155
+ console.log(`\nTool called: ${event.toolCall.name}`);
156
+ console.log(`Arguments: ${JSON.stringify(event.toolCall.arguments)}`);
157
+ break;
158
+ case "done":
159
+ console.log(`\nFinished: ${event.reason}`);
160
+ break;
161
+ case "error":
162
+ console.error(`Error: ${event.error}`);
163
+ break;
164
+ }
165
+ }
166
+
167
+ // Get the final message after streaming, add it to the context
168
+ const finalMessage = await s.result();
169
+ context.messages.push(finalMessage);
170
+
171
+ // Handle tool calls if any
172
+ const toolCalls = finalMessage.content.filter((b) => b.type === "toolCall");
173
+ for (const call of toolCalls) {
174
+ // Execute the tool
175
+ const result =
176
+ call.name === "get_time"
177
+ ? new Date().toLocaleString("en-US", {
178
+ timeZone: call.arguments.timezone || "UTC",
179
+ dateStyle: "full",
180
+ timeStyle: "long",
181
+ })
182
+ : "Unknown tool";
183
+
184
+ // Add tool result to context (supports text and images)
185
+ context.messages.push({
186
+ role: "toolResult",
187
+ toolCallId: call.id,
188
+ toolName: call.name,
189
+ content: [{ type: "text", text: result }],
190
+ isError: false,
191
+ timestamp: Date.now(),
192
+ });
193
+ }
194
+
195
+ // Continue if there were tool calls
196
+ if (toolCalls.length > 0) {
197
+ const continuation = await complete(model, context);
198
+ context.messages.push(continuation);
199
+ console.log("After tool execution:", continuation.content);
200
+ }
201
+
202
+ console.log(`Total tokens: ${finalMessage.usage.input} in, ${finalMessage.usage.output} out`);
203
+ console.log(`Cost: $${finalMessage.usage.cost.total.toFixed(4)}`);
204
+
205
+ // Option 2: Get complete response without streaming
206
+ const response = await complete(model, context);
207
+
208
+ for (const block of response.content) {
209
+ if (block.type === "text") {
210
+ console.log(block.text);
211
+ } else if (block.type === "toolCall") {
212
+ console.log(`Tool: ${block.name}(${JSON.stringify(block.arguments)})`);
213
+ }
214
+ }
215
+ ```
216
+
217
+ ## Tools
218
+
219
+ Tools enable LLMs to interact with external systems. This library uses **Zod** schemas for type-safe tool definitions with automatic validation. Schemas are converted to JSON Schema for providers as needed.
220
+
221
+ ### Defining Tools
222
+
223
+ ```typescript
224
+ import { z, Tool } from "@gajae-code/ai";
225
+
226
+ // Define tool parameters with Zod
227
+ const weatherTool: Tool = {
228
+ name: "get_weather",
229
+ description: "Get current weather for a location",
230
+ parameters: z.object({
231
+ location: z.string().describe("City name or coordinates"),
232
+ units: z.enum(["celsius", "fahrenheit"]).default("celsius"),
233
+ }),
234
+ };
235
+
236
+ const bookMeetingTool: Tool = {
237
+ name: "book_meeting",
238
+ description: "Schedule a meeting",
239
+ parameters: z.object({
240
+ title: z.string().min(1),
241
+ startTime: z.string().describe("ISO 8601 date-time"),
242
+ endTime: z.string().describe("ISO 8601 date-time"),
243
+ attendees: z.array(z.email()).min(1),
244
+ }),
245
+ };
246
+ ```
247
+
248
+ ### Handling Tool Calls
249
+
250
+ Tool results use content blocks and can include both text and images:
251
+
252
+ ```typescript
253
+ import * as fs from "node:fs";
254
+
255
+ const context: Context = {
256
+ messages: [{ role: "user", content: "What is the weather in London?" }],
257
+ tools: [weatherTool],
258
+ };
259
+
260
+ const response = await complete(model, context);
261
+
262
+ // Check for tool calls in the response
263
+ for (const block of response.content) {
264
+ if (block.type === "toolCall") {
265
+ // Execute your tool with the arguments
266
+ // See "Validating Tool Arguments" section for validation
267
+ const result = await executeWeatherApi(block.arguments);
268
+
269
+ // Add tool result with text content
270
+ context.messages.push({
271
+ role: "toolResult",
272
+ toolCallId: block.id,
273
+ toolName: block.name,
274
+ content: [{ type: "text", text: JSON.stringify(result) }],
275
+ isError: false,
276
+ timestamp: Date.now(),
277
+ });
278
+ }
279
+ }
280
+
281
+ // Tool results can also include images (for vision-capable models)
282
+ const imageBuffer = fs.readFileSync("chart.png");
283
+ context.messages.push({
284
+ role: "toolResult",
285
+ toolCallId: "tool_xyz",
286
+ toolName: "generate_chart",
287
+ content: [
288
+ { type: "text", text: "Generated chart showing temperature trends" },
289
+ { type: "image", data: imageBuffer.toBase64(), mimeType: "image/png" },
290
+ ],
291
+ isError: false,
292
+ timestamp: Date.now(),
293
+ });
294
+ ```
295
+
296
+ ### Streaming Tool Calls with Partial JSON
297
+
298
+ During streaming, tool call arguments are progressively parsed as they arrive. This enables real-time UI updates before the complete arguments are available:
299
+
300
+ ```typescript
301
+ const s = stream(model, context);
302
+
303
+ for await (const event of s) {
304
+ if (event.type === "toolcall_delta") {
305
+ const toolCall = event.partial.content[event.contentIndex];
306
+
307
+ // toolCall.arguments contains partially parsed JSON during streaming
308
+ // This allows for progressive UI updates
309
+ if (toolCall.type === "toolCall" && toolCall.arguments) {
310
+ // BE DEFENSIVE: arguments may be incomplete
311
+ // Example: Show file path being written even before content is complete
312
+ if (toolCall.name === "write_file" && toolCall.arguments.path) {
313
+ console.log(`Writing to: ${toolCall.arguments.path}`);
314
+
315
+ // Content might be partial or missing
316
+ if (toolCall.arguments.content) {
317
+ console.log(`Content preview: ${toolCall.arguments.content.substring(0, 100)}...`);
318
+ }
319
+ }
320
+ }
321
+ }
322
+
323
+ if (event.type === "toolcall_end") {
324
+ // Here toolCall.arguments is complete (but not yet validated)
325
+ const toolCall = event.toolCall;
326
+ console.log(`Tool completed: ${toolCall.name}`, toolCall.arguments);
327
+ }
328
+ }
329
+ ```
330
+
331
+ **Important notes about partial tool arguments:**
332
+
333
+ - During `toolcall_delta` events, `arguments` contains the best-effort parse of partial JSON
334
+ - Fields may be missing or incomplete - always check for existence before use
335
+ - String values may be truncated mid-word
336
+ - Arrays may be incomplete
337
+ - Nested objects may be partially populated
338
+ - At minimum, `arguments` will be an empty object `{}`, never `undefined`
339
+ - The Google provider does not support function call streaming. Instead, you will receive a single `toolcall_delta` event with the full arguments.
340
+
341
+ ### Validating Tool Arguments
342
+
343
+ When using `agentLoop`, tool arguments are automatically validated against your Zod parameter schemas before execution. If validation fails, the error is returned to the model as a tool result, allowing it to retry.
344
+
345
+ When implementing your own tool execution loop with `stream()` or `complete()`, use `validateToolCall` to validate arguments before passing them to your tools:
346
+
347
+ ```typescript
348
+ import { stream, validateToolCall, Tool } from "@gajae-code/ai";
349
+
350
+ const tools: Tool[] = [weatherTool, calculatorTool];
351
+ const s = stream(model, { messages, tools });
352
+
353
+ for await (const event of s) {
354
+ if (event.type === "toolcall_end") {
355
+ const toolCall = event.toolCall;
356
+
357
+ try {
358
+ // Validate arguments against the tool's schema (throws on invalid args)
359
+ const validatedArgs = validateToolCall(tools, toolCall);
360
+ const result = await executeMyTool(toolCall.name, validatedArgs);
361
+ // ... add tool result to context
362
+ } catch (error) {
363
+ // Validation failed - return error as tool result so model can retry
364
+ context.messages.push({
365
+ role: "toolResult",
366
+ toolCallId: toolCall.id,
367
+ toolName: toolCall.name,
368
+ content: [{ type: "text", text: error.message }],
369
+ isError: true,
370
+ timestamp: Date.now(),
371
+ });
372
+ }
373
+ }
374
+ }
375
+ ```
376
+
377
+ ### Complete Event Reference
378
+
379
+ All streaming events emitted during assistant message generation:
380
+
381
+ | Event Type | Description | Key Properties |
382
+ | ---------------- | ------------------------ | ------------------------------------------------------------------------------------------- |
383
+ | `start` | Stream begins | `partial`: Initial assistant message structure |
384
+ | `text_start` | Text block starts | `contentIndex`: Position in content array |
385
+ | `text_delta` | Text chunk received | `delta`: New text, `contentIndex`: Position |
386
+ | `text_end` | Text block complete | `content`: Full text, `contentIndex`: Position |
387
+ | `thinking_start` | Thinking block starts | `contentIndex`: Position in content array |
388
+ | `thinking_delta` | Thinking chunk received | `delta`: New text, `contentIndex`: Position |
389
+ | `thinking_end` | Thinking block complete | `content`: Full thinking, `contentIndex`: Position |
390
+ | `toolcall_start` | Tool call begins | `contentIndex`: Position in content array |
391
+ | `toolcall_delta` | Tool arguments streaming | `delta`: JSON chunk, `partial.content[contentIndex].arguments`: Partial parsed args |
392
+ | `toolcall_end` | Tool call complete | `toolCall`: Complete validated tool call with `id`, `name`, `arguments` |
393
+ | `done` | Stream complete | `reason`: Stop reason ("stop", "length", "toolUse"), `message`: Final assistant message |
394
+ | `error` | Error occurred | `reason`: Error type ("error" or "aborted"), `error`: AssistantMessage with partial content |
395
+
396
+ ## Image Input
397
+
398
+ Models with vision capabilities can process images. You can check if a model supports images via the `input` property. If you pass images to a non-vision model, they are silently ignored.
399
+
400
+ ```typescript
401
+ import * as fs from "node:fs";
402
+ import { getModel, complete } from "@gajae-code/ai";
403
+
404
+ const model = getModel("openai", "gpt-4o-mini");
405
+
406
+ // Check if model supports images
407
+ if (model.input.includes("image")) {
408
+ console.log("Model supports vision");
409
+ }
410
+
411
+ const imageBuffer = fs.readFileSync("image.png");
412
+ const base64Image = imageBuffer.toBase64();
413
+
414
+ const response = await complete(model, {
415
+ messages: [
416
+ {
417
+ role: "user",
418
+ content: [
419
+ { type: "text", text: "What is in this image?" },
420
+ { type: "image", data: base64Image, mimeType: "image/png" },
421
+ ],
422
+ },
423
+ ],
424
+ });
425
+
426
+ // Access the response
427
+ for (const block of response.content) {
428
+ if (block.type === "text") {
429
+ console.log(block.text);
430
+ }
431
+ }
432
+ ```
433
+
434
+ ## Thinking/Reasoning
435
+
436
+ Many models support thinking/reasoning capabilities where they can show their internal thought process. You can check if a model supports reasoning via the `reasoning` property. If you pass reasoning options to a non-reasoning model, they are silently ignored.
437
+
438
+ ### Unified Interface (streamSimple/completeSimple)
439
+
440
+ ```typescript
441
+ import { getModel, streamSimple, completeSimple } from "@gajae-code/ai";
442
+
443
+ // Many models across providers support thinking/reasoning
444
+ const model = getModel("anthropic", "anthropic-model-sonnet-4-20250514");
445
+ // or getModel('openai', 'gpt-5-mini');
446
+ // or getModel('google', 'gemini-2.5-flash');
447
+ // or getModel('xai', 'grok-code-fast-1');
448
+ // or getModel('groq', 'openai/gpt-oss-20b');
449
+ // or getModel('cerebras', 'gpt-oss-120b');
450
+ // or getModel('openrouter', 'z-ai/glm-4.5v');
451
+
452
+ // Check if model supports reasoning
453
+ if (model.reasoning) {
454
+ console.log("Model supports reasoning/thinking");
455
+ }
456
+
457
+ // Use the simplified reasoning option
458
+ const response = await completeSimple(
459
+ model,
460
+ {
461
+ messages: [{ role: "user", content: "Solve: 2x + 5 = 13" }],
462
+ },
463
+ {
464
+ reasoning: "medium", // 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' (xhigh maps to high on non-OpenAI providers)
465
+ }
466
+ );
467
+
468
+ // Access thinking and text blocks
469
+ for (const block of response.content) {
470
+ if (block.type === "thinking") {
471
+ console.log("Thinking:", block.thinking);
472
+ } else if (block.type === "text") {
473
+ console.log("Response:", block.text);
474
+ }
475
+ }
476
+ ```
477
+
478
+ ### Provider-Specific Options (stream/complete)
479
+
480
+ For fine-grained control, use the provider-specific options:
481
+
482
+ ```typescript
483
+ import { getModel, complete } from "@gajae-code/ai";
484
+
485
+ // OpenAI Reasoning (o1, o3, gpt-5)
486
+ const openaiModel = getModel("openai", "gpt-5-mini");
487
+ await complete(openaiModel, context, {
488
+ reasoningEffort: "medium",
489
+ reasoningSummary: "detailed", // OpenAI Responses API only
490
+ });
491
+
492
+ // Anthropic Thinking (Anthropic model Sonnet 4)
493
+ const anthropicModel = getModel("anthropic", "anthropic-model-sonnet-4-20250514");
494
+ await complete(anthropicModel, context, {
495
+ thinkingEnabled: true,
496
+ thinkingBudgetTokens: 8192, // Optional token limit
497
+ });
498
+
499
+ // Google Gemini Thinking
500
+ const googleModel = getModel("google", "gemini-2.5-flash");
501
+ await complete(googleModel, context, {
502
+ thinking: {
503
+ enabled: true,
504
+ budgetTokens: 8192, // -1 for dynamic, 0 to disable
505
+ },
506
+ });
507
+ ```
508
+
509
+ ### Streaming Thinking Content
510
+
511
+ When streaming, thinking content is delivered through specific events:
512
+
513
+ ```typescript
514
+ const s = streamSimple(model, context, { reasoning: "high" });
515
+
516
+ for await (const event of s) {
517
+ switch (event.type) {
518
+ case "thinking_start":
519
+ console.log("[Model started thinking]");
520
+ break;
521
+ case "thinking_delta":
522
+ process.stdout.write(event.delta); // Stream thinking content
523
+ break;
524
+ case "thinking_end":
525
+ console.log("\n[Thinking complete]");
526
+ break;
527
+ }
528
+ }
529
+ ```
530
+
531
+ ## Stop Reasons
532
+
533
+ Every `AssistantMessage` includes a `stopReason` field that indicates how the generation ended:
534
+
535
+ - `"stop"` - Normal completion, the model finished its response
536
+ - `"length"` - Output hit the maximum token limit
537
+ - `"toolUse"` - Model is calling tools and expects tool results
538
+ - `"error"` - An error occurred during generation
539
+ - `"aborted"` - Request was cancelled via abort signal
540
+
541
+ ## Error Handling
542
+
543
+ When a request ends with an error (including aborts and tool call validation errors), the streaming API emits an error event:
544
+
545
+ ```typescript
546
+ // In streaming
547
+ for await (const event of stream) {
548
+ if (event.type === "error") {
549
+ // event.reason is either "error" or "aborted"
550
+ // event.error is the AssistantMessage with partial content
551
+ console.error(`Error (${event.reason}):`, event.error.errorMessage);
552
+ console.log("Partial content:", event.error.content);
553
+ }
554
+ }
555
+
556
+ // The final message will have the error details
557
+ const message = await stream.result();
558
+ if (message.stopReason === "error" || message.stopReason === "aborted") {
559
+ console.error("Request failed:", message.errorMessage);
560
+ // message.content contains any partial content received before the error
561
+ // message.usage contains partial token counts and costs
562
+ }
563
+ ```
564
+
565
+ ### Aborting Requests
566
+
567
+ The abort signal allows you to cancel in-progress requests. Aborted requests have `stopReason === 'aborted'`:
568
+
569
+ ```typescript
570
+ import { getModel, stream } from "@gajae-code/ai";
571
+
572
+ const model = getModel("openai", "gpt-4o-mini");
573
+
574
+ // Abort after 2 seconds
575
+ const signal = AbortSignal.timeout(2000);
576
+
577
+ const s = stream(
578
+ model,
579
+ {
580
+ messages: [{ role: "user", content: "Write a long story" }],
581
+ },
582
+ {
583
+ signal,
584
+ }
585
+ );
586
+
587
+ for await (const event of s) {
588
+ if (event.type === "text_delta") {
589
+ process.stdout.write(event.delta);
590
+ } else if (event.type === "error") {
591
+ // event.reason tells you if it was "error" or "aborted"
592
+ console.log(`${event.reason === "aborted" ? "Aborted" : "Error"}:`, event.error.errorMessage);
593
+ }
594
+ }
595
+
596
+ // Get results (may be partial if aborted)
597
+ const response = await s.result();
598
+ if (response.stopReason === "aborted") {
599
+ console.log("Request was aborted:", response.errorMessage);
600
+ console.log("Partial content received:", response.content);
601
+ console.log("Tokens used:", response.usage);
602
+ }
603
+ ```
604
+
605
+ ### Continuing After Abort
606
+
607
+ Aborted messages can be added to the conversation context and continued in subsequent requests:
608
+
609
+ ```typescript
610
+ const context = {
611
+ messages: [{ role: "user", content: "Explain quantum computing in detail" }],
612
+ };
613
+
614
+ // First request gets aborted after 2 seconds
615
+ const controller1 = new AbortController();
616
+ setTimeout(() => controller1.abort(), 2000);
617
+
618
+ const partial = await complete(model, context, { signal: controller1.signal });
619
+
620
+ // Add the partial response to context
621
+ context.messages.push(partial);
622
+ context.messages.push({ role: "user", content: "Please continue" });
623
+
624
+ // Continue the conversation
625
+ const continuation = await complete(model, context);
626
+ ```
627
+
628
+ ### Common Stream Options
629
+
630
+ All providers accept the base `StreamOptions` (in addition to provider-specific options):
631
+
632
+ - `apiKey`: Override the provider API key
633
+ - `headers`: Extra request headers merged on top of model-defined headers
634
+ - `sessionId`: Provider-specific session identifier (prompt caching/routing)
635
+ - `signal`: Abort in-flight requests
636
+ - `onPayload`: Callback invoked with the provider request payload just before sending
637
+
638
+ Example:
639
+
640
+ ```typescript
641
+ const response = await complete(model, context, {
642
+ apiKey: "sk-live",
643
+ headers: { "X-Debug-Trace": "true" },
644
+ onPayload: (payload) => {
645
+ console.log("request payload", payload);
646
+ },
647
+ });
648
+ ```
649
+
650
+ ## APIs, Models, and Providers
651
+
652
+ The library implements 4 API interfaces, each with its own streaming function and options:
653
+
654
+ - **`anthropic-messages`**: Anthropic's Messages API (`streamAnthropic`, `AnthropicOptions`)
655
+ - **`google-generative-ai`**: Google's Generative AI API (`streamGoogle`, `GoogleOptions`)
656
+ - **`openai-completions`**: OpenAI's Chat Completions API (`streamOpenAICompletions`, `OpenAICompletionsOptions`)
657
+ - **`openai-responses`**: OpenAI's Responses API (`streamOpenAIResponses`, `OpenAIResponsesOptions`)
658
+
659
+ ### Providers and Models
660
+
661
+ A **provider** offers models through a specific API. For example:
662
+
663
+ - **Anthropic** models use the `anthropic-messages` API
664
+ - **Google** models use the `google-generative-ai` API
665
+ - **OpenAI** models use the `openai-responses` API
666
+ - **Mistral, xAI, Cerebras, Groq, etc.** models use the `openai-completions` API (OpenAI-compatible)
667
+
668
+ ### Querying Providers and Models
669
+
670
+ ```typescript
671
+ import { getProviders, getModels, getModel } from "@gajae-code/ai";
672
+
673
+ // Get all available providers
674
+ const providers = getProviders();
675
+ console.log(providers); // ['openai', 'anthropic', 'google', 'xai', 'groq', ...]
676
+
677
+ // Get all models from a provider (fully typed)
678
+ const anthropicModels = getModels("anthropic");
679
+ for (const model of anthropicModels) {
680
+ console.log(`${model.id}: ${model.name}`);
681
+ console.log(` API: ${model.api}`); // 'anthropic-messages'
682
+ console.log(` Context: ${model.contextWindow} tokens`);
683
+ console.log(` Vision: ${model.input.includes("image")}`);
684
+ console.log(` Reasoning: ${model.reasoning}`);
685
+ }
686
+
687
+ // Get a specific model (both provider and model ID are auto-completed in IDEs)
688
+ const model = getModel("openai", "gpt-4o-mini");
689
+ console.log(`Using ${model.name} via ${model.api} API`);
690
+ ```
691
+
692
+ ### Custom Models
693
+
694
+ You can create custom models for local inference servers or custom endpoints.
695
+
696
+ For local Ollama, `OLLAMA_API_KEY` is optional and mainly needed for authenticated/self-hosted gateways. `ollama` remains the local OpenAI-compatible runtime integration.
697
+
698
+ ```typescript
699
+ import { Model, stream } from "@gajae-code/ai";
700
+
701
+ // Example: local Ollama using the OpenAI-compatible API
702
+ const ollamaModel: Model<"openai-completions"> = {
703
+ id: "llama-3.1-8b",
704
+ name: "Llama 3.1 8B (Ollama)",
705
+ api: "openai-completions",
706
+ provider: "ollama",
707
+ baseUrl: "http://localhost:11434/v1",
708
+ reasoning: false,
709
+ input: ["text"],
710
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
711
+ contextWindow: 128000,
712
+ maxTokens: 32000,
713
+ };
714
+
715
+ const localResponse = await stream(ollamaModel, context, {
716
+ apiKey: process.env.OLLAMA_API_KEY, // Optional; local Ollama usually runs without auth
717
+ });
718
+
719
+ // Example: Ollama Cloud using the native /api/chat transport
720
+ const ollamaCloudModel: Model<"ollama-chat"> = {
721
+ id: "gpt-oss:120b",
722
+ name: "GPT OSS 120B (Ollama Cloud)",
723
+ api: "ollama-chat",
724
+ provider: "ollama-cloud",
725
+ baseUrl: "https://ollama.com",
726
+ reasoning: true,
727
+ input: ["text", "image"],
728
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
729
+ contextWindow: 262144,
730
+ maxTokens: 8192,
731
+ };
732
+
733
+ const cloudResponse = await stream(ollamaCloudModel, context, {
734
+ apiKey: process.env.OLLAMA_CLOUD_API_KEY,
735
+ });
736
+
737
+ // Example: LiteLLM proxy with explicit compat settings
738
+ const litellmModel: Model<"openai-completions"> = {
739
+ id: "gpt-4o",
740
+ name: "GPT-4o (via LiteLLM)",
741
+ api: "openai-completions",
742
+ provider: "litellm",
743
+ baseUrl: "http://localhost:4000/v1",
744
+ reasoning: false,
745
+ input: ["text", "image"],
746
+ cost: { input: 2.5, output: 10, cacheRead: 0, cacheWrite: 0 },
747
+ contextWindow: 128000,
748
+ maxTokens: 16384,
749
+ compat: {
750
+ supportsStore: false, // LiteLLM doesn't support the store field
751
+ },
752
+ };
753
+
754
+ // Example: Custom endpoint with headers (bypassing Cloudflare bot detection)
755
+ const proxyModel: Model<"anthropic-messages"> = {
756
+ id: "anthropic-model-sonnet-4",
757
+ name: "Anthropic model Sonnet 4 (Proxied)",
758
+ api: "anthropic-messages",
759
+ provider: "custom-proxy",
760
+ baseUrl: "https://proxy.example.com/v1",
761
+ reasoning: true,
762
+ input: ["text", "image"],
763
+ cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
764
+ contextWindow: 200000,
765
+ maxTokens: 8192,
766
+ headers: {
767
+ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
768
+ "X-Custom-Auth": "bearer-token-here",
769
+ },
770
+ };
771
+ ```
772
+
773
+ ### OpenAI Compatibility Settings
774
+
775
+ The `openai-completions` API is implemented by many providers with minor differences. By default, the library auto-detects compatibility settings based on `baseUrl` for known providers (Cerebras, xAI, Mistral, Chutes, etc.). For custom proxies or unknown endpoints, you can override these settings via the `compat` field:
776
+
777
+ ```typescript
778
+ interface OpenAICompat {
779
+ supportsStore?: boolean; // Whether provider supports the `store` field (default: true)
780
+ supportsDeveloperRole?: boolean; // Whether provider supports `developer` role vs `system` (default: true)
781
+ supportsReasoningEffort?: boolean; // Whether provider supports `reasoning_effort` (default: true)
782
+ maxTokensField?: "max_completion_tokens" | "max_tokens"; // Which field name to use (default: max_completion_tokens)
783
+ extraBody?: Record<string, unknown>; // Extra request-body fields for custom proxy routing or provider-specific options
784
+ }
785
+ ```
786
+
787
+ If `compat` is not set, the library falls back to URL-based detection. If `compat` is partially set, unspecified fields use the detected defaults. This is useful for:
788
+
789
+ - **LiteLLM proxies**: May not support `store` field
790
+ - **Custom inference servers**: May use non-standard field names
791
+ - **Self-hosted endpoints**: May have different feature support
792
+
793
+ ### Type Safety
794
+
795
+ Models are typed by their API, ensuring type-safe options:
796
+
797
+ ```typescript
798
+ // TypeScript knows this is an Anthropic model
799
+ const anthropic-model = getModel("anthropic", "anthropic-model-sonnet-4-20250514");
800
+
801
+ // So these options are type-checked for AnthropicOptions
802
+ await stream(anthropic-model, context, {
803
+ thinkingEnabled: true, // ✓ Valid for anthropic-messages
804
+ thinkingBudgetTokens: 2048, // ✓ Valid for anthropic-messages
805
+ // reasoningEffort: 'high' // ✗ TypeScript error: not valid for anthropic-messages
806
+ });
807
+ ```
808
+
809
+ ## Cross-Provider Handoffs
810
+
811
+ The library supports seamless handoffs between different LLM providers within the same conversation. This allows you to switch models mid-conversation while preserving context, including thinking blocks, tool calls, and tool results.
812
+
813
+ ### How It Works
814
+
815
+ When messages from one provider are sent to a different provider, the library automatically transforms them for compatibility:
816
+
817
+ - **User and tool result messages** are passed through unchanged
818
+ - **Assistant messages from the same provider/API** are preserved as-is
819
+ - **Assistant messages from different providers** have their thinking blocks converted to text with `<thinking>` tags
820
+ - **Tool calls and regular text** are preserved unchanged
821
+
822
+ ### Example: Multi-Provider Conversation
823
+
824
+ ```typescript
825
+ import { getModel, complete, Context } from "@gajae-code/ai";
826
+
827
+ // Start with Anthropic model
828
+ const anthropic-model = getModel("anthropic", "anthropic-model-sonnet-4-20250514");
829
+ const context: Context = {
830
+ messages: [],
831
+ };
832
+
833
+ context.messages.push({ role: "user", content: "What is 25 * 18?" });
834
+ const anthropic-modelResponse = await complete(anthropic-model, context, {
835
+ thinkingEnabled: true,
836
+ });
837
+ context.messages.push(anthropic-modelResponse);
838
+
839
+ // Switch to GPT-5 - it will see Anthropic model's thinking as <thinking> tagged text
840
+ const gpt5 = getModel("openai", "gpt-5-mini");
841
+ context.messages.push({ role: "user", content: "Is that calculation correct?" });
842
+ const gptResponse = await complete(gpt5, context);
843
+ context.messages.push(gptResponse);
844
+
845
+ // Switch to Gemini
846
+ const gemini = getModel("google", "gemini-2.5-flash");
847
+ context.messages.push({ role: "user", content: "What was the original question?" });
848
+ const geminiResponse = await complete(gemini, context);
849
+ ```
850
+
851
+ ### Provider Compatibility
852
+
853
+ All providers can handle messages from other providers, including:
854
+
855
+ - Text content
856
+ - Tool calls and tool results (including images in tool results)
857
+ - Thinking/reasoning blocks (transformed to tagged text for cross-provider compatibility)
858
+ - Aborted messages with partial content
859
+
860
+ This enables flexible workflows where you can:
861
+
862
+ - Start with a fast model for initial responses
863
+ - Switch to a more capable model for complex reasoning
864
+ - Use specialized models for specific tasks
865
+ - Maintain conversation continuity across provider outages
866
+
867
+ ## Context Serialization
868
+
869
+ The `Context` object can be easily serialized and deserialized using standard JSON methods, making it simple to persist conversations, implement chat history, or transfer contexts between services:
870
+
871
+ ```typescript
872
+ import { Context, getModel, complete } from "@gajae-code/ai";
873
+
874
+ // Create and use a context
875
+ const context: Context = {
876
+ systemPrompt: ["You are a helpful assistant."],
877
+ messages: [{ role: "user", content: "What is TypeScript?" }],
878
+ };
879
+
880
+ const model = getModel("openai", "gpt-4o-mini");
881
+ const response = await complete(model, context);
882
+ context.messages.push(response);
883
+
884
+ // Serialize the entire context
885
+ const serialized = JSON.stringify(context);
886
+ console.log("Serialized context size:", serialized.length, "bytes");
887
+
888
+ // Save to database, localStorage, file, etc.
889
+ localStorage.setItem("conversation", serialized);
890
+
891
+ // Later: deserialize and continue the conversation
892
+ const restored: Context = JSON.parse(localStorage.getItem("conversation")!);
893
+ restored.messages.push({ role: "user", content: "Tell me more about its type system" });
894
+
895
+ // Continue with any model
896
+ const newModel = getModel("anthropic", "anthropic-model-haiku-4-5-20251001");
897
+ const continuation = await complete(newModel, restored);
898
+ ```
899
+
900
+ > **Note**: If the context contains images (encoded as base64 as shown in the Image Input section), those will also be serialized.
901
+
902
+ ## Browser Usage
903
+
904
+ The library supports browser environments. You must pass the API key explicitly since environment variables are not available in browsers:
905
+
906
+ ```typescript
907
+ import { getModel, complete } from "@gajae-code/ai";
908
+
909
+ // API key must be passed explicitly in browser
910
+ const model = getModel("anthropic", "anthropic-model-haiku-4-5-20251001");
911
+
912
+ const response = await complete(
913
+ model,
914
+ {
915
+ messages: [{ role: "user", content: "Hello!" }],
916
+ },
917
+ {
918
+ apiKey: "your-api-key",
919
+ }
920
+ );
921
+ ```
922
+
923
+ > **Security Warning**: Exposing API keys in frontend code is dangerous. Anyone can extract and abuse your keys. Only use this approach for internal tools or demos. For production applications, use a backend proxy that keeps your API keys secure.
924
+
925
+ ### Environment Variables (Node.js only)
926
+
927
+ In Node.js environments, you can set environment variables to avoid passing API keys:
928
+
929
+ | Provider | Environment Variable(s) |
930
+ | -------------- | ---------------------------------------------------------------------------- |
931
+ | OpenAI | `OPENAI_API_KEY` |
932
+ | Anthropic | `ANTHROPIC_API_KEY` or `ANTHROPIC_OAUTH_TOKEN` (or `ANTHROPIC_FOUNDRY_API_KEY` when `ANTHROPIC_MODEL_CODE_USE_FOUNDRY=true`) |
933
+ | Google | `GEMINI_API_KEY` |
934
+ | Vertex AI | `GOOGLE_CLOUD_PROJECT` (or `GCLOUD_PROJECT`) + `GOOGLE_CLOUD_LOCATION` + ADC |
935
+ | Mistral | `MISTRAL_API_KEY` |
936
+ | Groq | `GROQ_API_KEY` |
937
+ | Cerebras | `CEREBRAS_API_KEY` |
938
+ | Together | `TOGETHER_API_KEY` |
939
+ | Qianfan | `QIANFAN_API_KEY` |
940
+ | Hugging Face | `HUGGINGFACE_HUB_TOKEN` or `HF_TOKEN` |
941
+ | Synthetic | `SYNTHETIC_API_KEY` |
942
+ | NVIDIA | `NVIDIA_API_KEY` |
943
+ | NanoGPT | `NANO_GPT_API_KEY` |
944
+ | Venice | `VENICE_API_KEY` |
945
+ | Moonshot | `MOONSHOT_API_KEY` |
946
+ | xAI | `XAI_API_KEY` |
947
+ | OpenRouter | `OPENROUTER_API_KEY` |
948
+ | LiteLLM | `LITELLM_API_KEY` |
949
+ | Ollama | `OLLAMA_API_KEY` (optional for local deployments) |
950
+ | Ollama Cloud | `OLLAMA_CLOUD_API_KEY` |
951
+ | Qwen Portal | `QWEN_OAUTH_TOKEN` or `QWEN_PORTAL_API_KEY` |
952
+ | zAI | `ZAI_API_KEY` |
953
+ | MiniMax Code | `MINIMAX_CODE_API_KEY` (international) or `MINIMAX_CODE_CN_API_KEY` (China) |
954
+ | Xiaomi MiMo | `XIAOMI_API_KEY` |
955
+ | ZenMux | `ZENMUX_API_KEY` |
956
+ | vLLM | `VLLM_API_KEY` |
957
+ | Cloudflare AI Gateway | `CLOUDFLARE_AI_GATEWAY_API_KEY` |
958
+ | GitHub Copilot | `COPILOT_GITHUB_TOKEN` or `GH_TOKEN` or `GITHUB_TOKEN` |
959
+
960
+ For Cloudflare AI Gateway models, use provider base URL format
961
+ `https://gateway.ai.cloudflare.com/v1/<account>/<gateway>/anthropic`.
962
+
963
+ For Anthropic Foundry routing, set `ANTHROPIC_MODEL_CODE_USE_FOUNDRY=true` plus:
964
+ `FOUNDRY_BASE_URL`, `ANTHROPIC_FOUNDRY_API_KEY`, optional `ANTHROPIC_CUSTOM_HEADERS`,
965
+ and optional mTLS material (`ANTHROPIC_MODEL_CODE_CLIENT_CERT`, `ANTHROPIC_MODEL_CODE_CLIENT_KEY`, `NODE_EXTRA_CA_CERTS`).
966
+
967
+ Provider endpoint defaults for the current OpenAI-compatible integrations:
968
+
969
+ - Together: `https://api.together.xyz/v1`
970
+ - Moonshot: `https://api.moonshot.ai/v1`
971
+ - Qianfan: `https://qianfan.baidubce.com/v2`
972
+ - NVIDIA: `https://integrate.api.nvidia.com/v1`
973
+ - NanoGPT: `https://nano-gpt.com/api/v1`
974
+ - Hugging Face Inference: `https://router.huggingface.co/v1`
975
+ - Venice: `https://api.venice.ai/api/v1`
976
+ - Xiaomi MiMo: `https://api.xiaomimimo.com/anthropic`
977
+ - ZenMux (OpenAI): `https://zenmux.ai/api/v1`
978
+ - ZenMux (Anthropic models): `https://zenmux.ai/api/anthropic`
979
+ - vLLM: `http://127.0.0.1:8000/v1`
980
+ - Ollama: local OpenAI-compatible runtime (`http://127.0.0.1:11434/v1`)
981
+ - Ollama Cloud: native Ollama API host (`https://ollama.com/api`, configured here as base URL `https://ollama.com`)
982
+ - LiteLLM: `http://localhost:4000/v1`
983
+ - Cloudflare AI Gateway: `https://gateway.ai.cloudflare.com/v1/<account>/<gateway>/anthropic`
984
+ - Qwen Portal: `https://portal.qwen.ai/v1`
985
+ When set, the library automatically uses these keys:
986
+
987
+ ```typescript
988
+ // Uses OPENAI_API_KEY from environment
989
+ const model = getModel("openai", "gpt-4o-mini");
990
+ const response = await complete(model, context);
991
+
992
+ // Or override with explicit key
993
+ const response = await complete(model, context, {
994
+ apiKey: "sk-different-key",
995
+ });
996
+ ```
997
+
998
+ ### Checking Environment Variables
999
+
1000
+ ```typescript
1001
+ import { getEnvApiKey } from "@gajae-code/ai";
1002
+
1003
+ // Check if an API key is set in environment variables
1004
+ const key = getEnvApiKey("openai"); // checks OPENAI_API_KEY
1005
+ ```
1006
+
1007
+ ## OAuth Providers
1008
+
1009
+ Several providers support OAuth authentication (some also support static API keys):
1010
+
1011
+ - **Anthropic** (Anthropic model Pro/Max subscription)
1012
+ - **OpenAI code provider** (ChatGPT Plus/Pro subscription, access to GPT-5.x OpenAI code models)
1013
+ - **GitHub Copilot** (Copilot subscription)
1014
+ - **Google Gemini CLI** (Gemini 2.0/2.5 via Google Cloud Code Assist; free tier or paid subscription)
1015
+ - **Antigravity** (Free Gemini 3, Anthropic model, GPT-OSS via Google Cloud)
1016
+ - **Qwen Portal** (Qwen OAuth token or API key)
1017
+
1018
+ For paid Cloud Code Assist subscriptions, set `GOOGLE_CLOUD_PROJECT` or `GOOGLE_CLOUD_PROJECT_ID` to your project ID.
1019
+
1020
+ ### Vertex AI (ADC)
1021
+
1022
+ Vertex AI models use Application Default Credentials (ADC):
1023
+
1024
+ - **Local development**: Run `gcloud auth application-default login`
1025
+ - **CI/Production**: Set `GOOGLE_APPLICATION_CREDENTIALS` to point to a service account JSON key file
1026
+
1027
+ Also set `GOOGLE_CLOUD_PROJECT` (or `GCLOUD_PROJECT`) and `GOOGLE_CLOUD_LOCATION`. You can also pass `project`/`location` in the call options.
1028
+
1029
+ Example:
1030
+
1031
+ ```bash
1032
+ # Local (uses your user credentials)
1033
+ gcloud auth application-default login
1034
+ export GOOGLE_CLOUD_PROJECT="my-project"
1035
+ export GOOGLE_CLOUD_LOCATION="us-central1"
1036
+
1037
+ # CI/Production (service account key file)
1038
+ export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
1039
+ ```
1040
+
1041
+ ```typescript
1042
+ import { getModel, complete } from "@gajae-code/ai";
1043
+
1044
+ (async () => {
1045
+ const model = getModel("google-vertex", "gemini-2.5-flash");
1046
+ const response = await complete(model, {
1047
+ messages: [{ role: "user", content: "Hello from Vertex AI" }],
1048
+ });
1049
+
1050
+ for (const block of response.content) {
1051
+ if (block.type === "text") console.log(block.text);
1052
+ }
1053
+ })().catch(console.error);
1054
+ ```
1055
+
1056
+ Official docs: [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials)
1057
+
1058
+ ### CLI Login
1059
+
1060
+ The quickest way to authenticate:
1061
+
1062
+ ```bash
1063
+ bunx @gajae-code/ai login # interactive provider selection
1064
+ bunx @gajae-code/ai login anthropic # login to specific provider
1065
+ bunx @gajae-code/ai login vllm # store vLLM API key (or placeholder for local no-auth)
1066
+ bunx @gajae-code/ai list # list available providers
1067
+ ```
1068
+
1069
+ Credentials are saved to `agent.db` in the agent directory. `/login qianfan` opens the Qianfan console and stores the pasted API key.
1070
+
1071
+ `login` supports OAuth providers (Anthropic, OpenAI code provider, GitHub Copilot, Gemini CLI, Antigravity) and API-key onboarding flows.
1072
+
1073
+ For the current API-key onboarding flows, the library covers Together, Moonshot, Qianfan, NVIDIA, NanoGPT, Hugging Face, Venice, Xiaomi, vLLM, LiteLLM, Cloudflare AI Gateway, Qwen Portal, and Ollama Cloud. Ollama remains the local runtime integration; set `OLLAMA_API_KEY` only when your local or self-hosted deployment enforces bearer auth.
1074
+
1075
+ ### Programmatic OAuth
1076
+
1077
+ The library provides login and token refresh functions. Credential storage is the caller's responsibility.
1078
+
1079
+ ```typescript
1080
+ import {
1081
+ // Login functions (return credentials, do not store)
1082
+ loginAnthropic,
1083
+ loginOpenAIOpenAI code,
1084
+ loginGitHubCopilot,
1085
+ loginGeminiCli,
1086
+ loginAntigravity,
1087
+ loginCloudflareAiGateway,
1088
+ loginHuggingface,
1089
+ loginLiteLLM,
1090
+ loginMoonshot,
1091
+ loginNvidia,
1092
+ loginNanoGPT,
1093
+ loginQianfan,
1094
+ loginQwenPortal,
1095
+ loginTogether,
1096
+ loginVenice,
1097
+ loginVllm,
1098
+ loginXiaomi,
1099
+
1100
+ // Token management
1101
+ refreshOAuthToken, // (provider, credentials) => new credentials
1102
+ getOAuthApiKey, // (provider, credentialsMap) => { newCredentials, apiKey } | null
1103
+
1104
+ // Types
1105
+ type OAuthProvider, // includes 'anthropic', 'openai-code', 'github-copilot', 'google-gemini-cli', 'google-antigravity', 'together', 'moonshot', 'qianfan', 'nvidia', 'nanogpt', 'huggingface', 'venice', 'xiaomi', 'vllm', 'litellm', 'cloudflare-ai-gateway', 'qwen-portal', ...
1106
+ type OAuthCredentials,
1107
+ } from "@gajae-code/ai";
1108
+ ```
1109
+
1110
+ `loginOpenAIOpenAI code` accepts an optional `originator` value used in the OAuth flow:
1111
+
1112
+ ```typescript
1113
+ await loginOpenAIOpenAI code({
1114
+ onAuth: ({ url }) => console.log(url),
1115
+ originator: "my-cli",
1116
+ });
1117
+ ```
1118
+
1119
+ ### Login Flow Example
1120
+
1121
+ ```typescript
1122
+ import { loginGitHubCopilot } from "@gajae-code/ai";
1123
+ import * as fs from "node:fs";
1124
+
1125
+ const credentials = await loginGitHubCopilot({
1126
+ onAuth: (url, instructions) => {
1127
+ console.log(`Open: ${url}`);
1128
+ if (instructions) console.log(instructions);
1129
+ },
1130
+ onPrompt: async (prompt) => {
1131
+ return await getUserInput(prompt.message);
1132
+ },
1133
+ onProgress: (message) => console.log(message),
1134
+ });
1135
+
1136
+ // Store credentials yourself
1137
+ const auth = { "github-copilot": { type: "oauth", ...credentials } };
1138
+ fs.writeFileSync("credentials.json", JSON.stringify(auth, null, 2));
1139
+ ```
1140
+
1141
+ ### Using OAuth Tokens
1142
+
1143
+ Use `getOAuthApiKey()` to get an API key, automatically refreshing if expired:
1144
+
1145
+ ```typescript
1146
+ import { getModel, complete, getOAuthApiKey } from "@gajae-code/ai";
1147
+ import * as fs from "node:fs";
1148
+
1149
+ // Load your stored credentials
1150
+ const auth = JSON.parse(fs.readFileSync("credentials.json", "utf-8"));
1151
+
1152
+ // Get API key (refreshes if expired)
1153
+ const result = await getOAuthApiKey("github-copilot", auth);
1154
+ if (!result) throw new Error("Not logged in");
1155
+
1156
+ // Save refreshed credentials
1157
+ auth["github-copilot"] = { type: "oauth", ...result.newCredentials };
1158
+ fs.writeFileSync("credentials.json", JSON.stringify(auth, null, 2));
1159
+
1160
+ // Use the API key
1161
+ const model = getModel("github-copilot", "gpt-4o");
1162
+ const response = await complete(
1163
+ model,
1164
+ {
1165
+ messages: [{ role: "user", content: "Hello!" }],
1166
+ },
1167
+ { apiKey: result.apiKey }
1168
+ );
1169
+ ```
1170
+
1171
+ ### Provider Notes
1172
+
1173
+ **OpenAI code provider**: Requires a ChatGPT Plus or Pro subscription. Provides access to GPT-5.x OpenAI code models with extended context windows and reasoning capabilities. The library automatically handles session-based prompt caching when `sessionId` is provided in stream options.
1174
+
1175
+ **GitHub Copilot**: If you get "The requested model is not supported" error, enable the model manually in VS Code: open Copilot Chat, click the model selector, select the model (warning icon), and click "Enable".
1176
+
1177
+ **Google Gemini CLI / Antigravity**: These use Google Cloud OAuth. The `apiKey` returned by `getOAuthApiKey()` is a JSON string containing both the token and project ID, which the library handles automatically.
1178
+
1179
+ ## License
1180
+
1181
+ MIT