rigspark-runtime 3.0.7

Hardware detection, backend adapters, lifecycle, memory and harness runtime for rigspark.
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
use crate::{
    http::{HttpError, Request, Transport, read_json},
    identity::{ProcessIdentity, ProcessProbe},
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::{collections::BTreeMap, time::Duration};
use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader};
use tokio_util::sync::CancellationToken;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ToolCall {
    pub name: String,
    #[serde(default)]
    pub arguments: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ChatMessage {
    pub role: String,
    pub content: String,
    #[serde(default)]
    pub tool_calls: Vec<ToolCall>,
    pub tool_name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Tool {
    pub name: String,
    pub description: String,
    pub parameters: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ChatInput {
    pub model: String,
    pub messages: Vec<ChatMessage>,
    #[serde(default)]
    pub tools: Vec<Tool>,
    pub temperature: Option<f64>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ChatResult {
    pub content: String,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tool_calls: Vec<ToolCall>,
}
#[derive(Debug, Serialize)]
pub struct EmbedResult {
    pub vectors: Vec<Vec<f64>>,
    pub dimension: usize,
}
fn model_id(value: &str) -> Result<(), HttpError> {
    if value.is_empty()
        || value.len() > 8192
        || !value.as_bytes()[0].is_ascii_lowercase() && !value.as_bytes()[0].is_ascii_digit()
        || !value.bytes().all(|byte| {
            byte.is_ascii_lowercase() || byte.is_ascii_digit() || b"._:/-".contains(&byte)
        })
        || value.split('/').any(|part| part == "..")
    {
        return Err(HttpError::Invalid);
    }
    Ok(())
}
fn chat_body(input: &ChatInput, stream: bool) -> Result<Value, HttpError> {
    model_id(&input.model)?;
    if input.messages.len() > 10000
        || input.tools.len() > 1024
        || input.temperature.is_some_and(|value| !value.is_finite())
    {
        return Err(HttpError::Invalid);
    }
    let mut messages = Vec::new();
    for message in &input.messages {
        if !["system", "user", "assistant", "tool"].contains(&message.role.as_str()) {
            return Err(HttpError::Invalid);
        }
        let mut value = json!({"role":message.role,"content":message.content});
        if !message.tool_calls.is_empty() {
            value["tool_calls"] = json!(
                message
                    .tool_calls
                    .iter()
                    .map(|call| json!({"function":{"name":call.name,"arguments":call.arguments}}))
                    .collect::<Vec<_>>()
            );
        }
        if let Some(name) = &message.tool_name {
            value["tool_name"] = json!(name);
        }
        messages.push(value);
    }
    let mut body = json!({"model":input.model,"messages":messages,"stream":stream});
    if !input.tools.is_empty() {
        body["tools"] = json!(
            input
                .tools
                .iter()
                .map(|tool| json!({"type":"function","function":tool}))
                .collect::<Vec<_>>()
        );
    }
    if let Some(temperature) = input.temperature {
        body["options"] = json!({"temperature":temperature});
    }
    Ok(body)
}
/// Ollama's wire shape: newer releases add `id` and `function.index`, which are ignored.
#[derive(Deserialize)]
struct NativeFunction {
    name: String,
    #[serde(default)]
    arguments: BTreeMap<String, Value>,
}
#[derive(Deserialize)]
struct NativeCall {
    function: NativeFunction,
}
impl From<NativeCall> for ToolCall {
    fn from(call: NativeCall) -> Self {
        Self {
            name: call.function.name,
            arguments: call.function.arguments,
        }
    }
}
#[derive(Deserialize)]
struct NativeMessage {
    #[serde(default)]
    content: String,
    #[serde(default)]
    tool_calls: Vec<NativeCall>,
}
#[derive(Deserialize)]
struct NativeChunk {
    message: Option<NativeMessage>,
    #[serde(rename = "done")]
    _done: Option<bool>,
}
fn decode_result(value: Value) -> Result<ChatResult, HttpError> {
    crate::usage::record(&value, crate::usage::Provider::Ollama);
    #[derive(Deserialize)]
    struct Reply {
        message: NativeMessage,
    }
    if value.get("error").is_some()
        || !value
            .get("message")
            .and_then(|message| message.get("content"))
            .is_some_and(Value::is_string)
    {
        return Err(HttpError::Response);
    }
    let response: Reply = serde_json::from_value(value).map_err(|_| HttpError::Response)?;
    Ok(ChatResult {
        content: response.message.content,
        tool_calls: response
            .message
            .tool_calls
            .into_iter()
            .map(ToolCall::from)
            .collect(),
    })
}
pub struct OllamaInference<'runtime> {
    transport: &'runtime dyn Transport,
    probe: &'runtime dyn ProcessProbe,
    executable: &'runtime str,
}
impl<'runtime> OllamaInference<'runtime> {
    pub fn new(
        transport: &'runtime dyn Transport,
        probe: &'runtime dyn ProcessProbe,
        executable: &'runtime str,
    ) -> Self {
        Self {
            transport,
            probe,
            executable,
        }
    }
    async fn check(
        &self,
        endpoint: &str,
        expected: &ProcessIdentity,
        cancel: &CancellationToken,
    ) -> Result<(), HttpError> {
        let url = crate::state::loopback(endpoint).map_err(|_| HttpError::Invalid)?;
        if expected.pid == 0
            || expected.executable != self.executable
            || expected.started.is_empty()
            || !std::path::Path::new(self.executable).is_absolute()
        {
            return Err(HttpError::Invalid);
        }
        let port = url.port_or_known_default().ok_or(HttpError::Invalid)?;
        let host = url.host_str().ok_or(HttpError::Invalid)?;
        let listener = crate::process_control::listener(self.probe, endpoint, cancel)
            .await
            .map_err(|_| {
                if cancel.is_cancelled() {
                    HttpError::Cancelled
                } else {
                    HttpError::Invalid
                }
            })?;
        crate::identity::choose_listener(std::slice::from_ref(&listener), port, host)
            .map_err(|_| HttpError::Invalid)?;
        if listener.identity.pid != expected.pid
            || listener.identity.executable != expected.executable
            || listener.identity.started != expected.started
        {
            return Err(HttpError::Invalid);
        }
        Ok(())
    }
    async fn trusted(
        &self,
        endpoint: &str,
        expected: &ProcessIdentity,
        cancel: &CancellationToken,
    ) -> Result<(), HttpError> {
        if cancel.is_cancelled() {
            return Err(HttpError::Cancelled);
        }
        self.check(endpoint, expected, cancel).await?;
        let version = read_json(
            self.transport,
            Request::new(endpoint, "/api/version", None, None)?,
            cancel,
            4096,
        )
        .await?;
        if !version
            .get("version")
            .and_then(Value::as_str)
            .is_some_and(|value| {
                !value.is_empty()
                    && value.len() <= 100
                    && value.bytes().all(|byte| !byte.is_ascii_control())
            })
        {
            return Err(HttpError::Response);
        }
        self.check(endpoint, expected, cancel).await
    }
    pub async fn chat(
        &self,
        endpoint: &str,
        expected: &ProcessIdentity,
        input: &ChatInput,
        cancel: &CancellationToken,
    ) -> Result<ChatResult, HttpError> {
        let request = Request::new(endpoint, "/api/chat", Some(chat_body(input, false)?), None)?;
        crate::usage::begin();
        self.trusted(endpoint, expected, cancel).await?;
        let result =
            decode_result(read_json(self.transport, request, cancel, 16 * 1024 * 1024).await?)?;
        self.check(endpoint, expected, cancel).await?;
        Ok(result)
    }
    pub async fn chat_stream(
        &self,
        endpoint: &str,
        expected: &ProcessIdentity,
        input: &ChatInput,
        cancel: &CancellationToken,
        on_delta: &mut (dyn FnMut(&str) + Send),
    ) -> Result<ChatResult, HttpError> {
        let request = Request::new(endpoint, "/api/chat", Some(chat_body(input, true)?), None)?;
        crate::usage::begin();
        self.trusted(endpoint, expected, cancel).await?;
        let operation = async {
            let response = self.transport.send(request).await?;
            if !(200..300).contains(&response.status) {
                return Err(HttpError::Status(response.status));
            }
            let mut reader = BufReader::new(response.body);
            let mut result = ChatResult {
                content: String::new(),
                tool_calls: Vec::new(),
            };
            let mut total = 0usize;
            loop {
                let mut line = Vec::new();
                let count = (&mut reader)
                    .take(65537)
                    .read_until(b'\n', &mut line)
                    .await
                    .map_err(|_| HttpError::Transport)?;
                if count == 0 {
                    break;
                }
                total += count;
                if count > 65536 || total > 16 * 1024 * 1024 {
                    return Err(HttpError::Limit);
                }
                let terminated = line.last() == Some(&b'\n');
                let records: Vec<_> = line
                    .split(|byte| *byte == b'\r' || *byte == b'\n')
                    .collect();
                for record in records
                    .iter()
                    .take(records.len().saturating_sub(usize::from(!terminated)))
                {
                    let Ok(chunk) = serde_json::from_slice::<NativeChunk>(record) else {
                        continue;
                    };
                    if let Ok(value) = serde_json::from_slice::<Value>(record) {
                        crate::usage::record(&value, crate::usage::Provider::Ollama);
                    }
                    if let Some(message) = chunk.message {
                        if !message.content.is_empty() {
                            on_delta(&message.content);
                            result.content.push_str(&message.content);
                        }
                        result
                            .tool_calls
                            .extend(message.tool_calls.into_iter().map(ToolCall::from));
                    }
                }
            }
            Ok(result)
        };
        let result = tokio::select! {biased;_=cancel.cancelled()=>Err(HttpError::Cancelled),result=tokio::time::timeout(Duration::from_secs(1800),operation)=>result.map_err(|_|HttpError::Timeout)?}?;
        self.check(endpoint, expected, cancel).await?;
        Ok(result)
    }
    pub async fn embed(
        &self,
        endpoint: &str,
        expected: &ProcessIdentity,
        model: &str,
        inputs: &[String],
        cancel: &CancellationToken,
    ) -> Result<EmbedResult, HttpError> {
        model_id(model)?;
        if inputs.is_empty()
            || inputs.len() > 1024
            || inputs
                .iter()
                .any(|input| input.is_empty() || input.len() > 1024 * 1024)
            || inputs.iter().map(String::len).sum::<usize>() > 4 * 1024 * 1024
        {
            return Err(HttpError::Invalid);
        }
        let request = Request::new(
            endpoint,
            "/api/embed",
            Some(json!({"model":model,"input":inputs})),
            None,
        )?;
        self.trusted(endpoint, expected, cancel).await?;
        let value = tokio::time::timeout(
            Duration::from_secs(30),
            read_json(self.transport, request, cancel, 16 * 1024 * 1024),
        )
        .await
        .map_err(|_| HttpError::Timeout)??;
        #[derive(Deserialize)]
        struct Embeddings {
            embeddings: Vec<Vec<f64>>,
        }
        let vectors = serde_json::from_value::<Embeddings>(value)
            .map_err(|_| HttpError::Response)?
            .embeddings;
        let dimension = vectors.first().map(Vec::len).ok_or(HttpError::Response)?;
        if vectors.len() != inputs.len()
            || dimension == 0
            || dimension > 8192
            || vectors.len() * dimension > 1000000
            || vectors.iter().any(|vector| {
                vector.len() != dimension || vector.iter().any(|value| !value.is_finite())
            })
        {
            return Err(HttpError::Response);
        }
        self.check(endpoint, expected, cancel).await?;
        Ok(EmbedResult { vectors, dimension })
    }
}

/// Loads `model` in Ollama with a one-token generation so a runtime that cannot run it
/// (for example an unknown architecture) fails now, with Ollama's reason, rather than on
/// the first chat. The reason is sanitized and bounded.
pub async fn preload(
    transport: &dyn Transport,
    endpoint: &str,
    model: &str,
    cancel: &CancellationToken,
) -> Result<(), String> {
    let request = Request::new(
        endpoint,
        "/api/generate",
        Some(serde_json::json!({
            "model": model,
            "prompt": "Hi",
            "stream": false,
            "options": {"num_predict": 1},
            "keep_alive": "30s",
        })),
        None,
    )
    .map_err(|error| error.to_string())?;
    let operation = async {
        let response = transport
            .send(request)
            .await
            .map_err(|error| error.to_string())?;
        let mut bytes = Vec::new();
        response
            .body
            .take(64 * 1024)
            .read_to_end(&mut bytes)
            .await
            .map_err(|_| "unreadable Ollama response".to_owned())?;
        if (200..300).contains(&response.status) {
            return Ok(());
        }
        let reason = serde_json::from_slice::<Value>(&bytes)
            .ok()
            .and_then(|value| {
                value
                    .get("error")
                    .and_then(Value::as_str)
                    .map(str::to_owned)
            })
            .map(|text| {
                rigspark_core::reports::strip_control(&text)
                    .chars()
                    .take(400)
                    .collect::<String>()
            })
            .filter(|text| !text.trim().is_empty())
            .unwrap_or_else(|| format!("Ollama returned HTTP {}", response.status));
        Err(reason)
    };
    tokio::select! {biased;_=cancel.cancelled()=>Err("cancelled".into()),result=tokio::time::timeout(Duration::from_secs(600),operation)=>result.map_err(|_|"loading the model timed out".to_owned())?}
}