|
1 | | -use anyhow::Result; |
2 | | - |
3 | | -use crate::adapters; |
4 | | -use crate::config; |
5 | | -use crate::core; |
6 | | -use crate::core::offline::optimize_prompt_for_local; |
7 | | - |
8 | | -use super::guidance::build_review_guidance; |
9 | | -use super::services::PipelineServices; |
10 | | -use super::session::ReviewSession; |
11 | | - |
12 | | -pub(super) fn specialized_passes(config: &config::Config) -> Vec<core::SpecializedPassKind> { |
13 | | - if !config.multi_pass_specialized { |
14 | | - return Vec::new(); |
15 | | - } |
16 | | - |
17 | | - let mut passes = vec![ |
18 | | - core::SpecializedPassKind::Security, |
19 | | - core::SpecializedPassKind::Correctness, |
20 | | - ]; |
21 | | - if config.strictness >= 2 { |
22 | | - passes.push(core::SpecializedPassKind::Style); |
23 | | - } |
24 | | - passes |
25 | | -} |
26 | | - |
27 | | -pub(super) fn build_review_request( |
28 | | - services: &PipelineServices, |
29 | | - session: &ReviewSession, |
30 | | - diff: &core::UnifiedDiff, |
31 | | - context_chunks: &[core::LLMContextChunk], |
32 | | - path_config: Option<&config::PathConfig>, |
33 | | - pass_kind: Option<core::SpecializedPassKind>, |
34 | | -) -> Result<adapters::llm::LLMRequest> { |
35 | | - let local_prompt_builder = core::PromptBuilder::new(build_prompt_config( |
36 | | - services, |
37 | | - session, |
38 | | - path_config, |
39 | | - pass_kind, |
40 | | - )); |
41 | | - let (system_prompt, user_prompt) = local_prompt_builder.build_prompt(diff, context_chunks)?; |
42 | | - |
43 | | - let (system_prompt, user_prompt) = if services.is_local { |
44 | | - let context_window = services.config.context_window.unwrap_or(8192); |
45 | | - optimize_prompt_for_local(&system_prompt, &user_prompt, context_window) |
46 | | - } else { |
47 | | - (system_prompt, user_prompt) |
48 | | - }; |
49 | | - |
50 | | - Ok(adapters::llm::LLMRequest { |
51 | | - system_prompt, |
52 | | - user_prompt, |
53 | | - temperature: None, |
54 | | - max_tokens: None, |
55 | | - response_schema: Some(review_comments_response_schema()), |
56 | | - }) |
57 | | -} |
58 | | - |
59 | | -fn build_prompt_config( |
60 | | - services: &PipelineServices, |
61 | | - session: &ReviewSession, |
62 | | - path_config: Option<&config::PathConfig>, |
63 | | - pass_kind: Option<core::SpecializedPassKind>, |
64 | | -) -> core::prompt::PromptConfig { |
65 | | - let mut local_prompt_config = services.base_prompt_config.clone(); |
66 | | - |
67 | | - if let Some(pass_kind) = pass_kind { |
68 | | - local_prompt_config.system_prompt = pass_kind.system_prompt(); |
69 | | - |
70 | | - if !session.enhanced_guidance.is_empty() { |
71 | | - local_prompt_config.system_prompt.push_str("\n\n"); |
72 | | - local_prompt_config |
73 | | - .system_prompt |
74 | | - .push_str(&session.enhanced_guidance); |
75 | | - } |
76 | | - if let Some(instructions) = session.auto_instructions.as_ref() { |
77 | | - local_prompt_config |
78 | | - .system_prompt |
79 | | - .push_str("\n\n# Project-specific instructions (auto-detected):\n"); |
80 | | - local_prompt_config.system_prompt.push_str(instructions); |
81 | | - } |
82 | | - |
83 | | - return local_prompt_config; |
84 | | - } |
85 | | - |
86 | | - if let Some(custom_prompt) = &services.config.system_prompt { |
87 | | - local_prompt_config.system_prompt = custom_prompt.clone(); |
88 | | - } |
89 | | - if let Some(path_config) = path_config { |
90 | | - if let Some(ref prompt) = path_config.system_prompt { |
91 | | - local_prompt_config.system_prompt = prompt.clone(); |
92 | | - } |
93 | | - } |
94 | | - if let Some(guidance) = build_review_guidance(&services.config, path_config) { |
95 | | - local_prompt_config.system_prompt.push_str("\n\n"); |
96 | | - local_prompt_config.system_prompt.push_str(&guidance); |
97 | | - } |
98 | | - if !session.enhanced_guidance.is_empty() { |
99 | | - local_prompt_config.system_prompt.push_str("\n\n"); |
100 | | - local_prompt_config |
101 | | - .system_prompt |
102 | | - .push_str(&session.enhanced_guidance); |
103 | | - } |
104 | | - if !services.feedback_context.is_empty() { |
105 | | - local_prompt_config.system_prompt.push_str("\n\n"); |
106 | | - local_prompt_config |
107 | | - .system_prompt |
108 | | - .push_str(&services.feedback_context); |
109 | | - } |
110 | | - if let Some(instructions) = session.auto_instructions.as_ref() { |
111 | | - local_prompt_config |
112 | | - .system_prompt |
113 | | - .push_str("\n\n# Project-specific instructions (auto-detected):\n"); |
114 | | - local_prompt_config.system_prompt.push_str(instructions); |
115 | | - } |
116 | | - |
117 | | - local_prompt_config |
118 | | -} |
119 | | - |
120 | | -pub(super) fn review_comments_response_schema() -> adapters::llm::StructuredOutputSchema { |
121 | | - adapters::llm::StructuredOutputSchema::json_schema( |
122 | | - "review_findings", |
123 | | - serde_json::json!({ |
124 | | - "type": "array", |
125 | | - "items": { |
126 | | - "type": "object", |
127 | | - "additionalProperties": false, |
128 | | - "required": ["line", "content", "severity", "category", "confidence", "fix_effort", "tags"], |
129 | | - "properties": { |
130 | | - "line": {"type": "integer", "minimum": 1}, |
131 | | - "content": {"type": "string"}, |
132 | | - "severity": {"type": "string", "enum": ["error", "warning", "info", "suggestion"]}, |
133 | | - "category": {"type": "string", "enum": ["bug", "security", "performance", "style", "best_practice"]}, |
134 | | - "confidence": {"type": ["number", "string"]}, |
135 | | - "fix_effort": {"type": "string", "enum": ["low", "medium", "high"]}, |
136 | | - "rule_id": {"type": ["string", "null"]}, |
137 | | - "suggestion": {"type": ["string", "null"]}, |
138 | | - "code_suggestion": {"type": ["string", "null"]}, |
139 | | - "tags": { |
140 | | - "type": "array", |
141 | | - "items": {"type": "string"} |
142 | | - } |
143 | | - } |
144 | | - } |
145 | | - }), |
146 | | - ) |
147 | | -} |
| 1 | +#[path = "request/passes.rs"] |
| 2 | +mod passes; |
| 3 | +#[path = "request/prompt.rs"] |
| 4 | +mod prompt; |
| 5 | +#[path = "request/schema.rs"] |
| 6 | +mod schema; |
| 7 | + |
| 8 | +pub(super) use passes::specialized_passes; |
| 9 | +pub(super) use prompt::build_review_request; |
0 commit comments