p = 'backend/app/routers/ai_mode.py'
s = open(p).read()

old = '''    model_id, api_key = get_model_for_task(db, "teach")
    if not model_id:
        raise HTTPException(503, "No AI model is configured. Ask an admin to set one up.")
'''
new = '''    # "I want to do questions on pediatric infectious disease" is one of the two
    # things this tutor is for, and it came back "That is not something I
    # discuss." Answered here, before any model is asked: a count, the reading,
    # and the button — or, where there is nothing, what to do instead. No model
    # call means no refusal, no invented number, and the same answer every time.
    if ai_mode_service.wants_practice(question):
        reply, citations, ids = ai_mode_service.practice_answer(db, current_user, question)
        db.add(ConversationMessage(conversation_id=conversation.id, role="user",
                                   content=question, citations=[]))
        answer = ConversationMessage(conversation_id=conversation.id, role="assistant",
                                     content=reply, citations=citations)
        db.add(answer)
        if not conversation.title or conversation.title == NEW_THREAD:
            conversation.title = thread_title(question)
        db.commit()
        db.refresh(answer)
        return _answer_json(answer, ids)

    model_id, api_key = get_model_for_task(db, "teach")
    if not model_id:
        raise HTTPException(503, "No AI model is configured. Ask an admin to set one up.")
'''
assert old in s
s = s.replace(old, new, 1)
open(p, 'w').write(s)
print('written')
