@aeratic/pagetailor 0.5.0 → 0.6.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/README.md +14 -4
- package/package.json +1 -1
- package/src/dev.js +5 -5
- package/src/index.js +1 -1
- package/src/llm.js +71 -11
package/README.md
CHANGED
|
@@ -18,8 +18,10 @@ npx pagetailor dev
|
|
|
18
18
|
# http://localhost:4747/?role=magazzino the same page for the warehouse role
|
|
19
19
|
# http://localhost:4747/?view=studio visual editor of the whole project
|
|
20
20
|
|
|
21
|
-
# 4. Enable the AI agent
|
|
21
|
+
# 4. Enable the AI agent with any provider: Anthropic, OpenAI, Gemini, Groq, Mistral, DeepSeek, xAI, OpenRouter, Ollama…
|
|
22
22
|
ANTHROPIC_API_KEY=sk-... npx pagetailor dev
|
|
23
|
+
OPENAI_API_KEY=sk-... npx pagetailor dev
|
|
24
|
+
PAGETAILOR_PROVIDER=ollama PAGETAILOR_MODEL=llama3.3 npx pagetailor dev
|
|
23
25
|
|
|
24
26
|
# 5. After editing components/registry.yaml, regenerate the component index
|
|
25
27
|
npx pagetailor registry
|
|
@@ -169,9 +171,17 @@ A user asks in natural language ("put the KPI at the top"); the agent turns it i
|
|
|
169
171
|
3. `runAgent(root, { page, user, role, request, complete })` appends the patches to the user's existing override for that page (same merge rule as the editor) and calls `propose` as `user-<user>`: applied at once for the own user, pending otherwise.
|
|
170
172
|
4. If the gate rejects, the gate errors are sent back once ("The previous patch was rejected: ... Fix it.") and the result is proposed again. The result reports `attempts`.
|
|
171
173
|
|
|
172
|
-
`complete` is any `async ({ system, user }) => text`, so any model or SDK works; pagetailor depends on none. `anthropicComplete({
|
|
174
|
+
`complete` is any `async ({ system, user }) => text`, so any model or SDK works; pagetailor depends on none. Built-in adapters over plain `fetch`: `anthropicComplete`, `openaiComplete` (OpenAI and every provider speaking its chat-completions format), `googleComplete` (Gemini). `createComplete({ provider, model, apiKey, baseUrl })` picks one by name: `anthropic`, `openai`, `google`, `groq`, `mistral`, `deepseek`, `xai`, `openrouter`, `ollama` (local, no key) or `compatible` (any URL). `completeFromEnv()` reads the same from the environment and is what `pagetailor dev` uses:
|
|
173
175
|
|
|
174
|
-
|
|
176
|
+
| Variable | Meaning |
|
|
177
|
+
| --- | --- |
|
|
178
|
+
| `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, `GEMINI_API_KEY`, `GROQ_API_KEY`, `MISTRAL_API_KEY`, `DEEPSEEK_API_KEY`, `XAI_API_KEY`, `OPENROUTER_API_KEY` | the key of the provider to use; with one key set nothing else is needed |
|
|
179
|
+
| `PAGETAILOR_PROVIDER` | force a provider when several keys are set, or choose `ollama` / `compatible` |
|
|
180
|
+
| `PAGETAILOR_MODEL` | the model; defaults exist for anthropic, openai, google, mistral, deepseek, required for the others |
|
|
181
|
+
| `PAGETAILOR_LLM_URL` | base URL for `compatible`, or to override a provider's URL (a proxy, an Ollama on another host) |
|
|
182
|
+
| `PAGETAILOR_LLM_KEY` | the key for `compatible` |
|
|
183
|
+
|
|
184
|
+
In `pagetailor dev`, set one of the variables above and use the "Change this page…" box in the header: `POST /api/agent` with `{ page, user, role, request }` (503 when no provider is configured). `GET /api/agent/prompt?page=&user=&role=&request=` returns the built prompt for debugging.
|
|
175
185
|
|
|
176
186
|
## Pages as answers
|
|
177
187
|
|
|
@@ -182,7 +192,7 @@ A user asks a question ("how did sales go this week?") and the agent composes a
|
|
|
182
192
|
3. Keep as page: `keepAnswer(root, { user, role, title, tree, why })` writes `pages/<slug>.yaml` with `owner: <user>` and adds the slug to `manifest.pages` (slugs are made unique with `-2`, `-3`).
|
|
183
193
|
4. Pin to home: `pinAnswer(root, { user, role, title, tree, why })` proposes, as `user-<user>`, an `insert` of a `layout/card` with the answer's widgets into the home's main container; on a grid it gets its own full-width row at the bottom. Same gate and proposal rules as any override.
|
|
184
194
|
|
|
185
|
-
Both go through the gate with the user's role first. In `pagetailor dev` use the "Ask…" box: `POST /api/ask` `{ user, role, request }` (503
|
|
195
|
+
Both go through the gate with the user's role first. In `pagetailor dev` use the "Ask…" box: `POST /api/ask` `{ user, role, request }` (503 when no provider is configured), `POST /api/ask/keep` and `POST /api/ask/pin` with `{ user, role, title, tree, why }`.
|
|
186
196
|
|
|
187
197
|
## Studio
|
|
188
198
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeratic/pagetailor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Pages as data: declarative YAML pages, typed data catalog, per-user overrides, validated for AI agents. Renders with json-render.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
package/src/dev.js
CHANGED
|
@@ -10,7 +10,7 @@ import { commitOverride, listHistory, rollback } from "./history.js";
|
|
|
10
10
|
import { propose, listPending, approve, reject } from "./proposals.js";
|
|
11
11
|
import { tokensToCss } from "./tokens.js";
|
|
12
12
|
import { buildAgentPrompt, runAgent, runAsk, keepAnswer, pinAnswer } from "./agent.js";
|
|
13
|
-
import {
|
|
13
|
+
import { completeFromEnv, LLM_HELP } from "./llm.js";
|
|
14
14
|
import { isProjectFile, readProjectFile, writeProjectFile, deleteProjectFile, listFileHistory, undoFile, readProject, scaffoldSource, scaffoldComponent } from "./files.js";
|
|
15
15
|
|
|
16
16
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
@@ -101,16 +101,16 @@ export async function dev(cwd, { port = 4747, host = "127.0.0.1" } = {}) {
|
|
|
101
101
|
return json(res, 200, await buildAgentPrompt(root, { page: q.page || "home", user: q.user || undefined, role: q.role || undefined, request: q.request ?? "" }));
|
|
102
102
|
}
|
|
103
103
|
if (url.pathname === "/api/agent" && req.method === "POST") {
|
|
104
|
-
|
|
104
|
+
const complete = completeFromEnv();
|
|
105
|
+
if (!complete) return json(res, 503, { error: LLM_HELP });
|
|
105
106
|
const b = await body(req);
|
|
106
|
-
const complete = anthropicComplete({ apiKey: process.env.ANTHROPIC_API_KEY, ...(process.env.PAGETAILOR_MODEL ? { model: process.env.PAGETAILOR_MODEL } : {}) });
|
|
107
107
|
const r = await runAgent(root, { page: b.page, user: b.user, role: b.role || undefined, request: b.request, complete });
|
|
108
108
|
return json(res, r.ok ? 200 : 400, r);
|
|
109
109
|
}
|
|
110
110
|
if (url.pathname === "/api/ask" && req.method === "POST") {
|
|
111
|
-
|
|
111
|
+
const complete = completeFromEnv();
|
|
112
|
+
if (!complete) return json(res, 503, { error: LLM_HELP });
|
|
112
113
|
const b = await body(req);
|
|
113
|
-
const complete = anthropicComplete({ apiKey: process.env.ANTHROPIC_API_KEY, ...(process.env.PAGETAILOR_MODEL ? { model: process.env.PAGETAILOR_MODEL } : {}) });
|
|
114
114
|
const r = await runAsk(root, { user: b.user || undefined, role: b.role || undefined, request: b.request, complete });
|
|
115
115
|
return json(res, r.ok ? 200 : 400, r);
|
|
116
116
|
}
|
package/src/index.js
CHANGED
|
@@ -15,5 +15,5 @@ export async function dev(...args) {
|
|
|
15
15
|
export { writeRegistryFile } from "./registry-file.js";
|
|
16
16
|
export { tokensToCss } from "./tokens.js";
|
|
17
17
|
export { describeCatalog, describePage, buildAgentPrompt, parseAgentReply, runAgent, buildAskPrompt, runAsk, keepAnswer, pinAnswer } from "./agent.js";
|
|
18
|
-
export { anthropicComplete } from "./llm.js";
|
|
18
|
+
export { anthropicComplete, openaiComplete, googleComplete, createComplete, completeFromEnv, PROVIDERS } from "./llm.js";
|
|
19
19
|
export { readProjectFile, writeProjectFile, deleteProjectFile, listFileHistory, undoFile, scaffoldSource, scaffoldComponent, readProject } from "./files.js";
|
package/src/llm.js
CHANGED
|
@@ -1,14 +1,74 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// Model adapters: each returns a `complete({ system, user }) => text` for runAgent / runAsk, over global fetch.
|
|
2
|
+
// Any provider works with the same shape; pagetailor depends on no SDK.
|
|
3
|
+
|
|
4
|
+
const post = async (url, headers, body) => {
|
|
5
|
+
const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body) });
|
|
6
|
+
const text = await res.text();
|
|
7
|
+
if (!res.ok) throw new Error(`${new URL(url).host} ${res.status}: ${text.slice(0, 500)}`);
|
|
8
|
+
return JSON.parse(text);
|
|
9
|
+
};
|
|
10
|
+
const need = (apiKey, name) => { if (!apiKey) throw new Error(`${name}: apiKey is required`); };
|
|
11
|
+
|
|
12
|
+
export function anthropicComplete({ apiKey, model = "claude-sonnet-5-5", baseUrl = "https://api.anthropic.com", maxTokens = 2048 } = {}) {
|
|
13
|
+
need(apiKey, "anthropicComplete");
|
|
4
14
|
return async ({ system, user }) => {
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
headers: { "content-type": "application/json", "x-api-key": apiKey, "anthropic-version": "2023-06-01" },
|
|
8
|
-
body: JSON.stringify({ model, max_tokens: 1024, system, messages: [{ role: "user", content: user }] }),
|
|
9
|
-
});
|
|
10
|
-
const text = await res.text();
|
|
11
|
-
if (!res.ok) throw new Error(`Anthropic API ${res.status}: ${text}`);
|
|
12
|
-
return (JSON.parse(text).content ?? []).filter(b => b.type === "text").map(b => b.text).join("");
|
|
15
|
+
const r = await post(`${baseUrl}/v1/messages`, { "x-api-key": apiKey, "anthropic-version": "2023-06-01" }, { model, max_tokens: maxTokens, system, messages: [{ role: "user", content: user }] });
|
|
16
|
+
return (r.content ?? []).filter(b => b.type === "text").map(b => b.text).join("");
|
|
13
17
|
};
|
|
14
18
|
}
|
|
19
|
+
|
|
20
|
+
// OpenAI chat completions; also the wire format of Groq, Mistral, DeepSeek, xAI, OpenRouter, Ollama and many others.
|
|
21
|
+
export function openaiComplete({ apiKey, model = "gpt-5.5", baseUrl = "https://api.openai.com/v1", maxTokens = 2048, headers = {} } = {}) {
|
|
22
|
+
if (baseUrl.includes("api.openai.com")) need(apiKey, "openaiComplete");
|
|
23
|
+
return async ({ system, user }) => {
|
|
24
|
+
const r = await post(`${baseUrl.replace(/\/$/, "")}/chat/completions`, { ...(apiKey ? { authorization: `Bearer ${apiKey}` } : {}), ...headers },
|
|
25
|
+
{ model, max_tokens: maxTokens, messages: [{ role: "system", content: system }, { role: "user", content: user }] });
|
|
26
|
+
const c = r.choices?.[0]?.message?.content;
|
|
27
|
+
return Array.isArray(c) ? c.filter(p => p.type === "text").map(p => p.text).join("") : c ?? "";
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function googleComplete({ apiKey, model = "gemini-3.8-flash", baseUrl = "https://generativelanguage.googleapis.com/v1beta", maxTokens = 2048 } = {}) {
|
|
32
|
+
need(apiKey, "googleComplete");
|
|
33
|
+
return async ({ system, user }) => {
|
|
34
|
+
const r = await post(`${baseUrl}/models/${model}:generateContent`, { "x-goog-api-key": apiKey },
|
|
35
|
+
{ system_instruction: { parts: [{ text: system }] }, contents: [{ role: "user", parts: [{ text: user }] }], generationConfig: { maxOutputTokens: maxTokens } });
|
|
36
|
+
return (r.candidates?.[0]?.content?.parts ?? []).map(p => p.text ?? "").join("");
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Known providers: env var holding the key, base URL and default model (null = PAGETAILOR_MODEL required).
|
|
41
|
+
export const PROVIDERS = {
|
|
42
|
+
anthropic: { key: "ANTHROPIC_API_KEY", make: anthropicComplete },
|
|
43
|
+
openai: { key: "OPENAI_API_KEY", make: openaiComplete },
|
|
44
|
+
google: { key: ["GEMINI_API_KEY", "GOOGLE_API_KEY"], make: googleComplete },
|
|
45
|
+
groq: { key: "GROQ_API_KEY", make: openaiComplete, baseUrl: "https://api.groq.com/openai/v1", model: null },
|
|
46
|
+
mistral: { key: "MISTRAL_API_KEY", make: openaiComplete, baseUrl: "https://api.mistral.ai/v1", model: "mistral-large-latest" },
|
|
47
|
+
deepseek: { key: "DEEPSEEK_API_KEY", make: openaiComplete, baseUrl: "https://api.deepseek.com/v1", model: "deepseek-chat" },
|
|
48
|
+
xai: { key: "XAI_API_KEY", make: openaiComplete, baseUrl: "https://api.x.ai/v1", model: null },
|
|
49
|
+
openrouter: { key: "OPENROUTER_API_KEY", make: openaiComplete, baseUrl: "https://openrouter.ai/api/v1", model: null },
|
|
50
|
+
ollama: { key: null, make: openaiComplete, baseUrl: "http://localhost:11434/v1", model: null },
|
|
51
|
+
compatible: { key: "PAGETAILOR_LLM_KEY", make: openaiComplete, baseUrl: null, model: null },
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// createComplete({ provider, model?, apiKey?, baseUrl? }): one call for every provider above.
|
|
55
|
+
export function createComplete({ provider, model, apiKey, baseUrl, ...rest } = {}) {
|
|
56
|
+
const p = PROVIDERS[provider];
|
|
57
|
+
if (!p) throw new Error(`unknown LLM provider "${provider}" (known: ${Object.keys(PROVIDERS).join(", ")})`);
|
|
58
|
+
const url = baseUrl ?? p.baseUrl;
|
|
59
|
+
if (provider === "compatible" && !url) throw new Error("provider compatible: baseUrl is required (PAGETAILOR_LLM_URL)");
|
|
60
|
+
const m = model ?? ("model" in p ? p.model : undefined);
|
|
61
|
+
if (m === null) throw new Error(`provider ${provider}: model is required (PAGETAILOR_MODEL)`);
|
|
62
|
+
return p.make({ apiKey, ...(m ? { model: m } : {}), ...(url ? { baseUrl: url } : {}), ...rest });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Reads the provider from the environment: PAGETAILOR_PROVIDER, or the first provider whose key is set.
|
|
66
|
+
// PAGETAILOR_MODEL overrides the model, PAGETAILOR_LLM_URL the base URL. Returns null when nothing is configured.
|
|
67
|
+
export function completeFromEnv(env = process.env) {
|
|
68
|
+
const keyOf = p => [].concat(PROVIDERS[p].key ?? []).map(k => env[k]).find(Boolean);
|
|
69
|
+
const provider = env.PAGETAILOR_PROVIDER || Object.keys(PROVIDERS).find(p => PROVIDERS[p].key && keyOf(p));
|
|
70
|
+
if (!provider) return null;
|
|
71
|
+
if (!PROVIDERS[provider]) throw new Error(`unknown LLM provider "${provider}" (known: ${Object.keys(PROVIDERS).join(", ")})`);
|
|
72
|
+
return createComplete({ provider, apiKey: keyOf(provider), model: env.PAGETAILOR_MODEL || undefined, baseUrl: env.PAGETAILOR_LLM_URL || undefined });
|
|
73
|
+
}
|
|
74
|
+
export const LLM_HELP = "set an LLM key (ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY, GROQ_API_KEY, MISTRAL_API_KEY, DEEPSEEK_API_KEY, XAI_API_KEY, OPENROUTER_API_KEY) or PAGETAILOR_PROVIDER=ollama|compatible, plus PAGETAILOR_MODEL";
|