princejs 1.6.0 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Readme.md CHANGED
@@ -124,7 +124,7 @@ app
124
124
 
125
125
  ---
126
126
 
127
- ## 🆕 v3.3.1 — New Tree-Shakable Features
127
+ ## New Tree-Shakable Features
128
128
 
129
129
  ```ts
130
130
  import { cache, email, ai, upload } from "princejs/helpers";
@@ -133,7 +133,7 @@ import { cron, openapi } from "princejs/scheduler";
133
133
 
134
134
  * `cache(60)(handler)` — In-memory cache
135
135
  * `email(to, subject, html)` — Resend.com
136
- * `ai(prompt)` — Grok-3 via xAI API
136
+ * `ai(prompt)` — Grok / OpenAI / Hugging Face
137
137
  * `upload()` — 1-line file upload
138
138
  * `cron("*/2 * * * *", task)` — Cron support
139
139
  * `openapi({ title, version })` — Auto docs
package/dist/helpers.js CHANGED
@@ -20,14 +20,46 @@ var email = async (to, subject, html) => {
20
20
  body: JSON.stringify({ from: "no-reply@princejs.dev", to, subject, html })
21
21
  });
22
22
  };
23
- var ai = async (prompt) => {
24
- const res = await fetch("https://api.x.ai/v1/chat/completions", {
23
+ var DEFAULTS = {
24
+ openai: { url: "https://api.openai.com/v1/chat/completions", model: "gpt-4o-mini" },
25
+ grok: { url: "https://api.x.ai/v1/chat/completions", model: "grok-beta" },
26
+ huggingface: { url: "" }
27
+ };
28
+ var ai = async (prompt, opts = {}) => {
29
+ const provider = opts.provider || "openai";
30
+ const config = DEFAULTS[provider];
31
+ if (provider === "huggingface" && !opts.model) {
32
+ throw new Error("Hugging Face requires 'model' option (e.g. meta-llama/Llama-3.3-70B-Instruct)");
33
+ }
34
+ const apiKey = opts.apiKey || Bun.env[`${provider.toUpperCase()}_API_KEY`];
35
+ if (!apiKey) {
36
+ throw new Error(`Missing ${provider.toUpperCase()}_API_KEY in environment`);
37
+ }
38
+ const url = provider === "huggingface" ? `https://api-inference.huggingface.co/models/${opts.model}` : config.url;
39
+ const headers = {
40
+ "Content-Type": "application/json",
41
+ Authorization: `Bearer ${apiKey}`
42
+ };
43
+ const body = provider === "huggingface" ? { inputs: prompt, parameters: { temperature: opts.temperature ?? 0.7 } } : {
44
+ model: opts.model || config.model,
45
+ messages: [{ role: "user", content: prompt }],
46
+ temperature: opts.temperature ?? 0.7,
47
+ max_tokens: 1024
48
+ };
49
+ const res = await fetch(url, {
25
50
  method: "POST",
26
- headers: { Authorization: `Bearer ${process.env.XAI_KEY}` },
27
- body: JSON.stringify({ model: "grok-beta", messages: [{ role: "user", content: prompt }] })
51
+ headers,
52
+ body: JSON.stringify(body)
28
53
  });
29
- const json = await res.json();
30
- return json.choices?.[0]?.message?.content || "";
54
+ if (!res.ok) {
55
+ const err = await res.text();
56
+ throw new Error(`${provider} API error ${res.status}: ${err}`);
57
+ }
58
+ const data = await res.json();
59
+ if (provider === "huggingface") {
60
+ return (data[0]?.generated_text || "").trim();
61
+ }
62
+ return data.choices?.[0]?.message?.content?.trim() || "No response";
31
63
  };
32
64
  var upload = () => {
33
65
  return async (req) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "princejs",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "An easy and fast backend framework — by a 13yo developer, for developers.",
5
5
  "main": "dist/prince.js",
6
6
  "types": "dist/prince.d.ts",