gproxy-protocol 4.1.11

GPROXY v4 connection modelling, operation declarations, vendor wire types and pairwise conversion
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use super::annotations;
use crate::transform::generate::reasoning_details as rd;
use crate::{
    Dialect,
    transform::{
        Report, TransformError, TransformErrorKind,
        identity::{IdentityFlow, IdentityRole, OutputItemKind, SourceIdentity, TargetIdPolicy},
    },
    wire::openai::{
        chat::{content as cc, response as c},
        responses::{input as i, response as r},
    },
};

pub(super) fn identity(
    flow: &mut IdentityFlow,
    policy: &TargetIdPolicy,
    source: Dialect,
    from: IdentityRole,
    to: IdentityRole,
    id: Option<String>,
    index: u64,
) -> Result<String, TransformError> {
    flow.resolve_as(from, to, SourceIdentity::new(source, id, index), policy)
        .map(|h| h.emitted_id)
        .map_err(|e| {
            TransformError::with_source(
                TransformErrorKind::Conflict,
                "response.identity",
                "response identity could not be bound",
                e,
            )
        })
}

pub(super) fn to_responses(
    message: c::ResponseMessage,
    logs: Option<c::Logprobs>,
    incomplete: bool,
    flow: &mut IdentityFlow,
    policy: &TargetIdPolicy,
    report: &mut Report,
    bindings: &super::super::client_tools::Bindings,
) -> Result<Vec<r::ResponseOutputItem>, TransformError> {
    let reasoning = crate::wire::openai::chat::visible_reasoning(
        &message.reasoning_content,
        &message.reasoning,
        &message.reasoning_details,
    );
    if message.audio.flatten().is_some() {
        return Err(TransformError::unsupported(
            "message.audio",
            "Responses has no native output-audio item on this wire",
        ));
    }
    let mut parts = Vec::new();
    let content_logs = logs.as_ref().and_then(|v| v.content.clone());
    if logs.as_ref().is_some_and(|v| v.refusal.is_some()) {
        report.omitted(
            "logprobs.refusal",
            "Responses refusal blocks have no logprob field",
        );
    }
    if let Some(text) = message.content {
        parts.push(i::OutputContent::Text(i::ResponseOutputText {
            type_: i::ResponseOutputTextType::ResponseOutputText,
            text,
            annotations: annotations::to_responses(message.annotations)?,
            logprobs: annotations::logs_to_responses(content_logs, report)?,
            rest: Default::default(),
        }));
    } else if message.annotations.is_some() || content_logs.is_some() {
        return Err(TransformError::invalid_result(
            "message.content",
            "text annotations or logprobs have no associated text",
        ));
    }
    if let Some(refusal) = message.refusal {
        parts.push(i::OutputContent::Refusal(i::ResponseOutputRefusal {
            type_: i::ResponseOutputRefusalType::ResponseOutputRefusal,
            refusal,
            rest: Default::default(),
        }));
    }
    let restored = rd::to_responses_output(
        message
            .reasoning_details
            .as_ref()
            .and_then(Option::as_deref)
            .unwrap_or(&[]),
        flow,
        policy,
    )?;
    let has_restored = !restored.is_empty();
    let mut output = restored
        .into_iter()
        .map(|mut item| {
            item.status = Some(if incomplete {
                i::ReasoningStatus::Incomplete
            } else {
                i::ReasoningStatus::Completed
            });
            r::ResponseOutputItem::Reasoning(item)
        })
        .collect::<Vec<_>>();
    if !has_restored && let Some(text) = reasoning {
        let id = identity(
            flow,
            policy,
            Dialect::OpenAiChat,
            IdentityRole::OutputItem(OutputItemKind::Reasoning),
            IdentityRole::OutputItem(OutputItemKind::Reasoning),
            None,
            0,
        )?;
        let mut item =
            i::ReasoningItem::builder(i::ReasoningItemType::ReasoningItem, id, Vec::new()).build();
        item.content = Some(vec![
            i::ReasoningContent::builder(i::ReasoningTextType::ReasoningText, text).build(),
        ]);
        item.status = Some(if incomplete {
            i::ReasoningStatus::Incomplete
        } else {
            i::ReasoningStatus::Completed
        });
        output.push(r::ResponseOutputItem::Reasoning(item));
    }
    if !parts.is_empty() {
        let id = identity(
            flow,
            policy,
            Dialect::OpenAiChat,
            IdentityRole::Message,
            IdentityRole::OutputItem(OutputItemKind::Message),
            None,
            0,
        )?;
        output.push(r::ResponseOutputItem::Message(i::ResponseOutputMessage {
            agent: None,
            id,
            content: parts,
            role: i::OutputMessageRole::Assistant,
            status: if incomplete {
                i::OutputMessageStatus::Incomplete
            } else {
                i::OutputMessageStatus::Completed
            },
            phase: None,
            type_: i::MessageType::Message,
            rest: Default::default(),
        }));
    }
    let calls = message.tool_calls.unwrap_or_default();
    if let Some(call) = message.function_call {
        if !calls.is_empty() {
            return Err(TransformError::invalid_result(
                "message.function_call",
                "legacy and current tool calls coexist ambiguously",
            ));
        }
        let call_id = flow
            .resolve_legacy_chat_call(SourceIdentity::new(Dialect::OpenAiChat, None, 0), policy)
            .map(|h| h.emitted_id)
            .map_err(|e| {
                TransformError::with_source(
                    TransformErrorKind::Conflict,
                    "response.identity",
                    "response identity could not be bound",
                    e,
                )
            })?;
        let id = identity(
            flow,
            policy,
            Dialect::OpenAiChat,
            IdentityRole::ToolCall,
            IdentityRole::OutputItem(OutputItemKind::FunctionCall),
            None,
            0,
        )?;
        output.push(r::ResponseOutputItem::FunctionCall(i::FunctionCall {
            agent: None,
            async_: None,
            type_: i::FunctionCallType::FunctionCall,
            arguments: call.arguments,
            call_id,
            name: call.name,
            id: Some(id),
            namespace: None,
            caller: None,
            status: Some(if incomplete {
                i::ItemStatus::Incomplete
            } else {
                i::ItemStatus::Completed
            }),
            rest: Default::default(),
        }));
        report.changed(
            "message.function_call",
            "legacy call receives an invocation-scoped call ID",
        );
    }
    for (index, call) in calls.into_iter().enumerate() {
        let index = u64::try_from(index)
            .map_err(|_| TransformError::invalid_result("tool_calls", "too many tool calls"))?;
        match call {
            cc::MessageToolCall::Function(call) => {
                let id = identity(
                    flow,
                    policy,
                    Dialect::OpenAiChat,
                    IdentityRole::ToolCall,
                    IdentityRole::OutputItem(bindings.kind(&call.function.name)),
                    Some(call.id.clone()),
                    index,
                )?;
                let call_id = identity(
                    flow,
                    policy,
                    Dialect::OpenAiChat,
                    IdentityRole::ToolCall,
                    IdentityRole::ToolCall,
                    Some(call.id),
                    index,
                )?;
                output.push(bindings.restore(i::FunctionCall {
                    agent: None,
                    async_: None,
                    type_: i::FunctionCallType::FunctionCall,
                    arguments: call.function.arguments,
                    call_id,
                    name: call.function.name,
                    id: Some(id),
                    namespace: None,
                    caller: None,
                    status: Some(if incomplete {
                        i::ItemStatus::Incomplete
                    } else {
                        i::ItemStatus::Completed
                    }),
                    rest: Default::default(),
                })?);
            }
            cc::MessageToolCall::Custom(call) => {
                let id = identity(
                    flow,
                    policy,
                    Dialect::OpenAiChat,
                    IdentityRole::ToolCall,
                    IdentityRole::OutputItem(OutputItemKind::CustomToolCall),
                    Some(call.id.clone()),
                    index,
                )?;
                let call_id = identity(
                    flow,
                    policy,
                    Dialect::OpenAiChat,
                    IdentityRole::ToolCall,
                    IdentityRole::ToolCall,
                    Some(call.id),
                    index,
                )?;
                output.push(r::ResponseOutputItem::CustomToolCall(i::CustomToolCall {
                    agent: None,
                    async_: None,
                    type_: i::CustomToolCallType::CustomToolCall,
                    call_id,
                    input: call.custom.input,
                    name: call.custom.name,
                    id: Some(id),
                    caller: None,
                    namespace: None,
                    rest: Default::default(),
                }));
            }
        }
    }
    Ok(output)
}

pub(super) struct ChatOutput {
    pub message: c::ResponseMessage,
    pub logprobs: Option<c::Logprobs>,
    pub has_tools: bool,
}

pub(super) fn to_chat(
    output: Vec<r::ResponseOutputItem>,
    completed: bool,
    flow: &mut IdentityFlow,
    policy: &TargetIdPolicy,
    report: &mut Report,
) -> Result<ChatOutput, TransformError> {
    let mut text = String::new();
    let mut reasoning = Vec::new();
    let mut details = Vec::new();
    let mut has_text = false;
    let mut refusal = String::new();
    let mut has_refusal = false;
    let mut annotations_out = Vec::new();
    let mut logs = Vec::new();
    let mut calls = Vec::new();
    for (index, item) in output.into_iter().enumerate() {
        if crate::transform::generate::multi_agent::omit_output(&item, report) {
            continue;
        }
        match item {
            r::ResponseOutputItem::Message(message) => {
                match message.status {
                    i::OutputMessageStatus::InProgress => {
                        return Err(TransformError::invalid_result(
                            "output.message.status",
                            "message is still in progress",
                        ));
                    }
                    i::OutputMessageStatus::Incomplete if completed => {
                        return Err(TransformError::invalid_result(
                            "output.message.status",
                            "completed response contains incomplete message",
                        ));
                    }
                    i::OutputMessageStatus::Completed | i::OutputMessageStatus::Incomplete => {}
                }
                if message.phase.is_some() {
                    report.omitted("output.message.phase", "Chat has no message phase field");
                }
                for part in message.content {
                    match part {
                        i::OutputContent::Text(part) => {
                            let offset = i64::try_from(text.chars().count()).map_err(|_| {
                                TransformError::invalid_result("output.text", "text index overflow")
                            })?;
                            annotations_out.extend(annotations::to_chat(
                                part.annotations,
                                offset,
                                report,
                            )?);
                            logs.extend(annotations::logs_to_chat(part.logprobs)?);
                            text.push_str(&part.text);
                            has_text = true;
                        }
                        i::OutputContent::Refusal(part) => {
                            refusal.push_str(&part.refusal);
                            has_refusal = true;
                        }
                    }
                }
            }
            r::ResponseOutputItem::FunctionCall(call) => {
                if call.async_ == Some(true) {
                    report.omitted(
                        "output.async",
                        "target tool calls have no asynchronous continuation marker",
                    );
                }
                if matches!(call.status, Some(i::ItemStatus::InProgress))
                    || (completed && matches!(call.status, Some(i::ItemStatus::Incomplete)))
                {
                    return Err(TransformError::invalid_result(
                        "output.function_call.status",
                        "tool call has not reached the response terminal state",
                    ));
                }
                if call.namespace.is_some()
                    || call
                        .caller
                        .flatten()
                        .is_some_and(|caller| matches!(caller, i::Caller::Program(_)))
                {
                    continue;
                }
                let id = identity(
                    flow,
                    policy,
                    Dialect::OpenAi,
                    IdentityRole::ToolCall,
                    IdentityRole::ToolCall,
                    Some(call.call_id),
                    index as u64,
                )?;
                calls.push(cc::MessageToolCall::Function(cc::ChatToolCall {
                    id,
                    function: cc::FunctionCall {
                        name: call.name,
                        arguments: call.arguments,
                        rest: Default::default(),
                    },
                    type_: cc::ChatToolCallType::Function,
                    rest: Default::default(),
                }));
            }
            r::ResponseOutputItem::CustomToolCall(call) => {
                if call.async_ == Some(true) {
                    report.omitted(
                        "output.async",
                        "target tool calls have no asynchronous continuation marker",
                    );
                }
                if call.namespace.is_some()
                    || call
                        .caller
                        .flatten()
                        .is_some_and(|caller| matches!(caller, i::Caller::Program(_)))
                {
                    continue;
                }
                let id = identity(
                    flow,
                    policy,
                    Dialect::OpenAi,
                    IdentityRole::ToolCall,
                    IdentityRole::ToolCall,
                    Some(call.call_id),
                    index as u64,
                )?;
                calls.push(cc::MessageToolCall::Custom(cc::CustomToolCall {
                    id,
                    custom: cc::CustomCall {
                        input: call.input,
                        name: call.name,
                        rest: Default::default(),
                    },
                    type_: cc::CustomToolCallType::Custom,
                    rest: Default::default(),
                }));
            }
            r::ResponseOutputItem::Reasoning(item) => {
                details.extend(rd::from_responses(&item, details.len() as i64));
                reasoning.extend(item.summary.into_iter().map(|part| part.text));
                reasoning.extend(item.content.into_iter().flatten().map(|part| part.text));
            }
            r::ResponseOutputItem::FileSearchCall(_)
            | r::ResponseOutputItem::FunctionCallOutput(_)
            | r::ResponseOutputItem::WebSearchCall(_)
            | r::ResponseOutputItem::ComputerCall(_)
            | r::ResponseOutputItem::ComputerCallOutput(_)
            | r::ResponseOutputItem::Program(_)
            | r::ResponseOutputItem::ProgramOutput(_)
            | r::ResponseOutputItem::ToolSearchCall(_)
            | r::ResponseOutputItem::ToolSearchOutput(_)
            | r::ResponseOutputItem::AdditionalTools(_)
            | r::ResponseOutputItem::MultiAgentCall(_)
            | r::ResponseOutputItem::MultiAgentCallOutput(_)
            | r::ResponseOutputItem::AgentMessage(_)
            | r::ResponseOutputItem::ConfigurationUpdate(_)
            | r::ResponseOutputItem::Compaction(_)
            | r::ResponseOutputItem::ImageGenerationCall(_)
            | r::ResponseOutputItem::CodeInterpreterCall(_)
            | r::ResponseOutputItem::LocalShellCall(_)
            | r::ResponseOutputItem::LocalShellCallOutput(_)
            | r::ResponseOutputItem::ShellCall(_)
            | r::ResponseOutputItem::ShellCallOutput(_)
            | r::ResponseOutputItem::ApplyPatchCall(_)
            | r::ResponseOutputItem::ApplyPatchCallOutput(_)
            | r::ResponseOutputItem::McpCall(_)
            | r::ResponseOutputItem::McpListTools(_)
            | r::ResponseOutputItem::McpApprovalRequest(_)
            | r::ResponseOutputItem::McpApprovalResponse(_)
            | r::ResponseOutputItem::CustomToolCallOutput(_) => continue,
        }
    }
    let has_tools = !calls.is_empty();
    Ok(ChatOutput {
        has_tools,
        logprobs: (!logs.is_empty()).then_some(c::Logprobs {
            content: Some(logs),
            refusal: None,
            rest: Default::default(),
        }),
        message: c::ResponseMessage {
            reasoning_details: (!details.is_empty()).then_some(Some(details)),
            reasoning_content: (!reasoning.is_empty()).then(|| Some(reasoning.join("\n"))),
            reasoning: None,
            content: has_text.then_some(text),
            refusal: has_refusal.then_some(refusal),
            role: c::ResponseRole::Assistant,
            annotations: (!annotations_out.is_empty()).then_some(annotations_out),
            audio: None,
            function_call: None,
            tool_calls: has_tools.then_some(calls),
            rest: Default::default(),
        },
    })
}