Blueprint v1.0.4: Implemented AI Context Optimization & Token Management

- core/utils.py: Added estimate_tokens(), truncate_to_tokens(), get_ai_cache(), set_ai_cache(), make_cache_key() utilities
- story/writer.py: Applied truncate_to_tokens() to prev_content (2000 tokens) and prev_sum (600 tokens) context injections
- story/editor.py: Applied truncate_to_tokens() to summary (1000t), last_chapter_text (800t), eval text (7500t), propagation contexts (2500t/3000t)
- web/routes/persona.py: Added MD5-keyed in-memory cache for persona analyze endpoint; truncated sample_text to 750 tokens
- ai/models.py: Added pre-dispatch payload size estimation with 30k-token warning threshold

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-20 23:30:39 -05:00
parent f04a241936
commit db70ad81f7
6 changed files with 79 additions and 9 deletions

View File

@@ -59,7 +59,7 @@ def evaluate_chapter_quality(text, chapter_title, genre, model, folder):
}}
"""
try:
response = model.generate_content([prompt, text[:30000]])
response = model.generate_content([prompt, utils.truncate_to_tokens(text, 7500)])
model_name = getattr(model, 'name', ai_models.logic_model_name)
utils.log_usage(folder, model_name, response.usage_metadata)
data = json.loads(utils.clean_json(response.text))
@@ -86,8 +86,8 @@ def check_pacing(bp, summary, last_chapter_text, last_chapter_data, remaining_ch
TASK: Analyze pacing.
CONTEXT:
- PREVIOUS_SUMMARY: {summary[-3000:]}
- CURRENT_CHAPTER: {last_chapter_text[-2000:]}
- PREVIOUS_SUMMARY: {utils.truncate_to_tokens(summary, 1000)}
- CURRENT_CHAPTER: {utils.truncate_to_tokens(last_chapter_text, 800)}
- UPCOMING: {json.dumps([c['title'] for c in remaining_chapters[:3]])}
- REMAINING_COUNT: {len(remaining_chapters)}
@@ -254,7 +254,7 @@ def check_and_propagate(bp, manuscript, changed_chap_num, folder, change_summary
TASK: Summarize the key events and ending state of this chapter for continuity tracking.
TEXT:
{changed_chap.get('content', '')[:10000]}
{utils.truncate_to_tokens(changed_chap.get('content', ''), 2500)}
FOCUS:
- Major plot points.
@@ -350,7 +350,7 @@ def check_and_propagate(bp, manuscript, changed_chap_num, folder, change_summary
CHANGE_SUMMARY: {current_context}
CHAPTER_TO_CHECK (Ch {target_chap['num']}):
{target_chap['content'][:12000]}
{utils.truncate_to_tokens(target_chap['content'], 3000)}
DECISION_LOGIC:
- If the chapter directly contradicts the change (references dead characters, items that no longer exist, events that didn't happen), status = REWRITE.