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
409
410
411
412
413
414
415
416
417
418
419
420
//! Dependency extraction and analysis for AgentScript files.
//!
//! This module identifies all external dependencies on Salesforce org configuration:
//! - **Objects/Fields**: Referenced via record actions (create://, read://, query://, etc.)
//! - **Flows**: Referenced via flow://FlowName
//! - **Apex Classes**: Referenced via apex://ClassName
//! - **Knowledge Bases**: Referenced in knowledge block
//! - **Connections**: Referenced for escalation routing
//!
//! This enables offline analysis of agent dependencies without round-tripping to the org.

use crate::ast::{ActionDef, ConnectionBlock, KnowledgeBlock};
use crate::AgentFile;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Type of Salesforce org dependency.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DependencyType {
    /// Salesforce Object (e.g., Account, Contact, custom__c)
    SObject(String),
    /// Salesforce Field on an object (Object.Field)
    Field { object: String, field: String },
    /// Flow (flow://FlowName)
    Flow(String),
    /// Apex Class (apex://ClassName)
    ApexClass(String),
    /// Apex Method (apex://ClassName.methodName)
    ApexMethod { class: String, method: String },
    /// Knowledge Base
    KnowledgeBase(String),
    /// Connection for escalation
    Connection(String),
    /// Prompt Template
    PromptTemplate(String),
    /// External Service
    ExternalService(String),
    /// Unknown/Custom target
    Custom(String),
}

impl DependencyType {
    /// Get the category of this dependency.
    pub fn category(&self) -> &'static str {
        match self {
            DependencyType::SObject(_) => "sobject",
            DependencyType::Field { .. } => "field",
            DependencyType::Flow(_) => "flow",
            DependencyType::ApexClass(_) => "apex_class",
            DependencyType::ApexMethod { .. } => "apex_method",
            DependencyType::KnowledgeBase(_) => "knowledge",
            DependencyType::Connection(_) => "connection",
            DependencyType::PromptTemplate(_) => "prompt_template",
            DependencyType::ExternalService(_) => "external_service",
            DependencyType::Custom(_) => "custom",
        }
    }

    /// Get the name of this dependency.
    pub fn name(&self) -> String {
        match self {
            DependencyType::SObject(name) => name.clone(),
            DependencyType::Field { object, field } => format!("{}.{}", object, field),
            DependencyType::Flow(name) => name.clone(),
            DependencyType::ApexClass(name) => name.clone(),
            DependencyType::ApexMethod { class, method } => format!("{}.{}", class, method),
            DependencyType::KnowledgeBase(name) => name.clone(),
            DependencyType::Connection(name) => name.clone(),
            DependencyType::PromptTemplate(name) => name.clone(),
            DependencyType::ExternalService(name) => name.clone(),
            DependencyType::Custom(target) => target.clone(),
        }
    }
}

/// A single dependency with its source location.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Dependency {
    /// The type and name of the dependency
    pub dep_type: DependencyType,
    /// Where this dependency is used (topic name or "start_agent")
    pub used_in: String,
    /// The action name that references this dependency
    pub action_name: String,
    /// Source span (start, end)
    pub span: (usize, usize),
}

/// Complete dependency report for an AgentScript file.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DependencyReport {
    /// All unique SObjects referenced
    pub sobjects: HashSet<String>,
    /// All unique fields referenced (Object.Field)
    pub fields: HashSet<String>,
    /// All Flows referenced
    pub flows: HashSet<String>,
    /// All Apex classes referenced
    pub apex_classes: HashSet<String>,
    /// All Knowledge bases referenced
    pub knowledge_bases: HashSet<String>,
    /// All Connections referenced
    pub connections: HashSet<String>,
    /// All Prompt Templates referenced
    pub prompt_templates: HashSet<String>,
    /// External services referenced
    pub external_services: HashSet<String>,
    /// All dependencies with full details
    pub all_dependencies: Vec<Dependency>,
    /// Dependencies grouped by type
    pub by_type: HashMap<String, Vec<Dependency>>,
    /// Dependencies grouped by topic
    pub by_topic: HashMap<String, Vec<Dependency>>,
}

impl DependencyReport {
    /// Check if a specific SObject is used.
    pub fn uses_sobject(&self, name: &str) -> bool {
        self.sobjects.contains(name)
    }

    /// Check if a specific Flow is used.
    pub fn uses_flow(&self, name: &str) -> bool {
        self.flows.contains(name)
    }

    /// Check if a specific Apex class is used.
    pub fn uses_apex_class(&self, name: &str) -> bool {
        self.apex_classes.contains(name)
    }

    /// Get all dependencies of a specific type.
    pub fn get_by_type(&self, category: &str) -> Vec<&Dependency> {
        self.by_type
            .get(category)
            .map(|deps| deps.iter().collect())
            .unwrap_or_default()
    }

    /// Get all dependencies used in a specific topic.
    pub fn get_by_topic(&self, topic: &str) -> Vec<&Dependency> {
        self.by_topic
            .get(topic)
            .map(|deps| deps.iter().collect())
            .unwrap_or_default()
    }

    /// Get total count of unique dependencies.
    pub fn unique_count(&self) -> usize {
        self.sobjects.len()
            + self.fields.len()
            + self.flows.len()
            + self.apex_classes.len()
            + self.knowledge_bases.len()
            + self.connections.len()
            + self.prompt_templates.len()
            + self.external_services.len()
    }
}

/// Extract all Salesforce org dependencies from an AgentScript AST.
pub fn extract_dependencies(ast: &AgentFile) -> DependencyReport {
    let mut report = DependencyReport::default();

    // Extract from knowledge block
    if let Some(knowledge) = &ast.knowledge {
        extract_from_knowledge(&knowledge.node, &mut report);
    }

    // Extract from connection blocks
    for connection in &ast.connections {
        extract_from_connection(&connection.node, &mut report);
    }

    // Extract from start_agent actions
    if let Some(start) = &ast.start_agent {
        if let Some(actions) = &start.node.actions {
            for action in &actions.node.actions {
                extract_from_action(
                    &action.node,
                    "start_agent",
                    (action.span.start, action.span.end),
                    &mut report,
                );
            }
        }
    }

    // Extract from topic actions
    for topic in &ast.topics {
        let topic_name = &topic.node.name.node;
        if let Some(actions) = &topic.node.actions {
            for action in &actions.node.actions {
                extract_from_action(
                    &action.node,
                    topic_name,
                    (action.span.start, action.span.end),
                    &mut report,
                );
            }
        }
    }

    // Build grouped views
    for dep in &report.all_dependencies {
        let category = dep.dep_type.category().to_string();
        report
            .by_type
            .entry(category)
            .or_default()
            .push(dep.clone());

        report
            .by_topic
            .entry(dep.used_in.clone())
            .or_default()
            .push(dep.clone());
    }

    report
}

/// Parse an action target and extract dependencies.
fn extract_from_action(
    action: &ActionDef,
    topic: &str,
    span: (usize, usize),
    report: &mut DependencyReport,
) {
    let action_name = action.name.node.clone();

    if let Some(target) = &action.target {
        let target_str = &target.node;
        let dep_type = parse_action_target(target_str);

        // Add to appropriate set
        match &dep_type {
            DependencyType::SObject(name) => {
                report.sobjects.insert(name.clone());
            }
            DependencyType::Field { object, field } => {
                report.sobjects.insert(object.clone());
                report.fields.insert(format!("{}.{}", object, field));
            }
            DependencyType::Flow(name) => {
                report.flows.insert(name.clone());
            }
            DependencyType::ApexClass(name) => {
                report.apex_classes.insert(name.clone());
            }
            DependencyType::ApexMethod { class, .. } => {
                report.apex_classes.insert(class.clone());
            }
            DependencyType::PromptTemplate(name) => {
                report.prompt_templates.insert(name.clone());
            }
            DependencyType::ExternalService(name) => {
                report.external_services.insert(name.clone());
            }
            _ => {}
        }

        report.all_dependencies.push(Dependency {
            dep_type,
            used_in: topic.to_string(),
            action_name,
            span,
        });
    }
}

/// Parse an action target string into a dependency type.
fn parse_action_target(target: &str) -> DependencyType {
    if let Some(name) = target.strip_prefix("flow://") {
        return DependencyType::Flow(name.to_string());
    }

    if let Some(name) = target.strip_prefix("apex://") {
        if let Some((class, method)) = name.split_once('.') {
            return DependencyType::ApexMethod {
                class: class.to_string(),
                method: method.to_string(),
            };
        }
        return DependencyType::ApexClass(name.to_string());
    }

    if let Some(name) = target.strip_prefix("prompt://") {
        return DependencyType::PromptTemplate(name.to_string());
    }

    if let Some(name) = target.strip_prefix("service://") {
        return DependencyType::ExternalService(name.to_string());
    }

    // Record operations: create://, read://, update://, delete://, query://
    for op in &["create://", "read://", "update://", "delete://", "query://"] {
        if let Some(rest) = target.strip_prefix(op) {
            // Check for field access (Object.Field)
            if let Some((object, field)) = rest.split_once('.') {
                return DependencyType::Field {
                    object: object.to_string(),
                    field: field.to_string(),
                };
            }
            return DependencyType::SObject(rest.to_string());
        }
    }

    DependencyType::Custom(target.to_string())
}

/// Extract dependencies from knowledge block.
fn extract_from_knowledge(knowledge: &KnowledgeBlock, report: &mut DependencyReport) {
    for entry in &knowledge.entries {
        let name = entry.node.name.node.clone();
        report.knowledge_bases.insert(name.clone());
        report.all_dependencies.push(Dependency {
            dep_type: DependencyType::KnowledgeBase(name.clone()),
            used_in: "knowledge".to_string(),
            action_name: name,
            span: (entry.span.start, entry.span.end),
        });
    }
}

/// Extract dependencies from a connection block.
fn extract_from_connection(connection: &ConnectionBlock, report: &mut DependencyReport) {
    // Register the connection name
    let connection_name = connection.name.node.clone();
    report.connections.insert(connection_name.clone());

    // Extract dependencies from entries
    for entry in &connection.entries {
        let name = entry.node.name.node.clone();
        report.all_dependencies.push(Dependency {
            dep_type: DependencyType::Connection(connection_name.clone()),
            used_in: format!("connection:{}", connection_name),
            action_name: name,
            span: (entry.span.start, entry.span.end),
        });
    }
}

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

    #[test]
    fn test_parse_flow_target() {
        let dep = parse_action_target("flow://Get_Customer_Details");
        assert!(matches!(dep, DependencyType::Flow(name) if name == "Get_Customer_Details"));
    }

    #[test]
    fn test_parse_apex_target() {
        let dep = parse_action_target("apex://OrderService");
        assert!(matches!(dep, DependencyType::ApexClass(name) if name == "OrderService"));

        let dep = parse_action_target("apex://OrderService.createOrder");
        assert!(matches!(dep, DependencyType::ApexMethod { class, method }
            if class == "OrderService" && method == "createOrder"));
    }

    #[test]
    fn test_parse_record_target() {
        let dep = parse_action_target("query://Account");
        assert!(matches!(dep, DependencyType::SObject(name) if name == "Account"));

        let dep = parse_action_target("read://Contact.Email");
        assert!(matches!(dep, DependencyType::Field { object, field }
            if object == "Contact" && field == "Email"));
    }

    #[test]
    fn test_parse_prompt_template() {
        let dep = parse_action_target("prompt://Customer_Greeting");
        assert!(matches!(dep, DependencyType::PromptTemplate(name) if name == "Customer_Greeting"));
    }

    #[test]
    fn test_parse_external_service() {
        let dep = parse_action_target("service://WeatherAPI");
        assert!(matches!(dep, DependencyType::ExternalService(name) if name == "WeatherAPI"));
    }

    #[test]
    #[ignore = "Recipe file uses {} empty object literal which is not valid AgentScript"]
    fn test_full_dependency_extraction() {
        // Load and parse a real recipe from the submodule
        let source = include_str!("../../agent-script-recipes/force-app/future_recipes/customerServiceAgent/aiAuthoringBundles/CustomerServiceAgent/CustomerServiceAgent.agent");
        let ast = crate::parse(source).unwrap();
        let report = extract_dependencies(&ast);

        // Check flows (multiple flow targets in this recipe)
        assert!(report.uses_flow("FetchCustomer"));
        assert!(report.uses_flow("SearchKnowledgeBase"));
        assert!(report.uses_flow("CreateCase"));
        assert!(report.uses_flow("UpdateCase"));
        assert!(report.uses_flow("EscalateCase"));
        assert!(report.uses_flow("SendSatisfactionSurvey"));

        // Check count of flows
        assert!(report.flows.len() >= 6, "Expected at least 6 flows, got {}", report.flows.len());

        // Check apex (IssueClassifier is called via apex://)
        assert!(report.uses_apex_class("IssueClassifier"));

        // Check grouping by topic - triage topic has many actions
        let triage_deps = report.get_by_topic("triage");
        assert!(!triage_deps.is_empty(), "Expected dependencies in triage topic");

        // Check grouping by type
        let flow_deps = report.get_by_type("flow");
        assert!(!flow_deps.is_empty());

        // Verify we can get a summary
        assert!(report.unique_count() > 0);
    }
}