// TASK social — the promotion text for X and Bluesky (release 29 G2): // `publish.x` and `publish.bluesky`, each `{ post, thread? }`. TEXT ONLY: // nothing posts it. A STEP on a done iteration, on the media task scaffold // (./media.ts): one unit per platform. // // The model writes a first post and up to SOCIAL_LIMITS.thread short posts // under it. CODE does the rest: // - the LINK is code's: appended under the first post (social.mjs // `withLink`): the video's public URL when the manifest has one // (`publish.videoUrl`), else the article's page on the homepage when the // article is published there and public; else none (said in the log). The // model never writes a URL; // - the COUNTS are code's (social.mjs): X 280 with every URL weighed as 23 // and an emoji as 2; Bluesky 300 graphemes. The first post is counted // with its link; // - grounded (./grounding.ts via ./clipEvidence.ts checkLine): every name // and number is the evidence's (the report and the cut's title, as the // YouTube text's); no stock phrase; third person (Q1: a first-person word // outside quotation marks is refused); words in quotation marks only when // a cited quote says them word for word; // - no links, hashtags, @mentions, timestamps or emoji in what the model // writes. // A post past its count, or a thread post failing a check, is repaired by // dropping sentences or thread posts (never rewriting); the better of two // attempts is kept. import { PROJECT_URL } from "../../../project"; import { readArticleState } from "../../registry"; import { ARTICLES_REPORTS_BASE } from "../../routes"; import { SOCIAL_LIMITS, SOCIAL_NAMES, hasLink, socialLength, socialLinkOf, withLink, type SocialPlatform, type SocialPost } from "../../social.mjs"; import { reportPagePath } from "../../views"; import { sentencesOf, type GroundingInput } from "../grounding"; import { checkLine } from "./clipEvidence"; import { runMediaTask, type MediaAttempt, type MediaContext, type MediaTaskInput, type MediaTaskSpec, type MediaUnit } from "./media"; import type { TaskDeps, TaskResult } from "./types"; import { evidenceOf } from "./youtube"; export type SocialInput = MediaTaskInput; /** What the model writes (no link): the first post and the thread under it. */ export type SocialValue = { post: string; thread: string[] }; type SocialUnit = MediaUnit & { platform: SocialPlatform; link: string | null; g: GroundingInput; evidence: string }; const oneLine = (s: unknown) => String(s ?? "").replace(/\s+/g, " ").trim(); // What the model may not write: a link (a scheme, www. or a bare domain X // would link: social.mjs hasLink), a timestamp, a hashtag, an @mention. const NOT_WRITTEN = /\b\d{1,2}:\d{2}\b|(^|\s)#\w|(^|\s)@\w/; const notWritten = (text: string) => hasLink(text) || NOT_WRITTEN.test(text); const EMOJI = /\p{Extended_Pictographic}/u; /** Room the link takes under the first post (its blank line included). */ export function linkRoom(platform: SocialPlatform, link: string | null): number { return link ? socialLength(platform, withLink("", link)) : 0; } /** The first post's limit before its link. */ export const postBudget = (platform: SocialPlatform, link: string | null) => SOCIAL_LIMITS[platform] - linkRoom(platform, link); const unitLabel = (p: SocialPlatform) => `${SOCIAL_NAMES[p]} post`; /** One post's problems (`first`: counted with the link under it). */ export function postProblems(text: string, u: Pick, where: string, first: boolean): { hard: string[]; soft: string[] } { const hard: string[] = []; if (!text) hard.push(`${where}: empty`); const limit = SOCIAL_LIMITS[u.platform]; const n = socialLength(u.platform, first ? withLink(text, u.link) : text); if (n > limit) hard.push(`${where} is ${n} ${u.platform === "bluesky" ? "graphemes" : "characters as X counts them"}${first && u.link ? " with its link" : ""} (at most ${limit})`); if (notWritten(text)) hard.push(`${where}: no links, timestamps, hashtags or @mentions — the link is added after it`); if (EMOJI.test(text)) hard.push(`${where}: no emoji`); const r = text ? checkLine(text, u.g, { quotes: true }) : { hard: [], soft: [] }; return { hard: [...hard, ...r.hard.map((h) => `${where}: ${h}`)], soft: r.soft.map((s) => `${where}: ${s}`) }; } export function socialSystem(platform: SocialPlatform, budget: number, hasLink: boolean): string { const name = SOCIAL_NAMES[platform]; const counted = platform === "x" ? "characters" : "characters (emoji and accented letters count as one)"; return ( `You write a post for ${name} that promotes a short evidence video made from a cited report, and optionally a short ` + `thread under it. Rules: (1) the first post is at most ${budget} ${counted}${hasLink ? "; a link to the video is added under it after you write it" : ""}; ` + `(2) the thread is 0 to ${SOCIAL_LIMITS.thread} more posts, each at most ${SOCIAL_LIMITS[platform]} characters, each adding a finding the ` + "first post does not; (3) plain and specific, no clickbait, no all-caps; (4) third person: you describe what the speaker " + "said, never as the speaker (no I, me, my, we, us or our outside quotation marks); (5) every name and number comes from " + "the evidence, and nothing goes beyond it; (6) words in quotation marks only when a quote below says them word for word; " + "(7) no links, hashtags, @mentions, timestamps or emoji." ); } /** The article's page on the homepage, when the article is published there and public; else null. */ async function articleUrlOf(ctx: MediaContext): Promise { const state = await readArticleState(ctx.input.article, ctx.handle.paths).catch(() => null); if (!state?.exists || !state.published || state.audience !== "public") return null; return `${PROJECT_URL.replace(/\/+$/, "")}${reportPagePath(ctx.input.article, ARTICLES_REPORTS_BASE)}`; } // The first post cut to its budget at a sentence, its failing sentences // dropped; the thread's failing posts dropped and the rest kept to the limit. function trimmed(v: SocialValue, u: SocialUnit): SocialValue | null { const kept: string[] = []; for (const s of sentencesOf(v.post)) { if (postProblems(s, u, "the post", false).hard.length) continue; if (postProblems([...kept, s].join(" "), u, "the post", true).hard.length) break; kept.push(s); } const post = kept.join(" "); if (!post) return null; const thread = v.thread.filter((t) => !postProblems(t, u, "a thread post", false).hard.length).slice(0, SOCIAL_LIMITS.thread); return { post, thread }; } const valueProblems = (v: SocialValue, u: SocialUnit) => { const first = postProblems(v.post, u, "the post", true); const hard = [...first.hard]; const soft = [...first.soft]; if (v.thread.length > SOCIAL_LIMITS.thread) hard.push(`the thread has ${v.thread.length} posts (at most ${SOCIAL_LIMITS.thread})`); v.thread.forEach((t, i) => { const r = postProblems(t, u, `thread post ${i + 1}`, false); hard.push(...r.hard); soft.push(...r.soft); }); return { hard, soft }; }; export const SOCIAL_SPEC: MediaTaskSpec = { task: "social", noun: "social post", async units(ctx) { const { text, g } = evidenceOf(ctx); const link = socialLinkOf(ctx.manifest, { articleUrl: await articleUrlOf(ctx) }); // Not a check (the model cannot fix it): said once in the log. ctx.handle.log(link ? `the posts link ${link}` : `no link: the manifest has no publish.videoUrl and "${ctx.input.article}" is not a public article on the homepage`); return (["x", "bluesky"] as const).map((platform) => ({ id: platform, target: { field: `publish.${platform}` }, label: unitLabel(platform), platform, link, g, evidence: text, })); }, schema: () => ({ type: "object", properties: { post: { type: "string" }, thread: { type: "array", items: { type: "string" } } }, required: ["post", "thread"], }), system: (u) => socialSystem(u.platform, postBudget(u.platform, u.link), !!u.link), user: (u) => `${u.evidence}\n\nWrite the ${SOCIAL_NAMES[u.platform]} post and its thread.`, parse(answer) { const a = answer as { post?: unknown; thread?: unknown } | null; // A post the model wrapped whole in quotation marks loses them; a quote // inside the post (or one that opens or ends it) is the speaker's, kept. const clean = (s: unknown) => { const t = oneLine(s); const m = /^["“]([^"“”]*)["”]$/.exec(t); return m ? m[1]!.trim() : t; }; return { post: clean(a?.post), thread: (Array.isArray(a?.thread) ? a.thread : []).map(clean).filter(Boolean), }; }, check: (v, u) => valueProblems(v, u), repair(attempts, u) { let best: MediaAttempt | null = null; for (const a of attempts) { if (!a.value) continue; const v = trimmed(a.value, u); if (!v) continue; const again = valueProblems(v, u); if (again.hard.length) continue; if (!best || v.post.length + v.thread.join("").length > best.value!.post.length + best.value!.thread.join("").length) { best = { value: v, hard: [], soft: [...again.soft, "trimmed: failing or overlong sentences and thread posts removed"] }; } } return best; }, // The field: the first post with its link under it, and the thread when there is one. toField: (v, u): SocialPost => ({ post: withLink(v.post, u.link), ...(v.thread.length ? { thread: v.thread } : {}) }), describe: (v, u) => `${SOCIAL_NAMES[u.platform]}: ${v.post.slice(0, 100)}${v.post.length > 100 ? "…" : ""}${v.thread.length ? ` (+${v.thread.length} in the thread)` : ""}`, }; export async function social(input: SocialInput, deps: TaskDeps): Promise { return runMediaTask(SOCIAL_SPEC, input, deps); }