strata-ctl: Rust control layer for the Strata engine (OpenAI-compatible API)
From-scratch rewrite of the Python serve (Python kept as behavioral spec). - engine: line-protocol supervisor (GEN/T/DONE/STOP/QUIT), silence watchdog, abort, is_idle probe. - config: tolerant shared-JSON loader; foreign keys ignored so one file drives strata-ctl and uostream; CLI --config/--host/--port/--serve. - inkling: o200k tokenizer wrapper + incremental UTF-8-safe detok; chat template via minijinja; channel-token dialect (reasoning streamed live); image bridge (in-process Vision, image=<path>); formatter assembling the API seam. - api: /v1/chat/completions (stream SSE + non-stream), /v1/models, /health; reasoning_content; structured tool_calls (llama.cpp --jinja style, client-side execution); stop sequences with hold-back; abort on client disconnect. 69 tests green + 2 real-file ignored; clippy clean. Text, images and tools validated live against the real Inkling engine.
This commit is contained in:
commit
597ac432e1
30 files changed
+7541
No files matched your search
@@ -0,0 +1,111 @@
|
||||
//! Tests for the Inkling chat-template layer: the custom tojson/raise_exception
|
||||
//! and generation-strip (deterministic, inline template), prepare_messages /
|
||||
//! finish_prompt (pure), and an #[ignore] render of the real chat_template.jinja.
|
||||
|
||||
use serde_json::json;
|
||||
use strata_ctl::inkling::dialect::tag;
|
||||
use strata_ctl::inkling::template::{finish_prompt, prepare_messages, ChatTemplate};
|
||||
|
||||
#[test]
|
||||
fn tojson_compact_and_sorted() {
|
||||
let t =
|
||||
ChatTemplate::new("{{ {'b': 1, 'a': 2} | tojson(sort_keys=true, separators=(',', ':')) }}")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
t.render(&json!([]), None, None).unwrap(),
|
||||
"{\"a\":2,\"b\":1}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generation_blocks_are_transparent() {
|
||||
let t = ChatTemplate::new("A{% generation %}B{% endgeneration %}C").unwrap();
|
||||
assert_eq!(t.render(&json!([]), None, None).unwrap(), "ABC");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn namespace_and_dictsort_work() {
|
||||
let src = "{%- set s = namespace(x=[]) -%}\
|
||||
{%- set s.x = s.x + [1, 2] -%}\
|
||||
{{ s.x | tojson(separators=(',', ':')) }}|\
|
||||
{%- for k, v in {'b': 1, 'a': 2} | dictsort(case_sensitive=true) -%}\
|
||||
{{ k }}={{ v }};{%- endfor -%}";
|
||||
let t = ChatTemplate::new(src).unwrap();
|
||||
assert_eq!(t.render(&json!([]), None, None).unwrap(), "[1,2]|a=2;b=1;");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raise_exception_fails_render() {
|
||||
let t = ChatTemplate::new("{{ raise_exception('boom') }}").unwrap();
|
||||
let err = t.render(&json!([]), None, None).unwrap_err();
|
||||
assert!(err.to_string().contains("boom"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepare_messages_parses_string_arguments() {
|
||||
let mut msgs = json!([
|
||||
{"role": "assistant", "tool_calls": [
|
||||
{"id": "c1", "type": "function",
|
||||
"function": {"name": "f", "arguments": "{\"a\": 1}"}}
|
||||
]},
|
||||
{"role": "assistant", "tool_calls": [
|
||||
{"id": "c2", "function": {"name": "g", "arguments": null}}
|
||||
]}
|
||||
]);
|
||||
prepare_messages(&mut msgs);
|
||||
assert_eq!(
|
||||
msgs[0]["tool_calls"][0]["function"]["arguments"],
|
||||
json!({"a": 1})
|
||||
);
|
||||
assert_eq!(msgs[1]["tool_calls"][0]["function"]["arguments"], json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finish_prompt_only_for_effort_zero() {
|
||||
// positive effort: untouched (model opens its own thinking)
|
||||
let p = "<|message_system|><|content_text|>Thinking effort level: 0.2<|end_message|>";
|
||||
assert_eq!(finish_prompt(p, false, false), p);
|
||||
|
||||
// effort zero, no opener yet: append the text opener (no tools)
|
||||
let z = "<|message_system|><|content_text|>Thinking effort level: 0<|end_message|>";
|
||||
let expect = format!("{z}{}{}", tag::MESSAGE_MODEL, tag::CONTENT_TEXT);
|
||||
assert_eq!(finish_prompt(z, false, false), expect);
|
||||
|
||||
// with tools: the empty-thinking opener
|
||||
let expect_tools = format!(
|
||||
"{z}{}{}{}",
|
||||
tag::MESSAGE_MODEL,
|
||||
tag::CONTENT_THINKING,
|
||||
tag::END_MESSAGE
|
||||
);
|
||||
assert_eq!(finish_prompt(z, true, false), expect_tools);
|
||||
|
||||
// idempotent: already ends with the text opener
|
||||
assert_eq!(finish_prompt(&expect, false, false), expect);
|
||||
}
|
||||
|
||||
/// Render the REAL template (host file). Run with:
|
||||
/// STRATA_TOK_DIR=~/.config/strata-inkling/inkling-tok cargo test -- --ignored
|
||||
#[test]
|
||||
#[ignore = "needs the real Inkling chat_template.jinja"]
|
||||
fn real_template_renders_a_turn() {
|
||||
let dir = std::env::var("STRATA_TOK_DIR")
|
||||
.unwrap_or_else(|_| "/home/federico/.config/strata-inkling/inkling-tok".to_string());
|
||||
let src = std::fs::read_to_string(format!("{dir}/chat_template.jinja")).unwrap();
|
||||
let t = ChatTemplate::new(&src).unwrap();
|
||||
|
||||
let msgs = json!([{"role": "user", "content": "ciao"}]);
|
||||
let out = t.render(&msgs, None, Some("low")).unwrap();
|
||||
assert!(out.contains("Thinking effort level: 0.2"), "{out}");
|
||||
assert!(
|
||||
out.contains("<|message_user|><|content_text|>ciao<|end_message|>"),
|
||||
"{out}"
|
||||
);
|
||||
|
||||
// effort none renders level 0
|
||||
let out0 = t.render(&msgs, None, Some("none")).unwrap();
|
||||
assert!(
|
||||
out0.contains("Thinking effort level: 0<|end_message|>"),
|
||||
"{out0}"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user