busbar-sf-agentscript 0.0.2

AgentScript parser, graph analysis, and LSP for Salesforce Agentforce
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
//! ASCII rendering for graph visualization.
//!
//! Provides terminal-friendly tree and diagram output for RefGraph structures.

use super::super::{RefGraph, RefNode};
use petgraph::visit::EdgeRef;
use std::collections::{HashMap, HashSet};

/// Render the topic flow graph as ASCII art.
///
/// Shows how topics connect to each other via transitions, delegations, and routing.
pub fn render_topic_flow(graph: &RefGraph) -> String {
    let inner = graph.inner();

    // Collect topic names and edges
    let mut topics: Vec<String> = vec!["start_agent".to_string()];
    let mut topic_idx: HashMap<String, usize> = HashMap::new();
    topic_idx.insert("start_agent".to_string(), 0);

    for name in graph.topic_names() {
        topic_idx.insert(name.to_string(), topics.len());
        topics.push(name.to_string());
    }

    // Collect edges by source
    let mut edges: HashMap<usize, Vec<(usize, String)>> = HashMap::new();

    for edge in inner.edge_references() {
        let edge_type = edge.weight().label();
        if edge_type == "transitions_to" || edge_type == "delegates" || edge_type == "routes" {
            let source_node = graph.get_node(edge.source());
            let target_node = graph.get_node(edge.target());

            if let (Some(src), Some(tgt)) = (source_node, target_node) {
                let src_name = get_topic_name(src);
                let tgt_name = get_topic_name(tgt);

                if let (Some(&src_id), Some(&tgt_id)) =
                    (topic_idx.get(&src_name), topic_idx.get(&tgt_name))
                {
                    edges
                        .entry(src_id)
                        .or_default()
                        .push((tgt_id, edge_type.to_string()));
                }
            }
        }
    }

    render_ascii_tree(&topics, &edges)
}

/// Render the actions graph as ASCII art.
///
/// Shows topics with their action definitions and reasoning actions.
pub fn render_actions_view(graph: &RefGraph) -> String {
    let inner = graph.inner();
    let mut labels: Vec<String> = Vec::new();
    let mut node_map: HashMap<usize, usize> = HashMap::new();

    for idx in inner.node_indices() {
        if let Some(node) = graph.get_node(idx) {
            let label = match node {
                RefNode::StartAgent { .. } => Some("start_agent".to_string()),
                RefNode::Topic { name, .. } => Some(format!("[{}]", name)),
                RefNode::ActionDef { name, .. } => Some(name.clone()),
                RefNode::ReasoningAction { name, target, .. } => {
                    if let Some(t) = target {
                        Some(format!("{}{}", name, t.split("://").last().unwrap_or(t)))
                    } else {
                        Some(name.clone())
                    }
                }
                _ => None,
            };

            if let Some(lbl) = label {
                node_map.insert(idx.index(), labels.len());
                labels.push(lbl);
            }
        }
    }

    // Collect edges
    let mut edges: HashMap<usize, Vec<(usize, String)>> = HashMap::new();

    for edge in inner.edge_references() {
        let edge_type = edge.weight().label();
        if edge_type == "invokes" || edge_type == "transitions_to" || edge_type == "delegates" {
            if let (Some(&src_id), Some(&tgt_id)) =
                (node_map.get(&edge.source().index()), node_map.get(&edge.target().index()))
            {
                edges
                    .entry(src_id)
                    .or_default()
                    .push((tgt_id, edge_type.to_string()));
            }
        }
    }

    render_ascii_tree(&labels, &edges)
}

/// Render a full structured view of the graph.
///
/// Shows a topic-centric view with variables, actions, and transitions.
pub fn render_full_view(graph: &RefGraph) -> String {
    let inner = graph.inner();
    let mut output = String::new();

    struct TopicInfo {
        actions: Vec<String>,
        reasoning: Vec<String>,
        transitions: Vec<String>,
        delegates: Vec<String>,
    }

    let mut topics: HashMap<String, TopicInfo> = HashMap::new();
    let mut start_routes: Vec<String> = Vec::new();
    let mut variables: Vec<(String, bool)> = Vec::new();

    // First pass: collect all nodes
    for idx in inner.node_indices() {
        if let Some(node) = graph.get_node(idx) {
            match node {
                RefNode::Variable { name, mutable, .. } => {
                    variables.push((name.clone(), *mutable));
                }
                RefNode::Topic { name, .. } => {
                    topics.entry(name.clone()).or_insert(TopicInfo {
                        actions: Vec::new(),
                        reasoning: Vec::new(),
                        transitions: Vec::new(),
                        delegates: Vec::new(),
                    });
                }
                RefNode::ActionDef { name, topic, .. } => {
                    if let Some(t) = topics.get_mut(topic) {
                        t.actions.push(name.clone());
                    }
                }
                RefNode::ReasoningAction {
                    name,
                    topic,
                    target,
                    ..
                } => {
                    if let Some(t) = topics.get_mut(topic) {
                        let desc = if let Some(tgt) = target {
                            format!("{}{}", name, tgt.split("://").last().unwrap_or(tgt))
                        } else {
                            name.clone()
                        };
                        t.reasoning.push(desc);
                    }
                }
                _ => {}
            }
        }
    }

    // Second pass: collect edges for transitions
    for edge in inner.edge_references() {
        let edge_type = edge.weight().label();
        let source_node = graph.get_node(edge.source());
        let target_node = graph.get_node(edge.target());

        if let (Some(src), Some(tgt)) = (source_node, target_node) {
            match (src, tgt, edge_type) {
                (RefNode::StartAgent { .. }, RefNode::Topic { name, .. }, "routes") => {
                    start_routes.push(name.clone());
                }
                (
                    RefNode::Topic { name: src_name, .. },
                    RefNode::Topic { name: tgt_name, .. },
                    "transitions_to",
                ) => {
                    if let Some(t) = topics.get_mut(src_name) {
                        if !t.transitions.contains(tgt_name) {
                            t.transitions.push(tgt_name.clone());
                        }
                    }
                }
                (
                    RefNode::Topic { name: src_name, .. },
                    RefNode::Topic { name: tgt_name, .. },
                    "delegates",
                ) => {
                    if let Some(t) = topics.get_mut(src_name) {
                        if !t.delegates.contains(tgt_name) {
                            t.delegates.push(tgt_name.clone());
                        }
                    }
                }
                _ => {}
            }
        }
    }

    // Render output
    output.push_str("┌─────────────────────────────────────────────────────────────┐\n");
    output.push_str("│  AGENT EXECUTION FLOW                                       │\n");
    output.push_str("└─────────────────────────────────────────────────────────────┘\n\n");

    // Variables summary
    if !variables.is_empty() {
        output.push_str("VARIABLES:\n");
        let mutable: Vec<_> = variables
            .iter()
            .filter(|(_, m)| *m)
            .map(|(n, _)| n.as_str())
            .collect();
        let linked: Vec<_> = variables
            .iter()
            .filter(|(_, m)| !*m)
            .map(|(n, _)| n.as_str())
            .collect();
        if !mutable.is_empty() {
            output.push_str(&format!("  Mutable: {}\n", mutable.join(", ")));
        }
        if !linked.is_empty() {
            output.push_str(&format!("  Linked:  {}\n", linked.join(", ")));
        }
        output.push('\n');
    }

    // Entry point
    output.push_str("ENTRY POINT:\n");
    output.push_str("  start_agent\n");
    if !start_routes.is_empty() {
        output.push_str(&format!("    routes to: {}\n", start_routes.join(", ")));
    }
    output.push('\n');

    // Topics
    output.push_str("TOPICS:\n");
    for (name, info) in &topics {
        output.push_str(&format!("\n  ┌─ {} ─────────────────────────────\n", name));

        if !info.actions.is_empty() {
            output.push_str("  │ Actions:\n");
            for action in &info.actions {
                output.push_str(&format!("  │   • {}\n", action));
            }
        }

        if !info.reasoning.is_empty() {
            output.push_str("  │ Reasoning:\n");
            for r in &info.reasoning {
                output.push_str(&format!("  │   ◆ {}\n", r));
            }
        }

        if !info.transitions.is_empty() {
            output.push_str(&format!("  │ Transitions → {}\n", info.transitions.join(", ")));
        }

        if !info.delegates.is_empty() {
            output.push_str(&format!("  │ Delegates ⇒ {}\n", info.delegates.join(", ")));
        }

        output.push_str("  └────────────────────────────────────────\n");
    }

    output
}

/// Render nodes and edges as an ASCII tree structure.
pub fn render_ascii_tree(
    labels: &[String],
    edges: &HashMap<usize, Vec<(usize, String)>>,
) -> String {
    let mut output = String::new();
    let mut visited: HashSet<usize> = HashSet::new();

    // Find root nodes (nodes with no incoming edges)
    let mut has_incoming: HashSet<usize> = HashSet::new();
    for targets in edges.values() {
        for (target, _) in targets {
            has_incoming.insert(*target);
        }
    }

    let roots: Vec<usize> = (0..labels.len())
        .filter(|i| !has_incoming.contains(i))
        .collect();

    // If no roots found (everything has incoming), start from node 0
    let start_nodes = if roots.is_empty() { vec![0] } else { roots };

    for (i, &root) in start_nodes.iter().enumerate() {
        if i > 0 {
            output.push('\n');
        }
        render_node(&mut output, labels, edges, root, "", true, &mut visited);
    }

    output
}

fn render_node(
    output: &mut String,
    labels: &[String],
    edges: &HashMap<usize, Vec<(usize, String)>>,
    node: usize,
    prefix: &str,
    is_last: bool,
    visited: &mut HashSet<usize>,
) {
    let connector = if prefix.is_empty() {
        ""
    } else if is_last {
        "└── "
    } else {
        "├── "
    };

    let label = labels.get(node).map(|s| s.as_str()).unwrap_or("?");

    // Check if this is a back-edge (cycle)
    if visited.contains(&node) {
        output.push_str(prefix);
        output.push_str(connector);
        output.push_str(label);
        output.push_str("\n");
        return;
    }

    output.push_str(prefix);
    output.push_str(connector);
    output.push_str(label);
    output.push('\n');

    visited.insert(node);

    if let Some(children) = edges.get(&node) {
        let new_prefix = if prefix.is_empty() {
            "".to_string()
        } else if is_last {
            format!("{}    ", prefix)
        } else {
            format!("{}", prefix)
        };

        for (i, (child, edge_type)) in children.iter().enumerate() {
            let child_is_last = i == children.len() - 1;

            // Show edge type for non-trivial edges
            let edge_indicator = match edge_type.as_str() {
                "transitions_to" => "",
                "delegates" => "",
                "routes" => "",
                "invokes" => "",
                "reads" => "",
                "writes" => "",
                _ => "",
            };

            if !edge_indicator.is_empty() {
                output.push_str(&new_prefix);
                output.push_str(if child_is_last { "" } else { "" });
                output.push_str(edge_indicator);

                let child_label = labels.get(*child).map(|s| s.as_str()).unwrap_or("?");
                if visited.contains(child) {
                    output.push_str(child_label);
                    output.push_str("\n");
                } else {
                    output.push_str(child_label);
                    output.push('\n');

                    // Recurse for this child's children
                    let deeper_prefix = if child_is_last {
                        format!("{}    ", new_prefix)
                    } else {
                        format!("{}", new_prefix)
                    };

                    visited.insert(*child);
                    if let Some(grandchildren) = edges.get(child) {
                        for (j, (grandchild, _gc_edge)) in grandchildren.iter().enumerate() {
                            let gc_is_last = j == grandchildren.len() - 1;
                            render_node(
                                output,
                                labels,
                                edges,
                                *grandchild,
                                &deeper_prefix,
                                gc_is_last,
                                visited,
                            );
                        }
                    }
                }
            } else {
                render_node(output, labels, edges, *child, &new_prefix, child_is_last, visited);
            }
        }
    }
}

fn get_topic_name(node: &RefNode) -> String {
    match node {
        RefNode::StartAgent { .. } => "start_agent".to_string(),
        RefNode::Topic { name, .. } => name.clone(),
        _ => "unknown".to_string(),
    }
}