quelch 0.8.0

Ingest data from Jira, Confluence, and more directly into Azure AI Search
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
//! Generate Copilot Studio agent configuration (topics, instructions) from quelch config.

use crate::config::{Config, SourceConfig};
use std::fmt::Write;

/// Generated output for a Copilot Studio agent.
pub struct CopilotOutput {
    /// Agent instructions (system prompt) as markdown text.
    pub instructions: String,
    /// OnKnowledgeRequested YAML topics, one per index.
    pub topics: Vec<GeneratedTopic>,
    /// Human-readable guide explaining how to use the generated files.
    pub guide: String,
}

/// A generated Copilot Studio topic.
pub struct GeneratedTopic {
    /// Suggested filename (e.g., "jira-search.mcs.yaml").
    pub filename: String,
    /// The YAML content.
    pub yaml: String,
}

/// Generate Copilot Studio agent configuration from a quelch config.
pub fn generate(config: &Config) -> CopilotOutput {
    let mut topics = Vec::new();

    for source in &config.sources {
        let index_name = source.index();
        let endpoint = &config.azure.endpoint;
        let semantic_config = format!("{index_name}-semantic-config");

        match source {
            SourceConfig::Jira(jira) => {
                topics.push(GeneratedTopic {
                    filename: format!("{}-search.mcs.yaml", jira.name),
                    yaml: jira_topic(endpoint, index_name, &semantic_config),
                });
            }
            SourceConfig::Confluence(conf) => {
                topics.push(GeneratedTopic {
                    filename: format!("{}-search.mcs.yaml", conf.name),
                    yaml: confluence_topic(endpoint, index_name, &semantic_config),
                });
            }
        }
    }

    let instructions = agent_instructions(config);
    let guide = usage_guide(config, &topics);

    CopilotOutput {
        instructions,
        topics,
        guide,
    }
}

/// Generate the OnKnowledgeRequested YAML topic for a Jira index.
fn jira_topic(endpoint: &str, index_name: &str, semantic_config: &str) -> String {
    format!(
        r#"# OnKnowledgeRequested topic for Jira issues index "{index_name}"
# Generated by quelch — paste into Copilot Studio code editor
#
# This topic fires when the agent needs to search for knowledge.
# It queries Azure AI Search with semantic search + the full set of
# filterable/facetable fields that quelch indexes.

kind: AdaptiveDialog
beginDialog:
  kind: OnKnowledgeRequested
  id: main
  intent: {{}}
  actions:
    - kind: HttpRequestAction
      id: jiraSearch
      method: POST
      url: "{endpoint}/indexes('{index_name}')/docs/search.post.search?api-version=2024-07-01"
      headers:
        Content-Type: application/json
        api-key: "{{{{System.Env.AZURE_SEARCH_API_KEY}}}}"
      body:
        kind: Json
        value:
          search: =System.SearchQuery
          queryType: semantic
          semanticConfiguration: "{semantic_config}"
          select: "summary,content,url,assignee,project,status,priority,issue_type,issue_key,labels,created_at,updated_at"
          top: 15
          searchMode: any
          count: true
          captions: extractive
          answers: extractive
      response: Topic.searchResults
      responseSchema:
        kind: Record
        properties:
          "@@odata.count":
            type: Number
          value:
            type:
              kind: Table
              properties:
                summary: String
                content: String
                url: String
                assignee: String
                project: String
                status: String
                priority: String
                issue_type: String
                issue_key: String
                labels: String
                created_at: String
                updated_at: String

    - kind: SetVariable
      id: setSearchResults
      variable: System.SearchResults
      value: |-
        =ForAll(Topic.searchResults.value,
        {{
          Content: issue_key & " [" & status & "] " & summary & " (assigned to: " & assignee & ", project: " & project & ", priority: " & priority & ")" & Char(10) & content,
          ContentLocation: url,
          Title: issue_key & " — " & summary
        }})

inputType: {{}}
outputType: {{}}
"#
    )
}

/// Generate the OnKnowledgeRequested YAML topic for a Confluence index.
fn confluence_topic(endpoint: &str, index_name: &str, semantic_config: &str) -> String {
    format!(
        r#"# OnKnowledgeRequested topic for Confluence pages index "{index_name}"
# Generated by quelch — paste into Copilot Studio code editor
#
# This topic fires when the agent needs to search for knowledge.
# It queries Azure AI Search with semantic search over chunked
# Confluence page content.

kind: AdaptiveDialog
beginDialog:
  kind: OnKnowledgeRequested
  id: main
  intent: {{}}
  actions:
    - kind: HttpRequestAction
      id: confluenceSearch
      method: POST
      url: "{endpoint}/indexes('{index_name}')/docs/search.post.search?api-version=2024-07-01"
      headers:
        Content-Type: application/json
        api-key: "{{{{System.Env.AZURE_SEARCH_API_KEY}}}}"
      body:
        kind: Json
        value:
          search: =System.SearchQuery
          queryType: semantic
          semanticConfiguration: "{semantic_config}"
          select: "page_title,content,url,space_key,author,chunk_heading,labels,created_at,updated_at"
          top: 15
          searchMode: any
          captions: extractive
          answers: extractive
      response: Topic.searchResults
      responseSchema:
        kind: Record
        properties:
          value:
            type:
              kind: Table
              properties:
                page_title: String
                content: String
                url: String
                space_key: String
                author: String
                chunk_heading: String
                labels: String
                created_at: String
                updated_at: String

    - kind: SetVariable
      id: setSearchResults
      variable: System.SearchResults
      value: |-
        =ForAll(Topic.searchResults.value,
        {{
          Content: "[" & space_key & "] " & page_title & If(!IsBlank(chunk_heading), " > " & chunk_heading, "") & Char(10) & content,
          ContentLocation: url,
          Title: page_title & If(!IsBlank(chunk_heading), " — " & chunk_heading, "")
        }})

inputType: {{}}
outputType: {{}}
"#
    )
}

/// Generate agent instructions (system prompt) tailored to the configured sources.
fn agent_instructions(config: &Config) -> String {
    let mut out = String::new();

    writeln!(out, "# Agent Instructions").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "You are a knowledge assistant with access to data from the following sources:"
    )
    .unwrap();
    writeln!(out).unwrap();

    let jira_sources: Vec<_> = config
        .sources
        .iter()
        .filter_map(|s| match s {
            SourceConfig::Jira(j) => Some(j),
            _ => None,
        })
        .collect();

    let confluence_sources: Vec<_> = config
        .sources
        .iter()
        .filter_map(|s| match s {
            SourceConfig::Confluence(c) => Some(c),
            _ => None,
        })
        .collect();

    if !jira_sources.is_empty() {
        writeln!(out, "## Jira Issues").unwrap();
        writeln!(out).unwrap();
        for j in &jira_sources {
            let projects = j.projects.join(", ");
            writeln!(out, "- **{}**: Issues from projects: {}", j.name, projects).unwrap();
        }
        writeln!(out).unwrap();
        writeln!(
            out,
            "Each Jira issue has the following fields that you can reference in your answers:"
        )
        .unwrap();
        writeln!(out).unwrap();
        writeln!(out, "| Field | Description | Example |").unwrap();
        writeln!(out, "|-------|-------------|---------|").unwrap();
        writeln!(out, "| issue_key | The Jira issue identifier | PROJ-123 |").unwrap();
        writeln!(
            out,
            "| summary | The issue title/summary | Fix login button on mobile |"
        )
        .unwrap();
        writeln!(
            out,
            "| description | Detailed issue description | Full text of the issue body |"
        )
        .unwrap();
        writeln!(out, "| status | Current status | Open, In Progress, Done |").unwrap();
        writeln!(
            out,
            "| status_category | Status category | To Do, In Progress, Done |"
        )
        .unwrap();
        writeln!(
            out,
            "| priority | Issue priority | Critical, High, Medium, Low |"
        )
        .unwrap();
        writeln!(
            out,
            "| issue_type | Type of issue | Bug, Story, Task, Epic |"
        )
        .unwrap();
        writeln!(
            out,
            "| assignee | Person assigned to the issue | John Doe |"
        )
        .unwrap();
        writeln!(
            out,
            "| reporter | Person who created the issue | Jane Smith |"
        )
        .unwrap();
        writeln!(out, "| project | Project key | PROJ |").unwrap();
        writeln!(
            out,
            "| labels | Tags/labels on the issue | backend, urgent |"
        )
        .unwrap();
        writeln!(
            out,
            "| comments | Discussion comments on the issue | Free text |"
        )
        .unwrap();
        writeln!(out, "| created_at | When the issue was created | Date |").unwrap();
        writeln!(
            out,
            "| updated_at | When the issue was last updated | Date |"
        )
        .unwrap();
        writeln!(out, "| url | Link to the issue in Jira | URL |").unwrap();
        writeln!(out).unwrap();
    }

    if !confluence_sources.is_empty() {
        writeln!(out, "## Confluence Pages").unwrap();
        writeln!(out).unwrap();
        for c in &confluence_sources {
            let spaces = c.spaces.join(", ");
            writeln!(out, "- **{}**: Pages from spaces: {}", c.name, spaces).unwrap();
        }
        writeln!(out).unwrap();
        writeln!(
            out,
            "Each Confluence page is split into chunks by heading. Fields available:"
        )
        .unwrap();
        writeln!(out).unwrap();
        writeln!(out, "| Field | Description |").unwrap();
        writeln!(out, "|-------|-------------|").unwrap();
        writeln!(out, "| page_title | The page title |").unwrap();
        writeln!(out, "| chunk_heading | Section heading within the page |").unwrap();
        writeln!(out, "| body | The text content of this section |").unwrap();
        writeln!(out, "| space_key | Confluence space key |").unwrap();
        writeln!(out, "| author | Page author |").unwrap();
        writeln!(out, "| labels | Page labels/tags |").unwrap();
        writeln!(out, "| url | Link to the page in Confluence |").unwrap();
        writeln!(out).unwrap();
    }

    writeln!(out, "## How to Answer Questions").unwrap();
    writeln!(out).unwrap();

    if !jira_sources.is_empty() {
        writeln!(out, "### Jira Questions").unwrap();
        writeln!(out).unwrap();
        writeln!(out, "When answering questions about Jira issues:").unwrap();
        writeln!(out).unwrap();
        writeln!(out, "- **Always include the issue key** (e.g., PROJ-123) and a link to the issue when referencing specific issues.").unwrap();
        writeln!(out, "- **Include relevant metadata** such as status, assignee, and priority when listing issues.").unwrap();
        writeln!(out, "- When asked about issues assigned to a person, search for their name and present matching results.").unwrap();
        writeln!(out, "- When asked to count or list \"all\" issues, be transparent that you can only search and return the most relevant results from the index — you may not have visibility into every issue. State the number of results you found and note that there may be more.").unwrap();
        writeln!(out, "- When asked about a specific project, mention the project key in your search to improve relevance.").unwrap();
        writeln!(
            out,
            "- Format issue lists as tables when presenting multiple issues."
        )
        .unwrap();
        writeln!(out).unwrap();
    }

    if !confluence_sources.is_empty() {
        writeln!(out, "### Confluence Questions").unwrap();
        writeln!(out).unwrap();
        writeln!(out, "When answering questions about Confluence content:").unwrap();
        writeln!(out).unwrap();
        writeln!(
            out,
            "- **Always link to the source page** so the user can read the full context."
        )
        .unwrap();
        writeln!(
            out,
            "- Reference the page title and section heading when quoting content."
        )
        .unwrap();
        writeln!(out, "- If multiple chunks from the same page are relevant, synthesize them into a coherent answer rather than repeating page references.").unwrap();
        writeln!(out).unwrap();
    }

    writeln!(out, "### General Guidelines").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "- If you cannot find relevant information, say so clearly rather than guessing."
    )
    .unwrap();
    writeln!(out, "- Always cite your sources with links.").unwrap();
    writeln!(
        out,
        "- Be concise but complete. Prefer tables for structured data and prose for explanations."
    )
    .unwrap();

    out
}

/// Generate a usage guide explaining what was generated and how to use it.
fn usage_guide(config: &Config, topics: &[GeneratedTopic]) -> String {
    let mut out = String::new();

    writeln!(out, "# Copilot Studio Agent Setup Guide").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "Generated by `quelch generate-agent` based on your quelch.yaml configuration."
    )
    .unwrap();
    writeln!(out).unwrap();

    writeln!(out, "## What Was Generated").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "| File | Purpose |").unwrap();
    writeln!(out, "|------|---------|").unwrap();
    writeln!(
        out,
        "| `agent-instructions.md` | System prompt / agent instructions — paste into your agent's Instructions field |"
    )
    .unwrap();
    for topic in topics {
        writeln!(
            out,
            "| `{}` | OnKnowledgeRequested topic — custom search logic for this index |",
            topic.filename
        )
        .unwrap();
    }
    writeln!(out, "| `guide.md` | This file — setup instructions |").unwrap();
    writeln!(out).unwrap();

    writeln!(out, "## Setup Steps").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "### 1. Create or Open Your Agent in Copilot Studio").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "Go to [Copilot Studio](https://copilotstudio.microsoft.com) and create a new agent or open an existing one."
    )
    .unwrap();
    writeln!(out).unwrap();

    writeln!(out, "### 2. Set the Agent Instructions").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "Copy the contents of `agent-instructions.md` into your agent's **Instructions** field."
    )
    .unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "This tells the agent what data it has access to and how to format answers."
    )
    .unwrap();
    writeln!(out).unwrap();

    writeln!(out, "### 3. Add the OnKnowledgeRequested Topics").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "For each generated `.mcs.yaml` file:").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "1. In Copilot Studio, go to **Topics**").unwrap();
    writeln!(out, "2. Click **Add** > **Topic** > **From blank**").unwrap();
    writeln!(
        out,
        "3. Click the ellipsis (**...**) in the top-right and select **Open code editor**"
    )
    .unwrap();
    writeln!(
        out,
        "4. Replace the default YAML with the contents of the generated file"
    )
    .unwrap();
    writeln!(out, "5. Save the topic").unwrap();
    writeln!(out).unwrap();

    writeln!(out, "### 4. Configure Authentication").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "The generated topics reference `{{{{System.Env.AZURE_SEARCH_API_KEY}}}}` for the API key."
    )
    .unwrap();
    writeln!(out).unwrap();
    writeln!(out, "You have two options to provide the API key:").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "**Option A: Environment variable** — Set `AZURE_SEARCH_API_KEY` as an environment variable in your Copilot Studio environment."
    )
    .unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "**Option B: Hardcode the key** — Replace `{{{{System.Env.AZURE_SEARCH_API_KEY}}}}` in the YAML with your actual Azure AI Search query key. Less secure but simpler for testing."
    )
    .unwrap();
    writeln!(out).unwrap();
    writeln!(out, "Use a **query key** (read-only), not your admin key.").unwrap();
    writeln!(out).unwrap();

    writeln!(
        out,
        "### 5. Remove Built-in Azure AI Search Knowledge Source"
    )
    .unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "If you previously added your Azure AI Search index as a built-in knowledge source (via the Knowledge page), **remove it**. The OnKnowledgeRequested topics replace the built-in integration with full query control. Having both would cause duplicate or conflicting results."
    )
    .unwrap();
    writeln!(out).unwrap();

    writeln!(out, "### 6. Publish and Test").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "Publish your agent and test with queries like:").unwrap();
    writeln!(out).unwrap();

    let has_jira = config
        .sources
        .iter()
        .any(|s| matches!(s, SourceConfig::Jira(_)));
    let has_confluence = config
        .sources
        .iter()
        .any(|s| matches!(s, SourceConfig::Confluence(_)));

    if has_jira {
        writeln!(out, "- \"Find Jira issues about wifi problems\"").unwrap();
        writeln!(out, "- \"Show me issues assigned to John Doe\"").unwrap();
        writeln!(
            out,
            "- \"What high-priority bugs are open in the PROJ project?\""
        )
        .unwrap();
        writeln!(out, "- \"Summarize recent activity in the ENG project\"").unwrap();
    }
    if has_confluence {
        writeln!(out, "- \"How do I set up the development environment?\"").unwrap();
        writeln!(out, "- \"What does our deployment process look like?\"").unwrap();
        writeln!(out, "- \"Find documentation about authentication\"").unwrap();
    }
    writeln!(out).unwrap();

    writeln!(out, "## How It Works").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "The `OnKnowledgeRequested` trigger fires when the agent's LLM decides it needs to search for information. The topic then:").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "1. Takes `System.SearchQuery` (a context-aware rewrite of the user's question)"
    )
    .unwrap();
    writeln!(
        out,
        "2. Sends it to Azure AI Search using semantic search with your configured index"
    )
    .unwrap();
    writeln!(
        out,
        "3. Returns up to 15 results with structured metadata (issue key, status, assignee, etc.)"
    )
    .unwrap();
    writeln!(
        out,
        "4. The agent's LLM uses these results to generate a grounded answer with citations"
    )
    .unwrap();
    writeln!(out).unwrap();
    writeln!(out, "The key advantage over the built-in Azure AI Search knowledge source is that the `Content` field in the results includes structured metadata (assignee, status, project, priority), which helps the LLM answer questions like \"who is assigned to this?\" or \"what's the status?\" even though it's using semantic search under the hood.").unwrap();
    writeln!(out).unwrap();

    writeln!(out, "## Customization").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "### Adding OData Filters").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "You can add a `filter` field to the search body to scope results. For example:"
    )
    .unwrap();
    writeln!(out).unwrap();
    writeln!(out, "```yaml").unwrap();
    writeln!(out, "body:").unwrap();
    writeln!(out, "  kind: Json").unwrap();
    writeln!(out, "  value:").unwrap();
    writeln!(out, "    search: =System.SearchQuery").unwrap();
    writeln!(
        out,
        "    filter: \"project eq 'PROJ' and status_category ne 'Done'\""
    )
    .unwrap();
    writeln!(out, "    # ... rest of search parameters").unwrap();
    writeln!(out, "```").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "Available filter fields for Jira:").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "| Field | Type | Example Filter |").unwrap();
    writeln!(out, "|-------|------|---------------|").unwrap();
    writeln!(out, "| assignee | string | `assignee eq 'John Doe'` |").unwrap();
    writeln!(out, "| project | string | `project eq 'PROJ'` |").unwrap();
    writeln!(out, "| status | string | `status eq 'In Progress'` |").unwrap();
    writeln!(
        out,
        "| status_category | string | `status_category eq 'Done'` |"
    )
    .unwrap();
    writeln!(out, "| priority | string | `priority eq 'High'` |").unwrap();
    writeln!(out, "| issue_type | string | `issue_type eq 'Bug'` |").unwrap();
    writeln!(
        out,
        "| labels | string collection | `labels/any(l: l eq 'urgent')` |"
    )
    .unwrap();
    writeln!(
        out,
        "| created_at | datetime | `created_at ge 2024-01-01T00:00:00Z` |"
    )
    .unwrap();
    writeln!(
        out,
        "| updated_at | datetime | `updated_at ge 2024-01-01T00:00:00Z` |"
    )
    .unwrap();
    writeln!(out).unwrap();

    writeln!(out, "### Changing Result Count").unwrap();
    writeln!(out).unwrap();
    writeln!(
        out,
        "The `top` parameter controls how many results are returned. The maximum useful value is **15** — Copilot Studio uses at most 15 snippets for answer generation."
    )
    .unwrap();
    writeln!(out).unwrap();

    writeln!(out, "## References").unwrap();
    writeln!(out).unwrap();
    writeln!(out, "- [Copilot Studio: Custom Knowledge Sources](https://learn.microsoft.com/en-us/microsoft-copilot-studio/guidance/custom-knowledge-sources)").unwrap();
    writeln!(out, "- [Azure AI Search POST Search API](https://learn.microsoft.com/en-us/rest/api/searchservice/documents/search-post)").unwrap();
    writeln!(out, "- [OData Filter Syntax](https://learn.microsoft.com/en-us/azure/search/search-query-odata-filter)").unwrap();
    writeln!(out, "- [Copilot Studio YAML Code Editor](https://learn.microsoft.com/en-us/microsoft-copilot-studio/guidance/topics-code-editor)").unwrap();

    out
}

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

    fn test_config() -> Config {
        Config {
            azure: AzureConfig {
                endpoint: "https://my-search.search.windows.net".to_string(),
                api_key: "test-key".to_string(),
            },
            sources: vec![
                SourceConfig::Jira(JiraSourceConfig {
                    name: "my-jira".to_string(),
                    url: "https://company.atlassian.net".to_string(),
                    auth: AuthConfig::Cloud {
                        email: "user@test.com".to_string(),
                        api_token: "token".to_string(),
                    },
                    projects: vec!["PROJ".to_string(), "ENG".to_string()],
                    index: "jira-issues".to_string(),
                }),
                SourceConfig::Confluence(ConfluenceSourceConfig {
                    name: "my-confluence".to_string(),
                    url: "https://company.atlassian.net/wiki".to_string(),
                    auth: AuthConfig::Cloud {
                        email: "user@test.com".to_string(),
                        api_token: "token".to_string(),
                    },
                    spaces: vec!["ENG".to_string(), "DOCS".to_string()],
                    index: "confluence-pages".to_string(),
                }),
            ],
            sync: SyncConfig::default(),
        }
    }

    #[test]
    fn generates_topics_for_each_source() {
        let output = generate(&test_config());
        assert_eq!(output.topics.len(), 2);
        assert_eq!(output.topics[0].filename, "my-jira-search.mcs.yaml");
        assert_eq!(output.topics[1].filename, "my-confluence-search.mcs.yaml");
    }

    #[test]
    fn jira_topic_contains_endpoint_and_index() {
        let output = generate(&test_config());
        let yaml = &output.topics[0].yaml;
        assert!(yaml.contains("my-search.search.windows.net"));
        assert!(yaml.contains("jira-issues"));
        assert!(yaml.contains("jira-issues-semantic-config"));
    }

    #[test]
    fn confluence_topic_contains_endpoint_and_index() {
        let output = generate(&test_config());
        let yaml = &output.topics[1].yaml;
        assert!(yaml.contains("my-search.search.windows.net"));
        assert!(yaml.contains("confluence-pages"));
        assert!(yaml.contains("confluence-pages-semantic-config"));
    }

    #[test]
    fn instructions_mention_configured_projects() {
        let output = generate(&test_config());
        assert!(output.instructions.contains("PROJ"));
        assert!(output.instructions.contains("ENG"));
    }

    #[test]
    fn instructions_mention_configured_spaces() {
        let output = generate(&test_config());
        assert!(output.instructions.contains("ENG"));
        assert!(output.instructions.contains("DOCS"));
    }

    #[test]
    fn guide_lists_generated_files() {
        let output = generate(&test_config());
        assert!(output.guide.contains("my-jira-search.mcs.yaml"));
        assert!(output.guide.contains("my-confluence-search.mcs.yaml"));
        assert!(output.guide.contains("agent-instructions.md"));
    }

    #[test]
    fn jira_only_config() {
        let config = Config {
            azure: AzureConfig {
                endpoint: "https://test.search.windows.net".to_string(),
                api_key: "key".to_string(),
            },
            sources: vec![SourceConfig::Jira(JiraSourceConfig {
                name: "jira".to_string(),
                url: "https://jira.example.com".to_string(),
                auth: AuthConfig::DataCenter {
                    pat: "pat".to_string(),
                },
                projects: vec!["HR".to_string()],
                index: "jira-idx".to_string(),
            })],
            sync: SyncConfig::default(),
        };
        let output = generate(&config);
        assert_eq!(output.topics.len(), 1);
        assert!(output.instructions.contains("Jira Issues"));
        assert!(!output.instructions.contains("Confluence Pages"));
    }
}