tandem-core 0.4.16

Core types and helpers for the Tandem engine
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
use std::collections::HashSet;

use tandem_types::ToolSchema;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ToolIntent {
    Chitchat,
    Knowledge,
    WorkspaceRead,
    WorkspaceWrite,
    ShellExec,
    WebLookup,
    MemoryOps,
    McpExplicit,
}

#[derive(Debug, Clone)]
pub struct ToolRoutingDecision {
    pub pass: u8,
    pub mode: &'static str,
    pub intent: ToolIntent,
    pub selected_count: usize,
    pub total_available_count: usize,
    pub mcp_included: bool,
}

pub fn tool_router_enabled() -> bool {
    std::env::var("TANDEM_TOOL_ROUTER_ENABLED")
        .ok()
        .map(|raw| {
            matches!(
                raw.trim().to_ascii_lowercase().as_str(),
                "1" | "true" | "on" | "yes"
            )
        })
        .unwrap_or(false)
}

pub fn max_tools_per_call() -> usize {
    std::env::var("TANDEM_TOOL_ROUTER_MAX_TOOLS")
        .ok()
        .and_then(|raw| raw.trim().parse::<usize>().ok())
        .filter(|v| *v > 0)
        .unwrap_or(12)
}

pub fn max_tools_per_call_expanded() -> usize {
    std::env::var("TANDEM_TOOL_ROUTER_MAX_TOOLS_EXPANDED")
        .ok()
        .and_then(|raw| raw.trim().parse::<usize>().ok())
        .filter(|v| *v > 0)
        .unwrap_or(24)
}

pub fn is_short_simple_prompt(input: &str) -> bool {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        return false;
    }
    if trimmed.len() > 72 {
        return false;
    }
    let words = trimmed.split_whitespace().count();
    words > 0 && words <= 10
}

pub fn classify_intent(input: &str) -> ToolIntent {
    let lower = input.trim().to_ascii_lowercase();
    if lower.is_empty() {
        return ToolIntent::Knowledge;
    }

    if lower.contains("mcp")
        || lower.contains("arcade")
        || lower.contains("mcp.")
        || lower.contains("integration")
    {
        return ToolIntent::McpExplicit;
    }

    if contains_any(
        &lower,
        &[
            "memory_search",
            "memory_store",
            "memory_list",
            "remember",
            "memory",
        ],
    ) {
        return ToolIntent::MemoryOps;
    }

    if contains_any(
        &lower,
        &[
            "websearch",
            "web search",
            "search web",
            "internet",
            "online",
            "website",
            "url",
        ],
    ) {
        return ToolIntent::WebLookup;
    }

    if contains_any(
        &lower,
        &[
            "run ",
            "execute",
            "bash",
            "shell",
            "command",
            "terminal",
            "powershell",
            "cmd",
        ],
    ) {
        return ToolIntent::ShellExec;
    }

    if contains_any(
        &lower,
        &[
            "write",
            "edit",
            "patch",
            "modify",
            "update file",
            "create file",
            "refactor",
            "apply",
        ],
    ) {
        return ToolIntent::WorkspaceWrite;
    }

    if contains_any(
        &lower,
        &[
            "read",
            "open file",
            "search",
            "grep",
            "find in",
            "codebase",
            "repository",
            "repo",
            ".rs",
            ".ts",
            ".py",
            "/src",
            "file",
            "folder",
            "directory",
        ],
    ) {
        return ToolIntent::WorkspaceRead;
    }

    if is_chitchat_phrase(&lower) {
        return ToolIntent::Chitchat;
    }

    ToolIntent::Knowledge
}

pub fn should_escalate_auto_tools(
    intent: ToolIntent,
    user_text: &str,
    first_pass_completion: &str,
) -> bool {
    if matches!(
        intent,
        ToolIntent::WorkspaceRead
            | ToolIntent::WorkspaceWrite
            | ToolIntent::ShellExec
            | ToolIntent::WebLookup
            | ToolIntent::MemoryOps
            | ToolIntent::McpExplicit
    ) {
        return true;
    }

    let completion = first_pass_completion.to_ascii_lowercase();
    if contains_any(
        &completion,
        &[
            "need to inspect",
            "need to read",
            "need to check files",
            "cannot access local files",
            "use tools",
            "tool access",
            "need to run",
            "need to search",
        ],
    ) {
        return true;
    }

    let lower_user = user_text.to_ascii_lowercase();
    contains_any(
        &lower_user,
        &[
            " in engine/",
            " in src/",
            " in docs/",
            "from code",
            "local code",
        ],
    )
}

pub fn select_tool_subset(
    available: Vec<ToolSchema>,
    intent: ToolIntent,
    request_allowlist: &HashSet<String>,
    expanded: bool,
) -> Vec<ToolSchema> {
    let max_count = if expanded {
        max_tools_per_call_expanded()
    } else {
        max_tools_per_call()
    };

    let mut selected = Vec::new();
    let mut seen = HashSet::new();
    let include_mcp = intent == ToolIntent::McpExplicit;

    for schema in available {
        let norm = normalize_tool_name(&schema.name);
        let explicitly_allowed = !request_allowlist.is_empty() && request_allowlist.contains(&norm);
        if !request_allowlist.is_empty() && !explicitly_allowed {
            continue;
        }
        if !include_mcp && norm.starts_with("mcp.") && !explicitly_allowed {
            continue;
        }
        if !tool_matches_intent(intent, &norm) && !explicitly_allowed {
            continue;
        }
        if seen.insert(norm) {
            selected.push(schema);
            if selected.len() >= max_count {
                break;
            }
        }
    }

    selected
}

pub fn default_mode_name() -> &'static str {
    "auto"
}

fn tool_matches_intent(intent: ToolIntent, name: &str) -> bool {
    match intent {
        ToolIntent::Chitchat | ToolIntent::Knowledge => false,
        ToolIntent::WorkspaceRead => matches!(
            name,
            "glob"
                | "read"
                | "grep"
                | "search"
                | "codesearch"
                | "lsp"
                | "list"
                | "ls"
                | "webfetch"
                | "webfetch_html"
        ),
        ToolIntent::WorkspaceWrite => matches!(
            name,
            "glob"
                | "read"
                | "grep"
                | "search"
                | "codesearch"
                | "write"
                | "edit"
                | "apply_patch"
                | "bash"
                | "batch"
        ),
        ToolIntent::ShellExec => matches!(
            name,
            "bash" | "batch" | "glob" | "read" | "grep" | "search" | "codesearch"
        ),
        ToolIntent::WebLookup => matches!(
            name,
            "websearch" | "webfetch" | "webfetch_html" | "read" | "grep"
        ),
        ToolIntent::MemoryOps => matches!(name, "memory_search" | "memory_store" | "memory_list"),
        ToolIntent::McpExplicit => {
            name.starts_with("mcp.") || matches!(name, "read" | "grep" | "search")
        }
    }
}

fn contains_any(haystack: &str, needles: &[&str]) -> bool {
    needles.iter().any(|needle| haystack.contains(needle))
}

fn is_chitchat_phrase(input: &str) -> bool {
    let normalized = input
        .chars()
        .filter_map(|ch| {
            if ch.is_alphanumeric() || ch.is_whitespace() {
                Some(ch)
            } else {
                None
            }
        })
        .collect::<String>();
    let trimmed = normalized.trim();
    matches!(
        trimmed,
        "hi" | "hello"
            | "hey"
            | "thanks"
            | "thank you"
            | "ok"
            | "okay"
            | "yo"
            | "good morning"
            | "good afternoon"
            | "good evening"
    )
}

pub fn normalize_tool_name(name: &str) -> String {
    let lowered = name.trim().to_ascii_lowercase().replace('-', "_");
    let canonical = if let Some(stripped) = strip_known_namespace(&lowered) {
        stripped
    } else {
        lowered
    };
    match canonical.as_str() {
        "shell" | "powershell" | "cmd" | "run_command" => "bash".to_string(),
        "todowrite" | "update_todo_list" | "update_todos" => "todo_write".to_string(),
        other => other.to_string(),
    }
}

fn strip_known_namespace(name: &str) -> Option<String> {
    const PREFIXES: [&str; 8] = [
        "default_api:",
        "default_api.",
        "functions.",
        "function.",
        "tools.",
        "tool.",
        "builtin:",
        "builtin.",
    ];
    for prefix in PREFIXES {
        if let Some(rest) = name.strip_prefix(prefix) {
            let trimmed = rest.trim();
            if !trimmed.is_empty() {
                return Some(trimmed.to_string());
            }
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    fn schema(name: &str) -> ToolSchema {
        ToolSchema {
            name: name.to_string(),
            description: String::new(),
            input_schema: serde_json::json!({}),
        }
    }

    #[test]
    fn classifies_short_greeting_as_chitchat() {
        assert_eq!(classify_intent("hello"), ToolIntent::Chitchat);
    }

    #[test]
    fn classifies_repo_query_as_workspace_read() {
        assert_eq!(
            classify_intent("use local code evidence in engine/src/main.rs"),
            ToolIntent::WorkspaceRead
        );
    }

    #[test]
    fn allowlist_can_force_selection_even_when_intent_has_no_default_tools() {
        let mut allowlist = HashSet::new();
        allowlist.insert("read".to_string());
        let selected = select_tool_subset(
            vec![schema("read"), schema("bash")],
            ToolIntent::Knowledge,
            &allowlist,
            false,
        );
        assert_eq!(selected.len(), 1);
        assert_eq!(normalize_tool_name(&selected[0].name), "read");
    }

    #[test]
    fn mcp_tools_hidden_without_explicit_intent_or_allowlist() {
        let selected = select_tool_subset(
            vec![schema("mcp.arcade.gmail_create"), schema("read")],
            ToolIntent::WorkspaceRead,
            &HashSet::new(),
            false,
        );
        assert_eq!(selected.len(), 1);
        assert_eq!(normalize_tool_name(&selected[0].name), "read");
    }
}