gproxy-protocol 4.1.13

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
use super::supplement::{ClaudeModelSupplement, Converted};
use crate::transform::{Report, TransformError};
use crate::wire::{claude::models as claude_models, openai::models as openai_models};
use time::{OffsetDateTime, format_description::well_known::Rfc3339};

pub(crate) fn converted<T>(value: T, report: Report) -> Converted<T> {
    Converted { value, report }
}

pub(crate) fn require<T>(value: Option<T>, field: &str) -> Result<T, TransformError> {
    value.ok_or_else(|| TransformError::missing_metadata(field))
}

pub(crate) fn non_empty(value: &str, field: &str) -> Result<String, TransformError> {
    if value.is_empty() {
        Err(TransformError::missing_metadata(field))
    } else {
        Ok(value.to_owned())
    }
}

pub(crate) fn choose<T: PartialEq>(
    source: Option<T>,
    supplement: Option<T>,
    field: &str,
) -> Result<T, TransformError> {
    match (source, supplement) {
        (Some(source), Some(supplement)) if source != supplement => Err(TransformError::new(
            crate::transform::TransformErrorKind::Conflict,
            field,
            "source and supplied metadata disagree",
        )),
        (Some(source), _) => Ok(source),
        (None, Some(supplement)) => Ok(supplement),
        (None, None) => Err(TransformError::missing_metadata(field)),
    }
}

pub(crate) fn choose_optional<T: PartialEq>(
    source: Option<T>,
    supplement: Option<T>,
    field: &str,
) -> Result<Option<T>, TransformError> {
    match (source, supplement) {
        (Some(source), Some(supplement)) if source != supplement => Err(TransformError::new(
            crate::transform::TransformErrorKind::Conflict,
            field,
            "source model representations disagree",
        )),
        (Some(source), _) => Ok(Some(source)),
        (None, supplement) => Ok(supplement),
    }
}

pub(crate) fn openai_identity(model: &openai_models::Model) -> Result<&str, TransformError> {
    if !model.id.is_empty() {
        return Ok(&model.id);
    }
    model
        .slug
        .as_deref()
        .filter(|slug| !slug.is_empty())
        .ok_or_else(|| TransformError::shape("model.id", "model id and slug are empty"))
}

pub(crate) fn openai_thinking(model: &openai_models::Model) -> Option<bool> {
    model.thinking_supported.or_else(|| {
        model
            .supported_reasoning_levels
            .as_ref()
            .and_then(|levels| {
                levels
                    .iter()
                    .any(|level| level.effort != openai_models::ModelReasoningEffort::None)
                    .then_some(true)
            })
    })
}

pub(crate) fn iso_to_epoch_with_loss(value: &str) -> Result<(i64, bool), TransformError> {
    OffsetDateTime::parse(value, &Rfc3339)
        .map(|date| {
            (
                date.unix_timestamp(),
                date.unix_timestamp_nanos() % 1_000_000_000 != 0,
            )
        })
        .map_err(|error| {
            TransformError::shape(
                "model.created_at",
                format!("invalid RFC3339 timestamp: {error}"),
            )
        })
}

pub(crate) fn parse_iso(value: &str) -> Result<OffsetDateTime, TransformError> {
    OffsetDateTime::parse(value, &Rfc3339).map_err(|error| {
        TransformError::shape(
            "model.created_at",
            format!("invalid RFC3339 timestamp: {error}"),
        )
    })
}

pub(crate) fn epoch_to_iso(value: i64) -> Result<String, TransformError> {
    OffsetDateTime::from_unix_timestamp(value)
        .map_err(|error| TransformError::shape("model.created", error.to_string()))?
        .format(&Rfc3339)
        .map_err(|error| TransformError::shape("model.created", error.to_string()))
}

pub(crate) fn openai_model_id(value: &str) -> Result<String, TransformError> {
    if value.is_empty() {
        Err(TransformError::shape("model.id", "model id is empty"))
    } else {
        Ok(value.to_owned())
    }
}

pub(crate) fn gemini_name(value: &str) -> Result<String, TransformError> {
    if value.is_empty() {
        return Err(TransformError::shape("model.name", "model id is empty"));
    }
    if value.starts_with("models/") {
        if value.len() == "models/".len() {
            return Err(TransformError::shape("model.name", "empty models resource"));
        }
        Ok(value.to_owned())
    } else {
        Ok(format!("models/{value}"))
    }
}

pub(crate) fn bare_gemini_id(value: &str) -> Result<String, TransformError> {
    let Some(id) = value.strip_prefix("models/") else {
        return Err(TransformError::shape(
            "model.name",
            "Gemini model resource must begin with models/",
        ));
    };
    if id.is_empty() {
        Err(TransformError::shape("model.name", "empty models resource"))
    } else {
        Ok(id.to_owned())
    }
}

pub(crate) fn openai_model(id: String, created: i64, owned_by: String) -> openai_models::Model {
    openai_models::Model::builder(
        id,
        Some(created),
        Some(openai_models::ModelObject::Model),
        Some(owned_by),
    )
    .build()
}

pub(crate) fn claude_model(
    id: String,
    created_at: String,
    display_name: String,
    max_input_tokens: Option<i64>,
    max_tokens: Option<i64>,
    supplement: ClaudeModelSupplement,
) -> claude_models::ModelInfo {
    claude_models::ModelInfo {
        id,
        allowed_fallback_models: Some(supplement.allowed_fallback_models),
        capabilities: Some(supplement.capabilities.into_wire()),
        created_at,
        display_name,
        line: Some(supplement.line),
        lifecycle: supplement.lifecycle,
        deprecated_at: supplement.deprecated_at.map(Some),
        retires_at: supplement.retires_at.map(Some),
        max_input_tokens,
        max_tokens,
        type_: claude_models::ModelType::Model,
        rest: Default::default(),
    }
}

pub(crate) fn merge_report(target: &mut Report, source: Report) {
    target.diagnostics.extend(source.diagnostics);
}

pub(crate) fn report_openai_loss(report: &mut Report) {
    report.omitted("model.owned_by", "target dialect has no owner field");
}

pub(crate) fn report_claude_loss(report: &mut Report) {
    for field in [
        "batch",
        "citations",
        "code_execution",
        "server_tools",
        "context_management",
        "compaction",
        "pdf_input",
        "structured_outputs",
        "thinking.types",
    ] {
        report.omitted(
            format!("model.capabilities.{field}"),
            "target has no corresponding Claude capability field",
        );
    }
    report.omitted("model.line", "target dialect has no model line field");
    report.omitted(
        "model.allowed_fallback_models",
        "target dialect has no fallback list",
    );
}

pub(crate) fn report_gemini_loss(report: &mut Report) {
    report.omitted(
        "model.base_model_id",
        "OpenAI model objects have no base-model id",
    );
    report.omitted(
        "model.version",
        "OpenAI model objects have no model version",
    );
    report.omitted(
        "model.temperature",
        "OpenAI model objects have no temperature field",
    );
    report.omitted(
        "model.max_temperature",
        "OpenAI model objects have no max_temperature field",
    );
    report.omitted("model.top_p", "OpenAI model objects have no top_p field");
    report.omitted("model.top_k", "OpenAI model objects have no top_k field");
}

pub(crate) fn report_openai_extensions(model: &openai_models::Model, report: &mut Report) {
    macro_rules! omit {
        ($($field:ident),* $(,)?) => {$(
            if model.$field.is_some() {
                report.omitted(concat!("model.", stringify!($field)),
                    "target model schema has no corresponding catalog field");
            }
        )*};
    }
    omit!(
        shutdown_date,
        instructions,
        output_modalities,
        supported_parameters,
        default_reasoning_level,
        service_tiers,
        default_service_tier,
        supported_actions,
        guardian,
        shell_type,
        visibility,
        supported_in_api,
        priority,
        additional_speed_tiers,
        available_access_programs,
        availability_nux,
        upgrade,
        model_messages,
        include_skills_usage_instructions,
        include_plugin_usage_instructions,
        include_apps_usage_instructions,
        supports_reasoning_summary_parameter,
        default_reasoning_summary,
        support_verbosity,
        default_verbosity,
        apply_patch_tool_type,
        web_search_tool_type,
        truncation_policy,
        supports_image_detail_original,
        auto_compact_token_limit,
        comp_hash,
        effective_context_window_percent,
        experimental_supported_tools,
        supports_search_tool,
        supports_experimental_context,
        use_responses_lite,
        supports_reasoning_effort_updates,
        node_repl_auto_review_required,
        node_repl_disabled,
        auto_review_model_override,
        model_specialty,
        tool_mode,
        multi_agent_version,
        multi_agent_reasoning_effort,
        base_instructions,
        prefer_websockets,
        supports_parallel_tool_calls,
        supports_reasoning_summaries,
        requires_sandboxed_review,
        available_in_plans,
        minimal_client_version,
    );
    if model.id.is_empty() && model.slug.is_some() {
        report.changed("model.slug", "slug used as target model identity");
    } else if let Some(slug) = &model.slug
        && slug != &model.id
    {
        report.omitted("model.slug", "target has only one model identity");
    }
    if model.max_context_window.is_some() {
        if model.context_window.is_none() {
            report.changed(
                "model.max_context_window",
                "maximum context window used as input token limit",
            );
        } else {
            report.omitted(
                "model.max_context_window",
                "target cannot distinguish the default window from its override ceiling",
            );
        }
    }
}

pub(crate) fn reconcile_openai_capabilities(
    model: &openai_models::Model,
    mut supplement: super::supplement::ClaudeModelSupplement,
) -> Result<super::supplement::ClaudeModelSupplement, TransformError> {
    supplement.capabilities.thinking.supported = choose(
        openai_thinking(model),
        Some(supplement.capabilities.thinking.supported),
        "model.thinking_supported",
    )?;
    if let Some(modalities) = &model.input_modalities {
        supplement.capabilities.image_input = choose(
            Some(modalities.iter().any(|modality| modality == "image")),
            Some(supplement.capabilities.image_input),
            "model.input_modalities.image",
        )?;
    }
    if let Some(levels) = &model.supported_reasoning_levels {
        use openai_models::ModelReasoningEffort as Effort;
        let has = |effort| levels.iter().any(|level| level.effort == effort);
        macro_rules! reconcile {
            ($($field:ident => $effort:ident),* $(,)?) => {$(
                supplement.capabilities.effort.$field = choose(
                    Some(has(Effort::$effort)), Some(supplement.capabilities.effort.$field),
                    concat!("model.supported_reasoning_levels.", stringify!($field)),
                )?;
            )*};
        }
        reconcile!(low => Low, medium => Medium, high => High, xhigh => Xhigh, max => Max);
        supplement.capabilities.effort.supported = choose(
            Some(
                [
                    Effort::Low,
                    Effort::Medium,
                    Effort::High,
                    Effort::Xhigh,
                    Effort::Max,
                ]
                .into_iter()
                .any(has),
            ),
            Some(supplement.capabilities.effort.supported),
            "model.supported_reasoning_levels",
        )?;
    }
    Ok(supplement)
}

/// Lifecycle has no equivalent in the other model directories; do not infer
/// availability from a retirement date or turn it into a generation capability.
/// OpenAI keeps the scheduled retirement as `shutdown_date`; see `openai_shutdown_date`.
pub(crate) fn report_claude_lifecycle(
    model: &claude_models::ModelInfo,
    keeps_retirement: bool,
    report: &mut Report,
) {
    for (field, present) in [
        ("lifecycle", model.lifecycle.is_some()),
        ("deprecated_at", model.deprecated_at.is_some()),
        (
            "retires_at",
            !keeps_retirement && model.retires_at.is_some(),
        ),
    ] {
        if present {
            report.omitted(
                format!("model.{field}"),
                "target has no Claude lifecycle field",
            );
        }
    }
}

/// Claude's scheduled retirement instant becomes OpenAI's announced shutdown date.
pub(crate) fn openai_shutdown_date(
    model: &claude_models::ModelInfo,
    report: &mut Report,
) -> Result<Option<Option<String>>, TransformError> {
    let Some(retires_at) = &model.retires_at else {
        return Ok(None);
    };
    let Some(retires_at) = retires_at else {
        return Ok(Some(None));
    };
    let instant = OffsetDateTime::parse(retires_at, &Rfc3339).map_err(|error| {
        TransformError::shape(
            "model.retires_at",
            format!("invalid RFC3339 timestamp: {error}"),
        )
    })?;
    let date = instant.to_offset(time::UtcOffset::UTC).date();
    if instant.to_offset(time::UtcOffset::UTC).time() != time::Time::MIDNIGHT {
        report.changed(
            "model.retires_at",
            "OpenAI shutdown_date keeps only the UTC calendar date",
        );
    }
    Ok(Some(Some(date.to_string())))
}