@appeeky/open-slideshow 0.0.0-stage → 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 +21 -0
- package/README.md +297 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +9 -0
- package/dist/brand.d.ts +57 -0
- package/dist/brand.js +263 -0
- package/dist/brandkit.d.ts +50 -0
- package/dist/brandkit.js +212 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +355 -0
- package/dist/decks.d.ts +44 -0
- package/dist/decks.js +100 -0
- package/dist/fonts.d.ts +45 -0
- package/dist/fonts.js +243 -0
- package/dist/gallery.d.ts +35 -0
- package/dist/gallery.js +109 -0
- package/dist/ideas.d.ts +50 -0
- package/dist/ideas.js +146 -0
- package/dist/images.d.ts +62 -0
- package/dist/images.js +241 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +22 -0
- package/dist/lint.d.ts +36 -0
- package/dist/lint.js +197 -0
- package/dist/mcp.d.ts +3 -0
- package/dist/mcp.js +303 -0
- package/dist/preview.d.ts +18 -0
- package/dist/preview.js +96 -0
- package/dist/publish.d.ts +105 -0
- package/dist/publish.js +406 -0
- package/dist/readability.d.ts +19 -0
- package/dist/readability.js +42 -0
- package/dist/render.d.ts +92 -0
- package/dist/render.js +647 -0
- package/dist/results.d.ts +89 -0
- package/dist/results.js +252 -0
- package/dist/schedule.d.ts +19 -0
- package/dist/schedule.js +114 -0
- package/dist/schema.d.ts +1159 -0
- package/dist/schema.js +211 -0
- package/dist/screenshots.d.ts +18 -0
- package/dist/screenshots.js +69 -0
- package/dist/shape.d.ts +31 -0
- package/dist/shape.js +169 -0
- package/dist/skill.d.ts +7 -0
- package/dist/skill.js +17 -0
- package/dist/templates.d.ts +47 -0
- package/dist/templates.js +330 -0
- package/dist/text.d.ts +40 -0
- package/dist/text.js +231 -0
- package/dist/themes.d.ts +43 -0
- package/dist/themes.js +202 -0
- package/dist/variants.d.ts +48 -0
- package/dist/variants.js +175 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/dist/video.d.ts +22 -0
- package/dist/video.js +59 -0
- package/dist/viral.d.ts +12 -0
- package/dist/viral.js +35 -0
- package/dist/workspace.d.ts +85 -0
- package/dist/workspace.js +322 -0
- package/package.json +65 -4
- package/skills/open-slideshow/SKILL.md +65 -0
- package/skills/open-slideshow/references/brand.md +64 -0
- package/skills/open-slideshow/references/channels.md +63 -0
- package/skills/open-slideshow/references/ideas.md +23 -0
- package/skills/open-slideshow/references/images.md +61 -0
- package/skills/open-slideshow/references/languages.md +51 -0
- package/skills/open-slideshow/references/publishing.md +70 -0
- package/skills/open-slideshow/references/results.md +47 -0
- package/skills/open-slideshow/references/review.md +81 -0
- package/skills/open-slideshow/references/screenshots.md +26 -0
- package/skills/open-slideshow/references/series.md +20 -0
- package/skills/open-slideshow/references/templates.md +96 -0
- package/skills/open-slideshow/references/troubleshooting.md +92 -0
- package/skills/open-slideshow/references/video.md +18 -0
- package/skills/open-slideshow/references/writing.md +76 -0
package/dist/ideas.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import YAML from "yaml";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { normalizeHook, specListing } from "./brand.js";
|
|
7
|
+
import { listChannels, listDecks, parseWith, readJsonl, UserError, writeAtomic } from "./workspace.js";
|
|
8
|
+
export const ideaSchema = z
|
|
9
|
+
.object({
|
|
10
|
+
id: z.string().min(1).max(64),
|
|
11
|
+
text: z.string().min(1).max(400),
|
|
12
|
+
/** Where it came from: review, comment, listing, playbook, channel, viral, user. */
|
|
13
|
+
source: z.string().min(1).max(40),
|
|
14
|
+
/** The words behind it (a review, a comment), so the slides can stay true to them. */
|
|
15
|
+
evidence: z.string().max(600).optional(),
|
|
16
|
+
status: z.enum(["new", "used", "skipped"]).default("new"),
|
|
17
|
+
deck: z.string().optional(),
|
|
18
|
+
added_at: z.string(),
|
|
19
|
+
})
|
|
20
|
+
.strict();
|
|
21
|
+
const fileSchema = z.object({ ideas: z.array(ideaSchema).default([]) }).strict();
|
|
22
|
+
export const ideasFile = (ws) => join(ws.dir, "ideas.yaml");
|
|
23
|
+
export function readIdeas(ws) {
|
|
24
|
+
const f = ideasFile(ws);
|
|
25
|
+
if (!existsSync(f))
|
|
26
|
+
return [];
|
|
27
|
+
return parseWith(fileSchema, YAML.parse(readFileSync(f, "utf8")) ?? {}, f).ideas;
|
|
28
|
+
}
|
|
29
|
+
function saveIdeas(ws, ideas) {
|
|
30
|
+
writeAtomic(ideasFile(ws), `# Slideshow ideas: open-slideshow adds them (ideas_collect), you or your agent pick one, it becomes a deck.\n${YAML.stringify({ ideas }, { lineWidth: 0 })}`);
|
|
31
|
+
}
|
|
32
|
+
const ideaId = (text) => createHash("sha256").update(normalizeHook(text)).digest("hex").slice(0, 10);
|
|
33
|
+
/** Adds ideas that are not in the queue yet (same words = same idea). Returns the ones added. */
|
|
34
|
+
export function addIdeas(ws, fresh, now = new Date(), max = Infinity) {
|
|
35
|
+
const ideas = readIdeas(ws);
|
|
36
|
+
const have = new Set(ideas.map((i) => i.id));
|
|
37
|
+
const added = [];
|
|
38
|
+
for (const f of fresh) {
|
|
39
|
+
if (added.length >= max)
|
|
40
|
+
break;
|
|
41
|
+
const id = ideaId(f.text);
|
|
42
|
+
if (have.has(id))
|
|
43
|
+
continue;
|
|
44
|
+
have.add(id);
|
|
45
|
+
const idea = ideaSchema.parse({ ...f, text: f.text.slice(0, 400), ...(f.evidence ? { evidence: f.evidence.slice(0, 600) } : {}), id, status: "new", added_at: now.toISOString() });
|
|
46
|
+
ideas.push(idea);
|
|
47
|
+
added.push(idea);
|
|
48
|
+
}
|
|
49
|
+
if (added.length)
|
|
50
|
+
saveIdeas(ws, ideas);
|
|
51
|
+
return added;
|
|
52
|
+
}
|
|
53
|
+
export function updateIdea(ws, id, patch) {
|
|
54
|
+
const ideas = readIdeas(ws);
|
|
55
|
+
const i = ideas.find((x) => x.id === id);
|
|
56
|
+
if (!i)
|
|
57
|
+
throw new UserError(`no idea ${id}; ideas_list shows them`);
|
|
58
|
+
Object.assign(i, patch);
|
|
59
|
+
saveIdeas(ws, ideas);
|
|
60
|
+
return i;
|
|
61
|
+
}
|
|
62
|
+
const clip = (s, n = 220) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
|
|
63
|
+
/** Reviews Spec pulled: appeeky/reviews/<store>/<YYYY-MM>.jsonl. */
|
|
64
|
+
function specReviews(ws) {
|
|
65
|
+
const dir = ws.spec?.reviews_dir;
|
|
66
|
+
if (!dir || !existsSync(dir))
|
|
67
|
+
return [];
|
|
68
|
+
const out = [];
|
|
69
|
+
for (const store of readdirSync(dir)) {
|
|
70
|
+
const sd = join(dir, store);
|
|
71
|
+
if (!/^(app-store|google-play)$/.test(store) || !existsSync(sd))
|
|
72
|
+
continue;
|
|
73
|
+
for (const f of readdirSync(sd).filter((f) => /^\d{4}-\d{2}\.jsonl$/.test(f)).sort().reverse().slice(0, 3))
|
|
74
|
+
out.push(...readJsonl(join(sd, f)));
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
/** Recent App Store reviews from Apple's public feed, for a workspace outside Spec. */
|
|
79
|
+
async function appStoreReviews(id, country) {
|
|
80
|
+
try {
|
|
81
|
+
const res = await fetch(`https://itunes.apple.com/${encodeURIComponent(country)}/rss/customerreviews/id=${encodeURIComponent(id)}/sortBy=mostRecent/json`, { signal: AbortSignal.timeout(10_000) });
|
|
82
|
+
if (!res.ok)
|
|
83
|
+
return [];
|
|
84
|
+
const data = (await res.json());
|
|
85
|
+
return (data.feed?.entry ?? []).map((e) => ({ title: e.title?.label, body: e.content?.label, rating: Number(e["im:rating"]?.label ?? 0), date: e.updated?.label }));
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const QUESTION = /\?|\b(how do|how can|does it|can it|is there|what about|why)\b/i;
|
|
92
|
+
const COMPLAINT = /\b(cancel|refund|charged|scam|rip ?off|subscription trap|crash(es|ed)?|bug(gy)?|broken|doesn.?t work|waste)\b/i;
|
|
93
|
+
const WISH = /\b(wish|please add|would love|need(s)? a|missing|feature request|should have|add an? )\b/i;
|
|
94
|
+
/**
|
|
95
|
+
* Turns what the repo and the store already know into slideshow ideas: questions people keep
|
|
96
|
+
* asking, what reviewers love or miss, the listing's own features, playbook wins and channel
|
|
97
|
+
* topics not used yet. Nothing is posted or sent anywhere.
|
|
98
|
+
*/
|
|
99
|
+
export async function collectIdeas(ws, opts = {}) {
|
|
100
|
+
const found = [];
|
|
101
|
+
const limit = opts.limit ?? 20;
|
|
102
|
+
const reviews = ws.spec ? specReviews(ws) : ws.settings.app.app_store_id && !opts.offline ? await appStoreReviews(ws.settings.app.app_store_id, ws.settings.app.country) : [];
|
|
103
|
+
for (const r of reviews) {
|
|
104
|
+
const words = `${r.title ?? ""} ${r.body ?? ""}`.trim();
|
|
105
|
+
if (words.length < 25 || COMPLAINT.test(words))
|
|
106
|
+
continue;
|
|
107
|
+
// A short title ("Try it!") says nothing; the first sentence of the review does.
|
|
108
|
+
const gist = clip((r.title ?? "").length >= 18 ? r.title : (r.body ?? r.title ?? "").split(/(?<=[.!?])\s/)[0], 90);
|
|
109
|
+
const wish = `${r.title ?? ""}. ${r.body ?? ""}`.split(/(?<=[.!?])\s+/).find((x) => WISH.test(x));
|
|
110
|
+
if (wish)
|
|
111
|
+
found.push({ text: `Answer a wish from reviews: ${clip(wish.trim(), 110)}`, source: "review", evidence: clip(words, 500) });
|
|
112
|
+
else if ((r.rating ?? 0) >= 5 && words.length > 60)
|
|
113
|
+
found.push({ text: `What one user loves, told as a story: ${gist}`, source: "review", evidence: clip(words, 500) });
|
|
114
|
+
}
|
|
115
|
+
if (ws.spec?.comments_file && existsSync(ws.spec.comments_file)) {
|
|
116
|
+
for (const c of readJsonl(ws.spec.comments_file)) {
|
|
117
|
+
if (c.own || c.reply || !c.text || c.text.length < 15 || !QUESTION.test(c.text))
|
|
118
|
+
continue;
|
|
119
|
+
found.push({ text: `A slideshow that answers: "${clip(c.text, 120)}"`, source: "comment", evidence: clip(c.text, 500) });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const listing = ws.mode === "spec" ? specListing(ws) : undefined;
|
|
123
|
+
if (listing) {
|
|
124
|
+
for (const line of listing.split("\n")) {
|
|
125
|
+
const m = line.match(/^\s*[•\-*]\s+(.{8,120})$/);
|
|
126
|
+
if (m)
|
|
127
|
+
found.push({ text: `Show the feature in use (with a screenshot slide): ${m[1].trim()}`, source: "listing", evidence: m[1].trim() });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (ws.spec?.playbook && existsSync(ws.spec.playbook)) {
|
|
131
|
+
for (const line of readFileSync(ws.spec.playbook, "utf8").split("\n")) {
|
|
132
|
+
if (/→.*\bwon\b/i.test(line))
|
|
133
|
+
found.push({ text: `Turn a win into a post: ${clip(line.replace(/^[-*]\s*/, ""), 160)}`, source: "playbook", evidence: clip(line, 500) });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const usedTopics = new Set(listDecks(ws).map((d) => normalizeHook(d.deck?.topic ?? "")).filter(Boolean));
|
|
137
|
+
for (const c of listChannels(ws))
|
|
138
|
+
for (const t of c.channel?.content.topics ?? [])
|
|
139
|
+
if (!usedTopics.has(normalizeHook(t)))
|
|
140
|
+
found.push({ text: `${c.id}: ${t}`, source: "channel" });
|
|
141
|
+
// Questions and wishes first: they come from real people.
|
|
142
|
+
const order = ["comment", "review", "listing", "playbook", "channel"];
|
|
143
|
+
found.sort((a, b) => order.indexOf(a.source) - order.indexOf(b.source));
|
|
144
|
+
const added = addIdeas(ws, found, new Date(), limit);
|
|
145
|
+
return { added, found: found.length };
|
|
146
|
+
}
|
package/dist/images.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type Workspace } from "./workspace.js";
|
|
2
|
+
export declare const MAX_IMAGE_BYTES: number;
|
|
3
|
+
export interface ImageMeta {
|
|
4
|
+
ref: string;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
source: string;
|
|
8
|
+
license: string;
|
|
9
|
+
author?: string;
|
|
10
|
+
url?: string;
|
|
11
|
+
landing_url?: string;
|
|
12
|
+
attribution?: string;
|
|
13
|
+
note?: string;
|
|
14
|
+
imported_at: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function isPrivateAddress(ip: string): boolean;
|
|
17
|
+
/** Refuses URLs that point at this machine or a private network, before every hop. */
|
|
18
|
+
export declare function assertPublicUrl(raw: string): Promise<URL>;
|
|
19
|
+
export declare function download(raw: string, retried?: boolean): Promise<Buffer>;
|
|
20
|
+
/** Decodes, turns upright, strips metadata (GPS too) and stores the image as JPEG under images/. */
|
|
21
|
+
export declare function storeImage(ws: Workspace, input: Buffer, meta: Omit<ImageMeta, "ref" | "width" | "height" | "imported_at">, opts?: {
|
|
22
|
+
minSize?: number;
|
|
23
|
+
}): Promise<ImageMeta>;
|
|
24
|
+
export declare function importImageFromUrl(ws: Workspace, url: string, meta?: Partial<Omit<ImageMeta, "ref" | "width" | "height" | "imported_at">>, opts?: {
|
|
25
|
+
minSize?: number;
|
|
26
|
+
}): Promise<ImageMeta>;
|
|
27
|
+
/** A file the user has. Paths outside the workspace are fine to read (it is the user's machine), never to reference. */
|
|
28
|
+
export declare function importImageFromFile(ws: Workspace, path: string, meta?: Partial<Omit<ImageMeta, "ref" | "width" | "height" | "imported_at">>): Promise<ImageMeta>;
|
|
29
|
+
export declare function listImages(ws: Workspace): ImageMeta[];
|
|
30
|
+
export declare function imageMeta(ws: Workspace, ref: string): ImageMeta | null;
|
|
31
|
+
/** Licenses people can record with image_license: their own, bought or licensed, or an open license. */
|
|
32
|
+
export declare const LICENSES: readonly ["own", "licensed", "cc0", "pdm", "by-4.0", "by-3.0", "by-2.0", "unsplash", "pexels", "unknown"];
|
|
33
|
+
/** Whether someone said the image may be posted: anything but unknown. */
|
|
34
|
+
export declare function licenseKnown(m: ImageMeta): boolean;
|
|
35
|
+
/** Records who may post an image and under which license; the image itself does not change. */
|
|
36
|
+
export declare function setImageLicense(ws: Workspace, ref: string, license: string, extra?: {
|
|
37
|
+
author?: string;
|
|
38
|
+
note?: string;
|
|
39
|
+
}): ImageMeta;
|
|
40
|
+
/** Licenses that need the author named somewhere: we put it in credits.txt and the caption suggestion. */
|
|
41
|
+
export declare function needsAttribution(m: ImageMeta | null): boolean;
|
|
42
|
+
export interface ImageHit {
|
|
43
|
+
provider: "openverse" | "unsplash" | "pexels";
|
|
44
|
+
id: string;
|
|
45
|
+
url: string;
|
|
46
|
+
thumb: string;
|
|
47
|
+
width?: number;
|
|
48
|
+
height?: number;
|
|
49
|
+
author?: string;
|
|
50
|
+
license: string;
|
|
51
|
+
landing_url?: string;
|
|
52
|
+
attribution?: string;
|
|
53
|
+
download_location?: string;
|
|
54
|
+
}
|
|
55
|
+
export declare function searchImages(query: string, opts?: {
|
|
56
|
+
provider?: ImageHit["provider"];
|
|
57
|
+
orientation?: "portrait" | "landscape" | "square";
|
|
58
|
+
limit?: number;
|
|
59
|
+
}): Promise<ImageHit[]>;
|
|
60
|
+
export declare function importHit(ws: Workspace, hit: ImageHit): Promise<ImageMeta>;
|
|
61
|
+
/** Thumbnails of the hits in one numbered picture, so the agent can look before it picks. */
|
|
62
|
+
export declare function hitsSheet(hits: ImageHit[]): Promise<Buffer | null>;
|
package/dist/images.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lookup } from "node:dns/promises";
|
|
3
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
4
|
+
import { isIP } from "node:net";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import sharp from "sharp";
|
|
7
|
+
import { imagesDir, isInside, readJson, UserError, writeAtomic } from "./workspace.js";
|
|
8
|
+
export const MAX_IMAGE_BYTES = 25 * 1024 * 1024;
|
|
9
|
+
const MAX_PIXELS = 80_000_000;
|
|
10
|
+
const ALLOWED_FORMATS = new Set(["jpeg", "png", "webp", "avif", "heif", "gif", "tiff"]);
|
|
11
|
+
// ---------- network safety ----------
|
|
12
|
+
function privateV4(ip) {
|
|
13
|
+
const [a, b] = ip.split(".").map(Number);
|
|
14
|
+
return a === 0 || a === 10 || a === 127 || (a === 169 && b === 254) || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168) || (a === 100 && b >= 64 && b <= 127) || a >= 224 || (a === 192 && b === 0) || (a === 198 && (b === 18 || b === 19));
|
|
15
|
+
}
|
|
16
|
+
export function isPrivateAddress(ip) {
|
|
17
|
+
if (isIP(ip) === 4)
|
|
18
|
+
return privateV4(ip);
|
|
19
|
+
const v6 = ip.toLowerCase();
|
|
20
|
+
if (v6 === "::" || v6 === "::1")
|
|
21
|
+
return true;
|
|
22
|
+
const mapped = v6.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/);
|
|
23
|
+
if (mapped)
|
|
24
|
+
return privateV4(mapped[1]);
|
|
25
|
+
return v6.startsWith("fc") || v6.startsWith("fd") || v6.startsWith("fe8") || v6.startsWith("fe9") || v6.startsWith("fea") || v6.startsWith("feb") || v6.startsWith("ff");
|
|
26
|
+
}
|
|
27
|
+
/** Refuses URLs that point at this machine or a private network, before every hop. */
|
|
28
|
+
export async function assertPublicUrl(raw) {
|
|
29
|
+
let url;
|
|
30
|
+
try {
|
|
31
|
+
url = new URL(raw);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
throw new UserError(`not a URL: ${raw}`);
|
|
35
|
+
}
|
|
36
|
+
if (url.protocol !== "https:")
|
|
37
|
+
throw new UserError("only https image URLs are allowed");
|
|
38
|
+
if (url.username || url.password)
|
|
39
|
+
throw new UserError("URLs with credentials are not allowed");
|
|
40
|
+
const host = url.hostname.replace(/^\[|\]$/g, "");
|
|
41
|
+
if (host === "localhost" || host.endsWith(".localhost") || host.endsWith(".local") || host.endsWith(".internal"))
|
|
42
|
+
throw new UserError("local addresses are not allowed");
|
|
43
|
+
const addrs = isIP(host) ? [{ address: host }] : await lookup(host, { all: true }).catch(() => {
|
|
44
|
+
throw new UserError(`cannot resolve ${host}`);
|
|
45
|
+
});
|
|
46
|
+
if (!addrs.length || addrs.some((a) => isPrivateAddress(a.address)))
|
|
47
|
+
throw new UserError(`${host} points to a private address`);
|
|
48
|
+
return url;
|
|
49
|
+
}
|
|
50
|
+
export async function download(raw, retried = false) {
|
|
51
|
+
let current = raw;
|
|
52
|
+
for (let hop = 0; hop < 4; hop++) {
|
|
53
|
+
const url = await assertPublicUrl(current);
|
|
54
|
+
const res = await fetch(url, { redirect: "manual", signal: AbortSignal.timeout(20_000), headers: { "user-agent": "open-slideshow" } });
|
|
55
|
+
if (res.status >= 300 && res.status < 400) {
|
|
56
|
+
const loc = res.headers.get("location");
|
|
57
|
+
if (!loc)
|
|
58
|
+
throw new UserError("redirect without a location");
|
|
59
|
+
current = new URL(loc, url).toString();
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (res.status === 429 || res.status === 503) {
|
|
63
|
+
const wait = Math.min(Number(res.headers.get("retry-after") ?? 2) || 2, 5);
|
|
64
|
+
if (!retried) {
|
|
65
|
+
await new Promise((r) => setTimeout(r, wait * 1000));
|
|
66
|
+
return download(raw, true);
|
|
67
|
+
}
|
|
68
|
+
throw new UserError(`${url.hostname} is limiting requests right now (HTTP ${res.status}); wait a minute or pick another image`);
|
|
69
|
+
}
|
|
70
|
+
if (!res.ok)
|
|
71
|
+
throw new UserError(`download failed: HTTP ${res.status}`);
|
|
72
|
+
const type = (res.headers.get("content-type") ?? "").split(";")[0].trim();
|
|
73
|
+
if (type && !type.startsWith("image/") && type !== "application/octet-stream")
|
|
74
|
+
throw new UserError(`not an image (${type})`);
|
|
75
|
+
if (type === "image/svg+xml")
|
|
76
|
+
throw new UserError("SVG images are not accepted");
|
|
77
|
+
const len = Number(res.headers.get("content-length") ?? 0);
|
|
78
|
+
if (len > MAX_IMAGE_BYTES)
|
|
79
|
+
throw new UserError("image is larger than 25 MB");
|
|
80
|
+
const chunks = [];
|
|
81
|
+
let total = 0;
|
|
82
|
+
for await (const chunk of res.body) {
|
|
83
|
+
total += chunk.byteLength;
|
|
84
|
+
if (total > MAX_IMAGE_BYTES)
|
|
85
|
+
throw new UserError("image is larger than 25 MB");
|
|
86
|
+
chunks.push(Buffer.from(chunk));
|
|
87
|
+
}
|
|
88
|
+
return Buffer.concat(chunks);
|
|
89
|
+
}
|
|
90
|
+
throw new UserError("too many redirects");
|
|
91
|
+
}
|
|
92
|
+
// ---------- storing ----------
|
|
93
|
+
/** Decodes, turns upright, strips metadata (GPS too) and stores the image as JPEG under images/. */
|
|
94
|
+
export async function storeImage(ws, input, meta, opts = {}) {
|
|
95
|
+
let info;
|
|
96
|
+
try {
|
|
97
|
+
info = await sharp(input, { limitInputPixels: MAX_PIXELS }).metadata();
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
throw new UserError("the file is not an image that can be read");
|
|
101
|
+
}
|
|
102
|
+
if (!info.format || !ALLOWED_FORMATS.has(info.format))
|
|
103
|
+
throw new UserError(`image format ${info.format ?? "unknown"} is not accepted (use JPEG, PNG, WebP, AVIF or HEIC)`);
|
|
104
|
+
const min = opts.minSize ?? 200;
|
|
105
|
+
if ((info.width ?? 0) < min || (info.height ?? 0) < min)
|
|
106
|
+
throw new UserError(`image is too small (${info.width}×${info.height}); use at least 200 px, and 600 px or more for a photo behind the words`);
|
|
107
|
+
const out = await sharp(input, { limitInputPixels: MAX_PIXELS, animated: false })
|
|
108
|
+
.rotate()
|
|
109
|
+
.resize({ width: 2160, height: 2700, fit: "inside", withoutEnlargement: true })
|
|
110
|
+
.flatten({ background: "#ffffff" })
|
|
111
|
+
.jpeg({ quality: 90, mozjpeg: true })
|
|
112
|
+
.toBuffer({ resolveWithObject: true });
|
|
113
|
+
const sha = createHash("sha256").update(out.data).digest("hex").slice(0, 16);
|
|
114
|
+
const ref = `${sha}.jpg`;
|
|
115
|
+
const dir = imagesDir(ws);
|
|
116
|
+
const file = join(dir, ref);
|
|
117
|
+
const full = { ref, width: out.info.width, height: out.info.height, ...meta, imported_at: new Date().toISOString() };
|
|
118
|
+
if (!existsSync(file))
|
|
119
|
+
writeAtomic(file, out.data);
|
|
120
|
+
const metaFile = join(dir, `${sha}.json`);
|
|
121
|
+
if (!existsSync(metaFile))
|
|
122
|
+
writeAtomic(metaFile, JSON.stringify(full, null, 2));
|
|
123
|
+
return readJson(metaFile, full);
|
|
124
|
+
}
|
|
125
|
+
export async function importImageFromUrl(ws, url, meta = {}, opts = {}) {
|
|
126
|
+
const buf = await download(url);
|
|
127
|
+
return storeImage(ws, buf, { source: meta.source ?? "url", license: meta.license ?? "unknown", url, ...meta }, opts);
|
|
128
|
+
}
|
|
129
|
+
/** A file the user has. Paths outside the workspace are fine to read (it is the user's machine), never to reference. */
|
|
130
|
+
export async function importImageFromFile(ws, path, meta = {}) {
|
|
131
|
+
if (!existsSync(path))
|
|
132
|
+
throw new UserError(`no file at ${path}`);
|
|
133
|
+
const st = statSync(path);
|
|
134
|
+
if (!st.isFile())
|
|
135
|
+
throw new UserError(`${path} is not a file`);
|
|
136
|
+
if (st.size > MAX_IMAGE_BYTES)
|
|
137
|
+
throw new UserError("image is larger than 25 MB");
|
|
138
|
+
if (/\.svgz?$/i.test(path))
|
|
139
|
+
throw new UserError("SVG images are not accepted");
|
|
140
|
+
return storeImage(ws, readFileSync(path), { source: meta.source ?? "local", license: meta.license ?? "own", ...meta });
|
|
141
|
+
}
|
|
142
|
+
export function listImages(ws) {
|
|
143
|
+
const dir = imagesDir(ws);
|
|
144
|
+
if (!existsSync(dir))
|
|
145
|
+
return [];
|
|
146
|
+
return readdirSync(dir)
|
|
147
|
+
.filter((f) => /^[0-9a-f]{16}\.json$/.test(f))
|
|
148
|
+
.map((f) => readJson(join(dir, f), null))
|
|
149
|
+
.filter((m) => !!m && existsSync(join(dir, m.ref)) && isInside(dir, join(dir, m.ref)));
|
|
150
|
+
}
|
|
151
|
+
export function imageMeta(ws, ref) {
|
|
152
|
+
const m = ref.match(/([0-9a-f]{16})\.jpg$/);
|
|
153
|
+
if (!m)
|
|
154
|
+
return null;
|
|
155
|
+
return readJson(join(imagesDir(ws), `${m[1]}.json`), null);
|
|
156
|
+
}
|
|
157
|
+
/** Licenses people can record with image_license: their own, bought or licensed, or an open license. */
|
|
158
|
+
export const LICENSES = ["own", "licensed", "cc0", "pdm", "by-4.0", "by-3.0", "by-2.0", "unsplash", "pexels", "unknown"];
|
|
159
|
+
/** Whether someone said the image may be posted: anything but unknown. */
|
|
160
|
+
export function licenseKnown(m) {
|
|
161
|
+
return !!m.license && m.license !== "unknown";
|
|
162
|
+
}
|
|
163
|
+
/** Records who may post an image and under which license; the image itself does not change. */
|
|
164
|
+
export function setImageLicense(ws, ref, license, extra = {}) {
|
|
165
|
+
const m = imageMeta(ws, ref);
|
|
166
|
+
if (!m)
|
|
167
|
+
throw new UserError(`no image ${ref}; image_list shows them`);
|
|
168
|
+
if (!LICENSES.includes(license))
|
|
169
|
+
throw new UserError(`license: one of ${LICENSES.join(", ")}`);
|
|
170
|
+
const next = { ...m, license, ...(extra.author ? { author: extra.author } : {}), ...(extra.note ? { note: extra.note } : {}) };
|
|
171
|
+
writeAtomic(join(imagesDir(ws), `${m.ref.replace(/\.jpg$/, "")}.json`), JSON.stringify(next, null, 2));
|
|
172
|
+
return next;
|
|
173
|
+
}
|
|
174
|
+
/** Licenses that need the author named somewhere: we put it in credits.txt and the caption suggestion. */
|
|
175
|
+
export function needsAttribution(m) {
|
|
176
|
+
return !!m && /^(by|cc-by|cc by)/i.test(m.license);
|
|
177
|
+
}
|
|
178
|
+
const OPENVERSE_PAGE = 20;
|
|
179
|
+
export async function searchImages(query, opts = {}) {
|
|
180
|
+
const limit = Math.min(Math.max(opts.limit ?? 12, 1), 30);
|
|
181
|
+
const provider = opts.provider ?? (process.env.UNSPLASH_ACCESS_KEY ? "unsplash" : process.env.PEXELS_API_KEY ? "pexels" : "openverse");
|
|
182
|
+
const q = encodeURIComponent(query.slice(0, 200));
|
|
183
|
+
if (provider === "unsplash") {
|
|
184
|
+
const key = process.env.UNSPLASH_ACCESS_KEY;
|
|
185
|
+
if (!key)
|
|
186
|
+
throw new UserError("set UNSPLASH_ACCESS_KEY (your own key from unsplash.com/developers) to search Unsplash");
|
|
187
|
+
const res = await fetch(`https://api.unsplash.com/search/photos?query=${q}&per_page=${limit}${opts.orientation ? `&orientation=${opts.orientation === "square" ? "squarish" : opts.orientation}` : ""}`, { headers: { authorization: `Client-ID ${key}`, "accept-version": "v1" }, signal: AbortSignal.timeout(15_000) });
|
|
188
|
+
if (!res.ok)
|
|
189
|
+
throw new UserError(`Unsplash: HTTP ${res.status}`);
|
|
190
|
+
const data = (await res.json());
|
|
191
|
+
return data.results.map((r) => ({ provider, id: r.id, url: r.urls.full, thumb: r.urls.small, width: r.width, height: r.height, author: r.user.name, license: "unsplash", landing_url: r.links.html, attribution: `Photo by ${r.user.name} on Unsplash`, download_location: r.links.download_location }));
|
|
192
|
+
}
|
|
193
|
+
if (provider === "pexels") {
|
|
194
|
+
const key = process.env.PEXELS_API_KEY;
|
|
195
|
+
if (!key)
|
|
196
|
+
throw new UserError("set PEXELS_API_KEY (your own key from pexels.com/api) to search Pexels");
|
|
197
|
+
const res = await fetch(`https://api.pexels.com/v1/search?query=${q}&per_page=${limit}${opts.orientation ? `&orientation=${opts.orientation}` : ""}`, { headers: { authorization: key }, signal: AbortSignal.timeout(15_000) });
|
|
198
|
+
if (!res.ok)
|
|
199
|
+
throw new UserError(`Pexels: HTTP ${res.status}`);
|
|
200
|
+
const data = (await res.json());
|
|
201
|
+
return data.photos.map((p) => ({ provider, id: String(p.id), url: p.src.original, thumb: p.src.medium, width: p.width, height: p.height, author: p.photographer, license: "pexels", landing_url: p.url, attribution: `Photo by ${p.photographer} on Pexels` }));
|
|
202
|
+
}
|
|
203
|
+
// Openverse needs no key; anonymous requests get at most 20 results a page (more is a 401). Only licenses that allow changes and commercial use: no ND, no NC, no SA.
|
|
204
|
+
const res = await fetch(`https://api.openverse.org/v1/images/?q=${q}&license=cc0,pdm,by&size=large&page_size=${Math.min(limit * 2, OPENVERSE_PAGE)}&mature=false${opts.orientation ? `&aspect_ratio=${opts.orientation === "portrait" ? "tall" : opts.orientation === "landscape" ? "wide" : "square"}` : ""}`, { headers: { "user-agent": "open-slideshow" }, signal: AbortSignal.timeout(15_000) });
|
|
205
|
+
if (!res.ok)
|
|
206
|
+
throw new UserError(`Openverse: HTTP ${res.status}`);
|
|
207
|
+
const data = (await res.json());
|
|
208
|
+
return data.results
|
|
209
|
+
.filter((r) => ["cc0", "pdm", "by"].includes(r.license) && (!r.width || !r.height || Math.min(r.width, r.height) >= 700))
|
|
210
|
+
.slice(0, limit)
|
|
211
|
+
.map((r) => ({ provider: "openverse", id: r.id, url: r.url, thumb: r.thumbnail, width: r.width, height: r.height, author: r.creator, license: r.license === "by" ? `by-${r.license_version ?? "4.0"}` : r.license, landing_url: r.foreign_landing_url, attribution: r.attribution }));
|
|
212
|
+
}
|
|
213
|
+
export async function importHit(ws, hit) {
|
|
214
|
+
if (hit.provider === "unsplash" && hit.download_location && process.env.UNSPLASH_ACCESS_KEY) {
|
|
215
|
+
// Unsplash's rules: count a download when a photo is used.
|
|
216
|
+
await fetch(hit.download_location, { headers: { authorization: `Client-ID ${process.env.UNSPLASH_ACCESS_KEY}` }, signal: AbortSignal.timeout(10_000) }).catch(() => undefined);
|
|
217
|
+
}
|
|
218
|
+
return importImageFromUrl(ws, hit.url, { source: hit.provider, license: hit.license, author: hit.author, landing_url: hit.landing_url, attribution: hit.attribution });
|
|
219
|
+
}
|
|
220
|
+
/** Thumbnails of the hits in one numbered picture, so the agent can look before it picks. */
|
|
221
|
+
export async function hitsSheet(hits) {
|
|
222
|
+
const thumbs = await Promise.all(hits.map(async (h) => {
|
|
223
|
+
try {
|
|
224
|
+
// Openverse's own thumbnails fail now and then (HTTP 424): fall back to the image itself.
|
|
225
|
+
const buf = await download(h.thumb).catch(() => download(h.url));
|
|
226
|
+
return await sharp(buf, { limitInputPixels: MAX_PIXELS }).rotate().resize(300, 375).jpeg().toBuffer();
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
}));
|
|
232
|
+
const ok = [];
|
|
233
|
+
thumbs.forEach((t, i) => {
|
|
234
|
+
if (t)
|
|
235
|
+
ok.push({ t, i });
|
|
236
|
+
});
|
|
237
|
+
if (!ok.length)
|
|
238
|
+
return null;
|
|
239
|
+
const { contactSheet } = await import("./render.js");
|
|
240
|
+
return contactSheet(ok.map((x) => x.t), "4:5", Math.min(ok.length, 6), ok.map((x) => `${x.i + 1}. ${hits[x.i].license}`));
|
|
241
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export * from "./schema.js";
|
|
2
|
+
export { THEMES, getTheme } from "./themes.js";
|
|
3
|
+
export { openWorkspace, initWorkspace, loadDeck, saveDeck, listDecks } from "./workspace.js";
|
|
4
|
+
export { brandContext } from "./brand.js";
|
|
5
|
+
export { newDeck, updateDeck, renderDeckById } from "./decks.js";
|
|
6
|
+
export { lintDeck } from "./lint.js";
|
|
7
|
+
export { themeGallery, templateGallery } from "./gallery.js";
|
|
8
|
+
export { TEMPLATES, getTemplate, followsTemplate } from "./templates.js";
|
|
9
|
+
export { publishDeck } from "./publish.js";
|
|
10
|
+
export { planSlots, dueSlots } from "./schedule.js";
|
|
11
|
+
export { createServer } from "./mcp.js";
|
|
12
|
+
export { SKILL } from "./skill.js";
|
|
13
|
+
export { syncResults, recordResult, insights, hookTraits, experimentVerdict } from "./results.js";
|
|
14
|
+
export { hookTest, seriesNext, listSeries, translateDeck, repostDeck } from "./variants.js";
|
|
15
|
+
export { collectIdeas, readIdeas, addIdeas, updateIdea } from "./ideas.js";
|
|
16
|
+
export { brandKit } from "./brandkit.js";
|
|
17
|
+
export { appScreenshots } from "./screenshots.js";
|
|
18
|
+
export { renderVideo, videoArgs } from "./video.js";
|
|
19
|
+
export { previewDeck } from "./preview.js";
|
|
20
|
+
export { tiktokZones } from "./readability.js";
|
|
21
|
+
export { setImageLicense, LICENSES } from "./images.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// The library Appeeky Spec (and anyone) can use: open-slideshow does not depend on Spec.
|
|
2
|
+
export * from "./schema.js";
|
|
3
|
+
export { THEMES, getTheme } from "./themes.js";
|
|
4
|
+
export { openWorkspace, initWorkspace, loadDeck, saveDeck, listDecks } from "./workspace.js";
|
|
5
|
+
export { brandContext } from "./brand.js";
|
|
6
|
+
export { newDeck, updateDeck, renderDeckById } from "./decks.js";
|
|
7
|
+
export { lintDeck } from "./lint.js";
|
|
8
|
+
export { themeGallery, templateGallery } from "./gallery.js";
|
|
9
|
+
export { TEMPLATES, getTemplate, followsTemplate } from "./templates.js";
|
|
10
|
+
export { publishDeck } from "./publish.js";
|
|
11
|
+
export { planSlots, dueSlots } from "./schedule.js";
|
|
12
|
+
export { createServer } from "./mcp.js";
|
|
13
|
+
export { SKILL } from "./skill.js";
|
|
14
|
+
export { syncResults, recordResult, insights, hookTraits, experimentVerdict } from "./results.js";
|
|
15
|
+
export { hookTest, seriesNext, listSeries, translateDeck, repostDeck } from "./variants.js";
|
|
16
|
+
export { collectIdeas, readIdeas, addIdeas, updateIdea } from "./ideas.js";
|
|
17
|
+
export { brandKit } from "./brandkit.js";
|
|
18
|
+
export { appScreenshots } from "./screenshots.js";
|
|
19
|
+
export { renderVideo, videoArgs } from "./video.js";
|
|
20
|
+
export { previewDeck } from "./preview.js";
|
|
21
|
+
export { tiktokZones } from "./readability.js";
|
|
22
|
+
export { setImageLicense, LICENSES } from "./images.js";
|
package/dist/lint.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type RenderContext } from "./render.js";
|
|
2
|
+
import { type Workspace } from "./workspace.js";
|
|
3
|
+
import type { Deck } from "./schema.js";
|
|
4
|
+
export declare const LIMITS: {
|
|
5
|
+
tiktok: {
|
|
6
|
+
photos: number;
|
|
7
|
+
title: number;
|
|
8
|
+
caption: number;
|
|
9
|
+
hashtags: number;
|
|
10
|
+
};
|
|
11
|
+
instagram: {
|
|
12
|
+
photos: number;
|
|
13
|
+
caption: number;
|
|
14
|
+
hashtags: number;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export interface LintIssue {
|
|
18
|
+
level: "error" | "warning";
|
|
19
|
+
code: string;
|
|
20
|
+
message: string;
|
|
21
|
+
slide?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface LintResult {
|
|
24
|
+
ok: boolean;
|
|
25
|
+
errors: LintIssue[];
|
|
26
|
+
warnings: LintIssue[];
|
|
27
|
+
}
|
|
28
|
+
export declare function platformsOf(deck: Deck): Set<"tiktok" | "instagram">;
|
|
29
|
+
export declare function captionText(deck: Deck): string;
|
|
30
|
+
export declare function renderContext(ws: Workspace, deck: Deck): RenderContext;
|
|
31
|
+
export interface LintOptions {
|
|
32
|
+
platforms?: ("tiktok" | "instagram")[];
|
|
33
|
+
/** Checks before posting: an image whose license is unknown becomes an error. */
|
|
34
|
+
publishing?: boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare function lintDeck(ws: Workspace, deck: Deck, opts?: LintOptions): Promise<LintResult>;
|