@press2ai/engine 0.1.0 → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Grzegorz Durtan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @press2ai/engine
2
+
3
+ Multi-tenant runtime + template contracts for Press2AI vertical landings. SSR on Cloudflare Workers, isomorphic renderer (SSG/browser on roadmap).
4
+
5
+ Used by the `otwarty-*` verticals. See [press2ai](https://codeberg.org/press2ai) for the full ecosystem.
6
+
7
+ ## Install
8
+
9
+ ```
10
+ npm install @press2ai/engine
11
+ ```
12
+
13
+ ## Subpath exports
14
+
15
+ - `@press2ai/engine` — core runtime + types
16
+ - `@press2ai/engine/template-trener` — fitness/trainer vertical template
17
+ - `@press2ai/engine/template-blog` — blog template
18
+ - `@press2ai/engine/types` — TypeScript type exports
19
+
20
+ ## Contract
21
+
22
+ `OtwartyTemplate<C, T>` discriminated union (`Presence | Commerce`). Render = pure function, isomorphic (SSR / SSG / browser). Templates are bundled as subpath exports — adding a vertical does not require engine changes.
23
+
24
+ Engine v0.1 is GET-only. Mutations (POST `/opt-out`, claim flow) live in per-vertical Hono wrappers.
25
+
26
+ ## License
27
+
28
+ MIT — see `LICENSE`. Part of the [Press2AI ecosystem](https://codeberg.org/press2ai).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@press2ai/engine",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Multi-tenant runtime + template contracts dla otwarty-* verticali. SSR na Cloudflare Workers, isomorphic renderer (SSG/browser w roadmapie).",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -15,6 +15,7 @@
15
15
  ".": "./src/index.ts",
16
16
  "./template-trener": "./src/template-trener.ts",
17
17
  "./template-blog": "./src/template-blog.ts",
18
+ "./ceidg-vertical": "./src/ceidg-vertical.ts",
18
19
  "./types": "./src/types.ts"
19
20
  },
20
21
  "scripts": {
@@ -23,7 +24,17 @@
23
24
  "dependencies": {
24
25
  "zod": "^3.23.8"
25
26
  },
27
+ "peerDependencies": {
28
+ "hono": "^4.0.0"
29
+ },
30
+ "peerDependenciesMeta": {
31
+ "hono": {
32
+ "optional": true
33
+ }
34
+ },
26
35
  "devDependencies": {
36
+ "@cloudflare/workers-types": "^4.20260101.0",
37
+ "hono": "^4.6.0",
27
38
  "typescript": "^5.5.0"
28
39
  }
29
40
  }
@@ -0,0 +1,305 @@
1
+ /**
2
+ * createVerticalApp — opinionated helper dla rodziny otwarty-* (CEIDG verticals).
3
+ *
4
+ * Każdy wertykal (`otwarty-trener`, `otwarty-terapeuta`, `otwarty-lekarz`, ...)
5
+ * dzielił dotąd ~230 LOC boilerplate'u: te same Hono routy, ten sam content
6
+ * loader, te same KV-cache helpery, te same opt-out handlery — różnica tylko
7
+ * w stałych. Helper wciąga to całe wnętrze do engine'u; wertykal staje się
8
+ * cienkim configiem (brand, copy, footer, kategoria SQL, pages, theme bundle).
9
+ *
10
+ * Co tu siedzi i dlaczego:
11
+ * - Engine v0.1 jest GET-only (czysty SSR). Mutacje (`POST /opt-out`)
12
+ * żyją tutaj, w warstwie `vertical`, bo to są handlery konkretnej rodziny
13
+ * wertykali, nie generycznego runtime'u. Engine core (`createSSRWorker`)
14
+ * pozostaje agnostyczny.
15
+ * - Schema CEIDG (`leads`, `lead_pkd`, `lead_categories`, `cities`) jest
16
+ * hard-coded w queries — to jest *cel* tego helpera. „Generic vertical
17
+ * framework" nie istnieje, dopóki nie ma trzeciej rodziny niż otwarty-*.
18
+ * - Theme bundle (`TrenerTheme`) jest **input**, nie tworzony tutaj. Engine
19
+ * nie zna theme-specialist-glossy. Wertykal podaje gotowy bundle (~10 LOC
20
+ * adaptera w landingu), helper tylko go wywołuje.
21
+ * - `loadContent` filtruje po `EXISTS lead_categories WHERE category=?`
22
+ * i — fix względem starego trener-landinga — `cities` query też filtruje
23
+ * po kategorii (wcześniej trener pokazywał miasta z całej tabeli leads,
24
+ * mieszając wszystkie wertykale).
25
+ * - Opt-out jest **globalny** — UPDATE leads bez filtra category. RODO
26
+ * sprzeciw to usunięcie danych osobowych, nie selektywne ukrycie w jednej
27
+ * kategorii. Komentarz przeniesiony z terapeuta-landinga.
28
+ *
29
+ * Hono jest peer dependency (każdy wertykal i tak go ma). Engine core nie
30
+ * importuje Hono — tylko ten subpath go używa.
31
+ */
32
+
33
+ import { Hono } from 'hono';
34
+ import { createSSRWorker } from './runtime/ssr';
35
+ import {
36
+ trenerTemplate,
37
+ type TrenerContent,
38
+ type TrenerProfile,
39
+ type TrenerTheme,
40
+ } from './template-trener';
41
+
42
+ /* ─────────────── Public types ─────────────── */
43
+
44
+ /** Kształt wiersza z `leads` JOIN `cities` JOIN (subselect) `lead_pkd`. */
45
+ export type CeidgLead = {
46
+ nip: string;
47
+ first_name: string;
48
+ last_name: string;
49
+ city: string | null;
50
+ company_name: string;
51
+ pkd: string | null;
52
+ slug: string;
53
+ claimed: number;
54
+ external_site_url: string | null;
55
+ fetched_at: number;
56
+ opted_out_at: number | null;
57
+ };
58
+
59
+ export type CeidgBindings = { DB: D1Database; CACHE: KVNamespace };
60
+
61
+ /** Statyczna strona prawna/info — pełne body HTML, opcjonalny meta description. */
62
+ export interface StaticPage {
63
+ title: string;
64
+ body: string;
65
+ description?: string;
66
+ }
67
+
68
+ export interface VerticalConfig {
69
+ /** Wartość `lead_categories.category`. Używana w SQL i w prefiksach KV cache. */
70
+ category: string;
71
+ /** Wersja landinga — wyświetlana w `/health` (debug) i w stopce. */
72
+ version: string;
73
+ brand: { siteName: string; description: string };
74
+ copy: {
75
+ heroBadge?: string;
76
+ heroTitle: string;
77
+ heroSubtitle?: string;
78
+ searchPlaceholder?: string;
79
+ };
80
+ /** PKD → human-readable jobTitle. Per-wertykal mapping. */
81
+ pkdToJobTitle(pkd: string | null | undefined): string;
82
+ /** Statyczne strony — RODO/regulamin/opt-out. Body HTML wkleja się w layout. */
83
+ pages: {
84
+ zasady: StaticPage;
85
+ optOutForm: StaticPage;
86
+ optOutDone: StaticPage;
87
+ /** Optional — RODO art. 14 dla linków społecznościowych. */
88
+ linkiInfo?: StaticPage;
89
+ };
90
+ /** llms.txt — header + intro paragraph. Lista profili dopisywana automatycznie. */
91
+ llms: { title: string; intro: string };
92
+ /** Theme bundle. Wertykal buduje adapter (Profile ↔ TrenerProfile) i podaje gotowy. */
93
+ theme: TrenerTheme;
94
+ }
95
+
96
+ /* ─────────────── Implementation ─────────────── */
97
+
98
+ const KV_TTL = 86400;
99
+ const OPTOUT_LIMIT = 5;
100
+ const OPTOUT_WINDOW_SEC = 3600;
101
+
102
+ export function createVerticalApp(config: VerticalConfig): Hono<{ Bindings: CeidgBindings }> {
103
+ const app = new Hono<{ Bindings: CeidgBindings }>();
104
+ app.onError((err, c) => c.json({ error: err.message, stack: err.stack }, 500));
105
+
106
+ const { category, version, brand, copy, pkdToJobTitle, pages, llms, theme } = config;
107
+
108
+ // ─────────── SQL ───────────
109
+ // EXISTS subselect zamiast JOIN: lead z N PKDs nie multiplikuje wierszy.
110
+ // (SELECT pkd ... LIMIT 1) wybiera jeden PKD per lead — wystarczy do
111
+ // pkdToJobTitle. To jest świadomy uproszczenie: lead z mieszanymi PKDs
112
+ // (np. fizjoterapia + psychologia) dostanie tylko jeden label.
113
+ const leadsQuery = `
114
+ SELECT l.nip, l.first_name, l.last_name, c.name as city,
115
+ l.company_name, l.slug, l.claimed, l.external_site_url, l.fetched_at,
116
+ l.opted_out_at,
117
+ (SELECT pkd FROM lead_pkd WHERE nip = l.nip LIMIT 1) as pkd
118
+ FROM leads l
119
+ LEFT JOIN cities c ON l.city_id = c.id
120
+ WHERE l.opted_out_at IS NULL
121
+ AND EXISTS (SELECT 1 FROM lead_categories WHERE lead_nip = l.nip AND category = ?)`;
122
+
123
+ // Cities query filtruje po kategorii — fix względem starego trener landinga
124
+ // który pokazywał miasta z całej tabeli leads (mieszane wertykale).
125
+ const citiesQuery = `
126
+ SELECT c.name, COUNT(*) as count
127
+ FROM leads l
128
+ JOIN cities c ON l.city_id = c.id
129
+ INNER JOIN lead_categories lc ON l.nip = lc.lead_nip AND lc.category = ?
130
+ WHERE l.opted_out_at IS NULL
131
+ GROUP BY c.name ORDER BY count DESC LIMIT 8`;
132
+
133
+ // ─────────── Content loader ───────────
134
+ function leadToProfile(l: CeidgLead): TrenerProfile {
135
+ return {
136
+ slug: l.slug,
137
+ firstName: l.first_name,
138
+ lastName: l.last_name,
139
+ jobTitle: pkdToJobTitle(l.pkd),
140
+ city: l.city ?? undefined,
141
+ specialties: [],
142
+ languages: [],
143
+ business: { name: l.company_name, taxId: l.nip, classification: l.pkd ? [l.pkd] : [] },
144
+ social: {},
145
+ };
146
+ }
147
+
148
+ async function loadContent(_host: string, env: unknown): Promise<TrenerContent> {
149
+ const e = env as CeidgBindings;
150
+ const [leadsRes, citiesRes] = await Promise.all([
151
+ e.DB.prepare(leadsQuery + ' ORDER BY c.name, l.last_name').bind(category).all<CeidgLead>(),
152
+ e.DB.prepare(citiesQuery).bind(category).all<{ name: string; count: number }>(),
153
+ ]);
154
+ return {
155
+ brand: { siteName: brand.siteName, description: brand.description },
156
+ copy: {
157
+ heroBadge: copy.heroBadge,
158
+ heroTitle: copy.heroTitle,
159
+ heroSubtitle: copy.heroSubtitle,
160
+ searchPlaceholder: copy.searchPlaceholder ?? 'Szukaj...',
161
+ },
162
+ profiles: (leadsRes.results ?? []).map(leadToProfile),
163
+ cities: citiesRes.results ?? [],
164
+ };
165
+ }
166
+
167
+ const engine = createSSRWorker({ template: trenerTemplate, theme, loadContent });
168
+
169
+ // ─────────── KV cache helper ───────────
170
+ async function cached<T>(kv: KVNamespace, key: string, fn: () => Promise<T>): Promise<T> {
171
+ const hit = await kv.get(key);
172
+ if (hit) return JSON.parse(hit) as T;
173
+ const data = await fn();
174
+ await kv.put(key, JSON.stringify(data), { expirationTtl: KV_TTL });
175
+ return data;
176
+ }
177
+
178
+ const ck = (suffix: string) => `vertical:${category}:${suffix}`;
179
+
180
+ // ─────────── Static page renderer ───────────
181
+ // Wszystkie strony statyczne idą przez ten helper — theme.layout dostarcza
182
+ // siteName/footer/CSS, wertykal podaje tylko {title, body, description}.
183
+ function renderStatic(page: StaticPage): string {
184
+ return theme.layout({ title: page.title, description: page.description }, page.body);
185
+ }
186
+
187
+ // ─────────── Routes: legacy / cached / mutations ───────────
188
+
189
+ app.get('/health', async (c) => {
190
+ const { results } = await c.env.DB
191
+ .prepare('SELECT COUNT(*) as cnt FROM lead_categories WHERE category = ?')
192
+ .bind(category)
193
+ .all();
194
+ return c.json({ ok: true, db: results, version, category });
195
+ });
196
+
197
+ app.get('/catalog.json', async (c) => {
198
+ const data = await cached(c.env.CACHE, ck('catalog.json'), async () => {
199
+ const { results } = await c.env.DB
200
+ .prepare(leadsQuery + ' ORDER BY c.name, l.last_name')
201
+ .bind(category)
202
+ .all<CeidgLead>();
203
+ const leads = results ?? [];
204
+ return {
205
+ version: 1,
206
+ source: 'ceidg',
207
+ category,
208
+ count: leads.length,
209
+ items: leads.map((l) => ({
210
+ slug: l.slug,
211
+ firstName: l.first_name,
212
+ lastName: l.last_name,
213
+ city: l.city,
214
+ companyName: l.company_name,
215
+ claimed: !!l.claimed,
216
+ url: '/' + l.slug,
217
+ })),
218
+ };
219
+ });
220
+ return c.json(data);
221
+ });
222
+
223
+ app.get('/llms.txt', async (c) => {
224
+ const text = await cached(c.env.CACHE, ck('llms.txt'), async () => {
225
+ const { results } = await c.env.DB
226
+ .prepare(leadsQuery + ' ORDER BY c.name, l.last_name')
227
+ .bind(category)
228
+ .all<CeidgLead>();
229
+ const leads = results ?? [];
230
+ return [
231
+ `# ${llms.title}`,
232
+ llms.intro,
233
+ '',
234
+ '## Wpisy',
235
+ ...leads.map((l) => `- ${l.first_name} ${l.last_name} (${l.city ?? '—'}) — /${l.slug}`),
236
+ ].join('\n');
237
+ });
238
+ return c.text(text);
239
+ });
240
+
241
+ app.get('/sitemap.xml', async (c) => {
242
+ const host = new URL(c.req.url).origin;
243
+ const xml = await cached(c.env.CACHE, ck(`sitemap:${host}`), async () => {
244
+ const { results } = await c.env.DB
245
+ .prepare(leadsQuery)
246
+ .bind(category)
247
+ .all<CeidgLead>();
248
+ const urls = [host + '/', ...(results ?? []).map((l) => host + '/' + l.slug)];
249
+ return (
250
+ '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' +
251
+ urls.map((u) => ` <url><loc>${u}</loc></url>`).join('\n') +
252
+ '\n</urlset>'
253
+ );
254
+ });
255
+ return c.body(xml, 200, { 'content-type': 'application/xml' });
256
+ });
257
+
258
+ app.get('/zasady', (c) => c.html(renderStatic(pages.zasady)));
259
+ if (pages.linkiInfo) {
260
+ app.get('/linki-info', (c) => c.html(renderStatic(pages.linkiInfo!)));
261
+ }
262
+ app.get('/opt-out', (c) => c.html(renderStatic(pages.optOutForm)));
263
+
264
+ app.post('/opt-out', async (c) => {
265
+ const form = await c.req.formData();
266
+ const nip = String(form.get('nip') ?? '').replace(/\D/g, '');
267
+ if (nip.length !== 10) return c.text('Nieprawidłowy NIP', 400);
268
+
269
+ const ip = c.req.header('cf-connecting-ip') ?? '0.0.0.0';
270
+ const rlKey = `vertical:${category}:rl:optout:${ip}`;
271
+ const rlVal = parseInt((await c.env.CACHE.get(rlKey)) ?? '0', 10);
272
+ if (rlVal >= OPTOUT_LIMIT) return c.text('Zbyt wiele prób. Spróbuj ponownie za godzinę.', 429);
273
+ await c.env.CACHE.put(rlKey, String(rlVal + 1), { expirationTtl: OPTOUT_WINDOW_SEC });
274
+
275
+ // Opt-out jest GLOBALNY — usuwa też z innych wertykali. RODO sprzeciw to
276
+ // usunięcie danych osobowych, nie selektywne ukrycie w jednej kategorii.
277
+ const res = await c.env.DB
278
+ .prepare(
279
+ 'UPDATE leads SET opted_out_at = ?, opted_out_ip = ?, opted_out_ua = ? WHERE nip = ? AND opted_out_at IS NULL',
280
+ )
281
+ .bind(Math.floor(Date.now() / 1000), ip, c.req.header('user-agent') ?? '', nip)
282
+ .run();
283
+
284
+ if (!res.meta.changes) return c.text('Nie znaleziono wpisu', 404);
285
+ return c.html(renderStatic(pages.optOutDone));
286
+ });
287
+
288
+ // ─────────── /:slug — claim redirect → engine fallback ───────────
289
+ app.get('/:slug', async (c) => {
290
+ const slug = c.req.param('slug');
291
+ const claim = await c.env.DB
292
+ .prepare('SELECT claimed, external_site_url FROM leads WHERE slug = ?')
293
+ .bind(slug)
294
+ .first<{ claimed: number; external_site_url: string | null }>();
295
+ if (claim?.claimed && claim.external_site_url) {
296
+ return c.redirect(claim.external_site_url);
297
+ }
298
+ return engine.fetch(c.req.raw, c.env);
299
+ });
300
+
301
+ // ─────────── / — engine ───────────
302
+ app.get('/', (c) => engine.fetch(c.req.raw, c.env));
303
+
304
+ return app;
305
+ }