Repository navigation
Expand file tree
/
Copy pathbuild_outline.py
More file actions
168 lines (146 loc) · 5.84 KB
/
Copy pathbuild_outline.py
File metadata and controls
168 lines (146 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
"""
Rebuild outline.md from the actual chapters.
Reads each chapter, calls the LLM for a structured summary,
and assembles into an outline that reflects the novel as-written.
"""
import os
import sys
import json
import re
from pathlib import Path
from dotenv import load_dotenv
BASE_DIR = Path(__file__).parent
load_dotenv(BASE_DIR / ".env")
JUDGE_MODEL = os.environ.get("AUTONOVEL_JUDGE_MODEL", "claude-sonnet-4-6")
API_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
API_BASE = os.environ.get("AUTONOVEL_API_BASE_URL", "https://api.anthropic.com")
CHAPTERS_DIR = BASE_DIR / "chapters"
def call_model(prompt, max_tokens=1500):
import httpx
headers = {
"x-api-key": API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
}
payload = {
"model": JUDGE_MODEL,
"max_tokens": max_tokens,
"temperature": 0.1,
"system": (
"You produce structured outline entries for novel chapters. "
"Be precise about what HAPPENS, what CHANGES, and what threads are planted/harvested. "
"Output valid JSON only."
),
"messages": [{"role": "user", "content": prompt}],
}
resp = httpx.post(f"{API_BASE}/v1/messages", headers=headers, json=payload, timeout=120)
resp.raise_for_status()
text = resp.json()["content"][0]["text"]
# Extract JSON from response
text = text.strip()
if text.startswith("```"):
text = re.sub(r'^```\w*\n?', '', text)
text = re.sub(r'\n?```$', '', text)
return json.loads(text)
def main():
# Load supporting docs for context
characters = (BASE_DIR / "characters.md").read_text()[:3000]
entries = []
for ch in range(1, 20):
path = CHAPTERS_DIR / f"ch_{ch:02d}.md"
text = path.read_text()
wc = len(text.split())
title_line = text.strip().split('\n')[0].lstrip('# ').strip()
prompt = f"""Analyze this chapter and produce a structured outline entry.
CHAPTER {ch}: "{title_line}" ({wc} words)
{text}
Return JSON with these fields:
- "title": the chapter title (string)
- "location": primary setting (string)
- "characters": list of characters who appear (list of strings)
- "summary": 2-3 sentence summary of what happens (string)
- "beats": list of 3-5 key story beats in order (list of strings)
- "try_fail": the try-fail cycle type: "yes-but", "no-and", "yes-and", or "no-but" (string)
- "plants": foreshadowing threads PLANTED in this chapter (list of strings)
- "harvests": foreshadowing threads PAID OFF in this chapter (list of strings)
- "emotional_arc": one sentence describing the emotional movement (string)
- "chapter_question": the question left open at chapter's end (string)
JSON only, no other text."""
data = call_model(prompt)
data["num"] = ch
data["words"] = wc
entries.append(data)
print(f" {ch:2d}. {title_line} ({wc}w)")
# Load existing outline header info
old_outline = (BASE_DIR / "outline.md").read_text()
# Build new outline
lines = []
lines.append("# THE SECOND SON OF THE HOUSE OF BELLS")
lines.append("## Chapter Outline (reflects actual novel as-written)")
lines.append("")
lines.append(f"**23 chapters, {sum(e['words'] for e in entries):,} words**")
lines.append("")
lines.append("---")
lines.append("")
for e in entries:
lines.append(f"### Ch {e['num']}: {e['title']}")
lines.append(f"**{e['words']} words** | **Location:** {e.get('location', 'N/A')}")
lines.append(f"- **Characters:** {', '.join(e.get('characters', []))}")
lines.append(f"- **Try-fail cycle:** {e.get('try_fail', 'N/A')}")
lines.append(f"- **Emotional arc:** {e.get('emotional_arc', 'N/A')}")
lines.append("")
lines.append(f"**Summary:** {e.get('summary', 'N/A')}")
lines.append("")
lines.append("**Beats:**")
for b in e.get("beats", []):
lines.append(f"1. {b}")
lines.append("")
if e.get("plants"):
lines.append("**Plants:**")
for p in e["plants"]:
lines.append(f"- {p}")
lines.append("")
if e.get("harvests"):
lines.append("**Harvests:**")
for h in e["harvests"]:
lines.append(f"- {h}")
lines.append("")
lines.append(f"**Chapter question:** {e.get('chapter_question', 'N/A')}")
lines.append("")
lines.append("---")
lines.append("")
# Foreshadowing ledger
lines.append("## FORESHADOWING LEDGER")
lines.append("")
lines.append("| Thread | Planted | Harvested |")
lines.append("|--------|---------|-----------|")
# Collect all plants and harvests
all_plants = {}
all_harvests = {}
for e in entries:
for p in e.get("plants", []):
key = p[:60]
if key not in all_plants:
all_plants[key] = []
all_plants[key].append(e["num"])
for h in e.get("harvests", []):
key = h[:60]
if key not in all_harvests:
all_harvests[key] = []
all_harvests[key].append(e["num"])
# Match plants to harvests by keyword overlap
all_threads = set(list(all_plants.keys()) + list(all_harvests.keys()))
for thread in sorted(all_threads):
planted = ", ".join(f"Ch {n}" for n in all_plants.get(thread, []))
harvested = ", ".join(f"Ch {n}" for n in all_harvests.get(thread, []))
lines.append(f"| {thread} | {planted} | {harvested} |")
lines.append("")
lines.append("---")
lines.append("")
lines.append("*Outline rebuilt from actual chapters, Cycle 5.*")
out = '\n'.join(lines)
(BASE_DIR / "outline.md").write_text(out)
print(f"\nSaved outline.md ({len(out.split())} words)")
if __name__ == "__main__":
main()