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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
use busbar_sf_agentscript::parser;

#[test]
fn test_reasoning_minimal() {
    let src = r#"config:
   agent_name: "Test"

start_agent topic_selector:
   description: "Welcome"

   reasoning:
      instructions:|
         Select the tool.
      actions:
         check_order: @utils.transition to @topic.order_status
            description: "Start checking order status"

topic order_status:
   description: "Looks up and explains order status"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert!(ast.start_agent.is_some());
            assert_eq!(ast.topics.len(), 1);
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed");
        }
    }
}

#[test]
#[ignore = "Known parser limitation — uses '...' placeholder syntax"]
fn test_dynamic_instructions() {
    let src = r#"config:
   agent_name: "Test"

topic main:
   description: "Main"

   reasoning:
      instructions:->
         if not @variables.order_id:
            | Ask for order number.

         if @variables.order_id and not @variables.status:
            run @actions.get_status
               with order_id=@variables.order_id
               set @variables.status = @outputs.status

         | Status: {!@variables.status}

         if @variables.status == "pending":
            | Order is being processed.

      actions:
         get_status: @actions.get_status
            with order_id=...
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 1);
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed");
        }
    }
}

#[test]
fn test_topic_with_actions_definitions() {
    // Mirrors the structure in ReasoningInstructions.agent
    let src = r#"config:
   agent_name: "Test"

start_agent topic_selector:
   description: "Welcome"

   reasoning:
      instructions:|
         Select the tool.
      actions:
         check_order: @utils.transition to @topic.order_status
            description: "Start checking order status"

topic order_status:
   description: "Looks up order status"

   actions:
      get_order_status:
         description: "Retrieves status"
         inputs:
            order_id: string
               description: "Order ID"
         outputs:
            status: string
               description: "Status"
         target: "flow://GetOrderStatus"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert!(ast.start_agent.is_some());
            assert_eq!(ast.topics.len(), 1);
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed");
        }
    }
}

#[test]
#[ignore = "Known parser limitation — uses '...' placeholder syntax"]
fn test_full_reasoning_instructions_structure() {
    // Full structure from ReasoningInstructions.agent
    let src = r#"config:
   agent_name: "ReasoningInstructions"
   agent_label: "ReasoningInstructions"
   description: "Checks order status"

variables:
   order_id: mutable string = ""
      description: "The order ID to check"

   order_status: mutable string = ""
      description: "Current status"

system:
   messages:
      welcome: "Hi! I can help you check your order status."
      error: "I had trouble looking up that order."

   instructions: "You are an order status assistant."

start_agent topic_selector:
   description: "Welcome customers"

   reasoning:
      instructions:|
         Select the tool that best matches the user's message.
      actions:
         check_order: @utils.transition to @topic.order_status
            description: "Start checking order status"

topic order_status:
   description: "Looks up and explains order status"

   actions:
      get_order_status:
         description: "Retrieves current status for an order"
         inputs:
            order_id: string
               description: "The unique order identifier"
         outputs:
            status: string
               description: "Current order status"
         target: "flow://GetOrderStatus"

   reasoning:
      instructions:->
         if not @variables.order_id:
            | Ask the customer for their order number.

         if @variables.order_id and not @variables.order_status:
            run @actions.get_order_status
               with order_id=@variables.order_id
               set @variables.order_status = @outputs.status

         | The customer's order has status: {!@variables.order_status}

         if @variables.order_status == "pending":
            | The order is being processed.

      actions:
         get_order_status: @actions.get_order_status
            with order_id=...
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert!(ast.start_agent.is_some());
            assert_eq!(ast.topics.len(), 1);
            assert!(ast.variables.is_some());
            assert!(ast.system.is_some());
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed");
        }
    }
}

#[test]
fn test_multiple_topics_with_dynamic_instructions() {
    // Minimal test for two topics each with instructions:->
    let src = r#"config:
   agent_name: "Test"

topic general:
   description: "General"

   reasoning:
      instructions:->
         | Hello
      actions:
         go: @utils.transition to @topic.other

topic other:
   description: "Other"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 2, "Should parse 2 topics");
            assert_eq!(ast.topics[0].node.name.node, "general");
            assert_eq!(ast.topics[1].node.name.node, "other");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_pipe_with_continuation() {
    // Tests | text with continuation lines (MORE indented than the |)
    // This is the pattern in SystemInstructionOverrides
    let src = r#"config:
   agent_name: "Test"

topic general:
   description: "General"

   reasoning:
      instructions:->
         | Hello world
           "continued text"
           "more text"
      actions:
         go: @utils.transition to @topic.other

topic other:
   description: "Other"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 2, "Should parse 2 topics");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
#[ignore = "Known parser limitation — uses '...' placeholder syntax"]
fn test_exact_first_40_lines() {
    // EXACT content from ReasoningInstructions.agent lines 1-40
    let src = r#"# ReasoningInstructions - Procedural Templating with Dynamic Instructions
# This agent demonstrates how to build instructions dynamically using procedures

config:
   agent_name: "ReasoningInstructions"
   agent_label: "ReasoningInstructions"
   description: "Checks order status and provides dynamic instructions based on order state"

variables:
   order_id: mutable string = ""
      description: "The order ID to check"

   order_status: mutable string = ""
      description: "Current status of the order"

   order_details: mutable object = {}
      description: "Full order details"

   tracking_number: mutable string = ""
      description: "Shipping tracking number"

system:
   messages:
      welcome: "Hi! I can help you check your order status. What's your order number?"
      error: "I had trouble looking up that order. Please verify the order number."

   instructions: "You are an order status assistant. Help customers track their orders and resolve issues."

start_agent topic_selector:
   description: "Welcome customers and begin helping with order status"

   reasoning:
      instructions:|
         Select the tool that best matches the user's message and conversation history. If it's unclear, make your best guess.
      actions:
         check_order: @utils.transition to @topic.order_status
            description: "Start checking order status"

topic order_status:
   description: "Looks up and explains order status"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert!(ast.start_agent.is_some());
            assert_eq!(ast.topics.len(), 1);
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed");
        }
    }
}

#[test]
fn test_start_agent_then_topic() {
    let src = r#"config:
   agent_name: "Test"

start_agent topic_selector:
   description: "Welcome"

   reasoning:
      instructions:|
         Select the tool.
      actions:
         check_order: @utils.transition to @topic.order_status
            description: "Start checking order status"

topic order_status:
   description: "Looks up and explains order status"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert!(ast.start_agent.is_some());
            assert_eq!(ast.topics.len(), 1);
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed");
        }
    }
}

#[test]
#[ignore = "Known parser limitation — uses '...' placeholder syntax"]
fn test_topic_with_dynamic_instructions_and_actions() {
    // This adds instructions:-> to the topic, which is what the failing recipes have
    let src = r#"config:
   agent_name: "Test"

start_agent topic_selector:
   description: "Welcome"

   reasoning:
      instructions:|
         Select the tool.
      actions:
         check_order: @utils.transition to @topic.order_status
            description: "Start checking order status"

topic order_status:
   description: "Looks up and explains order status"

   reasoning:
      instructions:->
         if not @variables.order_id:
            | Ask for order number.

      actions:
         get_order_status: @actions.get_order_status
            with order_id=...
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert!(ast.start_agent.is_some());
            assert_eq!(ast.topics.len(), 1);
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_exact_indentation_from_file() {
    // Exact indentation from SystemInstructionOverrides - 7 spaces for reasoning content
    let src = r#"config:
   agent_name: "Test"

topic general:
   description: "General"

   reasoning:
       instructions: ->
           | Hello
             "continued"

       actions:
           go: @utils.transition to @topic.other

topic other:
   description: "Other"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 2, "Should parse 2 topics");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_with_template_expressions() {
    // Add template expressions like in the real file
    let src = r#"config:
   agent_name: "Test"

topic general:
   description: "General"

   reasoning:
       instructions: ->
           | Hello
             "continued"
             Use {!@actions.go} to transition.

       actions:
           go: @utils.transition to @topic.other

topic other:
   description: "Other"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 2, "Should parse 2 topics");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_topic_with_system_override() {
    // Test topic-level system instruction override like in the real file
    let src = r#"config:
   agent_name: "Test"

topic general:
   description: "General"

   reasoning:
       instructions: ->
           | Hello

       actions:
           go: @utils.transition to @topic.professional

topic professional:
   description: "Professional"

   system:
       instructions: "You are a formal business professional."

   reasoning:
       instructions: ->
           | Professional mode

       actions:
           back: @utils.transition to @topic.general
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 2, "Should parse 2 topics");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_topic_with_system_simple() {
    // Minimal reproduction - topic with system block
    let src = r#"config:
   agent_name: "Test"

topic main:
   description: "Main"

   system:
       instructions: "Override instructions."

   reasoning:
       instructions: ->
           | Hello
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 1, "Should parse 1 topic");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_topic_with_action_definitions() {
    // Topic with action definitions (like topic order_status in ReasoningInstructions)
    let src = r#"config:
   agent_name: "Test"

start_agent main:
   description: "Entry"
   reasoning:
      instructions:|
         Help.
      actions:
         go: @utils.transition to @topic.order_status
            description: "Go"

topic order_status:
   description: "Looks up order status"

   actions:
      get_order_status:
         description: "Retrieves status"
         inputs:
            order_id: string
               description: "The order ID"
         outputs:
            status: string
               description: "Status"
         target: "flow://GetOrderStatus"

   reasoning:
      instructions:->
         | Check order status
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 1, "Should parse 1 topic");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
#[ignore = "Known parser limitation — uses '...' placeholder syntax"]
fn test_dynamic_instructions_with_run() {
    // Topic with instructions:-> containing run @actions
    let src = r#"config:
   agent_name: "Test"

topic main:
   description: "Main"

   actions:
      get_status:
         description: "Get status"
         inputs:
            order_id: string
         outputs:
            status: string
         target: "flow://GetStatus"

   reasoning:
      instructions:->
         if not @variables.order_id:
            | Ask for order number.

         if @variables.order_id and not @variables.status:
            run @actions.get_status
               with order_id=@variables.order_id
               set @variables.status = @outputs.status

         | Status: {!@variables.status}

      actions:
         get_status: @actions.get_status
            with order_id=...
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 1, "Should parse 1 topic");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_instructions_with_comments() {
    // Comments inside instructions:->
    let src = r#"config:
   agent_name: "Test"

topic main:
   description: "Main"

   reasoning:
      # Comment before instructions
      instructions:->
         # First check
         if not @variables.order_id:
            # Nested comment
            | Ask for order.

         # Another check
         | Done.
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 1, "Should parse 1 topic");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_comment_before_instructions() {
    // Just test comment before instructions
    let src = r#"config:
   agent_name: "Test"

topic main:
   description: "Main"

   reasoning:
      # Comment here
      instructions:|
         Help.
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 1, "Should parse 1 topic");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_comment_inside_dynamic_instructions() {
    // Comment INSIDE instructions:->
    let src = r#"config:
   agent_name: "Test"

topic main:
   description: "Main"

   reasoning:
      instructions:->
         # Comment inside
         | Help.
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 1, "Should parse 1 topic");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}

#[test]
fn test_prompt_template_structure() {
    // Test structure from PromptTemplateActions
    let src = r#"system:
    instructions: "You are an assistant."

    messages:
        welcome: "Hi!"
        error: "Error."

config:
    agent_name: "Test"
start_agent topic_selector:
    description: "Welcome"

    reasoning:
        instructions:|
            Select tool.
        actions:
            go: @utils.transition to @topic.main
                description: "Go"

topic main:
    description: "Main topic"
"#;
    match parser::parse(src) {
        Ok(ast) => {
            assert_eq!(ast.topics.len(), 1, "Should parse 1 topic");
        }
        Err(errors) => {
            for e in &errors {
                eprintln!("{}", e);
            }
            panic!("Parse failed with {} errors", errors.len());
        }
    }
}