File size: 1,725 Bytes
41ea373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebae6ab
41ea373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import difflib
from pydantic import BaseModel

from viral_script_engine.agents.llm_backend import LLMBackend
from viral_script_engine.environment.actions import ArbitratorAction

_SYSTEM_PROMPT = (
    "You are a professional script editor for short-form social media video. "
    "Apply ONLY the instruction given. Do not make any other changes. "
    "Do not add new ideas. Do not change the creator's voice or regional language patterns. "
    "Return ONLY the rewritten script text, no commentary."
)


class RewriteResult(BaseModel):
    rewritten_script: str
    diff: str
    word_count_delta: int


class RewriterAgent:
    def __init__(self, backend: str = "anthropic", model_name: str = "claude-haiku-4-5-20251001"):
        self.llm = LLMBackend(backend=backend, model_name=model_name)

    def rewrite(self, current_script: str, action: ArbitratorAction) -> RewriteResult:
        user_prompt = (
            f"CURRENT SCRIPT:\n{current_script}\n\n"
            f"ACTION TYPE: {action.action_type.value}\n"
            f"TARGET SECTION: {action.target_section}\n"
            f"INSTRUCTION: {action.instruction}\n\n"
            "Apply the instruction and return ONLY the rewritten script."
        )
        rewritten = self.llm.generate(_SYSTEM_PROMPT, user_prompt, max_tokens=2048)
        diff_lines = list(difflib.unified_diff(
            current_script.splitlines(keepends=True),
            rewritten.splitlines(keepends=True),
            fromfile="original",
            tofile="rewritten",
        ))
        return RewriteResult(
            rewritten_script=rewritten,
            diff="".join(diff_lines),
            word_count_delta=len(rewritten.split()) - len(current_script.split()),
        )