"""2.1 (English edition): a chat 'remembers' only because the whole history is re-sent every turn.

English twin of experiments/2_1_stateless/run.py. Same design, English corpus (experiments_en/corpus/memos.json).
Part A: an 8-turn conversation about a transcript bundle (W01, W07, W10, W12, W16, W17). We record the words the user
typed each turn and the input tokens the API actually billed for that turn.
Part B: the turn-8 question asked again with no history.
Part C (the bare "hi" baseline) is not re-run: it is language-neutral; see experiments/2_1_stateless/baseline_hi.json.

Usage (textbook repo root):
  op run --env-file experiments/aib.env -- /Users/grapeot/co/knowledge_working/.venv/bin/python experiments_en/2_1_stateless/run.py gemini-3.8-flash
  op run --env-file experiments/aib.env -- /Users/grapeot/co/knowledge_working/.venv/bin/python experiments_en/2_1_stateless/run.py deepseek-v4.1-flash
"""
import json
import pathlib
import sys

sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from common import chat, dump, log_cost  # noqa: E402

MODEL = sys.argv[1] if len(sys.argv) > 1 else 'gemini-3.8-flash'
HERE = pathlib.Path(__file__).resolve().parent
memos = {m['id']: m['text'] for m in json.load(open(HERE.parent / 'corpus/memos.json', encoding='utf-8'))['memos']}
BUNDLE_IDS = ['W01', 'W07', 'W10', 'W12', 'W16', 'W17']
transcript = ' '.join(memos[i] for i in BUNDLE_IDS)
turns = [
    'Here are transcripts of a few of my voice memos from this week. The names may be wrong. On my team, Saoirse '
    'handles deployment and Jaxon is on the backend. Please remember that.\n\n' + transcript,
    'How many people are mentioned?', 'Who handles deployment?', 'Why did the bill go up?',
    'What is Aoife working on?', "Who's coming next month?", 'What needs to be prepared?',
    "What's the name of the colleague who handles deployment?",
]
SYSTEM = 'You are a concise assistant. Answer in English.'


def words(s):
    return len(s.split())


msgs, rows = [{'role': 'system', 'content': SYSTEM}], []
for i, q in enumerate(turns, 1):
    msgs.append({'role': 'user', 'content': q})
    a, u, s = chat(msgs, MODEL, temperature=0)
    msgs.append({'role': 'assistant', 'content': a})
    rows.append({'turn': i, 'typed_words': words(q), 'typed_chars': len(q), 'prompt_tokens': u.get('prompt_tokens'),
                 'completion_tokens': u.get('completion_tokens'), 'answer': a})
    print(i, words(q), u.get('prompt_tokens'), a[:60].replace('\n', ' '))
fresh, u, _ = chat([msgs[0], {'role': 'user', 'content': turns[-1]}], MODEL, temperature=0)
print('fresh:', u.get('prompt_tokens'), fresh[:120].replace('\n', ' '))
dump({'model': MODEL, 'system': SYSTEM, 'bundle_memos': BUNDLE_IDS, 'bundle_words': words(turns[0]),
      'transcript_words': words(transcript), 'turns': rows,
      'fresh_call': {'question': turns[-1], 'prompt_tokens': u.get('prompt_tokens'), 'answer': fresh,
                     'answered_saoirse': 'saoirse' in fresh.lower()}},
     HERE / f'results_{MODEL}.json')
log_cost(f'2_1_stateless/run.py {MODEL}')
