rigg-core 0.17.0

Core resource types, configuration, and JSON normalization for rigg
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
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
//! Foundry Agent resource — YAML on-disk format
//!
//! Foundry agents are stored as a single YAML file per agent:
//! `agents/<agent-name>.yaml`
//!
//! The YAML format matches the Foundry portal's YAML view. Agent name is
//! derived from the filename (not stored in the YAML).

use serde_json::Value;

/// Fields to strip from agent API responses (transient/server-managed)
const VOLATILE_FIELDS: &[&str] = &["id", "created_at", "object", "version"];

/// Fields excluded from YAML (volatile + identity fields managed externally)
const YAML_EXCLUDE_FIELDS: &[&str] = &["name", "id", "created_at", "object", "version"];

/// Field order for YAML output (matches Foundry portal).
const AGENT_YAML_FIELD_ORDER: &[&str] = &[
    "kind",
    "model",
    "description",
    "temperature",
    "top_p",
    "response_format",
    "metadata",
    "instructions",
    "tools",
    "tool_resources",
];

/// Convert a flattened agent JSON value to ordered YAML string.
///
/// Strips identity/volatile fields (`name`, `id`, `created_at`, `object`, `version`),
/// omits empty optional fields, and orders remaining fields per `AGENT_YAML_FIELD_ORDER`.
pub fn agent_to_yaml(agent: &Value) -> String {
    let obj = agent.as_object().cloned().unwrap_or_default();

    // Build ordered map
    let mut ordered = serde_json::Map::new();

    // First: known fields in display order
    for &field in AGENT_YAML_FIELD_ORDER {
        if let Some(value) = obj.get(field) {
            // Skip empty/null optional fields
            if should_omit(field, value) {
                continue;
            }
            ordered.insert(field.to_string(), value.clone());
        }
    }

    // Then: any remaining fields not in the known list and not excluded
    for (key, value) in &obj {
        if YAML_EXCLUDE_FIELDS.contains(&key.as_str()) {
            continue;
        }
        if ordered.contains_key(key) {
            continue;
        }
        if should_omit(key, value) {
            continue;
        }
        ordered.insert(key.clone(), value.clone());
    }

    let ordered_value = Value::Object(ordered);
    serde_yaml::to_string(&ordered_value).unwrap_or_default()
}

/// Parse agent YAML back to a serde_json::Value for API operations.
pub fn yaml_to_agent(yaml: &str) -> Result<Value, serde_yaml::Error> {
    serde_yaml::from_str(yaml)
}

/// Returns volatile fields to strip from Foundry agent responses
pub fn agent_volatile_fields() -> &'static [&'static str] {
    VOLATILE_FIELDS
}

/// Strip empty optional fields from an agent value for consistent comparison.
///
/// The YAML format intentionally omits empty `description`, `metadata`, `tool_resources`,
/// etc. The server always returns these fields (as `""` or `{}`). Call this on remote
/// agent values before comparing to local YAML-derived values so that omitted-vs-empty
/// differences don't appear as drift.
pub fn strip_agent_empty_fields(value: &mut Value) {
    if let Some(obj) = value.as_object_mut() {
        obj.retain(|key, val| !should_omit(key, val));
    }
}

/// Whether an optional field should be omitted from YAML output.
fn should_omit(field: &str, value: &Value) -> bool {
    match value {
        Value::Null => true,
        Value::String(s) if s.is_empty() => true,
        Value::Array(arr) if arr.is_empty() && field == "tools" => false, // keep empty tools
        Value::Array(arr) if arr.is_empty() => true,
        Value::Object(map) if map.is_empty() && field == "tool_resources" => true,
        Value::Object(map) if map.is_empty() && field == "metadata" => true,
        _ => false,
    }
}

// --- Legacy support: keep wrap/flatten for API layer (in foundry.rs) ---
// decompose_agent / compose_agent / AgentFiles are removed.

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

    #[test]
    fn test_agent_to_yaml_full() {
        let agent = json!({
            "id": "asst_abc123",
            "name": "regulus",
            "kind": "prompt",
            "model": "gpt-5.2-chat",
            "description": "A regulatory compliance assistant",
            "temperature": 0.7,
            "top_p": 1.0,
            "instructions": "You are Regulus.\n\n## Personality\nFriendly and calm.",
            "tools": [
                {"type": "mcp", "server_label": "kb_test"}
            ],
            "tool_resources": {
                "file_search": {
                    "vector_store_ids": ["vs_abc123"]
                }
            },
            "created_at": 1700000000,
            "object": "assistant",
            "version": "1.0"
        });

        let yaml = agent_to_yaml(&agent);

        // Verify field order
        let lines: Vec<&str> = yaml.lines().collect();
        let first_field = lines.iter().find(|l| !l.starts_with("---")).unwrap();
        assert!(
            first_field.starts_with("kind:"),
            "First field should be 'kind', got: {}",
            first_field
        );

        // Verify volatile/identity fields are excluded
        assert!(!yaml.contains("name:"), "name should be excluded");
        assert!(
            !yaml.contains("id:"),
            "id should be excluded (but 'kind' may contain 'id' substring)"
        );
        assert!(
            !yaml.contains("created_at:"),
            "created_at should be excluded"
        );
        assert!(!yaml.contains("object:"), "object should be excluded");
        assert!(!yaml.contains("version:"), "version should be excluded");

        // Verify content is present
        assert!(yaml.contains("model: gpt-5.2-chat"));
        assert!(yaml.contains("description: A regulatory compliance assistant"));
        assert!(yaml.contains("temperature: 0.7"));
        assert!(yaml.contains("top_p: 1.0"));
        assert!(yaml.contains("You are Regulus."));
        assert!(yaml.contains("mcp"));
        assert!(yaml.contains("vs_abc123"));
    }

    #[test]
    fn test_agent_to_yaml_minimal() {
        let agent = json!({
            "name": "minimal",
            "model": "gpt-4o",
            "kind": "prompt"
        });

        let yaml = agent_to_yaml(&agent);

        assert!(yaml.contains("kind: prompt"));
        assert!(yaml.contains("model: gpt-4o"));
        // Optional fields should be absent
        assert!(!yaml.contains("description"));
        assert!(!yaml.contains("temperature"));
        assert!(!yaml.contains("top_p"));
        assert!(!yaml.contains("instructions"));
        assert!(!yaml.contains("tool_resources"));
    }

    #[test]
    fn test_agent_to_yaml_strips_volatile() {
        let agent = json!({
            "name": "test",
            "id": "asst_123",
            "model": "gpt-4o",
            "created_at": 1700000000,
            "object": "assistant",
            "version": "1.0"
        });

        let yaml = agent_to_yaml(&agent);

        assert!(!yaml.contains("created_at"));
        assert!(!yaml.contains("object"));
        assert!(!yaml.contains("version"));
        assert!(!yaml.contains("asst_123"));
        assert!(yaml.contains("model: gpt-4o"));
    }

    #[test]
    fn test_agent_to_yaml_multiline_instructions() {
        let agent = json!({
            "name": "test",
            "model": "gpt-4o",
            "instructions": "Line one.\n\nLine three.\nLine four."
        });

        let yaml = agent_to_yaml(&agent);

        // serde_yaml should use block scalar for multiline
        assert!(yaml.contains("instructions:"));
        assert!(yaml.contains("Line one."));
        assert!(yaml.contains("Line three."));
    }

    #[test]
    fn test_yaml_to_agent_roundtrip() {
        let agent = json!({
            "name": "roundtrip",
            "id": "asst_rt",
            "kind": "prompt",
            "model": "gpt-4o",
            "description": "Test agent",
            "temperature": 0.7,
            "instructions": "Be helpful.\n\nBe concise.",
            "tools": [
                {"type": "code_interpreter"}
            ],
            "tool_resources": {
                "file_search": {
                    "vector_store_ids": ["vs_1"]
                }
            }
        });

        let yaml = agent_to_yaml(&agent);
        let parsed = yaml_to_agent(&yaml).unwrap();

        // All non-excluded fields should round-trip
        assert_eq!(parsed["kind"], "prompt");
        assert_eq!(parsed["model"], "gpt-4o");
        assert_eq!(parsed["description"], "Test agent");
        assert_eq!(parsed["temperature"], 0.7);
        assert!(
            parsed["instructions"]
                .as_str()
                .unwrap()
                .contains("Be helpful.")
        );
        assert_eq!(parsed["tools"].as_array().unwrap().len(), 1);
        assert!(
            parsed["tool_resources"]["file_search"]["vector_store_ids"]
                .as_array()
                .unwrap()
                .len()
                == 1
        );

        // Excluded fields should NOT be present
        assert!(parsed.get("name").is_none());
        assert!(parsed.get("id").is_none());
    }

    #[test]
    fn test_yaml_to_agent_from_portal() {
        let portal_yaml = r#"
kind: prompt
model: gpt-5.2-chat
description: A regulatory compliance assistant
temperature: 0.7
top_p: 1.0
instructions: |
  You are Regulus, a knowledgeable and reliable AI assistant.

  ## Personality and Tone
  You are friendly, calm, and helpful.
tools:
  - type: mcp
    server_label: kb_regulatory_kb_9kdyn
    server_url: https://mklabsrch.search.windows.net/knowledgebases/regulatory-kb/mcp
    require_approval: never
    project_connection_id: kb-regulatory-kb-9kdyn
tool_resources:
  file_search:
    vector_store_ids:
      - vs_abc123
"#;
        let parsed = yaml_to_agent(portal_yaml).unwrap();

        assert_eq!(parsed["kind"], "prompt");
        assert_eq!(parsed["model"], "gpt-5.2-chat");
        assert_eq!(parsed["temperature"], 0.7);
        assert_eq!(parsed["top_p"], 1.0);
        assert!(parsed["instructions"].as_str().unwrap().contains("Regulus"));
        assert_eq!(parsed["tools"].as_array().unwrap().len(), 1);
        assert_eq!(parsed["tools"][0]["type"], "mcp");
        assert_eq!(
            parsed["tool_resources"]["file_search"]["vector_store_ids"][0],
            "vs_abc123"
        );
    }

    #[test]
    fn test_agent_volatile_fields() {
        let fields = agent_volatile_fields();
        assert!(fields.contains(&"id"));
        assert!(fields.contains(&"created_at"));
        assert!(fields.contains(&"object"));
        assert!(fields.contains(&"version"));
        assert!(!fields.contains(&"name"));
    }

    #[test]
    fn test_agent_to_yaml_non_object_returns_empty() {
        let yaml = agent_to_yaml(&json!("not an object"));
        // Should produce valid YAML (empty object)
        assert!(yaml.trim() == "{}" || yaml.trim().is_empty());
    }

    #[test]
    fn test_agent_to_yaml_preserves_unknown_fields() {
        let agent = json!({
            "name": "test",
            "model": "gpt-4o",
            "kind": "prompt",
            "custom_field": "custom_value"
        });

        let yaml = agent_to_yaml(&agent);
        assert!(yaml.contains("custom_field: custom_value"));
    }

    #[test]
    fn test_agent_to_yaml_empty_tools_preserved() {
        let agent = json!({
            "name": "test",
            "model": "gpt-4o",
            "tools": []
        });

        let yaml = agent_to_yaml(&agent);
        assert!(yaml.contains("tools:"));
    }

    #[test]
    fn test_strip_agent_empty_fields() {
        let mut value = json!({
            "name": "test",
            "model": "gpt-4o",
            "description": "",
            "metadata": {},
            "tool_resources": {},
            "instructions": "Be helpful."
        });
        strip_agent_empty_fields(&mut value);

        assert_eq!(value["name"], "test");
        assert_eq!(value["model"], "gpt-4o");
        assert_eq!(value["instructions"], "Be helpful.");
        assert!(value.get("description").is_none());
        assert!(value.get("metadata").is_none());
        assert!(value.get("tool_resources").is_none());
    }

    #[test]
    fn test_agent_to_yaml_empty_metadata_omitted() {
        let agent = json!({
            "name": "test",
            "model": "gpt-4o",
            "metadata": {}
        });

        let yaml = agent_to_yaml(&agent);
        assert!(!yaml.contains("metadata"));
    }

    #[test]
    fn test_agent_yaml_roundtrip_preserves_require_approval() {
        // Test string form: "never"
        let agent_never = json!({
            "name": "test",
            "kind": "prompt",
            "model": "gpt-4o",
            "tools": [{
                "type": "mcp",
                "server_label": "kb_test",
                "require_approval": "never"
            }]
        });

        let yaml = agent_to_yaml(&agent_never);
        let parsed = yaml_to_agent(&yaml).unwrap();
        assert_eq!(parsed["tools"][0]["require_approval"], "never");

        // Test string form: "always"
        let agent_always = json!({
            "name": "test",
            "kind": "prompt",
            "model": "gpt-4o",
            "tools": [{
                "type": "mcp",
                "server_label": "kb_test",
                "require_approval": "always"
            }]
        });

        let yaml = agent_to_yaml(&agent_always);
        let parsed = yaml_to_agent(&yaml).unwrap();
        assert_eq!(parsed["tools"][0]["require_approval"], "always");

        // Test object form with granular per-tool control
        let agent_object = json!({
            "name": "test",
            "kind": "prompt",
            "model": "gpt-4o",
            "tools": [{
                "type": "mcp",
                "server_label": "kb_test",
                "require_approval": {
                    "never": {"tool_names": ["safe_tool"]},
                    "always": {"tool_names": ["dangerous_tool"]}
                }
            }]
        });

        let yaml = agent_to_yaml(&agent_object);
        let parsed = yaml_to_agent(&yaml).unwrap();
        let ra = &parsed["tools"][0]["require_approval"];
        assert_eq!(ra["never"]["tool_names"][0], "safe_tool");
        assert_eq!(ra["always"]["tool_names"][0], "dangerous_tool");
    }

    #[test]
    fn test_agent_yaml_roundtrip_preserves_allowed_tools() {
        let agent = json!({
            "name": "test",
            "kind": "prompt",
            "model": "gpt-4o",
            "tools": [{
                "type": "mcp",
                "server_label": "kb_test",
                "allowed_tools": ["tool_a", "tool_b"]
            }]
        });

        let yaml = agent_to_yaml(&agent);
        let parsed = yaml_to_agent(&yaml).unwrap();
        let allowed = parsed["tools"][0]["allowed_tools"].as_array().unwrap();
        assert_eq!(allowed.len(), 2);
        assert_eq!(allowed[0], "tool_a");
        assert_eq!(allowed[1], "tool_b");
    }

    #[test]
    fn test_strip_agent_empty_fields_preserves_tool_permissions() {
        let mut value = json!({
            "name": "test",
            "model": "gpt-4o",
            "description": "",
            "metadata": {},
            "tools": [{
                "type": "mcp",
                "server_label": "kb_test",
                "require_approval": "never",
                "allowed_tools": ["tool_a", "tool_b"]
            }]
        });

        strip_agent_empty_fields(&mut value);

        // Empty top-level fields should be stripped
        assert!(value.get("description").is_none());
        assert!(value.get("metadata").is_none());

        // Tool permission fields inside tool objects must be untouched
        let tool = &value["tools"][0];
        assert_eq!(tool["require_approval"], "never");
        let allowed = tool["allowed_tools"].as_array().unwrap();
        assert_eq!(allowed.len(), 2);
        assert_eq!(allowed[0], "tool_a");
        assert_eq!(allowed[1], "tool_b");
    }
}