promptforge-core 0.1.0

PromptForge runtime core: prompt parser, HTTP client, section execution
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! Gemma-3 `tool_code` fence dialect.
//!
//! Local Gemma models served via llama.cpp never emit a `message.tool_calls`
//! array. Instead they put a sole ` ```tool_code ` fence (Python-style call
//! syntax) in `message.content`. Results are echoed as a follow-up user turn
//! rather than `role=tool` messages, because Gemma chat templates reject the
//! OpenAI tool-result shape with HTTP 400.
//!
//! This dialect also handles the interim fenced-JSON `tool_calls` blob that
//! some Gemma quantizations emit. The moving parts live in focused child
//! modules: [`codec`] (the symmetric `tool_code` call grammar and its
//! adversarial tests), [`content`] (fence scanning and three-way
//! classification), and [`guide`] (the injected system guide).

mod codec;
mod content;
mod guide;

use serde_json::Value;

use crate::client::{Message, ToolCall};
use crate::normalize::NormalizedTurn;
use crate::{Error, Result};

use self::codec::render_tool_code_fence;
use self::content::{ContentParse, parse_content_tool_dialect};
use self::guide::render_tool_guide;
use super::{
    DetectScore, DialectEvidence, DialectRequest, FramedToolResult, ToolDialect, ToolDialectId,
    correlate_tool_results,
};

/// The Gemma-3 `tool_code` fence dialect.
///
/// - `detect`: never matches an endpoint with explicit native tool-call support
///   (`supports_tool_calls == Some(true)`); otherwise it scores a Gemma-
///   fingerprinted model (template, model-id, or source), adding weight for an
///   explicit `Some(false)`. Unknown capability plus a Gemma fingerprint still
///   scores, since Gemma via llama.cpp never emits native `tool_calls`.
/// - `prepare_request`: copies tool schemas into a system guide, then strips
///   `tools` / `tool_choice` (Gemma rejects the native OpenAI tool array).
/// - `parse_turn`: recognizes sole `tool_code` fences and fenced JSON
///   `tool_calls` blobs; mixed prose stays text.
/// - `echo_tool_results`: pushes the assistant's rendered `tool_code` fence
///   then a user message with `TOOL RESULT` blocks and a continue trailer.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Gemma3ToolCodeDialect;

impl ToolDialect for Gemma3ToolCodeDialect {
    fn id(&self) -> ToolDialectId {
        ToolDialectId::Gemma3ToolCode
    }

    fn detect(&self, evidence: &DialectEvidence) -> Option<DetectScore> {
        // Native tool-call endpoints are never this dialect.
        if evidence.supports_tool_calls == Some(true) {
            return None;
        }

        // `<bos>` alone is too common; require the Gemma turn marker.
        let gemma_template = evidence
            .chat_template
            .as_deref()
            .is_some_and(|template| template.contains("<start_of_turn>"));
        let gemma_model = evidence
            .model_id
            .as_deref()
            .is_some_and(|id| id.to_ascii_lowercase().contains("gemma"));
        let gemma_source = evidence
            .source
            .as_deref()
            .is_some_and(|src| src.to_ascii_lowercase().contains("gemma"));

        // Require a Gemma fingerprint so other tools-unsupported models (e.g.
        // some Qwen GGUFs) do not resolve here from caps alone.
        if !gemma_template && !gemma_model && !gemma_source {
            return None;
        }

        let mut score: u8 = 0;
        if evidence.supports_tool_calls == Some(false) {
            score += 40;
        }
        if gemma_template {
            score += 30;
        }
        if gemma_model {
            score += 20;
        }
        if gemma_source {
            score += 10;
        }
        Some(DetectScore(score))
    }

    /// Emulate tools: copy schemas into a leading system guide, then strip the
    /// native OpenAI `tools` / `tool_choice` fields Gemma rejects.
    ///
    /// The complete guide is validated and built *before* any mutation, so an
    /// invalid request shape (non-object body, non-array `messages` or `tools`)
    /// returns a preparation error instead of a silently half-mutated success.
    ///
    /// # Errors
    /// Returns [`Error::MalformedResponse`] when the request body is not a JSON
    /// object, or when `messages` or `tools` is present but not an array.
    fn prepare_request(&self, request: &mut DialectRequest<'_>) -> Result<()> {
        request.validate_shape()?;

        // Build the guide first; only a validated, fully-built guide is applied.
        let guide = match request.get("tools") {
            None | Some(Value::Null) => None,
            Some(Value::Array(tools)) => render_tool_guide(tools),
            Some(_) => {
                return Err(Error::MalformedResponse(
                    "request `tools` was present but not an array".into(),
                ));
            }
        };

        // Validation passed: mutate atomically.
        request.remove("tools")?;
        request.remove("tool_choice")?;
        if let Some(guide) = guide {
            request.prepend_message(serde_json::json!({
                "role": "system",
                "content": guide,
            }))?;
        }
        Ok(())
    }

    fn parse_turn(&self, body: &Value) -> Result<NormalizedTurn> {
        // Choice/message/finish_reason/reasoning extraction is shared with the
        // OpenAI normalizer; only the content-fence recognition below is
        // dialect-specific (PF-NORM-006).
        let crate::normalize::TurnContext {
            message,
            finish_reason,
            reasoning_content,
        } = crate::normalize::turn_context(body)?;

        // Gemma never emits wire tool_calls; go straight to content parsing.
        if let Some(content) = message
            .get("content")
            .and_then(Value::as_str)
            .filter(|content| !content.trim().is_empty())
        {
            // Three-way classification: recognized calls become a tool turn,
            // ordinary prose stays text, and a recognized-but-malformed tool
            // fence propagates as a concrete error instead of masquerading as
            // final text.
            match parse_content_tool_dialect(content) {
                ContentParse::Calls(calls) => {
                    return Ok(NormalizedTurn {
                        outcome: crate::client::CompletionResult::ToolCalls(calls),
                        finish_reason,
                        reasoning_content,
                    });
                }
                ContentParse::NotProtocol => {
                    return Ok(NormalizedTurn {
                        outcome: crate::client::CompletionResult::Text(content.to_string()),
                        finish_reason,
                        reasoning_content,
                    });
                }
                ContentParse::Malformed(error) => return Err(error),
            }
        }

        Err(crate::normalize::empty_reply_error(
            reasoning_content.is_some(),
        ))
    }

    /// Echo tool-call results in the Gemma content-fence style.
    ///
    /// Pushes an assistant message with the rendered `tool_code` fence, then a
    /// user message containing `TOOL RESULT` blocks and a continue trailer.
    ///
    /// # Errors
    /// Returns an error, leaving the conversation unmodified, when `calls` and
    /// `results` fail [`correlate_tool_results`]. Correlation is validated in
    /// every build (not just debug) before either message is appended, so a
    /// count, ordering, or id-correlation break can never silently truncate or
    /// mislabel a result via `zip`.
    fn echo_tool_results(
        &self,
        conversation: &mut Vec<Message>,
        calls: &[ToolCall],
        results: &[FramedToolResult],
    ) -> Result<()> {
        correlate_tool_results(calls, results)?;

        let fence = render_tool_code_fence(calls)?;
        conversation.push(Message::assistant(fence));

        // Each result content is already trust-framed by the executor; it is
        // embedded verbatim as opaque data. The continuation is tool-neutral and
        // sits outside the data blocks, so it never assumes a specific tool
        // (e.g. fetch) and cannot be read as part of a result's framed payload.
        let mut parts: Vec<String> = Vec::with_capacity(results.len());
        for (call, result) in calls.iter().zip(results.iter()) {
            parts.push(format!(
                "TOOL RESULT {} ({}):\n{}",
                call.name,
                result.id(),
                result.content()
            ));
        }
        let mut follow_up = parts.join("\n\n");
        follow_up.push_str(
            "\n\nContinue the protocol: emit another tool_code fence to call a tool, or write your final answer using only the tool results above.",
        );
        conversation.push(Message::user(follow_up));
        Ok(())
    }
}

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

    #[test]
    fn sole_tool_code_fence_becomes_tool_calls() {
        let dialect = Gemma3ToolCodeDialect;
        let body = serde_json::json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": "```tool_code\nsearch(query=\"C++ Alliance founder\")\n```"
                },
                "finish_reason": "stop"
            }]
        });
        let turn = dialect.parse_turn(&body).unwrap();
        match turn.outcome {
            CompletionResult::ToolCalls(calls) => {
                assert_eq!(calls.len(), 1);
                assert_eq!(calls[0].id, "call_tool_code_0");
                assert_eq!(calls[0].name, "search");
                assert_eq!(
                    calls[0].arguments,
                    serde_json::json!({ "query": "C++ Alliance founder" })
                );
            }
            CompletionResult::Text(text) => panic!("expected tool calls, got text: {text}"),
        }
    }

    #[test]
    fn positional_search_maps_to_query() {
        let dialect = Gemma3ToolCodeDialect;
        let body = serde_json::json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": "```tool_code\nsearch(\"C++ Alliance organization\")\n```"
                }
            }]
        });
        let turn = dialect.parse_turn(&body).unwrap();
        match turn.outcome {
            CompletionResult::ToolCalls(calls) => {
                assert_eq!(calls[0].name, "search");
                assert_eq!(
                    calls[0].arguments,
                    serde_json::json!({ "query": "C++ Alliance organization" })
                );
            }
            CompletionResult::Text(text) => panic!("expected tool calls, got text: {text}"),
        }
    }

    #[test]
    fn positional_fetch_maps_to_url() {
        let dialect = Gemma3ToolCodeDialect;
        let body = serde_json::json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": "```tool_code\nfetch(\"https://cppalliance.org\")\n```"
                }
            }]
        });
        let turn = dialect.parse_turn(&body).unwrap();
        match turn.outcome {
            CompletionResult::ToolCalls(calls) => {
                assert_eq!(calls[0].name, "fetch");
                assert_eq!(
                    calls[0].arguments,
                    serde_json::json!({ "url": "https://cppalliance.org" })
                );
            }
            CompletionResult::Text(text) => panic!("expected tool calls, got text: {text}"),
        }
    }

    #[test]
    fn mixed_prose_stays_text() {
        let dialect = Gemma3ToolCodeDialect;
        let content = "Here is evidence.\n\n```tool_code\nsearch(query=\"x\")\n```\n";
        let body = serde_json::json!({
            "choices": [{
                "message": { "role": "assistant", "content": content }
            }]
        });
        let turn = dialect.parse_turn(&body).unwrap();
        match turn.outcome {
            CompletionResult::Text(text) => assert_eq!(text, content),
            CompletionResult::ToolCalls(_) => panic!("mixed prose must stay text"),
        }
    }

    #[test]
    fn fenced_json_tool_calls_parsed() {
        let dialect = Gemma3ToolCodeDialect;
        let body = serde_json::json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": "```json\n{\"tool_calls\":[{\"id\":\"1\",\"type\":\"function\",\"function\":{\"name\":\"fetch\",\"arguments\":\"{\\\"url\\\":\\\"https://example.com\\\"}\"}}]}\n```"
                }
            }]
        });
        let turn = dialect.parse_turn(&body).unwrap();
        match turn.outcome {
            CompletionResult::ToolCalls(calls) => {
                assert_eq!(calls[0].name, "fetch");
                assert_eq!(
                    calls[0].arguments,
                    serde_json::json!({ "url": "https://example.com" })
                );
            }
            CompletionResult::Text(text) => panic!("expected tool calls, got text: {text}"),
        }
    }

    #[test]
    fn fenced_json_with_malformed_arguments_is_malformed_not_text() {
        // A fenced tool_calls blob (protocol intent) whose arguments are invalid
        // must surface as a malformed error, preserving the concrete decode
        // failure, rather than silently falling back to final text.
        let dialect = Gemma3ToolCodeDialect;
        let body = serde_json::json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": "```json\n{\"tool_calls\":[{\"id\":\"1\",\"type\":\"function\",\"function\":{\"name\":\"fetch\",\"arguments\":\"not json\"}}]}\n```"
                }
            }]
        });
        assert!(
            matches!(dialect.parse_turn(&body), Err(Error::MalformedResponse(_))),
            "malformed fenced arguments must be a malformed error, not text"
        );
    }

    #[test]
    fn malformed_tool_code_fence_is_malformed_not_text() {
        let dialect = Gemma3ToolCodeDialect;
        // Leading tool_code fence commits to protocol; a bad call line is an error.
        let bad_call = serde_json::json!({
            "choices": [{ "message": { "content": "```tool_code\nnot a call\n```" } }]
        });
        assert!(matches!(
            dialect.parse_turn(&bad_call),
            Err(Error::MalformedResponse(_))
        ));

        // Unterminated fence is malformed, not text.
        let unterminated = serde_json::json!({
            "choices": [{ "message": { "content": "```tool_code\nsearch(query=\"a\")" } }]
        });
        assert!(matches!(
            dialect.parse_turn(&unterminated),
            Err(Error::MalformedResponse(_))
        ));

        // Trailing prose after a valid fence in a protocol turn is malformed.
        let trailing = serde_json::json!({
            "choices": [{ "message": { "content": "```tool_code\nsearch(query=\"a\")\n```\nthen prose" } }]
        });
        assert!(matches!(
            dialect.parse_turn(&trailing),
            Err(Error::MalformedResponse(_))
        ));
    }

    #[test]
    fn backticks_inside_quoted_value_do_not_close_fence() {
        // A ``` inside a quoted argument value must not terminate the fence at
        // the first triple-backtick; only a standalone ``` line closes it.
        let dialect = Gemma3ToolCodeDialect;
        let body = serde_json::json!({
            "choices": [{ "message": { "content": "```tool_code\necho(value=\"a ``` b\")\n```" } }]
        });
        let turn = dialect.parse_turn(&body).unwrap();
        match turn.outcome {
            CompletionResult::ToolCalls(calls) => {
                assert_eq!(calls.len(), 1);
                assert_eq!(
                    calls[0].arguments,
                    serde_json::json!({ "value": "a ``` b" })
                );
            }
            CompletionResult::Text(text) => panic!("expected tool calls, got text: {text}"),
        }
    }

    #[test]
    fn ordinary_json_data_fence_stays_text() {
        // A ```json code block that is not a tool_calls blob is ordinary data.
        let dialect = Gemma3ToolCodeDialect;
        let content = "```json\n{\"answer\": 42}\n```";
        let body = serde_json::json!({
            "choices": [{ "message": { "content": content } }]
        });
        let turn = dialect.parse_turn(&body).unwrap();
        assert!(matches!(turn.outcome, CompletionResult::Text(_)));
    }

    #[test]
    fn prepare_request_strips_tools_and_injects_guide() {
        let dialect = Gemma3ToolCodeDialect;
        let mut body = serde_json::json!({
            "model": "gemma-3",
            "messages": [{"role": "user", "content": "brief me"}],
            "tools": [{
                "type": "function",
                "function": {
                    "name": "search",
                    "description": "Web search",
                    "parameters": {
                        "type": "object",
                        "properties": { "query": { "type": "string" } },
                        "required": ["query"]
                    }
                }
            }],
            "tool_choice": "auto"
        });
        let mut req = DialectRequest::new(&mut body);
        dialect.prepare_request(&mut req).unwrap();
        assert!(body.get("tools").is_none());
        assert!(body.get("tool_choice").is_none());
        assert_eq!(body["model"], "gemma-3");
        let messages = body["messages"].as_array().unwrap();
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0]["role"], "system");
        let guide = messages[0]["content"].as_str().unwrap();
        assert!(guide.contains("search(query=...)"));
        assert!(guide.contains("```tool_code"));
        assert_eq!(messages[1]["role"], "user");
    }

    #[test]
    fn prepare_request_rejects_invalid_shapes() {
        let dialect = Gemma3ToolCodeDialect;

        // Non-object body.
        let mut body = serde_json::json!([1, 2, 3]);
        let mut req = DialectRequest::new(&mut body);
        assert!(dialect.prepare_request(&mut req).is_err());

        // Non-array messages.
        let mut body = serde_json::json!({ "messages": "oops", "tools": [] });
        let mut req = DialectRequest::new(&mut body);
        assert!(dialect.prepare_request(&mut req).is_err());
        // Body must be untouched on failure (validated before mutating).
        assert!(body.get("tools").is_some(), "no mutation on rejected shape");

        // Non-array tools.
        let mut body = serde_json::json!({ "messages": [], "tools": {} });
        let mut req = DialectRequest::new(&mut body);
        assert!(dialect.prepare_request(&mut req).is_err());
        assert!(body.get("tools").is_some(), "no mutation on rejected shape");
    }

    #[test]
    fn detect_gemma_props_scores() {
        let dialect = Gemma3ToolCodeDialect;
        let evidence = DialectEvidence {
            supports_tool_calls: Some(false),
            chat_template: Some("<start_of_turn>user\n".to_string()),
            model_id: Some("gemma-3-27b-it".to_string()),
            ..Default::default()
        };
        let score = dialect.detect(&evidence).expect("should score");
        assert!(score.0 >= 80, "expected high score, got {}", score.0);
    }

    #[test]
    fn detect_with_native_tools_returns_none() {
        let dialect = Gemma3ToolCodeDialect;
        let evidence = DialectEvidence {
            supports_tool_calls: Some(true),
            chat_template: Some("<start_of_turn>user\n".to_string()),
            model_id: Some("gemma-3-27b-it".to_string()),
            ..Default::default()
        };
        assert!(dialect.detect(&evidence).is_none());
    }

    #[test]
    fn detect_tools_false_without_gemma_fingerprint_returns_none() {
        let dialect = Gemma3ToolCodeDialect;
        let evidence = DialectEvidence {
            supports_tool_calls: Some(false),
            chat_template: Some("{{ messages }}".to_string()),
            model_id: Some("qwen3-0.6b".to_string()),
            ..Default::default()
        };
        assert!(dialect.detect(&evidence).is_none());
    }

    #[test]
    fn consecutive_fences_mint_unique_ids() {
        let dialect = Gemma3ToolCodeDialect;
        let content =
            "```tool_code\nsearch(query=\"a\")\n```\n\n```tool_code\nfetch(\"https://x\")\n```";
        let body = serde_json::json!({
            "choices": [{ "message": { "role": "assistant", "content": content } }]
        });
        let turn = dialect.parse_turn(&body).unwrap();
        match turn.outcome {
            CompletionResult::ToolCalls(calls) => {
                assert_eq!(calls.len(), 2);
                assert_eq!(calls[0].id, "call_tool_code_0");
                assert_eq!(
                    calls[1].id, "call_tool_code_1",
                    "ids must not restart per fence"
                );
            }
            CompletionResult::Text(text) => panic!("expected tool calls, got text: {text}"),
        }
    }

    #[test]
    fn echo_rejects_uncorrelated_results() {
        let dialect = Gemma3ToolCodeDialect;
        let calls = vec![ToolCall {
            id: "call_tool_code_0".into(),
            name: "search".into(),
            arguments: serde_json::json!({ "query": "rust" }),
        }];
        // Wrong id breaks correlation; the conversation must stay untouched.
        let results = vec![FramedToolResult::new("mismatched_id".into(), "text".into())];
        let mut conversation = Vec::new();
        let error = dialect
            .echo_tool_results(&mut conversation, &calls, &results)
            .expect_err("uncorrelated results must be rejected");
        assert!(matches!(error, Error::Internal(_)));
        assert!(
            conversation.is_empty(),
            "no message may be appended on failure"
        );
    }

    #[test]
    fn optional_params_survive_in_guide() {
        let dialect = Gemma3ToolCodeDialect;
        let mut body = serde_json::json!({
            "model": "gemma-3",
            "messages": [{"role": "user", "content": "hi"}],
            "tools": [{
                "type": "function",
                "function": {
                    "name": "search",
                    "parameters": {
                        "type": "object",
                        "properties": { "query": { "type": "string" }, "count": { "type": "number" } },
                        "required": ["query"]
                    }
                }
            }]
        });
        let mut req = DialectRequest::new(&mut body);
        dialect.prepare_request(&mut req).unwrap();
        let guide = body["messages"][0]["content"].as_str().unwrap();
        assert!(
            guide.contains("query=..."),
            "required param present: {guide}"
        );
        assert!(
            guide.contains("count=...?"),
            "optional param must survive marked: {guide}"
        );
    }

    #[test]
    fn echo_tool_results_produces_user_turn() {
        let dialect = Gemma3ToolCodeDialect;
        let calls = vec![ToolCall {
            id: "call_tool_code_0".into(),
            name: "search".into(),
            arguments: serde_json::json!({"query": "rust"}),
        }];
        let results = vec![FramedToolResult::new(
            "call_tool_code_0".into(),
            "result text".into(),
        )];
        let mut conversation = Vec::new();
        dialect
            .echo_tool_results(&mut conversation, &calls, &results)
            .expect("correlated results echo cleanly");

        assert_eq!(conversation.len(), 2);
        assert_eq!(conversation[0].role, "assistant");
        assert!(conversation[0].content.contains("```tool_code"));
        assert!(conversation[0].content.contains("search("));
        assert_eq!(conversation[1].role, "user");
        assert!(
            conversation[1]
                .content
                .contains("TOOL RESULT search (call_tool_code_0):")
        );
        assert!(conversation[1].content.contains("result text"));
        assert!(conversation[1].content.contains("Continue the protocol"));
    }
}