ts_wire/request.rs
1// Copyright (c) 2026 LightSeek Foundation
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Requests from a frontend to the engine (`engine/io_struct.py`).
22
23use crate::{
24 codec::{Dict, StrOrList},
25 multimodal::MultimodalInputs,
26 sampling::SamplingParams,
27 value::Value,
28};
29
30wire_struct! {
31 /// Continual-prompting session of a request. Array layout, untagged.
32 #[derive(Debug, Clone, PartialEq, Eq)]
33 pub struct SessionParams: array + Default {
34 /// Session id.
35 pub id: Option<String> = None,
36 /// The request in the session to continue from.
37 pub rid: Option<String> = None,
38 /// Token offset to continue from.
39 pub offset: Option<i64> = None,
40 /// Replace the continued request instead of branching.
41 pub replace: Option<bool> = None,
42 }
43}
44
45wire_struct! {
46 /// A tokenized generate request, the payload of an ADD message.
47 ///
48 /// The first two fields come from the Python `BaseReq` base class.
49 #[derive(Debug, Clone, PartialEq)]
50 pub struct TokenizedGenerateReqInput: array("TokenizedGenerateReqInput") {
51 /// Request id. Generate requests always carry a single string.
52 pub rid: Option<StrOrList> = None,
53 /// In-process HTTP worker address; unused across processes.
54 pub http_worker_ipc: Option<String> = None,
55 /// Prompt text, when the frontend has it.
56 pub input_text: Option<String> = None,
57 /// Prompt token ids; `None` on the input-embeds path.
58 pub input_ids: Option<Vec<i32>> = None,
59 /// Sampling parameters.
60 pub sampling_params: SamplingParams,
61 /// Return the sampled tokens' logprobs.
62 pub return_logprob: bool = false,
63 /// First prompt position to return logprobs for; `-1` is the last
64 /// prompt token only.
65 pub logprob_start_len: i64 = -1,
66 /// Top-k alternatives per position.
67 pub top_logprobs_num: i64 = 0,
68 /// Token ids to score per position.
69 pub token_ids_logprob: Option<Vec<i32>> = None,
70 /// Stream outputs as they are generated.
71 pub stream: bool = false,
72 /// Prompt embeddings (nested float lists).
73 pub input_embeds: Option<Vec<Value>> = None,
74 /// Continual-prompting session.
75 pub session_params: Option<SessionParams> = None,
76 /// Serialized custom logit processor.
77 pub custom_logit_processor: Option<String> = None,
78 /// Return hidden states.
79 pub return_hidden_states: bool = false,
80 /// Creation time, in seconds.
81 pub created_time: f64 = 0.0,
82 /// PD: prefill bootstrap host.
83 pub bootstrap_host: Option<String> = None,
84 /// PD: prefill bootstrap port.
85 pub bootstrap_port: Option<i64> = None,
86 /// PD: transfer room id; frontends mint it as a random 64-bit value.
87 pub bootstrap_room: Option<u64> = None,
88 /// Additional token-id streams for multi-stream models.
89 pub input_multi_ids: Option<Vec<Vec<i32>>> = None,
90 /// Model-specific per-request inputs.
91 pub input_extra_infos: Option<Vec<Dict<Value, Value>>> = None,
92 /// Prompt ids before multimodal placeholder substitution.
93 pub input_ids_unpadded: Option<Vec<i32>> = None,
94 /// Multimodal payload.
95 pub multimodal_inputs: Option<MultimodalInputs> = None,
96 /// Set by a validating transport: the engine finishes the request
97 /// with an abort carrying this reason.
98 pub validation_error: Option<String> = None,
99 /// Pin to an attention-DP rank.
100 pub data_parallel_rank: Option<i64> = None,
101 }
102}
103
104impl TokenizedGenerateReqInput {
105 /// A request with every optional field at its schema default.
106 pub fn new(rid: impl Into<String>, sampling_params: SamplingParams) -> Self {
107 Self {
108 rid: Some(StrOrList::One(rid.into())),
109 http_worker_ipc: None,
110 input_text: None,
111 input_ids: None,
112 sampling_params,
113 return_logprob: false,
114 logprob_start_len: -1,
115 top_logprobs_num: 0,
116 token_ids_logprob: None,
117 stream: false,
118 input_embeds: None,
119 session_params: None,
120 custom_logit_processor: None,
121 return_hidden_states: false,
122 created_time: 0.0,
123 bootstrap_host: None,
124 bootstrap_port: None,
125 bootstrap_room: None,
126 input_multi_ids: None,
127 input_extra_infos: None,
128 input_ids_unpadded: None,
129 multimodal_inputs: None,
130 validation_error: None,
131 data_parallel_rank: None,
132 }
133 }
134}