"""Forced meeting extraction request shared by both arms. MIT."""
import json
import os

MODEL = os.environ.get('MODEL', 'pareto')
BASE_URL = os.environ.get('BASE_URL', 'https://api.unbiased.ai/v1')


def request(item, config):
    action = {'type': 'object', 'additionalProperties': False, 'properties': {
        'task': {'type': 'string'}, 'owner': {'type': ['string', 'null']},
        'due_date': {'type': ['string', 'null']}, 'line': {'type': 'integer'}},
        'required': ['task', 'owner', 'due_date', 'line']}
    decision = {'type': 'object', 'additionalProperties': False, 'properties': {
        'decision': {'type': 'string'}, 'line': {'type': 'integer'}},
        'required': ['decision', 'line']}
    schema = {'type': 'object', 'additionalProperties': False, 'properties': {
        'actions': {'type': 'array', 'items': action},
        'decisions': {'type': 'array', 'items': decision}}, 'required': ['actions', 'decisions']}
    return {'messages': [{'role': 'system', 'content': config['prompt']}, {'role': 'user', 'content': item['text']}],
            'tools': [{'type': 'function', 'function': {'name': 'record_meeting', 'description': 'Record committed open tasks and agreed decisions with cited lines.', 'parameters': schema, 'strict': True}}],
            'tool_choice': config['tool_choice'], 'parallel_tool_calls': False,
            'max_tokens': config['max_tokens'], 'reasoning_effort': config['reasoning_effort']}


def main():
    """Optional SDK smoke entry point; scored runs use the published runner."""
    from pathlib import Path
    from openai import OpenAI
    config = json.loads(Path(__file__).with_name('eval.json').read_text())
    item = next(json.loads(line) for line in Path(__file__).with_name('fixture.jsonl').read_text().splitlines() if json.loads(line)['split'] == 'dev')
    key = 'UNBIASED_API_KEY' if BASE_URL == 'https://api.unbiased.ai/v1' else 'OPENAI_API_KEY'
    response = OpenAI(base_url=BASE_URL, api_key=os.environ[key]).chat.completions.create(model=MODEL, **request(item, config))
    calls = response.choices[0].message.tool_calls or []
    if len(calls) != 1 or calls[0].function.name != 'record_meeting':
        raise ValueError('Reject extra or missing tool calls; never silently take the first')
    print(json.dumps(json.loads(calls[0].function.arguments), indent=2))


if __name__ == '__main__':
    main()
