Link a goal to an intent.
(
&self,
intent_id: &str,
goal_name: &str,
)
| 690 | |
| 691 | /// Link a goal to an intent. |
| 692 | pub fn vault_intent_link( |
| 693 | &self, |
| 694 | intent_id: &str, |
| 695 | goal_name: &str, |
| 696 | ) -> Result<(), RepositoryError> { |
| 697 | let full_id = self.normalize_intent_id(intent_id)?; |
| 698 | let intent_file = |
| 699 | self.find_intent_path(&full_id)? |
| 700 | .ok_or_else(|| RepositoryError::InvalidOperation { |
| 701 | message: format!("Intent '{}' not found", full_id), |
| 702 | })?; |
| 703 | |
| 704 | // Read from disk first so we pick up any user/agent edits to the |
| 705 | // markdown body (same pattern as vault_intent_update). |
| 706 | let disk_path = self.vault_dir().join(&intent_file); |
| 707 | let (content_bytes, frontmatter_json) = if disk_path.exists() { |
| 708 | let file_content = std::fs::read_to_string(&disk_path)?; |
| 709 | let (fm_json, body) = crate::repository::vault::parse_vault_frontmatter(&file_content); |
| 710 | (body.into_bytes(), fm_json) |
| 711 | } else { |
| 712 | let entry = self.vault_retrieve(&intent_file)?.ok_or_else(|| { |
| 713 | RepositoryError::InvalidOperation { |
| 714 | message: format!("Intent '{}' not found", full_id), |
| 715 | } |
| 716 | })?; |
| 717 | (entry.content_bytes.clone(), entry.frontmatter_json.clone()) |
| 718 | }; |
| 719 | |
| 720 | // Verify goal exists |
| 721 | let goal_file = format!("goals/{}/_goal.md", goal_name); |
| 722 | if self.vault_retrieve(&goal_file)?.is_none() { |
| 723 | return Err(RepositoryError::InvalidOperation { |
| 724 | message: format!("Goal '{}' not found", goal_name), |
| 725 | }); |
| 726 | } |
| 727 | |
| 728 | // Update frontmatter to add goal to the list |
| 729 | let mut fm: serde_json::Map<String, serde_json::Value> = |
| 730 | serde_json::from_str(&frontmatter_json).unwrap_or_default(); |
| 731 | |
| 732 | let goals = fm |
| 733 | .entry("goals".to_string()) |
| 734 | .or_insert_with(|| serde_json::Value::Array(Vec::new())); |
| 735 | let goals = goals |
| 736 | .as_array_mut() |
| 737 | .ok_or_else(|| RepositoryError::InvalidOperation { |
| 738 | message: format!("Intent '{}' has a non-array goals field", full_id), |
| 739 | })?; |
| 740 | let goal_val = serde_json::Value::String(goal_name.to_string()); |
| 741 | if !goals.contains(&goal_val) { |
| 742 | goals.push(goal_val); |
| 743 | } |
| 744 | let goal_count = goals.len() as u32; |
| 745 | let new_fm = serde_json::to_string(&fm).unwrap_or_else(|_| "{}".to_string()); |
| 746 | |
| 747 | self.vault_store(&intent_file, VaultEntryType::Intent, content_bytes, new_fm)?; |
| 748 | |
| 749 | // Update manifest goal count |