File size: 21,180 Bytes
f440f03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::{
    fs::{self, File},
    io::Cursor,
    path::{Path, PathBuf},
};

use axum::{
    extract::Query,
    http::{header, HeaderMap, HeaderValue, StatusCode},
    response::IntoResponse,
    Json,
};
use serde::{Deserialize, Serialize};
use zip::{write::SimpleFileOptions, CompressionMethod, ZipArchive, ZipWriter};

use crate::inference::python_bridge::PythonBridge;
use crate::inference::response_validator::CodeGenerationPayload;

const GENERATED_PROJECTS_ROOT: &str = "/tmp/maris-ai/generated-projects";

#[derive(Deserialize)]
pub struct CodeRequest {
    pub prompt: String,
    pub language: Option<String>,
    pub context: Option<String>,
    pub repo_path: Option<String>,
    pub fallback_model: Option<String>,
}

#[derive(Deserialize)]
pub struct FixCodeRequest {
    pub code: String,
    pub error_message: Option<String>,
    pub language: Option<String>,
}

#[derive(Deserialize)]
pub struct DownloadProjectZipRequest {
    pub artifact_dir: String,
}

#[derive(Deserialize)]
pub struct CodeWorkspaceQuery {
    pub artifact_dir: String,
    pub repo_path: String,
}

#[derive(Deserialize)]
pub struct CodeWorkspaceMutationRequest {
    pub artifact_dir: String,
    pub repo_path: String,
}

#[derive(Serialize)]
pub struct CodeProjectFileResponse {
    pub path: String,
    pub absolute_path: Option<String>,
}

#[derive(Serialize)]
pub struct CodeResponse {
    pub code: String,
    pub explanation: String,
    pub language: String,
    pub detected_stack: String,
    pub files: Vec<CodeProjectFileResponse>,
    pub workspace_artifact_dir: Option<String>,
    pub bundle_path: Option<String>,
    pub bundle_download_url: Option<String>,
    pub entrypoint: Option<String>,
    pub repo_path: Option<String>,
}

#[derive(Serialize)]
pub struct CodeDiffFileResponse {
    pub path: String,
    pub status: String,
    pub patch: Option<String>,
}

#[derive(Serialize)]
pub struct CodePreviewResponse {
    pub artifact_dir: String,
    pub repo_path: String,
    pub has_changes: bool,
    pub added_files: usize,
    pub modified_files: usize,
    pub unchanged_files: usize,
    pub patch: String,
    pub files: Vec<CodeDiffFileResponse>,
}

#[derive(Serialize)]
pub struct CodeWorkspaceMutationResponse {
    pub artifact_dir: String,
    pub repo_path: String,
    pub applied_files: Vec<String>,
    pub imported_from_bundle: bool,
}

fn bundle_download_url(workspace_artifact_dir: Option<&str>) -> Option<String> {
    workspace_artifact_dir.map(|artifact_dir| {
        format!(
            "/api/code/download?artifact_dir={}",
            urlencoding::encode(artifact_dir)
        )
    })
}

fn generated_projects_root() -> PathBuf {
    PathBuf::from(GENERATED_PROJECTS_ROOT)
}

fn json_error(
    status: StatusCode,
    message: impl Into<String>,
) -> (StatusCode, Json<serde_json::Value>) {
    (status, Json(serde_json::json!({ "error": message.into() })))
}

fn validate_artifact_dir(
    artifact_dir: &str,
) -> Result<PathBuf, (StatusCode, Json<serde_json::Value>)> {
    let trimmed = artifact_dir.trim();
    if trimmed.is_empty() {
        return Err(json_error(
            StatusCode::BAD_REQUEST,
            "Trūkst artifact_dir parametra.",
        ));
    }

    let requested = PathBuf::from(trimmed);
    let canonical_requested = requested.canonicalize().map_err(|_| {
        json_error(
            StatusCode::NOT_FOUND,
            "Norādītā artefakta direktorija nav atrasta.",
        )
    })?;
    let root = generated_projects_root();
    let canonical_root = root.canonicalize().unwrap_or(root);
    if !canonical_requested.starts_with(&canonical_root) || !canonical_requested.is_dir() {
        return Err(json_error(
            StatusCode::BAD_REQUEST,
            "Nederīga artefakta direktorija.",
        ));
    }
    Ok(canonical_requested)
}

fn find_repo_root(path: &Path) -> Option<PathBuf> {
    let mut current = Some(path);
    while let Some(candidate) = current {
        if candidate.join(".git").exists() {
            return Some(candidate.to_path_buf());
        }
        current = candidate.parent();
    }
    None
}

fn validate_repo_path(repo_path: &str) -> Result<PathBuf, (StatusCode, Json<serde_json::Value>)> {
    let trimmed = repo_path.trim();
    if trimmed.is_empty() {
        return Err(json_error(
            StatusCode::BAD_REQUEST,
            "Trūkst repo_path parametra.",
        ));
    }

    let requested = PathBuf::from(trimmed);
    let canonical_requested = requested.canonicalize().map_err(|_| {
        json_error(
            StatusCode::NOT_FOUND,
            "Norādītā repozitorija direktorija nav atrasta.",
        )
    })?;
    let candidate_dir = if canonical_requested.is_file() {
        canonical_requested
            .parent()
            .map(Path::to_path_buf)
            .ok_or_else(|| {
                json_error(
                    StatusCode::BAD_REQUEST,
                    "Norādītais repo_path nav derīga direktorija.",
                )
            })?
    } else {
        canonical_requested
    };

    let repo_root = find_repo_root(&candidate_dir).ok_or_else(|| {
        json_error(
            StatusCode::BAD_REQUEST,
            "repo_path nav Git repozitorija darba telpā.",
        )
    })?;
    if !candidate_dir.starts_with(&repo_root) {
        return Err(json_error(
            StatusCode::BAD_REQUEST,
            "repo_path atrodas ārpus Git repozitorija saknes.",
        ));
    }

    Ok(candidate_dir)
}

fn append_directory_to_zip(
    writer: &mut ZipWriter<Cursor<Vec<u8>>>,
    root: &Path,
    current: &Path,
) -> anyhow::Result<()> {
    let options = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
    for entry in fs::read_dir(current)? {
        let entry = entry?;
        let path = entry.path();
        let relative = path
            .strip_prefix(root)?
            .to_string_lossy()
            .replace('\\', "/");
        if path.is_dir() {
            if !relative.is_empty() {
                writer.add_directory(format!("{relative}/"), options)?;
            }
            append_directory_to_zip(writer, root, &path)?;
        } else if path.is_file() {
            writer.start_file(relative, options)?;
            let mut file = File::open(&path)?;
            std::io::copy(&mut file, writer)?;
        }
    }
    Ok(())
}

fn zip_directory_to_bytes(artifact_dir: &Path) -> anyhow::Result<Vec<u8>> {
    let cursor = Cursor::new(Vec::new());
    let mut writer = ZipWriter::new(cursor);
    append_directory_to_zip(&mut writer, artifact_dir, artifact_dir)?;
    let cursor = writer.finish()?;
    Ok(cursor.into_inner())
}

fn collect_files(root: &Path, current: &Path, files: &mut Vec<PathBuf>) -> anyhow::Result<()> {
    for entry in fs::read_dir(current)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            collect_files(root, &path, files)?;
        } else if path.is_file() {
            files.push(path.strip_prefix(root)?.to_path_buf());
        }
    }
    Ok(())
}

fn read_text_lossy(path: &Path) -> anyhow::Result<String> {
    Ok(String::from_utf8_lossy(&fs::read(path)?).into_owned())
}

fn build_patch(relative_path: &str, current_content: &str, generated_content: &str) -> String {
    let current_lines: Vec<&str> = current_content.lines().collect();
    let generated_lines: Vec<&str> = generated_content.lines().collect();
    let current_start = if current_lines.is_empty() { 0 } else { 1 };
    let generated_start = if generated_lines.is_empty() { 0 } else { 1 };
    let mut patch = format!(
        "--- a/{relative_path}\n+++ b/{relative_path}\n@@ -{current_start},{} +{generated_start},{} @@\n",
        current_lines.len(),
        generated_lines.len()
    );
    for line in current_lines {
        patch.push('-');
        patch.push_str(line);
        patch.push('\n');
    }
    for line in generated_lines {
        patch.push('+');
        patch.push_str(line);
        patch.push('\n');
    }
    patch
}

fn build_preview_response(
    artifact_dir: &Path,
    repo_path: &Path,
) -> Result<CodePreviewResponse, (StatusCode, Json<serde_json::Value>)> {
    let mut relative_files = Vec::new();
    collect_files(artifact_dir, artifact_dir, &mut relative_files).map_err(|err| {
        json_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Neizdevās nolasīt workspace failus: {err}"),
        )
    })?;
    relative_files.sort();

    let mut files = Vec::new();
    let mut combined_patch = String::new();
    let mut added_files = 0usize;
    let mut modified_files = 0usize;
    let mut unchanged_files = 0usize;

    for relative in relative_files {
        let relative_display = relative.to_string_lossy().replace('\\', "/");
        let generated_path = artifact_dir.join(&relative);
        let generated_content = read_text_lossy(&generated_path).map_err(|err| {
            json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Neizdevās nolasīt ģenerēto failu `{relative_display}`: {err}"),
            )
        })?;
        let repo_file = repo_path.join(&relative);
        let current_content = if repo_file.exists() {
            Some(read_text_lossy(&repo_file).map_err(|err| {
                json_error(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Neizdevās nolasīt repo failu `{relative_display}`: {err}"),
                )
            })?)
        } else {
            None
        };

        let (status, patch) = match current_content {
            None => {
                added_files += 1;
                (
                    "added".to_string(),
                    Some(build_patch(&relative_display, "", &generated_content)),
                )
            }
            Some(current) if current == generated_content => {
                unchanged_files += 1;
                ("unchanged".to_string(), None)
            }
            Some(current) => {
                modified_files += 1;
                (
                    "modified".to_string(),
                    Some(build_patch(&relative_display, &current, &generated_content)),
                )
            }
        };

        if let Some(ref patch_body) = patch {
            if !combined_patch.is_empty() {
                combined_patch.push_str("\n\n");
            }
            combined_patch.push_str(patch_body);
        }

        files.push(CodeDiffFileResponse {
            path: relative_display,
            status,
            patch,
        });
    }

    Ok(CodePreviewResponse {
        artifact_dir: artifact_dir.to_string_lossy().into_owned(),
        repo_path: repo_path.to_string_lossy().into_owned(),
        has_changes: added_files > 0 || modified_files > 0,
        added_files,
        modified_files,
        unchanged_files,
        patch: combined_patch,
        files,
    })
}

fn copy_artifact_files_to_repo(
    artifact_dir: &Path,
    repo_path: &Path,
) -> Result<Vec<String>, (StatusCode, Json<serde_json::Value>)> {
    let mut relative_files = Vec::new();
    collect_files(artifact_dir, artifact_dir, &mut relative_files).map_err(|err| {
        json_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Neizdevās nolasīt workspace failus: {err}"),
        )
    })?;
    relative_files.sort();

    let mut applied_files = Vec::new();
    for relative in relative_files {
        let relative_display = relative.to_string_lossy().replace('\\', "/");
        let source = artifact_dir.join(&relative);
        let target = repo_path.join(&relative);
        if let Some(parent) = target.parent() {
            fs::create_dir_all(parent).map_err(|err| {
                json_error(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Neizdevās sagatavot direktoriju `{relative_display}`: {err}"),
                )
            })?;
        }
        fs::copy(&source, &target).map_err(|err| {
            json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Neizdevās ierakstīt failu `{relative_display}` repozitorijā: {err}"),
            )
        })?;
        applied_files.push(relative_display);
    }
    Ok(applied_files)
}

fn import_bundle_into_repo(
    artifact_dir: &Path,
    repo_path: &Path,
) -> Result<Vec<String>, (StatusCode, Json<serde_json::Value>)> {
    let bundle_path = artifact_dir.with_extension("zip");
    let bundle_bytes = if bundle_path.exists() {
        fs::read(&bundle_path).map_err(|err| {
            json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Neizdevās nolasīt zip bundle: {err}"),
            )
        })?
    } else {
        zip_directory_to_bytes(artifact_dir).map_err(|err| {
            json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Neizdevās izveidot zip bundle: {err}"),
            )
        })?
    };

    let mut archive = ZipArchive::new(Cursor::new(bundle_bytes)).map_err(|err| {
        json_error(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Neizdevās atvērt zip bundle: {err}"),
        )
    })?;
    let mut applied_files = Vec::new();
    for index in 0..archive.len() {
        let mut entry = archive.by_index(index).map_err(|err| {
            json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Neizdevās nolasīt zip ierakstu: {err}"),
            )
        })?;
        let Some(relative) = entry.enclosed_name() else {
            return Err(json_error(
                StatusCode::BAD_REQUEST,
                "Zip bundle satur nederīgu ceļu.",
            ));
        };
        let relative_display = relative.to_string_lossy().replace('\\', "/");
        let target = repo_path.join(relative);
        if entry.is_dir() {
            fs::create_dir_all(&target).map_err(|err| {
                json_error(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Neizdevās izveidot direktoriju `{relative_display}`: {err}"),
                )
            })?;
            continue;
        }
        if let Some(parent) = target.parent() {
            fs::create_dir_all(parent).map_err(|err| {
                json_error(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Neizdevās sagatavot direktoriju `{relative_display}`: {err}"),
                )
            })?;
        }
        let mut file = File::create(&target).map_err(|err| {
            json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Neizdevās sagatavot failu `{relative_display}`: {err}"),
            )
        })?;
        std::io::copy(&mut entry, &mut file).map_err(|err| {
            json_error(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Neizdevās importēt failu `{relative_display}`: {err}"),
            )
        })?;
        applied_files.push(relative_display);
    }
    applied_files.sort();
    Ok(applied_files)
}

pub async fn generate_code(
    Json(req): Json<CodeRequest>,
) -> Result<Json<CodeResponse>, (StatusCode, Json<serde_json::Value>)> {
    let bridge = PythonBridge::new();
    let payload = serde_json::json!({
        "prompt": req.prompt,
        "language": req.language.clone().unwrap_or_else(|| "Python".to_string()),
        "context": req.context.unwrap_or_default(),
        "repo_path": req.repo_path,
        "fallback_model": req.fallback_model,
    });

    match bridge
        .call::<CodeGenerationPayload>("code/generate", &payload)
        .await
    {
        Ok(result) => Ok(Json(CodeResponse {
            code: result.code,
            explanation: result.explanation,
            language: result.language,
            detected_stack: result.detected_stack,
            files: result
                .files
                .into_iter()
                .map(|file| CodeProjectFileResponse {
                    path: file.path,
                    absolute_path: file.absolute_path,
                })
                .collect(),
            workspace_artifact_dir: result.workspace_artifact_dir.clone(),
            bundle_path: result.bundle_path,
            bundle_download_url: bundle_download_url(result.workspace_artifact_dir.as_deref()),
            entrypoint: result.entrypoint,
            repo_path: result.repo_path,
        })),
        Err(e) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({ "error": e.to_string() })),
        )),
    }
}

pub async fn fix_code(
    Json(req): Json<FixCodeRequest>,
) -> Result<Json<CodeResponse>, (StatusCode, Json<serde_json::Value>)> {
    let bridge = PythonBridge::new();
    let payload = serde_json::json!({
        "code": req.code,
        "error_message": req.error_message.unwrap_or_default(),
        "language": req.language.clone().unwrap_or_else(|| "Python".to_string()),
    });

    match bridge
        .call::<CodeGenerationPayload>("code/fix", &payload)
        .await
    {
        Ok(result) => Ok(Json(CodeResponse {
            code: result.code,
            explanation: result.explanation,
            language: result.language,
            detected_stack: result.detected_stack,
            files: result
                .files
                .into_iter()
                .map(|file| CodeProjectFileResponse {
                    path: file.path,
                    absolute_path: file.absolute_path,
                })
                .collect(),
            workspace_artifact_dir: result.workspace_artifact_dir.clone(),
            bundle_path: result.bundle_path,
            bundle_download_url: bundle_download_url(result.workspace_artifact_dir.as_deref()),
            entrypoint: result.entrypoint,
            repo_path: result.repo_path,
        })),
        Err(e) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({ "error": e.to_string() })),
        )),
    }
}

pub async fn download_project_zip(
    Query(req): Query<DownloadProjectZipRequest>,
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
    let artifact_dir = validate_artifact_dir(&req.artifact_dir)?;
    let bundle_path = artifact_dir.with_extension("zip");
    let bytes = if bundle_path.exists() {
        fs::read(&bundle_path).map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(serde_json::json!({ "error": format!("Neizdevās nolasīt bundle: {e}") })),
            )
        })?
    } else {
        zip_directory_to_bytes(&artifact_dir).map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(serde_json::json!({ "error": format!("Neizdevās izveidot zip bundle: {e}") })),
            )
        })?
    };

    let file_stem = artifact_dir
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("project-bundle");
    let mut headers = HeaderMap::new();
    headers.insert(
        header::CONTENT_TYPE,
        HeaderValue::from_static("application/zip"),
    );
    headers.insert(
        header::CONTENT_DISPOSITION,
        HeaderValue::from_str(&format!("attachment; filename=\"{file_stem}.zip\""))
            .unwrap_or_else(|_| HeaderValue::from_static("attachment")),
    );
    headers.insert(
        header::CACHE_CONTROL,
        HeaderValue::from_static("no-store, max-age=0"),
    );

    Ok((headers, bytes))
}

pub async fn preview_code_changes(
    Query(req): Query<CodeWorkspaceQuery>,
) -> Result<Json<CodePreviewResponse>, (StatusCode, Json<serde_json::Value>)> {
    let artifact_dir = validate_artifact_dir(&req.artifact_dir)?;
    let repo_path = validate_repo_path(&req.repo_path)?;
    Ok(Json(build_preview_response(&artifact_dir, &repo_path)?))
}

pub async fn apply_workspace_to_repo(
    Json(req): Json<CodeWorkspaceMutationRequest>,
) -> Result<Json<CodeWorkspaceMutationResponse>, (StatusCode, Json<serde_json::Value>)> {
    let artifact_dir = validate_artifact_dir(&req.artifact_dir)?;
    let repo_path = validate_repo_path(&req.repo_path)?;
    let applied_files = copy_artifact_files_to_repo(&artifact_dir, &repo_path)?;
    Ok(Json(CodeWorkspaceMutationResponse {
        artifact_dir: artifact_dir.to_string_lossy().into_owned(),
        repo_path: repo_path.to_string_lossy().into_owned(),
        applied_files,
        imported_from_bundle: false,
    }))
}

pub async fn import_bundle_to_repo(
    Json(req): Json<CodeWorkspaceMutationRequest>,
) -> Result<Json<CodeWorkspaceMutationResponse>, (StatusCode, Json<serde_json::Value>)> {
    let artifact_dir = validate_artifact_dir(&req.artifact_dir)?;
    let repo_path = validate_repo_path(&req.repo_path)?;
    let applied_files = import_bundle_into_repo(&artifact_dir, &repo_path)?;
    Ok(Json(CodeWorkspaceMutationResponse {
        artifact_dir: artifact_dir.to_string_lossy().into_owned(),
        repo_path: repo_path.to_string_lossy().into_owned(),
        applied_files,
        imported_from_bundle: true,
    }))
}