@sahan-sac/ai-core 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,25 +1,48 @@
1
- # @sahan-sac/ai-core
2
-
3
- ## 0.3.1
4
-
5
- ### Patch Changes
6
-
7
- - 5e7f3cb: The build shares modules between subpaths (code splitting) instead of copying them into each one. Before, a class imported from two subpaths was two different classes, so `instanceof` failed (for example `EmailGuardError` from `@sahan-sac/email-kit/guards` against an error thrown through `./layout`), and module-level state such as caches existed once per subpath.
8
-
9
- ## 0.3.0
10
-
11
- ### Minor Changes
12
-
13
- - b9d2dd6: `createAiService().generate()` takes an optional `signal`. Aborting it stops the chain at once: running attempts are aborted, no further provider starts, and the result is `errorClass: "aborted"`. Aborted attempts are not logged and do not put a provider into cooldown.
14
- - 4fa21e3: Provider registry and more providers. New `@sahan-sac/ai-core/adapters`: each provider is an adapter with its own env guard and paid flag (`BUILTIN_ADAPTERS`), and `realProviders` builds the chain from it. Added OpenAI, Anthropic (Messages API over REST), DeepSeek, xAI, Perplexity and one custom OpenAI-compatible endpoint (`AI_CUSTOM_*`), all paid and gated by `AI_ALLOW_PAID` (the custom one unless `AI_CUSTOM_FREE`). `AI_PROVIDER_ORDER`, `AI_PROVIDER_ORDER_BLOG` and `AI_PROVIDER_ORDER_CHAT` choose which providers run and in what order; an explicit order is kept as given (`AiProvider.priority`), while cooling providers still drop behind healthy ones. `providerStatuses` explains each adapter's place for health screens, `textAiConfigured(env, purpose?)` checks one purpose's chain, and apps can pass their own adapters to `realProviders`. New factories `openAiCompatibleProvider` and `anthropicProvider`. Existing variables and `realProviders(env, purpose, fetch?)` work as before.
15
- - f465908: Owner guidance and on-demand checks. `@sahan-sac/ai-core/guard` adds `guidanceSection(text)`, which appends the site owner's standing guidance after a prompt's fixed rules (it steers, never overrides them, and never changes how fenced data is treated), `mergeGuidance(...parts)` to join global and per-feature guidance, and an optional `maxLength` on `wrapUserData`. `@sahan-sac/ai-core/adapters` adds `checkProvider(env, id)` and `checkChain(env, purpose)`: each sends a one-line prompt (a few tokens) through one adapter or a whole chain and reports who answered, how long it took and every fall-through, for a health screen's manual Check button. Neither runs on its own, and neither cools a provider down for real traffic.
16
-
17
- ## 0.2.0
18
-
19
- No changes in this release.
20
-
21
- ## 0.1.0
22
-
23
- ### Minor Changes
24
-
25
- - b620f44: First release. ai-core: the free-by-default provider chain (Gemini, OpenRouter, NVIDIA, then paid Vertex) with hedging, cooldowns and deadlines, env-driven model resolution, image generation, prompt guards and the shared env schema (ENABLE_BLOG_AI, ENABLE_CHATBOT). blog-kit: full-post, SEO, draft and cover generation, AI images through an ImageSink port, and pure blog utilities. chat-kit: runChat, guarded prompts, output filtering, the knowledge builder, the ChatStore contract and the visitor rate-limit cookie. Licensed Apache-2.0.
1
+ # @sahan-sac/ai-core
2
+
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - c0127d5: OmniRoute provider. `omniroute` is a free adapter for a self-hosted OmniRoute gateway: one OpenAI-compatible endpoint that pools many providers' free tiers and fails over between them. It turns on with `OMNIROUTE_BASE_URL` (https, or http on localhost; `/v1` is added when missing; credentials in the URL are refused), sends `OMNIROUTE_API_KEY` when set, and defaults to the gateway's `auto/best-free` model (`OMNIROUTE_MODEL`, `BLOG_OMNIROUTE_MODEL` and `CHAT_OMNIROUTE_MODEL` override it). It is first in the default order, so it leads the chat and blog chains whenever it is configured, and it never needs `AI_ALLOW_PAID`.
8
+
9
+ Free only, even with `AI_ALLOW_PAID`: a model override that is not a free route (`isFreeRoute`: ends `-free` or `:free`) is replaced by `auto/best-free`. `OMNIROUTE_ALLOW_PAID` lets an override name a paid route, but only together with `AI_ALLOW_PAID`; the gateway then counts as paid for that purpose and runs as a last resort. `ProviderAdapter.paid` takes an optional `purpose`, so an adapter can be paid for one purpose and free for the other.
10
+
11
+ ## 0.4.0
12
+
13
+ ### Minor Changes
14
+
15
+ - 396e692: Three more free-tier adapters:
16
+ - `groq`: Groq free plan, keyed by `GROQ_API_KEY`. Default models `openai/gpt-oss-120b` for the blog and `openai/gpt-oss-20b` for chat.
17
+ - `mistral`: Mistral free Experiment plan, keyed by `MISTRAL_API_KEY`. This plan may train on prompts, so the adapter runs in the blog chain only unless `AI_PROVIDER_ORDER_CHAT` lists it.
18
+ - `cloudflare`: Cloudflare Workers AI, with 10,000 free neurons a day. Needs `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_AI_API_TOKEN`.
19
+
20
+ None of them is paid, so none needs `AI_ALLOW_PAID`. Each model can be overridden per purpose. Adapters gain an optional `defaultPurposes`.
21
+
22
+ ### Patch Changes
23
+
24
+ - 1570cf6: A provider that answers 402 (no credits, or an empty prepaid wallet) now cools down for 30 minutes instead of being retried on every request, so an unfunded account costs one probe per window rather than a failed round trip each time. `cooldownFor` is exported.
25
+
26
+ ## 0.3.1
27
+
28
+ ### Patch Changes
29
+
30
+ - 5e7f3cb: The build shares modules between subpaths (code splitting) instead of copying them into each one. Before, a class imported from two subpaths was two different classes, so `instanceof` failed (for example `EmailGuardError` from `@sahan-sac/email-kit/guards` against an error thrown through `./layout`), and module-level state such as caches existed once per subpath.
31
+
32
+ ## 0.3.0
33
+
34
+ ### Minor Changes
35
+
36
+ - b9d2dd6: `createAiService().generate()` takes an optional `signal`. Aborting it stops the chain at once: running attempts are aborted, no further provider starts, and the result is `errorClass: "aborted"`. Aborted attempts are not logged and do not put a provider into cooldown.
37
+ - 4fa21e3: Provider registry and more providers. New `@sahan-sac/ai-core/adapters`: each provider is an adapter with its own env guard and paid flag (`BUILTIN_ADAPTERS`), and `realProviders` builds the chain from it. Added OpenAI, Anthropic (Messages API over REST), DeepSeek, xAI, Perplexity and one custom OpenAI-compatible endpoint (`AI_CUSTOM_*`), all paid and gated by `AI_ALLOW_PAID` (the custom one unless `AI_CUSTOM_FREE`). `AI_PROVIDER_ORDER`, `AI_PROVIDER_ORDER_BLOG` and `AI_PROVIDER_ORDER_CHAT` choose which providers run and in what order; an explicit order is kept as given (`AiProvider.priority`), while cooling providers still drop behind healthy ones. `providerStatuses` explains each adapter's place for health screens, `textAiConfigured(env, purpose?)` checks one purpose's chain, and apps can pass their own adapters to `realProviders`. New factories `openAiCompatibleProvider` and `anthropicProvider`. Existing variables and `realProviders(env, purpose, fetch?)` work as before.
38
+ - f465908: Owner guidance and on-demand checks. `@sahan-sac/ai-core/guard` adds `guidanceSection(text)`, which appends the site owner's standing guidance after a prompt's fixed rules (it steers, never overrides them, and never changes how fenced data is treated), `mergeGuidance(...parts)` to join global and per-feature guidance, and an optional `maxLength` on `wrapUserData`. `@sahan-sac/ai-core/adapters` adds `checkProvider(env, id)` and `checkChain(env, purpose)`: each sends a one-line prompt (a few tokens) through one adapter or a whole chain and reports who answered, how long it took and every fall-through, for a health screen's manual Check button. Neither runs on its own, and neither cools a provider down for real traffic.
39
+
40
+ ## 0.2.0
41
+
42
+ No changes in this release.
43
+
44
+ ## 0.1.0
45
+
46
+ ### Minor Changes
47
+
48
+ - b620f44: First release. ai-core: the free-by-default provider chain (Gemini, OpenRouter, NVIDIA, then paid Vertex) with hedging, cooldowns and deadlines, env-driven model resolution, image generation, prompt guards and the shared env schema (ENABLE_BLOG_AI, ENABLE_CHATBOT). blog-kit: full-post, SEO, draft and cover generation, AI images through an ImageSink port, and pure blog utilities. chat-kit: runChat, guarded prompts, output filtering, the knowledge builder, the ChatStore contract and the visitor rate-limit cookie. Licensed Apache-2.0.
package/LICENSE CHANGED
@@ -1,202 +1,202 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the
44
- purposes of this License, Derivative Works shall not include works
45
- that remain separable from, or merely link (or bind by name) to the
46
- interfaces of, the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including the
49
- original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright
52
- owner or by an individual or Legal Entity authorized to submit on
53
- behalf of the copyright owner. For the purposes of this definition,
54
- "submitted" means any form of electronic, verbal, or written
55
- communication sent to the Licensor or its representatives,
56
- including but not limited to communication on electronic mailing
57
- lists, source code control systems, and issue tracking systems
58
- that are managed by, or on behalf of, the Licensor for the purpose
59
- of discussing and improving the Work, but excluding communication
60
- that is conspicuously marked or otherwise designated in writing
61
- by the copyright owner as "Not a Contribution."
62
-
63
- "Contributor" shall mean Licensor and any individual or Legal Entity
64
- on behalf of whom a Contribution has been received by Licensor and
65
- subsequently incorporated within the Work.
66
-
67
- 2. Grant of Copyright License. Subject to the terms and conditions of
68
- this License, each Contributor hereby grants to You a perpetual,
69
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
- copyright license to reproduce, prepare Derivative Works of,
71
- publicly display, publicly perform, sublicense, and distribute the
72
- Work and such Derivative Works in Source or Object form.
73
-
74
- 3. Grant of Patent License. Subject to the terms and conditions of
75
- this License, each Contributor hereby grants to You a perpetual,
76
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
- (except as stated in this section) patent license to make, have made,
78
- use, offer to sell, sell, import, and otherwise transfer the Work,
79
- where such license applies only to those patent claims licensable
80
- by such Contributor that are necessarily infringed by their
81
- Contribution(s) alone or by combination of their Contribution(s)
82
- with the Work to which such Contribution(s) was submitted. If You
83
- institute patent litigation against any entity (including a
84
- cross-claim or counterclaim in a lawsuit) alleging that the Work
85
- or a Contribution incorporated within the Work constitutes direct
86
- or contributory patent infringement, then any patent licenses
87
- granted to You under this License for that Work shall terminate
88
- as of the date such litigation is filed.
89
-
90
- 4. Redistribution. You may reproduce and distribute copies of the
91
- Work or Derivative Works thereof in any medium, with or without
92
- modifications, and in Source or Object form, provided that You
93
- meet the following conditions:
94
-
95
- (a) You must give any other recipients of the Work or Derivative
96
- Works a copy of this License; and
97
-
98
- (b) You must cause any modified files to carry prominent notices
99
- stating that You changed the files; and
100
-
101
- (c) You must retain, in the Source form of any Derivative Works
102
- that You distribute, all copyright, patent, trademark, and
103
- attribution notices from the Source form of the Work,
104
- excluding those notices that do not pertain to any part of
105
- the Derivative Works; and
106
-
107
- (d) If the Work includes a "NOTICE" text file as part of its
108
- distribution, then any Derivative Works that You distribute must
109
- include a readable copy of the attribution notices contained
110
- within such NOTICE file, excluding those notices that do not
111
- pertain to any part of the Derivative Works, in at least one
112
- of the following places: within a NOTICE text file distributed
113
- as part of the Derivative Works; within the Source form or
114
- documentation, if provided along with the Derivative Works; or,
115
- within a display generated by the Derivative Works, if and
116
- wherever such third-party notices normally appear. The contents
117
- of the NOTICE file are for informational purposes only and
118
- do not modify the License. You may add Your own attribution
119
- notices within Derivative Works that You distribute, alongside
120
- or as an addendum to the NOTICE text from the Work, provided
121
- that such additional attribution notices cannot be construed
122
- as modifying the License.
123
-
124
- You may add Your own copyright statement to Your modifications and
125
- may provide additional or different license terms and conditions
126
- for use, reproduction, or distribution of Your modifications, or
127
- for any such Derivative Works as a whole, provided Your use,
128
- reproduction, and distribution of the Work otherwise complies with
129
- the conditions stated in this License.
130
-
131
- 5. Submission of Contributions. Unless You explicitly state otherwise,
132
- any Contribution intentionally submitted for inclusion in the Work
133
- by You to the Licensor shall be under the terms and conditions of
134
- this License, without any additional terms or conditions.
135
- Notwithstanding the above, nothing herein shall supersede or modify
136
- the terms of any separate license agreement you may have executed
137
- with Licensor regarding such Contributions.
138
-
139
- 6. Trademarks. This License does not grant permission to use the trade
140
- names, trademarks, service marks, or product names of the Licensor,
141
- except as required for reasonable and customary use in describing
142
- the origin of the Work and reproducing the content of the NOTICE file.
143
-
144
- 7. Disclaimer of Warranty. Unless required by applicable law or
145
- agreed to in writing, Licensor provides the Work (and each
146
- Contributor provides its Contributions) on an "AS IS" BASIS,
147
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
- implied, including, without limitation, any warranties or conditions
149
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
- PARTICULAR PURPOSE. You are solely responsible for determining the
151
- appropriateness of using or redistributing the Work and assume any
152
- risks associated with Your exercise of permissions under this License.
153
-
154
- 8. Limitation of Liability. In no event and under no legal theory,
155
- whether in tort (including negligence), contract, or otherwise,
156
- unless required by applicable law (such as deliberate and grossly
157
- negligent acts) or agreed to in writing, shall any Contributor be
158
- liable to You for damages, including any direct, indirect, special,
159
- incidental, or consequential damages of any character arising as a
160
- result of this License or out of the use or inability to use the
161
- Work (including but not limited to damages for loss of goodwill,
162
- work stoppage, computer failure or malfunction, or any and all
163
- other commercial damages or losses), even if such Contributor
164
- has been advised of the possibility of such damages.
165
-
166
- 9. Accepting Warranty or Additional Liability. While redistributing
167
- the Work or Derivative Works thereof, You may choose to offer,
168
- and charge a fee for, acceptance of support, warranty, indemnity,
169
- or other liability obligations and/or rights consistent with this
170
- License. However, in accepting such obligations, You may act only
171
- on Your own behalf and on Your sole responsibility, not on behalf
172
- of any other Contributor, and only if You agree to indemnify,
173
- defend, and hold each Contributor harmless for any liability
174
- incurred by, or claims asserted against, such Contributor by reason
175
- of your accepting any such warranty or additional liability.
176
-
177
- END OF TERMS AND CONDITIONS
178
-
179
- APPENDIX: How to apply the Apache License to your work.
180
-
181
- To apply the Apache License to your work, attach the following
182
- boilerplate notice, with the fields enclosed by brackets "[]"
183
- replaced with your own identifying information. (Don't include
184
- the brackets!) The text should be enclosed in the appropriate
185
- comment syntax for the file format. We also recommend that a
186
- file or class name and description of purpose be included on the
187
- same "printed page" as the copyright notice for easier
188
- identification within third-party archives.
189
-
190
- Copyright 2026 Sahan Sachintha
191
-
192
- Licensed under the Apache License, Version 2.0 (the "License");
193
- you may not use this file except in compliance with the License.
194
- You may obtain a copy of the License at
195
-
196
- http://www.apache.org/licenses/LICENSE-2.0
197
-
198
- Unless required by applicable law or agreed to in writing, software
199
- distributed under the License is distributed on an "AS IS" BASIS,
200
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
- See the License for the specific language governing permissions and
202
- limitations under the License.
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the
44
+ purposes of this License, Derivative Works shall not include works
45
+ that remain separable from, or merely link (or bind by name) to the
46
+ interfaces of, the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including the
49
+ original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright
52
+ owner or by an individual or Legal Entity authorized to submit on
53
+ behalf of the copyright owner. For the purposes of this definition,
54
+ "submitted" means any form of electronic, verbal, or written
55
+ communication sent to the Licensor or its representatives,
56
+ including but not limited to communication on electronic mailing
57
+ lists, source code control systems, and issue tracking systems
58
+ that are managed by, or on behalf of, the Licensor for the purpose
59
+ of discussing and improving the Work, but excluding communication
60
+ that is conspicuously marked or otherwise designated in writing
61
+ by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or Derivative
96
+ Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing
142
+ the origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Sahan Sachintha
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,120 +1,125 @@
1
- # @sahan-sac/ai-core
2
-
3
- The AI layer shared by `@sahan-sac/blog-kit` and `@sahan-sac/chat-kit`: a free-by-default provider chain with automatic fallback, a provider registry you can configure and extend from the environment, model resolution, image generation (NVIDIA FLUX, then paid Gemini/Vertex), and prompt-injection guards.
4
-
5
- Headless and framework-agnostic: no Prisma, no Next.js APIs, no React. Everything takes a parsed env or config object; only `aiEnvFromProcess()` reads `process.env`.
6
-
7
- ## How a request is answered
8
-
9
- Two patterns, kept separate:
10
-
11
- - **Adapters.** Every provider is wrapped as an `AiProvider` with one `generate(prompt, options)` that returns `{ ok: true, text }` or a classified failure (`http_429`, `timeout`, `truncated`, `invalid_output`, ...). It never throws. Built in: Gemini, OpenRouter (two keys), NVIDIA, OpenAI, Anthropic (Claude), DeepSeek, xAI (Grok), Perplexity, one custom OpenAI-compatible endpoint (vLLM, Ollama, LiteLLM, a gateway), and Vertex AI.
12
- - **Chain of responsibility.** `createAiService({ providers })` hands the prompt to the first provider. On any retryable failure it passes the prompt to the next one, until one answers, a non-retryable failure stops it, the deadline passes, or the caller aborts. On top of that:
13
- - a provider that timed out or was rate limited cools down and moves behind the healthy ones;
14
- - optional hedging starts the next provider while a slow one is still running, and the first valid answer wins;
15
- - an `accept` check turns a malformed reply into a failure, so the chain moves on.
16
-
17
- The registry (`@sahan-sac/ai-core/adapters`) decides which adapters form a chain and in what order:
18
-
19
- 1. `AI_PROVIDER_ORDER_BLOG` or `AI_PROVIDER_ORDER_CHAT` for that purpose, else `AI_PROVIDER_ORDER`, else the default order: `gemini, openrouter, openrouter-2, nvidia, openai, anthropic, deepseek, xai, perplexity, custom, vertex`.
20
- 2. An adapter runs only when its variables are set. A paid adapter also needs `AI_ALLOW_PAID=true`, even when it is listed.
21
- 3. **Explicit order:** only the listed ids run, in that order. Speed never reorders them; cooling still does.
22
- 4. **Default order:** paid adapters are last resorts, and the free ones are reordered by observed speed.
23
-
24
- ## Subpaths
25
-
26
- | Import | What |
27
- | --- | --- |
28
- | `@sahan-sac/ai-core/env` | `aiEnvSchema` (zod), `parseAiEnv(source)`, `aiEnvFromProcess()`, `AiEnv` |
29
- | `@sahan-sac/ai-core/adapters` | `BUILTIN_ADAPTERS`, `ProviderAdapter`, `realProviders(env, purpose, fetch?, adapters?)`, `chainPlan`, `providerStatuses` (why each adapter is in or out, for health screens), `checkProvider` / `checkChain` (manual checks), `configuredOrder`, `PAID_TEXT_MODELS` |
30
- | `@sahan-sac/ai-core/providers` | `createAiService` (the chain), `sharedAiHealth`, `realProviders` (re-exported), the provider factories: `geminiProvider`, `openRouterProvider`, `nvidiaProvider`, `openAiCompatibleProvider`, `anthropicProvider`, `vertexProvider` |
31
- | `@sahan-sac/ai-core/availability` | `textAiConfigured(env, purpose?)`, `blogAiEnabled`, `blogAiImagesEnabled`, `chatbotEnabled` |
32
- | `@sahan-sac/ai-core/models` | `textModels`, `imageModels`, verified free defaults, `paidAllowed`, `vertexConfigured` |
33
- | `@sahan-sac/ai-core/image` | `imageConfigFromEnv`, `generateImage` |
34
- | `@sahan-sac/ai-core/vertex` | `getVertexAccessToken` (service-account JWT exchange, cached) |
35
- | `@sahan-sac/ai-core/guard` | `wrapUserData(text, maxLength?)`, `guidanceSection`, `mergeGuidance`, `looksLikeLeak`, `ModelPrompt` |
36
- | `@sahan-sac/ai-core/log` | `AiLogger`, `consoleAiLogger` |
37
-
38
- `providers`, `adapters`, `image` and `vertex` reach `server-only` code: use them from server code only.
39
-
40
- ## Environment
41
-
42
- Spread the schema into your own env module so the names never drift:
43
-
44
- ```ts
45
- import { aiEnvSchema } from "@sahan-sac/ai-core/env";
46
-
47
- const schema = z.object({ ...aiEnvSchema.shape, DATABASE_URL: z.string() });
48
- ```
49
-
50
- | Variable | Default | Meaning |
51
- | --- | --- | --- |
52
- | `ENABLE_BLOG_AI` | `false` | Blog AI assistant (also needs a blog text provider) |
53
- | `ENABLE_CHATBOT` | `true` | Chatbot (also needs a chat text provider); `false`/`0`/`no`/`off` turns it off |
54
- | `AI_ALLOW_PAID` | `false` | Lets paid providers join the chains: OpenAI, Anthropic, DeepSeek, xAI, Perplexity, the custom endpoint (unless `AI_CUSTOM_FREE`), Vertex text and images, Gemini images |
55
- | `AI_PROVIDER_ORDER`, `AI_PROVIDER_ORDER_BLOG`, `AI_PROVIDER_ORDER_CHAT` | built-in order | Comma-separated adapter ids; only the listed ones run, in that order |
56
- | `GEMINI_API_KEY`, `OPENROUTER_API_KEY`, `OPENROUTER_API_KEY_2`, `NVIDIA_API_KEY` | unset | Free text providers; `NVIDIA_API_KEY` is also the free image provider |
57
- | `OPENROUTER_BASE_URL`, `OPENROUTER_ALLOW_PAID_MODELS` | OpenRouter API, `false` | OpenRouter settings |
58
- | `OPENAI_API_KEY`, `OPENAI_BASE_URL` | unset, OpenAI API | OpenAI (paid); the base URL also points it at any OpenAI-compatible gateway |
59
- | `ANTHROPIC_API_KEY`, `ANTHROPIC_BASE_URL` | unset, Anthropic API | Anthropic Messages API over REST (paid) |
60
- | `DEEPSEEK_API_KEY`, `XAI_API_KEY`, `PERPLEXITY_API_KEY` | unset | Paid OpenAI-compatible providers |
61
- | `AI_CUSTOM_BASE_URL`, `AI_CUSTOM_MODEL`, `AI_CUSTOM_API_KEY`, `AI_CUSTOM_NAME`, `AI_CUSTOM_FREE` | unset, `false` | One custom OpenAI-compatible endpoint. The key is optional for a local server. Paid unless `AI_CUSTOM_FREE=true` |
62
- | `GOOGLE_CLIENT_EMAIL`, `GOOGLE_PRIVATE_KEY`, `GOOGLE_CLOUD_PROJECT` | unset | Vertex service account (paid). Keep the key on one line with literal `\n` |
63
- | `GOOGLE_TOKEN_URI` | Google's token endpoint | Override only for testing |
64
- | `BLOG_<P>_MODEL`, `CHAT_<P>_MODEL`, `<P>_MODEL` | built-in defaults | Model per provider `<P>` (`GEMINI`, `OPENROUTER`, `NVIDIA`, `VERTEX`, `OPENAI`, `ANTHROPIC`, `DEEPSEEK`, `XAI`, `PERPLEXITY`): purpose variable, then provider-wide, then default. Paid-provider names change often, so set them |
65
- | `IMAGE_*_MODEL`, `IMAGEN_MODEL` | verified free defaults | Image models |
66
-
67
- ## Example
68
-
69
- ```ts
70
- import { chatbotEnabled } from "@sahan-sac/ai-core/availability";
71
- import { aiEnvFromProcess } from "@sahan-sac/ai-core/env";
72
- import { createAiService, realProviders, sharedAiHealth } from "@sahan-sac/ai-core/providers";
73
-
74
- const env = aiEnvFromProcess();
75
- if (chatbotEnabled(env)) {
76
- const ai = createAiService({ providers: realProviders(env, "chat"), health: sharedAiHealth, timeoutMs: 12_000, deadlineMs: 20_000 });
77
- const result = await ai.generate({ system: "You are helpful.", user: "Hello" }, { maxTokens: 200, signal: request.signal });
78
- // result.provider: who answered; result.attempts: every provider tried, with its error class.
79
- }
80
- ```
81
-
82
- Pass the request's `signal` so a client that disconnects stops the chain: every running attempt is aborted, no further provider starts, and the result is `{ ok: false, errorClass: "aborted" }`. An aborted attempt is not logged or cooled down.
83
-
84
- ## Your own adapter
85
-
86
- ```ts
87
- import { BUILTIN_ADAPTERS, realProviders, type ProviderAdapter } from "@sahan-sac/ai-core/adapters";
88
- import { openAiCompatibleProvider } from "@sahan-sac/ai-core/providers";
89
-
90
- const groq: ProviderAdapter = {
91
- id: "groq",
92
- label: "Groq",
93
- paid: () => false,
94
- missing: () => (process.env.GROQ_API_KEY ? [] : ["GROQ_API_KEY"]),
95
- create: ({ fetch }) =>
96
- openAiCompatibleProvider({ name: "groq", apiKey: process.env.GROQ_API_KEY!, baseUrl: "https://api.groq.com/openai/v1", model: "llama-3.3-70b-versatile", fetch }),
97
- };
98
-
99
- const providers = realProviders(env, "chat", undefined, [...BUILTIN_ADAPTERS, groq]);
100
- // Default order: the built-ins, then groq. Or list it: AI_PROVIDER_ORDER=groq,gemini
101
- ```
102
-
103
- For a settings screen, `providerStatuses(env, purpose)` reports each adapter's state: `active` (with its position), `not_configured` (with the missing variable names, never values), `needs_paid`, `not_in_order` or `unknown`.
104
-
105
- ## Manual checks
106
-
107
- Reachability for an AI provider means a real prompt, which spends tokens, so run it only when an operator asks:
108
-
109
- ```ts
110
- import { checkChain, checkProvider } from "@sahan-sac/ai-core/adapters";
111
-
112
- await checkProvider(env, "anthropic"); // { id, ok, provider, ms } or { ok: false, errorClass: "http_401", ... }
113
- await checkChain(env, "blog"); // the whole chain once: who answered, and `attempts` for every fall-through
114
- ```
115
-
116
- `checkProvider` ignores the order and `AI_ALLOW_PAID` (the operator asked for that provider) and answers `not_configured` without a call when a variable is missing. `checkChain` uses a fresh health record, so a failed check never cools a provider down for real traffic.
117
-
118
- ## Owner guidance
119
-
120
- `guidanceSection(text)` appends the site owner's standing guidance (voice, facts, do and don't) after a prompt's fixed rules; it returns `""` for empty text and caps the length. `mergeGuidance(global, perFeature)` joins the layers. Guidance steers the model but never overrides the rules above it or how fenced data is treated.
1
+ # @sahan-sac/ai-core
2
+
3
+ The AI layer shared by `@sahan-sac/blog-kit` and `@sahan-sac/chat-kit`: a free-by-default provider chain with automatic fallback, a provider registry you can configure and extend from the environment, model resolution, image generation (NVIDIA FLUX, then paid Gemini/Vertex), and prompt-injection guards.
4
+
5
+ Headless and framework-agnostic: no Prisma, no Next.js APIs, no React. Everything takes a parsed env or config object; only `aiEnvFromProcess()` reads `process.env`.
6
+
7
+ ## How a request is answered
8
+
9
+ Two patterns, kept separate:
10
+
11
+ - **Adapters.** Every provider is wrapped as an `AiProvider` with one `generate(prompt, options)` that returns `{ ok: true, text }` or a classified failure (`http_429`, `timeout`, `truncated`, `invalid_output`, ...). It never throws. Built in: Gemini, OpenRouter (two keys), NVIDIA, OpenAI, Anthropic (Claude), DeepSeek, xAI (Grok), Perplexity, one custom OpenAI-compatible endpoint (vLLM, Ollama, LiteLLM, a gateway), and Vertex AI.
12
+ - **Chain of responsibility.** `createAiService({ providers })` hands the prompt to the first provider. On any retryable failure it passes the prompt to the next one, until one answers, a non-retryable failure stops it, the deadline passes, or the caller aborts. On top of that:
13
+ - a provider that timed out or was rate limited cools down and moves behind the healthy ones;
14
+ - optional hedging starts the next provider while a slow one is still running, and the first valid answer wins;
15
+ - an `accept` check turns a malformed reply into a failure, so the chain moves on.
16
+
17
+ The registry (`@sahan-sac/ai-core/adapters`) decides which adapters form a chain and in what order:
18
+
19
+ 1. `AI_PROVIDER_ORDER_BLOG` or `AI_PROVIDER_ORDER_CHAT` for that purpose, else `AI_PROVIDER_ORDER`, else the default order: `gemini, openrouter, openrouter-2, nvidia, openai, anthropic, deepseek, xai, perplexity, custom, vertex`.
20
+ 2. An adapter runs only when its variables are set. A paid adapter also needs `AI_ALLOW_PAID=true`, even when it is listed.
21
+ 3. **Explicit order:** only the listed ids run, in that order. Speed never reorders them; cooling still does.
22
+ 4. **Default order:** paid adapters are last resorts, and the free ones are reordered by observed speed.
23
+
24
+ ## Subpaths
25
+
26
+ | Import | What |
27
+ | --- | --- |
28
+ | `@sahan-sac/ai-core/env` | `aiEnvSchema` (zod), `parseAiEnv(source)`, `aiEnvFromProcess()`, `AiEnv` |
29
+ | `@sahan-sac/ai-core/adapters` | `BUILTIN_ADAPTERS`, `ProviderAdapter`, `realProviders(env, purpose, fetch?, adapters?)`, `chainPlan`, `providerStatuses` (why each adapter is in or out, for health screens), `checkProvider` / `checkChain` (manual checks), `configuredOrder`, `PAID_TEXT_MODELS` |
30
+ | `@sahan-sac/ai-core/providers` | `createAiService` (the chain), `sharedAiHealth`, `realProviders` (re-exported), the provider factories: `geminiProvider`, `openRouterProvider`, `nvidiaProvider`, `openAiCompatibleProvider`, `anthropicProvider`, `vertexProvider` |
31
+ | `@sahan-sac/ai-core/availability` | `textAiConfigured(env, purpose?)`, `blogAiEnabled`, `blogAiImagesEnabled`, `chatbotEnabled` |
32
+ | `@sahan-sac/ai-core/models` | `textModels`, `imageModels`, verified free defaults, `paidAllowed`, `vertexConfigured` |
33
+ | `@sahan-sac/ai-core/image` | `imageConfigFromEnv`, `generateImage` |
34
+ | `@sahan-sac/ai-core/vertex` | `getVertexAccessToken` (service-account JWT exchange, cached) |
35
+ | `@sahan-sac/ai-core/guard` | `wrapUserData(text, maxLength?)`, `guidanceSection`, `mergeGuidance`, `looksLikeLeak`, `ModelPrompt` |
36
+ | `@sahan-sac/ai-core/log` | `AiLogger`, `consoleAiLogger` |
37
+
38
+ `providers`, `adapters`, `image` and `vertex` reach `server-only` code: use them from server code only.
39
+
40
+ ## Environment
41
+
42
+ Spread the schema into your own env module so the names never drift:
43
+
44
+ ```ts
45
+ import { aiEnvSchema } from "@sahan-sac/ai-core/env";
46
+
47
+ const schema = z.object({ ...aiEnvSchema.shape, DATABASE_URL: z.string() });
48
+ ```
49
+
50
+ | Variable | Default | Meaning |
51
+ | --- | --- | --- |
52
+ | `ENABLE_BLOG_AI` | `false` | Blog AI assistant (also needs a blog text provider) |
53
+ | `ENABLE_CHATBOT` | `true` | Chatbot (also needs a chat text provider); `false`/`0`/`no`/`off` turns it off |
54
+ | `AI_ALLOW_PAID` | `false` | Lets paid providers join the chains: OpenAI, Anthropic, DeepSeek, xAI, Perplexity, the custom endpoint (unless `AI_CUSTOM_FREE`), Vertex text and images, Gemini images |
55
+ | `AI_PROVIDER_ORDER`, `AI_PROVIDER_ORDER_BLOG`, `AI_PROVIDER_ORDER_CHAT` | built-in order | Comma-separated adapter ids; only the listed ones run, in that order |
56
+ | `GEMINI_API_KEY`, `OPENROUTER_API_KEY`, `OPENROUTER_API_KEY_2`, `NVIDIA_API_KEY` | unset | Free text providers; `NVIDIA_API_KEY` is also the free image provider |
57
+ | `OPENROUTER_BASE_URL`, `OPENROUTER_ALLOW_PAID_MODELS` | OpenRouter API, `false` | OpenRouter settings |
58
+ | `OPENAI_API_KEY`, `OPENAI_BASE_URL` | unset, OpenAI API | OpenAI (paid); the base URL also points it at any OpenAI-compatible gateway |
59
+ | `ANTHROPIC_API_KEY`, `ANTHROPIC_BASE_URL` | unset, Anthropic API | Anthropic Messages API over REST (paid) |
60
+ | `OMNIROUTE_BASE_URL`, `OMNIROUTE_API_KEY` | unset | Your self-hosted [OmniRoute](https://github.com/diegosouzapw/OmniRoute) gateway (https, or http on localhost; `/v1` added when missing) and its gateway key. Free only and first in the default order when set. Model `auto/best-free`; override with `OMNIROUTE_MODEL` or `BLOG_`/`CHAT_` variants. An override must be a free route (ends `-free` or `:free`), or the free default replaces it |
61
+ | `OMNIROUTE_ALLOW_PAID` | `false` | Lets an OmniRoute override name a paid route. `AI_ALLOW_PAID` alone never does; both must be on, and the paid route then runs as a last resort |
62
+ | `GROQ_API_KEY` | unset | Groq free plan (no card). Models `openai/gpt-oss-120b` (blog) and `openai/gpt-oss-20b` (chat); override with `GROQ_MODEL` or `BLOG_`/`CHAT_` variants |
63
+ | `MISTRAL_API_KEY` | unset | Mistral free (Experiment) plan, which may train on prompts. Blog chain only, unless `AI_PROVIDER_ORDER_CHAT` lists `mistral` |
64
+ | `CLOUDFLARE_ACCOUNT_ID`, `CLOUDFLARE_AI_API_TOKEN` | unset | Cloudflare Workers AI, 10,000 free neurons a day. Models `@cf/openai/gpt-oss-120b` / `-20b`; override with `CLOUDFLARE_AI_MODEL` |
65
+ | `DEEPSEEK_API_KEY`, `XAI_API_KEY`, `PERPLEXITY_API_KEY` | unset | Paid OpenAI-compatible providers |
66
+ | `AI_CUSTOM_BASE_URL`, `AI_CUSTOM_MODEL`, `AI_CUSTOM_API_KEY`, `AI_CUSTOM_NAME`, `AI_CUSTOM_FREE` | unset, `false` | One custom OpenAI-compatible endpoint. The key is optional for a local server. Paid unless `AI_CUSTOM_FREE=true` |
67
+ | `GOOGLE_CLIENT_EMAIL`, `GOOGLE_PRIVATE_KEY`, `GOOGLE_CLOUD_PROJECT` | unset | Vertex service account (paid). Keep the key on one line with literal `\n` |
68
+ | `GOOGLE_TOKEN_URI` | Google's token endpoint | Override only for testing |
69
+ | `BLOG_<P>_MODEL`, `CHAT_<P>_MODEL`, `<P>_MODEL` | built-in defaults | Model per provider `<P>` (`GEMINI`, `OPENROUTER`, `NVIDIA`, `VERTEX`, `OPENAI`, `ANTHROPIC`, `DEEPSEEK`, `XAI`, `PERPLEXITY`): purpose variable, then provider-wide, then default. Paid-provider names change often, so set them |
70
+ | `IMAGE_*_MODEL`, `IMAGEN_MODEL` | verified free defaults | Image models |
71
+
72
+ ## Example
73
+
74
+ ```ts
75
+ import { chatbotEnabled } from "@sahan-sac/ai-core/availability";
76
+ import { aiEnvFromProcess } from "@sahan-sac/ai-core/env";
77
+ import { createAiService, realProviders, sharedAiHealth } from "@sahan-sac/ai-core/providers";
78
+
79
+ const env = aiEnvFromProcess();
80
+ if (chatbotEnabled(env)) {
81
+ const ai = createAiService({ providers: realProviders(env, "chat"), health: sharedAiHealth, timeoutMs: 12_000, deadlineMs: 20_000 });
82
+ const result = await ai.generate({ system: "You are helpful.", user: "Hello" }, { maxTokens: 200, signal: request.signal });
83
+ // result.provider: who answered; result.attempts: every provider tried, with its error class.
84
+ }
85
+ ```
86
+
87
+ Pass the request's `signal` so a client that disconnects stops the chain: every running attempt is aborted, no further provider starts, and the result is `{ ok: false, errorClass: "aborted" }`. An aborted attempt is not logged or cooled down.
88
+
89
+ ## Your own adapter
90
+
91
+ ```ts
92
+ import { BUILTIN_ADAPTERS, realProviders, type ProviderAdapter } from "@sahan-sac/ai-core/adapters";
93
+ import { openAiCompatibleProvider } from "@sahan-sac/ai-core/providers";
94
+
95
+ const groq: ProviderAdapter = {
96
+ id: "groq",
97
+ label: "Groq",
98
+ paid: () => false,
99
+ missing: () => (process.env.GROQ_API_KEY ? [] : ["GROQ_API_KEY"]),
100
+ create: ({ fetch }) =>
101
+ openAiCompatibleProvider({ name: "groq", apiKey: process.env.GROQ_API_KEY!, baseUrl: "https://api.groq.com/openai/v1", model: "llama-3.3-70b-versatile", fetch }),
102
+ };
103
+
104
+ const providers = realProviders(env, "chat", undefined, [...BUILTIN_ADAPTERS, groq]);
105
+ // Default order: the built-ins, then groq. Or list it: AI_PROVIDER_ORDER=groq,gemini
106
+ ```
107
+
108
+ For a settings screen, `providerStatuses(env, purpose)` reports each adapter's state: `active` (with its position), `not_configured` (with the missing variable names, never values), `needs_paid`, `not_in_order` or `unknown`.
109
+
110
+ ## Manual checks
111
+
112
+ Reachability for an AI provider means a real prompt, which spends tokens, so run it only when an operator asks:
113
+
114
+ ```ts
115
+ import { checkChain, checkProvider } from "@sahan-sac/ai-core/adapters";
116
+
117
+ await checkProvider(env, "anthropic"); // { id, ok, provider, ms } or { ok: false, errorClass: "http_401", ... }
118
+ await checkChain(env, "blog"); // the whole chain once: who answered, and `attempts` for every fall-through
119
+ ```
120
+
121
+ `checkProvider` ignores the order and `AI_ALLOW_PAID` (the operator asked for that provider) and answers `not_configured` without a call when a variable is missing. `checkChain` uses a fresh health record, so a failed check never cools a provider down for real traffic.
122
+
123
+ ## Owner guidance
124
+
125
+ `guidanceSection(text)` appends the site owner's standing guidance (voice, facts, do and don't) after a prompt's fixed rules; it returns `""` for empty text and caps the length. `mergeGuidance(global, perFeature)` joins the layers. Guidance steers the model but never overrides the rules above it or how fenced data is treated.
@@ -1,6 +1,6 @@
1
1
  import './env.js';
2
2
  import './models.js';
3
- export { A as AdapterContext, a as AdapterState, b as AdapterStatus, B as BUILTIN_ADAPTERS, C as CHECK_PROMPT, D as DEFAULT_ORDER, P as PAID_TEXT_MODELS, c as ProviderAdapter, d as ProviderCheck, e as chainPlan, f as checkChain, g as checkProvider, h as configuredOrder, p as providerStatuses, realProviders } from './providers.js';
3
+ export { A as AdapterContext, a as AdapterState, b as AdapterStatus, B as BUILTIN_ADAPTERS, C as CHECK_PROMPT, D as DEFAULT_ORDER, F as FREE_TEXT_MODELS, P as PAID_TEXT_MODELS, c as ProviderAdapter, d as ProviderCheck, e as chainPlan, f as checkChain, g as checkProvider, h as configuredOrder, i as isFreeRoute, o as omniRouteBaseUrl, j as omniRouteModel, p as providerStatuses, realProviders } from './providers.js';
4
4
  import 'zod';
5
5
  import './guard.js';
6
6
  import './log.js';
package/dist/adapters.js CHANGED
@@ -2,14 +2,18 @@ import {
2
2
  BUILTIN_ADAPTERS,
3
3
  CHECK_PROMPT,
4
4
  DEFAULT_ORDER,
5
+ FREE_TEXT_MODELS,
5
6
  PAID_TEXT_MODELS,
6
7
  chainPlan,
7
8
  checkChain,
8
9
  checkProvider,
9
10
  configuredOrder,
11
+ isFreeRoute,
12
+ omniRouteBaseUrl,
13
+ omniRouteModel,
10
14
  providerStatuses,
11
15
  realProviders
12
- } from "./chunk-R3YY2Y7S.js";
16
+ } from "./chunk-D6KCXFSF.js";
13
17
  import "./chunk-BL4J2JNX.js";
14
18
  import "./chunk-PMC3UWQ2.js";
15
19
  import "./chunk-RBSLNDFW.js";
@@ -17,11 +21,15 @@ export {
17
21
  BUILTIN_ADAPTERS,
18
22
  CHECK_PROMPT,
19
23
  DEFAULT_ORDER,
24
+ FREE_TEXT_MODELS,
20
25
  PAID_TEXT_MODELS,
21
26
  chainPlan,
22
27
  checkChain,
23
28
  checkProvider,
24
29
  configuredOrder,
30
+ isFreeRoute,
31
+ omniRouteBaseUrl,
32
+ omniRouteModel,
25
33
  providerStatuses,
26
34
  realProviders
27
35
  };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  chainPlan
3
- } from "./chunk-R3YY2Y7S.js";
3
+ } from "./chunk-D6KCXFSF.js";
4
4
  import {
5
5
  imageConfigFromEnv
6
6
  } from "./chunk-MRNQPHWI.js";
@@ -23,6 +23,13 @@ function modelFor(env, provider, purpose, fallback) {
23
23
  return record[`${purpose.toUpperCase()}_${provider}_MODEL`] || record[`${provider}_MODEL`] || fallback[purpose];
24
24
  }
25
25
  var need = (env, ...names) => names.filter((name) => !env[name]);
26
+ var FREE_TEXT_MODELS = {
27
+ // OmniRoute's own alias: its "best free" combo, which only routes to free providers.
28
+ omniroute: { blog: "auto/best-free", chat: "auto/best-free" },
29
+ groq: { blog: "openai/gpt-oss-120b", chat: "openai/gpt-oss-20b" },
30
+ mistral: { blog: "mistral-medium-latest", chat: "mistral-small-latest" },
31
+ cloudflare: { blog: "@cf/openai/gpt-oss-120b", chat: "@cf/openai/gpt-oss-20b" }
32
+ };
26
33
  var PAID_TEXT_MODELS = {
27
34
  openai: { blog: "gpt-5-mini", chat: "gpt-5-nano" },
28
35
  anthropic: { blog: "claude-sonnet-5-5", chat: "claude-haiku-4-5-20251001" },
@@ -45,6 +52,28 @@ function compatibleAdapter(spec) {
45
52
  })
46
53
  };
47
54
  }
55
+ function omniRouteBaseUrl(env) {
56
+ const raw = env.OMNIROUTE_BASE_URL?.trim();
57
+ if (!raw) return null;
58
+ try {
59
+ const url = new URL(raw);
60
+ const local = /^(?:localhost|127\.0\.0\.1|\[::1\])$/.test(url.hostname);
61
+ if (url.username || url.password) return null;
62
+ if (!(url.protocol === "https:" || url.protocol === "http:" && local)) return null;
63
+ const base = `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
64
+ return base.endsWith("/v1") ? base : `${base}/v1`;
65
+ } catch {
66
+ return null;
67
+ }
68
+ }
69
+ function isFreeRoute(model) {
70
+ return /[-:/]free$/i.test(model.trim());
71
+ }
72
+ function omniRouteModel(env, purpose) {
73
+ const model = modelFor(env, "OMNIROUTE", purpose, FREE_TEXT_MODELS.omniroute);
74
+ return isFreeRoute(model) || env.OMNIROUTE_ALLOW_PAID && paidAllowed(env) ? model : FREE_TEXT_MODELS.omniroute[purpose];
75
+ }
76
+ var PURPOSES = ["blog", "chat"];
48
77
  var openRouterAdapter = (id, keyVar) => ({
49
78
  id,
50
79
  label: id === "openrouter" ? "OpenRouter" : "OpenRouter (second key)",
@@ -61,6 +90,25 @@ var openRouterAdapter = (id, keyVar) => ({
61
90
  })
62
91
  });
63
92
  var BUILTIN_ADAPTERS = [
93
+ {
94
+ // OmniRoute: a self-hosted gateway that pools many providers' free tiers
95
+ // behind one OpenAI-compatible endpoint and fails over between them. First
96
+ // in the default order, so it leads whenever its address is set. Free only
97
+ // unless OMNIROUTE_ALLOW_PAID (see omniRouteModel); a paid route then
98
+ // counts as paid, so it runs as a last resort like any paid provider.
99
+ id: "omniroute",
100
+ label: "OmniRoute (self-hosted gateway)",
101
+ paid: (env, purpose) => (purpose ? [purpose] : PURPOSES).some((each) => !isFreeRoute(omniRouteModel(env, each))),
102
+ missing: (env) => omniRouteBaseUrl(env) ? [] : ["OMNIROUTE_BASE_URL"],
103
+ create: ({ env, purpose, fetch: fetch2 }) => openAiCompatibleProvider({
104
+ name: "omniroute",
105
+ // A gateway may run without keys; the header must still be well formed.
106
+ apiKey: env.OMNIROUTE_API_KEY || "none",
107
+ baseUrl: omniRouteBaseUrl(env),
108
+ model: omniRouteModel(env, purpose),
109
+ fetch: fetch2
110
+ })
111
+ },
64
112
  {
65
113
  id: "gemini",
66
114
  label: "Gemini",
@@ -68,6 +116,19 @@ var BUILTIN_ADAPTERS = [
68
116
  missing: (env) => need(env, "GEMINI_API_KEY"),
69
117
  create: ({ env, purpose, fetch: fetch2 }) => geminiProvider({ apiKey: env.GEMINI_API_KEY, model: textModels(env, purpose).gemini, fetchImpl: fetch2 })
70
118
  },
119
+ {
120
+ id: "groq",
121
+ label: "Groq",
122
+ paid: () => false,
123
+ missing: (env) => need(env, "GROQ_API_KEY"),
124
+ create: ({ env, purpose, fetch: fetch2 }) => openAiCompatibleProvider({
125
+ name: "groq",
126
+ apiKey: env.GROQ_API_KEY,
127
+ baseUrl: "https://api.groq.com/openai/v1",
128
+ model: modelFor(env, "GROQ", purpose, FREE_TEXT_MODELS.groq),
129
+ fetch: fetch2
130
+ })
131
+ },
71
132
  openRouterAdapter("openrouter", "OPENROUTER_API_KEY"),
72
133
  openRouterAdapter("openrouter-2", "OPENROUTER_API_KEY_2"),
73
134
  {
@@ -83,6 +144,35 @@ var BUILTIN_ADAPTERS = [
83
144
  fetch: fetch2
84
145
  })
85
146
  },
147
+ {
148
+ id: "mistral",
149
+ label: "Mistral",
150
+ paid: () => false,
151
+ // The free plan may train on prompts: keep visitors' chat text out unless
152
+ // the operator lists it in AI_PROVIDER_ORDER_CHAT.
153
+ defaultPurposes: ["blog"],
154
+ missing: (env) => need(env, "MISTRAL_API_KEY"),
155
+ create: ({ env, purpose, fetch: fetch2 }) => openAiCompatibleProvider({
156
+ name: "mistral",
157
+ apiKey: env.MISTRAL_API_KEY,
158
+ baseUrl: "https://api.mistral.ai/v1",
159
+ model: modelFor(env, "MISTRAL", purpose, FREE_TEXT_MODELS.mistral),
160
+ fetch: fetch2
161
+ })
162
+ },
163
+ {
164
+ id: "cloudflare",
165
+ label: "Cloudflare Workers AI",
166
+ paid: () => false,
167
+ missing: (env) => need(env, "CLOUDFLARE_ACCOUNT_ID", "CLOUDFLARE_AI_API_TOKEN"),
168
+ create: ({ env, purpose, fetch: fetch2 }) => openAiCompatibleProvider({
169
+ name: "cloudflare",
170
+ apiKey: env.CLOUDFLARE_AI_API_TOKEN,
171
+ baseUrl: `https://api.cloudflare.com/client/v4/accounts/${encodeURIComponent(env.CLOUDFLARE_ACCOUNT_ID)}/ai/v1`,
172
+ model: modelFor(env, "CLOUDFLARE_AI", purpose, FREE_TEXT_MODELS.cloudflare),
173
+ fetch: fetch2
174
+ })
175
+ },
86
176
  compatibleAdapter({ id: "openai", label: "OpenAI", envPrefix: "OPENAI", keyVar: "OPENAI_API_KEY", baseUrl: "https://api.openai.com/v1", baseUrlVar: "OPENAI_BASE_URL" }),
87
177
  {
88
178
  id: "anthropic",
@@ -137,7 +227,7 @@ function configuredOrder(env, purpose) {
137
227
  }
138
228
  function providerStatuses(env, purpose, adapters = BUILTIN_ADAPTERS) {
139
229
  const order = configuredOrder(env, purpose);
140
- const ids = order ?? adapters.map((adapter) => adapter.id);
230
+ const ids = order ?? adapters.filter((adapter) => !adapter.defaultPurposes || adapter.defaultPurposes.includes(purpose)).map((adapter) => adapter.id);
141
231
  const byId = new Map(adapters.map((adapter) => [adapter.id, adapter]));
142
232
  let position = 0;
143
233
  const statuses = [];
@@ -148,13 +238,13 @@ function providerStatuses(env, purpose, adapters = BUILTIN_ADAPTERS) {
148
238
  continue;
149
239
  }
150
240
  const missing = adapter.missing(env);
151
- const paid = adapter.paid(env);
241
+ const paid = adapter.paid(env, purpose);
152
242
  const state = missing.length ? "not_configured" : paid && !paidAllowed(env) ? "needs_paid" : "active";
153
243
  statuses.push({ id, label: adapter.label, paid, state, missing, ...state === "active" ? { position: position++ } : {} });
154
244
  }
155
245
  for (const adapter of adapters) {
156
246
  if (!ids.includes(adapter.id)) {
157
- statuses.push({ id: adapter.id, label: adapter.label, paid: adapter.paid(env), state: "not_in_order", missing: adapter.missing(env) });
247
+ statuses.push({ id: adapter.id, label: adapter.label, paid: adapter.paid(env, purpose), state: "not_in_order", missing: adapter.missing(env) });
158
248
  }
159
249
  }
160
250
  return statuses;
@@ -203,7 +293,7 @@ function realProviders(env, purpose, fetchImpl, adapters = BUILTIN_ADAPTERS) {
203
293
  const explicit = configuredOrder(env, purpose) !== null;
204
294
  return chainPlan(env, purpose, adapters).map((adapter, index) => {
205
295
  const provider = adapter.create({ env, purpose, fetch: fetchImpl });
206
- return explicit ? { name: provider.name, generate: provider.generate, priority: index } : { name: provider.name, generate: provider.generate, lastResort: adapter.paid(env) || provider.lastResort };
296
+ return explicit ? { name: provider.name, generate: provider.generate, priority: index } : { name: provider.name, generate: provider.generate, lastResort: adapter.paid(env, purpose) || provider.lastResort };
207
297
  });
208
298
  }
209
299
 
@@ -239,6 +329,7 @@ var sharedAiHealth = globalThis.sahanAiHealth ??= createAiHealth();
239
329
  function cooldownFor(outcome) {
240
330
  if (outcome.ok) return 0;
241
331
  if (outcome.errorClass === "timeout" || outcome.status === 429) return AI_COOLDOWN_MS;
332
+ if (outcome.status === 402) return AI_COOLDOWN_MS * 30;
242
333
  if (outcome.status === 401 || outcome.status === 403 || outcome.status === 404) return AI_COOLDOWN_MS * 10;
243
334
  return 0;
244
335
  }
@@ -548,6 +639,7 @@ export {
548
639
  AI_COOLDOWN_MS,
549
640
  createAiHealth,
550
641
  sharedAiHealth,
642
+ cooldownFor,
551
643
  HEDGE,
552
644
  createAiService,
553
645
  openRouterProvider,
@@ -558,7 +650,11 @@ export {
558
650
  geminiOutcome,
559
651
  geminiProvider,
560
652
  vertexProvider,
653
+ FREE_TEXT_MODELS,
561
654
  PAID_TEXT_MODELS,
655
+ omniRouteBaseUrl,
656
+ isFreeRoute,
657
+ omniRouteModel,
562
658
  BUILTIN_ADAPTERS,
563
659
  DEFAULT_ORDER,
564
660
  configuredOrder,
@@ -26,6 +26,33 @@ var aiEnvSchema = z.object({
26
26
  OPENROUTER_ALLOW_PAID_MODELS: flag,
27
27
  GEMINI_API_KEY: text,
28
28
  NVIDIA_API_KEY: text,
29
+ // OmniRoute (./adapters): a self-hosted gateway pooling free tiers. The
30
+ // address turns it on; the key is a gateway key from its dashboard.
31
+ OMNIROUTE_BASE_URL: text,
32
+ OMNIROUTE_API_KEY: text,
33
+ OMNIROUTE_MODEL: text,
34
+ BLOG_OMNIROUTE_MODEL: text,
35
+ CHAT_OMNIROUTE_MODEL: text,
36
+ // Lets an OmniRoute model override name a paid route; AI_ALLOW_PAID must be
37
+ // on as well. Off: only free routes (`-free`, `:free`) are ever sent.
38
+ OMNIROUTE_ALLOW_PAID: flag,
39
+ // More free tiers (./adapters). Groq: free plan, no card. Mistral: free
40
+ // Experiment plan, which may train on prompts, so it joins the chat chain
41
+ // only when AI_PROVIDER_ORDER_CHAT lists it. Cloudflare Workers AI: 10,000
42
+ // free neurons a day; needs the account id and a Workers AI token.
43
+ GROQ_API_KEY: text,
44
+ GROQ_MODEL: text,
45
+ BLOG_GROQ_MODEL: text,
46
+ CHAT_GROQ_MODEL: text,
47
+ MISTRAL_API_KEY: text,
48
+ MISTRAL_MODEL: text,
49
+ BLOG_MISTRAL_MODEL: text,
50
+ CHAT_MISTRAL_MODEL: text,
51
+ CLOUDFLARE_ACCOUNT_ID: text,
52
+ CLOUDFLARE_AI_API_TOKEN: text,
53
+ CLOUDFLARE_AI_MODEL: text,
54
+ BLOG_CLOUDFLARE_AI_MODEL: text,
55
+ CHAT_CLOUDFLARE_AI_MODEL: text,
29
56
  // Model overrides, all optional (models.ts): purpose variable, then the
30
57
  // provider-wide *_MODEL, then a verified free default.
31
58
  OPENROUTER_MODEL: text,
package/dist/env.d.ts CHANGED
@@ -11,6 +11,25 @@ declare const aiEnvSchema: z.ZodObject<{
11
11
  OPENROUTER_ALLOW_PAID_MODELS: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<boolean, string | undefined>>;
12
12
  GEMINI_API_KEY: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
13
13
  NVIDIA_API_KEY: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
14
+ OMNIROUTE_BASE_URL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
15
+ OMNIROUTE_API_KEY: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
16
+ OMNIROUTE_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
17
+ BLOG_OMNIROUTE_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
18
+ CHAT_OMNIROUTE_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
19
+ OMNIROUTE_ALLOW_PAID: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<boolean, string | undefined>>;
20
+ GROQ_API_KEY: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
21
+ GROQ_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
22
+ BLOG_GROQ_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
23
+ CHAT_GROQ_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
24
+ MISTRAL_API_KEY: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
25
+ MISTRAL_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
26
+ BLOG_MISTRAL_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
27
+ CHAT_MISTRAL_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
28
+ CLOUDFLARE_ACCOUNT_ID: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
29
+ CLOUDFLARE_AI_API_TOKEN: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
30
+ CLOUDFLARE_AI_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
31
+ BLOG_CLOUDFLARE_AI_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
32
+ CHAT_CLOUDFLARE_AI_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
14
33
  OPENROUTER_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
15
34
  GEMINI_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
16
35
  NVIDIA_MODEL: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
package/dist/env.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  aiEnvFromProcess,
4
4
  aiEnvSchema,
5
5
  parseAiEnv
6
- } from "./chunk-ZIHRZXPE.js";
6
+ } from "./chunk-TEOP42ST.js";
7
7
  export {
8
8
  DEFAULT_GOOGLE_TOKEN_URI,
9
9
  aiEnvFromProcess,
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  aiEnvFromProcess,
3
3
  aiEnvSchema,
4
4
  parseAiEnv
5
- } from "./chunk-ZIHRZXPE.js";
5
+ } from "./chunk-TEOP42ST.js";
6
6
  export {
7
7
  aiEnvFromProcess,
8
8
  aiEnvSchema,
@@ -70,6 +70,13 @@ interface AiHealth {
70
70
  declare const createAiHealth: () => AiHealth;
71
71
  /** One per server instance, for request handlers; tests and scripts get a fresh one by default. */
72
72
  declare const sharedAiHealth: AiHealth;
73
+ /**
74
+ * Quota and slowness pass quickly; a wrong key or a retired model needs an
75
+ * operator, so it cools longer; an empty prepaid wallet (402) needs someone to
76
+ * add funds, so it cools longest and costs one probe per half hour instead of
77
+ * a failed round trip on every request.
78
+ */
79
+ declare function cooldownFor(outcome: AiOutcome): number;
73
80
  interface AiServiceDeps {
74
81
  providers: readonly AiProvider[];
75
82
  /** Per-provider attempt budget. */
@@ -188,12 +195,42 @@ interface ProviderAdapter {
188
195
  /** Lowercase id used in AI_PROVIDER_ORDER. */
189
196
  readonly id: string;
190
197
  readonly label: string;
191
- /** Costs money: runs only with AI_ALLOW_PAID. */
192
- paid(env: AiEnv): boolean;
198
+ /**
199
+ * Purposes it joins under the default order; unset means all. An explicit
200
+ * AI_PROVIDER_ORDER(_BLOG|_CHAT) still adds it anywhere it is listed.
201
+ */
202
+ readonly defaultPurposes?: readonly TextPurpose[];
203
+ /**
204
+ * Costs money: runs only with AI_ALLOW_PAID. `purpose` is set when the
205
+ * answer can differ per purpose; without it, true when any purpose is paid.
206
+ */
207
+ paid(env: AiEnv, purpose?: TextPurpose): boolean;
193
208
  /** Env variable names still missing; empty means configured. Never values. */
194
209
  missing(env: AiEnv): string[];
195
210
  create(context: AdapterContext): AiProvider;
196
211
  }
212
+ /**
213
+ * Defaults for the free-tier adapters that take an id per purpose. The blog
214
+ * writes long JSON, so it gets the larger model; chat gets the faster one.
215
+ */
216
+ declare const FREE_TEXT_MODELS: {
217
+ readonly omniroute: {
218
+ readonly blog: "auto/best-free";
219
+ readonly chat: "auto/best-free";
220
+ };
221
+ readonly groq: {
222
+ readonly blog: "openai/gpt-oss-120b";
223
+ readonly chat: "openai/gpt-oss-20b";
224
+ };
225
+ readonly mistral: {
226
+ readonly blog: "mistral-medium-latest";
227
+ readonly chat: "mistral-small-latest";
228
+ };
229
+ readonly cloudflare: {
230
+ readonly blog: "@cf/openai/gpt-oss-120b";
231
+ readonly chat: "@cf/openai/gpt-oss-20b";
232
+ };
233
+ };
197
234
  /**
198
235
  * Defaults for the paid adapters. Model names change often: set the
199
236
  * provider's *_MODEL variable rather than relying on these.
@@ -220,6 +257,21 @@ declare const PAID_TEXT_MODELS: {
220
257
  readonly chat: "sonar";
221
258
  };
222
259
  };
260
+ /**
261
+ * OMNIROUTE_BASE_URL as the gateway's `/v1` base, or null when unset or unsafe.
262
+ * https only, except plain http on localhost for a gateway on the same machine;
263
+ * never credentials in the URL.
264
+ */
265
+ declare function omniRouteBaseUrl(env: AiEnv): string | null;
266
+ /** An OmniRoute model id that names a free route: `auto/best-free`, `<provider>/<model>:free`. */
267
+ declare function isFreeRoute(model: string): boolean;
268
+ /**
269
+ * The OmniRoute model for a purpose. Free only by default: an override that is
270
+ * not a free route counts only with OMNIROUTE_ALLOW_PAID and AI_ALLOW_PAID both
271
+ * on; otherwise the free default replaces it, so the gateway keeps answering
272
+ * for free instead of dropping out of the chain.
273
+ */
274
+ declare function omniRouteModel(env: AiEnv, purpose: TextPurpose): string;
223
275
  declare const BUILTIN_ADAPTERS: readonly ProviderAdapter[];
224
276
  /** Free tiers first (fastest, most reliable first), paid last. */
225
277
  declare const DEFAULT_ORDER: string[];
@@ -290,4 +342,4 @@ declare function checkChain(env: AiEnv, purpose: TextPurpose, options?: {
290
342
  */
291
343
  declare function realProviders(env: AiEnv, purpose: TextPurpose, fetchImpl?: typeof fetch, adapters?: readonly ProviderAdapter[]): AiProvider[];
292
344
 
293
- export { type AdapterContext as A, AI_COOLDOWN_MS, AI_TIMEOUT_MS, type AiAttempt, type AiAttemptStatus, type AiGenerateOptions, type AiHealth, type AiOutcome, type AiProvider, type AiResult, type AiServiceDeps, BUILTIN_ADAPTERS as B, CHECK_PROMPT as C, DEFAULT_ORDER as D, HEDGE, type JsonMode, PAID_TEXT_MODELS as P, type AdapterState as a, anthropicOutcome, anthropicProvider, type AdapterStatus as b, type ProviderAdapter as c, createAiHealth, createAiService, type ProviderCheck as d, chainPlan as e, checkChain as f, checkProvider as g, geminiOutcome, geminiProvider, configuredOrder as h, nvidiaProvider, openAiCompatibleProvider, openRouterProvider, providerStatuses as p, realProviders, sharedAiHealth, vertexProvider };
345
+ export { type AdapterContext as A, AI_COOLDOWN_MS, AI_TIMEOUT_MS, type AiAttempt, type AiAttemptStatus, type AiGenerateOptions, type AiHealth, type AiOutcome, type AiProvider, type AiResult, type AiServiceDeps, BUILTIN_ADAPTERS as B, CHECK_PROMPT as C, DEFAULT_ORDER as D, FREE_TEXT_MODELS as F, HEDGE, type JsonMode, PAID_TEXT_MODELS as P, type AdapterState as a, anthropicOutcome, anthropicProvider, type AdapterStatus as b, type ProviderAdapter as c, cooldownFor, createAiHealth, createAiService, type ProviderCheck as d, chainPlan as e, checkChain as f, checkProvider as g, geminiOutcome, geminiProvider, configuredOrder as h, isFreeRoute as i, omniRouteModel as j, nvidiaProvider, omniRouteBaseUrl as o, openAiCompatibleProvider, openRouterProvider, providerStatuses as p, realProviders, sharedAiHealth, vertexProvider };
package/dist/providers.js CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  HEDGE,
5
5
  anthropicOutcome,
6
6
  anthropicProvider,
7
+ cooldownFor,
7
8
  createAiHealth,
8
9
  createAiService,
9
10
  geminiOutcome,
@@ -14,7 +15,7 @@ import {
14
15
  realProviders,
15
16
  sharedAiHealth,
16
17
  vertexProvider
17
- } from "./chunk-R3YY2Y7S.js";
18
+ } from "./chunk-D6KCXFSF.js";
18
19
  import "./chunk-BL4J2JNX.js";
19
20
  import "./chunk-PMC3UWQ2.js";
20
21
  import "./chunk-RBSLNDFW.js";
@@ -24,6 +25,7 @@ export {
24
25
  HEDGE,
25
26
  anthropicOutcome,
26
27
  anthropicProvider,
28
+ cooldownFor,
27
29
  createAiHealth,
28
30
  createAiService,
29
31
  geminiOutcome,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sahan-sac/ai-core",
3
- "version": "0.3.1",
3
+ "version": "0.5.0",
4
4
  "description": "Free-by-default AI provider chain with fallback (chain of responsibility over provider adapters: Gemini, OpenRouter, NVIDIA, OpenAI, Anthropic, DeepSeek, xAI, Perplexity, any OpenAI-compatible endpoint, Vertex), env-configurable order, hedging, cooldowns and deadlines, image generation and prompt-injection guards. Shared by @sahan-sac/blog-kit and @sahan-sac/chat-kit.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -92,6 +92,6 @@
92
92
  "scripts": {
93
93
  "build": "tsup",
94
94
  "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.build.json",
95
- "test": "node --conditions=react-server --import tsx --test \"src/**/*.test.ts\""
95
+ "test": "node ../../scripts/test-package.mjs --conditions=react-server"
96
96
  }
97
97
  }