Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,26 @@ LLM の選定の指示にも、同じ題材を選ばないことを入れてい
- 決まった構成は、「何が新しいか → なぜ注目か → 仕組み → 具体的な使いどころ・例 → ほかとの違い → 注意点・限界」で、最後に任意でまとめの節を足します。台本の節の種類 (`role`) とは、`hook`・`overview`・`detail`・`usecase`・`detail`・`caveat` (・`summary`) の順に対応します (`src/script/steps.ts`)。節の型がそろわない構成と、構成と節の並びが合わない台本は、作り直します。
- 事実は材料にあるものだけを使い、材料から言い切れないことは推測と分かる言い方にします。見直しの段階で、材料にない数字・固有名詞を指摘させて直します。プログラムも、材料にない数字の疑い (2 桁以上の数と小数) を見つけて見直しに渡し、最終の台本に残った疑いを `script.check.json` の `ungroundedNumbers` に残します。
- 見直しの出力は、台本の検査 (写し・必須の節・侮辱の語・Zod) を通します。通らなければ作り直し、それでも通らなければ (または LLM を呼べなければ)、検査を通った下書きを使います (`script.review.json` の `applied: false`)。
- 材料は、GitHub の題材では README に加えて、最新のリリースの注記 (あれば)、主な言語、最上位のディレクトリ・ファイルの名前、ライセンスです (`topic.json` の `materials` と `license`)。足切りで取った README は使い回し、本文の取得は 4〜5 回の API 呼び出しです。記事の本文 (`materials.articleBody`) は、取れるようになったら差し込めるように、型だけ用意しています。
- 材料は、GitHub の題材では README に加えて、最新のリリースの注記 (あれば)、主な言語、最上位のディレクトリ・ファイルの名前、ライセンスです (`topic.json` の `materials` と `license`)。足切りで取った README は使い回し、本文の取得は 4〜5 回の API 呼び出しです。記事の本文 (`materials.articleBody`) は、下の「記事の本文の取り方」のとおり、GitHub Blog と Qiita の題材だけ、台本の段階で取って差し込みます。
- `script.check.json` の `stageAttempts` に、段階ごとに LLM を呼んだ回数が残ります。

### 記事の本文の取り方

選んだ題材が GitHub Blog か Qiita の記事のときだけ、台本の段階 (dry-run 以外) で本文を取り、台本の材料 (`article`) に入れます。Zenn・はてなブックマーク・Hacker News のリンク先は取りません。

| ソース | 取り方 |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| GitHub Blog | フィード (`config/feeds.json` の `fullText.urlPattern` に合う URL) の `content:encoded` にある全文を使う。Changelog は対象外 |
| Qiita | 公式の API v2 (`GET /api/v2/items/<id>`) の `body` (Markdown) を使う。記事のページの HTML は取らない (公式ヘルプがスクレイピングを禁じている) |

- 取った HTML・Markdown から、本文の文字だけを残します。コードの塊は残し、画像・スクリプト・装飾は除きます。
- 長さの上限は `config/videoforge.json` の `articleBody.maxChars` (既定 12,000 文字、最大 30,000) です。待ちの上限は `articleBody.timeoutMs` です。
- 取れなかったとき (HTTP のエラー・全文がない・上限を超えた) は、警告を出し、今までどおり要約 (`excerpt`) で台本を作ります。
- 本文は `topic.json` には書きません。台本を作る間だけ実行のディレクトリに `article-body.txt` として置き、段階の終わりに消します。archive の許可の一覧には入れません。ログと `script.check.json` には、取れたかどうかと文字数だけを出します。
- 写しの検査 (`checkCopy`) は、説明・抜粋・リリースの注記に加えて、この本文にも当てます。本文と 30 字以上同じ文字列が続くセリフは、作り直しの対象です。
- 取るときの HTTP には、`videoforge/<版> (+https://github.com/HyuuNyackman/videoforge)` の User-Agent を付けます。
- 概要欄には、これまでどおり出典を載せ、本文をそのまま読み上げません。

### 台本の書き方の制限

台本の指示に、製品・企業・作者・利用者を貶めない、断定的に否定しない、根拠のない比較や噂を書かない、特定の思想・団体への賛否を書かない、ことを入れています。作った台本に `config/script-policy.json` の `insultTerms` (強い否定・侮辱の語) があれば、既存の作り直し (1 回) に回し、見つけた語を次の指示に載せます。
Expand Down
3 changes: 2 additions & 1 deletion config/feeds.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"id": "github-blog",
"name": "GitHub Blog",
"url": "https://github.blog/feed/",
"kind": "news"
"kind": "news",
"fullText": { "urlPattern": "^https://github\\.blog/(?!changelog/)" }
},
{
"id": "github-changelog",
Expand Down
1 change: 1 addition & 0 deletions config/videoforge.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"maxCallsPerModel": 8,
"maxTotalMs": 1500000
},
"articleBody": { "maxChars": 12000, "timeoutMs": 15000 },
"youtube": { "containsSyntheticMedia": false },
"speech": {
"engine": "voicevox",
Expand Down
12 changes: 12 additions & 0 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class ConfigError extends Error {
override name = "ConfigError";
}

/** `articleBody.maxChars` に書ける最大の値 (台本の材料に入れるときの上限も、これ) */
export const ARTICLE_BODY_MAX_CHARS_LIMIT = 30_000;

const SourceId = z.enum(["github-search", "rss", "hn"]);

// 未知のキーはエラーにする (綴りの間違いを黙って無視しない)
Expand Down Expand Up @@ -134,6 +137,15 @@ export const Config = z.strictObject({
path: ["shortsTargetMs"],
})
.prefault({}),
/** 記事の本文 (GitHub Blog の RSS の全文・Qiita の API) を台本の材料に使うときの設定 */
articleBody: z
.strictObject({
/** 本文の長さの上限 (文字数)。超えたら本文は使わず、要約で続ける */
maxChars: z.number().int().positive().max(ARTICLE_BODY_MAX_CHARS_LIMIT).default(12_000),
/** 本文を取る HTTP の呼び出し 1 回の待ちの上限 (ミリ秒) */
timeoutMs: z.number().int().positive().default(15_000),
})
.prefault({}),
/** 描画のテーマの id (描画の側が持つテーマから選ぶ) */
theme: z.string().min(1).default("dark"),
/** BGM。既定は無し */
Expand Down
2 changes: 1 addition & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const Materials = z.object({
languages: z.array(z.object({ name: z.string(), percent: z.number() })).optional(),
/** リポジトリの最上位の項目の名前 (ディレクトリは末尾に `/`) */
topLevelEntries: z.array(z.string()).optional(),
/** 記事の本文。記事のソースが取れるようになったら入れる (今は使わない) */
/** 記事の本文 (GitHub Blog・Qiita)。台本の段階が取って材料に入れるだけで、`topic.json` には書かない */
articleBody: z.string().optional(),
});
export type Materials = z.infer<typeof Materials>;
Expand Down
11 changes: 11 additions & 0 deletions src/ops/archive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { main } from "../cli/main.js";
import { Config } from "../core/config.js";
import { ARTICLE_BODY_FILE } from "../script/stage.js";
import {
ARCHIVE_ALLOWLIST,
ArchiveManifest,
Expand Down Expand Up @@ -102,6 +103,16 @@ describe("許可の一覧", () => {
});
});

describe("記事の本文", () => {
it("許可の一覧になく、実行のディレクトリにあっても archive に入らない", async () => {
expect(ARCHIVE_ALLOWLIST).not.toContain(ARTICLE_BODY_FILE);
await put(ARTICLE_BODY_FILE, "記事の本文");
const out = join(root, "out-body");
await stageArchive({ runDir, outDir: out });
expect(await tree(out)).not.toContain(ARTICLE_BODY_FILE);
});
});

describe("manifest", () => {
it("SHA・モデル名・版・設定・大きさが入る", async () => {
const config = Config.parse({ sources: ["rss"] });
Expand Down
17 changes: 17 additions & 0 deletions src/script/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,20 @@ describe("checkInsults", () => {
expect(checkScript(script, topic).ok).toBe(true);
});
});

describe("記事の本文の写しの検査", () => {
const body =
"この記事では、新しい仕組みによって、ビルドの時間が半分になったことを、順を追って説明しています。";
const articleTopic = { ...topic, materials: { articleBody: body } };

it("台本が本文の表現をそのまま写していると見つける", () => {
const script = withLine(body);
const { problems } = checkScript(script, articleTopic);
expect(problems.some((p) => p.code === "copy-run" && p.lineId === "s2-l1")).toBe(true);
});

it("本文がなければ、同じセリフでも写しとは見ない", () => {
const { problems } = checkScript(withLine(body), topic);
expect(problems.filter((p) => p.code === "copy-run")).toEqual([]);
});
});
10 changes: 6 additions & 4 deletions src/script/materials.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ARTICLE_BODY_MAX_CHARS_LIMIT } from "../core/config.js";
import type { Topic } from "../core/types.js";

/** 材料の種類。事実の一覧の `source` に使う (どの材料から来た事実か) */
Expand All @@ -23,13 +24,13 @@ export interface MaterialSection {
/** 1 つの材料をプロンプトに入れる長さの上限 (文字数) */
export const MATERIAL_MAX_CHARS = 6000;

const clip = (text: string): string => [...text].slice(0, MATERIAL_MAX_CHARS).join("");
const clip = (text: string, max = MATERIAL_MAX_CHARS): string => [...text].slice(0, max).join("");

/** 題材 (`topic.json`) から、台本の材料を種類ごとに取り出す。ないものは出さない。 */
export function materialSections(topic: Topic): MaterialSection[] {
const sections: MaterialSection[] = [];
const add = (id: MaterialId, label: string, text: string | undefined) => {
if (text && text.trim()) sections.push({ id, label, text: clip(text.trim()) });
const add = (id: MaterialId, label: string, text: string | undefined, max?: number) => {
if (text && text.trim()) sections.push({ id, label, text: clip(text.trim(), max) });
};
const meta = [
`題名: ${topic.title}`,
Expand Down Expand Up @@ -67,7 +68,8 @@ export function materialSections(topic: Topic): MaterialSection[] {
if (entries && entries.length > 0)
add("structure", "最上位のディレクトリ・ファイル", entries.join("\n"));
add("license", "ライセンス", topic.license);
add("article", "記事の本文", topic.materials?.articleBody);
// 記事の本文の長さは、取るときに設定の上限で絞っている
add("article", "記事の本文", topic.materials?.articleBody, ARTICLE_BODY_MAX_CHARS_LIMIT);
return sections;
}

Expand Down
82 changes: 79 additions & 3 deletions src/script/stage.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { mkdtemp, readFile, writeFile } from "node:fs/promises";
import { access, mkdtemp, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { parseConfig } from "../core/config.js";
import { readManifest } from "../core/manifest.js";
import { runStages } from "../core/runner.js";
import { PronunciationReport } from "../speech/dictionary/index.js";
import { Script } from "../core/script-schema.js";
import { createScriptStage } from "./stage.js";
import { ARTICLE_BODY_FILE, createScriptStage } from "./stage.js";
import {
happyReplies,
stagedClient,
Expand Down Expand Up @@ -137,4 +137,80 @@ describe("createScriptStage", () => {
}),
).rejects.toThrow(/GEMINI_API_KEY/);
});

describe("記事の本文", () => {
const SECRET = "本文だけに出てくる、ふしぎな固有の言い回し";
const qiita = { ...topic, url: "https://qiita.com/u/items/0123abcd", sourceId: "rss" };

async function runWith(articleFetch: typeof fetch) {
const { runDir, dictionaryPath, config } = await setup();
await writeFile(join(runDir, "topic.json"), JSON.stringify(qiita));
const client = stagedClient("gemini", happyReplies(topic.key));
const logs: string[] = [];
const stage = createScriptStage({
env: {},
dictionaryPath,
clients: [client],
articleFetch,
});
await runStages({
stages: [stage],
to: "script",
ctx: { runId: "r", runDir, config, dryRun: false, log: (m) => logs.push(m) },
});
return { runDir, client, logs };
}

it("Qiita の題材では本文を取って台本の材料に入れ、本文のファイルは段階の終わりに消す", async () => {
const api = vi.fn(async () => new Response(JSON.stringify({ body: SECRET })));
const { runDir, client, logs } = await runWith(api as unknown as typeof fetch);
expect(api).toHaveBeenCalledTimes(1);
expect(client.seen[0]!.prompt).toContain(SECRET);
// 本文は topic.json にも、ログにも、台本の検査の記録にも入らない。長さと成否だけ
expect(await readFile(join(runDir, "topic.json"), "utf8")).not.toContain(SECRET);
expect(logs.join("\n")).not.toContain(SECRET);
expect(logs.join("\n")).toContain(`記事の本文を取りました (${[...SECRET].length} 文字)`);
const check = await readFile(join(runDir, "script.check.json"), "utf8");
expect(check).not.toContain(SECRET);
expect(JSON.parse(check).articleBody).toEqual({ fetched: true, chars: [...SECRET].length });
await expect(access(join(runDir, ARTICLE_BODY_FILE))).rejects.toThrow();
});

it("取れなかったときは警告を出し、要約のままで続ける", async () => {
const api = vi.fn(async () => new Response("", { status: 500 }));
const { runDir, client, logs } = await runWith(api as unknown as typeof fetch);
expect(
logs.some((m) => m.includes("警告: 記事の本文を取れなかった") && m.includes("HTTP 500")),
).toBe(true);
expect(client.seen[0]!.prompt).not.toContain("[article]");
expect(
JSON.parse(await readFile(join(runDir, "script.check.json"), "utf8")).articleBody,
).toEqual({
fetched: false,
});
expect(
Script.parse(JSON.parse(await readFile(join(runDir, "script.json"), "utf8"))),
).toBeTruthy();
});

it("上限を超えた本文も、警告を出して要約で続ける", async () => {
const api = async () => new Response(JSON.stringify({ body: "あ".repeat(13_000) }));
const { client, logs } = await runWith(api as unknown as typeof fetch);
expect(logs.some((m) => m.includes("本文が長すぎます"))).toBe(true);
expect(client.seen[0]!.prompt).not.toContain("[article]");
});

it("dry-run では本文を取りに行かない", async () => {
const { runDir, dictionaryPath, config } = await setup();
await writeFile(join(runDir, "topic.json"), JSON.stringify(qiita));
const api = vi.fn();
const stage = createScriptStage({ env: {}, dictionaryPath, articleFetch: api as never });
await runStages({
stages: [stage],
to: "script",
ctx: { runId: "r", runDir, config, dryRun: true, log: () => {} },
});
expect(api).not.toHaveBeenCalled();
});
});
});
Loading