{# Magidonia p5 OpenCode bridge: model-native JSON protocol. #} {{- bos_token }} {%- if messages and messages[0]['role'] == 'system' %} {%- set raw_system = messages[0]['content'] if messages[0]['content'] is string else messages[0]['content'][0]['text'] %} {%- set loop_messages = messages[1:] %} {%- else %} {%- set raw_system = '' %} {%- set loop_messages = messages %} {%- endif %} {%- if raw_system|length > 0 %} {{- '[SYSTEM_PROMPT]' + raw_system + '[/SYSTEM_PROMPT]' }} {%- endif %} {%- if tools is defined and tools is not none and tools|length > 0 %} {{- '[AVAILABLE_TOOLS]' + (tools | tojson) + '[/AVAILABLE_TOOLS]' }} {%- endif %} {%- for message in loop_messages %} {%- if message['role'] == 'user' %} {{- '[INST]' }} {%- if message['content'] is string %} {{- message['content'] }} {%- else %} {%- for block in message['content'] %} {%- if block['type'] == 'text' %}{{- block['text'] if block['text'] is defined else block['content'] }} {%- elif block['type'] in ['image', 'image_url'] %}{{- '[IMG]' }} {%- endif %} {%- endfor %} {%- endif %} {{- '[/INST]' }} {%- elif message['role'] == 'assistant' %} {%- if message['reasoning_content'] is defined and message['reasoning_content'] is not none %} {{- '' + message['reasoning_content'] + '' }} {%- endif %} {%- if message['content'] is string %}{{- message['content'] }} {%- elif message['content'] is iterable %}{{- message['content'][0]['text'] }} {%- endif %} {%- if message['tool_calls'] is defined and message['tool_calls'] is not none %} {%- for tool in message['tool_calls'] %} {%- set args = tool['function']['arguments'] %} {%- if args is not string %}{%- set args = args|tojson %}{%- endif %} {{- '{"name":"' + tool['function']['name'] + '","arguments":' + args + '}' }} {%- endfor %} {%- endif %} {{- eos_token }} {%- elif message['role'] == 'tool' or message['role'] == 'tool_results' %} {%- set result = message['content']['content'] if message['content'] is mapping and message['content']['content'] is defined else message['content'] %} {{- '[TOOL_RESULTS]' + result|string + '[/TOOL_RESULTS]' + eos_token }} {%- endif %} {%- endfor %}