link-assistant-router 1.14.2

Link.Assistant.Router — Claude MAX OAuth proxy and token gateway for Anthropic APIs
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
//! Local emulation of `max_output_tokens` for backends that reject the field.
//!
//! The `ChatGPT` Codex backend answers HTTP 400 for any explicit output cap, so
//! the router strips the field before forwarding (see
//! [`crate::subscription_proxy`]). Stripping alone would silently return more
//! output than the caller authorised, and refusing the request breaks every
//! ordinary `OpenAI`-compatible client (`OpenCode`, Grok CLI,
//! `@link-assistant/agent`), which always sends one. This module therefore
//! enforces the cap inside the router: visible output text is truncated at the
//! caller's budget and the exchange is terminated with the protocol's
//! length/incomplete signal.
//!
//! The budget is an estimate. The router has no upstream tokenizer, so it uses
//! the same ~4 characters per token heuristic as
//! the bridge request-shape estimator, and hidden reasoning
//! tokens are not observable at all. The cap is therefore a best-effort output
//! bound, not an exact accounting of billed tokens.

use serde_json::{Value, json};

/// Characters per token used by the router's local estimator.
const CHARS_PER_TOKEN: u64 = 4;

/// Incremental budget over visible output text.
#[derive(Clone, Debug, Default)]
pub struct OutputTokenLimiter {
    /// Remaining characters, or `None` when the caller sent no cap.
    remaining: Option<u64>,
    stopped: bool,
}

impl OutputTokenLimiter {
    /// Create a limiter for an optional caller-supplied output-token cap.
    #[must_use]
    pub const fn new(limit: Option<u64>) -> Self {
        Self {
            remaining: match limit {
                Some(limit) => Some(limit.saturating_mul(CHARS_PER_TOKEN)),
                None => None,
            },
            stopped: false,
        }
    }

    /// Whether a cap is being enforced at all.
    #[must_use]
    pub const fn enabled(&self) -> bool {
        self.remaining.is_some()
    }

    /// Whether the budget has already been exhausted.
    #[must_use]
    pub const fn stopped(&self) -> bool {
        self.stopped
    }

    /// Return the leading part of `text` that fits in the budget, and whether
    /// this chunk exhausted it.
    pub fn push(&mut self, text: &str) -> (String, bool) {
        if self.stopped {
            return (String::new(), false);
        }
        let Some(remaining) = self.remaining else {
            return (text.to_string(), false);
        };
        let remaining = usize::try_from(remaining).unwrap_or(usize::MAX);
        if text.len() <= remaining {
            self.remaining = Some((remaining - text.len()) as u64);
            return (text.to_string(), false);
        }
        let mut cut = remaining;
        while cut > 0 && !text.is_char_boundary(cut) {
            cut -= 1;
        }
        self.remaining = Some(0);
        self.stopped = true;
        (text[..cut].to_string(), true)
    }
}

/// Truncate `text` to an output-token budget, returning `None` when it fits.
#[must_use]
pub fn truncate(text: &str, limit: u64) -> Option<String> {
    let mut limiter = OutputTokenLimiter::new(Some(limit));
    let (visible, truncated) = limiter.push(text);
    truncated.then_some(visible)
}

/// Enforce an output cap on a buffered Responses payload.
pub fn enforce_response_limit(response: &mut Value, limit: u64) {
    let mut limiter = OutputTokenLimiter::new(Some(limit));
    let mut truncated = false;
    if let Some(items) = response.get_mut("output").and_then(Value::as_array_mut) {
        for item in items.iter_mut() {
            let Some(parts) = item.get_mut("content").and_then(Value::as_array_mut) else {
                continue;
            };
            for part in parts.iter_mut() {
                let Some(text) = part.get("text").and_then(Value::as_str) else {
                    continue;
                };
                let (visible, hit) = limiter.push(text);
                if hit || visible.len() != text.len() {
                    part["text"] = Value::String(visible);
                }
                truncated |= hit;
            }
        }
    }
    if truncated {
        response["status"] = Value::String("incomplete".into());
        response["incomplete_details"] = json!({"reason": "max_output_tokens"});
    }
}

/// Enforce an output cap on a buffered Chat Completions payload.
pub fn enforce_chat_limit(response: &mut Value, limit: u64) {
    let Some(choice) = response
        .get_mut("choices")
        .and_then(Value::as_array_mut)
        .and_then(|choices| choices.first_mut())
    else {
        return;
    };
    let Some(text) = choice
        .pointer("/message/content")
        .and_then(Value::as_str)
        .map(str::to_string)
    else {
        return;
    };
    if let Some(visible) = truncate(&text, limit) {
        choice["message"]["content"] = Value::String(visible);
        choice["finish_reason"] = Value::String("length".into());
    }
}

/// Rewrite a relayed Codex Responses SSE stream.
///
/// The native `/v1/responses` surface is otherwise a byte-for-byte relay, which
/// must retain the upstream's concrete model while enforcing the caller's
/// output cap. This rewriter keeps the relay shape and stops the stream once
/// the cap is exhausted.
pub struct ResponsesStreamRewriter {
    requested_model: String,
    limiter: OutputTokenLimiter,
    buffer: Vec<u8>,
    finished: bool,
    last_response: Option<Value>,
    upstream_model: Option<String>,
    strict_identity: bool,
    allow_substitution: bool,
    selector_kind: crate::model_contract::ModelSelectorKind,
}

impl ResponsesStreamRewriter {
    /// Create a rewriter for one Codex-backed Responses request.
    #[must_use]
    pub fn new(requested_model: &str, limit: Option<u64>) -> Self {
        Self {
            requested_model: requested_model.to_string(),
            limiter: OutputTokenLimiter::new(limit),
            buffer: Vec::new(),
            finished: false,
            last_response: None,
            upstream_model: None,
            strict_identity: false,
            allow_substitution: false,
            selector_kind: crate::model_contract::ModelSelectorKind::Unknown,
        }
    }

    /// Inspect a translated stream under the credential's served-model policy.
    #[must_use]
    pub const fn with_model_policy(
        mut self,
        policy: &crate::model_contract::ModelAccessPolicy,
    ) -> Self {
        // This method marks a translated boundary. Translation must never
        // invent identity, including for intentionally unpinned credentials.
        self.strict_identity = true;
        self.allow_substitution = policy.allow_substitution;
        self
    }

    /// Attach exact selector semantics obtained from authenticated catalog or
    /// explicit operator configuration.
    #[must_use]
    pub const fn with_selector_kind(
        mut self,
        selector_kind: crate::model_contract::ModelSelectorKind,
    ) -> Self {
        self.selector_kind = selector_kind;
        self
    }

    /// Whether the rewriter has to inspect the stream at all.
    #[must_use]
    pub const fn active(&self) -> bool {
        !self.requested_model.is_empty() || self.limiter.enabled() || self.strict_identity
    }

    /// The first concrete upstream model seen so far.
    #[must_use]
    pub fn upstream_model(&self) -> Option<&str> {
        self.upstream_model.as_deref()
    }

    /// Push raw upstream bytes and return the rewritten SSE text.
    pub fn push(&mut self, chunk: &[u8]) -> String {
        if self.finished {
            return String::new();
        }
        let mut out = String::new();
        for block in crate::sse::push_blocks(&mut self.buffer, chunk) {
            out.push_str(&self.rewrite_block(&block));
            if self.finished {
                self.buffer.clear();
                break;
            }
        }
        out
    }

    /// Finish a translated stream after transport EOF.
    ///
    /// Providers normally terminate with `[DONE]`, but a clean HTTP EOF can
    /// otherwise bypass the missing-identity check. Never let that transport
    /// detail turn an unverifiable translated response into apparent success.
    pub fn finish(&mut self) -> String {
        if self.finished {
            return String::new();
        }
        let mut output = if self.buffer.is_empty() {
            String::new()
        } else {
            let trailing = String::from_utf8_lossy(&self.buffer).into_owned();
            self.buffer.clear();
            self.rewrite_block(&trailing)
        };
        if self.finished {
            return output;
        }
        self.finished = true;
        if self.strict_identity && self.upstream_model.is_none() {
            output.push_str(&Self::identity_error_block(
                &crate::model_contract::validate_served_model_for_selector(
                    &self.requested_model,
                    None,
                    self.allow_substitution,
                    self.selector_kind,
                )
                .expect_err("missing identity must fail"),
            ));
        }
        output
    }

    fn rewrite_block(&mut self, block: &str) -> String {
        let Some(payload) = data_payload(block) else {
            return format!("{block}\n\n");
        };
        if payload == "[DONE]" {
            if self.strict_identity && self.upstream_model.is_none() {
                self.finished = true;
                return Self::identity_error_block(
                    &crate::model_contract::validate_served_model_for_selector(
                        &self.requested_model,
                        None,
                        self.allow_substitution,
                        self.selector_kind,
                    )
                    .expect_err("missing identity must fail"),
                );
            }
            self.finished = true;
            return format!("{block}\n\n");
        }
        let Ok(mut event) = serde_json::from_str::<Value>(&payload) else {
            return format!("{block}\n\n");
        };
        let original = event.clone();
        if let Some(served) = crate::model_contract::served_model_from(&event) {
            if let Some(first) = self.upstream_model.as_deref()
                && first != served
            {
                self.finished = true;
                return Self::identity_error_block(&crate::model_contract::ServedModelError {
                    code: "served_model_changed".to_string(),
                    requested_model: self.requested_model.clone(),
                    served_model: Some(served.to_string()),
                });
            }
            if self.strict_identity
                && let Err(error) = crate::model_contract::validate_served_model_for_selector(
                    &self.requested_model,
                    Some(served),
                    self.allow_substitution,
                    self.selector_kind,
                )
            {
                self.finished = true;
                return Self::identity_error_block(&error);
            }
            self.upstream_model = Some(served.to_string());
        } else if self.strict_identity && self.upstream_model.is_none() {
            if event_contains_assistant_content(&event) {
                self.finished = true;
                return Self::identity_error_block(
                    &crate::model_contract::validate_served_model_for_selector(
                        &self.requested_model,
                        None,
                        self.allow_substitution,
                        self.selector_kind,
                    )
                    .expect_err("missing identity must fail"),
                );
            }
            // A translated client must not receive a lifecycle object that
            // makes its translator emit the requested selector as though it
            // were served identity. Identity-free preamble is dispensable and
            // is withheld until an identity-bearing object arrives.
            let upstream_error = event
                .get("type")
                .and_then(Value::as_str)
                .is_some_and(|kind| matches!(kind, "error" | "response.failed"))
                || event.get("error").is_some()
                || event.pointer("/response/error").is_some();
            if !upstream_error {
                return String::new();
            }
            self.finished = true;
        }
        if event.get("type").and_then(Value::as_str) == Some("response.created")
            && let Some(response) = event.get("response")
        {
            self.last_response = Some(response.clone());
        }
        if event.get("type").and_then(Value::as_str) != Some("response.output_text.delta")
            || !self.limiter.enabled()
        {
            if event == original {
                return format!("{block}\n\n");
            }
            return render_block(block, &event);
        }
        let delta = event
            .get("delta")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string();
        let (visible, hit) = self.limiter.push(&delta);
        event["delta"] = Value::String(visible.clone());
        let mut out = String::new();
        if !visible.is_empty() {
            out.push_str(&render_block(block, &event));
        }
        if hit {
            self.finished = true;
            out.push_str(&self.incomplete_block());
            out.push_str("data: [DONE]\n\n");
        }
        out
    }

    fn incomplete_block(&self) -> String {
        let mut response = self
            .last_response
            .clone()
            .unwrap_or_else(|| json!({"id": "", "object": "response", "model": ""}));
        response["status"] = Value::String("incomplete".into());
        response["incomplete_details"] = json!({"reason": "max_output_tokens"});
        let event = json!({"type": "response.incomplete", "response": response});
        format!("event: response.incomplete\ndata: {event}\n\n")
    }

    fn identity_error_block(error: &crate::model_contract::ServedModelError) -> String {
        let event = json!({
            "type": "error",
            "error": {
                "type": error.code,
                "message": error.to_string(),
                "requested_model": error.requested_model,
                "served_model": error.served_model,
            }
        });
        format!("event: error\ndata: {event}\n\ndata: [DONE]\n\n")
    }
}

fn event_contains_assistant_content(event: &Value) -> bool {
    event
        .get("type")
        .and_then(Value::as_str)
        .is_some_and(|kind| {
            kind.contains("output_text.delta")
                || kind.contains("content_block_delta")
                || kind.contains("function_call_arguments.delta")
        })
        || event
            .get("choices")
            .and_then(Value::as_array)
            .is_some_and(|choices| !choices.is_empty())
        || event
            .get("candidates")
            .or_else(|| event.pointer("/response/candidates"))
            .and_then(Value::as_array)
            .is_some_and(|candidates| !candidates.is_empty())
}

fn data_payload(block: &str) -> Option<String> {
    let payload = block
        .lines()
        .filter_map(|line| {
            line.trim_end_matches('\r')
                .strip_prefix("data:")
                .map(str::trim_start)
        })
        .collect::<Vec<_>>()
        .join("\n");
    (!payload.is_empty()).then_some(payload)
}

/// Re-emit an SSE block with its `data:` payload replaced, keeping `event:`
/// and comment lines in their original order.
fn render_block(block: &str, event: &Value) -> String {
    use std::fmt::Write as _;
    let mut rendered = String::new();
    for line in block.lines() {
        let line = line.trim_end_matches('\r');
        if line.starts_with("data:") {
            continue;
        }
        rendered.push_str(line);
        rendered.push('\n');
    }
    let _ = write!(rendered, "data: {event}\n\n");
    rendered
}

#[cfg(test)]
#[path = "output_limit_tests.rs"]
mod tests;