Something went wrong. Try again.
minimal agent cli memoria.tools
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228package codex
import ".."import "core:encoding/json"
Codex_Content :: struct { type: string, text: string,}
Codex_Summary :: struct { type: string, text: string,}
Codex_Optional_Summary :: union { [dynamic]Codex_Summary,}
Codex_Optional_Output :: union { string,}
Codex_Input :: struct { type: string, role: string `json:",omitempty"`, content: [dynamic]Codex_Content `json:",omitempty"`, call_id: string `json:",omitempty"`, name: string `json:",omitempty"`, arguments: string `json:",omitempty"`, output: Codex_Optional_Output `json:",omitempty"`, summary: Codex_Optional_Summary `json:",omitempty"`, encrypted_content: string `json:",omitempty"`, owned: bool `json:"-"`,}
Codex_Tool :: struct { type: string, name: string, description: string, parameters: json.Value,}
Codex_Text_Options :: struct { verbosity: string,}
Codex_Reasoning :: struct { effort: string `json:",omitempty"`, summary: string,}
Codex_Optional_Reasoning :: union { Codex_Reasoning,}
Codex_Request :: struct { model: string, stream: bool, store: bool, instructions: string, input: [dynamic]Codex_Input, tools: [dynamic]Codex_Tool `json:",omitempty"`, tool_choice: string, parallel_tool_calls: bool, include: [1]string, prompt_cache_key: string `json:",omitempty"`, text: Codex_Text_Options, reasoning: Codex_Optional_Reasoning `json:",omitempty"`,}
destroy_input :: proc(input: ^Codex_Input) { if input.owned { for index in 0 ..< len(input.content) { delete(input.content[index].type) delete(input.content[index].text) } delete(input.type) delete(input.role) delete(input.call_id) delete(input.name) delete(input.arguments) if output, ok := input.output.(string); ok { delete(output) } if summary, ok := input.summary.([dynamic]Codex_Summary); ok { for index in 0 ..< len(summary) { delete(summary[index].type) delete(summary[index].text) } delete(summary) } delete(input.encrypted_content) } delete(input.content) input^ = {}}
invalid_request :: proc(detail: string) -> provider.Provider_Error { return {kind = .Invalid_Request, detail = detail}}
serialize_request :: proc( request: ^provider.Provider_Request, session_id: string,) -> ( body: []u8, error: provider.Provider_Error,) { if request == nil || request.model == "" || session_id == "" { return nil, invalid_request("model and session ID are required") }
inputs: [dynamic]Codex_Input tools: [dynamic]Codex_Tool defer { for index in 0 ..< len(inputs) { destroy_input(&inputs[index]) } delete(inputs) for tool in tools { json.destroy_value(tool.parameters) } delete(tools) }
for message in request.messages { switch message.kind { case .Reasoning: // Visible summaries are represented by encrypted continuation items. continue case .User, .Assistant: role := message.kind == .User ? "user" : "assistant" if message.text != "" { content_type := message.kind == .User ? "input_text" : "output_text" content := make([dynamic]Codex_Content, 0, 1) append(&content, Codex_Content{type = content_type, text = message.text}) append(&inputs, Codex_Input{type = "message", role = role, content = content}) } for call in message.tool_calls { if call.id == "" || call.name == "" { return nil, invalid_request("tool calls require an ID and name") } append( &inputs, Codex_Input { type = "function_call", call_id = call.id, name = call.name, arguments = call.arguments, }, ) } case .Tool_Result: if message.tool_call_id == "" { return nil, invalid_request("tool results require a tool call ID") } append( &inputs, Codex_Input { type = "function_call_output", call_id = message.tool_call_id, output = message.text, }, ) case .Continuation: input: Codex_Input parse_error := json.unmarshal_string(message.continuation, &input, spec = .JSON) input.owned = true if parse_error != nil || input.type != "reasoning" || input.encrypted_content == "" { destroy_input(&input) return nil, invalid_request("continuation is not a valid Codex reasoning item") } append(&inputs, input) } }
for tool in request.tools { if tool.name == "" || tool.input_schema_json == "" { return nil, invalid_request("tools require a name and input schema") } schema: json.Value parse_error := json.unmarshal_string(tool.input_schema_json, &schema, spec = .JSON) _, object_ok := schema.(json.Object) if parse_error != nil || !object_ok { json.destroy_value(schema) return nil, invalid_request("tool input schema must be a JSON object") } append( &tools, Codex_Tool { type = "function", name = tool.name, description = tool.description, parameters = schema, }, ) }
wire_request := Codex_Request { model = request.model, stream = true, store = false, instructions = request.instructions, input = inputs, tools = tools, tool_choice = "auto", parallel_tool_calls = true, include = {"reasoning.encrypted_content"}, prompt_cache_key = request.prompt_cache_key, text = {verbosity = "low"}, } if request.variant != "" { effort := request.variant if effort == "minimal" do effort = "low" wire_request.reasoning = Codex_Reasoning { effort = effort, summary = "auto", } } encoded, marshal_error := json.marshal(wire_request) if marshal_error != nil { return nil, {kind = .Configure, detail = "could not encode Codex request"} } return encoded, {}}